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

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 11 06:30:17 PST 2024


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

>From a47216330a0e14025ba2c29a884d6a96d472dd73 Mon Sep 17 00:00:00 2001
From: Serge Pavlov <sepavloff at gmail.com>
Date: Wed, 9 Oct 2024 12:55:22 +0700
Subject: [PATCH 1/2] [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.
---
 clang/include/clang/AST/Stmt.h |  9 +++++++++
 clang/lib/CodeGen/CGStmt.cpp   |  5 +++++
 clang/test/CodeGen/fast-math.c | 18 +++++++++++++++++-
 3 files changed, 31 insertions(+), 1 deletion(-)

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

>From 63ccc1758ba9d01813ff2b129e533a15926db32e Mon Sep 17 00:00:00 2001
From: Serge Pavlov <sepavloff at gmail.com>
Date: Mon, 11 Nov 2024 21:28:57 +0700
Subject: [PATCH 2/2] Rename getInsideFPOptions to getActiveFPOptions

---
 clang/include/clang/AST/Stmt.h | 4 ++--
 clang/lib/CodeGen/CGStmt.cpp   | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index 805148bce4aff1..eb423cc3469d1a 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -1684,10 +1684,10 @@ class CompoundStmt final
     return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
   }
 
-  /// Get FPOptions inside this statement. They may differ from the outer
+  /// Get FPOptions active inside this statement. They may differ from the outer
   /// options due to pragmas.
   /// \param CurFPOptions FPOptions outside this statement.
-  FPOptions getInsideFPOptions(FPOptions CurFPOptions) const {
+  FPOptions getActiveFPOptions(FPOptions CurFPOptions) const {
     return hasStoredFPFeatures()
                ? getStoredFPFeatures().applyOverrides(CurFPOptions)
                : CurFPOptions;
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index da9bf76a54f963..d5ee66f2946458 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -524,7 +524,7 @@ CodeGenFunction::EmitCompoundStmtWithoutScope(const CompoundStmt &S,
 
   // Optionally set up the new FP environment, if the compound statement
   // contains a pragma that modifies it.
-  FPOptions NewFP = S.getInsideFPOptions(CurFPFeatures);
+  FPOptions NewFP = S.getActiveFPOptions(CurFPFeatures);
   CGFPOptionsRAII SavedFPFeatues(*this, NewFP);
 
   Address RetAlloca = Address::invalid();



More information about the cfe-commits mailing list