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 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 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
5b89089
feat: added initial functionality for building Rust files
madhav-madhusoodanan Jul 10, 2025
2624722
Added output comparison functionality
madhav-madhusoodanan Jul 10, 2025
9cd1cb0
feat: moved to_range to to_vector for extended iteration capabilities
madhav-madhusoodanan Jul 12, 2025
20b5d3b
Added constraint mapping
madhav-madhusoodanan Jul 13, 2025
661bf30
feat: added x86 C compilation and CI pipeline for testing x86 intrinsics
madhav-madhusoodanan Jul 19, 2025
cc33f60
Merge branch 'master' into x86_extension_intrinsic_test
madhav-madhusoodanan Jul 19, 2025
1d89c29
fix: update CI to avoid override issues
madhav-madhusoodanan Jul 19, 2025
a321ab4
Support generate_files option to skip file compilation and execution
madhav-madhusoodanan Jul 19, 2025
b705435
Fixed merge conflicts with master
madhav-madhusoodanan Jul 19, 2025
678d690
Merge branch 'master' into x86_extension_intrinsic_test
madhav-madhusoodanan Aug 2, 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
52 changes: 51 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ fi
# Test targets compiled with extra features.
case ${TARGET} in
x86_64-unknown-linux-gnu)
TEST_CPPFLAGS="-fuse-ld=lld -I/usr/include/x86_64-linux-gnu/"
TEST_CXX_COMPILER="clang++-19"
TEST_RUNNER="${CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER}"
export STDARCH_DISABLE_ASSERT_INSTR=1

export RUSTFLAGS="${RUSTFLAGS} -C target-feature=+avx"
Expand Down Expand Up @@ -181,6 +184,14 @@ case "${TARGET}" in
--linker "${CARGO_TARGET_AARCH64_BE_UNKNOWN_LINUX_GNU_LINKER}" \
--cxx-toolchain-dir "${AARCH64_BE_TOOLCHAIN}"
;;
x86_64-unknown-linux-gnu*)
CPPFLAGS="${TEST_CPPFLAGS}" RUSTFLAGS="${HOST_RUSTFLAGS}" RUST_LOG=warn \
cargo run "${INTRINSIC_TEST}" "${PROFILE}" \
--bin intrinsic-test -- intrinsics_data/x86-intel.xml \
--runner "${TEST_RUNNER}" \
--cppcompiler "${TEST_CXX_COMPILER}" \
--target "${TARGET}"
;;
*)
;;
esac
Expand Down
2 changes: 2 additions & 0 deletions crates/intrinsic-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,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"
4 changes: 2 additions & 2 deletions crates/intrinsic-test/src/arm/json_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ 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()
Expand Down
13 changes: 3 additions & 10 deletions crates/intrinsic-test/src/arm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::fs::{self, File};
use rayon::prelude::*;

use crate::arm::config::POLY128_OSTREAM_DEF;
use crate::common::SupportedArchitectureTest;
use crate::common::cli::ProcessedCli;
use crate::common::compare::compare_outputs;
use crate::common::gen_c::{write_main_cpp, write_mod_cpp};
Expand All @@ -28,13 +27,6 @@ pub struct ArmArchitectureTest {
cli_options: ProcessedCli,
}

fn chunk_info(intrinsic_count: usize) -> (usize, usize) {
let available_parallelism = std::thread::available_parallelism().unwrap().get();
let chunk_size = intrinsic_count.div_ceil(Ord::min(available_parallelism, intrinsic_count));

(chunk_size, intrinsic_count.div_ceil(chunk_size))
}

impl SupportedArchitectureTest for ArmArchitectureTest {
fn create(cli_options: ProcessedCli) -> Box<Self> {
let a32 = cli_options.target.contains("v7");
Expand Down Expand Up @@ -79,7 +71,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
.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.
Expand All @@ -97,11 +89,12 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
.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,
POLY128_OSTREAM_DEF,
Vec::from(platform_headers),
self.intrinsics.iter().map(|i| i.name.as_str()),
)
.unwrap();
Expand Down
44 changes: 23 additions & 21 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, 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 All @@ -40,7 +36,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len: Some(bl),
simd_len,
vec_len,
target,
metadata,
..
} = &self.0
{
Expand All @@ -50,7 +46,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 +102,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 +153,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 +169,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
bit_len,
simd_len: None,
vec_len: None,
target: target.to_string(),
metadata,
}))
}
}
Expand Down
8 changes: 5 additions & 3 deletions crates/intrinsic-test/src/common/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use std::ops::Range;
pub enum Constraint {
Equal(i64),
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(),
}
}
}
5 changes: 3 additions & 2 deletions 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 Expand Up @@ -140,12 +140,13 @@ pub fn write_main_cpp<'a>(
w: &mut impl std::io::Write,
architecture: &str,
arch_specific_definitions: &str,
headers: Vec<&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"] {
for header in headers.iter() {
writeln!(w, "#include <{header}>")?;
}

Expand Down
16 changes: 13 additions & 3 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,8 @@ pub struct IntrinsicType {
/// A value of `None` can be assumed to be 1 though.
pub vec_len: Option<u32>,

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

impl IntrinsicType {
Expand All @@ -133,7 +135,7 @@ impl IntrinsicType {
if let Some(bl) = self.bit_len {
bl
} else {
unreachable!("")
unreachable!("{}", self.kind)
}
}

Expand All @@ -153,6 +155,14 @@ impl IntrinsicType {
self.ptr
}

// pub fn set_bit_len(&mut self, value: Option<u32>) {
// self.bit_len = value;
// }

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 +332,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
7 changes: 7 additions & 0 deletions crates/intrinsic-test/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,10 @@ pub trait SupportedArchitectureTest {
fn build_rust_file(&self) -> bool;
fn compare_outputs(&self) -> bool;
}

pub fn chunk_info(intrinsic_count: usize) -> (usize, usize) {
let available_parallelism = std::thread::available_parallelism().unwrap().get();
let chunk_size = intrinsic_count.div_ceil(Ord::min(available_parallelism, intrinsic_count));

(chunk_size, intrinsic_count.div_ceil(chunk_size))
}
Loading
Loading