================
@@ -2707,27 +2707,31 @@ static bool checkFloatingPointResult(EvalInfo &Info, 
const Expr *E,
   if (Info.InConstantContext)
     return true;
 
+  // The output result is exact and no exceptions are raised, so it is safe to
+  // perform compile-time evaluation.
+  if (St == APFloat::opOK)
+    return true;
+
   FPOptions FPO = E->getFPFeaturesInEffect(Info.getLangOpts());
-  if ((St & APFloat::opInexact) &&
-      FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
-    // Inexact result means that it depends on rounding mode. If the requested
-    // mode is dynamic, the evaluation cannot be made in compile time.
+
+  if (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
----------------
hubert-reinterpretcast wrote:

This logic breaks once `<math.h>` functions are considered.

For `<math.h>` functions, a constant rounding mode (which we ignore for folding 
of run-time evaluations) should not allow folding that ignores the dynamic 
rounding mode without regard to `-frounding-math`. Such folding occurs with 
this PR in its current state:
```console
$ clang++ -w -frounding-math -S -o - -emit-llvm -Xclang -disable-llvm-passes 
-xc -<<<'double exp1() { _Pragma("STDC FENV_ROUND FE_DOWNWARD") return 
__builtin_exp(1.); }' | grep ret
  ret double f0x4005BF0A8B145769
```

**Note 1:** 
https://clang.llvm.org/docs/UsersManual.html#accessing-the-floating-point-environment
 makes a statement about the C standard in direct contradiction to C23 
subclause 7.6.2 paragraph 3. According to the C standard, `#pragma STDC 
FENV_ROUND FE_DYNAMIC` affords no permission for the program to change the 
dynamic rounding mode when `FENV_ACCESS` is "off".

**Note 2:** From the same section of the Clang documentation and with reference 
to the same paragraph of the C standard,
> The command line option `-frounding-math` behaves as if the translation unit 
> began with `#pragma STDC FENV_ROUND FE_DYNAMIC`.

is vacuous at best and incorrect at worst. Having the translation unit begin 
that way has no effect according to the C standard, and that is true of the 
Clang implementation; however, the option _does_ have an effect: 
https://godbolt.org/z/d8bcfe4jx.

https://github.com/llvm/llvm-project/pull/199808
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to