[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
@@ -314,7 +313,7 @@ class ComplexExprEmitter } QualType getPromotionType(FPOptionsOverride Features, QualType Ty, -bool IsDivOpCode = false) { +bool IsComplexDivisor = false) { Maetveis wrote: Thanks, done. Sorry for the force push, seems like graphite does that by default. https://github.com/llvm/llvm-project/pull/131451 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
https://github.com/Maetveis edited https://github.com/llvm/llvm-project/pull/131451 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/131453 From 9d50aa09e1f06ec145715896173750414ec75c0d Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 12:53:32 +0100 Subject: [PATCH] [Clang][CodeGen] Promote in complex compound divassign When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp | 13 +- clang/test/CodeGen/cx-complex-range.c | 534 ++ 2 files changed, 221 insertions(+), 326 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 34f40feac7958..a7c8b96da6853 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -1214,13 +1214,16 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); + const bool IsComplexDivisor = E->getOpcode() == BO_DivAssign && +E->getRHS()->getType()->isAnyComplexType(); + // Load the RHS and LHS operands. // __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=*/false); + PromotionTypeCR = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; @@ -1228,7 +1231,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.Ty->castAs()->getElementType(); QualType PromotionTypeRHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getRHS()->getType(), /*IsComplexDivisor=*/false); + E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1258,7 +1261,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, SourceLocation Loc = E->getExprLoc(); QualType PromotionTypeLHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationLHSType(), /*IsComplexDivisor=*/false); + E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/test/CodeGen/cx-complex-range.c b/clang/test/CodeGen/cx-complex-range.c index 06a349fbc2a47..a724e1ca8cb6d 100644 --- a/clang/test/CodeGen/cx-complex-range.c +++ b/clang/test/CodeGen/cx-complex-range.c @@ -721,44 +721,32 @@ _Complex float divf(_Complex float a, _Complex float b) { // PRMTD-NEXT:[[B_REAL:%.*]] = load float, ptr [[B_REALP]], align 4 // PRMTD-NEXT:[[B_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[B]], i32 0, i32 1 // PRMTD-NEXT:[[B_IMAG:%.*]] = load float, ptr [[B_IMAGP]], align 4 +// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[B_REAL]] to double +// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[B_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8 // PRMTD-NEXT:[[DOTREALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 0 // PRMTD-NEXT:[[DOTREAL:%.*]] = load float, ptr [[DOTREALP]], align 4 // PRMTD-NEXT:[[DOTIMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 1 // PRMTD-NEXT:[[DOTIMAG:%.*]] = load float, ptr [[DOTIMAGP]], align 4 -// PRMTD-NEXT:[[TMP1:%.*]] = call float @llvm.fabs.f32(float [[B_REAL]]) -// PRMTD-NEXT:[[TMP2:%.*]] = call float @llvm.fabs.f32(float [[B_IMAG]]) -// PRMTD-NEXT:[[ABS_CMP:%.*]] = fcmp ugt float [[TMP1]], [[TMP2]] -// PRMTD-NEXT:br i1 [[ABS_CMP]], label [[ABS_RHSR_GREATER_OR_EQUAL_ABS_RHSI:%.*]], label [[ABS_RHSR_LESS_THAN_ABS_RHSI:%.*]] -// PRMTD: abs_rhsr_greater_or_equal_abs_rhsi: -// PRMTD-NEXT:[[TMP3:%.*]] = fdiv float [[B_IMAG]], [[B_REAL]] -// PRMTD-NEXT:[[TMP4:%.*]] = fmul float [[TMP3]], [[B_IMAG]] -// PRMTD-NEXT:[[TMP5:%.*]] = fadd float [[B_REAL]], [[TMP4]] -// PRMTD-NEXT:[[TMP6:%.*]] = fmul float [[DOTIMAG]], [[TMP3]] -// PRMTD-NEXT:[[TMP7:%.*]] = fadd float [[DOTREAL]], [[TMP6]] -// PRMTD-NEXT:[[TMP8:%.*]] = fdiv float [[TMP7]], [[TMP5]] -// PRMTD-NEXT:[[TMP9:%.*]] = fmul float [[DOTREAL]], [[TMP3]] -// PRMTD-NEXT:[[TMP10:
[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/131451 From c6e05d776c23ea3d70ad4f82d6625ce68d9e6307 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 11:54:12 +0100 Subject: [PATCH] [Clang][CodeGen] Do not promote if complex divisor is real Relates-to: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp| 36 --- clang/test/CodeGen/cx-complex-range-real.c | 52 +++--- 2 files changed, 36 insertions(+), 52 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index ff7c55be246cc..34f40feac7958 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -286,8 +286,7 @@ class ComplexExprEmitter ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName, const BinOpInfo &Op); - QualType HigherPrecisionTypeForComplexArithmetic(QualType ElementType, - bool IsDivOpCode) { + QualType HigherPrecisionTypeForComplexArithmetic(QualType ElementType) { ASTContext &Ctx = CGF.getContext(); const QualType HigherElementType = Ctx.GetHigherPrecisionFPType(ElementType); @@ -314,7 +313,7 @@ class ComplexExprEmitter } QualType getPromotionType(FPOptionsOverride Features, QualType Ty, -bool IsDivOpCode = false) { +bool IsComplexDivisor) { if (auto *CT = Ty->getAs()) { QualType ElementType = CT->getElementType(); bool IsFloatingType = ElementType->isFloatingType(); @@ -325,10 +324,9 @@ class ComplexExprEmitter Features.getComplexRangeOverride() == CGF.getLangOpts().getComplexRange(); - if (IsDivOpCode && IsFloatingType && IsComplexRangePromoted && + if (IsComplexDivisor && IsFloatingType && IsComplexRangePromoted && (HasNoComplexRangeOverride || HasMatchingComplexRange)) -return HigherPrecisionTypeForComplexArithmetic(ElementType, - IsDivOpCode); +return HigherPrecisionTypeForComplexArithmetic(ElementType); if (ElementType.UseExcessPrecision(CGF.getContext())) return CGF.getContext().getComplexType(CGF.getContext().FloatTy); } @@ -339,9 +337,10 @@ class ComplexExprEmitter #define HANDLEBINOP(OP) \ ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \ -QualType promotionTy = getPromotionType( \ -E->getStoredFPFeaturesOrDefault(), E->getType(), \ -(E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false); \ +QualType promotionTy = \ +getPromotionType(E->getStoredFPFeaturesOrDefault(), 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()); \ @@ -639,7 +638,8 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryPlus(const UnaryOperator *E, QualType promotionTy = PromotionType.isNull() ? getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getSubExpr()->getType()) + E->getSubExpr()->getType(), + /*IsComplexDivisor=*/false) : PromotionType; ComplexPairTy result = VisitPlus(E, promotionTy); if (!promotionTy.isNull()) @@ -661,7 +661,8 @@ ComplexPairTy ComplexExprEmitter::VisitUnaryMinus(const UnaryOperator *E, QualType promotionTy = PromotionType.isNull() ? getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getSubExpr()->getType()) + E->getSubExpr()->getType(), + /*IsComplexDivisor=*/false) : PromotionType; ComplexPairTy result = VisitMinus(E, promotionTy); if (!promotionTy.isNull()) @@ -1218,14 +1219,16 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, // improve codegen a little. QualType PromotionTypeCR; PromotionTypeCR = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationResultType()); + E->getComputationResultType(), + /*IsComplexDivisor=*/false); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType()
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/131453 From 57b488a64f01dba352382e142fb2f60502395dd5 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 12:53:32 +0100 Subject: [PATCH] [Clang][CodeGen] Promote in complex compound divassign When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp | 13 +- clang/test/CodeGen/cx-complex-range.c | 534 ++ 2 files changed, 221 insertions(+), 326 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 34f40feac7958..a7c8b96da6853 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -1214,13 +1214,16 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); + const bool IsComplexDivisor = E->getOpcode() == BO_DivAssign && +E->getRHS()->getType()->isAnyComplexType(); + // Load the RHS and LHS operands. // __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=*/false); + PromotionTypeCR = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; @@ -1228,7 +1231,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.Ty->castAs()->getElementType(); QualType PromotionTypeRHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getRHS()->getType(), /*IsComplexDivisor=*/false); + E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1258,7 +1261,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, SourceLocation Loc = E->getExprLoc(); QualType PromotionTypeLHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationLHSType(), /*IsComplexDivisor=*/false); + E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/test/CodeGen/cx-complex-range.c b/clang/test/CodeGen/cx-complex-range.c index 06a349fbc2a47..a724e1ca8cb6d 100644 --- a/clang/test/CodeGen/cx-complex-range.c +++ b/clang/test/CodeGen/cx-complex-range.c @@ -721,44 +721,32 @@ _Complex float divf(_Complex float a, _Complex float b) { // PRMTD-NEXT:[[B_REAL:%.*]] = load float, ptr [[B_REALP]], align 4 // PRMTD-NEXT:[[B_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[B]], i32 0, i32 1 // PRMTD-NEXT:[[B_IMAG:%.*]] = load float, ptr [[B_IMAGP]], align 4 +// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[B_REAL]] to double +// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[B_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8 // PRMTD-NEXT:[[DOTREALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 0 // PRMTD-NEXT:[[DOTREAL:%.*]] = load float, ptr [[DOTREALP]], align 4 // PRMTD-NEXT:[[DOTIMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 1 // PRMTD-NEXT:[[DOTIMAG:%.*]] = load float, ptr [[DOTIMAGP]], align 4 -// PRMTD-NEXT:[[TMP1:%.*]] = call float @llvm.fabs.f32(float [[B_REAL]]) -// PRMTD-NEXT:[[TMP2:%.*]] = call float @llvm.fabs.f32(float [[B_IMAG]]) -// PRMTD-NEXT:[[ABS_CMP:%.*]] = fcmp ugt float [[TMP1]], [[TMP2]] -// PRMTD-NEXT:br i1 [[ABS_CMP]], label [[ABS_RHSR_GREATER_OR_EQUAL_ABS_RHSI:%.*]], label [[ABS_RHSR_LESS_THAN_ABS_RHSI:%.*]] -// PRMTD: abs_rhsr_greater_or_equal_abs_rhsi: -// PRMTD-NEXT:[[TMP3:%.*]] = fdiv float [[B_IMAG]], [[B_REAL]] -// PRMTD-NEXT:[[TMP4:%.*]] = fmul float [[TMP3]], [[B_IMAG]] -// PRMTD-NEXT:[[TMP5:%.*]] = fadd float [[B_REAL]], [[TMP4]] -// PRMTD-NEXT:[[TMP6:%.*]] = fmul float [[DOTIMAG]], [[TMP3]] -// PRMTD-NEXT:[[TMP7:%.*]] = fadd float [[DOTREAL]], [[TMP6]] -// PRMTD-NEXT:[[TMP8:%.*]] = fdiv float [[TMP7]], [[TMP5]] -// PRMTD-NEXT:[[TMP9:%.*]] = fmul float [[DOTREAL]], [[TMP3]] -// PRMTD-NEXT:[[TMP10:
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/131453 From 57b488a64f01dba352382e142fb2f60502395dd5 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 12:53:32 +0100 Subject: [PATCH] [Clang][CodeGen] Promote in complex compound divassign When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp | 13 +- clang/test/CodeGen/cx-complex-range.c | 534 ++ 2 files changed, 221 insertions(+), 326 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 34f40feac7958..a7c8b96da6853 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -1214,13 +1214,16 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); + const bool IsComplexDivisor = E->getOpcode() == BO_DivAssign && +E->getRHS()->getType()->isAnyComplexType(); + // Load the RHS and LHS operands. // __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=*/false); + PromotionTypeCR = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; @@ -1228,7 +1231,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.Ty->castAs()->getElementType(); QualType PromotionTypeRHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getRHS()->getType(), /*IsComplexDivisor=*/false); + E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1258,7 +1261,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, SourceLocation Loc = E->getExprLoc(); QualType PromotionTypeLHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationLHSType(), /*IsComplexDivisor=*/false); + E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/test/CodeGen/cx-complex-range.c b/clang/test/CodeGen/cx-complex-range.c index 06a349fbc2a47..a724e1ca8cb6d 100644 --- a/clang/test/CodeGen/cx-complex-range.c +++ b/clang/test/CodeGen/cx-complex-range.c @@ -721,44 +721,32 @@ _Complex float divf(_Complex float a, _Complex float b) { // PRMTD-NEXT:[[B_REAL:%.*]] = load float, ptr [[B_REALP]], align 4 // PRMTD-NEXT:[[B_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[B]], i32 0, i32 1 // PRMTD-NEXT:[[B_IMAG:%.*]] = load float, ptr [[B_IMAGP]], align 4 +// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[B_REAL]] to double +// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[B_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8 // PRMTD-NEXT:[[DOTREALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 0 // PRMTD-NEXT:[[DOTREAL:%.*]] = load float, ptr [[DOTREALP]], align 4 // PRMTD-NEXT:[[DOTIMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 1 // PRMTD-NEXT:[[DOTIMAG:%.*]] = load float, ptr [[DOTIMAGP]], align 4 -// PRMTD-NEXT:[[TMP1:%.*]] = call float @llvm.fabs.f32(float [[B_REAL]]) -// PRMTD-NEXT:[[TMP2:%.*]] = call float @llvm.fabs.f32(float [[B_IMAG]]) -// PRMTD-NEXT:[[ABS_CMP:%.*]] = fcmp ugt float [[TMP1]], [[TMP2]] -// PRMTD-NEXT:br i1 [[ABS_CMP]], label [[ABS_RHSR_GREATER_OR_EQUAL_ABS_RHSI:%.*]], label [[ABS_RHSR_LESS_THAN_ABS_RHSI:%.*]] -// PRMTD: abs_rhsr_greater_or_equal_abs_rhsi: -// PRMTD-NEXT:[[TMP3:%.*]] = fdiv float [[B_IMAG]], [[B_REAL]] -// PRMTD-NEXT:[[TMP4:%.*]] = fmul float [[TMP3]], [[B_IMAG]] -// PRMTD-NEXT:[[TMP5:%.*]] = fadd float [[B_REAL]], [[TMP4]] -// PRMTD-NEXT:[[TMP6:%.*]] = fmul float [[DOTIMAG]], [[TMP3]] -// PRMTD-NEXT:[[TMP7:%.*]] = fadd float [[DOTREAL]], [[TMP6]] -// PRMTD-NEXT:[[TMP8:%.*]] = fdiv float [[TMP7]], [[TMP5]] -// PRMTD-NEXT:[[TMP9:%.*]] = fmul float [[DOTREAL]], [[TMP3]] -// PRMTD-NEXT:[[TMP10:
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
Maetveis wrote: ### Merge activity * **Mar 19, 2:22 AM EDT**: A user started a stack merge that includes this pull request via [Graphite](https://app.graphite.dev/github/pr/llvm/llvm-project/131453). https://github.com/llvm/llvm-project/pull/131453 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [CodeGen][NFC] Run mem2reg and sroa on complex range tests (PR #131925)
https://github.com/Maetveis converted_to_draft https://github.com/llvm/llvm-project/pull/131925 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/131453 From 9d50aa09e1f06ec145715896173750414ec75c0d Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 12:53:32 +0100 Subject: [PATCH] [Clang][CodeGen] Promote in complex compound divassign When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp | 13 +- clang/test/CodeGen/cx-complex-range.c | 534 ++ 2 files changed, 221 insertions(+), 326 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 34f40feac7958..a7c8b96da6853 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -1214,13 +1214,16 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); + const bool IsComplexDivisor = E->getOpcode() == BO_DivAssign && +E->getRHS()->getType()->isAnyComplexType(); + // Load the RHS and LHS operands. // __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=*/false); + PromotionTypeCR = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; @@ -1228,7 +1231,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.Ty->castAs()->getElementType(); QualType PromotionTypeRHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getRHS()->getType(), /*IsComplexDivisor=*/false); + E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1258,7 +1261,7 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, SourceLocation Loc = E->getExprLoc(); QualType PromotionTypeLHS = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationLHSType(), /*IsComplexDivisor=*/false); + E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/test/CodeGen/cx-complex-range.c b/clang/test/CodeGen/cx-complex-range.c index 06a349fbc2a47..a724e1ca8cb6d 100644 --- a/clang/test/CodeGen/cx-complex-range.c +++ b/clang/test/CodeGen/cx-complex-range.c @@ -721,44 +721,32 @@ _Complex float divf(_Complex float a, _Complex float b) { // PRMTD-NEXT:[[B_REAL:%.*]] = load float, ptr [[B_REALP]], align 4 // PRMTD-NEXT:[[B_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[B]], i32 0, i32 1 // PRMTD-NEXT:[[B_IMAG:%.*]] = load float, ptr [[B_IMAGP]], align 4 +// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[B_REAL]] to double +// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[B_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8 // PRMTD-NEXT:[[DOTREALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 0 // PRMTD-NEXT:[[DOTREAL:%.*]] = load float, ptr [[DOTREALP]], align 4 // PRMTD-NEXT:[[DOTIMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 1 // PRMTD-NEXT:[[DOTIMAG:%.*]] = load float, ptr [[DOTIMAGP]], align 4 -// PRMTD-NEXT:[[TMP1:%.*]] = call float @llvm.fabs.f32(float [[B_REAL]]) -// PRMTD-NEXT:[[TMP2:%.*]] = call float @llvm.fabs.f32(float [[B_IMAG]]) -// PRMTD-NEXT:[[ABS_CMP:%.*]] = fcmp ugt float [[TMP1]], [[TMP2]] -// PRMTD-NEXT:br i1 [[ABS_CMP]], label [[ABS_RHSR_GREATER_OR_EQUAL_ABS_RHSI:%.*]], label [[ABS_RHSR_LESS_THAN_ABS_RHSI:%.*]] -// PRMTD: abs_rhsr_greater_or_equal_abs_rhsi: -// PRMTD-NEXT:[[TMP3:%.*]] = fdiv float [[B_IMAG]], [[B_REAL]] -// PRMTD-NEXT:[[TMP4:%.*]] = fmul float [[TMP3]], [[B_IMAG]] -// PRMTD-NEXT:[[TMP5:%.*]] = fadd float [[B_REAL]], [[TMP4]] -// PRMTD-NEXT:[[TMP6:%.*]] = fmul float [[DOTIMAG]], [[TMP3]] -// PRMTD-NEXT:[[TMP7:%.*]] = fadd float [[DOTREAL]], [[TMP6]] -// PRMTD-NEXT:[[TMP8:%.*]] = fdiv float [[TMP7]], [[TMP5]] -// PRMTD-NEXT:[[TMP9:%.*]] = fmul float [[DOTREAL]], [[TMP3]] -// PRMTD-NEXT:[[TMP10:
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis ready_for_review https://github.com/llvm/llvm-project/pull/131453 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
https://github.com/Maetveis ready_for_review https://github.com/llvm/llvm-project/pull/131451 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
https://github.com/Maetveis created https://github.com/llvm/llvm-project/pull/131451 Relates-to: https://github.com/llvm/llvm-project/issues/131129 From 45e679eba25f309130404fe879d36bb727872b62 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 11:54:12 +0100 Subject: [PATCH] [Clang][CodeGen] Do not promote if complex divisor is real Relates-to: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp| 17 --- clang/test/CodeGen/cx-complex-range-real.c | 52 +++--- 2 files changed, 24 insertions(+), 45 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index ff7c55be246cc..a8a65a2f956f8 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -286,8 +286,7 @@ class ComplexExprEmitter ComplexPairTy EmitComplexBinOpLibCall(StringRef LibCallName, const BinOpInfo &Op); - QualType HigherPrecisionTypeForComplexArithmetic(QualType ElementType, - bool IsDivOpCode) { + QualType HigherPrecisionTypeForComplexArithmetic(QualType ElementType) { ASTContext &Ctx = CGF.getContext(); const QualType HigherElementType = Ctx.GetHigherPrecisionFPType(ElementType); @@ -314,7 +313,7 @@ class ComplexExprEmitter } QualType getPromotionType(FPOptionsOverride Features, QualType Ty, -bool IsDivOpCode = false) { +bool IsComplexDivisor = false) { if (auto *CT = Ty->getAs()) { QualType ElementType = CT->getElementType(); bool IsFloatingType = ElementType->isFloatingType(); @@ -325,10 +324,9 @@ class ComplexExprEmitter Features.getComplexRangeOverride() == CGF.getLangOpts().getComplexRange(); - if (IsDivOpCode && IsFloatingType && IsComplexRangePromoted && + if (IsComplexDivisor && IsFloatingType && IsComplexRangePromoted && (HasNoComplexRangeOverride || HasMatchingComplexRange)) -return HigherPrecisionTypeForComplexArithmetic(ElementType, - IsDivOpCode); +return HigherPrecisionTypeForComplexArithmetic(ElementType); if (ElementType.UseExcessPrecision(CGF.getContext())) return CGF.getContext().getComplexType(CGF.getContext().FloatTy); } @@ -339,9 +337,10 @@ class ComplexExprEmitter #define HANDLEBINOP(OP) \ ComplexPairTy VisitBin##OP(const BinaryOperator *E) { \ -QualType promotionTy = getPromotionType( \ -E->getStoredFPFeaturesOrDefault(), E->getType(), \ -(E->getOpcode() == BinaryOperatorKind::BO_Div) ? true : false); \ +QualType promotionTy = \ +getPromotionType(E->getStoredFPFeaturesOrDefault(), 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()); \ diff --git a/clang/test/CodeGen/cx-complex-range-real.c b/clang/test/CodeGen/cx-complex-range-real.c index 1723075be30fd..94bc080d190bc 100644 --- a/clang/test/CodeGen/cx-complex-range-real.c +++ b/clang/test/CodeGen/cx-complex-range-real.c @@ -591,18 +591,13 @@ _Complex float mulbf(float a, _Complex float b) { // PRMTD-NEXT:[[A_REAL:%.*]] = load float, ptr [[A_REALP]], align 4 // PRMTD-NEXT:[[A_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[A]], i32 0, i32 1 // PRMTD-NEXT:[[A_IMAG:%.*]] = load float, ptr [[A_IMAGP]], align 4 -// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[A_REAL]] to double -// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[A_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load float, ptr [[B_ADDR]], align 4 -// PRMTD-NEXT:[[EXT2:%.*]] = fpext float [[TMP0]] to double -// PRMTD-NEXT:[[TMP1:%.*]] = fdiv double [[EXT]], [[EXT2]] -// PRMTD-NEXT:[[TMP2:%.*]] = fdiv double [[EXT1]], [[EXT2]] -// PRMTD-NEXT:[[UNPROMOTION:%.*]] = fptrunc double [[TMP1]] to float -// PRMTD-NEXT:[[UNPROMOTION3:%.*]] = fptrunc double [[TMP2]] to float +// PRMTD-NEXT:[[TMP1:%.*]] = fdiv float [[A_REAL]], [[TMP0]] +// PRMTD-NEXT:[[TMP2:%.*]] = fdiv float [[A_IMAG]], [[TMP0]] // PRMTD-NEXT:[[RETVAL_REALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[RETVAL]], i32 0, i32 0 // PRMTD-NEXT:[[RETVAL_IMAGP:%.*]] = getelementptr inbounds nuw { float, float
[llvm-branch-commits] [clang] [Clang][CodeGen] Do not promote if complex divisor is real (PR #131451)
Maetveis wrote: > [!WARNING] > This pull request is not mergeable via GitHub because a downstack PR is > open. Once all requirements are satisfied, merge this PR as a stack href="https://app.graphite.dev/github/pr/llvm/llvm-project/131451?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#131451** https://app.graphite.dev/github/pr/llvm/llvm-project/131451?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/131451?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#131447** https://app.graphite.dev/github/pr/llvm/llvm-project/131447?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * `main` This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn more about https://stacking.dev/?utm_source=stack-comment";>stacking. https://github.com/llvm/llvm-project/pull/131451 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
https://github.com/Maetveis created https://github.com/llvm/llvm-project/pull/131453 When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 From de638997789200d0ec86ca5f4c68c8e57daa0bb3 Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Sat, 15 Mar 2025 12:53:32 +0100 Subject: [PATCH] [Clang][CodeGen] Promote in complex compound divassign When `-fcomplex-arithmetic=promoted` is set complex divassign `/=` should promote to a wider type the same way division (without assignment) does. Prior to this change, Smith's algorithm would be used for divassign. Fixes: https://github.com/llvm/llvm-project/issues/131129 --- clang/lib/CodeGen/CGExprComplex.cpp | 18 +- clang/test/CodeGen/cx-complex-range.c | 534 ++ 2 files changed, 225 insertions(+), 327 deletions(-) diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index a8a65a2f956f8..dc1a34ee82805 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -1212,19 +1212,24 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, OpInfo.FPFeatures = E->getFPFeaturesInEffect(CGF.getLangOpts()); CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, OpInfo.FPFeatures); + const bool IsComplexDivisor = E->getOpcode() == BO_DivAssign && +E->getRHS()->getType()->isAnyComplexType(); + // Load the RHS and LHS operands. // __block variables need to have the rhs evaluated first, plus this should // improve codegen a little. QualType PromotionTypeCR; - PromotionTypeCR = getPromotionType(E->getStoredFPFeaturesOrDefault(), - E->getComputationResultType()); + PromotionTypeCR = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationResultType(), IsComplexDivisor); if (PromotionTypeCR.isNull()) PromotionTypeCR = E->getComputationResultType(); OpInfo.Ty = PromotionTypeCR; QualType ComplexElementTy = OpInfo.Ty->castAs()->getElementType(); - QualType PromotionTypeRHS = getPromotionType( - E->getStoredFPFeaturesOrDefault(), E->getRHS()->getType()); + QualType PromotionTypeRHS = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getRHS()->getType(), IsComplexDivisor); // The RHS should have been converted to the computation type. if (E->getRHS()->getType()->isRealFloatingType()) { @@ -1252,8 +1257,9 @@ EmitCompoundAssignLValue(const CompoundAssignOperator *E, // Load from the l-value and convert it. SourceLocation Loc = E->getExprLoc(); - QualType PromotionTypeLHS = getPromotionType( - E->getStoredFPFeaturesOrDefault(), E->getComputationLHSType()); + QualType PromotionTypeLHS = + getPromotionType(E->getStoredFPFeaturesOrDefault(), + E->getComputationLHSType(), IsComplexDivisor); if (LHSTy->isAnyComplexType()) { ComplexPairTy LHSVal = EmitLoadOfLValue(LHS, Loc); if (!PromotionTypeLHS.isNull()) diff --git a/clang/test/CodeGen/cx-complex-range.c b/clang/test/CodeGen/cx-complex-range.c index 06a349fbc2a47..a724e1ca8cb6d 100644 --- a/clang/test/CodeGen/cx-complex-range.c +++ b/clang/test/CodeGen/cx-complex-range.c @@ -721,44 +721,32 @@ _Complex float divf(_Complex float a, _Complex float b) { // PRMTD-NEXT:[[B_REAL:%.*]] = load float, ptr [[B_REALP]], align 4 // PRMTD-NEXT:[[B_IMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[B]], i32 0, i32 1 // PRMTD-NEXT:[[B_IMAG:%.*]] = load float, ptr [[B_IMAGP]], align 4 +// PRMTD-NEXT:[[EXT:%.*]] = fpext float [[B_REAL]] to double +// PRMTD-NEXT:[[EXT1:%.*]] = fpext float [[B_IMAG]] to double // PRMTD-NEXT:[[TMP0:%.*]] = load ptr, ptr [[A_ADDR]], align 8 // PRMTD-NEXT:[[DOTREALP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 0 // PRMTD-NEXT:[[DOTREAL:%.*]] = load float, ptr [[DOTREALP]], align 4 // PRMTD-NEXT:[[DOTIMAGP:%.*]] = getelementptr inbounds nuw { float, float }, ptr [[TMP0]], i32 0, i32 1 // PRMTD-NEXT:[[DOTIMAG:%.*]] = load float, ptr [[DOTIMAGP]], align 4 -// PRMTD-NEXT:[[TMP1:%.*]] = call float @llvm.fabs.f32(float [[B_REAL]]) -// PRMTD-NEXT:[[TMP2:%.*]] = call float @llvm.fabs.f32(float [[B_IMAG]]) -// PRMTD-NEXT:[[ABS_CMP:%.*]] = fcmp ugt float [[TMP1]], [[TMP2]] -// PRMTD-NEXT:br i1 [[ABS_CMP]], label [[ABS_RHSR_GREATER_OR_EQUAL_ABS_RHSI:%.*]], label [[ABS_RHSR_LESS_THAN_ABS_RHSI:%.*]] -// PRMTD: abs_rhsr_greater_or_equal_abs_rhsi: -// PRMTD-NEXT:[[TMP3:%.*]] = fdiv float [[B_IMAG]], [[B_REAL]] -// PRMTD-NEXT:[[TMP4:%.*]] = fmul float [[TMP3]], [[B_IMAG]] -// PRMTD-NEXT:[[TMP5:%.*]] = fadd float [[B_REAL]], [[TMP4]] -// PRM
[llvm-branch-commits] [clang] [Clang][CodeGen] Promote in complex compound divassign (PR #131453)
Maetveis wrote: > [!WARNING] > This pull request is not mergeable via GitHub because a downstack PR is > open. Once all requirements are satisfied, merge this PR as a stack href="https://app.graphite.dev/github/pr/llvm/llvm-project/131453?utm_source=stack-comment-downstack-mergeability-warning"; > >on Graphite. > https://graphite.dev/docs/merge-pull-requests";>Learn more * **#131453** https://app.graphite.dev/github/pr/llvm/llvm-project/131453?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> 👈 https://app.graphite.dev/github/pr/llvm/llvm-project/131453?utm_source=stack-comment-view-in-graphite"; target="_blank">(View in Graphite) * **#131451** https://app.graphite.dev/github/pr/llvm/llvm-project/131451?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * **#131447** https://app.graphite.dev/github/pr/llvm/llvm-project/131447?utm_source=stack-comment-icon"; target="_blank">https://static.graphite.dev/graphite-32x32-black.png"; alt="Graphite" width="10px" height="10px"/> * `main` This stack of pull requests is managed by https://graphite.dev?utm-source=stack-comment";>Graphite. Learn more about https://stacking.dev/?utm_source=stack-comment";>stacking. https://github.com/llvm/llvm-project/pull/131453 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
https://github.com/Maetveis edited https://github.com/llvm/llvm-project/pull/145865 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
Maetveis wrote: I separated the pre-commit test to https://github.com/llvm/llvm-project/pull/145906 https://github.com/llvm/llvm-project/pull/145865 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
https://github.com/Maetveis updated https://github.com/llvm/llvm-project/pull/145865 From 8da457d3d11d2807707bee5081114c27584e4d0f Mon Sep 17 00:00:00 2001 From: Gergely Meszaros Date: Thu, 26 Jun 2025 02:54:21 -0700 Subject: [PATCH 1/5] [FileCheck] Improve printing variables with escapes Firstly fix FileCheck printing string variables double-escaped (first regex, then C-style). This is confusing because it is not clear if the printed value is the literal value or exactly how it is escaped, without looking at FileCheck's source code. Secondly, only escape when doing so makes it easier to read the value (when the string contains tabs, newlines or non-printable characters). When the variable value is escaped, make a note of it in the output too, in order to avoid confusion. The common case that is motivating this change is variables that contain windows style paths with backslashes. These were printed as `"C:Program FilesMyAppfile.txt"`. Now prefer to print them as `"C:\Program Files\MyApp\file.txt"`. Printing the value literally also makes it easier to search for variables in the output, since the user can just copy-paste it. --- llvm/lib/FileCheck/FileCheck.cpp | 53 ++ llvm/lib/FileCheck/FileCheckImpl.h | 21 +--- llvm/test/FileCheck/var-escape.txt | 6 ++-- 3 files changed, 67 insertions(+), 13 deletions(-) diff --git a/llvm/lib/FileCheck/FileCheck.cpp b/llvm/lib/FileCheck/FileCheck.cpp index bcca499322aee..5ddb7420820dc 100644 --- a/llvm/lib/FileCheck/FileCheck.cpp +++ b/llvm/lib/FileCheck/FileCheck.cpp @@ -264,7 +264,7 @@ BinaryOperation::getImplicitFormat(const SourceMgr &SM) const { : *RightFormat; } -Expected NumericSubstitution::getResult() const { +Expected NumericSubstitution::getResultRegex() const { assert(ExpressionPointer->getAST() != nullptr && "Substituting empty expression"); Expected EvaluatedValue = ExpressionPointer->getAST()->eval(); @@ -274,7 +274,17 @@ Expected NumericSubstitution::getResult() const { return Format.getMatchingString(*EvaluatedValue); } -Expected StringSubstitution::getResult() const { +Expected NumericSubstitution::getResultForDiagnostics() const { + // The "regex" returned by getResultRegex() is just a numeric value + // like '42', '0x2A', '-17', 'DEADBEEF' etc. This is already suitable for use in diagnostics. + Expected Literal = getResultRegex(); + if (!Literal) +return Literal; + + return "\"" + std::move(*Literal) + "\""; +} + +Expected StringSubstitution::getResultRegex() const { // Look up the value and escape it so that we can put it into the regex. Expected VarVal = Context->getPatternVarValue(FromStr); if (!VarVal) @@ -282,6 +292,37 @@ Expected StringSubstitution::getResult() const { return Regex::escape(*VarVal); } +Expected StringSubstitution::getResultForDiagnostics() const { +Expected VarVal = Context->getPatternVarValue(FromStr); +if (!VarVal) + return VarVal.takeError(); + +std::string Result; +Result.reserve(VarVal->size() + 2); +raw_string_ostream OS(Result); + +OS << '"'; +// Escape the string if it contains any characters that +// make it hard to read, such as tabs, newlines, quotes, and non-printable characters. +// Note that we do not include backslashes in this set, because they are +// common in Windows paths and escaping them would make the output +// harder to read. +// However, when we do escape, backslashes are escaped as well, +// otherwise the output would be ambiguous. +const bool NeedsEscaping = llvm::any_of(*VarVal, [](char C) { + return C == '\t' || C == '\n' || C == '"' || !isPrint(C); +}); +if (NeedsEscaping) + OS.write_escaped(*VarVal); +else + OS << *VarVal; +OS << '"'; +if (NeedsEscaping) + OS << " (escaped value)"; + +return Result; +} + bool Pattern::isValidVarNameStart(char C) { return C == '_' || isAlpha(C); } Expected @@ -1106,7 +1147,7 @@ Pattern::MatchResult Pattern::match(StringRef Buffer, Error Errs = Error::success(); for (const auto &Substitution : Substitutions) { // Substitute and check for failure (e.g. use of undefined variable). - Expected Value = Substitution->getResult(); + Expected Value = Substitution->getResultRegex(); if (!Value) { // Convert to an ErrorDiagnostic to get location information. This is // done here rather than printMatch/printNoMatch since now we know which @@ -1210,7 +1251,7 @@ void Pattern::printSubstitutions(const SourceMgr &SM, StringRef Buffer, SmallString<256> Msg; raw_svector_ostream OS(Msg); - Expected MatchedValue = Substitution->getResult(); + Expected MatchedValue = Substitution->getResultForDiagnostics(); // Substitution failures are handled in printNoMatch(). if (!MatchedValue) { consumeError(Matc
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
@@ -0,0 +1,17 @@ +; RUN: echo -e "WINPATH=A:\windows\style\path" > %t +; RUN: echo -e "NOT_ESCAPED=shouldn't be escaped [a-Z]\+$" >> %t +; RUN: echo -e 'ESCAPED=\\ needs\to "be" escaped\000' >> %t + Maetveis wrote: Should be in #145906. https://github.com/llvm/llvm-project/pull/145865 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
https://github.com/Maetveis closed https://github.com/llvm/llvm-project/pull/145865 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [llvm] [FileCheck] Improve printing variables with escapes (PR #145865)
https://github.com/Maetveis reopened https://github.com/llvm/llvm-project/pull/145865 ___ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits