Add `mlirModuleFromOperation` to C API

At the moment `MlirModule`s can be converted to `MlirOperation`s, but not
the other way around (at least not without going around the C API). This
makes it impossible to e.g. run passes over a `ModuleOp` created through
`mlirOperationCreate`.

Reviewed By: nicolasvasilache, mehdi_amini

Differential Revision: https://reviews.llvm.org/D102497
This commit is contained in:
Adam Paszke 2021-05-17 10:14:02 +00:00 committed by Nicolas Vasilache
parent c29522d648
commit d89602ed62
3 changed files with 10 additions and 0 deletions

View File

@ -209,6 +209,10 @@ MLIR_CAPI_EXPORTED void mlirModuleDestroy(MlirModule module);
/// 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.
//===----------------------------------------------------------------------===//

View File

@ -181,6 +181,10 @@ MlirOperation mlirModuleGetOperation(MlirModule module) {
return wrap(unwrap(module).getOperation());
}
MlirModule mlirModuleFromOperation(MlirOperation op) {
return wrap(dyn_cast<ModuleOp>(unwrap(op)));
}
//===----------------------------------------------------------------------===//
// Operation state API.
//===----------------------------------------------------------------------===//

View File

@ -319,6 +319,7 @@ static void printFirstOfEach(MlirContext ctx, MlirOperation operation) {
MlirOperation parentOperation = operation;
block = mlirRegionGetFirstBlock(region);
operation = mlirBlockGetFirstOperation(block);
assert(mlirModuleIsNull(mlirModuleFromOperation(operation)));
// Verify that parent operation and block report correctly.
fprintf(stderr, "Parent operation eq: %d\n",
@ -460,6 +461,7 @@ static int constructAndTraverseIr(MlirContext ctx) {
MlirModule moduleOp = makeAndDumpAdd(ctx, location);
MlirOperation module = mlirModuleGetOperation(moduleOp);
assert(!mlirModuleIsNull(mlirModuleFromOperation(module)));
int errcode = collectStats(module);
if (errcode)