[llvm] [SandboxVec][VecUtils] Lane Enumerator (PR #188355)

Jorge Gorbe Moya via llvm-commits llvm-commits at lists.llvm.org
Sat Mar 28 09:03:25 PDT 2026


================
@@ -304,6 +305,69 @@ class VecUtils {
                                      WhereIt, Ctx, "Unpack");
   }
 
+  /// Iterate over all lanes and Value pairs commulatively.
+  // For example, given a range: {i32 %v0, <2 x i32> %v1, i32 %v2} we get:
+  //  Lane Elm
+  //   0   %v0
+  //   1   %v1
+  //   3   %v2
+  template <typename RangeT, typename RangeIteratorT>
+  class LaneValueEnumerator {
+    const RangeT &Range;
+    /// Cummulative Lane count.
+    unsigned Lane;
+    /// Points to current element.
+    RangeIteratorT It;
+    RangeIteratorT ItE;
+
+  public:
+    LaneValueEnumerator(const RangeT &Range, unsigned Lane, RangeIteratorT It,
+                        RangeIteratorT ItE)
+        : Range(Range), Lane(Lane), It(It), ItE(ItE) {}
+    using iterator_catecotry = std::input_iterator_tag;
+    // NOTE: dereference returns by value instead of by reference.
+    using value_type = std::pair<unsigned, Value *>;
+    using difference_type = std::ptrdiff_t;
+    using pointer = std::pair<unsigned, Value *> *;
+    using reference = std::pair<unsigned, Value *> &;
+    LaneValueEnumerator operator++() {
+      assert(It != ItE && "Already at end!");
+      auto *Ty = Utils::getExpectedType(*It);
+      if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
+        Lane += VecTy->getNumElements();
+      } else {
+        assert(!isa<VectorType>(Ty) && "Expected scalar type!");
+        Lane += 1;
+      }
+      ++It;
+      return *this;
+    }
+    value_type operator*() const { return {Lane, *It}; }
+    bool operator==(const LaneValueEnumerator &Other) const {
+      assert(&Range == &Other.Range && "Different ranges!");
+      // If at end, don't check Lane.
+      if (It == ItE)
+        return It == Other.It;
+      return It == Other.It && Lane == Other.Lane;
----------------
slackito wrote:

If we're asserting `Other` is an iterator over the same range, is it ever possible that `It == Other.It` but the lane count differs?

If you're checking `Lane == Other.Lane` because two iterators could have a different begin point (and thus reach the same element with a different count), do we care about making sure `ItE` is equal too?

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


More information about the llvm-commits mailing list