[PATCH] D109925: [AST] add warnings for out-of-bounds FP values when using relaxed FP math compile flags

Sanjay Patel via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 16 14:04:59 PDT 2021


spatel created this revision.
spatel added reviewers: sepavloff, jyknight, efriedma, dim, kparzysz, rsmith.
Herald added a subscriber: mcrosier.
spatel requested review of this revision.

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()
  ...


https://reviews.llvm.org/D109925

Files:
  clang/include/clang/Basic/DiagnosticASTKinds.td
  clang/lib/AST/ExprConstant.cpp
  clang/test/AST/warn-fp-values.c

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D109925.373059.patch
Type: text/x-patch
Size: 3766 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210916/a330b314/attachment-0001.bin>


More information about the cfe-commits mailing list