[llvm] [VPlan] Cleanup and generalize VPPhiAccessors CastInfo (NFC) (PR #190027)

Ramkumar Ramachandra via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 14:52:16 PDT 2026


https://github.com/artagnon updated https://github.com/llvm/llvm-project/pull/190027

>From 70c3b9ff22b90a6789fb4b7f39b5a139522be8ee Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 1 Apr 2026 20:43:50 +0100
Subject: [PATCH 1/3] [VPlan] Cleanup and generalize VPPhiAccessors CastInfo
 (NFC)

---
 llvm/lib/Transforms/Vectorize/VPlan.h | 64 ++++++++++++---------------
 1 file changed, 29 insertions(+), 35 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index ab47d927942db..cd3469020beea 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4151,50 +4151,44 @@ class LLVM_ABI_FOR_TEST VPScalarIVStepsRecipe : public VPRecipeWithIRFlags {
 #endif
 };
 
-/// Casting from VPRecipeBase -> VPPhiAccessors is supported for all recipe
-/// types implementing VPPhiAccessors. Used by isa<> & co.
-template <> struct CastIsPossible<VPPhiAccessors, const VPRecipeBase *> {
-  static inline bool isPossible(const VPRecipeBase *f) {
+/// Support casting from VPRecipeBase -> VPPhiAccessors.
+struct CastInfoVPPhiAccessors {
+  // Used by isa.
+  static inline bool isPossible(VPRecipeBase *R) {
     // TODO: include VPPredInstPHIRecipe too, once it implements VPPhiAccessors.
-    return isa<VPIRPhi, VPHeaderPHIRecipe, VPWidenPHIRecipe, VPPhi>(f);
+    return isa<VPPhi, VPIRPhi, VPWidenPHIRecipe, VPHeaderPHIRecipe>(R);
   }
-};
-/// Support casting from VPRecipeBase -> VPPhiAccessors, by down-casting to the
-/// recipe types implementing VPPhiAccessors. Used by cast<>, dyn_cast<> & co.
-template <typename SrcTy>
-struct CastInfoVPPhiAccessors : public CastIsPossible<VPPhiAccessors, SrcTy> {
-
-  using Self = CastInfo<VPPhiAccessors, SrcTy>;
-
-  /// doCast is used by cast<>.
-  static inline VPPhiAccessors *doCast(SrcTy R) {
-    return const_cast<VPPhiAccessors *>([R]() -> const VPPhiAccessors * {
-      switch (R->getVPRecipeID()) {
-      case VPRecipeBase::VPInstructionSC:
-        return cast<VPPhi>(R);
-      case VPRecipeBase::VPIRInstructionSC:
-        return cast<VPIRPhi>(R);
-      case VPRecipeBase::VPWidenPHISC:
-        return cast<VPWidenPHIRecipe>(R);
-      default:
-        return cast<VPHeaderPHIRecipe>(R);
-      }
-    }());
+  /// Used by cast.
+  static inline VPPhiAccessors *doCast(VPRecipeBase *R) {
+    switch (R->getVPRecipeID()) {
+    case VPRecipeBase::VPInstructionSC:
+      return cast<VPPhi>(R);
+    case VPRecipeBase::VPIRInstructionSC:
+      return cast<VPIRPhi>(R);
+    case VPRecipeBase::VPWidenPHISC:
+      return cast<VPWidenPHIRecipe>(R);
+    default:
+      return cast<VPHeaderPHIRecipe>(R);
+    }
   }
-
-  /// doCastIfPossible is used by dyn_cast<>.
-  static inline VPPhiAccessors *doCastIfPossible(SrcTy f) {
-    if (!Self::isPossible(f))
+  /// Used by dyn_cast.
+  static inline VPPhiAccessors *doCastIfPossible(VPRecipeBase *R) {
+    if (!isPossible(R))
       return nullptr;
-    return doCast(f);
+    return doCast(R);
   }
 };
 template <>
-struct CastInfo<VPPhiAccessors, VPRecipeBase *>
-    : CastInfoVPPhiAccessors<VPRecipeBase *> {};
+struct CastInfo<VPPhiAccessors, VPRecipeBase *> : CastInfoVPPhiAccessors {};
 template <>
 struct CastInfo<VPPhiAccessors, const VPRecipeBase *>
-    : CastInfoVPPhiAccessors<const VPRecipeBase *> {};
+    : public ConstStrippingForwardingCast<
+          VPPhiAccessors, const VPRecipeBase *,
+          CastInfo<VPPhiAccessors, VPRecipeBase *>> {};
+template <>
+struct CastInfo<VPPhiAccessors, VPRecipeBase>
+    : public ForwardToPointerCast<VPPhiAccessors, VPRecipeBase *,
+                                  CastInfo<VPPhiAccessors, VPRecipeBase *>> {};
 
 /// Casting from (const) VPRecipeBase -> (const) VPIRMetadata is supported for
 /// all recipe types implementing VPIRMetadata. Used by isa<> & co.

>From 0407b45ed618b2519eca9d893de313cc89095f82 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 1 Apr 2026 22:25:07 +0100
Subject: [PATCH 2/3] [VPlan] Inline CastInfo

---
 llvm/lib/Transforms/Vectorize/VPlan.h | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index cd3469020beea..49e93c0942d9a 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4152,7 +4152,7 @@ class LLVM_ABI_FOR_TEST VPScalarIVStepsRecipe : public VPRecipeWithIRFlags {
 };
 
 /// Support casting from VPRecipeBase -> VPPhiAccessors.
-struct CastInfoVPPhiAccessors {
+template <> struct CastInfo<VPPhiAccessors, VPRecipeBase *> {
   // Used by isa.
   static inline bool isPossible(VPRecipeBase *R) {
     // TODO: include VPPredInstPHIRecipe too, once it implements VPPhiAccessors.
@@ -4178,8 +4178,7 @@ struct CastInfoVPPhiAccessors {
     return doCast(R);
   }
 };
-template <>
-struct CastInfo<VPPhiAccessors, VPRecipeBase *> : CastInfoVPPhiAccessors {};
+
 template <>
 struct CastInfo<VPPhiAccessors, const VPRecipeBase *>
     : public ConstStrippingForwardingCast<

>From 7400864c06d3c7ebd9dd0a1acb4ee384e527eff7 Mon Sep 17 00:00:00 2001
From: Ramkumar Ramachandra <artagnon at tenstorrent.com>
Date: Wed, 1 Apr 2026 22:50:31 +0100
Subject: [PATCH 3/3] [VPlan] Inherit from DefaultDoCastIfPossible

---
 llvm/lib/Transforms/Vectorize/VPlan.h | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 49e93c0942d9a..dc01bb52a8cfa 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -4152,7 +4152,10 @@ class LLVM_ABI_FOR_TEST VPScalarIVStepsRecipe : public VPRecipeWithIRFlags {
 };
 
 /// Support casting from VPRecipeBase -> VPPhiAccessors.
-template <> struct CastInfo<VPPhiAccessors, VPRecipeBase *> {
+template <>
+struct CastInfo<VPPhiAccessors, VPRecipeBase *>
+    : DefaultDoCastIfPossible<VPPhiAccessors *, VPRecipeBase *,
+                              CastInfo<VPPhiAccessors, VPRecipeBase *>> {
   // Used by isa.
   static inline bool isPossible(VPRecipeBase *R) {
     // TODO: include VPPredInstPHIRecipe too, once it implements VPPhiAccessors.
@@ -4171,12 +4174,8 @@ template <> struct CastInfo<VPPhiAccessors, VPRecipeBase *> {
       return cast<VPHeaderPHIRecipe>(R);
     }
   }
-  /// Used by dyn_cast.
-  static inline VPPhiAccessors *doCastIfPossible(VPRecipeBase *R) {
-    if (!isPossible(R))
-      return nullptr;
-    return doCast(R);
-  }
+  /// Used by inherited doCastIfPossible to dyn_cast.
+  static inline VPPhiAccessors *castFailed() { return nullptr; }
 };
 
 template <>



More information about the llvm-commits mailing list