[Mlir-commits] [mlir] 4a966e5 - [mlir] NFC - Split out RunnerUtils that don't require a C++ runtime

Nicolas Vasilache llvmlistbot at llvm.org
Thu Feb 27 11:14:48 PST 2020


Author: Nicolas Vasilache
Date: 2020-02-27T14:14:11-05:00
New Revision: 4a966e5dd75160a7df0c7231e4c760a2bb127112

URL: https://github.com/llvm/llvm-project/commit/4a966e5dd75160a7df0c7231e4c760a2bb127112
DIFF: https://github.com/llvm/llvm-project/commit/4a966e5dd75160a7df0c7231e4c760a2bb127112.diff

LOG: [mlir] NFC - Split out RunnerUtils that don't require a C++ runtime

Summary:
This revision split out a new CRunnerUtils library that supports
MLIR execution on targets without a C++ runtime.

Differential Revision: https://reviews.llvm.org/D75257

Added: 
    mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
    mlir/lib/ExecutionEngine/CRunnerUtils.cpp

Modified: 
    mlir/include/mlir/ExecutionEngine/RunnerUtils.h
    mlir/lib/ExecutionEngine/CMakeLists.txt
    mlir/lib/ExecutionEngine/RunnerUtils.cpp
    mlir/test/CMakeLists.txt
    mlir/test/mlir-cpu-runner/bare_ptr_call_conv.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h b/mlir/include/mlir/ExecutionEngine/CRunnerUtils.h
new file mode 100644
index 000000000000..9f0dbf8eba12
--- /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 <cstdint>
+
+template <int N> 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 <typename T, int N> 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<T, N - 1> operator[](int64_t idx) {
+    StridedMemRefType<T, N - 1> res;
+    res.basePtr = basePtr;
+    res.data = data;
+    res.offset = offset + idx * strides[0];
+    dropFront<N>(sizes, res.sizes);
+    dropFront<N>(strides, res.strides);
+    return res;
+  }
+};
+
+/// StridedMemRef descriptor type specialized for rank 1.
+template <typename T> struct StridedMemRefType<T, 1> {
+  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 <typename T> struct StridedMemRefType<T, 0> {
+  T *basePtr;
+  T *data;
+  int64_t offset;
+};
+
+// Unranked MemRef
+template <typename T> 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
index 13d44d9c78d2..feb38ec829b5 100644
--- 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 <assert.h>
-#include <cstdint>
-#include <iostream>
-
 #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 <typename T, int N> struct StridedMemRefType;
-template <typename StreamType, typename T, int N>
-void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V);
-
-template <int N> 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 <typename T, int N> 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<T, N - 1> operator[](int64_t idx) {
-    StridedMemRefType<T, N - 1> res;
-    res.basePtr = basePtr;
-    res.data = data;
-    res.offset = offset + idx * strides[0];
-    dropFront<N>(sizes, res.sizes);
-    dropFront<N>(strides, res.strides);
-    return res;
-  }
-};
-
-/// StridedMemRef descriptor type specialized for rank 1.
-template <typename T> struct StridedMemRefType<T, 1> {
-  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 <typename T> struct StridedMemRefType<T, 0> {
-  T *basePtr;
-  T *data;
-  int64_t offset;
-};
+#include <assert.h>
+#include <iostream>
 
-// Unranked MemRef
-template <typename T> struct UnrankedMemRefType {
-  int64_t rank;
-  void *descriptor;
-};
+#include "mlir/ExecutionEngine/CRunnerUtils.h"
 
 template <typename StreamType, typename T, int N>
 void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &V) {
@@ -261,35 +218,27 @@ template <typename T> void printMemRef(StridedMemRefType<T, 0> &M) {
 ////////////////////////////////////////////////////////////////////////////////
 // Currently exposed C API.
 ////////////////////////////////////////////////////////////////////////////////
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_i8(UnrankedMemRefType<int8_t> *M);
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_f32(UnrankedMemRefType<float> *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<float, 0> *M);
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_1d_f32(StridedMemRefType<float, 1> *M);
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_2d_f32(StridedMemRefType<float, 2> *M);
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_3d_f32(StridedMemRefType<float, 3> *M);
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *M);
 
-extern "C" MLIR_RUNNER_UTILS_EXPORT void
+extern "C" MLIR_RUNNERUTILS_EXPORT void
 _mlir_ciface_print_memref_vector_4x4xf32(
     StridedMemRefType<Vector2D<4, 4, float>, 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
index bf93c33b7021..dc20e5fd4622 100644
--- 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 @@ target_link_libraries(MLIRExecutionEngine
 
   ${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
index 000000000000..a57958c66000
--- /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 <cinttypes>
+#include <cstdio>
+
+// 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
index 9ba29ff6b0fe..3f345e054b2a 100644
--- 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 <cinttypes>
-#include <cstdio>
-
 extern "C" void _mlir_ciface_print_memref_vector_4x4xf32(
     StridedMemRefType<Vector2D<4, 4, float>, 2> *M) {
   impl::printMemRef(*M);
@@ -85,16 +84,3 @@ extern "C" void
 _mlir_ciface_print_memref_4d_f32(StridedMemRefType<float, 4> *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
index e008bd62f892..97584f81d270 100644
--- a/mlir/test/CMakeLists.txt
+++ b/mlir/test/CMakeLists.txt
@@ -41,6 +41,7 @@ set(MLIR_TEST_DEPENDS
   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
index a0022afac7e7..1760feb365f0 100644
--- 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


        


More information about the Mlir-commits mailing list