-
Notifications
You must be signed in to change notification settings - Fork 289
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
madhav-madhusoodanan
wants to merge
23
commits into
rust-lang:master
Choose a base branch
from
madhav-madhusoodanan:restructure-intrinsic-test-crate
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Re-organize intrinsic-test
to enable seamless addition of behaviour testing for more architectures
#1758
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8727056
Feat: Moved majority of the code to `arm` module.
madhav-madhusoodanan bef04d8
Chore: Added `SupportedArchitectureTest` trait which must be implemen…
madhav-madhusoodanan ce43975
chore: Added `ProcessedCli` to extract the logic to pre-process CLI s…
madhav-madhusoodanan 2cdb2ac
chore: separated common logic within file creations, compile_c, compi…
madhav-madhusoodanan 846ed3c
chore: code consolidation
madhav-madhusoodanan 0baf79e
chore: added match block in `src/main.rs`
madhav-madhusoodanan d8b4a94
fixed `too many files open` issue
madhav-madhusoodanan 4a4190e
maintaining special list of targets which need different execution co…
madhav-madhusoodanan 8cf3cf6
rename struct for naming consistency
madhav-madhusoodanan c581afc
test commit to check if `load_Values_c` can be dissociated from targe…
madhav-madhusoodanan 8ddbc03
added target field within `IntrinsicType` to perform target level che…
madhav-madhusoodanan 1aede3e
Updated `Argument::from_c` to remove `ArgPrep` specific argument
madhav-madhusoodanan 8740a0e
introduced generic types and code refactor
madhav-madhusoodanan dc4065f
Added a macro to simplify <Arch>IntrinsicType definitions
madhav-madhusoodanan b54d0b2
renamed `a64_only` data member in `Intrinsic` to `arch_tags`
madhav-madhusoodanan 36f7e01
Removed aarch64-be specific execution command for rust test files
madhav-madhusoodanan ae42764
moved the C compilation commands into a struct for easier handling
madhav-madhusoodanan cb62771
Added dynamic dispatch for easier management of `<arch>ArchitectureTe…
madhav-madhusoodanan 68959e3
code cleanup
madhav-madhusoodanan dc5c5a1
chore: file renaming
madhav-madhusoodanan 030ce32
feat: made constraint common
madhav-madhusoodanan 5ac0c71
fix: aarch64_be issues wthin compilation
madhav-madhusoodanan e72b4c1
moved more code generation functionality to `common`
madhav-madhusoodanan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
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 | ||
" | ||
) | ||
} | ||
|
||
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; | ||
}"#; | ||
|
||
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(stdarch_neon_f16)] | ||
"#; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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, | ||
}; | ||
|
||
base_intrinsictype_trait_def_macro! {ArmIntrinsicType} | ||
|
||
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> { | ||
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> { | ||
self.arguments.clone() | ||
} | ||
|
||
fn results(&self) -> ArmIntrinsicType { | ||
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::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(), | ||
) | ||
}; | ||
|
||
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 { "" }, | ||
) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use super::intrinsic::ArmIntrinsicType; | ||
use crate::common::argument::{Argument, ArgumentList}; | ||
use crate::common::constraint::Constraint; | ||
use crate::common::intrinsic::Intrinsic; | ||
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition}; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
use std::path::Path; | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(deny_unknown_fields)] | ||
struct ReturnType { | ||
value: String, | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
#[serde(untagged, deny_unknown_fields)] | ||
pub enum ArgPrep { | ||
Register { | ||
#[serde(rename = "register")] | ||
#[allow(dead_code)] | ||
reg: String, | ||
}, | ||
Immediate { | ||
#[serde(rename = "minimum")] | ||
min: i64, | ||
#[serde(rename = "maximum")] | ||
max: i64, | ||
}, | ||
Nothing {}, | ||
} | ||
|
||
impl TryFrom<Value> for ArgPrep { | ||
type Error = serde_json::Error; | ||
|
||
fn try_from(value: Value) -> Result<Self, Self::Error> { | ||
serde_json::from_value(value) | ||
} | ||
} | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct JsonIntrinsic { | ||
#[serde(rename = "SIMD_ISA")] | ||
simd_isa: String, | ||
name: String, | ||
arguments: Vec<String>, | ||
return_type: ReturnType, | ||
#[serde(rename = "Arguments_Preparation")] | ||
args_prep: Option<HashMap<String, Value>>, | ||
#[serde(rename = "Architectures")] | ||
architectures: Vec<String>, | ||
} | ||
|
||
pub fn get_neon_intrinsics( | ||
filename: &Path, | ||
target: &String, | ||
) -> Result<Vec<Intrinsic<ArmIntrinsicType>>, Box<dyn std::error::Error>> { | ||
let file = std::fs::File::open(filename)?; | ||
let reader = std::io::BufReader::new(file); | ||
let json: Vec<JsonIntrinsic> = serde_json::from_reader(reader).expect("Couldn't parse JSON"); | ||
|
||
let parsed = json | ||
.into_iter() | ||
.filter_map(|intr| { | ||
if intr.simd_isa == "Neon" { | ||
Some(json_to_intrinsic(intr, target).expect("Couldn't parse JSON")) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
Ok(parsed) | ||
} | ||
|
||
fn json_to_intrinsic( | ||
mut intr: JsonIntrinsic, | ||
target: &String, | ||
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> { | ||
let name = intr.name.replace(['[', ']'], ""); | ||
|
||
let results = ArmIntrinsicType::from_c(&intr.return_type.value, target)?; | ||
|
||
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 constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok()); | ||
|
||
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint); | ||
|
||
// The JSON doesn't list immediates as const | ||
let IntrinsicType { | ||
ref mut constant, .. | ||
} = arg.ty.0; | ||
if arg.name.starts_with("imm") { | ||
*constant = true | ||
} | ||
arg | ||
}) | ||
.collect(); | ||
|
||
let arguments = ArgumentList::<ArmIntrinsicType> { args }; | ||
|
||
Ok(Intrinsic { | ||
name, | ||
arguments, | ||
results: *results, | ||
arch_tags: intr.architectures, | ||
}) | ||
} | ||
|
||
/// ARM-specific | ||
impl TryFrom<ArgPrep> for Constraint { | ||
type Error = (); | ||
|
||
fn try_from(prep: ArgPrep) -> Result<Self, Self::Error> { | ||
let parsed_ints = match prep { | ||
ArgPrep::Immediate { min, max } => Ok((min, max)), | ||
_ => Err(()), | ||
}; | ||
if let Ok((min, max)) = parsed_ints { | ||
if min == max { | ||
Ok(Constraint::Equal(min)) | ||
} else { | ||
Ok(Constraint::Range(min..max + 1)) | ||
} | ||
} else { | ||
Err(()) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see