-
Notifications
You must be signed in to change notification settings - Fork 287
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
20
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
20 commits
Select commit
Hold shift + click to select a range
bd0a675
Feat: Moved majority of the code to `arm` module.
madhav-madhusoodanan 7ff8497
Chore: Added `SupportedArchitectureTest` trait which must be implemen…
madhav-madhusoodanan 399f37b
chore: Added `ProcessedCli` to extract the logic to pre-process CLI s…
madhav-madhusoodanan 3e46d80
chore: separated common logic within file creations, compile_c, compi…
madhav-madhusoodanan 2777ceb
chore: code consolidation
madhav-madhusoodanan a2ce02c
chore: added match block in `src/main.rs`
madhav-madhusoodanan 5b20da3
fixed `too many files open` issue
madhav-madhusoodanan a6c6e5e
maintaining special list of targets which need different execution co…
madhav-madhusoodanan 66d21bd
rename struct for naming consistency
madhav-madhusoodanan 7b1b684
test commit to check if `load_Values_c` can be dissociated from targe…
madhav-madhusoodanan 90249a3
added target field within `IntrinsicType` to perform target level che…
madhav-madhusoodanan 1791b35
Updated `Argument::from_c` to remove `ArgPrep` specific argument
madhav-madhusoodanan 2058ab6
introduced generic types and code refactor
madhav-madhusoodanan 358016a
Added a macro to simplify <Arch>IntrinsicType definitions
madhav-madhusoodanan cc615b6
renamed `a64_only` data member in `Intrinsic` to `arch_tags`
madhav-madhusoodanan d395e9c
Removed aarch64-be specific execution command for rust test files
madhav-madhusoodanan 08325f3
moved the C compilation commands into a struct for easier handling
madhav-madhusoodanan 420f2ee
Added dynamic dispatch for easier management of `<arch>ArchitectureTe…
madhav-madhusoodanan b86b969
code cleanup
madhav-madhusoodanan f4650c0
chore: file renaming
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,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,60 @@ | ||
use super::json_parser::ArgPrep; | ||
|
||
use crate::common::argument::MetadataDefinition; | ||
use serde::Deserialize; | ||
use serde_json::Value; | ||
use std::ops::Range; | ||
|
||
#[derive(Debug, PartialEq, Clone, Deserialize)] | ||
pub enum Constraint { | ||
Equal(i64), | ||
Range(Range<i64>), | ||
} | ||
|
||
impl Constraint { | ||
pub fn to_range(&self) -> Range<i64> { | ||
match self { | ||
Constraint::Equal(eq) => *eq..*eq + 1, | ||
Constraint::Range(range) => range.clone(), | ||
} | ||
} | ||
} | ||
|
||
impl MetadataDefinition for Constraint { | ||
fn from_metadata(metadata: Option<Value>) -> Vec<Box<Self>> { | ||
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| { | ||
if let Value::Object(_) = a { | ||
a.try_into().ok() | ||
} else { | ||
None | ||
} | ||
}); | ||
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok()); | ||
vec![constraint] | ||
.into_iter() | ||
.filter_map(|a| a) | ||
.map(|a| Box::new(a)) | ||
.collect() | ||
} | ||
} | ||
|
||
/// 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(()) | ||
} | ||
} | ||
} |
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,273 @@ | ||
use super::config::{AARCH_CONFIGURATIONS, POLY128_OSTREAM_DEF, build_notices}; | ||
use super::intrinsic::ArmIntrinsicType; | ||
use crate::arm::constraint::Constraint; | ||
use crate::common::argument::Argument; | ||
use crate::common::compile_c::CompilationCommandBuilder; | ||
use crate::common::gen_c::{compile_c, create_c_filenames, generate_c_program}; | ||
use crate::common::gen_rust::{compile_rust, create_rust_filenames, generate_rust_program}; | ||
use crate::common::indentation::Indentation; | ||
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition}; | ||
use crate::common::intrinsic_types::IntrinsicTypeDefinition; | ||
use crate::common::write_file; | ||
use itertools::Itertools; | ||
use rayon::prelude::*; | ||
|
||
// The number of times each intrinsic will be called. | ||
const PASSES: u32 = 20; | ||
|
||
fn gen_code_c( | ||
indentation: Indentation, | ||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>, | ||
constraints: &[&Argument<ArmIntrinsicType, Constraint>], | ||
name: String, | ||
target: &str, | ||
) -> String { | ||
if let Some((current, constraints)) = constraints.split_last() { | ||
let range = current | ||
.metadata | ||
.iter() | ||
.map(|c| c.to_range()) | ||
.flat_map(|r| r.into_iter()); | ||
|
||
let body_indentation = indentation.nested(); | ||
range | ||
.map(|i| { | ||
format!( | ||
"{indentation}{{\n\ | ||
{body_indentation}{ty} {name} = {val};\n\ | ||
{pass}\n\ | ||
{indentation}}}", | ||
name = current.name, | ||
ty = current.ty.c_type(), | ||
val = i, | ||
pass = gen_code_c( | ||
body_indentation, | ||
intrinsic, | ||
constraints, | ||
format!("{name}-{i}"), | ||
target, | ||
) | ||
) | ||
}) | ||
.join("\n") | ||
} else { | ||
intrinsic.generate_loop_c(indentation, &name, PASSES, target) | ||
} | ||
} | ||
|
||
fn generate_c_program_arm( | ||
header_files: &[&str], | ||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>, | ||
target: &str, | ||
) -> String { | ||
let constraints = intrinsic | ||
.arguments | ||
.iter() | ||
.filter(|&i| i.has_constraint()) | ||
.collect_vec(); | ||
|
||
let indentation = Indentation::default(); | ||
generate_c_program( | ||
build_notices("// ").as_str(), | ||
header_files, | ||
"aarch64", | ||
&[POLY128_OSTREAM_DEF], | ||
intrinsic | ||
.arguments | ||
.gen_arglists_c(indentation, PASSES) | ||
.as_str(), | ||
gen_code_c( | ||
indentation.nested(), | ||
intrinsic, | ||
constraints.as_slice(), | ||
Default::default(), | ||
target, | ||
) | ||
.as_str(), | ||
) | ||
} | ||
|
||
fn gen_code_rust( | ||
indentation: Indentation, | ||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>, | ||
constraints: &[&Argument<ArmIntrinsicType, Constraint>], | ||
name: String, | ||
) -> String { | ||
if let Some((current, constraints)) = constraints.split_last() { | ||
let range = current | ||
.metadata | ||
.iter() | ||
.map(|c| c.to_range()) | ||
.flat_map(|r| r.into_iter()); | ||
|
||
let body_indentation = indentation.nested(); | ||
range | ||
.map(|i| { | ||
format!( | ||
"{indentation}{{\n\ | ||
{body_indentation}const {name}: {ty} = {val};\n\ | ||
{pass}\n\ | ||
{indentation}}}", | ||
name = current.name, | ||
ty = current.ty.rust_type(), | ||
val = i, | ||
pass = gen_code_rust( | ||
body_indentation, | ||
intrinsic, | ||
constraints, | ||
format!("{name}-{i}") | ||
) | ||
) | ||
}) | ||
.join("\n") | ||
} else { | ||
intrinsic.generate_loop_rust(indentation, &name, PASSES) | ||
} | ||
} | ||
|
||
fn generate_rust_program_arm( | ||
intrinsic: &Intrinsic<ArmIntrinsicType, Constraint>, | ||
target: &str, | ||
) -> String { | ||
let constraints = intrinsic | ||
.arguments | ||
.iter() | ||
.filter(|i| i.has_constraint()) | ||
.collect_vec(); | ||
|
||
let indentation = Indentation::default(); | ||
let final_target = if target.contains("v7") { | ||
"arm" | ||
} else { | ||
"aarch64" | ||
}; | ||
generate_rust_program( | ||
build_notices("// ").as_str(), | ||
AARCH_CONFIGURATIONS, | ||
final_target, | ||
intrinsic | ||
.arguments | ||
.gen_arglists_rust(indentation.nested(), PASSES) | ||
.as_str(), | ||
gen_code_rust( | ||
indentation.nested(), | ||
intrinsic, | ||
&constraints, | ||
Default::default(), | ||
) | ||
.as_str(), | ||
) | ||
} | ||
|
||
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 == "aarch64_be-unknown-linux-gnu" { | ||
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) | ||
} | ||
|
||
pub fn build_c( | ||
intrinsics: &Vec<Intrinsic<ArmIntrinsicType, Constraint>>, | ||
compiler: Option<&str>, | ||
target: &str, | ||
cxx_toolchain_dir: Option<&str>, | ||
) -> bool { | ||
let intrinsics_name_list = intrinsics | ||
.par_iter() | ||
.map(|i| i.name.clone()) | ||
.collect::<Vec<_>>(); | ||
let filename_mapping = create_c_filenames(&intrinsics_name_list); | ||
|
||
intrinsics.par_iter().for_each(|i| { | ||
let c_code = generate_c_program_arm(&["arm_neon.h", "arm_acle.h", "arm_fp16.h"], i, target); | ||
match filename_mapping.get(&i.name) { | ||
Some(filename) => write_file(filename, c_code), | ||
None => {} | ||
}; | ||
}); | ||
|
||
match compiler { | ||
None => true, | ||
Some(compiler) => compile_c_arm(&intrinsics_name_list, compiler, target, cxx_toolchain_dir), | ||
} | ||
} | ||
|
||
pub fn build_rust( | ||
intrinsics: &[Intrinsic<ArmIntrinsicType, Constraint>], | ||
toolchain: Option<&str>, | ||
target: &str, | ||
linker: Option<&str>, | ||
) -> bool { | ||
let intrinsics_name_list = intrinsics | ||
.par_iter() | ||
.map(|i| i.name.clone()) | ||
.collect::<Vec<_>>(); | ||
let filename_mapping = create_rust_filenames(&intrinsics_name_list); | ||
|
||
intrinsics.par_iter().for_each(|i| { | ||
let rust_code = generate_rust_program_arm(i, target); | ||
match filename_mapping.get(&i.name) { | ||
Some(filename) => write_file(filename, rust_code), | ||
None => {} | ||
} | ||
}); | ||
|
||
let intrinsics_name_list = intrinsics.iter().map(|i| i.name.as_str()).collect_vec(); | ||
|
||
compile_rust(&intrinsics_name_list, toolchain, target, linker) | ||
} |
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.
I think it might be best for the
gen_code_c
andgen_code_rust
functions to stay module-specific. Would this be a good idea @Amanieu @adamgemmell ?Constraints seem like a very ARM thing.
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 think overall you should try to keep as much generic as possible until you find an example of an intrinsic that wouldn't work otherwise - no point trying to guess what the thousands of intrinsics will want at this point.
If an architecture doesn't have any Constraints you could pass it an empty slice and that would be fine. Constraints are the "top-level" of generated code per intrinsic so it would be a bit awkward for the generator to go to platform-specific code and then jump back to generic code that might need to be aware of what constraints were set up and how they were named.
Constraints really are for anything const (i.e. needs to end up as an immediate at the assembly level), including enums. Scanning x86_64 intrinsics I found a few examples that would require a constraint (if you were to keep this model around):
I think what you end up doing depends on how your intrinsic reference is written too.
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
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.
Okay now I understand constraints. I was under the impression that they described something about the register lanes or the like.
Really, they just mention the possible min-max values of the argument.
I’ll make
Constraint
common too.