[clang] The pragma STDC CX_LIMITED_RANGE ON should have precedence. (PR #98520)

Zahira Ammarguellat via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 16 11:32:42 PDT 2024


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

>From da863999000a6b12f2930247de9df3daf0e5a608 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 11 Jul 2024 11:54:13 -0700
Subject: [PATCH 1/8] The pragma STDC CX_LIMITED_RANGE ON should have
 precedence over command line -fcomplex-arithmetic.

---
 clang/lib/CodeGen/CGExprComplex.cpp          | 35 ++++++---
 clang/test/CodeGen/pragma-cx-limited-range.c | 75 ++++++++++----------
 2 files changed, 64 insertions(+), 46 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 84ad3b566b647..44e04b323db6b 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -328,12 +328,14 @@ class ComplexExprEmitter
     }
   }
 
-  QualType getPromotionType(QualType Ty, bool IsDivOpCode = false) {
+  QualType getPromotionType(FPOptionsOverride Features, QualType Ty,
+                            bool IsDivOpCode = false) {
     if (auto *CT = Ty->getAs<ComplexType>()) {
       QualType ElementType = CT->getElementType();
       if (IsDivOpCode && ElementType->isFloatingType() &&
           CGF.getLangOpts().getComplexRange() ==
-              LangOptions::ComplexRangeKind::CX_Promoted)
+              LangOptions::ComplexRangeKind::CX_Promoted &&
+          !Features.hasComplexRangeOverride())
         return HigherPrecisionTypeForComplexArithmetic(ElementType,
                                                        IsDivOpCode);
       if (ElementType.UseExcessPrecision(CGF.getContext()))
@@ -347,7 +349,9 @@ class ComplexExprEmitter
 #define HANDLEBINOP(OP)                                                        \
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {                        \
     QualType promotionTy = getPromotionType(                                   \
-        E->getType(),                                                          \
+        (E->hasStoredFPFeatures() ? E->getStoredFPFeatures()                   \
+                                  : FPOptionsOverride()),                      \
+          E->getType(),                                                        \
         (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false);        \
     ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));            \
     if (!promotionTy.isNull())                                                 \
@@ -642,7 +646,10 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
 ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
                                                  QualType PromotionType) {
   QualType promotionTy = PromotionType.isNull()
-                             ? getPromotionType(E->getSubExpr()->getType())
+                             ? getPromotionType((E->hasStoredFPFeatures()
+                                                     ? E->getStoredFPFeatures()
+                                                     : FPOptionsOverride()),
+                                                E->getSubExpr()->getType())
                              : PromotionType;
   ComplexPairTy result = VisitPlus(E, promotionTy);
   if (!promotionTy.isNull())
@@ -662,7 +669,10 @@ ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
                                                   QualType PromotionType) {
   QualType promotionTy = PromotionType.isNull()
-                             ? getPromotionType(E->getSubExpr()->getType())
+                             ? getPromotionType((E->hasStoredFPFeatures()
+                                                     ? E->getStoredFPFeatures()
+                                                     : FPOptionsOverride()),
+                                                E->getSubExpr()->getType())
                              : PromotionType;
   ComplexPairTy result = VisitMinus(E, promotionTy);
   if (!promotionTy.isNull())
@@ -1223,13 +1233,19 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
   // __block variables need to have the rhs evaluated first, plus this should
   // improve codegen a little.
   QualType PromotionTypeCR;
-  PromotionTypeCR = getPromotionType(E->getComputationResultType());
+  PromotionTypeCR =
+      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
+                                                 : FPOptionsOverride()),
+                       E->getComputationResultType());
   if (PromotionTypeCR.isNull())
     PromotionTypeCR = E->getComputationResultType();
   OpInfo.Ty = PromotionTypeCR;
   QualType ComplexElementTy =
       OpInfo.Ty->castAs<ComplexType>()->getElementType();
