forked from M-Labs/nac3
WIP
This commit is contained in:
parent
4209ad0dff
commit
43e9a9539d
|
@ -0,0 +1,3 @@
|
||||||
|
#!/usr/bin/env bash
|
||||||
|
clang-irrt --target=wasm32 -x c++ -fno-discard-value-names -fno-exceptions -fno-rtti -O0 -emit-llvm -S -Wall -Wextra nac3core/irrt/irrt.cpp
|
||||||
|
clang -x c++ -fno-discard-value-names -fno-exceptions -fno-rtti -O0 -emit-llvm -S -Wall -Wextra nac3core/irrt/irrt_test.cpp
|
|
@ -41,7 +41,7 @@
|
||||||
'';
|
'';
|
||||||
installPhase =
|
installPhase =
|
||||||
''
|
''
|
||||||
PYTHON_SITEPACKAGES=$out/${pkgs.python3Packages.python.sitePackages}
|
u PYTHON_SITEPACKAGES=$out/${pkgs.python3Packages.python.sitePackages}
|
||||||
mkdir -p $PYTHON_SITEPACKAGES
|
mkdir -p $PYTHON_SITEPACKAGES
|
||||||
cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $PYTHON_SITEPACKAGES/nac3artiq.so
|
cp target/x86_64-unknown-linux-gnu/release/libnac3artiq.so $PYTHON_SITEPACKAGES/nac3artiq.so
|
||||||
|
|
||||||
|
@ -163,7 +163,10 @@
|
||||||
clippy
|
clippy
|
||||||
pre-commit
|
pre-commit
|
||||||
rustfmt
|
rustfmt
|
||||||
|
rust-analyzer
|
||||||
];
|
];
|
||||||
|
# https://nixos.wiki/wiki/Rust#Shell.nix_example
|
||||||
|
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||||
};
|
};
|
||||||
devShells.x86_64-linux.msys2 = pkgs.mkShell {
|
devShells.x86_64-linux.msys2 = pkgs.mkShell {
|
||||||
name = "nac3-dev-shell-msys2";
|
name = "nac3-dev-shell-msys2";
|
||||||
|
|
|
@ -31,6 +31,7 @@ fn compile_irrt(irrt_dir: &Path, out_dir: &Path) {
|
||||||
"-S",
|
"-S",
|
||||||
"-Wall",
|
"-Wall",
|
||||||
"-Wextra",
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
"-I",
|
"-I",
|
||||||
irrt_dir.to_str().unwrap(),
|
irrt_dir.to_str().unwrap(),
|
||||||
"-o",
|
"-o",
|
||||||
|
@ -100,6 +101,7 @@ fn compile_irrt_test(irrt_dir: &Path, out_dir: &Path) {
|
||||||
"-O0",
|
"-O0",
|
||||||
"-Wall",
|
"-Wall",
|
||||||
"-Wextra",
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
"-lm", // for `tgamma()`, `lgamma()`
|
"-lm", // for `tgamma()`, `lgamma()`
|
||||||
"-o",
|
"-o",
|
||||||
exe_path.to_str().unwrap(),
|
exe_path.to_str().unwrap(),
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
#include "irrt.hpp"
|
#include "irrt_everything.hpp"
|
||||||
|
|
||||||
// All the implementations are from `irrt.hpp`
|
/*
|
||||||
|
This file will be read by `clang-irrt` to conveniently produce LLVM IR for `nac3core/codegen`.
|
||||||
|
*/
|
||||||
|
|
|
@ -1,28 +1,19 @@
|
||||||
#ifndef IRRT_DONT_TYPEDEF_INTS
|
#pragma once
|
||||||
typedef _BitInt(8) int8_t;
|
|
||||||
typedef unsigned _BitInt(8) uint8_t;
|
#include "irrt_utils.hpp"
|
||||||
typedef _BitInt(32) int32_t;
|
#include "irrt_typedefs.hpp"
|
||||||
typedef unsigned _BitInt(32) uint32_t;
|
|
||||||
typedef _BitInt(64) int64_t;
|
/*
|
||||||
typedef unsigned _BitInt(64) uint64_t;
|
This header contains IRRT implementations
|
||||||
#endif
|
that do not deserved to be categorized (e.g., into numpy, etc.)
|
||||||
|
|
||||||
|
Check out other *.hpp files before including them here!!
|
||||||
|
*/
|
||||||
|
|
||||||
// NDArray indices are always `uint32_t`.
|
|
||||||
typedef uint32_t NDIndex;
|
|
||||||
// The type of an index or a value describing the length of a range/slice is
|
// The type of an index or a value describing the length of a range/slice is
|
||||||
// always `int32_t`.
|
// always `int32_t`.
|
||||||
typedef int32_t SliceIndex;
|
typedef int32_t SliceIndex;
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static T max(T a, T b) {
|
|
||||||
return a > b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
static T min(T a, T b) {
|
|
||||||
return a > b ? b : a;
|
|
||||||
}
|
|
||||||
|
|
||||||
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
// adapted from GNU Scientific Library: https://git.savannah.gnu.org/cgit/gsl.git/tree/sys/pow_int.c
|
||||||
// need to make sure `exp >= 0` before calling this function
|
// need to make sure `exp >= 0` before calling this function
|
||||||
template <typename T>
|
template <typename T>
|
||||||
|
@ -39,119 +30,6 @@ static T __nac3_int_exp_impl(T base, T exp) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename SizeT>
|
|
||||||
static SizeT __nac3_ndarray_calc_size_impl(
|
|
||||||
const SizeT *list_data,
|
|
||||||
SizeT list_len,
|
|
||||||
SizeT begin_idx,
|
|
||||||
SizeT end_idx
|
|
||||||
) {
|
|
||||||
__builtin_assume(end_idx <= list_len);
|
|
||||||
|
|
||||||
SizeT num_elems = 1;
|
|
||||||
for (SizeT i = begin_idx; i < end_idx; ++i) {
|
|
||||||
SizeT val = list_data[i];
|
|
||||||
__builtin_assume(val > 0);
|
|
||||||
num_elems *= val;
|
|
||||||
}
|
|
||||||
return num_elems;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename SizeT>
|
|
||||||
static void __nac3_ndarray_calc_nd_indices_impl(
|
|
||||||
SizeT index,
|
|
||||||
const SizeT *dims,
|
|
||||||
SizeT num_dims,
|
|
||||||
NDIndex *idxs
|
|
||||||
) {
|
|
||||||
SizeT stride = 1;
|
|
||||||
for (SizeT dim = 0; dim < num_dims; dim++) {
|
|
||||||
SizeT i = num_dims - dim - 1;
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
idxs[i] = (index / stride) % dims[i];
|
|
||||||
stride *= dims[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename SizeT>
|
|
||||||
static SizeT __nac3_ndarray_flatten_index_impl(
|
|
||||||
const SizeT *dims,
|
|
||||||
SizeT num_dims,
|
|
||||||
const NDIndex *indices,
|
|
||||||
SizeT num_indices
|
|
||||||
) {
|
|
||||||
SizeT idx = 0;
|
|
||||||
SizeT stride = 1;
|
|
||||||
for (SizeT i = 0; i < num_dims; ++i) {
|
|
||||||
SizeT ri = num_dims - i - 1;
|
|
||||||
if (ri < num_indices) {
|
|
||||||
idx += stride * indices[ri];
|
|
||||||
}
|
|
||||||
|
|
||||||
__builtin_assume(dims[i] > 0);
|
|
||||||
stride *= dims[ri];
|
|
||||||
}
|
|
||||||
return idx;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename SizeT>
|
|
||||||
static void __nac3_ndarray_calc_broadcast_impl(
|
|
||||||
const SizeT *lhs_dims,
|
|
||||||
SizeT lhs_ndims,
|
|
||||||
const SizeT *rhs_dims,
|
|
||||||
SizeT rhs_ndims,
|
|
||||||
SizeT *out_dims
|
|
||||||
) {
|
|
||||||
SizeT max_ndims = lhs_ndims > rhs_ndims ? lhs_ndims : rhs_ndims;
|
|
||||||
|
|
||||||
for (SizeT i = 0; i < max_ndims; ++i) {
|
|
||||||
const SizeT *lhs_dim_sz = i < lhs_ndims ? &lhs_dims[lhs_ndims - i - 1] : nullptr;
|
|
||||||
const SizeT *rhs_dim_sz = i < rhs_ndims ? &rhs_dims[rhs_ndims - i - 1] : nullptr;
|
|
||||||
SizeT *out_dim = &out_dims[max_ndims - i - 1];
|
|
||||||
|
|
||||||
if (lhs_dim_sz == nullptr) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (rhs_dim_sz == nullptr) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == 1) {
|
|
||||||
*out_dim = *rhs_dim_sz;
|
|
||||||
} else if (*rhs_dim_sz == 1) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else if (*lhs_dim_sz == *rhs_dim_sz) {
|
|
||||||
*out_dim = *lhs_dim_sz;
|
|
||||||
} else {
|
|
||||||
__builtin_unreachable();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename SizeT>
|
|
||||||
static void __nac3_ndarray_calc_broadcast_idx_impl(
|
|
||||||
const SizeT *src_dims,
|
|
||||||
SizeT src_ndims,
|
|
||||||
const NDIndex *in_idx,
|
|
||||||
NDIndex *out_idx
|
|
||||||
) {
|
|
||||||
for (SizeT i = 0; i < src_ndims; ++i) {
|
|
||||||
SizeT src_i = src_ndims - i - 1;
|
|
||||||
out_idx[src_i] = src_dims[src_i] == 1 ? 0 : in_idx[src_i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename SizeT>
|
|
||||||
static void __nac3_ndarray_strides_from_shape_impl(
|
|
||||||
SizeT ndims,
|
|
||||||
SizeT *shape,
|
|
||||||
SizeT *dst_strides
|
|
||||||
) {
|
|
||||||
SizeT stride_product = 1;
|
|
||||||
for (SizeT i = 0; i < ndims; i++) {
|
|
||||||
int dim_i = ndims - i - 1;
|
|
||||||
dst_strides[dim_i] = stride_product;
|
|
||||||
stride_product *= shape[dim_i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#define DEF_nac3_int_exp_(T) \
|
#define DEF_nac3_int_exp_(T) \
|
||||||
T __nac3_int_exp_##T(T base, T exp) {\
|
T __nac3_int_exp_##T(T base, T exp) {\
|
||||||
|
@ -334,104 +212,4 @@ extern "C" {
|
||||||
|
|
||||||
return j0(x);
|
return j0(x);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t __nac3_ndarray_calc_size(
|
|
||||||
const uint32_t *list_data,
|
|
||||||
uint32_t list_len,
|
|
||||||
uint32_t begin_idx,
|
|
||||||
uint32_t end_idx
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t __nac3_ndarray_calc_size64(
|
|
||||||
const uint64_t *list_data,
|
|
||||||
uint64_t list_len,
|
|
||||||
uint64_t begin_idx,
|
|
||||||
uint64_t end_idx
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_calc_size_impl(list_data, list_len, begin_idx, end_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_nd_indices(
|
|
||||||
uint32_t index,
|
|
||||||
const uint32_t* dims,
|
|
||||||
uint32_t num_dims,
|
|
||||||
NDIndex* idxs
|
|
||||||
) {
|
|
||||||
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_nd_indices64(
|
|
||||||
uint64_t index,
|
|
||||||
const uint64_t* dims,
|
|
||||||
uint64_t num_dims,
|
|
||||||
NDIndex* idxs
|
|
||||||
) {
|
|
||||||
__nac3_ndarray_calc_nd_indices_impl(index, dims, num_dims, idxs);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint32_t __nac3_ndarray_flatten_index(
|
|
||||||
const uint32_t* dims,
|
|
||||||
uint32_t num_dims,
|
|
||||||
const NDIndex* indices,
|
|
||||||
uint32_t num_indices
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t __nac3_ndarray_flatten_index64(
|
|
||||||
const uint64_t* dims,
|
|
||||||
uint64_t num_dims,
|
|
||||||
const NDIndex* indices,
|
|
||||||
uint64_t num_indices
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_flatten_index_impl(dims, num_dims, indices, num_indices);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast(
|
|
||||||
const uint32_t *lhs_dims,
|
|
||||||
uint32_t lhs_ndims,
|
|
||||||
const uint32_t *rhs_dims,
|
|
||||||
uint32_t rhs_ndims,
|
|
||||||
uint32_t *out_dims
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast64(
|
|
||||||
const uint64_t *lhs_dims,
|
|
||||||
uint64_t lhs_ndims,
|
|
||||||
const uint64_t *rhs_dims,
|
|
||||||
uint64_t rhs_ndims,
|
|
||||||
uint64_t *out_dims
|
|
||||||
) {
|
|
||||||
return __nac3_ndarray_calc_broadcast_impl(lhs_dims, lhs_ndims, rhs_dims, rhs_ndims, out_dims);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast_idx(
|
|
||||||
const uint32_t *src_dims,
|
|
||||||
uint32_t src_ndims,
|
|
||||||
const NDIndex *in_idx,
|
|
||||||
NDIndex *out_idx
|
|
||||||
) {
|
|
||||||
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_calc_broadcast_idx64(
|
|
||||||
const uint64_t *src_dims,
|
|
||||||
uint64_t src_ndims,
|
|
||||||
const NDIndex *in_idx,
|
|
||||||
NDIndex *out_idx
|
|
||||||
) {
|
|
||||||
__nac3_ndarray_calc_broadcast_idx_impl(src_dims, src_ndims, in_idx, out_idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_strides_from_shape(uint32_t ndims, uint32_t* shape, uint32_t* dst_strides) {
|
|
||||||
__nac3_ndarray_strides_from_shape_impl(ndims, shape, dst_strides);
|
|
||||||
}
|
|
||||||
|
|
||||||
void __nac3_ndarray_strides_from_shape64(uint64_t ndims, uint64_t* shape, uint64_t* dst_strides) {
|
|
||||||
__nac3_ndarray_strides_from_shape_impl(ndims, shape, dst_strides);
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_basic.hpp"
|
||||||
|
#include "irrt_numpy_ndarray.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
All IRRT implementations.
|
||||||
|
|
||||||
|
We don't have any pre-compiled objects, so we are writing all implementations in headers and
|
||||||
|
concatenate them with `#include` into one massive source file that contains all the IRRT stuff.
|
||||||
|
*/
|
|
@ -0,0 +1,196 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "irrt_utils.hpp"
|
||||||
|
#include "irrt_typedefs.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
|
NDArray-related implementations.
|
||||||
|
`*/
|
||||||
|
|
||||||
|
// NDArray indices are always `uint32_t`.
|
||||||
|
using NDIndex = uint32_t;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
namespace ndarray_util {
|
||||||
|
// Compute the strides of an ndarray given an ndarray `shape`
|
||||||
|
// and assuming that the ndarray is *fully C-contagious*.
|
||||||
|
//
|
||||||
|
// You might want to read up on https://ajcr.net/stride-guide-part-1/.
|
||||||
|
template <typename SizeT>
|
||||||
|
static void set_strides_by_shape(SizeT ndims, SizeT* dst_strides, const SizeT* shape) {
|
||||||
|
SizeT stride_product = 1;
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
int dim_i = ndims - i - 1;
|
||||||
|
dst_strides[dim_i] = stride_product;
|
||||||
|
stride_product *= shape[dim_i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the size/# of elements of an ndarray given its shape
|
||||||
|
template <typename SizeT>
|
||||||
|
static SizeT calc_size_from_shape(SizeT ndims, const SizeT* shape) {
|
||||||
|
SizeT size = 1;
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++) size *= shape[dim_i];
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
struct NDArrayIndicesIter {
|
||||||
|
SizeT ndims;
|
||||||
|
const SizeT *shape;
|
||||||
|
SizeT *indices;
|
||||||
|
|
||||||
|
void set_indices_zero() {
|
||||||
|
__builtin_memset(indices, 0, sizeof(SizeT) * ndims);
|
||||||
|
}
|
||||||
|
|
||||||
|
void next() {
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
SizeT dim_i = ndims - i - 1;
|
||||||
|
|
||||||
|
indices[dim_i]++;
|
||||||
|
if (indices[dim_i] < shape[dim_i]) {
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
indices[dim_i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// The NDArray object. `SizeT` is the *signed* size type of this ndarray.
|
||||||
|
//
|
||||||
|
// NOTE: The order of fields is IMPORTANT. DON'T TOUCH IT
|
||||||
|
//
|
||||||
|
// Some resources you might find helpful:
|
||||||
|
// - The official numpy implementations:
|
||||||
|
// - https://github.com/numpy/numpy/blob/735a477f0bc2b5b84d0e72d92f224bde78d4e069/doc/source/reference/c-api/types-and-structures.rst
|
||||||
|
// - On strides (about reshaping, slicing, C-contagiousness, etc)
|
||||||
|
// - https://ajcr.net/stride-guide-part-1/.
|
||||||
|
// - https://ajcr.net/stride-guide-part-2/.
|
||||||
|
// - https://ajcr.net/stride-guide-part-3/.
|
||||||
|
template <typename SizeT>
|
||||||
|
struct NDArray {
|
||||||
|
// The underlying data this `ndarray` is pointing to.
|
||||||
|
//
|
||||||
|
// NOTE: Formally this should be of type `void *`, but clang
|
||||||
|
// translates `void *` to `i8 *` when run with `-S -emit-llvm`,
|
||||||
|
// so we will put `uint8_t *` here for clarity.
|
||||||
|
uint8_t *data;
|
||||||
|
|
||||||
|
// The number of bytes of a single element in `data`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
SizeT itemsize;
|
||||||
|
|
||||||
|
// The number of dimensions of this shape.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
SizeT ndims;
|
||||||
|
|
||||||
|
// Array shape, with length equal to `ndims`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `unsigned`.
|
||||||
|
//
|
||||||
|
// NOTE: `shape` can contain 0.
|
||||||
|
// (those appear when the user makes an out of bounds slice into an ndarray, e.g., `np.zeros((3, 3))[400:].shape == (0, 3)`)
|
||||||
|
SizeT *shape;
|
||||||
|
|
||||||
|
// Array strides (stride value is in number of bytes, NOT number of elements), with length equal to `ndims`.
|
||||||
|
//
|
||||||
|
// The `SizeT` is treated as `signed`.
|
||||||
|
//
|
||||||
|
// NOTE: `strides` can have negative numbers.
|
||||||
|
// (those appear when there is a slice with a negative step, e.g., `my_array[::-1]`)
|
||||||
|
SizeT *strides;
|
||||||
|
|
||||||
|
// Calculate the size/# of elements of an `ndarray`.
|
||||||
|
// This function corresponds to `np.size(<ndarray>)` or `ndarray.size`
|
||||||
|
SizeT size() {
|
||||||
|
return ndarray_util::calc_size_from_shape(ndims, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate the number of bytes of its content of an `ndarray` *in its view*.
|
||||||
|
// This function corresponds to `ndarray.nbytes`
|
||||||
|
SizeT nbytes() {
|
||||||
|
return this->size() * itemsize;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_value_at_pelement(uint8_t* pelement, uint8_t* pvalue) {
|
||||||
|
__builtin_memcpy(pelement, pvalue, itemsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* get_pelement(SizeT *indices) {
|
||||||
|
uint8_t* element = data;
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++)
|
||||||
|
element += indices[dim_i] * strides[dim_i] * itemsize;
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Is the given `indices` valid/in-bounds?
|
||||||
|
bool in_bounds(SizeT *indices) {
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++) {
|
||||||
|
bool dim_ok = indices[dim_i] < shape[dim_i];
|
||||||
|
if (!dim_ok) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the ndarray with a value
|
||||||
|
void fill_generic(uint8_t* pvalue) {
|
||||||
|
NDArrayIndicesIter<SizeT> iter;
|
||||||
|
iter.ndims = this->ndims;
|
||||||
|
iter.shape = this->shape;
|
||||||
|
iter.indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * ndims);
|
||||||
|
iter.set_indices_zero();
|
||||||
|
|
||||||
|
for (SizeT i = 0; i < this->size(); i++, iter.next()) {
|
||||||
|
uint8_t* pelement = get_pelement(iter.indices);
|
||||||
|
set_value_at_pelement(pelement, pvalue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the strides of the ndarray with `ndarray_util::set_strides_by_shape`
|
||||||
|
void set_strides_by_shape() {
|
||||||
|
ndarray_util::set_strides_by_shape(ndims, strides, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
// https://numpy.org/doc/stable/reference/generated/numpy.eye.html
|
||||||
|
void set_to_eye(SizeT k, uint8_t* zero_pvalue, uint8_t* one_pvalue) {
|
||||||
|
__builtin_assume(ndims == 2);
|
||||||
|
|
||||||
|
// TODO: Better implementation
|
||||||
|
|
||||||
|
fill_generic(zero_pvalue);
|
||||||
|
for (SizeT i = 0; i < min(shape[0], shape[1]); i++) {
|
||||||
|
SizeT row = i;
|
||||||
|
SizeT col = i + k;
|
||||||
|
SizeT indices[2] = { row, col };
|
||||||
|
|
||||||
|
if (!in_bounds(indices)) continue;
|
||||||
|
|
||||||
|
uint8_t* pelement = get_pelement(indices);
|
||||||
|
set_value_at_pelement(pelement, one_pvalue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
uint32_t __nac3_ndarray_size(NDArray<int32_t>* ndarray) {
|
||||||
|
return ndarray->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t __nac3_ndarray_size64(NDArray<int64_t>* ndarray) {
|
||||||
|
return ndarray->size();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_fill_generic(NDArray<int32_t>* ndarray, uint8_t* pvalue) {
|
||||||
|
ndarray->fill_generic(pvalue);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_fill_generic64(NDArray<int64_t>* ndarray, uint8_t* pvalue) {
|
||||||
|
ndarray->fill_generic(pvalue);
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,17 +2,21 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
|
||||||
|
// set `IRRT_DONT_TYPEDEF_INTS` because `cstdint` has it all
|
||||||
#define IRRT_DONT_TYPEDEF_INTS
|
#define IRRT_DONT_TYPEDEF_INTS
|
||||||
#include "irrt.hpp"
|
#include "irrt_everything.hpp"
|
||||||
|
|
||||||
static void __test_fail(const char *file, int line) {
|
namespace {
|
||||||
// NOTE: Try to make the location info follow a format that
|
static void test_fail() {
|
||||||
// VSCode/other IDEs would recognize as a clickable URL.
|
printf("[!] Test failed\n");
|
||||||
printf("[!] test_fail() invoked at %s:%d", file, line);
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define test_fail() __test_fail(__FILE__, __LINE__);
|
static void __begin_test(const char* function_name, const char* file, int line) {
|
||||||
|
printf("######### Running %s @ %s:%d\n", function_name, file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define BEGIN_TEST() __begin_test(__FUNCTION__, __FILE__, __LINE__)
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool arrays_match(int len, T *as, T *bs) {
|
bool arrays_match(int len, T *as, T *bs) {
|
||||||
|
@ -23,40 +27,163 @@ bool arrays_match(int len, T *as, T *bs) {
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void debug_print_array(const char* format, int len, T *as) {
|
void debug_print_array(const char* format, int len, T* as) {
|
||||||
printf("[");
|
printf("[");
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
if (i != 0) printf(", ");
|
if (i != 0) printf(", ");
|
||||||
printf(format, as[i]);
|
printf(format, as[i]);
|
||||||
}
|
}
|
||||||
printf("]\n");
|
printf("]");
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
bool assert_arrays_match(const char *label, const char *format, int len, T *expected, T *got) {
|
void assert_arrays_match(const char* label, const char* format, int len, T* expected, T* got) {
|
||||||
auto match = arrays_match(len, expected, got);
|
if (!arrays_match(len, expected, got)) {
|
||||||
|
|
||||||
if (!match) {
|
|
||||||
printf("expected %s: ", label);
|
printf("expected %s: ", label);
|
||||||
debug_print_array(format, len, expected);
|
debug_print_array(format, len, expected);
|
||||||
|
printf("\n");
|
||||||
printf("got %s: ", label);
|
printf("got %s: ", label);
|
||||||
debug_print_array(format, len, got);
|
debug_print_array(format, len, got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
}
|
}
|
||||||
|
|
||||||
return match;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_strides_from_shape() {
|
template <typename T>
|
||||||
const uint64_t ndims = 4;
|
void assert_values_match(const char* label, const char* format, T expected, T got) {
|
||||||
uint64_t shape[ndims] = { 999, 3, 5, 7 };
|
if (expected != got) {
|
||||||
uint64_t strides[ndims] = { 0 };
|
printf("expected %s: ", label);
|
||||||
__nac3_ndarray_strides_from_shape64(ndims, shape, strides);
|
printf(format, expected);
|
||||||
|
printf("\n");
|
||||||
|
printf("got %s: ", label);
|
||||||
|
printf(format, got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uint64_t expected_strides[ndims] = { 3*5*7, 5*7, 7, 1 };
|
void test_calc_size_from_shape_normal() {
|
||||||
if (!assert_arrays_match("strides", "%u", ndims, expected_strides, strides)) test_fail();
|
// Test shapes with normal values
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 2, 3, 5, 7 };
|
||||||
|
debug_print_array("%d", 4, shape);
|
||||||
|
assert_values_match("size", "%d", 210, ndarray_util::calc_size_from_shape<int32_t>(4, shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_calc_size_from_shape_has_zero() {
|
||||||
|
// Test shapes with 0 in them
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 2, 0, 5, 7 };
|
||||||
|
assert_values_match("size", "%d", 0, ndarray_util::calc_size_from_shape<int32_t>(4, shape));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_set_strides_by_shape() {
|
||||||
|
// Test `set_strides_by_shape()`
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[4] = { 99, 3, 5, 7 };
|
||||||
|
int32_t strides[4] = { 0 };
|
||||||
|
ndarray_util::set_strides_by_shape(4, strides, shape);
|
||||||
|
|
||||||
|
int32_t expected_strides[4] = { 105, 35, 7, 1 };
|
||||||
|
assert_arrays_match("strides", "%u", 4u, expected_strides, strides);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_indices_iter_normal() {
|
||||||
|
// Test NDArrayIndicesIter normal behavior
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
int32_t shape[3] = { 1, 2, 3 };
|
||||||
|
int32_t indices[3] = { 0, 0, 0 };
|
||||||
|
auto iter = NDArrayIndicesIter<int32_t> {
|
||||||
|
.ndims = 3u,
|
||||||
|
.shape = shape,
|
||||||
|
.indices = indices
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_arrays_match("indices #0", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 0 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #1", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 1 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #2", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 2 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #3", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 0 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #4", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 1 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #5", "%u", 3u, iter.indices, (int32_t[3]) { 0, 1, 2 });
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #6", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 0 }); // Loops back
|
||||||
|
iter.next();
|
||||||
|
assert_arrays_match("indices #7", "%u", 3u, iter.indices, (int32_t[3]) { 0, 0, 1 });
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_fill_generic() {
|
||||||
|
// Test ndarray fill_generic
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
// Choose a type that's neither int32_t nor uint64_t (candidates of SizeT) to spice it up
|
||||||
|
// Also make all the octets non-zero, to see if `memcpy` in `fill_generic` is working perfectly.
|
||||||
|
uint16_t fill_value = 0xFACE;
|
||||||
|
|
||||||
|
uint16_t in_data[6] = { 100, 101, 102, 103, 104, 105 }; // Fill `data` with values that != `999`
|
||||||
|
int32_t in_itemsize = sizeof(uint16_t);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 2, 3 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides,
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
ndarray.fill_generic((uint8_t*) &fill_value); // `fill_generic` here
|
||||||
|
|
||||||
|
uint16_t expected_data[6] = { fill_value, fill_value, fill_value, fill_value, fill_value, fill_value };
|
||||||
|
assert_arrays_match("data", "0x%hX", 6, expected_data, in_data);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ndarray_set_to_eye() {
|
||||||
|
double in_data[9] = { 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0, 99.0 };
|
||||||
|
int32_t in_itemsize = sizeof(double);
|
||||||
|
const int32_t in_ndims = 2;
|
||||||
|
int32_t in_shape[in_ndims] = { 3, 3 };
|
||||||
|
int32_t in_strides[in_ndims] = {};
|
||||||
|
NDArray<int32_t> ndarray = {
|
||||||
|
.data = (uint8_t*) in_data,
|
||||||
|
.itemsize = in_itemsize,
|
||||||
|
.ndims = in_ndims,
|
||||||
|
.shape = in_shape,
|
||||||
|
.strides = in_strides,
|
||||||
|
};
|
||||||
|
ndarray.set_strides_by_shape();
|
||||||
|
|
||||||
|
double zero = 0.0;
|
||||||
|
double one = 1.0;
|
||||||
|
ndarray.set_to_eye(1, (uint8_t*) &zero, (uint8_t*) &one);
|
||||||
|
|
||||||
|
assert_values_match("in_data[0]", "%f", 0.0, in_data[0]);
|
||||||
|
assert_values_match("in_data[1]", "%f", 1.0, in_data[1]);
|
||||||
|
assert_values_match("in_data[2]", "%f", 0.0, in_data[2]);
|
||||||
|
assert_values_match("in_data[3]", "%f", 0.0, in_data[3]);
|
||||||
|
assert_values_match("in_data[4]", "%f", 0.0, in_data[4]);
|
||||||
|
assert_values_match("in_data[5]", "%f", 1.0, in_data[5]);
|
||||||
|
assert_values_match("in_data[6]", "%f", 0.0, in_data[6]);
|
||||||
|
assert_values_match("in_data[7]", "%f", 0.0, in_data[7]);
|
||||||
|
assert_values_match("in_data[8]", "%f", 0.0, in_data[8]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
test_strides_from_shape();
|
test_calc_size_from_shape_normal();
|
||||||
|
test_calc_size_from_shape_has_zero();
|
||||||
|
test_set_strides_by_shape();
|
||||||
|
test_ndarray_indices_iter_normal();
|
||||||
|
test_ndarray_fill_generic();
|
||||||
|
test_ndarray_set_to_eye();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// This is made toggleable since `irrt_test.cpp` itself would include
|
||||||
|
// headers that define the `int_t` family.
|
||||||
|
#ifndef IRRT_DONT_TYPEDEF_INTS
|
||||||
|
typedef _BitInt(8) int8_t;
|
||||||
|
typedef unsigned _BitInt(8) uint8_t;
|
||||||
|
typedef _BitInt(32) int32_t;
|
||||||
|
typedef unsigned _BitInt(32) uint32_t;
|
||||||
|
typedef _BitInt(64) int64_t;
|
||||||
|
typedef unsigned _BitInt(64) uint64_t;
|
||||||
|
#endif
|
|
@ -0,0 +1,11 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T max(T a, T b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T min(T a, T b) {
|
||||||
|
return a > b ? b : a;
|
||||||
|
}
|
|
@ -702,53 +702,54 @@ pub fn call_numpy_min<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
BasicValueEnum::PointerValue(n)
|
BasicValueEnum::PointerValue(n)
|
||||||
if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
|
if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
|
||||||
{
|
{
|
||||||
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty);
|
todo!()
|
||||||
let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty);
|
// let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty);
|
||||||
|
// let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty);
|
||||||
|
|
||||||
let n = NDArrayValue::from_ptr_val(n, llvm_usize, None);
|
// let n = NDArrayValue::from_ptr_val(n, llvm_usize, None);
|
||||||
let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.dim_sizes(), (None, None));
|
// let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.dim_sizes(), (None, None));
|
||||||
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
// if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
||||||
let n_sz_eqz = ctx
|
// let n_sz_eqz = ctx
|
||||||
.builder
|
// .builder
|
||||||
.build_int_compare(IntPredicate::NE, n_sz, n_sz.get_type().const_zero(), "")
|
// .build_int_compare(IntPredicate::NE, n_sz, n_sz.get_type().const_zero(), "")
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
|
|
||||||
ctx.make_assert(
|
// ctx.make_assert(
|
||||||
generator,
|
// generator,
|
||||||
n_sz_eqz,
|
// n_sz_eqz,
|
||||||
"0:ValueError",
|
// "0:ValueError",
|
||||||
"zero-size array to reduction operation minimum which has no identity",
|
// "zero-size array to reduction operation minimum which has no identity",
|
||||||
[None, None, None],
|
// [None, None, None],
|
||||||
ctx.current_loc,
|
// ctx.current_loc,
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
|
|
||||||
let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?;
|
// let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?;
|
||||||
unsafe {
|
// unsafe {
|
||||||
let identity =
|
// let identity =
|
||||||
n.data().get_unchecked(ctx, generator, &llvm_usize.const_zero(), None);
|
// n.data().get_unchecked(ctx, generator, &llvm_usize.const_zero(), None);
|
||||||
ctx.builder.build_store(accumulator_addr, identity).unwrap();
|
// ctx.builder.build_store(accumulator_addr, identity).unwrap();
|
||||||
}
|
// }
|
||||||
|
|
||||||
gen_for_callback_incrementing(
|
// gen_for_callback_incrementing(
|
||||||
generator,
|
// generator,
|
||||||
ctx,
|
// ctx,
|
||||||
llvm_usize.const_int(1, false),
|
// llvm_usize.const_int(1, false),
|
||||||
(n_sz, false),
|
// (n_sz, false),
|
||||||
|generator, ctx, _, idx| {
|
// |generator, ctx, _, idx| {
|
||||||
let elem = unsafe { n.data().get_unchecked(ctx, generator, &idx, None) };
|
// let elem = unsafe { n.data().get_unchecked(ctx, generator, &idx, None) };
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
// let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
||||||
let result = call_min(ctx, (elem_ty, accumulator), (elem_ty, elem));
|
// let result = call_min(ctx, (elem_ty, accumulator), (elem_ty, elem));
|
||||||
ctx.builder.build_store(accumulator_addr, result).unwrap();
|
// ctx.builder.build_store(accumulator_addr, result).unwrap();
|
||||||
|
|
||||||
Ok(())
|
// Ok(())
|
||||||
},
|
// },
|
||||||
llvm_usize.const_int(1, false),
|
// llvm_usize.const_int(1, false),
|
||||||
)?;
|
// )?;
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
// let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
||||||
accumulator
|
// accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
||||||
|
@ -920,53 +921,54 @@ pub fn call_numpy_max<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
BasicValueEnum::PointerValue(n)
|
BasicValueEnum::PointerValue(n)
|
||||||
if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
|
if a_ty.obj_id(&ctx.unifier).is_some_and(|id| id == PrimDef::NDArray.id()) =>
|
||||||
{
|
{
|
||||||
let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty);
|
todo!()
|
||||||
let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty);
|
// let (elem_ty, _) = unpack_ndarray_var_tys(&mut ctx.unifier, a_ty);
|
||||||
|
// let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty);
|
||||||
|
|
||||||
let n = NDArrayValue::from_ptr_val(n, llvm_usize, None);
|
// let n = NDArrayValue::from_ptr_val(n, llvm_usize, None);
|
||||||
let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.dim_sizes(), (None, None));
|
// let n_sz = irrt::call_ndarray_calc_size(generator, ctx, &n.dim_sizes(), (None, None));
|
||||||
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
// if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
||||||
let n_sz_eqz = ctx
|
// let n_sz_eqz = ctx
|
||||||
.builder
|
// .builder
|
||||||
.build_int_compare(IntPredicate::NE, n_sz, n_sz.get_type().const_zero(), "")
|
// .build_int_compare(IntPredicate::NE, n_sz, n_sz.get_type().const_zero(), "")
|
||||||
.unwrap();
|
// .unwrap();
|
||||||
|
|
||||||
ctx.make_assert(
|
// ctx.make_assert(
|
||||||
generator,
|
// generator,
|
||||||
n_sz_eqz,
|
// n_sz_eqz,
|
||||||
"0:ValueError",
|
// "0:ValueError",
|
||||||
"zero-size array to reduction operation minimum which has no identity",
|
// "zero-size array to reduction operation minimum which has no identity",
|
||||||
[None, None, None],
|
// [None, None, None],
|
||||||
ctx.current_loc,
|
// ctx.current_loc,
|
||||||
);
|
// );
|
||||||
}
|
// }
|
||||||
|
|
||||||
let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?;
|
// let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?;
|
||||||
unsafe {
|
// unsafe {
|
||||||
let identity =
|
// let identity =
|
||||||
n.data().get_unchecked(ctx, generator, &llvm_usize.const_zero(), None);
|
// n.data().get_unchecked(ctx, generator, &llvm_usize.const_zero(), None);
|
||||||
ctx.builder.build_store(accumulator_addr, identity).unwrap();
|
// ctx.builder.build_store(accumulator_addr, identity).unwrap();
|
||||||
}
|
// }
|
||||||
|
|
||||||
gen_for_callback_incrementing(
|
// gen_for_callback_incrementing(
|
||||||
generator,
|
// generator,
|
||||||
ctx,
|
// ctx,
|
||||||
llvm_usize.const_int(1, false),
|
// llvm_usize.const_int(1, false),
|
||||||
(n_sz, false),
|
// (n_sz, false),
|
||||||
|generator, ctx, _, idx| {
|
// |generator, ctx, _, idx| {
|
||||||
let elem = unsafe { n.data().get_unchecked(ctx, generator, &idx, None) };
|
// let elem = unsafe { n.data().get_unchecked(ctx, generator, &idx, None) };
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
// let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
||||||
let result = call_max(ctx, (elem_ty, accumulator), (elem_ty, elem));
|
// let result = call_max(ctx, (elem_ty, accumulator), (elem_ty, elem));
|
||||||
ctx.builder.build_store(accumulator_addr, result).unwrap();
|
// ctx.builder.build_store(accumulator_addr, result).unwrap();
|
||||||
|
|
||||||
Ok(())
|
// Ok(())
|
||||||
},
|
// },
|
||||||
llvm_usize.const_int(1, false),
|
// llvm_usize.const_int(1, false),
|
||||||
)?;
|
// )?;
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
// let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
||||||
accumulator
|
// accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use crate::codegen::{
|
use crate::codegen::{
|
||||||
irrt::{call_ndarray_calc_size, call_ndarray_flatten_index},
|
llvm_intrinsics::call_int_umin, stmt::gen_for_callback_incrementing, CodeGenContext,
|
||||||
llvm_intrinsics::call_int_umin,
|
CodeGenerator,
|
||||||
stmt::gen_for_callback_incrementing,
|
|
||||||
CodeGenContext, CodeGenerator,
|
|
||||||
};
|
};
|
||||||
use inkwell::context::Context;
|
use inkwell::context::Context;
|
||||||
use inkwell::types::{ArrayType, BasicType, StructType};
|
use inkwell::types::{ArrayType, BasicType, StructType};
|
||||||
|
@ -12,6 +10,7 @@ use inkwell::{
|
||||||
values::{BasicValueEnum, IntValue, PointerValue},
|
values::{BasicValueEnum, IntValue, PointerValue},
|
||||||
AddressSpace, IntPredicate,
|
AddressSpace, IntPredicate,
|
||||||
};
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
/// A LLVM type that is used to represent a non-primitive type in NAC3.
|
/// A LLVM type that is used to represent a non-primitive type in NAC3.
|
||||||
pub trait ProxyType<'ctx>: Into<Self::Base> {
|
pub trait ProxyType<'ctx>: Into<Self::Base> {
|
||||||
|
@ -1601,7 +1600,8 @@ impl<'ctx> ArrayLikeValue<'ctx> for NDArrayDataProxy<'ctx, '_> {
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
generator: &G,
|
generator: &G,
|
||||||
) -> IntValue<'ctx> {
|
) -> IntValue<'ctx> {
|
||||||
call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
|
todo!()
|
||||||
|
// call_ndarray_calc_size(generator, ctx, &self.as_slice_value(ctx, generator), (None, None))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1675,17 +1675,19 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> ArrayLikeIndexer<'ctx, Index>
|
||||||
indices_elem_ty.get_bit_width()
|
indices_elem_ty.get_bit_width()
|
||||||
);
|
);
|
||||||
|
|
||||||
let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
|
todo!()
|
||||||
|
|
||||||
unsafe {
|
// let index = call_ndarray_flatten_index(generator, ctx, *self.0, indices);
|
||||||
ctx.builder
|
|
||||||
.build_in_bounds_gep(
|
// unsafe {
|
||||||
self.base_ptr(ctx, generator),
|
// ctx.builder
|
||||||
&[index],
|
// .build_in_bounds_gep(
|
||||||
name.unwrap_or_default(),
|
// self.base_ptr(ctx, generator),
|
||||||
)
|
// &[index],
|
||||||
.unwrap()
|
// name.unwrap_or_default(),
|
||||||
}
|
// )
|
||||||
|
// .unwrap()
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
fn ptr_offset<G: CodeGenerator + ?Sized>(
|
||||||
|
@ -1761,3 +1763,307 @@ impl<'ctx, Index: UntypedArrayLikeAccessor<'ctx>> UntypedArrayLikeMutator<'ctx,
|
||||||
for NDArrayDataProxy<'ctx, '_>
|
for NDArrayDataProxy<'ctx, '_>
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct StructField<'ctx> {
|
||||||
|
/// The GEP index of this struct field.
|
||||||
|
pub gep_index: u32,
|
||||||
|
/// Name of this struct field.
|
||||||
|
///
|
||||||
|
/// Used for generating names.
|
||||||
|
pub name: &'static str,
|
||||||
|
/// The type of this struct field.
|
||||||
|
pub ty: BasicTypeEnum<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct StructFields<'ctx> {
|
||||||
|
/// Name of the struct.
|
||||||
|
///
|
||||||
|
/// Used for generating names.
|
||||||
|
pub name: &'static str,
|
||||||
|
|
||||||
|
/// All the [`StructField`]s of this struct.
|
||||||
|
///
|
||||||
|
/// **NOTE:** The index position of a [`StructField`]
|
||||||
|
/// matches the element's [`StructField::index`].
|
||||||
|
pub fields: Vec<StructField<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct StructFieldsBuilder<'ctx> {
|
||||||
|
gep_index_counter: u32,
|
||||||
|
/// Name of the struct to be built.
|
||||||
|
name: &'static str,
|
||||||
|
fields: Vec<StructField<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructField<'ctx> {
|
||||||
|
pub fn gep(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) -> PointerValue<'ctx> {
|
||||||
|
ctx.builder.build_struct_gep(ptr, self.gep_index, self.name).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) -> BasicValueEnum<'ctx> {
|
||||||
|
ctx.builder.build_load(self.gep(ctx, ptr), self.name).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store<V>(&self, ctx: &CodeGenContext<'ctx, '_>, ptr: PointerValue<'ctx>, value: V)
|
||||||
|
where
|
||||||
|
V: BasicValue<'ctx>,
|
||||||
|
{
|
||||||
|
ctx.builder.build_store(ptr, value).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type IsInstanceError = String;
|
||||||
|
type IsInstanceResult = Result<(), IsInstanceError>;
|
||||||
|
|
||||||
|
pub fn check_basic_types_match<'ctx, A, B>(expected: A, got: B) -> IsInstanceResult
|
||||||
|
where
|
||||||
|
A: BasicType<'ctx>,
|
||||||
|
B: BasicType<'ctx>,
|
||||||
|
{
|
||||||
|
let expected = expected.as_basic_type_enum();
|
||||||
|
let got = got.as_basic_type_enum();
|
||||||
|
|
||||||
|
// Put those logic into here,
|
||||||
|
// otherwise there is always a fallback reporting on any kind of mismatch
|
||||||
|
match (expected, got) {
|
||||||
|
(BasicTypeEnum::IntType(expected), BasicTypeEnum::IntType(got)) => {
|
||||||
|
if expected.get_bit_width() != got.get_bit_width() {
|
||||||
|
return Err(format!(
|
||||||
|
"Expected IntType ({expected}-bit(s)), got IntType ({got}-bit(s))"
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(expected, got) => {
|
||||||
|
if expected != got {
|
||||||
|
return Err(format!("Expected {expected}, got {got}"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructFields<'ctx> {
|
||||||
|
pub fn num_fields(&self) -> u32 {
|
||||||
|
self.fields.len() as u32
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_struct_type(&self, ctx: &'ctx Context) -> StructType<'ctx> {
|
||||||
|
let llvm_fields = self.fields.iter().map(|field| field.ty).collect_vec();
|
||||||
|
ctx.struct_type(llvm_fields.as_slice(), false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_type(&self, scrutinee: StructType<'ctx>) -> IsInstanceResult {
|
||||||
|
// Check scrutinee's number of struct fields
|
||||||
|
if scrutinee.count_fields() != self.num_fields() {
|
||||||
|
return Err(format!(
|
||||||
|
"Expected {expected_count} field(s) in `{struct_name}` type, got {got_count}",
|
||||||
|
struct_name = self.name,
|
||||||
|
expected_count = self.num_fields(),
|
||||||
|
got_count = scrutinee.count_fields(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the scrutinee's field types
|
||||||
|
for field in self.fields.iter() {
|
||||||
|
let expected_field_ty = field.ty;
|
||||||
|
let got_field_ty = scrutinee.get_field_type_at_index(field.gep_index).unwrap();
|
||||||
|
|
||||||
|
if let Err(field_err) = check_basic_types_match(expected_field_ty, got_field_ty) {
|
||||||
|
return Err(format!(
|
||||||
|
"Field GEP index {gep_index} does not match the expected type of ({struct_name}::{field_name}): {field_err}",
|
||||||
|
gep_index = field.gep_index,
|
||||||
|
struct_name = self.name,
|
||||||
|
field_name = field.name,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructFieldsBuilder<'ctx> {
|
||||||
|
fn start(name: &'static str) -> Self {
|
||||||
|
StructFieldsBuilder { gep_index_counter: 0, name, fields: Vec::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_field(&mut self, name: &'static str, ty: BasicTypeEnum<'ctx>) -> StructField<'ctx> {
|
||||||
|
let index = self.gep_index_counter;
|
||||||
|
self.gep_index_counter += 1;
|
||||||
|
StructField { gep_index: index, name, ty }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end(self) -> StructFields<'ctx> {
|
||||||
|
StructFields { name: self.name, fields: self.fields }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct NpArrayType<'ctx> {
|
||||||
|
pub size_type: IntType<'ctx>,
|
||||||
|
pub elem_type: BasicTypeEnum<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct NpArrayStructFields<'ctx> {
|
||||||
|
pub whole_struct: StructFields<'ctx>,
|
||||||
|
pub data: StructField<'ctx>,
|
||||||
|
pub itemsize: StructField<'ctx>,
|
||||||
|
pub ndims: StructField<'ctx>,
|
||||||
|
pub shape: StructField<'ctx>,
|
||||||
|
pub strides: StructField<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> NpArrayType<'ctx> {
|
||||||
|
pub fn new_opaque_elem(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
size_type: IntType<'ctx>,
|
||||||
|
) -> NpArrayType<'ctx> {
|
||||||
|
NpArrayType { size_type, elem_type: ctx.ctx.i8_type().as_basic_type_enum() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn struct_type(&self, ctx: &CodeGenContext<'ctx, '_>) -> StructType<'ctx> {
|
||||||
|
self.fields().whole_struct.as_struct_type(ctx.ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn fields(&self) -> NpArrayStructFields<'ctx> {
|
||||||
|
let mut builder = StructFieldsBuilder::start("NpArray");
|
||||||
|
|
||||||
|
let addrspace = AddressSpace::default();
|
||||||
|
|
||||||
|
let byte_type = self.size_type.get_context().i8_type();
|
||||||
|
|
||||||
|
// Make sure the struct matches PERFECTLY with that defined in `nac3core/irrt`.
|
||||||
|
let data = builder.add_field("data", byte_type.ptr_type(addrspace).into());
|
||||||
|
let itemsize = builder.add_field("itemsize", self.size_type.into());
|
||||||
|
let ndims = builder.add_field("ndims", self.size_type.into());
|
||||||
|
let shape = builder.add_field("shape", self.size_type.ptr_type(addrspace).into());
|
||||||
|
let strides = builder.add_field("strides", self.size_type.ptr_type(addrspace).into());
|
||||||
|
|
||||||
|
NpArrayStructFields { whole_struct: builder.end(), data, itemsize, ndims, shape, strides }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allocate an `ndarray` on stack, with the following notes:
|
||||||
|
///
|
||||||
|
/// - `ndarray.ndims` will be initialized to `in_ndims`.
|
||||||
|
/// - `ndarray.itemsize` will be initialized to the size of `self.elem_type.size_of()`.
|
||||||
|
/// - `ndarray.shape` and `ndarray.strides` will be allocated on the stack with number of elements being `in_ndims`,
|
||||||
|
/// all with empty/uninitialized values.
|
||||||
|
pub fn alloca(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
in_ndims: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> NpArrayValue<'ctx> {
|
||||||
|
let fields = self.fields();
|
||||||
|
let ptr =
|
||||||
|
ctx.builder.build_alloca(fields.whole_struct.as_struct_type(ctx.ctx), name).unwrap();
|
||||||
|
|
||||||
|
// Allocate `in_dims` number of `size_type` on the stack for `shape` and `strides`
|
||||||
|
let allocated_shape =
|
||||||
|
ctx.builder.build_array_alloca(fields.shape.ty, in_ndims, "allocated_shape").unwrap();
|
||||||
|
let allocated_strides = ctx
|
||||||
|
.builder
|
||||||
|
.build_array_alloca(fields.strides.ty, in_ndims, "allocated_strides")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let value = NpArrayValue { ty: *self, ptr };
|
||||||
|
value.store_ndims(ctx, in_ndims);
|
||||||
|
value.store_itemsize(ctx, self.elem_type.size_of().unwrap());
|
||||||
|
value.store_shape(ctx, allocated_shape);
|
||||||
|
value.store_strides(ctx, allocated_strides);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct NpArrayValue<'ctx> {
|
||||||
|
pub ty: NpArrayType<'ctx>,
|
||||||
|
pub ptr: PointerValue<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> NpArrayValue<'ctx> {
|
||||||
|
pub fn load_ndims(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
||||||
|
let field = self.ty.fields().ndims;
|
||||||
|
field.load(ctx, self.ptr).into_int_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_ndims(&self, ctx: &CodeGenContext<'ctx, '_>, value: IntValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().ndims;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_itemsize(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
||||||
|
let field = self.ty.fields().itemsize;
|
||||||
|
field.load(ctx, self.ptr).into_int_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_itemsize(&self, ctx: &CodeGenContext<'ctx, '_>, value: IntValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().itemsize;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_shape(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.load(ctx, self.ptr).into_pointer_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_shape(&self, ctx: &CodeGenContext<'ctx, '_>, value: PointerValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_strides(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.load(ctx, self.ptr).into_pointer_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn store_strides(&self, ctx: &CodeGenContext<'ctx, '_>, value: PointerValue<'ctx>) {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.store(ctx, self.ptr, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: DOCUMENT ME -- NDIMS WOULD NEVER CHANGE!!!!!
|
||||||
|
pub fn shape_slice(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
||||||
|
let field = self.ty.fields().shape;
|
||||||
|
field.gep(ctx, self.ptr);
|
||||||
|
|
||||||
|
let ndims = self.load_ndims(ctx);
|
||||||
|
|
||||||
|
TypedArrayLikeAdapter {
|
||||||
|
adapted: ArraySliceValue(self.ptr, ndims, Some(field.name)),
|
||||||
|
downcast_fn: Box::new(|_ctx, x| x.into_int_value()),
|
||||||
|
upcast_fn: Box::new(|_ctx, x| x.as_basic_value_enum()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: DOCUMENT ME -- NDIMS WOULD NEVER CHANGE!!!!!
|
||||||
|
pub fn strides_slice(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
||||||
|
let field = self.ty.fields().strides;
|
||||||
|
field.gep(ctx, self.ptr);
|
||||||
|
|
||||||
|
let ndims = self.load_ndims(ctx);
|
||||||
|
|
||||||
|
TypedArrayLikeAdapter {
|
||||||
|
adapted: ArraySliceValue(self.ptr, ndims, Some(field.name)),
|
||||||
|
downcast_fn: Box::new(|_ctx, x| x.into_int_value()),
|
||||||
|
upcast_fn: Box::new(|_ctx, x| x.as_basic_value_enum()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,10 +1,14 @@
|
||||||
use crate::typecheck::typedef::Type;
|
use crate::{
|
||||||
|
codegen::classes::{NDArrayType, NpArrayType},
|
||||||
|
typecheck::typedef::Type,
|
||||||
|
util::SizeVariant,
|
||||||
|
};
|
||||||
|
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
classes::{
|
classes::{
|
||||||
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue,
|
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayValue, NpArrayValue,
|
||||||
TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
|
TypedArrayLikeAdapter, UntypedArrayLikeAccessor,
|
||||||
},
|
},
|
||||||
llvm_intrinsics, CodeGenContext, CodeGenerator,
|
llvm_intrinsics, CodeGenContext, CodeGenerator,
|
||||||
|
@ -16,8 +20,8 @@ use inkwell::{
|
||||||
context::Context,
|
context::Context,
|
||||||
memory_buffer::MemoryBuffer,
|
memory_buffer::MemoryBuffer,
|
||||||
module::Module,
|
module::Module,
|
||||||
types::{BasicTypeEnum, IntType},
|
types::{BasicType, BasicTypeEnum, FunctionType, IntType, PointerType},
|
||||||
values::{BasicValueEnum, CallSiteValue, FloatValue, IntValue},
|
values::{BasicValueEnum, CallSiteValue, FloatValue, FunctionValue, IntValue, PointerValue},
|
||||||
AddressSpace, IntPredicate,
|
AddressSpace, IntPredicate,
|
||||||
};
|
};
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
|
@ -565,367 +569,62 @@ pub fn call_j0<'ctx>(ctx: &CodeGenContext<'ctx, '_>, v: FloatValue<'ctx>) -> Flo
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a call to `__nac3_ndarray_calc_size`. Returns an [`IntValue`] representing the
|
fn get_size_variant<'ctx>(ty: IntType<'ctx>) -> SizeVariant {
|
||||||
/// calculated total size.
|
match ty.get_bit_width() {
|
||||||
///
|
32 => SizeVariant::Bits32,
|
||||||
/// * `dims` - An [`ArrayLikeIndexer`] containing the size of each dimension.
|
64 => SizeVariant::Bits64,
|
||||||
/// * `range` - The dimension index to begin and end (exclusively) calculating the dimensions for,
|
_ => unreachable!("Unsupported int type bit width {}", ty.get_bit_width()),
|
||||||
/// or [`None`] if starting from the first dimension and ending at the last dimension respectively.
|
}
|
||||||
pub fn call_ndarray_calc_size<'ctx, G, Dims>(
|
}
|
||||||
generator: &G,
|
|
||||||
|
fn get_size_type_dependent_function<'ctx, BuildFuncTypeFn>(
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
dims: &Dims,
|
size_type: IntType<'ctx>,
|
||||||
(begin, end): (Option<IntValue<'ctx>>, Option<IntValue<'ctx>>),
|
base_name: &str,
|
||||||
) -> IntValue<'ctx>
|
build_func_type: BuildFuncTypeFn,
|
||||||
|
) -> FunctionValue<'ctx>
|
||||||
where
|
where
|
||||||
G: CodeGenerator + ?Sized,
|
BuildFuncTypeFn: Fn() -> FunctionType<'ctx>,
|
||||||
Dims: ArrayLikeIndexer<'ctx>,
|
|
||||||
{
|
{
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let mut fn_name = base_name.to_owned();
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
match get_size_variant(size_type) {
|
||||||
|
SizeVariant::Bits32 => {
|
||||||
|
// The original fn_name is the correct function name
|
||||||
|
}
|
||||||
|
SizeVariant::Bits64 => {
|
||||||
|
// Append "64" at the end, this is the naming convention for 64-bit
|
||||||
|
fn_name.push_str("64");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let ndarray_calc_size_fn_name = match llvm_usize.get_bit_width() {
|
// Get (or declare then get if does not exist) the corresponding function
|
||||||
32 => "__nac3_ndarray_calc_size",
|
ctx.module.get_function(&fn_name).unwrap_or_else(|| {
|
||||||
64 => "__nac3_ndarray_calc_size64",
|
let fn_type = build_func_type();
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
ctx.module.add_function(&fn_name, fn_type, None)
|
||||||
};
|
})
|
||||||
let ndarray_calc_size_fn_t = llvm_usize.fn_type(
|
}
|
||||||
&[llvm_pusize.into(), llvm_usize.into(), llvm_usize.into(), llvm_usize.into()],
|
|
||||||
false,
|
fn get_ndarray_struct_ptr<'ctx>(ctx: &'ctx Context, size_type: IntType<'ctx>) -> PointerType<'ctx> {
|
||||||
);
|
let i8_type = ctx.i8_type();
|
||||||
let ndarray_calc_size_fn =
|
|
||||||
ctx.module.get_function(ndarray_calc_size_fn_name).unwrap_or_else(|| {
|
let ndarray_ty = NpArrayType { size_type, elem_type: i8_type.as_basic_type_enum() };
|
||||||
ctx.module.add_function(ndarray_calc_size_fn_name, ndarray_calc_size_fn_t, None)
|
let struct_ty = ndarray_ty.fields().whole_struct.as_struct_type(ctx);
|
||||||
});
|
struct_ty.ptr_type(AddressSpace::default())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_size<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray: NpArrayValue<'ctx>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
let size_type = ndarray.ty.size_type;
|
||||||
|
let function = get_size_type_dependent_function(ctx, size_type, "__nac3_ndarray_size", || {
|
||||||
|
size_type.fn_type(&[get_ndarray_struct_ptr(ctx.ctx, size_type).into()], false)
|
||||||
|
});
|
||||||
|
|
||||||
let begin = begin.unwrap_or_else(|| llvm_usize.const_zero());
|
|
||||||
let end = end.unwrap_or_else(|| dims.size(ctx, generator));
|
|
||||||
ctx.builder
|
ctx.builder
|
||||||
.build_call(
|
.build_call(function, &[ndarray.ptr.into()], "size")
|
||||||
ndarray_calc_size_fn,
|
|
||||||
&[
|
|
||||||
dims.base_ptr(ctx, generator).into(),
|
|
||||||
dims.size(ctx, generator).into(),
|
|
||||||
begin.into(),
|
|
||||||
end.into(),
|
|
||||||
],
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
.try_as_basic_value()
|
||||||
|
.unwrap_left()
|
||||||
/// Generates a call to `__nac3_ndarray_calc_nd_indices`. Returns a [`TypeArrayLikeAdpater`]
|
.into_int_value()
|
||||||
/// containing `i32` indices of the flattened index.
|
|
||||||
///
|
|
||||||
/// * `index` - The index to compute the multidimensional index for.
|
|
||||||
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
|
||||||
/// `NDArray`.
|
|
||||||
pub fn call_ndarray_calc_nd_indices<'ctx, G: CodeGenerator + ?Sized>(
|
|
||||||
generator: &G,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
index: IntValue<'ctx>,
|
|
||||||
ndarray: NDArrayValue<'ctx>,
|
|
||||||
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
|
||||||
let llvm_void = ctx.ctx.void_type();
|
|
||||||
let llvm_i32 = ctx.ctx.i32_type();
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
|
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
|
||||||
|
|
||||||
let ndarray_calc_nd_indices_fn_name = match llvm_usize.get_bit_width() {
|
|
||||||
32 => "__nac3_ndarray_calc_nd_indices",
|
|
||||||
64 => "__nac3_ndarray_calc_nd_indices64",
|
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
|
||||||
};
|
|
||||||
let ndarray_calc_nd_indices_fn =
|
|
||||||
ctx.module.get_function(ndarray_calc_nd_indices_fn_name).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_void.fn_type(
|
|
||||||
&[llvm_usize.into(), llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into()],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.module.add_function(ndarray_calc_nd_indices_fn_name, fn_type, None)
|
|
||||||
});
|
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
|
||||||
let ndarray_dims = ndarray.dim_sizes();
|
|
||||||
|
|
||||||
let indices = ctx.builder.build_array_alloca(llvm_i32, ndarray_num_dims, "").unwrap();
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(
|
|
||||||
ndarray_calc_nd_indices_fn,
|
|
||||||
&[
|
|
||||||
index.into(),
|
|
||||||
ndarray_dims.base_ptr(ctx, generator).into(),
|
|
||||||
ndarray_num_dims.into(),
|
|
||||||
indices.into(),
|
|
||||||
],
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
TypedArrayLikeAdapter::from(
|
|
||||||
ArraySliceValue::from_ptr_val(indices, ndarray_num_dims, None),
|
|
||||||
Box::new(|_, v| v.into_int_value()),
|
|
||||||
Box::new(|_, v| v.into()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn call_ndarray_flatten_index_impl<'ctx, G, Indices>(
|
|
||||||
generator: &G,
|
|
||||||
ctx: &CodeGenContext<'ctx, '_>,
|
|
||||||
ndarray: NDArrayValue<'ctx>,
|
|
||||||
indices: &Indices,
|
|
||||||
) -> IntValue<'ctx>
|
|
||||||
where
|
|
||||||
G: CodeGenerator + ?Sized,
|
|
||||||
Indices: ArrayLikeIndexer<'ctx>,
|
|
||||||
{
|
|
||||||
let llvm_i32 = ctx.ctx.i32_type();
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
|
|
||||||
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
|
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
|
||||||
|
|
||||||
debug_assert_eq!(
|
|
||||||
IntType::try_from(indices.element_type(ctx, generator))
|
|
||||||
.map(IntType::get_bit_width)
|
|
||||||
.unwrap_or_default(),
|
|
||||||
llvm_i32.get_bit_width(),
|
|
||||||
"Expected i32 value for argument `indices` to `call_ndarray_flatten_index_impl`"
|
|
||||||
);
|
|
||||||
debug_assert_eq!(
|
|
||||||
indices.size(ctx, generator).get_type().get_bit_width(),
|
|
||||||
llvm_usize.get_bit_width(),
|
|
||||||
"Expected usize integer value for argument `indices_size` to `call_ndarray_flatten_index_impl`"
|
|
||||||
);
|
|
||||||
|
|
||||||
let ndarray_flatten_index_fn_name = match llvm_usize.get_bit_width() {
|
|
||||||
32 => "__nac3_ndarray_flatten_index",
|
|
||||||
64 => "__nac3_ndarray_flatten_index64",
|
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
|
||||||
};
|
|
||||||
let ndarray_flatten_index_fn =
|
|
||||||
ctx.module.get_function(ndarray_flatten_index_fn_name).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_usize.fn_type(
|
|
||||||
&[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_usize.into()],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.module.add_function(ndarray_flatten_index_fn_name, fn_type, None)
|
|
||||||
});
|
|
||||||
|
|
||||||
let ndarray_num_dims = ndarray.load_ndims(ctx);
|
|
||||||
let ndarray_dims = ndarray.dim_sizes();
|
|
||||||
|
|
||||||
let index = ctx
|
|
||||||
.builder
|
|
||||||
.build_call(
|
|
||||||
ndarray_flatten_index_fn,
|
|
||||||
&[
|
|
||||||
ndarray_dims.base_ptr(ctx, generator).into(),
|
|
||||||
ndarray_num_dims.into(),
|
|
||||||
indices.base_ptr(ctx, generator).into(),
|
|
||||||
indices.size(ctx, generator).into(),
|
|
||||||
],
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.map(CallSiteValue::try_as_basic_value)
|
|
||||||
.map(|v| v.map_left(BasicValueEnum::into_int_value))
|
|
||||||
.map(Either::unwrap_left)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
index
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates a call to `__nac3_ndarray_flatten_index`. Returns the flattened index for the
|
|
||||||
/// multidimensional index.
|
|
||||||
///
|
|
||||||
/// * `ndarray` - LLVM pointer to the `NDArray`. This value must be the LLVM representation of an
|
|
||||||
/// `NDArray`.
|
|
||||||
/// * `indices` - The multidimensional index to compute the flattened index for.
|
|
||||||
pub fn call_ndarray_flatten_index<'ctx, G, Index>(
|
|
||||||
generator: &mut G,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
ndarray: NDArrayValue<'ctx>,
|
|
||||||
indices: &Index,
|
|
||||||
) -> IntValue<'ctx>
|
|
||||||
where
|
|
||||||
G: CodeGenerator + ?Sized,
|
|
||||||
Index: ArrayLikeIndexer<'ctx>,
|
|
||||||
{
|
|
||||||
call_ndarray_flatten_index_impl(generator, ctx, ndarray, indices)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates a call to `__nac3_ndarray_calc_broadcast`. Returns a tuple containing the number of
|
|
||||||
/// dimension and size of each dimension of the resultant `ndarray`.
|
|
||||||
pub fn call_ndarray_calc_broadcast<'ctx, G: CodeGenerator + ?Sized>(
|
|
||||||
generator: &mut G,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
lhs: NDArrayValue<'ctx>,
|
|
||||||
rhs: NDArrayValue<'ctx>,
|
|
||||||
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
|
||||||
|
|
||||||
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
|
|
||||||
32 => "__nac3_ndarray_calc_broadcast",
|
|
||||||
64 => "__nac3_ndarray_calc_broadcast64",
|
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
|
||||||
};
|
|
||||||
let ndarray_calc_broadcast_fn =
|
|
||||||
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_usize.fn_type(
|
|
||||||
&[
|
|
||||||
llvm_pusize.into(),
|
|
||||||
llvm_usize.into(),
|
|
||||||
llvm_pusize.into(),
|
|
||||||
llvm_usize.into(),
|
|
||||||
llvm_pusize.into(),
|
|
||||||
],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
|
|
||||||
});
|
|
||||||
|
|
||||||
let lhs_ndims = lhs.load_ndims(ctx);
|
|
||||||
let rhs_ndims = rhs.load_ndims(ctx);
|
|
||||||
let min_ndims = llvm_intrinsics::call_int_umin(ctx, lhs_ndims, rhs_ndims, None);
|
|
||||||
|
|
||||||
gen_for_callback_incrementing(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
llvm_usize.const_zero(),
|
|
||||||
(min_ndims, false),
|
|
||||||
|generator, ctx, _, idx| {
|
|
||||||
let idx = ctx.builder.build_int_sub(min_ndims, idx, "").unwrap();
|
|
||||||
let (lhs_dim_sz, rhs_dim_sz) = unsafe {
|
|
||||||
(
|
|
||||||
lhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None),
|
|
||||||
rhs.dim_sizes().get_typed_unchecked(ctx, generator, &idx, None),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let llvm_usize_const_one = llvm_usize.const_int(1, false);
|
|
||||||
let lhs_eqz = ctx
|
|
||||||
.builder
|
|
||||||
.build_int_compare(IntPredicate::EQ, lhs_dim_sz, llvm_usize_const_one, "")
|
|
||||||
.unwrap();
|
|
||||||
let rhs_eqz = ctx
|
|
||||||
.builder
|
|
||||||
.build_int_compare(IntPredicate::EQ, rhs_dim_sz, llvm_usize_const_one, "")
|
|
||||||
.unwrap();
|
|
||||||
let lhs_or_rhs_eqz = ctx.builder.build_or(lhs_eqz, rhs_eqz, "").unwrap();
|
|
||||||
|
|
||||||
let lhs_eq_rhs = ctx
|
|
||||||
.builder
|
|
||||||
.build_int_compare(IntPredicate::EQ, lhs_dim_sz, rhs_dim_sz, "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let is_compatible = ctx.builder.build_or(lhs_or_rhs_eqz, lhs_eq_rhs, "").unwrap();
|
|
||||||
|
|
||||||
ctx.make_assert(
|
|
||||||
generator,
|
|
||||||
is_compatible,
|
|
||||||
"0:ValueError",
|
|
||||||
"operands could not be broadcast together",
|
|
||||||
[None, None, None],
|
|
||||||
ctx.current_loc,
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
llvm_usize.const_int(1, false),
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let max_ndims = llvm_intrinsics::call_int_umax(ctx, lhs_ndims, rhs_ndims, None);
|
|
||||||
let lhs_dims = lhs.dim_sizes().base_ptr(ctx, generator);
|
|
||||||
let lhs_ndims = lhs.load_ndims(ctx);
|
|
||||||
let rhs_dims = rhs.dim_sizes().base_ptr(ctx, generator);
|
|
||||||
let rhs_ndims = rhs.load_ndims(ctx);
|
|
||||||
let out_dims = ctx.builder.build_array_alloca(llvm_usize, max_ndims, "").unwrap();
|
|
||||||
let out_dims = ArraySliceValue::from_ptr_val(out_dims, max_ndims, None);
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(
|
|
||||||
ndarray_calc_broadcast_fn,
|
|
||||||
&[
|
|
||||||
lhs_dims.into(),
|
|
||||||
lhs_ndims.into(),
|
|
||||||
rhs_dims.into(),
|
|
||||||
rhs_ndims.into(),
|
|
||||||
out_dims.base_ptr(ctx, generator).into(),
|
|
||||||
],
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
TypedArrayLikeAdapter::from(
|
|
||||||
out_dims,
|
|
||||||
Box::new(|_, v| v.into_int_value()),
|
|
||||||
Box::new(|_, v| v.into()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generates a call to `__nac3_ndarray_calc_broadcast_idx`. Returns an [`ArrayAllocaValue`]
|
|
||||||
/// containing the indices used for accessing `array` corresponding to the index of the broadcasted
|
|
||||||
/// array `broadcast_idx`.
|
|
||||||
pub fn call_ndarray_calc_broadcast_index<
|
|
||||||
'ctx,
|
|
||||||
G: CodeGenerator + ?Sized,
|
|
||||||
BroadcastIdx: UntypedArrayLikeAccessor<'ctx>,
|
|
||||||
>(
|
|
||||||
generator: &mut G,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
array: NDArrayValue<'ctx>,
|
|
||||||
broadcast_idx: &BroadcastIdx,
|
|
||||||
) -> TypedArrayLikeAdapter<'ctx, IntValue<'ctx>> {
|
|
||||||
let llvm_i32 = ctx.ctx.i32_type();
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
let llvm_pi32 = llvm_i32.ptr_type(AddressSpace::default());
|
|
||||||
let llvm_pusize = llvm_usize.ptr_type(AddressSpace::default());
|
|
||||||
|
|
||||||
let ndarray_calc_broadcast_fn_name = match llvm_usize.get_bit_width() {
|
|
||||||
32 => "__nac3_ndarray_calc_broadcast_idx",
|
|
||||||
64 => "__nac3_ndarray_calc_broadcast_idx64",
|
|
||||||
bw => unreachable!("Unsupported size type bit width: {}", bw),
|
|
||||||
};
|
|
||||||
let ndarray_calc_broadcast_fn =
|
|
||||||
ctx.module.get_function(ndarray_calc_broadcast_fn_name).unwrap_or_else(|| {
|
|
||||||
let fn_type = llvm_usize.fn_type(
|
|
||||||
&[llvm_pusize.into(), llvm_usize.into(), llvm_pi32.into(), llvm_pi32.into()],
|
|
||||||
false,
|
|
||||||
);
|
|
||||||
|
|
||||||
ctx.module.add_function(ndarray_calc_broadcast_fn_name, fn_type, None)
|
|
||||||
});
|
|
||||||
|
|
||||||
let broadcast_size = broadcast_idx.size(ctx, generator);
|
|
||||||
let out_idx = ctx.builder.build_array_alloca(llvm_i32, broadcast_size, "").unwrap();
|
|
||||||
|
|
||||||
let array_dims = array.dim_sizes().base_ptr(ctx, generator);
|
|
||||||
let array_ndims = array.load_ndims(ctx);
|
|
||||||
let broadcast_idx_ptr = unsafe {
|
|
||||||
broadcast_idx.ptr_offset_unchecked(ctx, generator, &llvm_usize.const_zero(), None)
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.builder
|
|
||||||
.build_call(
|
|
||||||
ndarray_calc_broadcast_fn,
|
|
||||||
&[array_dims.into(), array_ndims.into(), broadcast_idx_ptr.into(), out_idx.into()],
|
|
||||||
"",
|
|
||||||
)
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
TypedArrayLikeAdapter::from(
|
|
||||||
ArraySliceValue::from_ptr_val(out_idx, broadcast_size, None),
|
|
||||||
Box::new(|_, v| v.into_int_value()),
|
|
||||||
Box::new(|_, v| v.into()),
|
|
||||||
)
|
|
||||||
}
|
}
|
|
@ -1,16 +1,13 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
codegen::{
|
codegen::{
|
||||||
classes::{
|
classes::{
|
||||||
ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue, NDArrayType, NDArrayValue,
|
check_basic_types_match, ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue,
|
||||||
ProxyType, ProxyValue, TypedArrayLikeAccessor, TypedArrayLikeAdapter,
|
NDArrayType, NDArrayValue, NpArrayType, NpArrayValue, ProxyType, ProxyValue,
|
||||||
TypedArrayLikeMutator, UntypedArrayLikeAccessor, UntypedArrayLikeMutator,
|
TypedArrayLikeAccessor, TypedArrayLikeAdapter, TypedArrayLikeMutator,
|
||||||
|
UntypedArrayLikeAccessor, UntypedArrayLikeMutator,
|
||||||
},
|
},
|
||||||
expr::gen_binop_expr_with_values,
|
expr::gen_binop_expr_with_values,
|
||||||
irrt::{
|
irrt::calculate_len_for_slice_range,
|
||||||
calculate_len_for_slice_range, call_ndarray_calc_broadcast,
|
|
||||||
call_ndarray_calc_broadcast_index, call_ndarray_calc_nd_indices,
|
|
||||||
call_ndarray_calc_size,
|
|
||||||
},
|
|
||||||
llvm_intrinsics::{self, call_memcpy_generic},
|
llvm_intrinsics::{self, call_memcpy_generic},
|
||||||
stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback},
|
stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback},
|
||||||
CodeGenContext, CodeGenerator,
|
CodeGenContext, CodeGenerator,
|
||||||
|
@ -26,14 +23,140 @@ use crate::{
|
||||||
typedef::{FunSignature, Type, TypeEnum},
|
typedef::{FunSignature, Type, TypeEnum},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use inkwell::types::{AnyTypeEnum, BasicTypeEnum, PointerType};
|
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
types::BasicType,
|
types::BasicType,
|
||||||
values::{BasicValueEnum, IntValue, PointerValue},
|
values::{BasicValueEnum, IntValue, PointerValue},
|
||||||
AddressSpace, IntPredicate, OptimizationLevel,
|
AddressSpace, IntPredicate, OptimizationLevel,
|
||||||
};
|
};
|
||||||
|
use inkwell::{
|
||||||
|
types::{AnyTypeEnum, BasicTypeEnum, IntType, PointerType},
|
||||||
|
values::BasicValue,
|
||||||
|
};
|
||||||
use nac3parser::ast::{Operator, StrRef};
|
use nac3parser::ast::{Operator, StrRef};
|
||||||
|
|
||||||
|
fn memory_copy_slice<'ctx, G, T, Dst, Src>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
dst: Dst,
|
||||||
|
src: Src,
|
||||||
|
) -> Result<(), String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
Dst: TypedArrayLikeMutator<'ctx, T>,
|
||||||
|
Src: TypedArrayLikeAccessor<'ctx, T>,
|
||||||
|
{
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
// Check `src.size` == `dst.size`, otherwise throw an Exception
|
||||||
|
let size_ok = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_compare(IntPredicate::EQ, src.size(ctx, generator), dst.size(ctx, generator), "")
|
||||||
|
.unwrap();
|
||||||
|
ctx.make_assert(
|
||||||
|
generator,
|
||||||
|
size_ok,
|
||||||
|
"0:ValueError",
|
||||||
|
"copy slice mismatched",
|
||||||
|
[None, None, None],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Copy data
|
||||||
|
let len = dst.size(ctx, generator);
|
||||||
|
gen_for_callback_incrementing(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
llvm_usize.const_zero(),
|
||||||
|
(len, false),
|
||||||
|
|generator, ctx, _, idx| {
|
||||||
|
let value = src.get_typed(ctx, generator, &idx, None);
|
||||||
|
dst.set_typed(ctx, generator, &idx, value);
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
llvm_usize.const_int(1, false),
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn allocate_ndarray<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_type: BasicTypeEnum<'ctx>,
|
||||||
|
in_ndims: IntValue<'ctx>,
|
||||||
|
name: &'static str,
|
||||||
|
) -> NpArrayValue<'ctx>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
let ndarray_ty = NpArrayType { elem_type, size_type };
|
||||||
|
ndarray_ty.alloca(ctx, in_ndims, name)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn user_shape_set<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
in_shape: BasicValueEnum<'ctx>,
|
||||||
|
in_shape_ty: Type,
|
||||||
|
dst_shape: TypedArrayLikeAdapter<'ctx, IntValue<'ctx>>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
// Check `in_shape_ty` to determine what to do determining on the user's input
|
||||||
|
match &*ctx.unifier.get_ty(in_shape_ty) {
|
||||||
|
TypeEnum::TObj { obj_id, .. }
|
||||||
|
if *obj_id == ctx.primitives.list.obj_id(&ctx.unifier).unwrap() =>
|
||||||
|
{
|
||||||
|
// 1. A list of ints; e.g., `np.empty([600, 800, 3])`
|
||||||
|
|
||||||
|
// NOTE: If there are no logic errors, the list's element type MUST BE int32.
|
||||||
|
|
||||||
|
// List has to be a pointer
|
||||||
|
let BasicValueEnum::PointerValue(shape_list_ptr) = in_shape else { unreachable!() };
|
||||||
|
|
||||||
|
let shape_list = ListValue::from_ptr_val(shape_list_ptr, llvm_usize, None);
|
||||||
|
memory_copy_slice(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
dst_shape,
|
||||||
|
TypedArrayLikeAdapter::from(
|
||||||
|
shape_list.data(),
|
||||||
|
Box::new(|_ctx, value| value.into_int_value()),
|
||||||
|
Box::new(|_ctx, value| value.as_basic_value_enum()),
|
||||||
|
),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
TypeEnum::TTuple { ty, .. } => {
|
||||||
|
// 2. A tuple of ints; e.g., `np.empty((600, 800, 3))`
|
||||||
|
|
||||||
|
// Tuple has to be a struct
|
||||||
|
// Read [`codegen::expr::gen_expr`] to see how `nac3core` translates a Python tuple into LLVM.
|
||||||
|
let BasicValueEnum::StructValue(shape_tuple) = in_shape else { unreachable!() };
|
||||||
|
|
||||||
|
let ndims = ty.len();
|
||||||
|
for dim_i in 0..ndims {
|
||||||
|
let dim = ctx
|
||||||
|
.builder
|
||||||
|
.build_extract_value(shape_tuple, dim_i as u32, format!("dim{dim_i}").as_str())
|
||||||
|
.unwrap()
|
||||||
|
.into_int_value();
|
||||||
|
|
||||||
|
let idx = llvm_usize.const_int(dim_i as u64, false);
|
||||||
|
dst_shape.set_typed(ctx, generator, &idx, dim);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TypeEnum::TObj { obj_id, .. }
|
||||||
|
if *obj_id == ctx.primitives.int32.obj_id(&ctx.unifier).unwrap() =>
|
||||||
|
{
|
||||||
|
// 3. A scalar `int32`; e.g., `np.empty(3)`, this is functionally equivalent to `np.empty([3])`
|
||||||
|
let shape_int = in_shape.into_int_value();
|
||||||
|
dst_shape.set_typed(ctx, generator, &llvm_usize.const_zero(), shape_int);
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
// /// Creates an uninitialized `NDArray` instance.
|
// /// Creates an uninitialized `NDArray` instance.
|
||||||
// fn create_ndarray_uninitialized<'ctx, G: CodeGenerator + ?Sized>(
|
// fn create_ndarray_uninitialized<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
// generator: &mut G,
|
// generator: &mut G,
|
||||||
|
|
|
@ -23,3 +23,4 @@ pub mod codegen;
|
||||||
pub mod symbol_resolver;
|
pub mod symbol_resolver;
|
||||||
pub mod toplevel;
|
pub mod toplevel;
|
||||||
pub mod typecheck;
|
pub mod typecheck;
|
||||||
|
pub mod util;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::iter::once;
|
use std::iter::once;
|
||||||
|
|
||||||
|
use crate::util::SizeVariant;
|
||||||
use helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails};
|
use helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
use inkwell::{
|
use inkwell::{
|
||||||
|
@ -278,19 +279,10 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper enum used by [`BuiltinBuilder`]
|
fn size_variant_to_int_type(variant: SizeVariant, primitives: &PrimitiveStore) -> Type {
|
||||||
#[derive(Clone, Copy)]
|
match variant {
|
||||||
enum SizeVariant {
|
SizeVariant::Bits32 => primitives.int32,
|
||||||
Bits32,
|
SizeVariant::Bits64 => primitives.int64,
|
||||||
Bits64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SizeVariant {
|
|
||||||
fn of_int(self, primitives: &PrimitiveStore) -> Type {
|
|
||||||
match self {
|
|
||||||
SizeVariant::Bits32 => primitives.int32,
|
|
||||||
SizeVariant::Bits64 => primitives.int64,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1061,7 +1053,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
);
|
);
|
||||||
|
|
||||||
// The size variant of the function determines the size of the returned int.
|
// The size variant of the function determines the size of the returned int.
|
||||||
let int_sized = size_variant.of_int(self.primitives);
|
let int_sized = size_variant_to_int_type(size_variant, self.primitives);
|
||||||
|
|
||||||
let ndarray_int_sized =
|
let ndarray_int_sized =
|
||||||
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
||||||
|
@ -1086,7 +1078,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
|
let ret_elem_ty = size_variant_to_int_type(size_variant, &ctx.primitives);
|
||||||
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?))
|
Ok(Some(builtin_fns::call_round(generator, ctx, (arg_ty, arg), ret_elem_ty)?))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -1127,7 +1119,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty));
|
make_ndarray_ty(self.unifier, self.primitives, Some(float), Some(common_ndim.ty));
|
||||||
|
|
||||||
// The size variant of the function determines the type of int returned
|
// The size variant of the function determines the type of int returned
|
||||||
let int_sized = size_variant.of_int(self.primitives);
|
let int_sized = size_variant_to_int_type(size_variant, self.primitives);
|
||||||
let ndarray_int_sized =
|
let ndarray_int_sized =
|
||||||
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
make_ndarray_ty(self.unifier, self.primitives, Some(int_sized), Some(common_ndim.ty));
|
||||||
|
|
||||||
|
@ -1150,7 +1142,7 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
let arg_ty = fun.0.args[0].ty;
|
let arg_ty = fun.0.args[0].ty;
|
||||||
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
let arg = args[0].1.clone().to_basic_value_enum(ctx, generator, arg_ty)?;
|
||||||
|
|
||||||
let ret_elem_ty = size_variant.of_int(&ctx.primitives);
|
let ret_elem_ty = size_variant_to_int_type(size_variant, &ctx.primitives);
|
||||||
let func = match kind {
|
let func = match kind {
|
||||||
Kind::Ceil => builtin_fns::call_ceil,
|
Kind::Ceil => builtin_fns::call_ceil,
|
||||||
Kind::Floor => builtin_fns::call_floor,
|
Kind::Floor => builtin_fns::call_floor,
|
||||||
|
|
|
@ -34,6 +34,7 @@ pub mod numpy;
|
||||||
pub mod type_annotation;
|
pub mod type_annotation;
|
||||||
use composer::*;
|
use composer::*;
|
||||||
use type_annotation::*;
|
use type_annotation::*;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test;
|
mod test;
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum SizeVariant {
|
||||||
|
Bits32,
|
||||||
|
Bits64,
|
||||||
|
}
|
Loading…
Reference in New Issue