forked from M-Labs/nac3
Compare commits
28 Commits
ndarray-st
...
ndarray-st
Author | SHA1 | Date |
---|---|---|
lyken | 059b130aff | |
lyken | 7742fbf9e0 | |
lyken | 71e05c17b9 | |
lyken | e4998ccec8 | |
lyken | 9a82b033b6 | |
lyken | 3b87bd36f3 | |
lyken | b4d5b2a41f | |
lyken | 259958aded | |
lyken | 867f6ccf8e | |
lyken | 23ed5642fb | |
lyken | 2f7e75d7cf | |
lyken | 8863cd64a9 | |
lyken | 9e78139373 | |
lyken | 259481e8d0 | |
lyken | 5faac4b9d4 | |
lyken | c4d54b198b | |
lyken | 9ad7a78dbe | |
lyken | 1721ebac66 | |
lyken | f033639415 | |
lyken | 3116f11814 | |
lyken | 5047379ac0 | |
lyken | 6c10e3d056 | |
lyken | 2dbc1ec659 | |
Sebastien Bourdeauducq | c80378063a | |
abdul124 | 513d30152b | |
abdul124 | 45e9360c4d | |
abdul124 | 2e01b77fc8 | |
abdul124 | cea7cade51 |
|
@ -13,6 +13,7 @@
|
||||||
''
|
''
|
||||||
mkdir -p $out/bin
|
mkdir -p $out/bin
|
||||||
ln -s ${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang $out/bin/clang-irrt
|
ln -s ${pkgs.llvmPackages_14.clang-unwrapped}/bin/clang $out/bin/clang-irrt
|
||||||
|
ln -s ${pkgs.llvmPackages_14.clang}/bin/clang $out/bin/clang-irrt-test
|
||||||
ln -s ${pkgs.llvmPackages_14.llvm.out}/bin/llvm-as $out/bin/llvm-as-irrt
|
ln -s ${pkgs.llvmPackages_14.llvm.out}/bin/llvm-as $out/bin/llvm-as-irrt
|
||||||
'';
|
'';
|
||||||
nac3artiq = pkgs.python3Packages.toPythonModule (
|
nac3artiq = pkgs.python3Packages.toPythonModule (
|
||||||
|
@ -23,6 +24,7 @@
|
||||||
cargoLock = {
|
cargoLock = {
|
||||||
lockFile = ./Cargo.lock;
|
lockFile = ./Cargo.lock;
|
||||||
};
|
};
|
||||||
|
cargoTestFlags = [ "--features" "test" ];
|
||||||
passthru.cargoLock = cargoLock;
|
passthru.cargoLock = cargoLock;
|
||||||
nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_14.clang llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ];
|
nativeBuildInputs = [ pkgs.python3 pkgs.llvmPackages_14.clang llvm-tools-irrt pkgs.llvmPackages_14.llvm.out llvm-nac3 ];
|
||||||
buildInputs = [ pkgs.python3 llvm-nac3 ];
|
buildInputs = [ pkgs.python3 llvm-nac3 ];
|
||||||
|
@ -161,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";
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
[features]
|
||||||
|
test = []
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "nac3core"
|
name = "nac3core"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
|
@ -3,20 +3,34 @@ use std::{
|
||||||
env,
|
env,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::Write,
|
io::Write,
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
const CMD_IRRT_CLANG: &str = "clang-irrt";
|
||||||
const FILE: &str = "src/codegen/irrt/irrt.cpp";
|
const CMD_IRRT_CLANG_TEST: &str = "clang-irrt-test";
|
||||||
|
const CMD_IRRT_LLVM_AS: &str = "llvm-as-irrt";
|
||||||
|
|
||||||
|
fn get_out_dir() -> PathBuf {
|
||||||
|
PathBuf::from(env::var("OUT_DIR").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_irrt_dir() -> &'static Path {
|
||||||
|
Path::new("irrt")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compile `irrt.cpp` for use in `src/codegen`
|
||||||
|
fn compile_irrt_cpp() {
|
||||||
|
let out_dir = get_out_dir();
|
||||||
|
let irrt_dir = get_irrt_dir();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
|
* HACK: Sadly, clang doesn't let us emit generic LLVM bitcode.
|
||||||
* Compiling for WASM32 and filtering the output with regex is the closest we can get.
|
* Compiling for WASM32 and filtering the output with regex is the closest we can get.
|
||||||
*/
|
*/
|
||||||
|
let irrt_cpp_path = irrt_dir.join("irrt.cpp");
|
||||||
let flags: &[&str] = &[
|
let flags: &[&str] = &[
|
||||||
"--target=wasm32",
|
"--target=wasm32",
|
||||||
FILE,
|
|
||||||
"-x",
|
"-x",
|
||||||
"c++",
|
"c++",
|
||||||
"-fno-discard-value-names",
|
"-fno-discard-value-names",
|
||||||
|
@ -31,15 +45,19 @@ fn main() {
|
||||||
"-S",
|
"-S",
|
||||||
"-Wall",
|
"-Wall",
|
||||||
"-Wextra",
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
|
"-I",
|
||||||
|
irrt_dir.to_str().unwrap(),
|
||||||
"-o",
|
"-o",
|
||||||
"-",
|
"-",
|
||||||
|
irrt_cpp_path.to_str().unwrap(),
|
||||||
];
|
];
|
||||||
|
|
||||||
println!("cargo:rerun-if-changed={FILE}");
|
// Tell Cargo to rerun if any file under `irrt_dir` (recursive) changes
|
||||||
let out_dir = env::var("OUT_DIR").unwrap();
|
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
|
||||||
let out_path = Path::new(&out_dir);
|
|
||||||
|
|
||||||
let output = Command::new("clang-irrt")
|
// Compile IRRT and capture the LLVM IR output
|
||||||
|
let output = Command::new(CMD_IRRT_CLANG)
|
||||||
.args(flags)
|
.args(flags)
|
||||||
.output()
|
.output()
|
||||||
.map(|o| {
|
.map(|o| {
|
||||||
|
@ -52,7 +70,17 @@ fn main() {
|
||||||
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
let output = std::str::from_utf8(&output.stdout).unwrap().replace("\r\n", "\n");
|
||||||
let mut filtered_output = String::with_capacity(output.len());
|
let mut filtered_output = String::with_capacity(output.len());
|
||||||
|
|
||||||
let regex_filter = Regex::new(r"(?ms:^define.*?\}$)|(?m:^declare.*?$)").unwrap();
|
// Filter out irrelevant IR
|
||||||
|
//
|
||||||
|
// Regex:
|
||||||
|
// - `(?ms:^define.*?\}$)` captures LLVM `define` blocks
|
||||||
|
// - `(?m:^declare.*?$)` captures LLVM `declare` lines
|
||||||
|
// - `(?m:^%.+?=\s*type\s*\{.+?\}$)` captures LLVM `type` declarations
|
||||||
|
// - `(?m:^@.+?=.+$)` captures global constants
|
||||||
|
let regex_filter = Regex::new(
|
||||||
|
r"(?ms:^define.*?\}$)|(?m:^declare.*?$)|(?m:^%.+?=\s*type\s*\{.+?\}$)|(?m:^@.+?=.+$)",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
for f in regex_filter.captures_iter(&output) {
|
for f in regex_filter.captures_iter(&output) {
|
||||||
assert_eq!(f.len(), 1);
|
assert_eq!(f.len(), 1);
|
||||||
filtered_output.push_str(&f[0]);
|
filtered_output.push_str(&f[0]);
|
||||||
|
@ -63,20 +91,73 @@ fn main() {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.replace_all(&filtered_output, "");
|
.replace_all(&filtered_output, "");
|
||||||
|
|
||||||
println!("cargo:rerun-if-env-changed=DEBUG_DUMP_IRRT");
|
// For debugging
|
||||||
if env::var("DEBUG_DUMP_IRRT").is_ok() {
|
// Doing `DEBUG_DUMP_IRRT=1 cargo build -p nac3core` dumps the LLVM IR generated
|
||||||
let mut file = File::create(out_path.join("irrt.ll")).unwrap();
|
const DEBUG_DUMP_IRRT: &str = "DEBUG_DUMP_IRRT";
|
||||||
|
println!("cargo:rerun-if-env-changed={DEBUG_DUMP_IRRT}");
|
||||||
|
if env::var(DEBUG_DUMP_IRRT).is_ok() {
|
||||||
|
let mut file = File::create(out_dir.join("irrt.ll")).unwrap();
|
||||||
file.write_all(output.as_bytes()).unwrap();
|
file.write_all(output.as_bytes()).unwrap();
|
||||||
let mut file = File::create(out_path.join("irrt-filtered.ll")).unwrap();
|
|
||||||
|
let mut file = File::create(out_dir.join("irrt-filtered.ll")).unwrap();
|
||||||
file.write_all(filtered_output.as_bytes()).unwrap();
|
file.write_all(filtered_output.as_bytes()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut llvm_as = Command::new("llvm-as-irrt")
|
// Assemble the emitted and filtered IR to .bc
|
||||||
|
// That .bc will be integrated into nac3core's codegen
|
||||||
|
let mut llvm_as = Command::new(CMD_IRRT_LLVM_AS)
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.arg("-o")
|
.arg("-o")
|
||||||
.arg(out_path.join("irrt.bc"))
|
.arg(out_dir.join("irrt.bc"))
|
||||||
.spawn()
|
.spawn()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
|
llvm_as.stdin.as_mut().unwrap().write_all(filtered_output.as_bytes()).unwrap();
|
||||||
assert!(llvm_as.wait().unwrap().success());
|
assert!(llvm_as.wait().unwrap().success());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Compile `irrt_test.cpp` for testing
|
||||||
|
fn compile_irrt_test_cpp() {
|
||||||
|
let out_dir = get_out_dir();
|
||||||
|
let irrt_dir = get_irrt_dir();
|
||||||
|
|
||||||
|
let exe_path = out_dir.join("irrt_test.out"); // Output path of the compiled test executable
|
||||||
|
let irrt_test_cpp_path = irrt_dir.join("irrt_test.cpp");
|
||||||
|
let flags: &[&str] = &[
|
||||||
|
irrt_test_cpp_path.to_str().unwrap(),
|
||||||
|
"-x",
|
||||||
|
"c++",
|
||||||
|
"-I",
|
||||||
|
irrt_dir.to_str().unwrap(),
|
||||||
|
"-g",
|
||||||
|
"-fno-discard-value-names",
|
||||||
|
"-O0",
|
||||||
|
"-Wall",
|
||||||
|
"-Wextra",
|
||||||
|
"-Werror=return-type",
|
||||||
|
"-lm", // for `tgamma()`, `lgamma()`
|
||||||
|
"-I",
|
||||||
|
irrt_dir.to_str().unwrap(),
|
||||||
|
"-o",
|
||||||
|
exe_path.to_str().unwrap(),
|
||||||
|
];
|
||||||
|
|
||||||
|
Command::new(CMD_IRRT_CLANG_TEST)
|
||||||
|
.args(flags)
|
||||||
|
.output()
|
||||||
|
.map(|o| {
|
||||||
|
assert!(o.status.success(), "{}", std::str::from_utf8(&o.stderr).unwrap());
|
||||||
|
o
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
println!("cargo:rerun-if-changed={}", irrt_dir.to_str().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
compile_irrt_cpp();
|
||||||
|
|
||||||
|
// https://github.com/rust-lang/cargo/issues/2549
|
||||||
|
// `cargo test -F test` to also build `irrt_test.cpp
|
||||||
|
if cfg!(feature = "test") {
|
||||||
|
compile_irrt_test_cpp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
#define IRRT_DEFINE_TYPEDEF_INTS
|
||||||
|
#include <irrt_everything.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.
|
||||||
|
*/
|
|
@ -1,9 +1,7 @@
|
||||||
using int8_t = _BitInt(8);
|
#pragma once
|
||||||
using uint8_t = unsigned _BitInt(8);
|
|
||||||
using int32_t = _BitInt(32);
|
#include <irrt/utils.hpp>
|
||||||
using uint32_t = unsigned _BitInt(32);
|
#include <irrt/int_defs.hpp>
|
||||||
using int64_t = _BitInt(64);
|
|
||||||
using uint64_t = unsigned _BitInt(64);
|
|
||||||
|
|
||||||
// NDArray indices are always `uint32_t`.
|
// NDArray indices are always `uint32_t`.
|
||||||
using NDIndex = uint32_t;
|
using NDIndex = uint32_t;
|
||||||
|
@ -11,16 +9,6 @@ using NDIndex = uint32_t;
|
||||||
using SliceIndex = int32_t;
|
using SliceIndex = int32_t;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
template <typename T>
|
|
||||||
const T& max(const T& a, const T& b) {
|
|
||||||
return a > b ? a : b;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
const T& min(const T& a, const 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>
|
|
@ -0,0 +1,85 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
#include <irrt/utils.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// nac3core's "str" struct type definition
|
||||||
|
template <typename SizeT>
|
||||||
|
struct Str {
|
||||||
|
const char* content;
|
||||||
|
SizeT length;
|
||||||
|
};
|
||||||
|
|
||||||
|
// A limited set of errors IRRT could use.
|
||||||
|
typedef uint32_t ErrorId;
|
||||||
|
struct ErrorIds {
|
||||||
|
ErrorId index_error;
|
||||||
|
ErrorId value_error;
|
||||||
|
ErrorId assertion_error;
|
||||||
|
ErrorId runtime_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ErrorContext {
|
||||||
|
// Context
|
||||||
|
ErrorIds* error_ids;
|
||||||
|
|
||||||
|
// Error thrown by IRRT
|
||||||
|
ErrorId error_id;
|
||||||
|
const char* message_template; // MUST BE `&'static`
|
||||||
|
uint64_t param1;
|
||||||
|
uint64_t param2;
|
||||||
|
uint64_t param3;
|
||||||
|
|
||||||
|
void initialize(ErrorIds* error_ids) {
|
||||||
|
this->error_ids = error_ids;
|
||||||
|
clear_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear_error() {
|
||||||
|
// Point the message_template to an empty str. Don't set it to nullptr as a sentinel
|
||||||
|
this->message_template = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_error(ErrorId error_id, const char* message, uint64_t param1 = 0, uint64_t param2 = 0, uint64_t param3 = 0) {
|
||||||
|
this->error_id = error_id;
|
||||||
|
this->message_template = message;
|
||||||
|
this->param1 = param1;
|
||||||
|
this->param2 = param2;
|
||||||
|
this->param3 = param3;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool has_error() {
|
||||||
|
return !cstr_utils::is_empty(message_template);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
void set_error_str(Str<SizeT> *dst_str) {
|
||||||
|
dst_str->content = message_template;
|
||||||
|
dst_str->length = (SizeT) cstr_utils::length(message_template);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
void __nac3_error_context_initialize(ErrorContext* errctx, ErrorIds* error_ids) {
|
||||||
|
errctx->initialize(error_ids);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t __nac3_error_context_has_no_error(ErrorContext* errctx) {
|
||||||
|
return !errctx->has_error();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_error_context_get_error_str(ErrorContext* errctx, Str<int32_t> *dst_str) {
|
||||||
|
errctx->set_error_str<int32_t>(dst_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_error_context_get_error_str64(ErrorContext* errctx, Str<int64_t> *dst_str) {
|
||||||
|
errctx->set_error_str<int64_t>(dst_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_error_dummy_raise(ErrorContext* errctx) {
|
||||||
|
errctx->set_error(errctx->error_ids->runtime_error, "THROWN FROM __nac3_error_dummy_raise!!!!!!");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// This is made toggleable since `irrt_test.cpp` itself would include
|
||||||
|
// headers that define these typedefs
|
||||||
|
#ifdef IRRT_DEFINE_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,155 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
#include <irrt/ndarray/ndarray_util.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
//
|
||||||
|
// This pointer should point to the first element of the ndarray directly
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the strides of the ndarray with `ndarray_util::set_strides_by_shape`
|
||||||
|
void set_strides_by_shape() {
|
||||||
|
ndarray_util::set_strides_by_shape(itemsize, ndims, strides, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* get_pelement_by_indices(const SizeT *indices) {
|
||||||
|
uint8_t* element = data;
|
||||||
|
for (SizeT dim_i = 0; dim_i < ndims; dim_i++)
|
||||||
|
element += indices[dim_i] * strides[dim_i];
|
||||||
|
return element;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t* get_nth_pelement(SizeT nth) {
|
||||||
|
SizeT* indices = (SizeT*) __builtin_alloca(sizeof(SizeT) * this->ndims);
|
||||||
|
ndarray_util::set_indices_by_nth(this->ndims, this->shape, indices, nth);
|
||||||
|
return get_pelement_by_indices(indices);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the pointer to the nth element of the ndarray as if it were flattened.
|
||||||
|
uint8_t* checked_get_nth_pelement(ErrorContext* errctx, SizeT nth) {
|
||||||
|
SizeT arr_size = this->size();
|
||||||
|
if (!(0 <= nth && nth < arr_size)) {
|
||||||
|
errctx->set_error(
|
||||||
|
errctx->error_ids->index_error,
|
||||||
|
"index {0} is out of bounds, valid range is {1} <= index < {2}",
|
||||||
|
nth, 0, arr_size
|
||||||
|
);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return get_nth_pelement(nth);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_pelement_value(uint8_t* pelement, const uint8_t* pvalue) {
|
||||||
|
__builtin_memcpy(pelement, pvalue, itemsize);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill the ndarray with a value
|
||||||
|
void fill_generic(const uint8_t* pvalue) {
|
||||||
|
const SizeT size = this->size();
|
||||||
|
for (SizeT i = 0; i < size; i++) {
|
||||||
|
uint8_t* pelement = get_nth_pelement(i); // No need for checked_get_nth_pelement
|
||||||
|
set_pelement_value(pelement, 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();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t __nac3_ndarray_nbytes(NDArray<int32_t>* ndarray) {
|
||||||
|
return ndarray->nbytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t __nac3_ndarray_nbytes64(NDArray<int64_t>* ndarray) {
|
||||||
|
return ndarray->nbytes();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_util_assert_shape_no_negative(ErrorContext* errctx, int32_t ndims, int32_t* shape) {
|
||||||
|
ndarray_util::assert_shape_no_negative(errctx, ndims, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_util_assert_shape_no_negative64(ErrorContext* errctx, int64_t ndims, int64_t* shape) {
|
||||||
|
ndarray_util::assert_shape_no_negative(errctx, ndims, shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_set_strides_by_shape(NDArray<int32_t>* ndarray) {
|
||||||
|
ndarray->set_strides_by_shape();
|
||||||
|
}
|
||||||
|
|
||||||
|
void __nac3_ndarray_set_strides_by_shape64(NDArray<int64_t>* ndarray) {
|
||||||
|
ndarray->set_strides_by_shape();
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,107 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
namespace ndarray_util {
|
||||||
|
|
||||||
|
// Throw an error if there is an axis with negative dimension
|
||||||
|
template <typename SizeT>
|
||||||
|
void assert_shape_no_negative(ErrorContext* errctx, SizeT ndims, const SizeT* shape) {
|
||||||
|
for (SizeT axis = 0; axis < ndims; axis++) {
|
||||||
|
if (shape[axis] < 0) {
|
||||||
|
errctx->set_error(
|
||||||
|
errctx->error_ids->value_error,
|
||||||
|
"negative dimensions are not allowed; axis {0} has dimension {1}",
|
||||||
|
axis, shape[axis]
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the size/# of elements of an ndarray given its shape
|
||||||
|
template <typename SizeT>
|
||||||
|
SizeT calc_size_from_shape(SizeT ndims, const SizeT* shape) {
|
||||||
|
SizeT size = 1;
|
||||||
|
for (SizeT axis = 0; axis < ndims; axis++) size *= shape[axis];
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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>
|
||||||
|
void set_strides_by_shape(SizeT itemsize, SizeT ndims, SizeT* dst_strides, const SizeT* shape) {
|
||||||
|
SizeT stride_product = 1;
|
||||||
|
for (SizeT i = 0; i < ndims; i++) {
|
||||||
|
int axis = ndims - i - 1;
|
||||||
|
dst_strides[axis] = stride_product * itemsize;
|
||||||
|
stride_product *= shape[axis];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
void set_indices_by_nth(SizeT ndims, const SizeT* shape, SizeT* indices, SizeT nth) {
|
||||||
|
for (int32_t i = 0; i < ndims; i++) {
|
||||||
|
int32_t axis = ndims - i - 1;
|
||||||
|
int32_t dim = shape[axis];
|
||||||
|
|
||||||
|
indices[axis] = nth % dim;
|
||||||
|
nth /= dim;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename SizeT>
|
||||||
|
bool can_broadcast_shape_to(
|
||||||
|
const SizeT target_ndims,
|
||||||
|
const SizeT *target_shape,
|
||||||
|
const SizeT src_ndims,
|
||||||
|
const SizeT *src_shape
|
||||||
|
) {
|
||||||
|
/*
|
||||||
|
// See https://numpy.org/doc/stable/user/basics.broadcasting.html
|
||||||
|
|
||||||
|
This function handles this example:
|
||||||
|
```
|
||||||
|
Image (3d array): 256 x 256 x 3
|
||||||
|
Scale (1d array): 3
|
||||||
|
Result (3d array): 256 x 256 x 3
|
||||||
|
```
|
||||||
|
|
||||||
|
Other interesting examples to consider:
|
||||||
|
- `can_broadcast_shape_to([3], [1, 1, 1, 1, 3]) == true`
|
||||||
|
- `can_broadcast_shape_to([3], [3, 1]) == false`
|
||||||
|
- `can_broadcast_shape_to([256, 256, 3], [256, 1, 3]) == true`
|
||||||
|
|
||||||
|
In cases when the shapes contain zero(es):
|
||||||
|
- `can_broadcast_shape_to([0], [1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0], [2]) == false`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1, 1, 1, 1]) == true`
|
||||||
|
- `can_broadcast_shape_to([0, 4, 0, 0], [1, 4, 1, 1]) == true`
|
||||||
|
- `can_broadcast_shape_to([4, 3], [0, 3]) == false`
|
||||||
|
- `can_broadcast_shape_to([4, 3], [0, 0]) == false`
|
||||||
|
*/
|
||||||
|
|
||||||
|
// This is essentially doing the following in Python:
|
||||||
|
// `for target_dim, src_dim in itertools.zip_longest(target_shape[::-1], src_shape[::-1], fillvalue=1)`
|
||||||
|
for (SizeT i = 0; i < max(target_ndims, src_ndims); i++) {
|
||||||
|
SizeT target_axis = target_ndims - i - 1;
|
||||||
|
SizeT src_axis = src_ndims - i - 1;
|
||||||
|
|
||||||
|
bool target_dim_exists = target_axis >= 0;
|
||||||
|
bool src_dim_exists = src_axis >= 0;
|
||||||
|
|
||||||
|
SizeT target_dim = target_dim_exists ? target_shape[target_axis] : 1;
|
||||||
|
SizeT src_dim = src_dim_exists ? src_shape[src_axis] : 1;
|
||||||
|
|
||||||
|
bool ok = src_dim == 1 || target_dim == src_dim;
|
||||||
|
if (!ok) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,60 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
namespace string {
|
||||||
|
bool is_empty(const char* str) {
|
||||||
|
return str[0] == '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t compare(const char* a, const char* b) {
|
||||||
|
uint32_t i = 0;
|
||||||
|
while (true) {
|
||||||
|
if (a[i] < b[i]) {
|
||||||
|
return -1;
|
||||||
|
} else if (a[i] > b[i]) {
|
||||||
|
return 1;
|
||||||
|
} else { // a[i] == b[i]
|
||||||
|
if (a[i] == '\0') {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t equal(const char* a, const char* b) {
|
||||||
|
return compare(a, b) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t length(const char* str) {
|
||||||
|
uint32_t length = 0;
|
||||||
|
while (*str != '\0') {
|
||||||
|
length++;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool copy(const char* src, char* dst, uint32_t dst_max_size) {
|
||||||
|
for (uint32_t i = 0; i < dst_max_size; i++) {
|
||||||
|
bool is_last = i + 1 == dst_max_size;
|
||||||
|
if (is_last && src[i] != '\0') {
|
||||||
|
dst[i] = '\0';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src[i] == '\0') {
|
||||||
|
dst[i] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
dst[i] = src[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
template <typename T>
|
||||||
|
const T& max(const T& a, const T& b) {
|
||||||
|
return a > b ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
const T& min(const T& a, const T& b) {
|
||||||
|
return a > b ? b : a;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool arrays_match(int len, T* as, T* bs) {
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (as[i] != bs[i]) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
uint32_t int_log_floor(T value, T base) {
|
||||||
|
uint32_t result = 0;
|
||||||
|
while (value >= base) {
|
||||||
|
result++;
|
||||||
|
value /= base;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace cstr_utils {
|
||||||
|
bool is_empty(const char* str) {
|
||||||
|
return str[0] == '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t compare(const char* a, const char* b) {
|
||||||
|
uint32_t i = 0;
|
||||||
|
while (true) {
|
||||||
|
if (a[i] < b[i]) {
|
||||||
|
return -1;
|
||||||
|
} else if (a[i] > b[i]) {
|
||||||
|
return 1;
|
||||||
|
} else { // a[i] == b[i]
|
||||||
|
if (a[i] == '\0') {
|
||||||
|
return 0;
|
||||||
|
} else {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int8_t equal(const char* a, const char* b) {
|
||||||
|
return compare(a, b) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t length(const char* str) {
|
||||||
|
uint32_t length = 0;
|
||||||
|
while (*str != '\0') {
|
||||||
|
length++;
|
||||||
|
str++;
|
||||||
|
}
|
||||||
|
return length;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool copy(const char* src, char* dst, uint32_t dst_max_size) {
|
||||||
|
for (uint32_t i = 0; i < dst_max_size; i++) {
|
||||||
|
bool is_last = i + 1 == dst_max_size;
|
||||||
|
if (is_last && src[i] != '\0') {
|
||||||
|
dst[i] = '\0';
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (src[i] == '\0') {
|
||||||
|
dst[i] = '\0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
dst[i] = src[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
__builtin_unreachable();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <irrt/core.hpp>
|
||||||
|
#include <irrt/error_context.hpp>
|
||||||
|
#include <irrt/int_defs.hpp>
|
||||||
|
#include <irrt/utils.hpp>
|
||||||
|
#include <irrt/ndarray/ndarray.hpp>
|
|
@ -0,0 +1,18 @@
|
||||||
|
// This file will be compiled like a real C++ program,
|
||||||
|
// and we do have the luxury to use the standard libraries.
|
||||||
|
// That is if the nix flakes do not have issues... especially on msys2...
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include <irrt_everything.hpp>
|
||||||
|
|
||||||
|
#include <test/core.hpp>
|
||||||
|
#include <test/test_core.hpp>
|
||||||
|
#include <test/test_utils.hpp>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
run_test_core();
|
||||||
|
run_test_utils();
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -0,0 +1,88 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Include this header for every test_*.cpp
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
#include <test/print.hpp>
|
||||||
|
|
||||||
|
// Some utils can be used here
|
||||||
|
#include "../irrt/utils.hpp"
|
||||||
|
|
||||||
|
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__)
|
||||||
|
|
||||||
|
void test_fail() {
|
||||||
|
printf("[!] Test failed. Exiting with status code 1.\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void debug_print_array(int len, const T* as) {
|
||||||
|
printf("[");
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
if (i != 0) printf(", ");
|
||||||
|
print_value(as[i]);
|
||||||
|
}
|
||||||
|
printf("]");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_assertion_passed(const char* file, int line) {
|
||||||
|
printf("[*] Assertion passed on %s:%d\n", file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_assertion_failed(const char* file, int line) {
|
||||||
|
printf("[!] Assertion failed on %s:%d\n", file, line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __assert_true(const char* file, int line, bool cond) {
|
||||||
|
if (cond) {
|
||||||
|
print_assertion_passed(file, line);
|
||||||
|
} else {
|
||||||
|
print_assertion_failed(file, line);
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define assert_true(cond) __assert_true(__FILE__, __LINE__, cond)
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void __assert_arrays_match(const char* file, int line, int len, const T* expected, const T* got) {
|
||||||
|
if (arrays_match(len, expected, got)) {
|
||||||
|
print_assertion_passed(file, line);
|
||||||
|
} else {
|
||||||
|
print_assertion_failed(file, line);
|
||||||
|
printf("Expect = ");
|
||||||
|
debug_print_array(len, expected);
|
||||||
|
printf("\n");
|
||||||
|
printf(" Got = ");
|
||||||
|
debug_print_array(len, got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define assert_arrays_match(len, expected, got) __assert_arrays_match(__FILE__, __LINE__, len, expected, got)
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void __assert_values_match(const char* file, int line, T expected, T got) {
|
||||||
|
if (expected == got) {
|
||||||
|
print_assertion_passed(file, line);
|
||||||
|
} else {
|
||||||
|
print_assertion_failed(file, line);
|
||||||
|
printf("Expect = ");
|
||||||
|
print_value(expected);
|
||||||
|
printf("\n");
|
||||||
|
printf(" Got = ");
|
||||||
|
print_value(got);
|
||||||
|
printf("\n");
|
||||||
|
test_fail();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#define assert_values_match(expected, got) __assert_values_match(__FILE__, __LINE__, expected, got)
|
|
@ -0,0 +1,42 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
void print_value(T value);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(char value) {
|
||||||
|
printf("'%c' (ord=%d)", value, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(int8_t value) {
|
||||||
|
printf("%d", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(int32_t value) {
|
||||||
|
printf("%d", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(uint8_t value) {
|
||||||
|
printf("%u", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(uint32_t value) {
|
||||||
|
printf("%u", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(double value) {
|
||||||
|
printf("%f", value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void print_value(char* value) {
|
||||||
|
printf("%s", value);
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <test/core.hpp>
|
||||||
|
#include <irrt/core.hpp>
|
||||||
|
|
||||||
|
void test_int_exp() {
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
assert_values_match(125, __nac3_int_exp_impl<int32_t>(5, 3));
|
||||||
|
assert_values_match(3125, __nac3_int_exp_impl<int32_t>(5, 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_test_core() {
|
||||||
|
test_int_exp();
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <test/core.hpp>
|
||||||
|
#include <irrt/utils.hpp>
|
||||||
|
|
||||||
|
void test_int_log_10() {
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
assert_values_match((uint32_t) 0, int_log_floor(0, 10));
|
||||||
|
assert_values_match((uint32_t) 0, int_log_floor(9, 10));
|
||||||
|
assert_values_match((uint32_t) 1, int_log_floor(10, 10));
|
||||||
|
assert_values_match((uint32_t) 1, int_log_floor(11, 10));
|
||||||
|
assert_values_match((uint32_t) 1, int_log_floor(99, 10));
|
||||||
|
assert_values_match((uint32_t) 2, int_log_floor(100, 10));
|
||||||
|
assert_values_match((uint32_t) 2, int_log_floor(101, 10));
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_cstr_utils() {
|
||||||
|
BEGIN_TEST();
|
||||||
|
|
||||||
|
assert_values_match((uint32_t) 42, (uint32_t) cstr_utils::length("THROWN FROM __nac3_error_dummy_raise!!!!!!"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_test_utils() {
|
||||||
|
test_int_log_10();
|
||||||
|
test_cstr_utils();
|
||||||
|
}
|
|
@ -661,90 +661,6 @@ pub fn call_min<'ctx>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `np_min` builtin function.
|
|
||||||
pub fn call_numpy_min<'ctx, G: CodeGenerator + ?Sized>(
|
|
||||||
generator: &mut G,
|
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
|
||||||
a: (Type, BasicValueEnum<'ctx>),
|
|
||||||
) -> Result<BasicValueEnum<'ctx>, String> {
|
|
||||||
const FN_NAME: &str = "np_min";
|
|
||||||
|
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
|
||||||
|
|
||||||
let (a_ty, a) = a;
|
|
||||||
|
|
||||||
Ok(match a {
|
|
||||||
BasicValueEnum::IntValue(_) | BasicValueEnum::FloatValue(_) => {
|
|
||||||
debug_assert!([
|
|
||||||
ctx.primitives.bool,
|
|
||||||
ctx.primitives.int32,
|
|
||||||
ctx.primitives.uint32,
|
|
||||||
ctx.primitives.int64,
|
|
||||||
ctx.primitives.uint64,
|
|
||||||
ctx.primitives.float,
|
|
||||||
]
|
|
||||||
.iter()
|
|
||||||
.any(|ty| ctx.unifier.unioned(a_ty, *ty)));
|
|
||||||
|
|
||||||
a
|
|
||||||
}
|
|
||||||
|
|
||||||
BasicValueEnum::PointerValue(n)
|
|
||||||
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);
|
|
||||||
let llvm_ndarray_ty = ctx.get_llvm_type(generator, elem_ty);
|
|
||||||
|
|
||||||
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));
|
|
||||||
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
|
||||||
let n_sz_eqz = ctx
|
|
||||||
.builder
|
|
||||||
.build_int_compare(IntPredicate::NE, n_sz, n_sz.get_type().const_zero(), "")
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
ctx.make_assert(
|
|
||||||
generator,
|
|
||||||
n_sz_eqz,
|
|
||||||
"0:ValueError",
|
|
||||||
"zero-size array to reduction operation minimum which has no identity",
|
|
||||||
[None, None, None],
|
|
||||||
ctx.current_loc,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let accumulator_addr = generator.gen_var_alloc(ctx, llvm_ndarray_ty, None)?;
|
|
||||||
unsafe {
|
|
||||||
let identity =
|
|
||||||
n.data().get_unchecked(ctx, generator, &llvm_usize.const_zero(), None);
|
|
||||||
ctx.builder.build_store(accumulator_addr, identity).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
gen_for_callback_incrementing(
|
|
||||||
generator,
|
|
||||||
ctx,
|
|
||||||
llvm_usize.const_int(1, false),
|
|
||||||
(n_sz, false),
|
|
||||||
|generator, ctx, _, idx| {
|
|
||||||
let elem = unsafe { n.data().get_unchecked(ctx, generator, &idx, None) };
|
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
|
||||||
let result = call_min(ctx, (elem_ty, accumulator), (elem_ty, elem));
|
|
||||||
ctx.builder.build_store(accumulator_addr, result).unwrap();
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
},
|
|
||||||
llvm_usize.const_int(1, false),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
|
||||||
accumulator
|
|
||||||
}
|
|
||||||
|
|
||||||
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Invokes the `np_minimum` builtin function.
|
/// Invokes the `np_minimum` builtin function.
|
||||||
pub fn call_numpy_minimum<'ctx, G: CodeGenerator + ?Sized>(
|
pub fn call_numpy_minimum<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
|
@ -877,18 +793,20 @@ pub fn call_max<'ctx>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Invokes the `np_max` builtin function.
|
/// Invokes the `np_max`, `np_min`, `np_argmax`, `np_argmin` functions
|
||||||
pub fn call_numpy_max<'ctx, G: CodeGenerator + ?Sized>(
|
/// * `fn_name`: Can be one of `"np_argmin"`, `"np_argmax"`, `"np_max"`, `"np_min"`
|
||||||
|
pub fn call_numpy_max_min<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator: &mut G,
|
generator: &mut G,
|
||||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
a: (Type, BasicValueEnum<'ctx>),
|
a: (Type, BasicValueEnum<'ctx>),
|
||||||
|
fn_name: &str,
|
||||||
) -> Result<BasicValueEnum<'ctx>, String> {
|
) -> Result<BasicValueEnum<'ctx>, String> {
|
||||||
const FN_NAME: &str = "np_max";
|
debug_assert!(["np_argmin", "np_argmax", "np_max", "np_min"].iter().any(|f| *f == fn_name));
|
||||||
|
|
||||||
|
let llvm_int64 = ctx.ctx.i64_type();
|
||||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
let (a_ty, a) = a;
|
let (a_ty, a) = a;
|
||||||
|
|
||||||
Ok(match a {
|
Ok(match a {
|
||||||
BasicValueEnum::IntValue(_) | BasicValueEnum::FloatValue(_) => {
|
BasicValueEnum::IntValue(_) | BasicValueEnum::FloatValue(_) => {
|
||||||
debug_assert!([
|
debug_assert!([
|
||||||
|
@ -902,9 +820,12 @@ pub fn call_numpy_max<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
.iter()
|
.iter()
|
||||||
.any(|ty| ctx.unifier.unioned(a_ty, *ty)));
|
.any(|ty| ctx.unifier.unioned(a_ty, *ty)));
|
||||||
|
|
||||||
a
|
match fn_name {
|
||||||
|
"np_argmin" | "np_argmax" => llvm_int64.const_zero().into(),
|
||||||
|
"np_max" | "np_min" => a,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()) =>
|
||||||
{
|
{
|
||||||
|
@ -923,41 +844,81 @@ pub fn call_numpy_max<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
generator,
|
generator,
|
||||||
n_sz_eqz,
|
n_sz_eqz,
|
||||||
"0:ValueError",
|
"0:ValueError",
|
||||||
"zero-size array to reduction operation minimum which has no identity",
|
format!("zero-size array to reduction operation {fn_name}").as_str(),
|
||||||
[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)?;
|
||||||
|
let res_idx = generator.gen_var_alloc(ctx, llvm_int64.into(), 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();
|
||||||
|
ctx.builder.build_store(res_idx, llvm_int64.const_zero()).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
gen_for_callback_incrementing(
|
gen_for_callback_incrementing(
|
||||||
generator,
|
generator,
|
||||||
ctx,
|
ctx,
|
||||||
llvm_usize.const_int(1, false),
|
llvm_int64.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 cur_idx = ctx.builder.build_load(res_idx, "").unwrap();
|
||||||
|
|
||||||
|
let result = match fn_name {
|
||||||
|
"np_argmin" | "np_min" => {
|
||||||
|
call_min(ctx, (elem_ty, accumulator), (elem_ty, elem))
|
||||||
|
}
|
||||||
|
"np_argmax" | "np_max" => {
|
||||||
|
call_max(ctx, (elem_ty, accumulator), (elem_ty, elem))
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let updated_idx = match (accumulator, result) {
|
||||||
|
(BasicValueEnum::IntValue(m), BasicValueEnum::IntValue(n)) => ctx
|
||||||
|
.builder
|
||||||
|
.build_select(
|
||||||
|
ctx.builder.build_int_compare(IntPredicate::NE, m, n, "").unwrap(),
|
||||||
|
idx.into(),
|
||||||
|
cur_idx,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
(BasicValueEnum::FloatValue(m), BasicValueEnum::FloatValue(n)) => ctx
|
||||||
|
.builder
|
||||||
|
.build_select(
|
||||||
|
ctx.builder
|
||||||
|
.build_float_compare(FloatPredicate::ONE, m, n, "")
|
||||||
|
.unwrap(),
|
||||||
|
idx.into(),
|
||||||
|
cur_idx,
|
||||||
|
"",
|
||||||
|
)
|
||||||
|
.unwrap(),
|
||||||
|
_ => unsupported_type(ctx, fn_name, &[elem_ty, elem_ty]),
|
||||||
|
};
|
||||||
|
ctx.builder.build_store(res_idx, updated_idx).unwrap();
|
||||||
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_int64.const_int(1, false),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let accumulator = ctx.builder.build_load(accumulator_addr, "").unwrap();
|
match fn_name {
|
||||||
accumulator
|
"np_argmin" | "np_argmax" => ctx.builder.build_load(res_idx, "").unwrap(),
|
||||||
|
"np_max" | "np_min" => ctx.builder.build_load(accumulator_addr, "").unwrap(),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => unsupported_type(ctx, FN_NAME, &[a_ty]),
|
_ => unsupported_type(ctx, fn_name, &[a_ty]),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -576,6 +576,21 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
params: [Option<IntValue<'ctx>>; 3],
|
params: [Option<IntValue<'ctx>>; 3],
|
||||||
loc: Location,
|
loc: Location,
|
||||||
) {
|
) {
|
||||||
|
let error_id = self.resolver.get_string_id(name);
|
||||||
|
let error_id = self.ctx.i32_type().const_int(error_id as u64, false);
|
||||||
|
self.raise_exn_by_id(generator, error_id, msg, params, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn raise_exn_by_id<G: CodeGenerator + ?Sized>(
|
||||||
|
&mut self,
|
||||||
|
generator: &mut G,
|
||||||
|
error_id: IntValue<'ctx>,
|
||||||
|
msg: BasicValueEnum<'ctx>,
|
||||||
|
params: [Option<IntValue<'ctx>>; 3],
|
||||||
|
loc: Location,
|
||||||
|
) {
|
||||||
|
assert_eq!(error_id.get_type().get_bit_width(), 32);
|
||||||
|
|
||||||
let zelf = if let Some(exception_val) = self.exception_val {
|
let zelf = if let Some(exception_val) = self.exception_val {
|
||||||
exception_val
|
exception_val
|
||||||
} else {
|
} else {
|
||||||
|
@ -588,8 +603,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
let zero = int32.const_zero();
|
let zero = int32.const_zero();
|
||||||
unsafe {
|
unsafe {
|
||||||
let id_ptr = self.builder.build_in_bounds_gep(zelf, &[zero, zero], "exn.id").unwrap();
|
let id_ptr = self.builder.build_in_bounds_gep(zelf, &[zero, zero], "exn.id").unwrap();
|
||||||
let id = self.resolver.get_string_id(name);
|
self.builder.build_store(id_ptr, error_id).unwrap();
|
||||||
self.builder.build_store(id_ptr, int32.const_int(id as u64, false)).unwrap();
|
|
||||||
let ptr = self
|
let ptr = self
|
||||||
.builder
|
.builder
|
||||||
.build_in_bounds_gep(zelf, &[zero, int32.const_int(5, false)], "exn.msg")
|
.build_in_bounds_gep(zelf, &[zero, int32.const_int(5, false)], "exn.msg")
|
||||||
|
@ -652,6 +666,32 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||||
self.raise_exn(generator, err_name, err_msg, params, loc);
|
self.raise_exn(generator, err_name, err_msg, params, loc);
|
||||||
self.builder.position_at_end(then_block);
|
self.builder.position_at_end(then_block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn make_assert_impl_by_id<G: CodeGenerator + ?Sized>(
|
||||||
|
&mut self,
|
||||||
|
generator: &mut G,
|
||||||
|
cond: IntValue<'ctx>,
|
||||||
|
err_id: IntValue<'ctx>,
|
||||||
|
err_msg: BasicValueEnum<'ctx>,
|
||||||
|
params: [Option<IntValue<'ctx>>; 3],
|
||||||
|
loc: Location,
|
||||||
|
) {
|
||||||
|
let i1 = self.ctx.bool_type();
|
||||||
|
let i1_true = i1.const_all_ones();
|
||||||
|
// we assume that the condition is most probably true, so the normal path is the most
|
||||||
|
// probable path
|
||||||
|
// even if this assumption is violated, it does not matter as exception unwinding is
|
||||||
|
// slow anyway...
|
||||||
|
let cond = call_expect(self, cond, i1_true, Some("expect"));
|
||||||
|
let current_bb = self.builder.get_insert_block().unwrap();
|
||||||
|
let current_fun = current_bb.get_parent().unwrap();
|
||||||
|
let then_block = self.ctx.insert_basic_block_after(current_bb, "succ");
|
||||||
|
let exn_block = self.ctx.append_basic_block(current_fun, "fail");
|
||||||
|
self.builder.build_conditional_branch(cond, then_block, exn_block).unwrap();
|
||||||
|
self.builder.position_at_end(exn_block);
|
||||||
|
self.raise_exn_by_id(generator, err_id, err_msg, params, loc);
|
||||||
|
self.builder.position_at_end(then_block);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// See [`CodeGenerator::gen_constructor`].
|
/// See [`CodeGenerator::gen_constructor`].
|
||||||
|
@ -2457,7 +2497,29 @@ pub fn gen_expr<'ctx, G: CodeGenerator>(
|
||||||
Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()),
|
Some((_, Some(static_value), _)) => ValueEnum::Static(static_value.clone()),
|
||||||
None => {
|
None => {
|
||||||
let resolver = ctx.resolver.clone();
|
let resolver = ctx.resolver.clone();
|
||||||
resolver.get_symbol_value(*id, ctx).unwrap()
|
if let Some(res) = resolver.get_symbol_value(*id, ctx) {
|
||||||
|
res
|
||||||
|
} else {
|
||||||
|
// Allow "raise Exception" short form
|
||||||
|
let def_id = resolver.get_identifier_def(*id).map_err(|e| {
|
||||||
|
format!("{} (at {})", e.iter().next().unwrap(), expr.location)
|
||||||
|
})?;
|
||||||
|
let def = ctx.top_level.definitions.read();
|
||||||
|
if let TopLevelDef::Class { constructor, .. } = *def[def_id.0].read() {
|
||||||
|
let TypeEnum::TFunc(signature) =
|
||||||
|
ctx.unifier.get_ty(constructor.unwrap()).as_ref().clone()
|
||||||
|
else {
|
||||||
|
return Err(format!(
|
||||||
|
"Failed to resolve symbol {} (at {})",
|
||||||
|
id, expr.location
|
||||||
|
));
|
||||||
|
};
|
||||||
|
return Ok(generator
|
||||||
|
.gen_call(ctx, None, (&signature, def_id), Vec::default())?
|
||||||
|
.map(Into::into));
|
||||||
|
}
|
||||||
|
return Err(format!("Failed to resolve symbol {} (at {})", id, expr.location));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
ExprKind::List { elts, .. } => {
|
ExprKind::List { elts, .. } => {
|
||||||
|
|
|
@ -0,0 +1,196 @@
|
||||||
|
use inkwell::types::IntType;
|
||||||
|
use inkwell::values::IntValue;
|
||||||
|
|
||||||
|
use crate::codegen::optics::*;
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
use crate::codegen::CodeGenerator;
|
||||||
|
|
||||||
|
use super::util::get_sized_dependent_function_name;
|
||||||
|
use super::util::FunctionBuilder;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct StrLens<'ctx> {
|
||||||
|
pub size_type: IntType<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: nac3core has hardcoded a lot of "str"
|
||||||
|
pub struct StrFields<'ctx> {
|
||||||
|
pub content: GepGetter<AddressLens<IntLens<'ctx>>>,
|
||||||
|
pub length: GepGetter<IntLens<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructureOptic<'ctx> for StrLens<'ctx> {
|
||||||
|
type Fields = StrFields<'ctx>;
|
||||||
|
|
||||||
|
fn struct_name(&self) -> &'static str {
|
||||||
|
"str"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
|
||||||
|
StrFields {
|
||||||
|
content: builder.add_field("content", AddressLens(IntLens(builder.ctx.i8_type()))),
|
||||||
|
length: builder.add_field("length", IntLens(self.size_type)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ErrorIdsFields<'ctx> {
|
||||||
|
pub index_error: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub value_error: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub assertion_error: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub runtime_error: GepGetter<IntLens<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ErrorIdsLens;
|
||||||
|
|
||||||
|
impl<'ctx> StructureOptic<'ctx> for ErrorIdsLens {
|
||||||
|
type Fields = ErrorIdsFields<'ctx>;
|
||||||
|
|
||||||
|
fn struct_name(&self) -> &'static str {
|
||||||
|
"ErrorIds"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
|
||||||
|
let i32_lens = IntLens(builder.ctx.i32_type());
|
||||||
|
ErrorIdsFields {
|
||||||
|
index_error: builder.add_field("index_error", i32_lens),
|
||||||
|
value_error: builder.add_field("value_error", i32_lens),
|
||||||
|
assertion_error: builder.add_field("assertion_error", i32_lens),
|
||||||
|
runtime_error: builder.add_field("runtime_error", i32_lens),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ErrorContextFields<'ctx> {
|
||||||
|
pub error_ids: GepGetter<AddressLens<ErrorIdsLens>>,
|
||||||
|
pub error_id: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub message_template: GepGetter<AddressLens<IntLens<'ctx>>>,
|
||||||
|
pub param1: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub param2: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub param3: GepGetter<IntLens<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct ErrorContextLens;
|
||||||
|
|
||||||
|
impl<'ctx> StructureOptic<'ctx> for ErrorContextLens {
|
||||||
|
type Fields = ErrorContextFields<'ctx>;
|
||||||
|
|
||||||
|
fn struct_name(&self) -> &'static str {
|
||||||
|
"ErrorContext"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
|
||||||
|
ErrorContextFields {
|
||||||
|
error_ids: builder.add_field("error_ids", AddressLens(ErrorIdsLens)),
|
||||||
|
error_id: builder.add_field("error_id", IntLens(builder.ctx.i32_type())),
|
||||||
|
message_template: builder
|
||||||
|
.add_field("message_template", AddressLens(IntLens(builder.ctx.i8_type()))),
|
||||||
|
param1: builder.add_field("param1", IntLens(builder.ctx.i64_type())),
|
||||||
|
param2: builder.add_field("param2", IntLens(builder.ctx.i64_type())),
|
||||||
|
param3: builder.add_field("param3", IntLens(builder.ctx.i64_type())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_error_ids<'ctx>(ctx: &CodeGenContext<'ctx, '_>) -> Address<'ctx, ErrorIdsLens> {
|
||||||
|
// ErrorIdsLens.get_fields(ctx.ctx).assertion_error.
|
||||||
|
let error_ids = ErrorIdsLens.alloca(ctx, "error_ids");
|
||||||
|
let llvm_i32 = ctx.ctx.i32_type();
|
||||||
|
|
||||||
|
let get_string_id =
|
||||||
|
|string_id| llvm_i32.const_int(ctx.resolver.get_string_id(string_id) as u64, false);
|
||||||
|
|
||||||
|
error_ids.focus(ctx, |fields| &fields.index_error).store(ctx, &get_string_id("0:IndexError"));
|
||||||
|
error_ids.focus(ctx, |fields| &fields.value_error).store(ctx, &get_string_id("0:ValueError"));
|
||||||
|
error_ids
|
||||||
|
.focus(ctx, |fields| &fields.assertion_error)
|
||||||
|
.store(ctx, &get_string_id("0:AssertionError"));
|
||||||
|
error_ids
|
||||||
|
.focus(ctx, |fields| &fields.runtime_error)
|
||||||
|
.store(ctx, &get_string_id("0:RuntimeError"));
|
||||||
|
|
||||||
|
error_ids
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_error_context_initialize<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
errctx: &Address<'ctx, ErrorContextLens>,
|
||||||
|
error_ids: &Address<'ctx, ErrorIdsLens>,
|
||||||
|
) {
|
||||||
|
FunctionBuilder::begin(ctx, "__nac3_error_context_initialize")
|
||||||
|
.arg("errctx", &AddressLens(ErrorContextLens), errctx)
|
||||||
|
.arg("error_ids", &AddressLens(ErrorIdsLens), error_ids)
|
||||||
|
.returning_void();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_error_context_has_no_error<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
errctx: &Address<'ctx, ErrorContextLens>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
FunctionBuilder::begin(ctx, "__nac3_error_context_has_no_error")
|
||||||
|
.arg("errctx", &AddressLens(ErrorContextLens), errctx)
|
||||||
|
.returning("has_error", &IntLens(ctx.ctx.bool_type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_error_context_get_error_str<'ctx>(
|
||||||
|
size_type: IntType<'ctx>,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
errctx: &Address<'ctx, ErrorContextLens>,
|
||||||
|
dst_str: &Address<'ctx, StrLens<'ctx>>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
FunctionBuilder::begin(
|
||||||
|
ctx,
|
||||||
|
&get_sized_dependent_function_name(size_type, "__nac3_error_context_get_error_str"),
|
||||||
|
)
|
||||||
|
.arg("errctx", &AddressLens(ErrorContextLens), errctx)
|
||||||
|
.arg("dst_str", &AddressLens(StrLens { size_type }), dst_str)
|
||||||
|
.returning("has_error", &IntLens(ctx.ctx.bool_type()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn prepare_error_context<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
) -> Address<'ctx, ErrorContextLens> {
|
||||||
|
let error_ids = build_error_ids(ctx);
|
||||||
|
let errctx_ptr = ErrorContextLens.alloca(ctx, "errctx");
|
||||||
|
call_nac3_error_context_initialize(ctx, &errctx_ptr, &error_ids);
|
||||||
|
errctx_ptr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn check_error_context<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
errctx_ptr: &Address<'ctx, ErrorContextLens>,
|
||||||
|
) {
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let has_error = call_nac3_error_context_has_no_error(ctx, errctx_ptr);
|
||||||
|
let error_str_ptr = StrLens { size_type }.alloca(ctx, "error_str");
|
||||||
|
call_nac3_error_context_get_error_str(size_type, ctx, errctx_ptr, &error_str_ptr);
|
||||||
|
|
||||||
|
let error_id = errctx_ptr.focus(ctx, |fields| &fields.error_id).load(ctx, "error_id");
|
||||||
|
let error_str = error_str_ptr.load(ctx, "error_str");
|
||||||
|
let param1 = errctx_ptr.focus(ctx, |fields| &fields.param1).load(ctx, "param1");
|
||||||
|
let param2 = errctx_ptr.focus(ctx, |fields| &fields.param2).load(ctx, "param2");
|
||||||
|
let param3 = errctx_ptr.focus(ctx, |fields| &fields.param3).load(ctx, "param3");
|
||||||
|
ctx.make_assert_impl_by_id(
|
||||||
|
generator,
|
||||||
|
has_error,
|
||||||
|
error_id,
|
||||||
|
error_str.get_llvm_value(),
|
||||||
|
[Some(param1), Some(param2), Some(param3)],
|
||||||
|
ctx.current_loc,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_dummy_raise<G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext,
|
||||||
|
) {
|
||||||
|
let errctx = prepare_error_context(ctx);
|
||||||
|
FunctionBuilder::begin(ctx, "__nac3_error_dummy_raise")
|
||||||
|
.arg("errctx", &AddressLens(ErrorContextLens), &errctx)
|
||||||
|
.returning_void();
|
||||||
|
check_error_context(generator, ctx, &errctx);
|
||||||
|
}
|
|
@ -21,6 +21,11 @@ use inkwell::{
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
use nac3parser::ast::Expr;
|
use nac3parser::ast::Expr;
|
||||||
|
|
||||||
|
pub mod error_context;
|
||||||
|
pub mod numpy;
|
||||||
|
pub mod test;
|
||||||
|
pub mod util;
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn load_irrt(ctx: &Context) -> Module {
|
pub fn load_irrt(ctx: &Context) -> Module {
|
||||||
let bitcode_buf = MemoryBuffer::create_from_memory_range(
|
let bitcode_buf = MemoryBuffer::create_from_memory_range(
|
||||||
|
|
|
@ -0,0 +1,415 @@
|
||||||
|
use std::marker::PhantomData;
|
||||||
|
|
||||||
|
use inkwell::{
|
||||||
|
types::{BasicType, BasicTypeEnum, IntType},
|
||||||
|
values::{BasicValueEnum, IntValue, PointerValue},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::optics::*;
|
||||||
|
use crate::{
|
||||||
|
codegen::{
|
||||||
|
classes::{ListValue, UntypedArrayLikeAccessor},
|
||||||
|
stmt::gen_for_callback_incrementing,
|
||||||
|
CodeGenContext, CodeGenerator,
|
||||||
|
},
|
||||||
|
typecheck::typedef::{Type, TypeEnum},
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
error_context::{check_error_context, prepare_error_context, ErrorContextLens},
|
||||||
|
util::{get_sized_dependent_function_name, FunctionBuilder},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct NpArrayFields<'ctx> {
|
||||||
|
pub data: GepGetter<AddressLens<IntLens<'ctx>>>,
|
||||||
|
pub itemsize: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub ndims: GepGetter<IntLens<'ctx>>,
|
||||||
|
pub shape: GepGetter<AddressLens<IntLens<'ctx>>>,
|
||||||
|
pub strides: GepGetter<AddressLens<IntLens<'ctx>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct NpArrayLens<'ctx> {
|
||||||
|
pub size_type: IntType<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> StructureOptic<'ctx> for NpArrayLens<'ctx> {
|
||||||
|
type Fields = NpArrayFields<'ctx>;
|
||||||
|
|
||||||
|
fn struct_name(&self) -> &'static str {
|
||||||
|
"NDArray"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields {
|
||||||
|
NpArrayFields {
|
||||||
|
data: builder.add_field("data", AddressLens(IntLens(builder.ctx.i8_type()))),
|
||||||
|
itemsize: builder.add_field("itemsize", IntLens(builder.ctx.i8_type())),
|
||||||
|
ndims: builder.add_field("ndims", IntLens(builder.ctx.i8_type())),
|
||||||
|
shape: builder.add_field("shape", AddressLens(IntLens(self.size_type))),
|
||||||
|
strides: builder.add_field("strides", AddressLens(IntLens(self.size_type))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Other convenient utilities for NpArray
|
||||||
|
impl<'ctx> Address<'ctx, NpArrayLens<'ctx>> {
|
||||||
|
pub fn shape_array(&self, ctx: &CodeGenContext<'ctx, '_>) -> ArraySlice<'ctx, IntLens<'ctx>> {
|
||||||
|
let ndims = self.focus(ctx, |fields| &fields.ndims).load(ctx, "ndims");
|
||||||
|
let shape_base_ptr = self.focus(ctx, |fields| &fields.shape).load(ctx, "shape");
|
||||||
|
ArraySlice { num_elements: ndims, base: shape_base_ptr }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn strides_array(&self, ctx: &CodeGenContext<'ctx, '_>) -> ArraySlice<'ctx, IntLens<'ctx>> {
|
||||||
|
let ndims = self.focus(ctx, |fields| &fields.ndims).load(ctx, "ndims");
|
||||||
|
let strides_base_ptr = self.focus(ctx, |fields| &fields.strides).load(ctx, "strides");
|
||||||
|
ArraySlice { num_elements: ndims, base: strides_base_ptr }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type ProducerWriteToArray<'ctx, G, ElementOptic> = Box<
|
||||||
|
dyn Fn(
|
||||||
|
&mut G,
|
||||||
|
&mut CodeGenContext<'ctx, '_>,
|
||||||
|
&ArraySlice<'ctx, ElementOptic>,
|
||||||
|
) -> Result<(), String>
|
||||||
|
+ 'ctx,
|
||||||
|
>;
|
||||||
|
|
||||||
|
struct Producer<'ctx, G: CodeGenerator + ?Sized, ElementOptic> {
|
||||||
|
pub count: IntValue<'ctx>,
|
||||||
|
pub write_to_array: ProducerWriteToArray<'ctx, G, ElementOptic>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: UPDATE DOCUMENTATION
|
||||||
|
/// LLVM-typed implementation for generating a [`Producer`] that sets a list of ints.
|
||||||
|
///
|
||||||
|
/// * `elem_ty` - The element type of the `NDArray`.
|
||||||
|
/// * `shape` - The `shape` parameter used to construct the `NDArray`.
|
||||||
|
///
|
||||||
|
/// ### Notes on `shape`
|
||||||
|
///
|
||||||
|
/// Just like numpy, the `shape` argument can be:
|
||||||
|
/// 1. A list of `int32`; e.g., `np.empty([600, 800, 3])`
|
||||||
|
/// 2. A tuple of `int32`; e.g., `np.empty((600, 800, 3))`
|
||||||
|
/// 3. A scalar `int32`; e.g., `np.empty(3)`, this is functionally equivalent to `np.empty([3])`
|
||||||
|
///
|
||||||
|
/// See also [`typecheck::type_inferencer::fold_numpy_function_call_shape_argument`] to
|
||||||
|
/// learn how `shape` gets from being a Python user expression to here.
|
||||||
|
pub fn parse_input_shape_arg<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
shape: BasicValueEnum<'ctx>,
|
||||||
|
shape_ty: Type,
|
||||||
|
) -> Producer<'ctx, G, IntLens<'ctx>>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
match &*ctx.unifier.get_ty(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])`
|
||||||
|
|
||||||
|
// A list has to be a PointerValue
|
||||||
|
let shape_list = ListValue::from_ptr_val(shape.into_pointer_value(), size_type, None);
|
||||||
|
|
||||||
|
// Create `Producer`
|
||||||
|
let ndims = shape_list.load_size(ctx, Some("count"));
|
||||||
|
Producer {
|
||||||
|
count: ndims,
|
||||||
|
write_to_array: Box::new(move |ctx, generator, dst_array| {
|
||||||
|
// Basically iterate through the list and write to `dst_slice` accordingly
|
||||||
|
let init_val = size_type.const_zero();
|
||||||
|
let max_val = (ndims, false);
|
||||||
|
let incr_val = size_type.const_int(1, false);
|
||||||
|
gen_for_callback_incrementing(
|
||||||
|
ctx,
|
||||||
|
generator,
|
||||||
|
init_val,
|
||||||
|
max_val,
|
||||||
|
|generator, ctx, _hooks, axis| {
|
||||||
|
// Get the dimension at `axis`
|
||||||
|
let dim =
|
||||||
|
shape_list.data().get(ctx, generator, &axis, None).into_int_value();
|
||||||
|
|
||||||
|
// Cast `dim` to SizeT
|
||||||
|
let dim = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_s_extend_or_bit_cast(dim, size_type, "dim_casted")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Write
|
||||||
|
dst_array.ix(ctx, axis, "dim").store(ctx, &dim);
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
incr_val,
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TypeEnum::TTuple { ty: tuple_types } => {
|
||||||
|
// 2. A tuple of ints; e.g., `np.empty((600, 800, 3))`
|
||||||
|
|
||||||
|
// Get the length/size of the tuple, which also happens to be the value of `ndims`.
|
||||||
|
let ndims = tuple_types.len();
|
||||||
|
|
||||||
|
// A tuple has to be a StructValue
|
||||||
|
// Read [`codegen::expr::gen_expr`] to see how `nac3core` translates a Python tuple into LLVM.
|
||||||
|
let shape_tuple = shape.into_struct_value();
|
||||||
|
|
||||||
|
Producer {
|
||||||
|
count: size_type.const_int(ndims as u64, false),
|
||||||
|
write_to_array: Box::new(move |_generator, ctx, dst_array| {
|
||||||
|
for axis in 0..ndims {
|
||||||
|
// Get the dimension at `axis`
|
||||||
|
let dim = ctx
|
||||||
|
.builder
|
||||||
|
.build_extract_value(
|
||||||
|
shape_tuple,
|
||||||
|
axis as u32,
|
||||||
|
format!("dim{axis}").as_str(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
.into_int_value();
|
||||||
|
|
||||||
|
// Cast `dim` to SizeT
|
||||||
|
let dim = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_s_extend_or_bit_cast(dim, size_type, "dim_casted")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Write
|
||||||
|
dst_array
|
||||||
|
.ix(ctx, size_type.const_int(axis as u64, false), "dim")
|
||||||
|
.store(ctx, &dim);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TypeEnum::TObj { obj_id, .. }
|
||||||
|
if *obj_id == ctx.primitives.int32.obj_id(&ctx.unifier).unwrap() =>
|
||||||
|
{
|
||||||
|
// 3. A scalar int; e.g., `np.empty(3)`, this is functionally equivalent to `np.empty([3])`
|
||||||
|
|
||||||
|
// The value has to be an integer
|
||||||
|
let shape_int = shape.into_int_value();
|
||||||
|
|
||||||
|
Producer {
|
||||||
|
count: size_type.const_int(1, false),
|
||||||
|
write_to_array: Box::new(move |_generator, ctx, dst_array| {
|
||||||
|
// Cast `shape_int` to SizeT
|
||||||
|
let dim = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_s_extend_or_bit_cast(shape_int, size_type, "dim_casted")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Write
|
||||||
|
dst_array
|
||||||
|
.ix(ctx, size_type.const_zero() /* Only index 0 is set */, "dim")
|
||||||
|
.store(ctx, &dim);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => panic!("parse_input_shape_arg encountered unknown type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn alloca_ndarray<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_type: BasicTypeEnum<'ctx>,
|
||||||
|
ndims: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Address<'ctx, NpArrayLens<'ctx>>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
// Allocate ndarray
|
||||||
|
let ndarray_ptr = NpArrayLens { size_type }.alloca(ctx, name);
|
||||||
|
|
||||||
|
// Set ndims
|
||||||
|
ndarray_ptr.focus(ctx, |fields| &fields.ndims).store(ctx, &ndims);
|
||||||
|
|
||||||
|
// Set itemsize
|
||||||
|
let itemsize = elem_type.size_of().unwrap();
|
||||||
|
let itemsize =
|
||||||
|
ctx.builder.build_int_s_extend_or_bit_cast(itemsize, size_type, "itemsize").unwrap();
|
||||||
|
ndarray_ptr.focus(ctx, |fields| &fields.itemsize).store(ctx, &itemsize);
|
||||||
|
|
||||||
|
// Allocate and set shape
|
||||||
|
let shape_ptr = ctx.builder.build_array_alloca(size_type, ndims, "shape").unwrap();
|
||||||
|
ndarray_ptr
|
||||||
|
.focus(ctx, |fields| &fields.shape)
|
||||||
|
.store(ctx, &Address { addressee_optic: IntLens(size_type), address: shape_ptr });
|
||||||
|
|
||||||
|
// Allocate and set strides
|
||||||
|
let strides_ptr = ctx.builder.build_array_alloca(size_type, ndims, "strides").unwrap();
|
||||||
|
ndarray_ptr
|
||||||
|
.focus(ctx, |fields| &fields.strides)
|
||||||
|
.store(ctx, &Address { addressee_optic: IntLens(size_type), address: strides_ptr });
|
||||||
|
|
||||||
|
Ok(ndarray_ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum NDArrayInitMode<'ctx, G: CodeGenerator + ?Sized> {
|
||||||
|
NDim { ndim: IntValue<'ctx> },
|
||||||
|
Shape { shape: Producer<'ctx, G, IntLens<'ctx>> },
|
||||||
|
ShapeAndAllocaData { shape: Producer<'ctx, G, IntLens<'ctx>> },
|
||||||
|
}
|
||||||
|
|
||||||
|
/// TODO: DOCUMENT ME
|
||||||
|
pub fn alloca_ndarray_and_init<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_type: BasicTypeEnum<'ctx>,
|
||||||
|
init_mode: NDArrayInitMode<'ctx, G>,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Address<'ctx, NpArrayLens<'ctx>>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
// It is implemented verbosely in order to make the initialization modes super clear in their intent.
|
||||||
|
match init_mode {
|
||||||
|
NDArrayInitMode::NDim { ndim } => {
|
||||||
|
let ndarray = alloca_ndarray(generator, ctx, elem_type, ndims, name)?;
|
||||||
|
Ok(ndarray)
|
||||||
|
}
|
||||||
|
NDArrayInitMode::Shape { shape } => {
|
||||||
|
let ndims = shape.count;
|
||||||
|
let ndarray_ptr = alloca_ndarray(generator, ctx, elem_type, ndims, name)?;
|
||||||
|
|
||||||
|
// Fill `ndarray.shape`
|
||||||
|
(shape.write_to_array)(generator, ctx, &ndarray_ptr.shape_array(ctx))?;
|
||||||
|
|
||||||
|
// Check if `shape` has bad inputs
|
||||||
|
call_nac3_ndarray_util_assert_shape_no_negative(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndims,
|
||||||
|
&ndarray_ptr.focus(ctx, |fields| &fields.shape).load(ctx, "shape"),
|
||||||
|
);
|
||||||
|
|
||||||
|
// NOTE: DO NOT DO `set_strides_by_shape` HERE.
|
||||||
|
// Simply this is because we specified that `SetShape` wouldn't do `set_strides_by_shape`
|
||||||
|
|
||||||
|
Ok(ndarray_ptr)
|
||||||
|
}
|
||||||
|
NDArrayInitMode::ShapeAndAllocaData { shape } => {
|
||||||
|
let ndims = shape.count;
|
||||||
|
let ndarray_ptr = alloca_ndarray(generator, ctx, elem_type, ndims, name)?;
|
||||||
|
|
||||||
|
// Fill `ndarray.shape`
|
||||||
|
(shape.write_to_array)(generator, ctx, &ndarray_ptr.shape_array(ctx))?;
|
||||||
|
|
||||||
|
// Check if `shape` has bad inputs
|
||||||
|
call_nac3_ndarray_util_assert_shape_no_negative(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
ndims,
|
||||||
|
&ndarray_ptr.focus(ctx, |fields| &fields.shape).load(ctx, "shape"),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Now we populate `ndarray.data` by alloca-ing.
|
||||||
|
// But first, we need to know the size of the ndarray to know how many elements to alloca,
|
||||||
|
// since calculating nbytes of an ndarray requires `ndarray.shape` to be set.
|
||||||
|
let ndarray_nbytes = call_nac3_ndarray_nbytes(generator, ctx, &ndarray_ptr);
|
||||||
|
|
||||||
|
// Alloca `data` and assign it to `ndarray.data`
|
||||||
|
let data_ptr =
|
||||||
|
ctx.builder.build_array_alloca(ctx.ctx.i8_type(), ndarray_nbytes, "data").unwrap();
|
||||||
|
ndarray_ptr.focus(ctx, |fields| &fields.data).store(
|
||||||
|
ctx,
|
||||||
|
&Address { addressee_optic: IntLens::int8(ctx.ctx), address: data_ptr },
|
||||||
|
);
|
||||||
|
|
||||||
|
// Finally, do `set_strides_by_shape`
|
||||||
|
// Check out https://ajcr.net/stride-guide-part-1/ to see what numpy "strides" are.
|
||||||
|
call_nac3_ndarray_set_strides_by_shape(generator, ctx, &ndarray_ptr);
|
||||||
|
|
||||||
|
Ok(ndarray_ptr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call_nac3_ndarray_util_assert_shape_no_negative<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndims: IntValue<'ctx>,
|
||||||
|
shape_ptr: &Address<'ctx, IntLens<'ctx>>,
|
||||||
|
) {
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
let errctx = prepare_error_context(ctx);
|
||||||
|
FunctionBuilder::begin(
|
||||||
|
ctx,
|
||||||
|
&get_sized_dependent_function_name(
|
||||||
|
size_type,
|
||||||
|
"__nac3_ndarray_util_assert_shape_no_negative",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.arg("errctx", &AddressLens(ErrorContextLens), &errctx)
|
||||||
|
.arg("ndims", &IntLens(size_type), &ndims)
|
||||||
|
.arg("shape", &AddressLens(IntLens(size_type)), shape_ptr)
|
||||||
|
.returning_void();
|
||||||
|
check_error_context(generator, ctx, &errctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call_nac3_ndarray_set_strides_by_shape<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray_ptr: &Address<'ctx, NpArrayLens<'ctx>>,
|
||||||
|
) {
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
FunctionBuilder::begin(
|
||||||
|
ctx,
|
||||||
|
&get_sized_dependent_function_name(
|
||||||
|
size_type,
|
||||||
|
"__nac3_ndarray_util_assert_shape_no_negative",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.arg("ndarray", &AddressLens(NpArrayLens { size_type }), ndarray_ptr)
|
||||||
|
.returning_void();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call_nac3_ndarray_nbytes<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray_ptr: &Address<'ctx, NpArrayLens<'ctx>>,
|
||||||
|
) -> IntValue<'ctx> {
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
FunctionBuilder::begin(
|
||||||
|
ctx,
|
||||||
|
&get_sized_dependent_function_name(
|
||||||
|
size_type,
|
||||||
|
"__nac3_ndarray_util_assert_shape_no_negative",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.arg("ndarray", &AddressLens(NpArrayLens { size_type }), ndarray_ptr)
|
||||||
|
.returning("nbytes", &IntLens(size_type))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn call_nac3_ndarray_fill_generic<'ctx, G: CodeGenerator + ?Sized>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
ndarray_ptr: &Address<'ctx, NpArrayLens<'ctx>>,
|
||||||
|
fill_value_ptr: PointerValue<'ctx>,
|
||||||
|
) {
|
||||||
|
let size_type = generator.get_size_type(ctx.ctx);
|
||||||
|
|
||||||
|
FunctionBuilder::begin(
|
||||||
|
ctx,
|
||||||
|
&get_sized_dependent_function_name(size_type, "__nac3_ndarray_fill_generic"),
|
||||||
|
)
|
||||||
|
.arg("ndarray", &AddressLens(NpArrayLens { size_type }), ndarray_ptr)
|
||||||
|
.arg("pvalue", &OpaqueAddressLens, fill_value_ptr)
|
||||||
|
.returning_void();
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::{path::Path, process::Command};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn run_irrt_test() {
|
||||||
|
assert!(
|
||||||
|
cfg!(feature = "test"),
|
||||||
|
"Please do `cargo test -F test` to compile `irrt_test.out` and run test"
|
||||||
|
);
|
||||||
|
|
||||||
|
let irrt_test_out_path = Path::new(concat!(env!("OUT_DIR"), "/irrt_test.out"));
|
||||||
|
let output = Command::new(irrt_test_out_path.to_str().unwrap()).output().unwrap();
|
||||||
|
if !output.status.success() {
|
||||||
|
eprintln!("irrt_test failed with status {}:", output.status);
|
||||||
|
eprintln!("====== stdout ======");
|
||||||
|
eprintln!("{}", String::from_utf8(output.stdout).unwrap());
|
||||||
|
eprintln!("====== stderr ======");
|
||||||
|
eprintln!("{}", String::from_utf8(output.stderr).unwrap());
|
||||||
|
eprintln!("====================");
|
||||||
|
|
||||||
|
panic!("irrt_test failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,77 @@
|
||||||
|
use inkwell::{
|
||||||
|
types::{BasicMetadataTypeEnum, BasicType, IntType},
|
||||||
|
values::{AnyValue, BasicMetadataValueEnum},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::optics::*;
|
||||||
|
use crate::{codegen::CodeGenContext, util::SizeVariant};
|
||||||
|
|
||||||
|
fn get_size_variant(ty: IntType) -> SizeVariant {
|
||||||
|
match ty.get_bit_width() {
|
||||||
|
32 => SizeVariant::Bits32,
|
||||||
|
64 => SizeVariant::Bits64,
|
||||||
|
_ => unreachable!("Unsupported int type bit width {}", ty.get_bit_width()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn get_sized_dependent_function_name(ty: IntType, fn_name: &str) -> String {
|
||||||
|
let mut fn_name = fn_name.to_owned();
|
||||||
|
match get_size_variant(ty) {
|
||||||
|
SizeVariant::Bits32 => {
|
||||||
|
// Do nothing, `fn_name` already has the correct name
|
||||||
|
}
|
||||||
|
SizeVariant::Bits64 => {
|
||||||
|
// Append "64", this is the naming convention
|
||||||
|
fn_name.push_str("64");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn_name
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Variadic argument?
|
||||||
|
pub struct FunctionBuilder<'ctx, 'a> {
|
||||||
|
ctx: &'a CodeGenContext<'ctx, 'a>,
|
||||||
|
fn_name: &'a str,
|
||||||
|
arguments: Vec<(BasicMetadataTypeEnum<'ctx>, BasicMetadataValueEnum<'ctx>)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, 'a> FunctionBuilder<'ctx, 'a> {
|
||||||
|
pub fn begin(ctx: &'a CodeGenContext<'ctx, 'a>, fn_name: &'a str) -> Self {
|
||||||
|
FunctionBuilder { ctx, fn_name, arguments: Vec::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
// The name is for self-documentation
|
||||||
|
#[must_use]
|
||||||
|
pub fn arg<S: MemoryOptic<'ctx>>(mut self, _name: &'static str, optic: &S, arg: &S::MemoryValue) -> Self {
|
||||||
|
self.arguments
|
||||||
|
.push((optic.get_llvm_type(self.ctx.ctx).into(), arg.get_llvm_value().into()));
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn returning<S: Prism<'ctx>>(self, name: &'static str, return_prism: &S) -> S::MemoryValue {
|
||||||
|
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
|
||||||
|
|
||||||
|
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
|
||||||
|
let return_type = return_prism.get_llvm_type(self.ctx.ctx);
|
||||||
|
let fn_type = return_type.fn_type(¶m_tys, false);
|
||||||
|
self.ctx.module.add_function(self.fn_name, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
let ret = self.ctx.builder.build_call(function, ¶m_vals, name).unwrap();
|
||||||
|
return_prism.review(ret.as_any_value_enum())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Code duplication, but otherwise returning<S: Optic<'ctx>> cannot resolve S if return_optic = None
|
||||||
|
pub fn returning_void(self) {
|
||||||
|
let (param_tys, param_vals): (Vec<_>, Vec<_>) = self.arguments.into_iter().unzip();
|
||||||
|
|
||||||
|
let function = self.ctx.module.get_function(self.fn_name).unwrap_or_else(|| {
|
||||||
|
let return_type = self.ctx.ctx.void_type();
|
||||||
|
let fn_type = return_type.fn_type(¶m_tys, false);
|
||||||
|
self.ctx.module.add_function(self.fn_name, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
self.ctx.builder.build_call(function, ¶m_vals, "").unwrap();
|
||||||
|
}
|
||||||
|
}
|
|
@ -35,6 +35,54 @@ fn get_float_intrinsic_repr(ctx: &Context, ft: FloatType) -> &'static str {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Invokes the [`llvm.lifetime.start`](https://releases.llvm.org/14.0.0/docs/LangRef.html#llvm-lifetime-start-intrinsic)
|
||||||
|
/// intrinsic.
|
||||||
|
pub fn call_lifetime_start<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
size: IntValue<'ctx>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) {
|
||||||
|
const FN_NAME: &str = "llvm.lifetime.start";
|
||||||
|
// NOTE: inkwell temporary workaround, see [`call_stackrestore`] for details
|
||||||
|
let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
||||||
|
let llvm_void = ctx.ctx.void_type();
|
||||||
|
let llvm_i64 = ctx.ctx.i64_type();
|
||||||
|
let llvm_p0i8 = ctx.ctx.i8_type().ptr_type(AddressSpace::default());
|
||||||
|
let fn_type = llvm_void.fn_type(&[llvm_i64.into(), llvm_p0i8.into()], false);
|
||||||
|
|
||||||
|
ctx.module.add_function(FN_NAME, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(intrinsic_fn, &[size.into(), ptr.into()], "")
|
||||||
|
.map(CallSiteValue::try_as_basic_value)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Invokes the [`llvm.lifetime.end`](https://releases.llvm.org/14.0.0/docs/LangRef.html#llvm-lifetime-end-intrinsic)
|
||||||
|
/// intrinsic.
|
||||||
|
pub fn call_lifetime_end<'ctx>(
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
size: IntValue<'ctx>,
|
||||||
|
ptr: PointerValue<'ctx>,
|
||||||
|
) {
|
||||||
|
const FN_NAME: &str = "llvm.lifetime.end";
|
||||||
|
// NOTE: inkwell temporary workaround, see [`call_stackrestore`] for details
|
||||||
|
let intrinsic_fn = ctx.module.get_function(FN_NAME).unwrap_or_else(|| {
|
||||||
|
let llvm_void = ctx.ctx.void_type();
|
||||||
|
let llvm_i64 = ctx.ctx.i64_type();
|
||||||
|
let llvm_p0i8 = ctx.ctx.i8_type().ptr_type(AddressSpace::default());
|
||||||
|
let fn_type = llvm_void.fn_type(&[llvm_i64.into(), llvm_p0i8.into()], false);
|
||||||
|
|
||||||
|
ctx.module.add_function(FN_NAME, fn_type, None)
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.builder
|
||||||
|
.build_call(intrinsic_fn, &[size.into(), ptr.into()], "")
|
||||||
|
.map(CallSiteValue::try_as_basic_value)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
/// Invokes the [`llvm.stacksave`](https://llvm.org/docs/LangRef.html#llvm-stacksave-intrinsic)
|
/// Invokes the [`llvm.stacksave`](https://llvm.org/docs/LangRef.html#llvm-stacksave-intrinsic)
|
||||||
/// intrinsic.
|
/// intrinsic.
|
||||||
pub fn call_stacksave<'ctx>(
|
pub fn call_stacksave<'ctx>(
|
||||||
|
|
|
@ -23,8 +23,10 @@ use inkwell::{
|
||||||
values::{BasicValueEnum, FunctionValue, IntValue, PhiValue, PointerValue},
|
values::{BasicValueEnum, FunctionValue, IntValue, PhiValue, PointerValue},
|
||||||
AddressSpace, IntPredicate, OptimizationLevel,
|
AddressSpace, IntPredicate, OptimizationLevel,
|
||||||
};
|
};
|
||||||
|
use irrt::error_context::StrLens;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use nac3parser::ast::{Location, Stmt, StrRef};
|
use nac3parser::ast::{Location, Stmt, StrRef};
|
||||||
|
use optics::MemoryOptic as _;
|
||||||
use parking_lot::{Condvar, Mutex};
|
use parking_lot::{Condvar, Mutex};
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
|
@ -42,6 +44,8 @@ mod generator;
|
||||||
pub mod irrt;
|
pub mod irrt;
|
||||||
pub mod llvm_intrinsics;
|
pub mod llvm_intrinsics;
|
||||||
pub mod numpy;
|
pub mod numpy;
|
||||||
|
pub mod numpy_new;
|
||||||
|
pub mod optics;
|
||||||
pub mod stmt;
|
pub mod stmt;
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -646,6 +650,8 @@ pub fn gen_func_impl<
|
||||||
..primitives
|
..primitives
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let llvm_str_ty =
|
||||||
|
StrLens { size_type: generator.get_size_type(context) }.get_llvm_type(context);
|
||||||
let mut type_cache: HashMap<_, _> = [
|
let mut type_cache: HashMap<_, _> = [
|
||||||
(primitives.int32, context.i32_type().into()),
|
(primitives.int32, context.i32_type().into()),
|
||||||
(primitives.int64, context.i64_type().into()),
|
(primitives.int64, context.i64_type().into()),
|
||||||
|
@ -653,21 +659,7 @@ pub fn gen_func_impl<
|
||||||
(primitives.uint64, context.i64_type().into()),
|
(primitives.uint64, context.i64_type().into()),
|
||||||
(primitives.float, context.f64_type().into()),
|
(primitives.float, context.f64_type().into()),
|
||||||
(primitives.bool, context.i8_type().into()),
|
(primitives.bool, context.i8_type().into()),
|
||||||
(primitives.str, {
|
(primitives.str, llvm_str_ty),
|
||||||
let name = "str";
|
|
||||||
match module.get_struct_type(name) {
|
|
||||||
None => {
|
|
||||||
let str_type = context.opaque_struct_type("str");
|
|
||||||
let fields = [
|
|
||||||
context.i8_type().ptr_type(AddressSpace::default()).into(),
|
|
||||||
generator.get_size_type(context).into(),
|
|
||||||
];
|
|
||||||
str_type.set_body(&fields, false);
|
|
||||||
str_type.into()
|
|
||||||
}
|
|
||||||
Some(t) => t.as_basic_type_enum(),
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
(primitives.range, RangeType::new(context).as_base_type().into()),
|
(primitives.range, RangeType::new(context).as_base_type().into()),
|
||||||
(primitives.exception, {
|
(primitives.exception, {
|
||||||
let name = "Exception";
|
let name = "Exception";
|
||||||
|
@ -677,7 +669,7 @@ pub fn gen_func_impl<
|
||||||
let exception = context.opaque_struct_type("Exception");
|
let exception = context.opaque_struct_type("Exception");
|
||||||
let int32 = context.i32_type().into();
|
let int32 = context.i32_type().into();
|
||||||
let int64 = context.i64_type().into();
|
let int64 = context.i64_type().into();
|
||||||
let str_ty = module.get_struct_type("str").unwrap().as_basic_type_enum();
|
let str_ty = llvm_str_ty;
|
||||||
let fields = [int32, str_ty, int32, int32, str_ty, str_ty, int64, int64, int64];
|
let fields = [int32, str_ty, int32, int32, str_ty, str_ty, int64, int64, int64];
|
||||||
exception.set_body(&fields, false);
|
exception.set_body(&fields, false);
|
||||||
exception.ptr_type(AddressSpace::default()).as_basic_type_enum()
|
exception.ptr_type(AddressSpace::default()).as_basic_type_enum()
|
||||||
|
|
|
@ -0,0 +1,91 @@
|
||||||
|
use inkwell::values::{BasicValueEnum, PointerValue};
|
||||||
|
use nac3parser::ast::StrRef;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
codegen::optics::build_opaque_alloca, symbol_resolver::ValueEnum, toplevel::DefinitionId, typecheck::typedef::{FunSignature, Type}
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
irrt::{
|
||||||
|
self,
|
||||||
|
numpy::{alloca_ndarray_and_init, parse_input_shape_arg, NDArrayInitMode, NpArrayLens},
|
||||||
|
},
|
||||||
|
optics::Address,
|
||||||
|
CodeGenContext, CodeGenerator,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// LLVM-typed implementation for generating the implementation for constructing an empty `NDArray`.
|
||||||
|
fn call_ndarray_empty_impl<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: BasicValueEnum<'ctx>,
|
||||||
|
shape_ty: Type,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Address<'ctx, NpArrayLens<'ctx>>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
let elem_type = ctx.get_llvm_type(generator, elem_ty);
|
||||||
|
let shape = parse_input_shape_arg(generator, ctx, shape, shape_ty);
|
||||||
|
let ndarray_ptr = alloca_ndarray_and_init(
|
||||||
|
generator,
|
||||||
|
ctx,
|
||||||
|
elem_type,
|
||||||
|
NDArrayInitMode::ShapeAndAllocaData { shape },
|
||||||
|
name,
|
||||||
|
)?;
|
||||||
|
Ok(ndarray_ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn call_ndarray_fill_impl<'ctx, G>(
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
elem_ty: Type,
|
||||||
|
shape: BasicValueEnum<'ctx>,
|
||||||
|
shape_ty: Type,
|
||||||
|
fill_value: BasicValueEnum<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Result<Address<'ctx, NpArrayLens<'ctx>>, String>
|
||||||
|
where
|
||||||
|
G: CodeGenerator + ?Sized,
|
||||||
|
{
|
||||||
|
let ndarray_ptr = call_ndarray_empty_impl(generator, ctx, elem_ty, shape, shape_ty, name)?;
|
||||||
|
|
||||||
|
// NOTE: fill_value's type is not checked!!
|
||||||
|
let fill_value_ptr = build_opaque_alloca(ctx, fill_value.get_type(), name);
|
||||||
|
fill_value_ptr.store(ctx, );
|
||||||
|
// let fill_value_ptr = ctx.builder.build_alloca(, "fill_value_ptr").unwrap();
|
||||||
|
// ctx.builder.build_store(fill_value_ptr, fill_value);
|
||||||
|
|
||||||
|
// let ok = irrt::numpy::call_nac3_ndarray_fill_generic(generator, ctx, ndarray_ptr, Address { fill_value_ptr } );
|
||||||
|
todo!()
|
||||||
|
Ok(ndarray_ptr)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generates LLVM IR for `np.empty`.
|
||||||
|
pub fn gen_ndarray_empty<'ctx, G>(
|
||||||
|
context: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
obj: &Option<(Type, ValueEnum<'ctx>)>,
|
||||||
|
fun: (&FunSignature, DefinitionId),
|
||||||
|
args: &[(Option<StrRef>, ValueEnum<'ctx>)],
|
||||||
|
generator: &mut dyn CodeGenerator,
|
||||||
|
) -> Result<PointerValue<'ctx>, String> {
|
||||||
|
assert!(obj.is_none());
|
||||||
|
assert_eq!(args.len(), 1);
|
||||||
|
|
||||||
|
// Parse arguments
|
||||||
|
let shape_ty = fun.0.args[0].ty;
|
||||||
|
let shape = args[0].1.clone().to_basic_value_enum(context, generator, shape_ty)?;
|
||||||
|
|
||||||
|
// Implementation
|
||||||
|
let ndarray_ptr = call_ndarray_empty_impl(
|
||||||
|
generator,
|
||||||
|
context,
|
||||||
|
context.primitives.float,
|
||||||
|
shape,
|
||||||
|
shape_ty,
|
||||||
|
"empty_ndarray",
|
||||||
|
)?;
|
||||||
|
Ok(ndarray_ptr.address)
|
||||||
|
}
|
|
@ -0,0 +1,130 @@
|
||||||
|
use inkwell::{
|
||||||
|
context::Context,
|
||||||
|
types::{BasicType, BasicTypeEnum},
|
||||||
|
values::{AnyValue, BasicValue, BasicValueEnum, PointerValue},
|
||||||
|
AddressSpace,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::core::{MemoryGetter, MemoryOptic, MemorySetter, OpticValue, Prism};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Address<'ctx, AddresseeOptic> {
|
||||||
|
pub addressee_optic: AddresseeOptic,
|
||||||
|
pub address: PointerValue<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddresseeOptic> Address<'ctx, AddresseeOptic> {
|
||||||
|
pub fn cast_to<S: MemoryOptic<'ctx>>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
new_optic: S,
|
||||||
|
) -> Address<'ctx, S> {
|
||||||
|
let to_ptr_type = new_optic.get_llvm_type(ctx.ctx).ptr_type(AddressSpace::default());
|
||||||
|
let casted_address =
|
||||||
|
ctx.builder.build_pointer_cast(self.address, to_ptr_type, "ptr_casted").unwrap();
|
||||||
|
Address { addressee_optic: new_optic, address: casted_address }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cast_to_opaque(&self) -> OpaqueAddress<'ctx> {
|
||||||
|
OpaqueAddress(self.address)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddresseeOptic> OpticValue<'ctx> for Address<'ctx, AddresseeOptic> {
|
||||||
|
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
|
||||||
|
self.address.as_basic_value_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct AddressLens<AddresseeOptic>(pub AddresseeOptic);
|
||||||
|
|
||||||
|
impl<'ctx, AddresseeOptic: MemoryOptic<'ctx>> MemoryOptic<'ctx> for AddressLens<AddresseeOptic> {
|
||||||
|
type MemoryValue = Address<'ctx, AddresseeOptic>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
|
||||||
|
self.0.get_llvm_type(ctx).ptr_type(AddressSpace::default()).as_basic_type_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddresseeOptic: MemoryOptic<'ctx>> Prism<'ctx> for AddressLens<AddresseeOptic> {
|
||||||
|
fn review<V: AnyValue<'ctx>>(&self, value: V) -> Self::MemoryValue {
|
||||||
|
Address {
|
||||||
|
addressee_optic: self.0.clone(),
|
||||||
|
address: value.as_any_value_enum().into_pointer_value(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddressesOptic: MemoryOptic<'ctx>> MemoryGetter<'ctx> for AddressLens<AddressesOptic> {
|
||||||
|
fn get(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Self::MemoryValue {
|
||||||
|
self.review(ctx.builder.build_load(pointer, name).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddressesOptic: MemoryOptic<'ctx>> MemorySetter<'ctx> for AddressLens<AddressesOptic> {
|
||||||
|
fn set(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
value: &Self::MemoryValue,
|
||||||
|
) {
|
||||||
|
ctx.builder.build_store(pointer, value.address).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// To make [`Address`] convenient to use
|
||||||
|
impl<'ctx, AddresseeOptic: MemoryGetter<'ctx>> Address<'ctx, AddresseeOptic> {
|
||||||
|
pub fn load(&self, ctx: &CodeGenContext<'ctx, '_>, name: &str) -> AddresseeOptic::MemoryValue {
|
||||||
|
self.addressee_optic.get(ctx, self.address, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// To make [`Address`] convenient to use
|
||||||
|
impl<'ctx, AddresseeOptic: MemorySetter<'ctx>> Address<'ctx, AddresseeOptic> {
|
||||||
|
pub fn store(&self, ctx: &CodeGenContext<'ctx, '_>, value: &AddresseeOptic::MemoryValue) {
|
||||||
|
self.addressee_optic.set(ctx, self.address, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct OpaqueAddress<'ctx>(pub PointerValue<'ctx>);
|
||||||
|
|
||||||
|
impl<'ctx> OpaqueAddress<'ctx> {
|
||||||
|
pub fn cast_to<AddresseeOptic: MemoryOptic<'ctx>>(
|
||||||
|
&self,
|
||||||
|
ctx: &'ctx CodeGenContext,
|
||||||
|
addressee_optic: AddresseeOptic,
|
||||||
|
name: &str,
|
||||||
|
) -> Address<'ctx, AddresseeOptic> {
|
||||||
|
let ptr = ctx.builder.build_pointer_cast(
|
||||||
|
self.0,
|
||||||
|
addressee_optic.get_llvm_type(ctx.ctx).ptr_type(AddressSpace::default()),
|
||||||
|
name,
|
||||||
|
);
|
||||||
|
Address { addressee_optic, address: ptr }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct OpaqueAddressLens;
|
||||||
|
|
||||||
|
impl<'ctx> MemoryOptic<'ctx> for OpaqueAddressLens {
|
||||||
|
type MemoryValue = BasicValueEnum<'ctx>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
|
||||||
|
ctx.i8_type().ptr_type(AddressSpace::default()).as_basic_type_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> OpaqueAddress<'ctx> {
|
||||||
|
pub fn store(&self, ctx: &CodeGenContext<'ctx, '_>, value: BasicValueEnum<'ctx>) {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
use inkwell::{
|
||||||
|
context::Context,
|
||||||
|
types::BasicTypeEnum,
|
||||||
|
values::{AnyValue, BasicValue, BasicValueEnum, PointerValue},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::address::Address;
|
||||||
|
|
||||||
|
// TODO: Write a taxonomy
|
||||||
|
|
||||||
|
pub trait OpticValue<'ctx> {
|
||||||
|
fn get_llvm_value(&self) -> BasicValueEnum<'ctx>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T: BasicValue<'ctx>> OpticValue<'ctx> for T {
|
||||||
|
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
|
||||||
|
self.as_basic_value_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: The interface is unintuitive
|
||||||
|
pub trait MemoryOptic<'ctx>: Clone {
|
||||||
|
type MemoryValue: OpticValue<'ctx>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx>;
|
||||||
|
|
||||||
|
fn alloca(&self, ctx: &CodeGenContext<'ctx, '_>, name: &str) -> Address<'ctx, Self> {
|
||||||
|
let ptr = ctx.builder.build_alloca(self.get_llvm_type(ctx.ctx), name).unwrap();
|
||||||
|
Address { addressee_optic: self.clone(), address: ptr }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Prism<'ctx>: MemoryOptic<'ctx> {
|
||||||
|
// TODO: Return error if `review` fails
|
||||||
|
fn review<V: AnyValue<'ctx>>(&self, value: V) -> Self::MemoryValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MemoryGetter<'ctx>: MemoryOptic<'ctx> {
|
||||||
|
fn get(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Self::MemoryValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait MemorySetter<'ctx>: MemoryOptic<'ctx> {
|
||||||
|
fn set(&self, ctx: &CodeGenContext<'ctx, '_>, pointer: PointerValue<'ctx>, value: &Self::MemoryValue);
|
||||||
|
}
|
|
@ -0,0 +1,53 @@
|
||||||
|
use inkwell::{
|
||||||
|
context::Context,
|
||||||
|
types::{BasicType, BasicTypeEnum},
|
||||||
|
values::PointerValue,
|
||||||
|
AddressSpace,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
address::Address,
|
||||||
|
core::{MemoryGetter, MemoryOptic},
|
||||||
|
};
|
||||||
|
|
||||||
|
// ((Memory, Pointer) -> ElementOptic::Value*)
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct GepGetter<ElementOptic> {
|
||||||
|
/// The LLVM GEP index
|
||||||
|
pub gep_index: u64,
|
||||||
|
/// Element (or field in the context of `struct`s) name. Used for cosmetics.
|
||||||
|
pub name: &'static str,
|
||||||
|
/// The lens to view the actual value after applying this [`FieldLens<T>`]
|
||||||
|
pub element_optic: ElementOptic,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, ElementOptic: MemoryOptic<'ctx>> MemoryOptic<'ctx> for GepGetter<ElementOptic> {
|
||||||
|
type MemoryValue = Address<'ctx, ElementOptic>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
|
||||||
|
self.element_optic.get_llvm_type(ctx).ptr_type(AddressSpace::default()).as_basic_type_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, ElementOptic: MemoryOptic<'ctx>> MemoryGetter<'ctx> for GepGetter<ElementOptic> {
|
||||||
|
fn get(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Self::MemoryValue {
|
||||||
|
let llvm_i32 = ctx.ctx.i32_type(); // TODO: I think I'm not supposed to *just* use i32 for GEP like that
|
||||||
|
let element_ptr = unsafe {
|
||||||
|
ctx.builder
|
||||||
|
.build_in_bounds_gep(
|
||||||
|
pointer,
|
||||||
|
&[llvm_i32.const_zero(), llvm_i32.const_int(self.gep_index, false)],
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
};
|
||||||
|
Address { address: element_ptr, addressee_optic: self.element_optic.clone() }
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,66 @@
|
||||||
|
use inkwell::{
|
||||||
|
context::Context,
|
||||||
|
types::{BasicType, BasicTypeEnum, IntType},
|
||||||
|
values::{AnyValue, BasicValue, IntValue, PointerValue},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::core::{MemoryGetter, MemorySetter, MemoryOptic, Prism};
|
||||||
|
|
||||||
|
// NOTE: I wanted to make Int8Lens, Int16Lens, Int32Lens, with all
|
||||||
|
// having the trait IsIntLens, and implement `impl <S: IsIntLens> Optic<S> for T`,
|
||||||
|
// but that clashes with StructureOptic!!
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct IntLens<'ctx>(pub IntType<'ctx>);
|
||||||
|
|
||||||
|
impl<'ctx> IntLens<'ctx> {
|
||||||
|
#[must_use]
|
||||||
|
pub fn int8(ctx: &'ctx Context) -> IntLens<'ctx> {
|
||||||
|
IntLens(ctx.i8_type())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn int32(ctx: &'ctx Context) -> IntLens<'ctx> {
|
||||||
|
IntLens(ctx.i32_type())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn int64(ctx: &'ctx Context) -> IntLens<'ctx> {
|
||||||
|
IntLens(ctx.i64_type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> MemoryOptic<'ctx> for IntLens<'ctx> {
|
||||||
|
type MemoryValue = IntValue<'ctx>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, _ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
|
||||||
|
self.0.as_basic_type_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> Prism<'ctx> for IntLens<'ctx> {
|
||||||
|
fn review<V: AnyValue<'ctx>>(&self, value: V) -> Self::MemoryValue {
|
||||||
|
let int = value.as_any_value_enum().into_int_value();
|
||||||
|
debug_assert_eq!(int.get_type().get_bit_width(), self.0.get_bit_width());
|
||||||
|
int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> MemoryGetter<'ctx> for IntLens<'ctx> {
|
||||||
|
fn get(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Self::MemoryValue {
|
||||||
|
self.review(ctx.builder.build_load(pointer, name).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> MemorySetter<'ctx> for IntLens<'ctx> {
|
||||||
|
fn set(&self, ctx: &CodeGenContext<'ctx, '_>, pointer: PointerValue<'ctx>, int: &Self::MemoryValue) {
|
||||||
|
debug_assert_eq!(int.get_type().get_bit_width(), self.0.get_bit_width());
|
||||||
|
ctx.builder.build_store(pointer, int.as_basic_value_enum()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,65 @@
|
||||||
|
use inkwell::values::IntValue;
|
||||||
|
|
||||||
|
use crate::codegen::{CodeGenContext, CodeGenerator};
|
||||||
|
|
||||||
|
use super::address::Address;
|
||||||
|
|
||||||
|
// Name inspired by https://hackage.haskell.org/package/lens-5.3.2/docs/Control-Lens-At.html#t:Ixed
|
||||||
|
pub trait Ixed<'ctx, ElementOptic> {
|
||||||
|
// TODO: Interface/Method to expose the IntType of index?
|
||||||
|
// or even make index itself parameterized? (probably no)
|
||||||
|
|
||||||
|
fn ix(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
index: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Address<'ctx, ElementOptic>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Can do interface seggregation
|
||||||
|
pub trait BoundedIxed<'ctx, ElementOptic>: Ixed<'ctx, ElementOptic> {
|
||||||
|
fn num_elements(&self, ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx>;
|
||||||
|
|
||||||
|
// Check if 0 <= index < self.num_elements()
|
||||||
|
fn ix_bounds_checked<G: CodeGenerator + ?Sized>(
|
||||||
|
&self,
|
||||||
|
generator: &mut G,
|
||||||
|
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||||
|
index: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Address<'ctx, ElementOptic> {
|
||||||
|
let num_elements = self.num_elements(ctx);
|
||||||
|
let int_type = num_elements.get_type(); // NOTE: Weird get_type(), see comment under `trait Ixed`
|
||||||
|
|
||||||
|
assert_eq!(int_type.get_bit_width(), index.get_type().get_bit_width()); // Might as well check bit width to catch bugs
|
||||||
|
|
||||||
|
// TODO: SGE or UGE? or make it defined by the implementee?
|
||||||
|
|
||||||
|
// Check `0 <= index`
|
||||||
|
let lower_bounded = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_compare(
|
||||||
|
inkwell::IntPredicate::SLE,
|
||||||
|
int_type.const_zero(),
|
||||||
|
index,
|
||||||
|
"lower_bounded",
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Check `index < num_elements`
|
||||||
|
let upper_bounded = ctx
|
||||||
|
.builder
|
||||||
|
.build_int_compare(inkwell::IntPredicate::SLT, index, num_elements, "upper_bounded")
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
// Compute `0 <= index && index < num_elements`
|
||||||
|
let bounded = ctx.builder.build_and(lower_bounded, upper_bounded, "bounded").unwrap();
|
||||||
|
|
||||||
|
// Assert `bounded`
|
||||||
|
ctx.make_assert(generator, bounded, "0:IndexError", "nac3core LLVM codegen attempting to access out of bounds array index {0}. Must satisfy 0 <= index < {2}", [Some(index), Some(num_elements), None], ctx.current_loc);
|
||||||
|
|
||||||
|
// ...and finally do indexing
|
||||||
|
self.ix(ctx, index, name)
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,15 @@
|
||||||
|
pub mod address;
|
||||||
|
pub mod core;
|
||||||
|
pub mod gep;
|
||||||
|
pub mod int;
|
||||||
|
pub mod ixed;
|
||||||
|
pub mod slice;
|
||||||
|
pub mod structure;
|
||||||
|
|
||||||
|
pub use address::*;
|
||||||
|
pub use core::*;
|
||||||
|
pub use gep::*;
|
||||||
|
pub use int::*;
|
||||||
|
pub use ixed::*;
|
||||||
|
pub use slice::*;
|
||||||
|
pub use structure::*;
|
|
@ -0,0 +1,36 @@
|
||||||
|
use super::{
|
||||||
|
core::MemoryOptic,
|
||||||
|
ixed::{BoundedIxed, Ixed},
|
||||||
|
};
|
||||||
|
|
||||||
|
use inkwell::values::IntValue;
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::address::Address;
|
||||||
|
|
||||||
|
pub struct ArraySlice<'ctx, ElementOptic> {
|
||||||
|
pub num_elements: IntValue<'ctx>,
|
||||||
|
pub base: Address<'ctx, ElementOptic>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, ElementOptic: MemoryOptic<'ctx>> Ixed<'ctx, ElementOptic> for ArraySlice<'ctx, ElementOptic> {
|
||||||
|
fn ix(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
index: IntValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Address<'ctx, ElementOptic> {
|
||||||
|
let element_addr =
|
||||||
|
unsafe { ctx.builder.build_in_bounds_gep(self.base.address, &[index], name).unwrap() };
|
||||||
|
Address { address: element_addr, addressee_optic: self.base.addressee_optic.clone() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, ElementOptic: MemoryOptic<'ctx>> BoundedIxed<'ctx, ElementOptic>
|
||||||
|
for ArraySlice<'ctx, ElementOptic>
|
||||||
|
{
|
||||||
|
fn num_elements(&self, _ctx: &CodeGenContext<'ctx, '_>) -> IntValue<'ctx> {
|
||||||
|
self.num_elements
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,139 @@
|
||||||
|
use inkwell::{
|
||||||
|
context::Context,
|
||||||
|
types::{BasicType, BasicTypeEnum},
|
||||||
|
values::{BasicValue, BasicValueEnum, PointerValue, StructValue},
|
||||||
|
};
|
||||||
|
use itertools::Itertools;
|
||||||
|
|
||||||
|
use crate::codegen::CodeGenContext;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
address::Address,
|
||||||
|
core::{MemoryGetter, MemorySetter, MemoryOptic, OpticValue},
|
||||||
|
gep::GepGetter,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub trait StructureOptic<'ctx>: Clone {
|
||||||
|
// Fields of optics
|
||||||
|
type Fields;
|
||||||
|
|
||||||
|
// TODO: Make it an associated function instead?
|
||||||
|
fn struct_name(&self) -> &'static str;
|
||||||
|
|
||||||
|
fn build_fields(&self, builder: &mut FieldBuilder<'ctx>) -> Self::Fields;
|
||||||
|
|
||||||
|
fn get_fields(&self, ctx: &'ctx Context) -> Self::Fields {
|
||||||
|
let mut builder = FieldBuilder::new(ctx, self.struct_name());
|
||||||
|
self.build_fields(&mut builder)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct OpticalStructValue<'ctx, StructOptic> {
|
||||||
|
optic: StructOptic,
|
||||||
|
llvm: StructValue<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, StructOptic> OpticValue<'ctx> for OpticalStructValue<'ctx, StructOptic> {
|
||||||
|
fn get_llvm_value(&self) -> BasicValueEnum<'ctx> {
|
||||||
|
self.llvm.as_basic_value_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: check StructType
|
||||||
|
impl<'ctx, T: StructureOptic<'ctx>> MemoryOptic<'ctx> for T {
|
||||||
|
type MemoryValue = OpticalStructValue<'ctx, Self>;
|
||||||
|
|
||||||
|
fn get_llvm_type(&self, ctx: &'ctx Context) -> BasicTypeEnum<'ctx> {
|
||||||
|
let mut builder = FieldBuilder::new(ctx, self.struct_name());
|
||||||
|
self.build_fields(&mut builder); // Self::Fields is discarded
|
||||||
|
|
||||||
|
let field_types =
|
||||||
|
builder.fields.iter().map(|field_info| field_info.llvm_type).collect_vec();
|
||||||
|
ctx.struct_type(&field_types, false).as_basic_type_enum()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T: StructureOptic<'ctx>> MemoryGetter<'ctx> for T {
|
||||||
|
fn get(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
name: &str,
|
||||||
|
) -> Self::MemoryValue {
|
||||||
|
OpticalStructValue {
|
||||||
|
optic: self.clone(),
|
||||||
|
llvm: ctx.builder.build_load(pointer, name).unwrap().into_struct_value(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, T: StructureOptic<'ctx>> MemorySetter<'ctx> for T {
|
||||||
|
fn set(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
pointer: PointerValue<'ctx>,
|
||||||
|
value: &Self::MemoryValue,
|
||||||
|
) {
|
||||||
|
ctx.builder.build_store(pointer, value.llvm).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx, AddresseeOptic: StructureOptic<'ctx>> Address<'ctx, AddresseeOptic> {
|
||||||
|
pub fn focus<GetFieldGepFn, FieldElementOptic: MemoryOptic<'ctx>>(
|
||||||
|
&self,
|
||||||
|
ctx: &CodeGenContext<'ctx, '_>,
|
||||||
|
get_field_gep_fn: GetFieldGepFn,
|
||||||
|
) -> Address<'ctx, FieldElementOptic>
|
||||||
|
where
|
||||||
|
GetFieldGepFn: FnOnce(&AddresseeOptic::Fields) -> &GepGetter<FieldElementOptic>,
|
||||||
|
{
|
||||||
|
let fields = self.addressee_optic.get_fields(ctx.ctx);
|
||||||
|
let field = get_field_gep_fn(&fields);
|
||||||
|
field.get(ctx, self.address, field.name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only used by [`FieldBuilder`]
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct FieldInfo<'ctx> {
|
||||||
|
gep_index: u64,
|
||||||
|
name: &'ctx str,
|
||||||
|
llvm_type: BasicTypeEnum<'ctx>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct FieldBuilder<'ctx> {
|
||||||
|
pub ctx: &'ctx Context,
|
||||||
|
gep_index_counter: u64,
|
||||||
|
struct_name: &'ctx str,
|
||||||
|
fields: Vec<FieldInfo<'ctx>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'ctx> FieldBuilder<'ctx> {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(ctx: &'ctx Context, struct_name: &'ctx str) -> Self {
|
||||||
|
FieldBuilder { ctx, gep_index_counter: 0, struct_name, fields: Vec::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn next_gep_index(&mut self) -> u64 {
|
||||||
|
let index = self.gep_index_counter;
|
||||||
|
self.gep_index_counter += 1;
|
||||||
|
index
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn add_field<ElementOptic: MemoryOptic<'ctx>>(
|
||||||
|
&mut self,
|
||||||
|
name: &'static str,
|
||||||
|
element_optic: ElementOptic,
|
||||||
|
) -> GepGetter<ElementOptic> {
|
||||||
|
let gep_index = self.next_gep_index();
|
||||||
|
|
||||||
|
self.fields.push(FieldInfo {
|
||||||
|
gep_index,
|
||||||
|
name,
|
||||||
|
llvm_type: element_optic.get_llvm_type(self.ctx),
|
||||||
|
});
|
||||||
|
|
||||||
|
GepGetter { gep_index, name, element_optic }
|
||||||
|
}
|
||||||
|
}
|
|
@ -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(crate) 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,20 +279,11 @@ pub fn get_builtins(unifier: &mut Unifier, primitives: &PrimitiveStore) -> Built
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A helper enum used by [`BuiltinBuilder`]
|
fn get_size_variant_of_int(size_variant: SizeVariant, primitives: &PrimitiveStore) -> Type {
|
||||||
#[derive(Clone, Copy)]
|
match size_variant {
|
||||||
enum SizeVariant {
|
|
||||||
Bits32,
|
|
||||||
Bits64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl SizeVariant {
|
|
||||||
fn of_int(self, primitives: &PrimitiveStore) -> Type {
|
|
||||||
match self {
|
|
||||||
SizeVariant::Bits32 => primitives.int32,
|
SizeVariant::Bits32 => primitives.int32,
|
||||||
SizeVariant::Bits64 => primitives.int64,
|
SizeVariant::Bits64 => primitives.int64,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BuiltinBuilder<'a> {
|
struct BuiltinBuilder<'a> {
|
||||||
|
@ -510,7 +502,9 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
|
|
||||||
PrimDef::FunMin | PrimDef::FunMax => self.build_min_max_function(prim),
|
PrimDef::FunMin | PrimDef::FunMax => self.build_min_max_function(prim),
|
||||||
|
|
||||||
PrimDef::FunNpMin | PrimDef::FunNpMax => self.build_np_min_max_function(prim),
|
PrimDef::FunNpArgmin | PrimDef::FunNpArgmax | PrimDef::FunNpMin | PrimDef::FunNpMax => {
|
||||||
|
self.build_np_max_min_function(prim)
|
||||||
|
}
|
||||||
|
|
||||||
PrimDef::FunNpMinimum | PrimDef::FunNpMaximum => {
|
PrimDef::FunNpMinimum | PrimDef::FunNpMaximum => {
|
||||||
self.build_np_minimum_maximum_function(prim)
|
self.build_np_minimum_maximum_function(prim)
|
||||||
|
@ -1059,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 = get_size_variant_of_int(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));
|
||||||
|
@ -1084,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 = get_size_variant_of_int(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)?))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
|
@ -1125,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 = get_size_variant_of_int(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));
|
||||||
|
|
||||||
|
@ -1148,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 = get_size_variant_of_int(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,
|
||||||
|
@ -1555,10 +1549,19 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the functions `np_min()` and `np_max()`.
|
/// Build the functions `np_max()`, `np_min()`, `np_argmax()` and `np_argmin()`
|
||||||
fn build_np_min_max_function(&mut self, prim: PrimDef) -> TopLevelDef {
|
/// Calls `call_numpy_max_min` with the function name
|
||||||
debug_assert_prim_is_allowed(prim, &[PrimDef::FunNpMin, PrimDef::FunNpMax]);
|
fn build_np_max_min_function(&mut self, prim: PrimDef) -> TopLevelDef {
|
||||||
|
debug_assert_prim_is_allowed(
|
||||||
|
prim,
|
||||||
|
&[PrimDef::FunNpArgmin, PrimDef::FunNpArgmax, PrimDef::FunNpMin, PrimDef::FunNpMax],
|
||||||
|
);
|
||||||
|
|
||||||
|
let (var_map, ret_ty) = match prim {
|
||||||
|
PrimDef::FunNpArgmax | PrimDef::FunNpArgmin => {
|
||||||
|
(self.num_or_ndarray_var_map.clone(), self.primitives.int64)
|
||||||
|
}
|
||||||
|
PrimDef::FunNpMax | PrimDef::FunNpMin => {
|
||||||
let ret_ty = self.unifier.get_fresh_var(Some("R".into()), None);
|
let ret_ty = self.unifier.get_fresh_var(Some("R".into()), None);
|
||||||
let var_map = self
|
let var_map = self
|
||||||
.num_or_ndarray_var_map
|
.num_or_ndarray_var_map
|
||||||
|
@ -1566,28 +1569,25 @@ impl<'a> BuiltinBuilder<'a> {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.chain(once((ret_ty.id, ret_ty.ty)))
|
.chain(once((ret_ty.id, ret_ty.ty)))
|
||||||
.collect::<IndexMap<_, _>>();
|
.collect::<IndexMap<_, _>>();
|
||||||
|
(var_map, ret_ty.ty)
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
create_fn_by_codegen(
|
create_fn_by_codegen(
|
||||||
self.unifier,
|
self.unifier,
|
||||||
&var_map,
|
&var_map,
|
||||||
prim.name(),
|
prim.name(),
|
||||||
ret_ty.ty,
|
ret_ty,
|
||||||
&[(self.float_or_ndarray_ty.ty, "a")],
|
&[(self.num_or_ndarray_ty.ty, "a")],
|
||||||
Box::new(move |ctx, _, fun, args, generator| {
|
Box::new(move |ctx, _, fun, args, generator| {
|
||||||
let a_ty = fun.0.args[0].ty;
|
let a_ty = fun.0.args[0].ty;
|
||||||
let a = args[0].1.clone().to_basic_value_enum(ctx, generator, a_ty)?;
|
let a = args[0].1.clone().to_basic_value_enum(ctx, generator, a_ty)?;
|
||||||
|
|
||||||
let func = match prim {
|
Ok(Some(builtin_fns::call_numpy_max_min(generator, ctx, (a_ty, a), prim.name())?))
|
||||||
PrimDef::FunNpMin => builtin_fns::call_numpy_min,
|
|
||||||
PrimDef::FunNpMax => builtin_fns::call_numpy_max,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(Some(func(generator, ctx, (a_ty, a))?))
|
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Build the functions `np_minimum()` and `np_maximum()`.
|
/// Build the functions `np_minimum()` and `np_maximum()`.
|
||||||
fn build_np_minimum_maximum_function(&mut self, prim: PrimDef) -> TopLevelDef {
|
fn build_np_minimum_maximum_function(&mut self, prim: PrimDef) -> TopLevelDef {
|
||||||
debug_assert_prim_is_allowed(prim, &[PrimDef::FunNpMinimum, PrimDef::FunNpMaximum]);
|
debug_assert_prim_is_allowed(prim, &[PrimDef::FunNpMinimum, PrimDef::FunNpMaximum]);
|
||||||
|
|
|
@ -62,9 +62,11 @@ pub enum PrimDef {
|
||||||
FunMin,
|
FunMin,
|
||||||
FunNpMin,
|
FunNpMin,
|
||||||
FunNpMinimum,
|
FunNpMinimum,
|
||||||
|
FunNpArgmin,
|
||||||
FunMax,
|
FunMax,
|
||||||
FunNpMax,
|
FunNpMax,
|
||||||
FunNpMaximum,
|
FunNpMaximum,
|
||||||
|
FunNpArgmax,
|
||||||
FunAbs,
|
FunAbs,
|
||||||
FunNpIsNan,
|
FunNpIsNan,
|
||||||
FunNpIsInf,
|
FunNpIsInf,
|
||||||
|
@ -216,9 +218,11 @@ impl PrimDef {
|
||||||
PrimDef::FunMin => fun("min", None),
|
PrimDef::FunMin => fun("min", None),
|
||||||
PrimDef::FunNpMin => fun("np_min", None),
|
PrimDef::FunNpMin => fun("np_min", None),
|
||||||
PrimDef::FunNpMinimum => fun("np_minimum", None),
|
PrimDef::FunNpMinimum => fun("np_minimum", None),
|
||||||
|
PrimDef::FunNpArgmin => fun("np_argmin", None),
|
||||||
PrimDef::FunMax => fun("max", None),
|
PrimDef::FunMax => fun("max", None),
|
||||||
PrimDef::FunNpMax => fun("np_max", None),
|
PrimDef::FunNpMax => fun("np_max", None),
|
||||||
PrimDef::FunNpMaximum => fun("np_maximum", None),
|
PrimDef::FunNpMaximum => fun("np_maximum", None),
|
||||||
|
PrimDef::FunNpArgmax => fun("np_argmax", None),
|
||||||
PrimDef::FunAbs => fun("abs", None),
|
PrimDef::FunAbs => fun("abs", None),
|
||||||
PrimDef::FunNpIsNan => fun("np_isnan", None),
|
PrimDef::FunNpIsNan => fun("np_isnan", None),
|
||||||
PrimDef::FunNpIsInf => fun("np_isinf", None),
|
PrimDef::FunNpIsInf => fun("np_isinf", None),
|
||||||
|
|
|
@ -398,7 +398,10 @@ impl<'a> Fold<()> for Inferencer<'a> {
|
||||||
}
|
}
|
||||||
if let Some(exc) = exc {
|
if let Some(exc) = exc {
|
||||||
self.virtual_checks.push((
|
self.virtual_checks.push((
|
||||||
exc.custom.unwrap(),
|
match &*self.unifier.get_ty(exc.custom.unwrap()) {
|
||||||
|
TypeEnum::TFunc(sign) => sign.ret,
|
||||||
|
_ => exc.custom.unwrap(),
|
||||||
|
},
|
||||||
self.primitives.exception,
|
self.primitives.exception,
|
||||||
exc.location,
|
exc.location,
|
||||||
));
|
));
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
/// A helper enum used by [`BuiltinBuilder`]
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub enum SizeVariant {
|
||||||
|
Bits32,
|
||||||
|
Bits64,
|
||||||
|
}
|
|
@ -183,8 +183,10 @@ def patch(module):
|
||||||
module.np_isinf = np.isinf
|
module.np_isinf = np.isinf
|
||||||
module.np_min = np.min
|
module.np_min = np.min
|
||||||
module.np_minimum = np.minimum
|
module.np_minimum = np.minimum
|
||||||
|
module.np_argmin = np.argmin
|
||||||
module.np_max = np.max
|
module.np_max = np.max
|
||||||
module.np_maximum = np.maximum
|
module.np_maximum = np.maximum
|
||||||
|
module.np_argmax = np.argmax
|
||||||
module.np_sin = np.sin
|
module.np_sin = np.sin
|
||||||
module.np_cos = np.cos
|
module.np_cos = np.cos
|
||||||
module.np_exp = np.exp
|
module.np_exp = np.exp
|
||||||
|
|
|
@ -867,6 +867,13 @@ def test_ndarray_minimum_broadcast_rhs_scalar():
|
||||||
output_ndarray_float_2(min_x_zeros)
|
output_ndarray_float_2(min_x_zeros)
|
||||||
output_ndarray_float_2(min_x_ones)
|
output_ndarray_float_2(min_x_ones)
|
||||||
|
|
||||||
|
def test_ndarray_argmin():
|
||||||
|
x = np_array([[1., 2.], [3., 4.]])
|
||||||
|
y = np_argmin(x)
|
||||||
|
|
||||||
|
output_ndarray_float_2(x)
|
||||||
|
output_int64(y)
|
||||||
|
|
||||||
def test_ndarray_max():
|
def test_ndarray_max():
|
||||||
x = np_identity(2)
|
x = np_identity(2)
|
||||||
y = np_max(x)
|
y = np_max(x)
|
||||||
|
@ -910,6 +917,13 @@ def test_ndarray_maximum_broadcast_rhs_scalar():
|
||||||
output_ndarray_float_2(max_x_zeros)
|
output_ndarray_float_2(max_x_zeros)
|
||||||
output_ndarray_float_2(max_x_ones)
|
output_ndarray_float_2(max_x_ones)
|
||||||
|
|
||||||
|
def test_ndarray_argmax():
|
||||||
|
x = np_array([[1., 2.], [3., 4.]])
|
||||||
|
y = np_argmax(x)
|
||||||
|
|
||||||
|
output_ndarray_float_2(x)
|
||||||
|
output_int64(y)
|
||||||
|
|
||||||
def test_ndarray_abs():
|
def test_ndarray_abs():
|
||||||
x = np_identity(2)
|
x = np_identity(2)
|
||||||
y = abs(x)
|
y = abs(x)
|
||||||
|
@ -1524,11 +1538,13 @@ def run() -> int32:
|
||||||
test_ndarray_minimum_broadcast()
|
test_ndarray_minimum_broadcast()
|
||||||
test_ndarray_minimum_broadcast_lhs_scalar()
|
test_ndarray_minimum_broadcast_lhs_scalar()
|
||||||
test_ndarray_minimum_broadcast_rhs_scalar()
|
test_ndarray_minimum_broadcast_rhs_scalar()
|
||||||
|
test_ndarray_argmin()
|
||||||
test_ndarray_max()
|
test_ndarray_max()
|
||||||
test_ndarray_maximum()
|
test_ndarray_maximum()
|
||||||
test_ndarray_maximum_broadcast()
|
test_ndarray_maximum_broadcast()
|
||||||
test_ndarray_maximum_broadcast_lhs_scalar()
|
test_ndarray_maximum_broadcast_lhs_scalar()
|
||||||
test_ndarray_maximum_broadcast_rhs_scalar()
|
test_ndarray_maximum_broadcast_rhs_scalar()
|
||||||
|
test_ndarray_argmax()
|
||||||
test_ndarray_abs()
|
test_ndarray_abs()
|
||||||
test_ndarray_isnan()
|
test_ndarray_isnan()
|
||||||
test_ndarray_isinf()
|
test_ndarray_isinf()
|
||||||
|
|
Loading…
Reference in New Issue