@@ -2626,6 +2626,85 @@ class PyOpSuccessors : public Sliceable<PyOpSuccessors, PyBlock> {
2626
2626
PyOperationRef operation;
2627
2627
};
2628
2628
2629
+ // / A list of block successors. Internally, these are stored as consecutive
2630
+ // / elements, random access is cheap. The (returned) successor list is
2631
+ // / associated with the operation and block whose successors these are, and thus
2632
+ // / extends the lifetime of this operation and block.
2633
+ class PyBlockSuccessors : public Sliceable <PyBlockSuccessors, PyBlock> {
2634
+ public:
2635
+ static constexpr const char *pyClassName = " BlockSuccessors" ;
2636
+
2637
+ PyBlockSuccessors (PyBlock block, PyOperationRef operation,
2638
+ intptr_t startIndex = 0 , intptr_t length = -1 ,
2639
+ intptr_t step = 1 )
2640
+ : Sliceable(startIndex,
2641
+ length == -1 ? mlirBlockGetNumSuccessors(block.get())
2642
+ : length,
2643
+ step),
2644
+ operation (operation), block(block) {}
2645
+
2646
+ private:
2647
+ // / Give the parent CRTP class access to hook implementations below.
2648
+ friend class Sliceable <PyBlockSuccessors, PyBlock>;
2649
+
2650
+ intptr_t getRawNumElements () {
2651
+ block.checkValid ();
2652
+ return mlirBlockGetNumSuccessors (block.get ());
2653
+ }
2654
+
2655
+ PyBlock getRawElement (intptr_t pos) {
2656
+ MlirBlock block = mlirBlockGetSuccessor (this ->block .get (), pos);
2657
+ return PyBlock (operation, block);
2658
+ }
2659
+
2660
+ PyBlockSuccessors slice (intptr_t startIndex, intptr_t length, intptr_t step) {
2661
+ return PyBlockSuccessors (block, operation, startIndex, length, step);
2662
+ }
2663
+
2664
+ PyOperationRef operation;
2665
+ PyBlock block;
2666
+ };
2667
+
2668
+ // / A list of block predecessors. Internally, these are stored as consecutive
2669
+ // / elements, random access is cheap. The (returned) predecessor list is
2670
+ // / associated with the operation and block whose predecessors these are, and
2671
+ // / thus extends the lifetime of this operation and block.
2672
+ class PyBlockPredecessors : public Sliceable <PyBlockPredecessors, PyBlock> {
2673
+ public:
2674
+ static constexpr const char *pyClassName = " BlockPredecessors" ;
2675
+
2676
+ PyBlockPredecessors (PyBlock block, PyOperationRef operation,
2677
+ intptr_t startIndex = 0 , intptr_t length = -1 ,
2678
+ intptr_t step = 1 )
2679
+ : Sliceable(startIndex,
2680
+ length == -1 ? mlirBlockGetNumPredecessors(block.get())
2681
+ : length,
2682
+ step),
2683
+ operation (operation), block(block) {}
2684
+
2685
+ private:
2686
+ // / Give the parent CRTP class access to hook implementations below.
2687
+ friend class Sliceable <PyBlockPredecessors, PyBlock>;
2688
+
2689
+ intptr_t getRawNumElements () {
2690
+ block.checkValid ();
2691
+ return mlirBlockGetNumPredecessors (block.get ());
2692
+ }
2693
+
2694
+ PyBlock getRawElement (intptr_t pos) {
2695
+ MlirBlock block = mlirBlockGetPredecessor (this ->block .get (), pos);
2696
+ return PyBlock (operation, block);
2697
+ }
2698
+
2699
+ PyBlockPredecessors slice (intptr_t startIndex, intptr_t length,
2700
+ intptr_t step) {
2701
+ return PyBlockPredecessors (block, operation, startIndex, length, step);
2702
+ }
2703
+
2704
+ PyOperationRef operation;
2705
+ PyBlock block;
2706
+ };
2707
+
2629
2708
// / A list of operation attributes. Can be indexed by name, producing
2630
2709
// / attributes, or by index, producing named attributes.
2631
2710
class PyOpAttributeMap {
@@ -3655,7 +3734,19 @@ void mlir::python::populateIRCore(nb::module_ &m) {
3655
3734
},
3656
3735
nb::arg (" operation" ),
3657
3736
" Appends an operation to this block. If the operation is currently "
3658
- " in another block, it will be moved." );
3737
+ " in another block, it will be moved." )
3738
+ .def_prop_ro (
3739
+ " successors" ,
3740
+ [](PyBlock &self) {
3741
+ return PyBlockSuccessors (self, self.getParentOperation ());
3742
+ },
3743
+ " Returns the list of Block successors." )
3744
+ .def_prop_ro (
3745
+ " predecessors" ,
3746
+ [](PyBlock &self) {
3747
+ return PyBlockPredecessors (self, self.getParentOperation ());
3748
+ },
3749
+ " Returns the list of Block predecessors." );
3659
3750
3660
3751
// ----------------------------------------------------------------------------
3661
3752
// Mapping of PyInsertionPoint.
@@ -4099,6 +4190,8 @@ void mlir::python::populateIRCore(nb::module_ &m) {
4099
4190
PyBlockArgumentList::bind (m);
4100
4191
PyBlockIterator::bind (m);
4101
4192
PyBlockList::bind (m);
4193
+ PyBlockSuccessors::bind (m);
4194
+ PyBlockPredecessors::bind (m);
4102
4195
PyOperationIterator::bind (m);
4103
4196
PyOperationList::bind (m);
4104
4197
PyOpAttributeMap::bind (m);
0 commit comments