[clang] [clang] `__builtin_expect_with_probability` accepts value depenent parameter (PR #156113)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Aug 29 15:06:04 PDT 2025
https://github.com/Mr-Anyone created https://github.com/llvm/llvm-project/pull/156113
Defer the `CheckBuiltinFunctionCall` to template instantiation.
fixes #153082
>From 151ba2e45b4317d9d01eb1613186772704a3dcb7 Mon Sep 17 00:00:00 2001
From: Vincent <llvm at viceroygroup.ca>
Date: Thu, 14 Aug 2025 22:26:28 +0800
Subject: [PATCH] [clang] `__builtin_expect_with_probability` accepts value
depenent parameter
Defer the `CheckBuiltinFunctionCall` to template instantiation.
fixes #153082
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Sema/SemaChecking.cpp | 3 +++
.../Sema/builtin-expect-with-probability.cpp | 16 ++++++++++++++++
3 files changed, 20 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9a05eea9de8ac..fd015cea7c3de 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -260,6 +260,7 @@ Bug Fixes in This Version
cast chain. (#GH149967).
- Fixed a crash with incompatible pointer to integer conversions in designated
initializers involving string literals. (#GH154046)
+- ``__builtin_expect_with_probability`` now works with a value dependent parameter. (GH153082).
- Clang now emits a frontend error when a function marked with the `flatten` attribute
calls another function that requires target features not enabled in the caller. This
prevents a fatal error in the backend.
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 6e777fb9aec8e..875a1df747c29 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -2855,6 +2855,9 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
SmallVector<PartialDiagnosticAt, 8> Notes;
Expr::EvalResult Eval;
Eval.Diag = &Notes;
+ if (ProbArg->isValueDependent() || ProbArg->isTypeDependent())
+ break;
+
if ((!ProbArg->EvaluateAsConstantExpr(Eval, Context)) ||
!Eval.Val.isFloat()) {
Diag(ProbArg->getBeginLoc(), diag::err_probability_not_constant_float)
diff --git a/clang/test/Sema/builtin-expect-with-probability.cpp b/clang/test/Sema/builtin-expect-with-probability.cpp
index c55cde84b2548..70101d6a626af 100644
--- a/clang/test/Sema/builtin-expect-with-probability.cpp
+++ b/clang/test/Sema/builtin-expect-with-probability.cpp
@@ -57,3 +57,19 @@ void test(int x, double p) { // expected-note {{declared here}}
dummy = __builtin_expect_with_probability(x > 0, 1, pi);
expect_taken<S>(x);
}
+
+namespace gh153082{
+ template <typename T> void f() {
+ (void) __builtin_expect_with_probability(0, 0, ({ 0; }));
+ }
+
+ template <typename T> void more_test_case(long a) {
+ (void)__builtin_expect_with_probability(a, 0, sizeof(T)/3.9); // expected-error {{probability argument to __builtin_expect_with_probability is outside the range [0.0, 1.0]}} \
+ // expected-error {{probability argument}}
+ }
+
+ template void more_test_case<short>(long);
+ template void more_test_case<char>(long);
+ template void more_test_case<int>(long); // expected-note {{in instantiation of function template specialization 'gh153082::more_test_case<int>' requested here}}
+ template void more_test_case<long long>(long); // expected-note {{instantiation of function template specialization 'gh153082::more_test_case<long long>' requested here}}
+};
More information about the cfe-commits
mailing list