Skip to content

Commit f20828e

Browse files
committed
[mlir-c] Add mlirIRDLLoadDialects
1 parent 46b011d commit f20828e

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===-- mlir-c/Dialect/IRDL.h - C API for IRDL dialect ------------*- 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+
/// Load all the dialects defined in the module.
22+
MLIR_CAPI_EXPORTED MlirLogicalResult mlirIRDLLoadDialects(MlirModule module);
23+
24+
#ifdef __cplusplus
25+
}
26+
#endif
27+
28+
#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
@@ -40,6 +40,15 @@ add_mlir_upstream_c_api_library(MLIRCAPIControlFlow
4040
MLIRControlFlowDialect
4141
)
4242

43+
add_mlir_upstream_c_api_library(MLIRCAPIIRDL
44+
IRDL.cpp
45+
46+
PARTIAL_SOURCES_INTENDED
47+
LINK_LIBS PUBLIC
48+
MLIRCAPIIR
49+
MLIRIRDL
50+
)
51+
4352
add_mlir_upstream_c_api_library(MLIRCAPIMath
4453
Math.cpp
4554

mlir/lib/CAPI/Dialect/IRDL.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
using namespace mlir;
15+
16+
MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(IRDL, irdl, irdl::IRDLDialect)
17+
18+
extern "C" MlirLogicalResult mlirIRDLLoadDialects(MlirModule module) {
19+
return wrap(irdl::loadDialects(unwrap(module)));
20+
}

mlir/test/CAPI/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ _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+
MLIRCAPIRegisterEverything
47+
)
48+
4149
_add_capi_test_executable(mlir-capi-llvm-test
4250
llvm.c
4351
LINK_LIBS PRIVATE

mlir/test/CAPI/irdl.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===- irdl.c - Test of IRDL dialect C API --------------------------------===//
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+
#include "mlir-c/Dialect/IRDL.h"
13+
#include "mlir-c/IR.h"
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
18+
// CHECK-LABEL: testLoadDialect
19+
int testLoadDialect(MlirContext ctx) {
20+
fprintf(stderr, "testLoadDialect\n");
21+
22+
const char *moduleString = "irdl.dialect @cmath {"
23+
" irdl.type @complex {"
24+
" %0 = irdl.is f32"
25+
" %1 = irdl.is f64"
26+
" %2 = irdl.any_of(%0, %1)"
27+
" irdl.parameters(%2)"
28+
" }"
29+
" irdl.operation @mul {"
30+
" %0 = irdl.is f32"
31+
" %1 = irdl.is f64"
32+
" %2 = irdl.any_of(%0, %1)"
33+
" %3 = irdl.parametric @complex<%2>"
34+
" irdl.operands(%3, %3)"
35+
" irdl.results(%3)"
36+
" }"
37+
"}";
38+
39+
MlirModule module =
40+
mlirModuleCreateParse(ctx, mlirStringRefCreateFromCString(moduleString));
41+
42+
if (mlirModuleIsNull(module))
43+
return 1;
44+
45+
MlirLogicalResult result = mlirIRDLLoadDialects(module);
46+
if (mlirLogicalResultIsFailure(result))
47+
return 2;
48+
49+
if (!mlirContextIsRegisteredOperation(
50+
ctx, mlirStringRefCreateFromCString("cmath.mul")))
51+
return 3;
52+
53+
mlirModuleDestroy(module);
54+
55+
return 0;
56+
}
57+
58+
int main(void) {
59+
MlirContext ctx = mlirContextCreate();
60+
if (mlirContextIsNull(ctx))
61+
return 1;
62+
mlirDialectHandleRegisterDialect(mlirGetDialectHandle__irdl__(), ctx);
63+
int result = testLoadDialect(ctx);
64+
mlirContextDestroy(ctx);
65+
if (result)
66+
return result;
67+
68+
return EXIT_SUCCESS;
69+
}

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

0 commit comments

Comments
 (0)