[Mlir-commits] [mlir] [MLIR] Add IRDL dialect loading to C API (PR #91852)
Théo Degioanni
llvmlistbot at llvm.org
Sat May 11 06:17:21 PDT 2024
https://github.com/Moxinilian updated https://github.com/llvm/llvm-project/pull/91852
>From 5a38d6981887b64f3019e0dcd45d38931c95898e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20Degioanni?=
<theo.degioanni.llvm.deluge062 at simplelogin.fr>
Date: Sat, 11 May 2024 12:42:07 +0200
Subject: [PATCH 1/5] add IRDL dialect loading to C API
---
mlir/include/mlir-c/Dialect/IRDL.h | 29 +++++++++++++++
mlir/lib/CAPI/Dialect/CMakeLists.txt | 9 +++++
mlir/lib/CAPI/Dialect/IRDL.cpp | 18 +++++++++
mlir/test/CAPI/CMakeLists.txt | 7 ++++
mlir/test/CAPI/irdl.c | 55 ++++++++++++++++++++++++++++
5 files changed, 118 insertions(+)
create mode 100644 mlir/include/mlir-c/Dialect/IRDL.h
create mode 100644 mlir/lib/CAPI/Dialect/IRDL.cpp
create mode 100644 mlir/test/CAPI/irdl.c
diff --git a/mlir/include/mlir-c/Dialect/IRDL.h b/mlir/include/mlir-c/Dialect/IRDL.h
new file mode 100644
index 0000000000000..c4d6ffd989af9
--- /dev/null
+++ b/mlir/include/mlir-c/Dialect/IRDL.h
@@ -0,0 +1,29 @@
+//===-- mlir-c/Dialect/IRDL.h - C API for IRDL --------------------*- C -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
+// Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_C_DIALECT_IRDL_H
+#define MLIR_C_DIALECT_IRDL_H
+
+#include "mlir-c/IR.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(IRDL, irdl);
+
+/// Loads all IRDL dialects in the provided module, registering the dialects in
+/// the module's associated context.
+MLIR_CAPI_EXPORTED MlirLogicalResult mlirLoadIRDLDialects(MlirModule module);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // MLIR_C_DIALECT_IRDL_H
diff --git a/mlir/lib/CAPI/Dialect/CMakeLists.txt b/mlir/lib/CAPI/Dialect/CMakeLists.txt
index 58b8739043f9d..4e141b60ff8cc 100644
--- a/mlir/lib/CAPI/Dialect/CMakeLists.txt
+++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt
@@ -72,6 +72,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIGPU
MLIRPass
)
+add_mlir_upstream_c_api_library(MLIRCAPIIRDL
+ IRDL.cpp
+
+ PARTIAL_SOURCES_INTENDED
+ LINK_LIBS PUBLIC
+ MLIRCAPIIR
+ MLIRIRDL
+)
+
add_mlir_upstream_c_api_library(MLIRCAPILLVM
LLVM.cpp
diff --git a/mlir/lib/CAPI/Dialect/IRDL.cpp b/mlir/lib/CAPI/Dialect/IRDL.cpp
new file mode 100644
index 0000000000000..cb9dc8ceb6795
--- /dev/null
+++ b/mlir/lib/CAPI/Dialect/IRDL.cpp
@@ -0,0 +1,18 @@
+//===- IRDL.cpp - C Interface for IRDL dialect ----------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "mlir-c/Dialect/IRDL.h"
+#include "mlir/CAPI/Registration.h"
+#include "mlir/Dialect/IRDL/IR/IRDL.h"
+#include "mlir/Dialect/IRDL/IRDLLoading.h"
+
+MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(IRDL, irdl, mlir::irdl::IRDLDialect)
+
+MlirLogicalResult mlirLoadIRDLDialects(MlirModule module) {
+ return wrap(mlir::irdl::loadDialects(unwrap(module)));
+}
diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt
index b9cd63ef7c673..57b342a5e26b2 100644
--- a/mlir/test/CAPI/CMakeLists.txt
+++ b/mlir/test/CAPI/CMakeLists.txt
@@ -38,6 +38,13 @@ _add_capi_test_executable(mlir-capi-ir-test
MLIRCAPIRegisterEverything
)
+_add_capi_test_executable(mlir-capi-irdl-test
+ irdl.c
+ LINK_LIBS PRIVATE
+ MLIRCAPIIR
+ MLIRCAPIIRDL
+)
+
_add_capi_test_executable(mlir-capi-llvm-test
llvm.c
LINK_LIBS PRIVATE
diff --git a/mlir/test/CAPI/irdl.c b/mlir/test/CAPI/irdl.c
new file mode 100644
index 0000000000000..f3198264ed910
--- /dev/null
+++ b/mlir/test/CAPI/irdl.c
@@ -0,0 +1,55 @@
+//===- irdl.c - Test for the C bindings for IRDL registration -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
+// Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+/* RUN: mlir-capi-irdl-test 2>&1 | FileCheck %s
+ */
+
+#include "mlir-c/Dialect/IRDL.h"
+#include "mlir-c/IR.h"
+
+const char irdlDialect[] = "\
+ irdl.dialect @foo {\
+ irdl.operation @op {\
+ %i32 = irdl.is i32\
+ irdl.results(%i32)\
+ }\
+ }\
+ irdl.dialect @bar {\
+ irdl.operation @op {\
+ %i32 = irdl.is i32\
+ irdl.operands(%i32)\
+ }\
+ }";
+
+// CHECK: module {
+// CHECK-NEXT: %[[RES:.*]] = "foo.op"() : () -> i32
+// CHECK-NEXT: "bar.op"(%[[RES]]) : (i32) -> ()
+// CHECK-NEXT: }
+const char newDialectUsage[] = "\
+ module {\
+ %res = \"foo.op\"() : () -> i32\
+ \"bar.op\"(%res) : (i32) -> ()\
+ }";
+
+int main(void) {
+ MlirContext ctx = mlirContextCreate();
+ mlirDialectHandleLoadDialect(mlirGetDialectHandle__irdl__(), ctx);
+
+ MlirModule dialectDecl =
+ mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(irdlDialect));
+
+ mlirLoadIRDLDialects(dialectDecl);
+
+ MlirModule usingModule = mlirModuleCreateParse(
+ ctx, mlirStringRefCreateFromCString(newDialectUsage));
+
+ mlirOperationDump(mlirModuleGetOperation(usingModule));
+
+ return 0;
+}
>From 2c77d5fab52fba760d4d46e0afbbed3f2ed03e89 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20Degioanni?=
<theo.degioanni.llvm.deluge062 at simplelogin.fr>
Date: Sat, 11 May 2024 12:49:13 +0200
Subject: [PATCH 2/5] ensure the dialect definition can be destroyed after
loading
---
mlir/test/CAPI/irdl.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/mlir/test/CAPI/irdl.c b/mlir/test/CAPI/irdl.c
index f3198264ed910..df75e1f082250 100644
--- a/mlir/test/CAPI/irdl.c
+++ b/mlir/test/CAPI/irdl.c
@@ -45,11 +45,13 @@ int main(void) {
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(irdlDialect));
mlirLoadIRDLDialects(dialectDecl);
+ mlirModuleDestroy(dialectDecl);
MlirModule usingModule = mlirModuleCreateParse(
ctx, mlirStringRefCreateFromCString(newDialectUsage));
mlirOperationDump(mlirModuleGetOperation(usingModule));
+ mlirContextDestroy(ctx);
return 0;
}
>From cf3885e97a75e517e85c536cd3c6a22e5bc2a247 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20Degioanni?=
<theo.degioanni.llvm.deluge062 at simplelogin.fr>
Date: Sat, 11 May 2024 13:31:40 +0200
Subject: [PATCH 3/5] deallocate usingModule in test
---
mlir/test/CAPI/irdl.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/mlir/test/CAPI/irdl.c b/mlir/test/CAPI/irdl.c
index df75e1f082250..b35345b664b14 100644
--- a/mlir/test/CAPI/irdl.c
+++ b/mlir/test/CAPI/irdl.c
@@ -52,6 +52,7 @@ int main(void) {
mlirOperationDump(mlirModuleGetOperation(usingModule));
+ mlirModuleDestroy(usingModule);
mlirContextDestroy(ctx);
return 0;
}
>From 73f872940e9f885f7f0cdd41545c7a929f298edd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20Degioanni?=
<theo.degioanni.llvm.deluge062 at simplelogin.fr>
Date: Sat, 11 May 2024 15:16:26 +0200
Subject: [PATCH 4/5] fully add irdl test to the build system
---
mlir/test/CMakeLists.txt | 1 +
mlir/test/lit.cfg.py | 1 +
2 files changed, 2 insertions(+)
diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt
index 5319a9cb33e00..8806a1dd92238 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -101,6 +101,7 @@ configure_lit_site_cfg(
set(MLIR_TEST_DEPENDS
FileCheck count not split-file
mlir-capi-ir-test
+ mlir-capi-irdl-test
mlir-capi-llvm-test
mlir-capi-pass-test
mlir-capi-quant-test
diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py
index 4740e7d137e8d..38a55d9adfc2a 100644
--- a/mlir/test/lit.cfg.py
+++ b/mlir/test/lit.cfg.py
@@ -100,6 +100,7 @@ def add_runtime(name):
"mlir-lsp-server",
"mlir-capi-execution-engine-test",
"mlir-capi-ir-test",
+ "mlir-capi-ir-test",
"mlir-capi-llvm-test",
"mlir-capi-pass-test",
"mlir-capi-pdl-test",
>From 569ea88bbd26e1eaa7b57173fe7eec8606bc7ee7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Th=C3=A9o=20Degioanni?=
<theo.degioanni.llvm.deluge062 at simplelogin.fr>
Date: Sat, 11 May 2024 15:17:07 +0200
Subject: [PATCH 5/5] fix typo in test cfg
---
mlir/test/lit.cfg.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/mlir/test/lit.cfg.py b/mlir/test/lit.cfg.py
index 38a55d9adfc2a..ea6d9ae71b773 100644
--- a/mlir/test/lit.cfg.py
+++ b/mlir/test/lit.cfg.py
@@ -100,7 +100,7 @@ def add_runtime(name):
"mlir-lsp-server",
"mlir-capi-execution-engine-test",
"mlir-capi-ir-test",
- "mlir-capi-ir-test",
+ "mlir-capi-irdl-test",
"mlir-capi-llvm-test",
"mlir-capi-pass-test",
"mlir-capi-pdl-test",
More information about the Mlir-commits
mailing list