diff options
| author | bors <bors@rust-lang.org> | 2025-07-22 15:25:31 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2025-07-22 15:25:31 +0000 |
| commit | 2e5367566819ca7878baa9600ae7a93eb0e37bbf (patch) | |
| tree | 6b65029625eb4b6e726b826197e617728788430e /library/stdarch/crates/intrinsic-test/src/common | |
| parent | 35487a2e7c80012129c38f55c970109a1538c91f (diff) | |
| parent | 9b7d31c851cabc2e6e541d3cf146787d597a9166 (diff) | |
| download | rust-2e5367566819ca7878baa9600ae7a93eb0e37bbf.tar.gz rust-2e5367566819ca7878baa9600ae7a93eb0e37bbf.zip | |
Auto merge of #144222 - Kobzol:stdarch-push, r=folkertdev
stdarch subtree update Subtree update of `stdarch` to https://github.com/rust-lang/stdarch/commit/5531955678494ee28ec02130a6d94082ad4532da. Created using https://github.com/rust-lang/josh-sync. I saw that there were non-trivial changes made to `std_detect` in `stdarch` recently. So I want to get them merged here before we move forward with https://github.com/rust-lang/rust/pull/143412. r? `@folkertdev`
Diffstat (limited to 'library/stdarch/crates/intrinsic-test/src/common')
8 files changed, 372 insertions, 380 deletions
diff --git a/library/stdarch/crates/intrinsic-test/src/common/argument.rs b/library/stdarch/crates/intrinsic-test/src/common/argument.rs index 443ccb919f4..1df4f55995e 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/argument.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/argument.rs @@ -76,7 +76,7 @@ where Argument { pos, name: String::from(var_name), - ty: *ty, + ty: ty, constraint, } } @@ -125,19 +125,23 @@ where /// Creates a line for each argument that initializes an array for C from which `loads` argument /// values can be loaded as a sliding window. /// e.g `const int32x2_t a_vals = {0x3effffff, 0x3effffff, 0x3f7fffff}`, if loads=2. - pub fn gen_arglists_c(&self, indentation: Indentation, loads: u32) -> String { - self.iter() - .filter(|&arg| !arg.has_constraint()) - .map(|arg| { - format!( - "{indentation}const {ty} {name}_vals[] = {values};", - ty = arg.ty.c_scalar_type(), - name = arg.name, - values = arg.ty.populate_random(indentation, loads, &Language::C) - ) - }) - .collect::<Vec<_>>() - .join("\n") + pub fn gen_arglists_c( + &self, + w: &mut impl std::io::Write, + indentation: Indentation, + loads: u32, + ) -> std::io::Result<()> { + for arg in self.iter().filter(|&arg| !arg.has_constraint()) { + writeln!( + w, + "{indentation}const {ty} {name}_vals[] = {values};", + ty = arg.ty.c_scalar_type(), + name = arg.name, + values = arg.ty.populate_random(indentation, loads, &Language::C) + )? + } + + Ok(()) } /// Creates a line for each argument that initializes an array for Rust from which `loads` argument diff --git a/library/stdarch/crates/intrinsic-test/src/common/cli.rs b/library/stdarch/crates/intrinsic-test/src/common/cli.rs index 1d572723008..beae6a4b044 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/cli.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/cli.rs @@ -60,7 +60,7 @@ pub struct ProcessedCli { pub filename: PathBuf, pub toolchain: Option<String>, pub cpp_compiler: Option<String>, - pub c_runner: String, + pub runner: String, pub target: String, pub linker: Option<String>, pub cxx_toolchain_dir: Option<String>, @@ -70,7 +70,7 @@ pub struct ProcessedCli { impl ProcessedCli { pub fn new(cli_options: Cli) -> Self { let filename = cli_options.input; - let c_runner = cli_options.runner.unwrap_or_default(); + let runner = cli_options.runner.unwrap_or_default(); let target = cli_options.target; let linker = cli_options.linker; let cxx_toolchain_dir = cli_options.cxx_toolchain_dir; @@ -102,7 +102,7 @@ impl ProcessedCli { Self { toolchain, cpp_compiler, - c_runner, + runner, target, linker, cxx_toolchain_dir, diff --git a/library/stdarch/crates/intrinsic-test/src/common/compare.rs b/library/stdarch/crates/intrinsic-test/src/common/compare.rs index 9e0cbe8cd6a..cb55922eb19 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/compare.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/compare.rs @@ -2,27 +2,25 @@ use super::cli::FailureReason; use rayon::prelude::*; use std::process::Command; -pub fn compare_outputs( - intrinsic_name_list: &Vec<String>, - toolchain: &str, - runner: &str, - target: &str, -) -> bool { +pub fn compare_outputs(intrinsic_name_list: &Vec<String>, runner: &str, target: &str) -> bool { + fn runner_command(runner: &str) -> Command { + let mut it = runner.split_whitespace(); + let mut cmd = Command::new(it.next().unwrap()); + cmd.args(it); + + cmd + } + let intrinsics = intrinsic_name_list .par_iter() .filter_map(|intrinsic_name| { - let c = Command::new("sh") - .arg("-c") - .arg(format!("{runner} ./c_programs/{intrinsic_name}")) + let c = runner_command(runner) + .arg("./c_programs/intrinsic-test-programs") + .arg(intrinsic_name) .output(); - let rust = Command::new("sh") - .current_dir("rust_programs") - .arg("-c") - .arg(format!( - "cargo {toolchain} run --target {target} --bin {intrinsic_name} --release", - )) - .env("RUSTFLAGS", "-Cdebuginfo=0") + let rust = runner_command(runner) + .arg(format!("target/{target}/release/{intrinsic_name}")) .output(); let (c, rust) = match (c, rust) { @@ -42,8 +40,8 @@ pub fn compare_outputs( if !rust.status.success() { error!( "Failed to run Rust program for intrinsic {intrinsic_name}\nstdout: {stdout}\nstderr: {stderr}", - stdout = std::str::from_utf8(&rust.stdout).unwrap_or(""), - stderr = std::str::from_utf8(&rust.stderr).unwrap_or(""), + stdout = String::from_utf8_lossy(&rust.stdout), + stderr = String::from_utf8_lossy(&rust.stderr), ); return Some(FailureReason::RunRust(intrinsic_name.clone())); } diff --git a/library/stdarch/crates/intrinsic-test/src/common/compile_c.rs b/library/stdarch/crates/intrinsic-test/src/common/compile_c.rs index aebb7b111e2..0c905a149e4 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/compile_c.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/compile_c.rs @@ -5,11 +5,7 @@ pub struct CompilationCommandBuilder { cxx_toolchain_dir: Option<String>, arch_flags: Vec<String>, optimization: String, - include_paths: Vec<String>, project_root: Option<String>, - output: String, - input: String, - linker: Option<String>, extra_flags: Vec<String>, } @@ -21,11 +17,7 @@ impl CompilationCommandBuilder { cxx_toolchain_dir: None, arch_flags: Vec::new(), optimization: "2".to_string(), - include_paths: Vec::new(), project_root: None, - output: String::new(), - input: String::new(), - linker: None, extra_flags: Vec::new(), } } @@ -57,37 +49,12 @@ impl CompilationCommandBuilder { self } - /// Sets a list of include paths for compilation. - /// The paths that are passed must be relative to the - /// "cxx_toolchain_dir" directory path. - pub fn set_include_paths(mut self, paths: Vec<&str>) -> Self { - self.include_paths = paths.into_iter().map(|path| path.to_string()).collect(); - self - } - /// Sets the root path of all the generated test files. pub fn set_project_root(mut self, path: &str) -> Self { self.project_root = Some(path.to_string()); self } - /// The name of the output executable, without any suffixes - pub fn set_output_name(mut self, path: &str) -> Self { - self.output = path.to_string(); - self - } - - /// The name of the input C file, without any suffixes - pub fn set_input_name(mut self, path: &str) -> Self { - self.input = path.to_string(); - self - } - - pub fn set_linker(mut self, linker: String) -> Self { - self.linker = Some(linker); - self - } - pub fn add_extra_flags(mut self, flags: Vec<&str>) -> Self { let mut flags: Vec<String> = flags.into_iter().map(|f| f.to_string()).collect(); self.extra_flags.append(&mut flags); @@ -100,55 +67,69 @@ impl CompilationCommandBuilder { } impl CompilationCommandBuilder { - pub fn make_string(self) -> String { - let arch_flags = self.arch_flags.join("+"); + pub fn into_cpp_compilation(self) -> CppCompilation { + let mut cpp_compiler = std::process::Command::new(self.compiler); + + if let Some(project_root) = self.project_root { + cpp_compiler.current_dir(project_root); + } + let flags = std::env::var("CPPFLAGS").unwrap_or("".into()); - let project_root = self.project_root.unwrap_or_default(); - let project_root_str = project_root.as_str(); - let mut output = self.output.clone(); - if self.linker.is_some() { - output += ".o" - }; - let mut command = format!( - "{} {flags} -march={arch_flags} \ - -O{} \ - -o {project_root}/{} \ - {project_root}/{}.cpp", - self.compiler, self.optimization, output, self.input, - ); - - command = command + " " + self.extra_flags.join(" ").as_str(); + cpp_compiler.args(flags.split_whitespace()); + + cpp_compiler.arg(format!("-march={}", self.arch_flags.join("+"))); + + cpp_compiler.arg(format!("-O{}", self.optimization)); + + cpp_compiler.args(self.extra_flags); if let Some(target) = &self.target { - command = command + " --target=" + target; + cpp_compiler.arg(format!("--target={target}")); } - if let (Some(linker), Some(cxx_toolchain_dir)) = (&self.linker, &self.cxx_toolchain_dir) { - let include_args = self - .include_paths - .iter() - .map(|path| "--include-directory=".to_string() + cxx_toolchain_dir + path) - .collect::<Vec<_>>() - .join(" "); - - command = command - + " -c " - + include_args.as_str() - + " && " - + linker - + " " - + project_root_str - + "/" - + &output - + " -o " - + project_root_str - + "/" - + &self.output - + " && rm " - + project_root_str - + "/" - + &output; - } - command + CppCompilation(cpp_compiler) + } +} + +pub struct CppCompilation(std::process::Command); + +fn clone_command(command: &std::process::Command) -> std::process::Command { + let mut cmd = std::process::Command::new(command.get_program()); + if let Some(current_dir) = command.get_current_dir() { + cmd.current_dir(current_dir); + } + cmd.args(command.get_args()); + + for (key, val) in command.get_envs() { + cmd.env(key, val.unwrap_or_default()); + } + + cmd +} + +impl CppCompilation { + pub fn command_mut(&mut self) -> &mut std::process::Command { + &mut self.0 + } + + pub fn compile_object_file( + &self, + input: &str, + output: &str, + ) -> std::io::Result<std::process::Output> { + let mut cmd = clone_command(&self.0); + cmd.args([input, "-c", "-o", output]); + cmd.output() + } + + pub fn link_executable( + &self, + inputs: impl Iterator<Item = String>, + output: &str, + ) -> std::io::Result<std::process::Output> { + let mut cmd = clone_command(&self.0); + cmd.args(inputs); + cmd.args(["-o", output]); + cmd.output() } } diff --git a/library/stdarch/crates/intrinsic-test/src/common/gen_c.rs b/library/stdarch/crates/intrinsic-test/src/common/gen_c.rs index 1cfb66c39b9..905efb6d890 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/gen_c.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/gen_c.rs @@ -1,8 +1,3 @@ -use itertools::Itertools; -use rayon::prelude::*; -use std::collections::BTreeMap; -use std::process::Command; - use super::argument::Argument; use super::indentation::Indentation; use super::intrinsic::IntrinsicDefinition; @@ -11,104 +6,16 @@ use super::intrinsic_helpers::IntrinsicTypeDefinition; // The number of times each intrinsic will be called. const PASSES: u32 = 20; -// Formats the main C program template with placeholders -pub fn format_c_main_template( - notices: &str, - header_files: &[&str], - arch_identifier: &str, - arch_specific_definitions: &[&str], - arglists: &str, - passes: &str, -) -> String { - format!( - r#"{notices}{header_files} -#include <iostream> -#include <cstring> -#include <iomanip> -#include <sstream> - -template<typename T1, typename T2> T1 cast(T2 x) {{ - static_assert(sizeof(T1) == sizeof(T2), "sizeof T1 and T2 must be the same"); - T1 ret{{}}; - memcpy(&ret, &x, sizeof(T1)); - return ret; -}} - -std::ostream& operator<<(std::ostream& os, float16_t value) {{ - uint16_t temp = 0; - memcpy(&temp, &value, sizeof(float16_t)); - std::stringstream ss; - ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << temp; - os << ss.str(); - return os; -}} - -#ifdef __{arch_identifier}__ -{arch_specific_definitions} -#endif - -{arglists} - -int main(int argc, char **argv) {{ -{passes} - return 0; -}}"#, - header_files = header_files - .iter() - .map(|header| format!("#include <{header}>")) - .collect::<Vec<_>>() - .join("\n"), - arch_specific_definitions = arch_specific_definitions.join("\n"), - ) -} - -pub fn compile_c_programs(compiler_commands: &[String]) -> bool { - compiler_commands - .par_iter() - .map(|compiler_command| { - let output = Command::new("sh").arg("-c").arg(compiler_command).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 - } - }) - .find_any(|x| !x) - .is_none() -} - -// Creates directory structure and file path mappings -pub fn setup_c_file_paths(identifiers: &Vec<String>) -> BTreeMap<&String, String> { - let _ = std::fs::create_dir("c_programs"); - identifiers - .par_iter() - .map(|identifier| { - let c_filename = format!(r#"c_programs/{identifier}.cpp"#); - - (identifier, c_filename) - }) - .collect::<BTreeMap<&String, String>>() -} - pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>( + w: &mut impl std::io::Write, intrinsic: &dyn IntrinsicDefinition<T>, indentation: Indentation, additional: &str, passes: u32, - _target: &str, -) -> String { +) -> std::io::Result<()> { let body_indentation = indentation.nested(); - format!( + writeln!( + w, "{indentation}for (int i=0; i<{passes}; i++) {{\n\ {loaded_args}\ {body_indentation}auto __return_value = {intrinsic_call}({args});\n\ @@ -121,78 +28,172 @@ pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>( ) } -pub fn generate_c_constraint_blocks<T: IntrinsicTypeDefinition>( +pub fn generate_c_constraint_blocks<'a, T: IntrinsicTypeDefinition + 'a>( + w: &mut impl std::io::Write, intrinsic: &dyn IntrinsicDefinition<T>, indentation: Indentation, - constraints: &[&Argument<T>], + constraints: &mut (impl Iterator<Item = &'a Argument<T>> + Clone), name: String, - target: &str, -) -> String { - if let Some((current, constraints)) = constraints.split_last() { - let range = current - .constraint - .iter() - .map(|c| c.to_range()) - .flat_map(|r| r.into_iter()); - - let body_indentation = indentation.nested(); - range - .map(|i| { - format!( - "{indentation}{{\n\ - {body_indentation}{ty} {name} = {val};\n\ - {pass}\n\ - {indentation}}}", - name = current.name, - ty = current.ty.c_type(), - val = i, - pass = generate_c_constraint_blocks( - intrinsic, - body_indentation, - constraints, - format!("{name}-{i}"), - target, - ) - ) - }) - .join("\n") - } else { - generate_c_test_loop(intrinsic, indentation, &name, PASSES, target) +) -> std::io::Result<()> { + let Some(current) = constraints.next() else { + return generate_c_test_loop(w, intrinsic, indentation, &name, PASSES); + }; + + let body_indentation = indentation.nested(); + for i in current.constraint.iter().flat_map(|c| c.to_range()) { + let ty = current.ty.c_type(); + + writeln!(w, "{indentation}{{")?; + writeln!(w, "{body_indentation}{ty} {} = {i};", current.name)?; + + generate_c_constraint_blocks( + w, + intrinsic, + body_indentation, + &mut constraints.clone(), + format!("{name}-{i}"), + )?; + + writeln!(w, "{indentation}}}")?; } + + Ok(()) } // Compiles C test programs using specified compiler -pub fn create_c_test_program<T: IntrinsicTypeDefinition>( +pub fn create_c_test_function<T: IntrinsicTypeDefinition>( + w: &mut impl std::io::Write, intrinsic: &dyn IntrinsicDefinition<T>, - header_files: &[&str], - target: &str, - c_target: &str, - notices: &str, - arch_specific_definitions: &[&str], -) -> String { +) -> std::io::Result<()> { + let indentation = Indentation::default(); + + writeln!(w, "int run_{}() {{", intrinsic.name())?; + + // Define the arrays of arguments. let arguments = intrinsic.arguments(); - let constraints = arguments - .iter() - .filter(|&i| i.has_constraint()) - .collect_vec(); + arguments.gen_arglists_c(w, indentation.nested(), PASSES)?; - let indentation = Indentation::default(); - format_c_main_template( - notices, - header_files, - c_target, - arch_specific_definitions, - intrinsic - .arguments() - .gen_arglists_c(indentation, PASSES) - .as_str(), - generate_c_constraint_blocks( - intrinsic, - indentation.nested(), - constraints.as_slice(), - Default::default(), - target, - ) - .as_str(), - ) + generate_c_constraint_blocks( + w, + intrinsic, + indentation.nested(), + &mut arguments.iter().rev().filter(|&i| i.has_constraint()), + Default::default(), + )?; + + writeln!(w, " return 0;")?; + writeln!(w, "}}")?; + + Ok(()) +} + +pub fn write_mod_cpp<T: IntrinsicTypeDefinition>( + w: &mut impl std::io::Write, + notice: &str, + architecture: &str, + platform_headers: &[&str], + intrinsics: &[impl IntrinsicDefinition<T>], +) -> std::io::Result<()> { + write!(w, "{notice}")?; + + for header in platform_headers { + writeln!(w, "#include <{header}>")?; + } + + writeln!( + w, + r#" +#include <iostream> +#include <cstring> +#include <iomanip> +#include <sstream> + +template<typename T1, typename T2> T1 cast(T2 x) {{ + static_assert(sizeof(T1) == sizeof(T2), "sizeof T1 and T2 must be the same"); + T1 ret{{}}; + memcpy(&ret, &x, sizeof(T1)); + return ret; +}} + +std::ostream& operator<<(std::ostream& os, float16_t value); + + + +"# + )?; + + writeln!(w, "#ifdef __{architecture}__")?; + writeln!( + w, + "std::ostream& operator<<(std::ostream& os, poly128_t value);" + )?; + writeln!(w, "#endif")?; + + for intrinsic in intrinsics { + create_c_test_function(w, intrinsic)?; + } + + Ok(()) +} + +pub fn write_main_cpp<'a>( + w: &mut impl std::io::Write, + architecture: &str, + arch_specific_definitions: &str, + intrinsics: impl Iterator<Item = &'a str> + Clone, +) -> std::io::Result<()> { + writeln!(w, "#include <iostream>")?; + writeln!(w, "#include <string>")?; + + for header in ["arm_neon.h", "arm_acle.h", "arm_fp16.h"] { + writeln!(w, "#include <{header}>")?; + } + + writeln!( + w, + r#" +#include <cstring> +#include <iomanip> +#include <sstream> + +std::ostream& operator<<(std::ostream& os, float16_t value) {{ + uint16_t temp = 0; + memcpy(&temp, &value, sizeof(float16_t)); + std::stringstream ss; + ss << "0x" << std::setfill('0') << std::setw(4) << std::hex << temp; + os << ss.str(); + return os; +}} +"# + )?; + + writeln!(w, "#ifdef __{architecture}__")?; + writeln!(w, "{arch_specific_definitions }")?; + writeln!(w, "#endif")?; + + for intrinsic in intrinsics.clone() { + writeln!(w, "extern int run_{intrinsic}(void);")?; + } + + writeln!(w, "int main(int argc, char **argv) {{")?; + writeln!(w, " std::string intrinsic_name = argv[1];")?; + + writeln!(w, " if (false) {{")?; + + for intrinsic in intrinsics { + writeln!(w, " }} else if (intrinsic_name == \"{intrinsic}\") {{")?; + writeln!(w, " return run_{intrinsic}();")?; + } + + writeln!(w, " }} else {{")?; + writeln!( + w, + " std::cerr << \"Unknown command: \" << intrinsic_name << \"\\n\";" + )?; + writeln!(w, " return -1;")?; + writeln!(w, " }}")?; + + writeln!(w, "}}")?; + + Ok(()) } diff --git a/library/stdarch/crates/intrinsic-test/src/common/gen_rust.rs b/library/stdarch/crates/intrinsic-test/src/common/gen_rust.rs index 52bccaf905c..0e4a95ab528 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/gen_rust.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/gen_rust.rs @@ -2,7 +2,6 @@ use itertools::Itertools; use rayon::prelude::*; use std::collections::BTreeMap; use std::fs::File; -use std::io::Write; use std::process::Command; use super::argument::Argument; @@ -23,8 +22,8 @@ pub fn format_rust_main_template( ) -> String { format!( r#"{notices}#![feature(simd_ffi)] -#![feature(link_llvm_intrinsics)] #![feature(f16)] +#![allow(unused)] {configurations} {definitions} @@ -38,6 +37,42 @@ fn main() {{ ) } +fn write_cargo_toml(w: &mut impl std::io::Write, binaries: &[String]) -> std::io::Result<()> { + writeln!( + w, + concat!( + "[package]\n", + "name = \"intrinsic-test-programs\"\n", + "version = \"{version}\"\n", + "authors = [{authors}]\n", + "license = \"{license}\"\n", + "edition = \"2018\"\n", + "[workspace]\n", + "[dependencies]\n", + "core_arch = {{ path = \"../crates/core_arch\" }}", + ), + version = env!("CARGO_PKG_VERSION"), + authors = env!("CARGO_PKG_AUTHORS") + .split(":") + .format_with(", ", |author, fmt| fmt(&format_args!("\"{author}\""))), + license = env!("CARGO_PKG_LICENSE"), + )?; + + for binary in binaries { + writeln!( + w, + concat!( + "[[bin]]\n", + "name = \"{binary}\"\n", + "path = \"{binary}/main.rs\"\n", + ), + binary = binary, + )?; + } + + Ok(()) +} + pub fn compile_rust_programs( binaries: Vec<String>, toolchain: Option<&str>, @@ -45,56 +80,20 @@ pub fn compile_rust_programs( linker: Option<&str>, ) -> bool { let mut cargo = File::create("rust_programs/Cargo.toml").unwrap(); - cargo - .write_all( - format!( - r#"[package] -name = "intrinsic-test-programs" -version = "{version}" -authors = [{authors}] -license = "{license}" -edition = "2018" -[workspace] -[dependencies] -core_arch = {{ path = "../crates/core_arch" }} -{binaries}"#, - version = env!("CARGO_PKG_VERSION"), - authors = env!("CARGO_PKG_AUTHORS") - .split(":") - .format_with(", ", |author, fmt| fmt(&format_args!("\"{author}\""))), - license = env!("CARGO_PKG_LICENSE"), - binaries = binaries - .iter() - .map(|binary| { - format!( - r#"[[bin]] -name = "{binary}" -path = "{binary}/main.rs""#, - ) - }) - .collect::<Vec<_>>() - .join("\n") - ) - .into_bytes() - .as_slice(), - ) - .unwrap(); - - let toolchain = match toolchain { - None => return true, - Some(t) => t, - }; + write_cargo_toml(&mut cargo, &binaries).unwrap(); /* If there has been a linker explicitly set from the command line then * we want to set it via setting it in the RUSTFLAGS*/ - let cargo_command = format!("cargo {toolchain} build --target {target} --release"); + let mut cargo_command = Command::new("cargo"); + cargo_command.current_dir("rust_programs"); - let mut command = Command::new("sh"); - command - .current_dir("rust_programs") - .arg("-c") - .arg(cargo_command); + if let Some(toolchain) = toolchain { + if !toolchain.is_empty() { + cargo_command.arg(toolchain); + } + } + cargo_command.args(["build", "--target", target, "--release"]); let mut rust_flags = "-Cdebuginfo=0".to_string(); if let Some(linker) = linker { @@ -102,11 +101,11 @@ path = "{binary}/main.rs""#, rust_flags.push_str(linker); rust_flags.push_str(" -C link-args=-static"); - command.env("CPPFLAGS", "-fuse-ld=lld"); + cargo_command.env("CPPFLAGS", "-fuse-ld=lld"); } - command.env("RUSTFLAGS", rust_flags); - let output = command.output(); + cargo_command.env("RUSTFLAGS", rust_flags); + let output = cargo_command.output(); if let Ok(output) = output { if output.status.success() { diff --git a/library/stdarch/crates/intrinsic-test/src/common/intrinsic_helpers.rs b/library/stdarch/crates/intrinsic-test/src/common/intrinsic_helpers.rs index 3d200b19461..697f9c8754d 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/intrinsic_helpers.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/intrinsic_helpers.rs @@ -9,13 +9,21 @@ use super::indentation::Indentation; use super::values::value_for_array; #[derive(Debug, PartialEq, Copy, Clone)] +pub enum Sign { + Signed, + Unsigned, +} + +#[derive(Debug, PartialEq, Copy, Clone)] pub enum TypeKind { BFloat, Float, - Int, - UInt, + Int(Sign), + Char(Sign), Poly, Void, + Mask, + Vector, } impl FromStr for TypeKind { @@ -23,12 +31,17 @@ impl FromStr for TypeKind { fn from_str(s: &str) -> Result<Self, Self::Err> { match s { - "bfloat" => Ok(Self::BFloat), - "float" => Ok(Self::Float), - "int" => Ok(Self::Int), + "bfloat" | "BF16" => Ok(Self::BFloat), + "float" | "double" | "FP16" | "FP32" | "FP64" => Ok(Self::Float), + "int" | "long" | "short" | "SI8" | "SI16" | "SI32" | "SI64" => { + Ok(Self::Int(Sign::Signed)) + } "poly" => Ok(Self::Poly), - "uint" | "unsigned" => Ok(Self::UInt), + "char" => Ok(Self::Char(Sign::Signed)), + "uint" | "unsigned" | "UI8" | "UI16" | "UI32" | "UI64" => Ok(Self::Int(Sign::Unsigned)), "void" => Ok(Self::Void), + "MASK" => Ok(Self::Mask), + "M64" | "M128" | "M256" | "M512" => Ok(Self::Vector), _ => Err(format!("Impossible to parse argument kind {s}")), } } @@ -42,10 +55,14 @@ impl fmt::Display for TypeKind { match self { Self::BFloat => "bfloat", Self::Float => "float", - Self::Int => "int", - Self::UInt => "uint", + Self::Int(Sign::Signed) => "int", + Self::Int(Sign::Unsigned) => "uint", Self::Poly => "poly", Self::Void => "void", + Self::Char(Sign::Signed) => "char", + Self::Char(Sign::Unsigned) => "unsigned char", + Self::Mask => "mask", + Self::Vector => "vector", } ) } @@ -56,9 +73,10 @@ impl TypeKind { pub fn c_prefix(&self) -> &str { match self { Self::Float => "float", - Self::Int => "int", - Self::UInt => "uint", + Self::Int(Sign::Signed) => "int", + Self::Int(Sign::Unsigned) => "uint", Self::Poly => "poly", + Self::Char(Sign::Signed) => "char", _ => unreachable!("Not used: {:#?}", self), } } @@ -66,10 +84,13 @@ impl TypeKind { /// Gets the rust prefix for the type kind i.e. i, u, f. pub fn rust_prefix(&self) -> &str { match self { + Self::BFloat => "bf", Self::Float => "f", - Self::Int => "i", - Self::UInt => "u", + Self::Int(Sign::Signed) => "i", + Self::Int(Sign::Unsigned) => "u", Self::Poly => "u", + Self::Char(Sign::Unsigned) => "u", + Self::Char(Sign::Signed) => "i", _ => unreachable!("Unused type kind: {:#?}", self), } } @@ -133,11 +154,14 @@ impl IntrinsicType { } pub fn c_scalar_type(&self) -> String { - format!( - "{prefix}{bits}_t", - prefix = self.kind().c_prefix(), - bits = self.inner_size() - ) + match self.kind() { + TypeKind::Char(_) => String::from("char"), + _ => format!( + "{prefix}{bits}_t", + prefix = self.kind().c_prefix(), + bits = self.inner_size() + ), + } } pub fn rust_scalar_type(&self) -> String { @@ -155,8 +179,8 @@ impl IntrinsicType { bit_len: Some(8), .. } => match kind { - TypeKind::Int => "(int)", - TypeKind::UInt => "(unsigned int)", + TypeKind::Int(Sign::Signed) => "(int)", + TypeKind::Int(Sign::Unsigned) => "(unsigned int)", TypeKind::Poly => "(unsigned int)(uint8_t)", _ => "", }, @@ -172,6 +196,21 @@ impl IntrinsicType { 128 => "", _ => panic!("invalid bit_len"), }, + IntrinsicType { + kind: TypeKind::Float, + bit_len: Some(bit_len), + .. + } => match bit_len { + 16 => "(float16_t)", + 32 => "(float)", + 64 => "(double)", + 128 => "", + _ => panic!("invalid bit_len"), + }, + IntrinsicType { + kind: TypeKind::Char(_), + .. + } => "(char)", _ => "", } } @@ -185,7 +224,7 @@ impl IntrinsicType { match self { IntrinsicType { bit_len: Some(bit_len @ (8 | 16 | 32 | 64)), - kind: kind @ (TypeKind::Int | TypeKind::UInt | TypeKind::Poly), + kind: kind @ (TypeKind::Int(_) | TypeKind::Poly | TypeKind::Char(_)), simd_len, vec_len, .. @@ -201,7 +240,8 @@ impl IntrinsicType { .format_with(",\n", |i, fmt| { let src = value_for_array(*bit_len, i); assert!(src == 0 || src.ilog2() < *bit_len); - if *kind == TypeKind::Int && (src >> (*bit_len - 1)) != 0 { + if *kind == TypeKind::Int(Sign::Signed) && (src >> (*bit_len - 1)) != 0 + { // `src` is a two's complement representation of a negative value. let mask = !0u64 >> (64 - *bit_len); let ones_compl = src ^ mask; @@ -257,7 +297,7 @@ impl IntrinsicType { .. } => false, IntrinsicType { - kind: TypeKind::Int | TypeKind::UInt | TypeKind::Poly, + kind: TypeKind::Int(_) | TypeKind::Poly, .. } => true, _ => unimplemented!(), @@ -282,7 +322,9 @@ pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> { fn get_lane_function(&self) -> String; /// can be implemented in an `impl` block - fn from_c(_s: &str, _target: &str) -> Result<Box<Self>, String>; + fn from_c(_s: &str, _target: &str) -> Result<Self, String> + where + Self: Sized; /// Gets a string containing the typename for this type in C format. /// can be directly defined in `impl` blocks diff --git a/library/stdarch/crates/intrinsic-test/src/common/write_file.rs b/library/stdarch/crates/intrinsic-test/src/common/write_file.rs index 0ba3e829a6b..92dd70b7c57 100644 --- a/library/stdarch/crates/intrinsic-test/src/common/write_file.rs +++ b/library/stdarch/crates/intrinsic-test/src/common/write_file.rs @@ -1,5 +1,3 @@ -use super::gen_c::create_c_test_program; -use super::gen_c::setup_c_file_paths; use super::gen_rust::{create_rust_test_program, setup_rust_file_paths}; use super::intrinsic::IntrinsicDefinition; use super::intrinsic_helpers::IntrinsicTypeDefinition; @@ -11,37 +9,6 @@ pub fn write_file(filename: &String, code: String) { file.write_all(code.into_bytes().as_slice()).unwrap(); } -pub fn write_c_testfiles<T: IntrinsicTypeDefinition + Sized>( - intrinsics: &Vec<&dyn IntrinsicDefinition<T>>, - target: &str, - c_target: &str, - headers: &[&str], - notice: &str, - arch_specific_definitions: &[&str], -) -> Vec<String> { - let intrinsics_name_list = intrinsics - .iter() - .map(|i| i.name().clone()) - .collect::<Vec<_>>(); - let filename_mapping = setup_c_file_paths(&intrinsics_name_list); - - intrinsics.iter().for_each(|&i| { - let c_code = create_c_test_program( - i, - headers, - target, - c_target, - notice, - arch_specific_definitions, - ); - if let Some(filename) = filename_mapping.get(&i.name()) { - write_file(filename, c_code) - }; - }); - - intrinsics_name_list -} - pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>( intrinsics: Vec<&dyn IntrinsicDefinition<T>>, rust_target: &str, |
