Index: lib/CodeGen/MIR/MIRParser.cpp =================================================================== --- lib/CodeGen/MIR/MIRParser.cpp +++ lib/CodeGen/MIR/MIRParser.cpp @@ -13,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "llvm/CodeGen/MIR/MIRParser.h" +#include "YAMLMapping.h" #include "llvm/ADT/StringRef.h" #include "llvm/ADT/STLExtras.h" #include "llvm/AsmParser/Parser.h" @@ -38,10 +39,16 @@ MIRParserImpl(std::unique_ptr Contents, StringRef Filename, LLVMContext &Context); - /// Try to parse the optional LLVM module in the MIR file. + /// Try to parse the optional LLVM module and the machine functions in the MIR + /// file. /// - /// Return null if an error occurred while parsing the LLVM module. - std::unique_ptr parseLLVMModule(SMDiagnostic &Error); + /// Return null if an error occurred. + std::unique_ptr parse(SMDiagnostic &Error); + + /// Parse the machine function in the current YAML document. + /// + /// Return true if an error occurred. + bool parseMachineFunction(yaml::Input &In); }; } // end anonymous namespace @@ -52,21 +59,56 @@ SM.AddNewSourceBuffer(std::move(Contents), SMLoc()); } -std::unique_ptr MIRParserImpl::parseLLVMModule(SMDiagnostic &Error) { - yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer()); +static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) { + *reinterpret_cast(Context) = Diag; +} +std::unique_ptr MIRParserImpl::parse(SMDiagnostic &Error) { + yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(), + /*Ctxt=*/nullptr, handleYAMLDiag, &Error); + + if (!In.setCurrentDocument()) { + if (!Error.getMessage().empty()) + return nullptr; + // Create an empty module when the MIR file is empty. + return llvm::make_unique(Filename, Context); + } + + std::unique_ptr M; // Parse the block scalar manually so that we can return unique pointer // without having to go trough YAML traits. - if (In.setCurrentDocument()) { - if (const auto *BSN = - dyn_cast_or_null(In.getCurrentNode())) { - return parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, - Context); - } + if (const auto *BSN = + dyn_cast_or_null(In.getCurrentNode())) { + M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error, + Context); + if (!M) + return M; + In.nextDocument(); + if (!In.setCurrentDocument()) + return M; + } else { + // Create an new, empty module. + M = llvm::make_unique(Filename, Context); } - // Create an new, empty module. - return llvm::make_unique(Filename, Context); + // Parse the machine functions. + do { + if (parseMachineFunction(In)) + return nullptr; + In.nextDocument(); + } while (In.setCurrentDocument()); + + return M; +} + +bool MIRParserImpl::parseMachineFunction(yaml::Input &In) { + yaml::MachineFunction MF; + yaml::yamlize(In, MF, false); + if (In.error()) + return true; + // TODO: Initialize the real machine function with the state in the yaml + // machine function later on. + return false; } std::unique_ptr llvm::parseMIRFile(StringRef Filename, @@ -86,5 +128,5 @@ LLVMContext &Context) { auto Filename = Contents->getBufferIdentifier(); MIRParserImpl Parser(std::move(Contents), Filename, Context); - return Parser.parseLLVMModule(Error); + return Parser.parse(Error); } Index: lib/CodeGen/MIR/MIRPrinter.h =================================================================== --- lib/CodeGen/MIR/MIRPrinter.h +++ lib/CodeGen/MIR/MIRPrinter.h @@ -7,9 +7,8 @@ // //===----------------------------------------------------------------------===// // -// This file declares the function that prints out the LLVM IR using the MIR -// serialization format. -// TODO: Print out machine functions. +// This file declares the functions that print out the LLVM IR and machine +// functions using the MIR serialization format. // //===----------------------------------------------------------------------===// @@ -18,12 +17,17 @@ namespace llvm { +class MachineFunction; class Module; class raw_ostream; /// Print LLVM IR using the MIR serialization format to the given output stream. void printMIR(raw_ostream &OS, const Module &Mod); +/// Print a machine function using the MIR serialization format to the given +/// output stream. +void printMIR(raw_ostream &OS, const MachineFunction &MF); + } // end namespace llvm #endif Index: lib/CodeGen/MIR/MIRPrinter.cpp =================================================================== --- lib/CodeGen/MIR/MIRPrinter.cpp +++ lib/CodeGen/MIR/MIRPrinter.cpp @@ -13,8 +13,10 @@ //===----------------------------------------------------------------------===// #include "MIRPrinter.h" +#include "YAMLMapping.h" #include "llvm/ADT/STLExtras.h" #include "llvm/IR/Module.h" +#include "llvm/CodeGen/MachineFunction.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/raw_ostream.h" #include "llvm/Support/YAMLTraits.h" @@ -32,6 +34,8 @@ MIRPrinter(raw_ostream &OS); void printModule(const Module &Mod); + + void printFunction(const MachineFunction &MF); }; } // end anonymous namespace @@ -60,7 +64,19 @@ Out << const_cast(Mod); } +void MIRPrinter::printFunction(const MachineFunction &MF) { + yaml::MachineFunction YamlMF; + YamlMF.Name = MF.getName(); + yaml::Output Out(OS); + Out << YamlMF; +} + void llvm::printMIR(raw_ostream &OS, const Module &Mod) { MIRPrinter Printer(OS); Printer.printModule(Mod); } + +void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { + MIRPrinter Printer(OS); + Printer.printFunction(MF); +} Index: lib/CodeGen/MIR/MIRPrintingPass.cpp =================================================================== --- lib/CodeGen/MIR/MIRPrintingPass.cpp +++ lib/CodeGen/MIR/MIRPrintingPass.cpp @@ -28,6 +28,7 @@ struct MIRPrintingPass : public MachineFunctionPass { static char ID; raw_ostream &OS; + std::string MachineFunctions; MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {} MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {} @@ -40,12 +41,16 @@ } virtual bool runOnMachineFunction(MachineFunction &MF) override { - // TODO: Print out the machine function. + std::string Str; + raw_string_ostream StrOS(Str); + printMIR(StrOS, MF); + MachineFunctions.append(StrOS.str()); return false; } virtual bool doFinalization(Module &M) override { printMIR(OS, M); + OS << MachineFunctions; return false; } }; Index: lib/CodeGen/MIR/YAMLMapping.h =================================================================== --- /dev/null +++ lib/CodeGen/MIR/YAMLMapping.h @@ -0,0 +1,37 @@ +//===- YAMLMapping.h - Describes the mapping between MIR and YAML ---------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the mapping between various MIR data structures and +// their corresponding YAML representation. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_LIB_CODEGEN_MIR_YAMLMAPPING_H +#define LLVM_LIB_CODEGEN_MIR_YAMLMAPPING_H + +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/YAMLTraits.h" + +namespace llvm { +namespace yaml { + +struct MachineFunction { + StringRef Name; +}; + +template <> struct MappingTraits { + static void mapping(IO &YamlIO, MachineFunction &MF) { + YamlIO.mapRequired("name", MF.Name); + } +}; + +} // end namespace yaml +} // end namespace llvm + +#endif Index: test/CodeGen/MIR/machine-function-missing-name.mir =================================================================== --- /dev/null +++ test/CodeGen/MIR/machine-function-missing-name.mir @@ -0,0 +1,22 @@ +# RUN: not llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s 2>&1 | FileCheck %s +# This test ensures that an error is reported when a machine function doesn't +# have a name attribute. + +--- | + + define i32 @foo() { + ret i32 0 + } + + define i32 @bar() { + ret i32 0 + } + +... +--- +# CHECK: [[@LINE+1]]:1: error: missing required key 'name' +nme: foo +... +--- +name: bar +... Index: test/CodeGen/MIR/machine-function.mir =================================================================== --- /dev/null +++ test/CodeGen/MIR/machine-function.mir @@ -0,0 +1,24 @@ +# RUN: llc -start-after branch-folder -stop-after branch-folder -o /dev/null %s | FileCheck %s +# This test ensures that the MIR parser parses machine functions correctly. + +--- | + + define i32 @foo() { + ret i32 0 + } + + define i32 @bar() { + ret i32 0 + } + +... +--- +# CHECK: name: foo +# CHECK-NEXT: ... +name: foo +... +--- +# CHECK: name: bar +# CHECK-NEXT: ... +name: bar +...