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 @@ -209,6 +209,10 @@ /// Views the module as a generic operation. MLIR_CAPI_EXPORTED MlirOperation mlirModuleGetOperation(MlirModule module); +/// Views the generic operation as a module. +/// The returned module is null when the input operation was not a ModuleOp. +MLIR_CAPI_EXPORTED MlirModule mlirModuleFromOperation(MlirOperation op); + //===----------------------------------------------------------------------===// // Operation state. //===----------------------------------------------------------------------===// 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 @@ -181,6 +181,10 @@ return wrap(unwrap(module).getOperation()); } +MlirModule mlirModuleFromOperation(MlirOperation op) { + return wrap(dyn_cast(unwrap(op))); +} + //===----------------------------------------------------------------------===// // Operation state API. //===----------------------------------------------------------------------===//