-  QualType PromotionTypeRHS = getPromotionType(E->getRHS()->getType());
+  QualType PromotionTypeRHS =
+      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
+                                                 : FPOptionsOverride()),
+                       E->getRHS()->getType());
 
   // The RHS should have been converted to the computation type.
   if (E->getRHS()->getType()->isRealFloatingType()) {
@@ -1257,7 +1273,10 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
 
   // Load from the l-value and convert it.
   SourceLocation Loc = E->getExprLoc();
-  QualType PromotionTypeLHS = getPromotionType(E->getComputationLHSType());
+  QualType PromotionTypeLHS =
+      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
+                                                 : FPOptionsOverride()),
+                       E->getComputationLHSType());
   if (LHSTy->isAnyComplexType()) {
     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
     if (!PromotionTypeLHS.isNull())
diff --git a/clang/test/CodeGen/pragma-cx-limited-range.c b/clang/test/CodeGen/pragma-cx-limited-range.c
index 68615348c1871..7732db1dc616d 100644
--- a/clang/test/CodeGen/pragma-cx-limited-range.c
+++ b/clang/test/CodeGen/pragma-cx-limited-range.c
@@ -106,21 +106,17 @@ _Complex float pragma_on_div(_Complex float a, _Complex float b) {
   // IMPRVD-NEXT: fdiv float
   // IMPRVD-NEXT: fdiv float
 
-  // PRMTD:   fpext float {{.*}} to double
-  // PRMTD:   fpext float {{.*}} to double
-  // PRMTD:   fmul double
-  // PRMTD:   fmul double
-  // PRMTD:   fadd double
-  // PRMTD:   fmul double
-  // PRMTD:   fmul double
-  // PRMTD:   fadd double
-  // PRMTD:   fmul double
-  // PRMTD:   fmul double
-  // PRMTD:   fsub double
-  // PRMTD:   fdiv double
-  // PRMTD:   fdiv double
-  // PRMTD:   fptrunc double
-  // PRMTD:   fptrunc double
+  // PRMTD: fmul float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fsub float
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: fdiv float
 
   return a / b;
 }
@@ -135,7 +131,7 @@ _Complex float pragma_off_div(_Complex float a, _Complex float b) {
 
   // IMPRVD: call {{.*}} @__divsc3
 
-  // PRMTD: call {{.*}} @__divdc3
+  // PRMTD: call {{.*}} @__divsc3
 
   return a / b;
 }
@@ -218,28 +214,31 @@ _Complex float pragma_default_div(_Complex float a, _Complex float b) {
   // IMPRVD-NEXT: fsub float
   // IMPRVD-NEXT: fdiv float
 
-  // PRMTD: load float, ptr {{.*}}
-  // PRMTD: fpext float {{.*}} to double
-  // PRMTD-NEXT: fpext float {{.*}} to double
-  // PRMTD-NEXT: getelementptr inbounds { float, float }, ptr {{.*}}, i32 0, i32 0
-  // PRMTD-NEXT: load float, ptr {{.*}}
-  // PRMTD-NEXT: getelementptr inbounds { float, float }, ptr {{.*}}, i32 0, i32 1
-  // PRMTD-NEXT: load float, ptr {{.*}}
-  // PRMTD-NEXT: fpext float {{.*}} to double
-  // PRMTD-NEXT: fpext float {{.*}} to double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fadd double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fadd double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fmul double
-  // PRMTD-NEXT: fsub double
-  // PRMTD-NEXT: fdiv double
-  // PRMTD-NEXT: fdiv double
-  // PRMTD-NEXT: fptrunc double {{.*}} to float
-  // PRMTD-NEXT: fptrunc double {{.*}} to float
+  // PRMTD: call{{.*}}float @llvm.fabs.f32(float {{.*}})
+  // PRMTD-NEXT: call{{.*}}float @llvm.fabs.f32(float {{.*}})
+  // PRMTD-NEXT: fcmp{{.*}}ugt float {{.*}}, {{.*}}
+  // PRMTD-NEXT:   br i1 {{.*}}, label
+  // PRMTD:  abs_rhsr_greater_or_equal_abs_rhsi:
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fsub float
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: br label
+  // PRMTD: abs_rhsr_less_than_abs_rhsi:
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fadd float
+  // PRMTD-NEXT: fdiv float
+  // PRMTD-NEXT: fmul float
+  // PRMTD-NEXT: fsub float
+  // PRMTD-NEXT: fdiv float
 
   return a / b;
 }

>From 1e59c26b411747483cf4dda968501247e8ea411a Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Thu, 11 Jul 2024 12:09:06 -0700
Subject: [PATCH 2/8] Fix format.

---
 clang/lib/CodeGen/CGExprComplex.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 44e04b323db6b..9dc82afb6606e 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -351,7 +351,7 @@ class ComplexExprEmitter
     QualType promotionTy = getPromotionType(                                   \
         (E->hasStoredFPFeatures() ? E->getStoredFPFeatures()                   \
                                   : FPOptionsOverride()),                      \
-          E->getType(),                                                        \
+        E->getType(),                                                          \
         (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false);        \
     ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));            \
     if (!promotionTy.isNull())                                                 \

