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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions mlir/include/mlir-c/Dialect/IRDL.h
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions mlir/lib/CAPI/Dialect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 20 additions & 0 deletions mlir/lib/CAPI/Dialect/IRDL.cpp
Original file line number Diff line number Diff line change
@@ -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)));
}
8 changes: 8 additions & 0 deletions mlir/test/CAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions mlir/test/CAPI/irdl.c
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions mlir/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading