[Mlir-commits] [mlir] 1337622 - [MLIR] Add IRDL dialect loading to C API (#91852)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat May 11 10:45:38 PDT 2024
Author: Théo Degioanni
Date: 2024-05-11T19:45:34+02:00
New Revision: 1337622a492f4e77604b09ac8ff97042e46d8d42
URL: https://github.com/llvm/llvm-project/commit/1337622a492f4e77604b09ac8ff97042e46d8d42
DIFF: https://github.com/llvm/llvm-project/commit/1337622a492f4e77604b09ac8ff97042e46d8d42.diff
LOG: [MLIR] Add IRDL dialect loading to C API (#91852)
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.
Added:
mlir/include/mlir-c/Dialect/IRDL.h
mlir/lib/CAPI/Dialect/IRDL.cpp
mlir/test/CAPI/irdl.c
Modified:
mlir/lib/CAPI/Dialect/CMakeLists.txt
mlir/test/CAPI/CMakeLists.txt
mlir/test/CMakeLists.txt
mlir/test/lit.cfg.py
Removed:
################################################################################
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;
+}
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..ea6d9ae71b773 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-irdl-test",
"mlir-capi-llvm-test",
"mlir-capi-pass-test",
"mlir-capi-pdl-test",
More information about the Mlir-commits
mailing list