>From 102fa19d8062d13824f184a357e6139b2a8aa637 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Fri, 12 Jul 2024 13:16:40 -0700
Subject: [PATCH 3/8] Addressed review comments.

---
 clang/lib/CodeGen/CGExprComplex.cpp          | 15 +++++--
 clang/test/CodeGen/pragma-cx-limited-range.c | 47 +++++++++-----------
 2 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 9dc82afb6606e..de526a74b0f2d 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -332,10 +332,17 @@ class ComplexExprEmitter
                             bool IsDivOpCode = false) {
     if (auto *CT = Ty->getAs<ComplexType>()) {
       QualType ElementType = CT->getElementType();
-      if (IsDivOpCode && ElementType->isFloatingType() &&
-          CGF.getLangOpts().getComplexRange() ==
-              LangOptions::ComplexRangeKind::CX_Promoted &&
-          !Features.hasComplexRangeOverride())
+      bool isFloatingType = ElementType->isFloatingType();
+      bool isComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
+                                    LangOptions::ComplexRangeKind::CX_Promoted;
+      bool hasNoComplexRangeOverride = !Features.hasComplexRangeOverride();
+      bool hasMatchingComplexRange =
+          Features.hasComplexRangeOverride() &&
+          Features.getComplexRangeOverride() ==
+              CGF.getLangOpts().getComplexRange();
+
+      if (IsDivOpCode && isFloatingType && isComplexRangePromoted &&
+          (hasNoComplexRangeOverride || hasMatchingComplexRange))
         return HigherPrecisionTypeForComplexArithmetic(ElementType,
                                                        IsDivOpCode);
       if (ElementType.UseExcessPrecision(CGF.getContext()))
diff --git a/clang/test/CodeGen/pragma-cx-limited-range.c b/clang/test/CodeGen/pragma-cx-limited-range.c
index 7732db1dc616d..1c9bf40fd714f 100644
--- a/clang/test/CodeGen/pragma-cx-limited-range.c
+++ b/clang/test/CodeGen/pragma-cx-limited-range.c
@@ -214,31 +214,28 @@ _Complex float pragma_default_div(_Complex float a, _Complex float b) {
   // IMPRVD-NEXT: fsub float
   // IMPRVD-NEXT: fdiv float
 
-  // PRMTD: call{{.*}}float @llvm.fabs.f32(float {{.*}})
-  // PRMTD-NEXT: call{{.*}}float @llvm.fabs.f32(float {{.*}})
-  // PRMTD-NEXT: fcmp{{.*}}ugt float {{.*}}, {{.*}}
-  // PRMTD-NEXT:   br i1 {{.*}}, label
-  // PRMTD:  abs_rhsr_greater_or_equal_abs_rhsi:
-  // PRMTD-NEXT: fdiv float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fadd float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fadd float
-  // PRMTD-NEXT: fdiv float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fsub float
-  // PRMTD-NEXT: fdiv float
-  // PRMTD-NEXT: br label
-  // PRMTD: abs_rhsr_less_than_abs_rhsi:
-  // PRMTD-NEXT: fdiv float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fadd float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fadd float
-  // PRMTD-NEXT: fdiv float
-  // PRMTD-NEXT: fmul float
-  // PRMTD-NEXT: fsub float
-  // PRMTD-NEXT: fdiv float
+  // PRMTD: load float, ptr {{.*}}
+  // PRMTD: fpext float {{.*}} to double
+  // PRMTD-NEXT: fpext float {{.*}} to double
+  // PRMTD-NEXT: getelementptr inbounds { float, float }, ptr {{.*}}, i32 0, i32 0
+  // PRMTD-NEXT: load float, ptr {{.*}}
+  // PRMTD-NEXT: getelementptr inbounds { float, float }, ptr {{.*}}, i32 0, i32 1
+  // PRMTD-NEXT: load float, ptr {{.*}}
+  // PRMTD-NEXT: fpext float {{.*}} to double
+  // PRMTD-NEXT: fpext float {{.*}} to double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fadd double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fadd double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fmul double
+  // PRMTD-NEXT: fsub double
+  // PRMTD-NEXT: fdiv double
+  // PRMTD-NEXT: fdiv double
+  // PRMTD-NEXT: fptrunc double {{.*}} to float
+  // PRMTD-NEXT: fptrunc double {{.*}} to float
 
   return a / b;
 }

>From 24cb25aae2e8954574aa85a6746d84a5d80e1b90 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Fri, 12 Jul 2024 13:25:03 -0700
Subject: [PATCH 4/8] Fix format.

---
 clang/lib/CodeGen/CGExprComplex.cpp | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index de526a74b0f2d..fc3382983c5a6 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -336,10 +336,9 @@ class ComplexExprEmitter
       bool isComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
                                     LangOptions::ComplexRangeKind::CX_Promoted;
       bool hasNoComplexRangeOverride = !Features.hasComplexRangeOverride();
-      bool hasMatchingComplexRange =
-          Features.hasComplexRangeOverride() &&
-          Features.getComplexRangeOverride() ==
-              CGF.getLangOpts().getComplexRange();
+      bool hasMatchingComplexRange = Features.hasComplexRangeOverride() &&
+                                     Features.getComplexRangeOverride() ==
+                                         CGF.getLangOpts().getComplexRange();
 
       if (IsDivOpCode && isFloatingType && isComplexRangePromoted &&
           (hasNoComplexRangeOverride || hasMatchingComplexRange))

>From e75c1778660fd8536e37bbe145e856e5f6ef13ec Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Mon, 15 Jul 2024 08:51:44 -0700
Subject: [PATCH 5/8] Addressed revew comments.

---
 clang/lib/CodeGen/CGExprComplex.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index fc3382983c5a6..7877081c6d7db 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -332,16 +332,16 @@ class ComplexExprEmitter
                             bool IsDivOpCode = false) {
     if (auto *CT = Ty->getAs<ComplexType>()) {
       QualType ElementType = CT->getElementType();
-      bool isFloatingType = ElementType->isFloatingType();
-      bool isComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
+      bool IsFloatingType = ElementType->isFloatingType();
+      bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() ==
                                     LangOptions::ComplexRangeKind::CX_Promoted;
-      bool hasNoComplexRangeOverride = !Features.hasComplexRangeOverride();
-      bool hasMatchingComplexRange = Features.hasComplexRangeOverride() &&
+      bool HasNoComplexRangeOverride = !Features.hasComplexRangeOverride();
+      bool HasMatchingComplexRange = Features.hasComplexRangeOverride() &&
                                      Features.getComplexRangeOverride() ==
                                          CGF.getLangOpts().getComplexRange();
 
-      if (IsDivOpCode && isFloatingType && isComplexRangePromoted &&
-          (hasNoComplexRangeOverride || hasMatchingComplexRange))
+      if (IsDivOpCode && IsFloatingType && IsComplexRangePromoted &&
+          (HasNoComplexRangeOverride || HasMatchingComplexRange))
         return HigherPrecisionTypeForComplexArithmetic(ElementType,
                                                        IsDivOpCode);
       if (ElementType.UseExcessPrecision(CGF.getContext()))

>From 26adc4fa692f3bc76945c9c4ce7c7a242dc4ccb9 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Mon, 15 Jul 2024 10:48:19 -0700
Subject: [PATCH 6/8] Refactor code.

---
 clang/lib/CodeGen/CGExprComplex.cpp | 51 ++++++++++++++---------------
 1 file changed, 24 insertions(+), 27 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 7877081c6d7db..0f68d0d9aaa9e 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -352,12 +352,17 @@ class ComplexExprEmitter
     return QualType();
   }
 
+  template <typename T>
+  FPOptionsOverride getStoredFPFeaturesOrDefault(const T *E,
+                                                 const CodeGenFunction &CGF) {
+    return E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
+                                    : FPOptionsOverride();
+  }
+
 #define HANDLEBINOP(OP)                                                        \
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {                        \
     QualType promotionTy = getPromotionType(                                   \
-        (E->hasStoredFPFeatures() ? E->getStoredFPFeatures()                   \
-                                  : FPOptionsOverride()),                      \
-        E->getType(),                                                          \
+        getStoredFPFeaturesOrDefault(E, CGF), E->getType(),                    \
         (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false);        \
     ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));            \
     if (!promotionTy.isNull())                                                 \
@@ -651,12 +656,11 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
 
 ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
                                                  QualType PromotionType) {
-  QualType promotionTy = PromotionType.isNull()
-                             ? getPromotionType((E->hasStoredFPFeatures()
-                                                     ? E->getStoredFPFeatures()
-                                                     : FPOptionsOverride()),
-                                                E->getSubExpr()->getType())
-                             : PromotionType;
+  QualType promotionTy =
+      PromotionType.isNull()
+          ? getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+                             E->getSubExpr()->getType())
+          : PromotionType;
   ComplexPairTy result = VisitPlus(E, promotionTy);
   if (!promotionTy.isNull())
     return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
@@ -674,12 +678,11 @@ ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
 
 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
                                                   QualType PromotionType) {
-  QualType promotionTy = PromotionType.isNull()
-                             ? getPromotionType((E->hasStoredFPFeatures()
-                                                     ? E->getStoredFPFeatures()
-                                                     : FPOptionsOverride()),
-                                                E->getSubExpr()->getType())
-                             : PromotionType;
+  QualType promotionTy =
+      PromotionType.isNull()
+          ? getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+                             E->getSubExpr()->getType())
+          : PromotionType;
   ComplexPairTy result = VisitMinus(E, promotionTy);
   if (!promotionTy.isNull())
     return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
