[clang] Fix opencl -cl-fast-relaxed-math flag to have similar behaviour as ff… (PR #208709)
Hardik Kumar via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 05:25:21 PDT 2026
https://github.com/hardikxk created https://github.com/llvm/llvm-project/pull/208709
Previously when using `-cl-fast-relaxed-math` flag along with `fhonor-nans` or `fhonor-infinities` the honor flags did not override the relaxed math flag. This behaviour is something that is not seen in case of ffast-math for general targets.
This patch makes adjustments to how the driver and cc1 handles the relaxed math flag for opencl. The flag was in the path of `fast-math` so when someone used the `-cl-fast-relaxed-math` flag they were ultimately overriding everything with fast-math. This was the main reason why honor flags were not overriding relaxed math (atleast that's what I have understood from my time working on this).
I have added tests for this as well and modified CodeGen cc1 tests according to the changes in the patch.
I have personally not tested this on any AMD targets myself yet. So I am unsure about this breaking anything (hopefully it should rather fix).
closes #178514
>From 50e3902f52bfb4799dad3c500883e2dc8d1ae3f3 Mon Sep 17 00:00:00 2001
From: hardikxk <hardikxk at gmail.com>
Date: Fri, 10 Jul 2026 17:53:18 +0530
Subject: [PATCH] Fix opencl -cl-fast-relaxed-math flag to have similar
behaviour as ffast-math flag.
Previously when using `-cl-fast-relaxed-math` flag along with `fhonor-nans` or `fhonor-infinities` the honor flags did not override the relaxed math flag. This behaviour is something that is not seen in case of ffast-math for general targets.
This patch makes adjustments to how the driver and cc1 handles the relaxed math flag for opencl. The flag was in the path of `fast-math` so when someone used the `-cl-fast-relaxed-math` flag they were ultimately overriding everything with fast-math. This was the main reason why honor flags were not overriding relaxed math (atleast that's what I have understood from my time working on this).
I have added tests for this as well and modified CodeGen cc1 tests according to the changes in the patch.
I have personally not tested this on any AMD targets myself yet. So I am unsure about this breaking anything (hopefully it should rather fix).
closes #178514
---
clang/include/clang/Options/Options.td | 3 +--
clang/lib/Driver/ToolChains/Clang.cpp | 4 ++++
clang/test/CodeGenOpenCL/amdgpu-ieee.cl | 2 +-
clang/test/CodeGenOpenCL/relaxed-fpmath.cl | 4 ++--
clang/test/Driver/opencl.cl | 4 ++++
5 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index dedbab4869997..9994b890413ad 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -2454,8 +2454,7 @@ def ffp_exception_behavior_EQ : Joined<["-"], "ffp-exception-behavior=">, Group<
defm fast_math : BoolFOption<"fast-math",
LangOpts<"FastMath">, Default<hlsl.KeyPath>,
PosFlag<SetTrue, [], [ClangOption, CC1Option, FC1Option, FlangOption],
- "Allow aggressive, lossy floating-point optimizations",
- [cl_fast_relaxed_math.KeyPath]>,
+ "Allow aggressive, lossy floating-point optimizations">,
NegFlag<SetFalse, [], [ClangOption, CC1Option, FC1Option, FlangOption]>>;
defm math_errno : BoolFOption<"math-errno",
LangOpts<"MathErrno">, DefaultFalse,
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index a30b01a675b99..941cbb3c5e067 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -3295,6 +3295,10 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
restoreFPContractState();
break;
+ case options::OPT_cl_fast_relaxed_math:
+ applyFastMath(true, A->getSpelling());
+ break;
+
case options::OPT_Ofast:
// If -Ofast is the optimization level, then -ffast-math should be enabled
if (!OFastEnabled)
diff --git a/clang/test/CodeGenOpenCL/amdgpu-ieee.cl b/clang/test/CodeGenOpenCL/amdgpu-ieee.cl
index 35ddbcfb3d4bf..7f9b9f21047da 100644
--- a/clang/test/CodeGenOpenCL/amdgpu-ieee.cl
+++ b/clang/test/CodeGenOpenCL/amdgpu-ieee.cl
@@ -9,7 +9,7 @@
// RUN: | FileCheck -check-prefixes=COMMON,OFF %s
// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -O0 -emit-llvm -o - %s \
// RUN: -target-feature +dx10-clamp-and-ieee-mode \
-// RUN: -mno-amdgpu-ieee -cl-fast-relaxed-math \
+// RUN: -mno-amdgpu-ieee -cl-fast-relaxed-math -menable-no-nans \
// RUN: | FileCheck -check-prefixes=COMMON,OFF %s
// Check AMDGCN ISA generation.
diff --git a/clang/test/CodeGenOpenCL/relaxed-fpmath.cl b/clang/test/CodeGenOpenCL/relaxed-fpmath.cl
index 9228e2b680f38..e875a6c9d1ad8 100644
--- a/clang/test/CodeGenOpenCL/relaxed-fpmath.cl
+++ b/clang/test/CodeGenOpenCL/relaxed-fpmath.cl
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 %s -emit-llvm -o - | FileCheck %s -check-prefix=NORMAL
-// RUN: %clang_cc1 %s -emit-llvm -cl-fast-relaxed-math -o - | FileCheck %s -check-prefix=FAST
+// RUN: %clang_cc1 %s -emit-llvm -cl-fast-relaxed-math -menable-no-nans -menable-no-infs -mreassociate -freciprocal-math -fapprox-func -fno-signed-zeros -o - | FileCheck %s -check-prefix=FAST
// RUN: %clang_cc1 %s -emit-llvm -menable-no-infs -menable-no-nans -cl-finite-math-only -o - | FileCheck %s -check-prefix=FINITE
// RUN: %clang_cc1 %s -emit-llvm -cl-unsafe-math-optimizations -o - | FileCheck %s -check-prefix=UNSAFE
// RUN: %clang_cc1 %s -emit-llvm -cl-mad-enable -o - | FileCheck %s -check-prefix=MAD
@@ -8,7 +8,7 @@
// Check the fp options are correct with PCH.
// RUN: %clang_cc1 %s -DGEN_PCH=1 -finclude-default-header -triple spir-unknown-unknown -emit-pch -o %t.pch
// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -o - | FileCheck %s -check-prefix=NORMAL
-// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -cl-fast-relaxed-math -o - | FileCheck %s -check-prefix=FAST
+// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -cl-fast-relaxed-math -menable-no-nans -menable-no-infs -mreassociate -freciprocal-math -fapprox-func -fno-signed-zeros -o - | FileCheck %s -check-prefix=FAST
// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -menable-no-infs -menable-no-nans -cl-finite-math-only -o - | FileCheck %s -check-prefix=FINITE
// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -cl-unsafe-math-optimizations -o - | FileCheck %s -check-prefix=UNSAFE
// RUN: %clang_cc1 %s -include-pch %t.pch -fno-validate-pch -emit-llvm -cl-mad-enable -o - | FileCheck %s -check-prefix=MAD
diff --git a/clang/test/Driver/opencl.cl b/clang/test/Driver/opencl.cl
index ba941d5f48feb..8024c27f69f69 100644
--- a/clang/test/Driver/opencl.cl
+++ b/clang/test/Driver/opencl.cl
@@ -13,6 +13,8 @@
// RUN: %clang -S -### -cl-kernel-arg-info %s 2>&1 | FileCheck --check-prefix=CHECK-KERNEL-ARG-INFO %s
// RUN: %clang -S -### -cl-unsafe-math-optimizations %s 2>&1 | FileCheck --check-prefix=CHECK-UNSAFE-MATH-OPT %s
// RUN: %clang -S -### -cl-fast-relaxed-math %s 2>&1 | FileCheck --check-prefix=CHECK-FAST-RELAXED-MATH %s
+// RUN: %clang -S -### -cl-fast-relaxed-math -fhonor-nans %s 2>&1 | FileCheck --check-prefixes=CHECK-FAST-RELAXED-MATH,NO-NNAN %s
+// RUN: %clang -S -### -cl-fast-relaxed-math -fhonor-infinities %s 2>&1 | FileCheck --check-prefixes=CHECK-FAST-RELAXED-MATH,NO-NINF %s
// RUN: %clang -S -### -cl-mad-enable %s 2>&1 | FileCheck --check-prefix=CHECK-MAD-ENABLE %s
// RUN: %clang -S -### -cl-no-signed-zeros %s 2>&1 | FileCheck --check-prefix=CHECK-NO-SIGNED-ZEROS %s
// RUN: %clang -S -### -cl-denorms-are-zero %s 2>&1 | FileCheck --check-prefix=CHECK-DENORMS-ARE-ZERO %s
@@ -43,6 +45,8 @@
// CHECK-FAST-RELAXED-MATH: "-cc1" {{.*}} "-cl-fast-relaxed-math"
// CHECK-MAD-ENABLE: "-cc1" {{.*}} "-cl-mad-enable"
// CHECK-NO-SIGNED-ZEROS: "-cc1" {{.*}} "-cl-no-signed-zeros"
+// NO-NNAN-NOT: "menable-no-nans"
+// NO-NINF-NOT: "menable-no-infs"
// This is not forwarded
// CHECK-DENORMS-ARE-ZERO-NOT: "-cl-denorms-are-zero"
More information about the cfe-commits
mailing list