diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h --- a/mlir/include/mlir-c/IR.h +++ b/mlir/include/mlir-c/IR.h @@ -517,6 +517,13 @@ MlirBlock reference, MlirBlock block); +/// Returns first region attached to the operation. +MLIR_CAPI_EXPORTED MlirRegion mlirOperationGetFirstRegion(MlirOperation op); + +/// Returns the region immediately following the given region in its parent +/// operation. +MLIR_CAPI_EXPORTED MlirRegion mlirRegionGetNextInOperation(MlirRegion region); + //===----------------------------------------------------------------------===// // Block API. //===----------------------------------------------------------------------===// diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp --- a/mlir/lib/CAPI/IR/IR.cpp +++ b/mlir/lib/CAPI/IR/IR.cpp @@ -379,6 +379,22 @@ return wrap(&unwrap(op)->getRegion(static_cast(pos))); } +MlirRegion mlirOperationGetFirstRegion(MlirOperation op) { + Operation *cppOp = unwrap(op); + if (cppOp->getNumRegions() == 0) + return wrap(static_cast(nullptr)); + return wrap(&cppOp->getRegion(0)); +} + +MlirRegion mlirRegionGetNextInOperation(MlirRegion region) { + Region *cppRegion = unwrap(region); + Operation *parent = cppRegion->getParentOp(); + intptr_t next = cppRegion->getRegionNumber() + 1; + if (parent->getNumRegions() > next) + return wrap(&parent->getRegion(next)); + return wrap(static_cast(nullptr)); +} + MlirOperation mlirOperationGetNextInBlock(MlirOperation op) { return wrap(unwrap(op)->getNextNode()); } diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c --- a/mlir/test/CAPI/ir.c +++ b/mlir/test/CAPI/ir.c @@ -226,8 +226,8 @@ ++stats->numOpResults; } - for (unsigned i = 0; i < numRegions; ++i) { - MlirRegion region = mlirOperationGetRegion(operation, i); + MlirRegion region = mlirOperationGetFirstRegion(operation); + while (!mlirRegionIsNull(region)) { for (MlirBlock block = mlirRegionGetFirstBlock(region); !mlirBlockIsNull(block); block = mlirBlockGetNextInRegion(block)) { ++stats->numBlocks; @@ -255,6 +255,7 @@ head->next = node; } } + region = mlirRegionGetNextInOperation(region); } return 0; }