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);
107 initializeRegisterInfo(const MachineFunction &MF,
108 MachineRegisterInfo &RegInfo,
109 const yaml::MachineFunction &YamlMF,
110 DenseMap<unsigned, unsigned> &VirtualRegisterSlots);
112 bool initializeFrameInfo(const Function &F, MachineFrameInfo &MFI,
113 const yaml::MachineFunction &YamlMF,
114 DenseMap<unsigned, int> &StackObjectSlots,
115 DenseMap<unsigned, int> &FixedStackObjectSlots);
117 bool initializeConstantPool(MachineConstantPool &ConstantPool,
118 const yaml::MachineFunction &YamlMF,
119 const MachineFunction &MF,
120 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
122 bool initializeJumpTableInfo(MachineFunction &MF,
123 const yaml::MachineJumpTable &YamlJTI,
124 PerFunctionMIParsingState &PFS);
127 /// Return a MIR diagnostic converted from an MI string diagnostic.
128 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
129 SMRange SourceRange);
131 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
132 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
133 SMRange SourceRange);
135 /// Create an empty function with the given name.
136 void createDummyFunction(StringRef Name, Module &M);
138 void initNames2RegClasses(const MachineFunction &MF);
140 /// Check if the given identifier is a name of a register class.
142 /// Return null if the name isn't a register class.
143 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
147 } // end namespace llvm
149 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
150 StringRef Filename, LLVMContext &Context)
151 : SM(), Filename(Filename), Context(Context) {
152 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
155 bool MIRParserImpl::error(const Twine &Message) {
156 Context.diagnose(DiagnosticInfoMIRParser(
157 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
161 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
162 Context.diagnose(DiagnosticInfoMIRParser(
163 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
167 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
168 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
169 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
173 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
174 DiagnosticSeverity Kind;
175 switch (Diag.getKind()) {
176 case SourceMgr::DK_Error:
179 case SourceMgr::DK_Warning:
182 case SourceMgr::DK_Note:
186 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
189 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
190 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
193 std::unique_ptr<Module> MIRParserImpl::parse() {
194 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
195 /*Ctxt=*/nullptr, handleYAMLDiag, this);
198 if (!In.setCurrentDocument()) {
201 // Create an empty module when the MIR file is empty.
202 return llvm::make_unique<Module>(Filename, Context);
205 std::unique_ptr<Module> M;
206 bool NoLLVMIR = false;
207 // Parse the block scalar manually so that we can return unique pointer
208 // without having to go trough YAML traits.
209 if (const auto *BSN =
210 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
212 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
215 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
219 if (!In.setCurrentDocument())
222 // Create an new, empty module.
223 M = llvm::make_unique<Module>(Filename, Context);
227 // Parse the machine functions.
229 if (parseMachineFunction(In, *M, NoLLVMIR))
232 } while (In.setCurrentDocument());
237 bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
239 auto MF = llvm::make_unique<yaml::MachineFunction>();
240 yaml::yamlize(In, *MF, false);
243 auto FunctionName = MF->Name;
244 if (Functions.find(FunctionName) != Functions.end())
245 return error(Twine("redefinition of machine function '") + FunctionName +
247 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
249 createDummyFunction(FunctionName, M);
250 else if (!M.getFunction(FunctionName))
251 return error(Twine("function '") + FunctionName +
252 "' isn't defined in the provided LLVM IR");
256 void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
257 auto &Context = M.getContext();
258 Function *F = cast<Function>(M.getOrInsertFunction(
259 Name, FunctionType::get(Type::getVoidTy(Context), false)));
260 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
261 new UnreachableInst(Context, BB);
264 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
265 auto It = Functions.find(MF.getName());
266 if (It == Functions.end())
267 return error(Twine("no machine function information for function '") +
268 MF.getName() + "' in the MIR file");
269 // TODO: Recreate the machine function.
270 const yaml::MachineFunction &YamlMF = *It->getValue();
271 if (YamlMF.Alignment)
272 MF.setAlignment(YamlMF.Alignment);
273 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
274 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
275 PerFunctionMIParsingState PFS;
276 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
277 PFS.VirtualRegisterSlots))
279 if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF,
280 PFS.StackObjectSlots, PFS.FixedStackObjectSlots))
282 if (!YamlMF.Constants.empty()) {
283 auto *ConstantPool = MF.getConstantPool();
284 assert(ConstantPool && "Constant pool must be created");
285 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
286 PFS.ConstantPoolSlots))
290 const auto &F = *MF.getFunction();
291 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
292 const BasicBlock *BB = nullptr;
293 const yaml::StringValue &Name = YamlMBB.Name;
294 if (!Name.Value.empty()) {
295 BB = dyn_cast_or_null<BasicBlock>(
296 F.getValueSymbolTable().lookup(Name.Value));
298 return error(Name.SourceRange.Start,
299 Twine("basic block '") + Name.Value +
300 "' is not defined in the function '" + MF.getName() +
303 auto *MBB = MF.CreateMachineBasicBlock(BB);
304 MF.insert(MF.end(), MBB);
306 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
308 return error(Twine("redefinition of machine basic block with id #") +
312 if (YamlMF.BasicBlocks.empty())
313 return error(Twine("machine function '") + Twine(MF.getName()) +
314 "' requires at least one machine basic block in its body");
315 // Initialize the jump table after creating all the MBBs so that the MBB
316 // references can be resolved.
317 if (!YamlMF.JumpTableInfo.Entries.empty() &&
318 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
320 // Initialize the machine basic blocks after creating them all so that the
321 // machine instructions parser can resolve the MBB references.
323 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
324 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
328 // FIXME: This is a temporary workaround until the reserved registers can be
330 MF.getRegInfo().freezeReservedRegs(MF);
335 bool MIRParserImpl::initializeMachineBasicBlock(
336 MachineFunction &MF, MachineBasicBlock &MBB,
337 const yaml::MachineBasicBlock &YamlMBB,
338 const PerFunctionMIParsingState &PFS) {
339 MBB.setAlignment(YamlMBB.Alignment);
340 if (YamlMBB.AddressTaken)
341 MBB.setHasAddressTaken();
342 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
344 // Parse the successors.
345 for (const auto &MBBSource : YamlMBB.Successors) {
346 MachineBasicBlock *SuccMBB = nullptr;
347 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
349 return error(Error, MBBSource.SourceRange);
350 // TODO: Report an error when adding the same successor more than once.
351 MBB.addSuccessor(SuccMBB);
353 // Parse the liveins.
354 for (const auto &LiveInSource : YamlMBB.LiveIns) {
356 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
358 return error(Error, LiveInSource.SourceRange);
361 // Parse the instructions.
362 for (const auto &MISource : YamlMBB.Instructions) {
363 MachineInstr *MI = nullptr;
364 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
365 return error(Error, MISource.SourceRange);
366 MBB.insert(MBB.end(), MI);
371 bool MIRParserImpl::initializeRegisterInfo(
372 const MachineFunction &MF, MachineRegisterInfo &RegInfo,
373 const yaml::MachineFunction &YamlMF,
374 DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
375 assert(RegInfo.isSSA());
378 assert(RegInfo.tracksLiveness());
379 if (!YamlMF.TracksRegLiveness)
380 RegInfo.invalidateLiveness();
381 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
383 // Parse the virtual register information.
384 for (const auto &VReg : YamlMF.VirtualRegisters) {
385 const auto *RC = getRegClass(MF, VReg.Class.Value);
387 return error(VReg.Class.SourceRange.Start,
388 Twine("use of undefined register class '") +
389 VReg.Class.Value + "'");
390 unsigned Reg = RegInfo.createVirtualRegister(RC);
391 // TODO: Report an error when the same virtual register with the same ID is
393 VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
398 bool MIRParserImpl::initializeFrameInfo(
399 const Function &F, MachineFrameInfo &MFI,
400 const yaml::MachineFunction &YamlMF,
401 DenseMap<unsigned, int> &StackObjectSlots,
402 DenseMap<unsigned, int> &FixedStackObjectSlots) {
403 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
404 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
405 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
406 MFI.setHasStackMap(YamlMFI.HasStackMap);
407 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
408 MFI.setStackSize(YamlMFI.StackSize);
409 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
410 if (YamlMFI.MaxAlignment)
411 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
412 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
413 MFI.setHasCalls(YamlMFI.HasCalls);
414 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
415 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
416 MFI.setHasVAStart(YamlMFI.HasVAStart);
417 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
419 // Initialize the fixed frame objects.
420 for (const auto &Object : YamlMF.FixedStackObjects) {
422 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
423 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
424 Object.IsImmutable, Object.IsAliased);
426 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
427 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
428 // TODO: Report an error when objects are redefined.
429 FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
432 // Initialize the ordinary frame objects.
433 for (const auto &Object : YamlMF.StackObjects) {
435 const AllocaInst *Alloca = nullptr;
436 const yaml::StringValue &Name = Object.Name;
437 if (!Name.Value.empty()) {
438 Alloca = dyn_cast_or_null<AllocaInst>(
439 F.getValueSymbolTable().lookup(Name.Value));
441 return error(Name.SourceRange.Start,
442 "alloca instruction named '" + Name.Value +
443 "' isn't defined in the function '" + F.getName() +
446 if (Object.Type == yaml::MachineStackObject::VariableSized)
447 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
449 ObjectIdx = MFI.CreateStackObject(
450 Object.Size, Object.Alignment,
451 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
452 MFI.setObjectOffset(ObjectIdx, Object.Offset);
453 // TODO: Report an error when objects are redefined.
454 StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
459 bool MIRParserImpl::initializeConstantPool(
460 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
461 const MachineFunction &MF,
462 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
463 const auto &M = *MF.getFunction()->getParent();
465 for (const auto &YamlConstant : YamlMF.Constants) {
466 const Constant *Value = dyn_cast_or_null<Constant>(
467 parseConstantValue(YamlConstant.Value.Value, Error, M));
469 return error(Error, YamlConstant.Value.SourceRange);
471 YamlConstant.Alignment
472 ? YamlConstant.Alignment
473 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
474 // TODO: Report an error when the same constant pool value ID is redefined.
475 ConstantPoolSlots.insert(std::make_pair(
476 YamlConstant.ID, ConstantPool.getConstantPoolIndex(Value, Alignment)));
481 bool MIRParserImpl::initializeJumpTableInfo(
482 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
483 PerFunctionMIParsingState &PFS) {
484 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
486 for (const auto &Entry : YamlJTI.Entries) {
487 std::vector<MachineBasicBlock *> Blocks;
488 for (const auto &MBBSource : Entry.Blocks) {
489 MachineBasicBlock *MBB = nullptr;
490 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
491 return error(Error, MBBSource.SourceRange);
492 Blocks.push_back(MBB);
494 unsigned Index = JTI->createJumpTableIndex(Blocks);
495 // TODO: Report an error when the same jump table slot ID is redefined.
496 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
501 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
502 SMRange SourceRange) {
503 assert(SourceRange.isValid() && "Invalid source range");
504 SMLoc Loc = SourceRange.Start;
505 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
506 *Loc.getPointer() == '\'';
507 // Translate the location of the error from the location in the MI string to
508 // the corresponding location in the MIR file.
509 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
512 // TODO: Translate any source ranges as well.
513 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
517 SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
518 SMRange SourceRange) {
519 assert(SourceRange.isValid());
521 // Translate the location of the error from the location in the llvm IR string
522 // to the corresponding location in the MIR file.
523 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
524 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
525 unsigned Column = Error.getColumnNo();
526 StringRef LineStr = Error.getLineContents();
527 SMLoc Loc = Error.getLoc();
529 // Get the full line and adjust the column number by taking the indentation of
530 // LLVM IR into account.
531 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
533 if (L.line_number() == Line) {
535 Loc = SMLoc::getFromPointer(LineStr.data());
536 auto Indent = LineStr.find(Error.getLineContents());
537 if (Indent != StringRef::npos)
543 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
544 Error.getMessage(), LineStr, Error.getRanges(),
548 void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
549 if (!Names2RegClasses.empty())
551 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
552 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
553 const auto *RC = TRI->getRegClass(I);
554 Names2RegClasses.insert(
555 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
559 const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
561 initNames2RegClasses(MF);
562 auto RegClassInfo = Names2RegClasses.find(Name);
563 if (RegClassInfo == Names2RegClasses.end())
565 return RegClassInfo->getValue();
568 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
569 : Impl(std::move(Impl)) {}
571 MIRParser::~MIRParser() {}
573 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
575 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
576 return Impl->initializeMachineFunction(MF);
579 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
581 LLVMContext &Context) {
582 auto FileOrErr = MemoryBuffer::getFile(Filename);
583 if (std::error_code EC = FileOrErr.getError()) {
584 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
585 "Could not open input file: " + EC.message());
588 return createMIRParser(std::move(FileOrErr.get()), Context);
591 std::unique_ptr<MIRParser>
592 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
593 LLVMContext &Context) {
594 auto Filename = Contents->getBufferIdentifier();
595 return llvm::make_unique<MIRParser>(
596 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));