Skip to content

Miscompilation/Assertion "FPConstrained should be enabled on entire function"' failed with FENV_ACCESS and default function argument #64823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
hubert-reinterpretcast opened this issue Aug 19, 2023 · 3 comments
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. confirmed Verified by a second party floating-point Floating-point math miscompilation

Comments

@hubert-reinterpretcast
Copy link
Collaborator

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

#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)
@hubert-reinterpretcast hubert-reinterpretcast added clang:codegen IR generation bugs: mangling, exceptions, etc. miscompilation floating-point Floating-point math labels Aug 19, 2023
@llvmbot
Copy link
Member

llvmbot commented Aug 19, 2023

@llvm/issue-subscribers-clang-codegen

@hubert-reinterpretcast
Copy link
Collaborator Author

As can be expected, also fails with default member initializers (at least with aggregate initialization): https://godbolt.org/z/6GjxszdEW

#include <float.h>
struct ChangeRounding {
  ChangeRounding();
  ~ChangeRounding();
};
int foo(), bar();

#pragma STDC FENV_ACCESS ON

struct Ex {
  int x = (ChangeRounding(), (float)0xFFFFFFFF != (float)0x100000000 ? foo() : bar());
};

#pragma STDC FENV_ACCESS OFF
Ex q() { return {}; }

@hubert-reinterpretcast
Copy link
Collaborator Author

@cor3ntin, fyi (as you requested).

@arsenm arsenm added the confirmed Verified by a second party label Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. confirmed Verified by a second party floating-point Floating-point math miscompilation
Projects
None yet
Development

No branches or pull requests

3 participants