[clang] [Clang][Driver] Override complex number calculation method by -fno-fa… (PR #132680)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Mar 26 02:04:56 PDT 2025
https://github.com/s-watanabe314 updated https://github.com/llvm/llvm-project/pull/132680
>From 9265cb96cc3129fada722d7195a1cf04e985ba33 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Fri, 14 Mar 2025 11:56:32 +0900
Subject: [PATCH 1/2] [Clang][Driver] Override complex number calculation
method by -fno-fast-math
This patch fixes a bug where -fno-fast-math doesn't revert the complex
number calculation method to the default. The priority of overriding
options related to complex number calculations differs slightly from
GCC, as discussed in:
https://discourse.llvm.org/t/the-priority-of-fno-fast-math-regarding-complex-number-calculations/84679
---
clang/lib/Driver/ToolChains/Clang.cpp | 22 +++++++++-
clang/test/Driver/range.c | 61 +++++++++++++++++++++++----
2 files changed, 74 insertions(+), 9 deletions(-)
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index fb3ed2db0e3c0..484ced9885883 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2997,6 +2997,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None;
std::string ComplexRangeStr = "";
std::string GccRangeComplexOption = "";
+ std::string LastComplexRangeOption = "";
auto setComplexRange = [&](LangOptions::ComplexRangeKind NewRange) {
// Warn if user expects to perform full implementation of complex
@@ -3015,7 +3016,12 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
if (Aggressive) {
HonorINFs = false;
HonorNaNs = false;
- setComplexRange(LangOptions::ComplexRangeKind::CX_Basic);
+ // If the last specified option related to complex range is
+ // -fno-fast-math, override 'Range' without warning.
+ if (LastComplexRangeOption == "-fno-fast-math")
+ Range = LangOptions::ComplexRangeKind::CX_Basic;
+ else
+ setComplexRange(LangOptions::ComplexRangeKind::CX_Basic);
} else {
HonorINFs = true;
HonorNaNs = true;
@@ -3080,6 +3086,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-limited-range");
}
GccRangeComplexOption = "-fcx-limited-range";
+ LastComplexRangeOption = "-fcx-limited-range";
Range = LangOptions::ComplexRangeKind::CX_Basic;
break;
case options::OPT_fno_cx_limited_range:
@@ -3093,6 +3100,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
"-fno-cx-limited-range");
}
GccRangeComplexOption = "-fno-cx-limited-range";
+ LastComplexRangeOption = "-fno-cx-limited-range";
Range = LangOptions::ComplexRangeKind::CX_Full;
break;
case options::OPT_fcx_fortran_rules:
@@ -3102,6 +3110,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
else
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-fortran-rules");
GccRangeComplexOption = "-fcx-fortran-rules";
+ LastComplexRangeOption = "-fcx-fortran-rules";
Range = LangOptions::ComplexRangeKind::CX_Improved;
break;
case options::OPT_fno_cx_fortran_rules:
@@ -3114,6 +3123,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
"-fno-cx-fortran-rules");
}
GccRangeComplexOption = "-fno-cx-fortran-rules";
+ LastComplexRangeOption = "-fno-cx-fortran-rules";
Range = LangOptions::ComplexRangeKind::CX_Full;
break;
case options::OPT_fcomplex_arithmetic_EQ: {
@@ -3148,6 +3158,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
ComplexArithmeticStr(RangeVal));
}
}
+ LastComplexRangeOption = "-fcomplex-arithmetic";
Range = RangeVal;
break;
}
@@ -3201,6 +3212,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
} else
D.Diag(diag::err_drv_unsupported_option_argument)
<< A->getSpelling() << Val;
+ LastComplexRangeOption = "-ffp-model";
break;
}
@@ -3386,6 +3398,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
[[fallthrough]];
case options::OPT_ffast_math:
applyFastMath(true);
+ LastComplexRangeOption = "-ffast-math";
if (A->getOption().getID() == options::OPT_Ofast)
LastFpContractOverrideOption = "-Ofast";
else
@@ -3403,6 +3416,13 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
ApproxFunc = false;
SignedZeros = true;
restoreFPContractState();
+ // If the last specified option related to complex range is -ffast-math,
+ // override 'Range' without warning.
+ if (LastComplexRangeOption == "-ffast-math")
+ Range = LangOptions::ComplexRangeKind::CX_Full;
+ else
+ setComplexRange(LangOptions::ComplexRangeKind::CX_Full);
+ LastComplexRangeOption = "-fno-fast-math";
LastFpContractOverrideOption = "";
break;
} // End switch (A->getOption().getID())
diff --git a/clang/test/Driver/range.c b/clang/test/Driver/range.c
index da5748d7c723c..315b54ea693a2 100644
--- a/clang/test/Driver/range.c
+++ b/clang/test/Driver/range.c
@@ -177,14 +177,45 @@
// RUN: %clang -### -target x86_64 -ffast-math -fcomplex-arithmetic=basic -c %s 2>&1 \
// RUN: | FileCheck --check-prefix=BASIC %s
-// BASIC: -complex-range=basic
-// FULL: -complex-range=full
-// PRMTD: -complex-range=promoted
-// BASIC-NOT: -complex-range=improved
-// CHECK-NOT: -complex-range=basic
-// IMPRVD: -complex-range=improved
-// IMPRVD-NOT: -complex-range=basic
-// CHECK-NOT: -complex-range=improved
+// RUN: %clang -### -target x86_64 -fcx-limited-range -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN21 %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-cx-limited-range -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -target x86_64 -fcx-fortran-rules -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN22 %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-cx-fortran-rules -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -Werror -target x86_64 -ffast-math -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -target x86_64 -fcomplex-arithmetic=basic -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN23 %s
+
+// RUN: %clang -### -target x86_64 -fcomplex-arithmetic=promoted -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN24 %s
+
+// RUN: %clang -### -target x86_64 -fcomplex-arithmetic=improved -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN25 %s
+
+// RUN: %clang -### -Werror -target x86_64 -fcomplex-arithmetic=full -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -target x86_64 -ffp-model=aggressive -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN23 %s
+
+// RUN: %clang -### -target x86_64 -ffp-model=fast -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL,WARN24 %s
+
+// RUN: %clang -### -Werror -target x86_64 -ffp-model=precise -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -Werror -target x86_64 -ffp-model=strict -fno-fast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
// WARN1: warning: overriding '-fcx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
// WARN2: warning: overriding '-fno-cx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
@@ -196,5 +227,19 @@
// WARN14: overriding '-complex-range=promoted' option with '-fcx-limited-range' [-Woverriding-option]
// WARN17: warning: overriding '-fcomplex-arithmetic=full' option with '-fcomplex-arithmetic=basic' [-Woverriding-option]
// WARN20: warning: overriding '-fcx-fortran-rules' option with '-fcx-limited-range' [-Woverriding-option]
+// WARN21: warning: overriding '-fcx-limited-range' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+// WARN22: warning: overriding '-fcx-fortran-rules' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+// WARN23: warning: overriding '-fcomplex-arithmetic=basic' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+// WARN24: warning: overriding '-fcomplex-arithmetic=promoted' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+// WARN25: warning: overriding '-fcomplex-arithmetic=improved' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+
+// BASIC: -complex-range=basic
+// FULL: -complex-range=full
+// PRMTD: -complex-range=promoted
+// BASIC-NOT: -complex-range=improved
+// CHECK-NOT: -complex-range=basic
+// IMPRVD: -complex-range=improved
+// IMPRVD-NOT: -complex-range=basic
+// CHECK-NOT: -complex-range=improved
// ERR: error: unsupported argument 'foo' to option '-fcomplex-arithmetic='
>From b0e198b2a0d2e93d9fd8038a9485cd5fd1a64841 Mon Sep 17 00:00:00 2001
From: s-watanabe314 <watanabe.shu-06 at fujitsu.com>
Date: Wed, 26 Mar 2025 18:01:20 +0900
Subject: [PATCH 2/2] Add test cases where -fno-fast-math is specified first
---
clang/test/Driver/range.c | 44 +++++++++++++++++++++++++++++++++++++++
1 file changed, 44 insertions(+)
diff --git a/clang/test/Driver/range.c b/clang/test/Driver/range.c
index 315b54ea693a2..96597b843d127 100644
--- a/clang/test/Driver/range.c
+++ b/clang/test/Driver/range.c
@@ -216,6 +216,44 @@
// RUN: %clang -### -Werror -target x86_64 -ffp-model=strict -fno-fast-math \
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+// RUN: %clang -### -target x86_64 -fno-fast-math -fcx-limited-range \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC,WARN26 %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fno-cx-limited-range \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fcx-fortran-rules \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD,WARN27 %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fno-cx-fortran-rules \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-fast-math -ffast-math \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fcomplex-arithmetic=basic \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fcomplex-arithmetic=promoted \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -fcomplex-arithmetic=improved \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-fast-math -fcomplex-arithmetic=full \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -ffp-model=aggressive \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
+
+// RUN: %clang -### -target x86_64 -fno-fast-math -ffp-model=fast \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD,WARN31 %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-fast-math -ffp-model=precise \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
+
+// RUN: %clang -### -Werror -target x86_64 -fno-fast-math -ffp-model=strict \
+// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
// WARN1: warning: overriding '-fcx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
// WARN2: warning: overriding '-fno-cx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
@@ -232,6 +270,12 @@
// WARN23: warning: overriding '-fcomplex-arithmetic=basic' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
// WARN24: warning: overriding '-fcomplex-arithmetic=promoted' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
// WARN25: warning: overriding '-fcomplex-arithmetic=improved' option with '-fcomplex-arithmetic=full' [-Woverriding-option]
+// WARN26: warning: overriding '-complex-range=full' option with '-fcx-limited-range' [-Woverriding-option]
+// WARN27: warning: overriding '-complex-range=full' option with '-fcx-fortran-rules' [-Woverriding-option]
+// WARN28: warning: overriding '-complex-range=full' option with '-fcomplex-arithmetic=basic' [-Woverriding-option]
+// WARN29: warning: overriding '-complex-range=full' option with '-fcomplex-arithmetic=promoted' [-Woverriding-option]
+// WARN30: warning: overriding '-complex-range=full' option with '-fcomplex-arithmetic=improved' [-Woverriding-option]
+// WARN31: warning: overriding '-fcomplex-arithmetic=full' option with '-fcomplex-arithmetic=promoted' [-Woverriding-option]
// BASIC: -complex-range=basic
// FULL: -complex-range=full
More information about the cfe-commits
mailing list