[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-12 Thread Zahira Ammarguellat via cfe-commits

https://github.com/zahiraam updated 
https://github.com/llvm/llvm-project/pull/81316

>From 5cf9dd1073ce6ae72aebbbd8fc4723ec48b39816 Mon Sep 17 00:00:00 2001
From: Ammarguellat 
Date: Fri, 9 Feb 2024 12:38:34 -0800
Subject: [PATCH 1/3] Full control of range reduction for complex
 multiplication and division.

---
 clang/include/clang/Basic/LangOptions.h  |  26 ++-
 clang/include/clang/Driver/Options.td|  10 +-
 clang/lib/CodeGen/CGExprComplex.cpp  |  28 ++-
 clang/lib/Driver/ToolChains/Clang.cpp|  86 +++
 clang/test/CodeGen/cx-complex-range.c| 232 +++
 clang/test/CodeGen/pragma-cx-limited-range.c |   2 +-
 clang/test/Driver/range.c| 101 +---
 7 files changed, 307 insertions(+), 178 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index c1cc5548ef10c0..5ef29909bfa502 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -414,7 +414,31 @@ class LangOptions : public LangOptionsBase {
 IncompleteOnly = 3,
   };
 
-  enum ComplexRangeKind { CX_Full, CX_Limited, CX_Fortran, CX_None };
+  /// Controls for the complex arithmetic range rules.
+  enum ComplexRangeKind {
+/// Implementation of complex division and multiplication using a call to
+/// runtime library functions (generally the case, but the BE might
+/// sometimes replace the library call if it knows enough about the
+/// potential range of the inputs). Overflow and non-finite values are
+/// handled.
+CX_Full,
+
+/// Implementation of complex division using the Smith algorithm at source
+/// precision. Overflow is handled.
+CX_Smith,
+
+/// Implementation of complex division using algebraic formulas at higher
+/// precision. Overflow is handled.
+CX_Extend,
+
+/// Implementation of complex division and multiplication using algebraic
+/// formulas at source precision. Overflow and non-finites values are not
+/// handled.
+CX_Limited,
+
+/// No range rule is enabled.
+CX_None
+  };
 
 public:
   /// The used language standard.
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 4b232b8aab722a..527ab27a3d2358 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1030,12 +1030,18 @@ def fno_cx_fortran_rules : Joined<["-"], 
"fno-cx-fortran-rules">,
   Group, Visibility<[ClangOption, CC1Option]>,
   HelpText<"Range reduction is disabled for complex arithmetic operations.">;
 
+def fcomplex_arithmetic_EQ : Joined<["-"], "fcomplex-arithmetic=">, 
Group,
+  Visibility<[ClangOption, CC1Option]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>;
+
 def complex_range_EQ : Joined<["-"], "complex-range=">, Group,
   Visibility<[CC1Option]>,
-  Values<"full,limited,fortran">, NormalizedValuesScope<"LangOptions">,
-  NormalizedValues<["CX_Full", "CX_Limited", "CX_Fortran"]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>,
   MarshallingInfoEnum, "CX_Full">;
 
+
 // OpenCL-only Options
 def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group,
   Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index 9ddf0e763f139b..f41c69190b0e5c 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -283,9 +283,23 @@ class ComplexExprEmitter
   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
 const BinOpInfo );
 
