2025-03-27 22:10:00 +04:00
|
|
|
use itertools::Itertools;
|
|
|
|
|
use rayon::prelude::*;
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
2025-05-25 23:52:56 +05:30
|
|
|
use super::argument::Argument;
|
|
|
|
|
use super::indentation::Indentation;
|
|
|
|
|
use super::intrinsic::{IntrinsicDefinition, format_f16_return_value};
|
|
|
|
|
use super::intrinsic_helpers::IntrinsicTypeDefinition;
|
|
|
|
|
|
|
|
|
|
// The number of times each intrinsic will be called.
|
|
|
|
|
const PASSES: u32 = 20;
|
|
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
pub fn format_rust_main_template(
|
2025-03-27 22:10:00 +04:00
|
|
|
notices: &str,
|
2025-05-21 13:50:33 +05:30
|
|
|
definitions: &str,
|
2025-03-27 22:10:00 +04:00
|
|
|
configurations: &str,
|
|
|
|
|
arch_definition: &str,
|
|
|
|
|
arglists: &str,
|
|
|
|
|
passes: &str,
|
|
|
|
|
) -> String {
|
|
|
|
|
format!(
|
|
|
|
|
r#"{notices}#![feature(simd_ffi)]
|
|
|
|
|
#![feature(f16)]
|
2025-07-08 09:59:03 +02:00
|
|
|
#![allow(unused)]
|
2025-03-27 22:10:00 +04:00
|
|
|
{configurations}
|
2025-05-21 13:50:33 +05:30
|
|
|
{definitions}
|
|
|
|
|
|
2025-03-27 22:10:00 +04:00
|
|
|
use core_arch::arch::{arch_definition}::*;
|
|
|
|
|
|
|
|
|
|
fn main() {{
|
|
|
|
|
{arglists}
|
|
|
|
|
{passes}
|
|
|
|
|
}}
|
|
|
|
|
"#,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-08 09:59:03 +02:00
|
|
|
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(())
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
pub fn compile_rust_programs(
|
2025-05-11 00:27:40 +05:30
|
|
|
binaries: Vec<String>,
|
2025-03-27 22:10:00 +04:00
|
|
|
toolchain: Option<&str>,
|
|
|
|
|
target: &str,
|
|
|
|
|
linker: Option<&str>,
|
|
|
|
|
) -> bool {
|
|
|
|
|
let mut cargo = File::create("rust_programs/Cargo.toml").unwrap();
|
2025-07-08 09:59:03 +02:00
|
|
|
write_cargo_toml(&mut cargo, &binaries).unwrap();
|
2025-03-27 22:10:00 +04:00
|
|
|
|
|
|
|
|
/* If there has been a linker explicitly set from the command line then
|
|
|
|
|
* we want to set it via setting it in the RUSTFLAGS*/
|
|
|
|
|
|
2025-07-08 09:59:03 +02:00
|
|
|
let mut cargo_command = Command::new("cargo");
|
|
|
|
|
cargo_command.current_dir("rust_programs");
|
2025-03-27 22:10:00 +04:00
|
|
|
|
2025-07-08 09:59:03 +02:00
|
|
|
if let Some(toolchain) = toolchain {
|
|
|
|
|
if !toolchain.is_empty() {
|
|
|
|
|
cargo_command.arg(toolchain);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cargo_command.args(["build", "--target", target, "--release"]);
|
2025-03-27 22:10:00 +04:00
|
|
|
|
|
|
|
|
let mut rust_flags = "-Cdebuginfo=0".to_string();
|
|
|
|
|
if let Some(linker) = linker {
|
|
|
|
|
rust_flags.push_str(" -C linker=");
|
|
|
|
|
rust_flags.push_str(linker);
|
|
|
|
|
rust_flags.push_str(" -C link-args=-static");
|
|
|
|
|
|
2025-07-08 09:59:03 +02:00
|
|
|
cargo_command.env("CPPFLAGS", "-fuse-ld=lld");
|
2025-03-27 22:10:00 +04:00
|
|
|
}
|
|
|
|
|
|
2025-07-08 09:59:03 +02:00
|
|
|
cargo_command.env("RUSTFLAGS", rust_flags);
|
|
|
|
|
let output = cargo_command.output();
|
2025-03-27 22:10:00 +04:00
|
|
|
|
|
|
|
|
if let Ok(output) = output {
|
|
|
|
|
if output.status.success() {
|
|
|
|
|
true
|
|
|
|
|
} else {
|
|
|
|
|
error!(
|
|
|
|
|
"Failed to compile code for rust 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 {
|
2025-07-06 07:15:54 +00:00
|
|
|
error!("Command failed: {output:#?}");
|
2025-03-27 22:10:00 +04:00
|
|
|
false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
// Creates directory structure and file path mappings
|
|
|
|
|
pub fn setup_rust_file_paths(identifiers: &Vec<String>) -> BTreeMap<&String, String> {
|
2025-03-27 22:10:00 +04:00
|
|
|
identifiers
|
|
|
|
|
.par_iter()
|
|
|
|
|
.map(|identifier| {
|
2025-05-31 05:06:05 +00:00
|
|
|
let rust_dir = format!("rust_programs/{identifier}");
|
2025-03-27 22:10:00 +04:00
|
|
|
let _ = std::fs::create_dir_all(&rust_dir);
|
2025-05-31 05:06:05 +00:00
|
|
|
let rust_filename = format!("{rust_dir}/main.rs");
|
2025-03-27 22:10:00 +04:00
|
|
|
|
2025-03-30 12:15:29 +04:00
|
|
|
(identifier, rust_filename)
|
2025-03-27 22:10:00 +04:00
|
|
|
})
|
2025-03-30 12:15:29 +04:00
|
|
|
.collect::<BTreeMap<&String, String>>()
|
2025-03-27 22:10:00 +04:00
|
|
|
}
|
2025-05-25 23:52:56 +05:30
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
pub fn generate_rust_test_loop<T: IntrinsicTypeDefinition>(
|
2025-05-25 23:52:56 +05:30
|
|
|
intrinsic: &dyn IntrinsicDefinition<T>,
|
|
|
|
|
indentation: Indentation,
|
|
|
|
|
additional: &str,
|
|
|
|
|
passes: u32,
|
|
|
|
|
) -> String {
|
|
|
|
|
let constraints = intrinsic.arguments().as_constraint_parameters_rust();
|
|
|
|
|
let constraints = if !constraints.is_empty() {
|
|
|
|
|
format!("::<{constraints}>")
|
|
|
|
|
} else {
|
|
|
|
|
constraints
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let return_value = format_f16_return_value(intrinsic);
|
|
|
|
|
let indentation2 = indentation.nested();
|
|
|
|
|
let indentation3 = indentation2.nested();
|
|
|
|
|
format!(
|
|
|
|
|
"{indentation}for i in 0..{passes} {{\n\
|
|
|
|
|
{indentation2}unsafe {{\n\
|
|
|
|
|
{loaded_args}\
|
|
|
|
|
{indentation3}let __return_value = {intrinsic_call}{const}({args});\n\
|
|
|
|
|
{indentation3}println!(\"Result {additional}-{{}}: {{:?}}\", i + 1, {return_value});\n\
|
|
|
|
|
{indentation2}}}\n\
|
|
|
|
|
{indentation}}}",
|
|
|
|
|
loaded_args = intrinsic.arguments().load_values_rust(indentation3),
|
|
|
|
|
intrinsic_call = intrinsic.name(),
|
|
|
|
|
const = constraints,
|
|
|
|
|
args = intrinsic.arguments().as_call_param_rust(),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
pub fn generate_rust_constraint_blocks<T: IntrinsicTypeDefinition>(
|
2025-05-25 23:52:56 +05:30
|
|
|
intrinsic: &dyn IntrinsicDefinition<T>,
|
|
|
|
|
indentation: Indentation,
|
|
|
|
|
constraints: &[&Argument<T>],
|
|
|
|
|
name: String,
|
|
|
|
|
) -> 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}const {name}: {ty} = {val};\n\
|
|
|
|
|
{pass}\n\
|
|
|
|
|
{indentation}}}",
|
|
|
|
|
name = current.name,
|
|
|
|
|
ty = current.ty.rust_type(),
|
|
|
|
|
val = i,
|
2025-05-26 10:30:23 +05:30
|
|
|
pass = generate_rust_constraint_blocks(
|
2025-05-25 23:52:56 +05:30
|
|
|
intrinsic,
|
|
|
|
|
body_indentation,
|
|
|
|
|
constraints,
|
|
|
|
|
format!("{name}-{i}")
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.join("\n")
|
|
|
|
|
} else {
|
2025-05-26 10:30:23 +05:30
|
|
|
generate_rust_test_loop(intrinsic, indentation, &name, PASSES)
|
2025-05-25 23:52:56 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-26 10:30:23 +05:30
|
|
|
// Top-level function to create complete test program
|
|
|
|
|
pub fn create_rust_test_program<T: IntrinsicTypeDefinition>(
|
2025-05-25 23:52:56 +05:30
|
|
|
intrinsic: &dyn IntrinsicDefinition<T>,
|
|
|
|
|
target: &str,
|
|
|
|
|
notice: &str,
|
|
|
|
|
definitions: &str,
|
|
|
|
|
cfg: &str,
|
|
|
|
|
) -> String {
|
|
|
|
|
let arguments = intrinsic.arguments();
|
|
|
|
|
let constraints = arguments
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|i| i.has_constraint())
|
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
|
|
let indentation = Indentation::default();
|
2025-05-26 10:30:23 +05:30
|
|
|
format_rust_main_template(
|
2025-05-25 23:52:56 +05:30
|
|
|
notice,
|
|
|
|
|
definitions,
|
|
|
|
|
cfg,
|
|
|
|
|
target,
|
|
|
|
|
intrinsic
|
|
|
|
|
.arguments()
|
|
|
|
|
.gen_arglists_rust(indentation.nested(), PASSES)
|
|
|
|
|
.as_str(),
|
2025-05-26 10:30:23 +05:30
|
|
|
generate_rust_constraint_blocks(
|
2025-05-25 23:52:56 +05:30
|
|
|
intrinsic,
|
|
|
|
|
indentation.nested(),
|
|
|
|
|
&constraints,
|
|
|
|
|
Default::default(),
|
|
|
|
|
)
|
|
|
|
|
.as_str(),
|
|
|
|
|
)
|
|
|
|
|
}
|