https://github.com/lukel97 updated https://github.com/llvm/llvm-project/pull/235022
>From 65fac40bdb0b9d8c63d19e69461c62eccfca4303 Mon Sep 17 00:00:00 2001 From: Luke Lau <[email protected]> Date: Fri, 23 Oct 2026 20:43:23 +0800 Subject: [PATCH] [VPlan] Move extract-lane(0, canWideIV) combine before unrolling Some extract-lane combines can't be performed before unrolling, e.g. the one that combines it to Instruction::ExtractElement can end up extracting from a different part. But the extract-lane(0, canWideIV) can because at index 0 you're always extracting from the first part. Do this before unrolling, which improves codegen of unrolled code. --- .../Transforms/Vectorize/VPlanTransforms.cpp | 12 +++--- .../VPlan/early_exit_with_stores_vplan.ll | 40 ++++++++----------- .../interleave_uncountable_exits.ll | 16 ++------ 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp index b4c30755ee9714..45664fe57e7242 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp @@ -1360,6 +1360,12 @@ static VPValue *simplifyRecipe(VPlan &Plan, VPSingleDefRecipe *Def) { A->getScalarType() == Def->getScalarType()) return A; + // Replace extract-lane(0, canonical-WIDEN-INDUCTION) with the region's + // scalar canonical IV. + VPWidenIntOrFpInductionRecipe *WidenIV; + if (match(Def, m_ExtractLane(m_ZeroInt(), m_CanonicalWidenIV(WidenIV)))) + return WidenIV->getRegion()->getCanonicalIV(); + // Some simplifications can only be applied after unrolling. Perform them // below. if (!Plan.isUnrolled()) @@ -1371,12 +1377,6 @@ static VPValue *simplifyRecipe(VPlan &Plan, VPSingleDefRecipe *Def) { vputils::isSingleScalar(Def->getOperand(1))) return Def->getOperand(1); - // Replace extract-lane(0, canonical-WIDEN-INDUCTION) with the region's - // scalar canonical IV. - VPWidenIntOrFpInductionRecipe *WidenIV; - if (match(Def, m_ExtractLane(m_ZeroInt(), m_CanonicalWidenIV(WidenIV)))) - return WidenIV->getRegion()->getCanonicalIV(); - // Simplify unrolled VectorPointer without offset, or with zero offset, to // just the pointer operand. if (auto *VPR = dyn_cast<VPVectorPointerRecipe>(Def)) diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/early_exit_with_stores_vplan.ll b/llvm/test/Transforms/LoopVectorize/VPlan/early_exit_with_stores_vplan.ll index ed3ead1afd1bcd..80abe86a071c10 100644 --- a/llvm/test/Transforms/LoopVectorize/VPlan/early_exit_with_stores_vplan.ll +++ b/llvm/test/Transforms/LoopVectorize/VPlan/early_exit_with_stores_vplan.ll @@ -21,7 +21,6 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; CHECK-NEXT: vp<[[VP3:%[0-9]+]]> = CANONICAL-IV ; CHECK-EMPTY: ; CHECK-NEXT: vector.body: -; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: vp<[[VP4:%[0-9]+]]> = SCALAR-STEPS vp<[[VP3]]>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: CLONE ir<%ee.addr> = getelementptr inbounds nuw ir<%pred>, vp<[[VP4]]> ; CHECK-NEXT: vp<[[VP5:%[0-9]+]]> = vector-pointer inbounds nuw i16, ir<%ee.addr>, ir<1> @@ -44,17 +43,16 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; CHECK-EMPTY: ; CHECK-NEXT: middle.block: ; CHECK-NEXT: EMIT vp<[[VP12:%[0-9]+]]> = first-active-lane vp<[[VP6]]> -; CHECK-NEXT: EMIT vp<[[VP13:%[0-9]+]]> = extract-lane ir<0>, ir<%iv> -; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = add vp<[[VP13]]>, vp<[[VP12]]> -; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = icmp eq vp<[[VP14]]>, ir<20> -; CHECK-NEXT: EMIT branch-on-cond vp<[[VP15]]> +; CHECK-NEXT: EMIT vp<[[VP13:%[0-9]+]]> = add vp<[[VP3]]>, vp<[[VP12]]> +; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = icmp eq vp<[[VP13]]>, ir<20> +; CHECK-NEXT: EMIT branch-on-cond vp<[[VP14]]> ; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<exit>: ; CHECK-NEXT: No successors ; CHECK-EMPTY: ; CHECK-NEXT: scalar.ph: -; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP14]]>, middle.block ], [ ir<0>, ir-bb<entry> ] +; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP13]]>, middle.block ], [ ir<0>, ir-bb<entry> ] ; CHECK-NEXT: Successor(s): ir-bb<for.body> ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<for.body>: @@ -110,7 +108,6 @@ define void @loop_contains_store_after_uncountable_exit(ptr dereferenceable(40) ; CHECK-NEXT: vp<[[VP3:%[0-9]+]]> = CANONICAL-IV ; CHECK-EMPTY: ; CHECK-NEXT: vector.body: -; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: vp<[[VP4:%[0-9]+]]> = SCALAR-STEPS vp<[[VP3]]>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: CLONE ir<%ee.addr> = getelementptr inbounds nuw ir<%pred>, vp<[[VP4]]> ; CHECK-NEXT: vp<[[VP5:%[0-9]+]]> = vector-pointer inbounds nuw i16, ir<%ee.addr>, ir<1> @@ -133,17 +130,16 @@ define void @loop_contains_store_after_uncountable_exit(ptr dereferenceable(40) ; CHECK-EMPTY: ; CHECK-NEXT: middle.block: ; CHECK-NEXT: EMIT vp<[[VP12:%[0-9]+]]> = first-active-lane vp<[[VP6]]> -; CHECK-NEXT: EMIT vp<[[VP13:%[0-9]+]]> = extract-lane ir<0>, ir<%iv> -; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = add vp<[[VP13]]>, vp<[[VP12]]> -; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = icmp eq vp<[[VP14]]>, ir<20> -; CHECK-NEXT: EMIT branch-on-cond vp<[[VP15]]> +; CHECK-NEXT: EMIT vp<[[VP13:%[0-9]+]]> = add vp<[[VP3]]>, vp<[[VP12]]> +; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = icmp eq vp<[[VP13]]>, ir<20> +; CHECK-NEXT: EMIT branch-on-cond vp<[[VP14]]> ; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<exit>: ; CHECK-NEXT: No successors ; CHECK-EMPTY: ; CHECK-NEXT: scalar.ph: -; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP14]]>, middle.block ], [ ir<0>, ir-bb<entry> ] +; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP13]]>, middle.block ], [ ir<0>, ir-bb<entry> ] ; CHECK-NEXT: Successor(s): ir-bb<for.body> ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<for.body>: @@ -195,7 +191,6 @@ define i16 @uncountable_exit_with_live_out(ptr dereferenceable(40) noalias %arra ; CHECK-NEXT: vp<[[VP3:%[0-9]+]]> = CANONICAL-IV ; CHECK-EMPTY: ; CHECK-NEXT: vector.body: -; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: vp<[[VP4:%[0-9]+]]> = SCALAR-STEPS vp<[[VP3]]>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: CLONE ir<%ee.addr> = getelementptr inbounds nuw ir<%pred>, vp<[[VP4]]> ; CHECK-NEXT: vp<[[VP5:%[0-9]+]]> = vector-pointer inbounds nuw i16, ir<%ee.addr>, ir<1> @@ -220,10 +215,9 @@ define i16 @uncountable_exit_with_live_out(ptr dereferenceable(40) noalias %arra ; CHECK-NEXT: EMIT vp<[[VP12:%[0-9]+]]> = extract-last-part ir<%data> ; CHECK-NEXT: EMIT vp<[[VP13:%[0-9]+]]> = extract-last-lane vp<[[VP12]]> ; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = first-active-lane vp<[[VP6]]> -; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = extract-lane ir<0>, ir<%iv> -; CHECK-NEXT: EMIT vp<[[VP16:%[0-9]+]]> = add vp<[[VP15]]>, vp<[[VP14]]> -; CHECK-NEXT: EMIT vp<[[VP17:%[0-9]+]]> = icmp eq vp<[[VP16]]>, ir<20> -; CHECK-NEXT: EMIT branch-on-cond vp<[[VP17]]> +; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = add vp<[[VP3]]>, vp<[[VP14]]> +; CHECK-NEXT: EMIT vp<[[VP16:%[0-9]+]]> = icmp eq vp<[[VP15]]>, ir<20> +; CHECK-NEXT: EMIT branch-on-cond vp<[[VP16]]> ; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<exit>: @@ -231,7 +225,7 @@ define i16 @uncountable_exit_with_live_out(ptr dereferenceable(40) noalias %arra ; CHECK-NEXT: No successors ; CHECK-EMPTY: ; CHECK-NEXT: scalar.ph: -; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP16]]>, middle.block ], [ ir<0>, ir-bb<entry> ] +; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP15]]>, middle.block ], [ ir<0>, ir-bb<entry> ] ; CHECK-NEXT: Successor(s): ir-bb<for.body> ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<for.body>: @@ -287,7 +281,6 @@ define void @combined_exit_conditions(ptr align 4 dereferenceable(80) readonly % ; CHECK-NEXT: vp<[[VP3:%[0-9]+]]> = CANONICAL-IV ; CHECK-EMPTY: ; CHECK-NEXT: vector.body: -; CHECK-NEXT: ir<%iv> = WIDEN-INDUCTION nuw nsw ir<0>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: vp<[[VP4:%[0-9]+]]> = SCALAR-STEPS vp<[[VP3]]>, ir<1>, vp<[[VP0]]> ; CHECK-NEXT: CLONE ir<%ee.ptr> = getelementptr inbounds nuw ir<%pred>, vp<[[VP4]]> ; CHECK-NEXT: vp<[[VP5:%[0-9]+]]> = vector-pointer inbounds nuw i32, ir<%ee.ptr>, ir<1> @@ -313,17 +306,16 @@ define void @combined_exit_conditions(ptr align 4 dereferenceable(80) readonly % ; CHECK-EMPTY: ; CHECK-NEXT: middle.block: ; CHECK-NEXT: EMIT vp<[[VP14:%[0-9]+]]> = first-active-lane vp<[[VP7]]> -; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = extract-lane ir<0>, ir<%iv> -; CHECK-NEXT: EMIT vp<[[VP16:%[0-9]+]]> = add vp<[[VP15]]>, vp<[[VP14]]> -; CHECK-NEXT: EMIT vp<[[VP17:%[0-9]+]]> = icmp eq vp<[[VP16]]>, ir<20> -; CHECK-NEXT: EMIT branch-on-cond vp<[[VP17]]> +; CHECK-NEXT: EMIT vp<[[VP15:%[0-9]+]]> = add vp<[[VP3]]>, vp<[[VP14]]> +; CHECK-NEXT: EMIT vp<[[VP16:%[0-9]+]]> = icmp eq vp<[[VP15]]>, ir<20> +; CHECK-NEXT: EMIT branch-on-cond vp<[[VP16]]> ; CHECK-NEXT: Successor(s): ir-bb<exit>, scalar.ph ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<exit>: ; CHECK-NEXT: No successors ; CHECK-EMPTY: ; CHECK-NEXT: scalar.ph: -; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP16]]>, middle.block ], [ ir<0>, ir-bb<entry> ] +; CHECK-NEXT: EMIT-SCALAR vp<%bc.resume.val> = phi [ vp<[[VP15]]>, middle.block ], [ ir<0>, ir-bb<entry> ] ; CHECK-NEXT: Successor(s): ir-bb<for.body> ; CHECK-EMPTY: ; CHECK-NEXT: ir-bb<for.body>: diff --git a/llvm/test/Transforms/LoopVectorize/interleave_uncountable_exits.ll b/llvm/test/Transforms/LoopVectorize/interleave_uncountable_exits.ll index e2d87f66648dd2..bc20670a126038 100644 --- a/llvm/test/Transforms/LoopVectorize/interleave_uncountable_exits.ll +++ b/llvm/test/Transforms/LoopVectorize/interleave_uncountable_exits.ll @@ -11,8 +11,6 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; CHECK-NEXT: br label %[[VECTOR_BODY:.*]] ; CHECK: [[VECTOR_BODY]]: ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ] -; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ] -; CHECK-NEXT: [[STEP_ADD:%.*]] = add nuw <4 x i64> [[VEC_IND]], splat (i64 4) ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds nuw i16, ptr [[PRED]], i64 [[INDEX]] ; CHECK-NEXT: [[TMP13:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP1]], i64 8 ; CHECK-NEXT: [[WIDE_LOAD:%.*]] = load <4 x i16>, ptr [[TMP1]], align 2 @@ -40,7 +38,6 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; CHECK-NEXT: [[TMP6:%.*]] = call i1 @llvm.vector.reduce.or.v4i1(<4 x i1> [[TMP12]]) ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8 ; CHECK-NEXT: [[TMP7:%.*]] = icmp eq i64 [[INDEX_NEXT]], 16 -; CHECK-NEXT: [[VEC_IND_NEXT]] = add nuw nsw <4 x i64> [[STEP_ADD]], splat (i64 4) ; CHECK-NEXT: [[TMP8:%.*]] = or i1 [[TMP6]], [[TMP7]] ; CHECK-NEXT: br i1 [[TMP8]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]] ; CHECK: [[MIDDLE_BLOCK]]: @@ -50,10 +47,7 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; CHECK-NEXT: [[TMP19:%.*]] = add i64 0, [[TMP18]] ; CHECK-NEXT: [[TMP20:%.*]] = icmp ne i64 [[TMP18]], 4 ; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP20]], i64 [[TMP19]], i64 [[TMP17]] -; CHECK-NEXT: [[TMP26:%.*]] = extractelement <4 x i64> [[VEC_IND]], i64 0 -; CHECK-NEXT: [[TMP27:%.*]] = extractelement <4 x i64> [[STEP_ADD]], i64 -4 -; CHECK-NEXT: [[TMP25:%.*]] = select i1 false, i64 [[TMP27]], i64 [[TMP26]] -; CHECK-NEXT: [[TMP9:%.*]] = add i64 [[TMP25]], [[TMP3]] +; CHECK-NEXT: [[TMP9:%.*]] = add i64 [[INDEX]], [[TMP3]] ; CHECK-NEXT: [[TMP10:%.*]] = icmp eq i64 [[TMP9]], 20 ; CHECK-NEXT: br i1 [[TMP10]], label %[[EXIT:.*]], label %[[SCALAR_PH:.*]] ; CHECK: [[SCALAR_PH]]: @@ -83,9 +77,8 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; VF1IC2-NEXT: br label %[[VECTOR_BODY:.*]] ; VF1IC2: [[VECTOR_BODY]]: ; VF1IC2-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[PRED_STORE_CONTINUE2:.*]] ] -; VF1IC2-NEXT: [[TMP0:%.*]] = add i64 [[INDEX]], 0 ; VF1IC2-NEXT: [[TMP1:%.*]] = add i64 [[INDEX]], 1 -; VF1IC2-NEXT: [[TMP2:%.*]] = getelementptr inbounds nuw i16, ptr [[PRED]], i64 [[TMP0]] +; VF1IC2-NEXT: [[TMP2:%.*]] = getelementptr inbounds nuw i16, ptr [[PRED]], i64 [[INDEX]] ; VF1IC2-NEXT: [[TMP3:%.*]] = getelementptr inbounds nuw i16, ptr [[PRED]], i64 [[TMP1]] ; VF1IC2-NEXT: [[TMP4:%.*]] = load i16, ptr [[TMP2]], align 2 ; VF1IC2-NEXT: [[TMP5:%.*]] = load i16, ptr [[TMP3]], align 2 @@ -98,7 +91,7 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; VF1IC2-NEXT: [[TMP12:%.*]] = xor i1 [[TMP11]], true ; VF1IC2-NEXT: br i1 [[TMP10]], label %[[PRED_STORE_IF:.*]], label %[[PRED_STORE_CONTINUE:.*]] ; VF1IC2: [[PRED_STORE_IF]]: -; VF1IC2-NEXT: [[TMP13:%.*]] = getelementptr inbounds nuw i16, ptr [[ARRAY]], i64 [[TMP0]] +; VF1IC2-NEXT: [[TMP13:%.*]] = getelementptr inbounds nuw i16, ptr [[ARRAY]], i64 [[INDEX]] ; VF1IC2-NEXT: [[TMP14:%.*]] = load i16, ptr [[TMP13]], align 2 ; VF1IC2-NEXT: [[TMP15:%.*]] = add nsw i16 [[TMP14]], 1 ; VF1IC2-NEXT: store i16 [[TMP15]], ptr [[TMP13]], align 2 @@ -126,8 +119,7 @@ define void @loop_contains_store_condition_load_has_single_user(ptr dereferencea ; VF1IC2-NEXT: [[TMP27:%.*]] = add i64 0, [[TMP26]] ; VF1IC2-NEXT: [[TMP28:%.*]] = icmp ne i64 [[TMP26]], 1 ; VF1IC2-NEXT: [[TMP29:%.*]] = select i1 [[TMP28]], i64 [[TMP27]], i64 [[TMP24]] -; VF1IC2-NEXT: [[TMP30:%.*]] = select i1 false, i64 [[TMP1]], i64 [[TMP0]] -; VF1IC2-NEXT: [[TMP31:%.*]] = add i64 [[TMP30]], [[TMP29]] +; VF1IC2-NEXT: [[TMP31:%.*]] = add i64 [[INDEX]], [[TMP29]] ; VF1IC2-NEXT: [[TMP32:%.*]] = icmp eq i64 [[TMP31]], 20 ; VF1IC2-NEXT: br i1 [[TMP32]], label %[[EXIT:.*]], label %[[SCALAR_PH:.*]] ; VF1IC2: [[SCALAR_PH]]: _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
