Skip to content

[mlir-c] Add mlirIRDLLoadDialects #89949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

Pangoraw
Copy link

This allows C-API users to load IRDL dialects.

I noticed in #86329 that MlirModule is being depriviledged in favour of MlirOperation so I am not sure if I need to change the signature of mlirIRDLLoadDialects to mlirIRDLLoadDialects(MlirOperation).

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the mlir label Apr 24, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 24, 2024

@llvm/pr-subscribers-mlir

Author: Paul Berg (Pangoraw)

Changes

This allows C-API users to load IRDL dialects.

I noticed in #86329 that MlirModule is being depriviledged in favour of MlirOperation so I am not sure if I need to change the signature of mlirIRDLLoadDialects to mlirIRDLLoadDialects(MlirOperation).


Full diff: https://github.com/llvm/llvm-project/pull/89949.diff

5 Files Affected:

  • (added) mlir/include/mlir-c/Dialect/IRDL.h (+28)
  • (modified) mlir/lib/CAPI/Dialect/CMakeLists.txt (+9)
  • (added) mlir/lib/CAPI/Dialect/IRDL.cpp (+20)
  • (modified) mlir/test/CAPI/CMakeLists.txt (+8)
  • (added) mlir/test/CAPI/irdl.c (+69)
diff --git a/mlir/include/mlir-c/Dialect/IRDL.h b/mlir/include/mlir-c/Dialect/IRDL.h
new file mode 100644
index 00000000000000..774fa8f7747c21
--- /dev/null
+++ b/mlir/include/mlir-c/Dialect/IRDL.h
@@ -0,0 +1,28 @@
+//===-- mlir-c/Dialect/IRDL.h - C API for IRDL dialect ------------*- 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);
+
+/// Load all the dialects defined in the module.
+MLIR_CAPI_EXPORTED MlirLogicalResult mlirIRDLLoadDialects(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 58b8739043f9df..3106318babb1fd 100644
--- a/mlir/lib/CAPI/Dialect/CMakeLists.txt
+++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt
@@ -40,6 +40,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIControlFlow
   MLIRControlFlowDialect
 )
 
+add_mlir_upstream_c_api_library(MLIRCAPIIRDL
+  IRDL.cpp
+
+  PARTIAL_SOURCES_INTENDED
+  LINK_LIBS PUBLIC
+  MLIRCAPIIR
+  MLIRIRDL
+)
+
 add_mlir_upstream_c_api_library(MLIRCAPIMath
   Math.cpp
 
diff --git a/mlir/lib/CAPI/Dialect/IRDL.cpp b/mlir/lib/CAPI/Dialect/IRDL.cpp
new file mode 100644
index 00000000000000..757aba3c795354
--- /dev/null
+++ b/mlir/lib/CAPI/Dialect/IRDL.cpp
@@ -0,0 +1,20 @@
+//===- 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"
+
+using namespace mlir;
+
+MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(IRDL, irdl, irdl::IRDLDialect)
+
+extern "C" MlirLogicalResult mlirIRDLLoadDialects(MlirModule module) {
+  return wrap(irdl::loadDialects(unwrap(module)));
+}
diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt
index b9cd63ef7c673d..a20990812e289a 100644
--- a/mlir/test/CAPI/CMakeLists.txt
+++ b/mlir/test/CAPI/CMakeLists.txt
@@ -38,6 +38,14 @@ _add_capi_test_executable(mlir-capi-ir-test
     MLIRCAPIRegisterEverything
 )
 
+_add_capi_test_executable(mlir-capi-irdl-test
+  irdl.c
+  LINK_LIBS PRIVATE
+    MLIRCAPIIR
+    MLIRCAPIIRDL
+    MLIRCAPIRegisterEverything
+)
+
 _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 00000000000000..a7d79c6d541b62
--- /dev/null
+++ b/mlir/test/CAPI/irdl.c
@@ -0,0 +1,69 @@
+//===- irdl.c - Test of IRDL dialect C API --------------------------------===//
+//
+// 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"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+// CHECK-LABEL: testLoadDialect
+int testLoadDialect(MlirContext ctx) {
+  fprintf(stderr, "testLoadDialect\n");
+
+  const char *moduleString = "irdl.dialect @cmath {"
+                             "  irdl.type @complex {"
+                             "    %0 = irdl.is f32"
+                             "    %1 = irdl.is f64"
+                             "    %2 = irdl.any_of(%0, %1)"
+                             "    irdl.parameters(%2)"
+                             "  }"
+                             "  irdl.operation @mul {"
+                             "    %0 = irdl.is f32"
+                             "    %1 = irdl.is f64"
+                             "    %2 = irdl.any_of(%0, %1)"
+                             "    %3 = irdl.parametric @complex<%2>"
+                             "    irdl.operands(%3, %3)"
+                             "    irdl.results(%3)"
+                             "  }"
+                             "}";
+
+  MlirModule module =
+      mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
+
+  if (mlirModuleIsNull(module))
+    return 1;
+
+  MlirLogicalResult result = mlirIRDLLoadDialects(module);
+  if (mlirLogicalResultIsFailure(result))
+    return 2;
+
+  if (!mlirContextIsRegisteredOperation(
+          ctx, mlirStringRefCreateFromCString("cmath.mul")))
+    return 3;
+
+  mlirModuleDestroy(module);
+
+  return 0;
+}
+
+int main(void) {
+  MlirContext ctx = mlirContextCreate();
+  if (mlirContextIsNull(ctx))
+    return 1;
+  mlirDialectHandleRegisterDialect(mlirGetDialectHandle__irdl__(), ctx);
+  int result = testLoadDialect(ctx);
+  mlirContextDestroy(ctx);
+  if (result)
+    return result;
+
+  return EXIT_SUCCESS;
+}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants