1 //===- MIRParser.cpp - MIR serialization format parser implementation -----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the class that parses the optional LLVM IR and machine
11 // functions that are stored in MIR files.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/CodeGen/MIRParser/MIRParser.h"
16 #include "MIParser.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/AsmParser/Parser.h"
22 #include "llvm/AsmParser/SlotMapping.h"
23 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/CodeGen/MIRYamlMapping.h"
27 #include "llvm/IR/BasicBlock.h"
28 #include "llvm/IR/DiagnosticInfo.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/IR/Module.h"
32 #include "llvm/IR/ValueSymbolTable.h"
33 #include "llvm/Support/LineIterator.h"
34 #include "llvm/Support/SMLoc.h"
35 #include "llvm/Support/SourceMgr.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/YAMLTraits.h"
38 #include <memory>
39 
40 using namespace llvm;
41 
42 namespace llvm {
43 
44 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
45 /// file.
46 class MIRParserImpl {
47   SourceMgr SM;
48   StringRef Filename;
49   LLVMContext &Context;
50   StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
51   SlotMapping IRSlots;
52   /// Maps from register class names to register classes.
53   StringMap<const TargetRegisterClass *> Names2RegClasses;
54 
55 public:
56   MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
57                 LLVMContext &Context);
58 
59   void reportDiagnostic(const SMDiagnostic &Diag);
60 
61   /// Report an error with the given message at unknown location.
62   ///
63   /// Always returns true.
64   bool error(const Twine &Message);
65 
66   /// Report an error with the given message at the given location.
67   ///
68   /// Always returns true.
69   bool error(SMLoc Loc, const Twine &Message);
70 
71   /// Report a given error with the location translated from the location in an
72   /// embedded string literal to a location in the MIR file.
73   ///
74   /// Always returns true.
75   bool error(const SMDiagnostic &Error, SMRange SourceRange);
76 
77   /// Try to parse the optional LLVM module and the machine functions in the MIR
78   /// file.
79   ///
80   /// Return null if an error occurred.
81   std::unique_ptr<Module> parse();
82 
83   /// Parse the machine function in the current YAML document.
84   ///
85   /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
86   /// A dummy IR function is created and inserted into the given module when
87   /// this parameter is true.
88   ///
89   /// Return true if an error occurred.
90   bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
91 
92   /// Initialize the machine function to the state that's described in the MIR
93   /// file.
94   ///
95   /// Return true if error occurred.
96   bool initializeMachineFunction(MachineFunction &MF);
97 
98   /// Initialize the machine basic block using it's YAML representation.
99   ///
100   /// Return true if an error occurred.
101   bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
102                                    const yaml::MachineBasicBlock &YamlMBB,
103                                    const PerFunctionMIParsingState &PFS);
104 
105   bool
106   initializeRegisterInfo(const MachineFunction &MF,
107                          MachineRegisterInfo &RegInfo,
108                          const yaml::MachineFunction &YamlMF,
109                          DenseMap<unsigned, unsigned> &VirtualRegisterSlots);
110 
111   bool initializeFrameInfo(const Function &F, MachineFrameInfo &MFI,
112                            const yaml::MachineFunction &YamlMF,
113                            DenseMap<unsigned, int> &StackObjectSlots,
114                            DenseMap<unsigned, int> &FixedStackObjectSlots);
115 
116   bool initializeJumpTableInfo(MachineFunction &MF,
117                                const yaml::MachineJumpTable &YamlJTI,
118                                PerFunctionMIParsingState &PFS);
119 
120 private:
121   /// Return a MIR diagnostic converted from an MI string diagnostic.
122   SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
123                                     SMRange SourceRange);
124 
125   /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
126   SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
127                                         SMRange SourceRange);
128 
129   /// Create an empty function with the given name.
130   void createDummyFunction(StringRef Name, Module &M);
131 
132   void initNames2RegClasses(const MachineFunction &MF);
133 
134   /// Check if the given identifier is a name of a register class.
135   ///
136   /// Return null if the name isn't a register class.
137   const TargetRegisterClass *getRegClass(const MachineFunction &MF,
138                                          StringRef Name);
139 };
140 
141 } // end namespace llvm
142 
143 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
144                              StringRef Filename, LLVMContext &Context)
145     : SM(), Filename(Filename), Context(Context) {
146   SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
147 }
148 
149 bool MIRParserImpl::error(const Twine &Message) {
150   Context.diagnose(DiagnosticInfoMIRParser(
151       DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
152   return true;
153 }
154 
155 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
156   Context.diagnose(DiagnosticInfoMIRParser(
157       DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
158   return true;
159 }
160 
161 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
162   assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
163   reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
164   return true;
165 }
166 
167 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
168   DiagnosticSeverity Kind;
169   switch (Diag.getKind()) {
170   case SourceMgr::DK_Error:
171     Kind = DS_Error;
172     break;
173   case SourceMgr::DK_Warning:
174     Kind = DS_Warning;
175     break;
176   case SourceMgr::DK_Note:
177     Kind = DS_Note;
178     break;
179   }
180   Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
181 }
182 
183 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
184   reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
185 }
186 
187 std::unique_ptr<Module> MIRParserImpl::parse() {
188   yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
189                  /*Ctxt=*/nullptr, handleYAMLDiag, this);
190   In.setContext(&In);
191 
192   if (!In.setCurrentDocument()) {
193     if (In.error())
194       return nullptr;
195     // Create an empty module when the MIR file is empty.
196     return llvm::make_unique<Module>(Filename, Context);
197   }
198 
199   std::unique_ptr<Module> M;
200   bool NoLLVMIR = false;
201   // Parse the block scalar manually so that we can return unique pointer
202   // without having to go trough YAML traits.
203   if (const auto *BSN =
204           dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
205     SMDiagnostic Error;
206     M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
207                       Context, &IRSlots);
208     if (!M) {
209       reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
210       return M;
211     }
212     In.nextDocument();
213     if (!In.setCurrentDocument())
214       return M;
215   } else {
216     // Create an new, empty module.
217     M = llvm::make_unique<Module>(Filename, Context);
218     NoLLVMIR = true;
219   }
220 
221   // Parse the machine functions.
222   do {
223     if (parseMachineFunction(In, *M, NoLLVMIR))
224       return nullptr;
225     In.nextDocument();
226   } while (In.setCurrentDocument());
227 
228   return M;
229 }
230 
231 bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
232                                          bool NoLLVMIR) {
233   auto MF = llvm::make_unique<yaml::MachineFunction>();
234   yaml::yamlize(In, *MF, false);
235   if (In.error())
236     return true;
237   auto FunctionName = MF->Name;
238   if (Functions.find(FunctionName) != Functions.end())
239     return error(Twine("redefinition of machine function '") + FunctionName +
240                  "'");
241   Functions.insert(std::make_pair(FunctionName, std::move(MF)));
242   if (NoLLVMIR)
243     createDummyFunction(FunctionName, M);
244   else if (!M.getFunction(FunctionName))
245     return error(Twine("function '") + FunctionName +
246                  "' isn't defined in the provided LLVM IR");
247   return false;
248 }
249 
250 void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
251   auto &Context = M.getContext();
252   Function *F = cast<Function>(M.getOrInsertFunction(
253       Name, FunctionType::get(Type::getVoidTy(Context), false)));
254   BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
255   new UnreachableInst(Context, BB);
256 }
257 
258 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
259   auto It = Functions.find(MF.getName());
260   if (It == Functions.end())
261     return error(Twine("no machine function information for function '") +
262                  MF.getName() + "' in the MIR file");
263   // TODO: Recreate the machine function.
264   const yaml::MachineFunction &YamlMF = *It->getValue();
265   if (YamlMF.Alignment)
266     MF.setAlignment(YamlMF.Alignment);
267   MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
268   MF.setHasInlineAsm(YamlMF.HasInlineAsm);
269   PerFunctionMIParsingState PFS;
270   if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
271                              PFS.VirtualRegisterSlots))
272     return true;
273   if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF,
274                           PFS.StackObjectSlots, PFS.FixedStackObjectSlots))
275     return true;
276 
277   const auto &F = *MF.getFunction();
278   for (const auto &YamlMBB : YamlMF.BasicBlocks) {
279     const BasicBlock *BB = nullptr;
280     const yaml::StringValue &Name = YamlMBB.Name;
281     if (!Name.Value.empty()) {
282       BB = dyn_cast_or_null<BasicBlock>(
283           F.getValueSymbolTable().lookup(Name.Value));
284       if (!BB)
285         return error(Name.SourceRange.Start,
286                      Twine("basic block '") + Name.Value +
287                          "' is not defined in the function '" + MF.getName() +
288                          "'");
289     }
290     auto *MBB = MF.CreateMachineBasicBlock(BB);
291     MF.insert(MF.end(), MBB);
292     bool WasInserted =
293         PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
294     if (!WasInserted)
295       return error(Twine("redefinition of machine basic block with id #") +
296                    Twine(YamlMBB.ID));
297   }
298 
299   if (YamlMF.BasicBlocks.empty())
300     return error(Twine("machine function '") + Twine(MF.getName()) +
301                  "' requires at least one machine basic block in its body");
302   // Initialize the jump table after creating all the MBBs so that the MBB
303   // references can be resolved.
304   if (!YamlMF.JumpTableInfo.Entries.empty() &&
305       initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
306     return true;
307   // Initialize the machine basic blocks after creating them all so that the
308   // machine instructions parser can resolve the MBB references.
309   unsigned I = 0;
310   for (const auto &YamlMBB : YamlMF.BasicBlocks) {
311     if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
312                                     PFS))
313       return true;
314   }
315   return false;
316 }
317 
318 bool MIRParserImpl::initializeMachineBasicBlock(
319     MachineFunction &MF, MachineBasicBlock &MBB,
320     const yaml::MachineBasicBlock &YamlMBB,
321     const PerFunctionMIParsingState &PFS) {
322   MBB.setAlignment(YamlMBB.Alignment);
323   if (YamlMBB.AddressTaken)
324     MBB.setHasAddressTaken();
325   MBB.setIsLandingPad(YamlMBB.IsLandingPad);
326   SMDiagnostic Error;
327   // Parse the successors.
328   for (const auto &MBBSource : YamlMBB.Successors) {
329     MachineBasicBlock *SuccMBB = nullptr;
330     if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
331                           Error))
332       return error(Error, MBBSource.SourceRange);
333     // TODO: Report an error when adding the same successor more than once.
334     MBB.addSuccessor(SuccMBB);
335   }
336   // Parse the liveins.
337   for (const auto &LiveInSource : YamlMBB.LiveIns) {
338     unsigned Reg = 0;
339     if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
340                                     IRSlots, Error))
341       return error(Error, LiveInSource.SourceRange);
342     MBB.addLiveIn(Reg);
343   }
344   // Parse the instructions.
345   for (const auto &MISource : YamlMBB.Instructions) {
346     MachineInstr *MI = nullptr;
347     if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
348       return error(Error, MISource.SourceRange);
349     MBB.insert(MBB.end(), MI);
350   }
351   return false;
352 }
353 
354 bool MIRParserImpl::initializeRegisterInfo(
355     const MachineFunction &MF, MachineRegisterInfo &RegInfo,
356     const yaml::MachineFunction &YamlMF,
357     DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
358   assert(RegInfo.isSSA());
359   if (!YamlMF.IsSSA)
360     RegInfo.leaveSSA();
361   assert(RegInfo.tracksLiveness());
362   if (!YamlMF.TracksRegLiveness)
363     RegInfo.invalidateLiveness();
364   RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
365 
366   // Parse the virtual register information.
367   for (const auto &VReg : YamlMF.VirtualRegisters) {
368     const auto *RC = getRegClass(MF, VReg.Class.Value);
369     if (!RC)
370       return error(VReg.Class.SourceRange.Start,
371                    Twine("use of undefined register class '") +
372                        VReg.Class.Value + "'");
373     unsigned Reg = RegInfo.createVirtualRegister(RC);
374     // TODO: Report an error when the same virtual register with the same ID is
375     // redefined.
376     VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
377   }
378   return false;
379 }
380 
381 bool MIRParserImpl::initializeFrameInfo(
382     const Function &F, MachineFrameInfo &MFI,
383     const yaml::MachineFunction &YamlMF,
384     DenseMap<unsigned, int> &StackObjectSlots,
385     DenseMap<unsigned, int> &FixedStackObjectSlots) {
386   const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
387   MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
388   MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
389   MFI.setHasStackMap(YamlMFI.HasStackMap);
390   MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
391   MFI.setStackSize(YamlMFI.StackSize);
392   MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
393   if (YamlMFI.MaxAlignment)
394     MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
395   MFI.setAdjustsStack(YamlMFI.AdjustsStack);
396   MFI.setHasCalls(YamlMFI.HasCalls);
397   MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
398   MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
399   MFI.setHasVAStart(YamlMFI.HasVAStart);
400   MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
401 
402   // Initialize the fixed frame objects.
403   for (const auto &Object : YamlMF.FixedStackObjects) {
404     int ObjectIdx;
405     if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
406       ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
407                                         Object.IsImmutable, Object.IsAliased);
408     else
409       ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
410     MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
411     // TODO: Report an error when objects are redefined.
412     FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
413   }
414 
415   // Initialize the ordinary frame objects.
416   for (const auto &Object : YamlMF.StackObjects) {
417     int ObjectIdx;
418     const AllocaInst *Alloca = nullptr;
419     const yaml::StringValue &Name = Object.Name;
420     if (!Name.Value.empty()) {
421       Alloca = dyn_cast_or_null<AllocaInst>(
422           F.getValueSymbolTable().lookup(Name.Value));
423       if (!Alloca)
424         return error(Name.SourceRange.Start,
425                      "alloca instruction named '" + Name.Value +
426                          "' isn't defined in the function '" + F.getName() +
427                          "'");
428     }
429     if (Object.Type == yaml::MachineStackObject::VariableSized)
430       ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
431     else
432       ObjectIdx = MFI.CreateStackObject(
433           Object.Size, Object.Alignment,
434           Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
435     MFI.setObjectOffset(ObjectIdx, Object.Offset);
436     // TODO: Report an error when objects are redefined.
437     StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
438   }
439   return false;
440 }
441 
442 bool MIRParserImpl::initializeJumpTableInfo(
443     MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
444     PerFunctionMIParsingState &PFS) {
445   MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
446   SMDiagnostic Error;
447   for (const auto &Entry : YamlJTI.Entries) {
448     std::vector<MachineBasicBlock *> Blocks;
449     for (const auto &MBBSource : Entry.Blocks) {
450       MachineBasicBlock *MBB = nullptr;
451       if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
452         return error(Error, MBBSource.SourceRange);
453       Blocks.push_back(MBB);
454     }
455     unsigned Index = JTI->createJumpTableIndex(Blocks);
456     // TODO: Report an error when the same jump table slot ID is redefined.
457     PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
458   }
459   return false;
460 }
461 
462 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
463                                                  SMRange SourceRange) {
464   assert(SourceRange.isValid() && "Invalid source range");
465   SMLoc Loc = SourceRange.Start;
466   bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
467                   *Loc.getPointer() == '\'';
468   // Translate the location of the error from the location in the MI string to
469   // the corresponding location in the MIR file.
470   Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
471                            (HasQuote ? 1 : 0));
472 
473   // TODO: Translate any source ranges as well.
474   return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
475                        Error.getFixIts());
476 }
477 
478 SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
479                                                      SMRange SourceRange) {
480   assert(SourceRange.isValid());
481 
482   // Translate the location of the error from the location in the llvm IR string
483   // to the corresponding location in the MIR file.
484   auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
485   unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
486   unsigned Column = Error.getColumnNo();
487   StringRef LineStr = Error.getLineContents();
488   SMLoc Loc = Error.getLoc();
489 
490   // Get the full line and adjust the column number by taking the indentation of
491   // LLVM IR into account.
492   for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
493        L != E; ++L) {
494     if (L.line_number() == Line) {
495       LineStr = *L;
496       Loc = SMLoc::getFromPointer(LineStr.data());
497       auto Indent = LineStr.find(Error.getLineContents());
498       if (Indent != StringRef::npos)
499         Column += Indent;
500       break;
501     }
502   }
503 
504   return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
505                       Error.getMessage(), LineStr, Error.getRanges(),
506                       Error.getFixIts());
507 }
508 
509 void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
510   if (!Names2RegClasses.empty())
511     return;
512   const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
513   for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
514     const auto *RC = TRI->getRegClass(I);
515     Names2RegClasses.insert(
516         std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
517   }
518 }
519 
520 const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
521                                                       StringRef Name) {
522   initNames2RegClasses(MF);
523   auto RegClassInfo = Names2RegClasses.find(Name);
524   if (RegClassInfo == Names2RegClasses.end())
525     return nullptr;
526   return RegClassInfo->getValue();
527 }
528 
529 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
530     : Impl(std::move(Impl)) {}
531 
532 MIRParser::~MIRParser() {}
533 
534 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
535 
536 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
537   return Impl->initializeMachineFunction(MF);
538 }
539 
540 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
541                                                          SMDiagnostic &Error,
542                                                          LLVMContext &Context) {
543   auto FileOrErr = MemoryBuffer::getFile(Filename);
544   if (std::error_code EC = FileOrErr.getError()) {
545     Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
546                          "Could not open input file: " + EC.message());
547     return nullptr;
548   }
549   return createMIRParser(std::move(FileOrErr.get()), Context);
550 }
551 
552 std::unique_ptr<MIRParser>
553 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
554                       LLVMContext &Context) {
555   auto Filename = Contents->getBufferIdentifier();
556   return llvm::make_unique<MIRParser>(
557       llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
558 }
559