diff --git a/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h b/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h new file mode 100644 --- /dev/null +++ b/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h @@ -0,0 +1,89 @@ +//===- CRunnerUtils.h - Utils for debugging MLIR execution ----------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file declares basic classes and functions to manipulate structured MLIR +// types at runtime. Entities in this file are must be retargetable, including +// on targets without a C++ runtime. +// +//===----------------------------------------------------------------------===// + +#ifndef EXECUTIONENGINE_CRUNNERUTILS_H_ +#define EXECUTIONENGINE_CRUNNERUTILS_H_ + +#ifdef _WIN32 +#ifndef MLIR_CRUNNERUTILS_EXPORT +#ifdef mlir_c_runner_utils_EXPORTS +/* We are building this library */ +#define MLIR_CRUNNERUTILS_EXPORT __declspec(dllexport) +#else +/* We are using this library */ +#define MLIR_CRUNNERUTILS_EXPORT __declspec(dllimport) +#endif // mlir_c_runner_utils_EXPORTS +#endif // MLIR_CRUNNERUTILS_EXPORT +#else +#define MLIR_CRUNNERUTILS_EXPORT +#endif // _WIN32 + +#include + +template void dropFront(int64_t arr[N], int64_t *res) { + for (unsigned i = 1; i < N; ++i) + *(res + i - 1) = arr[i]; +} + +/// StridedMemRef descriptor type with static rank. +template struct StridedMemRefType { + T *basePtr; + T *data; + int64_t offset; + int64_t sizes[N]; + int64_t strides[N]; + // This operator[] is extremely slow and only for sugaring purposes. + StridedMemRefType operator[](int64_t idx) { + StridedMemRefType res; + res.basePtr = basePtr; + res.data = data; + res.offset = offset + idx * strides[0]; + dropFront(sizes, res.sizes); + dropFront(strides, res.strides); + return res; + } +}; + +/// StridedMemRef descriptor type specialized for rank 1. +template struct StridedMemRefType { + T *basePtr; + T *data; + int64_t offset; + int64_t sizes[1]; + int64_t strides[1]; + T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); } +}; + +/// StridedMemRef descriptor type specialized for rank 0. +template struct StridedMemRefType { + T *basePtr; + T *data; + int64_t offset; +}; + +// Unranked MemRef +template struct UnrankedMemRefType { + int64_t rank; + void *descriptor; +}; + +// Small runtime support "lib" for vector.print lowering. +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_f32(float f); +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_f64(double d); +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_open(); +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_close(); +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_comma(); +extern "C" MLIR_CRUNNERUTILS_EXPORT void print_newline(); + +#endif // EXECUTIONENGINE_CRUNNERUTILS_H_ diff --git a/mlir/include/mlir/ExecutionEngine/RunnerUtils.h b/mlir/include/mlir/ExecutionEngine/RunnerUtils.h --- a/mlir/include/mlir/ExecutionEngine/RunnerUtils.h +++ b/mlir/include/mlir/ExecutionEngine/RunnerUtils.h @@ -5,78 +5,35 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// +// +// This file declares basic classes and functions to debug structured MLIR +// types at runtime. Entities in this file may not be compatible with targets +// without a C++ runtime. These may be progressively migrated to CRunnerUtils.h +// over time. +// +//===----------------------------------------------------------------------===// #ifndef EXECUTIONENGINE_RUNNERUTILS_H_ #define EXECUTIONENGINE_RUNNERUTILS_H_ -#include -#include -#include - #ifdef _WIN32 -#ifndef MLIR_RUNNER_UTILS_EXPORT +#ifndef MLIR_RUNNERUTILS_EXPORT #ifdef mlir_runner_utils_EXPORTS /* We are building this library */ -#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllexport) +#define MLIR_RUNNERUTILS_EXPORT __declspec(dllexport) #else /* We are using this library */ -#define MLIR_RUNNER_UTILS_EXPORT __declspec(dllimport) +#define MLIR_RUNNERUTILS_EXPORT __declspec(dllimport) #endif // mlir_runner_utils_EXPORTS -#endif // MLIR_RUNNER_UTILS_EXPORT +#endif // MLIR_RUNNERUTILS_EXPORT #else -#define MLIR_RUNNER_UTILS_EXPORT +#define MLIR_RUNNERUTILS_EXPORT #endif // _WIN32 -template struct StridedMemRefType; -template -void printMemRefMetaData(StreamType &os, StridedMemRefType &V); - -template void dropFront(int64_t arr[N], int64_t *res) { - for (unsigned i = 1; i < N; ++i) - *(res + i - 1) = arr[i]; -} - -/// StridedMemRef descriptor type with static rank. -template struct StridedMemRefType { - T *basePtr; - T *data; - int64_t offset; - int64_t sizes[N]; - int64_t strides[N]; - // This operator[] is extremely slow and only for sugaring purposes. - StridedMemRefType operator[](int64_t idx) { - StridedMemRefType res; - res.basePtr = basePtr; - res.data = data; - res.offset = offset + idx * strides[0]; - dropFront(sizes, res.sizes); - dropFront(strides, res.strides); - return res; - } -}; - -/// StridedMemRef descriptor type specialized for rank 1. -template struct StridedMemRefType { - T *basePtr; - T *data; - int64_t offset; - int64_t sizes[1]; - int64_t strides[1]; - T &operator[](int64_t idx) { return *(data + offset + idx * strides[0]); } -}; - -/// StridedMemRef descriptor type specialized for rank 0. -template struct StridedMemRefType { - T *basePtr; - T *data; - int64_t offset; -}; +#include +#include -// Unranked MemRef -template struct UnrankedMemRefType { - int64_t rank; - void *descriptor; -}; +#include "mlir/ExecutionEngine/CRunnerUtils.h" template void printMemRefMetaData(StreamType &os, StridedMemRefType &V) { @@ -261,35 +218,27 @@ //////////////////////////////////////////////////////////////////////////////// // Currently exposed C API. //////////////////////////////////////////////////////////////////////////////// -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_i8(UnrankedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_f32(UnrankedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_memref_f32(int64_t rank, - void *ptr); +extern "C" MLIR_RUNNERUTILS_EXPORT void print_memref_f32(int64_t rank, + void *ptr); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_0d_f32(StridedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_1d_f32(StridedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_2d_f32(StridedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_3d_f32(StridedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_4d_f32(StridedMemRefType *M); -extern "C" MLIR_RUNNER_UTILS_EXPORT void +extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_print_memref_vector_4x4xf32( StridedMemRefType, 2> *M); -// Small runtime support "lib" for vector.print lowering. -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f32(float f); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_f64(double d); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_open(); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_close(); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_comma(); -extern "C" MLIR_RUNNER_UTILS_EXPORT void print_newline(); - #endif // EXECUTIONENGINE_RUNNERUTILS_H_ diff --git a/mlir/lib/ExecutionEngine/CMakeLists.txt b/mlir/lib/ExecutionEngine/CMakeLists.txt --- a/mlir/lib/ExecutionEngine/CMakeLists.txt +++ b/mlir/lib/ExecutionEngine/CMakeLists.txt @@ -1,4 +1,5 @@ set(LLVM_OPTIONAL_SOURCES + CRunnerUtils.cpp ExecutionEngine.cpp RunnerUtils.cpp OptUtils.cpp @@ -34,5 +35,12 @@ ${outlibs}) +add_llvm_library(mlir_c_runner_utils SHARED CRunnerUtils.cpp) +target_compile_definitions(mlir_c_runner_utils PRIVATE mlir_c_runner_utils_EXPORTS) + add_llvm_library(mlir_runner_utils SHARED RunnerUtils.cpp) +target_link_libraries(mlir_runner_utils + + mlir_c_runner_utils +) target_compile_definitions(mlir_runner_utils PRIVATE mlir_runner_utils_EXPORTS) diff --git a/mlir/lib/ExecutionEngine/CRunnerUtils.cpp b/mlir/lib/ExecutionEngine/CRunnerUtils.cpp new file mode 100644 --- /dev/null +++ b/mlir/lib/ExecutionEngine/CRunnerUtils.cpp @@ -0,0 +1,29 @@ +//===- CRunnerUtils.cpp - Utils for MLIR execution ------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// +// +// This file implements basic functions to manipulate structured MLIR types at +// runtime. Entities in this file are meant to be retargetable, including on +// targets without a C++ runtime, and must be kept C compatible. +// +//===----------------------------------------------------------------------===// + +#include +#include + +// Small runtime support "lib" for vector.print lowering. +// By providing elementary printing methods only, this +// library can remain fully unaware of low-level implementation +// details of our vectors. Also useful for direct LLVM IR output. +extern "C" void print_i32(int32_t i) { fprintf(stdout, "%" PRId32, i); } +extern "C" void print_i64(int64_t l) { fprintf(stdout, "%" PRId64, l); } +extern "C" void print_f32(float f) { fprintf(stdout, "%g", f); } +extern "C" void print_f64(double d) { fprintf(stdout, "%lg", d); } +extern "C" void print_open() { fputs("( ", stdout); } +extern "C" void print_close() { fputs(" )", stdout); } +extern "C" void print_comma() { fputs(", ", stdout); } +extern "C" void print_newline() { fputc('\n', stdout); } diff --git a/mlir/lib/ExecutionEngine/RunnerUtils.cpp b/mlir/lib/ExecutionEngine/RunnerUtils.cpp --- a/mlir/lib/ExecutionEngine/RunnerUtils.cpp +++ b/mlir/lib/ExecutionEngine/RunnerUtils.cpp @@ -1,4 +1,4 @@ -//===- RunnerUtils.cpp - Utils for MLIR CPU execution ---------------------===// +//===- RunnerUtils.cpp - Utils for MLIR exec on targets with a C++ runtime ===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. @@ -6,16 +6,15 @@ // //===----------------------------------------------------------------------===// // -// Utilities for interfacing MLIR types with C code as well as printing, -// debugging etc. +// This file imlpements basic functions to debug structured MLIR types at +// runtime. Entities in this file may not be compatible with targets without a +// C++ runtime. These may be progressively migrated to CRunnerUtils.cpp over +// time. // //===----------------------------------------------------------------------===// #include "mlir/ExecutionEngine/RunnerUtils.h" -#include -#include - extern "C" void _mlir_ciface_print_memref_vector_4x4xf32( StridedMemRefType, 2> *M) { impl::printMemRef(*M); @@ -85,16 +84,3 @@ _mlir_ciface_print_memref_4d_f32(StridedMemRefType *M) { impl::printMemRef(*M); } - -// Small runtime support "lib" for vector.print lowering. -// By providing elementary printing methods only, this -// library can remain fully unaware of low-level implementation -// details of our vectors. Also useful for direct LLVM IR output. -extern "C" void print_i32(int32_t i) { fprintf(stdout, "%" PRId32, i); } -extern "C" void print_i64(int64_t l) { fprintf(stdout, "%" PRId64, l); } -extern "C" void print_f32(float f) { fprintf(stdout, "%g", f); } -extern "C" void print_f64(double d) { fprintf(stdout, "%lg", d); } -extern "C" void print_open() { fputs("( ", stdout); } -extern "C" void print_close() { fputs(" )", stdout); } -extern "C" void print_comma() { fputs(", ", stdout); } -extern "C" void print_newline() { fputc('\n', stdout); } diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -41,6 +41,7 @@ cblas cblas_interface mlir_runner_utils + mlir_c_runner_utils ) if(LLVM_BUILD_EXAMPLES) diff --git a/mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir b/mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir --- a/mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir +++ b/mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir @@ -1,4 +1,4 @@ -// RUN: mlir-opt %s -convert-loop-to-std -convert-std-to-llvm='use-bare-ptr-memref-call-conv=1' | mlir-cpu-runner -shared-libs=%linalg_test_lib_dir/libmlir_runner_utils%shlibext -entry-point-result=void | FileCheck %s +// RUN: mlir-opt %s -convert-loop-to-std -convert-std-to-llvm='use-bare-ptr-memref-call-conv=1' | mlir-cpu-runner -shared-libs=%linalg_test_lib_dir/libmlir_c_runner_utils%shlibext -entry-point-result=void | FileCheck %s // Verify bare pointer memref calling convention. `simple_add1_add2_test` // gets two 2xf32 memrefs, adds 1.0f to the first one and 2.0f to the second