[PATCH] D89360: Treat constant contexts as being in the default rounding mode.

Serge Pavlov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 14 04:56:31 PDT 2020


sepavloff added a comment.

I would propose to consider solution in D88498 <https://reviews.llvm.org/D88498>. It tries to fix the real reason of the malfunction - using dynamic rounding mode for evaluation of global variable initializers. With the workaround for constexpr functions it allows to compile the test attached and still correctly detect runtime nature of local variable initializers.



================
Comment at: clang/lib/AST/ExprConstant.cpp:2533-2534
+  // exception state matches the default floating-point environment.
+  if (Info.InConstantContext)
+    return true;
+
----------------
It turns off the check made by this function. In the case of global variable initializer it fixes the error (using dynamic rounding mode instead of default) but for local variable initializer it creates a new error. Constant evaluator cannot detect that the initializer in the code:
```
void g() {
  const int k = 3 * (1.0 / 3.0);
  ...
}
```
is not a constant expression.


================
Comment at: clang/test/SemaCXX/rounding-math.cpp:9
+
+constexpr int f(int n) { return int(n * (1.0 / 3.0)); }
+
----------------
This code requires additional solution. The function body is built using dynamic rounding mode, which breaks its constexprness. We can avoid this kind of errors if we assume that body of a constexpr function is parsed using constant rounding mode (in this example it is the default mode). It makes parsing constexpr function different from non-constexpr ones, but enables convenient use:
```
constexpr int add(float x, float y) { return x + y; }

#pragma STDC FENV_ROUND FE_UPWARD
int a2 = add(2.0F, 0x1.000002p0F);

#pragma STDC FENV_ROUND FE_DOWNWARD
int a3 = add(2.0F, 0x1.000002p0F);
```
If calls of constexpr functions are processes with FP options acting in the call site, a call to constexpr function becomes equivalent to execution of statements of its body.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89360/new/

https://reviews.llvm.org/D89360



More information about the cfe-commits mailing list