From f3f767992172026e68ff5d9297bb8c17c1ddbf83 Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Tue, 2 Dec 2025 10:45:57 +0000 Subject: [PATCH 1/2] [LV] Add test for cost of select VPInstruction. --- .../LoopVectorize/AArch64/select-costs.ll | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll index 403fc9f316d35..f4f07512f6b4d 100644 --- a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll +++ b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll @@ -73,3 +73,26 @@ exit: %1 = select i1 %all.off, i32 1, i32 %0 ret i32 %1 } + +define i32 @select_vpinst_for_tail_folding(i8 %n) { +; CHECK: LV: Checking a loop in 'select_vpinst_for_tail_folding' +; CHECK: Cost of 6 for VF 2: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> +; CHECK: Cost of 12 for VF 4: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> +; CHECK: LV: Selecting VF: 1 + +entry: + %c = icmp ne i8 %n, 0 + %ext = zext i1 %c to i32 + br label %loop + +loop: + %iv = phi i32 [ %ext, %entry ], [ %iv.next, %loop ] + %red = phi i32 [ 0, %entry ], [ %red.next, %loop ] + %iv.next = add i32 %iv, 1 + %red.next = mul i32 %red, %iv + %ec = icmp eq i32 %iv, 12 + br i1 %ec, label %exit, label %loop + +exit: + ret i32 %red.next +} From d7fa21f1149c9f42a37c5d22b2c449deda4f658d Mon Sep 17 00:00:00 2001 From: Florian Hahn Date: Tue, 2 Dec 2025 11:08:53 +0000 Subject: [PATCH 2/2] [VPlan] Use predicate in VPInstruction::computeCost for selects. In some cases, the lowering a select depends on the predicate. If the condition of a select is a compare instruction, thread the predicate through to the TTI hook. --- llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp | 5 ++--- llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp index 6491a2ce6813b..61d4ea110ee9c 100644 --- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp +++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp @@ -1008,9 +1008,8 @@ InstructionCost VPInstruction::computeCost(ElementCount VF, switch (getOpcode()) { case Instruction::Select: { - // TODO: It may be possible to improve this by analyzing where the - // condition operand comes from. - CmpInst::Predicate Pred = CmpInst::BAD_ICMP_PREDICATE; + llvm::CmpPredicate Pred = CmpInst::BAD_ICMP_PREDICATE; + match(getOperand(0), m_Cmp(Pred, m_VPValue(), m_VPValue())); auto *CondTy = Ctx.Types.inferScalarType(getOperand(0)); auto *VecTy = Ctx.Types.inferScalarType(getOperand(1)); if (!vputils::onlyFirstLaneUsed(this)) { diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll index f4f07512f6b4d..20409f66fc51f 100644 --- a/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll +++ b/llvm/test/Transforms/LoopVectorize/AArch64/select-costs.ll @@ -76,9 +76,9 @@ exit: define i32 @select_vpinst_for_tail_folding(i8 %n) { ; CHECK: LV: Checking a loop in 'select_vpinst_for_tail_folding' -; CHECK: Cost of 6 for VF 2: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> -; CHECK: Cost of 12 for VF 4: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> -; CHECK: LV: Selecting VF: 1 +; CHECK: Cost of 1 for VF 2: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> +; CHECK: Cost of 1 for VF 4: EMIT vp<{{.+}}> = select vp<{{.+}}>, ir<%red.next>, ir<%red> +; CHECK: LV: Selecting VF: 4 entry: %c = icmp ne i8 %n, 0