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 @@ -421,6 +421,17 @@ /// Checks whether a region is null. static inline bool mlirRegionIsNull(MlirRegion region) { return !region.ptr; } +/// Checks whether two region handles point to the same region. This does not +/// perform deep comparison. +MLIR_CAPI_EXPORTED bool mlirRegionEqual(MlirRegion region, MlirRegion other); + +/// Return the region containing this region or NULL if the region is +/// attached to a top-level operation. +MLIR_CAPI_EXPORTED MlirRegion mlirRegionGetParentRegion(MlirRegion); + +/// Return the parent operation this region is attached to. +MLIR_CAPI_EXPORTED MlirOperation mlirRegionGetParentOperation(MlirRegion); + /// Gets the first block in the region. MLIR_CAPI_EXPORTED MlirBlock mlirRegionGetFirstBlock(MlirRegion region); @@ -467,6 +478,12 @@ /// perform deep comparison. MLIR_CAPI_EXPORTED bool mlirBlockEqual(MlirBlock block, MlirBlock other); +/// Returns the closest surrounding region that contains this block. +MLIR_CAPI_EXPORTED MlirRegion mlirBlockGetParentRegion(MlirBlock); + +/// Returns the closest surrounding operation that contains this block. +MLIR_CAPI_EXPORTED MlirOperation mlirBlockGetParentOperation(MlirBlock); + /// Returns the block immediately following the given block in its parent /// region. MLIR_CAPI_EXPORTED MlirBlock mlirBlockGetNextInRegion(MlirBlock block); 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 @@ -400,6 +400,18 @@ MlirRegion mlirRegionCreate() { return wrap(new Region); } +bool mlirRegionEqual(MlirRegion region, MlirRegion other) { + return unwrap(region) == unwrap(other); +} + +MlirRegion mlirRegionGetParentRegion(MlirRegion region) { + return wrap(unwrap(region)->getParentRegion()); +} + +MlirOperation mlirRegionGetParentOperation(MlirRegion region) { + return wrap(unwrap(region)->getParentOp()); +} + MlirBlock mlirRegionGetFirstBlock(MlirRegion region) { Region *cppRegion = unwrap(region); if (cppRegion->empty()) @@ -461,6 +473,14 @@ return unwrap(block) == unwrap(other); } +MlirRegion mlirBlockGetParentRegion(MlirBlock block) { + return wrap(unwrap(block)->getParent()); +} + +MlirOperation mlirBlockGetParentOperation(MlirBlock block) { + return wrap(unwrap(block)->getParentOp()); +} + MlirBlock mlirBlockGetNextInRegion(MlirBlock block) { return wrap(unwrap(block)->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 @@ -536,6 +536,10 @@ mlirBlockAppendOwnedOperation(block2, op5); mlirBlockAppendOwnedOperation(block3, op6); mlirBlockAppendOwnedOperation(block4, op7); + + assert(mlirRegionEqual(mlirBlockGetParentRegion(block1), region)); + assert(mlirOperationEqual(mlirBlockGetParentOperation(block1), op)); + assert(mlirOperationEqual(mlirRegionGetParentOperation(region), op)); mlirOperationDump(op); mlirOperationDestroy(op);