Skip to content

Commit a7f150c

Browse files
committed
[clang] Set FPOptions at the beginning of CompoundStmt
CompoundStmt has FPOptions, that should be set for IRBuilder when generating code if that statement. It must fix the issue #84648.
1 parent b2edeb5 commit a7f150c

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

clang/include/clang/AST/Stmt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,6 +1684,15 @@ class CompoundStmt final
16841684
return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
16851685
}
16861686

1687+
/// Get FPOptions inside this statement. They may differ from the outer
1688+
/// options due to pragmas.
1689+
/// \param CurFPOptions FPOptions outside this statement.
1690+
FPOptions getInsideFPOptions(FPOptions CurFPOptions) const {
1691+
return hasStoredFPFeatures()
1692+
? getStoredFPFeatures().applyOverrides(CurFPOptions)
1693+
: CurFPOptions;
1694+
}
1695+
16871696
using body_iterator = Stmt **;
16881697
using body_range = llvm::iterator_range<body_iterator>;
16891698

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,11 @@ CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
522522
assert((!GetLast || (GetLast && ExprResult)) &&
523523
"If GetLast is true then the CompoundStmt must have a StmtExprResult");
524524

525+
// Optionally set up the new FP environment, if the compound statement
526+
// contains a pragma that modifies it.
527+
FPOptions NewFP = S.getInsideFPOptions(CurFPFeatures);
528+
CGFPOptionsRAII SavedFPFeatues(*this, NewFP);
529+
525530
Address RetAlloca = Address::invalid();
526531

527532
for (auto *CurStmt : S.body()) {

clang/test/CodeGen/fast-math.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %clang_cc1 -ffast-math -ffp-contract=fast -emit-llvm -o - %s | FileCheck %s
1+
// RUN: %clang_cc1 -ffast-math -ffp-contract=fast -emit-llvm -O2 -o - %s | FileCheck %s
22
float f0, f1, f2;
33

44
void foo(void) {
@@ -9,3 +9,19 @@ void foo(void) {
99

1010
// CHECK: ret
1111
}
12+
13+
float issue_84648a(float *x) {
14+
return x[0] == x[1] ? x[1] : x[0];
15+
}
16+
17+
// CHECK-LABEL: define{{.*}} float @issue_84648a(ptr {{.*}})
18+
// CHECK: [[VAL:%.+]] = load float, ptr
19+
// CHECK: ret float [[VAL]]
20+
21+
float issue_84648b(float *x) {
22+
#pragma float_control(precise, on)
23+
return x[0] == x[1] ? x[1] : x[0];
24+
}
25+
26+
// CHECK-LABEL: define{{.*}} float @issue_84648b(ptr{{.*}} %x)
27+
// CHECK: fcmp oeq

0 commit comments

Comments
 (0)