Index: lib/CodeGen/MIRParser/MIRParser.cpp =================================================================== --- lib/CodeGen/MIRParser/MIRParser.cpp +++ lib/CodeGen/MIRParser/MIRParser.cpp @@ -19,6 +19,7 @@ #include "llvm/AsmParser/Parser.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MIRYamlMapping.h" +#include "llvm/IR/Instructions.h" #include "llvm/IR/Module.h" #include "llvm/Support/SMLoc.h" #include "llvm/Support/SourceMgr.h" @@ -51,13 +52,17 @@ /// Parse the machine function in the current YAML document. /// /// Return true if an error occurred. - bool parseMachineFunction(yaml::Input &In); + bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR); /// Initialize the machine function to the state that's described in the MIR /// file. /// /// Return true if error occurred. bool initializeMachineFunction(MachineFunction &MF, SMDiagnostic &Error); + +private: + /// Create an empty function with the given name. + void createDummyFunction(StringRef Name, Module &M); }; } // end namespace llvm @@ -84,6 +89,7 @@ } std::unique_ptr M; + bool NoLLVMIR = false; // Parse the block scalar manually so that we can return unique pointer // without having to go trough YAML traits. if (const auto *BSN = @@ -98,11 +104,12 @@ } else { // Create an new, empty module. M = llvm::make_unique(Filename, Context); + NoLLVMIR = true; } // Parse the machine functions. do { - if (parseMachineFunction(In)) + if (parseMachineFunction(In, *M, NoLLVMIR)) return nullptr; In.nextDocument(); } while (In.setCurrentDocument()); @@ -110,16 +117,27 @@ return M; } -bool MIRParserImpl::parseMachineFunction(yaml::Input &In) { +bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M, + bool NoLLVMIR) { auto MF = llvm::make_unique(); yaml::yamlize(In, *MF, false); if (In.error()) return true; auto FunctionName = MF->Name; Functions.insert(std::make_pair(FunctionName, std::move(MF))); + if (NoLLVMIR) + createDummyFunction(FunctionName, M); return false; } +void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) { + auto &Context = M.getContext(); + Function *F = cast(M.getOrInsertFunction( + Name, FunctionType::get(Type::getVoidTy(Context), false))); + BasicBlock *BB = BasicBlock::Create(Context, "Entry", F); + ReturnInst::Create(Context, nullptr, BB); +} + bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF, SMDiagnostic &Error) { auto It = Functions.find(MF.getName()); Index: test/CodeGen/MIR/llvmIRMissing.mir =================================================================== --- test/CodeGen/MIR/llvmIRMissing.mir +++ test/CodeGen/MIR/llvmIRMissing.mir @@ -1,5 +1,7 @@ -# RUN: llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s +# RUN: llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s # This test ensures that the MIR parser accepts files without the LLVM IR. --- +# CHECK: name: foo +name: foo ...