Skip to content

[IGNORE] Test the artifacts that are being generated by intrinsic-test at the master branch #1886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions crates/intrinsic-test/src/arm/argument.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use crate::arm::intrinsic::ArmIntrinsicType;
use crate::common::argument::Argument;

impl Argument<ArmIntrinsicType> {
pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
let split_index = arg
.rfind([' ', '*'])
.expect("Couldn't split type and argname");

(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
}
}
17 changes: 11 additions & 6 deletions crates/intrinsic-test/src/arm/json_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ fn json_to_intrinsic(
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
let name = intr.name.replace(['[', ']'], "");

let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?;
let mut results = ArmIntrinsicType::from_c(&intr.return_type.value)?;
results.set_metadata("target".to_string(), target.to_string());

let args = intr
.arguments
.into_iter()
.enumerate()
.map(|(i, arg)| {
let arg_name = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg).1;
let metadata = intr.args_prep.as_mut();
let metadata = metadata.and_then(|a| a.remove(arg_name));
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| a.try_into().ok());
let (type_name, arg_name) = Argument::<ArmIntrinsicType>::type_and_name_from_c(&arg);
let ty = ArmIntrinsicType::from_c(type_name)
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));

let arg_prep = intr.args_prep.as_mut();
let arg_prep = arg_prep.and_then(|a| a.remove(arg_name));
let arg_prep: Option<ArgPrep> = arg_prep.and_then(|a| a.try_into().ok());
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());

let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint);
let mut arg =
Argument::<ArmIntrinsicType>::new(i, arg_name.to_string(), ty, constraint);

// The JSON doesn't list immediates as const
let IntrinsicType {
Expand Down
54 changes: 32 additions & 22 deletions crates/intrinsic-test/src/arm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
mod argument;
mod compile;
mod config;
mod intrinsic;
mod json_parser;
mod types;

use std::fs::File;
use std::fs::{self, File};

use rayon::prelude::*;

Expand Down Expand Up @@ -69,28 +70,31 @@ impl SupportedArchitectureTest for ArmArchitectureTest {

let (chunk_size, chunk_count) = chunk_info(self.intrinsics.len());

let cpp_compiler = compile::build_cpp_compilation(&self.cli_options).unwrap();
let cpp_compiler_wrapped = compile::build_cpp_compilation(&self.cli_options);

let notice = &build_notices("// ");
fs::create_dir_all("c_programs").unwrap();
self.intrinsics
.par_chunks(chunk_size)
.enumerate()
.map(|(i, chunk)| {
let c_filename = format!("c_programs/mod_{i}.cpp");
let mut file = File::create(&c_filename).unwrap();
let mut file = fs::File::create(&c_filename).unwrap();
write_mod_cpp(&mut file, notice, c_target, platform_headers, chunk).unwrap();

// compile this cpp file into a .o file
let output = cpp_compiler
.compile_object_file(&format!("mod_{i}.cpp"), &format!("mod_{i}.o"))?;
assert!(output.status.success(), "{output:?}");
if let Some(cpp_compiler) = cpp_compiler_wrapped.as_ref() {
let output = cpp_compiler
.compile_object_file(&format!("mod_{i}.cpp"), &format!("mod_{i}.o"))?;
assert!(output.status.success(), "{output:?}");
}

Ok(())
})
.collect::<Result<(), std::io::Error>>()
.unwrap();

let mut file = File::create("c_programs/main.cpp").unwrap();
let mut file = fs::File::create("c_programs/main.cpp").unwrap();
write_main_cpp(
&mut file,
c_target,
Expand All @@ -100,20 +104,22 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
.unwrap();

// compile this cpp file into a .o file
info!("compiling main.cpp");
let output = cpp_compiler
.compile_object_file("main.cpp", "intrinsic-test-programs.o")
.unwrap();
assert!(output.status.success(), "{output:?}");

let object_files = (0..chunk_count)
.map(|i| format!("mod_{i}.o"))
.chain(["intrinsic-test-programs.o".to_owned()]);

let output = cpp_compiler
.link_executable(object_files, "intrinsic-test-programs")
.unwrap();
assert!(output.status.success(), "{output:?}");
if let Some(cpp_compiler) = cpp_compiler_wrapped.as_ref() {
info!("compiling main.cpp");
let output = cpp_compiler
.compile_object_file("main.cpp", "intrinsic-test-programs.o")
.unwrap();
assert!(output.status.success(), "{output:?}");

let object_files = (0..chunk_count)
.map(|i| format!("mod_{i}.o"))
.chain(["intrinsic-test-programs.o".to_owned()]);

let output = cpp_compiler
.link_executable(object_files, "intrinsic-test-programs")
.unwrap();
assert!(output.status.success(), "{output:?}");
}

true
}
Expand Down Expand Up @@ -172,7 +178,11 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
.collect::<Result<(), std::io::Error>>()
.unwrap();

compile_rust_programs(toolchain, target, linker)
if self.cli_options.toolchain.is_some() {
compile_rust_programs(toolchain, target, linker)
} else {
true
}
}

fn compare_outputs(&self) -> bool {
Expand Down
20 changes: 14 additions & 6 deletions crates/intrinsic-test/src/arm/types.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use super::intrinsic::ArmIntrinsicType;
use crate::common::cli::Language;
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, Sign, TypeKind};
Expand Down Expand Up @@ -40,7 +42,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len: Some(bl),
simd_len,
vec_len,
target,
metadata,
..
} = &self.0
{
Expand All @@ -50,7 +52,11 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
""
};

