@@ -95,6 +95,15 @@ class MIR2VecVocabTestFixture : public ::testing::Test {
9595 }
9696};
9797
98+ // Function to find an opcode by name
99+ static int findOpcodeByName (const TargetInstrInfo *TII, StringRef Name) {
100+ for (unsigned Opcode = 1 ; Opcode < TII->getNumOpcodes (); ++Opcode) {
101+ if (TII->getName (Opcode) == Name)
102+ return Opcode;
103+ }
104+ return -1 ; // Not found
105+ }
106+
98107TEST_F (MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
99108 // Test that same base opcodes get same canonical indices
100109 std::string BaseName1 = MIRVocabulary::extractBaseOpcodeName (" ADD16ri" );
@@ -106,10 +115,10 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
106115
107116 // Create a MIRVocabulary instance to test the mapping
108117 // Use a minimal MIRVocabulary to trigger canonical mapping construction
109- VocabMap VM ;
118+ VocabMap VMap ;
110119 Embedding Val = Embedding (64 , 1 .0f );
111- VM [" ADD" ] = Val;
112- MIRVocabulary TestVocab (std::move (VM ), TII);
120+ VMap [" ADD" ] = Val;
121+ MIRVocabulary TestVocab (std::move (VMap ), TII);
113122
114123 unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName1);
115124 unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName2);
@@ -140,9 +149,19 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
140149 6880u ); // X86 has >6880 unique base opcodes
141150
142151 // Check that the embeddings for opcodes not in the vocab are zero vectors
143- EXPECT_TRUE (TestVocab[AddIndex].approximatelyEquals (Val));
144- EXPECT_TRUE (TestVocab[SubIndex].approximatelyEquals (Embedding (64 , 0 .0f )));
145- EXPECT_TRUE (TestVocab[MovIndex].approximatelyEquals (Embedding (64 , 0 .0f )));
152+ int Add32rrOpcode = findOpcodeByName (TII, " ADD32rr" );
153+ ASSERT_NE (Add32rrOpcode, -1 ) << " ADD32rr opcode not found" ;
154+ EXPECT_TRUE (TestVocab[Add32rrOpcode].approximatelyEquals (Val));
155+
156+ int Sub32rrOpcode = findOpcodeByName (TII, " SUB32rr" );
157+ ASSERT_NE (Sub32rrOpcode, -1 ) << " SUB32rr opcode not found" ;
158+ EXPECT_TRUE (
159+ TestVocab[Sub32rrOpcode].approximatelyEquals (Embedding (64 , 0 .0f )));
160+
161+ int Mov32rrOpcode = findOpcodeByName (TII, " MOV32rr" );
162+ ASSERT_NE (Mov32rrOpcode, -1 ) << " MOV32rr opcode not found" ;
163+ EXPECT_TRUE (
164+ TestVocab[Mov32rrOpcode].approximatelyEquals (Embedding (64 , 0 .0f )));
146165}
147166
148167// Test deterministic mapping
@@ -152,9 +171,9 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
152171
153172 // Create a MIRVocabulary instance to test deterministic mapping
154173 // Use a minimal MIRVocabulary to trigger canonical mapping construction
155- VocabMap VM ;
156- VM [" ADD" ] = Embedding (64 , 1 .0f );
157- MIRVocabulary TestVocab (std::move (VM ), TII);
174+ VocabMap VMap ;
175+ VMap [" ADD" ] = Embedding (64 , 1 .0f );
176+ MIRVocabulary TestVocab (std::move (VMap ), TII);
158177
159178 unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName);
160179 unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName);
@@ -172,16 +191,11 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
172191
173192// Test MIRVocabulary construction
174193TEST_F (MIR2VecVocabTestFixture, VocabularyConstruction) {
175- // Test empty MIRVocabulary
176- MIRVocabulary EmptyVocab;
177- EXPECT_FALSE (EmptyVocab.isValid ());
178-
179- // Test MIRVocabulary with embeddings via VocabMap
180- VocabMap VM;
181- VM[" ADD" ] = Embedding (128 , 1 .0f ); // Dimension 128, all values 1.0
182- VM[" SUB" ] = Embedding (128 , 2 .0f ); // Dimension 128, all values 2.0
194+ VocabMap VMap;
195+ VMap[" ADD" ] = Embedding (128 , 1 .0f ); // Dimension 128, all values 1.0
196+ VMap[" SUB" ] = Embedding (128 , 2 .0f ); // Dimension 128, all values 2.0
183197
184- MIRVocabulary Vocab (std::move (VM ), TII);
198+ MIRVocabulary Vocab (std::move (VMap ), TII);
185199 EXPECT_TRUE (Vocab.isValid ());
186200 EXPECT_EQ (Vocab.getDimension (), 128u );
187201
0 commit comments