[Mlir-commits] [mlir] [MLIR] Use make_isa_range to implement getOps (NFC). (PR #224051)

Florian Hahn llvmlistbot at llvm.org
Thu Sep 17 01:21:25 PDT 2026


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

>From d28926f73cfec65b6a72491d1e46488b9f94240b Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Mon, 14 Sep 2026 20:26:22 +0100
Subject: [PATCH] [MLIR] Use make_isa_range to implement getOps (NFC).

Simplify implementation using make_isa_range from
https://github.com/llvm/llvm-project/pull/223414.
---
 mlir/include/mlir/IR/Block.h        | 20 +++--------------
 mlir/include/mlir/IR/BlockSupport.h | 34 -----------------------------
 mlir/include/mlir/IR/Region.h       | 23 ++++---------------
 3 files changed, 7 insertions(+), 70 deletions(-)

diff --git a/mlir/include/mlir/IR/Block.h b/mlir/include/mlir/IR/Block.h
index c84921db220af..3638595358e25 100644
--- a/mlir/include/mlir/IR/Block.h
+++ b/mlir/include/mlir/IR/Block.h
@@ -16,6 +16,7 @@
 #include "mlir/IR/BlockSupport.h"
 #include "mlir/IR/Visitors.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/SmallPtrSet.h"
 
 namespace llvm {
@@ -206,26 +207,11 @@ class alignas(8) Block : public IRObjectWithUseList<BlockOperand>,
   /// Recomputes the ordering of child operations within the block.
   void recomputeOpOrder();
 
-  /// This class provides iteration over the held operations of a block for a
-  /// specific operation type.
-  template <typename OpT>
-  using op_iterator = detail::op_iterator<OpT, iterator>;
-
   /// Return an iterator range over the operations within this block that are of
   /// 'OpT'.
   template <typename OpT>
-  iterator_range<op_iterator<OpT>> getOps() {
-    auto endIt = end();
-    return {detail::op_filter_iterator<OpT, iterator>(begin(), endIt),
-            detail::op_filter_iterator<OpT, iterator>(endIt, endIt)};
-  }
-  template <typename OpT>
-  op_iterator<OpT> op_begin() {
-    return detail::op_filter_iterator<OpT, iterator>(begin(), end());
-  }
-  template <typename OpT>
-  op_iterator<OpT> op_end() {
-    return detail::op_filter_iterator<OpT, iterator>(end(), end());
+  auto getOps() {
+    return llvm::make_isa_range<OpT>(*this);
   }
 
   /// Return an iterator range over the operation within this block excluding
diff --git a/mlir/include/mlir/IR/BlockSupport.h b/mlir/include/mlir/IR/BlockSupport.h
index 782e977adbd7f..9468d800077c1 100644
--- a/mlir/include/mlir/IR/BlockSupport.h
+++ b/mlir/include/mlir/IR/BlockSupport.h
@@ -135,40 +135,6 @@ class BlockRange final
 // Operation Iterators
 //===----------------------------------------------------------------------===//
 
-namespace detail {
-/// A utility iterator that filters out operations that are not 'OpT'.
-template <typename OpT, typename IteratorT>
-class op_filter_iterator
-    : public llvm::filter_iterator<IteratorT, bool (*)(Operation &)> {
-  static bool filter(Operation &op) { return llvm::isa<OpT>(op); }
-
-public:
-  op_filter_iterator(IteratorT it, IteratorT end)
-      : llvm::filter_iterator<IteratorT, bool (*)(Operation &)>(it, end,
-                                                                &filter) {}
-
-  /// Allow implicit conversion to the underlying iterator.
-  operator const IteratorT &() const { return this->wrapped(); }
-};
-
-/// This class provides iteration over the held operations of a block for a
-/// specific operation type.
-template <typename OpT, typename IteratorT>
-class op_iterator
-    : public llvm::mapped_iterator<op_filter_iterator<OpT, IteratorT>,
-                                   OpT (*)(Operation &)> {
-  static OpT unwrap(Operation &op) { return cast<OpT>(op); }
-
-public:
-  /// Initializes the iterator to the specified filter iterator.
-  op_iterator(op_filter_iterator<OpT, IteratorT> it)
-      : llvm::mapped_iterator<op_filter_iterator<OpT, IteratorT>,
-                              OpT (*)(Operation &)>(it, &unwrap) {}
-
-  /// Allow implicit conversion to the underlying block iterator.
-  operator const IteratorT &() const { return this->wrapped(); }
-};
-} // namespace detail
 } // namespace mlir
 
 namespace llvm {
diff --git a/mlir/include/mlir/IR/Region.h b/mlir/include/mlir/IR/Region.h
index a61eaaa54d401..1e433e759005c 100644
--- a/mlir/include/mlir/IR/Region.h
+++ b/mlir/include/mlir/IR/Region.h
@@ -173,32 +173,17 @@ class Region {
     Block::iterator operation;
   };
 
-  /// This class provides iteration over the held operations of a region for a
-  /// specific operation type.
-  template <typename OpT>
-  using op_iterator = detail::op_iterator<OpT, OpIterator>;
-
   /// Return iterators that walk the operations nested directly within this
   /// region.
   OpIterator op_begin() { return OpIterator(this); }
   OpIterator op_end() { return OpIterator(this, /*end=*/true); }
   iterator_range<OpIterator> getOps() { return {op_begin(), op_end()}; }
 
-  /// Return iterators that walk operations of type 'T' nested directly within
-  /// this region.
-  template <typename OpT>
-  op_iterator<OpT> op_begin() {
-    return detail::op_filter_iterator<OpT, OpIterator>(op_begin(), op_end());
-  }
-  template <typename OpT>
-  op_iterator<OpT> op_end() {
-    return detail::op_filter_iterator<OpT, OpIterator>(op_end(), op_end());
-  }
+  /// Return an iterator range over the operations nested directly within this
+  /// region that are of type 'OpT'.
   template <typename OpT>
-  iterator_range<op_iterator<OpT>> getOps() {
-    auto endIt = op_end();
-    return {detail::op_filter_iterator<OpT, OpIterator>(op_begin(), endIt),
-            detail::op_filter_iterator<OpT, OpIterator>(endIt, endIt)};
+  auto getOps() {
+    return llvm::make_isa_range<OpT>(llvm::make_range(op_begin(), op_end()));
   }
 
   //===--------------------------------------------------------------------===//



More information about the Mlir-commits mailing list