1 //===- MIRParser.cpp - MIR serialization format parser implementation -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that parses the optional LLVM IR and machine
11 // functions that are stored in MIR files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MIRParser/MIRParser.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/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/MIRYamlMapping.h"
28 #include "llvm/IR/BasicBlock.h"
29 #include "llvm/IR/DiagnosticInfo.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/LLVMContext.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/ValueSymbolTable.h"
34 #include "llvm/Support/LineIterator.h"
35 #include "llvm/Support/SMLoc.h"
36 #include "llvm/Support/SourceMgr.h"
37 #include "llvm/Support/MemoryBuffer.h"
38 #include "llvm/Support/YAMLTraits.h"
45 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
51 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
53 /// Maps from register class names to register classes.
54 StringMap<const TargetRegisterClass *> Names2RegClasses;
57 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
58 LLVMContext &Context);
60 void reportDiagnostic(const SMDiagnostic &Diag);
62 /// Report an error with the given message at unknown location.
64 /// Always returns true.
65 bool error(const Twine &Message);
67 /// Report an error with the given message at the given location.
69 /// Always returns true.
70 bool error(SMLoc Loc, const Twine &Message);
72 /// Report a given error with the location translated from the location in an
73 /// embedded string literal to a location in the MIR file.
75 /// Always returns true.
76 bool error(const SMDiagnostic &Error, SMRange SourceRange);
78 /// Try to parse the optional LLVM module and the machine functions in the MIR
81 /// Return null if an error occurred.
82 std::unique_ptr<Module> parse();
84 /// Parse the machine function in the current YAML document.
86 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
87 /// A dummy IR function is created and inserted into the given module when
88 /// this parameter is true.
90 /// Return true if an error occurred.
91 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
93 /// Initialize the machine function to the state that's described in the MIR
96 /// Return true if error occurred.
97 bool initializeMachineFunction(MachineFunction &MF);
99 /// Initialize the machine basic block using it's YAML representation.
101 /// Return true if an error occurred.
102 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
103 const yaml::MachineBasicBlock &YamlMBB,
104 const PerFunctionMIParsingState &PFS);
106 bool initializeRegisterInfo(MachineFunction &MF,
107 const yaml::MachineFunction &YamlMF,
108 PerFunctionMIParsingState &PFS);
110 bool initializeFrameInfo(MachineFunction &MF,
111 const yaml::MachineFunction &YamlMF,
112 PerFunctionMIParsingState &PFS);
114 bool parseCalleeSavedRegister(MachineFunction &MF,
115 PerFunctionMIParsingState &PFS,
116 std::vector<CalleeSavedInfo> &CSIInfo,
117 const yaml::StringValue &RegisterSource,
120 bool initializeConstantPool(MachineConstantPool &ConstantPool,
121 const yaml::MachineFunction &YamlMF,
122 const MachineFunction &MF,
123 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
125 bool initializeJumpTableInfo(MachineFunction &MF,
126 const yaml::MachineJumpTable &YamlJTI,
127 PerFunctionMIParsingState &PFS);
130 /// Return a MIR diagnostic converted from an MI string diagnostic.
131 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
132 SMRange SourceRange);
134 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
135 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
136 SMRange SourceRange);
138 /// Create an empty function with the given name.
139 void createDummyFunction(StringRef Name, Module &M);
141 void initNames2RegClasses(const MachineFunction &MF);
143 /// Check if the given identifier is a name of a register class.
145 /// Return null if the name isn't a register class.
146 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
150 } // end namespace llvm
152 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
153 StringRef Filename, LLVMContext &Context)
154 : SM(), Filename(Filename), Context(Context) {
155 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
158 bool MIRParserImpl::error(const Twine &Message) {
159 Context.diagnose(DiagnosticInfoMIRParser(
160 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
164 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
165 Context.diagnose(DiagnosticInfoMIRParser(
166 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
170 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
171 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
172 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
176 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
177 DiagnosticSeverity Kind;
178 switch (Diag.getKind()) {
179 case SourceMgr::DK_Error:
182 case SourceMgr::DK_Warning:
185 case SourceMgr::DK_Note:
189 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
192 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
193 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
196 std::unique_ptr<Module> MIRParserImpl::parse() {
197 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
198 /*Ctxt=*/nullptr, handleYAMLDiag, this);
201 if (!In.setCurrentDocument()) {
204 // Create an empty module when the MIR file is empty.
205 return llvm::make_unique<Module>(Filename, Context);
208 std::unique_ptr<Module> M;
209 bool NoLLVMIR = false;
210 // Parse the block scalar manually so that we can return unique pointer
211 // without having to go trough YAML traits.
212 if (const auto *BSN =
213 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
215 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
218 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
222 if (!In.setCurrentDocument())
225 // Create an new, empty module.
226 M = llvm::make_unique<Module>(Filename, Context);
230 // Parse the machine functions.
232 if (parseMachineFunction(In, *M, NoLLVMIR))
235 } while (In.setCurrentDocument());
240 bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
242 auto MF = llvm::make_unique<yaml::MachineFunction>();
243 yaml::yamlize(In, *MF, false);
246 auto FunctionName = MF->Name;
247 if (Functions.find(FunctionName) != Functions.end())
248 return error(Twine("redefinition of machine function '") + FunctionName +
250 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
252 createDummyFunction(FunctionName, M);
253 else if (!M.getFunction(FunctionName))
254 return error(Twine("function '") + FunctionName +
255 "' isn't defined in the provided LLVM IR");
259 void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
260 auto &Context = M.getContext();
261 Function *F = cast<Function>(M.getOrInsertFunction(
262 Name, FunctionType::get(Type::getVoidTy(Context), false)));
263 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
264 new UnreachableInst(Context, BB);
267 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
268 auto It = Functions.find(MF.getName());
269 if (It == Functions.end())
270 return error(Twine("no machine function information for function '") +
271 MF.getName() + "' in the MIR file");
272 // TODO: Recreate the machine function.
273 const yaml::MachineFunction &YamlMF = *It->getValue();
274 if (YamlMF.Alignment)
275 MF.setAlignment(YamlMF.Alignment);
276 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
277 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
278 PerFunctionMIParsingState PFS;
279 if (initializeRegisterInfo(MF, YamlMF, PFS))
281 if (initializeFrameInfo(MF, YamlMF, PFS))
283 if (!YamlMF.Constants.empty()) {
284 auto *ConstantPool = MF.getConstantPool();
285 assert(ConstantPool && "Constant pool must be created");
286 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
287 PFS.ConstantPoolSlots))
291 const auto &F = *MF.getFunction();
292 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
293 const BasicBlock *BB = nullptr;
294 const yaml::StringValue &Name = YamlMBB.Name;
295 const yaml::StringValue &IRBlock = YamlMBB.IRBlock;
296 if (!Name.Value.empty()) {
297 BB = dyn_cast_or_null<BasicBlock>(
298 F.getValueSymbolTable().lookup(Name.Value));
300 return error(Name.SourceRange.Start,
301 Twine("basic block '") + Name.Value +
302 "' is not defined in the function '" + MF.getName() +
305 if (!IRBlock.Value.empty()) {
306 // TODO: Report an error when both name and ir block are specified.
308 if (parseIRBlockReference(BB, SM, MF, IRBlock.Value, PFS, IRSlots, Error))
309 return error(Error, IRBlock.SourceRange);
311 auto *MBB = MF.CreateMachineBasicBlock(BB);
312 MF.insert(MF.end(), MBB);
314 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
316 return error(Twine("redefinition of machine basic block with id #") +
320 if (YamlMF.BasicBlocks.empty())
321 return error(Twine("machine function '") + Twine(MF.getName()) +
322 "' requires at least one machine basic block in its body");
323 // Initialize the jump table after creating all the MBBs so that the MBB
324 // references can be resolved.
325 if (!YamlMF.JumpTableInfo.Entries.empty() &&
326 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
328 // Initialize the machine basic blocks after creating them all so that the
329 // machine instructions parser can resolve the MBB references.
331 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
332 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
336 // FIXME: This is a temporary workaround until the reserved registers can be
338 MF.getRegInfo().freezeReservedRegs(MF);
343 bool MIRParserImpl::initializeMachineBasicBlock(
344 MachineFunction &MF, MachineBasicBlock &MBB,
345 const yaml::MachineBasicBlock &YamlMBB,
346 const PerFunctionMIParsingState &PFS) {
347 MBB.setAlignment(YamlMBB.Alignment);
348 if (YamlMBB.AddressTaken)
349 MBB.setHasAddressTaken();
350 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
352 // Parse the successors.
353 for (const auto &MBBSource : YamlMBB.Successors) {
354 MachineBasicBlock *SuccMBB = nullptr;
355 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
357 return error(Error, MBBSource.SourceRange);
358 // TODO: Report an error when adding the same successor more than once.
359 MBB.addSuccessor(SuccMBB);
361 // Parse the liveins.
362 for (const auto &LiveInSource : YamlMBB.LiveIns) {
364 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
366 return error(Error, LiveInSource.SourceRange);
369 // Parse the instructions.
370 for (const auto &MISource : YamlMBB.Instructions) {
371 MachineInstr *MI = nullptr;
372 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
373 return error(Error, MISource.SourceRange);
374 MBB.insert(MBB.end(), MI);
379 bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
380 const yaml::MachineFunction &YamlMF,
381 PerFunctionMIParsingState &PFS) {
382 MachineRegisterInfo &RegInfo = MF.getRegInfo();
383 assert(RegInfo.isSSA());
386 assert(RegInfo.tracksLiveness());
387 if (!YamlMF.TracksRegLiveness)
388 RegInfo.invalidateLiveness();
389 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
392 // Parse the virtual register information.
393 for (const auto &VReg : YamlMF.VirtualRegisters) {
394 const auto *RC = getRegClass(MF, VReg.Class.Value);
396 return error(VReg.Class.SourceRange.Start,
397 Twine("use of undefined register class '") +
398 VReg.Class.Value + "'");
399 unsigned Reg = RegInfo.createVirtualRegister(RC);
400 // TODO: Report an error when the same virtual register with the same ID is
402 PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
403 if (!VReg.PreferredRegister.Value.empty()) {
404 unsigned PreferredReg = 0;
405 if (parseNamedRegisterReference(PreferredReg, SM, MF,
406 VReg.PreferredRegister.Value, PFS,
408 return error(Error, VReg.PreferredRegister.SourceRange);
409 RegInfo.setSimpleHint(Reg, PreferredReg);
413 // Parse the liveins.
414 for (const auto &LiveIn : YamlMF.LiveIns) {
416 if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
418 return error(Error, LiveIn.Register.SourceRange);
420 if (!LiveIn.VirtualRegister.Value.empty()) {
421 if (parseVirtualRegisterReference(
422 VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
423 return error(Error, LiveIn.VirtualRegister.SourceRange);
425 RegInfo.addLiveIn(Reg, VReg);
430 bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
431 const yaml::MachineFunction &YamlMF,
432 PerFunctionMIParsingState &PFS) {
433 MachineFrameInfo &MFI = *MF.getFrameInfo();
434 const Function &F = *MF.getFunction();
435 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
436 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
437 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
438 MFI.setHasStackMap(YamlMFI.HasStackMap);
439 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
440 MFI.setStackSize(YamlMFI.StackSize);
441 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
442 if (YamlMFI.MaxAlignment)
443 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
444 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
445 MFI.setHasCalls(YamlMFI.HasCalls);
446 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
447 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
448 MFI.setHasVAStart(YamlMFI.HasVAStart);
449 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
451 std::vector<CalleeSavedInfo> CSIInfo;
452 // Initialize the fixed frame objects.
453 for (const auto &Object : YamlMF.FixedStackObjects) {
455 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
456 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
457 Object.IsImmutable, Object.IsAliased);
459 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
460 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
461 // TODO: Report an error when objects are redefined.
462 PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
463 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
468 // Initialize the ordinary frame objects.
469 for (const auto &Object : YamlMF.StackObjects) {
471 const AllocaInst *Alloca = nullptr;
472 const yaml::StringValue &Name = Object.Name;
473 if (!Name.Value.empty()) {
474 Alloca = dyn_cast_or_null<AllocaInst>(
475 F.getValueSymbolTable().lookup(Name.Value));
477 return error(Name.SourceRange.Start,
478 "alloca instruction named '" + Name.Value +
479 "' isn't defined in the function '" + F.getName() +
482 if (Object.Type == yaml::MachineStackObject::VariableSized)
483 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
485 ObjectIdx = MFI.CreateStackObject(
486 Object.Size, Object.Alignment,
487 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
488 MFI.setObjectOffset(ObjectIdx, Object.Offset);
489 // TODO: Report an error when objects are redefined.
490 PFS.StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
491 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
495 MFI.setCalleeSavedInfo(CSIInfo);
496 if (!CSIInfo.empty())
497 MFI.setCalleeSavedInfoValid(true);
501 bool MIRParserImpl::parseCalleeSavedRegister(
502 MachineFunction &MF, PerFunctionMIParsingState &PFS,
503 std::vector<CalleeSavedInfo> &CSIInfo,
504 const yaml::StringValue &RegisterSource, int FrameIdx) {
505 if (RegisterSource.Value.empty())
509 if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
511 return error(Error, RegisterSource.SourceRange);
512 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
516 bool MIRParserImpl::initializeConstantPool(
517 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
518 const MachineFunction &MF,
519 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
520 const auto &M = *MF.getFunction()->getParent();
522 for (const auto &YamlConstant : YamlMF.Constants) {
523 const Constant *Value = dyn_cast_or_null<Constant>(
524 parseConstantValue(YamlConstant.Value.Value, Error, M));
526 return error(Error, YamlConstant.Value.SourceRange);
528 YamlConstant.Alignment
529 ? YamlConstant.Alignment
530 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
531 // TODO: Report an error when the same constant pool value ID is redefined.
532 ConstantPoolSlots.insert(std::make_pair(
533 YamlConstant.ID, ConstantPool.getConstantPoolIndex(Value, Alignment)));
538 bool MIRParserImpl::initializeJumpTableInfo(
539 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
540 PerFunctionMIParsingState &PFS) {
541 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
543 for (const auto &Entry : YamlJTI.Entries) {
544 std::vector<MachineBasicBlock *> Blocks;
545 for (const auto &MBBSource : Entry.Blocks) {
546 MachineBasicBlock *MBB = nullptr;
547 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
548 return error(Error, MBBSource.SourceRange);
549 Blocks.push_back(MBB);
551 unsigned Index = JTI->createJumpTableIndex(Blocks);
552 // TODO: Report an error when the same jump table slot ID is redefined.
553 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
558 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
559 SMRange SourceRange) {
560 assert(SourceRange.isValid() && "Invalid source range");
561 SMLoc Loc = SourceRange.Start;
562 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
563 *Loc.getPointer() == '\'';
564 // Translate the location of the error from the location in the MI string to
565 // the corresponding location in the MIR file.
566 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
569 // TODO: Translate any source ranges as well.
570 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
574 SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
575 SMRange SourceRange) {
576 assert(SourceRange.isValid());
578 // Translate the location of the error from the location in the llvm IR string
579 // to the corresponding location in the MIR file.
580 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
581 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
582 unsigned Column = Error.getColumnNo();
583 StringRef LineStr = Error.getLineContents();
584 SMLoc Loc = Error.getLoc();
586 // Get the full line and adjust the column number by taking the indentation of
587 // LLVM IR into account.
588 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
590 if (L.line_number() == Line) {
592 Loc = SMLoc::getFromPointer(LineStr.data());
593 auto Indent = LineStr.find(Error.getLineContents());
594 if (Indent != StringRef::npos)
600 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
601 Error.getMessage(), LineStr, Error.getRanges(),
605 void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
606 if (!Names2RegClasses.empty())
608 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
609 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
610 const auto *RC = TRI->getRegClass(I);
611 Names2RegClasses.insert(
612 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
616 const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
618 initNames2RegClasses(MF);
619 auto RegClassInfo = Names2RegClasses.find(Name);
620 if (RegClassInfo == Names2RegClasses.end())
622 return RegClassInfo->getValue();
625 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
626 : Impl(std::move(Impl)) {}
628 MIRParser::~MIRParser() {}
630 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
632 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
633 return Impl->initializeMachineFunction(MF);
636 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
638 LLVMContext &Context) {
639 auto FileOrErr = MemoryBuffer::getFile(Filename);
640 if (std::error_code EC = FileOrErr.getError()) {
641 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
642 "Could not open input file: " + EC.message());
645 return createMIRParser(std::move(FileOrErr.get()), Context);
648 std::unique_ptr<MIRParser>
649 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
650 LLVMContext &Context) {
651 auto Filename = Contents->getBufferIdentifier();
652 return llvm::make_unique<MIRParser>(
653 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));