Skip to content

Commit 8b87987

Browse files
seven-milesmeenai
authored andcommitted
[CIR][Transforms][NFC] Use unique_ptr to encapsulate LowerModule (llvm#752)
Currently `LowerModule` mimics `CodeGenModule` and uses many raw references. It cannot be moved or copied. Value semantic does not fit the need. For example, we cannot pass LowerModule around. A better practice would be to use `unique_ptr` to encapsulate it. In the future, we hold its ownership in some long-lived contexts (it's `CodeGeneratorImpl` for `CodeGenModule`) and pass references to it around safely.
1 parent c8a0eaa commit 8b87987

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

clang/lib/CIR/Dialect/Transforms/CallConvLowering.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,23 @@ struct CallConvLoweringPattern : public OpRewritePattern<FuncOp> {
3636
return op.emitError("function has no AST information");
3737

3838
auto modOp = op->getParentOfType<ModuleOp>();
39-
LowerModule lowerModule = createLowerModule(modOp, rewriter);
39+
std::unique_ptr<LowerModule> lowerModule =
40+
createLowerModule(modOp, rewriter);
4041

4142
// Rewrite function calls before definitions. This should be done before
4243
// lowering the definition.
4344
auto calls = op.getSymbolUses(module);
4445
if (calls.has_value()) {
4546
for (auto call : calls.value()) {
4647
auto callOp = cast<CallOp>(call.getUser());
47-
if (lowerModule.rewriteFunctionCall(callOp, op).failed())
48+
if (lowerModule->rewriteFunctionCall(callOp, op).failed())
4849
return failure();
4950
}
5051
}
5152

5253
// TODO(cir): Instead of re-emmiting every load and store, bitcast arguments
5354
// and return values to their ABI-specific counterparts when possible.
54-
if (lowerModule.rewriteFunctionDefinition(op).failed())
55+
if (lowerModule->rewriteFunctionDefinition(op).failed())
5556
return failure();
5657

5758
return success();

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,8 @@ LogicalResult LowerModule::rewriteFunctionCall(CallOp callOp, FuncOp funcOp) {
217217
}
218218

219219
// TODO: not to create it every time
220-
LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter) {
220+
std::unique_ptr<LowerModule> createLowerModule(ModuleOp module,
221+
PatternRewriter &rewriter) {
221222
// Fetch the LLVM data layout string.
222223
auto dataLayoutStr = cast<StringAttr>(
223224
module->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()));
@@ -237,7 +238,8 @@ LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter) {
237238
auto context = CIRLowerContext(module, langOpts);
238239
context.initBuiltinTypes(*targetInfo);
239240

240-
return LowerModule(context, module, dataLayoutStr, *targetInfo, rewriter);
241+
return std::make_unique<LowerModule>(context, module, dataLayoutStr,
242+
*targetInfo, rewriter);
241243
}
242244

243245
} // namespace cir

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerModule.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ class LowerModule {
9191
LogicalResult rewriteFunctionCall(CallOp callOp, FuncOp funcOp);
9292
};
9393

94-
LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter);
94+
std::unique_ptr<LowerModule> createLowerModule(ModuleOp module,
95+
PatternRewriter &rewriter);
9596

9697
} // namespace cir
9798
} // namespace mlir

0 commit comments

Comments
 (0)