[clang] [CIR][SSCP] Return null callable region for function declarations (PR #228641)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Oct 2 19:57:17 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clangir
Author: Konstantinos Parasyris (koparasy)
<details>
<summary>Changes</summary>
Declarations and aliases have empty bodies; returning them made SCCP treat external calls as never returning and fold their results to constants.
---
Full diff: https://github.com/llvm/llvm-project/pull/228641.diff
2 Files Affected:
- (modified) clang/lib/CIR/Dialect/IR/CIRDialect.cpp (+5-2)
- (added) clang/test/CIR/Transforms/sccp-external-call.cir (+26)
``````````diff
diff --git a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
index 15cebd358040e..51b5bd1c58f4b 100644
--- a/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRDialect.cpp
@@ -3092,8 +3092,11 @@ bool cir::FuncOp::isCxxTrivialMemberFunction() {
}
mlir::Region *cir::FuncOp::getCallableRegion() {
- // TODO(CIR): This function will have special handling for aliases and a
- // check for an external function, once those features have been upstreamed.
+ // Declarations and aliases have no body to analyze or inline. Returning the
+ // empty region would make interprocedural analyses (e.g. SCCP) treat calls
+ // to them as having no returns, leaving their results uninitialized.
+ if (getBody().empty())
+ return nullptr;
return &getBody();
}
diff --git a/clang/test/CIR/Transforms/sccp-external-call.cir b/clang/test/CIR/Transforms/sccp-external-call.cir
new file mode 100644
index 0000000000000..23253ef7a5a08
--- /dev/null
+++ b/clang/test/CIR/Transforms/sccp-external-call.cir
@@ -0,0 +1,26 @@
+// Regression test: SCCP must treat a call to a declaration as producing an
+// unknown value. cir::FuncOp::getCallableRegion() must return null for
+// declarations; otherwise the call result is never initialized and joining it
+// with a constant arm folds the whole ternary to that constant.
+// RUN: cir-opt %s --sccp | FileCheck %s
+
+module {
+ cir.func private @g() -> !cir.bool
+
+ cir.func @f(%arg0: !cir.bool) -> !cir.bool {
+ %0 = cir.ternary(%arg0, true {
+ %1 = cir.call @g() : () -> !cir.bool
+ cir.yield %1 : !cir.bool
+ }, false {
+ %1 = cir.const #cir.bool<false> : !cir.bool
+ cir.yield %1 : !cir.bool
+ }) : (!cir.bool) -> !cir.bool
+ cir.return %0 : !cir.bool
+ }
+}
+
+// CHECK: cir.func{{.*}} @f
+// CHECK: %[[#ret:]] = cir.ternary
+// CHECK: %[[#call:]] = cir.call @g()
+// CHECK: cir.yield %[[#call]]
+// CHECK: cir.return %[[#ret]] : !cir.bool
``````````
</details>
https://github.com/llvm/llvm-project/pull/228641
More information about the cfe-commits
mailing list