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