Skip to content

Re-organize intrinsic-test to enable seamless addition of behaviour testing for more architectures #1758

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

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bf7aee7
Feat: Moved majority of the code to `arm` module.
madhav-madhusoodanan Mar 25, 2025
4c25aa7
Chore: Added `SupportedArchitectureTest` trait which must be implemen…
madhav-madhusoodanan Mar 25, 2025
5b9f0e5
chore: Added `ProcessedCli` to extract the logic to pre-process CLI s…
madhav-madhusoodanan Mar 26, 2025
bf7812c
chore: separated common logic within file creations, compile_c, compi…
madhav-madhusoodanan Mar 27, 2025
7118074
chore: code consolidation
madhav-madhusoodanan Mar 27, 2025
0b6d424
chore: added match block in `src/main.rs`
madhav-madhusoodanan Mar 27, 2025
b215673
fixed `too many files open` issue
madhav-madhusoodanan Mar 30, 2025
8b1a734
maintaining special list of targets which need different execution co…
madhav-madhusoodanan Apr 2, 2025
18e49d6
rename struct for naming consistency
madhav-madhusoodanan Apr 2, 2025
83110d3
test commit to check if `load_Values_c` can be dissociated from targe…
madhav-madhusoodanan Apr 13, 2025
0d4be1c
added target field within `IntrinsicType` to perform target level che…
madhav-madhusoodanan Apr 14, 2025
02a69bd
Updated `Argument::from_c` to remove `ArgPrep` specific argument
madhav-madhusoodanan Apr 14, 2025
a4b46a7
introduced generic types and code refactor
madhav-madhusoodanan Apr 16, 2025
e570aad
Added a macro to simplify <Arch>IntrinsicType definitions
madhav-madhusoodanan Apr 16, 2025
a668076
renamed `a64_only` data member in `Intrinsic` to `arch_tags`
madhav-madhusoodanan Apr 16, 2025
f1b2931
Removed aarch64-be specific execution command for rust test files
madhav-madhusoodanan Apr 17, 2025
fb84942
moved the C compilation commands into a struct for easier handling
madhav-madhusoodanan Apr 18, 2025
757dd74
Added dynamic dispatch for easier management of `<arch>ArchitectureTe…
madhav-madhusoodanan Apr 19, 2025
4935acb
code cleanup
madhav-madhusoodanan Apr 19, 2025
c1d2d77
chore: file renaming
madhav-madhusoodanan Apr 23, 2025
90aff41
feat: made constraint common
madhav-madhusoodanan Apr 28, 2025
941637c
fix: aarch64_be issues wthin compilation
madhav-madhusoodanan May 4, 2025
a4f084e
moved more code generation functionality to `common`
madhav-madhusoodanan May 10, 2025
66a88fe
feat: merging changes related to f16 formatting
madhav-madhusoodanan May 21, 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
64 changes: 64 additions & 0 deletions crates/intrinsic-test/src/arm/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::common::compile_c::CompilationCommandBuilder;
use crate::common::gen_c::compile_c;

pub fn compile_c_arm(
intrinsics_name_list: &Vec<String>,
compiler: &str,
target: &str,
cxx_toolchain_dir: Option<&str>,
) -> bool {
// -ffp-contract=off emulates Rust's approach of not fusing separate mul-add operations
let mut command = CompilationCommandBuilder::new()
.add_arch_flags(vec!["armv8.6-a", "crypto", "crc", "dotprod", "fp16"])
.set_compiler(compiler)
.set_target(target)
.set_opt_level("2")
.set_cxx_toolchain_dir(cxx_toolchain_dir)
.set_project_root("c_programs")
.add_extra_flags(vec!["-ffp-contract=off", "-Wno-narrowing"]);

if !target.contains("v7") {
command = command.add_arch_flags(vec!["faminmax", "lut", "sha3"]);
}

/*
* clang++ cannot link an aarch64_be object file, so we invoke
* aarch64_be-unknown-linux-gnu's C++ linker. This ensures that we
* are testing the intrinsics against LLVM.
*
* Note: setting `--sysroot=<...>` which is the obvious thing to do
* does not work as it gets caught up with `#include_next <stdlib.h>`
* not existing...
*/
if target.contains("aarch64_be") {
command = command
.set_linker(
cxx_toolchain_dir.unwrap_or("").to_string() + "/bin/aarch64_be-none-linux-gnu-g++",
)
.set_include_paths(vec![
"/include",
"/aarch64_be-none-linux-gnu/include",
"/aarch64_be-none-linux-gnu/include/c++/14.2.1",
"/aarch64_be-none-linux-gnu/include/c++/14.2.1/aarch64_be-none-linux-gnu",
"/aarch64_be-none-linux-gnu/include/c++/14.2.1/backward",
"/aarch64_be-none-linux-gnu/libc/usr/include",
]);
}

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

