diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir-c/Dialect/LLVM.h @@ -0,0 +1,30 @@ +//===-- mlir-c/Dialect/LLVM.h - C API for LLVM --------------------*- 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_LLVM_H +#define MLIR_C_DIALECT_LLVM_H + +#include "mlir-c/IR.h" +#include "mlir-c/Registration.h" + +#ifdef __cplusplus +extern "C" { +#endif + +MLIR_DECLARE_CAPI_DIALECT_REGISTRATION(LLVM, llvm); + +/// Creates an llvm.ptr type. +MLIR_CAPI_EXPORTED MlirType mlirLLVMPointerTypeGet(MlirType pointee, + unsigned addressSpace); + +#ifdef __cplusplus +} +#endif + +#endif // MLIR_C_DIALECT_LLVM_H diff --git a/mlir/lib/CAPI/Dialect/CMakeLists.txt b/mlir/lib/CAPI/Dialect/CMakeLists.txt --- a/mlir/lib/CAPI/Dialect/CMakeLists.txt +++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt @@ -27,6 +27,15 @@ MLIRPass ) +add_mlir_public_c_api_library(MLIRCAPILLVM + LLVM.cpp + + PARTIAL_SOURCES_INTENDED + LINK_LIBS PUBLIC + MLIRCAPIIR + MLIRLLVMIR +) + add_mlir_public_c_api_library(MLIRCAPILinalg Linalg.cpp LinalgPasses.cpp diff --git a/mlir/lib/CAPI/Dialect/LLVM.cpp b/mlir/lib/CAPI/Dialect/LLVM.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/CAPI/Dialect/LLVM.cpp @@ -0,0 +1,21 @@ +//===- LLVM.cpp - C Interface for LLVM 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/LLVM.h" +#include "mlir/CAPI/Registration.h" +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/Dialect/LLVMIR/LLVMTypes.h" + +using namespace mlir; +using namespace mlir::LLVM; + +MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(LLVM, llvm, LLVMDialect) + +MlirType mlirLLVMPointerTypeGet(MlirType pointee, unsigned addressSpace) { + return wrap(LLVMPointerType::get(unwrap(pointee), addressSpace)); +} diff --git a/mlir/test/CAPI/CMakeLists.txt b/mlir/test/CAPI/CMakeLists.txt --- a/mlir/test/CAPI/CMakeLists.txt +++ b/mlir/test/CAPI/CMakeLists.txt @@ -25,6 +25,10 @@ ir.c ) +_add_capi_test_executable(mlir-capi-llvm-test + llvm.c +) + _add_capi_test_executable(mlir-capi-pass-test pass.c ) diff --git a/mlir/test/CAPI/llvm.c b/mlir/test/CAPI/llvm.c new file mode 100644 --- /dev/null +++ b/mlir/test/CAPI/llvm.c @@ -0,0 +1,48 @@ +//===- llvm.c - Test of llvm APIs -----------------------------------------===// +// +// 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-llvm-test 2>&1 | FileCheck %s + +#include "mlir-c/Dialect/LLVM.h" +#include "mlir-c/IR.h" +#include "mlir-c/BuiltinTypes.h" + +#include +#include +#include +#include +#include + +// CHECK-LABEL: testTypeCreation() +static void testTypeCreation(MlirContext ctx) { + fprintf(stderr, "testTypeCreation()\n"); + MlirType i32 = mlirIntegerTypeGet(ctx, 32); + + const char *i32p_text = "!llvm.ptr"; + MlirType i32p = mlirLLVMPointerTypeGet(i32, 0); + MlirType i32p_ref = mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32p_text)); + // CHECK: !llvm.ptr: 1 + fprintf(stderr, "%s: %d\n", i32p_text, mlirTypeEqual(i32p, i32p_ref)); + + const char *i32p4_text = "!llvm.ptr"; + MlirType i32p4 = mlirLLVMPointerTypeGet(i32, 4); + MlirType i32p4_ref = mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32p4_text)); + // CHECK: !llvm.ptr: 1 + fprintf(stderr, "%s: %d\n", i32p4_text, mlirTypeEqual(i32p4, i32p4_ref)); +} + +int main() { + MlirContext ctx = mlirContextCreate(); + mlirDialectHandleRegisterDialect(mlirGetDialectHandle__llvm__(), ctx); + mlirContextGetOrLoadDialect(ctx, mlirStringRefCreateFromCString("llvm")); + testTypeCreation(ctx); + mlirContextDestroy(ctx); + return 0; +} + diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -66,6 +66,7 @@ FileCheck count not mlir-capi-execution-engine-test mlir-capi-ir-test + mlir-capi-llvm-test mlir-capi-pass-test mlir-capi-sparse-tensor-test mlir-cpu-runner