blob: a7c69e2d43ef65baa464af00f9a67a83a5877729 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- MIRParser.h - MIR serialization format parser ------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
24namespace llvm {
25
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020026class Function;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010027class MIRParserImpl;
28class MachineModuleInfo;
29class SMDiagnostic;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020030class StringRef;
31
32typedef llvm::function_ref<Optional<std::string>(StringRef)>
33 DataLayoutCallbackTy;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034
35/// This class initializes machine functions by applying the state loaded from
36/// a MIR file.
37class MIRParser {
38 std::unique_ptr<MIRParserImpl> Impl;
39
40public:
41 MIRParser(std::unique_ptr<MIRParserImpl> Impl);
42 MIRParser(const MIRParser &) = delete;
43 ~MIRParser();
44
45 /// Parses the optional LLVM IR module in the MIR file.
46 ///
47 /// A new, empty module is created if the LLVM IR isn't present.
48 /// \returns nullptr if a parsing error occurred.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020049 std::unique_ptr<Module> parseIRModule(
50 DataLayoutCallbackTy DataLayoutCallback = [](StringRef) { return None; });
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051
Andrew Scullcdfcccc2018-10-05 20:58:37 +010052 /// Parses MachineFunctions in the MIR file and add them to the given
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010053 /// MachineModuleInfo \p MMI.
54 ///
55 /// \returns true if an error occurred.
56 bool parseMachineFunctions(Module &M, MachineModuleInfo &MMI);
57};
58
59/// This function is the main interface to the MIR serialization format parser.
60///
61/// It reads in a MIR file and returns a MIR parser that can parse the embedded
62/// LLVM IR module and initialize the machine functions by parsing the machine
63/// function's state.
64///
65/// \param Filename - The name of the file to parse.
66/// \param Error - Error result info.
67/// \param Context - Context which will be used for the parsed LLVM IR module.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020068/// \param ProcessIRFunction - function to run on every IR function or stub
69/// loaded from the MIR file.
70std::unique_ptr<MIRParser> createMIRParserFromFile(
71 StringRef Filename, SMDiagnostic &Error, LLVMContext &Context,
72 std::function<void(Function &)> ProcessIRFunction = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010073
74/// This function is another interface to the MIR serialization format parser.
75///
76/// It returns a MIR parser that works with the given memory buffer and that can
77/// parse the embedded LLVM IR module and initialize the machine functions by
78/// parsing the machine function's state.
79///
80/// \param Contents - The MemoryBuffer containing the machine level IR.
81/// \param Context - Context which will be used for the parsed LLVM IR module.
82std::unique_ptr<MIRParser>
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020083createMIRParser(std::unique_ptr<MemoryBuffer> Contents, LLVMContext &Context,
84 std::function<void(Function &)> ProcessIRFunction = nullptr);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010085
86} // end namespace llvm
87
88#endif // LLVM_CODEGEN_MIRPARSER_MIRPARSER_H