https://github.com/yxsamliu updated 
https://github.com/llvm/llvm-project/pull/238779

>From e3729a3ca4e9ffd673e576919544d55365c195d1 Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <[email protected]>
Date: Wed, 4 Nov 2026 02:19:29 +0000
Subject: [PATCH 1/2] [AMDGPU] Honor FP option overrides in finite-only library
 selection

With -ffast-math -fno-finite-math-only, Clang restores NaN and infinity
handling in generated code, but the AMDGPU driver still selects the ROCm
finite-only control library. This can make expm1f overflow return NaN
instead of positive infinity.

Resolve NaN and infinity assumptions in argument order to select the
finite-only control library. Honor floating-point models, fast-math and
finite-math options, individual NaN/Inf overrides, and effective -Ofast.
Existing OpenCL controls keep their current behavior.

Fixes https://github.com/ROCm/llvm-project/issues/5242
---
 clang/docs/ReleaseNotes.md             |   5 +
 clang/lib/Driver/ToolChains/AMDGPU.cpp |  28 +++++-
 clang/test/Driver/hip-device-libs.hip  | 128 +++++++++++++++++++++++++
 3 files changed, 158 insertions(+), 3 deletions(-)

diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 409d2459b0580b..c2da797d7f4f31 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1265,6 +1265,11 @@ documentation](CIR/index.md) for more information 
regarding ClangIR's status and
 
 #### AMDGPU Support
 
+- Clang now honors floating-point model and individual finite-math, NaN, and
+  infinity option overrides when selecting the ROCm finite-only control 
library.
+  In particular, `-ffast-math -fno-finite-math-only` selects the control 
library
+  that preserves NaN and infinity handling.
+
 - Deprecated the following builtins in favor of `__builtin_amdgcn_ballot_w32` 
or
   `__builtin_amdgcn_ballot_w64`:
   - `__builtin_amdgcn_uicmp`
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp 
b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index 47c36ee18d2272..af922c1a4d5f42 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -35,6 +35,25 @@ using namespace clang::driver::toolchains;
 using namespace clang;
 using namespace llvm::opt;
 
+static bool isNoHonorEnabled(const ArgList &Args, OptSpecifier NoHonorOption,
+                             OptSpecifier HonorOption) {
+  // Ignore -Ofast when another optimization level overrides it.
+  OptSpecifier OfastOption = isOptimizationLevelFast(Args)
+                                 ? options::OPT_Ofast
+                                 : options::OPT_ffast_math;
+  const Arg *A = Args.getLastArg(
+      NoHonorOption, HonorOption, options::OPT_ffast_math,
+      options::OPT_fno_fast_math, options::OPT_ffinite_math_only,
+      options::OPT_fno_finite_math_only, options::OPT_ffp_model_EQ,
+      OfastOption);
+  if (A && A->getOption().matches(options::OPT_ffp_model_EQ))
+    return StringRef(A->getValue()) == "aggressive";
+  return A && (A->getOption().matches(NoHonorOption) ||
+               A->getOption().matches(options::OPT_ffast_math) ||
+               A->getOption().matches(options::OPT_ffinite_math_only) ||
+               A->getOption().matches(options::OPT_Ofast));
+}
+
 RocmInstallationDetector::CommonBitcodeLibsPreferences::
     CommonBitcodeLibsPreferences(const Driver &D,
                                  const llvm::opt::ArgList &DriverArgs,
@@ -73,8 +92,11 @@ RocmInstallationDetector::CommonBitcodeLibsPreferences::
             : DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 
DefaultDAZ;
 
   FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only) ||
-               DriverArgs.hasFlag(options::OPT_ffinite_math_only,
-                                  options::OPT_fno_finite_math_only, false);
+               DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math) ||
+               (isNoHonorEnabled(DriverArgs, options::OPT_fno_honor_infinities,
+                                 options::OPT_fhonor_infinities) &&
+                isNoHonorEnabled(DriverArgs, options::OPT_fno_honor_nans,
+                                 options::OPT_fhonor_nans));
 
   UnsafeMathOpt =
       DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations) ||
