llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang gives a different answer for __builtin_constant_p than GCC

Open AaronBallman opened this issue 4 years ago • 9 comments

Consider the following C code:

_Static_assert(__builtin_constant_p((1,2)), "what?");

In Clang, the static assertion fails and in GCC the static assertion passes. C disallows comma operators explicitly as constant expressions, and Clang does get that correct in some cases:

_Static_assert((1, 2), "what?");

In both Clang and GCC the static assertion fails (as it should): https://godbolt.org/z/odTKMrqWe

AaronBallman avatar Jun 08 '22 17:06 AaronBallman

@llvm/issue-subscribers-c

llvmbot avatar Jun 08 '22 17:06 llvmbot

@llvm/issue-subscribers-clang-frontend

llvmbot avatar Jun 08 '22 17:06 llvmbot

A bit off-topic: FYI: In C23 6.6 Constant expressions, paragraph 10 changed:

An implementation may accept other forms of constant expressions, however, they are not an integer constant expression.

pmor13 avatar Sep 20 '22 20:09 pmor13

So the gcc documents for builtin_constant_p says:

You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile time and hence that GCC can perform constant-folding on expressions involving that value.

constant folding seems a much less strict term and sounds like it basically covers whatever the implementation can turn into a constant at compile-time and (1,2) looks like it reasonably fits that category. Although I can see in general case guaranteeing that clang folds for all the same cases may not be reasonable.

shafik avatar Oct 03 '22 16:10 shafik

Consider __builtin_constant_p in GCC returns 1 and constant folding was performed, clang returns 1 for compatibility with GCC but does not perform constant folding (For instance, the example code mentioned in https://github.com/llvm/llvm-project/issues/58078).

I'm wondering if we should actually return whether or not the expression in __builtin_constant_p is "folded" in clang AST, using the single recursive evaluation method Expr::Evaluate. In this way, we may return different values from GCC, but implemented this builtin function as the same semantic as GCC documentation.

If so, https://github.com/llvm/llvm-project/issues/58078 may duplicate to this issue (and should be closed).

Link: https://clang.llvm.org/docs/InternalsManual.html#constant-folding-in-the-clang-ast Link: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html#index-_005f_005fbuiltin_005fconstant_005fp

inclyc avatar Oct 05 '22 01:10 inclyc

I'm wondering if we should actually return whether or not the expression in __builtin_constant_p is "folded" in clang AST, using the single recursive evaluation method Expr::Evaluate. In this way, we may return different values from GCC, but implemented this builtin function as the same semantic as GCC documentation.

I think that an important part of current semantics of GCC's builtin is, that whenever the builtin is evaluated in a const-context, the expression passed to the builtin is also evaluated in a const-context. This results in the behaviour you described in the GCC bug-report and it allows me to define IS_CONSTEXPR like I currently do. Can clang's builtin be changed to do the same?

I think that would allow you to return the actual value of whether the expression is "folded" in clang, but it would also increase the compatibility with GCC.

h-2 avatar Oct 05 '22 11:10 h-2

Any progress on this?

vrukesh avatar Jan 21 '26 11:01 vrukesh

Some other discussions on __builtin_constant_p

https://github.com/llvm/llvm-project/pull/159599 https://github.com/llvm/llvm-project/issues/154024 https://github.com/llvm/llvm-project/issues/35381

shafik avatar Jan 21 '26 19:01 shafik

Suprisingly, I went by link in topic message ( https://godbolt.org/z/odTKMrqWe ) and Godbolt shows me opposite:

  • GCC fails static assertion _Static_assert(__builtin_constant_p((1,2)), "what?");, but Clang doesn't.

What did I wrong?

funny-falcon avatar Mar 31 '26 09:03 funny-falcon