-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][ExprConst] Fix second arg of __builtin_{clzg,ctzg} not always being evaluated #86742
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[clang][ExprConst] Fix second arg of __builtin_{clzg,ctzg} not always being evaluated #86742
Conversation
@llvm/pr-subscribers-clang Author: OverMighty (overmighty) ChangesSee #86577 (comment). cc @efriedma-quic @nickdesaulniers Full diff: https://github.com/llvm/llvm-project/pull/86742.diff 2 Files Affected:
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 5a36621dc5cce2..dae8f32fc02951 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -12361,12 +12361,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;
+ std::optional<APSInt> Fallback;
+ if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) {
+ APSInt FallbackTemp;
+ if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))
+ return false;
+ Fallback = FallbackTemp;
+ }
+
if (!Val) {
- if (BuiltinOp == Builtin::BI__builtin_clzg && E->getNumArgs() > 1) {
- if (!EvaluateInteger(E->getArg(1), Val, Info))
- return false;
- return Success(Val, E);
- }
+ if (Fallback)
+ return Success(*Fallback, E);
// When the argument is 0, the result of GCC builtins is undefined,
// whereas for Microsoft intrinsics, the result is the bit-width of the
@@ -12425,12 +12430,17 @@ bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
if (!EvaluateInteger(E->getArg(0), Val, Info))
return false;
+ std::optional<APSInt> Fallback;
+ if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) {
+ APSInt FallbackTemp;
+ if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info))
+ return false;
+ Fallback = FallbackTemp;
+ }
+
if (!Val) {
- if (BuiltinOp == Builtin::BI__builtin_ctzg && E->getNumArgs() > 1) {
- if (!EvaluateInteger(E->getArg(1), Val, Info))
- return false;
- return Success(Val, E);
- }
+ if (Fallback)
+ return Success(*Fallback, E);
return Error(E);
}
diff --git a/clang/test/Sema/constant-builtins-all-args-evaluated.cpp b/clang/test/Sema/constant-builtins-all-args-evaluated.cpp
new file mode 100644
index 00000000000000..0dee74ddf690cf
--- /dev/null
+++ b/clang/test/Sema/constant-builtins-all-args-evaluated.cpp
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+constexpr int increment(int& x) {
+ x++;
+ return x;
+}
+
+constexpr int test_clzg_0() {
+ int x = 0;
+ [[maybe_unused]] int unused = __builtin_clzg(0U, increment(x));
+ return x;
+}
+
+static_assert(test_clzg_0() == 1);
+
+constexpr int test_clzg_1() {
+ int x = 0;
+ [[maybe_unused]] int unused = __builtin_clzg(1U, increment(x));
+ return x;
+}
+
+static_assert(test_clzg_1() == 1);
+
+constexpr int test_ctzg_0() {
+ int x = 0;
+ [[maybe_unused]] int unused = __builtin_ctzg(0U, increment(x));
+ return x;
+}
+
+static_assert(test_ctzg_0() == 1);
+
+constexpr int test_ctzg_1() {
+ int x = 0;
+ [[maybe_unused]] int unused = __builtin_ctzg(1U, increment(x));
+ return x;
+}
+
+static_assert(test_ctzg_1() == 1);
|
APSInt FallbackTemp; | ||
if (!EvaluateInteger(E->getArg(1), FallbackTemp, Info)) | ||
return false; | ||
Fallback = FallbackTemp; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess using a bool HasFallback
instead of std::optional
would be more efficient but defaulting to std::optional
feels like better practice.
… always being evaluated Co-authored-by: Richard Smith <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
||
constexpr int test_clzg_0() { | ||
int x = 0; | ||
(void)__builtin_clzg(0U, increment(x)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Random thought that popped up while in bed at 2 AM: I probably could have just used ++x
as second argument.
… always being evaluated
Could someone merge this for me if it looks good to everyone? |
See #86577 (comment).
cc @efriedma-quic @nickdesaulniers