[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.
This commit is contained in:
29
mlir/include/mlir-c/Dialect/IRDL.h
Normal file
29
mlir/include/mlir-c/Dialect/IRDL.h
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
|
||||
18
mlir/lib/CAPI/Dialect/IRDL.cpp
Normal file
18
mlir/lib/CAPI/Dialect/IRDL.cpp
Normal file
@@ -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)));
|
||||
}
|
||||
@@ -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
|
||||
|
||||
58
mlir/test/CAPI/irdl.c
Normal file
58
mlir/test/CAPI/irdl.c
Normal file
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -100,6 +100,7 @@ tools = [
|
||||
"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",
|
||||
|
||||
Reference in New Issue
Block a user