Skip to content

Commit b40c534

Browse files
zahiraamjhuber6gryppnbpateljrtc27
authored
[clang] Add support for -fcx-limited-range, #pragma CX_LIMITED_RANGE and -fcx-fortran-rules. (#70244)
This patch adds the #pragma CX_LIMITED_RANGE defined in the C specification. It also adds the options -f[no]cx-limited-range and -f[no]cx-fortran-rules. -fcx-limited-range enables algebraic formulas for complex multiplication and division. This option is enabled with -ffast-math. -fcx-fortran-rules enables algebraic formulas for complex multiplication and enables Smith’s algorithm for complex division (SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962)). --------- Signed-off-by: Med Ismail Bennani <[email protected]> Co-authored-by: Joseph Huber <[email protected]> Co-authored-by: Guray Ozen <[email protected]> Co-authored-by: Nishant Patel <[email protected]> Co-authored-by: Jessica Clarke <[email protected]> Co-authored-by: Petr Hosek <[email protected]> Co-authored-by: Joseph Huber <[email protected]> Co-authored-by: Craig Topper <[email protected]> Co-authored-by: Alexander Yermolovich <[email protected]> Co-authored-by: Usama Hameed <[email protected]> Co-authored-by: Philip Reames <[email protected]> Co-authored-by: Evgenii Kudriashov <[email protected]> Co-authored-by: Fangrui Song <[email protected]> Co-authored-by: Aart Bik <[email protected]> Co-authored-by: Valentin Clement <[email protected]> Co-authored-by: Youngsuk Kim <[email protected]> Co-authored-by: Arthur Eubanks <[email protected]> Co-authored-by: Jan Svoboda <[email protected]> Co-authored-by: Walter Erquinigo <[email protected]> Co-authored-by: Eric <[email protected]> Co-authored-by: Fazlay Rabbi <[email protected]> Co-authored-by: Pete Lawrence <[email protected]> Co-authored-by: Jonas Devlieghere <[email protected]> Co-authored-by: Adrian Prantl <[email protected]> Co-authored-by: Owen Pan <[email protected]> Co-authored-by: LLVM GN Syncbot <[email protected]> Co-authored-by: Med Ismail Bennani <[email protected]> Co-authored-by: Congcong Cai <[email protected]> Co-authored-by: Rik Huijzer <[email protected]> Co-authored-by: Wang Pengcheng <[email protected]> Co-authored-by: Yuanfang Chen <[email protected]> Co-authored-by: Kazu Hirata <[email protected]> Co-authored-by: Mehdi Amini <[email protected]> Co-authored-by: Aiden Grossman <[email protected]> Co-authored-by: Rana Pratap Reddy <[email protected]> Co-authored-by: Yingwei Zheng <[email protected]> Co-authored-by: Piotr Zegar <[email protected]> Co-authored-by: KAWASHIMA Takahiro <[email protected]> Co-authored-by: Tobias Hieta <[email protected]> Co-authored-by: Luke Lau <[email protected]> Co-authored-by: Shivam Gupta <[email protected]> Co-authored-by: cor3ntin <[email protected]> Co-authored-by: Yeting Kuo <[email protected]> Co-authored-by: Stanislav Mekhanoshin <[email protected]> Co-authored-by: David Spickett <[email protected]> Co-authored-by: Matthew Devereau <[email protected]> Co-authored-by: Martin Storsjö <[email protected]> Co-authored-by: Qiu Chaofan <[email protected]> Co-authored-by: Pierre van Houtryve <[email protected]> Co-authored-by: Mikael Holmen <[email protected]> Co-authored-by: Uday Bondhugula <[email protected]> Co-authored-by: Nikita Popov <[email protected]> Co-authored-by: Johannes Reifferscheid <[email protected]> Co-authored-by: Benjamin Kramer <[email protected]> Co-authored-by: Oliver Stannard <[email protected]> Co-authored-by: Dmitry Vyukov <[email protected]> Co-authored-by: Benjamin Maxwell <[email protected]> Co-authored-by: Piotr Sobczak <[email protected]> Co-authored-by: Simon Pilgrim <[email protected]> Co-authored-by: Timm Bäder <[email protected]> Co-authored-by: Sunil Kuravinakop <[email protected]> Co-authored-by: zhongyunde 00443407 <[email protected]> Co-authored-by: Christudasan Devadasan <[email protected]> Co-authored-by: bjacob <[email protected]> Co-authored-by: Weining Lu <[email protected]> Co-authored-by: Andrzej Warzyński <[email protected]> Co-authored-by: Jay Foad <[email protected]> Co-authored-by: Markus Mützel <[email protected]> Co-authored-by: Erik Jonsson <[email protected]> Co-authored-by: Pete Steinfeld <[email protected]> Co-authored-by: Alexey Bataev <[email protected]> Co-authored-by: Louis Dionne <[email protected]> Co-authored-by: Qizhi Hu <[email protected]>
1 parent 8b5af31 commit b40c534

