https://github.com/akash-manna-sky created https://github.com/llvm/llvm-project/pull/244915
Fixes #66555 Under `-ffast-math`, `#pragma float_control(precise, on)` still lowered complex division and multiplication with the fast-math formulas. CodeGen picks the algorithm from the `ComplexRange` field of the expression's `FPOptions`, which `-ffast-math` sets to `basic`, and `setFPPreciseEnabled` reset every other fast-math bit but left that one alone. `setFPPreciseEnabled` now overrides the complex range too, `full` for precise on and `basic` for precise off, as the driver does for `-ffp-model=precise` and `-ffast-math`. CodeGen's promotion check and Sema's precision-loss warning now read the range from the `FPOptions` in effect. AI Aided! >From 2c76509c8d286137500791f3a91719b8885f566f Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Fri, 20 Nov 2026 23:05:12 +0530 Subject: [PATCH] [Clang][Sema] Apply `float_control`(precise) to complex arithmetic Fixes #66555 Under `-ffast-math`, `#pragma float_control(precise, on)` still lowered complex division and multiplication with the fast-math formulas. CodeGen picks the algorithm from the `ComplexRange` field of the expression's `FPOptions`, which `-ffast-math` sets to `basic`, and `setFPPreciseEnabled` reset every other fast-math bit but left that one alone. `setFPPreciseEnabled` now overrides the complex range too, `full` for precise on and `basic` for precise off, as the driver does for `-ffp-model=precise` and `-ffast-math`. CodeGen's promotion check and Sema's precision-loss warning now read the range from the `FPOptions` in effect. LLM tools were used for this contribution. --- clang/docs/LanguageExtensions.md | 7 +- clang/docs/ReleaseNotes.md | 1 + clang/include/clang/Basic/LangOptions.h | 3 + clang/lib/CodeGen/CGExprComplex.cpp | 38 ++--- clang/lib/Sema/SemaExpr.cpp | 2 +- clang/test/CodeGen/pragma-cx-limited-range.c | 166 +++++++++++++++++++ 6 files changed, 191 insertions(+), 26 deletions(-) diff --git a/clang/docs/LanguageExtensions.md b/clang/docs/LanguageExtensions.md index 30018364300adb..9ed4222dd4a48a 100644 --- a/clang/docs/LanguageExtensions.md +++ b/clang/docs/LanguageExtensions.md @@ -6149,12 +6149,15 @@ settings can be pushed or popped. When `pragma float_control(precise, on)` is enabled, the section of code governed by the pragma uses precise floating point semantics, effectively `-ffast-math` is disabled and `-ffp-contract=on` -(fused multiply add) is enabled. This pragma enables `-fmath-errno`. +(fused multiply add) is enabled. This pragma enables `-fmath-errno`. Complex +multiplication and division use the `-fcomplex-arithmetic=full` +implementation. When `pragma float_control(precise, off)` is enabled, unsafe-floating point optimizations are enabled in the section of code governed by the pragma. Effectively `-ffast-math` is enabled and `-ffp-contract=fast`. This pragma -disables `-fmath-errno`. +disables `-fmath-errno`. Complex multiplication and division use the +`-fcomplex-arithmetic=basic` implementation. When `pragma float_control(except, on)` is enabled, the section of code governed by the pragma behaves as though the command-line option diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index a72577e6b33d71..0766c3dea0a99f 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -865,6 +865,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and - Fixed an assertion crash when instantiating a nested requirement with an invalid constraint. (#GH213575) - Clang now defines the GCC-compatible predefined macro `__SIG_ATOMIC_TYPE__`. (#GH213895) - Fixed IEEE f128 complex mul/div using the IBM f128 libcalls on powerpc. (#GH216820) +- Fixed `#pragma float_control(precise, on)` not applying to complex multiplication and division under `-ffast-math`. (#GH66555) - Fixed an ICE that occurred when a structured binding pack is expanded outside the lambda where it was declared. (#GH214160) - Fixed a crash when a statement expression in the initializer of a new-expression starts with a loop or a label. (#GH175427) - Fixed a crash in an implicit assignment operator when a `noreturn` member or base `operator=` is followed by trivially copyable members. (#GH111612) diff --git a/clang/include/clang/Basic/LangOptions.h b/clang/include/clang/Basic/LangOptions.h index 8607fbae8d8fce..b32fef2b09ab6e 100644 --- a/clang/include/clang/Basic/LangOptions.h +++ b/clang/include/clang/Basic/LangOptions.h @@ -1068,6 +1068,9 @@ class FPOptionsOverride { setAllowReciprocalOverride(!Value); setAllowApproxFuncOverride(!Value); setMathErrnoOverride(Value); + /* -ffast-math implies -fcomplex-arithmetic=basic, precise mode full */ + setComplexRangeOverride(Value ? LangOptions::CX_Full + : LangOptions::CX_Basic); if (Value) /* Precise mode implies fp_contract=on and disables ffast-math */ setAllowFPContractWithinStatement(); diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 59746b7801fa85..d3f943ec28b1f3 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -314,20 +314,15 @@ class ComplexExprEmitter } } - QualType getPromotionType(FPOptionsOverride Features, QualType Ty, + QualType getPromotionType(FPOptions Features, QualType Ty, bool IsComplexDivisor) { if (auto *CT = Ty->getAs<ComplexType>()) { QualType ElementType = CT->getElementType().getCanonicalType(); bool IsFloatingType = ElementType->isFloatingType(); - bool IsComplexRangePromoted = CGF.getLangOpts().getComplexRange() == + bool IsComplexRangePromoted = Features.getComplexRange() == LangOptions::ComplexRangeKind::CX_Promoted; - bool HasNoComplexRangeOverride = !Features.hasComplexRangeOverride(); - bool HasMatchingComplexRange = Features.hasComplexRangeOverride() && - Features.getComplexRangeOverride() == - CGF.getLangOpts().getComplexRange(); - if (IsComplexDivisor && IsFloatingType && IsComplexRangePromoted && - (HasNoComplexRangeOverride || HasMatchingComplexRange)) + if (IsComplexDivisor && IsFloatingType && IsComplexRangePromoted) return HigherPrecisionTypeForComplexArithmetic(ElementType); if (ElementType.UseExcessPrecision(CGF.getContext())) return CGF.getContext().getComplexType(CGF.getContext().FloatTy); @@ -339,10 +334,10 @@ class ComplexExprEmitter #define HANDLEBINOP(OP) \ ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \ - QualType promotionTy = \ - getPromotionType(E->getStoredFPFeaturesOrDefault(), E->getType(), \ - (E->getOpcode() == BinaryOperatorKind::BO_Div && \ - E->getRHS()->getType()->isAnyComplexType())); \ + QualType promotionTy = getPromotionType( \ + E->getFPFeaturesInEffect(CGF.getLangOpts()), E->getType(), \ + (E->getOpcode() == BinaryOperatorKind::BO_Div && \ + E->getRHS()->getType()->isAnyComplexType())); \ ComplexPairTy result = EmitBin##OP(EmitBinOps(E, promotionTy)); \ if (!promotionTy.isNull()) \ result = CGF.EmitUnPromotedValue(result, E->getType()); \ @@ -659,7 +654,7 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E, QualType PromotionType) { QualType promotionTy = PromotionType.isNull() - ? getPromotionType(E->getStoredFPFeaturesOrDefault(), + ? getPromotionType(E->getFPFeaturesInEffect(CGF.getLangOpts()), E->getSubExpr()->getType(), /*IsComplexDivisor=*/false) : PromotionType; @@ -682,7 +677,7 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E, QualType PromotionType) { QualType promotionTy = PromotionType.isNull() - ? getPromotionType(E->getStoredFPFeaturesOrDefault(), + ? getPromotionType(E->getFPFeaturesInEffect(CGF.getLangOpts()), E->getSubExpr()->getType(), /*IsComplexDivisor=*/false) : PromotionType; @@ -1245,17 +1240,15 @@ LValue ComplexExprEmitter::EmitCompoundAssignLValue( // __block variables need to have the rhs evaluated first, plus this should // improve codegen a little. QualType PromotionTypeCR; - PromotionTypeCR = - getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationResultType(), IsComplexDivisor); + PromotionTypeCR = getPromotionType( + OpInfo.FPFeatures, E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; QualType ComplexElementTy = OpInfo.Ty->castAs<ComplexType>()->getElementType(); - QualType PromotionTypeRHS = - getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getRHS()->getType(), IsComplexDivisor); + QualType PromotionTypeRHS = getPromotionType( + OpInfo.FPFeatures, E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1283,9 +1276,8 @@ LValue ComplexExprEmitter::EmitCompoundAssignLValue( // Load from the l-value and convert it. SourceLocation Loc = E->getExprLoc(); - QualType PromotionTypeLHS = - getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationLHSType(), IsComplexDivisor); + QualType PromotionTypeLHS = getPromotionType( + OpInfo.FPFeatures, E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 873ad8d8e1cb92..09601cb720cf71 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -11355,7 +11355,7 @@ static void DetectPrecisionLossInComplexDivision(Sema &S, QualType DivisorTy, return; QualType ElementType = CT->getElementType().getCanonicalType(); - bool IsComplexRangePromoted = S.getLangOpts().getComplexRange() == + bool IsComplexRangePromoted = S.CurFPFeatures.getComplexRange() == LangOptions::ComplexRangeKind::CX_Promoted; if (!ElementType->isFloatingType() || !IsComplexRangePromoted) return; diff --git a/clang/test/CodeGen/pragma-cx-limited-range.c b/clang/test/CodeGen/pragma-cx-limited-range.c index 67b6bbb80e3cff..1f999287ead9a0 100644 --- a/clang/test/CodeGen/pragma-cx-limited-range.c +++ b/clang/test/CodeGen/pragma-cx-limited-range.c @@ -16,6 +16,10 @@ // RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown \ // RUN: -complex-range=full -o - | FileCheck --check-prefix=FULL %s +// RUN: %clang_cc1 %s -O0 -emit-llvm -triple x86_64-unknown-unknown \ +// RUN: -ffast-math -ffp-contract=fast -complex-range=basic -o - \ +// RUN: | FileCheck --check-prefix=FAST %s + _Complex float pragma_on_mul(_Complex float a, _Complex float b) { #pragma STDC CX_LIMITED_RANGE ON // LABEL: define {{.*}} @pragma_on_mul( @@ -239,3 +243,165 @@ _Complex float pragma_default_div(_Complex float a, _Complex float b) { return a / b; } + +// GH66555: float_control(precise) overrides the command-line complex range. +_Complex float pragma_precise_off_div(_Complex float a, _Complex float b) { +#pragma float_control(precise, off) + // FULL-LABEL: @pragma_precise_off_div( + // FULL: fdiv fast float + // FULL-NEXT: fdiv fast float + + // BASIC-LABEL: @pragma_precise_off_div( + // BASIC: fdiv fast float + // BASIC-NEXT: fdiv fast float + + // IMPRVD-LABEL: @pragma_precise_off_div( + // IMPRVD: fdiv fast float + // IMPRVD-NEXT: fdiv fast float + + // PRMTD-LABEL: @pragma_precise_off_div( + // PRMTD: fdiv fast float + // PRMTD-NEXT: fdiv fast float + + // FAST-LABEL: @pragma_precise_off_div( + // FAST: fdiv fast float + // FAST-NEXT: fdiv fast float + + return a / b; +} + +_Complex float pragma_precise_off_mul(_Complex float a, _Complex float b) { +#pragma float_control(precise, off) + // FULL-LABEL: @pragma_precise_off_mul( + // FULL: fmul fast float + // FULL-NEXT: fmul fast float + // FULL-NEXT: fmul fast float + // FULL-NEXT: fmul fast float + // FULL-NEXT: fsub fast float + // FULL-NEXT: fadd fast float + // FULL-NOT: @__mulsc3 + + // BASIC-LABEL: @pragma_precise_off_mul( + // BASIC: fmul fast float + // BASIC-NEXT: fmul fast float + // BASIC-NEXT: fmul fast float + // BASIC-NEXT: fmul fast float + // BASIC-NEXT: fsub fast float + // BASIC-NEXT: fadd fast float + // BASIC-NOT: @__mulsc3 + + // IMPRVD-LABEL: @pragma_precise_off_mul( + // IMPRVD: fmul fast float + // IMPRVD-NEXT: fmul fast float + // IMPRVD-NEXT: fmul fast float + // IMPRVD-NEXT: fmul fast float + // IMPRVD-NEXT: fsub fast float + // IMPRVD-NEXT: fadd fast float + // IMPRVD-NOT: @__mulsc3 + + // PRMTD-LABEL: @pragma_precise_off_mul( + // PRMTD: fmul fast float + // PRMTD-NEXT: fmul fast float + // PRMTD-NEXT: fmul fast float + // PRMTD-NEXT: fmul fast float + // PRMTD-NEXT: fsub fast float + // PRMTD-NEXT: fadd fast float + // PRMTD-NOT: @__mulsc3 + + // FAST-LABEL: @pragma_precise_off_mul( + // FAST: fmul fast float + // FAST-NEXT: fmul fast float + // FAST-NEXT: fmul fast float + // FAST-NEXT: fmul fast float + // FAST-NEXT: fsub fast float + // FAST-NEXT: fadd fast float + // FAST-NOT: @__mulsc3 + + return a * b; +} + +#pragma float_control(push) +#pragma float_control(precise, on) +_Complex float pragma_precise_on_div(_Complex float a, _Complex float b) { + // FULL-LABEL: @pragma_precise_on_div( + // FULL-NOT: fdiv + // FULL: call {{.*}} @__divsc3 + // FULL-NOT: fdiv + + // BASIC-LABEL: @pragma_precise_on_div( + // BASIC-NOT: fdiv + // BASIC: call {{.*}} @__divsc3 + // BASIC-NOT: fdiv + + // IMPRVD-LABEL: @pragma_precise_on_div( + // IMPRVD-NOT: fdiv + // IMPRVD: call {{.*}} @__divsc3 + // IMPRVD-NOT: fdiv + + // PRMTD-LABEL: @pragma_precise_on_div( + // PRMTD-NOT: fdiv + // PRMTD: call {{.*}} @__divsc3 + // PRMTD-NOT: fdiv + + // FAST-LABEL: @pragma_precise_on_div( + // FAST-NOT: fdiv + // FAST: call {{.*}} @__divsc3 + // FAST-NOT: fdiv + + return a / b; +} + +_Complex float pragma_precise_on_mul(_Complex float a, _Complex float b) { + // FULL-LABEL: @pragma_precise_on_mul( + // FULL: call {{.*}} @__mulsc3 + + // BASIC-LABEL: @pragma_precise_on_mul( + // BASIC: call {{.*}} @__mulsc3 + + // IMPRVD-LABEL: @pragma_precise_on_mul( + // IMPRVD: call {{.*}} @__mulsc3 + + // PRMTD-LABEL: @pragma_precise_on_mul( + // PRMTD: call {{.*}} @__mulsc3 + + // FAST-LABEL: @pragma_precise_on_mul( + // FAST: fmul float + // FAST-NEXT: fmul float + // FAST-NEXT: fmul float + // FAST-NEXT: fmul float + // FAST-NEXT: fsub float + // FAST-NEXT: fadd float + // FAST: call {{.*}} @__mulsc3 + + return a * b; +} + +void pragma_precise_on_div_assign(_Complex float *a, _Complex float b) { + // FULL-LABEL: @pragma_precise_on_div_assign( + // FULL-NOT: fdiv + // FULL: call {{.*}} @__divsc3 + // FULL-NOT: fdiv + + // BASIC-LABEL: @pragma_precise_on_div_assign( + // BASIC-NOT: fdiv + // BASIC: call {{.*}} @__divsc3 + // BASIC-NOT: fdiv + + // IMPRVD-LABEL: @pragma_precise_on_div_assign( + // IMPRVD-NOT: fdiv + // IMPRVD: call {{.*}} @__divsc3 + // IMPRVD-NOT: fdiv + + // PRMTD-LABEL: @pragma_precise_on_div_assign( + // PRMTD-NOT: fdiv + // PRMTD: call {{.*}} @__divsc3 + // PRMTD-NOT: fdiv + + // FAST-LABEL: @pragma_precise_on_div_assign( + // FAST-NOT: fdiv + // FAST: call {{.*}} @__divsc3 + // FAST-NOT: fdiv + + *a /= b; +} +#pragma float_control(pop) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
