Skip to content

Commit 4463352

Browse files
authored
[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 5e91e4f commit 4463352

26 files changed

+69
-48
lines changed

clang/include/clang/CIR/CIRGenerator.h

+1
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

+10-7
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

+8-2
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

+2-2
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

+8-7
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

+1
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

+10-3
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
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/Dialect/Func/IR/FuncOps.h"
1110
#include "mlir/IR/Block.h"
1211
#include "mlir/IR/Operation.h"
@@ -16,6 +15,7 @@
1615
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1716
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1817
#include "clang/CIR/Dialect/Passes.h"
18+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1919

2020
using namespace mlir;
2121
using namespace cir;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/Dialect/Func/IR/FuncOps.h"
1110
#include "mlir/IR/Block.h"
1211
#include "mlir/IR/Operation.h"
@@ -16,6 +15,7 @@
1615
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1716
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1817
#include "clang/CIR/Dialect/Passes.h"
18+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1919
#include "llvm/ADT/SmallVector.h"
2020

2121
using namespace mlir;

clang/lib/CIR/Dialect/Transforms/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ add_subdirectory(TargetLowering)
22

33
add_clang_library(MLIRCIRTransforms
44
LifetimeCheck.cpp
5-
LoweringPrepare.cpp
65
CIRCanonicalize.cpp
76
CIRSimplify.cpp
87
DropAST.cpp

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#include "clang/CIR/Dialect/Passes.h"
1010

11-
#include "PassDetail.h"
1211
#include "mlir/Dialect/Func/IR/FuncOps.h"
1312
#include "clang/AST/ASTContext.h"
1413
#include "clang/CIR/Dialect/IR/CIRDialect.h"
14+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1515

1616
#include "llvm/ADT/SetOperations.h"
1717
#include "llvm/ADT/SmallSet.h"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010
// function region.
1111
//
1212
//===----------------------------------------------------------------------===//
13-
#include "PassDetail.h"
1413
#include "mlir/Dialect/Func/IR/FuncOps.h"
1514
#include "mlir/IR/PatternMatch.h"
1615
#include "mlir/Support/LogicalResult.h"
1716
#include "mlir/Transforms/DialectConversion.h"
1817
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1918
#include "clang/CIR/Dialect/IR/CIRDialect.h"
2019
#include "clang/CIR/Dialect/Passes.h"
20+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
2121

2222
using namespace mlir;
2323
using namespace cir;

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#include "PassDetail.h"
21
#include "mlir/Dialect/Func/IR/FuncOps.h"
32
#include "mlir/IR/PatternMatch.h"
43
#include "mlir/Support/LogicalResult.h"
54
#include "mlir/Transforms/DialectConversion.h"
65
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
76
#include "clang/CIR/Dialect/IR/CIRDialect.h"
87
#include "clang/CIR/Dialect/Passes.h"
8+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
99

1010
#include "llvm/Support/TimeProfiler.h"
1111

@@ -54,4 +54,4 @@ void GotoSolverPass::runOnOperation() {
5454

5555
std::unique_ptr<Pass> mlir::createGotoSolverPass() {
5656
return std::make_unique<GotoSolverPass>();
57-
}
57+
}

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

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

9-
#include "PassDetail.h"
109
#include "mlir/Dialect/Func/IR/FuncOps.h"
1110
#include "mlir/IR/PatternMatch.h"
1211
#include "mlir/Support/LogicalResult.h"
1312
#include "mlir/Transforms/DialectConversion.h"
1413
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1514
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1615
#include "clang/CIR/Dialect/Passes.h"
16+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1717

1818
#include "llvm/Support/TimeProfiler.h"
1919

@@ -62,4 +62,4 @@ void HoistAllocasPass::runOnOperation() {
6262

6363
std::unique_ptr<Pass> mlir::createHoistAllocasPass() {
6464
return std::make_unique<HoistAllocasPass>();
65-
}
65+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/Dialect/Func/IR/FuncOps.h"
1110
#include "mlir/IR/BuiltinAttributes.h"
1211
#include "mlir/IR/Region.h"
@@ -16,6 +15,7 @@
1615
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
1716
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1817
#include "clang/CIR/Dialect/Passes.h"
18+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1919
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/ADT/StringMap.h"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/Dialect/Func/IR/FuncOps.h"
1110
#include "mlir/IR/BuiltinAttributes.h"
1211
#include "mlir/IR/Region.h"
@@ -16,6 +15,7 @@
1615
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
1716
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1817
#include "clang/CIR/Dialect/Passes.h"
18+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1919
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
2020
#include "llvm/ADT/SmallVector.h"
2121
#include "llvm/ADT/StringMap.h"

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
10-
119
#include "clang/AST/ASTContext.h"
1210
#include "clang/AST/Attr.h"
1311
#include "clang/AST/DeclCXX.h"
1412
#include "clang/AST/DeclTemplate.h"
1513
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
1614
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1715
#include "clang/CIR/Dialect/Passes.h"
16+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1817

1918
#include "clang/CIR/Interfaces/CIRLoopOpInterface.h"
2019
#include "llvm/ADT/SetOperations.h"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/IR/PatternMatch.h"
1110
#include "mlir/Support/LogicalResult.h"
1211
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
1312
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1413
#include "clang/CIR/Dialect/Passes.h"
14+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1515

1616
using namespace mlir;
1717
using namespace cir;

clang/lib/CIR/Dialect/Transforms/StdHelpers.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "PassDetail.h"
109
#include "mlir/IR/BuiltinAttributes.h"
1110
#include "mlir/IR/Region.h"
1211
#include "clang/AST/ASTContext.h"
1312
#include "clang/Basic/Module.h"
1413
#include "clang/CIR/Dialect/Builder/CIRBaseBuilder.h"
1514
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1615
#include "clang/CIR/Dialect/Passes.h"
16+
#include "clang/CIR/Dialect/Transforms/PassDetail.h"
1717
#include "clang/CIR/Interfaces/ASTAttrInterfaces.h"
1818
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/ADT/StringMap.h"

clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/LoweringPrepareAArch64CXXABI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
// TODO(cir): Refactor this to follow some level of codegen parity.
1616

17-
#include "../LoweringPrepareItaniumCXXABI.h"
1817
#include "clang/AST/CharUnits.h"
1918
#include "clang/CIR/Dialect/IR/CIRDataLayout.h"
2019
#include "clang/CIR/Dialect/IR/CIRTypes.h"
20+
#include "clang/CIR/Dialect/Transforms/LoweringPrepareItaniumCXXABI.h"
2121
#include "clang/CIR/MissingFeatures.h"
2222

2323
#include <assert.h>

clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/LoweringPrepareItaniumCXXABI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// TODO(cir): Refactor this to follow some level of codegen parity.
1616

17-
#include "../LoweringPrepareItaniumCXXABI.h"
17+
#include "clang/CIR/Dialect/Transforms/LoweringPrepareItaniumCXXABI.h"
1818
#include "mlir/IR/BuiltinAttributes.h"
1919
#include "mlir/IR/Value.h"
2020
#include "mlir/IR/ValueRange.h"

0 commit comments

Comments
 (0)