Skip to content

Commit 57aba62

Browse files
htyulanza
authored andcommitted
[CIR][Lowering] Add an empty LoweringPrepare pass (#236)
This change is a prerequisite of #235
1 parent 4155a13 commit 57aba62

File tree

6 files changed

+68
-0
lines changed

6 files changed

+68
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ std::unique_ptr<Pass> createLifetimeCheckPass(ArrayRef<StringRef> remark,
2828
clang::ASTContext *astCtx);
2929
std::unique_ptr<Pass> createMergeCleanupsPass();
3030
std::unique_ptr<Pass> createDropASTPass();
31+
std::unique_ptr<Pass> createLoweringPreparePass();
32+
std::unique_ptr<Pass> createLoweringPreparePass(clang::ASTContext *astCtx);
3133

3234
//===----------------------------------------------------------------------===//
3335
// Registration

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,14 @@ def DropAST : Pass<"cir-drop-ast"> {
6565
let dependentDialects = ["cir::CIRDialect"];
6666
}
6767

68+
def LoweringPrepare : Pass<"cir-lowering-prepare"> {
69+
let summary = "Preparation work before lowering to LLVM dialect";
70+
let description = [{
71+
This pass does preparation work for LLVM lowering. For example, it may
72+
expand the global variable initialziation in a more ABI-friendly form.
73+
}];
74+
let constructor = "mlir::createLoweringPreparePass()";
75+
let dependentDialects = ["cir::CIRDialect"];
76+
}
77+
6878
#endif // MLIR_DIALECT_CIR_PASSES

clang/lib/CIR/CodeGen/CIRPasses.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule,
4545
pm.addPass(std::move(lifetimePass));
4646
}
4747

48+
pm.addPass(mlir::createLoweringPreparePass(&astCtx));
49+
4850
// FIXME: once CIRCodenAction fixes emission other than CIR we
4951
// need to run this right before dialect emission.
5052
pm.addPass(mlir::createDropASTPass());

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_clang_library(MLIRCIRTransforms
22
LifetimeCheck.cpp
3+
LoweringPrepare.cpp
34
MergeCleanups.cpp
45
DropAST.cpp
56

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
using namespace mlir;
15+
using namespace cir;
16+
17+
namespace {
18+
struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
19+
LoweringPreparePass() = default;
20+
void runOnOperation() override;
21+
22+
///
23+
/// AST related
24+
/// -----------
25+
26+
clang::ASTContext *astCtx;
27+
void setASTContext(clang::ASTContext *c) { astCtx = c; }
28+
29+
/// Tracks current module.
30+
ModuleOp theModule;
31+
};
32+
} // namespace
33+
34+
35+
void LoweringPreparePass::runOnOperation() {
36+
assert(astCtx && "Missing ASTContext, please construct with the right ctor");
37+
auto* op = getOperation();
38+
if (isa<::mlir::ModuleOp>(op)) {
39+
theModule = cast<::mlir::ModuleOp>(op);
40+
}
41+
}
42+
43+
std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
44+
return std::make_unique<LoweringPreparePass>();
45+
}
46+
47+
std::unique_ptr<Pass> mlir::createLoweringPreparePass(clang::ASTContext *astCtx) {
48+
auto pass = std::make_unique<LoweringPreparePass>();
49+
pass->setASTContext(astCtx);
50+
return std::move(pass);
51+
}

clang/test/CIR/mlirprint.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ int foo(void) {
1010

1111
// CIR: IR Dump After MergeCleanups (cir-merge-cleanups)
1212
// CIR: cir.func @foo() -> !s32i
13+
// CIR: IR Dump After LoweringPrepare (cir-lowering-prepare)
14+
// CIR: cir.func @foo() -> !s32i
1315
// CIR: IR Dump After DropAST (cir-drop-ast)
1416
// CIR: cir.func @foo() -> !s32i
1517
// LLVM: IR Dump After cir::direct::ConvertCIRToLLVMPass (cir-to-llvm)

0 commit comments

Comments
 (0)