LLVM 24.0.0git
MIR2Vec.cpp
Go to the documentation of this file.
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
16#include "llvm/ADT/Statistic.h"
18#include "llvm/IR/Module.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/Errc.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 {
39
40// FIXME: Use a default vocab when not specified
42 VocabFile("mir2vec-vocab-path",
43 cl::desc("Path to the vocabulary file for MIR2Vec"), cl::init(""),
45static cl::opt<float>
46 OpcWeight("mir2vec-opc-weight", cl::init(1.0),
47 cl::desc("Weight for machine opcode embeddings"),
49static cl::opt<float>
50 CommonOperandWeight("mir2vec-common-operand-weight", cl::init(1.0),
51 cl::desc("Weight for common operand embeddings"),
53static cl::opt<float>
54 RegOperandWeight("mir2vec-reg-operand-weight", cl::init(1.0),
55 cl::desc("Weight for register operand embeddings"),
58 "mir2vec-kind",
60 "Generate symbolic embeddings for MIR")),
61 cl::init(MIR2VecKind::Symbolic), cl::desc("MIR2Vec embedding kind"),
63
65 "mir2vec-print-all-vocab-entries", cl::init(false),
66 cl::desc("Print all vocabulary entries including zero embeddings"),
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,
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, PhysicalRegisterMap,
97 VirtualRegisterMap);
98 Layout.TotalEntries = Storage.size();
99}
100
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())
110 "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())
118 "Failed to create valid vocabulary storage");
119 Vocab.ZeroEmbedding = Embedding(Vocab.Storage.getDimension(), 0.0);
120 return std::move(Vocab);
121}
122
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_]+)");
140
141 if (BaseOpcodeRegex.match(InstrName, &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
154 assert(!UniqueBaseOpcodeNames.empty() && "Canonical mapping not built");
155 auto It = std::find(UniqueBaseOpcodeNames.begin(),
156 UniqueBaseOpcodeNames.end(), BaseName.str());
157 assert(It != UniqueBaseOpcodeNames.end() &&
158 "Base name not found in unique opcodes");
159 return std::distance(UniqueBaseOpcodeNames.begin(), It);
160}
161
162unsigned MIRVocabulary::getCanonicalOpcodeIndex(unsigned Opcode) const {
163 auto BaseOpcode = extractBaseOpcodeName(TII.getName(Opcode));
164 return getCanonicalIndexForBaseName(BaseOpcode);
165}
166
167unsigned
169 auto It = llvm::find_if(CommonOperands, [&](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(std::begin(CommonOperands), It);
176}
177
178unsigned
180 bool IsPhysical) const {
181 auto It = std::find(RegisterOperandNames.begin(), RegisterOperandNames.end(),
182 RegName);
183 assert(It != RegisterOperandNames.end() &&
184 "Register name not found in register operands");
185 unsigned LocalIndex = std::distance(RegisterOperandNames.begin(), 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(It, 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(COpcodeName); It != OpcodeMap.end()) {
255 auto COpcodeIndex = getCanonicalIndexForBaseName(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(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(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(TII.getName(Opcode));
331 UniqueBaseOpcodeNames.insert(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(RC);
346 if (!RegClass)
347 continue;
348
349 // Get the register class name
350 StringRef ClassName = TRI.getRegClassName(RegClass);
351 RegisterOperandNames.push_back(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
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(TII.getName(Opcode));
429 if (DummyOpcMap.count(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(RC);
444 if (!RegClass)
445 continue;
446
447 std::string ClassName = TRI.getRegClassName(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
455 std::move(DummyOpcMap), std::move(DummyOperandMap),
456 std::move(DummyPhyRegMap), 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(
471 "MIR2Vec vocabulary file path not specified; set it "
472 "using --mir2vec-vocab-path");
473
474 auto BufOrError = MemoryBuffer::getFileOrSTDIN(VocabFile, /*IsText=*/true);
475 if (!BufOrError)
476 return createFileError(VocabFile, BufOrError.getError());
477
478 auto Content = BufOrError.get()->getBuffer();
479
480 Expected<json::Value> ParsedVocabValue = json::parse(Content);
481 if (!ParsedVocabValue)
482 return ParsedVocabValue.takeError();
483
484 unsigned OpcodeDim = 0, CommonOperandDim = 0, PhyRegOperandDim = 0,
485 VirtRegOperandDim = 0;
487 "Opcodes", *ParsedVocabValue, OpcodeVocab, OpcodeDim))
488 return Err;
489
491 "CommonOperands", *ParsedVocabValue, CommonOperandVocab,
492 CommonOperandDim))
493 return Err;
494
496 "PhysicalRegisters", *ParsedVocabValue, PhyRegVocabMap,
497 PhyRegOperandDim))
498 return Err;
499
501 "VirtualRegisters", *ParsedVocabValue, VirtRegVocabMap,
502 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(
510 "MIR2Vec vocabulary sections have different dimensions");
511 }
512
513 return Error::success();
514}
515
518 VocabMap OpcVocab, CommonOperandVocab, PhyRegVocabMap, VirtRegVocabMap;
519
520 if (Error Err = readVocabulary(OpcVocab, CommonOperandVocab, PhyRegVocabMap,
521 VirtRegVocabMap))
522 return std::move(Err);
523
524 const TargetSubtargetInfo &ST = MF.getSubtarget();
526 std::move(OpcVocab), std::move(CommonOperandVocab),
527 std::move(PhyRegVocabMap), std::move(VirtRegVocabMap), *ST.getInstrInfo(),
528 *ST.getRegisterInfo());
529}
530
533 for (const auto &F : M) {
534 if (F.isDeclaration())
535 continue;
536
537 if (auto *MF = MMI.getMachineFunction(F))
538 return buildVocabulary(*MF);
539 }
541 "No machine functions found in module");
542}
543
546 "MIR2Vec Vocabulary Analysis", false, true)
549 "MIR2Vec Vocabulary Analysis", false, true)
550
551StringRef MIR2VecVocabLegacyAnalysis::getPassName() const {
552 return "MIR2Vec Vocabulary Analysis";
553}
554
555AnalysisKey MIR2VecVocabAnalysis::Key;
556
560 auto VocabOrErr = buildVocabulary(MF);
561 if (!VocabOrErr)
562 reportFatalUsageError(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) {
575 return std::make_unique<SymbolicMIREmbedder>(MF, Vocab);
576 }
577 return nullptr;
578}
579
585
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 "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;
604 }
605
606 return MBBVector;
607}
608
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(&MF))
617 MFuncVector += computeEmbeddings(*MBB);
618 return MFuncVector;
619}
620
624
625std::unique_ptr<SymbolicMIREmbedder>
627 const MIRVocabulary &Vocab) {
628 return std::make_unique<SymbolicMIREmbedder>(MF, Vocab);
629}
630
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(MO, MF.getRegInfo());
642
643 return InstructionEmbedding;
644}
645
646//===----------------------------------------------------------------------===//
647// Printer Passes
648//===----------------------------------------------------------------------===//
649
652 "MIR2Vec Vocabulary Printer Pass", false, true)
656 "MIR2Vec Vocabulary Printer Pass", false, true)
657
661
664 auto MIR2VecVocabOrErr = Analysis.getMIR2VecVocabulary(M);
665
666 if (!MIR2VecVocabOrErr) {
667 OS << "MIR2Vec Vocabulary Printer: Failed to get vocabulary - "
668 << toString(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
691
692static void printEmbeddings(const MachineFunction &MF,
693 const mir2vec::MIRVocabulary &MIRVocab,
694 raw_ostream &OS) {
695 auto Emb = mir2vec::MIREmbedder::create(MIR2VecEmbeddingKind, MF, 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
729 "MIR2Vec Embedder Printer Pass", false, true)
733 "MIR2Vec Embedder Printer Pass", false, true)
734
737 auto VocabOrErr =
738 Analysis.getMIR2VecVocabulary(*MF.getFunction().getParent());
739 assert(VocabOrErr && "Failed to get MIR2Vec vocabulary");
740 printEmbeddings(MF, *VocabOrErr, OS);
741 return false;
742}
743
747
const AArch64Subtarget & Subtarget
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
block Block Frequency Analysis
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Module.h This file contains the declarations for the Module class.
#define RegName(no)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
static void printEmbeddings(const MachineFunction &MF, const mir2vec::MIRVocabulary &MIRVocab, raw_ostream &OS)
Definition MIR2Vec.cpp:692
static Error readVocabulary(VocabMap &OpcodeVocab, VocabMap &CommonOperandVocab, VocabMap &PhyRegVocabMap, VocabMap &VirtRegVocabMap)
Definition MIR2Vec.cpp:465
static Expected< mir2vec::MIRVocabulary > buildVocabulary(const MachineFunction &MF)
Definition MIR2Vec.cpp:517
std::map< std::string, Embedding > VocabMap
Definition MIR2Vec.cpp:463
This file defines the MIR2Vec framework for generating Machine IR embeddings.
Register Reg
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
SmallVector< MachineBasicBlock *, 4 > MBBVector
MachineRegisterInfo & MRI
const char * Msg
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const unsigned InvalidIndex
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
unsigned getID() const
getID() - Return the register class ID number.
This pass prints the MIR2Vec embeddings for machine functions, basic blocks, and instructions.
Definition MIR2Vec.h:458
MIR2VecPrinterLegacyPass(raw_ostream &OS)
Definition MIR2Vec.h:463
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition MIR2Vec.cpp:735
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition MIR2Vec.cpp:749
New pass manager analysis that provides the MIR2Vec vocabulary for a machine function.
Definition MIR2Vec.h:483
mir2vec::MIRVocabulary Result
Definition MIR2Vec.h:488
LLVM_ABI Result run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
Definition MIR2Vec.cpp:558
Pass to analyze and populate MIR2Vec vocabulary from a module.
Definition MIR2Vec.h:404
This pass prints the embeddings in the MIR2Vec vocabulary.
Definition MIR2Vec.h:435
bool doFinalization(Module &M) override
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
Definition MIR2Vec.cpp:662
bool runOnMachineFunction(MachineFunction &MF) override
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
Definition MIR2Vec.cpp:658
MIR2VecVocabPrinterLegacyPass(raw_ostream &OS)
Definition MIR2Vec.h:440
LLVM_ABI Expected< mir2vec::MIRVocabulary > getVocabulary(const Module &M)
Definition MIR2Vec.cpp:532
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
@ MO_Register
Register operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
const TargetRegisterClass * getRegClassOrNull(Register Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, bool IsText=false, bool RequiresNullTerminator=true, std::optional< Align > Alignment=std::nullopt)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
LLVM_ABI bool match(StringRef String, SmallVectorImpl< StringRef > *Matches=nullptr, std::string *Error=nullptr) const
matches - Match the regex against a given String.
Definition Regex.cpp:84
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:79
constexpr unsigned id() const
Definition Register.h:100
constexpr bool isPhysical() const
Return true if the specified register number is in the physical register namespace.
Definition Register.h:83
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
char back() const
Get the last character in the string.
Definition StringRef.h:153
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition StringRef.h:642
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
TargetSubtargetInfo - Generic base class for all target subtargets.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Generic storage class for section-based vocabularies.
Definition IR2Vec.h:154
static LLVM_ABI Error parseVocabSection(StringRef Key, const json::Value &ParsedVocabValue, VocabMap &TargetVocab, unsigned &Dim)
Parse a vocabulary section from JSON and populate the target vocabulary map.
Definition IR2Vec.cpp:295
unsigned getDimension() const
Get vocabulary dimension.
Definition IR2Vec.h:193
bool isValid() const
Check if vocabulary is valid (has data)
Definition IR2Vec.h:196
const unsigned Dimension
Dimension of the embeddings; Captured from the vocabulary.
Definition MIR2Vec.h:328
const MIRVocabulary & Vocab
Definition MIR2Vec.h:325
const float RegOperandWeight
Definition MIR2Vec.h:331
const float CommonOperandWeight
Definition MIR2Vec.h:331
LLVM_ABI Embedding computeEmbeddings() const
Function to compute embeddings.
Definition MIR2Vec.cpp:609
const float OpcWeight
Weight for opcode embeddings.
Definition MIR2Vec.h:331
const MachineFunction & MF
Definition MIR2Vec.h:324
static LLVM_ABI std::unique_ptr< MIREmbedder > create(MIR2VecKind Mode, const MachineFunction &MF, const MIRVocabulary &Vocab)
Factory method to create an Embedder object of the specified kind Returns nullptr if the requested ki...
Definition MIR2Vec.cpp:570
LLVM_ABI MIREmbedder(const MachineFunction &MF, const MIRVocabulary &Vocab)
Definition MIR2Vec.cpp:580
Class for storing and accessing the MIR2Vec vocabulary.
Definition MIR2Vec.h:86
const Embedding & getOperandEmbedding(const MachineOperand &Operand, const MachineRegisterInfo &MRI) const
Definition MIR2Vec.h:284
LLVM_ABI unsigned getCanonicalIndexForOperandName(StringRef OperandName) const
Definition MIR2Vec.cpp:168
LLVM_ABI unsigned getCanonicalIndexForRegisterClass(StringRef RegName, bool IsPhysical=true) const
Definition MIR2Vec.cpp:179
static LLVM_ABI std::string extractBaseOpcodeName(StringRef InstrName)
Static method for extracting base opcode names (public for testing)
Definition MIR2Vec.cpp:123
static LLVM_ABI Expected< MIRVocabulary > createDummyVocabForTest(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, unsigned Dim=1)
Create a dummy vocabulary for testing purposes.
Definition MIR2Vec.cpp:418
static LLVM_ABI Expected< MIRVocabulary > create(VocabMap &&OpcMap, VocabMap &&CommonOperandsMap, VocabMap &&PhyRegMap, VocabMap &&VirtRegMap, const TargetInstrInfo &TII, const TargetRegisterInfo &TRI)
Factory method to create MIRVocabulary from vocabulary map.
Definition MIR2Vec.cpp:101
LLVM_ABI std::string getStringKey(unsigned Pos) const
Get the string key for a vocabulary entry at the given position.
Definition MIR2Vec.cpp:189
LLVM_ABI unsigned getCanonicalIndexForBaseName(StringRef BaseName) const
Get indices from opcode or operand names.
Definition MIR2Vec.cpp:153
static std::unique_ptr< SymbolicMIREmbedder > create(const MachineFunction &MF, const MIRVocabulary &Vocab)
Definition MIR2Vec.cpp:626
SymbolicMIREmbedder(const MachineFunction &F, const MIRVocabulary &Vocab)
Definition MIR2Vec.cpp:621
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
@ Entry
Definition COFF.h:862
OperandType
Operands are tagged with one of the values of this enum.
Definition MCInstrDesc.h:59
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
LLVM_ABI llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition JSON.cpp:681
LLVM_ABI llvm::cl::OptionCategory MIR2VecCategory
static cl::opt< float > OpcWeight("mir2vec-opc-weight", cl::init(1.0), cl::desc("Weight for machine opcode embeddings"), cl::cat(MIR2VecCategory))
static cl::opt< float > RegOperandWeight("mir2vec-reg-operand-weight", cl::init(1.0), cl::desc("Weight for register operand embeddings"), cl::cat(MIR2VecCategory))
static cl::opt< bool > PrintAllVocabEntries("mir2vec-print-all-vocab-entries", cl::init(false), cl::desc("Print all vocabulary entries including zero embeddings"), cl::cat(MIR2VecCategory))
ir2vec::Embedding Embedding
Definition MIR2Vec.h:79
cl::opt< MIR2VecKind > MIR2VecEmbeddingKind("mir2vec-kind", cl::values(clEnumValN(MIR2VecKind::Symbolic, "symbolic", "Generate symbolic embeddings for MIR")), cl::init(MIR2VecKind::Symbolic), cl::desc("MIR2Vec embedding kind"), cl::cat(MIR2VecCategory))
static cl::opt< std::string > VocabFile("mir2vec-vocab-path", cl::desc("Path to the vocabulary file for MIR2Vec"), cl::init(""), cl::cat(MIR2VecCategory))
static cl::opt< float > CommonOperandWeight("mir2vec-common-operand-weight", cl::init(1.0), cl::desc("Weight for common operand embeddings"), cl::cat(MIR2VecCategory))
This is an optimization pass for GlobalISel generic memory operations.
Error createFileError(const Twine &F, Error E)
Concatenate a source file path and/or name with an Error.
Definition Error.h:1415
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
@ illegal_byte_sequence
Definition Errc.h:52
@ invalid_argument
Definition Errc.h:56
LLVM_ABI MachineFunctionPass * createMIR2VecPrinterLegacyPass(raw_ostream &OS)
Create a machine pass that prints MIR2Vec embeddings.
Definition MIR2Vec.cpp:744
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
LLVM_ABI MachineFunctionPass * createMIR2VecVocabPrinterLegacyPass(raw_ostream &OS)
MIR2VecVocabPrinter pass - This pass prints out the MIR2Vec vocabulary contents to the given stream a...
Definition MIR2Vec.cpp:688
MIR2VecKind
Definition MIR2Vec.h:69
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1788
iterator_range< df_iterator< T > > depth_first(const T &G)
MCRegisterClass TargetRegisterClass
Definition FastISel.h:58
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29