Skip to content

Commit c67c95f

Browse files
s-watanabe314tomtor
authored andcommitted
[Clang][Driver] Override complex number calculation method by -fno-fast-math (llvm#132680)
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
1 parent 8acd710 commit c67c95f

File tree

2 files changed

+112
-10
lines changed

2 files changed

+112
-10
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2831,8 +2831,9 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
28312831
StringRef Float16ExcessPrecision = "";
28322832
StringRef BFloat16ExcessPrecision = "";
28332833
LangOptions::ComplexRangeKind Range = LangOptions::ComplexRangeKind::CX_None;
2834-
std::string ComplexRangeStr = "";
2835-
std::string GccRangeComplexOption = "";
2834+
std::string ComplexRangeStr;
2835+
std::string GccRangeComplexOption;
2836+
std::string LastComplexRangeOption;
28362837

28372838
auto setComplexRange = [&](LangOptions::ComplexRangeKind NewRange) {
28382839
// Warn if user expects to perform full implementation of complex
@@ -2916,6 +2917,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29162917
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-limited-range");
29172918
}
29182919
GccRangeComplexOption = "-fcx-limited-range";
2920+
LastComplexRangeOption = A->getSpelling();
29192921
Range = LangOptions::ComplexRangeKind::CX_Basic;
29202922
break;
29212923
case options::OPT_fno_cx_limited_range:
@@ -2929,6 +2931,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29292931
"-fno-cx-limited-range");
29302932
}
29312933
GccRangeComplexOption = "-fno-cx-limited-range";
2934+
LastComplexRangeOption = A->getSpelling();
29322935
Range = LangOptions::ComplexRangeKind::CX_Full;
29332936
break;
29342937
case options::OPT_fcx_fortran_rules:
@@ -2938,6 +2941,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29382941
else
29392942
EmitComplexRangeDiag(D, GccRangeComplexOption, "-fcx-fortran-rules");
29402943
GccRangeComplexOption = "-fcx-fortran-rules";
2944+
LastComplexRangeOption = A->getSpelling();
29412945
Range = LangOptions::ComplexRangeKind::CX_Improved;
29422946
break;
29432947
case options::OPT_fno_cx_fortran_rules:
@@ -2950,6 +2954,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29502954
"-fno-cx-fortran-rules");
29512955
}
29522956
GccRangeComplexOption = "-fno-cx-fortran-rules";
2957+
LastComplexRangeOption = A->getSpelling();
29532958
Range = LangOptions::ComplexRangeKind::CX_Full;
29542959
break;
29552960
case options::OPT_fcomplex_arithmetic_EQ: {
@@ -2984,6 +2989,8 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
29842989
ComplexArithmeticStr(RangeVal));
29852990
}
29862991
}
2992+
LastComplexRangeOption =
2993+
Args.MakeArgString(A->getSpelling() + A->getValue());
29872994
Range = RangeVal;
29882995
break;
29892996
}
@@ -3037,6 +3044,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
30373044
} else
30383045
D.Diag(diag::err_drv_unsupported_option_argument)
30393046
<< A->getSpelling() << Val;
3047+
LastComplexRangeOption = A->getSpelling();
30403048
break;
30413049
}
30423050

@@ -3222,6 +3230,7 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
32223230
[[fallthrough]];
32233231
case options::OPT_ffast_math:
32243232
applyFastMath(true);
3233+
LastComplexRangeOption = A->getSpelling();
32253234
if (A->getOption().getID() == options::OPT_Ofast)
32263235
LastFpContractOverrideOption = "-Ofast";
32273236
else
@@ -3239,6 +3248,15 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
32393248
ApproxFunc = false;
32403249
SignedZeros = true;
32413250
restoreFPContractState();
3251+
// If the last specified option related to complex range is not
3252+
// -ffast-math or -ffp-model=, emit warning.
3253+
if (LastComplexRangeOption != "-ffast-math" &&
3254+
LastComplexRangeOption != "-ffp-model=" &&
3255+
Range != LangOptions::ComplexRangeKind::CX_Full)
3256+
EmitComplexRangeDiag(D, LastComplexRangeOption, "-fno-fast-math");
3257+
Range = LangOptions::ComplexRangeKind::CX_None;
3258+
LastComplexRangeOption = "";
3259+
GccRangeComplexOption = "";
32423260
LastFpContractOverrideOption = "";
32433261
break;
32443262
} // End switch (A->getOption().getID())

clang/test/Driver/range.c

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,14 +177,83 @@
177177
// RUN: %clang -### -target x86_64 -ffast-math -fcomplex-arithmetic=basic -c %s 2>&1 \
178178
// RUN: | FileCheck --check-prefix=BASIC %s
179179

