1 //===- MIRParser.h - MIR serialization format parser ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This MIR serialization library is currently a work in progress. It can't 10 // serialize machine functions at this time. 11 // 12 // This file declares the functions that parse the MIR serialization format 13 // files. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #ifndef LLVM_CODEGEN_MIRPARSER_MIRPARSER_H 18 #define LLVM_CODEGEN_MIRPARSER_MIRPARSER_H 19 20 #include "llvm/ADT/STLFunctionalExtras.h" 21 #include "llvm/ADT/StringRef.h" 22 #include <functional> 23 #include <memory> 24 #include <optional> 25 26 namespace llvm { 27 28 class Function; 29 class LLVMContext; 30 class MemoryBuffer; 31 class Module; 32 class MIRParserImpl; 33 class MachineModuleInfo; 34 class SMDiagnostic; 35 class StringRef; 36 37 typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)> 38 DataLayoutCallbackTy; 39 40 /// This class initializes machine functions by applying the state loaded from 41 /// a MIR file. 42 class MIRParser { 43 std::unique_ptr<MIRParserImpl> Impl; 44 45 public: 46 MIRParser(std::unique_ptr<MIRParserImpl> Impl); 47 MIRParser(const MIRParser &) = delete; 48 ~MIRParser(); 49 50 /// Parses the optional LLVM IR module in the MIR file. 51 /// 52 /// A new, empty module is created if the LLVM IR isn't present. 53 /// \returns nullptr if a parsing error occurred. 54 std::unique_ptr<Module> 55 parseIRModule(DataLayoutCallbackTy DataLayoutCallback = 56 [](StringRef, StringRef) { return std::nullopt; }); 57 58 /// Parses MachineFunctions in the MIR file and add them to the given 59 /// MachineModuleInfo \p MMI. 60 /// 61 /// \returns true if an error occurred. 62 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI); 63 }; 64 65 /// This function is the main interface to the MIR serialization format parser. 66 /// 67 /// It reads in a MIR file and returns a MIR parser that can parse the embedded 68 /// LLVM IR module and initialize the machine functions by parsing the machine 69 /// function's state. 70 /// 71 /// \param Filename - The name of the file to parse. 72 /// \param Error - Error result info. 73 /// \param Context - Context which will be used for the parsed LLVM IR module. 74 /// \param ProcessIRFunction - function to run on every IR function or stub 75 /// loaded from the MIR file. 76 std::unique_ptr<MIRParser> createMIRParserFromFile( 77 StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, 78 std::function<void(Function &)> ProcessIRFunction = nullptr); 79 80 /// This function is another interface to the MIR serialization format parser. 81 /// 82 /// It returns a MIR parser that works with the given memory buffer and that can 83 /// parse the embedded LLVM IR module and initialize the machine functions by 84 /// parsing the machine function's state. 85 /// 86 /// \param Contents - The MemoryBuffer containing the machine level IR. 87 /// \param Context - Context which will be used for the parsed LLVM IR module. 88 std::unique_ptr<MIRParser> 89 createMIRParser(std::unique_ptr<MemoryBuffer> Contents, LLVMContext &Context, 90 std::function<void(Function &)> ProcessIRFunction = nullptr); 91 92 } // end namespace llvm 93 94 #endif // LLVM_CODEGEN_MIRPARSER_MIRPARSER_H 95