[llvm] [VPlan] Support isa/dyn_cast from VPRecipeBase to VPIRMetadata (NFC). (PR #166245)

David Sherwood via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 4 05:39:34 PST 2025


================
@@ -3815,6 +3815,76 @@ template <>
 struct CastInfo<VPPhiAccessors, const VPRecipeBase *>
     : CastInfoVPPhiAccessors<const VPRecipeBase *> {};
 
+/// Casting from VPRecipeBase -> VPIRMetadata is supported for all recipe types
+/// implementing VPIRMetadata. Used by isa<> & co.
+template <> struct CastIsPossible<VPIRMetadata, const VPRecipeBase *> {
+  static inline bool isPossible(const VPRecipeBase *R) {
+    return isa<VPInstruction, VPWidenRecipe, VPWidenCastRecipe,
+               VPWidenIntrinsicRecipe, VPWidenCallRecipe, VPWidenSelectRecipe,
+               VPReplicateRecipe, VPInterleaveRecipe, VPInterleaveEVLRecipe,
+               VPWidenLoadRecipe, VPWidenLoadEVLRecipe, VPWidenStoreRecipe,
+               VPWidenStoreEVLRecipe>(R);
+  }
+};
+
+/// Support casting from VPRecipeBase -> VPIRMetadata, by down-casting to the
+/// recipe types implementing VPIRMetadata. Used by cast<>, dyn_cast<> & co.
+template <typename SrcTy>
+struct CastInfoVPIRMetadata : public CastIsPossible<VPIRMetadata, SrcTy> {
+
+  using Self = CastInfo<VPIRMetadata, SrcTy>;
+
+  /// doCast is used by cast<>.
+  static inline VPIRMetadata *doCast(SrcTy R) {
+    return const_cast<VPIRMetadata *>([R]() -> const VPIRMetadata * {
----------------
david-arm wrote:

This looks a bit odd. We potentially take a non-const SrcTy, cast it to a non-const VPInstruction, etc., then return `const VPIRMetadata *` from the lambda function, which then returns `VPIRMetadata *`. Why not just do

```
  return cast<VPIRMetadata *> ([R]() -> VPIRMetadata * {
    ...
```

?

https://github.com/llvm/llvm-project/pull/166245


More information about the llvm-commits mailing list