[llvm] [VPlan] Add simple driver option to run some individual transforms. (PR #178522)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 13 05:53:47 PST 2026


https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/178522

>From 7924d5a48a94ffaa6a4e8859dbd8fa56cced0450 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Sat, 10 Jan 2026 21:55:31 +0000
Subject: [PATCH 1/6] [VPlan] Add simple driver option to run some individual
 transforms.

Add an alternative to test VPlan in more isolation via a new
`vplan-test-transform` option, which builds VPlan0 for each loop in the
input IR and then can invoke a set of transforms on it.

In order to allow different recipe types to be created, a new
widen-from-metadata transform is added, which transforms VPInstructions
to different recipes, based on custom !vplan.widen metadata. Currently
this supports creating widen & replicate recipes, but can easily be
extended in the future.

Currently the handling is intentionally bare-bones, to be extended
gradually as needed.
---
 .../Transforms/Vectorize/LoopVectorize.cpp    | 19 +++++
 .../Vectorize/VPlanConstruction.cpp           | 48 ++++++++++++
 .../Transforms/Vectorize/VPlanTransforms.cpp  | 59 +++++++++++++++
 .../Transforms/Vectorize/VPlanTransforms.h    | 11 +++
 .../LoopVectorize/VPlan/vplan-licm.ll         | 38 ++++++++++
 .../LoopVectorize/VPlan/vplan-merge-blocks.ll | 42 +++++++++++
 .../VPlan/vplan-remove-dead-recipes.ll        | 40 ++++++++++
 .../VPlan/vplan-simplify-recipes.ll           | 35 +++++++++
 .../VPlan/vplan-widen-from-metadata.ll        | 74 +++++++++++++++++++
 9 files changed, 366 insertions(+)
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index bfa41cda621b0..14cf16f279b67 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -377,6 +377,12 @@ static cl::opt<bool> VPlanBuildStressTest(
         "out right after the build (stress test the VPlan H-CFG construction "
         "in the VPlan-native vectorization path)."));
 
+static cl::opt<std::string> VPlanTestTransform(
+    "vplan-test-transform", cl::init(""), cl::Hidden,
+    cl::desc(
+        "Build VPlan and run specified transform(s) for testing. "
+        "Comma-separated list. Example: 'widen-from-metadata,simplify,print'"));
+
 cl::opt<bool> llvm::EnableLoopInterleaving(
     "interleave-loops", cl::init(true), cl::Hidden,
     cl::desc("Enable loop interleaving in Loop vectorization passes"));
@@ -9755,6 +9761,19 @@ bool LoopVectorizePass::processLoop(Loop *L) {
 
   PredicatedScalarEvolution PSE(*SE, *L);
 
+  // VPlan test transform mode: build minimal VPlan and run requested
+  // transforms.
+  if (!VPlanTestTransform.empty()) {
+    auto Plan = VPlanTransforms::buildVPlan0(
+        L, *LI, Type::getInt64Ty(F->getContext()), DebugLoc(), PSE);
+    VPlanTransforms::handleEarlyExits(*Plan, /*HasUncountableExit=*/false);
+    VPlanTransforms::addMiddleCheck(*Plan, /*RequiresScalarEpilogueCheck=*/true,
+                                    /*TailFolded=*/false);
+    VPlanTransforms::createLoopRegions(*Plan);
+    VPlanTransforms::runTestTransforms(*Plan, VPlanTestTransform, TLI);
+    return false;
+  }
+
   // Query this against the original loop and save it here because the profile
   // of the original loop header may change as the transformation happens.
   bool OptForSize = llvm::shouldOptimizeForSize(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 4846abdab3c55..ee35f735b4f69 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -18,6 +18,7 @@
 #include "VPlanDominatorTree.h"
 #include "VPlanPatternMatch.h"
 #include "VPlanTransforms.h"
+#include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/Analysis/LoopInfo.h"
 #include "llvm/Analysis/LoopIterator.h"
 #include "llvm/Analysis/ScalarEvolution.h"
@@ -249,6 +250,11 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
           MD.setMetadata(LLVMContext::MD_noalias, NoAliasMD);
       }
 
+      // Preserve vplan.widen metadata for testing VPlan transforms.
+      unsigned VPlanWidenKind = Inst->getContext().getMDKindID("vplan.widen");
+      if (MDNode *WidenMD = Inst->getMetadata(VPlanWidenKind))
+        MD.setMetadata(VPlanWidenKind, WidenMD);
+
       // Translate LLVM-IR operands into VPValue operands and set them in the
       // new VPInstruction.
       SmallVector<VPValue *, 4> VPOperands;
@@ -1560,3 +1566,45 @@ bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan) {
   }
   return true;
 }
+
+void VPlanTransforms::widenFromMetadata(VPlan &Plan,
+                                        const TargetLibraryInfo *TLI) {
+  unsigned VPlanWidenKind = Plan.getContext().getMDKindID("vplan.widen");
+  VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
+
+  ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
+      VectorRegion);
+  for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(RPOT)) {
+    for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
+      auto *VPI = dyn_cast<VPInstruction>(&R);
+      if (!VPI)
+        continue;
+
+      MDNode *MD = VPI->getMetadata(VPlanWidenKind);
+      if (!MD || MD->getNumOperands() == 0)
+        continue;
+
+      auto *KindMD = dyn_cast<MDString>(MD->getOperand(0));
+      if (!KindMD)
+        continue;
+
+      StringRef Kind = KindMD->getString();
+      VPRecipeBase *NewR = nullptr;
+      if (Kind == "widen") {
+        NewR = new VPWidenRecipe(*VPI->getUnderlyingInstr(), VPI->operands(),
+                                 *VPI, *VPI, VPI->getDebugLoc());
+      } else if (Kind == "replicate") {
+        NewR = new VPReplicateRecipe(VPI->getUnderlyingInstr(), VPI->operands(),
+                                     /*IsSingleScalar=*/false,
+                                     /*Mask=*/nullptr, *VPI, *VPI,
+                                     VPI->getDebugLoc());
+      } else {
+        continue;
+      }
+
+      NewR->insertBefore(VPI);
+      VPI->replaceAllUsesWith(NewR->getVPSingleValue());
+      VPI->eraseFromParent();
+    }
+  }
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 478e72e6d6cfc..9f6ecbd481f23 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -5387,3 +5387,62 @@ void VPlanTransforms::addExitUsersForFirstOrderRecurrences(VPlan &Plan,
     }
   }
 }
