[Mlir-commits] [mlir] 35d4593 - Add C API files for the LLVM dialect
Jacques Pienaar
llvmlistbot at llvm.org
Fri Jul 2 11:56:08 PDT 2021
Author: Adam Paszke
Date: 2021-07-02T11:55:44-07:00
New Revision: 35d4593e6b555f852088211f5561c0e360c98c91
URL: https://github.com/llvm/llvm-project/commit/35d4593e6b555f852088211f5561c0e360c98c91
DIFF: https://github.com/llvm/llvm-project/commit/35d4593e6b555f852088211f5561c0e360c98c91.diff
LOG: 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
Added:
mlir/include/mlir-c/Dialect/LLVM.h
mlir/lib/CAPI/Dialect/LLVM.cpp
mlir/test/CAPI/llvm.c
Modified:
mlir/lib/CAPI/Dialect/CMakeLists.txt
mlir/test/CAPI/CMakeLists.txt
mlir/test/CMakeLists.txt
Removed:
################################################################################
diff --git a/mlir/include/mlir-c/Dialect/LLVM.h b/mlir/include/mlir-c/Dialect/LLVM.h
new file mode 100644
index 0000000000000..d3c5217eace07
--- /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 053fce30d5ddc..ab8ac73c7c53b 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 0000000000000..be0e6c5d0ca57
--- /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 6e377e5b987c5..a0a812936a99f 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 0000000000000..bbabb6f18898a
--- /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 <assert.h>
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+// CHECK-LABEL: testTypeCreation()
+static void testTypeCreation(MlirContext ctx) {
+ fprintf(stderr, "testTypeCreation()\n");
+ MlirType i32 = mlirIntegerTypeGet(ctx, 32);
+
+ const char *i32p_text = "!llvm.ptr<i32>";
+ MlirType i32p = mlirLLVMPointerTypeGet(i32, 0);
+ MlirType i32p_ref = mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32p_text));
+ // CHECK: !llvm.ptr<i32>: 1
+ fprintf(stderr, "%s: %d\n", i32p_text, mlirTypeEqual(i32p, i32p_ref));
+
+ const char *i32p4_text = "!llvm.ptr<i32, 4>";
+ MlirType i32p4 = mlirLLVMPointerTypeGet(i32, 4);
+ MlirType i32p4_ref = mlirTypeParseGet(ctx, mlirStringRefCreateFromCString(i32p4_text));
+ // CHECK: !llvm.ptr<i32, 4>: 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 416cfee7efade..1340ec3f786e2 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
More information about the Mlir-commits
mailing list