Skip to content

intrinsic-test: Adding x86 behavioural testing. #1894

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 14 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
53 changes: 52 additions & 1 deletion Cargo.lock

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

12 changes: 12 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,15 @@ 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
3 changes: 3 additions & 0 deletions crates/intrinsic-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ 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"
regex = "1.11.1"
4 changes: 4 additions & 0 deletions crates/intrinsic-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ extern crate log;

mod arm;
mod common;
mod x86;

use arm::ArmArchitectureTest;
use common::SupportedArchitectureTest;
use common::cli::{Cli, ProcessedCli};
use x86::X86ArchitectureTest;

fn main() {
pretty_env_logger::init();
Expand All @@ -21,6 +23,8 @@ fn main() {
Some(ArmArchitectureTest::create(processed_cli_options))
}

"x86_64-unknown-linux-gnu" => Some(X86ArchitectureTest::create(processed_cli_options)),

_ => None,
};

Expand Down
38 changes: 38 additions & 0 deletions crates/intrinsic-test/src/x86/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::common::cli::ProcessedCli;
use crate::common::compile_c::{CompilationCommandBuilder, CppCompilation};

pub fn build_cpp_compilation(config: &ProcessedCli) -> Option<CppCompilation> {
let cpp_compiler = config.cpp_compiler.as_ref()?;

// -ffp-contract=off emulates Rust's approach of not fusing separate mul-add operations
let mut command = CompilationCommandBuilder::new()
.add_arch_flags([
"avx",
"avx2",
"avx512f",
"avx512cd",
"avx512dq",
"avx512vl",
"avx512bw",
"avx512bf16",
"avx512bitalg",
"lzcnt",
"popcnt",
"adx",
"aes",
])
.set_compiler(cpp_compiler)
.set_target(&config.target)
.set_opt_level("2")
.set_cxx_toolchain_dir(config.cxx_toolchain_dir.as_deref())
.set_project_root("c_programs")
.add_extra_flags(vec!["-ffp-contract=off", "-Wno-narrowing"]);

if !cpp_compiler.contains("clang") {
command = command.add_extra_flag("-flax-vector-conversions");
}

let cpp_compiler = command.into_cpp_compilation();

Some(cpp_compiler)
}
25 changes: 25 additions & 0 deletions crates/intrinsic-test/src/x86/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pub fn build_notices(line_prefix: &str) -> String {
format!(
"\
{line_prefix}This is a transient test file, not intended for distribution. Some aspects of the
{line_prefix}test are derived from an XML specification, published under the same license as the
{line_prefix}`intrinsic-test` crate.\n
"
)
}

// Format f16 values (and vectors containing them) in a way that is consistent with C.
pub const F16_FORMATTING_DEF: &str = r#"
#[repr(transparent)]
struct Hex<T>(T);
"#;

pub const X86_CONFIGURATIONS: &str = r#"
#![cfg_attr(target_arch = "x86", feature(stdarch_x86_avx512_bf16))]
#![cfg_attr(target_arch = "x86", feature(stdarch_x86_avx512_f16))]
#![cfg_attr(target_arch = "x86", feature(stdarch_x86_rtm))]
#![cfg_attr(target_arch = "x86", feature(stdarch_x86_rtm))]
#![cfg_attr(target_arch = "x86_64", feature(x86_amx_intrinsics))]
#![cfg_attr(target_arch = "x86_64", feature(stdarch_x86_avx512_f16))]
#![feature(fmt_helpers_for_derive)]
"#;
20 changes: 20 additions & 0 deletions crates/intrinsic-test/src/x86/constraint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::common::constraint::Constraint;

