diff --git a/.gitignore b/.gitignore index 97647e1e70..3a1fbc6423 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ target tags crates/stdarch-gen/aarch64.rs crates/stdarch-gen/arm.rs +c_programs/* +rust_programs/* \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 73f69ca46f..6efd6b189a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ members = [ "crates/core_arch", "crates/std_detect", "crates/stdarch-gen", + "crates/intrinsic-test", "examples/" ] exclude = [ diff --git a/crates/intrinsic-test/Cargo.toml b/crates/intrinsic-test/Cargo.toml new file mode 100644 index 0000000000..4cdf811e5c --- /dev/null +++ b/crates/intrinsic-test/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "intrinsic-test" +version = "0.1.0" +authors = ["Jamie Cunliffe "] +edition = "2018" + +[dependencies] +lazy_static = "1.4.0" +serde = { version = "1", features = ["derive"] } +csv = "1.1" +clap = "2.33.3" +regex = "1.4.2" +log = "0.4.11" +pretty_env_logger = "0.4.0" +rayon = "1.5.0" +diff = "0.1.12" \ No newline at end of file diff --git a/crates/intrinsic-test/README.md b/crates/intrinsic-test/README.md new file mode 100644 index 0000000000..0f60aaa040 --- /dev/null +++ b/crates/intrinsic-test/README.md @@ -0,0 +1,23 @@ +Generate and run programs using equivalent C and Rust intrinsics, checking that +each produces the same result from random inputs. + +# Usage +``` +USAGE: + intrinsic-test [OPTIONS] + +FLAGS: + -h, --help Prints help information + -V, --version Prints version information + +OPTIONS: + --cppcompiler The C++ compiler to use for compiling the c++ code [default: clang++] + --toolchain The rust toolchain to use for building the rust code [default: nightly] + +ARGS: + The input file containing the intrinsics +``` + +The intrinsic.csv is the arm neon tracking google sheet (https://docs.google.com/spreadsheets/d/1MqW1g8c7tlhdRWQixgdWvR4uJHNZzCYAf4V0oHjZkwA/edit#gid=0) +that contains the intrinsic list. The done percentage column should be renamed to "enabled". + diff --git a/crates/intrinsic-test/src/argument.rs b/crates/intrinsic-test/src/argument.rs new file mode 100644 index 0000000000..96ed440ee7 --- /dev/null +++ b/crates/intrinsic-test/src/argument.rs @@ -0,0 +1,137 @@ +use serde::{Deserialize, Deserializer}; + +use crate::types::IntrinsicType; +use crate::Language; + +/// An argument for the intrinsic. +#[derive(Debug, PartialEq, Clone)] +pub struct Argument { + /// The argument's index in the intrinsic function call. + pub pos: usize, + /// The argument name. + pub name: String, + /// The type of the argument. + pub ty: IntrinsicType, +} + +impl Argument { + /// Creates an argument from a Rust style signature i.e. `name: type` + fn from_rust(pos: usize, arg: &str) -> Result { + let mut parts = arg.split(':'); + let name = parts.next().unwrap().trim().to_string(); + let ty = IntrinsicType::from_rust(parts.next().unwrap().trim())?; + + Ok(Self { pos, name, ty }) + } + + fn to_c_type(&self) -> String { + self.ty.c_type() + } + + fn is_simd(&self) -> bool { + self.ty.is_simd() + } + + pub fn is_ptr(&self) -> bool { + self.ty.is_ptr() + } +} + +#[derive(Debug, PartialEq, Clone)] +pub struct ArgumentList { + pub args: Vec, +} + +impl ArgumentList { + /// Creates an argument list from a Rust function signature, the data for + /// this function should only be the arguments. + /// e.g. for `fn test(a: u32, b: u32) -> u32` data should just be `a: u32, b: u32` + fn from_rust_arguments(data: &str) -> Result { + let args = data + .split(',') + .enumerate() + .map(|(idx, arg)| Argument::from_rust(idx, arg)) + .collect::>()?; + + Ok(Self { args }) + } + + /// Converts the argument list into the call paramters for a C function call. + /// e.g. this would generate something like `a, &b, c` + pub fn as_call_param_c(&self) -> String { + self.args + .iter() + .map(|arg| match arg.ty { + IntrinsicType::Ptr { .. } => { + format!("&{}", arg.name) + } + IntrinsicType::Type { .. } => arg.name.clone(), + }) + .collect::>() + .join(", ") + } + + /// Converts the argument list into the call paramters for a Rust function. + /// e.g. this would generate something like `a, b, c` + pub fn as_call_param_rust(&self) -> String { + self.args + .iter() + .map(|arg| arg.name.clone()) + .collect::>() + .join(", ") + } + + /// Creates a line that initializes this argument for C code. + /// e.g. `int32x2_t a = { 0x1, 0x2 };` + pub fn init_random_values_c(&self, pass: usize) -> String { + self.iter() + .map(|arg| { + format!( + "{ty} {name} = {{ {values} }};", + ty = arg.to_c_type(), + name = arg.name, + values = arg.ty.populate_random(pass, &Language::C) + ) + }) + .collect::>() + .join("\n ") + } + + /// Creates a line that initializes this argument for Rust code. + /// e.g. `let a = transmute([0x1, 0x2]);` + pub fn init_random_values_rust(&self, pass: usize) -> String { + self.iter() + .map(|arg| { + if arg.is_simd() { + format!( + "let {name} = ::std::mem::transmute([{values}]);", + name = arg.name, + values = arg.ty.populate_random(pass, &Language::Rust), + ) + } else { + format!( + "let {name} = {value};", + name = arg.name, + value = arg.ty.populate_random(pass, &Language::Rust) + ) + } + }) + .collect::>() + .join("\n ") + } + + pub fn iter(&self) -> std::slice::Iter<'_, Argument> { + self.args.iter() + } +} + +impl<'de> Deserialize<'de> for ArgumentList { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + use serde::de::Error; + let s = String::deserialize(deserializer)?; + Self::from_rust_arguments(&s).map_err(Error::custom) + } +} diff --git a/crates/intrinsic-test/src/intrinsic.rs b/crates/intrinsic-test/src/intrinsic.rs new file mode 100644 index 0000000000..499cf7612c --- /dev/null +++ b/crates/intrinsic-test/src/intrinsic.rs @@ -0,0 +1,112 @@ +use crate::types::{IntrinsicType, TypeKind}; + +use super::argument::ArgumentList; +use serde::de::Unexpected; +use serde::{de, Deserialize, Deserializer}; + +/// An intrinsic +#[derive(Deserialize, Debug, PartialEq, Clone)] +pub struct Intrinsic { + /// If the intrinsic should be tested. + #[serde(deserialize_with = "bool_from_string")] + pub enabled: bool, + + /// The function name of this intrinsic. + pub name: String, + + /// Any arguments for this intrinsinc. + #[serde(rename = "args")] + pub arguments: ArgumentList, + + /// The return type of this intrinsic. + #[serde(rename = "return")] + pub results: IntrinsicType, +} + +impl Intrinsic { + /// Generates a std::cout for the intrinsics results that will match the + /// rust debug output format for the return type. + pub fn print_result_c(&self, index: usize) -> String { + let lanes = if self.results.num_lanes() > 1 { + (0..self.results.num_lanes()) + .map(|idx| -> std::string::String { + format!( + "{cast}{lane_fn}(__return_value, {lane})", + cast = self.results.c_promotion(), + lane_fn = self.results.get_lane_function(), + lane = idx + ) + }) + .collect::>() + .join(r#" << ", " << "#) + } else { + format!( + "{promote}cast<{cast}>(__return_value)", + cast = match self.results.kind() { + TypeKind::Float if self.results.inner_size() == 32 => "float".to_string(), + TypeKind::Float if self.results.inner_size() == 64 => "double".to_string(), + TypeKind::Int => format!("int{}_t", self.results.inner_size()), + TypeKind::UInt => format!("uint{}_t", self.results.inner_size()), + TypeKind::Poly => format!("poly{}_t", self.results.inner_size()), + ty => todo!("print_result_c - Unknown type: {:#?}", ty), + }, + promote = self.results.c_promotion(), + ) + }; + + format!( + r#"std::cout << "Result {idx}: {ty}" << std::fixed << std::setprecision(150) << {lanes} << "{close}" << std::endl;"#, + ty = if self.results.is_simd() { + format!("{}(", self.results.c_type()) + } else { + String::from("") + }, + close = if self.results.is_simd() { ")" } else { "" }, + lanes = lanes, + idx = index, + ) + } + + pub fn generate_pass_rust(&self, index: usize) -> String { + format!( + r#" + unsafe {{ + {initialized_args} + let res = {intrinsic_call}({args}); + println!("Result {idx}: {{:.150?}}", res); + }}"#, + initialized_args = self.arguments.init_random_values_rust(index), + intrinsic_call = self.name, + args = self.arguments.as_call_param_rust(), + idx = index, + ) + } + + pub fn generate_pass_c(&self, index: usize) -> String { + format!( + r#" {{ + {initialized_args} + auto __return_value = {intrinsic_call}({args}); + {print_result} + }}"#, + initialized_args = self.arguments.init_random_values_c(index), + intrinsic_call = self.name, + args = self.arguments.as_call_param_c(), + print_result = self.print_result_c(index) + ) + } +} + +fn bool_from_string<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + match String::deserialize(deserializer)?.to_uppercase().as_ref() { + "TRUE" => Ok(true), + "FALSE" => Ok(false), + other => Err(de::Error::invalid_value( + Unexpected::Str(other), + &"TRUE or FALSE", + )), + } +} diff --git a/crates/intrinsic-test/src/main.rs b/crates/intrinsic-test/src/main.rs new file mode 100644 index 0000000000..22e1be5e4f --- /dev/null +++ b/crates/intrinsic-test/src/main.rs @@ -0,0 +1,380 @@ +#[macro_use] +extern crate lazy_static; +#[macro_use] +extern crate log; + +use std::fs::File; +use std::io::Write; +use std::process::Command; + +use clap::{App, Arg}; +use intrinsic::Intrinsic; +use rayon::prelude::*; +use types::TypeKind; + +mod argument; +mod intrinsic; +mod types; +mod values; + +#[derive(Debug, PartialEq)] +pub enum Language { + Rust, + C, +} + +fn generate_c_program(header_file: &str, intrinsic: &Intrinsic) -> String { + format!( + r#"#include <{header_file}> +#include +#include +#include +#include +template T1 cast(T2 x) {{ + static_assert(sizeof(T1) == sizeof(T2), "sizeof T1 and T2 must be the same"); + T1 ret = 0; + memcpy(&ret, &x, sizeof(T1)); + return ret; +}} +std::ostream& operator<<(std::ostream& os, poly128_t value) {{ + std::stringstream temp; + do {{ + int n = value % 10; + value /= 10; + temp << n; + }} while (value != 0); + std::string tempstr(temp.str()); + std::string res(tempstr.rbegin(), tempstr.rend()); + os << res; + return os; +}} +int main(int argc, char **argv) {{ +{passes} + return 0; +}}"#, + header_file = header_file, + passes = (1..20) + .map(|idx| intrinsic.generate_pass_c(idx)) + .collect::>() + .join("\n"), + ) +} + +fn generate_rust_program(intrinsic: &Intrinsic) -> String { + format!( + r#"#![feature(simd_ffi)] +#![feature(link_llvm_intrinsics)] +#![feature(stdsimd)] +#![allow(overflowing_literals)] +use core::arch::aarch64::*; +#[allow(unused_macros)] +macro_rules! bits_to_float( + ($t:ty, $bits:expr) => ( + {{ let x: $t = ::std::mem::transmute($bits); x }} + ) +); + +fn main() {{ +{passes} +}} +"#, + passes = (1..20) + .map(|idx| intrinsic.generate_pass_rust(idx)) + .collect::>() + .join("\n"), + ) +} + +fn compile_c(c_filename: &str, intrinsic: &Intrinsic, compiler: &str) -> bool { + let output = Command::new("sh") + .arg("-c") + .arg(format!( + "{cpp} -Wno-narrowing -O2 -target {target} -o c_programs/{intrinsic} {filename}", + target = "aarch64-unknown-linux-gnu", + filename = c_filename, + intrinsic = intrinsic.name, + cpp = compiler, + )) + .output(); + if let Ok(output) = output { + if output.status.success() { + true + } else { + let stderr = std::str::from_utf8(&output.stderr).unwrap_or(""); + if stderr.contains("error: use of undeclared identifier") { + warn!("Skipping intrinsic due to no support: {}", intrinsic.name); + true + } else { + error!( + "Failed to compile code for intrinsic: {}\n\nstdout:\n{}\n\nstderr:\n{}", + intrinsic.name, + std::str::from_utf8(&output.stdout).unwrap_or(""), + std::str::from_utf8(&output.stderr).unwrap_or("") + ); + false + } + } + } else { + error!("Command failed: {:#?}", output); + false + } +} + +fn build_c(intrinsics: &Vec, compiler: &str) -> bool { + let _ = std::fs::create_dir("c_programs"); + intrinsics + .par_iter() + .map(|i| { + let c_filename = format!(r#"c_programs/{}.cpp"#, i.name); + let mut file = File::create(&c_filename).unwrap(); + + let c_code = generate_c_program("arm_neon.h", &i); + file.write_all(c_code.into_bytes().as_slice()).unwrap(); + compile_c(&c_filename, &i, compiler) + }) + .find_any(|x| !x) + .is_none() +} + +fn build_rust(intrinsics: &Vec, toolchain: &str) -> bool { + intrinsics.iter().for_each(|i| { + let rust_dir = format!(r#"rust_programs/{}"#, i.name); + let _ = std::fs::create_dir_all(&rust_dir); + let rust_filename = format!(r#"{}/main.rs"#, rust_dir); + let mut file = File::create(&rust_filename).unwrap(); + + let c_code = generate_rust_program(&i); + file.write_all(c_code.into_bytes().as_slice()).unwrap(); + }); + + let mut cargo = File::create("rust_programs/Cargo.toml").unwrap(); + cargo + .write_all( + format!( + r#"[package] +name = "intrinsic-test" +version = "{version}" +authors = ["{authors}"] +edition = "2018" +[workspace] +{binaries}"#, + version = env!("CARGO_PKG_VERSION"), + authors = env!("CARGO_PKG_AUTHORS"), + binaries = intrinsics + .iter() + .map(|i| { + format!( + r#"[[bin]] +name = "{intrinsic}" +path = "{intrinsic}/main.rs""#, + intrinsic = i.name + ) + }) + .collect::>() + .join("\n") + ) + .into_bytes() + .as_slice(), + ) + .unwrap(); + + let output = Command::new("sh") + .current_dir("rust_programs") + .arg("-c") + .arg(format!( + "cargo +{toolchain} build --release", + toolchain = toolchain, + )) + .output(); + if let Ok(output) = output { + if output.status.success() { + true + } else { + error!( + "Failed to compile code for intrinsics\n\nstdout:\n{}\n\nstderr:\n{}", + std::str::from_utf8(&output.stdout).unwrap_or(""), + std::str::from_utf8(&output.stderr).unwrap_or("") + ); + false + } + } else { + error!("Command failed: {:#?}", output); + false + } +} + +fn main() { + pretty_env_logger::init(); + + let matches = App::new("Intrinsic test tool") + .about("Generates Rust and C programs for intrinsics and compares the output") + .arg( + Arg::with_name("INPUT") + .help("The input file containing the intrinsics") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("TOOLCHAIN") + .takes_value(true) + .default_value("nightly") + .long("toolchain") + .help("The rust toolchain to use for building the rust code"), + ) + .arg( + Arg::with_name("CPPCOMPILER") + .takes_value(true) + .default_value("clang++") + .long("cppcompiler") + .help("The C++ compiler to use for compiling the c++ code"), + ) + .get_matches(); + + let filename = matches.value_of("INPUT").unwrap(); + let toolchain = matches.value_of("TOOLCHAIN").unwrap(); + let cpp_compiler = matches.value_of("CPPCOMPILER").unwrap(); + + let mut csv_reader = csv::Reader::from_reader(std::fs::File::open(filename).unwrap()); + + let mut intrinsics = csv_reader + .deserialize() + .filter_map(|x| -> Option { + debug!("Processing {:#?}", x); + match x { + Ok(a) => Some(a), + e => { + error!("{:#?}", e); + None + } + } + }) + // Only perform the test for intrinsics that are enabled... + .filter(|i| i.enabled) + // Not sure how we would compare intrinsic that returns void. + .filter(|i| i.results.kind() != TypeKind::Void) + .filter(|i| i.results.kind() != TypeKind::BFloat) + .filter(|i| !(i.results.kind() == TypeKind::Float && i.results.inner_size() == 16)) + .filter(|i| { + i.arguments + .iter() + .find(|a| a.ty.kind() == TypeKind::BFloat) + .is_none() + }) + .filter(|i| { + i.arguments + .iter() + .find(|a| a.ty.kind() == TypeKind::Float && a.ty.inner_size() == 16) + .is_none() + }) + // Skip pointers for now, we would probably need to look at the return + // type to work out how many elements we need to point to. + .filter(|i| i.arguments.iter().find(|a| a.is_ptr()).is_none()) + // intrinsics with a lane parameter have constraints, deal with them later. + .filter(|i| { + i.arguments + .iter() + .find(|a| a.name.starts_with("lane")) + .is_none() + }) + .filter(|i| i.arguments.iter().find(|a| a.name == "n").is_none()) + .collect::>(); + intrinsics.dedup(); + + if !build_c(&intrinsics, cpp_compiler) { + std::process::exit(2); + } + + if !build_rust(&intrinsics, &toolchain) { + std::process::exit(3); + } + + if !compare_outputs(&intrinsics, &toolchain) { + std::process::exit(1) + } +} + +enum FailureReason { + RunC(String), + RunRust(String), + Difference(String, String, String), +} + +fn compare_outputs(intrinsics: &Vec, toolchain: &str) -> bool { + let intrinsics = intrinsics + .par_iter() + .filter_map(|intrinsic| { + let c = Command::new("sh") + .arg("-c") + .arg(format!( + "./c_programs/{intrinsic}", + intrinsic = intrinsic.name, + )) + .output(); + let rust = Command::new("sh") + .current_dir("rust_programs") + .arg("-c") + .arg(format!( + "cargo +{toolchain} run --release --bin {intrinsic}", + intrinsic = intrinsic.name, + toolchain = toolchain, + )) + .output(); + + let (c, rust) = match (c, rust) { + (Ok(c), Ok(rust)) => (c, rust), + a => panic!("{:#?}", a), + }; + + if !c.status.success() { + error!("Failed to run C program for intrinsic {}", intrinsic.name); + return Some(FailureReason::RunC(intrinsic.name.clone())); + } + + if !rust.status.success() { + error!( + "Failed to run rust program for intrinsic {}", + intrinsic.name + ); + return Some(FailureReason::RunRust(intrinsic.name.clone())); + } + + info!("Comparing intrinsic: {}", intrinsic.name); + + let c = std::str::from_utf8(&c.stdout) + .unwrap() + .to_lowercase() + .replace("-nan", "nan"); + let rust = std::str::from_utf8(&rust.stdout) + .unwrap() + .to_lowercase() + .replace("-nan", "nan"); + + if c == rust { + None + } else { + Some(FailureReason::Difference(intrinsic.name.clone(), c, rust)) + } + }) + .collect::>(); + + intrinsics.iter().for_each(|reason| match reason { + FailureReason::Difference(intrinsic, c, rust) => { + println!("Difference for intrinsic: {}", intrinsic); + let diff = diff::lines(c, rust); + diff.iter().for_each(|diff| match diff { + diff::Result::Left(c) => println!("C: {}", c), + diff::Result::Right(rust) => println!("Rust: {}", rust), + diff::Result::Both(_, _) => (), + }); + println!("****************************************************************"); + } + FailureReason::RunC(intrinsic) => { + println!("Failed to run C program for intrinsic {}", intrinsic) + } + FailureReason::RunRust(intrinsic) => { + println!("Failed to run rust program for intrinsic {}", intrinsic) + } + }); + println!("{} differences found", intrinsics.len()); + intrinsics.is_empty() +} diff --git a/crates/intrinsic-test/src/types.rs b/crates/intrinsic-test/src/types.rs new file mode 100644 index 0000000000..c037ed3ae5 --- /dev/null +++ b/crates/intrinsic-test/src/types.rs @@ -0,0 +1,483 @@ +use regex::Regex; +use serde::{Deserialize, Deserializer}; +use std::fmt; +use std::str::FromStr; + +use crate::values::values_for_pass; +use crate::Language; + +#[derive(Debug, PartialEq, Copy, Clone)] +pub enum TypeKind { + BFloat, + Float, + Int, + UInt, + Poly, + Void, +} + +impl FromStr for TypeKind { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "bfloat" => Ok(Self::BFloat), + "float" => Ok(Self::Float), + "int" => Ok(Self::Int), + "poly" => Ok(Self::Poly), + "uint" | "unsigned" => Ok(Self::UInt), + "void" => Ok(Self::Void), + _ => Err(format!("Impossible to parse argument kind {}", s)), + } + } +} + +impl fmt::Display for TypeKind { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + Self::BFloat => "bfloat", + Self::Float => "float", + Self::Int => "int", + Self::UInt => "uint", + Self::Poly => "poly", + Self::Void => "void", + } + ) + } +} + +impl TypeKind { + /// Gets the type part of a c typedef for a type that's in the form of {type}{size}_t. + pub fn c_prefix(&self) -> &str { + match self { + Self::Float => "float", + Self::Int => "int", + Self::UInt => "uint", + Self::Poly => "poly", + _ => unreachable!("Not used: {:#?}", self), + } + } + + /// Gets the rust prefix for the type kind i.e. i, u, f. + pub fn rust_prefix(&self) -> &str { + match self { + Self::Float => "f", + Self::Int => "i", + Self::UInt => "u", + Self::Poly => "u", + _ => unreachable!("Unused type kind: {:#?}", self), + } + } +} + +#[derive(Debug, PartialEq, Clone)] +pub enum IntrinsicType { + Ptr { + constant: bool, + child: Box, + }, + Type { + constant: bool, + kind: TypeKind, + /// The bit length of this type (e.g. 32 for u32). + bit_len: Option, + + /// Length of the SIMD vector (i.e. 4 for uint32x4_t), A value of `None` + /// means this is not a simd type. A `None` can be assumed to be 1, + /// although in some places a distinction is needed between `u64` and + /// `uint64x1_t` this signals that. + simd_len: Option, + + /// The number of rows for SIMD matrices (i.e. 2 for uint8x8x2_t). + /// A value of `None` represents a type that does not contain any + /// rows encoded in the type (e.g. uint8x8_t). + /// A value of `None` can be assumed to be 1 though. + vec_len: Option, + }, +} + +impl IntrinsicType { + /// Get the TypeKind for this type, recursing into pointers. + pub fn kind(&self) -> TypeKind { + match *self { + IntrinsicType::Ptr { ref child, .. } => child.kind(), + IntrinsicType::Type { kind, .. } => kind, + } + } + + /// Get the size of a single element inside this type, recursing into + /// pointers, i.e. a pointer to a u16 would be 16 rather than the size + /// of a pointer. + pub fn inner_size(&self) -> u32 { + match *self { + IntrinsicType::Ptr { ref child, .. } => child.inner_size(), + IntrinsicType::Type { + bit_len: Some(bl), .. + } => bl, + _ => unreachable!(""), + } + } + + pub fn num_lanes(&self) -> u32 { + match *self { + IntrinsicType::Ptr { ref child, .. } => child.num_lanes(), + IntrinsicType::Type { + simd_len: Some(sl), .. + } => sl, + _ => 1, + } + } + + /// Determine if the type is a simd type, this will treat a type such as + /// `uint64x1` as simd. + pub fn is_simd(&self) -> bool { + match *self { + IntrinsicType::Ptr { ref child, .. } => child.is_simd(), + IntrinsicType::Type { + simd_len: None, + vec_len: None, + .. + } => false, + _ => true, + } + } + + pub fn is_ptr(&self) -> bool { + match *self { + IntrinsicType::Ptr { .. } => true, + IntrinsicType::Type { .. } => false, + } + } + + pub fn from_rust(ty: &str) -> Result { + lazy_static! { + static ref SIMD_TYPE: Regex = Regex::new(r#"([a-z]*)([0-9]*)x([0-9]*)_t"#).unwrap(); + static ref MULTI_SIMD_TYPE: Regex = + Regex::new(r#"([a-z]*)([0-9]*)x([0-9]*)x([0-9]*)_t"#).unwrap(); + static ref RUST_TYPE: Regex = Regex::new(r#"([iuf]|float|poly)([0-9]+)"#).unwrap(); + } + + debug!("Parsing type: {}", ty); + + if let Some(ty) = ty.strip_prefix('*') { + let (constant, ty) = if let Some(ty) = ty.strip_prefix("const") { + (true, ty.trim()) + } else if let Some(ty) = ty.strip_prefix("mut") { + (false, ty.trim()) + } else { + (false, ty) + }; + return Ok(Self::Ptr { + constant, + child: Box::new(Self::from_rust(ty)?), + }); + } + + let (constant, ty) = if let Some(ty) = ty.strip_prefix("const") { + (true, ty.trim()) + } else { + (false, ty) + }; + + if let Some(captures) = MULTI_SIMD_TYPE.captures(ty) { + let kind = captures + .get(1) + .map(|s| s.as_str().parse::().unwrap()) + .unwrap(); + let bit_len = captures.get(2).map(|s| s.as_str().parse::().unwrap()); + let simd_len = captures.get(3).map(|s| s.as_str().parse::().unwrap()); + let vec_len = captures.get(4).map(|s| s.as_str().parse::().unwrap()); + Ok(Self::Type { + constant, + kind, + bit_len, + simd_len, + vec_len, + }) + } else if let Some(captures) = SIMD_TYPE.captures(ty) { + let kind = captures + .get(1) + .map(|s| s.as_str().parse::().unwrap()) + .unwrap(); + let bit_len = captures.get(2).map(|s| s.as_str().parse::().unwrap()); + let simd_len = captures.get(3).map(|s| s.as_str().parse::().unwrap()); + + Ok(Self::Type { + constant, + kind, + bit_len, + simd_len, + vec_len: None, + }) + } else if let Some(captures) = RUST_TYPE.captures(ty) { + let kind = captures + .get(1) + .map(|s| match s.as_str() { + "i" => TypeKind::Int, + "u" => TypeKind::UInt, + "f" => TypeKind::Float, + "float" => TypeKind::Float, + "poly" => TypeKind::Poly, + a => panic!("Unexpected type: {} found", a), + }) + .unwrap(); + let bit_len = captures.get(2).map(|s| s.as_str().parse::().unwrap()); + Ok(Self::Type { + constant, + kind, + bit_len, + simd_len: None, + vec_len: None, + }) + } else { + match ty { + "int" => Ok(Self::Type { + constant, + kind: TypeKind::Int, + bit_len: Some(32), + simd_len: None, + vec_len: None, + }), + "void" => Ok(Self::Type { + constant: false, + kind: TypeKind::Void, + bit_len: None, + simd_len: None, + vec_len: None, + }), + _ => Err(format!("Failed to parse type: {}", ty)), + } + } + } + + #[allow(unused)] + fn c_scalar_type(&self) -> String { + format!( + "{prefix}{bits}_t", + prefix = self.kind().c_prefix(), + bits = self.inner_size() + ) + } + + fn rust_scalar_type(&self) -> String { + format!( + "{prefix}{bits}", + prefix = self.kind().rust_prefix(), + bits = self.inner_size() + ) + } + + /// Gets a string containing the typename for this type in C format. + pub fn c_type(&self) -> String { + match self { + IntrinsicType::Ptr { child, .. } => child.c_type(), + IntrinsicType::Type { + constant, + kind, + bit_len: Some(bit_len), + simd_len: None, + vec_len: None, + .. + } => format!( + "{}{}{}_t", + if *constant { "const " } else { "" }, + kind.c_prefix(), + bit_len + ), + IntrinsicType::Type { + kind, + bit_len: Some(bit_len), + simd_len: Some(simd_len), + vec_len: None, + .. + } => format!("{}{}x{}_t", kind.c_prefix(), bit_len, simd_len), + IntrinsicType::Type { + kind, + bit_len: Some(bit_len), + simd_len: Some(simd_len), + vec_len: Some(vec_len), + .. + } => format!("{}{}x{}x{}_t", kind.c_prefix(), bit_len, simd_len, vec_len), + _ => todo!("{:#?}", self), + } + } + + /// Gets a cast for this type if needs promotion. + /// This is required for 8 bit types due to printing as the 8 bit types use + /// a char and when using that in `std::cout` it will print as a character, + /// which means value of 0 will be printed as a null byte. + pub fn c_promotion(&self) -> &str { + match *self { + IntrinsicType::Type { + kind, + bit_len: Some(bit_len), + .. + } if bit_len == 8 => match kind { + TypeKind::Int => "(int)", + TypeKind::UInt => "(unsigned int)", + TypeKind::Poly => "(unsigned int)", + _ => "", + }, + _ => "", + } + } + + /// Generates a comma list of values that can be used to initialize an + /// argument for the intrinsic call. + /// This is determistic based on the pass number. + /// + /// * `pass`: The pass index, i.e. the iteration index for the call to an intrinsic + /// + /// Returns a string such as + /// * `0x1, 0x7F, 0xFF` if `language` is `Language::C` + /// * `0x1 as _, 0x7F as _, 0xFF as _` if `language` is `Language::Rust` + pub fn populate_random(&self, pass: usize, language: &Language) -> String { + match self { + IntrinsicType::Ptr { child, .. } => child.populate_random(pass, language), + IntrinsicType::Type { + bit_len: Some(bit_len), + kind, + simd_len, + vec_len, + .. + } if kind == &TypeKind::Int || kind == &TypeKind::UInt || kind == &TypeKind::Poly => (0 + ..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1))) + .map(|i| { + format!( + "{}{}", + values_for_pass(*bit_len, i, pass), + match language { + &Language::Rust => format!(" as {ty} ", ty = self.rust_scalar_type()), + &Language::C => String::from(""), + } + ) + }) + .collect::>() + .join(","), + IntrinsicType::Type { + kind: TypeKind::Float, + bit_len: Some(32), + simd_len, + vec_len, + .. + } => (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1))) + .map(|i| { + format!( + "{}{})", + match language { + &Language::Rust => "bits_to_float!(f32, ", + &Language::C => "cast(", + }, + values_for_pass(32, i, pass), + ) + }) + .collect::>() + .join(","), + IntrinsicType::Type { + kind: TypeKind::Float, + bit_len: Some(64), + simd_len, + vec_len, + .. + } => (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1))) + .map(|i| { + format!( + "{}{}{})", + match language { + &Language::Rust => "bits_to_float!(f64,", + &Language::C => "cast(", + }, + values_for_pass(64, i, pass), + match language { + &Language::Rust => " as u64", + &Language::C => "", + } + ) + }) + .collect::>() + .join(","), + _ => unreachable!("populate random: {:#?}", self), + } + } + + /// Determines the load function for this type. + #[allow(unused)] + pub fn get_load_function(&self) -> String { + match self { + IntrinsicType::Ptr { child, .. } => child.get_load_function(), + IntrinsicType::Type { + kind: k, + bit_len: Some(bl), + simd_len, + vec_len, + .. + } => { + let quad = if (simd_len.unwrap_or(1) * bl) > 64 { + "q" + } else { + "" + }; + format!( + "vld{len}{quad}_{type}{size}", + type = match k { + TypeKind::UInt => "u", + TypeKind::Int => "s", + TypeKind::Float => "f", + TypeKind::Poly => "p", + x => todo!("get_load_function TypeKind: {:#?}", x), + }, + size = bl, + quad = quad, + len = vec_len.unwrap_or(1), + ) + } + _ => todo!("get_load_function IntrinsicType: {:#?}", self), + } + } + + /// Determines the get lane function for this type. + pub fn get_lane_function(&self) -> String { + match self { + IntrinsicType::Ptr { child, .. } => child.get_lane_function(), + IntrinsicType::Type { + kind: k, + bit_len: Some(bl), + simd_len, + .. + } => { + let quad = if (simd_len.unwrap_or(1) * bl) > 64 { + "q" + } else { + "" + }; + format!( + "vget{quad}_lane_{type}{size}", + type = match k { + TypeKind::UInt => "u", + TypeKind::Int => "s", + TypeKind::Float => "f", + TypeKind::Poly => "p", + x => todo!("get_load_function TypeKind: {:#?}", x), + }, + size = bl, + quad = quad, + ) + } + _ => todo!("get_lane_function IntrinsicType: {:#?}", self), + } + } +} + +impl<'de> Deserialize<'de> for IntrinsicType { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + use serde::de::Error; + let s = String::deserialize(deserializer)?; + Self::from_rust(&s).map_err(Error::custom) + } +} diff --git a/crates/intrinsic-test/src/values.rs b/crates/intrinsic-test/src/values.rs new file mode 100644 index 0000000000..4565edca09 --- /dev/null +++ b/crates/intrinsic-test/src/values.rs @@ -0,0 +1,126 @@ +/// Gets a hex constant value for a single lane in in a determistic way +/// * `bits`: The number of bits for the type, only 8, 16, 32, 64 are valid values +/// * `simd`: The index of the simd lane we are generating for +/// * `pass`: The index of the pass we are generating the values for +pub fn values_for_pass(bits: u32, simd: u32, pass: usize) -> String { + let index = pass + (simd as usize); + + if bits == 8 { + format!("{:#X}", VALUES_8[index % VALUES_8.len()]) + } else if bits == 16 { + format!("{:#X}", VALUES_16[index % VALUES_16.len()]) + } else if bits == 32 { + format!("{:#X}", VALUES_32[index % VALUES_32.len()]) + } else if bits == 64 { + format!("{:#X}", VALUES_64[index % VALUES_64.len()]) + } else { + panic!("Unknown size: {}", bits); + } +} + +pub const VALUES_8: &[u8] = &[ + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, + 0xf0, 0x80, 0x3b, 0xff, +]; + +pub const VALUES_16: &[u16] = &[ + 0x0000, // 0.0 + 0x0400, // The smallest normal value. + 0x37ff, // The value just below 0.5. + 0x3800, // 0.5 + 0x3801, // The value just above 0.5. + 0x3bff, // The value just below 1.0. + 0x3c00, // 1.0 + 0x3c01, // The value just above 1.0. + 0x3e00, // 1.5 + 0x4900, // 10 + 0x7bff, // The largest finite value. + 0x7c00, // Infinity. + // NaNs. + // - Quiet NaNs + 0x7f23, 0x7e00, // - Signalling NaNs + 0x7d23, 0x7c01, // Subnormals. + // - A recognisable bit pattern. + 0x0012, // - The largest subnormal value. + 0x03ff, // - The smallest subnormal value. + 0x0001, // The same values again, but negated. + 0x8000, 0x8400, 0xb7ff, 0xb800, 0xb801, 0xbbff, 0xbc00, 0xbc01, 0xbe00, 0xc900, 0xfbff, 0xfc00, + 0xff23, 0xfe00, 0xfd23, 0xfc01, 0x8012, 0x83ff, 0x8001, +]; + +pub const VALUES_32: &[u32] = &[ + // Simple values. + 0x00000000, // 0.0 + 0x00800000, // The smallest normal value. + 0x3effffff, // The value just below 0.5. + 0x3f000000, // 0.5 + 0x3f000001, // The value just above 0.5. + 0x3f7fffff, // The value just below 1.0. + 0x3f800000, // 1.0 + 0x3f800001, // The value just above 1.0. + 0x3fc00000, // 1.5 + 0x41200000, // 10 + 0x7f8fffff, // The largest finite value. + 0x7f800000, // Infinity. + // NaNs. + // - Quiet NaNs + 0x7fd23456, 0x7fc00000, // - Signalling NaNs + 0x7f923456, 0x7f800001, // Subnormals. + // - A recognisable bit pattern. + 0x00123456, // - The largest subnormal value. + 0x007fffff, // - The smallest subnormal value. + 0x00000001, // The same values again, but negated. + 0x80000000, 0x80800000, 0xbeffffff, 0xbf000000, 0xbf000001, 0xbf7fffff, 0xbf800000, 0xbf800001, + 0xbfc00000, 0xc1200000, 0xff8fffff, 0xff800000, 0xffd23456, 0xffc00000, 0xff923456, 0xff800001, + 0x80123456, 0x807fffff, 0x80000001, +]; + +pub const VALUES_64: &[u64] = &[ + // Simple values. + 0x0000000000000000, // 0.0 + 0x0010000000000000, // The smallest normal value. + 0x3fdfffffffffffff, // The value just below 0.5. + 0x3fe0000000000000, // 0.5 + 0x3fe0000000000001, // The value just above 0.5. + 0x3fefffffffffffff, // The value just below 1.0. + 0x3ff0000000000000, // 1.0 + 0x3ff0000000000001, // The value just above 1.0. + 0x3ff8000000000000, // 1.5 + 0x4024000000000000, // 10 + 0x7fefffffffffffff, // The largest finite value. + 0x7ff0000000000000, // Infinity. + // NaNs. + // - Quiet NaNs + 0x7ff923456789abcd, + 0x7ff8000000000000, + // - Signalling NaNs + 0x7ff123456789abcd, + 0x7ff0000000000000, + // Subnormals. + // - A recognisable bit pattern. + 0x000123456789abcd, + // - The largest subnormal value. + 0x000fffffffffffff, + // - The smallest subnormal value. + 0x0000000000000001, + // The same values again, but negated. + 0x8000000000000000, + 0x8010000000000000, + 0xbfdfffffffffffff, + 0xbfe0000000000000, + 0xbfe0000000000001, + 0xbfefffffffffffff, + 0xbff0000000000000, + 0xbff0000000000001, + 0xbff8000000000000, + 0xc024000000000000, + 0xffefffffffffffff, + 0xfff0000000000000, + 0xfff923456789abcd, + 0xfff8000000000000, + 0xfff123456789abcd, + 0xfff0000000000000, + 0x800123456789abcd, + 0x800fffffffffffff, + 0x8000000000000001, +];