[Mlir-commits] [mlir] [MLIR] Add IRDL dialect loading to C API (PR #91852)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat May 11 05:23:14 PDT 2024
=?utf-8?q?Théo?= Degioanni
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/91852 at github.com>
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Théo Degioanni (Moxinilian)
<details>
<summary>Changes</summary>
Being able to add custom dialects is one of the big missing pieces of the C API. This change should make it achievable via IRDL. Hopefully this should open custom dialect definition to non-C++ users of MLIR.
---
Full diff: https://github.com/llvm/llvm-project/pull/91852.diff
5 Files Affected:
- (added) mlir/include/mlir-c/Dialect/IRDL.h (+29)
- (modified) mlir/lib/CAPI/Dialect/CMakeLists.txt (+9)
- (added) mlir/lib/CAPI/Dialect/IRDL.cpp (+18)
- (modified) mlir/test/CAPI/CMakeLists.txt (+7)
- (added) mlir/test/CAPI/irdl.c (+58)
``````````diff
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..b35345b664b14
--- /dev/null
+++ b/mlir/test/CAPI/irdl.c
@@ -0,0 +1,58 @@
+//===- 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);
+ mlirModuleDestroy(dialectDecl);
+
+ MlirModule usingModule = mlirModuleCreateParse(
+ ctx, mlirStringRefCreateFromCString(newDialectUsage));
+
+ mlirOperationDump(mlirModuleGetOperation(usingModule));
+
+ mlirModuleDestroy(usingModule);
+ mlirContextDestroy(ctx);
+ return 0;
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/91852
More information about the Mlir-commits
mailing list