Skip to content

Commit f2b3b5d

Browse files
Sharpening the parsing logic:
1. filtered any intrinsic arguments where the only parameter is of type "void" 2. Better parsing for intrinsic return type 3. Filtering intrinsics that are difficult to test (void returns) 4. Reduced the variants in TypeKind (which differed only in bit_len)
1 parent 2934899 commit f2b3b5d

File tree

5 files changed

+190
-44
lines changed

5 files changed

+190
-44
lines changed

crates/intrinsic-test/src/common/intrinsic_helpers.rs

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ use super::values::value_for_array;
1212
pub enum TypeKind {
1313
BFloat,
1414
Float,
15-
Double,
1615

1716
// if signed, then the inner value is true
1817
Int(bool),
1918
Char(bool),
20-
Short(bool),
2119
Poly,
2220
Void,
2321
}
@@ -29,9 +27,8 @@ impl FromStr for TypeKind {
2927
match s {
3028
"bfloat" => Ok(Self::BFloat),
3129
"float" => Ok(Self::Float),
32-
"double" => Ok(Self::Double),
33-
"int" | "long" => Ok(Self::Int(true)),
34-
"short" => Ok(Self::Short(true)),
30+
"double" => Ok(Self::Float),
31+
"int" | "long" | "short" => Ok(Self::Int(true)),
3532
"poly" => Ok(Self::Poly),
3633
"char" => Ok(Self::Char(true)),
3734
"uint" | "unsigned" => Ok(Self::Int(false)),
@@ -49,15 +46,13 @@ impl fmt::Display for TypeKind {
4946
match self {
5047
Self::BFloat => "bfloat",
5148
Self::Float => "float",
52-
Self::Double => "double",
49+
// Self::Double => "double",
5350
Self::Int(true) => "int",
5451
Self::Int(false) => "uint",
5552
Self::Poly => "poly",
5653
Self::Void => "void",
5754
Self::Char(true) => "char",
5855
Self::Char(false) => "unsigned char",
59-
Self::Short(true) => "short",
60-
Self::Short(false) => "unsigned short",
6156
}
6257
)
6358
}
@@ -72,7 +67,6 @@ impl TypeKind {
7267
Self::Int(false) => "uint",
7368
Self::Poly => "poly",
7469
Self::Char(true) => "char",
75-
Self::Double => "double",
7670
_ => unreachable!("Not used: {:#?}", self),
7771
}
7872
}
@@ -126,7 +120,7 @@ impl IntrinsicType {
126120
if let Some(bl) = self.bit_len {
127121
bl
128122
} else {
129-
unreachable!("")
123+
unreachable!("{}", self.kind)
130124
}
131125
}
132126

@@ -159,11 +153,21 @@ impl IntrinsicType {
159153
}
160154

161155
pub fn c_scalar_type(&self) -> String {
162-
format!(
163-
"{prefix}{bits}_t",
164-
prefix = self.kind().c_prefix(),
165-
bits = self.inner_size()
166-
)
156+
match self {
157+
IntrinsicType {
158+
kind: TypeKind::Void,
159+
..
160+
} => String::from("void"),
161+
IntrinsicType {
162+
kind: TypeKind::Char(_),
163+
..
164+
} => String::from("char"),
165+
_ => format!(
166+
"{prefix}{bits}_t",
167+
prefix = self.kind().c_prefix(),
168+
bits = self.inner_size()
169+
),
170+
}
167171
}
168172

