Skip to content

Commit d6005d1

Browse files
authored
[CIR][ABI][NFC] Make createLowerModule public (#734)
Although currently LowerModule is not ready for formal usage, we need it for target-specific lowering to LLVM. This PR temporarily public the symbol `createLowerModule` to reuse the logic of preparing a `LowerModule`, making it easier for future refactor (making `TargetLoweringInfo` available for most stages in CIR Lowering).
1 parent fa1e9b5 commit d6005d1

File tree

3 files changed

+35
-35
lines changed

3 files changed

+35
-35
lines changed

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

+2-34
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// FIXME(cir): This header file is not exposed to the public API, but can be
10-
// reused by CIR ABI lowering since it holds target-specific information.
11-
#include "../../../Basic/Targets.h"
129

1310
#include "TargetLowering/LowerModule.h"
1411
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1512
#include "mlir/IR/BuiltinOps.h"
1613
#include "mlir/IR/PatternMatch.h"
1714
#include "mlir/Pass/Pass.h"
1815
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19-
#include "clang/Basic/TargetOptions.h"
2016
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2117

2218
#define GEN_PASS_DEF_CALLCONVLOWERING
@@ -25,35 +21,6 @@
2521
namespace mlir {
2622
namespace cir {
2723

28-
namespace {
29-
30-
LowerModule createLowerModule(FuncOp op, PatternRewriter &rewriter) {
31-
auto module = op->getParentOfType<mlir::ModuleOp>();
32-
33-
// Fetch the LLVM data layout string.
34-
auto dataLayoutStr = cast<StringAttr>(
35-
module->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()));
36-
37-
// Fetch target information.
38-
llvm::Triple triple(
39-
cast<StringAttr>(module->getAttr("cir.triple")).getValue());
40-
clang::TargetOptions targetOptions;
41-
targetOptions.Triple = triple.str();
42-
auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);
43-
44-
// FIXME(cir): This just uses the default language options. We need to account
45-
// for custom options.
46-
// Create context.
47-
assert(!::cir::MissingFeatures::langOpts());
48-
clang::LangOptions langOpts;
49-
auto context = CIRLowerContext(module, langOpts);
50-
context.initBuiltinTypes(*targetInfo);
51-
52-
return LowerModule(context, module, dataLayoutStr, *targetInfo, rewriter);
53-
}
54-
55-
} // namespace
56-
5724
//===----------------------------------------------------------------------===//
5825
// Rewrite Patterns
5926
//===----------------------------------------------------------------------===//
@@ -68,7 +35,8 @@ struct CallConvLoweringPattern : public OpRewritePattern<FuncOp> {
6835
if (!op.getAst())
6936
return op.emitError("function has no AST information");
7037

71-
LowerModule lowerModule = createLowerModule(op, rewriter);
38+
auto modOp = op->getParentOfType<ModuleOp>();
39+
LowerModule lowerModule = createLowerModule(modOp, rewriter);
7240

7341
// Rewrite function calls before definitions. This should be done before
7442
// lowering the definition.

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

+31-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#include "LowerModule.h"
14+
// FIXME(cir): This header file is not exposed to the public API, but can be
15+
// reused by CIR ABI lowering since it holds target-specific information.
16+
#include "../../../../Basic/Targets.h"
17+
#include "clang/Basic/TargetOptions.h"
18+
1519
#include "CIRLowerContext.h"
1620
#include "LowerFunction.h"
21+
#include "LowerModule.h"
1722
#include "TargetInfo.h"
23+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1824
#include "mlir/IR/Attributes.h"
1925
#include "mlir/IR/BuiltinAttributes.h"
2026
#include "mlir/IR/PatternMatch.h"
@@ -208,5 +214,29 @@ LogicalResult LowerModule::rewriteFunctionCall(CallOp callOp, FuncOp funcOp) {
208214
return success();
209215
}
210216

217+
// TODO: not to create it every time
218+
LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter) {
219+
// Fetch the LLVM data layout string.
220+
auto dataLayoutStr = cast<StringAttr>(
221+
module->getAttr(LLVM::LLVMDialect::getDataLayoutAttrName()));
222+
223+
// Fetch target information.
224+
llvm::Triple triple(
225+
cast<StringAttr>(module->getAttr("cir.triple")).getValue());
226+
clang::TargetOptions targetOptions;
227+
targetOptions.Triple = triple.str();
228+
auto targetInfo = clang::targets::AllocateTarget(triple, targetOptions);
229+
230+
// FIXME(cir): This just uses the default language options. We need to account
231+
// for custom options.
232+
// Create context.
233+
assert(!::cir::MissingFeatures::langOpts());
234+
clang::LangOptions langOpts;
235+
auto context = CIRLowerContext(module, langOpts);
236+
context.initBuiltinTypes(*targetInfo);
237+
238+
return LowerModule(context, module, dataLayoutStr, *targetInfo, rewriter);
239+
}
240+
211241
} // namespace cir
212242
} // namespace mlir

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

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

94+
LowerModule createLowerModule(ModuleOp module, PatternRewriter &rewriter);
95+
9496
} // namespace cir
9597
} // namespace mlir
9698

0 commit comments

Comments
 (0)