[clang] [CIR] Upstream the basic structure of LoweringPrepare pass (PR #148545)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Jul 13 12:15:12 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Amr Hesham (AmrDeveloper)
<details>
<summary>Changes</summary>
Upstream, the basic structure of the LoweringPrepare pass as a prerequisite for other ComplexType PR's
https://github.com/llvm/llvm-project/issues/141365
---
Full diff: https://github.com/llvm/llvm-project/pull/148545.diff
5 Files Affected:
- (modified) clang/include/clang/CIR/Dialect/Passes.h (+1)
- (modified) clang/include/clang/CIR/Dialect/Passes.td (+14-4)
- (modified) clang/lib/CIR/Dialect/Transforms/CMakeLists.txt (+1)
- (added) clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp (+40)
- (modified) clang/lib/CIR/Lowering/CIRPasses.cpp (+2)
``````````diff
diff --git a/clang/include/clang/CIR/Dialect/Passes.h b/clang/include/clang/CIR/Dialect/Passes.h
index dbecf81acf7bb..02210ec0a8336 100644
--- a/clang/include/clang/CIR/Dialect/Passes.h
+++ b/clang/include/clang/CIR/Dialect/Passes.h
@@ -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);
diff --git a/clang/include/clang/CIR/Dialect/Passes.td b/clang/include/clang/CIR/Dialect/Passes.td
index de775e69f0073..59c06f2e13f22 100644
--- a/clang/include/clang/CIR/Dialect/Passes.td
+++ b/clang/include/clang/CIR/Dialect/Passes.td
@@ -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.
}];
@@ -72,4 +72,14 @@ def CIRFlattenCFG : Pass<"cir-flatten-cfg"> {
let dependentDialects = ["cir::CIRDialect"];
}
+def LoweringPrepare : Pass<"cir-lowering-prepare"> {
+ let summary = "Preparation work before lowering to LLVM dialect";
+ let description = [{
+ This pass does preparation work for LLVM lowering. 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
diff --git a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
index 4dece5b57e450..18beca7b9a680 100644
--- a/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
+++ b/clang/lib/CIR/Dialect/Transforms/CMakeLists.txt
@@ -3,6 +3,7 @@ add_clang_library(MLIRCIRTransforms
CIRSimplify.cpp
FlattenCFG.cpp
HoistAllocas.cpp
+ LoweringPrepare.cpp
DEPENDS
MLIRCIRPassIncGen
diff --git a/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
new file mode 100644
index 0000000000000..5493b86a0a321
--- /dev/null
+++ b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp
@@ -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>();
+}
diff --git a/clang/lib/CIR/Lowering/CIRPasses.cpp b/clang/lib/CIR/Lowering/CIRPasses.cpp
index 7a581939580a9..5607abc98e319 100644
--- a/clang/lib/CIR/Lowering/CIRPasses.cpp
+++ b/clang/lib/CIR/Lowering/CIRPasses.cpp
@@ -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);
``````````
</details>
https://github.com/llvm/llvm-project/pull/148545
More information about the cfe-commits
mailing list