pub fn map_constraints(imm_type: &String) -> Option<Constraint> {
match imm_type.as_str() {
"_MM_FROUND" => Some(Constraint::Range(0..4)),
"_MM_INDEX_SCALE" => Some(Constraint::Set(vec![1, 2, 4, 8])),
"_MM_CMPINT" => Some(Constraint::Range(0..8)),
"_MM_REDUCE" => Some(Constraint::Range(0..8)),
"_MM_FROUND_SAE" => Some(Constraint::Range(0..8)),
"_MM_MANTISSA_NORM" => Some(Constraint::Range(0..4)),
"_MM_MANTISSA_NORM_ENUM" => Some(Constraint::Range(0..4)),
"_MM_MANTISSA_SIGN" => Some(Constraint::Range(0..3)),
"_MM_PERM" => Some(Constraint::Range(0..256)),
"_MM_PERM_ENUM" => Some(Constraint::Range(0..256)),
"_MM_CMPINT_ENUM" => Some(Constraint::Range(0..8)),
"_MM_ROUND_MODE" => Some(Constraint::Set(vec![0, 0x2000, 0x4000, 0x6000])),
"_CMP_" => Some(Constraint::Range(0..32)),
_ => None,
}
}
109 changes: 109 additions & 0 deletions crates/intrinsic-test/src/x86/intrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
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 crate::x86::xml_parser::Parameter;
use std::ops::{Deref, DerefMut};

#[derive(Debug, Clone, PartialEq)]
pub struct X86IntrinsicType {
pub data: IntrinsicType,
pub param: Parameter,
}

impl Deref for X86IntrinsicType {
type Target = IntrinsicType;

fn deref(&self) -> &Self::Target {
&self.data
}
}

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

impl IntrinsicDefinition<X86IntrinsicType> for Intrinsic<X86IntrinsicType> {
fn arguments(&self) -> ArgumentList<X86IntrinsicType> {
self.arguments.clone()
}

fn results(&self) -> X86IntrinsicType {
self.results.clone()
}

fn name(&self) -> String {
self.name.clone()
}

/// Generates a std::cout for the intrinsics results that will match the
/// rust debug output format for the return type. The generated line assumes
/// there is an int i in scope which is the current pass number.
fn print_result_c(&self, indentation: Indentation, additional: &str) -> String {
let lanes = if self.results().num_vectors() > 1 {
(0..self.results().num_vectors())
.map(|vector| {
format!(
r#""{ty}(" << {lanes} << ")""#,
ty = self.results().c_single_vector_type(),
lanes = (0..self.results().num_lanes())
.map(move |idx| -> std::string::String {
format!(
"{cast}{lane_fn}(__return_value.val[{vector}], {lane})",
cast = self.results().c_promotion(),
lane_fn = self.results().get_lane_function(),
lane = idx,
vector = vector,
)
})
.collect::<Vec<_>>()
.join(r#" << ", " << "#)
)
})
.collect::<Vec<_>>()
.join(r#" << ", " << "#)
} else if self.results().num_lanes() > 1 {
(0..self.results().num_lanes())
.map(|idx| -> std::string::String {
format!(
"{cast}{lane_fn}(__return_value, {lane})",
cast = self.results().c_promotion(),
lane_fn = self.results().get_lane_function(),
lane = idx
)
})
.collect::<Vec<_>>()
.join(r#" << ", " << "#)
} else {
format!(
"{promote}cast<{cast}>(__return_value)",
cast = match self.results.kind() {
TypeKind::Void => "void".to_string(),
TypeKind::Float if self.results().inner_size() == 64 => "double".to_string(),
TypeKind::Float if self.results().inner_size() == 32 => "float".to_string(),
// TypeKind::Float if self.results().inner_size() == 16 => "float16_t".to_string(),
// TypeKind::Int(true) if self.results().inner_size() == 64 => "long".to_string(),
// TypeKind::Int(false) if self.results().inner_size() == 64 => "unsigned long".to_string(),
// TypeKind::Int(true) if self.results().inner_size() == 32 => "int".to_string(),
// TypeKind::Int(false) if self.results().inner_size() == 32 => "unsigned int".to_string(),
// TypeKind::Int(true) if self.results().inner_size() == 16 => "short".to_string(),
// TypeKind::Int(false) if self.results().inner_size() == 16 => "unsigned short".to_string(),
_ => self.results.c_scalar_type(),
},
promote = self.results().c_promotion(),
)
};

format!(
r#"{indentation}std::cout << "Result {additional}-" << i+1 << ": {ty}" << std::fixed << std::setprecision(150) << {lanes} << "{close}" << std::endl;"#,
ty = if self.results().is_simd() {
format!("{}(", self.results().c_type())
} else {
String::from("")
},
close = if self.results.is_simd() { ")" } else { "" },
)
}
}
Loading
Loading