169173
pub fn rust_scalar_type(&self) -> String {
@@ -198,6 +202,21 @@ impl IntrinsicType {
198202
128 => "",
199203
_ => panic!("invalid bit_len"),
200204
},
205+
IntrinsicType {
206+
kind: TypeKind::Float,
207+
bit_len: Some(bit_len),
208+
..
209+
} => match bit_len {
210+
16 => "(float16_t)",
211+
32 => "(float)",
212+
64 => "(double)",
213+
128 => "",
214+
_ => panic!("invalid bit_len"),
215+
},
216+
IntrinsicType {
217+
kind: TypeKind::Char(_),
218+
..
219+
} => "(char)",
201220
_ => "",
202221
}
203222
}
@@ -271,6 +290,30 @@ impl IntrinsicType {
271290
)))
272291
)
273292
}
293+
IntrinsicType {
294+
kind: TypeKind::Void,
295+
bit_len: Some(bit_len),
296+
constant: true,
297+
ptr_constant: false,
298+
simd_len,
299+
vec_len,
300+
..
301+
} => {
302+
let (prefix, cast_prefix, cast_suffix, suffix) = match (language) {
303+
(&Language::Rust) => ("[", "(", ")", "]"),
304+
(&Language::C) => ("{", "void*(", ")", "}"),
305+
_ => unreachable!(),
306+
};
307+
format!(
308+
"{prefix}\n{body}\n{indentation}{suffix}",
309+
body = (0..(simd_len.unwrap_or(1) * vec_len.unwrap_or(1) + loads - 1))
310+
.format_with(",\n", |i, fmt| fmt(&format_args!(
311+
"{indentation}{cast_prefix}{src:#x}{cast_suffix}",
312+
indentation = indentation.nested(),
313+
src = value_for_array(*bit_len, i)
314+
)))
315+
)
316+
}
274317
_ => unimplemented!("populate random: {:#?}", self),
275318
}
276319
}

crates/intrinsic-test/src/x86/intrinsic.rs

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::common::argument::ArgumentList;
22
use crate::common::indentation::Indentation;
33
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
4-
use crate::common::intrinsic_helpers::IntrinsicType;
4+
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
55
use std::ops::{Deref, DerefMut};
66

