Skip to content

[CIR] Upstream the basic structure of LoweringPrepare pass #148545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/CIR/Dialect/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ std::unique_ptr<Pass> createCIRCanonicalizePass();
std::unique_ptr<Pass> createCIRFlattenCFGPass();
std::unique_ptr<Pass> createCIRSimplifyPass();
std::unique_ptr<Pass> createHoistAllocasPass();
std::unique_ptr<Pass> createLoweringPreparePass();

void populateCIRPreLoweringPasses(mlir::OpPassManager &pm);

Expand Down
19 changes: 15 additions & 4 deletions clang/include/clang/CIR/Dialect/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def CIRSimplify : Pass<"cir-simplify"> {
let summary = "Performs CIR simplification and code optimization";
let description = [{
The pass performs semantics-preserving code simplifications and optimizations
on CIR while maintaining strict program correctness.
on CIR while maintaining strict program correctness.

Unlike the `cir-canonicalize` pass, these transformations may reduce the IR's
structural similarity to the original source code as a trade-off for improved
code quality. This can affect debugging fidelity by altering intermediate
representations of folded expressions, hoisted operations, and other
representations of folded expressions, hoisted operations, and other
optimized constructs.

Example transformations include ternary expression folding and code hoisting
while preserving program semantics.
}];
Expand Down Expand Up @@ -72,4 +72,15 @@ def CIRFlattenCFG : Pass<"cir-flatten-cfg"> {
let dependentDialects = ["cir::CIRDialect"];
}

def LoweringPrepare : Pass<"cir-lowering-prepare"> {
let summary = "Lower to more fine-grained CIR operations before lowering to
other dialects";
let description = [{
This pass does preparation work for lowering to other dialects. For example,
it may expand the global variable initialziation in a more ABI-friendly form.
}];
let constructor = "mlir::createLoweringPreparePass()";
let dependentDialects = ["cir::CIRDialect"];
}

#endif // CLANG_CIR_DIALECT_PASSES_TD
1 change: 1 addition & 0 deletions clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_clang_library(MLIRCIRTransforms
CIRSimplify.cpp
FlattenCFG.cpp
HoistAllocas.cpp
LoweringPrepare.cpp

DEPENDS
MLIRCIRPassIncGen
Expand Down
40 changes: 40 additions & 0 deletions clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//===- LoweringPrepare.cpp - pareparation work for LLVM lowering ----------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "PassDetail.h"
#include "clang/AST/ASTContext.h"
#include "clang/CIR/Dialect/IR/CIRDialect.h"
#include "clang/CIR/Dialect/Passes.h"

#include <memory>

using namespace mlir;
using namespace cir;

namespace {
struct LoweringPreparePass : public LoweringPrepareBase<LoweringPreparePass> {
LoweringPreparePass() = default;
void runOnOperation() override;

void runOnOp(Operation *op);
};

} // namespace

void LoweringPreparePass::runOnOp(Operation *op) {}

void LoweringPreparePass::runOnOperation() {
llvm::SmallVector<Operation *> opsToTransform;

for (auto *o : opsToTransform)
runOnOp(o);
}

std::unique_ptr<Pass> mlir::createLoweringPreparePass() {
return std::make_unique<LoweringPreparePass>();
}
2 changes: 2 additions & 0 deletions clang/lib/CIR/Lowering/CIRPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ mlir::LogicalResult runCIRToCIRPasses(mlir::ModuleOp theModule,
if (enableCIRSimplify)
pm.addPass(mlir::createCIRSimplifyPass());

pm.addPass(mlir::createLoweringPreparePass());

pm.enableVerifier(enableVerifier);
(void)mlir::applyPassManagerCLOptions(pm);
return pm.run(theModule);
Expand Down
Loading