@@ -1239,19 +1242,15 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
   // __block variables need to have the rhs evaluated first, plus this should
   // improve codegen a little.
   QualType PromotionTypeCR;
-  PromotionTypeCR =
-      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
-                                                 : FPOptionsOverride()),
-                       E->getComputationResultType());
+  PromotionTypeCR = getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+                                     E->getComputationResultType());
   if (PromotionTypeCR.isNull())
     PromotionTypeCR = E->getComputationResultType();
   OpInfo.Ty = PromotionTypeCR;
   QualType ComplexElementTy =
       OpInfo.Ty->castAs<ComplexType>()->getElementType();
-  QualType PromotionTypeRHS =
-      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
-                                                 : FPOptionsOverride()),
-                       E->getRHS()->getType());
+  QualType PromotionTypeRHS = getPromotionType(
+      getStoredFPFeaturesOrDefault(E, CGF), E->getRHS()->getType());
 
   // The RHS should have been converted to the computation type.
   if (E->getRHS()->getType()->isRealFloatingType()) {
@@ -1279,10 +1278,8 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
 
   // Load from the l-value and convert it.
   SourceLocation Loc = E->getExprLoc();
-  QualType PromotionTypeLHS =
-      getPromotionType((E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
-                                                 : FPOptionsOverride()),
-                       E->getComputationLHSType());
+  QualType PromotionTypeLHS = getPromotionType(
+      getStoredFPFeaturesOrDefault(E, CGF), E->getComputationLHSType());
   if (LHSTy->isAnyComplexType()) {
     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
     if (!PromotionTypeLHS.isNull())

>From a48cda1b099ba87fd97562d4dd9aed6212214e5a Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Tue, 16 Jul 2024 11:15:28 -0700
Subject: [PATCH 7/8] Fix getStoredFOFeaturesOrDefault function.

---
 clang/include/clang/AST/Expr.h      | 19 +++++++++++++++++++
 clang/include/clang/AST/Stmt.h      |  5 +++++
 clang/lib/CodeGen/CGExprComplex.cpp | 22 ++++++++--------------
 3 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h
index a8add9d1337c6..5b813bfc2faf9 100644
--- a/clang/include/clang/AST/Expr.h
+++ b/clang/include/clang/AST/Expr.h
@@ -2333,6 +2333,11 @@ class UnaryOperator final
     return getTrailingFPFeatures();
   }
 
+  /// Get the store FPOptionsOverride or default if not stored.
+  FPOptionsOverride getStoredFPFeaturesOrDefault() const {
+    return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
+  }
+
 protected:
   /// Set FPFeatures in trailing storage, used by Serialization & ASTImporter.
   void setStoredFPFeatures(FPOptionsOverride F) { getTrailingFPFeatures() = F; }
@@ -3096,6 +3101,11 @@ class CallExpr : public Expr {
     *getTrailingFPFeatures() = F;
   }
 
+  /// Get the store FPOptionsOverride or default if not stored.
+  FPOptionsOverride getStoredFPFeaturesOrDefault() const {
+    return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
+  }
+
   /// Get the FP features status of this operator. Only meaningful for
   /// operations on floating point types.
   FPOptions getFPFeaturesInEffect(const LangOptions &LO) const {
@@ -3592,6 +3602,11 @@ class CastExpr : public Expr {
     return *getTrailingFPFeatures();
   }
 
+  /// Get the store FPOptionsOverride or default if not stored.
+  FPOptionsOverride getStoredFPFeaturesOrDefault() const {
+    return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
+  }
+
   /// Get the FP features status of this operation. Only meaningful for
   /// operations on floating point types.
   FPOptions getFPFeaturesInEffect(const LangOptions &LO) const {
@@ -4038,6 +4053,10 @@ class BinaryOperator : public Expr {
     assert(BinaryOperatorBits.HasFPFeatures);
     *getTrailingFPFeatures() = F;
   }
+  /// Get the store FPOptionsOverride or default if not stored.
+  FPOptionsOverride getStoredFPFeaturesOrDefault() const {
+    return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
+  }
 
   /// Get the FP features status of this operator. Only meaningful for
   /// operations on floating point types.
diff --git a/clang/include/clang/AST/Stmt.h b/clang/include/clang/AST/Stmt.h
index 9cd7a364cd3f1..e91e89d728ca0 100644
--- a/clang/include/clang/AST/Stmt.h
+++ b/clang/include/clang/AST/Stmt.h
@@ -1658,6 +1658,11 @@ class CompoundStmt final
     return *getTrailingObjects<FPOptionsOverride>();
   }
 
+  /// Get the store FPOptionsOverride or default if not stored.
+  FPOptionsOverride getStoredFPFeaturesOrDefault() const {
+    return hasStoredFPFeatures() ? getStoredFPFeatures() : FPOptionsOverride();
+  }
+
   using body_iterator = Stmt **;
   using body_range = llvm::iterator_range<body_iterator>;
 
diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 0f68d0d9aaa9e..09e154a71259d 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -352,17 +352,10 @@ class ComplexExprEmitter
     return QualType();
   }
 
-  template <typename T>
-  FPOptionsOverride getStoredFPFeaturesOrDefault(const T *E,
-                                                 const CodeGenFunction &CGF) {
-    return E->hasStoredFPFeatures() ? E->getStoredFPFeatures()
-                                    : FPOptionsOverride();
-  }
-
 #define HANDLEBINOP(OP)                                                        \
   ComplexPairTy VisitBin##OP(const BinaryOperator *E) {                        \
     QualType promotionTy = getPromotionType(                                   \
-        getStoredFPFeaturesOrDefault(E, CGF), E->getType(),                    \
+        E->getStoredFPFeaturesOrDefault(), E->getType(),                       \
         (E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false);        \
     ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy));            \
     if (!promotionTy.isNull())                                                 \
