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

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Oct 25, 2023

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)).

This reverts commit a3a7d63.

When compiling with MSVC2022 in  C++32 mode this is giving an
error.
Compiling this simple test case:
t1.cpp:
with -std=c++23 will give the following error:

In file included from C:\Users\zahiraam\t1.cpp:1:
c:\Program files\Microsoft Visual
Studio\2022\Professional\VC\Tools\MSVC\14.35.32215\include\vector:3329:16:
error:
      compile with '-ffixed-point' to enable fixed point types
       3329 |         _Vbase _Accum = 0;
             |                ^
c:\Program files\Microsoft Visual
Studio\2022\Professional\VC\Tools\MSVC\14.35.32215\include\vector:3329:23:
error:
      expected unqualified-id
       3329 |         _Vbase _Accum = 0;
             |                       ^
Please full error in
llvm#67750 (comment)
@github-actions
Copy link

github-actions bot commented Oct 25, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

zahiraam and others added 17 commits October 25, 2023 12:28
Summary:
This should be `kind` and not `arch`.
This PR fixes the incorrect `mov` instruction in PTX. We actually move a
predicate here, not u32, so the correct instruction should be
`mov.pred`.
If gpu.alloc has no asyn deependency ( in case if gpu.alloc has
hostShared allocation), create a new stream & synchronize. This PR is
follow up to llvm#66401
When a target sets LLVM_ENABLE_RUNTIMES, we should only generate proxy
targets for those runtimes rather than using the global list which may
contain runtimes that are not supported by that particular target.
…70229)

Summary:
This patch simply adds the `-fconvergent-functions` flag to the GPU
compilation. This is in relation to the behaviour of SIMT
architectures under divergence. With the flag, we assume every function
is convergent by default and rely on the compiler's divergence analysis
to transform it if possible.

Fixes: llvm#63853
…tion (llvm#70228)

Summary:
While this is technically a no-op for AMDGPU hardware, in cases where
the user would see fit to add an explicit wavefront sync on Nvidia
hardware, we should also inform the LLVM optimizer that this control
flow is convergent so we do not reorder blocks.
This includes support for using GPRs, FPRs, and stack.
…et (llvm#69399)

This is pre-cursor patch to enabling type units with DWARF5 acceleration
tables.
With this change it allows for entries to contain offsets directly, this
way type
units do not need to be preserved until .debug_names is written out.
…with F/D extensions. (llvm#69804)

This a simple patch to get initial FP support started.
…with F/D extensions. (llvm#69805)

This includes the plumbing for ValueMapping and PartialMapping.
…#69388)

[lldb] Refactor InstrumentationRuntimeAsan and add a new plugin
InstrumentationRuntimeLibsanitizers.

This commit refactors InstrumentationRuntimeASan by pulling out reusable
code into a separate ReportRetriever class. The purpose of the
refactoring is to allow reuse of the ReportRetriever class in another
plugin.

The commit also adds InstrumentationRuntimeASanLibsanitizers, a new runtime
plugin for ASan. The plugin provides the same
functionality as InstrumentationRuntimeASan, but provides a different
set of symbols/library names to search for while activating the plugin.

rdar://112491689
Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR summary seems to say that -ffast-math enables -fcx-fortran-rules, but the GCC documentations says that it enables -fcx-limited-range. Also, where is that implemented? Should the pragma override it?

return Call;
}

// EmitRangeReductionDiv - Implements the Smith's algorithm.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// EmitRangeReductionDiv - Implements the Smith's algorithm.
// EmitRangeReductionDiv - Implements Smith's algorithm for complex division.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -220,6 +220,10 @@ 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.")
LANGOPT(CxLimitedRange, 1, 0, "Enable use of algebraic expansions of complex arithmetics.")
LANGOPT(CxFortranRules, 1, 0, "Enable use of range reduction for complex arithmetics.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these not redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thanks.


def complex_range_EQ : Joined<["-"], "complex-range=">, Group<f_Group>,
Visibility<[CC1Option]>,
Values<"cx_full,cx_limited,cx_fortran">, NormalizedValuesScope<"LangOptions">,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cx_ prefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 4, 2023

"The PR summary seems to say that -ffast-math enables -fcx-fortran-rules, but the GCC documentations says that it enables -fcx-limited-range. Also, where is that implemented? Should the pragma override it?"
@rjmccall Thanks for the review.
Changed the implementation so that's it's compatible with GCC: -ffast-math implies limited range.
The pragma overrides it. Code at about line #3172 in Clang.cpp.

llvm::Value *AD = Builder.CreateFMul(LHSr, RHSi); // ad
llvm::Value *DSTi = Builder.CreateFAdd(BC, AD); // bc+ad
return ComplexPairTy(DSTr, DSTi);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do this as a check in the code below right after we emit ResR and ResI? Everything before that seems to be the same.

CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, Op.FPFeatures);
if (RHSi && !CGF.getLangOpts().FastMath) {
if (RHSi && Op.FPFeatures.getComplexRange() == LangOptions::CX_Fortran) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just hoist the !RHSi case up here? That would simplify a lot of these conditions. And if you have it early-exit, you can also have a single check for !LHSi instead of repeating it in every block.

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``, but this option is enabled by
``-ffast-math``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also talk about this in the main documentation, and not just the release notes?

@@ -872,6 +883,9 @@ Floating Point Support in Clang
``__builtin_exp10f128`` builtins.
- Add ``__builtin_iszero``, ``__builtin_issignaling`` and
``__builtin_issubnormal``.
- ``#pragma STDC CX_LIMITED_RANGE on-off-switch`` enables the naive mathematical
formulas for complex division and multiplication with no NaN checking of
results.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

- 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.

I think we should add a __has_feature check for this and document it here.

Copy link
Contributor Author

@zahiraam zahiraam Dec 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The feature would be the pragma?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Code should be able to check for whether the pragma is supported.

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 5, 2023

@rjmccall Aaron has objected to the change I made in Pragma.cpp:992 (call to DiscardUntilEndOfDirective) but I think it's correct (I have put it back)?
If we don't discard the tokens to the end of the directive, we wind up getting some additional error messages because it keeps visiting the remaining tokens in the directive and therefore generates additional errors. With this change we are getting the expected warning. Let me know what you think.
Thanks.

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the refactor in division looks a lot better. My comment about the multiplication path still stands. Otherwise, I think this is pretty close. Aaron, are your concerns addressed?

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 7, 2023

Thanks, the refactor in division looks a lot better. My comment about the multiplication path still stands. Otherwise, I think this is pretty close. Aaron, are your concerns addressed?

Sorry! I missed that.

Copy link
Contributor

@rjmccall rjmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Please give the other reviewers a day or two in case they have more feedback.

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 7, 2023

LGTM. Please give the other reviewers a day or two in case they have more feedback.

Thank you!

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@zahiraam zahiraam merged commit b40c534 into llvm:main Dec 11, 2023
@arichardson
Copy link
Member

@zahiraam I'd suggest you edit the commit message next time when you merge, all the merged commits should not be mentioned in the co-authored-by list.

@zahiraam
Copy link
Contributor Author

@zahiraam I'd suggest you edit the commit message next time when you merge, all the merged commits should not be mentioned in the co-authored-by list.

@arichardson Sorry I didn't notice that. Is there something I can do at this point?

@arichardson
Copy link
Member

@zahiraam I'd suggest you edit the commit message next time when you merge, all the merged commits should not be mentioned in the co-authored-by list.

@arichardson Sorry I didn't notice that. Is there something I can do at this point?

No it's in the repository now so effectively immutable. It's not a big deal just letting you know for future patches.

@zahiraam
Copy link
Contributor Author

@zahiraam I'd suggest you edit the commit message next time when you merge, all the merged commits should not be mentioned in the co-authored-by list.

@arichardson Sorry I didn't notice that. Is there something I can do at this point?

No it's in the repository now so effectively immutable. It's not a big deal just letting you know for future patches.

Thanks! will watch for it next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.