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/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"
44 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
50 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
54 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
55 LLVMContext &Context);
57 void reportDiagnostic(const SMDiagnostic &Diag);
59 /// Report an error with the given message at unknown location.
61 /// Always returns true.
62 bool error(const Twine &Message);
64 /// Report an error with the given message at the given location.
66 /// Always returns true.
67 bool error(SMLoc Loc, const Twine &Message);
69 /// Report a given error with the location translated from the location in an
70 /// embedded string literal to a location in the MIR file.
72 /// Always returns true.
73 bool error(const SMDiagnostic &Error, SMRange SourceRange);
75 /// Try to parse the optional LLVM module and the machine functions in the MIR
78 /// Return null if an error occurred.
79 std::unique_ptr<Module> parse();
81 /// Parse the machine function in the current YAML document.
83 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
84 /// A dummy IR function is created and inserted into the given module when
85 /// this parameter is true.
87 /// Return true if an error occurred.
88 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
90 /// Initialize the machine function to the state that's described in the MIR
93 /// Return true if error occurred.
94 bool initializeMachineFunction(MachineFunction &MF);
96 /// Initialize the machine basic block using it's YAML representation.
98 /// Return true if an error occurred.
99 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
100 const yaml::MachineBasicBlock &YamlMBB,
101 const PerFunctionMIParsingState &PFS);
103 bool initializeRegisterInfo(MachineRegisterInfo &RegInfo,
104 const yaml::MachineFunction &YamlMF);
106 bool initializeFrameInfo(MachineFrameInfo &MFI,
107 const yaml::MachineFrameInfo &YamlMFI);
110 /// Return a MIR diagnostic converted from an MI string diagnostic.
111 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
112 SMRange SourceRange);
114 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
115 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
116 SMRange SourceRange);
118 /// Create an empty function with the given name.
119 void createDummyFunction(StringRef Name, Module &M);
122 } // end namespace llvm
124 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
125 StringRef Filename, LLVMContext &Context)
126 : SM(), Filename(Filename), Context(Context) {
127 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
130 bool MIRParserImpl::error(const Twine &Message) {
131 Context.diagnose(DiagnosticInfoMIRParser(
132 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
136 bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
137 Context.diagnose(DiagnosticInfoMIRParser(
138 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
142 bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
143 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
144 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
148 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
149 DiagnosticSeverity Kind;
150 switch (Diag.getKind()) {
151 case SourceMgr::DK_Error:
154 case SourceMgr::DK_Warning:
157 case SourceMgr::DK_Note:
161 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
164 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
165 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
168 std::unique_ptr<Module> MIRParserImpl::parse() {
169 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
170 /*Ctxt=*/nullptr, handleYAMLDiag, this);
173 if (!In.setCurrentDocument()) {
176 // Create an empty module when the MIR file is empty.
177 return llvm::make_unique<Module>(Filename, Context);
180 std::unique_ptr<Module> M;
181 bool NoLLVMIR = false;
182 // Parse the block scalar manually so that we can return unique pointer
183 // without having to go trough YAML traits.
184 if (const auto *BSN =
185 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
187 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
190 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
194 if (!In.setCurrentDocument())
197 // Create an new, empty module.
198 M = llvm::make_unique<Module>(Filename, Context);
202 // Parse the machine functions.
204 if (parseMachineFunction(In, *M, NoLLVMIR))
207 } while (In.setCurrentDocument());
212 bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
214 auto MF = llvm::make_unique<yaml::MachineFunction>();
215 yaml::yamlize(In, *MF, false);
218 auto FunctionName = MF->Name;
219 if (Functions.find(FunctionName) != Functions.end())
220 return error(Twine("redefinition of machine function '") + FunctionName +
222 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
224 createDummyFunction(FunctionName, M);
225 else if (!M.getFunction(FunctionName))
226 return error(Twine("function '") + FunctionName +
227 "' isn't defined in the provided LLVM IR");
231 void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
232 auto &Context = M.getContext();
233 Function *F = cast<Function>(M.getOrInsertFunction(
234 Name, FunctionType::get(Type::getVoidTy(Context), false)));
235 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
236 new UnreachableInst(Context, BB);
239 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
240 auto It = Functions.find(MF.getName());
241 if (It == Functions.end())
242 return error(Twine("no machine function information for function '") +
243 MF.getName() + "' in the MIR file");
244 // TODO: Recreate the machine function.
245 const yaml::MachineFunction &YamlMF = *It->getValue();
246 if (YamlMF.Alignment)
247 MF.setAlignment(YamlMF.Alignment);
248 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
249 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
250 if (initializeRegisterInfo(MF.getRegInfo(), YamlMF))
252 if (initializeFrameInfo(*MF.getFrameInfo(), YamlMF.FrameInfo))
255 PerFunctionMIParsingState PFS;
256 const auto &F = *MF.getFunction();
257 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
258 const BasicBlock *BB = nullptr;
259 const yaml::StringValue &Name = YamlMBB.Name;
260 if (!Name.Value.empty()) {
261 BB = dyn_cast_or_null<BasicBlock>(
262 F.getValueSymbolTable().lookup(Name.Value));
264 return error(Name.SourceRange.Start,
265 Twine("basic block '") + Name.Value +
266 "' is not defined in the function '" + MF.getName() +
269 auto *MBB = MF.CreateMachineBasicBlock(BB);
270 MF.insert(MF.end(), MBB);
272 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
274 return error(Twine("redefinition of machine basic block with id #") +
278 // Initialize the machine basic blocks after creating them all so that the
279 // machine instructions parser can resolve the MBB references.
281 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
282 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
289 bool MIRParserImpl::initializeMachineBasicBlock(
290 MachineFunction &MF, MachineBasicBlock &MBB,
291 const yaml::MachineBasicBlock &YamlMBB,
292 const PerFunctionMIParsingState &PFS) {
293 MBB.setAlignment(YamlMBB.Alignment);
294 if (YamlMBB.AddressTaken)
295 MBB.setHasAddressTaken();
296 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
298 // Parse the successors.
299 for (const auto &MBBSource : YamlMBB.Successors) {
300 MachineBasicBlock *SuccMBB = nullptr;
301 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
303 return error(Error, MBBSource.SourceRange);
304 // TODO: Report an error when adding the same successor more than once.
305 MBB.addSuccessor(SuccMBB);
307 // Parse the instructions.
308 for (const auto &MISource : YamlMBB.Instructions) {
309 MachineInstr *MI = nullptr;
310 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
311 return error(Error, MISource.SourceRange);
312 MBB.insert(MBB.end(), MI);
317 bool MIRParserImpl::initializeRegisterInfo(
318 MachineRegisterInfo &RegInfo, const yaml::MachineFunction &YamlMF) {
319 assert(RegInfo.isSSA());
322 assert(RegInfo.tracksLiveness());
323 if (!YamlMF.TracksRegLiveness)
324 RegInfo.invalidateLiveness();
325 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
329 bool MIRParserImpl::initializeFrameInfo(MachineFrameInfo &MFI,
330 const yaml::MachineFrameInfo &YamlMFI) {
331 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
332 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
333 MFI.setHasStackMap(YamlMFI.HasStackMap);
334 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
335 MFI.setStackSize(YamlMFI.StackSize);
336 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
337 if (YamlMFI.MaxAlignment)
338 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
339 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
340 MFI.setHasCalls(YamlMFI.HasCalls);
341 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
342 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
343 MFI.setHasVAStart(YamlMFI.HasVAStart);
344 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
348 SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
349 SMRange SourceRange) {
350 assert(SourceRange.isValid() && "Invalid source range");
351 SMLoc Loc = SourceRange.Start;
352 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
353 *Loc.getPointer() == '\'';
354 // Translate the location of the error from the location in the MI string to
355 // the corresponding location in the MIR file.
356 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
359 // TODO: Translate any source ranges as well.
360 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
364 SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
365 SMRange SourceRange) {
366 assert(SourceRange.isValid());
368 // Translate the location of the error from the location in the llvm IR string
369 // to the corresponding location in the MIR file.
370 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
371 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
372 unsigned Column = Error.getColumnNo();
373 StringRef LineStr = Error.getLineContents();
374 SMLoc Loc = Error.getLoc();
376 // Get the full line and adjust the column number by taking the indentation of
377 // LLVM IR into account.
378 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
380 if (L.line_number() == Line) {
382 Loc = SMLoc::getFromPointer(LineStr.data());
383 auto Indent = LineStr.find(Error.getLineContents());
384 if (Indent != StringRef::npos)
390 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
391 Error.getMessage(), LineStr, Error.getRanges(),
395 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
396 : Impl(std::move(Impl)) {}
398 MIRParser::~MIRParser() {}
400 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
402 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
403 return Impl->initializeMachineFunction(MF);
406 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
408 LLVMContext &Context) {
409 auto FileOrErr = MemoryBuffer::getFile(Filename);
410 if (std::error_code EC = FileOrErr.getError()) {
411 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
412 "Could not open input file: " + EC.message());
415 return createMIRParser(std::move(FileOrErr.get()), Context);
418 std::unique_ptr<MIRParser>
419 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
420 LLVMContext &Context) {
421 auto Filename = Contents->getBufferIdentifier();
422 return llvm::make_unique<MIRParser>(
423 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));