[Mlir-commits] [mlir] d89602e - Add `mlirModuleFromOperation` to C API
Nicolas Vasilache
llvmlistbot at llvm.org
Mon May 17 03:18:00 PDT 2021
Author: Adam Paszke
Date: 2021-05-17T10:14:16Z
New Revision: d89602ed62f3e5f47781659059db6a8cc11122fe
URL: https://github.com/llvm/llvm-project/commit/d89602ed62f3e5f47781659059db6a8cc11122fe
DIFF: https://github.com/llvm/llvm-project/commit/d89602ed62f3e5f47781659059db6a8cc11122fe.diff
LOG: 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
Added:
Modified:
mlir/include/mlir-c/IR.h
mlir/lib/CAPI/IR/IR.cpp
mlir/test/CAPI/ir.c
Removed:
################################################################################
diff --git a/mlir/include/mlir-c/IR.h b/mlir/include/mlir-c/IR.h
index 1b243165cbb36..638eea9b86c95 100644
--- a/mlir/include/mlir-c/IR.h
+++ b/mlir/include/mlir-c/IR.h
@@ -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.
//===----------------------------------------------------------------------===//
diff --git a/mlir/lib/CAPI/IR/IR.cpp b/mlir/lib/CAPI/IR/IR.cpp
index 4e21835164ab2..ebabd6899e06b 100644
--- a/mlir/lib/CAPI/IR/IR.cpp
+++ b/mlir/lib/CAPI/IR/IR.cpp
@@ -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.
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/CAPI/ir.c b/mlir/test/CAPI/ir.c
index 7176cbb2625f8..42dfa532727eb 100644
--- a/mlir/test/CAPI/ir.c
+++ b/mlir/test/CAPI/ir.c
@@ -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)
More information about the Mlir-commits
mailing list