Skip to content

[clang] Add support for -fcx-limited-range, #pragma CX_LIMITED_RANGE and -fcx-fortran-rules. #70244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 158 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
340e377
Revert "[clang] Support fixed point types in C++ (#67750)"
zahiraam Oct 23, 2023
a9268af
Fix format.
zahiraam Oct 23, 2023
14a8ea1
Fix format.
zahiraam Oct 24, 2023
c867506
Merge branch 'main' of https://github.com/zahiraam/llvm-project
zahiraam Oct 24, 2023
18ba317
Merge branch 'llvm:main' into main
zahiraam Oct 24, 2023
27aee3a
Merge branch 'llvm:main' into main
zahiraam Oct 25, 2023
02f54eb
Merge branch 'llvm:main' into main
zahiraam Oct 25, 2023
6f636b9
Add support for -fcx-limited-range and #pragma CX_LIMTED_RANGE.
zahiraam Oct 10, 2023
ea7caab
Fix LIT test failing.
zahiraam Oct 11, 2023
8b0af10
Fixed LIT test and added fno-cx-limited-range.
zahiraam Oct 16, 2023
f363411
Fixed a few things.
zahiraam Oct 18, 2023
2aa7663
Fixed a few things.
zahiraam Oct 25, 2023
a616aae
Fix format.
zahiraam Oct 25, 2023
2898d30
Fix format again.
zahiraam Oct 25, 2023
0441590
Fix format and error (pragma_unknow.c).
zahiraam Oct 26, 2023
34c236d
Merge branch 'main' into ComplexRange
zahiraam Oct 26, 2023
c361788
[OpenMP][Obvious] Fix incorrect variant selector in test
jhuber6 Oct 25, 2023
16a418a
[mlir][nvvm] Fix mov.u32 to mov.pred (#70027)
grypp Oct 25, 2023
4482595
[MLIR] Modify lowering of gpu.alloc op to llvm (#69969)
nbpatel Oct 25, 2023
db249b3
[clang] Fix trailing whitespace in DiagnosticParseKinds.td
jrtc27 Oct 25, 2023
883fb88
[CMake] Correctly handle LLVM_ENABLE_RUNTIMES in targets (#69869)
petrhosek Oct 25, 2023
ba6f8b7
[libc] Compile the GPU functions with '-fconvergent-functions' (#70229)
jhuber6 Oct 25, 2023
ea0f7ed
[Libomptarget] Add a wavefront sync builtin for the AMDGPU implementa…
jhuber6 Oct 25, 2023
a48d12c
[RISCV][GISel] Add FP calling convention support (#69138)
topperc Oct 25, 2023
ebd7155
[LLVM[NFC] Refactor to allow debug_names entries to conatain DIE offs…
ayermolo Oct 25, 2023
631033c
[RISCV][GISel] Add legalizer support for G_FADD/G_FSUB/G_FMUL/G_FDIV …
topperc Oct 25, 2023
2f4581a
[RISCV][GISel] Add missing using LegalityPredicates.
topperc Oct 25, 2023
b6bca1a
[RISCV][GISel] Add regbank selection for G_FADD/G_FSUB/G_FMUL/G_FDIV …
topperc Oct 25, 2023
eedfcfb
[lldb] Refactor InstrumentationRuntimeAsan and add a new plugin (#69388)
usama54321 Oct 25, 2023
3089665
[RISCV] Use a switch instead of a series of if-clauses [nfc]
preames Oct 25, 2023
e25ecbc
[X86][GlobalISel] Add legalization of 64-bit G_ICMP for i686 (#69478)
e-kud Oct 25, 2023
37679cc
Revert "[RISCV] Use a switch instead of a series of if-clauses [nfc]"
preames Oct 25, 2023
3f2afd1
[RISCV][GISel] Add instruction selection for G_FADD/G_FSUB/G_FMUL/G_F…
topperc Oct 25, 2023
6156876
[RISCV] Use a switch instead of a series of if-clauses [nfc] (try 2)
preames Oct 25, 2023
176f9b0
[Driver][test] Improve mcmodel.c
MaskRay Oct 25, 2023
9e1cd6b
[mlir][sparse] add verification of absent value in sparse_tensor.unar…
aartbik Oct 25, 2023
6f3507f
[flang][NFC] Update comment to be generic
clementval Oct 25, 2023
e9ce038
[mlir][sparse] test for linalg tensor semantics (#70254)
aartbik Oct 25, 2023
28f2649
[LowerMemIntrinsics] Remove no-op ptr-to-ptr bitcasts (NFC)
JOE1994 Oct 25, 2023
7a0aeb3
[gn build] Manually port 078ae8cd
aeubanks Oct 25, 2023
3c97be0
[clang][deps] Fix `__has_include` behavior with umbrella headers (#70…
jansvoboda11 Oct 25, 2023
ba3c721
[LLDB][NFC] Remove DWARFASTParserClang as friend from SymbolFileDWARF…
walter-erquinigo Oct 25, 2023
b303e8b
Improve CI output. (#70236)
EricWF Oct 25, 2023
697f9a6
[OpenMP 5.2] Deprecate old syntax of linear clause (#70152)
mdfazlay Oct 25, 2023
8223886
[AMDGPU] Fix gcc -Wparentheses warning. NFC (#70239)
topperc Oct 25, 2023
d009ddb
[lldb] Part 1 of 2 - Refactor `CommandObject::Execute(...)` return `v…
PortalPete Oct 25, 2023
3689d0d
[lldb] Add test dependency on the `runtimes` instead of the `cxx` target
JDevlieghere Oct 25, 2023
f40b66c
Fix log format strings
adrian-prantl Oct 25, 2023
00784f9
[llvm] Remove no-op ptr-to-ptr bitcasts (NFC)
JOE1994 Oct 25, 2023
5aef4db
[clang-format] Fix a JavaScript import order bug
owenca Oct 25, 2023
c099ac3
[RISCV] Add an experimental pseudoinstruction to represent a remateri…
topperc Oct 26, 2023
6cf7036
[gn build] Port 109aa586f073
llvmgnsyncbot Oct 26, 2023
f9589c2
[lldb] Fix assertions caused by un-checked errors in ScriptedProcess
medismailben Oct 26, 2023
5d3d0cb
[clang-tidy]fix misc-unused-using-decls false positive false for usin…
HerrCai0907 Oct 26, 2023
809a104
[mlir][doc] Slightly clarify bufferization documentation (#70212)
rikhuijzer Oct 26, 2023
463d790
[TableGen][NFC] Format CompressInstEmitter (#68564)
wangpc-pp Oct 26, 2023
5ee5af8
[OpenMP] [OMPD] Fix CMake install command
yuanfang-chen Oct 26, 2023
3818cc5
[Driver] Use StringSet::contains (NFC)
kazutakahirata Oct 26, 2023
d2b6e42
[mlir][doc] Improve Destination-passing-style documentation (#70283)
joker-eph Oct 26, 2023
318e553
[mlir][sparse] avoid excessive macro magic (#70276)
aartbik Oct 26, 2023
4586059
[Driver] Remove some misused NoXarchOption
MaskRay Oct 26, 2023
2fea779
[Github] Add lld to docs CI (#69821)
boomanaiden154 Oct 26, 2023
ed19359
[AMDGPU] Lower __builtin_amdgcn_read_exec_hi to use amdgcn_ballot (#6…
ranapratap55 Oct 26, 2023
7a3be7b
[RISCV] Separate addend from FMA operands to support cascade FMA. NFC…
dtcxzyw Oct 26, 2023
bf8922b
[clang-tidy] Support functional cast in bugprone-dangling-handle (#69…
PiotrZSL Oct 26, 2023
d7228ec
[clang-tidy] Improved cppcoreguidelines-pro-type-const-cast (#69501)
PiotrZSL Oct 26, 2023
29b407c
[clang-tidy] Improved cppcoreguidelines-narrowing-conversions.IgnoreC…
PiotrZSL Oct 26, 2023
33440fe
Apply clang-tidy fixes for misc-include-cleaner in MLIR examples
joker-eph Oct 20, 2023
9a41775
[clang-tidy][NFC]refactor PreferMemberInitializerCheck for readability
HerrCai0907 Oct 26, 2023
7415974
[AArch64] Prevent argument promotion of vector with size > 128 bits (…
kawashima-fj Oct 26, 2023
f4a4d65
[Support] Better error msg when cache dir can't be created. (#69575)
tru Oct 26, 2023
c2ce8b3
[RISCV] Add tests for vmadd for VP intrinsics. NFC (#70042)
lukel97 Oct 26, 2023
9f339f8
[RecursiveASTVisitor] Fix RecursiveASTVisitor (RAV) fails to visit th…
xgupta Oct 26, 2023
f732ac0
[RISCV] Support predefined macro __riscv_misaligned_[fast,avoid]. (#6…
yetingk Oct 26, 2023
ed339ac
[gn build] Port 897cc8a7d7c0
llvmgnsyncbot Oct 26, 2023
47a14ee
[AMDGPU] Shrink to SOPK with 32-bit signed literals (#70263)
rampitec Oct 26, 2023
cd3d43e
[lldb][lldb-server] Enable sending RegisterFlags as XML (#69951)
DavidSpickett Oct 26, 2023
a1731fd
[AArch64][SVE2] Use rshrnb for masked stores (#70026)
MDevereau Oct 26, 2023
f5c770d
[clang] [Gnu] Improve GCCVersion parsing to match versions such as "1…
mstorsjo Oct 26, 2023
631b1ed
[PowerPC] Fix use of FPSCR builtins in smmintrin.h (#67299)
ecnelises Oct 26, 2023
055d75c
[AMDGPU] Use `S_CSELECT` for uniform i1 ext (#69703)
Pierre-vh Oct 26, 2023
ca6cb59
[LLD] [COFF] Recognize Itanium vtables for ICF (#70196)
mstorsjo Oct 26, 2023
8151f76
[test][AggressiveInstCombine] Precommit testcase for #69925
mikaelholmen Oct 25, 2023
f28c00e
[AggressiveInstCombine] Ignore debug instructions when load combining…
mikaelholmen Oct 25, 2023
0acb3c1
[mlir][cuda] Avoid driver call to check max shared memory (#70021)
grypp Oct 26, 2023
1a814ac
[mlir] Fixed typo in type (128x64 -> 64x128) in TMA load test (#70022)
grypp Oct 26, 2023
e1fb6c4
NFC. Move out and expose affine expression simplification utility out…
bondhugula Oct 26, 2023
ad48b00
[IR] Require index width to be ule pointer width (#70015)
nikic Oct 26, 2023
b153735
[Bazel] Fixes for ec6da06.
jreiffers Oct 26, 2023
351f361
[scev-aa] Make TypeSize -> uint64_t conversion explicit
d0k Oct 26, 2023
a3a52cf
Revert "[AArch64] Move SLS later in pass pipeline"
ostannard Oct 26, 2023
6a669c0
[InstCombine] Add additional demanded bits tests for shifts (NFC)
nikic Oct 26, 2023
a42ed20
[libc] Add --sweep-min-size flag for benchmarks (#70302)
dvyukov Oct 26, 2023
39bc9e6
[mlir] Remove `printCString()` from RunnerUtils (#70197)
MacDue Oct 26, 2023
422ac5e
[AMDGPU] Rematerialize scalar loads (#68778)
piotrAMD Oct 26, 2023
19827d3
[DAG] Update test case for Issue #69965
RKSimon Oct 26, 2023
dc8742a
[DAG] SimplifyDemandedBits - ensure we drop NSW/NUW flags when we sim…
RKSimon Oct 26, 2023
319c703
[clang][Interp][NFC] Make InlineDescriptor::Desc const
tbaederr Oct 26, 2023
be812a2
[clang][NFC] Move a variable into the closest scope
tbaederr Oct 26, 2023
549ce8d
[clang][Interp][NFC] Make Block::Desc const
tbaederr Oct 26, 2023
2385c41
[Inline Spiller] Pre-commit test
piotrAMD Oct 26, 2023
f25f5fa
[Inline Spiller] Consider bundles when marking defs as dead
piotrAMD Oct 26, 2023
63ae6c4
[OpenMP] Patch for Support to loop bind clause : Checking Parent Region
SunilKuravinakop Oct 26, 2023
a818ade
[SimplifyCFG] Precommit tests for PR65835
vfdff Oct 21, 2023
9bb5863
[SimplifyCFG] Delete the unnecessary range check for small mask opera…
vfdff Sep 2, 2023
fc9aeed
[InstCombine] Drop exact flag instead of increasing demanded bits (#7…
nikic Oct 26, 2023
adb8729
[clang][Interp][NFC] Make another Descriptor param const
tbaederr Oct 26, 2023
dfc72a4
[mlir][ArmSVE] Add `-arm-sve-legalize-vector-storage` pass (#68794)
MacDue Oct 26, 2023
ea99198
Revert "[AMDGPU] Cleanup hasUnwantedEffectsWhenEXECEmpty function (#7…
cdevadas Oct 26, 2023
af4b7b0
[libc] memmove optimizations (#70043)
dvyukov Oct 26, 2023
0686b10
[Bazel] Fixes for 96e040a.
jreiffers Oct 26, 2023
9616648
Add `isBatchVecmat` utilities for `linalg.batch_vecmat` (#70284)
bjacob Oct 26, 2023
4ba08e1
[LoongArch][test] Add some ABI regression tests for empty struct. NFC
SixWeining Oct 26, 2023
ee1537f
[clang][Interp] Handle unknown-size arrays better (#68868)
tbaederr Oct 26, 2023
0b6b89c
[mlir][SVE] Add an e2e test for vectorization of linalg.matmul (#69592)
banach-space Oct 26, 2023
70ad714
Revert "[AMDGPU] Use `S_CSELECT` for uniform i1 ext (#69703)"
jayfoad Oct 26, 2023
047b6a9
[AMDGPU] precommit lit test for PR 69924.
cdevadas Oct 26, 2023
3717ebd
[flang][runtime] Avoid dependency on libc++ for `std::__libcpp_verbos…
mmuetzel Oct 26, 2023
68caa11
[InstSimplify] Remove redundant pointer icmp fold (NFCI)
nikic Oct 26, 2023
f1db4a6
Revert "[SimplifyCFG] Delete the unnecessary range check for small ma…
vfdff Oct 26, 2023
d31aa94
Let M68kMCCodeEmitter set Scratch size. (#69898)
erikfjon Oct 26, 2023
dd24faf
[VP] Check if VP ops with functional intrinsics are speculatable (#69…
lukel97 Oct 26, 2023
de654c2
[flang] Regularize TODO messages for coarray intrinsics (#70281)
psteinfeld Oct 26, 2023
d224047
Revert "[mlir][SVE] Add an e2e test for vectorization of linalg.matmu…
banach-space Oct 26, 2023
d935f01
[clang][Interp] Correctly emit destructors for multi-dimensional arra…
tbaederr Oct 26, 2023
c3b2c82
[SLP]Improve isGatherShuffledEntry by trying per-register shuffle.
alexey-bataev Jul 26, 2023
2666565
[libc++] Encode additional ODR-affecting properties in the ABI tag (#…
ldionne Oct 26, 2023
ba0939d
[clang][Interp][NFC] Fix a doc comment
tbaederr Oct 26, 2023
2631a70
[analyzer] Loop should contain CXXForRangeStmt (#70190)
jcsxky Oct 26, 2023
8855846
[clang][Interp] Add explicit dummy descriptors (#68888)
tbaederr Oct 26, 2023
cb4dc33
Merge branch 'ComplexRange' of https://github.com/zahiraam/llvm-proje…
zahiraam Oct 26, 2023
d00dac6
Added relationship between the pragma and the option.
zahiraam Nov 1, 2023
997599b
Fixed typo.
zahiraam Nov 1, 2023
fdfea86
Merge branch 'main' into ComplexRange
zahiraam Nov 2, 2023
da638f2
Fixed RN.
zahiraam Nov 2, 2023
f157a44
Merge branch 'ComplexRange' of https://github.com/zahiraam/llvm-proje…
zahiraam Nov 2, 2023
26a7156
Fixed lit test fails.
zahiraam Nov 2, 2023
2db96e5
Addressed review comments.
zahiraam Nov 6, 2023
593147f
Removed unnecessary statement.
zahiraam Nov 6, 2023
9ec514e
Added a fix for the 2 failing LIT tests.
zahiraam Nov 7, 2023
2d06971
Addressed review comments.
zahiraam Nov 8, 2023
b3aee2d
Added fno-cx-* tests and removed CX-None.
zahiraam Nov 17, 2023
0dd678a
Merge branch 'llvm:main' into main
zahiraam Nov 20, 2023
f687c9d
Merge branch 'llvm:main' into ComplexRange
zahiraam Nov 20, 2023
7d253e8
Fix format.
zahiraam Nov 20, 2023
3f1ee9e
Merge branch 'ComplexRange' of https://github.com/zahiraam/llvm-proje…
zahiraam Nov 20, 2023
b0494a6
complex-range is an cc1 option only.
zahiraam Nov 29, 2023
8b73deb
Addressed review comments.
zahiraam Nov 30, 2023
e1ac710
Changed the code so that the -ffast-math implies limited range
zahiraam Dec 4, 2023
9732355
Fixed LIT test fails.
zahiraam Dec 4, 2023
1cba9db
Merge remote-tracking branch 'origin/main' into ComplexRange
zahiraam Dec 4, 2023
05dd05a
Simplified EmitBinDiv and added documentation.
zahiraam Dec 5, 2023
6cb40f4
Added suggestion from reviewer in the RN.
zahiraam Dec 5, 2023
795d005
Fixed the pragma_unknown.c LIT test.
zahiraam Dec 5, 2023
6381438
Adding feature.
zahiraam Dec 5, 2023
81f7c9d
Removed DiscardUntilEndOfDirective() from LexOnOffSwitch.
zahiraam Dec 6, 2023
8a718e8
Fixed pragma_unknown.c.
zahiraam Dec 7, 2023
e34b37d
Addressed missed review comments.
zahiraam Dec 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,16 @@ New Compiler Flags

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

* ``-fcx-limited-range`` enables the naive mathematical formulas for complex
division and multiplication with no NaN checking of results. The default is
``-fno-cx-limited-range``, but this option is enabled by ``-ffast-math``.

* ``-fcx-fortran-rules`` enables the naive mathematical formulas for complex
multiplication and enables application of Smith's algorithm for complex
division. See SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8
(1962). The default is ``-fno-cx-fortran-rules``.


Deprecated Compiler Flags
-------------------------

Expand Down Expand Up @@ -872,6 +882,9 @@ Floating Point Support in Clang
``__builtin_exp10f128`` builtins.
- Add ``__builtin_iszero``, ``__builtin_issignaling`` and
``__builtin_issubnormal``.
- Add support for C99's ``#pragma STDC CX_LIMITED_RANGE`` feature. This
enables the naive mathematical formulas for complex multiplication and
division, which are faster but do not correctly handle overflow and infinities.

AST Matchers
------------
Expand Down
15 changes: 15 additions & 0 deletions clang/docs/UsersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,7 @@ floating point semantic models: precise (the default), strict, and fast.
With the exception of ``-ffp-contract=fast``, using any of the options
below to disable any of the individual optimizations in ``-ffast-math``
will cause ``__FAST_MATH__`` to no longer be set.
``-ffast-math`` enables ``-fcx-limited-range``.

This option implies:

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

.. option:: -fcx-limited-range:

This option enables the naive mathematical formulas for complex division and
multiplication with no NaN checking of results. The default is
``-fno-cx-limited-range``, but this option is enabled by the ``-ffast-math``
option.

.. option:: -fcx-fortran-rules:

This option enables the naive mathematical formulas for complex
multiplication and enables application of Smith's algorithm for complex
division. See SMITH, R. L. Algorithm 116: Complex division. Commun.
ACM 5, 8 (1962). The default is ``-fno-cx-fortran-rules``.

.. _floating-point-environment:

Accessing the floating point environment
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/FPOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ OPTION(FPEvalMethod, LangOptions::FPEvalMethodKind, 2, AllowApproxFunc)
OPTION(Float16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, FPEvalMethod)
OPTION(BFloat16ExcessPrecision, LangOptions::ExcessPrecisionKind, 2, Float16ExcessPrecision)
OPTION(MathErrno, bool, 1, BFloat16ExcessPrecision)
OPTION(ComplexRange, LangOptions::ComplexRangeKind, 2, MathErrno)
#undef OPTION
1 change: 1 addition & 0 deletions clang/include/clang/Basic/Features.def
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ FEATURE(scudo, LangOpts.Sanitize.hasOneOf(SanitizerKind::Scudo))
FEATURE(swiftasynccc,
PP.getTargetInfo().checkCallingConvention(CC_SwiftAsync) ==
clang::TargetInfo::CCCR_OK)
FEATURE(pragma_stdc_cx_limited_range, true)
// Objective-C features
FEATURE(objc_arr, LangOpts.ObjCAutoRefCount) // FIXME: REMOVE?
FEATURE(objc_arc, LangOpts.ObjCAutoRefCount)
Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ BENIGN_LANGOPT(NoSignedZero , 1, 0, "Permit Floating Point optimization wit
BENIGN_LANGOPT(AllowRecip , 1, 0, "Permit Floating Point reciprocal")
BENIGN_LANGOPT(ApproxFunc , 1, 0, "Permit Floating Point approximation")

ENUM_LANGOPT(ComplexRange, ComplexRangeKind, 2, CX_Full, "Enable use of range reduction for complex arithmetics.")

BENIGN_LANGOPT(ObjCGCBitmapPrint , 1, 0, "printing of GC's bitmap layout for __weak/__strong ivars")

BENIGN_LANGOPT(AccessControl , 1, 1, "C++ access control")
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ class LangOptions : public LangOptionsBase {
IncompleteOnly = 3,
};

enum ComplexRangeKind { CX_Full, CX_Limited, CX_Fortran };

public:
/// The used language standard.
LangStandard::Kind LangStd;
Expand Down Expand Up @@ -740,6 +742,7 @@ class FPOptions {
setAllowFEnvAccess(true);
else
setAllowFEnvAccess(LangOptions::FPM_Off);
setComplexRange(LO.getComplexRange());
}

bool allowFPContractWithinStatement() const {
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Basic/TokenKinds.def
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,11 @@ PRAGMA_ANNOTATION(pragma_fenv_access_ms)
// handles them.
PRAGMA_ANNOTATION(pragma_fenv_round)

// Annotation for #pragma STDC CX_LIMITED_RANGE
// The lexer produces these so that they only take effect when the parser
// handles them.
PRAGMA_ANNOTATION(pragma_cx_limited_range)

// Annotation for #pragma float_control
// The lexer produces these so that they only take effect when the parser
// handles them.
Expand Down
24 changes: 24 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,30 @@ defm offload_uniform_block : BoolFOption<"offload-uniform-block",
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Don't assume">,
BothFlags<[], [ClangOption], " that kernels are launched with uniform block sizes (default true for CUDA/HIP and false otherwise)">>;

def fcx_limited_range : Joined<["-"], "fcx-limited-range">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Basic algebraic expansions of complex arithmetic operations "
"involving are enabled.">;

def fno_cx_limited_range : Joined<["-"], "fno-cx-limited-range">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Basic algebraic expansions of complex arithmetic operations "
"involving are disabled.">;

def fcx_fortran_rules : Joined<["-"], "fcx-fortran-rules">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Range reduction is enabled for complex arithmetic operations.">;

def fno_cx_fortran_rules : Joined<["-"], "fno-cx-fortran-rules">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Range reduction is disabled for complex arithmetic operations.">;

def complex_range_EQ : Joined<["-"], "complex-range=">, Group<f_Group>,
Visibility<[CC1Option]>,
Values<"full,limited,fortran">, NormalizedValuesScope<"LangOptions">,
NormalizedValues<["CX_Full", "CX_Limited", "CX_Fortran"]>,
MarshallingInfoEnum<LangOpts<"ComplexRange">, "CX_Full">;

// OpenCL-only Options
def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group<opencl_Group>,
Visibility<[ClangOption, CC1Option]>,
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/Parse/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,10 @@ class Parser : public CodeCompletionHandler {
/// #pragma STDC FENV_ROUND...
void HandlePragmaFEnvRound();

/// Handle the annotation token produced for
/// #pragma STDC CX_LIMITED_RANGE...
void HandlePragmaCXLimitedRange();

/// Handle the annotation token produced for
/// #pragma float_control
void HandlePragmaFloatControl();
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10997,6 +10997,11 @@ class Sema final {
/// \#pragma STDC FENV_ACCESS
void ActOnPragmaFEnvAccess(SourceLocation Loc, bool IsEnabled);

/// ActOnPragmaCXLimitedRange - Called on well formed
/// \#pragma STDC CX_LIMITED_RANGE
void ActOnPragmaCXLimitedRange(SourceLocation Loc,
LangOptions::ComplexRangeKind Range);

/// Called on well formed '\#pragma clang fp' that has option 'exceptions'.
void ActOnPragmaFPExceptions(SourceLocation Loc,
LangOptions::FPExceptionModeKind);
Expand Down
166 changes: 134 additions & 32 deletions clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ class ComplexExprEmitter
ComplexPairTy EmitBinSub(const BinOpInfo &Op);
ComplexPairTy EmitBinMul(const BinOpInfo &Op);
ComplexPairTy EmitBinDiv(const BinOpInfo &Op);
ComplexPairTy EmitAlgebraicDiv(llvm::Value *A, llvm::Value *B, llvm::Value *C,
llvm::Value *D);
ComplexPairTy EmitRangeReductionDiv(llvm::Value *A, llvm::Value *B,
llvm::Value *C, llvm::Value *D);

ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
const BinOpInfo &Op);
Expand Down Expand Up @@ -781,6 +785,10 @@ ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
ResR = Builder.CreateFSub(AC, BD, "mul_r");
ResI = Builder.CreateFAdd(AD, BC, "mul_i");

if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited ||
Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran)
return ComplexPairTy(ResR, ResI);

// Emit the test for the real part becoming NaN and create a branch to
// handle it. We test for NaN by comparing the number to itself.
Value *IsRNaN = Builder.CreateFCmpUNO(ResR, ResR, "isnan_cmp");
Expand Down Expand Up @@ -846,23 +854,139 @@ ComplexPairTy ComplexExprEmitter::EmitBinMul(const BinOpInfo &Op) {
return ComplexPairTy(ResR, ResI);
}

ComplexPairTy ComplexExprEmitter::EmitAlgebraicDiv(llvm::Value *LHSr,
llvm::Value *LHSi,
llvm::Value *RHSr,
llvm::Value *RHSi) {
// (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
llvm::Value *DSTr, *DSTi;

llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd

llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd

llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad

DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
return ComplexPairTy(DSTr, DSTi);
}

// EmitFAbs - Emit a call to @llvm.fabs.
static llvm::Value *EmitllvmFAbs(CodeGenFunction &CGF, llvm::Value *Value) {
llvm::Function *Func =
CGF.CGM.getIntrinsic(llvm::Intrinsic::fabs, Value->getType());
llvm::Value *Call = CGF.Builder.CreateCall(Func, Value);
return Call;
}

// EmitRangeReductionDiv - Implements Smith's algorithm for complex division.
// SMITH, R. L. Algorithm 116: Complex division. Commun. ACM 5, 8 (1962).
ComplexPairTy ComplexExprEmitter::EmitRangeReductionDiv(llvm::Value *LHSr,
llvm::Value *LHSi,
llvm::Value *RHSr,
llvm::Value *RHSi) {
// (a + ib) / (c + id) = (e + if)
llvm::Value *FAbsRHSr = EmitllvmFAbs(CGF, RHSr); // |c|
llvm::Value *FAbsRHSi = EmitllvmFAbs(CGF, RHSi); // |d|
// |c| >= |d|
llvm::Value *IsR = Builder.CreateFCmpUGT(FAbsRHSr, FAbsRHSi, "abs_cmp");

llvm::BasicBlock *TrueBB =
CGF.createBasicBlock("abs_rhsr_greater_or_equal_abs_rhsi");
llvm::BasicBlock *FalseBB =
CGF.createBasicBlock("abs_rhsr_less_than_abs_rhsi");
llvm::BasicBlock *ContBB = CGF.createBasicBlock("complex_div");
Builder.CreateCondBr(IsR, TrueBB, FalseBB);

CGF.EmitBlock(TrueBB);
// abs(c) >= abs(d)
// r = d/c
// tmp = c + rd
// e = (a + br)/tmp
// f = (b - ar)/tmp
llvm::Value *DdC = Builder.CreateFDiv(RHSi, RHSr); // r=d/c

llvm::Value *RD = Builder.CreateFMul(DdC, RHSi); // rd
llvm::Value *CpRD = Builder.CreateFAdd(RHSr, RD); // tmp=c+rd

llvm::Value *T3 = Builder.CreateFMul(LHSi, DdC); // br
llvm::Value *T4 = Builder.CreateFAdd(LHSr, T3); // a+br
llvm::Value *DSTTr = Builder.CreateFDiv(T4, CpRD); // (a+br)/tmp

llvm::Value *T5 = Builder.CreateFMul(LHSr, DdC); // ar
llvm::Value *T6 = Builder.CreateFSub(LHSi, T5); // b-ar
llvm::Value *DSTTi = Builder.CreateFDiv(T6, CpRD); // (b-ar)/tmp
Builder.CreateBr(ContBB);

CGF.EmitBlock(FalseBB);
// abs(c) < abs(d)
// r = c/d
// tmp = d + rc
// e = (ar + b)/tmp
// f = (br - a)/tmp
llvm::Value *CdD = Builder.CreateFDiv(RHSr, RHSi); // r=c/d

llvm::Value *RC = Builder.CreateFMul(CdD, RHSr); // rc
llvm::Value *DpRC = Builder.CreateFAdd(RHSi, RC); // tmp=d+rc

llvm::Value *T7 = Builder.CreateFMul(LHSr, RC); // ar
llvm::Value *T8 = Builder.CreateFAdd(T7, LHSi); // ar+b
llvm::Value *DSTFr = Builder.CreateFDiv(T8, DpRC); // (ar+b)/tmp

llvm::Value *T9 = Builder.CreateFMul(LHSi, CdD); // br
llvm::Value *T10 = Builder.CreateFSub(T9, LHSr); // br-a
llvm::Value *DSTFi = Builder.CreateFDiv(T10, DpRC); // (br-a)/tmp
Builder.CreateBr(ContBB);

// Phi together the computation paths.
CGF.EmitBlock(ContBB);
llvm::PHINode *VALr = Builder.CreatePHI(DSTTr->getType(), 2);
VALr->addIncoming(DSTTr, TrueBB);
VALr->addIncoming(DSTFr, FalseBB);
llvm::PHINode *VALi = Builder.CreatePHI(DSTTi->getType(), 2);
VALi->addIncoming(DSTTi, TrueBB);
VALi->addIncoming(DSTFi, FalseBB);
return ComplexPairTy(VALr, VALi);
}

// See C11 Annex G.5.1 for the semantics of multiplicative operators on complex
// typed values.
ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
llvm::Value *LHSr = Op.LHS.first, *LHSi = Op.LHS.second;
llvm::Value *RHSr = Op.RHS.first, *RHSi = Op.RHS.second;

llvm::Value *DSTr, *DSTi;
if (LHSr->getType()->isFloatingPointTy()) {
// If we have a complex operand on the RHS and FastMath is not allowed, we
// delegate to a libcall to handle all of the complexities and minimize
// underflow/overflow cases. When FastMath is allowed we construct the
// divide inline using the same algorithm as for integer operands.
//
// FIXME: We would be able to avoid the libcall in many places if we
// supported imaginary types in addition to complex types.
CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
if (RHSi && !CGF.getLangOpts().FastMath) {
if (!RHSi) {
assert(LHSi && "Can have at most one non-complex operand!");

DSTr = Builder.CreateFDiv(LHSr, RHSr);
DSTi = Builder.CreateFDiv(LHSi, RHSr);
return ComplexPairTy(DSTr, DSTi);
}
llvm::Value *OrigLHSi = LHSi;
if (!LHSi)
LHSi = llvm::Constant::getNullValue(RHSi->getType());
if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran)
return EmitRangeReductionDiv(LHSr, LHSi, RHSr, RHSi);
else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Limited)
return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
else if (!CGF.getLangOpts().FastMath) {
LHSi = OrigLHSi;
// If we have a complex operand on the RHS and FastMath is not allowed, we
// delegate to a libcall to handle all of the complexities and minimize
// underflow/overflow cases. When FastMath is allowed we construct the
// divide inline using the same algorithm as for integer operands.
//
// FIXME: We would be able to avoid the libcall in many places if we
// supported imaginary types in addition to complex types.
BinOpInfo LibCallOp = Op;
// If LHS was a real, supply a null imaginary part.
if (!LHSi)
Expand All @@ -884,30 +1008,8 @@ ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
case llvm::Type::FP128TyID:
return EmitComplexBinOpLibCall("__divtc3", LibCallOp);
}
} else if (RHSi) {
if (!LHSi)
LHSi = llvm::Constant::getNullValue(RHSi->getType());

// (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
llvm::Value *AC = Builder.CreateFMul(LHSr, RHSr); // a*c
llvm::Value *BD = Builder.CreateFMul(LHSi, RHSi); // b*d
llvm::Value *ACpBD = Builder.CreateFAdd(AC, BD); // ac+bd

llvm::Value *CC = Builder.CreateFMul(RHSr, RHSr); // c*c
llvm::Value *DD = Builder.CreateFMul(RHSi, RHSi); // d*d
llvm::Value *CCpDD = Builder.CreateFAdd(CC, DD); // cc+dd

llvm::Value *BC = Builder.CreateFMul(LHSi, RHSr); // b*c
llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // a*d
llvm::Value *BCmAD = Builder.CreateFSub(BC, AD); // bc-ad

DSTr = Builder.CreateFDiv(ACpBD, CCpDD);
DSTi = Builder.CreateFDiv(BCmAD, CCpDD);
} else {
assert(LHSi && "Can have at most one non-complex operand!");

DSTr = Builder.CreateFDiv(LHSr, RHSr);
DSTi = Builder.CreateFDiv(LHSi, RHSr);
return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
}
} else {
assert(Op.LHS.second && Op.RHS.second &&
Expand Down
Loading