[llvm-branch-commits] [llvm] release/22.x: [VPlan] Account for any-of costs in legacy cost model (PR #202771)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Jun 12 12:11:07 PDT 2026


https://github.com/dyung updated https://github.com/llvm/llvm-project/pull/202771

>From e80beda6e2558f03f17351f6805e95c8687a82a9 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 12 Mar 2026 21:51:23 +0000
Subject: [PATCH] [VPlan] Account for any-of costs in legacy cost model

Some VPlan transforms, like vectorizing fmin without fast-math,
introduce AnyOfs, which have costs assigned in the VPlan-based cost
model, but not the legacy cost model. Account for their cost like done
for other similar VPInstrctions, like EVL.

Fixes https://github.com/llvm/llvm-project/issues/185867.

(cherry picked from commit 475cc4fe0b4065775db470bb512c9c9142242e55)
---
 .../Transforms/Vectorize/LoopVectorize.cpp    |  1 +
 .../LoopVectorize/X86/cost-any-of.ll          | 41 +++++++++++++++++++
 2 files changed, 42 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/X86/cost-any-of.ll

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index cdc6ecfa21bcb..77589e06d9c33 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -4282,6 +4282,7 @@ VectorizationFactor LoopVectorizationPlanner::selectVectorizationFactor() {
             break;
           }
           case VPInstruction::ExplicitVectorLength:
+          case VPInstruction::AnyOf:
             C += VPI->cost(VF, CostCtx);
             break;
           default:
diff --git a/llvm/test/Transforms/LoopVectorize/X86/cost-any-of.ll b/llvm/test/Transforms/LoopVectorize/X86/cost-any-of.ll
new file mode 100644
index 0000000000000..a03ae83a8d617
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/X86/cost-any-of.ll
@@ -0,0 +1,41 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -passes=loop-vectorize -S %s | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; Test case for https://github.com/llvm/llvm-project/issues/185867.
+define void @fminnum_with_any_of_cost(ptr %p) #0 {
+; CHECK-LABEL: define void @fminnum_with_any_of_cost(
+; CHECK-SAME: ptr [[P:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:  [[SCALAR_PH:.*]]:
+; CHECK-NEXT:    br label %[[LOOP:.*]]
+; CHECK:       [[LOOP]]:
+; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ 0, %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[RED:%.*]] = phi float [ 0.000000e+00, %[[SCALAR_PH]] ], [ [[MIN:%.*]], %[[LOOP]] ]
+; CHECK-NEXT:    [[GEP:%.*]] = getelementptr inbounds float, ptr [[P]], i64 [[IV]]
+; CHECK-NEXT:    [[LDV:%.*]] = load float, ptr [[GEP]], align 4
+; CHECK-NEXT:    [[MIN]] = tail call float @llvm.minnum.f32(float [[RED]], float [[LDV]])
+; CHECK-NEXT:    [[IV_NEXT]] = add i64 [[IV]], 1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i64 [[IV_NEXT]], 140
+; CHECK-NEXT:    br i1 [[CMP]], label %[[EXIT:.*]], label %[[LOOP]]
+; CHECK:       [[EXIT]]:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %red = phi float [ 0.000000e+00, %entry ], [ %min, %loop ]
+  %gep = getelementptr inbounds float, ptr %p, i64 %iv
+  %ldv = load float, ptr %gep, align 4
+  %min = tail call float @llvm.minnum.f32(float %red, float %ldv)
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp eq i64 %iv.next, 140
+  br i1 %cmp, label %exit, label %loop
+
+exit:
+  ret void
+}
+
+attributes #0 = { "target-cpu"="znver4" }



More information about the llvm-branch-commits mailing list