180-
// BASIC: -complex-range=basic
181-
// FULL: -complex-range=full
182-
// PRMTD: -complex-range=promoted
183-
// BASIC-NOT: -complex-range=improved
184-
// CHECK-NOT: -complex-range=basic
185-
// IMPRVD: -complex-range=improved
186-
// IMPRVD-NOT: -complex-range=basic
187-
// CHECK-NOT: -complex-range=improved
180+
// RUN: %clang -### --target=x86_64 -fcx-limited-range -fno-fast-math \
181+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN21 %s
182+
183+
// RUN: %clang -### -Werror --target=x86_64 -fno-cx-limited-range -fno-fast-math \
184+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
185+
186+
// RUN: %clang -### --target=x86_64 -fcx-fortran-rules -fno-fast-math \
187+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN22 %s
188+
189+
// RUN: %clang -### -Werror --target=x86_64 -fno-cx-fortran-rules -fno-fast-math \
190+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
191+
192+
// RUN: %clang -### -Werror --target=x86_64 -ffast-math -fno-fast-math \
193+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
194+
195+
// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=basic -fno-fast-math \
196+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN23 %s
197+
198+
// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=promoted -fno-fast-math \
199+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN24 %s
200+
201+
// RUN: %clang -### --target=x86_64 -fcomplex-arithmetic=improved -fno-fast-math \
202+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE,WARN25 %s
203+
204+
// RUN: %clang -### -Werror --target=x86_64 -fcomplex-arithmetic=full -fno-fast-math \
205+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
206+
207+
// RUN: %clang -### -Werror --target=x86_64 -ffp-model=aggressive -fno-fast-math \
208+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
209+
210+
// RUN: %clang -### -Werror --target=x86_64 -ffp-model=fast -fno-fast-math \
211+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
212+
213+
// RUN: %clang -### -Werror --target=x86_64 -ffp-model=precise -fno-fast-math \
214+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
215+
216+
// RUN: %clang -### -Werror --target=x86_64 -ffp-model=strict -fno-fast-math \
217+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=RANGE %s
218+
219+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcx-limited-range \
220+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
221+
222+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fno-cx-limited-range \
223+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
224+
225+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcx-fortran-rules \
226+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD %s
227+
228+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fno-cx-fortran-rules \
229+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
230+
231+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffast-math \
232+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
233+
234+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=basic \
235+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
236+
237+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=promoted \
238+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD %s
239+
240+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=improved \
241+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=IMPRVD %s
242+
243+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -fcomplex-arithmetic=full \
244+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
245+
246+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=aggressive \
247+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=BASIC %s
248+
249+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=fast \
250+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=PRMTD %s
251+
252+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=precise \
253+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
254+
255+
// RUN: %clang -### -Werror --target=x86_64 -fno-fast-math -ffp-model=strict \
256+
// RUN: -c %s 2>&1 | FileCheck --check-prefixes=FULL %s
188257

189258
// WARN1: warning: overriding '-fcx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
190259
// WARN2: warning: overriding '-fno-cx-limited-range' option with '-fcx-fortran-rules' [-Woverriding-option]
@@ -196,5 +265,20 @@
196265
// WARN14: overriding '-complex-range=promoted' option with '-fcx-limited-range' [-Woverriding-option]
197266
// WARN17: warning: overriding '-fcomplex-arithmetic=full' option with '-fcomplex-arithmetic=basic' [-Woverriding-option]
198267
// WARN20: warning: overriding '-fcx-fortran-rules' option with '-fcx-limited-range' [-Woverriding-option]
268+
// WARN21: warning: overriding '-fcx-limited-range' option with '-fno-fast-math' [-Woverriding-option]
269+
// WARN22: warning: overriding '-fcx-fortran-rules' option with '-fno-fast-math' [-Woverriding-option]
270+
// WARN23: warning: overriding '-fcomplex-arithmetic=basic' option with '-fno-fast-math' [-Woverriding-option]
271+
// WARN24: warning: overriding '-fcomplex-arithmetic=promoted' option with '-fno-fast-math' [-Woverriding-option]
272+
// WARN25: warning: overriding '-fcomplex-arithmetic=improved' option with '-fno-fast-math' [-Woverriding-option]
273+
274+
// BASIC: -complex-range=basic
275+
// FULL: -complex-range=full
276+
// PRMTD: -complex-range=promoted
277+
// BASIC-NOT: -complex-range=improved
278+
// CHECK-NOT: -complex-range=basic
279+
// IMPRVD: -complex-range=improved
280+
// IMPRVD-NOT: -complex-range=basic
281+
// CHECK-NOT: -complex-range=improved
282+
// RANGE-NOT: -complex-range=
199283

200284
// ERR: error: unsupported argument 'foo' to option '-fcomplex-arithmetic='

0 commit comments

Comments
 (0)