[llvm] [Flang-rt][build] fixes building warnings under gcc: (PR #173955)
Eugene Epshteyn via llvm-commits
llvm-commits at lists.llvm.org
Tue Dec 30 20:49:18 PST 2025
================
@@ -452,7 +452,13 @@ float RTNAME(Rand)(int *i, const char *sourceFile, int line) {
if (radix == 2) {
mask = ~(unsigned)0u << (32 - digits + 1);
} else if (radix == 16) {
- mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);
+ int shift_val = ((8 - digits) * 4 + 1);
+ if (shift_val < 0) {
+ Terminator terminator{sourceFile, line};
+ terminator.Crash("Radix 16: negative shift for mask. digits value maybe invalid.");
+ } else {
+ mask = ~(unsigned)0u << shift_val;
+ }
----------------
eugeneepshteyn wrote:
> Please forgive me if I have offended you.
I'm not offended, just trying to identify underlying issue.
I think I know what it is. Here's the original code for reference:
```
constexpr int radix = std::numeric_limits<float>::radix;
constexpr int digits = std::numeric_limits<float>::digits;
if (radix == 2) {
mask = ~(unsigned)0u << (32 - digits + 1);
} else if (radix == 16) {
mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);
} else {
```
`radix` in your case is probably 2. `digits` is a compile time constant, and with radix 2 it's likely greater than 8. Even though the branch `if (radix == 16)` will not be taken, the compiler will evaluate all the constant expressions and will compute `mask = ~(unsigned)0u << ((8 - digits) * 4 + 1);` even though this branch won't be taken at runtime. (In fact, this branch may be eliminated later after the constant folding, but the warning would be emitted anyway.)
To properly fix this need to use `if constexpr`. Could you please try changing `if` here to `if constexpr` and see if this warning goes away?
https://github.com/llvm/llvm-project/pull/173955
More information about the llvm-commits
mailing list