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

Serge Pavlov via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 9 02:40:33 PDT 2024


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

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

It must fix the issue #84648.

>From a7f150c90c301cdd5f55cf6eb44f4e8cf77625f1 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] [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



More information about the cfe-commits mailing list