<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61165>61165</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
__builtin_expect_with_probability not honoring constexpr statement
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
HFTrader
</td>
</tr>
</table>
<pre>
Consider this case [also on Godbolt](https://godbolt.org/z/4nMeTsoMv)
```c++
#include <cstdint>
#include <bit> // since C++20
template< std::size_t N >
uint64_t msb( uint64_t n ) {
constexpr float P = 0.01f*N;
if ( __builtin_expect_with_probability(n==0,1, P ) )
return 0;
return std::bit_width(n);
}
template uint64_t msb<1>( uint64_t );
template uint64_t msb<90>( uint64_t );
```
Both gcc and icc compile without warnings. However clang (trunk as of today) breaks with the following message:
```
<source>:7:52: error: probability argument to __builtin_expect_with_probability must be constant floating-point expression
if ( __builtin_expect_with_probability(n==0,1, P ) )
^
<source>:7:52: note: read of non-constexpr variable 'P' is not allowed in a constant expression
<source>:6:21: note: declared here
constexpr float P = 0.01f*N;
^
1 error generated.
Compiler returned: 1
```
Although `P` has just been declared constexpr, __builtin_expect_with_probability does not honor it.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVU-P2joQ_zTDZbQosUkChxxYKK-XVnvoHTn2kLg1NrInu91--icTCvtWqCs9NYqA2Jk_vz-DVUq290QtVI9QbWdq5CHE9vPuW1SG4qwL5rXdBJ-soYg82IRaJUKoHpVLAYPHf4LpgmOotiCWA_MpgVyD2IHY9dPWPMQexO4XiN3Cf6FvKXx5BrGCYgvF-vJZF9OtQTzme1oV0nrtRkMIcqMTG-sZ5Kd7u53NOzgVxmS9JtxMyUTxthTT8eQUE8gNJja5WblO9hftGb_iNftoPdeLPeMxdSCWeH32CGKF0Fx6RETUwSemn6eIBxcU4xOC3GIxL8oDiPVXkL_ftQfMufb7brSO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