Skip to content

Conversation

@artagnon
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Ramkumar Ramachandra (artagnon)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/151540.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h (+6)
  • (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+9-12)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h index d133610ef4f75..7e6d83c09f860 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h +++ b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h @@ -461,6 +461,12 @@ m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) { return m_BinaryOr<Op0_t, Op1_t, /*Commutative*/ true>(Op0, Op1); } +template <typename Op0_t, typename Op1_t> +inline AllBinaryRecipe_match<Op0_t, Op1_t, Instruction::ICmp, false> +m_ICmp(const Op0_t &Op0, const Op1_t &Op1) { + return m_Binary<Instruction::ICmp, Op0_t, Op1_t, false>(Op0, Op1); +} + template <typename Op0_t, typename Op1_t> using GEPLikeRecipe_match = BinaryRecipe_match<Op0_t, Op1_t, Instruction::GetElementPtr, false, diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp index a1d12a3a01e5e..253144e64f9f1 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -1383,11 +1383,10 @@ static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan, // Currently only handle cases where the single user is a header-mask // comparison with the backedge-taken-count. - if (!match( - *WideIV->user_begin(), - m_Binary<Instruction::ICmp>( - m_Specific(WideIV), - m_Broadcast(m_Specific(Plan.getOrCreateBackedgeTakenCount()))))) + if (!match(*WideIV->user_begin(), + m_ICmp(m_Specific(WideIV), + m_Broadcast( + m_Specific(Plan.getOrCreateBackedgeTakenCount()))))) continue; // Update IV operands and comparison bound to use new narrower type. @@ -1420,9 +1419,8 @@ static bool isConditionTrueViaVFAndUF(VPValue *Cond, VPlan &Plan, }); auto *CanIV = Plan.getCanonicalIV(); - if (!match(Cond, m_Binary<Instruction::ICmp>( - m_Specific(CanIV->getBackedgeValue()), - m_Specific(&Plan.getVectorTripCount()))) || + if (!match(Cond, m_ICmp(m_Specific(CanIV->getBackedgeValue()), + m_Specific(&Plan.getVectorTripCount()))) || cast<VPRecipeWithIRFlags>(Cond->getDefiningRecipe())->getPredicate() != CmpInst::ICMP_EQ) return false; @@ -1835,7 +1833,7 @@ void VPlanTransforms::truncateToMinimalBitwidths( VPW->dropPoisonGeneratingFlags(); if (OldResSizeInBits != NewResSizeInBits && - !match(&R, m_Binary<Instruction::ICmp>(m_VPValue(), m_VPValue()))) { + !match(&R, m_ICmp(m_VPValue(), m_VPValue()))) { // Extend result to original width. auto *Ext = new VPWidenCastRecipe(Instruction::ZExt, ResultVPV, OldResTy); @@ -1844,9 +1842,8 @@ void VPlanTransforms::truncateToMinimalBitwidths( Ext->setOperand(0, ResultVPV); assert(OldResSizeInBits > NewResSizeInBits && "Nothing to shrink?"); } else { - assert( - match(&R, m_Binary<Instruction::ICmp>(m_VPValue(), m_VPValue())) && - "Only ICmps should not need extending the result."); + assert(match(&R, m_ICmp(m_VPValue(), m_VPValue())) && + "Only ICmps should not need extending the result."); } assert(!isa<VPWidenStoreRecipe>(&R) && "stores cannot be narrowed"); 
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

@llvm/pr-subscribers-vectorizers

Author: Ramkumar Ramachandra (artagnon)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/151540.diff

2 Files Affected:

  • (modified) llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h (+6)
  • (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+9-12)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h index d133610ef4f75..7e6d83c09f860 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h +++ b/llvm/lib/Transforms/Vectorize/VPlanPatternMatch.h @@ -461,6 +461,12 @@ m_c_BinaryOr(const Op0_t &Op0, const Op1_t &Op1) { return m_BinaryOr<Op0_t, Op1_t, /*Commutative*/ true>(Op0, Op1); } +template <typename Op0_t, typename Op1_t> +inline AllBinaryRecipe_match<Op0_t, Op1_t, Instruction::ICmp, false> +m_ICmp(const Op0_t &Op0, const Op1_t &Op1) { + return m_Binary<Instruction::ICmp, Op0_t, Op1_t, false>(Op0, Op1); +} + template <typename Op0_t, typename Op1_t> using GEPLikeRecipe_match = BinaryRecipe_match<Op0_t, Op1_t, Instruction::GetElementPtr, false, diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp index a1d12a3a01e5e..253144e64f9f1 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -1383,11 +1383,10 @@ static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan, // Currently only handle cases where the single user is a header-mask // comparison with the backedge-taken-count. - if (!match( - *WideIV->user_begin(), - m_Binary<Instruction::ICmp>( - m_Specific(WideIV), - m_Broadcast(m_Specific(Plan.getOrCreateBackedgeTakenCount()))))) + if (!match(*WideIV->user_begin(), + m_ICmp(m_Specific(WideIV), + m_Broadcast( + m_Specific(Plan.getOrCreateBackedgeTakenCount()))))) continue; // Update IV operands and comparison bound to use new narrower type. @@ -1420,9 +1419,8 @@ static bool isConditionTrueViaVFAndUF(VPValue *Cond, VPlan &Plan, }); auto *CanIV = Plan.getCanonicalIV(); - if (!match(Cond, m_Binary<Instruction::ICmp>( - m_Specific(CanIV->getBackedgeValue()), - m_Specific(&Plan.getVectorTripCount()))) || + if (!match(Cond, m_ICmp(m_Specific(CanIV->getBackedgeValue()), + m_Specific(&Plan.getVectorTripCount()))) || cast<VPRecipeWithIRFlags>(Cond->getDefiningRecipe())->getPredicate() != CmpInst::ICMP_EQ) return false; @@ -1835,7 +1833,7 @@ void VPlanTransforms::truncateToMinimalBitwidths( VPW->dropPoisonGeneratingFlags(); if (OldResSizeInBits != NewResSizeInBits && - !match(&R, m_Binary<Instruction::ICmp>(m_VPValue(), m_VPValue()))) { + !match(&R, m_ICmp(m_VPValue(), m_VPValue()))) { // Extend result to original width. auto *Ext = new VPWidenCastRecipe(Instruction::ZExt, ResultVPV, OldResTy); @@ -1844,9 +1842,8 @@ void VPlanTransforms::truncateToMinimalBitwidths( Ext->setOperand(0, ResultVPV); assert(OldResSizeInBits > NewResSizeInBits && "Nothing to shrink?"); } else { - assert( - match(&R, m_Binary<Instruction::ICmp>(m_VPValue(), m_VPValue())) && - "Only ICmps should not need extending the result."); + assert(match(&R, m_ICmp(m_VPValue(), m_VPValue())) && + "Only ICmps should not need extending the result."); } assert(!isa<VPWidenStoreRecipe>(&R) && "stores cannot be narrowed"); 
@artagnon
Copy link
Contributor Author

artagnon commented Aug 1, 2025

I think we want to match the CmpPredicate here?

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this should also allow capturing the predicate

@artagnon artagnon changed the title [VPlan] Introduce m_ICmp matcher [VPlan] Introduce m_[Specific]ICmp matcher Aug 4, 2025
Copy link
Contributor

@lukel97 lukel97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Just noting that we don't seem to bind the condition code anywhere anymore but I think that's fine, it's good to keep things in sync with LLVM IR's patternmatch

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks

@artagnon artagnon merged commit 0923881 into llvm:main Aug 6, 2025
9 checks passed
@artagnon artagnon deleted the vplan-pm-icmp branch August 6, 2025 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment