Skip to content

Adding the x86 part of behavioural testing for std::arch intrinsics #1814

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 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
4a3d05f
fix: updated function definition of "from_c" in IntrinsicTypeDefinition
madhav-madhusoodanan May 30, 2025
38fd826
Feat: started the skeleton for x86 module, added XML intrinsic parsin…
madhav-madhusoodanan May 30, 2025
a786643
feat: added functionality to convert XML description of intrinsics to
madhav-madhusoodanan Jun 3, 2025
7b8e08c
feat: added the simple set of argument types for X86 intrinsics
madhav-madhusoodanan Jun 8, 2025
e95bd9b
feat: added X86IntrinsicType parsing from string.
madhav-madhusoodanan Jun 8, 2025
241f309
fix: removing Box<> types from IntrinsicType in "from_c" definition for
madhav-madhusoodanan Jun 8, 2025
c77800a
feat: implemented c_type for X86IntrinsicType
madhav-madhusoodanan Jun 8, 2025
287b83c
Sharpening the parsing logic:
madhav-madhusoodanan Jun 13, 2025
6ee8e57
feat: demote "target" to a 2nd class variable, since it is only rarely
madhav-madhusoodanan Jun 16, 2025
16bd12c
Add x86/config.rs to intrinsic-test
madhav-madhusoodanan Jun 16, 2025
e827a04
Fix: unused variables.
madhav-madhusoodanan Jun 16, 2025
2784b45
feat: fetching c_type representation from IntrinsicType's hashmap dir…
madhav-madhusoodanan Jun 17, 2025
d0f2d78
feat: changed from TypeKind::Int(bool) to TypeKind::Int(Sign) for more
madhav-madhusoodanan Jun 19, 2025
758d7ec
Feat: setup load function for x86 intrinsics
madhav-madhusoodanan Jun 24, 2025
6e190b2
feat: implemented c_single_vector_type and fixed logical errors in
madhav-madhusoodanan Jun 24, 2025
e947072
feat: added vector types to support intrinsics that does not represent a
madhav-madhusoodanan Jul 4, 2025
e57c4bf
feat: added memsize and rust_type implementation
madhav-madhusoodanan Jul 4, 2025
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
2 changes: 2 additions & 0 deletions crates/intrinsic-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ pretty_env_logger = "0.5.0"
rayon = "1.5.0"
diff = "0.1.12"
itertools = "0.14.0"
quick-xml = { version = "0.37.5", features = ["serialize", "overlapped-lists"] }
serde-xml-rs = "0.8.0"
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..])
}
}
15 changes: 11 additions & 4 deletions crates/intrinsic-test/src/arm/intrinsic.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::common::argument::ArgumentList;
use crate::common::indentation::Indentation;
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
use std::ops::Deref;
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, Sign, TypeKind};
use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, PartialEq)]
pub struct ArmIntrinsicType(pub IntrinsicType);
Expand All @@ -15,6 +15,12 @@ impl Deref for ArmIntrinsicType {
}
}

impl DerefMut for ArmIntrinsicType {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {
self.arguments.clone()
Expand Down Expand Up @@ -73,8 +79,9 @@ impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
TypeKind::Float if self.results().inner_size() == 16 => "float16_t".to_string(),
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::Int(Sign::Signed) => format!("int{}_t", self.results().inner_size()),
TypeKind::Int(Sign::Unsigned) =>
format!("uint{}_t", self.results().inner_size()),
TypeKind::Poly => format!("poly{}_t", self.results().inner_size()),
ty => todo!("print_result_c - Unknown type: {:#?}", ty),
},
Expand Down
22 changes: 14 additions & 8 deletions crates/intrinsic-test/src/arm/json_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,26 @@ 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);
arg.ty
.set_metadata("target".to_string(), target.to_string());

// The JSON doesn't list immediates as const
let IntrinsicType {
Expand All @@ -110,7 +116,7 @@ fn json_to_intrinsic(
Ok(Intrinsic {
name,
arguments,
results: *results,
results: results,
arch_tags: intr.architectures,
})
}
Expand Down
1 change: 1 addition & 0 deletions crates/intrinsic-test/src/arm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod argument;
mod compile;
mod config;
mod intrinsic;
Expand Down
77 changes: 41 additions & 36 deletions crates/intrinsic-test/src/arm/types.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
use std::collections::HashMap;

use super::intrinsic::ArmIntrinsicType;
use crate::common::cli::Language;
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, Sign, TypeKind};

impl IntrinsicTypeDefinition for ArmIntrinsicType {
/// Gets a string containing the typename for this type in C format.
/// This assumes that the metadata hashmap contains this value at the
/// "type" key
fn c_type(&self) -> String {
let prefix = self.0.kind.c_prefix();
let const_prefix = if self.0.constant { "const " } else { "" };

if let (Some(bit_len), simd_len, vec_len) =
(self.0.bit_len, self.0.simd_len, self.0.vec_len)
{
match (simd_len, vec_len) {
(None, None) => format!("{const_prefix}{prefix}{bit_len}_t"),
(Some(simd), None) => format!("{prefix}{bit_len}x{simd}_t"),
(Some(simd), Some(vec)) => format!("{prefix}{bit_len}x{simd}x{vec}_t"),
(None, Some(_)) => todo!("{:#?}", self), // Likely an invalid case
}
} else {
todo!("{:#?}", self)
}
self.metadata
.get("type")
.expect("Failed to extract the C typename in Aarch!")
.replace("*", "")
.replace(" ", "")
.trim()
.to_string()
}

fn c_single_vector_type(&self) -> String {
Expand Down Expand Up @@ -59,7 +55,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len: Some(bl),
simd_len,
vec_len,
target,
metadata,
..
} = &self.0
{
Expand All @@ -69,12 +65,16 @@ 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 {
TypeKind::UInt => "u",
TypeKind::Int => "s",
TypeKind::Int(Sign::Unsigned) => "u",
TypeKind::Int(Sign::Signed) => "s",
TypeKind::Float => "f",
// The ACLE doesn't support 64-bit polynomial loads on Armv7
// if armv7 and bl == 64, use "s", else "p"
Expand Down Expand Up @@ -107,8 +107,8 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
format!(
"vget{quad}_lane_{type}{size}",
type = match k {
TypeKind::UInt => "u",
TypeKind::Int => "s",
TypeKind::Int(Sign::Unsigned) => "u",
TypeKind::Int(Sign::Signed) => "s",
TypeKind::Float => "f",
TypeKind::Poly => "p",
x => todo!("get_load_function TypeKind: {:#?}", x),
Expand All @@ -121,20 +121,25 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
}
}

fn from_c(s: &str, target: &str) -> Result<Box<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);
temp_return.map(|mut op| {
let edited = op.as_mut();
edited.0.ptr = true;
edited.0.ptr_constant = constant;
op
let temp_return = ArmIntrinsicType::from_c(s);

// We are not adding the metadata hashmap here, since
// it is the return of a recursive call and the
// inner call would handle it.
temp_return.and_then(|mut op| {
op.ptr = true;
op.ptr_constant = constant;
Ok(op)
})
} else {
// [const ]TYPE[{bitlen}[x{simdlen}[x{vec_len}]]][_t]
Expand Down Expand Up @@ -163,32 +168,32 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
),
None => None,
};
Ok(Box::new(ArmIntrinsicType(IntrinsicType {
Ok(ArmIntrinsicType(IntrinsicType {
ptr: false,
ptr_constant: false,
constant,
kind: arg_kind,
bit_len: Some(bit_len),
simd_len,
vec_len,
target: target.to_string(),
})))
metadata,
}))
} else {
let kind = start.parse::<TypeKind>()?;
let bit_len = match kind {
TypeKind::Int => Some(32),
TypeKind::Int(_) => Some(32),
_ => None,
};
Ok(Box::new(ArmIntrinsicType(IntrinsicType {
Ok(ArmIntrinsicType(IntrinsicType {
ptr: false,
ptr_constant: false,
constant,
kind: start.parse::<TypeKind>()?,
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
3 changes: 1 addition & 2 deletions crates/intrinsic-test/src/common/gen_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>(
indentation: Indentation,
additional: &str,
passes: u32,
_target: &str,
) -> String {
let body_indentation = indentation.nested();
format!(
Expand Down Expand Up @@ -157,7 +156,7 @@ pub fn generate_c_constraint_blocks<T: IntrinsicTypeDefinition>(
})
.join("\n")
} else {
generate_c_test_loop(intrinsic, indentation, &name, PASSES, target)
generate_c_test_loop(intrinsic, indentation, &name, PASSES)
}
}

Expand Down
Loading
Loading