File tree

21 files changed

+960
-136
lines changed

21 files changed

+960
-136
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,16 @@ New Compiler Flags
264264

265265
* ``-fopenacc`` was added as a part of the effort to support OpenACC in clang.
266266

267+
* ``-fcx-limited-range`` enables the naive mathematical formulas for complex
268+
division and multiplication with no NaN checking of results. The default is
269+
``-fno-cx-limited-range``, but this option is enabled by ``-ffast-math``.
270+
271+
* ``-fcx-fortran-rules`` enables the naive mathematical formulas for complex
272+
multiplication and enables application of Smith's algorithm for complex
273+
division. See SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8
274+
(1962). The default is ``-fno-cx-fortran-rules``.
275+
276+
267277
Deprecated Compiler Flags
268278
-------------------------
269279

@@ -1002,6 +1012,9 @@ Floating Point Support in Clang
10021012
``__builtin_exp10f128`` builtins.
10031013
- Add ``__builtin_iszero``, ``__builtin_issignaling`` and
10041014
``__builtin_issubnormal``.
1015+
- Add support for C99's ``#pragma STDC CX_LIMITED_RANGE`` feature. This
1016+
enables the naive mathematical formulas for complex multiplication and
1017+
division, which are faster but do not correctly handle overflow and infinities.
10051018

10061019
AST Matchers
10071020
------------

clang/docs/UsersManual.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1468,6 +1468,7 @@ floating point semantic models: precise (the default), strict, and fast.
14681468
With the exception of ``-ffp-contract=fast``, using any of the options
14691469
below to disable any of the individual optimizations in ``-ffast-math``
14701470
will cause ``__FAST_MATH__`` to no longer be set.
1471+
``-ffast-math`` enables ``-fcx-limited-range``.
14711472

14721473
This option implies:
14731474

@@ -1834,6 +1835,20 @@ floating point semantic models: precise (the default), strict, and fast.
18341835
* ``16`` - Forces ``_Float16`` operations to be emitted without using excess
18351836
precision arithmetic.
18361837

1838+
.. option:: -fcx-limited-range:
1839+
1840+
This option enables the naive mathematical formulas for complex division and
1841+
multiplication with no NaN checking of results. The default is
1842+
``-fno-cx-limited-range``, but this option is enabled by the ``-ffast-math``
1843+
option.
1844+
1845+
.. option:: -fcx-fortran-rules:
1846+
1847+
This option enables the naive mathematical formulas for complex
1848+
multiplication and enables application of Smith's algorithm for complex
1849+
division. See SMITH, R. L. Algorithm 116: Complex division. Commun.
1850+
ACM 5, 8 (1962). The default is ``-fno-cx-fortran-rules``.
1851+
18371852
.. _floating-point-environment:
18381853

18391854
Accessing the floating point environment

clang/include/clang/Basic/FPOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ OPTION(FPEvalMethod, LangOptions::FPEvalMethodKind, 2, AllowApproxFunc)
2828
OPTION(Float16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, FPEvalMethod)
2929
OPTION(BFloat16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, Float16ExcessPrecision)
3030
OPTION(MathErrno, bool, 1, BFloat16ExcessPrecision)
31+
OPTION(ComplexRange, LangOptions::ComplexRangeKind, 2, MathErrno)
3132
#undef OPTION

