Skip to content

Commit af99f18

Browse files
authored
[CIR] Upstream the basic structure of LoweringPrepare pass (#148545)
Upstream, the basic structure of the LoweringPrepare pass as a prerequisite for other ComplexType PR's #141365
1 parent c6ac07b commit af99f18

File tree

5 files changed

+59
-4
lines changed

5 files changed

+59
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ std::unique_ptr<Pass> createCIRCanonicalizePass();
2424
std::unique_ptr<Pass> createCIRFlattenCFGPass();
2525
std::unique_ptr<Pass> createCIRSimplifyPass();
2626
std::unique_ptr<Pass> createHoistAllocasPass();
27+
std::unique_ptr<Pass> createLoweringPreparePass();
2728

2829
void populateCIRPreLoweringPasses(mlir::OpPassManager &pm);
2930

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ def CIRSimplify : Pass<"cir-simplify"> {
3333
let summary = "Performs CIR simplification and code optimization";
3434
let description = [{
3535
The pass performs semantics-preserving code simplifications and optimizations
36-
on CIR while maintaining strict program correctness.
37-
36+
on CIR while maintaining strict program correctness.
37+
3838
Unlike the `cir-canonicalize` pass, these transformations may reduce the IR's
3939
structural similarity to the original source code as a trade-off for improved
4040
code quality. This can affect debugging fidelity by altering intermediate
41-
representations of folded expressions, hoisted operations, and other
41+
representations of folded expressions, hoisted operations, and other
4242
optimized constructs.
43-
43+
4444
Example transformations include ternary expression folding and code hoisting
4545
while preserving program semantics.
4646
}];
@@ -72,4 +72,15 @@ def CIRFlattenCFG : Pass<"cir-flatten-cfg"> {
7272
let dependentDialects = ["cir::CIRDialect"];
7373
}
7474

75+
def LoweringPrepare : Pass<"cir-lowering-prepare"> {
76+
let summary = "Lower to more fine-grained CIR operations before lowering to
77+
other dialects";
78+
let description = [{
79+
This pass does preparation work for lowering to other dialects. For example,
80+
it may expand the global variable initialziation in a more ABI-friendly form.
81+
}];
82+
let constructor = "mlir::createLoweringPreparePass()";
83+
let dependentDialects = ["cir::CIRDialect"];
84+
}
85+
7586
#endif // CLANG_CIR_DIALECT_PASSES_TD

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ add_clang_library(MLIRCIRTransforms
33
CIRSimplify.cpp
44
FlattenCFG.cpp
55
HoistAllocas.cpp
6+
LoweringPrepare.cpp
67

78
DEPENDS
89
MLIRCIRPassIncGen
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "PassDetail.h"
10+
#include "clang/AST/ASTContext.h"
11+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
12+
#include "clang/CIR/Dialect/Passes.h"
13+
14+
#include <memory>
15+
16+
using namespace mlir;
17+
using namespace cir;
18+
19+
namespace {
20+
struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
21+
LoweringPreparePass() = default;
22+
void runOnOperation() override;
23+
24+
void runOnOp(Operation *op);
25+
};
26+
27+
} // namespace
28+
29+
void LoweringPreparePass::runOnOp(Operation *op) {}
30+
31+
void LoweringPreparePass::runOnOperation() {
32+
llvm::SmallVector<Operation *> opsToTransform;
33+
34+
for (auto *o : opsToTransform)
35+
runOnOp(o);
36+
}
37+
38+
std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
39+
return std::make_unique<LoweringPreparePass>();
40+
}

clang/lib/CIR/Lowering/CIRPasses.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule,
3131
if (enableCIRSimplify)
3232
pm.addPass(mlir::createCIRSimplifyPass());
3333

34+
pm.addPass(mlir::createLoweringPreparePass());
35+
3436
pm.enableVerifier(enableVerifier);
3537
(void)mlir::applyPassManagerCLOptions(pm);
3638
return pm.run(theModule);

0 commit comments

Comments
 (0)