@@ -656,9 +649,10 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,
 
 ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
                                                  QualType PromotionType) {
+  E->hasStoredFPFeatures();
   QualType promotionTy =
       PromotionType.isNull()
-          ? getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+          ? getPromotionType(E->getStoredFPFeaturesOrDefault(),
                              E->getSubExpr()->getType())
           : PromotionType;
   ComplexPairTy result = VisitPlus(E, promotionTy);
@@ -677,10 +671,10 @@ ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
 }
 
 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
-                                                  QualType PromotionType) {
+                                                      QualType PromotionType) {
   QualType promotionTy =
       PromotionType.isNull()
-          ? getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+          ? getPromotionType(E->getStoredFPFeaturesOrDefault(),
                              E->getSubExpr()->getType())
           : PromotionType;
   ComplexPairTy result = VisitMinus(E, promotionTy);
@@ -1242,7 +1236,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
   // __block variables need to have the rhs evaluated first, plus this should
   // improve codegen a little.
   QualType PromotionTypeCR;
-  PromotionTypeCR = getPromotionType(getStoredFPFeaturesOrDefault(E, CGF),
+  PromotionTypeCR = getPromotionType(E->getStoredFPFeaturesOrDefault(),
                                      E->getComputationResultType());
   if (PromotionTypeCR.isNull())
     PromotionTypeCR = E->getComputationResultType();
@@ -1250,7 +1244,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
   QualType ComplexElementTy =
       OpInfo.Ty->castAs<ComplexType>()->getElementType();
   QualType PromotionTypeRHS = getPromotionType(
-      getStoredFPFeaturesOrDefault(E, CGF), E->getRHS()->getType());
+      E->getStoredFPFeaturesOrDefault(), E->getRHS()->getType());
 
   // The RHS should have been converted to the computation type.
   if (E->getRHS()->getType()->isRealFloatingType()) {
@@ -1279,7 +1273,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E,
   // Load from the l-value and convert it.
   SourceLocation Loc = E->getExprLoc();
   QualType PromotionTypeLHS = getPromotionType(
-      getStoredFPFeaturesOrDefault(E, CGF), E->getComputationLHSType());
+      E->getStoredFPFeaturesOrDefault(), E->getComputationLHSType());
   if (LHSTy->isAnyComplexType()) {
     ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
     if (!PromotionTypeLHS.isNull())

>From afcc92a7b99be25e4ec4e2471fa5a03854944134 Mon Sep 17 00:00:00 2001
From: Zahira Ammarguellat <zahira.ammarguellat at intel.com>
Date: Tue, 16 Jul 2024 11:32:13 -0700
Subject: [PATCH 8/8] Fix format.

---
 clang/lib/CodeGen/CGExprComplex.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp
index 09e154a71259d..e2f868c314148 100644
--- a/clang/lib/CodeGen/CGExprComplex.cpp
+++ b/clang/lib/CodeGen/CGExprComplex.cpp
@@ -671,7 +671,7 @@ ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,
 }
 
 ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
-                                                      QualType PromotionType) {
+                                                  QualType PromotionType) {
   QualType promotionTy =
       PromotionType.isNull()
           ? getPromotionType(E->getStoredFPFeaturesOrDefault(),



More information about the cfe-commits mailing list