let choose_workaround = language == Language::C && target.contains("v7");
let choose_workaround = language == Language::C
&& metadata
.get("target")
.filter(|value| value.contains("v7"))
.is_some();
format!(
"vld{len}{quad}_{type}{size}",
type = match k {
Expand Down Expand Up @@ -102,15 +108,17 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
}
}

fn from_c(s: &str, target: &str) -> Result<Self, String> {
fn from_c(s: &str) -> Result<Self, String> {
const CONST_STR: &str = "const";
let mut metadata: HashMap<String, String> = HashMap::new();
metadata.insert("type".to_string(), s.to_string());
if let Some(s) = s.strip_suffix('*') {
let (s, constant) = match s.trim().strip_suffix(CONST_STR) {
Some(stripped) => (stripped, true),
None => (s, false),
};
let s = s.trim_end();
let temp_return = ArmIntrinsicType::from_c(s, target);
let temp_return = ArmIntrinsicType::from_c(s);
temp_return.map(|mut op| {
op.ptr = true;
op.ptr_constant = constant;
Expand Down Expand Up @@ -151,7 +159,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len: Some(bit_len),
simd_len,
vec_len,
target: target.to_string(),
metadata,
}))
} else {
let kind = start.parse::<TypeKind>()?;
Expand All @@ -167,7 +175,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len,
simd_len: None,
vec_len: None,
target: target.to_string(),
metadata,
}))
}
}
Expand Down
36 changes: 9 additions & 27 deletions crates/intrinsic-test/src/common/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ impl<T> Argument<T>
where
T: IntrinsicTypeDefinition,
{
pub fn new(pos: usize, name: String, ty: T, constraint: Option<Constraint>) -> Self {
Argument {
pos,
name,
ty,
constraint,
}
}

pub fn to_c_type(&self) -> String {
self.ty.c_type()
}
Expand All @@ -36,14 +45,6 @@ where
self.constraint.is_some()
}

pub fn type_and_name_from_c(arg: &str) -> (&str, &str) {
let split_index = arg
.rfind([' ', '*'])
.expect("Couldn't split type and argname");

(arg[..split_index + 1].trim_end(), &arg[split_index + 1..])
}

/// The binding keyword (e.g. "const" or "let") for the array of possible test inputs.
fn rust_vals_array_binding(&self) -> impl std::fmt::Display {
if self.ty.is_rust_vals_array_const() {
Expand All @@ -62,25 +63,6 @@ where
}
}

pub fn from_c(
pos: usize,
arg: &str,
target: &str,
constraint: Option<Constraint>,
) -> Argument<T> {
let (ty, var_name) = Self::type_and_name_from_c(arg);

let ty =
T::from_c(ty, target).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));

Argument {
pos,
name: String::from(var_name),
ty: ty,
constraint,
}
}

