Issue 243307
Summary [EarlyCSE] strictfp `cbrt` marked `memory(none)` is CSEd across `fesetround`
Labels new issue
Assignees
Reporter 20000419
    `cbrt(2.0)` under `FE_DOWNWARD`, then the same call under `FE_UPWARD`, must observe both modes. glibc returns `0x3ff428a2f98d728a` and `0x3ff428a2f98d728d`. clang `-O1` and above returns `728a` twice. gcc `-O2 -frounding-math -ftrapping-math` keeps both values. The second call is deleted.

C 7.6.1 with `FENV_ACCESS ON`, and `-ffp-model=strict`, forbid assuming a constant floating-point environment. Annex F recommends a correctly rounded `cbrt` and does not require it; this report does not rest on that recommendation. The platform `cbrt` does change with the mode (down / nearest / up are `728a` / `728c` / `728d`). clang still deletes the later calls. `cbrtf(2)` is the same deletion. A second call with a different argument is not merged.

```c
#pragma STDC FENV_ACCESS ON
#include <fenv.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

static unsigned long long bits(double x) {
  unsigned long long u;
  memcpy(&u, &x, 8);
  return u;
}

static void f(double x) {
  fesetround(FE_DOWNWARD);
  double down = cbrt(x);
  fesetround(FE_UPWARD);
  double up = cbrt(x);
  printf("%llx %llx\n", bits(down), bits(up));
}

int main(void) {
  f(2.0);
  return 0;
}
```

```console
$ clang -O0 -ffp-model=strict -frounding-math t.c -lm && ./a.out
3ff428a2f98d728a 3ff428a2f98d728d
$ clang -O2 -ffp-model=strict -frounding-math t.c -lm && ./a.out
3ff428a2f98d728a 3ff428a2f98d728a
$ gcc -O2 -frounding-math -ftrapping-math t.c -lm && ./a.out
3ff428a2f98d728a 3ff428a2f98d728d
```

Where (main `c7014922f582`, 2026-11-17):

- `clang/include/clang/Basic/Builtins.td` `def Cbrt` (line 4267, `FPMathTemplate` for `cbrt`/`cbrtf`/`cbrtl`): `let Attributes = [NoThrow, Const];`. `sqrt` on the next libm block is `ConstIgnoringErrnoAndExceptions`.
- `SemaDecl.cpp` adds `ConstAttr` for the plain const path without looking at the exception mode. `CGCall.cpp` turns that into `memory(none)` on the call. Frontend IR (`-O0 -Xclang -disable-llvm-passes`) is `call double @cbrt(...) #7` with `#7 = { nounwind strictfp willreturn memory(none) }`.
- There is no `llvm.experimental.constrained.cbrt`. `rint`/`nearbyint`/`floor`/`ceil` are also `Const` in this table, but `-ffp-model=strict` lowers those to constrained intrinsics. `cbrt` stays a libcall.
- `EarlyCSE.cpp` `SimpleValue::canHandle` (line 122) CSEs any `doesNotAccessMemory()` call. The special case refuses `ebStrict` / `round.dynamic` only for listed constrained arithmetic intrinsics. A strictfp libcall with `memory(none)` is CSEd across `fesetround`.

Pass: opt-bisect limit 12 (`sroa on f`) still prints `728a 728d`; limit 13 (`early-cse on f`) prints `728a 728a`. `opt -passes=sroa,early-cse` alone deletes the second call. Full `-O2` IR keeps one `call @cbrt` between the two `fesetround`s and passes that value twice to `printf`.

This is not constant folding and not #46253. A host constant of `cbrt(2)` in the default mode is `728c`. The kept value is the downward call's result, and one runtime call remains.

Versions (`-ffp-model=strict -frounding-math`; good `728a 728d`, bad `728a 728a`):

- clang 15–17 and 19–24: `-O0` OK; `-O1`/`-O2`/`-O3`/`-Os` FAIL
- clang trunk `58561153d5fc` (2026-11-15) and CE clang trunk `abcbd442e332` (2026-11-16): `-O0` OK; `-O1`/`-O2` FAIL
- clang-18: not installed
- gcc `-O0`/`-O2`/`-O3`/`-Os` with `-frounding-math -ftrapping-math`: OK
- clang-24 `-O2` with only the pragma (no `-ffp-model`, no `-frounding-math`): also fails; frontend still marks the call `strictfp memory(none)`

Related but different: #46253 is one call at the default rounding mode (clang's constant 1 ulp above gcc). #241652 is AggressiveInstCombine `foldSqrt` of a strictfp `sqrt` libcall to a nearest-even constant — different pass and function.

Possible fix (not tested): mark `Cbrt` `ConstIgnoringErrnoAndExceptions`, like `Sqrt`, so `ConstAttr` is not added when the exception mode is strict. Do not put `memory(none)` on a strictfp `cbrt` call.

Godbolt (executed): https://godbolt.org/z/4rqd6qbYn
CE clang trunk `-O0 -ffp-model=strict -frounding-math -lm` → `3ff428a2f98d728a 3ff428a2f98d728d`; `-O2` same flags → `3ff428a2f98d728a 3ff428a2f98d728a`.

cc @nikic @jcranmer-intel

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

Assisted-by: Grok

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to