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/IR/Module.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include <memory>
23 
24 namespace llvm {
25 
26 class Function;
27 class MIRParserImpl;
28 class MachineModuleInfo;
29 class SMDiagnostic;
30 class StringRef;
31 
32 /// This class initializes machine functions by applying the state loaded from
33 /// a MIR file.
34 class MIRParser {
35   std::unique_ptr<MIRParserImpl> Impl;
36 
37 public:
38   MIRParser(std::unique_ptr<MIRParserImpl> Impl);
39   MIRParser(const MIRParser &) = delete;
40   ~MIRParser();
41 
42   /// Parses the optional LLVM IR module in the MIR file.
43   ///
44   /// A new, empty module is created if the LLVM IR isn't present.
45   /// \returns nullptr if a parsing error occurred.
46   std::unique_ptr<Module> parseIRModule();
47 
48   /// Parses MachineFunctions in the MIR file and add them to the given
49   /// MachineModuleInfo \p MMI.
50   ///
51   /// \returns true if an error occurred.
52   bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
53 };
54 
55 /// This function is the main interface to the MIR serialization format parser.
56 ///
57 /// It reads in a MIR file and returns a MIR parser that can parse the embedded
58 /// LLVM IR module and initialize the machine functions by parsing the machine
59 /// function's state.
60 ///
61 /// \param Filename - The name of the file to parse.
62 /// \param Error - Error result info.
63 /// \param Context - Context which will be used for the parsed LLVM IR module.
64 /// \param ProcessIRFunction - function to run on every IR function or stub
65 /// loaded from the MIR file.
66 std::unique_ptr<MIRParser> createMIRParserFromFile(
67     StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
68     std::function<void(Function &)> ProcessIRFunction = nullptr);
69 
70 /// This function is another interface to the MIR serialization format parser.
71 ///
72 /// It returns a MIR parser that works with the given memory buffer and that can
73 /// parse the embedded LLVM IR module and initialize the machine functions by
74 /// parsing the machine function's state.
75 ///
76 /// \param Contents - The MemoryBuffer containing the machine level IR.
77 /// \param Context - Context which will be used for the parsed LLVM IR module.
78 std::unique_ptr<MIRParser>
79 createMIRParser(std::unique_ptr<MemoryBuffer> Contents, LLVMContext &Context,
80                 std::function<void(Function &)> ProcessIRFunction = nullptr);
81 
82 } // end namespace llvm
83 
84 #endif // LLVM_CODEGEN_MIRPARSER_MIRPARSER_H
85