1//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the class that parses the optional LLVM IR and machine
10// functions that are stored in MIR files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/MIRParser/MIRParser.h"
15#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/StringRef.h"
17#include "llvm/AsmParser/Parser.h"
18#include "llvm/AsmParser/SlotMapping.h"
19#include "llvm/CodeGen/MIRParser/MIParser.h"
20#include "llvm/CodeGen/MIRYamlMapping.h"
21#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFunctionAnalysis.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/CodeGen/TargetFrameLowering.h"
28#include "llvm/IR/BasicBlock.h"
29#include "llvm/IR/DebugInfoMetadata.h"
30#include "llvm/IR/DiagnosticInfo.h"
31#include "llvm/IR/Instructions.h"
32#include "llvm/IR/LLVMContext.h"
33#include "llvm/IR/Module.h"
34#include "llvm/IR/ValueSymbolTable.h"
35#include "llvm/Support/LineIterator.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/SMLoc.h"
38#include "llvm/Support/SourceMgr.h"
39#include "llvm/Support/YAMLTraits.h"
40#include "llvm/Target/TargetMachine.h"
41#include <memory>
42
43using namespace llvm;
44
45namespace llvm {
46class MDNode;
47class RegisterBank;
48
49/// This class implements the parsing of LLVM IR that's embedded inside a MIR
50/// file.
51class MIRParserImpl {
52 SourceMgr SM;
53 LLVMContext &Context;
54 yaml::Input In;
55 StringRef Filename;
56 SlotMapping IRSlots;
57 std::unique_ptr<PerTargetMIParsingState> Target;
58
59 /// True when the MIR file doesn't have LLVM IR. Dummy IR functions are
60 /// created and inserted into the given module when this is true.
61 bool NoLLVMIR = false;
62 /// True when a well formed MIR file does not contain any MIR/machine function
63 /// parts.
64 bool NoMIRDocuments = false;
65
66 std::function<void(Function &)> ProcessIRFunction;
67
68public:
69 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
70 LLVMContext &Context,
71 std::function<void(Function &)> ProcessIRFunction);
72
73 void reportDiagnostic(const SMDiagnostic &Diag);
74
75 /// Report an error with the given message at unknown location.
76 ///
77 /// Always returns true.
78 bool error(const Twine &Message);
79
80 /// Report an error with the given message at the given location.
81 ///
82 /// Always returns true.
83 bool error(SMLoc Loc, const Twine &Message);
84
85 /// Report a given error with the location translated from the location in an
86 /// embedded string literal to a location in the MIR file.
87 ///
88 /// Always returns true.
89 bool error(const SMDiagnostic &Error, SMRange SourceRange);
90
91 /// Try to parse the optional LLVM module and the machine functions in the MIR
92 /// file.
93 ///
94 /// Return null if an error occurred.
95 std::unique_ptr<Module>
96 parseIRModule(DataLayoutCallbackTy DataLayoutCallback);
97
98 /// Create an empty function with the given name.
99 Function *createDummyFunction(StringRef Name, Module &M);
100
101 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI,
102 ModuleAnalysisManager *FAM = nullptr);
103
104 /// Parse the machine function in the current YAML document.
105 ///
106 ///
107 /// Return true if an error occurred.
108 bool parseMachineFunction(Module &M, MachineModuleInfo &MMI,
109 ModuleAnalysisManager *FAM,
110 Module::iterator &FirstUnvisitedFunction);
111
112 /// Initialize the machine function to the state that's described in the MIR
113 /// file.
114 ///
115 /// Return true if error occurred.
116 bool initializeMachineFunction(const yaml::MachineFunction &YamlMF,
117 MachineFunction &MF);
118
119 bool initializeCallSiteInfo(PerFunctionMIParsingState &PFS,
120 const yaml::MachineFunction &YamlMF);
121
122 bool initializePrefetchTargets(PerFunctionMIParsingState &PFS,
123 const yaml::MachineFunction &YamlMF);
124
125 bool parseRegisterInfo(PerFunctionMIParsingState &PFS,
126 const yaml::MachineFunction &YamlMF);
127
128 bool setupRegisterInfo(const PerFunctionMIParsingState &PFS,
129 const yaml::MachineFunction &YamlMF);
130
131 bool initializeFrameInfo(PerFunctionMIParsingState &PFS,
132 const yaml::MachineFunction &YamlMF);
133
134 bool initializeSaveRestorePoints(
135 PerFunctionMIParsingState &PFS,
136 const std::vector<yaml::SaveRestorePointEntry> &YamlSRPoints,
137 llvm::SaveRestorePoints &SaveRestorePoints);
138
139 bool parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
140 std::vector<CalleeSavedInfo> &CSIInfo,
141 const yaml::StringValue &RegisterSource,
142 bool IsRestored, int FrameIdx);
143
144 struct VarExprLoc {
145 DILocalVariable *DIVar = nullptr;
146 DIExpression *DIExpr = nullptr;
147 DILocation *DILoc = nullptr;
148 };
149
150 std::optional<VarExprLoc> parseVarExprLoc(PerFunctionMIParsingState &PFS,
151 const yaml::StringValue &VarStr,
152 const yaml::StringValue &ExprStr,
153 const yaml::StringValue &LocStr);
154 template <typename T>
155 bool parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
156 const T &Object,
157 int FrameIdx);
158
159 bool initializeConstantPool(PerFunctionMIParsingState &PFS,
160 MachineConstantPool &ConstantPool,
161 const yaml::MachineFunction &YamlMF);
162
163 bool initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
164 const yaml::MachineJumpTable &YamlJTI);
165
166 bool parseMachineMetadataNodes(PerFunctionMIParsingState &PFS,
167 MachineFunction &MF,
168 const yaml::MachineFunction &YMF);
169
170 bool parseCalledGlobals(PerFunctionMIParsingState &PFS, MachineFunction &MF,
171 const yaml::MachineFunction &YMF);
172
173private:
174 bool parseMDNode(PerFunctionMIParsingState &PFS, MDNode *&Node,
175 const yaml::StringValue &Source);
176
177 bool parseMBBReference(PerFunctionMIParsingState &PFS,
178 MachineBasicBlock *&MBB,
179 const yaml::StringValue &Source);
180
181 bool parseMachineMetadata(PerFunctionMIParsingState &PFS,
182 const yaml::StringValue &Source);
183
184 /// Return a MIR diagnostic converted from an MI string diagnostic.
185 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
186 SMRange SourceRange);
187
188 /// Return a MIR diagnostic converted from a diagnostic located in a YAML
189 /// block scalar string.
190 SMDiagnostic diagFromBlockStringDiag(const SMDiagnostic &Error,
191 SMRange SourceRange);
192
193 bool computeFunctionProperties(MachineFunction &MF,
194 const yaml::MachineFunction &YamlMF);
195
196 void setupDebugValueTracking(MachineFunction &MF,
197 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF);
198
199 bool parseMachineInst(MachineFunction &MF, yaml::MachineInstrLoc MILoc,
200 MachineInstr const *&MI);
201};
202
203} // end namespace llvm
204
205static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
206 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
207}
208
209MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
210 StringRef Filename, LLVMContext &Context,
211 std::function<void(Function &)> Callback)
212 : Context(Context),
213 In(SM.getMemoryBuffer(i: SM.AddNewSourceBuffer(F: std::move(Contents), IncludeLoc: SMLoc()))
214 ->getBuffer(),
215 nullptr, handleYAMLDiag, this),
216 Filename(Filename), ProcessIRFunction(Callback) {
217 In.setContext(&In);
218}
219
220bool MIRParserImpl::error(const Twine &Message) {
221 Context.diagnose(DI: DiagnosticInfoMIRParser(
222 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
223 return true;
224}
225
226bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
227 Context.diagnose(DI: DiagnosticInfoMIRParser(
228 DS_Error, SM.GetMessage(Loc, Kind: SourceMgr::DK_Error, Msg: Message)));
229 return true;
230}
231
232bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
233 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
234 reportDiagnostic(Diag: diagFromMIStringDiag(Error, SourceRange));
235 return true;
236}
237
238void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
239 DiagnosticSeverity Kind;
240 switch (Diag.getKind()) {
241 case SourceMgr::DK_Error:
242 Kind = DS_Error;
243 break;
244 case SourceMgr::DK_Warning:
245 Kind = DS_Warning;
246 break;
247 case SourceMgr::DK_Note:
248 Kind = DS_Note;
249 break;
250 case SourceMgr::DK_Remark:
251 llvm_unreachable("remark unexpected");
252 break;
253 }
254 Context.diagnose(DI: DiagnosticInfoMIRParser(Kind, Diag));
255}
256
257std::unique_ptr<Module>
258MIRParserImpl::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) {
259 if (!In.setCurrentDocument()) {
260 if (In.error())
261 return nullptr;
262 // Create an empty module when the MIR file is empty.
263 NoMIRDocuments = true;
264 auto M = std::make_unique<Module>(args&: Filename, args&: Context);
265 if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple().str(),
266 M->getDataLayoutStr()))
267 M->setDataLayout(*LayoutOverride);
268 return M;
269 }
270
271 std::unique_ptr<Module> M;
272 // Parse the block scalar manually so that we can return unique pointer
273 // without having to go trough YAML traits.
274 if (const auto *BSN =
275 dyn_cast_or_null<yaml::BlockScalarNode>(Val: In.getCurrentNode())) {
276 SMDiagnostic Error;
277 M = parseAssembly(F: MemoryBufferRef(BSN->getValue(), Filename), Err&: Error,
278 Context, Slots: &IRSlots, DataLayoutCallback);
279 if (!M) {
280 reportDiagnostic(Diag: diagFromBlockStringDiag(Error, SourceRange: BSN->getSourceRange()));
281 return nullptr;
282 }
283 In.nextDocument();
284 if (!In.setCurrentDocument())
285 NoMIRDocuments = true;
286 } else {
287 // Create an new, empty module.
288 M = std::make_unique<Module>(args&: Filename, args&: Context);
289 if (auto LayoutOverride = DataLayoutCallback(M->getTargetTriple().str(),
290 M->getDataLayoutStr()))
291 M->setDataLayout(*LayoutOverride);
292 NoLLVMIR = true;
293 }
294 return M;
295}
296
297bool MIRParserImpl::parseMachineFunctions(Module &M, MachineModuleInfo &MMI,
298 ModuleAnalysisManager *MAM) {
299 if (NoMIRDocuments)
300 return false;
301
302 // Parse the machine functions.
303 auto FirstUnvisitedFunction = M.begin();
304 do {
305 if (parseMachineFunction(M, MMI, FAM: MAM, FirstUnvisitedFunction))
306 return true;
307 In.nextDocument();
308 } while (In.setCurrentDocument());
309
310 return false;
311}
312
313Function *MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
314 auto &Context = M.getContext();
315 Function *F =
316 Function::Create(Ty: FunctionType::get(Result: Type::getVoidTy(C&: Context), isVarArg: false),
317 Linkage: Function::ExternalLinkage, N: Name, M);
318 BasicBlock *BB = BasicBlock::Create(Context, Name: "entry", Parent: F);
319 new UnreachableInst(Context, BB);
320
321 if (ProcessIRFunction)
322 ProcessIRFunction(*F);
323
324 return F;
325}
326
327static Function *
328getNextUnusedUnnamedFunction(const Module &M,
329 Module::iterator &FirstUnvisitedFunction) {
330 for (; FirstUnvisitedFunction != M.end(); ++FirstUnvisitedFunction)
331 if (!FirstUnvisitedFunction->hasName())
332 return &*FirstUnvisitedFunction++;
333
334 return nullptr;
335}
336
337bool MIRParserImpl::parseMachineFunction(
338 Module &M, MachineModuleInfo &MMI, ModuleAnalysisManager *MAM,
339 Module::iterator &FirstUnvisitedFunction) {
340 // Parse the yaml.
341 yaml::MachineFunction YamlMF;
342 yaml::EmptyContext Ctx;
343
344 const TargetMachine &TM = MMI.getTarget();
345 YamlMF.MachineFuncInfo = std::unique_ptr<yaml::MachineFunctionInfo>(
346 TM.createDefaultFuncInfoYAML());
347
348 yaml::yamlize(io&: In, Val&: YamlMF, false, Ctx);
349 if (In.error())
350 return true;
351
352 // Search for the corresponding IR function.
353 StringRef FunctionName = YamlMF.Name;
354 Function *F = M.getFunction(Name: FunctionName);
355 if (!F) {
356 if (NoLLVMIR) {
357 F = createDummyFunction(Name: FunctionName, M);
358 } else if (!FunctionName.empty() ||
359 !(F = getNextUnusedUnnamedFunction(M, FirstUnvisitedFunction))) {
360 return error(Message: Twine("function '") + FunctionName +
361 "' isn't defined in the provided LLVM IR");
362 }
363 }
364
365 if (!MAM) {
366 if (MMI.getMachineFunction(F: *F) != nullptr)
367 return error(Message: Twine("redefinition of machine function '") + FunctionName +
368 "'");
369
370 // Create the MachineFunction.
371 MachineFunction &MF = MMI.getOrCreateMachineFunction(F&: *F);
372 if (initializeMachineFunction(YamlMF, MF))
373 return true;
374 } else {
375 auto &FAM =
376 MAM->getResult<FunctionAnalysisManagerModuleProxy>(IR&: M).getManager();
377 if (FAM.getCachedResult<MachineFunctionAnalysis>(IR&: *F))
378 return error(Message: Twine("redefinition of machine function '") + FunctionName +
379 "'");
380
381 // Create the MachineFunction.
382 MachineFunction &MF = FAM.getResult<MachineFunctionAnalysis>(IR&: *F).getMF();
383 if (initializeMachineFunction(YamlMF, MF))
384 return true;
385 }
386
387 return false;
388}
389
390static bool isSSA(const MachineFunction &MF) {
391 const MachineRegisterInfo &MRI = MF.getRegInfo();
392 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
393 Register Reg = Register::index2VirtReg(Index: I);
394 if (!MRI.hasOneDef(RegNo: Reg) && !MRI.def_empty(RegNo: Reg))
395 return false;
396
397 // Subregister defs are invalid in SSA.
398 const MachineOperand *RegDef = MRI.getOneDef(Reg);
399 if (RegDef && RegDef->getSubReg() != 0)
400 return false;
401 }
402 return true;
403}
404
405bool MIRParserImpl::computeFunctionProperties(
406 MachineFunction &MF, const yaml::MachineFunction &YamlMF) {
407 MachineFunctionProperties &Properties = MF.getProperties();
408
409 bool HasPHI = false;
410 bool HasInlineAsm = false;
411 bool HasFakeUses = false;
412 bool AllTiedOpsRewritten = true, HasTiedOps = false;
413 for (const MachineBasicBlock &MBB : MF) {
414 for (const MachineInstr &MI : MBB) {
415 if (MI.isPHI())
416 HasPHI = true;
417 if (MI.isInlineAsm())
418 HasInlineAsm = true;
419 if (MI.isFakeUse())
420 HasFakeUses = true;
421 for (unsigned I = 0; I < MI.getNumOperands(); ++I) {
422 const MachineOperand &MO = MI.getOperand(i: I);
423 if (!MO.isReg() || !MO.getReg())
424 continue;
425 unsigned DefIdx;
426 if (MO.isUse() && MI.isRegTiedToDefOperand(UseOpIdx: I, DefOpIdx: &DefIdx)) {
427 HasTiedOps = true;
428 if (MO.getReg() != MI.getOperand(i: DefIdx).getReg())
429 AllTiedOpsRewritten = false;
430 }
431 }
432 }
433 }
434
435 // Helper function to sanity-check and set properties that are computed, but
436 // may be explicitly set from the input MIR
437 auto ComputedPropertyHelper =
438 [&Properties](std::optional<bool> ExplicitProp, bool ComputedProp,
439 MachineFunctionProperties::Property P) -> bool {
440 // Prefer explicitly given values over the computed properties
441 if (ExplicitProp.value_or(u&: ComputedProp))
442 Properties.set(P);
443 else
444 Properties.reset(P);
445
446 // Check for conflict between the explicit values and the computed ones
447 return ExplicitProp && *ExplicitProp && !ComputedProp;
448 };
449
450 if (ComputedPropertyHelper(YamlMF.NoPHIs, !HasPHI,
451 MachineFunctionProperties::Property::NoPHIs)) {
452 return error(Message: MF.getName() +
453 " has explicit property NoPhi, but contains at least one PHI");
454 }
455
456 MF.setHasInlineAsm(HasInlineAsm);
457
458 if (HasTiedOps && AllTiedOpsRewritten)
459 Properties.setTiedOpsRewritten();
460
461 if (ComputedPropertyHelper(YamlMF.IsSSA, isSSA(MF),
462 MachineFunctionProperties::Property::IsSSA)) {
463 return error(Message: MF.getName() +
464 " has explicit property IsSSA, but is not valid SSA");
465 }
466
467 const MachineRegisterInfo &MRI = MF.getRegInfo();
468 if (ComputedPropertyHelper(YamlMF.NoVRegs, MRI.getNumVirtRegs() == 0,
469 MachineFunctionProperties::Property::NoVRegs)) {
470 return error(
471 Message: MF.getName() +
472 " has explicit property NoVRegs, but contains virtual registers");
473 }
474
475 // For hasFakeUses we follow similar logic to the ComputedPropertyHelper,
476 // except for caring about the inverse case only, i.e. when the property is
477 // explicitly set to false and Fake Uses are present; having HasFakeUses=true
478 // on a function without fake uses is harmless.
479 if (YamlMF.HasFakeUses && !*YamlMF.HasFakeUses && HasFakeUses)
480 return error(
481 Message: MF.getName() +
482 " has explicit property hasFakeUses=false, but contains fake uses");
483 MF.setHasFakeUses(YamlMF.HasFakeUses.value_or(u&: HasFakeUses));
484
485 return false;
486}
487
488bool MIRParserImpl::parseMachineInst(MachineFunction &MF,
489 yaml::MachineInstrLoc MILoc,
490 MachineInstr const *&MI) {
491 if (MILoc.BlockNum >= MF.size()) {
492 return error(Message: Twine(MF.getName()) +
493 Twine(" instruction block out of range.") +
494 " Unable to reference bb:" + Twine(MILoc.BlockNum));
495 }
496 auto BB = std::next(x: MF.begin(), n: MILoc.BlockNum);
497 if (MILoc.Offset >= BB->size())
498 return error(
499 Message: Twine(MF.getName()) + Twine(" instruction offset out of range.") +
500 " Unable to reference instruction at bb: " + Twine(MILoc.BlockNum) +
501 " at offset:" + Twine(MILoc.Offset));
502 MI = &*std::next(x: BB->instr_begin(), n: MILoc.Offset);
503 return false;
504}
505
506bool MIRParserImpl::initializeCallSiteInfo(
507 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) {
508 MachineFunction &MF = PFS.MF;
509 SMDiagnostic Error;
510 const TargetMachine &TM = MF.getTarget();
511 for (auto &YamlCSInfo : YamlMF.CallSitesInfo) {
512 yaml::MachineInstrLoc MILoc = YamlCSInfo.CallLocation;
513 const MachineInstr *CallI;
514 if (parseMachineInst(MF, MILoc, MI&: CallI))
515 return true;
516 if (!CallI->isCall(Type: MachineInstr::IgnoreBundle))
517 return error(Message: Twine(MF.getName()) +
518 Twine(" call site info should reference call "
519 "instruction. Instruction at bb:") +
520 Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) +
521 " is not a call instruction");
522 MachineFunction::CallSiteInfo CSInfo;
523 for (auto ArgRegPair : YamlCSInfo.ArgForwardingRegs) {
524 Register Reg;
525 if (parseNamedRegisterReference(PFS, Reg, Src: ArgRegPair.Reg.Value, Error))
526 return error(Error, SourceRange: ArgRegPair.Reg.SourceRange);
527 CSInfo.ArgRegPairs.emplace_back(Args&: Reg, Args&: ArgRegPair.ArgNo);
528 }
529 if (!YamlCSInfo.CalleeTypeIds.empty()) {
530 for (auto CalleeTypeId : YamlCSInfo.CalleeTypeIds) {
531 IntegerType *Int64Ty = Type::getInt64Ty(C&: Context);
532 CSInfo.CalleeTypeIds.push_back(Elt: ConstantInt::get(Ty: Int64Ty, V: CalleeTypeId,
533 /*isSigned=*/IsSigned: false));
534 }
535 }
536
537 if (TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection)
538 MF.addCallSiteInfo(CallI: &*CallI, CallInfo: std::move(CSInfo));
539 }
540
541 if (!YamlMF.CallSitesInfo.empty() &&
542 !(TM.Options.EmitCallSiteInfo || TM.Options.EmitCallGraphSection))
543 return error(Message: "call site info provided but not used");
544 return false;
545}
546
547void MIRParserImpl::setupDebugValueTracking(
548 MachineFunction &MF, PerFunctionMIParsingState &PFS,
549 const yaml::MachineFunction &YamlMF) {
550 // Compute the value of the "next instruction number" field.
551 unsigned MaxInstrNum = 0;
552 for (auto &MBB : MF)
553 for (auto &MI : MBB)
554 MaxInstrNum = std::max(a: MI.peekDebugInstrNum(), b: MaxInstrNum);
555 MF.setDebugInstrNumberingCount(MaxInstrNum);
556
557 // Load any substitutions.
558 for (const auto &Sub : YamlMF.DebugValueSubstitutions) {
559 MF.makeDebugValueSubstitution({Sub.SrcInst, Sub.SrcOp},
560 {Sub.DstInst, Sub.DstOp}, SubReg: Sub.Subreg);
561 }
562
563 // Flag for whether we're supposed to be using DBG_INSTR_REF.
564 MF.setUseDebugInstrRef(YamlMF.UseDebugInstrRef);
565}
566
567bool
568MIRParserImpl::initializeMachineFunction(const yaml::MachineFunction &YamlMF,
569 MachineFunction &MF) {
570 // TODO: Recreate the machine function.
571 if (Target) {
572 // Avoid clearing state if we're using the same subtarget again.
573 Target->setTarget(MF.getSubtarget());
574 } else {
575 Target.reset(p: new PerTargetMIParsingState(MF.getSubtarget()));
576 }
577
578 MF.setAlignment(YamlMF.Alignment.valueOrOne());
579 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
580 MF.setHasWinCFI(YamlMF.HasWinCFI);
581
582 MF.setCallsEHReturn(YamlMF.CallsEHReturn);
583 MF.setCallsUnwindInit(YamlMF.CallsUnwindInit);
584 MF.setHasEHContTarget(YamlMF.HasEHContTarget);
585 MF.setHasEHScopes(YamlMF.HasEHScopes);
586 MF.setHasEHFunclets(YamlMF.HasEHFunclets);
587 MF.setIsOutlined(YamlMF.IsOutlined);
588
589 MachineFunctionProperties &Props = MF.getProperties();
590 if (YamlMF.Legalized)
591 Props.setLegalized();
592 if (YamlMF.RegBankSelected)
593 Props.setRegBankSelected();
594 if (YamlMF.Selected)
595 Props.setSelected();
596 if (YamlMF.FailedISel)
597 Props.setFailedISel();
598 if (YamlMF.FailsVerification)
599 Props.setFailsVerification();
600 if (YamlMF.TracksDebugUserValues)
601 Props.setTracksDebugUserValues();
602
603 PerFunctionMIParsingState PFS(MF, SM, IRSlots, *Target);
604 if (parseRegisterInfo(PFS, YamlMF))
605 return true;
606 if (initializePrefetchTargets(PFS, YamlMF))
607 return true;
608 if (!YamlMF.Constants.empty()) {
609 auto *ConstantPool = MF.getConstantPool();
610 assert(ConstantPool && "Constant pool must be created");
611 if (initializeConstantPool(PFS, ConstantPool&: *ConstantPool, YamlMF))
612 return true;
613 }
614 if (!YamlMF.MachineMetadataNodes.empty() &&
615 parseMachineMetadataNodes(PFS, MF, YMF: YamlMF))
616 return true;
617
618 StringRef BlockStr = YamlMF.Body.Value.Value;
619 SMDiagnostic Error;
620 SourceMgr BlockSM;
621 BlockSM.AddNewSourceBuffer(
622 F: MemoryBuffer::getMemBuffer(InputData: BlockStr, BufferName: "",/*RequiresNullTerminator=*/false),
623 IncludeLoc: SMLoc());
624 PFS.SM = &BlockSM;
625 if (parseMachineBasicBlockDefinitions(PFS, Src: BlockStr, Error)) {
626 reportDiagnostic(
627 Diag: diagFromBlockStringDiag(Error, SourceRange: YamlMF.Body.Value.SourceRange));
628 return true;
629 }
630 // Check Basic Block Section Flags.
631 if (MF.hasBBSections()) {
632 MF.assignBeginEndSections();
633 }
634 PFS.SM = &SM;
635
636 // Initialize the frame information after creating all the MBBs so that the
637 // MBB references in the frame information can be resolved.
638 if (initializeFrameInfo(PFS, YamlMF))
639 return true;
640 // Initialize the jump table after creating all the MBBs so that the MBB
641 // references can be resolved.
642 if (!YamlMF.JumpTableInfo.Entries.empty() &&
643 initializeJumpTableInfo(PFS, YamlJTI: YamlMF.JumpTableInfo))
644 return true;
645 // Parse the machine instructions after creating all of the MBBs so that the
646 // parser can resolve the MBB references.
647 StringRef InsnStr = YamlMF.Body.Value.Value;
648 SourceMgr InsnSM;
649 InsnSM.AddNewSourceBuffer(
650 F: MemoryBuffer::getMemBuffer(InputData: InsnStr, BufferName: "", /*RequiresNullTerminator=*/false),
651 IncludeLoc: SMLoc());
652 PFS.SM = &InsnSM;
653 if (parseMachineInstructions(PFS, Src: InsnStr, Error)) {
654 reportDiagnostic(
655 Diag: diagFromBlockStringDiag(Error, SourceRange: YamlMF.Body.Value.SourceRange));
656 return true;
657 }
658 PFS.SM = &SM;
659
660 if (setupRegisterInfo(PFS, YamlMF))
661 return true;
662
663 if (YamlMF.MachineFuncInfo) {
664 const TargetMachine &TM = MF.getTarget();
665 // Note this is called after the initial constructor of the
666 // MachineFunctionInfo based on the MachineFunction, which may depend on the
667 // IR.
668
669 SMRange SrcRange;
670 if (TM.parseMachineFunctionInfo(*YamlMF.MachineFuncInfo, PFS, Error,
671 SourceRange&: SrcRange)) {
672 return error(Error, SourceRange: SrcRange);
673 }
674 }
675
676 // Set the reserved registers after parsing MachineFuncInfo. The target may
677 // have been recording information used to select the reserved registers
678 // there.
679 // FIXME: This is a temporary workaround until the reserved registers can be
680 // serialized.
681 MachineRegisterInfo &MRI = MF.getRegInfo();
682 MRI.freezeReservedRegs();
683
684 if (computeFunctionProperties(MF, YamlMF))
685 return true;
686
687 if (initializeCallSiteInfo(PFS, YamlMF))
688 return true;
689
690 if (parseCalledGlobals(PFS, MF, YMF: YamlMF))
691 return true;
692
693 if (initializePrefetchTargets(PFS, YamlMF))
694 return true;
695
696 setupDebugValueTracking(MF, PFS, YamlMF);
697
698 MF.getSubtarget().mirFileLoaded(MF);
699
700 MF.verify(p: nullptr, Banner: nullptr, OS: &errs());
701 return false;
702}
703
704bool MIRParserImpl::initializePrefetchTargets(
705 PerFunctionMIParsingState &PFS, const yaml::MachineFunction &YamlMF) {
706 MachineFunction &MF = PFS.MF;
707 SMDiagnostic Error;
708 DenseMap<UniqueBBID, SmallVector<unsigned>> Targets;
709 for (const auto &YamlTarget : YamlMF.PrefetchTargets) {
710 CallsiteID Target;
711 if (llvm::parsePrefetchTarget(PFS, Target, Src: YamlTarget.Value, Error))
712 return error(Error, SourceRange: YamlTarget.SourceRange);
713 Targets[Target.BBID].push_back(Elt: Target.CallsiteIndex);
714 }
715 MF.setPrefetchTargets(Targets);
716 return false;
717}
718
719bool MIRParserImpl::parseRegisterInfo(PerFunctionMIParsingState &PFS,
720 const yaml::MachineFunction &YamlMF) {
721 MachineFunction &MF = PFS.MF;
722 MachineRegisterInfo &RegInfo = MF.getRegInfo();
723 assert(RegInfo.tracksLiveness());
724 if (!YamlMF.TracksRegLiveness)
725 RegInfo.invalidateLiveness();
726
727 SMDiagnostic Error;
728 // Parse the virtual register information.
729 for (const auto &VReg : YamlMF.VirtualRegisters) {
730 VRegInfo &Info = PFS.getVRegInfo(Num: VReg.ID.Value);
731 if (Info.Explicit)
732 return error(Loc: VReg.ID.SourceRange.Start,
733 Message: Twine("redefinition of virtual register '%") +
734 Twine(VReg.ID.Value) + "'");
735 Info.Explicit = true;
736
737 if (VReg.Class.Value == "_") {
738 Info.Kind = VRegInfo::GENERIC;
739 Info.D.RegBank = nullptr;
740 } else {
741 const auto *RC = Target->getRegClass(Name: VReg.Class.Value);
742 if (RC) {
743 Info.Kind = VRegInfo::NORMAL;
744 Info.D.RC = RC;
745 } else {
746 const RegisterBank *RegBank = Target->getRegBank(Name: VReg.Class.Value);
747 if (!RegBank)
748 return error(
749 Loc: VReg.Class.SourceRange.Start,
750 Message: Twine("use of undefined register class or register bank '") +
751 VReg.Class.Value + "'");
752 Info.Kind = VRegInfo::REGBANK;
753 Info.D.RegBank = RegBank;
754 }
755 }
756
757 if (!VReg.PreferredRegister.Value.empty()) {
758 if (Info.Kind != VRegInfo::NORMAL)
759 return error(Loc: VReg.Class.SourceRange.Start,
760 Message: Twine("preferred register can only be set for normal vregs"));
761
762 if (parseRegisterReference(PFS, Reg&: Info.PreferredReg,
763 Src: VReg.PreferredRegister.Value, Error))
764 return error(Error, SourceRange: VReg.PreferredRegister.SourceRange);
765 }
766
767 for (const auto &FlagStringValue : VReg.RegisterFlags) {
768 uint8_t FlagValue;
769 if (Target->getVRegFlagValue(FlagName: FlagStringValue.Value, FlagValue))
770 return error(Loc: FlagStringValue.SourceRange.Start,
771 Message: Twine("use of undefined register flag '") +
772 FlagStringValue.Value + "'");
773 Info.Flags |= FlagValue;
774 }
775 RegInfo.noteNewVirtualRegister(Reg: Info.VReg);
776 }
777
778 // Parse the liveins.
779 for (const auto &LiveIn : YamlMF.LiveIns) {
780 Register Reg;
781 if (parseNamedRegisterReference(PFS, Reg, Src: LiveIn.Register.Value, Error))
782 return error(Error, SourceRange: LiveIn.Register.SourceRange);
783 Register VReg;
784 if (!LiveIn.VirtualRegister.Value.empty()) {
785 VRegInfo *Info;
786 if (parseVirtualRegisterReference(PFS, Info, Src: LiveIn.VirtualRegister.Value,
787 Error))
788 return error(Error, SourceRange: LiveIn.VirtualRegister.SourceRange);
789 VReg = Info->VReg;
790 }
791 RegInfo.addLiveIn(Reg, vreg: VReg);
792 }
793
794 // Parse the callee saved registers (Registers that will
795 // be saved for the caller).
796 if (YamlMF.CalleeSavedRegisters) {
797 SmallVector<MCPhysReg, 16> CalleeSavedRegisters;
798 for (const auto &RegSource : *YamlMF.CalleeSavedRegisters) {
799 Register Reg;
800 if (parseNamedRegisterReference(PFS, Reg, Src: RegSource.Value, Error))
801 return error(Error, SourceRange: RegSource.SourceRange);
802 CalleeSavedRegisters.push_back(Elt: Reg.id());
803 }
804 RegInfo.setCalleeSavedRegs(CalleeSavedRegisters);
805 }
806
807 // Stash any VirtRegMap state on MRI.
808 // VirtRegMap::init() will use that information to get pre-populated
809 // on the first analysis run.
810 for (const auto &VReg : YamlMF.VirtualRegisters) {
811 if (VReg.SplitFrom.Value.empty() && VReg.AssignedPhys.Value.empty())
812 continue;
813
814 auto It = PFS.VRegInfos.find(Val: VReg.ID.Value);
815 if (It == PFS.VRegInfos.end())
816 continue;
817 Register ChildReg = It->second->VReg;
818
819 MachineRegisterInfo::PendingVirtRegMapEntry Pending;
820 Pending.VReg = ChildReg;
821
822 if (!VReg.SplitFrom.Value.empty()) {
823 VRegInfo *Parent = nullptr;
824 if (parseVirtualRegisterReference(PFS, Info&: Parent, Src: VReg.SplitFrom.Value,
825 Error))
826 return error(Error, SourceRange: VReg.SplitFrom.SourceRange);
827 if (Parent->VReg == ChildReg)
828 return error(Loc: VReg.SplitFrom.SourceRange.Start,
829 Message: Twine("'split-from' references the same vreg as 'id' (%") +
830 Twine(VReg.ID.Value) + ")");
831 Pending.SplitFrom = Parent->VReg;
832 }
833 if (!VReg.AssignedPhys.Value.empty()) {
834 Register Phys;
835 if (parseRegisterReference(PFS, Reg&: Phys, Src: VReg.AssignedPhys.Value, Error))
836 return error(Error, SourceRange: VReg.AssignedPhys.SourceRange);
837 if (!Phys.isPhysical())
838 return error(
839 Loc: VReg.AssignedPhys.SourceRange.Start,
840 Message: Twine("'assigned-phys' must be a physical register, got '") +
841 VReg.AssignedPhys.Value + "'");
842 Pending.AssignedPhys = Phys.asMCReg();
843 }
844 RegInfo.addPendingVirtRegMapEntry(Entry: Pending);
845 }
846
847 return false;
848}
849
850bool MIRParserImpl::setupRegisterInfo(const PerFunctionMIParsingState &PFS,
851 const yaml::MachineFunction &YamlMF) {
852 MachineFunction &MF = PFS.MF;
853 MachineRegisterInfo &MRI = MF.getRegInfo();
854 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
855
856 SmallVector<std::string> Errors;
857
858 // Create VRegs
859 auto populateVRegInfo = [&](const VRegInfo &Info, const Twine &Name) {
860 Register Reg = Info.VReg;
861 switch (Info.Kind) {
862 case VRegInfo::UNKNOWN:
863 Errors.push_back(
864 Elt: (Twine("Cannot determine class/bank of virtual register ") + Name +
865 " in function '" + MF.getName() + "'")
866 .str());
867 break;
868 case VRegInfo::NORMAL:
869 if (!Info.D.RC->isAllocatable()) {
870 Errors.push_back(Elt: (Twine("Cannot use non-allocatable class '") +
871 TRI->getRegClassName(Class: Info.D.RC) +
872 "' for virtual register " + Name + " in function '" +
873 MF.getName() + "'")
874 .str());
875 break;
876 }
877
878 MRI.setRegClass(Reg, RC: Info.D.RC);
879 if (Info.PreferredReg != 0)
880 MRI.setSimpleHint(VReg: Reg, PrefReg: Info.PreferredReg);
881 break;
882 case VRegInfo::GENERIC:
883 break;
884 case VRegInfo::REGBANK:
885 MRI.setRegBank(Reg, RegBank: *Info.D.RegBank);
886 break;
887 }
888 };
889
890 for (const auto &P : PFS.VRegInfosNamed) {
891 const VRegInfo &Info = *P.second;
892 populateVRegInfo(Info, Twine(P.first()));
893 }
894
895 for (auto P : PFS.VRegInfos) {
896 const VRegInfo &Info = *P.second;
897 populateVRegInfo(Info, Twine(P.first.id()));
898 }
899
900 // Compute MachineRegisterInfo::UsedPhysRegMask
901 for (const MachineBasicBlock &MBB : MF) {
902 // Make sure MRI knows about registers clobbered by unwinder.
903 if (MBB.isEHPad())
904 if (auto *RegMask = TRI->getCustomEHPadPreservedMask(MF))
905 MRI.addPhysRegsUsedFromRegMask(RegMask);
906
907 for (const MachineInstr &MI : MBB) {
908 for (const MachineOperand &MO : MI.operands()) {
909 if (!MO.isRegMask())
910 continue;
911 MRI.addPhysRegsUsedFromRegMask(RegMask: MO.getRegMask());
912 }
913 }
914 }
915
916 if (Errors.empty())
917 return false;
918
919 // Report errors in a deterministic order.
920 sort(C&: Errors);
921 for (auto &E : Errors)
922 error(Message: E);
923 return true;
924}
925
926bool MIRParserImpl::initializeFrameInfo(PerFunctionMIParsingState &PFS,
927 const yaml::MachineFunction &YamlMF) {
928 MachineFunction &MF = PFS.MF;
929 MachineFrameInfo &MFI = MF.getFrameInfo();
930 const TargetFrameLowering *TFI = MF.getSubtarget().getFrameLowering();
931 const Function &F = MF.getFunction();
932 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
933 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
934 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
935 MFI.setHasStackMap(YamlMFI.HasStackMap);
936 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
937 MFI.setStackSize(YamlMFI.StackSize);
938 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
939 if (YamlMFI.MaxAlignment)
940 MFI.ensureMaxAlignment(Alignment: Align(YamlMFI.MaxAlignment));
941 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
942 MFI.setHasCalls(YamlMFI.HasCalls);
943 if (YamlMFI.FramePointerPolicy != FramePointerKind::None)
944 MFI.setFramePointerPolicy(YamlMFI.FramePointerPolicy);
945 if (YamlMFI.MaxCallFrameSize != ~0u)
946 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
947 MFI.setCVBytesOfCalleeSavedRegisters(YamlMFI.CVBytesOfCalleeSavedRegisters);
948 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
949 MFI.setHasVAStart(YamlMFI.HasVAStart);
950 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
951 MFI.setHasTailCall(YamlMFI.HasTailCall);
952 MFI.setCalleeSavedInfoValid(YamlMFI.IsCalleeSavedInfoValid);
953 MFI.setLocalFrameSize(YamlMFI.LocalFrameSize);
954 llvm::SaveRestorePoints SavePoints;
955 if (initializeSaveRestorePoints(PFS, YamlSRPoints: YamlMFI.SavePoints, SaveRestorePoints&: SavePoints))
956 return true;
957 MFI.setSavePoints(SavePoints);
958 llvm::SaveRestorePoints RestorePoints;
959 if (initializeSaveRestorePoints(PFS, YamlSRPoints: YamlMFI.RestorePoints, SaveRestorePoints&: RestorePoints))
960 return true;
961 MFI.setRestorePoints(RestorePoints);
962
963 std::vector<CalleeSavedInfo> CSIInfo;
964 // Initialize the fixed frame objects.
965 for (const auto &Object : YamlMF.FixedStackObjects) {
966 int ObjectIdx;
967 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
968 ObjectIdx = MFI.CreateFixedObject(Size: Object.Size, SPOffset: Object.Offset,
969 IsImmutable: Object.IsImmutable, isAliased: Object.IsAliased);
970 else
971 ObjectIdx = MFI.CreateFixedSpillStackObject(Size: Object.Size, SPOffset: Object.Offset);
972
973 if (!TFI->isSupportedStackID(ID: Object.StackID))
974 return error(Loc: Object.ID.SourceRange.Start,
975 Message: Twine("StackID is not supported by target"));
976 MFI.setStackID(ObjectIdx, ID: Object.StackID);
977 MFI.setObjectAlignment(ObjectIdx, Alignment: Object.Alignment.valueOrOne());
978 if (!PFS.FixedStackObjectSlots.insert(KV: std::make_pair(x: Object.ID.Value,
979 y&: ObjectIdx))
980 .second)
981 return error(Loc: Object.ID.SourceRange.Start,
982 Message: Twine("redefinition of fixed stack object '%fixed-stack.") +
983 Twine(Object.ID.Value) + "'");
984 if (parseCalleeSavedRegister(PFS, CSIInfo, RegisterSource: Object.CalleeSavedRegister,
985 IsRestored: Object.CalleeSavedRestored, FrameIdx: ObjectIdx))
986 return true;
987 if (parseStackObjectsDebugInfo(PFS, Object, FrameIdx: ObjectIdx))
988 return true;
989 }
990
991 for (const auto &Object : YamlMF.EntryValueObjects) {
992 SMDiagnostic Error;
993 Register Reg;
994 if (parseNamedRegisterReference(PFS, Reg, Src: Object.EntryValueRegister.Value,
995 Error))
996 return error(Error, SourceRange: Object.EntryValueRegister.SourceRange);
997 if (!Reg.isPhysical())
998 return error(Loc: Object.EntryValueRegister.SourceRange.Start,
999 Message: "Expected physical register for entry value field");
1000 std::optional<VarExprLoc> MaybeInfo = parseVarExprLoc(
1001 PFS, VarStr: Object.DebugVar, ExprStr: Object.DebugExpr, LocStr: Object.DebugLoc);
1002 if (!MaybeInfo)
1003 return true;
1004 if (MaybeInfo->DIVar || MaybeInfo->DIExpr || MaybeInfo->DILoc)
1005 PFS.MF.setVariableDbgInfo(Var: MaybeInfo->DIVar, Expr: MaybeInfo->DIExpr,
1006 Reg: Reg.asMCReg(), Loc: MaybeInfo->DILoc);
1007 }
1008
1009 // Initialize the ordinary frame objects.
1010 for (const auto &Object : YamlMF.StackObjects) {
1011 int ObjectIdx;
1012 const AllocaInst *Alloca = nullptr;
1013 const yaml::StringValue &Name = Object.Name;
1014 if (!Name.Value.empty()) {
1015 Alloca = dyn_cast_or_null<AllocaInst>(
1016 Val: F.getValueSymbolTable()->lookup(Name: Name.Value));
1017 if (!Alloca)
1018 return error(Loc: Name.SourceRange.Start,
1019 Message: "alloca instruction named '" + Name.Value +
1020 "' isn't defined in the function '" + F.getName() +
1021 "'");
1022 }
1023 if (!TFI->isSupportedStackID(ID: Object.StackID))
1024 return error(Loc: Object.ID.SourceRange.Start,
1025 Message: Twine("StackID is not supported by target"));
1026 if (Object.Type == yaml::MachineStackObject::VariableSized)
1027 ObjectIdx =
1028 MFI.CreateVariableSizedObject(Alignment: Object.Alignment.valueOrOne(), Alloca);
1029 else
1030 ObjectIdx = MFI.CreateStackObject(
1031 Size: Object.Size, Alignment: Object.Alignment.valueOrOne(),
1032 isSpillSlot: Object.Type == yaml::MachineStackObject::SpillSlot, Alloca,
1033 ID: Object.StackID);
1034 MFI.setObjectOffset(ObjectIdx, SPOffset: Object.Offset);
1035
1036 if (!PFS.StackObjectSlots.insert(KV: std::make_pair(x: Object.ID.Value, y&: ObjectIdx))
1037 .second)
1038 return error(Loc: Object.ID.SourceRange.Start,
1039 Message: Twine("redefinition of stack object '%stack.") +
1040 Twine(Object.ID.Value) + "'");
1041 if (parseCalleeSavedRegister(PFS, CSIInfo, RegisterSource: Object.CalleeSavedRegister,
1042 IsRestored: Object.CalleeSavedRestored, FrameIdx: ObjectIdx))
1043 return true;
1044 if (Object.LocalOffset)
1045 MFI.mapLocalFrameObject(ObjectIndex: ObjectIdx, Offset: *Object.LocalOffset);
1046 if (parseStackObjectsDebugInfo(PFS, Object, FrameIdx: ObjectIdx))
1047 return true;
1048 }
1049 MFI.setCalleeSavedInfo(CSIInfo);
1050 if (!CSIInfo.empty())
1051 MFI.setCalleeSavedInfoValid(true);
1052
1053 // Initialize the various stack object references after initializing the
1054 // stack objects.
1055 if (!YamlMFI.StackProtector.Value.empty()) {
1056 SMDiagnostic Error;
1057 int FI;
1058 if (parseStackObjectReference(PFS, FI, Src: YamlMFI.StackProtector.Value, Error))
1059 return error(Error, SourceRange: YamlMFI.StackProtector.SourceRange);
1060 MFI.setStackProtectorIndex(FI);
1061 }
1062
1063 if (!YamlMFI.FunctionContext.Value.empty()) {
1064 SMDiagnostic Error;
1065 int FI;
1066 if (parseStackObjectReference(PFS, FI, Src: YamlMFI.FunctionContext.Value, Error))
1067 return error(Error, SourceRange: YamlMFI.FunctionContext.SourceRange);
1068 MFI.setFunctionContextIndex(FI);
1069 }
1070
1071 return false;
1072}
1073
1074bool MIRParserImpl::parseCalleeSavedRegister(PerFunctionMIParsingState &PFS,
1075 std::vector<CalleeSavedInfo> &CSIInfo,
1076 const yaml::StringValue &RegisterSource, bool IsRestored, int FrameIdx) {
1077 if (RegisterSource.Value.empty())
1078 return false;
1079 Register Reg;
1080 SMDiagnostic Error;
1081 if (parseNamedRegisterReference(PFS, Reg, Src: RegisterSource.Value, Error))
1082 return error(Error, SourceRange: RegisterSource.SourceRange);
1083 CalleeSavedInfo CSI(Reg, FrameIdx);
1084 CSI.setRestored(IsRestored);
1085 CSIInfo.push_back(x: CSI);
1086 return false;
1087}
1088
1089/// Verify that given node is of a certain type. Return true on error.
1090template <typename T>
1091static bool typecheckMDNode(T *&Result, MDNode *Node,
1092 const yaml::StringValue &Source,
1093 StringRef TypeString, MIRParserImpl &Parser) {
1094 if (!Node)
1095 return false;
1096 Result = dyn_cast<T>(Node);
1097 if (!Result)
1098 return Parser.error(Loc: Source.SourceRange.Start,
1099 Message: "expected a reference to a '" + TypeString +
1100 "' metadata node");
1101 return false;
1102}
1103
1104std::optional<MIRParserImpl::VarExprLoc> MIRParserImpl::parseVarExprLoc(
1105 PerFunctionMIParsingState &PFS, const yaml::StringValue &VarStr,
1106 const yaml::StringValue &ExprStr, const yaml::StringValue &LocStr) {
1107 MDNode *Var = nullptr;
1108 MDNode *Expr = nullptr;
1109 MDNode *Loc = nullptr;
1110 if (parseMDNode(PFS, Node&: Var, Source: VarStr) || parseMDNode(PFS, Node&: Expr, Source: ExprStr) ||
1111 parseMDNode(PFS, Node&: Loc, Source: LocStr))
1112 return std::nullopt;
1113 DILocalVariable *DIVar = nullptr;
1114 DIExpression *DIExpr = nullptr;
1115 DILocation *DILoc = nullptr;
1116 if (typecheckMDNode(Result&: DIVar, Node: Var, Source: VarStr, TypeString: "DILocalVariable", Parser&: *this) ||
1117 typecheckMDNode(Result&: DIExpr, Node: Expr, Source: ExprStr, TypeString: "DIExpression", Parser&: *this) ||
1118 typecheckMDNode(Result&: DILoc, Node: Loc, Source: LocStr, TypeString: "DILocation", Parser&: *this))
1119 return std::nullopt;
1120 return VarExprLoc{.DIVar: DIVar, .DIExpr: DIExpr, .DILoc: DILoc};
1121}
1122
1123template <typename T>
1124bool MIRParserImpl::parseStackObjectsDebugInfo(PerFunctionMIParsingState &PFS,
1125 const T &Object, int FrameIdx) {
1126 std::optional<VarExprLoc> MaybeInfo =
1127 parseVarExprLoc(PFS, VarStr: Object.DebugVar, ExprStr: Object.DebugExpr, LocStr: Object.DebugLoc);
1128 if (!MaybeInfo)
1129 return true;
1130 // Debug information can only be attached to stack objects; Fixed stack
1131 // objects aren't supported.
1132 if (MaybeInfo->DIVar || MaybeInfo->DIExpr || MaybeInfo->DILoc)
1133 PFS.MF.setVariableDbgInfo(Var: MaybeInfo->DIVar, Expr: MaybeInfo->DIExpr, Slot: FrameIdx,
1134 Loc: MaybeInfo->DILoc);
1135 return false;
1136}
1137
1138bool MIRParserImpl::parseMDNode(PerFunctionMIParsingState &PFS,
1139 MDNode *&Node, const yaml::StringValue &Source) {
1140 if (Source.Value.empty())
1141 return false;
1142 SMDiagnostic Error;
1143 if (llvm::parseMDNode(PFS, Node, Src: Source.Value, Error))
1144 return error(Error, SourceRange: Source.SourceRange);
1145 return false;
1146}
1147
1148bool MIRParserImpl::initializeConstantPool(PerFunctionMIParsingState &PFS,
1149 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF) {
1150 DenseMap<unsigned, unsigned> &ConstantPoolSlots = PFS.ConstantPoolSlots;
1151 const MachineFunction &MF = PFS.MF;
1152 const auto &M = *MF.getFunction().getParent();
1153 SMDiagnostic Error;
1154 for (const auto &YamlConstant : YamlMF.Constants) {
1155 if (YamlConstant.IsTargetSpecific)
1156 // FIXME: Support target-specific constant pools
1157 return error(Loc: YamlConstant.Value.SourceRange.Start,
1158 Message: "Can't parse target-specific constant pool entries yet");
1159 const Constant *Value = dyn_cast_or_null<Constant>(
1160 Val: parseConstantValue(Asm: YamlConstant.Value.Value, Err&: Error, M));
1161 if (!Value)
1162 return error(Error, SourceRange: YamlConstant.Value.SourceRange);
1163 const Align PrefTypeAlign =
1164 M.getDataLayout().getPrefTypeAlign(Ty: Value->getType());
1165 const Align Alignment = YamlConstant.Alignment.value_or(u: PrefTypeAlign);
1166 unsigned Index = ConstantPool.getConstantPoolIndex(C: Value, Alignment);
1167 if (!ConstantPoolSlots.insert(KV: std::make_pair(x: YamlConstant.ID.Value, y&: Index))
1168 .second)
1169 return error(Loc: YamlConstant.ID.SourceRange.Start,
1170 Message: Twine("redefinition of constant pool item '%const.") +
1171 Twine(YamlConstant.ID.Value) + "'");
1172 }
1173 return false;
1174}
1175
1176// Return true if basic block was incorrectly specified in MIR
1177bool MIRParserImpl::initializeSaveRestorePoints(
1178 PerFunctionMIParsingState &PFS,
1179 const std::vector<yaml::SaveRestorePointEntry> &YamlSRPoints,
1180 llvm::SaveRestorePoints &SaveRestorePoints) {
1181 SMDiagnostic Error;
1182 MachineBasicBlock *MBB = nullptr;
1183 for (const yaml::SaveRestorePointEntry &Entry : YamlSRPoints) {
1184 if (parseMBBReference(PFS, MBB, Source: Entry.Point.Value))
1185 return true;
1186
1187 std::vector<CalleeSavedInfo> Registers;
1188 for (auto &RegStr : Entry.Registers) {
1189 Register Reg;
1190 if (parseNamedRegisterReference(PFS, Reg, Src: RegStr.Value, Error))
1191 return error(Error, SourceRange: RegStr.SourceRange);
1192 Registers.push_back(x: CalleeSavedInfo(Reg));
1193 }
1194 SaveRestorePoints.try_emplace(Key: MBB, Args: std::move(Registers));
1195 }
1196 return false;
1197}
1198
1199bool MIRParserImpl::initializeJumpTableInfo(PerFunctionMIParsingState &PFS,
1200 const yaml::MachineJumpTable &YamlJTI) {
1201 MachineJumpTableInfo *JTI = PFS.MF.getOrCreateJumpTableInfo(JTEntryKind: YamlJTI.Kind);
1202 for (const auto &Entry : YamlJTI.Entries) {
1203 std::vector<MachineBasicBlock *> Blocks;
1204 for (const auto &MBBSource : Entry.Blocks) {
1205 MachineBasicBlock *MBB = nullptr;
1206 if (parseMBBReference(PFS, MBB, Source: MBBSource.Value))
1207 return true;
1208 Blocks.push_back(x: MBB);
1209 }
1210 unsigned Index = JTI->createJumpTableIndex(DestBBs: Blocks);
1211 if (!PFS.JumpTableSlots.insert(KV: std::make_pair(x: Entry.ID.Value, y&: Index))
1212 .second)
1213 return error(Loc: Entry.ID.SourceRange.Start,
1214 Message: Twine("redefinition of jump table entry '%jump-table.") +
1215 Twine(Entry.ID.Value) + "'");
1216 }
1217 return false;
1218}
1219
1220bool MIRParserImpl::parseMBBReference(PerFunctionMIParsingState &PFS,
1221 MachineBasicBlock *&MBB,
1222 const yaml::StringValue &Source) {
1223 SMDiagnostic Error;
1224 if (llvm::parseMBBReference(PFS, MBB, Src: Source.Value, Error))
1225 return error(Error, SourceRange: Source.SourceRange);
1226 return false;
1227}
1228
1229bool MIRParserImpl::parseMachineMetadata(PerFunctionMIParsingState &PFS,
1230 const yaml::StringValue &Source) {
1231 SMDiagnostic Error;
1232 if (llvm::parseMachineMetadata(PFS, Src: Source.Value, SourceRange: Source.SourceRange, Error))
1233 return error(Error, SourceRange: Source.SourceRange);
1234 return false;
1235}
1236
1237bool MIRParserImpl::parseMachineMetadataNodes(
1238 PerFunctionMIParsingState &PFS, MachineFunction &MF,
1239 const yaml::MachineFunction &YMF) {
1240 for (const auto &MDS : YMF.MachineMetadataNodes) {
1241 if (parseMachineMetadata(PFS, Source: MDS))
1242 return true;
1243 }
1244 // Report missing definitions from forward referenced nodes.
1245 if (!PFS.MachineForwardRefMDNodes.empty())
1246 return error(Loc: PFS.MachineForwardRefMDNodes.begin()->second.second,
1247 Message: "use of undefined metadata '!" +
1248 Twine(PFS.MachineForwardRefMDNodes.begin()->first) + "'");
1249 return false;
1250}
1251
1252bool MIRParserImpl::parseCalledGlobals(PerFunctionMIParsingState &PFS,
1253 MachineFunction &MF,
1254 const yaml::MachineFunction &YMF) {
1255 Function &F = MF.getFunction();
1256 for (const auto &YamlCG : YMF.CalledGlobals) {
1257 yaml::MachineInstrLoc MILoc = YamlCG.CallSite;
1258 const MachineInstr *CallI;
1259 if (parseMachineInst(MF, MILoc, MI&: CallI))
1260 return true;
1261 if (!CallI->isCall(Type: MachineInstr::IgnoreBundle))
1262 return error(Message: Twine(MF.getName()) +
1263 Twine(" called global should reference call "
1264 "instruction. Instruction at bb:") +
1265 Twine(MILoc.BlockNum) + " at offset:" + Twine(MILoc.Offset) +
1266 " is not a call instruction");
1267
1268 auto Callee =
1269 F.getParent()->getValueSymbolTable().lookup(Name: YamlCG.Callee.Value);
1270 if (!Callee)
1271 return error(Loc: YamlCG.Callee.SourceRange.Start,
1272 Message: "use of undefined global '" + YamlCG.Callee.Value + "'");
1273 if (!isa<GlobalValue>(Val: Callee))
1274 return error(Loc: YamlCG.Callee.SourceRange.Start,
1275 Message: "use of non-global value '" + YamlCG.Callee.Value + "'");
1276
1277 MF.addCalledGlobal(MI: CallI, Details: {.Callee: cast<GlobalValue>(Val: Callee), .TargetFlags: YamlCG.Flags});
1278 }
1279
1280 return false;
1281}
1282
1283SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
1284 SMRange SourceRange) {
1285 assert(SourceRange.isValid() && "Invalid source range");
1286 SMLoc Loc = SourceRange.Start;
1287 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
1288 *Loc.getPointer() == '\'';
1289 // Translate the location of the error from the location in the MI string to
1290 // the corresponding location in the MIR file.
1291 Loc = Loc.getFromPointer(Ptr: Loc.getPointer() + Error.getColumnNo() +
1292 (HasQuote ? 1 : 0));
1293
1294 // TODO: Translate any source ranges as well.
1295 return SM.GetMessage(Loc, Kind: Error.getKind(), Msg: Error.getMessage(), Ranges: {},
1296 FixIts: Error.getFixIts());
1297}
1298
1299SMDiagnostic MIRParserImpl::diagFromBlockStringDiag(const SMDiagnostic &Error,
1300 SMRange SourceRange) {
1301 assert(SourceRange.isValid());
1302
1303 // Translate the location of the error from the location in the llvm IR string
1304 // to the corresponding location in the MIR file.
1305 auto LineAndColumn = SM.getLineAndColumn(Loc: SourceRange.Start);
1306 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
1307 unsigned Column = Error.getColumnNo();
1308 StringRef LineStr = Error.getLineContents();
1309 SMLoc Loc = Error.getLoc();
1310
1311 // Get the full line and adjust the column number by taking the indentation of
1312 // LLVM IR into account.
1313 for (line_iterator L(*SM.getMemoryBuffer(i: SM.getMainFileID()), false), E;
1314 L != E; ++L) {
1315 if (L.line_number() == Line) {
1316 LineStr = *L;
1317 Loc = SMLoc::getFromPointer(Ptr: LineStr.data());
1318 auto Indent = LineStr.find(Str: Error.getLineContents());
1319 if (Indent != StringRef::npos)
1320 Column += Indent;
1321 break;
1322 }
1323 }
1324
1325 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
1326 Error.getMessage(), LineStr, Error.getRanges(),
1327 Error.getFixIts());
1328}
1329
1330MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
1331 : Impl(std::move(Impl)) {}
1332
1333MIRParser::~MIRParser() = default;
1334
1335std::unique_ptr<Module>
1336MIRParser::parseIRModule(DataLayoutCallbackTy DataLayoutCallback) {
1337 return Impl->parseIRModule(DataLayoutCallback);
1338}
1339
1340bool MIRParser::parseMachineFunctions(Module &M, MachineModuleInfo &MMI) {
1341 return Impl->parseMachineFunctions(M, MMI);
1342}
1343
1344bool MIRParser::parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM) {
1345 auto &MMI = MAM.getResult<MachineModuleAnalysis>(IR&: M).getMMI();
1346 return Impl->parseMachineFunctions(M, MMI, MAM: &MAM);
1347}
1348
1349std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(
1350 StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
1351 std::function<void(Function &)> ProcessIRFunction) {
1352 auto FileOrErr = MemoryBuffer::getFileOrSTDIN(Filename, /*IsText=*/true);
1353 if (std::error_code EC = FileOrErr.getError()) {
1354 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
1355 "could not open input file: " + EC.message());
1356 return nullptr;
1357 }
1358 return createMIRParser(Contents: std::move(FileOrErr.get()), Context,
1359 ProcessIRFunction);
1360}
1361
1362std::unique_ptr<MIRParser>
1363llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
1364 LLVMContext &Context,
1365 std::function<void(Function &)> ProcessIRFunction) {
1366 auto Filename = Contents->getBufferIdentifier();
1367 if (Context.shouldDiscardValueNames()) {
1368 Context.diagnose(DI: DiagnosticInfoMIRParser(
1369 DS_Error,
1370 SMDiagnostic(
1371 Filename, SourceMgr::DK_Error,
1372 "cannot read MIR with a Context that discards named Values")));
1373 return nullptr;
1374 }
1375 return std::make_unique<MIRParser>(args: std::make_unique<MIRParserImpl>(
1376 args: std::move(Contents), args&: Filename, args&: Context, args&: ProcessIRFunction));
1377}
1378