From 35d4593e6b555f852088211f5561c0e360c98c91 Mon Sep 17 00:00:00 2001 From: Adam Paszke Date: Fri, 2 Jul 2021 10:03:46 -0700 Subject: [PATCH] Add C API files for the LLVM dialect For now only expose a builder for the LLVM pointer type. Reviewed By: jpienaar, ftynse Differential Revision: https://reviews.llvm.org/D105346 --- mlir/include/mlir-c/Dialect/LLVM.h | 30 ++++++++++++++++++++++ mlir/lib/CAPI/Dialect/CMakeLists.txt | 9 +++++++ mlir/lib/CAPI/Dialect/LLVM.cpp | 21 ++++++++++++++++ mlir/test/CAPI/CMakeLists.txt | 4 +++ mlir/test/CAPI/llvm.c | 48 ++++++++++++++++++++++++++++++++++++ mlir/test/CMakeLists.txt | 1 + 6 files changed, 113 insertions(+) create mode 100644 mlir/include/mlir-c/Dialect/LLVM.h create mode 100644 mlir/lib/CAPI/Dialect/LLVM.cpp create mode 100644 mlir/test/CAPI/llvm.c diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h new file mode 100644 index 0000000..d3c5217 --- /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 index 053fce3..ab8ac73 100644 --- a/mlir/lib/CAPI/Dialect/CMakeLists.txt +++ b/mlir/lib/CAPI/Dialect/CMakeLists.txt @@ -27,6 +27,15 @@ add_mlir_public_c_api_library(MLIRCAPIGPU 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 index 0000000..be0e6c5 --- /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 index 6e377e5..a0a8129 100644 --- a/mlir/test/CAPI/CMakeLists.txt +++ b/mlir/test/CAPI/CMakeLists.txt @@ -25,6 +25,10 @@ _add_capi_test_executable(mlir-capi-ir-test 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 index 0000000..bbabb6f --- /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 index 416cfee..1340ec3 100644 --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -66,6 +66,7 @@ set(MLIR_TEST_DEPENDS 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 -- 2.7.4