MIRParser.h 3.5 KB

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