https://github.com/spall created 
https://github.com/llvm/llvm-project/pull/243282

Simplify -ffp-contract state tracking by replacing 'LastSeenFfpContractOption' 
and 'SeenUnsafeMathModeOption' with a single optional fallback value 
'FPContractFallback';

Explicit -ffp-contract options set the fallback to their value. Unsafe-math 
options establish "on" as the fallback only when none exists. Disabling unsafe 
math restores that fallback, preserving existing option-ordering behavior and 
CUDA/HIP exceptions.

Share the contraction-enabling logic through enableUnsafeFPContract() and 
remove a redundant diagnostic-state reset.

>From 6a2815c9a5e1bbd7c6b65af487fca41b2cd7b5d8 Mon Sep 17 00:00:00 2001
From: Sarah Spall <[email protected]>
Date: Thu, 12 Nov 2026 16:05:57 -0800
Subject: [PATCH] try to reconfigure fp contract flag

---
 clang/lib/Driver/ToolChains/Clang.cpp | 29 ++++++++++++++-------------
 1 file changed, 15 insertions(+), 14 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index bd80f146f3a45d..e3268d610ecdaf 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -62,6 +62,7 @@
 #include "llvm/TargetParser/RISCVTargetParser.h"
 #include <cctype>
 #include <iterator>
+#include <optional>
 
 using namespace clang::driver;
 using namespace clang::driver::tools;
@@ -3010,9 +3011,10 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   // CUDA and HIP don't rely on the frontend to pass an ffp-contract option.
   // If one wasn't given by the user, don't pass it here.
   StringRef FPContract;
-  StringRef LastSeenFfpContractOption;
+  // Explicit contraction options and unsafe math establish a fallback.
+  // Without one, disabling unsafe math leaves precise/strict models unchanged.
+  std::optional<StringRef> FPContractFallback;
   StringRef LastFpContractOverrideOption;
-  bool SeenUnsafeMathModeOption = false;
   if (!JA.isDeviceOffloading(Action::OFK_Cuda) &&
       !JA.isOffloading(Action::OFK_HIP))
     FPContract = "on";
@@ -3023,6 +3025,12 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   std::string ComplexRangeStr;
   StringRef LastComplexRangeOption;
 
+  auto enableUnsafeFPContract = [&]() {
+    FPContract = "fast";
+    if (!FPContractFallback)
+      FPContractFallback = "on";
+  };
+
   // Lambda to set fast-math options. This is also used by -ffp-model=fast
   auto applyFastMath = [&](bool Aggressive, StringRef CallerOption) {
     if (Aggressive) {
@@ -3045,8 +3053,7 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
     TrappingMath = false;
     RoundingFPMath = false;
     FPExceptionBehavior = "";
-    FPContract = "fast";
-    SeenUnsafeMathModeOption = true;
+    enableUnsafeFPContract();
   };
 
   // Lambda to consolidate common handling for fp-contract
@@ -3059,12 +3066,8 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
     // option. If we have not seen an unsafe-math option or -ffp-contract,
     // we leave the FPContract state unchanged.
     if (!JA.isDeviceOffloading(Action::OFK_Cuda) &&
-        !JA.isOffloading(Action::OFK_HIP)) {
-      if (LastSeenFfpContractOption != "")
-        FPContract = LastSeenFfpContractOption;
-      else if (SeenUnsafeMathModeOption)
-        FPContract = "on";
-    }
+        !JA.isOffloading(Action::OFK_HIP) && FPContractFallback)
+      FPContract = *FPContractFallback;
     // In this case, we're reverting to the last explicit fp-contract option
     // or the platform default
     LastFpContractOverrideOption = "";
@@ -3267,7 +3270,7 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
         }
 
         FPContract = Val;
-        LastSeenFfpContractOption = Val;
+        FPContractFallback = Val;
         LastFpContractOverrideOption = "";
       } else
         D.Diag(diag::err_drv_unsupported_option_argument)
@@ -3346,9 +3349,8 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
       ApproxFunc = true;
       TrappingMath = false;
       FPExceptionBehavior = "";
-      FPContract = "fast";
+      enableUnsafeFPContract();
       LastFpContractOverrideOption = "-funsafe-math-optimizations";
-      SeenUnsafeMathModeOption = true;
       break;
     case options::OPT_fno_unsafe_math_optimizations:
       AssociativeMath = false;
@@ -3393,7 +3395,6 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
       else
         Range = LangOptions::ComplexRangeKind::CX_None;
       LastComplexRangeOption = "";
-      LastFpContractOverrideOption = "";
       break;
     } // End switch (A->getOption().getID())
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to