let compiler_commands = intrinsics_name_list
.iter()
.map(|intrinsic_name| {
command
.clone()
.set_input_name(intrinsic_name)
.set_output_name(intrinsic_name)
.to_string()
})
.collect::<Vec<_>>();

compile_c(&compiler_commands)
}
122 changes: 122 additions & 0 deletions crates/intrinsic-test/src/arm/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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 a JSON specification, published under the same license as the
{line_prefix}`intrinsic-test` crate.\n
"
)
}
Comment on lines +1 to +9

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, how important is this notice?

I'm totally in favour of documentation, but seeing as the files that are being generated are purely for testing purposes I thought it'd be best to clarify.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would like to keep this notice for Arm intrinsics if possible. For us it's not technically mandatory to include, but it reduces risk for us.

I'm not in a position to speak for what's best for other architectures unfortunately.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see


pub const POLY128_OSTREAM_DEF: &str = r#"std::ostream& operator<<(std::ostream& os, poly128_t value) {
std::stringstream temp;
do {
int n = value % 10;
value /= 10;
temp << n;
} while (value != 0);
std::string tempstr(temp.str());
std::string res(tempstr.rbegin(), tempstr.rend());
os << res;
return os;
}"#;

// Format f16 values (and vectors containing them) in a way that is consistent with C.
pub const F16_FORMATTING_DEF: &str = r#"
/// Used to continue `Debug`ging SIMD types as `MySimd(1, 2, 3, 4)`, as they
/// were before moving to array-based simd.
#[inline]
fn debug_simd_finish<T: core::fmt::Debug, const N: usize>(
formatter: &mut core::fmt::Formatter<'_>,
type_name: &str,
array: &[T; N],
) -> core::fmt::Result {
core::fmt::Formatter::debug_tuple_fields_finish(
formatter,
type_name,
&core::array::from_fn::<&dyn core::fmt::Debug, N, _>(|i| &array[i]),
)
}

#[repr(transparent)]
struct Hex<T>(T);

impl<T: DebugHexF16> core::fmt::Debug for Hex<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
<T as DebugHexF16>::fmt(&self.0, f)
}
}

fn debug_f16<T: DebugHexF16>(x: T) -> impl core::fmt::Debug {
Hex(x)
}

trait DebugHexF16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result;
}

impl DebugHexF16 for f16 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{:#06x?}", self.to_bits())
}
}

impl DebugHexF16 for float16x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 4]>(*self) };
debug_simd_finish(f, "float16x4_t", &array)
}
}

impl DebugHexF16 for float16x8_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let array = unsafe { core::mem::transmute::<_, [Hex<f16>; 8]>(*self) };
debug_simd_finish(f, "float16x8_t", &array)
}
}

impl DebugHexF16 for float16x4x2_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x2_t", &[Hex(self.0), Hex(self.1)])
}
}
impl DebugHexF16 for float16x4x3_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
}
}
impl DebugHexF16 for float16x4x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x4x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
}
}

impl DebugHexF16 for float16x8x2_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x2_t", &[Hex(self.0), Hex(self.1)])
}
}
impl DebugHexF16 for float16x8x3_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x3_t", &[Hex(self.0), Hex(self.1), Hex(self.2)])
}
}
impl DebugHexF16 for float16x8x4_t {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
debug_simd_finish(f, "float16x8x4_t", &[Hex(self.0), Hex(self.1), Hex(self.2), Hex(self.3)])
}
}
"#;