77
#[derive(Debug, Clone, PartialEq)]
@@ -38,7 +38,69 @@ impl IntrinsicDefinition<X86IntrinsicType> for Intrinsic<X86IntrinsicType> {
3838
/// Generates a std::cout for the intrinsics results that will match the
3939
/// rust debug output format for the return type. The generated line assumes
4040
/// there is an int i in scope which is the current pass number.
41-
fn print_result_c(&self, _indentation: Indentation, _additional: &str) -> String {
42-
todo!("print_result_c in Intrinsic<X86IntrinsicType> needs to be implemented!");
41+
fn print_result_c(&self, indentation: Indentation, additional: &str) -> String {
42+
let lanes = if self.results().num_vectors() > 1 {
43+
(0..self.results().num_vectors())
44+
.map(|vector| {
45+
format!(
46+
r#""{ty}(" << {lanes} << ")""#,
47+
ty = self.results().c_single_vector_type(),
48+
lanes = (0..self.results().num_lanes())
49+
.map(move |idx| -> std::string::String {
50+
format!(
51+
"{cast}{lane_fn}(__return_value.val[{vector}], {lane})",
52+
cast = self.results().c_promotion(),
53+
lane_fn = self.results().get_lane_function(),
54+
lane = idx,
55+
vector = vector,
56+
)
57+
})
58+
.collect::<Vec<_>>()
59+
.join(r#" << ", " << "#)
60+
)
61+
})
62+
.collect::<Vec<_>>()
63+
.join(r#" << ", " << "#)
64+
} else if self.results().num_lanes() > 1 {
65+
(0..self.results().num_lanes())
66+
.map(|idx| -> std::string::String {
67+
format!(
68+
"{cast}{lane_fn}(__return_value, {lane})",
69+
cast = self.results().c_promotion(),
70+
lane_fn = self.results().get_lane_function(),
71+
lane = idx
72+
)
73+
})
74+
.collect::<Vec<_>>()
75+
.join(r#" << ", " << "#)
76+
} else {
77+
format!(
78+
"{promote}cast<{cast}>(__return_value)",
79+
cast = match self.results.kind() {
80+
TypeKind::Void => "void".to_string(),
81+
TypeKind::Float if self.results().inner_size() == 64 => "double".to_string(),
82+
TypeKind::Float if self.results().inner_size() == 32 => "float".to_string(),
83+
// TypeKind::Float if self.results().inner_size() == 16 => "float16_t".to_string(),
84+
// TypeKind::Int(true) if self.results().inner_size() == 64 => "long".to_string(),
85+
// TypeKind::Int(false) if self.results().inner_size() == 64 => "unsigned long".to_string(),
86+
// TypeKind::Int(true) if self.results().inner_size() == 32 => "int".to_string(),
87+
// TypeKind::Int(false) if self.results().inner_size() == 32 => "unsigned int".to_string(),
88+
// TypeKind::Int(true) if self.results().inner_size() == 16 => "short".to_string(),
89+
// TypeKind::Int(false) if self.results().inner_size() == 16 => "unsigned short".to_string(),
90+
_ => self.results.c_scalar_type(),
91+
},
92+
promote = self.results().c_promotion(),
93+
)
94+
};
95+
96+
format!(
97+
r#"{indentation}std::cout << "Result {additional}-" << i+1 << ": {ty}" << std::fixed << std::setprecision(150) << {lanes} << "{close}" << std::endl;"#,
98+
ty = if self.results().is_simd() {
99+
format!("{}(", self.results().c_type())
100+
} else {
101+
String::from("")
102+
},
103+
close = if self.results.is_simd() { ")" } else { "" },
104+
)
43105
}
44106
}

crates/intrinsic-test/src/x86/mod.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod xml_parser;
66
use crate::common::SupportedArchitectureTest;
77
use crate::common::cli::ProcessedCli;
88
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
9+
use crate::common::intrinsic_helpers::TypeKind;
910
use crate::common::write_file::{write_c_testfiles, write_rust_testfiles};
1011
use config::build_notices;
1112
use intrinsic::X86IntrinsicType;
@@ -18,9 +19,24 @@ pub struct X86ArchitectureTest {
1819

1920
impl SupportedArchitectureTest for X86ArchitectureTest {
2021
fn create(cli_options: ProcessedCli) -> Box<Self> {
21-
let intrinsics = get_xml_intrinsics(&cli_options.filename, &cli_options.target)
22+
let mut intrinsics = get_xml_intrinsics(&cli_options.filename, &cli_options.target)
2223
.expect("Error parsing input file");
2324

25+
intrinsics.sort_by(|a, b| a.name.cmp(&b.name));
26+
let intrinsics = intrinsics
27+
.into_iter()
28+
// Not sure how we would compare intrinsic that returns void.
29+
.filter(|i| i.results.kind() != TypeKind::Void)
30+
.filter(|i| i.results.kind() != TypeKind::BFloat)
31+
.filter(|i| i.arguments().args.len() > 0)
32+
.filter(|i| !i.arguments.iter().any(|a| a.ty.kind() == TypeKind::BFloat))
33+
// Skip pointers for now, we would probably need to look at the return
34+
// type to work out how many elements we need to point to.
35+
.filter(|i| !i.arguments.iter().any(|a| a.is_ptr()))
36+
.filter(|i| !i.arguments.iter().any(|a| a.ty.inner_size() == 128))
37+
.filter(|i| !cli_options.skip.contains(&i.name))
38+
.collect::<Vec<_>>();
39+
2440
Box::new(Self {
2541
intrinsics: intrinsics,
2642
cli_options: cli_options,

crates/intrinsic-test/src/x86/types.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
1111
let part_1 = match self.kind {
1212
TypeKind::Int(false) => "unsigned int",
1313
TypeKind::Char(false) => "unsigned char",
14-
TypeKind::Short(false) => "unsigned short",
15-
TypeKind::Short(true) => "short",
1614
_ => self.kind.c_prefix(),
1715
};
1816
let part_2 = if self.ptr {
@@ -65,14 +63,24 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
6563

6664
// TODO: make the unwrapping safe
6765
let kind = TypeKind::from_str(s_split.unwrap()).expect("Unable to parse type!");
66+
67+
let kind = if let Some(_) = s.find("unsigned") {
68+
match kind {
69+
TypeKind::Int(_) => TypeKind::Int(false),
70+
TypeKind::Char(_) => TypeKind::Char(false),
71+
a => a,
72+
}
73+
} else {
74+
kind
75+
};
6876
let mut ptr_constant = false;
6977
let mut constant = false;
7078
let mut ptr = false;
7179

7280
if let Some(_) = s.matches("const*").next() {
7381
ptr_constant = true;
7482
};
75-
if let Some(_) = s.matches("const").next() {
83+
if let Some(_) = s.matches("const ").next() {
7684
constant = true;
7785
};
7886
if let Some(_) = s.matches("*").next() {

0 commit comments

Comments
 (0)