llvm/mlir/include/mlir/ExecutionEngine/RunnerUtils.h

//===- RunnerUtils.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 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 MLIR_EXECUTIONENGINE_RUNNERUTILS_H
#define MLIR_EXECUTIONENGINE_RUNNERUTILS_H

#ifdef _WIN32
#ifndef MLIR_RUNNERUTILS_EXPORT
#ifdef mlir_runner_utils_EXPORTS
// We are building this library
#define MLIR_RUNNERUTILS_EXPORT
#else
// We are using this library
#define MLIR_RUNNERUTILS_EXPORT
#endif // mlir_runner_utils_EXPORTS
#endif // MLIR_RUNNERUTILS_EXPORT
#else
// Non-windows: use visibility attributes.
#define MLIR_RUNNERUTILS_EXPORT
#endif // _WIN32

#include <assert.h>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>

#include "mlir/ExecutionEngine/CRunnerUtils.h"
#include "mlir/ExecutionEngine/Float16bits.h"

template <typename T, typename StreamType>
void printMemRefMetaData(StreamType &os, const DynamicMemRefType<T> &v) {}

template <typename StreamType, typename T, int N>
void printMemRefMetaData(StreamType &os, StridedMemRefType<T, N> &v) {}

template <typename StreamType, typename T>
void printUnrankedMemRefMetaData(StreamType &os, UnrankedMemRefType<T> &v) {}

////////////////////////////////////////////////////////////////////////////////
// Templated instantiation follows.
////////////////////////////////////////////////////////////////////////////////
namespace impl {
index_type;
complex64;
complex32;

template <typename T, int M, int... Dims>
std::ostream &operator<<(std::ostream &os, const Vector<T, M, Dims...> &v);

template <int... Dims>
struct StaticSizeMult {};

StaticSizeMult<N, Dims...>;

static inline void printSpace(std::ostream &os, int count) {}

template <typename T, int M, int... Dims>
struct VectorDataPrinter {};

template <typename T, int M, int... Dims>
void VectorDataPrinter<T, M, Dims...>::print(std::ostream &os,
                                             const Vector<T, M, Dims...> &val) {}

template <typename T, int M, int... Dims>
std::ostream &operator<<(std::ostream &os, const Vector<T, M, Dims...> &v) {}

template <typename T>
struct MemRefDataPrinter {};

template <typename T>
void MemRefDataPrinter<T>::printFirst(std::ostream &os, T *base, int64_t dim,
                                      int64_t rank, int64_t offset,
                                      const int64_t *sizes,
                                      const int64_t *strides) {}

template <typename T>
void MemRefDataPrinter<T>::print(std::ostream &os, T *base, int64_t dim,
                                 int64_t rank, int64_t offset,
                                 const int64_t *sizes, const int64_t *strides) {}

template <typename T>
void MemRefDataPrinter<T>::printLast(std::ostream &os, T *base, int64_t dim,
                                     int64_t rank, int64_t offset,
                                     const int64_t *sizes,
                                     const int64_t *strides) {}

template <typename T, int N>
void printMemRefShape(StridedMemRefType<T, N> &m) {}

template <typename T>
void printMemRefShape(UnrankedMemRefType<T> &m) {}

template <typename T>
void printMemRef(const DynamicMemRefType<T> &m) {}

template <typename T, int N>
void printMemRef(StridedMemRefType<T, N> &m) {}

template <typename T>
void printMemRef(UnrankedMemRefType<T> &m) {}

/// Verify the result of two computations are equivalent up to a small
/// numerical error and return the number of errors.
template <typename T>
struct MemRefDataVerifier {};

template <typename T>
bool MemRefDataVerifier<T>::verifyRelErrorSmallerThan(T actual, T expected,
                                                      T epsilon) {}

template <typename T>
bool MemRefDataVerifier<T>::verifyElem(T actual, T expected) {}

template <>
inline bool MemRefDataVerifier<double>::verifyElem(double actual,
                                                   double expected) {}

template <>
inline bool MemRefDataVerifier<float>::verifyElem(float actual,
                                                  float expected) {}

template <typename T>
int64_t MemRefDataVerifier<T>::verify(std::ostream &os, T *actualBasePtr,
                                      T *expectedBasePtr, int64_t dim,
                                      int64_t offset, const int64_t *sizes,
                                      const int64_t *strides,
                                      int64_t &printCounter) {}

/// Verify the equivalence of two dynamic memrefs and return the number of
/// errors or -1 if the shape of the memrefs do not match.
template <typename T>
int64_t verifyMemRef(const DynamicMemRefType<T> &actual,
                     const DynamicMemRefType<T> &expected) {}

/// Verify the equivalence of two unranked memrefs and return the number of
/// errors or -1 if the shape of the memrefs do not match.
template <typename T>
int64_t verifyMemRef(UnrankedMemRefType<T> &actual,
                     UnrankedMemRefType<T> &expected) {}

} // namespace impl

