My Project
ModuleTranslation.h
Go to the documentation of this file.
1 //===- ModuleTranslation.h - MLIR to LLVM conversion ------------*- C++ -*-===//
2 //
3 // Part of the MLIR 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 implements the translation between an MLIR LLVM dialect module and
10 // the corresponding LLVMIR module. It only handles core LLVM IR operations.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
15 #define MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
16 
18 #include "mlir/IR/Block.h"
19 #include "mlir/IR/Module.h"
20 #include "mlir/IR/Value.h"
21 
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/IRBuilder.h"
25 #include "llvm/IR/Value.h"
26 
27 namespace mlir {
28 class Attribute;
29 class Location;
30 class ModuleOp;
31 class Operation;
32 
33 namespace LLVM {
34 
35 class LLVMFuncOp;
36 
37 // Implementation class for module translation. Holds a reference to the module
38 // being translated, and the mappings between the original and the translated
39 // functions, basic blocks and values. It is practically easier to hold these
40 // mappings in one class since the conversion of control flow operations
41 // needs to look up block and function mappings.
43 public:
44  template <typename T = ModuleTranslation>
45  static std::unique_ptr<llvm::Module> translateModule(Operation *m) {
46  if (!satisfiesLLVMModule(m))
47  return nullptr;
48  if (failed(checkSupportedModuleOps(m)))
49  return nullptr;
50  auto llvmModule = prepareLLVMModule(m);
51  if (!llvmModule)
52  return nullptr;
53 
54  T translator(m);
55  translator.llvmModule = std::move(llvmModule);
56  translator.convertGlobals();
57  if (failed(translator.convertFunctions()))
58  return nullptr;
59 
60  return std::move(translator.llvmModule);
61  }
62 
65  static Block &getModuleBody(Operation *m) { return m->getRegion(0).front(); }
66 
67 protected:
68  // Translate the given MLIR module expressed in MLIR LLVM IR dialect into an
69  // LLVM IR module. The MLIR LLVM IR dialect holds a pointer to an
70  // LLVMContext, the LLVM IR module will be created in that context.
71  explicit ModuleTranslation(Operation *module) : mlirModule(module) {
72  assert(satisfiesLLVMModule(mlirModule) &&
73  "mlirModule should honor LLVM's module semantics.");
74  }
75  virtual ~ModuleTranslation() {}
76 
78  llvm::IRBuilder<> &builder);
79  static std::unique_ptr<llvm::Module> prepareLLVMModule(Operation *m);
80 
83 
84 private:
86  static LogicalResult checkSupportedModuleOps(Operation *m);
87 
88  LogicalResult convertFunctions();
89  void convertGlobals();
90  LogicalResult convertOneFunction(LLVMFuncOp func);
91  void connectPHINodes(LLVMFuncOp func);
92  LogicalResult convertBlock(Block &bb, bool ignoreArguments);
93 
94  llvm::Constant *getLLVMConstant(llvm::Type *llvmType, Attribute attr,
95  Location loc);
96 
97  // Original and translated module.
98  Operation *mlirModule;
99  std::unique_ptr<llvm::Module> llvmModule;
100 
101  // Mappings between llvm.mlir.global definitions and corresponding globals.
103 
104 protected:
105  // Mappings between original and translated values, used for lookups.
106  llvm::StringMap<llvm::Function *> functionMapping;
109 };
110 
111 } // namespace LLVM
112 } // namespace mlir
113 
114 #endif // MLIR_TARGET_LLVMIR_MODULETRANSLATION_H
Definition: InferTypeOpInterface.cpp:20
Definition: Operation.h:27
Definition: Attributes.h:139
Block represents an ordered list of Operations.
Definition: Block.h:21
Block & front()
Definition: Region.h:54
Definition: LLVM.h:48
bool failed(LogicalResult result)
Definition: LogicalResult.h:45
virtual ~ModuleTranslation()
Definition: ModuleTranslation.h:75
Definition: Location.h:52
Definition: ModuleTranslation.h:42
Definition: LogicalResult.h:18
DenseMap< Block *, llvm::BasicBlock * > blockMapping
Definition: ModuleTranslation.h:108
Definition: Attributes.h:53
bool satisfiesLLVMModule(Operation *op)
Definition: LLVMDialect.cpp:1687
static Block & getModuleBody(Operation *m)
Definition: ModuleTranslation.h:65
static std::unique_ptr< llvm::Module > prepareLLVMModule(Operation *m)
Definition: ModuleTranslation.cpp:497
Definition: LLVM.h:35
SmallVector< llvm::Value *, 8 > lookupValues(ValueRange values)
A helper to look up remapped operands in the value remapping table.
Definition: ModuleTranslation.cpp:488
ModuleTranslation(Operation *module)
Definition: ModuleTranslation.h:71
DenseMap< Value, llvm::Value * > valueMapping
Definition: ModuleTranslation.h:107
static std::unique_ptr< llvm::Module > translateModule(Operation *m)
Definition: ModuleTranslation.h:45
Definition: OperationSupport.h:640
virtual LogicalResult convertOperation(Operation &op, llvm::IRBuilder<> &builder)
Definition: ModuleTranslation.cpp:157
Region & getRegion(unsigned index)
Returns the region held by this operation at position &#39;index&#39;.
Definition: Operation.h:369
llvm::StringMap< llvm::Function * > functionMapping
Definition: ModuleTranslation.h:106