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