clang/include/clang/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
104104
FEATURE(swiftasynccc,
105105
PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
106106
clang::TargetInfo::CCCR_OK)
107+
FEATURE(pragma_stdc_cx_limited_range, true)
107108
// Objective-C features
108109
FEATURE(objc_arr, LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
109110
FEATURE(objc_arc, LangOpts.ObjCAutoRefCount)

clang/include/clang/Basic/LangOptions.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ BENIGN_LANGOPT(NoSignedZero , 1, 0, "Permit Floating Point optimization wit
220220
BENIGN_LANGOPT(AllowRecip , 1, 0, "Permit Floating Point reciprocal")
221221
BENIGN_LANGOPT(ApproxFunc , 1, 0, "Permit Floating Point approximation")
222222

223+
ENUM_LANGOPT(ComplexRange, ComplexRangeKind, 2, CX_Full, "Enable use of range reduction for complex arithmetics.")
224+
223225
BENIGN_LANGOPT(ObjCGCBitmapPrint , 1, 0, "printing of GC's bitmap layout for __weak/__strong ivars")
224226

225227
BENIGN_LANGOPT(AccessControl , 1, 1, "C++ access control")

clang/include/clang/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ class LangOptions : public LangOptionsBase {
392392
IncompleteOnly = 3,
393393
};
394394

395+
enum ComplexRangeKind { CX_Full, CX_Limited, CX_Fortran };
396+
395397
public:
396398
/// The used language standard.
397399
LangStandard::Kind LangStd;
@@ -741,6 +743,7 @@ class FPOptions {
741743
setAllowFEnvAccess(true);
742744
else
743745
setAllowFEnvAccess(LangOptions::FPM_Off);
746+
setComplexRange(LO.getComplexRange());
744747
}
745748

746749
bool allowFPContractWithinStatement() const {

clang/include/clang/Basic/TokenKinds.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,11 @@ PRAGMA_ANNOTATION(pragma_fenv_access_ms)
911911
// handles them.
912912
PRAGMA_ANNOTATION(pragma_fenv_round)
913913

914+
// Annotation for #pragma STDC CX_LIMITED_RANGE
915+
// The lexer produces these so that they only take effect when the parser
916+
// handles them.
917+
PRAGMA_ANNOTATION(pragma_cx_limited_range)
918+
914919
// Annotation for #pragma float_control
915920
// The lexer produces these so that they only take effect when the parser
916921
// handles them.

clang/include/clang/Driver/Options.td

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,30 @@ defm offload_uniform_block : BoolFOption<"offload-uniform-block",
10101010
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Don't assume">,
10111011
BothFlags<[], [ClangOption], " that kernels are launched with uniform block sizes (default true for CUDA/HIP and false otherwise)">>;
10121012

1013+
def fcx_limited_range : Joined<["-"], "fcx-limited-range">,
1014+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1015+
HelpText<"Basic algebraic expansions of complex arithmetic operations "
1016+
"involving are enabled.">;
1017+
1018+
def fno_cx_limited_range : Joined<["-"], "fno-cx-limited-range">,
1019+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1020+
HelpText<"Basic algebraic expansions of complex arithmetic operations "
1021+
"involving are disabled.">;
1022+
1023+
def fcx_fortran_rules : Joined<["-"], "fcx-fortran-rules">,
1024+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1025+
HelpText<"Range reduction is enabled for complex arithmetic operations.">;
1026+
1027+
def fno_cx_fortran_rules : Joined<["-"], "fno-cx-fortran-rules">,
1028+
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
1029+
HelpText<"Range reduction is disabled for complex arithmetic operations.">;
1030+
1031+
def complex_range_EQ : Joined<["-"], "complex-range=">, Group<f_Group>,
1032+
Visibility<[CC1Option]>,
1033+
Values<"full,limited,fortran">, NormalizedValuesScope<"LangOptions">,
1034+
NormalizedValues<["CX_Full", "CX_Limited", "CX_Fortran"]>,
1035+
MarshallingInfoEnum<LangOpts<"ComplexRange">, "CX_Full">;
1036+
10131037
// OpenCL-only Options
10141038
def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group<opencl_Group>,
10151039
Visibility<[ClangOption, CC1Option]>,

clang/include/clang/Parse/Parser.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ class Parser : public CodeCompletionHandler {
769769
/// #pragma STDC FENV_ROUND...
770770
void HandlePragmaFEnvRound();
771771

772+
/// Handle the annotation token produced for
773+
/// #pragma STDC CX_LIMITED_RANGE...
774+
void HandlePragmaCXLimitedRange();
775+
772776
/// Handle the annotation token produced for
773777
/// #pragma float_control
774778
void HandlePragmaFloatControl();

clang/include/clang/Sema/Sema.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11023,6 +11023,11 @@ class Sema final {
1102311023
/// \#pragma STDC FENV_ACCESS
1102411024
void ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled);
1102511025

11026+
/// ActOnPragmaCXLimitedRange - Called on well formed
11027+
/// \#pragma STDC CX_LIMITED_RANGE
11028+
void ActOnPragmaCXLimitedRange(SourceLocation Loc,
11029+
LangOptions::ComplexRangeKind Range);
11030+
1102611031
/// Called on well formed '\#pragma clang fp' that has option 'exceptions'.
1102711032
void ActOnPragmaFPExceptions(SourceLocation Loc,
1102811033
LangOptions::FPExceptionModeKind);

0 commit comments

Comments
 (0)