pub const AARCH_CONFIGURATIONS: &str = r#"
#![cfg_attr(target_arch = "arm", feature(stdarch_arm_neon_intrinsics))]
#![cfg_attr(target_arch = "arm", feature(stdarch_aarch32_crc32))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_fcma))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_dotprod))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_i8mm))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sha3))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_sm4))]
#![cfg_attr(any(target_arch = "aarch64", target_arch = "arm64ec"), feature(stdarch_neon_ftts))]
#![feature(fmt_helpers_for_derive)]
#![feature(stdarch_neon_f16)]
"#;
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
use crate::format::Indentation;
use crate::types::{IntrinsicType, TypeKind};
use crate::base_intrinsictype_trait_def_macro;
use crate::common::argument::ArgumentList;
use crate::common::cli::Language;
use crate::common::indentation::Indentation;
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
use crate::common::intrinsic_helpers::{
BaseIntrinsicTypeDefinition, IntrinsicTypeDefinition, TypeKind,
};

use super::argument::ArgumentList;
base_intrinsictype_trait_def_macro! {ArmIntrinsicType}

/// An intrinsic
#[derive(Debug, PartialEq, Clone)]
pub struct Intrinsic {
/// The function name of this intrinsic.
pub name: String,

/// Any arguments for this intrinsic.
pub arguments: ArgumentList,
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {
self.arguments.clone()
}

/// The return type of this intrinsic.
pub results: IntrinsicType,
fn results(&self) -> ArmIntrinsicType {
self.results.clone()
}

/// Whether this intrinsic is only available on A64.
pub a64_only: bool,
}
fn name(&self) -> String {
self.name.clone()
}

impl Intrinsic {
/// 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.
pub fn print_result_c(&self, indentation: Indentation, additional: &str) -> String {
let lanes = if self.results.num_vectors() > 1 {
(0..self.results.num_vectors())
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())
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(),
cast = self.results().c_promotion(),
lane_fn = self.results().get_lane_function(),
lane = idx,
vector = vector,
)
Expand All @@ -46,13 +48,13 @@ impl Intrinsic {
})
.collect::<Vec<_>>()
.join(r#" << ", " << "#)
} else if self.results.num_lanes() > 1 {
(0..self.results.num_lanes())
} 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(),
cast = self.results().c_promotion(),
lane_fn = self.results().get_lane_function(),
lane = idx
)
})
Expand All @@ -62,37 +64,35 @@ impl Intrinsic {
format!(
"{promote}cast<{cast}>(__return_value)",
cast = match self.results.kind() {
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::Poly => format!("poly{}_t", self.results.inner_size()),
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::Poly => format!("poly{}_t", self.results().inner_size()),
ty => todo!("print_result_c - Unknown type: {:#?}", ty),
},
promote = self.results.c_promotion(),
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())
ty = if self.results().is_simd() {
format!("{}(", self.results().c_type())
} else {
String::from("")
},
close = if self.results.is_simd() { ")" } else { "" },
lanes = lanes,
additional = additional,
)
}

pub fn generate_loop_c(
fn generate_loop_c(
&self,
indentation: Indentation,
additional: &str,
passes: u32,
target: &str,
_target: &str,
) -> String {
let body_indentation = indentation.nested();
format!(
Expand All @@ -101,14 +101,14 @@ impl Intrinsic {
{body_indentation}auto __return_value = {intrinsic_call}({args});\n\
{print_result}\n\
{indentation}}}",
loaded_args = self.arguments.load_values_c(body_indentation, target),
loaded_args = self.arguments.load_values_c(body_indentation),
intrinsic_call = self.name,
args = self.arguments.as_call_param_c(),
print_result = self.print_result_c(body_indentation, additional)
)
}

pub fn generate_loop_rust(
fn generate_loop_rust(
&self,
indentation: Indentation,
additional: &str,
Expand Down Expand Up @@ -146,7 +146,6 @@ impl Intrinsic {
intrinsic_call = self.name,
const = constraints,
args = self.arguments.as_call_param_rust(),
additional = additional,
)
}
}
Loading