-  QualType getPromotionType(QualType Ty) {
+  QualType getPromotionType(QualType Ty, bool IsDivOpCode = false) {
 if (auto *CT = Ty->getAs()) {
   QualType ElementType = CT->getElementType();
+  if (CGF.getLangOpts().getComplexRange() ==
+  LangOptions::ComplexRangeKind::CX_Extend &&
+  IsDivOpCode) {
+if (ElementType->isFloatingType()) {
+  if (const auto *BT = dyn_cast(ElementType))
+switch (BT->getKind()) {
+case BuiltinType::Kind::Float:
+  return 
CGF.getContext().getComplexType(CGF.getContext().DoubleTy);
+default:
+  return CGF.getContext().getComplexType(
+  CGF.getContext().LongDoubleTy);
+}
+}
+  }
   if (ElementType.UseExcessPrecision(CGF.getContext()))
 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
 }
@@ -296,7 +310,9 @@ class ComplexExprEmitter
 
 #define HANDLEBINOP(OP)
\
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {
\
-QualType promotionTy = 

[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-12 Thread Zahira Ammarguellat via cfe-commits

https://github.com/zahiraam updated 
https://github.com/llvm/llvm-project/pull/81316

>From 5cf9dd1073ce6ae72aebbbd8fc4723ec48b39816 Mon Sep 17 00:00:00 2001
From: Ammarguellat 
Date: Fri, 9 Feb 2024 12:38:34 -0800
Subject: [PATCH 1/3] Full control of range reduction for complex
 multiplication and division.

---
 clang/include/clang/Basic/LangOptions.h  |  26 ++-
 clang/include/clang/Driver/Options.td|  10 +-
 clang/lib/CodeGen/CGExprComplex.cpp  |  28 ++-
 clang/lib/Driver/ToolChains/Clang.cpp|  86 +++
 clang/test/CodeGen/cx-complex-range.c| 232 +++
 clang/test/CodeGen/pragma-cx-limited-range.c |   2 +-
 clang/test/Driver/range.c| 101 +---
 7 files changed, 307 insertions(+), 178 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index c1cc5548ef10c0..5ef29909bfa502 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -414,7 +414,31 @@ class LangOptions : public LangOptionsBase {
 IncompleteOnly = 3,
   };
 
-  enum ComplexRangeKind { CX_Full, CX_Limited, CX_Fortran, CX_None };
+  /// Controls for the complex arithmetic range rules.
+  enum ComplexRangeKind {
+/// Implementation of complex division and multiplication using a call to
+/// runtime library functions (generally the case, but the BE might
+/// sometimes replace the library call if it knows enough about the
+/// potential range of the inputs). Overflow and non-finite values are
+/// handled.
+CX_Full,
+
+/// Implementation of complex division using the Smith algorithm at source
+/// precision. Overflow is handled.
+CX_Smith,
+
+/// Implementation of complex division using algebraic formulas at higher
+/// precision. Overflow is handled.
+CX_Extend,
+
+/// Implementation of complex division and multiplication using algebraic
+/// formulas at source precision. Overflow and non-finites values are not
+/// handled.
+CX_Limited,
+
+/// No range rule is enabled.
+CX_None
+  };
 
 public:
   /// The used language standard.
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 4b232b8aab722a..527ab27a3d2358 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1030,12 +1030,18 @@ def fno_cx_fortran_rules : Joined<["-"], 
"fno-cx-fortran-rules">,
   Group, Visibility<[ClangOption, CC1Option]>,
   HelpText<"Range reduction is disabled for complex arithmetic operations.">;
 
+def fcomplex_arithmetic_EQ : Joined<["-"], "fcomplex-arithmetic=">, 
Group,
+  Visibility<[ClangOption, CC1Option]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>;
+
 def complex_range_EQ : Joined<["-"], "complex-range=">, Group,
   Visibility<[CC1Option]>,
-  Values<"full,limited,fortran">, NormalizedValuesScope<"LangOptions">,
-  NormalizedValues<["CX_Full", "CX_Limited", "CX_Fortran"]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>,
   MarshallingInfoEnum, "CX_Full">;
 
+
 // OpenCL-only Options
 def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group,
   Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index 9ddf0e763f139b..f41c69190b0e5c 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -283,9 +283,23 @@ class ComplexExprEmitter
   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
 const BinOpInfo );
 
-  QualType getPromotionType(QualType Ty) {
+  QualType getPromotionType(QualType Ty, bool IsDivOpCode = false) {
 if (auto *CT = Ty->getAs()) {
   QualType ElementType = CT->getElementType();
+  if (CGF.getLangOpts().getComplexRange() ==
+  LangOptions::ComplexRangeKind::CX_Extend &&
+  IsDivOpCode) {
+if (ElementType->isFloatingType()) {
+  if (const auto *BT = dyn_cast(ElementType))
+switch (BT->getKind()) {
+case BuiltinType::Kind::Float:
+  return 
CGF.getContext().getComplexType(CGF.getContext().DoubleTy);
+default:
+  return CGF.getContext().getComplexType(
+  CGF.getContext().LongDoubleTy);
+}
+}
+  }
   if (ElementType.UseExcessPrecision(CGF.getContext()))
 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
 }
@@ -296,7 +310,9 @@ class ComplexExprEmitter
 
 #define HANDLEBINOP(OP)
\
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {
\
-QualType promotionTy = 

[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-09 Thread Zahira Ammarguellat via cfe-commits

https://github.com/zahiraam updated 
https://github.com/llvm/llvm-project/pull/81316

>From 5cf9dd1073ce6ae72aebbbd8fc4723ec48b39816 Mon Sep 17 00:00:00 2001
From: Ammarguellat 
Date: Fri, 9 Feb 2024 12:38:34 -0800
Subject: [PATCH 1/2] Full control of range reduction for complex
 multiplication and division.

---
 clang/include/clang/Basic/LangOptions.h  |  26 ++-
 clang/include/clang/Driver/Options.td|  10 +-
 clang/lib/CodeGen/CGExprComplex.cpp  |  28 ++-
 clang/lib/Driver/ToolChains/Clang.cpp|  86 +++
 clang/test/CodeGen/cx-complex-range.c| 232 +++
 clang/test/CodeGen/pragma-cx-limited-range.c |   2 +-
 clang/test/Driver/range.c| 101 +---
 7 files changed, 307 insertions(+), 178 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.h 
b/clang/include/clang/Basic/LangOptions.h
index c1cc5548ef10c0..5ef29909bfa502 100644
--- a/clang/include/clang/Basic/LangOptions.h
+++ b/clang/include/clang/Basic/LangOptions.h
@@ -414,7 +414,31 @@ class LangOptions : public LangOptionsBase {
 IncompleteOnly = 3,
   };
 
-  enum ComplexRangeKind { CX_Full, CX_Limited, CX_Fortran, CX_None };
+  /// Controls for the complex arithmetic range rules.
+  enum ComplexRangeKind {
+/// Implementation of complex division and multiplication using a call to
+/// runtime library functions (generally the case, but the BE might
+/// sometimes replace the library call if it knows enough about the
+/// potential range of the inputs). Overflow and non-finite values are
+/// handled.
+CX_Full,
+
+/// Implementation of complex division using the Smith algorithm at source
+/// precision. Overflow is handled.
+CX_Smith,
+
+/// Implementation of complex division using algebraic formulas at higher
+/// precision. Overflow is handled.
+CX_Extend,
+
+/// Implementation of complex division and multiplication using algebraic
+/// formulas at source precision. Overflow and non-finites values are not
+/// handled.
+CX_Limited,
+
+/// No range rule is enabled.
+CX_None
+  };
 
 public:
   /// The used language standard.
diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 4b232b8aab722a..527ab27a3d2358 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -1030,12 +1030,18 @@ def fno_cx_fortran_rules : Joined<["-"], 
"fno-cx-fortran-rules">,
   Group, Visibility<[ClangOption, CC1Option]>,
   HelpText<"Range reduction is disabled for complex arithmetic operations.">;
 
+def fcomplex_arithmetic_EQ : Joined<["-"], "fcomplex-arithmetic=">, 
Group,
+  Visibility<[ClangOption, CC1Option]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>;
+
 def complex_range_EQ : Joined<["-"], "complex-range=">, Group,
   Visibility<[CC1Option]>,
-  Values<"full,limited,fortran">, NormalizedValuesScope<"LangOptions">,
-  NormalizedValues<["CX_Full", "CX_Limited", "CX_Fortran"]>,
+  Values<"full,smith,extend,limited">, NormalizedValuesScope<"LangOptions">,
+  NormalizedValues<["CX_Full", "CX_Smith", "CX_Extend", "CX_Limited"]>,
   MarshallingInfoEnum, "CX_Full">;
 
+
 // OpenCL-only Options
 def cl_opt_disable : Flag<["-"], "cl-opt-disable">, Group,
   Visibility<[ClangOption, CC1Option]>,
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index 9ddf0e763f139b..f41c69190b0e5c 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -283,9 +283,23 @@ class ComplexExprEmitter
   ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName,
 const BinOpInfo );
 
-  QualType getPromotionType(QualType Ty) {
+  QualType getPromotionType(QualType Ty, bool IsDivOpCode = false) {
 if (auto *CT = Ty->getAs()) {
   QualType ElementType = CT->getElementType();
+  if (CGF.getLangOpts().getComplexRange() ==
+  LangOptions::ComplexRangeKind::CX_Extend &&
+  IsDivOpCode) {
+if (ElementType->isFloatingType()) {
+  if (const auto *BT = dyn_cast(ElementType))
+switch (BT->getKind()) {
+case BuiltinType::Kind::Float:
+  return 
CGF.getContext().getComplexType(CGF.getContext().DoubleTy);
+default:
+  return CGF.getContext().getComplexType(
+  CGF.getContext().LongDoubleTy);
+}
+}
+  }
   if (ElementType.UseExcessPrecision(CGF.getContext()))
 return CGF.getContext().getComplexType(CGF.getContext().FloatTy);
 }
@@ -296,7 +310,9 @@ class ComplexExprEmitter
 
 #define HANDLEBINOP(OP)
\
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {
\
-QualType promotionTy = 

[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-09 Thread Zahira Ammarguellat via cfe-commits

https://github.com/zahiraam edited 
https://github.com/llvm/llvm-project/pull/81316
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-09 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff e5638c5a00682243b1ee012d7dd8292aa221dff8 
5cf9dd1073ce6ae72aebbbd8fc4723ec48b39816 -- 
clang/include/clang/Basic/LangOptions.h clang/lib/CodeGen/CGExprComplex.cpp 
clang/lib/Driver/ToolChains/Clang.cpp clang/test/CodeGen/cx-complex-range.c 
clang/test/CodeGen/pragma-cx-limited-range.c clang/test/Driver/range.c
``





View the diff from clang-format here.


``diff
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp 
b/clang/lib/CodeGen/CGExprComplex.cpp
index f41c69190b..e01be6e5fd 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -315,8 +315,7 @@ public:
 (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false);
\
 ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));
\
 if (!promotionTy.isNull()) 
\
-  result = 
\
-  CGF.EmitUnPromotedValue(result, E->getType());   
\
+  result = CGF.EmitUnPromotedValue(result, E->getType());  
\
 return result; 
\
   }
 

``




https://github.com/llvm/llvm-project/pull/81316
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [CLANG] Full control of range reduction for complex multiplication and division. (PR #81316)

2024-02-09 Thread Zahira Ammarguellat via cfe-commits

https://github.com/zahiraam edited 
https://github.com/llvm/llvm-project/pull/81316
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits