Skip to content

Commit

Permalink
The pragma STDC CX_LIMITED_RANGE ON should have precedence. (llvm#98520)
Browse files Browse the repository at this point in the history
The `pragma STDC CX_LIMITED_RANGE` should have precedence over the
command line `-fcomplex-arithmetic`.
  • Loading branch information
zahiraam authored and sgundapa committed Jul 23, 2024
1 parent 64368c9 commit d608e1f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
19 changes: 19 additions & 0 deletions clang/include/clang/AST/Expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions clang/include/clang/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down
44 changes: 30 additions & 14 deletions clang/lib/CodeGen/CGExprComplex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,12 +328,20 @@ 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)
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()))
Expand All @@ -347,7 +355,7 @@ class ComplexExprEmitter
#define HANDLEBINOP(OP) \
ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \
QualType promotionTy = getPromotionType( \
E->getType(), \
E->getStoredFPFeaturesOrDefault(), E->getType(), \
(E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false); \
ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \
if (!promotionTy.isNull()) \
Expand Down Expand Up @@ -641,9 +649,12 @@ ComplexPairTy ComplexExprEmitter::EmitCast(CastKind CK, Expr *Op,

ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E,
QualType PromotionType) {
QualType promotionTy = PromotionType.isNull()
? getPromotionType(E->getSubExpr()->getType())
: PromotionType;
E->hasStoredFPFeatures();
QualType promotionTy =
PromotionType.isNull()
? getPromotionType(E->getStoredFPFeaturesOrDefault(),
E->getSubExpr()->getType())
: PromotionType;
ComplexPairTy result = VisitPlus(E, promotionTy);
if (!promotionTy.isNull())
return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
Expand All @@ -661,9 +672,11 @@ ComplexPairTy ComplexExprEmitter::VisitPlus(const UnaryOperator *E,

ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E,
QualType PromotionType) {
QualType promotionTy = PromotionType.isNull()
? getPromotionType(E->getSubExpr()->getType())
: PromotionType;
QualType promotionTy =
PromotionType.isNull()
? getPromotionType(E->getStoredFPFeaturesOrDefault(),
E->getSubExpr()->getType())
: PromotionType;
ComplexPairTy result = VisitMinus(E, promotionTy);
if (!promotionTy.isNull())
return CGF.EmitUnPromotedValue(result, E->getSubExpr()->getType());
Expand Down Expand Up @@ -1218,13 +1231,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->getComputationResultType());
PromotionTypeCR = getPromotionType(E->getStoredFPFeaturesOrDefault(),
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->getStoredFPFeaturesOrDefault(), E->getRHS()->getType());

// The RHS should have been converted to the computation type.
if (E->getRHS()->getType()->isRealFloatingType()) {
Expand Down Expand Up @@ -1252,7 +1267,8 @@ 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->getStoredFPFeaturesOrDefault(), E->getComputationLHSType());
if (LHSTy->isAnyComplexType()) {
ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc);
if (!PromotionTypeLHS.isNull())
Expand Down
28 changes: 12 additions & 16 deletions clang/test/CodeGen/pragma-cx-limited-range.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit d608e1f

Please sign in to comment.