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

via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 02:41:09 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Serge Pavlov (spavloff)

<details>
<summary>Changes</summary>

CompoundStmt has FPOptions, that should be set for IRBuilder when generating code if that statement.

It must fix the issue #<!-- -->84648.

---
Full diff: https://github.com/llvm/llvm-project/pull/111654.diff


3 Files Affected:

- (modified) clang/include/clang/AST/Stmt.h (+9) 
- (modified) clang/lib/CodeGen/CGStmt.cpp (+5) 
- (modified) clang/test/CodeGen/fast-math.c (+17-1) 


``````````diff
diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index 83fafbabb1d460..805148bce4aff1 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -1684,6 +1684,15 @@ class CompoundStmt final
     return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
   }
 
+  /// Get FPOptions inside this statement. They may differ from the outer
+  /// options due to pragmas.
+  /// \param CurFPOptions FPOptions outside this statement.
+  FPOptions getInsideFPOptions(FPOptions CurFPOptions) const {
+    return hasStoredFPFeatures()
+               ? getStoredFPFeatures().applyOverrides(CurFPOptions)
+               : CurFPOptions;
+  }
+
   using body_iterator = Stmt **;
   using body_range = llvm::iterator_range<body_iterator>;
 
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 41dc91c578c800..da9bf76a54f963 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -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.getInsideFPOptions(CurFPFeatures);
+  CGFPOptionsRAII SavedFPFeatues(*this, NewFP);
+
   Address RetAlloca = Address::invalid();
 
   for (auto *CurStmt : S.body()) {
diff --git a/clang/test/CodeGen/fast-math.c b/clang/test/CodeGen/fast-math.c
index 6ebd65a22c92be..048a5aeb9649ba 100644
--- a/clang/test/CodeGen/fast-math.c
+++ b/clang/test/CodeGen/fast-math.c
@@ -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;
 
 void foo(void) {
@@ -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

``````````

</details>


https://github.com/llvm/llvm-project/pull/111654


More information about the cfe-commits mailing list