[clang] f97b083 - [CIR][SSCP] Return null callable region for function declarations (#228641)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 5 15:55:55 PDT 2026
Author: Konstantinos Parasyris
Date: 2026-10-05T22:55:48Z
New Revision: f97b083335d6c5f3dbf3a2b1acccb1bdd059983c
URL: https://github.com/llvm/llvm-project/commit/f97b083335d6c5f3dbf3a2b1acccb1bdd059983c
DIFF: https://github.com/llvm/llvm-project/commit/f97b083335d6c5f3dbf3a2b1acccb1bdd059983c.diff
LOG: [CIR][SSCP] Return null callable region for function declarations (#228641)
Declarations and aliases have empty bodies; returning them made SCCP
treat external calls as never returning and fold their results to
constants.
Added:
clang/test/CIR/Transforms/sccp-external-call.cir
Modified:
clang/lib/CIR/Dialect/IR/CIRDialect.cpp
Removed:
################################################################################
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
More information about the cfe-commits
mailing list