[Regression 16 -> 17] Template instantiation ignores FENV_ACCESS being ON for both definition and instantiation
Consider:
#pragma STDC FENV_ACCESS ON
template <typename>
int b() {
int x;
if ((float)0xFFFFFFFF != (float)0x100000000) {
x = 1;
}
return x;
}
int f() { return b<void>(); }
Clang 16 generates the floating-point operations (as is appropriate for FENV_ACCESS being ON):
f(): # @f()
mov eax, 4294967295
cvtsi2ss xmm0, rax
movabs rax, 4294967296
cvtsi2ss xmm1, rax
ucomiss xmm0, xmm1
mov eax, 1
ret
Clang "17" currently generates no floating-point operations:
f(): # @f()
ret
Compiler Explorer: https://godbolt.org/z/PcoaaxzKn
Reverting fde5924dcc69fe814085482df259b8cfee236f2c restores the previous behaviour.
@llvm/issue-subscribers-clang-frontend
This defect is not caused by https://github.com/llvm/llvm-project/commit/fde5924dcc69fe814085482df259b8cfee236f2c. The reason is the elimination of ImplicitCastExpr when a template is instantiated, see comment: https://github.com/llvm/llvm-project/blob/a5fe6c7f5e2d1d265bd7c312ef55259fee7a68f9/clang/lib/Sema/TreeTransform.h#L11629-L11631 ImplicitCastExpr keeps FP options, including rounding direction. Removing them results in FPOptions loss, they are taken from the current Sema state, which is incorrect behavior.
Revert of https://github.com/llvm/llvm-project/commit/fde5924dcc69fe814085482df259b8cfee236f2c accidentally fixed the behavior, because FP options used to generate code were taken from Sema state (and nof from AST node) and they were the same as at the point of the template definition. If, for example, #pragma STDC FENV_ACCESS OFF is put at the end of the file, the problem would be observed with the commit reverted.
I will prepare a fix soon.
FWIW, rc3 is supposed to go out in about a week and I believe we're hoping that's one of the last rcs, so we don't have much bake time for a fix. If the fix ends up being trivial and obviously correct, then I think it may still be able to make 17.x. I'm a bit more worried about it not being a trivial change though. It sounds like https://github.com/llvm/llvm-project/commit/fde5924dcc69fe814085482df259b8cfee236f2c accidentally exposed an issue that is causing more miscompiles and a revert of it would get us back to the same stability as Clang 16.x, even though it's not actually a "fix" per se. Am I correct? If so, we may want to consider doing that revert from the 17.x branch so we don't introduce more miscompilations and then do the real fix for 18.x with plenty of time for testing fallout.
I put a patch https://reviews.llvm.org/D158158, which should fix the issue.It is not a full-fledged solution (it does not support variable templates), but it should be enough for this issue. It is small and obvious, hopefully this can facilitate the review.
https://github.com/llvm/llvm-project/commit/fde5924dcc69fe814085482df259b8cfee236f2c fixes https://github.com/llvm/llvm-project/issues/63542, so revert of it exposes that issue.
fde5924 fixes #63542, so revert of it exposes that issue.
To make sure I'm on the same page -- a revert would bring us back to the Clang 16 behavior (bugs and all)?
It is not a full-fledged solution (it does not support variable templates), but it should be enough for this issue.
It seems variable templates have another layer of problems to begin with.
Clang 16 will not generate a call to foo for the following:
#include <float.h>
struct ChangeRounding {
ChangeRounding();
~ChangeRounding();
};
int foo(), bar();
#pragma STDC FENV_ACCESS ON
template <typename T>
int var = (ChangeRounding(), (T)0xFFFFFFFF != (T)0x100000000 ? foo() : bar());
int main() {
return var<float>;
}
https://godbolt.org/z/Mnq5PoP8o
Those changes do seem quite simple and I think they're safe for a backport to 17.x, but I'd appreciate a few opinions from @hubert-reinterpretcast and @jcranmer-intel to see if they agree we should backport. The alternatives are: leave the regression in Clang 16, or revert the original changes from just the 17.x branch so we're back in the (previously also bad) state we were in for 16.
The change https://github.com/llvm/llvm-project/commit/0baf85c331090fbe2d2b42214ed0664d55feb0b5 requires also https://github.com/llvm/llvm-project/commit/73e5a70e676850b79f196e01e2194a2485041584. The latter fixes a test from the former.
Those changes do seem quite simple and I think they're safe for a backport to 17.x
I agree, and I think we should backport. The original changes fixed an issue in Clang 16 that was encountered by multiple parties (so reverting is not my first choice). With the additional changes, the "regressions" known to me are addressed.
Excellent, thank you @hubert-reinterpretcast
@spavloff, can you handle the backport for both commits to 17.x? (I think it's fine to squash them into one commit and only put up one PR)
/cherry-pick 0baf85c331090fbe2d2b42214ed0664d55feb0b5 /cherry-pick 73e5a70e676850b79f196e01e2194a2485041584
/branch llvm/llvm-project-release-prs/issue64605
/pull-request llvm/llvm-project-release-prs#627
/cherry-pick https://github.com/llvm/llvm-project/commit/0baf85c331090fbe2d2b42214ed0664d55feb0b5 https://github.com/llvm/llvm-project/commit/73e5a70e676850b79f196e01e2194a2485041584
/branch llvm/llvm-project-release-prs/issue64605
The branch https://github.com/llvm/llvm-project-release-prs/tree/llvm-issue64605 now contains correct commits.
This is wrongly marked as "Done" in the release status board. Perhaps better to move it to "Needs merge" to avoid it falling through the cracks.
Thanks for noticing @h-vetinari. Feel free to ping me directly in the future so I don't miss them.
(there's a lot of issues)
Not sure where to put this, but ever since 73e5a70e676850b79f196e01e2194a2485041584 was cherry-picked to the release/17.x branch as 73e5a70e676850b79f196e01e2194a2485041584, the test clang/test/SemaCXX/template-64605.cpp has been failing on multiple release bots:
- https://lab.llvm.org/buildbot/#/builders/148/builds/354
- https://lab.llvm.org/buildbot/#/builders/143/builds/524
- https://lab.llvm.org/buildbot/#/builders/144/builds/505
- https://lab.llvm.org/buildbot/#/builders/260/builds/35
- https://lab.llvm.org/buildbot/#/builders/261/builds/35
Not sure where to put this, but ever since 73e5a70 was cherry-picked to the release/17.x branch as 73e5a70, the test
clang/test/SemaCXX/template-64605.cpphas been failing on multiple release bots:
- https://lab.llvm.org/buildbot/#/builders/148/builds/354
- https://lab.llvm.org/buildbot/#/builders/143/builds/524
- https://lab.llvm.org/buildbot/#/builders/144/builds/505
- https://lab.llvm.org/buildbot/#/builders/260/builds/35
- https://lab.llvm.org/buildbot/#/builders/261/builds/35
~~This should be fixed, I just forgot to push the commit. Will do that now.~~
Never mind, I thought about another issue. Yes this seems like a problem. @spavloff can you have a look?