@@ -1158,7 +1180,7 @@ RocmInstallationDetector::getCommonBitcodeLibs(
   else if (Pref.GPUSan && Pref.IsOpenMP)
     AddBCLib(getOCKLPath());
   AddBCLib(getUnsafeMathPath(Pref.UnsafeMathOpt || Pref.FastRelaxedMath));
-  AddBCLib(getFiniteOnlyPath(Pref.FiniteOnly || Pref.FastRelaxedMath));
+  AddBCLib(getFiniteOnlyPath(Pref.FiniteOnly));
   AddBCLib(getWavefrontSize64Path(Pref.Wave64));
   AddBCLib(LibDeviceFile);
   auto ABIVerPath = getABIVersionPath(Pref.ABIVer);
diff --git a/clang/test/Driver/hip-device-libs.hip 
b/clang/test/Driver/hip-device-libs.hip
index 77d0bca4565ddf..55e459567b2466 100644
--- a/clang/test/Driver/hip-device-libs.hip
+++ b/clang/test/Driver/hip-device-libs.hip
@@ -136,6 +136,134 @@
 // RUN:   %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
 
+// Individual math options override earlier umbrella options.
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fno-finite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fno-finite-math-only -fno-associative-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-finite-math-only -ffast-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fno-finite-math-only -ffinite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fno-fast-math -ffinite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffinite-math-only -fno-fast-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fno-associative-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fhonor-infinities \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -fhonor-nans \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fhonor-infinities -ffast-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fhonor-nans -ffast-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-honor-infinities -fno-honor-nans \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-honor-infinities -fno-honor-nans -fno-finite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-finite-math-only -fno-honor-infinities -fno-honor-nans \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+
+// FP models and the effective -Ofast setting also affect finite-only math.
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=precise \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=fast \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=strict \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-finite-math-only -ffp-model=aggressive \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=aggressive -fno-finite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=precise -fno-honor-infinities -fno-honor-nans \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=aggressive -fhonor-infinities \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fhonor-infinities -ffp-model=aggressive \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffast-math -ffp-model=precise \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=precise -ffast-math \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -Ofast \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -Ofast -fno-finite-math-only \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -fno-finite-math-only -Ofast \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -Ofast -O2 \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -Ofast -ffp-model=precise \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=precise -Ofast \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
+// RUN:   -ffp-model=aggressive -Ofast -O2 \
+// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
+// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
+
 // Test -ffinite-math-only
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -ffinite-math-only --rocm-path=%S/Inputs/rocm \

>From edeb704518497c52a010b2ed86438d8c2e8243fa Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <[email protected]>
Date: Wed, 18 Nov 2026 20:50:39 +0000
Subject: [PATCH 2/2] [Clang] Share NaN and infinity option handling with
 AMDGPU

The frontend and ROCm library selection both need to honor the final
NaN and infinity options. Use the same ordered FP option handling in
both places, and keep focused HIP checks for the library choice.

AI assisted.
---
 clang/include/clang/Driver/CommonArgs.h    | 11 +++
 clang/lib/Driver/ToolChains/AMDGPU.cpp     | 33 ++------
 clang/lib/Driver/ToolChains/Clang.cpp      | 56 ++++---------
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 36 ++++++++
 clang/test/Driver/hip-device-libs.hip      | 96 +---------------------
 5 files changed, 77 insertions(+), 155 deletions(-)

diff --git a/clang/include/clang/Driver/CommonArgs.h 
b/clang/include/clang/Driver/CommonArgs.h
index 138e18f8641b90..8925875a12e02e 100644
--- a/clang/include/clang/Driver/CommonArgs.h
+++ b/clang/include/clang/Driver/CommonArgs.h
@@ -166,6 +166,17 @@ llvm::StringRef getLTOParallelism(const llvm::opt::ArgList 
&Args,
 
 bool areOptimizationsEnabled(const llvm::opt::ArgList &Args);
 
+/// NaN and infinity options shared by frontend jobs and device library
+/// selection.
+struct FloatingPointValueOptions {
+  bool HonorInfs = true;
+  bool HonorNaNs = true;
+
+  /// Apply the next argument and return whether it affects these options.
+  bool apply(const llvm::opt::Arg &A, bool OFastEnabled);
+  bool isFiniteOnly() const { return !HonorInfs && !HonorNaNs; }
+};
+
 bool isUseSeparateSections(const llvm::Triple &Triple);
 /// Append -ffunction-sections / -fdata-sections to \p CmdArgs when the
 /// corresponding flags are enabled (explicitly or by target default).
diff --git a/clang/lib/Driver/ToolChains/AMDGPU.cpp 
b/clang/lib/Driver/ToolChains/AMDGPU.cpp
index af922c1a4d5f42..a9bba6120821b3 100644
--- a/clang/lib/Driver/ToolChains/AMDGPU.cpp
+++ b/clang/lib/Driver/ToolChains/AMDGPU.cpp
@@ -35,25 +35,6 @@ using namespace clang::driver::toolchains;
 using namespace clang;
 using namespace llvm::opt;
 
-static bool isNoHonorEnabled(const ArgList &Args, OptSpecifier NoHonorOption,
-                             OptSpecifier HonorOption) {
-  // Ignore -Ofast when another optimization level overrides it.
-  OptSpecifier OfastOption = isOptimizationLevelFast(Args)
-                                 ? options::OPT_Ofast
-                                 : options::OPT_ffast_math;
-  const Arg *A = Args.getLastArg(
-      NoHonorOption, HonorOption, options::OPT_ffast_math,
-      options::OPT_fno_fast_math, options::OPT_ffinite_math_only,
-      options::OPT_fno_finite_math_only, options::OPT_ffp_model_EQ,
-      OfastOption);
-  if (A && A->getOption().matches(options::OPT_ffp_model_EQ))
-    return StringRef(A->getValue()) == "aggressive";
-  return A && (A->getOption().matches(NoHonorOption) ||
-               A->getOption().matches(options::OPT_ffast_math) ||
-               A->getOption().matches(options::OPT_ffinite_math_only) ||
-               A->getOption().matches(options::OPT_Ofast));
-}
-
 RocmInstallationDetector::CommonBitcodeLibsPreferences::
     CommonBitcodeLibsPreferences(const Driver &D,
                                  const llvm::opt::ArgList &DriverArgs,
@@ -91,12 +72,14 @@ RocmInstallationDetector::CommonBitcodeLibsPreferences::
                                  DefaultDAZ)
             : DriverArgs.hasArg(options::OPT_cl_denorms_are_zero) || 
DefaultDAZ;
 
-  FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only) ||
-               DriverArgs.hasArg(options::OPT_cl_fast_relaxed_math) ||
-               (isNoHonorEnabled(DriverArgs, options::OPT_fno_honor_infinities,
-                                 options::OPT_fhonor_infinities) &&
-                isNoHonorEnabled(DriverArgs, options::OPT_fno_honor_nans,
-                                 options::OPT_fhonor_nans));
+  FloatingPointValueOptions ValueOptions;
+  const bool OFastEnabled = isOptimizationLevelFast(DriverArgs);
+  for (const Arg *A : DriverArgs)
+    ValueOptions.apply(*A, OFastEnabled);
+
+  FiniteOnly = DriverArgs.hasArg(options::OPT_cl_finite_math_only,
+                                 options::OPT_cl_fast_relaxed_math) ||
+               ValueOptions.isFiniteOnly();
 
   UnsafeMathOpt =
       DriverArgs.hasArg(options::OPT_cl_unsafe_math_optimizations) ||
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp 
b/clang/lib/Driver/ToolChains/Clang.cpp
index d8819c952e5add..5a401ea9bfc0c0 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2984,8 +2984,7 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   // "umbrella" flags, so we do this by stepping through the flags 
incrementally
   // adjusting what we think is enabled/disabled, then at the end setting the
   // LLVM flags based on the final state.
-  bool HonorINFs = true;
-  bool HonorNaNs = true;
+  FloatingPointValueOptions ValueOptions;
   bool ApproxFunc = false;
   // -fmath-errno is the default on some platforms, e.g. BSD-derived OSes.
   bool MathErrno = TC.IsMathErrnoDefault();
@@ -3026,13 +3025,9 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   // Lambda to set fast-math options. This is also used by -ffp-model=fast
   auto applyFastMath = [&](bool Aggressive, StringRef CallerOption) {
     if (Aggressive) {
-      HonorINFs = false;
-      HonorNaNs = false;
       setComplexRange(D, CallerOption, LangOptions::ComplexRangeKind::CX_Basic,
                       LastComplexRangeOption, Range);
     } else {
-      HonorINFs = true;
-      HonorNaNs = true;
       setComplexRange(D, CallerOption,
                       LangOptions::ComplexRangeKind::CX_Promoted,
                       LastComplexRangeOption, Range);
@@ -3076,6 +3071,7 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   }
 
   for (const Arg *A : Args) {
+    bool IsValueOption = ValueOptions.apply(*A, OFastEnabled);
     llvm::scope_exit CheckMathErrnoForVecLib(
         [&, MathErrnoBeforeArg = MathErrno] {
           if (NoMathErrnoWasImpliedByVecLib && !MathErrnoBeforeArg && 
MathErrno)
@@ -3083,8 +3079,10 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
         });
 
     switch (A->getOption().getID()) {
-    // If this isn't an FP option skip the claim below
-    default: continue;
+    default:
+      if (!IsValueOption)
+        continue;
+      break;
 
     case options::OPT_fcx_limited_range:
       setComplexRange(D, A->getSpelling(),
@@ -3127,9 +3125,7 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
       break;
     }
     case options::OPT_ffp_model_EQ: {
-      // If -ffp-model= is seen, reset to fno-fast-math
-      HonorINFs = true;
-      HonorNaNs = true;
+      // If -ffp-model= is seen, reset to fno-fast-math.
       ApproxFunc = false;
       // Turning *off* -ffast-math restores the toolchain default.
       MathErrno = TC.IsMathErrnoDefault();
@@ -3184,10 +3180,6 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
     }
 
     // Options controlling individual features
-    case options::OPT_fhonor_infinities:    HonorINFs = true;         break;
-    case options::OPT_fno_honor_infinities: HonorINFs = false;        break;
-    case options::OPT_fhonor_nans:          HonorNaNs = true;         break;
-    case options::OPT_fno_honor_nans:       HonorNaNs = false;        break;
     case options::OPT_fapprox_func:         ApproxFunc = true;        break;
     case options::OPT_fno_approx_func:      ApproxFunc = false;       break;
     case options::OPT_fmath_errno:          MathErrno = true;         break;
@@ -3330,15 +3322,6 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
       BFloat16ExcessPrecision = Float16ExcessPrecision;
       break;
     }
-    case options::OPT_ffinite_math_only:
-      HonorINFs = false;
-      HonorNaNs = false;
-      break;
-    case options::OPT_fno_finite_math_only:
-      HonorINFs = true;
-      HonorNaNs = true;
-      break;
-
     case options::OPT_funsafe_math_optimizations:
       AssociativeMath = true;
       ReciprocalMath = true;
@@ -3375,8 +3358,6 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
         LastFpContractOverrideOption = "-ffast-math";
       break;
     case options::OPT_fno_fast_math:
-      HonorINFs = true;
-      HonorNaNs = true;
       // Turning on -ffast-math (with either flag) removes the need for
       // MathErrno. However, turning *off* -ffast-math merely restores the
       // toolchain default (which may be false).
@@ -3406,9 +3387,9 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
     if (StrictFPModel) {
       // If -ffp-model=strict has been specified on command line but
       // subsequent options conflict then emit warning diagnostic.
-      if (HonorINFs && HonorNaNs && !AssociativeMath && !ReciprocalMath &&
-          SignedZeros && TrappingMath && RoundingFPMath && !ApproxFunc &&
-          FPContract == "off")
+      if (ValueOptions.HonorInfs && ValueOptions.HonorNaNs &&
+          !AssociativeMath && !ReciprocalMath && SignedZeros && TrappingMath &&
+          RoundingFPMath && !ApproxFunc && FPContract == "off")
         // OK: Current Arg doesn't conflict with -ffp-model=strict
         ;
       else {
@@ -3432,10 +3413,10 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
     A->claim();
   }
 
-  if (!HonorINFs)
+  if (!ValueOptions.HonorInfs)
     CmdArgs.push_back("-menable-no-infs");
 
-  if (!HonorNaNs)
+  if (!ValueOptions.HonorNaNs)
     CmdArgs.push_back("-menable-no-nans");
 
   if (ApproxFunc)
@@ -3512,16 +3493,15 @@ static void RenderFloatingPointOptions(const ToolChain 
&TC, const Driver &D,
   // -ffast-math enables the __FAST_MATH__ preprocessor macro, but check for 
the
   // individual features enabled by -ffast-math instead of the option itself as
   // that's consistent with gcc's behaviour.
-  if (!HonorINFs && !HonorNaNs && !MathErrno && AssociativeMath && ApproxFunc 
&&
-      ReciprocalMath && !SignedZeros && !TrappingMath && !RoundingFPMath)
+  if (ValueOptions.isFiniteOnly() && !MathErrno && AssociativeMath &&
+      ApproxFunc && ReciprocalMath && !SignedZeros && !TrappingMath &&
+      !RoundingFPMath)
     CmdArgs.push_back("-ffast-math");
 
-  // Handle __FINITE_MATH_ONLY__ similarly.
-  // The -ffinite-math-only is added to CmdArgs when !HonorINFs && !HonorNaNs.
-  // Otherwise process the Xclang arguments to determine if -menable-no-infs 
and
-  // -menable-no-nans are set by the user.
+  // Define __FINITE_MATH_ONLY__ if both NaNs and infinities are disabled,
+  // including when both flags are passed directly to cc1 via -Xclang.
   bool shouldAddFiniteMathOnly = false;
-  if (!HonorINFs && !HonorNaNs) {
+  if (ValueOptions.isFiniteOnly()) {
     shouldAddFiniteMathOnly = true;
   } else {
     bool InfValues = true;
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp 
b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index db5250db329a9f..cce7e0f679de8e 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -2004,6 +2004,42 @@ void tools::linkXRayRuntimeDeps(const ToolChain &TC,
     CmdArgs.push_back("-ldl");
 }
 
+bool FloatingPointValueOptions::apply(const Arg &A, bool OFastEnabled) {
+  switch (A.getOption().getID()) {
+  default:
+    return false;
+  case options::OPT_ffp_model_EQ:
+    HonorInfs = HonorNaNs = StringRef(A.getValue()) != "aggressive";
+    break;
+  case options::OPT_Ofast:
+    if (!OFastEnabled)
+      return false;
+    [[fallthrough]];
+  case options::OPT_ffast_math:
+  case options::OPT_cl_fast_relaxed_math:
+  case options::OPT_ffinite_math_only:
+    HonorInfs = HonorNaNs = false;
+    break;
+  case options::OPT_fno_fast_math:
+  case options::OPT_fno_finite_math_only:
+    HonorInfs = HonorNaNs = true;
+    break;
+  case options::OPT_fhonor_infinities:
+    HonorInfs = true;
+    break;
+  case options::OPT_fno_honor_infinities:
+    HonorInfs = false;
+    break;
+  case options::OPT_fhonor_nans:
+    HonorNaNs = true;
+    break;
+  case options::OPT_fno_honor_nans:
+    HonorNaNs = false;
+    break;
+  }
+  return true;
+}
+
 bool tools::areOptimizationsEnabled(const ArgList &Args) {
   // Find the last -O arg and see if it is non-zero.
   if (Arg *A = Args.getLastArg(options::OPT_O_Group))
diff --git a/clang/test/Driver/hip-device-libs.hip 
b/clang/test/Driver/hip-device-libs.hip
index 55e459567b2466..38119611e2eeea 100644
--- a/clang/test/Driver/hip-device-libs.hip
+++ b/clang/test/Driver/hip-device-libs.hip
@@ -136,13 +136,9 @@
 // RUN:   %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
 
-// Individual math options override earlier umbrella options.
+// Later options override individual NaN and infinity assumptions.
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -fno-finite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -fno-finite-math-only -fno-associative-math \
+// RUN:   -O3 -ffast-math -fno-finite-math-only -fno-associative-math \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
@@ -150,22 +146,6 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -fno-finite-math-only -ffinite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -fno-fast-math -ffinite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffinite-math-only -fno-fast-math \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -fno-associative-math \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -ffast-math -fhonor-infinities \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
@@ -174,95 +154,27 @@
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fhonor-infinities -ffast-math \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fhonor-nans -ffast-math \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -fno-honor-infinities -fno-honor-nans \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-honor-infinities -fno-honor-nans -fno-finite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-finite-math-only -fno-honor-infinities -fno-honor-nans \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
 
-// FP models and the effective -Ofast setting also affect finite-only math.
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=precise \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=fast \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=strict \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
+// Models and the effective optimization level feed library selection.
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -fno-finite-math-only -ffp-model=aggressive \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=aggressive -fno-finite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=precise -fno-honor-infinities -fno-honor-nans \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=aggressive -fhonor-infinities \
+// RUN:   -fno-honor-infinities -fno-honor-nans -ffp-model=precise \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fhonor-infinities -ffp-model=aggressive \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffast-math -ffp-model=precise \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=UNSAFE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=precise -ffast-math \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FAST
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -Ofast \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -Ofast -fno-finite-math-only \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -fno-finite-math-only -Ofast \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
 // RUN:   -Ofast -O2 \
 // RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
 // RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -Ofast -ffp-model=precise \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=ALL,ROCMDIR
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=precise -Ofast \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
-// RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \
-// RUN:   -ffp-model=aggressive -Ofast -O2 \
-// RUN:   --rocm-path=%S/Inputs/rocm %S/Inputs/hip_multiple_inputs/b.hip \
-// RUN: 2>&1 | FileCheck %s --check-prefixes=FINITE
 
 // Test -ffinite-math-only
 // RUN: %clang -### --target=x86_64-linux-gnu --offload-arch=gfx900 \

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

Reply via email to