+
+namespace {
+/// Uniform signature for test transforms.
+using TestTransformFn = void (*)(VPlan &, const TargetLibraryInfo *);
+
+/// Entry for a VPlan test transform.
+struct TestTransformEntry {
+  StringLiteral Name;
+  TestTransformFn Fn;
+};
+
+} // namespace
+
+// Wrappers to adapt different function signatures to uniform TestTransformFn.
+template <void (*Fn)(VPlan &)>
+static void wrapTransform(VPlan &P, const TargetLibraryInfo *) {
+  Fn(P);
+}
+
+template <bool (*Fn)(VPlan &)>
+static void wrapTransform(VPlan &P, const TargetLibraryInfo *) {
+  Fn(P);
+}
+
+static const TestTransformEntry TestTransforms[] = {
+    {"simplify-recipes", wrapTransform<VPlanTransforms::simplifyRecipes>},
+    {"remove-dead-recipes", wrapTransform<VPlanTransforms::removeDeadRecipes>},
+    {"simplify-blends", wrapTransform<simplifyBlends>},
+    {"merge-blocks", wrapTransform<mergeBlocksIntoPredecessors>},
+    {"licm", wrapTransform<licm>},
+    {"cse", wrapTransform<VPlanTransforms::cse>},
+    {"optimize", wrapTransform<VPlanTransforms::optimize>},
+    {"widen-from-metadata", VPlanTransforms::widenFromMetadata},
+};
+
+static const TestTransformEntry *getTestTransform(StringRef Name) {
+  for (const auto &E : TestTransforms)
+    if (E.Name == Name)
+      return &E;
+  return nullptr;
+}
+
+void VPlanTransforms::runTestTransforms(VPlan &Plan, StringRef Pipeline,
+                                        const TargetLibraryInfo *TLI) {
+  SmallVector<StringRef> Passes;
+  Pipeline.split(Passes, ',');
+
+  for (StringRef PassName : Passes) {
+    PassName = PassName.trim();
+    if (PassName == "print") {
+      dbgs() << "VPlan:\n" << Plan << "\n";
+      continue;
+    }
+    const TestTransformEntry *E = getTestTransform(PassName);
+    if (!E)
+      report_fatal_error("Unknown VPlan test transform: " + PassName);
+    E->Fn(Plan, TLI);
+  }
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index e0d09a099647a..1289b9baed07a 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -440,6 +440,17 @@ struct VPlanTransforms {
   /// users in the original exit block using the VPIRInstruction wrapping to the
   /// LCSSA phi.
   static void addExitUsersForFirstOrderRecurrences(VPlan &Plan, VFRange &Range);
+
+  /// Run test transforms specified by comma-separated \p Pipeline names.
+  /// For testing VPlan transforms in isolation.
+  LLVM_ABI_FOR_TEST static void runTestTransforms(VPlan &Plan,
+                                                  StringRef Pipeline,
+                                                  const TargetLibraryInfo *TLI);
+
+  /// Convert VPInstructions to recipes based on !vplan.widen metadata.
+  /// For testing recipe-level transforms without full legality analysis.
+  LLVM_ABI_FOR_TEST static void widenFromMetadata(VPlan &Plan,
+                                                  const TargetLibraryInfo *TLI);
 };
 
 } // namespace llvm
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
new file mode 100644
index 0000000000000..91a6e62486eb3
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
@@ -0,0 +1,38 @@
+; REQUIRES: asserts
+
+; Test the licm VPlan transform that performs loop-invariant code motion.
+
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,licm,print' -disable-output %s 2>&1 | FileCheck %s
+
+; Check that loop-invariant add is hoisted to vector.ph.
+; CHECK-LABEL: VPlan ' for UF>=1' {
+; CHECK:      vector.ph:
+; CHECK-NEXT:   WIDEN ir<%invariant> = add ir<%N>, ir<100>
+; CHECK-NEXT: Successor(s): vector loop
+; CHECK:      <x1> vector loop: {
+; CHECK:        vector.body:
+; CHECK-NOT:      WIDEN ir<%invariant> = add
+; CHECK:        No successors
+; CHECK:      }
+
+define void @test_licm(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  ; Loop-invariant computation that should be hoisted
+  %invariant = add i64 %N, 100, !vplan.widen !0
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  %add = add i32 %val, 1, !vplan.widen !0
+  store i32 %add, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %invariant
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
new file mode 100644
index 0000000000000..e88cdc4320a15
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
@@ -0,0 +1,42 @@
+; REQUIRES: asserts
+
+; Test the merge-blocks VPlan transform that merges blocks into predecessors.
+; This test uses a loop with early exit to create multiple blocks in VPlan.
+
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,merge-blocks,print' -disable-output %s 2>&1 | FileCheck %s
+
+; Check that continue block is merged into vector.body.
+; CHECK-LABEL: VPlan ' for UF>=1' {
+; CHECK: <x1> vector loop: {
+; CHECK:   vector.body:
+; CHECK:     EMIT ir<%cond> = icmp sgt ir<%val>, ir<100>
+; CHECK-NOT: Successor(s): continue
+; CHECK:     WIDEN ir<%add> = add ir<%val>, ir<1>
+; CHECK:     EMIT branch-on-count
+; CHECK:   No successors
+; CHECK: }
+
+define void @test_merge_blocks(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %continue ]
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  ; Early exit condition
+  %cond = icmp sgt i32 %val, 100
+  br i1 %cond, label %exit, label %continue
+
+continue:
+  %add = add i32 %val, 1, !vplan.widen !0
+  store i32 %add, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %N
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
new file mode 100644
index 0000000000000..eddedafa731f9
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
@@ -0,0 +1,40 @@
+; REQUIRES: asserts
+
+; Test the remove-dead-recipes VPlan transform that removes unused recipes.
+
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,remove-dead-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+
+; Check that dead recipes (%cmp and %4) are removed.
+; CHECK-LABEL: VPlan ' for UF>=1' {
+; CHECK: <x1> vector loop: {
+; CHECK:   vector.body:
+; CHECK:     EMIT vp<[[CAN_IV:%.+]]> = CANONICAL-INDUCTION
+; CHECK:     EMIT ir<%gep> = getelementptr
+; CHECK:     EMIT ir<%val> = load ir<%gep>
+; CHECK:     WIDEN ir<%add> = add ir<%val>, ir<1>
+; CHECK:     EMIT store ir<%add>, ir<%gep>
+; CHECK-NOT: EMIT ir<%cmp> = icmp
+; CHECK-NOT: EMIT vp<{{%.+}}> = not ir<%cmp>
+; CHECK:     EMIT branch-on-count
+; CHECK:   No successors
+; CHECK: }
+
+define void @test_dce(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  %add = add i32 %val, 1, !vplan.widen !0
+  store i32 %add, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %N
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
new file mode 100644
index 0000000000000..9ec5a9607abd4
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
@@ -0,0 +1,35 @@
+; REQUIRES: asserts
+
+; Test the simplify-recipes VPlan transform that simplifies recipe patterns.
+
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,simplify-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+
+; Check that 'or %val, 0' is simplified, so store uses %val directly.
+; CHECK-LABEL: VPlan ' for UF>=1' {
+; CHECK: <x1> vector loop: {
+; CHECK:   vector.body:
+; CHECK:     EMIT ir<%val> = load ir<%gep>
+; CHECK:     EMIT store ir<%val>, ir<%gep>
+; CHECK:   No successors
+; CHECK: }
+
+define void @test_simplify(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  ; Identity: or X, 0 -> X (should be simplified away)
+  %result = or i32 %val, 0, !vplan.widen !0
+  store i32 %result, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %N
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
new file mode 100644
index 0000000000000..b53ce9fdde23a
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
@@ -0,0 +1,74 @@
+; REQUIRES: asserts
+
+; Test the widen-from-metadata VPlan transform that converts VPInstructions to
+; recipes based on !vplan.widen metadata.
+
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=WIDEN
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=REPLICATE
+
+; Test widen conversion: VPInstruction with !vplan.widen !{!"widen"}
+; should be converted to VPWidenRecipe (WIDEN instead of EMIT).
+; WIDEN-LABEL: VPlan ' for UF>=1' {
+; WIDEN: <x1> vector loop: {
+; WIDEN:   vector.body:
+; WIDEN:     EMIT vp<[[CAN_IV:%.+]]> = CANONICAL-INDUCTION
+; WIDEN:     EMIT ir<%gep> = getelementptr
+; WIDEN:     EMIT ir<%val> = load ir<%gep>
+; WIDEN:     WIDEN ir<%add> = add ir<%val>, ir<1>
+; WIDEN:     EMIT store ir<%add>, ir<%gep>
+; WIDEN:     EMIT branch-on-count
+; WIDEN:   No successors
+; WIDEN: }
+
+; Test replicate conversion: VPInstruction with !vplan.widen !{!"replicate"}
+; should be converted to VPReplicateRecipe (REPLICATE instead of EMIT).
+; REPLICATE-LABEL: VPlan ' for UF>=1' {
+; REPLICATE: <x1> vector loop: {
+; REPLICATE:   vector.body:
+; REPLICATE:     EMIT vp<[[CAN_IV:%.+]]> = CANONICAL-INDUCTION
+; REPLICATE:     EMIT ir<%gep> = getelementptr
+; REPLICATE:     EMIT ir<%val> = load ir<%gep>
+; REPLICATE:     REPLICATE ir<%add> = add ir<%val>, ir<1>
+; REPLICATE:     EMIT store ir<%add>, ir<%gep>
+; REPLICATE:     EMIT branch-on-count
+; REPLICATE:   No successors
+; REPLICATE: }
+
+define void @test_widen(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  %add = add i32 %val, 1, !vplan.widen !0
+  store i32 %add, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %N
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+define void @test_replicate(ptr %A, i64 %N) {
+entry:
+  br label %loop
+
+loop:
+  %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+  %gep = getelementptr i32, ptr %A, i64 %iv
+  %val = load i32, ptr %gep
+  %add = add i32 %val, 1, !vplan.widen !1
+  store i32 %add, ptr %gep
+  %iv.next = add i64 %iv, 1
+  %cmp = icmp slt i64 %iv.next, %N
+  br i1 %cmp, label %loop, label %exit
+
+exit:
+  ret void
+}
+
+!0 = !{!"widen"}
+!1 = !{!"replicate"}

>From 3cea23db0b3a891aeee93f99c64ab19b66a0b977 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 30 Jan 2026 16:50:48 +0000
Subject: [PATCH 2/6] !fixup address comments, thanks

---
 .../Transforms/Vectorize/LoopVectorize.cpp    | 32 ++++-----
 .../Transforms/Vectorize/VPlanTransforms.cpp  | 65 ++++++-------------
 .../LoopVectorize/VPlan/lit.local.cfg         |  4 ++
 .../LoopVectorize/VPlan/vplan-licm.ll         |  4 +-
 .../LoopVectorize/VPlan/vplan-merge-blocks.ll |  4 +-
 .../VPlan/vplan-remove-dead-recipes.ll        |  4 +-
 .../VPlan/vplan-simplify-recipes.ll           |  4 +-
 .../VPlan/vplan-widen-from-metadata.ll        |  6 +-
 8 files changed, 45 insertions(+), 78 deletions(-)
 create mode 100644 llvm/test/Transforms/LoopVectorize/VPlan/lit.local.cfg

diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 3e2021bc9d81b..2c3f4f56832db 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -9729,6 +9729,20 @@ bool LoopVectorizePass::processLoop(Loop *L) {
                     << L->getHeader()->getParent()->getName() << "' from "
                     << L->getLocStr() << "\n");
 
+  PredicatedScalarEvolution PSE(*SE, *L);
+
+  // Function containing loop
+  Function *F = L->getHeader()->getParent();
+
+  // VPlan test transform mode: build minimal VPlan and run requested
+  // transforms.
+  if (!VPlanTestTransform.empty()) {
+    auto Plan = VPlanTransforms::buildVPlan0(
+        L, *LI, Type::getInt64Ty(F->getContext()), DebugLoc(), PSE);
+    VPlanTransforms::runTestTransforms(*Plan, VPlanTestTransform, TLI);
+    return false;
+  }
+
   LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE, TTI);
 
   LLVM_DEBUG(
@@ -9742,9 +9756,6 @@ bool LoopVectorizePass::processLoop(Loop *L) {
              << " width=" << Hints.getWidth()
              << " interleave=" << Hints.getInterleave() << "\n");
 
-  // Function containing loop
-  Function *F = L->getHeader()->getParent();
-
   // Looking at the diagnostic output is the only way to determine if a loop
   // was vectorized (other than looking at the IR or machine code), so it
   // is important to generate an optimization remark for each loop. Most of
@@ -9758,21 +9769,6 @@ bool LoopVectorizePass::processLoop(Loop *L) {
     return false;
   }
 
-  PredicatedScalarEvolution PSE(*SE, *L);
-
-  // VPlan test transform mode: build minimal VPlan and run requested
-  // transforms.
-  if (!VPlanTestTransform.empty()) {
-    auto Plan = VPlanTransforms::buildVPlan0(
-        L, *LI, Type::getInt64Ty(F->getContext()), DebugLoc(), PSE);
-    VPlanTransforms::handleEarlyExits(*Plan, /*HasUncountableExit=*/false);
-    VPlanTransforms::addMiddleCheck(*Plan, /*RequiresScalarEpilogueCheck=*/true,
-                                    /*TailFolded=*/false);
-    VPlanTransforms::createLoopRegions(*Plan);
-    VPlanTransforms::runTestTransforms(*Plan, VPlanTestTransform, TLI);
-    return false;
-  }
-
   // Query this against the original loop and save it here because the profile
   // of the original loop header may change as the transformation happens.
   bool OptForSize = llvm::shouldOptimizeForSize(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 06ceec40905a8..9dcab7281a001 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -26,6 +26,7 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SetVector.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Analysis/IVDescriptors.h"
@@ -41,6 +42,7 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/TypeSize.h"
 #include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
+#include <functional>
 
 using namespace llvm;
 using namespace VPlanPatternMatch;
@@ -5390,49 +5392,24 @@ void VPlanTransforms::addExitUsersForFirstOrderRecurrences(VPlan &Plan,
   }
 }
 
-namespace {
-/// Uniform signature for test transforms.
-using TestTransformFn = void (*)(VPlan &, const TargetLibraryInfo *);
-
-/// Entry for a VPlan test transform.
-struct TestTransformEntry {
-  StringLiteral Name;
-  TestTransformFn Fn;
-};
-
-} // namespace
-
-// Wrappers to adapt different function signatures to uniform TestTransformFn.
-template <void (*Fn)(VPlan &)>
-static void wrapTransform(VPlan &P, const TargetLibraryInfo *) {
-  Fn(P);
-}
-
-template <bool (*Fn)(VPlan &)>
-static void wrapTransform(VPlan &P, const TargetLibraryInfo *) {
-  Fn(P);
-}
-
-static const TestTransformEntry TestTransforms[] = {
-    {"simplify-recipes", wrapTransform<VPlanTransforms::simplifyRecipes>},
-    {"remove-dead-recipes", wrapTransform<VPlanTransforms::removeDeadRecipes>},
-    {"simplify-blends", wrapTransform<simplifyBlends>},
-    {"merge-blocks", wrapTransform<mergeBlocksIntoPredecessors>},
-    {"licm", wrapTransform<licm>},
-    {"cse", wrapTransform<VPlanTransforms::cse>},
-    {"optimize", wrapTransform<VPlanTransforms::optimize>},
-    {"widen-from-metadata", VPlanTransforms::widenFromMetadata},
-};
-
-static const TestTransformEntry *getTestTransform(StringRef Name) {
-  for (const auto &E : TestTransforms)
-    if (E.Name == Name)
-      return &E;
-  return nullptr;
-}
-
 void VPlanTransforms::runTestTransforms(VPlan &Plan, StringRef Pipeline,
                                         const TargetLibraryInfo *TLI) {
+  StringMap<std::function<void()>> TestTransforms = {
+      {"cse", std::bind(VPlanTransforms::cse, std::ref(Plan))},
+      {"create-loop-regions",
+       std::bind(VPlanTransforms::createLoopRegions, std::ref(Plan))},
+      {"simplify-recipes",
+       std::bind(VPlanTransforms::simplifyRecipes, std::ref(Plan))},
+      {"remove-dead-recipes",
+       std::bind(VPlanTransforms::removeDeadRecipes, std::ref(Plan))},
+      {"simplify-blends", std::bind(simplifyBlends, std::ref(Plan))},
+      {"merge-blocks", std::bind(mergeBlocksIntoPredecessors, std::ref(Plan))},
+      {"licm", std::bind(licm, std::ref(Plan))},
+      {"optimize", std::bind(VPlanTransforms::optimize, std::ref(Plan))},
+      {"widen-from-metadata",
+       std::bind(VPlanTransforms::widenFromMetadata, std::ref(Plan), TLI)},
+  };
+
   SmallVector<StringRef> Passes;
   Pipeline.split(Passes, ',');
 
@@ -5442,9 +5419,9 @@ void VPlanTransforms::runTestTransforms(VPlan &Plan, StringRef Pipeline,
       dbgs() << "VPlan:\n" << Plan << "\n";
       continue;
     }
-    const TestTransformEntry *E = getTestTransform(PassName);
-    if (!E)
+    auto It = TestTransforms.find(PassName);
+    if (It == TestTransforms.end())
       report_fatal_error("Unknown VPlan test transform: " + PassName);
-    E->Fn(Plan, TLI);
+    It->second();
   }
 }
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/lit.local.cfg b/llvm/test/Transforms/LoopVectorize/VPlan/lit.local.cfg
new file mode 100644
index 0000000000000..9861880d9b5bb
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/lit.local.cfg
@@ -0,0 +1,4 @@
+config.suffixes = [".ll"]
+
+if not 'asserts' in config.available_features:
+    config.unsupported = True
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
index 91a6e62486eb3..0f92bfa8a52c6 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
@@ -1,8 +1,6 @@
-; REQUIRES: asserts
-
 ; Test the licm VPlan transform that performs loop-invariant code motion.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,licm,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,licm,print' -disable-output %s 2>&1 | FileCheck %s
 
 ; Check that loop-invariant add is hoisted to vector.ph.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
index e88cdc4320a15..fe8b2ec45336c 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
@@ -1,9 +1,7 @@
-; REQUIRES: asserts
-
 ; Test the merge-blocks VPlan transform that merges blocks into predecessors.
 ; This test uses a loop with early exit to create multiple blocks in VPlan.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,merge-blocks,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,merge-blocks,print' -disable-output %s 2>&1 | FileCheck %s
 
 ; Check that continue block is merged into vector.body.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
index eddedafa731f9..732b24fa9f067 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
@@ -1,8 +1,6 @@
-; REQUIRES: asserts
-
 ; Test the remove-dead-recipes VPlan transform that removes unused recipes.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,remove-dead-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,remove-dead-recipes,print' -disable-output %s 2>&1 | FileCheck %s
 
 ; Check that dead recipes (%cmp and %4) are removed.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
index 9ec5a9607abd4..2952b5f8efed2 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
@@ -1,8 +1,6 @@
-; REQUIRES: asserts
-
 ; Test the simplify-recipes VPlan transform that simplifies recipe patterns.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,simplify-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,simplify-recipes,print' -disable-output %s 2>&1 | FileCheck %s
 
 ; Check that 'or %val, 0' is simplified, so store uses %val directly.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
index b53ce9fdde23a..000a78b925694 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
@@ -1,10 +1,8 @@
-; REQUIRES: asserts
-
 ; Test the widen-from-metadata VPlan transform that converts VPInstructions to
 ; recipes based on !vplan.widen metadata.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=WIDEN
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=REPLICATE
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=WIDEN
+; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=REPLICATE
 
 ; Test widen conversion: VPInstruction with !vplan.widen !{!"widen"}
 ; should be converted to VPWidenRecipe (WIDEN instead of EMIT).

>From 6068d782a10b799be8df22575d6ba37418c9697a Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 30 Jan 2026 19:48:31 +0000
Subject: [PATCH 3/6] !fixup use !vplan.replicate explicitly

---
 .../Vectorize/VPlanConstruction.cpp           | 22 +++++++++----------
 .../LoopVectorize/VPlan/vplan-licm.ll         |  8 +++----
 .../LoopVectorize/VPlan/vplan-merge-blocks.ll |  6 ++---
 .../VPlan/vplan-remove-dead-recipes.ll        |  6 ++---
 .../VPlan/vplan-simplify-recipes.ll           |  6 ++---
 .../VPlan/vplan-widen-from-metadata.ll        | 17 ++++++--------
 6 files changed, 26 insertions(+), 39 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index ee35f735b4f69..3f708e6ec5c20 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -250,10 +250,15 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
           MD.setMetadata(LLVMContext::MD_noalias, NoAliasMD);
       }
 
-      // Preserve vplan.widen metadata for testing VPlan transforms.
+      // Preserve vplan.widen and vplan.replicate metadata for testing VPlan
+      // transforms.
       unsigned VPlanWidenKind = Inst->getContext().getMDKindID("vplan.widen");
       if (MDNode *WidenMD = Inst->getMetadata(VPlanWidenKind))
         MD.setMetadata(VPlanWidenKind, WidenMD);
+      unsigned VPlanReplicateKind =
+          Inst->getContext().getMDKindID("vplan.replicate");
+      if (MDNode *ReplicateMD = Inst->getMetadata(VPlanReplicateKind))
+        MD.setMetadata(VPlanReplicateKind, ReplicateMD);
 
       // Translate LLVM-IR operands into VPValue operands and set them in the
       // new VPInstruction.
@@ -1570,6 +1575,8 @@ bool VPlanTransforms::handleMultiUseReductions(VPlan &Plan) {
 void VPlanTransforms::widenFromMetadata(VPlan &Plan,
                                         const TargetLibraryInfo *TLI) {
   unsigned VPlanWidenKind = Plan.getContext().getMDKindID("vplan.widen");
+  unsigned VPlanReplicateKind =
+      Plan.getContext().getMDKindID("vplan.replicate");
   VPRegionBlock *VectorRegion = Plan.getVectorLoopRegion();
 
   ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<VPBlockBase *>> RPOT(
@@ -1580,20 +1587,11 @@ void VPlanTransforms::widenFromMetadata(VPlan &Plan,
       if (!VPI)
         continue;
 
-      MDNode *MD = VPI->getMetadata(VPlanWidenKind);
-      if (!MD || MD->getNumOperands() == 0)
-        continue;
-
-      auto *KindMD = dyn_cast<MDString>(MD->getOperand(0));
-      if (!KindMD)
-        continue;
-
-      StringRef Kind = KindMD->getString();
       VPRecipeBase *NewR = nullptr;
-      if (Kind == "widen") {
+      if (VPI->getMetadata(VPlanWidenKind)) {
         NewR = new VPWidenRecipe(*VPI->getUnderlyingInstr(), VPI->operands(),
                                  *VPI, *VPI, VPI->getDebugLoc());
-      } else if (Kind == "replicate") {
+      } else if (VPI->getMetadata(VPlanReplicateKind)) {
         NewR = new VPReplicateRecipe(VPI->getUnderlyingInstr(), VPI->operands(),
                                      /*IsSingleScalar=*/false,
                                      /*Mask=*/nullptr, *VPI, *VPI,
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
index 0f92bfa8a52c6..8e0ae454289e3 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-licm.ll
@@ -1,6 +1,6 @@
 ; Test the licm VPlan transform that performs loop-invariant code motion.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,licm,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata;licm>' -disable-output %s | FileCheck %s
 
 ; Check that loop-invariant add is hoisted to vector.ph.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
@@ -20,10 +20,10 @@ entry:
 loop:
   %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
   ; Loop-invariant computation that should be hoisted
-  %invariant = add i64 %N, 100, !vplan.widen !0
+  %invariant = add i64 %N, 100, !vplan.widen !{}
   %gep = getelementptr i32, ptr %A, i64 %iv
   %val = load i32, ptr %gep
-  %add = add i32 %val, 1, !vplan.widen !0
+  %add = add i32 %val, 1, !vplan.widen !{}
   store i32 %add, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %invariant
@@ -32,5 +32,3 @@ loop:
 exit:
   ret void
 }
-
-!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
index fe8b2ec45336c..bce6a4d6c98f5 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-merge-blocks.ll
@@ -1,7 +1,7 @@
 ; Test the merge-blocks VPlan transform that merges blocks into predecessors.
 ; This test uses a loop with early exit to create multiple blocks in VPlan.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,merge-blocks,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata;merge-blocks>' -disable-output %s | FileCheck %s
 
 ; Check that continue block is merged into vector.body.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
@@ -27,7 +27,7 @@ loop:
   br i1 %cond, label %exit, label %continue
 
 continue:
-  %add = add i32 %val, 1, !vplan.widen !0
+  %add = add i32 %val, 1, !vplan.widen !{}
   store i32 %add, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %N
@@ -36,5 +36,3 @@ continue:
 exit:
   ret void
 }
-
-!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
index 732b24fa9f067..ac4288c6ba630 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-remove-dead-recipes.ll
@@ -1,6 +1,6 @@
 ; Test the remove-dead-recipes VPlan transform that removes unused recipes.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,remove-dead-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata;remove-dead-recipes>' -disable-output %s | FileCheck %s
 
 ; Check that dead recipes (%cmp and %4) are removed.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
@@ -25,7 +25,7 @@ loop:
   %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
   %gep = getelementptr i32, ptr %A, i64 %iv
   %val = load i32, ptr %gep
-  %add = add i32 %val, 1, !vplan.widen !0
+  %add = add i32 %val, 1, !vplan.widen !{}
   store i32 %add, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %N
@@ -34,5 +34,3 @@ loop:
 exit:
   ret void
 }
-
-!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
index 2952b5f8efed2..d78c6512d9aee 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-simplify-recipes.ll
@@ -1,6 +1,6 @@
 ; Test the simplify-recipes VPlan transform that simplifies recipe patterns.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,simplify-recipes,print' -disable-output %s 2>&1 | FileCheck %s
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata;simplify-recipes>' -disable-output %s | FileCheck %s
 
 ; Check that 'or %val, 0' is simplified, so store uses %val directly.
 ; CHECK-LABEL: VPlan ' for UF>=1' {
@@ -20,7 +20,7 @@ loop:
   %gep = getelementptr i32, ptr %A, i64 %iv
   %val = load i32, ptr %gep
   ; Identity: or X, 0 -> X (should be simplified away)
-  %result = or i32 %val, 0, !vplan.widen !0
+  %result = or i32 %val, 0, !vplan.widen !{}
   store i32 %result, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %N
@@ -29,5 +29,3 @@ loop:
 exit:
   ret void
 }
-
-!0 = !{!"widen"}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
index 000a78b925694..f91b37d215b94 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/vplan-widen-from-metadata.ll
@@ -1,10 +1,10 @@
 ; Test the widen-from-metadata VPlan transform that converts VPInstructions to
-; recipes based on !vplan.widen metadata.
+; recipes based on !vplan.widen and !vplan.replicate metadata.
 
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=WIDEN
-; RUN: opt -passes=loop-vectorize -vplan-test-transform='create-loop-regions,widen-from-metadata,print' -disable-output %s 2>&1 | FileCheck %s --check-prefix=REPLICATE
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata>' -disable-output %s | FileCheck %s --check-prefix=WIDEN
+; RUN: opt -passes='vplan-test<create-loop-regions;widen-from-metadata>' -disable-output %s | FileCheck %s --check-prefix=REPLICATE
 
-; Test widen conversion: VPInstruction with !vplan.widen !{!"widen"}
+; Test widen conversion: VPInstruction with !vplan.widen
 ; should be converted to VPWidenRecipe (WIDEN instead of EMIT).
 ; WIDEN-LABEL: VPlan ' for UF>=1' {
 ; WIDEN: <x1> vector loop: {
@@ -18,7 +18,7 @@
 ; WIDEN:   No successors
 ; WIDEN: }
 
-; Test replicate conversion: VPInstruction with !vplan.widen !{!"replicate"}
+; Test replicate conversion: VPInstruction with !vplan.replicate
 ; should be converted to VPReplicateRecipe (REPLICATE instead of EMIT).
 ; REPLICATE-LABEL: VPlan ' for UF>=1' {
 ; REPLICATE: <x1> vector loop: {
@@ -40,7 +40,7 @@ loop:
   %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
   %gep = getelementptr i32, ptr %A, i64 %iv
   %val = load i32, ptr %gep
-  %add = add i32 %val, 1, !vplan.widen !0
+  %add = add i32 %val, 1, !vplan.widen !{}
   store i32 %add, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %N
@@ -58,7 +58,7 @@ loop:
   %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
   %gep = getelementptr i32, ptr %A, i64 %iv
   %val = load i32, ptr %gep
-  %add = add i32 %val, 1, !vplan.widen !1
+  %add = add i32 %val, 1, !vplan.replicate !{}
   store i32 %add, ptr %gep
   %iv.next = add i64 %iv, 1
   %cmp = icmp slt i64 %iv.next, %N
@@ -67,6 +67,3 @@ loop:
 exit:
   ret void
 }
-
-!0 = !{!"widen"}
-!1 = !{!"replicate"}

>From 27e2bbbe84cb764989d51a344e9310d0abcdf00e Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 30 Jan 2026 19:55:38 +0000
Subject: [PATCH 4/6] !fixup run via test pass

---
 .../llvm/Transforms/Vectorize/VPlanTestPass.h | 36 +++++++++++++
 llvm/lib/Passes/PassBuilder.cpp               |  7 +++
 llvm/lib/Passes/PassRegistry.def              |  4 ++
 llvm/lib/Transforms/Vectorize/CMakeLists.txt  |  1 +
 .../Transforms/Vectorize/LoopVectorize.cpp    | 25 ++-------
 .../Vectorize/VPlanConstruction.cpp           |  7 +--
 .../Transforms/Vectorize/VPlanTestPass.cpp    | 53 +++++++++++++++++++
 .../Transforms/Vectorize/VPlanTransforms.cpp  |  9 +---
 .../Transforms/Vectorize/VPlanTransforms.h    |  3 +-
 9 files changed, 113 insertions(+), 32 deletions(-)
 create mode 100644 llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
 create mode 100644 llvm/lib/Transforms/Vectorize/VPlanTestPass.cpp

diff --git a/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h b/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
new file mode 100644
index 0000000000000..54350a6ace6b7
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
@@ -0,0 +1,36 @@
+//===- VPlanTestPass.h - Test VPlan transforms ----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass is a lightweight testing harness for VPlan transforms. It builds
+// VPlan0 for loops and runs specified transforms.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_VECTORIZE_VPLANTESTPASS_H
+#define LLVM_TRANSFORMS_VECTORIZE_VPLANTESTPASS_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+
+class VPlanTestPass : public PassInfoMixin<VPlanTestPass> {
+  std::string TransformPipeline;
+
+public:
+  VPlanTestPass(std::string Pipeline = "")
+      : TransformPipeline(std::move(Pipeline)) {}
+
+  PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
+
+  void printPipeline(raw_ostream &OS,
+                     function_ref<StringRef(StringRef)> MapClassName2PassName);
+};
+
+} // end namespace llvm
+
+#endif // LLVM_TRANSFORMS_VECTORIZE_VPLANTESTPASS_H
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 8bb78c8c7df63..a96943b4ee427 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -381,6 +381,7 @@
 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
 #include "llvm/Transforms/Vectorize/SandboxVectorizer/SandboxVectorizer.h"
+#include "llvm/Transforms/Vectorize/VPlanTestPass.h"
 #include "llvm/Transforms/Vectorize/VectorCombine.h"
 #include <optional>
 
@@ -502,6 +503,12 @@ static Expected<OptimizationLevel> parseOptLevelParam(StringRef S) {
       inconvertibleErrorCode());
 }
 
+static Expected<std::string> parseVPlanTestPassOptions(StringRef Params) {
+  // VPlanTestPass accepts a semicolon-separated list of transform names, e.g.
+  // "widen-from-metadata;licm"
+  return Params.str();
+}
+
 PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
                          std::optional<PGOOptions> PGOOpt,
                          PassInstrumentationCallbacks *PIC,
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 2cfb5b2592601..92cfd2474c16b 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -577,6 +577,10 @@ FUNCTION_PASS_WITH_PARAMS(
     "cfguard", "CFGuardPass",
     [](CFGuardPass::Mechanism M) { return CFGuardPass(M); },
     parseCFGuardPassOptions, "check;dispatch")
+FUNCTION_PASS_WITH_PARAMS(
+    "vplan-test", "VPlanTestPass",
+    [](std::string Pipeline) { return VPlanTestPass(std::move(Pipeline)); },
+    parseVPlanTestPassOptions, "")
 FUNCTION_PASS_WITH_PARAMS(
     "early-cse", "EarlyCSEPass",
     [](bool UseMemorySSA) { return EarlyCSEPass(UseMemorySSA); },
diff --git a/llvm/lib/Transforms/Vectorize/CMakeLists.txt b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
index 9f4a242214471..19c0cf13e983b 100644
--- a/llvm/lib/Transforms/Vectorize/CMakeLists.txt
+++ b/llvm/lib/Transforms/Vectorize/CMakeLists.txt
@@ -25,6 +25,7 @@ add_llvm_component_library(LLVMVectorize
   VPlan.cpp
   VPlanAnalysis.cpp
   VPlanConstruction.cpp
+  VPlanTestPass.cpp
   VPlanPredicator.cpp
   VPlanRecipes.cpp
   VPlanSLP.cpp
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 2c3f4f56832db..90664b2416896 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -377,12 +377,6 @@ static cl::opt<bool> VPlanBuildStressTest(
         "out right after the build (stress test the VPlan H-CFG construction "
         "in the VPlan-native vectorization path)."));
 
-static cl::opt<std::string> VPlanTestTransform(
-    "vplan-test-transform", cl::init(""), cl::Hidden,
-    cl::desc(
-        "Build VPlan and run specified transform(s) for testing. "
-        "Comma-separated list. Example: 'widen-from-metadata,simplify,print'"));
-
 cl::opt<bool> llvm::EnableLoopInterleaving(
     "interleave-loops", cl::init(true), cl::Hidden,
     cl::desc("Enable loop interleaving in Loop vectorization passes"));
@@ -9729,20 +9723,6 @@ bool LoopVectorizePass::processLoop(Loop *L) {
                     << L->getHeader()->getParent()->getName() << "' from "
                     << L->getLocStr() << "\n");
 
-  PredicatedScalarEvolution PSE(*SE, *L);
-
-  // Function containing loop
-  Function *F = L->getHeader()->getParent();
-
-  // VPlan test transform mode: build minimal VPlan and run requested
-  // transforms.
-  if (!VPlanTestTransform.empty()) {
-    auto Plan = VPlanTransforms::buildVPlan0(
-        L, *LI, Type::getInt64Ty(F->getContext()), DebugLoc(), PSE);
-    VPlanTransforms::runTestTransforms(*Plan, VPlanTestTransform, TLI);
-    return false;
-  }
-
   LoopVectorizeHints Hints(L, InterleaveOnlyWhenForced, *ORE, TTI);
 
   LLVM_DEBUG(
@@ -9756,6 +9736,9 @@ bool LoopVectorizePass::processLoop(Loop *L) {
              << " width=" << Hints.getWidth()
              << " interleave=" << Hints.getInterleave() << "\n");
 
+  // Function containing loop
+  Function *F = L->getHeader()->getParent();
+
   // Looking at the diagnostic output is the only way to determine if a loop
   // was vectorized (other than looking at the IR or machine code), so it
   // is important to generate an optimization remark for each loop. Most of
@@ -9769,6 +9752,8 @@ bool LoopVectorizePass::processLoop(Loop *L) {
     return false;
   }
 
+  PredicatedScalarEvolution PSE(*SE, *L);
+
   // Query this against the original loop and save it here because the profile
   // of the original loop header may change as the transformation happens.
   bool OptForSize = llvm::shouldOptimizeForSize(
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 3f708e6ec5c20..278a3adb48f7d 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -180,6 +180,10 @@ VPValue *PlainCFGBuilder::getOrCreateVPOperand(Value *IRVal) {
 void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
                                                   BasicBlock *BB) {
   VPIRBuilder.setInsertPoint(VPBB);
+  unsigned VPlanWidenKind = BB->getContext().getMDKindID("vplan.widen");
+  unsigned VPlanReplicateKind = BB->getContext().getMDKindID("vplan.replicate");
+          
+
   // TODO: Model and preserve debug intrinsics in VPlan.
   for (Instruction &InstRef : BB->instructionsWithoutDebug(false)) {
     Instruction *Inst = &InstRef;
@@ -252,11 +256,8 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
 
       // Preserve vplan.widen and vplan.replicate metadata for testing VPlan
       // transforms.
-      unsigned VPlanWidenKind = Inst->getContext().getMDKindID("vplan.widen");
       if (MDNode *WidenMD = Inst->getMetadata(VPlanWidenKind))
         MD.setMetadata(VPlanWidenKind, WidenMD);
-      unsigned VPlanReplicateKind =
-          Inst->getContext().getMDKindID("vplan.replicate");
       if (MDNode *ReplicateMD = Inst->getMetadata(VPlanReplicateKind))
         MD.setMetadata(VPlanReplicateKind, ReplicateMD);
 
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTestPass.cpp b/llvm/lib/Transforms/Vectorize/VPlanTestPass.cpp
new file mode 100644
index 0000000000000..fc24b9c34136d
--- /dev/null
+++ b/llvm/lib/Transforms/Vectorize/VPlanTestPass.cpp
@@ -0,0 +1,53 @@
+//===- VPlanTestPass.cpp - Test VPlan transforms -------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass is a lightweight testing harness for VPlan transforms. It builds
+// VPlan0 for loops and runs specified transforms.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Vectorize/VPlanTestPass.h"
+#include "VPlan.h"
+#include "VPlanTransforms.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/Analysis/ScalarEvolution.h"
+#include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/IR/DebugLoc.h"
+#include "llvm/IR/PassManager.h"
+
+using namespace llvm;
+
+PreservedAnalyses VPlanTestPass::run(Function &F, FunctionAnalysisManager &AM) {
+  if (TransformPipeline.empty())
+    return PreservedAnalyses::all();
+
+  auto &LI = AM.getResult<LoopAnalysis>(F);
+  auto &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
+  auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
+
+  for (Loop *L : LI) {
+    PredicatedScalarEvolution PSE(SE, *L);
+
+    auto Plan = VPlanTransforms::buildVPlan0(
+        L, LI, Type::getInt64Ty(F.getContext()), DebugLoc(), PSE);
+
+    VPlanTransforms::runTestTransforms(*Plan, TransformPipeline, &TLI);
+    outs() << *Plan << "\n";
+  }
+
+  return PreservedAnalyses::all();
+}
+
+void VPlanTestPass::printPipeline(
+    raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
+  static_cast<PassInfoMixin<VPlanTestPass> *>(this)->printPipeline(
+      OS, MapClassName2PassName);
+  OS << '<';
+  OS << TransformPipeline;
+  OS << '>';
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 9dcab7281a001..8a0cd7570661c 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -26,8 +26,8 @@
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SetOperations.h"
 #include "llvm/ADT/SetVector.h"
-#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/SmallPtrSet.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Analysis/IVDescriptors.h"
 #include "llvm/Analysis/InstSimplifyFolder.h"
@@ -5411,14 +5411,9 @@ void VPlanTransforms::runTestTransforms(VPlan &Plan, StringRef Pipeline,
   };
 
   SmallVector<StringRef> Passes;
-  Pipeline.split(Passes, ',');
+  Pipeline.split(Passes, ';');
 
   for (StringRef PassName : Passes) {
-    PassName = PassName.trim();
-    if (PassName == "print") {
-      dbgs() << "VPlan:\n" << Plan << "\n";
-      continue;
-    }
     auto It = TestTransforms.find(PassName);
     if (It == TestTransforms.end())
       report_fatal_error("Unknown VPlan test transform: " + PassName);
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index 1289b9baed07a..bdaf0c1d9747f 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -441,8 +441,7 @@ struct VPlanTransforms {
   /// LCSSA phi.
   static void addExitUsersForFirstOrderRecurrences(VPlan &Plan, VFRange &Range);
 
-  /// Run test transforms specified by comma-separated \p Pipeline names.
-  /// For testing VPlan transforms in isolation.
+  /// Run test transforms specified by semi-colon-separated \p Pipeline names.
   LLVM_ABI_FOR_TEST static void runTestTransforms(VPlan &Plan,
                                                   StringRef Pipeline,
                                                   const TargetLibraryInfo *TLI);

>From 8d58f6e0358cf036bb90caf6be1c93894c7e3dab Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 9 Feb 2026 21:14:05 +0000
Subject: [PATCH 5/6] !fixup fix formatting

---
 llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 235ad5d5fb396..c64b12886bd62 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -185,7 +185,6 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
   VPIRBuilder.setInsertPoint(VPBB);
   unsigned VPlanWidenKind = BB->getContext().getMDKindID("vplan.widen");
   unsigned VPlanReplicateKind = BB->getContext().getMDKindID("vplan.replicate");
-          
 
   // TODO: Model and preserve debug intrinsics in VPlan.
   for (Instruction &InstRef : BB->instructionsWithoutDebug(false)) {

>From abd78998fecbc25601c025dab7c1ee418b7179b6 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Fri, 13 Feb 2026 13:53:12 +0000
Subject: [PATCH 6/6] !fixp use StringRef, thanks

---
 llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h | 5 ++---
 llvm/lib/Passes/PassBuilder.cpp                        | 6 ------
 llvm/lib/Passes/PassRegistry.def                       | 4 ++--
 3 files changed, 4 insertions(+), 11 deletions(-)

diff --git a/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h b/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
index 54350a6ace6b7..22ed7d53ae48c 100644
--- a/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
+++ b/llvm/include/llvm/Transforms/Vectorize/VPlanTestPass.h
@@ -19,11 +19,10 @@
 namespace llvm {
 
 class VPlanTestPass : public PassInfoMixin<VPlanTestPass> {
-  std::string TransformPipeline;
+  StringRef TransformPipeline;
 
 public:
-  VPlanTestPass(std::string Pipeline = "")
-      : TransformPipeline(std::move(Pipeline)) {}
+  VPlanTestPass(StringRef Pipeline = "") : TransformPipeline(Pipeline) {}
 
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
 
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 5963f0d74115f..7eb4628976131 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -504,12 +504,6 @@ static Expected<OptimizationLevel> parseOptLevelParam(StringRef S) {
       inconvertibleErrorCode());
 }
 
-static Expected<std::string> parseVPlanTestPassOptions(StringRef Params) {
-  // VPlanTestPass accepts a semicolon-separated list of transform names, e.g.
-  // "widen-from-metadata;licm"
-  return Params.str();
-}
-
 PassBuilder::PassBuilder(TargetMachine *TM, PipelineTuningOptions PTO,
                          std::optional<PGOOptions> PGOOpt,
                          PassInstrumentationCallbacks *PIC,
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 92cfd2474c16b..ea1e5802a9a90 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -579,8 +579,8 @@ FUNCTION_PASS_WITH_PARAMS(
     parseCFGuardPassOptions, "check;dispatch")
 FUNCTION_PASS_WITH_PARAMS(
     "vplan-test", "VPlanTestPass",
-    [](std::string Pipeline) { return VPlanTestPass(std::move(Pipeline)); },
-    parseVPlanTestPassOptions, "")
+    [](StringRef Pipeline) { return VPlanTestPass(Pipeline); },
+    [](StringRef Params) -> Expected<StringRef> { return Params; }, "")
 FUNCTION_PASS_WITH_PARAMS(
     "early-cse", "EarlyCSEPass",
     [](bool UseMemorySSA) { return EarlyCSEPass(UseMemorySSA); },



More information about the llvm-commits mailing list