MLIR  19.0.0git
ModuleToObject.h
Go to the documentation of this file.
1 //===- ModuleToObject.h - Module to object base class -----------*- 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 file declares the base class for transforming operations into binary
10 // objects.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TARGET_LLVM_MODULETOOBJECT_H
15 #define MLIR_TARGET_LLVM_MODULETOOBJECT_H
16 
17 #include "mlir/IR/Operation.h"
18 #include "llvm/IR/Module.h"
19 
20 namespace llvm {
21 class TargetMachine;
22 } // namespace llvm
23 
24 namespace mlir {
25 namespace LLVM {
26 class ModuleTranslation;
27 /// Utility base class for transforming operations into binary objects, by
28 /// default it returns the serialized LLVM bitcode for the module. The
29 /// operations being transformed must be translatable into LLVM IR.
31 public:
32  ModuleToObject(Operation &module, StringRef triple, StringRef chip,
33  StringRef features = {}, int optLevel = 3);
34  virtual ~ModuleToObject();
35 
36  /// Returns the operation being serialized.
38 
39  /// Runs the serialization pipeline, returning `std::nullopt` on error.
40  virtual std::optional<SmallVector<char, 0>> run();
41 
42 protected:
43  // Hooks to be implemented by derived classes.
44 
45  /// Hook for computing the Datalayout
46  virtual void setDataLayoutAndTriple(llvm::Module &module);
47 
48  /// Hook for loading bitcode files, returns std::nullopt on failure.
49  virtual std::optional<SmallVector<std::unique_ptr<llvm::Module>>>
50  loadBitcodeFiles(llvm::Module &module) {
52  }
53 
54  /// Hook for performing additional actions on a loaded bitcode file.
55  virtual LogicalResult handleBitcodeFile(llvm::Module &module) {
56  return success();
57  }
58 
59  /// Hook for performing additional actions on the llvmModule pre linking.
60  virtual void handleModulePreLink(llvm::Module &module) {}
61 
62  /// Hook for performing additional actions on the llvmModule post linking.
63  virtual void handleModulePostLink(llvm::Module &module) {}
64 
65  /// Serializes the LLVM IR bitcode to an object file, by default it serializes
66  /// to LLVM bitcode.
67  virtual std::optional<SmallVector<char, 0>>
68  moduleToObject(llvm::Module &llvmModule);
69 
70 protected:
71  /// Create the target machine based on the target triple and chip.
72  /// This can fail if the target is not available.
73  std::optional<llvm::TargetMachine *> getOrCreateTargetMachine();
74 
75  /// Loads a bitcode file from path.
76  std::unique_ptr<llvm::Module> loadBitcodeFile(llvm::LLVMContext &context,
77  StringRef path);
78 
79  /// Loads multiple bitcode files.
81  llvm::LLVMContext &context, ArrayRef<std::string> fileList,
82  SmallVector<std::unique_ptr<llvm::Module>> &llvmModules,
83  bool failureOnError = true);
84 
85  /// Translates the operation to LLVM IR.
86  std::unique_ptr<llvm::Module>
87  translateToLLVMIR(llvm::LLVMContext &llvmContext);
88 
89  /// Link the llvmModule to other bitcode file.
90  LogicalResult linkFiles(llvm::Module &module,
91  SmallVector<std::unique_ptr<llvm::Module>> &&libs);
92 
93  /// Optimize the module.
94  virtual LogicalResult optimizeModule(llvm::Module &module, int optL);
95 
96  /// Utility function for translating to ISA, returns `std::nullopt` on
97  /// failure.
98  static std::optional<std::string>
99  translateToISA(llvm::Module &llvmModule, llvm::TargetMachine &targetMachine);
100 
101 protected:
102  /// Module to transform to a binary object.
104 
105  /// Target triple.
106  StringRef triple;
107 
108  /// Target chip.
109  StringRef chip;
110 
111  /// Target features.
112  StringRef features;
113 
114  /// Optimization level.
115  int optLevel;
116 
117 private:
118  /// The TargetMachine created for the given Triple, if available.
119  /// Accessible through `getOrCreateTargetMachine()`.
120  std::unique_ptr<llvm::TargetMachine> targetMachine;
121 };
122 } // namespace LLVM
123 } // namespace mlir
124 
125 #endif // MLIR_TARGET_LLVM_MODULETOOBJECT_H
Utility base class for transforming operations into binary objects, by default it returns the seriali...
StringRef features
Target features.
std::unique_ptr< llvm::Module > translateToLLVMIR(llvm::LLVMContext &llvmContext)
Translates the operation to LLVM IR.
virtual std::optional< SmallVector< char, 0 > > run()
Runs the serialization pipeline, returning std::nullopt on error.
static std::optional< std::string > translateToISA(llvm::Module &llvmModule, llvm::TargetMachine &targetMachine)
Utility function for translating to ISA, returns std::nullopt on failure.
virtual std::optional< SmallVector< char, 0 > > moduleToObject(llvm::Module &llvmModule)
Serializes the LLVM IR bitcode to an object file, by default it serializes to LLVM bitcode.
virtual void setDataLayoutAndTriple(llvm::Module &module)
Hook for computing the Datalayout.
virtual void handleModulePreLink(llvm::Module &module)
Hook for performing additional actions on the llvmModule pre linking.
StringRef triple
Target triple.
int optLevel
Optimization level.
ModuleToObject(Operation &module, StringRef triple, StringRef chip, StringRef features={}, int optLevel=3)
virtual LogicalResult handleBitcodeFile(llvm::Module &module)
Hook for performing additional actions on a loaded bitcode file.
std::optional< llvm::TargetMachine * > getOrCreateTargetMachine()
Create the target machine based on the target triple and chip.
Operation & getOperation()
Returns the operation being serialized.
LogicalResult loadBitcodeFilesFromList(llvm::LLVMContext &context, ArrayRef< std::string > fileList, SmallVector< std::unique_ptr< llvm::Module >> &llvmModules, bool failureOnError=true)
Loads multiple bitcode files.
virtual LogicalResult optimizeModule(llvm::Module &module, int optL)
Optimize the module.
LogicalResult linkFiles(llvm::Module &module, SmallVector< std::unique_ptr< llvm::Module >> &&libs)
Link the llvmModule to other bitcode file.
StringRef chip
Target chip.
std::unique_ptr< llvm::Module > loadBitcodeFile(llvm::LLVMContext &context, StringRef path)
Loads a bitcode file from path.
virtual std::optional< SmallVector< std::unique_ptr< llvm::Module > > > loadBitcodeFiles(llvm::Module &module)
Hook for loading bitcode files, returns std::nullopt on failure.
virtual void handleModulePostLink(llvm::Module &module)
Hook for performing additional actions on the llvmModule post linking.
Operation & module
Module to transform to a binary object.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Include the generated interface declarations.
Definition: CallGraph.h:229
Include the generated interface declarations.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26