@@ -95,6 +95,15 @@ class MIR2VecVocabTestFixture : public ::testing::Test {
95
95
}
96
96
};
97
97
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
+
98
107
TEST_F (MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
99
108
// Test that same base opcodes get same canonical indices
100
109
std::string BaseName1 = MIRVocabulary::extractBaseOpcodeName (" ADD16ri" );
@@ -106,10 +115,10 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
106
115
107
116
// Create a MIRVocabulary instance to test the mapping
108
117
// Use a minimal MIRVocabulary to trigger canonical mapping construction
109
- VocabMap VM ;
118
+ VocabMap VMap ;
110
119
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);
113
122
114
123
unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName1);
115
124
unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName2);
@@ -140,9 +149,19 @@ TEST_F(MIR2VecVocabTestFixture, CanonicalOpcodeMappingTest) {
140
149
6880u ); // X86 has >6880 unique base opcodes
141
150
142
151
// 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 )));
146
165
}
147
166
148
167
// Test deterministic mapping
@@ -152,9 +171,9 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
152
171
153
172
// Create a MIRVocabulary instance to test deterministic mapping
154
173
// 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);
158
177
159
178
unsigned Index1 = TestVocab.getCanonicalIndexForBaseName (BaseName);
160
179
unsigned Index2 = TestVocab.getCanonicalIndexForBaseName (BaseName);
@@ -172,16 +191,11 @@ TEST_F(MIR2VecVocabTestFixture, DeterministicMapping) {
172
191
173
192
// Test MIRVocabulary construction
174
193
TEST_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
183
197
184
- MIRVocabulary Vocab (std::move (VM ), TII);
198
+ MIRVocabulary Vocab (std::move (VMap ), TII);
185
199
EXPECT_TRUE (Vocab.isValid ());
186
200
EXPECT_EQ (Vocab.getDimension (), 128u );
187
201
0 commit comments