fn as_call_param_c(&self) -> String {
self.ty.as_call_param_c(&self.name)
}
Expand Down
11 changes: 8 additions & 3 deletions crates/intrinsic-test/src/common/constraint.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
use serde::Deserialize;
use std::ops::Range;

/// Describes the values to test for a const generic parameter.
#[derive(Debug, PartialEq, Clone, Deserialize)]
pub enum Constraint {
/// Test a single value.
Equal(i64),
/// Test a range of values, e.g. `0..16`.
Range(Range<i64>),
Set(Vec<i64>),
}

impl Constraint {
pub fn to_range(&self) -> Range<i64> {
pub fn to_vector(&self) -> Vec<i64> {
match self {
Constraint::Equal(eq) => *eq..*eq + 1,
Constraint::Range(range) => range.clone(),
Constraint::Equal(eq) => vec![*eq],
Constraint::Range(range) => range.clone().collect::<Vec<i64>>(),
Constraint::Set(values) => values.clone(),
}
}
}
2 changes: 1 addition & 1 deletion crates/intrinsic-test/src/common/gen_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn generate_c_constraint_blocks<'a, T: IntrinsicTypeDefinition + 'a>(
};

let body_indentation = indentation.nested();
for i in current.constraint.iter().flat_map(|c| c.to_range()) {
for i in current.constraint.iter().flat_map(|c| c.to_vector()) {
let ty = current.ty.c_type();

writeln!(w, "{indentation}{{")?;
Expand Down
11 changes: 5 additions & 6 deletions crates/intrinsic-test/src/common/gen_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,8 @@ pub fn compile_rust_programs(toolchain: Option<&str>, target: &str, linker: Opti
// Do not use the target directory of the workspace please.
cargo_command.env("CARGO_TARGET_DIR", "target");

if let Some(toolchain) = toolchain
&& !toolchain.is_empty()
{
cargo_command.arg(toolchain);
if toolchain.is_some_and(|val| !val.is_empty()) {
cargo_command.arg(toolchain.unwrap());
}
cargo_command.args(["build", "--target", target, "--release"]);

Expand Down Expand Up @@ -251,12 +249,13 @@ pub fn generate_rust_test_loop<T: IntrinsicTypeDefinition>(

/// Generate the specializations (unique sequences of const-generic arguments) for this intrinsic.
fn generate_rust_specializations<'a>(
constraints: &mut impl Iterator<Item = std::ops::Range<i64>>,
constraints: &mut impl Iterator<Item = Vec<i64>>,
) -> Vec<Vec<u8>> {
let mut specializations = vec![vec![]];

for constraint in constraints {
specializations = constraint
.into_iter()
.flat_map(|right| {
specializations.iter().map(move |left| {
let mut left = left.clone();
Expand Down Expand Up @@ -288,7 +287,7 @@ pub fn create_rust_test_module<T: IntrinsicTypeDefinition>(
let specializations = generate_rust_specializations(
&mut arguments
.iter()
.filter_map(|i| i.constraint.as_ref().map(|v| v.to_range())),
.filter_map(|i| i.constraint.as_ref().map(|v| v.to_vector())),
);

generate_rust_test_loop(w, intrinsic, indentation, &specializations, PASSES)?;
Expand Down
9 changes: 7 additions & 2 deletions crates/intrinsic-test/src/common/intrinsic_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;
Expand Down Expand Up @@ -121,7 +122,7 @@ pub struct IntrinsicType {
/// A value of `None` can be assumed to be 1 though.
pub vec_len: Option<u32>,

pub target: String,
pub metadata: HashMap<String, String>,
}

impl IntrinsicType {
Expand Down Expand Up @@ -153,6 +154,10 @@ impl IntrinsicType {
self.ptr
}

pub fn set_metadata(&mut self, key: String, value: String) {
self.metadata.insert(key, value);
}

pub fn c_scalar_type(&self) -> String {
match self.kind() {
TypeKind::Char(_) => String::from("char"),
Expand Down Expand Up @@ -322,7 +327,7 @@ 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<Self, String>
fn from_c(_s: &str) -> Result<Self, String>
where
Self: Sized;

Expand Down
Loading