Skip to content

Conversation

Mr-Anyone
Copy link
Contributor

Defer the CheckBuiltinFunctionCall to template instantiation.

fixes #153082

…rameter

Defer the `CheckBuiltinFunctionCall` to template instantiation.

fixes llvm#153082
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 29, 2025

@llvm/pr-subscribers-clang

Author: Vincent (Mr-Anyone)

Changes

Defer the CheckBuiltinFunctionCall to template instantiation.

fixes #153082


Full diff: https://github.com/llvm/llvm-project/pull/156113.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Sema/SemaChecking.cpp (+3)
  • (modified) clang/test/Sema/builtin-expect-with-probability.cpp (+16)
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}}
+};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICE with statement expression + builtin: Assertion failed: (!isValueDependent() && "Expression evaluator can't be called on a dependent expression.")
2 participants