Skip to content

Commit 0881a4f

Browse files
committed
[mlir] make ModuleTranslation mapping fields private
ModuleTranslation contains multiple fields that keep track of the mappings between various MLIR and LLVM IR components. The original ModuleTranslation extension model was based on inheritance, with these fields being protected and thus accessible in the ModuleTranslation and derived classes. The inheritance-based model doesn't scale to translation of more than one derived dialect and will be progressively replaced with a more flexible one based on dialect interfaces and a translation state that is separate from ModuleTranslation. This change prepares the replacement by making the mappings private and providing public methods to access them. Depends On D96436 Reviewed By: mehdi_amini Differential Revision: https://reviews.llvm.org/D96437
1 parent 9a08f76 commit 0881a4f

File tree

3 files changed

+132
-89
lines changed

3 files changed

+132
-89
lines changed

mlir/include/mlir/Target/LLVMIR/ModuleTranslation.h

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,63 @@ class ModuleTranslation {
7777
/// module requirements.
7878
static Block &getModuleBody(Operation *m) { return m->getRegion(0).front(); }
7979

80+
/// Stores the mapping between a function name and its LLVM IR representation.
81+
void mapFunction(StringRef name, llvm::Function *func) {
82+
auto result = functionMapping.try_emplace(name, func);
83+
(void)result;
84+
assert(result.second &&
85+
"attempting to map a function that is already mapped");
86+
}
87+
88+
/// Finds an LLVM IR function by its name.
89+
llvm::Function *lookupFunction(StringRef name) const {
90+
return functionMapping.lookup(name);
91+
}
92+
93+
/// Stores the mapping between an MLIR value and its LLVM IR counterpart.
94+
void mapValue(Value mlir, llvm::Value *llvm) { mapValue(mlir) = llvm; }
95+
96+
/// Provides write-once access to store the LLVM IR value corresponding to the
97+
/// given MLIR value.
98+
llvm::Value *&mapValue(Value value) {
99+
llvm::Value *&llvm = valueMapping[value];
100+
assert(llvm == nullptr &&
101+
"attempting to map a value that is already mapped");
102+
return llvm;
103+
}
104+
105+
/// Finds an LLVM IR value corresponding to the given MLIR value.
106+
llvm::Value *lookupValue(Value value) const {
107+
return valueMapping.lookup(value);
108+
}
109+
110+
/// Stores the mapping between an MLIR block and LLVM IR basic block.
111+
void mapBlock(Block *mlir, llvm::BasicBlock *llvm) {
112+
auto result = blockMapping.try_emplace(mlir, llvm);
113+
(void)result;
114+
assert(result.second && "attempting to map a block that is already mapped");
115+
}
116+
117+
/// Finds an LLVM IR basic block that corresponds to the given MLIR block.
118+
llvm::BasicBlock *lookupBlock(Block *block) const {
119+
return blockMapping.lookup(block);
120+
}
121+
122+
/// Stores the mapping between an MLIR operation with successors and a
123+
/// corresponding LLVM IR instruction.
124+
void mapBranch(Operation *mlir, llvm::Instruction *llvm) {
125+
auto result = branchMapping.try_emplace(mlir, llvm);
126+
(void)result;
127+
assert(result.second &&
128+
"attempting to map a branch that is already mapped");
129+
}
130+
131+
/// Finds an LLVM IR instruction that corresponds to the given MLIR operation
132+
/// with successors.
133+
llvm::Instruction *lookupBranch(Operation *op) const {
134+
return branchMapping.lookup(op);
135+
}
136+
80137
protected:
81138
/// Translate the given MLIR module expressed in MLIR LLVM IR dialect into an
82139
/// LLVM IR module. The MLIR LLVM IR dialect holds a pointer to an
@@ -94,8 +151,6 @@ class ModuleTranslation {
94151
virtual LogicalResult convertOmpMaster(Operation &op,
95152
llvm::IRBuilder<> &builder);
96153
void convertOmpOpRegions(Region &region, StringRef blockName,
97-
DenseMap<Value, llvm::Value *> &valueMapping,
98-
DenseMap<Block *, llvm::BasicBlock *> &blockMapping,
99154
llvm::BasicBlock &sourceBlock,
100155
llvm::BasicBlock &continuationBlock,
101156
llvm::IRBuilder<> &builder,
@@ -147,7 +202,7 @@ class ModuleTranslation {
147202
/// A stateful object used to translate types.
148203
TypeToLLVMIRTranslator typeTranslator;
149204

150-
protected:
205+
private:
151206
/// Mappings between original and translated values, used for lookups.
152207
llvm::StringMap<llvm::Function *> functionMapping;
153208
DenseMap<Value, llvm::Value *> valueMapping;

0 commit comments

Comments
 (0)