////////////////////////////////////////////////////////////////////////////////
// Currently exposed C API.
////////////////////////////////////////////////////////////////////////////////
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeI8(UnrankedMemRefType<int8_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeI32(UnrankedMemRefType<int32_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeI64(UnrankedMemRefType<int64_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeF32(UnrankedMemRefType<float> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeF64(UnrankedMemRefType<double> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeInd(UnrankedMemRefType<impl::index_type> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeC32(UnrankedMemRefType<impl::complex32> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefShapeC64(UnrankedMemRefType<impl::complex64> *m);

extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefI8(UnrankedMemRefType<int8_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefI16(UnrankedMemRefType<int16_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefI32(UnrankedMemRefType<int32_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefI64(UnrankedMemRefType<int64_t> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefF16(UnrankedMemRefType<f16> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefBF16(UnrankedMemRefType<bf16> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefF32(UnrankedMemRefType<float> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefF64(UnrankedMemRefType<double> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefInd(UnrankedMemRefType<impl::index_type> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefC32(UnrankedMemRefType<impl::complex32> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemrefC64(UnrankedMemRefType<impl::complex64> *m);

extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_nanoTime();

extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefI32(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefI64(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefF32(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefF64(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefInd(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefC32(int64_t rank, void *ptr);
extern "C" MLIR_RUNNERUTILS_EXPORT void printMemrefC64(int64_t rank, void *ptr);

extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref0dF32(StridedMemRefType<float, 0> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dF32(StridedMemRefType<float, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref2dF32(StridedMemRefType<float, 2> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref3dF32(StridedMemRefType<float, 3> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref4dF32(StridedMemRefType<float, 4> *m);

extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dI8(StridedMemRefType<int8_t, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dI32(StridedMemRefType<int32_t, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dI64(StridedMemRefType<int64_t, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dF64(StridedMemRefType<double, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dInd(StridedMemRefType<impl::index_type, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dC32(StridedMemRefType<impl::complex32, 1> *m);
extern "C" MLIR_RUNNERUTILS_EXPORT void
_mlir_ciface_printMemref1dC64(StridedMemRefType<impl::complex64, 1> *m);

extern "C" MLIR_RUNNERUTILS_EXPORT void _mlir_ciface_printMemrefVector4x4xf32(
    StridedMemRefType<Vector2D<4, 4, float>, 2> *m);

extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI8(
    UnrankedMemRefType<int8_t> *actual, UnrankedMemRefType<int8_t> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI16(
    UnrankedMemRefType<int16_t> *actual, UnrankedMemRefType<int16_t> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI32(
    UnrankedMemRefType<int32_t> *actual, UnrankedMemRefType<int32_t> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefI64(
    UnrankedMemRefType<int64_t> *actual, UnrankedMemRefType<int64_t> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefBF16(
    UnrankedMemRefType<bf16> *actual, UnrankedMemRefType<bf16> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF16(
    UnrankedMemRefType<f16> *actual, UnrankedMemRefType<f16> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF32(
    UnrankedMemRefType<float> *actual, UnrankedMemRefType<float> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t _mlir_ciface_verifyMemRefF64(
    UnrankedMemRefType<double> *actual, UnrankedMemRefType<double> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t
_mlir_ciface_verifyMemRefInd(UnrankedMemRefType<impl::index_type> *actual,
                             UnrankedMemRefType<impl::index_type> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t
_mlir_ciface_verifyMemRefC32(UnrankedMemRefType<impl::complex32> *actual,
                             UnrankedMemRefType<impl::complex32> *expected);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t
_mlir_ciface_verifyMemRefC64(UnrankedMemRefType<impl::complex64> *actual,
                             UnrankedMemRefType<impl::complex64> *expected);

extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefI32(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefF32(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefF64(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefInd(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefC32(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);
extern "C" MLIR_RUNNERUTILS_EXPORT int64_t verifyMemRefC64(int64_t rank,
                                                           void *actualPtr,
                                                           void *expectedPtr);

#endif // MLIR_EXECUTIONENGINE_RUNNERUTILS_H