[llvm] [VPlan] Generalize licm-sink to strip zero-user bail (PR #187077)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 25 04:55:46 PDT 2026
https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/187077
>From b9f5adb4703a2c403b45ef580f099dcbf652b22e Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Tue, 17 Mar 2026 16:44:27 +0000
Subject: [PATCH] [VPlan] Generalize licm-sink to strip zero-user bail
In preparation to sink unpredicated stores, strip the zero-user bail in
the licm transform, generalizing it to sink to all successors of the
vector loop. The patch doesn't make real functional changes, because
there are never users of a recipe in different successors of the vector
loop region.
---
.../Transforms/Vectorize/VPlanTransforms.cpp | 67 ++++++++++---------
.../LoopVectorize/AArch64/select-costs.ll | 8 +--
.../{sink-to-early-exit.ll => licm-sink.ll} | 52 +++++++++++++-
3 files changed, 89 insertions(+), 38 deletions(-)
rename llvm/test/Transforms/LoopVectorize/{sink-to-early-exit.ll => licm-sink.ll} (61%)
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index cfc973afc7fd4..5dc3f90488a70 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -2730,9 +2730,8 @@ static void licm(VPlan &Plan) {
#ifndef NDEBUG
VPDominatorTree VPDT(Plan);
#endif
- // Sink recipes with no users inside the vector loop region if all users are
- // in the same exit block of the region.
- // TODO: Extend to sink recipes from inner loops.
+ // Sink recipes with in the vector loop region to successors of the loop
+ // region.
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
vp_post_order_shallow(LoopRegion->getEntry()))) {
for (VPRecipeBase &R : make_early_inc_range(reverse(*VPBB))) {
@@ -2747,43 +2746,47 @@ static void licm(VPlan &Plan) {
// TODO: Use R.definedValues() instead of casting to VPSingleDefRecipe to
// support recipes with multiple defined values (e.g., interleaved loads).
auto *Def = cast<VPSingleDefRecipe>(&R);
- // Skip recipes without users as we cannot determine a sink block.
- // TODO: Clone sinkable recipes without users to all exit blocks to reduce
- // their execution frequency.
- if (Def->getNumUsers() == 0)
- continue;
- VPBasicBlock *SinkBB = nullptr;
- // Cannot sink the recipe if any user
- // * is defined in any loop region, or
- // * is a phi, or
- // * multiple users in different blocks.
- if (any_of(Def->users(), [&SinkBB](VPUser *U) {
+ // Cannot sink the recipe if the user is defined in a loop region or a
+ // non-successor of the vector loop region. Cannot sink if user is a phi
+ // either.
+ if (any_of(Def->users(), [&LoopRegion](VPUser *U) {
auto *UserR = cast<VPRecipeBase>(U);
VPBasicBlock *Parent = UserR->getParent();
// TODO: If the user is a PHI node, we should check the block of
// incoming value. Support PHI node users if needed.
- if (UserR->isPhi() || Parent->getEnclosingLoopRegion())
- return true;
- // TODO: Support sinking when users are in multiple blocks.
- if (SinkBB && SinkBB != Parent)
- return true;
- SinkBB = Parent;
- return false;
+ return UserR->isPhi() || Parent->getEnclosingLoopRegion() ||
+ Parent->getSinglePredecessor() != LoopRegion;
}))
continue;
- // Only sink to dedicated exit blocks of the loop region.
- if (SinkBB->getSinglePredecessor() != LoopRegion)
- continue;
-
- // TODO: This will need to be a check instead of a assert after
- // conditional branches in vectorized loops are supported.
- assert(VPDT.properlyDominates(VPBB, SinkBB) &&
- "Defining block must dominate sink block");
- // TODO: Clone the recipe if users are on multiple exit paths, instead of
- // just moving.
- Def->moveBefore(*SinkBB, SinkBB->getFirstNonPhi());
+ // Compute the users' parent blocks if there are users. Otherwise, sink to
+ // all successor blocks.
+ SmallVector<VPBasicBlock *> SinkBlocks;
+ if (Def->users().empty())
+ append_range(SinkBlocks, map_range(LoopRegion->successors(),
+ [](VPBlockBase *SuccBB) {
+ return cast<VPBasicBlock>(SuccBB);
+ }));
+ else
+ append_range(SinkBlocks, map_range(Def->users(), [](VPUser *U) {
+ return cast<VPRecipeBase>(U)->getParent();
+ }));
+
+ for (auto *SinkTo : SinkBlocks) {
+ // This will need to be a check instead of a assert after conditional
+ // branches in vectorized loops are supported.
+ assert(VPDT.properlyDominates(VPBB, SinkTo) &&
+ "Defining block must dominate sink block");
+ auto *Clone = Def->clone();
+ Clone->insertBefore(*SinkTo, SinkTo->getFirstNonPhi());
+ Def->replaceUsesWithIf(Clone, [&SinkTo](VPUser &U, unsigned) {
+ return cast<VPRecipeBase>(U).getParent() == SinkTo;
+ });
+ }
+ // Avoid erroneously bailing on the already-sunk recipe when we get to the
+ // next recipe in post-order and query its users.
+ Def->eraseFromParent();
}
}
}
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
index add503acb9420..5a36042ad19b3 100644
--- a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll
@@ -92,10 +92,10 @@ exit:
define i32 @select_xor_cond(ptr %src, i1 %c.0) {
; CHECK: LV: Checking a loop in 'select_xor_cond'
-; CHECK: Cost of 1 for VF 2: WIDEN ir<%sel> = select ir<%c>, ir<false>, ir<%c.0>
-; CHECK: Cost of 1 for VF 4: WIDEN ir<%sel> = select ir<%c>, ir<false>, ir<%c.0>
-; CHECK: Cost of 1 for VF 8: WIDEN ir<%sel> = select ir<%c>, ir<false>, ir<%c.0>
-; CHECK: Cost of 1 for VF 16: WIDEN ir<%sel> = select ir<%c>, ir<false>, ir<%c.0>
+; CHECK: Cost of 1 for VF 2: WIDEN ir<%sel> = select ir<%c>{{.*}}, ir<false>, ir<%c.0>
+; CHECK: Cost of 1 for VF 4: WIDEN ir<%sel> = select ir<%c>{{.*}}, ir<false>, ir<%c.0>
+; CHECK: Cost of 1 for VF 8: WIDEN ir<%sel> = select ir<%c>{{.*}}, ir<false>, ir<%c.0>
+; CHECK: Cost of 1 for VF 16: WIDEN ir<%sel> = select ir<%c>{{.*}}, ir<false>, ir<%c.0>
; CHECK: LV: Selecting VF: 4.
entry:
diff --git a/llvm/test/Transforms/LoopVectorize/sink-to-early-exit.ll b/llvm/test/Transforms/LoopVectorize/licm-sink.ll
similarity index 61%
rename from llvm/test/Transforms/LoopVectorize/sink-to-early-exit.ll
rename to llvm/test/Transforms/LoopVectorize/licm-sink.ll
index d40aac7f3b138..d4c58e667c08e 100644
--- a/llvm/test/Transforms/LoopVectorize/sink-to-early-exit.ll
+++ b/llvm/test/Transforms/LoopVectorize/licm-sink.ll
@@ -1,8 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
; RUN: opt -S < %s -p loop-vectorize -force-vector-width=4 | FileCheck %s
-; TODO: Recipe used only in the early exit block can be sunk there.
-
declare void @init_mem(ptr, i64)
define i64 @sink_to_early_exit(i64 %offset) {
@@ -74,3 +72,53 @@ loop.early.exit:
loop.end:
ret i64 0
}
+
+define i32 @sink_to_middle_block(ptr %src, i1 %c.0) {
+; CHECK-LABEL: define i32 @sink_to_middle_block(
+; CHECK-SAME: ptr [[SRC:%.*]], i1 [[C_0:%.*]]) {
+; CHECK-NEXT: [[ENTRY:.*:]]
+; CHECK-NEXT: br label %[[VECTOR_PH:.*]]
+; CHECK: [[VECTOR_PH]]:
+; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i1> poison, i1 [[C_0]], i64 0
+; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i1> [[BROADCAST_SPLATINSERT]], <4 x i1> poison, <4 x i32> zeroinitializer
+; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
+; CHECK: [[VECTOR_BODY]]:
+; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; CHECK-NEXT: [[TMP0:%.*]] = getelementptr i8, ptr [[SRC]], i64 [[INDEX]]
+; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i8>, ptr [[TMP0]], align 1
+; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
+; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i64 [[INDEX_NEXT]], 20
+; CHECK-NEXT: br i1 [[TMP1]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; CHECK: [[MIDDLE_BLOCK]]:
+; CHECK-NEXT: [[TMP2:%.*]] = icmp eq <4 x i8> [[WIDE_LOAD]], zeroinitializer
+; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[TMP2]], <4 x i1> zeroinitializer, <4 x i1> [[BROADCAST_SPLAT]]
+; CHECK-NEXT: [[TMP4:%.*]] = zext <4 x i1> [[TMP3]] to <4 x i32>
+; CHECK-NEXT: [[VECTOR_RECUR_EXTRACT_FOR_PHI:%.*]] = extractelement <4 x i32> [[TMP4]], i32 2
+; CHECK-NEXT: [[TMP5:%.*]] = extractelement <4 x i1> [[TMP2]], i32 3
+; CHECK-NEXT: br label %[[EXIT:.*]]
+; CHECK: [[EXIT]]:
+; CHECK-NEXT: [[EXT_C:%.*]] = zext i1 [[TMP5]] to i32
+; CHECK-NEXT: [[RES:%.*]] = add i32 [[EXT_C]], [[VECTOR_RECUR_EXTRACT_FOR_PHI]]
+; CHECK-NEXT: ret i32 [[RES]]
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+ %p = phi i32 [ 0, %entry ], [ %ext.sel, %loop ]
+ %gep.src = getelementptr i8, ptr %src, i64 %iv
+ %0 = load i8, ptr %gep.src, align 1
+ %c = icmp eq i8 %0, 0
+ %not.c = xor i1 %c, true
+ %sel = select i1 %not.c, i1 %c.0, i1 false
+ %ext.sel = zext i1 %sel to i32
+ %iv.next = add i64 %iv, 1
+ %ec = icmp eq i64 %iv, 19
+ br i1 %ec, label %exit, label %loop
+
+exit:
+ %ext.c = zext i1 %c to i32
+ %res = add i32 %ext.c, %p
+ ret i32 %res
+}
More information about the llvm-commits
mailing list