@@ -87,6 +87,15 @@ class MIR2VecVocabTestFixture : public ::testing::Test {
8787 }
8888};
8989
90+ // Function to find an opcode by name
91+ static int findOpcodeByName (const TargetInstrInfo *TII, StringRef Name) {
92+ for (unsigned Opcode = 1 ; Opcode < TII->getNumOpcodes (); ++Opcode) {
93+ if (TII->getName (Opcode) == Name)
94+ return Opcode;
95+ }
96+ return -1 ; // Not found
97+ }
98+
9099TEST_F (MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
91100 // Test that same base opcodes get same canonical indices
92101 std::string BaseName1 = MIRVocabulary::extractBaseOpcodeName (" ADD16ri" );
@@ -98,10 +107,10 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
98107
99108 // Create a MIRVocabulary instance to test the mapping
100109 // Use a minimal MIRVocabulary to trigger canonical mapping construction
101- VocabMap VM ;
110+ VocabMap VMap ;
102111 Embedding Val = Embedding (64 , 1 .0f );
103- VM [" ADD" ] = Val;
104- MIRVocabulary TestVocab (std::move (VM ), TII);
112+ VMap [" ADD" ] = Val;
113+ MIRVocabulary TestVocab (std::move (VMap ), TII);
105114
106115 unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName1);
107116 unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName2);
@@ -132,9 +141,19 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
132141 6880u ); // X86 has >6880 unique base opcodes
133142
134143 // Check that the embeddings for opcodes not in the vocab are zero vectors
135- EXPECT_TRUE (TestVocab[AddIndex].approximatelyEquals (Val));
136- EXPECT_TRUE (TestVocab[SubIndex].approximatelyEquals (Embedding (64 , 0 .0f )));
137- EXPECT_TRUE (TestVocab[MovIndex].approximatelyEquals (Embedding (64 , 0 .0f )));
144+ int Add32rrOpcode = findOpcodeByName (TII, " ADD32rr" );
145+ ASSERT_NE (Add32rrOpcode, -1 ) << " ADD32rr opcode not found" ;
146+ EXPECT_TRUE (TestVocab[Add32rrOpcode].approximatelyEquals (Val));
147+
148+ int Sub32rrOpcode = findOpcodeByName (TII, " SUB32rr" );
149+ ASSERT_NE (Sub32rrOpcode, -1 ) << " SUB32rr opcode not found" ;
150+ EXPECT_TRUE (
151+ TestVocab[Sub32rrOpcode].approximatelyEquals (Embedding (64 , 0 .0f )));
152+
153+ int Mov32rrOpcode = findOpcodeByName (TII, " MOV32rr" );
154+ ASSERT_NE (Mov32rrOpcode, -1 ) << " MOV32rr opcode not found" ;
155+ EXPECT_TRUE (
156+ TestVocab[Mov32rrOpcode].approximatelyEquals (Embedding (64 , 0 .0f )));
138157}
139158
140159// Test deterministic mapping
@@ -144,9 +163,9 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
144163
145164 // Create a MIRVocabulary instance to test deterministic mapping
146165 // Use a minimal MIRVocabulary to trigger canonical mapping construction
147- VocabMap VM ;
148- VM [" ADD" ] = Embedding (64 , 1 .0f );
149- MIRVocabulary TestVocab (std::move (VM ), TII);
166+ VocabMap VMap ;
167+ VMap [" ADD" ] = Embedding (64 , 1 .0f );
168+ MIRVocabulary TestVocab (std::move (VMap ), TII);
150169
151170 unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName);
152171 unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName);
@@ -164,16 +183,11 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
164183
165184// Test MIRVocabulary construction
166185TEST_F (MIR2VecVocabTestFixture, VocabularyConstruction) {
167- // Test empty MIRVocabulary
168- MIRVocabulary EmptyVocab;
169- EXPECT_FALSE (EmptyVocab.isValid ());
170-
171- // Test MIRVocabulary with embeddings via VocabMap
172- VocabMap VM;
173- VM[" ADD" ] = Embedding (128 , 1 .0f ); // Dimension 128, all values 1.0
174- VM[" SUB" ] = Embedding (128 , 2 .0f ); // Dimension 128, all values 2.0
186+ VocabMap VMap;
187+ VMap[" ADD" ] = Embedding (128 , 1 .0f ); // Dimension 128, all values 1.0
188+ VMap[" SUB" ] = Embedding (128 , 2 .0f ); // Dimension 128, all values 2.0
175189
176- MIRVocabulary Vocab (std::move (VM ), TII);
190+ MIRVocabulary Vocab (std::move (VMap ), TII);
177191 EXPECT_TRUE (Vocab.isValid ());
178192 EXPECT_EQ (Vocab.getDimension (), 128u );
179193
0 commit comments