<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/64823>64823</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Miscompilation/Assertion "FPConstrained should be enabled on entire function"' failed with FENV_ACCESS and default function argument
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:codegen,
miscompilation,
floating-point
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
hubert-reinterpretcast
</td>
</tr>
</table>
<pre>
With the following source, asserts-enabled builds of Clang hit an assertion:
```
/root/llvm-project/clang/lib/CodeGen/CodeGenFunction.cpp:161: void clang::CodeGen::CodeGenFunction::CGFPOptionsRAII::ConstructorHelper(clang::FPOptions): Assertion `(CGF.CurFuncDecl == nullptr || CGF.Builder.getIsFPConstrained() || isa<CXXConstructorDecl>(CGF.CurFuncDecl) || isa<CXXDestructorDecl>(CGF.CurFuncDecl) || (NewExceptionBehavior == llvm::fp::ebIgnore && NewRoundingBehavior == llvm::RoundingMode::NearestTiesToEven)) && "FPConstrained should be enabled on entire function"' failed.
```
If it is accepted that the pragma state at the point where code is lexically present is to be respected when the code is evaluated, then the non-asserts builds miscompile the code (as can be seen by inspecting the assembly obtained with `-O`; the call to `foo` is not generated).
https://godbolt.org/z/5PKqY5nfa
```cpp
#include <float.h>
struct ChangeRounding {
ChangeRounding();
~ChangeRounding();
};
int foo(), bar();
#pragma STDC FENV_ACCESS ON
struct Ex {
Ex() = default;
Ex(const Ex &, int = (ChangeRounding(), (float)0xFFFFFFFF != (float)0x100000000 ? foo() : bar()));
};
#pragma STDC FENV_ACCESS OFF
void q(const Ex &e) { Ex e2(e); }
```
Clang version info:
```
clang version 18.0.0 (https://github.com/llvm/llvm-project.git 6eb0332e72e5d9c5db1f89bac28770bc6f0725c2)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVc2O2zYQfhr6MlhBHq4t-eCDV7bSRZFN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