Skip to content

[clang] Set FPOptions at the beginning of CompoundStmt #111654

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -1684,6 +1684,15 @@ class CompoundStmt final
return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
}

/// Get FPOptions active inside this statement. They may differ from the outer
/// options due to pragmas.
/// \param CurFPOptions FPOptions outside this statement.
FPOptions getActiveFPOptions(FPOptions CurFPOptions) const {
return hasStoredFPFeatures()
? getStoredFPFeatures().applyOverrides(CurFPOptions)
: CurFPOptions;
}

using body_iterator = Stmt **;
using body_range = llvm::iterator_range<body_iterator>;

Expand Down
5 changes: 5 additions & 0 deletions clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,11 @@ CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
assert((!GetLast || (GetLast && ExprResult)) &&
"If GetLast is true then the CompoundStmt must have a StmtExprResult");

// Optionally set up the new FP environment, if the compound statement
// contains a pragma that modifies it.
FPOptions NewFP = S.getActiveFPOptions(CurFPFeatures);
CGFPOptionsRAII SavedFPFeatues(*this, NewFP);

Address RetAlloca = Address::invalid();

for (auto *CurStmt : S.body()) {
Expand Down
18 changes: 17 additions & 1 deletion clang/test/CodeGen/fast-math.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %clang_cc1 -ffast-math -ffp-contract=fast -emit-llvm -o - %s | FileCheck %s
// RUN: %clang_cc1 -ffast-math -ffp-contract=fast -emit-llvm -O2 -o - %s | FileCheck %s
float f0, f1, f2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that this issue exists only at O2, right? https://godbolt.org/z/xGcWG1aM6
Shouldn't that be reflected in the code somewhere?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this fact is reflected in #84648. Actually the problem is incorrect optimization caused by wrong fast-math flags.

Also your example cannot demonstrate the issue because it is C++ code. In C++ mode instructions like SelectInst or PHINode do not get fast-math flags.


void foo(void) {
Expand All @@ -9,3 +9,19 @@ void foo(void) {

// CHECK: ret
}

float issue_84648a(float *x) {
return x[0] == x[1] ? x[1] : x[0];
}

// CHECK-LABEL: define{{.*}} float @issue_84648a(ptr {{.*}})
// CHECK: [[VAL:%.+]] = load float, ptr
// CHECK: ret float [[VAL]]

float issue_84648b(float *x) {
#pragma float_control(precise, on)
return x[0] == x[1] ? x[1] : x[0];
}

// CHECK-LABEL: define{{.*}} float @issue_84648b(ptr{{.*}} %x)
// CHECK: fcmp oeq
Loading