Skip to content

Commit 1337622

Browse files
authored
[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.
1 parent cdd7821 commit 1337622

File tree

7 files changed

+123
-0
lines changed

7 files changed

+123
-0
lines changed

mlir/include/mlir-c/Dialect/IRDL.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===-- mlir-c/Dialect/IRDL.h - C API for IRDL --------------------*- C -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
#ifndef MLIR_C_DIALECT_IRDL_H
11+
#define MLIR_C_DIALECT_IRDL_H
12+
13+
#include "mlir-c/IR.h"
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(IRDL, irdl);
20+
21+
/// Loads all IRDL dialects in the provided module, registering the dialects in
22+
/// the module's associated context.
23+
MLIR_CAPI_EXPORTED MlirLogicalResult mlirLoadIRDLDialects(MlirModule module);
24+
25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
29+
#endif // MLIR_C_DIALECT_IRDL_H

mlir/lib/CAPI/Dialect/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIGPU
7272
MLIRPass
7373
)
7474

75+
add_mlir_upstream_c_api_library(MLIRCAPIIRDL
76+
IRDL.cpp
77+
78+
PARTIAL_SOURCES_INTENDED
79+
LINK_LIBS PUBLIC
80+
MLIRCAPIIR
81+
MLIRIRDL
82+
)
83+
7584
add_mlir_upstream_c_api_library(MLIRCAPILLVM
7685
LLVM.cpp
7786

mlir/lib/CAPI/Dialect/IRDL.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===- IRDL.cpp - C Interface for IRDL dialect ----------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "mlir-c/Dialect/IRDL.h"
10+
#include "mlir/CAPI/Registration.h"
11+
#include "mlir/Dialect/IRDL/IR/IRDL.h"
12+
#include "mlir/Dialect/IRDL/IRDLLoading.h"
13+
14+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(IRDL, irdl, mlir::irdl::IRDLDialect)
15+
16+
MlirLogicalResult mlirLoadIRDLDialects(MlirModule module) {
17+
return wrap(mlir::irdl::loadDialects(unwrap(module)));
18+
}

mlir/test/CAPI/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ _add_capi_test_executable(mlir-capi-ir-test
3838
MLIRCAPIRegisterEverything
3939
)
4040

41+
_add_capi_test_executable(mlir-capi-irdl-test
42+
irdl.c
43+
LINK_LIBS PRIVATE
44+
MLIRCAPIIR
45+
MLIRCAPIIRDL
46+
)
47+
4148
_add_capi_test_executable(mlir-capi-llvm-test
4249
llvm.c
4350
LINK_LIBS PRIVATE

mlir/test/CAPI/irdl.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===- irdl.c - Test for the C bindings for IRDL registration -------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+
// Exceptions.
5+
// See https://llvm.org/LICENSE.txt for license information.
6+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
/* RUN: mlir-capi-irdl-test 2>&1 | FileCheck %s
11+
*/
12+
13+
#include "mlir-c/Dialect/IRDL.h"
14+
#include "mlir-c/IR.h"
15+
16+
const char irdlDialect[] = "\
17+
irdl.dialect @foo {\
18+
irdl.operation @op {\
19+
%i32 = irdl.is i32\
20+
irdl.results(%i32)\
21+
}\
22+
}\
23+
irdl.dialect @bar {\
24+
irdl.operation @op {\
25+
%i32 = irdl.is i32\
26+
irdl.operands(%i32)\
27+
}\
28+
}";
29+
30+
// CHECK: module {
31+
// CHECK-NEXT: %[[RES:.*]] = "foo.op"() : () -> i32
32+
// CHECK-NEXT: "bar.op"(%[[RES]]) : (i32) -> ()
33+
// CHECK-NEXT: }
34+
const char newDialectUsage[] = "\
35+
module {\
36+
%res = \"foo.op\"() : () -> i32\
37+
\"bar.op\"(%res) : (i32) -> ()\
38+
}";
39+
40+
int main(void) {
41+
MlirContext ctx = mlirContextCreate();
42+
mlirDialectHandleLoadDialect(mlirGetDialectHandle__irdl__(), ctx);
43+
44+
MlirModule dialectDecl =
45+
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(irdlDialect));
46+
47+
mlirLoadIRDLDialects(dialectDecl);
48+
mlirModuleDestroy(dialectDecl);
49+
50+
MlirModule usingModule = mlirModuleCreateParse(
51+
ctx, mlirStringRefCreateFromCString(newDialectUsage));
52+
53+
mlirOperationDump(mlirModuleGetOperation(usingModule));
54+
55+
mlirModuleDestroy(usingModule);
56+
mlirContextDestroy(ctx);
57+
return 0;
58+
}

mlir/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ configure_lit_site_cfg(
101101
set(MLIR_TEST_DEPENDS
102102
FileCheck count not split-file
103103
mlir-capi-ir-test
104+
mlir-capi-irdl-test
104105
mlir-capi-llvm-test
105106
mlir-capi-pass-test
106107
mlir-capi-quant-test

mlir/test/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def add_runtime(name):
100100
"mlir-lsp-server",
101101
"mlir-capi-execution-engine-test",
102102
"mlir-capi-ir-test",
103+
"mlir-capi-irdl-test",
103104
"mlir-capi-llvm-test",
104105
"mlir-capi-pass-test",
105106
"mlir-capi-pdl-test",

0 commit comments

Comments
 (0)