[llvm] [VPlan] Process combineRecipes in a worklist (PR #213899)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 04:49:07 PDT 2026
https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/213899
>From fb6d19972d1ec6f5ac6626cac1bcd183c141805a Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 4 Aug 2026 18:29:23 +0800
Subject: [PATCH 1/8] Precommit test
---
.../test/Transforms/LoopVectorize/blend-i1.ll | 58 +++++++++++++++++++
1 file changed, 58 insertions(+)
create mode 100644 llvm/test/Transforms/LoopVectorize/blend-i1.ll
diff --git a/llvm/test/Transforms/LoopVectorize/blend-i1.ll b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
new file mode 100644
index 0000000000000..576462fcb474f
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
@@ -0,0 +1,58 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --filter-out-after "^scalar.ph" --version 6
+; RUN: opt -p loop-vectorize -force-vector-width=4 -S %s | FileCheck %s
+
+define void @blend_of_i1_consts(ptr %p, i32 %n) {
+; CHECK-LABEL: define void @blend_of_i1_consts(
+; CHECK-SAME: ptr [[P:%.*]], i32 [[N:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
+; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_SCEVCHECK:.*]]
+; CHECK: [[VECTOR_SCEVCHECK]]:
+; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[N]], -1
+; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[TMP0]], 0
+; CHECK-NEXT: br i1 [[TMP1]], label %[[SCALAR_PH]], label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 4
+; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt <4 x i32> [[VEC_IND]], splat (i32 42)
+; CHECK-NEXT: [[TMP3:%.*]] = xor <4 x i1> [[TMP2]], splat (i1 true)
+; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP3]] to <4 x i32>
+; CHECK-NEXT: [[TMP5:%.*]] = getelementptr i32, ptr [[P]], i32 [[INDEX]]
+; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[TMP5]], align 4
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
+; CHECK-NEXT: [[VEC_IND_NEXT]] = add <4 x i32> [[VEC_IND]], splat (i32 4)
+; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
+; CHECK-NEXT: br i1 [[TMP6]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]]
+; CHECK-NEXT: br i1 [[CMP_N]], [[EXIT:label %.*]], label %[[SCALAR_PH]]
+; CHECK: [[SCALAR_PH]]:
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i32 [ 0, %entry ], [ %iv.next, %latch ]
+ %c = icmp sgt i32 %iv, 42
+ br i1 %c, label %then, label %latch
+
+then:
+ br label %latch
+
+latch:
+ %phi = phi i1 [ true, %loop ], [ false, %then ]
+ %zext = zext i1 %phi to i32
+ %gep = getelementptr i32, ptr %p, i32 %iv
+ store i32 %zext, ptr %gep
+
+ %iv.next = add i32 %iv, 1
+ %ec = icmp eq i32 %iv.next, %n
+ br i1 %ec, label %exit, label %loop
+
+exit:
+ ret void
+}
>From 2f492324f7cf847ec8b90aec409db970f38512f1 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Thu, 30 Jul 2026 22:38:04 +0800
Subject: [PATCH 2/8] [VPlan] Process simplifyRecipes in a worklist
This brings simplifyRecipes further in line with InstCombine, and asides from unlocking more simplifications it also helps avoid spurious test churn whenever passes are moved around simplifyRecipes.
For now just push the new recipe onto the worklist, not its users.
This uses a post order traversal so we maintain the same simplification order as before.
I've gone through and checked every simplification we do is a canonicalisation that converges, and I checked on llvm-test-suite + SPEC CPU 2017 in various configurations that we don't hit any cycles.
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 37 ++++++++++++-------
.../test/Transforms/LoopVectorize/blend-i1.ll | 5 +--
.../dont-fold-tail-for-divisible-TC.ll | 12 ++----
.../LoopVectorize/iv_outside_user.ll | 3 +-
4 files changed, 30 insertions(+), 27 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 5b95f5948cf7d..8c76e356b7365 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1714,21 +1714,30 @@ static VPSingleDefRecipe *combineRecipe(VPlan &Plan, VPSingleDefRecipe *Def) {
}
void VPlanTransforms::combineRecipes(VPlan &Plan) {
- ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
+ SetVector<VPRecipeBase *> Worklist;
+ PostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> POT(
Plan.getEntry());
- for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
- for (VPRecipeBase &R : make_early_inc_range(*VPBB))
- if (auto *Def = dyn_cast<VPSingleDefRecipe>(&R))
- if (VPSingleDefRecipe *New = combineRecipe(Plan, Def)) {
- if (New != Def) {
- // Replace the recipe with a new one.
- Def->replaceAllUsesWith(New);
- Def->eraseFromParent();
- } else if (vputils::isDeadRecipe(R)) {
- // Recipe was modified - it may be dead now.
- Def->eraseFromParent();
- }
- }
+ for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(POT))
+ for (VPRecipeBase &R : reverse(*VPBB))
+ Worklist.insert(&R);
+
+ while (!Worklist.empty()) {
+ auto *Def = dyn_cast<VPSingleDefRecipe>(Worklist.pop_back_val());
+ if (!Def)
+ continue;
+ if (VPSingleDefRecipe *New = combineRecipe(Plan, Def)) {
+ if (New != Def) {
+ // Replace the recipe with a new one.
+ Def->replaceAllUsesWith(New);
+ Def->eraseFromParent();
+ if (VPRecipeBase *NewR = New->getDefiningRecipe())
+ Worklist.insert(NewR);
+ // TODO: Append users to the worklist.
+ } else if (vputils::isDeadRecipe(*Def)) {
+ // Recipe was modified - it may be dead now.
+ Def->eraseFromParent();
+ }
+ }
}
}
diff --git a/llvm/test/Transforms/LoopVectorize/blend-i1.ll b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
index 576462fcb474f..03823a06ccd83 100644
--- a/llvm/test/Transforms/LoopVectorize/blend-i1.ll
+++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
@@ -12,14 +12,13 @@ define void @blend_of_i1_consts(ptr %p, i32 %n) {
; CHECK-NEXT: [[TMP1:%.*]] = icmp slt i32 [[TMP0]], 0
; CHECK-NEXT: br i1 [[TMP1]], label %[[SCALAR_PH]], label %[[VECTOR_PH:.*]]
; CHECK: [[VECTOR_PH]]:
-; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 4
+; CHECK-NEXT: [[N_MOD_VF:%.*]] = and i32 [[N]], 3
; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[TMP2:%.*]] = icmp sgt <4 x i32> [[VEC_IND]], splat (i32 42)
-; CHECK-NEXT: [[TMP3:%.*]] = xor <4 x i1> [[TMP2]], splat (i1 true)
+; CHECK-NEXT: [[TMP3:%.*]] = icmp sle <4 x i32> [[VEC_IND]], splat (i32 42)
; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP3]] to <4 x i32>
; CHECK-NEXT: [[TMP5:%.*]] = getelementptr i32, ptr [[P]], i32 [[INDEX]]
; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[TMP5]], align 4
diff --git a/llvm/test/Transforms/LoopVectorize/dont-fold-tail-for-divisible-TC.ll b/llvm/test/Transforms/LoopVectorize/dont-fold-tail-for-divisible-TC.ll
index 7998205e0638c..6455a488bfdd6 100644
--- a/llvm/test/Transforms/LoopVectorize/dont-fold-tail-for-divisible-TC.ll
+++ b/llvm/test/Transforms/LoopVectorize/dont-fold-tail-for-divisible-TC.ll
@@ -13,24 +13,20 @@ define void @alignTC(ptr noalias nocapture %A, i32 %n) optsize {
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[ALIGNEDTC]], 4
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
; CHECK: vector.ph:
-; CHECK-NEXT: [[N_MOD_VF:%.*]] = and i32 [[ALIGNEDTC]], 3
-; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[ALIGNEDTC]], [[N_MOD_VF]]
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
; CHECK: vector.body:
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, ptr [[A:%.*]], i32 [[INDEX]]
; CHECK-NEXT: store <4 x i32> splat (i32 13), ptr [[TMP1]], align 1
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
-; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
-; CHECK-NEXT: br i1 [[TMP3]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[ALIGNEDTC]]
+; CHECK-NEXT: br i1 [[TMP2]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
; CHECK: middle.block:
-; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[ALIGNEDTC]], [[N_VEC]]
-; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
+; CHECK-NEXT: br label [[EXIT:%.*]]
; CHECK: scalar.ph:
-; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: br label [[LOOP:%.*]]
; CHECK: loop:
-; CHECK-NEXT: [[RIV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[RIVPLUS1:%.*]], [[LOOP]] ]
+; CHECK-NEXT: [[RIV:%.*]] = phi i32 [ 0, [[SCALAR_PH]] ], [ [[RIVPLUS1:%.*]], [[LOOP]] ]
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[A]], i32 [[RIV]]
; CHECK-NEXT: store i32 13, ptr [[ARRAYIDX]], align 1
; CHECK-NEXT: [[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
diff --git a/llvm/test/Transforms/LoopVectorize/iv_outside_user.ll b/llvm/test/Transforms/LoopVectorize/iv_outside_user.ll
index 175b5c7b075a0..2f1a042fe4c47 100644
--- a/llvm/test/Transforms/LoopVectorize/iv_outside_user.ll
+++ b/llvm/test/Transforms/LoopVectorize/iv_outside_user.ll
@@ -767,7 +767,6 @@ define i32 @postinc_sub(i32 %k) {
; TAILFOLD-NEXT: [[N_RND_UP:%.*]] = add i32 [[K]], 1
; TAILFOLD-NEXT: [[N_MOD_VF:%.*]] = and i32 [[N_RND_UP]], 1
; TAILFOLD-NEXT: [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
-; TAILFOLD-NEXT: [[TMP0:%.*]] = sub i32 [[K]], [[K]]
; TAILFOLD-NEXT: br label %[[VECTOR_BODY:.*]]
; TAILFOLD: [[VECTOR_BODY]]:
; TAILFOLD-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
@@ -777,7 +776,7 @@ define i32 @postinc_sub(i32 %k) {
; TAILFOLD: [[MIDDLE_BLOCK]]:
; TAILFOLD-NEXT: br label %[[FOR_END:.*]]
; TAILFOLD: [[FOR_END]]:
-; TAILFOLD-NEXT: ret i32 [[TMP0]]
+; TAILFOLD-NEXT: ret i32 0
;
entry:
br label %for.body
>From 90d7782af1a1cac82fcc7b047fce589854dda66d Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 28 Aug 2026 10:47:00 +0800
Subject: [PATCH 3/8] Use make_pointer_range
---
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 8c76e356b7365..9a18e968ccaa8 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1718,8 +1718,7 @@ void VPlanTransforms::combineRecipes(VPlan &Plan) {
PostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> POT(
Plan.getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(POT))
- for (VPRecipeBase &R : reverse(*VPBB))
- Worklist.insert(&R);
+ Worklist.insert_range(make_pointer_range(reverse(*VPBB)));
while (!Worklist.empty()) {
auto *Def = dyn_cast<VPSingleDefRecipe>(Worklist.pop_back_val());
>From 9a0ef7d05b842d2bc25ec62e4b58decfadbf6d45 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 1 Sep 2026 21:25:13 +0800
Subject: [PATCH 4/8] Address review comments
- Limit to double initial worklist size
- Use VPSingleDefRecipe type
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 17 +++++++++++------
1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 9a18e968ccaa8..824c5d1b9c4c1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1714,22 +1714,27 @@ static VPSingleDefRecipe *combineRecipe(VPlan &Plan, VPSingleDefRecipe *Def) {
}
void VPlanTransforms::combineRecipes(VPlan &Plan) {
- SetVector<VPRecipeBase *> Worklist;
+ SetVector<VPSingleDefRecipe *> Worklist;
PostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> POT(
Plan.getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(POT))
- Worklist.insert_range(make_pointer_range(reverse(*VPBB)));
+ for (VPRecipeBase &R : reverse(*VPBB))
+ if (auto *Def = dyn_cast<VPSingleDefRecipe>(&R))
+ Worklist.insert(Def);
+
+ [[maybe_unused]] unsigned InitWorklistSize = Worklist.size();
while (!Worklist.empty()) {
- auto *Def = dyn_cast<VPSingleDefRecipe>(Worklist.pop_back_val());
- if (!Def)
- continue;
+ assert(Worklist.size() < InitWorklistSize * 2 &&
+ "Worklist is growing large, possible cycle?");
+ VPSingleDefRecipe *Def = Worklist.pop_back_val();
if (VPSingleDefRecipe *New = combineRecipe(Plan, Def)) {
if (New != Def) {
// Replace the recipe with a new one.
Def->replaceAllUsesWith(New);
Def->eraseFromParent();
- if (VPRecipeBase *NewR = New->getDefiningRecipe())
+ if (auto *NewR = dyn_cast_if_present<VPSingleDefRecipe>(
+ New->getDefiningRecipe()))
Worklist.insert(NewR);
// TODO: Append users to the worklist.
} else if (vputils::isDeadRecipe(*Def)) {
>From e0614e9049c7e7c0e2a0d274b4c4745f67a615e9 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 1 Sep 2026 21:39:28 +0800
Subject: [PATCH 5/8] Switch to SmallVector with space on stack
SmallVector allows for much larger small sizes than SetVector, so use 256 to match InstructionWorklist.
We don't need to worry about duplicate worklist entries until we add users to the worklist.
---
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 824c5d1b9c4c1..cd9821aee3dfc 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1714,13 +1714,13 @@ static VPSingleDefRecipe *combineRecipe(VPlan &Plan, VPSingleDefRecipe *Def) {
}
void VPlanTransforms::combineRecipes(VPlan &Plan) {
- SetVector<VPSingleDefRecipe *> Worklist;
+ SmallVector<VPSingleDefRecipe *, 256> Worklist;
PostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> POT(
Plan.getEntry());
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(POT))
for (VPRecipeBase &R : reverse(*VPBB))
if (auto *Def = dyn_cast<VPSingleDefRecipe>(&R))
- Worklist.insert(Def);
+ Worklist.push_back(Def);
[[maybe_unused]] unsigned InitWorklistSize = Worklist.size();
@@ -1735,8 +1735,8 @@ void VPlanTransforms::combineRecipes(VPlan &Plan) {
Def->eraseFromParent();
if (auto *NewR = dyn_cast_if_present<VPSingleDefRecipe>(
New->getDefiningRecipe()))
- Worklist.insert(NewR);
- // TODO: Append users to the worklist.
+ Worklist.push_back(NewR);
+ // TODO: Append users to the worklist (might need a setvector)
} else if (vputils::isDeadRecipe(*Def)) {
// Recipe was modified - it may be dead now.
Def->eraseFromParent();
>From a91d1ba2ff4ce6ebb909450d991ca1cb3308f322 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 4 Sep 2026 18:43:16 +0800
Subject: [PATCH 6/8] Make then branch non-dead
---
llvm/test/Transforms/LoopVectorize/blend-i1.ll | 16 ++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)
diff --git a/llvm/test/Transforms/LoopVectorize/blend-i1.ll b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
index 03823a06ccd83..bed2556cef650 100644
--- a/llvm/test/Transforms/LoopVectorize/blend-i1.ll
+++ b/llvm/test/Transforms/LoopVectorize/blend-i1.ll
@@ -1,9 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --filter-out-after "^scalar.ph" --version 6
-; RUN: opt -p loop-vectorize -force-vector-width=4 -S %s | FileCheck %s
+; RUN: opt -p loop-vectorize -force-target-supports-masked-memory-ops -force-vector-width=4 -S %s | FileCheck %s
-define void @blend_of_i1_consts(ptr %p, i32 %n) {
+define void @blend_of_i1_consts(ptr %p, ptr noalias %q, i32 %n) {
; CHECK-LABEL: define void @blend_of_i1_consts(
-; CHECK-SAME: ptr [[P:%.*]], i32 [[N:%.*]]) {
+; CHECK-SAME: ptr [[P:%.*]], ptr noalias [[Q:%.*]], i32 [[N:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_SCEVCHECK:.*]]
@@ -18,8 +18,10 @@ define void @blend_of_i1_consts(ptr %p, i32 %n) {
; CHECK: [[VECTOR_BODY]]:
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
-; CHECK-NEXT: [[TMP3:%.*]] = icmp sle <4 x i32> [[VEC_IND]], splat (i32 42)
-; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP3]] to <4 x i32>
+; CHECK-NEXT: [[TMP8:%.*]] = icmp sgt <4 x i32> [[VEC_IND]], splat (i32 42)
+; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[Q]], i32 [[INDEX]]
+; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> zeroinitializer, ptr align 4 [[TMP7]], <4 x i1> [[TMP8]])
+; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP8]] to <4 x i32>
; CHECK-NEXT: [[TMP5:%.*]] = getelementptr i32, ptr [[P]], i32 [[INDEX]]
; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[TMP5]], align 4
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
@@ -40,10 +42,12 @@ loop:
br i1 %c, label %then, label %latch
then:
+ %q.gep = getelementptr i32, ptr %q, i32 %iv
+ store i32 0, ptr %q.gep
br label %latch
latch:
- %phi = phi i1 [ true, %loop ], [ false, %then ]
+ %phi = phi i1 [ false, %loop ], [ true, %then ]
%zext = zext i1 %phi to i32
%gep = getelementptr i32, ptr %p, i32 %iv
store i32 %zext, ptr %gep
>From 885af64a71727b5973e1988e51f87443f36ed193 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Fri, 4 Sep 2026 18:47:29 +0800
Subject: [PATCH 7/8] Unindent loop
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 27 ++++++++++---------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index cd9821aee3dfc..4575564d82b1c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1728,19 +1728,20 @@ void VPlanTransforms::combineRecipes(VPlan &Plan) {
assert(Worklist.size() < InitWorklistSize * 2 &&
"Worklist is growing large, possible cycle?");
VPSingleDefRecipe *Def = Worklist.pop_back_val();
- if (VPSingleDefRecipe *New = combineRecipe(Plan, Def)) {
- if (New != Def) {
- // Replace the recipe with a new one.
- Def->replaceAllUsesWith(New);
- Def->eraseFromParent();
- if (auto *NewR = dyn_cast_if_present<VPSingleDefRecipe>(
- New->getDefiningRecipe()))
- Worklist.push_back(NewR);
- // TODO: Append users to the worklist (might need a setvector)
- } else if (vputils::isDeadRecipe(*Def)) {
- // Recipe was modified - it may be dead now.
- Def->eraseFromParent();
- }
+ VPSingleDefRecipe *New = combineRecipe(Plan, Def);
+ if (!New)
+ continue;
+ if (New != Def) {
+ // Replace the recipe with a new one.
+ Def->replaceAllUsesWith(New);
+ Def->eraseFromParent();
+ if (auto *NewR =
+ dyn_cast_if_present<VPSingleDefRecipe>(New->getDefiningRecipe()))
+ Worklist.push_back(NewR);
+ // TODO: Append users to the worklist (might need a setvector)
+ } else if (vputils::isDeadRecipe(*Def)) {
+ // Recipe was modified - it may be dead now.
+ Def->eraseFromParent();
}
}
}
>From 6015194d0e7f5a535b0ce375f54658d873cdfca9 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 8 Sep 2026 16:58:23 +0800
Subject: [PATCH 8/8] No need to dyn_cast to singledef recipe anymore
---
llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 4575564d82b1c..86bee88ab2b62 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -1735,9 +1735,7 @@ void VPlanTransforms::combineRecipes(VPlan &Plan) {
// Replace the recipe with a new one.
Def->replaceAllUsesWith(New);
Def->eraseFromParent();
- if (auto *NewR =
- dyn_cast_if_present<VPSingleDefRecipe>(New->getDefiningRecipe()))
- Worklist.push_back(NewR);
+ Worklist.push_back(New);
// TODO: Append users to the worklist (might need a setvector)
} else if (vputils::isDeadRecipe(*Def)) {
// Recipe was modified - it may be dead now.
More information about the llvm-commits
mailing list