[llvm] [VPlan] Add VPPhiAccessors to provide interface for phi recipes (NFC) (PR #129388)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 28 10:40:15 PDT 2025


================
@@ -1171,6 +1171,59 @@ class VPIRInstruction : public VPRecipeBase {
   void extractLastLaneOfFirstOperand(VPBuilder &Builder);
 };
 
+/// Helper type to provide functions to access incoming values and blocks for
+/// phi-like recipes. RecipeTy must be a sub-class of VPRecipeBase.
+template <typename RecipeTy> class VPPhiAccessors {
+  /// Return a VPRecipeBase* to the current object.
+  const VPRecipeBase *getAsRecipe() const {
+    return static_cast<const RecipeTy *>(this);
+  }
+
+public:
+  /// Returns the incoming VPValue with index \p Idx.
+  VPValue *getIncomingValue(unsigned Idx) const {
+    return getAsRecipe()->getOperand(Idx);
+  }
----------------
fhahn wrote:

All recipes are single defs, but we now unfortunately have some recipes (e.g. VPIRPhi) where the base class `VPIRInstruction` inherits from `VPSingleDefRecipe`, but inheriting from `VPSinglePhiDefRecipe` would not be approriate, hence the trait/mix-in. Down the road, we could also support casting any recipe that supports it to `VPPhiAccessors`, e.g. for verifying all phi-like nodes that implement the trait.

Alternatively we could manually add definitions of `getIncomingBlock` and `getIncomingValue` to all relevant classes?

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


More information about the llvm-commits mailing list