1//===- MIR2Vec.cpp - Implementation of MIR2Vec ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4// Exceptions. See the LICENSE file for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10/// This file implements the MIR2Vec algorithm for Machine IR embeddings.
11///
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MIR2Vec.h"
15#include "llvm/ADT/DepthFirstIterator.h"
16#include "llvm/ADT/Statistic.h"
17#include "llvm/CodeGen/TargetInstrInfo.h"
18#include "llvm/IR/Module.h"
19#include "llvm/InitializePasses.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/Errc.h"
22#include "llvm/Support/MemoryBuffer.h"
23#include "llvm/Support/Regex.h"
24#include <array>
25
26using namespace llvm;
27using namespace mir2vec;
28
29#define DEBUG_TYPE "mir2vec"
30
31STATISTIC(MIRVocabMissCounter,
32 "Number of lookups to MIR entities not present in the vocabulary");
33STATISTIC(MIRClasslessRegCounter,
34 "Number of register operands with no register class");
35
36namespace llvm {
37namespace mir2vec {
38cl::OptionCategory MIR2VecCategory("MIR2Vec Options");
39
40// FIXME: Use a default vocab when not specified
41static cl::opt<std::string>
42 VocabFile("mir2vec-vocab-path",
43 cl::desc("Path to the vocabulary file for MIR2Vec"), cl::init(Val: ""),
44 cl::cat(MIR2VecCategory));
45static cl::opt<float>
46 OpcWeight("mir2vec-opc-weight", cl::init(Val: 1.0),
47 cl::desc("Weight for machine opcode embeddings"),
48 cl::cat(MIR2VecCategory));
49static cl::opt<float>
50 CommonOperandWeight("mir2vec-common-operand-weight", cl::init(Val: 1.0),
51 cl::desc("Weight for common operand embeddings"),
52 cl::cat(MIR2VecCategory));
53static cl::opt<float>
54 RegOperandWeight("mir2vec-reg-operand-weight", cl::init(Val: 1.0),
55 cl::desc("Weight for register operand embeddings"),
56 cl::cat(MIR2VecCategory));
57cl::opt<MIR2VecKind> MIR2VecEmbeddingKind(
58 "mir2vec-kind",
59 cl::values(clEnumValN(MIR2VecKind::Symbolic, "symbolic",
60 "Generate symbolic embeddings for MIR")),
61 cl::init(Val: MIR2VecKind::Symbolic), cl::desc("MIR2Vec embedding kind"),
62 cl::cat(MIR2VecCategory));
63
64static cl::opt<bool> PrintAllVocabEntries(
65 "mir2vec-print-all-vocab-entries", cl::init(Val: false),
66 cl::desc("Print all vocabulary entries including zero embeddings"),
67 cl::cat(MIR2VecCategory));
68
69} // namespace mir2vec
70} // namespace llvm
71
72//===----------------------------------------------------------------------===//
73// Vocabulary
74//===----------------------------------------------------------------------===//
75
76MIRVocabulary::MIRVocabulary(VocabMap &&OpcodeMap, VocabMap &&CommonOperandMap,
77 VocabMap &&PhysicalRegisterMap,
78 VocabMap &&VirtualRegisterMap,
79 const TargetInstrInfo &TII,
80 const TargetRegisterInfo &TRI)
81 : TII(TII), TRI(TRI) {
82 buildCanonicalOpcodeMapping();
83 unsigned CanonicalOpcodeCount = UniqueBaseOpcodeNames.size();
84 assert(CanonicalOpcodeCount > 0 &&
85 "No canonical opcodes found for target - invalid vocabulary");
86
87 buildRegisterOperandMapping();
88
89 // Define layout of vocabulary sections
90 Layout.OpcodeBase = 0;
91 Layout.CommonOperandBase = CanonicalOpcodeCount;
92 // We expect same classes for physical and virtual registers
93 Layout.PhyRegBase = Layout.CommonOperandBase + std::size(CommonOperands);
94 Layout.VirtRegBase = Layout.PhyRegBase + RegisterOperandNames.size();
95
96 generateStorage(OpcodeMap, CommonOperandMap, PhyRegMap: PhysicalRegisterMap,
97 VirtRegMap: VirtualRegisterMap);
98 Layout.TotalEntries = Storage.size();
99}
100
101Expected<MIRVocabulary> MIRVocabulary::create(VocabMap &&OpcodeMap,
102 VocabMap &&CommonOperandMap,
103 VocabMap &&PhyRegMap,
104 VocabMap &&VirtRegMap,
105 const TargetInstrInfo &TII,
106 const TargetRegisterInfo &TRI) {
107 if (OpcodeMap.empty() || CommonOperandMap.empty() || PhyRegMap.empty() ||
108 VirtRegMap.empty())
109 return createStringError(EC: errc::invalid_argument,
110 S: "Empty vocabulary entries provided");
111
112 MIRVocabulary Vocab(std::move(OpcodeMap), std::move(CommonOperandMap),
113 std::move(PhyRegMap), std::move(VirtRegMap), TII, TRI);
114
115 // Validate Storage after construction
116 if (!Vocab.Storage.isValid())
117 return createStringError(EC: errc::invalid_argument,
118 S: "Failed to create valid vocabulary storage");
119 Vocab.ZeroEmbedding = Embedding(Vocab.Storage.getDimension(), 0.0);
120 return std::move(Vocab);
121}
122
123std::string MIRVocabulary::extractBaseOpcodeName(StringRef InstrName) {
124 // Extract base instruction name using regex to capture letters and
125 // underscores Examples: "ADD32rr" -> "ADD", "ARITH_FENCE" -> "ARITH_FENCE"
126 //
127 // TODO: Consider more sophisticated extraction:
128 // - Handle complex prefixes like "AVX1_SETALLONES" correctly (Currently, it
129 // would naively map to "AVX")
130 // - Extract width suffixes (8,16,32,64) as separate features
131 // - Capture addressing mode suffixes (r,i,m,ri,etc.) for better analysis
132 // (Currently, instances like "MOV32mi" map to "MOV", but "ADDPDrr" would map
133 // to "ADDPDrr")
134
135 assert(!InstrName.empty() && "Instruction name should not be empty");
136
137 // Use regex to extract initial sequence of letters and underscores
138 static const Regex BaseOpcodeRegex("([a-zA-Z_]+)");
139 SmallVector<StringRef, 2> Matches;
140
141 if (BaseOpcodeRegex.match(String: InstrName, Matches: &Matches) && Matches.size() > 1) {
142 StringRef Match = Matches[1];
143 // Trim trailing underscores
144 while (!Match.empty() && Match.back() == '_')
145 Match = Match.drop_back();
146 return Match.str();
147 }
148
149 // Fallback to original name if no pattern matches
150 return InstrName.str();
151}
152
153unsigned MIRVocabulary::getCanonicalIndexForBaseName(StringRef BaseName) const {
154 assert(!UniqueBaseOpcodeNames.empty() && "Canonical mapping not built");
155 auto It = std::find(first: UniqueBaseOpcodeNames.begin(),
156 last: UniqueBaseOpcodeNames.end(), val: BaseName.str());
157 assert(It != UniqueBaseOpcodeNames.end() &&
158 "Base name not found in unique opcodes");
159 return std::distance(first: UniqueBaseOpcodeNames.begin(), last: It);
160}
161
162unsigned MIRVocabulary::getCanonicalOpcodeIndex(unsigned Opcode) const {
163 auto BaseOpcode = extractBaseOpcodeName(InstrName: TII.getName(Opcode));
164 return getCanonicalIndexForBaseName(BaseName: BaseOpcode);
165}
166
167unsigned
168MIRVocabulary::getCanonicalIndexForOperandName(StringRef OperandName) const {
169 auto It = llvm::find_if(Range: CommonOperands, P: [&](const CommonOperandEntry &E) {
170 return E.Name == OperandName;
171 });
172 assert(It != std::end(CommonOperands) &&
173 "Operand name not found in common operands");
174 return Layout.CommonOperandBase +
175 std::distance(first: std::begin(arr: CommonOperands), last: It);
176}
177
178unsigned
179MIRVocabulary::getCanonicalIndexForRegisterClass(StringRef RegName,
180 bool IsPhysical) const {
181 auto It = std::find(first: RegisterOperandNames.begin(), last: RegisterOperandNames.end(),
182 val: RegName);
183 assert(It != RegisterOperandNames.end() &&
184 "Register name not found in register operands");
185 unsigned LocalIndex = std::distance(first: RegisterOperandNames.begin(), last: It);
186 return (IsPhysical ? Layout.PhyRegBase : Layout.VirtRegBase) + LocalIndex;
187}
188
189std::string MIRVocabulary::getStringKey(unsigned Pos) const {
190 assert(Pos < Layout.TotalEntries && "Position out of bounds in vocabulary");
191
192 // Handle opcodes section
193 if (Pos < Layout.CommonOperandBase) {
194 // Convert canonical index back to base opcode name
195 auto It = UniqueBaseOpcodeNames.begin();
196 std::advance(i&: It, n: Pos);
197 assert(It != UniqueBaseOpcodeNames.end() &&
198 "Canonical index out of bounds in opcode section");
199 return *It;
200 }
201
202 auto getLocalIndex = [](unsigned Pos, size_t BaseOffset, size_t Bound,
203 const char *Msg) {
204 unsigned LocalIndex = Pos - BaseOffset;
205 assert(LocalIndex < Bound && Msg);
206 return LocalIndex;
207 };
208
209 // Handle common operands section
210 if (Pos < Layout.PhyRegBase) {
211 unsigned LocalIndex =
212 getLocalIndex(Pos, Layout.CommonOperandBase, std::size(CommonOperands),
213 "Local index out of bounds in common operands");
214 return CommonOperands[LocalIndex].Name.str();
215 }
216
217 // Handle physical registers section
218 if (Pos < Layout.VirtRegBase) {
219 unsigned LocalIndex =
220 getLocalIndex(Pos, Layout.PhyRegBase, RegisterOperandNames.size(),
221 "Local index out of bounds in physical registers");
222 return "PhyReg_" + RegisterOperandNames[LocalIndex];
223 }
224
225 // Handle virtual registers section
226 unsigned LocalIndex =
227 getLocalIndex(Pos, Layout.VirtRegBase, RegisterOperandNames.size(),
228 "Local index out of bounds in virtual registers");
229 return "VirtReg_" + RegisterOperandNames[LocalIndex];
230}
231
232void MIRVocabulary::generateStorage(const VocabMap &OpcodeMap,
233 const VocabMap &CommonOperandsMap,
234 const VocabMap &PhyRegMap,
235 const VocabMap &VirtRegMap) {
236
237 // Helper for handling missing entities in the vocabulary.
238 // Currently, we use a zero vector. In the future, we will throw an error to
239 // ensure that *all* known entities are present in the vocabulary.
240 auto handleMissingEntity = [](StringRef Key) {
241 LLVM_DEBUG(errs() << "MIR2Vec: Missing vocabulary entry for " << Key
242 << "; using zero vector. This will result in an error "
243 "in the future.\n");
244 ++MIRVocabMissCounter;
245 };
246
247 // Initialize opcode embeddings section
248 unsigned EmbeddingDim = OpcodeMap.begin()->second.size();
249 std::vector<Embedding> OpcodeEmbeddings(Layout.CommonOperandBase,
250 Embedding(EmbeddingDim));
251
252 // Populate opcode embeddings using canonical mapping
253 for (auto COpcodeName : UniqueBaseOpcodeNames) {
254 if (auto It = OpcodeMap.find(x: COpcodeName); It != OpcodeMap.end()) {
255 auto COpcodeIndex = getCanonicalIndexForBaseName(BaseName: COpcodeName);
256 assert(COpcodeIndex < Layout.CommonOperandBase &&
257 "Canonical index out of bounds");
258 OpcodeEmbeddings[COpcodeIndex] = It->second;
259 } else {
260 handleMissingEntity(COpcodeName);
261 }
262 }
263
264 // Initialize common operand embeddings section
265 std::vector<Embedding> CommonOperandEmbeddings(std::size(CommonOperands),
266 Embedding(EmbeddingDim));
267 unsigned OperandIndex = 0;
268 for (const CommonOperandEntry &Entry : CommonOperands) {
269 if (auto It = CommonOperandsMap.find(x: Entry.Name.str());
270 It != CommonOperandsMap.end()) {
271 CommonOperandEmbeddings[OperandIndex] = It->second;
272 } else {
273 handleMissingEntity(Entry.Name);
274 }
275 ++OperandIndex;
276 }
277
278 // Helper lambda for creating register operand embeddings
279 auto createRegisterEmbeddings = [&](const VocabMap &RegMap) {
280 std::vector<Embedding> RegEmbeddings(TRI.getNumRegClasses(),
281 Embedding(EmbeddingDim));
282 unsigned RegOperandIndex = 0;
283 for (const auto &RegOperandName : RegisterOperandNames) {
284 if (auto It = RegMap.find(x: RegOperandName); It != RegMap.end())
285 RegEmbeddings[RegOperandIndex] = It->second;
286 else
287 handleMissingEntity(RegOperandName);
288 ++RegOperandIndex;
289 }
290 return RegEmbeddings;
291 };
292
293 // Initialize register operand embeddings sections
294 std::vector<Embedding> PhyRegEmbeddings = createRegisterEmbeddings(PhyRegMap);
295 std::vector<Embedding> VirtRegEmbeddings =
296 createRegisterEmbeddings(VirtRegMap);
297
298 // Scale the vocabulary sections based on the provided weights
299 auto scaleVocabSection = [](std::vector<Embedding> &Embeddings,
300 double Weight) {
301 for (auto &Embedding : Embeddings)
302 Embedding *= Weight;
303 };
304 scaleVocabSection(OpcodeEmbeddings, OpcWeight);
305 scaleVocabSection(CommonOperandEmbeddings, CommonOperandWeight);
306 scaleVocabSection(PhyRegEmbeddings, RegOperandWeight);
307 scaleVocabSection(VirtRegEmbeddings, RegOperandWeight);
308
309 std::vector<std::vector<Embedding>> Sections(
310 static_cast<unsigned>(Section::MaxSections));
311 Sections[static_cast<unsigned>(Section::Opcodes)] =
312 std::move(OpcodeEmbeddings);
313 Sections[static_cast<unsigned>(Section::CommonOperands)] =
314 std::move(CommonOperandEmbeddings);
315 Sections[static_cast<unsigned>(Section::PhyRegisters)] =
316 std::move(PhyRegEmbeddings);
317 Sections[static_cast<unsigned>(Section::VirtRegisters)] =
318 std::move(VirtRegEmbeddings);
319
320 Storage = ir2vec::VocabStorage(std::move(Sections));
321}
322
323void MIRVocabulary::buildCanonicalOpcodeMapping() {
324 // Check if already built
325 if (!UniqueBaseOpcodeNames.empty())
326 return;
327
328 // Build mapping from opcodes to canonical base opcode indices
329 for (unsigned Opcode = 0; Opcode < TII.getNumOpcodes(); ++Opcode) {
330 std::string BaseOpcode = extractBaseOpcodeName(InstrName: TII.getName(Opcode));
331 UniqueBaseOpcodeNames.insert(x: BaseOpcode);
332 }
333
334 LLVM_DEBUG(dbgs() << "MIR2Vec: Built canonical mapping for target with "
335 << UniqueBaseOpcodeNames.size()
336 << " unique base opcodes\n");
337}
338
339void MIRVocabulary::buildRegisterOperandMapping() {
340 // Check if already built
341 if (!RegisterOperandNames.empty())
342 return;
343
344 for (unsigned RC = 0; RC < TRI.getNumRegClasses(); ++RC) {
345 const TargetRegisterClass *RegClass = TRI.getRegClass(i: RC);
346 if (!RegClass)
347 continue;
348
349 // Get the register class name
350 StringRef ClassName = TRI.getRegClassName(Class: RegClass);
351 RegisterOperandNames.push_back(Elt: ClassName.str());
352 }
353}
354
355std::optional<unsigned> MIRVocabulary::getCommonOperandIndex(
356 MachineOperand::MachineOperandType OperandType) const {
357 assert(OperandType != MachineOperand::MO_Register &&
358 "Expected non-register operand type");
359 assert(OperandType <= MachineOperand::MO_Last &&
360 "Operand type out of bounds");
361 static constexpr unsigned InvalidIndex = ~0u;
362 static constexpr auto OperandTypeToIndex = [] {
363 std::array<unsigned, MachineOperand::MO_Last + 1> Map{};
364 for (unsigned &I : Map)
365 I = InvalidIndex;
366 for (unsigned I = 0; I < std::size(CommonOperands); ++I)
367 Map[CommonOperands[I].Type] = I;
368 return Map;
369 }();
370 unsigned Index = OperandTypeToIndex[OperandType];
371 if (Index == InvalidIndex)
372 return std::nullopt;
373 return Index;
374}
375
376std::optional<unsigned>
377MIRVocabulary::getRegisterOperandIndex(Register Reg,
378 const MachineRegisterInfo &MRI) const {
379 assert(!RegisterOperandNames.empty() && "Register operand mapping not built");
380 assert(Reg.isValid() && "Invalid register; not expected here");
381 assert((Reg.isPhysical() || Reg.isVirtual()) &&
382 "Expected a physical or virtual register");
383
384 const TargetRegisterClass *RegClass = nullptr;
385
386 // For physical registers, use TRI to get minimal register class as a
387 // physical register can belong to multiple classes. For virtual
388 // registers, use MRI to uniquely identify the assigned register class.
389 if (Reg.isPhysical())
390 RegClass = TRI.getMinimalPhysRegClass(Reg);
391 else
392 RegClass = MRI.getRegClassOrNull(Reg);
393
394 // Not every register belongs to a register class. This can happen for
395 // physical registers, e.g. X86's $mxcsr and $fpcw or AMDGPU's $mode, for
396 // which getMinimalPhysRegClass() returns nullptr. It can also happen for
397 // generic virtual registers that have not yet been through (or completed)
398 // GlobalISel's register bank selection, and thus carry an LLT or a
399 // RegisterBank instead of a TargetRegisterClass, for which
400 // getRegClassOrNull() returns nullptr.
401 // TODO: Avoid special-casing these registers at every use site. Classless
402 // registers currently fall back to a zero embedding in getRegisterEmbedding()
403 // and to VirtRegBase in getEntityIDForRegister(), which is the same ad-hoc
404 // handling the invalid/stack-slot cases already get. Give them a real
405 // vocabulary representation instead -- e.g. an explicit "no register class"
406 // entry, or keying generic vregs on their LLT/RegisterBank -- so that the
407 // lookup is total and the callers need no fallbacks.
408 if (!RegClass) {
409 LLVM_DEBUG(errs() << "MIR2Vec: No register class for register " << Reg.id()
410 << "; using zero vector.\n");
411 ++MIRClasslessRegCounter;
412 return std::nullopt;
413 }
414
415 return RegClass->getID();
416}
417
418Expected<MIRVocabulary> MIRVocabulary::createDummyVocabForTest(
419 const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, unsigned Dim) {
420 assert(Dim > 0 && "Dimension must be greater than zero");
421
422 float DummyVal = 0.1f;
423
424 VocabMap DummyOpcMap, DummyOperandMap, DummyPhyRegMap, DummyVirtRegMap;
425
426 // Process opcodes directly without creating temporary vocabulary
427 for (unsigned Opcode = 0; Opcode < TII.getNumOpcodes(); ++Opcode) {
428 std::string BaseOpcode = extractBaseOpcodeName(InstrName: TII.getName(Opcode));
429 if (DummyOpcMap.count(x: BaseOpcode) == 0) { // Only add if not already present
430 DummyOpcMap[BaseOpcode] = Embedding(Dim, DummyVal);
431 DummyVal += 0.1f;
432 }
433 }
434
435 // Add common operands
436 for (const CommonOperandEntry &Entry : CommonOperands) {
437 DummyOperandMap[Entry.Name.str()] = Embedding(Dim, DummyVal);
438 DummyVal += 0.1f;
439 }
440
441 // Process register classes directly
442 for (unsigned RC = 0; RC < TRI.getNumRegClasses(); ++RC) {
443 const TargetRegisterClass *RegClass = TRI.getRegClass(i: RC);
444 if (!RegClass)
445 continue;
446
447 std::string ClassName = TRI.getRegClassName(Class: RegClass);
448 DummyPhyRegMap[ClassName] = Embedding(Dim, DummyVal);
449 DummyVirtRegMap[ClassName] = Embedding(Dim, DummyVal);
450 DummyVal += 0.1f;
451 }
452
453 // Create vocabulary directly without temporary instance
454 return MIRVocabulary::create(
455 OpcodeMap: std::move(DummyOpcMap), CommonOperandMap: std::move(DummyOperandMap),
456 PhyRegMap: std::move(DummyPhyRegMap), VirtRegMap: std::move(DummyVirtRegMap), TII, TRI);
457}
458
459//===----------------------------------------------------------------------===//
460// MIR2VecVocabProvider and MIR2VecVocabLegacyAnalysis
461//===----------------------------------------------------------------------===//
462
463using VocabMap = std::map<std::string, Embedding>;
464
465static Error readVocabulary(VocabMap &OpcodeVocab, VocabMap &CommonOperandVocab,
466 VocabMap &PhyRegVocabMap,
467 VocabMap &VirtRegVocabMap) {
468 if (VocabFile.empty())
469 return createStringError(
470 EC: errc::invalid_argument,
471 S: "MIR2Vec vocabulary file path not specified; set it "
472 "using --mir2vec-vocab-path");
473
474 auto BufOrError = MemoryBuffer::getFileOrSTDIN(Filename: VocabFile, /*IsText=*/true);
475 if (!BufOrError)
476 return createFileError(F: VocabFile, EC: BufOrError.getError());
477
478 auto Content = BufOrError.get()->getBuffer();
479
480 Expected<json::Value> ParsedVocabValue = json::parse(JSON: Content);
481 if (!ParsedVocabValue)
482 return ParsedVocabValue.takeError();
483
484 unsigned OpcodeDim = 0, CommonOperandDim = 0, PhyRegOperandDim = 0,
485 VirtRegOperandDim = 0;
486 if (auto Err = ir2vec::VocabStorage::parseVocabSection(
487 Key: "Opcodes", ParsedVocabValue: *ParsedVocabValue, TargetVocab&: OpcodeVocab, Dim&: OpcodeDim))
488 return Err;
489
490 if (auto Err = ir2vec::VocabStorage::parseVocabSection(
491 Key: "CommonOperands", ParsedVocabValue: *ParsedVocabValue, TargetVocab&: CommonOperandVocab,
492 Dim&: CommonOperandDim))
493 return Err;
494
495 if (auto Err = ir2vec::VocabStorage::parseVocabSection(
496 Key: "PhysicalRegisters", ParsedVocabValue: *ParsedVocabValue, TargetVocab&: PhyRegVocabMap,
497 Dim&: PhyRegOperandDim))
498 return Err;
499
500 if (auto Err = ir2vec::VocabStorage::parseVocabSection(
501 Key: "VirtualRegisters", ParsedVocabValue: *ParsedVocabValue, TargetVocab&: VirtRegVocabMap,
502 Dim&: VirtRegOperandDim))
503 return Err;
504
505 // All sections must have the same embedding dimension
506 if (!(OpcodeDim == CommonOperandDim && CommonOperandDim == PhyRegOperandDim &&
507 PhyRegOperandDim == VirtRegOperandDim)) {
508 return createStringError(
509 EC: errc::illegal_byte_sequence,
510 S: "MIR2Vec vocabulary sections have different dimensions");
511 }
512
513 return Error::success();
514}
515
516static Expected<mir2vec::MIRVocabulary>
517buildVocabulary(const MachineFunction &MF) {
518 VocabMap OpcVocab, CommonOperandVocab, PhyRegVocabMap, VirtRegVocabMap;
519
520 if (Error Err = readVocabulary(OpcodeVocab&: OpcVocab, CommonOperandVocab, PhyRegVocabMap,
521 VirtRegVocabMap))
522 return std::move(Err);
523
524 const TargetSubtargetInfo &ST = MF.getSubtarget();
525 return mir2vec::MIRVocabulary::create(
526 OpcodeMap: std::move(OpcVocab), CommonOperandMap: std::move(CommonOperandVocab),
527 PhyRegMap: std::move(PhyRegVocabMap), VirtRegMap: std::move(VirtRegVocabMap), TII: *ST.getInstrInfo(),
528 TRI: *ST.getRegisterInfo());
529}
530
531Expected<mir2vec::MIRVocabulary>
532MIR2VecVocabProvider::getVocabulary(const Module &M) {
533 for (const auto &F : M) {
534 if (F.isDeclaration())
535 continue;
536
537 if (auto *MF = MMI.getMachineFunction(F))
538 return buildVocabulary(MF: *MF);
539 }
540 return createStringError(EC: errc::invalid_argument,
541 S: "No machine functions found in module");
542}
543
544char MIR2VecVocabLegacyAnalysis::ID = 0;
545INITIALIZE_PASS_BEGIN(MIR2VecVocabLegacyAnalysis, "mir2vec-vocab-analysis",
546 "MIR2Vec Vocabulary Analysis", false, true)
547INITIALIZE_PASS_DEPENDENCY(MachineModuleInfoWrapperPass)
548INITIALIZE_PASS_END(MIR2VecVocabLegacyAnalysis, "mir2vec-vocab-analysis",
549 "MIR2Vec Vocabulary Analysis", false, true)
550
551StringRef MIR2VecVocabLegacyAnalysis::getPassName() const {
552 return "MIR2Vec Vocabulary Analysis";
553}
554
555AnalysisKey MIR2VecVocabAnalysis::Key;
556
557MIR2VecVocabAnalysis::Result
558MIR2VecVocabAnalysis::run(MachineFunction &MF,
559 MachineFunctionAnalysisManager &MFAM) {
560 auto VocabOrErr = buildVocabulary(MF);
561 if (!VocabOrErr)
562 reportFatalUsageError(Err: VocabOrErr.takeError());
563 return std::move(*VocabOrErr);
564}
565
566//===----------------------------------------------------------------------===//
567// MIREmbedder and its subclasses
568//===----------------------------------------------------------------------===//
569
570std::unique_ptr<MIREmbedder> MIREmbedder::create(MIR2VecKind Mode,
571 const MachineFunction &MF,
572 const MIRVocabulary &Vocab) {
573 switch (Mode) {
574 case MIR2VecKind::Symbolic:
575 return std::make_unique<SymbolicMIREmbedder>(args: MF, args: Vocab);
576 }
577 return nullptr;
578}
579
580MIREmbedder::MIREmbedder(const MachineFunction &MF, const MIRVocabulary &Vocab)
581 : MF(MF), Vocab(Vocab), Dimension(Vocab.getDimension()),
582 OpcWeight(mir2vec::OpcWeight),
583 CommonOperandWeight(mir2vec::CommonOperandWeight),
584 RegOperandWeight(mir2vec::RegOperandWeight) {}
585
586Embedding MIREmbedder::computeEmbeddings(const MachineBasicBlock &MBB) const {
587 Embedding MBBVector(Dimension, 0);
588
589 // Get instruction info for opcode name resolution
590 const auto &Subtarget = MF.getSubtarget();
591 const auto *TII = Subtarget.getInstrInfo();
592 if (!TII) {
593 MF.getFunction().getContext().emitError(
594 ErrorStr: "MIR2Vec: No TargetInstrInfo available; cannot compute embeddings");
595 return MBBVector;
596 }
597
598 // Process each machine instruction in the basic block
599 for (const auto &MI : MBB) {
600 // Skip debug instructions and other metadata
601 if (MI.isDebugInstr())
602 continue;
603 MBBVector += computeEmbeddings(MI);
604 }
605
606 return MBBVector;
607}
608
609Embedding MIREmbedder::computeEmbeddings() const {
610 Embedding MFuncVector(Dimension, 0);
611
612 if (MF.empty())
613 return MFuncVector;
614
615 // Consider all reachable machine basic blocks in the function
616 for (const auto *MBB : depth_first(G: &MF))
617 MFuncVector += computeEmbeddings(MBB: *MBB);
618 return MFuncVector;
619}
620
621SymbolicMIREmbedder::SymbolicMIREmbedder(const MachineFunction &MF,
622 const MIRVocabulary &Vocab)
623 : MIREmbedder(MF, Vocab) {}
624
625std::unique_ptr<SymbolicMIREmbedder>
626SymbolicMIREmbedder::create(const MachineFunction &MF,
627 const MIRVocabulary &Vocab) {
628 return std::make_unique<SymbolicMIREmbedder>(args: MF, args: Vocab);
629}
630
631Embedding SymbolicMIREmbedder::computeEmbeddings(const MachineInstr &MI) const {
632 // Skip debug instructions and other metadata
633 if (MI.isDebugInstr())
634 return Embedding(Dimension, 0);
635
636 // Opcode embedding
637 Embedding InstructionEmbedding = Vocab[MI.getOpcode()];
638
639 // Add operand contributions
640 for (const MachineOperand &MO : MI.operands())
641 InstructionEmbedding += Vocab.getOperandEmbedding(Operand: MO, MRI: MF.getRegInfo());
642
643 return InstructionEmbedding;
644}
645
646//===----------------------------------------------------------------------===//
647// Printer Passes
648//===----------------------------------------------------------------------===//
649
650char MIR2VecVocabPrinterLegacyPass::ID = 0;
651INITIALIZE_PASS_BEGIN(MIR2VecVocabPrinterLegacyPass, "print-mir2vec-vocab",
652 "MIR2Vec Vocabulary Printer Pass", false, true)
653INITIALIZE_PASS_DEPENDENCY(MIR2VecVocabLegacyAnalysis)
654INITIALIZE_PASS_DEPENDENCY(MachineModuleInfoWrapperPass)
655INITIALIZE_PASS_END(MIR2VecVocabPrinterLegacyPass, "print-mir2vec-vocab",
656 "MIR2Vec Vocabulary Printer Pass", false, true)
657
658bool MIR2VecVocabPrinterLegacyPass::runOnMachineFunction(MachineFunction &MF) {
659 return false;
660}
661
662bool MIR2VecVocabPrinterLegacyPass::doFinalization(Module &M) {
663 auto &Analysis = getAnalysis<MIR2VecVocabLegacyAnalysis>();
664 auto MIR2VecVocabOrErr = Analysis.getMIR2VecVocabulary(M);
665
666 if (!MIR2VecVocabOrErr) {
667 OS << "MIR2Vec Vocabulary Printer: Failed to get vocabulary - "
668 << toString(E: MIR2VecVocabOrErr.takeError()) << "\n";
669 return false;
670 }
671
672 auto &MIR2VecVocab = *MIR2VecVocabOrErr;
673 unsigned Pos = 0;
674 for (const auto &Entry : MIR2VecVocab) {
675 // Skip zero embeddings to avoid printing entries not in the vocabulary.
676 // This makes the output stable across changes to the opcode list.
677 if (PrintAllVocabEntries || !Entry.isZero()) {
678 OS << "Key: " << MIR2VecVocab.getStringKey(Pos) << ": ";
679 Entry.print(OS);
680 }
681 ++Pos;
682 }
683
684 return false;
685}
686
687MachineFunctionPass *
688llvm::createMIR2VecVocabPrinterLegacyPass(raw_ostream &OS) {
689 return new MIR2VecVocabPrinterLegacyPass(OS);
690}
691
692static void printEmbeddings(const MachineFunction &MF,
693 const mir2vec::MIRVocabulary &MIRVocab,
694 raw_ostream &OS) {
695 auto Emb = mir2vec::MIREmbedder::create(Mode: MIR2VecEmbeddingKind, MF, Vocab: MIRVocab);
696 if (!Emb) {
697 OS << "Error creating MIR2Vec embeddings for function " << MF.getName()
698 << "\n";
699 return;
700 }
701
702 OS << "MIR2Vec embeddings for machine function " << MF.getName() << ":\n";
703 OS << "Machine Function vector: ";
704 Emb->getMFunctionVector().print(OS);
705
706 OS << "Machine basic block vectors:\n";
707 for (const MachineBasicBlock &MBB : MF) {
708 OS << "Machine basic block: " << MBB.getFullName() << ":\n";
709 Emb->getMBBVector(MBB).print(OS);
710 }
711
712 OS << "Machine instruction vectors:\n";
713 for (const MachineBasicBlock &MBB : MF) {
714 for (const MachineInstr &MI : MBB) {
715 // Skip debug instructions as they are not
716 // embedded
717 if (MI.isDebugInstr())
718 continue;
719
720 OS << "Machine instruction: ";
721 MI.print(OS);
722 Emb->getMInstVector(MI).print(OS);
723 }
724 }
725}
726
727char MIR2VecPrinterLegacyPass::ID = 0;
728INITIALIZE_PASS_BEGIN(MIR2VecPrinterLegacyPass, "print-mir2vec",
729 "MIR2Vec Embedder Printer Pass", false, true)
730INITIALIZE_PASS_DEPENDENCY(MIR2VecVocabLegacyAnalysis)
731INITIALIZE_PASS_DEPENDENCY(MachineModuleInfoWrapperPass)
732INITIALIZE_PASS_END(MIR2VecPrinterLegacyPass, "print-mir2vec",
733 "MIR2Vec Embedder Printer Pass", false, true)
734
735bool MIR2VecPrinterLegacyPass::runOnMachineFunction(MachineFunction &MF) {
736 auto &Analysis = getAnalysis<MIR2VecVocabLegacyAnalysis>();
737 auto VocabOrErr =
738 Analysis.getMIR2VecVocabulary(M: *MF.getFunction().getParent());
739 assert(VocabOrErr && "Failed to get MIR2Vec vocabulary");
740 printEmbeddings(MF, MIRVocab: *VocabOrErr, OS);
741 return false;
742}
743
744MachineFunctionPass *llvm::createMIR2VecPrinterLegacyPass(raw_ostream &OS) {
745 return new MIR2VecPrinterLegacyPass(OS);
746}
747
748PreservedAnalyses
749MIR2VecPrinterPass::run(MachineFunction &MF,
750 MachineFunctionAnalysisManager &MFAM) {
751 printEmbeddings(MF, MIRVocab: MFAM.getResult<MIR2VecVocabAnalysis>(IR&: MF), OS);
752 return PreservedAnalyses::all();
753}
754