xref: /llvm-project/llvm/include/llvm/CodeGen/MIRParser/MIRParser.h (revision 6ea0c0a28343b2676baf480db490b5a27fa11d7c)
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 template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager;
38 using ModuleAnalysisManager = AnalysisManager<Module>;
39 
40 typedef llvm::function_ref<std::optional<std::string>(StringRef, StringRef)>
41     DataLayoutCallbackTy;
42 
43 /// This class initializes machine functions by applying the state loaded from
44 /// a MIR file.
45 class MIRParser {
46   std::unique_ptr<MIRParserImpl> Impl;
47 
48 public:
49   MIRParser(std::unique_ptr<MIRParserImpl> Impl);
50   MIRParser(const MIRParser &) = delete;
51   ~MIRParser();
52 
53   /// Parses the optional LLVM IR module in the MIR file.
54   ///
55   /// A new, empty module is created if the LLVM IR isn't present.
56   /// \returns nullptr if a parsing error occurred.
57   std::unique_ptr<Module>
58   parseIRModule(DataLayoutCallbackTy DataLayoutCallback =
59                     [](StringRef, StringRef) { return std::nullopt; });
60 
61   /// Parses MachineFunctions in the MIR file and add them to the given
62   /// MachineModuleInfo \p MMI.
63   ///
64   /// \returns true if an error occurred.
65   bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
66 
67   /// Parses MachineFunctions in the MIR file and add them as the result
68   /// of MachineFunctionAnalysis in ModulePassManager \p MAM.
69   /// User should register at least MachineFunctionAnalysis,
70   /// MachineModuleAnalysis, FunctionAnalysisManagerModuleProxy and
71   /// PassInstrumentationAnalysis in \p MAM before parsing MIR.
72   ///
73   /// \returns true if an error occurred.
74   bool parseMachineFunctions(Module &M, ModuleAnalysisManager &MAM);
75 };
76 
77 /// This function is the main interface to the MIR serialization format parser.
78 ///
79 /// It reads in a MIR file and returns a MIR parser that can parse the embedded
80 /// LLVM IR module and initialize the machine functions by parsing the machine
81 /// function's state.
82 ///
83 /// \param Filename - The name of the file to parse.
84 /// \param Error - Error result info.
85 /// \param Context - Context which will be used for the parsed LLVM IR module.
86 /// \param ProcessIRFunction - function to run on every IR function or stub
87 /// loaded from the MIR file.
88 std::unique_ptr<MIRParser> createMIRParserFromFile(
89     StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
90     std::function<void(Function &)> ProcessIRFunction = nullptr);
91 
92 /// This function is another interface to the MIR serialization format parser.
93 ///
94 /// It returns a MIR parser that works with the given memory buffer and that can
95 /// parse the embedded LLVM IR module and initialize the machine functions by
96 /// parsing the machine function's state.
97 ///
98 /// \param Contents - The MemoryBuffer containing the machine level IR.
99 /// \param Context - Context which will be used for the parsed LLVM IR module.
100 std::unique_ptr<MIRParser>
101 createMIRParser(std::unique_ptr<MemoryBuffer> Contents, LLVMContext &Context,
102                 std::function<void(Function &)> ProcessIRFunction = nullptr);
103 
104 } // end namespace llvm
105 
106 #endif // LLVM_CODEGEN_MIRPARSER_MIRPARSER_H
107