Skip to content

Commit 59c60ae

Browse files
committed
[CIR][NFC] Move LoweringPrepare into CIRGen (#1092)
Move LP into CIRGen and give it a handle on the CIRGenModule. A lot of code has been duplicated from CIRGen into cir/Dialect/Transforms in order to let LP live there, but with more necessary CIRGen features (e.g. EH scope and cleanups) going to be used in LP it doesn't make sense to keep it separate. Add this patch that just refactors LoweringPrepare into the CIRGen directory and give it a handle on the CGM.
1 parent 74de6c4 commit 59c60ae

26 files changed

+67
-47
lines changed

clang/include/clang/CIR/CIRGenerator.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class CIRGenerator : public clang::ASTConsumer {
9898
std::unique_ptr<mlir::MLIRContext> takeContext() {
9999
return std::move(mlirCtx);
100100
};
101+
clang::CIRGen::CIRGenModule &getCGM() { return *CGM; }
101102

102103
bool verifyModule();
103104

clang/include/clang/CIR/CIRToCIRPasses.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
namespace clang {
2020
class ASTContext;
21-
}
21+
namespace CIRGen {
22+
class CIRGenModule;
23+
} // namespace CIRGen
24+
} // namespace clang
2225

2326
namespace mlir {
2427
class MLIRContext;
@@ -30,12 +33,12 @@ namespace cir {
3033
// Run set of cleanup/prepare/etc passes CIR <-> CIR.
3134
mlir::LogicalResult runCIRToCIRPasses(
3235
mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx,
33-
clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime,
34-
llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer,
35-
llvm::StringRef idiomRecognizerOpts, bool enableLibOpt,
36-
llvm::StringRef libOptOpts, std::string &passOptParsingFailure,
37-
bool enableCIRSimplify, bool flattenCIR, bool emitMLIR,
38-
bool enableCallConvLowering, bool enableMem2reg);
36+
clang::CIRGen::CIRGenModule &cgm, clang::ASTContext &astCtx,
37+
bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts,
38+
bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts,
39+
bool enableLibOpt, llvm::StringRef libOptOpts,
40+
std::string &passOptParsingFailure, bool enableCIRSimplify, bool flattenCIR,
41+
bool emitMLIR, bool enableCallConvLowering, bool enableMem2reg);
3942

4043
} // namespace cir
4144

clang/include/clang/CIR/Dialect/Passes.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
namespace clang {
1919
class ASTContext;
20-
}
20+
namespace CIRGen {
21+
class CIRGenModule;
22+
} // namespace CIRGen
23+
} // namespace clang
24+
2125
namespace mlir {
2226

2327
std::unique_ptr<Pass> createLifetimeCheckPass();
@@ -31,7 +35,9 @@ std::unique_ptr<Pass> createCIRSimplifyPass();
3135
std::unique_ptr<Pass> createDropASTPass();
3236
std::unique_ptr<Pass> createSCFPreparePass();
3337
std::unique_ptr<Pass> createLoweringPreparePass();
34-
std::unique_ptr<Pass> createLoweringPreparePass(clang::ASTContext *astCtx);
38+
std::unique_ptr<Pass>
39+
createLoweringPreparePass(clang::ASTContext *astCtx,
40+
clang::CIRGen::CIRGenModule &cgm);
3541
std::unique_ptr<Pass> createIdiomRecognizerPass();
3642
std::unique_ptr<Pass> createIdiomRecognizerPass(clang::ASTContext *astCtx);
3743
std::unique_ptr<Pass> createLibOptPass();

clang/include/clang/CIR/Dialect/Passes.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def SCFPrepare : Pass<"cir-mlir-scf-prepare"> {
109109

110110
def HoistAllocas : Pass<"cir-hoist-allocas"> {
111111
let summary = "Hoist allocas to the entry of the function";
112-
let description = [{
112+
let description = [{
113113
This pass hoist all non-dynamic allocas to the entry of the function.
114114
This is helpful for later code generation.
115115
}];
@@ -119,7 +119,7 @@ def HoistAllocas : Pass<"cir-hoist-allocas"> {
119119

120120
def FlattenCFG : Pass<"cir-flatten-cfg"> {
121121
let summary = "Produces flatten cfg";
122-
let description = [{
122+
let description = [{
123123
This pass transforms CIR and inline all the nested regions. Thus,
124124
the next post condtions are met after the pass applied:
125125
- there is not any nested region in a function body

clang/lib/CIR/CodeGen/CIRPasses.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "clang/AST/ASTContext.h"
1414
#include "clang/CIR/Dialect/Passes.h"
1515

16+
#include "CIRGenModule.h"
1617
#include "mlir/IR/BuiltinOps.h"
1718
#include "mlir/Pass/Pass.h"
1819
#include "mlir/Pass/PassManager.h"
@@ -24,12 +25,12 @@
2425
namespace cir {
2526
mlir::LogicalResult runCIRToCIRPasses(
2627
mlir::ModuleOp theModule, mlir::MLIRContext *mlirCtx,
27-
clang::ASTContext &astCtx, bool enableVerifier, bool enableLifetime,
28-
llvm::StringRef lifetimeOpts, bool enableIdiomRecognizer,
29-
llvm::StringRef idiomRecognizerOpts, bool enableLibOpt,
30-
llvm::StringRef libOptOpts, std::string &passOptParsingFailure,
31-
bool enableCIRSimplify, bool flattenCIR, bool emitMLIR,
32-
bool enableCallConvLowering, bool enableMem2Reg) {
28+
clang::CIRGen::CIRGenModule &cgm, clang::ASTContext &astCtx,
29+
bool enableVerifier, bool enableLifetime, llvm::StringRef lifetimeOpts,
30+
bool enableIdiomRecognizer, llvm::StringRef idiomRecognizerOpts,
31+
bool enableLibOpt, llvm::StringRef libOptOpts,
32+
std::string &passOptParsingFailure, bool enableCIRSimplify, bool flattenCIR,
33+
bool emitMLIR, bool enableCallConvLowering, bool enableMem2Reg) {
3334

3435
llvm::TimeTraceScope scope("CIR To CIR Passes");
3536

@@ -73,7 +74,7 @@ mlir::LogicalResult runCIRToCIRPasses(
7374
if (enableCIRSimplify)
7475
pm.addPass(mlir::createCIRSimplifyPass());
7576

76-
pm.addPass(mlir::createLoweringPreparePass(&astCtx));
77+
pm.addPass(mlir::createLoweringPreparePass(&astCtx, cgm));
7778

7879
if (flattenCIR || enableMem2Reg)
7980
mlir::populateCIRPreLoweringPasses(pm, enableCallConvLowering);

clang/lib/CIR/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ add_clang_library(clangCIR
4444
CIRPasses.cpp
4545
CIRRecordLayoutBuilder.cpp
4646
ConstantInitBuilder.cpp
47+
LoweringPrepare.cpp
4748
TargetInfo.cpp
4849

4950
DEPENDS

clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp renamed to clang/lib/CIR/CodeGen/LoweringPrepare.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "LoweringPrepareCXXABI.h"
10-
#include "PassDetail.h"
9+
#include "CIRGenModule.h"
10+
1111
#include "mlir/IR/BuiltinAttributes.h"
1212
#include "mlir/IR/Region.h"
1313
#include "clang/AST/ASTContext.h"
@@ -19,6 +19,8 @@
1919
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2020
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2121
#include "clang/CIR/Dialect/Passes.h"
22+
#include "clang/CIR/Dialect/Transforms/LoweringPrepareCXXABI.h"
23+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
2224
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
2325
#include "llvm/ADT/APFloat.h"
2426
#include "llvm/ADT/SmallVector.h"
@@ -121,6 +123,7 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
121123

122124
clang::ASTContext *astCtx;
123125
std::shared_ptr<cir::LoweringPrepareCXXABI> cxxABI;
126+
clang::CIRGen::CIRGenModule *cgm;
124127

125128
void setASTContext(clang::ASTContext *c) {
126129
astCtx = c;
@@ -147,6 +150,8 @@ struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
147150
}
148151
}
149152

153+
void setCGM(clang::CIRGen::CIRGenModule &cgm) { this->cgm = &cgm; }
154+
150155
/// Tracks current module.
151156
ModuleOp theModule;
152157

@@ -1205,8 +1210,10 @@ std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
12051210
}
12061211

12071212
std::unique_ptr<Pass>
1208-
mlir::createLoweringPreparePass(clang::ASTContext *astCtx) {
1213+
mlir::createLoweringPreparePass(clang::ASTContext *astCtx,
1214+
clang::CIRGen::CIRGenModule &cgm) {
12091215
auto pass = std::make_unique<LoweringPreparePass>();
12101216
pass->setASTContext(astCtx);
1217+
pass->setCGM(cgm);
12111218
return std::move(pass);
12121219
}

0 commit comments

Comments
 (0)