This is an archive of the discontinued LLVM Phabricator instance.

[AST] add warnings for out-of-bounds FP values when using relaxed FP math compile flags
Needs ReviewPublic

Authored by spatel on Sep 16 2021, 2:04 PM.

Details

Summary

There's currently ongoing discussion on the dev lists about how to handle related cases with isnan() / isinf(), but I don't think the problem addressed by this patch is controversial:
If the compile specified loose FP math because special values like NaN / Inf / -0.0 are not expected, then we should warn if the code contains an obvious occurrence of any of those values.

My understanding of clang isn't good though. Is this the right place to detect the unexpected values? I commented in the test file that I don't see the expected warnings in all cases before CodeGen. But if we run through to IR creation, then I sometimes see duplicate warnings for the same line of code.

For example, I see duplicate 3 warnings on this program based on https://llvm.org/PR51775 :

% cat 51775.c 
#include <math.h>
#include <stdlib.h>
int main() {
    const double d = strtod("1E+1000000", NULL);
    return d == HUGE_VAL;
}
% clang -O1 -ffast-math 51775.c -S -o -
51775.c:5:17: warning: floating-point infinity may be optimized out of computation or comparison [-Wliteral-range]
    return d == HUGE_VAL;
                ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h:57:28: note: expanded from macro 'HUGE_VAL'
#   define    HUGE_VAL     __builtin_huge_val()
                           ^
51775.c:5:17: warning: floating-point infinity may be optimized out of computation or comparison [-Wliteral-range]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h:57:28: note: expanded from macro 'HUGE_VAL'
#   define    HUGE_VAL     __builtin_huge_val()
                           ^
51775.c:5:17: warning: floating-point infinity may be optimized out of computation or comparison [-Wliteral-range]
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h:57:28: note: expanded from macro 'HUGE_VAL'
#   define    HUGE_VAL     __builtin_huge_val()
...

Diff Detail

Event Timeline

spatel created this revision.Sep 16 2021, 2:04 PM
spatel requested review of this revision.Sep 16 2021, 2:04 PM
Matt added a subscriber: Matt.Sep 16 2021, 3:43 PM

I don't think the constant evaluator is a right place for such warnings. The option -ffast-math is a hint to the compiler, which, in particular, tells that no operation on Nans/Infs take place. It allows a compiler to generate better code. None is pertinent for the constant evaluator, as it already has all information necessary for the evaluation and it does not generate any code. It does not make sense to add a new mode for constant evaluation as well. To make the warnings useful you probably need to check if a constant value is an operand of an arithmetic operation.

clang/test/AST/warn-fp-values.c
49

-ffast-math does not prohibit using negative zero, it only treat it identically to positive zero.