Skip to content

[CLANG] Full support of complex multiplication and division. #81514

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 23 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
13fd739
[CLANG] Full support of complex multiplication and division.
zahiraam Feb 12, 2024
eb9a35c
Changed the names of the values for the option and added
zahiraam Feb 20, 2024
d3c4f78
Merge remote-tracking branch 'origin/main' into ComplexRange
zahiraam Feb 20, 2024
4aa0925
Fix LIT tests.
zahiraam Feb 21, 2024
fcd5665
Merge remote-tracking branch 'origin/main' into ComplexRange
zahiraam Feb 21, 2024
2ddba9a
Addressed only a few issue from reviewer's comment.
zahiraam Feb 22, 2024
e62c462
Fixed warnings.
zahiraam Feb 26, 2024
f635f94
Merge branch 'llvm:main' into ComplexRange
zahiraam Feb 26, 2024
1d61aa6
Fix format.
zahiraam Feb 26, 2024
148b6ce
Merge branch 'ComplexRange' of https://github.com/zahiraam/llvm-proje…
zahiraam Feb 26, 2024
5aa2711
Fix format.
zahiraam Feb 26, 2024
ba9a8da
Fixed LIT test nofpclass.c and fixed the type promotion.
zahiraam Feb 27, 2024
9098908
Addressed review comments. Fixed the warnings code in Clang.cpp
zahiraam Feb 27, 2024
52181c7
Added more tests to cx-complex-range.c
zahiraam Feb 28, 2024
a9449de
Set the default value of option to full.
zahiraam Feb 29, 2024
e20741e
Wrote a more general function to deal with next larger types
zahiraam Mar 4, 2024
0d97b9b
Addressed review comments.
zahiraam Mar 11, 2024
bc3fa4f
Fix format.
zahiraam Mar 11, 2024
ec6296f
Merge branch 'main' into ComplexRange
zahiraam Mar 12, 2024
3117dbd
Addressed some of the review comments and working on the rest.
zahiraam Mar 15, 2024
0a08598
Addressed a few more comments. The last issue still WIP is the
zahiraam Mar 15, 2024
a558d31
Added LIT tests.
zahiraam Mar 18, 2024
dec045b
Changed the type of FPHasBeenPromoted to bool.
zahiraam Mar 20, 2024
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
28 changes: 11 additions & 17 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -1057,23 +1057,17 @@ def complex_range_EQ : Joined<["-"], "complex-range=">, Group<f_Group>,
NormalizedValues<["CX_Full", "CX_Improved", "CX_Promoted", "CX_Basic"]>,
MarshallingInfoEnum<LangOpts<"ComplexRange">, "CX_Full">;

def fcx_limited_range : Flag<["-"], "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 : Flag<["-"], "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 : Flag<["-"], "fcx-fortran-rules">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Range reduction is enabled for complex arithmetic operations.">;

def fno_cx_fortran_rules : Flag<["-"], "fno-cx-fortran-rules">,
Group<f_Group>, Visibility<[ClangOption, CC1Option]>,
HelpText<"Range reduction is disabled for complex arithmetic operations.">;
defm cx_limited_range: BoolOptionWithoutMarshalling<"f", "cx-limited-range",
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Basic algebraic expansions of "
"complex arithmetic operations involving are enabled.">,
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Basic algebraic expansions "
"of complex arithmetic operations involving are disabled.">>;

defm cx_fortran_rules: BoolOptionWithoutMarshalling<"f", "cx-fortran-rules",
PosFlag<SetTrue, [], [ClangOption, CC1Option], "Range reduction is enabled for "
"complex arithmetic operations.">,
NegFlag<SetFalse, [], [ClangOption, CC1Option], "Range reduction is disabled "
"for complex arithmetic operations">>;

// OpenCL-only Options
def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group<opencl_Group>,
Expand Down
14 changes: 10 additions & 4 deletions clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ class ComplexExprEmitter
CGF.getContext().getFloatTypeSemantics(ElementType);
const llvm::fltSemantics &HigherElementTypeSemantics =
CGF.getContext().getFloatTypeSemantics(HigherElementType);
// Check that LongDouble Size > Double Size.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd word this as "Check that the promoted type can handle the intermediate values without overflowing."

// This can be interpreted as:
// SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal <=
// LargerType.LargestFiniteVal.
Copy link
Contributor

Choose a reason for hiding this comment

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

Not entirely accurate, it's (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal) * 2

// In terms of exponent it gives this formula:
// (SmallerType.LargestFiniteVal * SmallerType.LargestFiniteVal
// doubles the exponent of SmallerType.LargestFiniteVal);
if (llvm::APFloat::semanticsMaxExponent(ElementTypeSemantics) * 2 + 1 <=
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd appreciate a comment here explaining why this is the correct check, so that future people reading this code can understand why this is being doing.

llvm::APFloat::semanticsMaxExponent(HigherElementTypeSemantics)) {
return CGF.getContext().getComplexType(HigherElementType);
Expand Down Expand Up @@ -1036,10 +1043,9 @@ ComplexPairTy ComplexExprEmitter::EmitBinDiv(const BinOpInfo &Op) {
else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Basic ||
Op.FPFeatures.getComplexRange() == LangOptions::CX_Promoted)
return EmitAlgebraicDiv(LHSr, LHSi, RHSr, RHSi);
else if (!CGF.getLangOpts().FastMath ||
// '-ffast-math' is used in the command line but followed by an
// '-fno-cx-limited-range' or '-fcomplex-arithmetic=full'.
Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) {
// '-ffast-math' is used in the command line but followed by an
// '-fno-cx-limited-range' or '-fcomplex-arithmetic=full'.
else if (Op.FPFeatures.getComplexRange() == LangOptions::CX_Full) {
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
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2794,8 +2794,10 @@ static void RenderFloatingPointOptions(const ToolChain &TC, const Driver &D,
// ffast-math enables basic range rules for complex multiplication and
Copy link
Contributor

Choose a reason for hiding this comment

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

We may have a naming problem here. The term "limited range rules" has a direct definition in the C standard, whereas "basic range rules" is essentially our construct to get the same behavior.

// division.
// Warn if user expects to perform full implementation of complex
// multiplication or division in the presence of nnan or ninf flags.
if (Range == LangOptions::ComplexRangeKind::CX_Full)
// multiplication or division in the presence of nan or ninf flags.
if (Range == LangOptions::ComplexRangeKind::CX_Full ||
Range == LangOptions::ComplexRangeKind::CX_Improved ||
Range == LangOptions::ComplexRangeKind::CX_Promoted)
EmitComplexRangeDiag(
D, ComplexArithmeticStr(Range),
!GccRangeComplexOption.empty()
Expand Down