Skip to content

Commit d46a708

Browse files
feat: demote "target" to a 2nd class variable, since it is only rarely
necessary. Further notes: 1. IntrinsicType.target -> IntrinsicType.metadata 2. Maintaining a HashMap to support arbitrary data storage for other arch
1 parent d92ce71 commit d46a708

File tree

10 files changed

+54
-41
lines changed

10 files changed

+54
-41
lines changed

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

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

77
#[derive(Debug, Clone, PartialEq)]
88
pub struct ArmIntrinsicType(pub IntrinsicType);
@@ -15,6 +15,12 @@ impl Deref for ArmIntrinsicType {
1515
}
1616
}
1717

18+
impl DerefMut for ArmIntrinsicType {
19+
fn deref_mut(&mut self) -> &mut Self::Target {
20+
&mut self.0
21+
}
22+
}
23+
1824
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
1925
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {
2026
self.arguments.clone()

crates/intrinsic-test/src/arm/json_parser.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ fn json_to_intrinsic(
7979
) -> Result<Intrinsic<ArmIntrinsicType>, Box<dyn std::error::Error>> {
8080
let name = intr.name.replace(['[', ']'], "");
8181

82-
let results = ArmIntrinsicType::from_c(&intr.return_type.value, &target.to_string())?;
83-
82+
let mut results = ArmIntrinsicType::from_c(&intr.return_type.value)?;
83+
results.set_metadata("target".to_string(), target.to_string());
8484
let args = intr
8585
.arguments
8686
.into_iter()
@@ -92,7 +92,9 @@ fn json_to_intrinsic(
9292
let arg_prep: Option<ArgPrep> = metadata.and_then(|a| a.try_into().ok());
9393
let constraint: Option<Constraint> = arg_prep.and_then(|a| a.try_into().ok());
9494

95-
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, target, constraint);
95+
let mut arg = Argument::<ArmIntrinsicType>::from_c(i, &arg, constraint);
96+
arg.ty
97+
.set_metadata("target".to_string(), target.to_string());
9698

9799
// The JSON doesn't list immediates as const
98100
let IntrinsicType {

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::collections::HashMap;
2+
13
use super::intrinsic::ArmIntrinsicType;
24
use crate::common::cli::Language;
35
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
@@ -59,7 +61,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
5961
bit_len: Some(bl),
6062
simd_len,
6163
vec_len,
62-
target,
64+
metadata,
6365
..
6466
} = &self.0
6567
{
@@ -69,7 +71,11 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
6971
""
7072
};
7173

72-
let choose_workaround = language == Language::C && target.contains("v7");
74+
let choose_workaround = language == Language::C
75+
&& metadata
76+
.get("target")
77+
.filter(|value| value.contains("v7"))
78+
.is_some();
7379
format!(
7480
"vld{len}{quad}_{type}{size}",
7581
type = match k {
@@ -121,18 +127,18 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
121127
}
122128
}
123129

124-
fn from_c(s: &str, target: &String) -> Result<Self, String> {
130+
fn from_c(s: &str) -> Result<Self, String> {
125131
const CONST_STR: &str = "const";
126132
if let Some(s) = s.strip_suffix('*') {
127133
let (s, constant) = match s.trim().strip_suffix(CONST_STR) {
128134
Some(stripped) => (stripped, true),
129135
None => (s, false),
130136
};
131137
let s = s.trim_end();
132-
let temp_return = ArmIntrinsicType::from_c(s, target);
138+
let temp_return = ArmIntrinsicType::from_c(s);
133139
temp_return.and_then(|mut op| {
134-
op.0.ptr = true;
135-
op.0.ptr_constant = constant;
140+
op.ptr = true;
141+
op.ptr_constant = constant;
136142
Ok(op)
137143
})
138144
} else {
@@ -170,7 +176,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
170176
bit_len: Some(bit_len),
171177
simd_len,
172178
vec_len,
173-
target: target.to_string(),
179+
metadata: HashMap::new(),
174180
}))
175181
} else {
176182
let kind = start.parse::<TypeKind>()?;
@@ -186,7 +192,7 @@ impl IntrinsicTypeDefinition for ArmIntrinsicType {
186192
bit_len,
187193
simd_len: None,
188194
vec_len: None,
189-
target: target.to_string(),
195+
metadata: HashMap::new(),
190196
}))
191197
}
192198
}

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,10 @@ where
7171
}
7272
}
7373

74-
pub fn from_c(
75-
pos: usize,
76-
arg: &str,
77-
target: &str,
78-
constraint: Option<Constraint>,
79-
) -> Argument<T> {
74+
pub fn from_c(pos: usize, arg: &str, constraint: Option<Constraint>) -> Argument<T> {
8075
let (ty, var_name) = Self::type_and_name_from_c(arg);
8176

82-
let ty = T::from_c(ty, &target.to_string())
83-
.unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
77+
let ty = T::from_c(ty).unwrap_or_else(|_| panic!("Failed to parse argument '{arg}'"));
8478

8579
Argument {
8680
pos,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>(
105105
indentation: Indentation,
106106
additional: &str,
107107
passes: u32,
108-
_target: &str,
109108
) -> String {
110109
let body_indentation = indentation.nested();
111110
format!(
@@ -157,7 +156,7 @@ pub fn generate_c_constraint_blocks<T: IntrinsicTypeDefinition>(
157156
})
158157
.join("\n")
159158
} else {
160-
generate_c_test_loop(intrinsic, indentation, &name, PASSES, target)
159+
generate_c_test_loop(intrinsic, indentation, &name, PASSES)
161160
}
162161
}
163162

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::fmt;
23
use std::ops::Deref;
34
use std::str::FromStr;
@@ -18,21 +19,22 @@ pub enum TypeKind {
1819
Char(bool),
1920
Poly,
2021
Void,
22+
Mask,
2123
}
2224

2325
impl FromStr for TypeKind {
2426
type Err = String;
2527

2628
fn from_str(s: &str) -> Result<Self, Self::Err> {
2729
match s {
28-
"bfloat" => Ok(Self::BFloat),
29-
"float" => Ok(Self::Float),
30-
"double" => Ok(Self::Float),
31-
"int" | "long" | "short" => Ok(Self::Int(true)),
30+
"bfloat" | "BF16" => Ok(Self::BFloat),
31+
"float" | "double" | "FP16" | "FP32" | "FP64" => Ok(Self::Float),
32+
"int" | "long" | "short" | "SI8" | "SI16" | "SI32" | "SI64" => Ok(Self::Int(true)),
3233
"poly" => Ok(Self::Poly),
3334
"char" => Ok(Self::Char(true)),
34-
"uint" | "unsigned" => Ok(Self::Int(false)),
35+
"uint" | "unsigned" | "UI8" | "UI16" | "UI32" | "UI64" => Ok(Self::Int(false)),
3536
"void" => Ok(Self::Void),
37+
"MASK" => Ok(Self::Mask),
3638
_ => Err(format!("Impossible to parse argument kind {s}")),
3739
}
3840
}
@@ -52,6 +54,7 @@ impl fmt::Display for TypeKind {
5254
Self::Void => "void",
5355
Self::Char(true) => "char",
5456
Self::Char(false) => "unsigned char",
57+
Self::Mask => "mask",
5558
}
5659
)
5760
}
@@ -107,7 +110,8 @@ pub struct IntrinsicType {
107110
/// A value of `None` can be assumed to be 1 though.
108111
pub vec_len: Option<u32>,
109112

110-
pub target: String,
113+
// pub target: String,
114+
pub metadata: HashMap<String, String>,
111115
}
112116

113117
impl IntrinsicType {
@@ -151,6 +155,10 @@ impl IntrinsicType {
151155
self.vec_len = value;
152156
}
153157

158+
pub fn set_metadata(&mut self, key: String, value: String) {
159+
self.metadata.insert(key, value);
160+
}
161+
154162
pub fn c_scalar_type(&self) -> String {
155163
match self {
156164
IntrinsicType {
@@ -322,7 +330,7 @@ pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> {
322330
fn get_lane_function(&self) -> String;
323331

324332
/// can be implemented in an `impl` block
325-
fn from_c(_s: &str, _target: &String) -> Result<Self, String>
333+
fn from_c(_s: &str) -> Result<Self, String>
326334
where
327335
Self: Sized;
328336

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ impl Deref for X86IntrinsicType {
1616
}
1717

1818
impl DerefMut for X86IntrinsicType {
19-
// type Target = IntrinsicType;
2019
fn deref_mut(&mut self) -> &mut Self::Target {
2120
&mut self.0
2221
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ pub struct X86ArchitectureTest {
1919

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

2525
intrinsics.sort_by(|a, b| a.name.cmp(&b.name));
2626
let intrinsics = intrinsics

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::collections::HashMap;
12
use std::str::FromStr;
23

34
use super::intrinsic::X86IntrinsicType;
@@ -36,7 +37,7 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
3637
todo!("get_lane_function for X86IntrinsicType needs to be implemented!");
3738
}
3839

39-
fn from_c(s: &str, target: &String) -> Result<Self, String> {
40+
fn from_c(s: &str) -> Result<Self, String> {
4041
let mut s_copy = s.to_string();
4142
s_copy = s_copy
4243
.replace("*", "")
@@ -88,7 +89,7 @@ impl IntrinsicTypeDefinition for X86IntrinsicType {
8889
bit_len: None,
8990
simd_len: None,
9091
vec_len: None,
91-
target: target.to_string(),
92+
metadata: HashMap::new(),
9293
}))
9394
}
9495
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ struct Instruction {
7373

7474
pub fn get_xml_intrinsics(
7575
filename: &Path,
76-
target: &String,
7776
) -> Result<Vec<Intrinsic<X86IntrinsicType>>, Box<dyn std::error::Error>> {
7877
let file = std::fs::File::open(filename)?;
7978
let reader = std::io::BufReader::new(file);
@@ -85,15 +84,15 @@ pub fn get_xml_intrinsics(
8584
.into_iter()
8685
.filter_map(|intr| {
8786
// Some(xml_to_intrinsic(intr, target).expect("Couldn't parse XML properly!"))
88-
xml_to_intrinsic(intr, target).ok()
87+
xml_to_intrinsic(intr).ok()
8988
})
9089
.collect();
9190

9291
Ok(parsed_intrinsics)
9392
}
9493

95-
fn parse_observable(param: &Parameter, target: &String) -> Result<X86IntrinsicType, String> {
96-
let ty = X86IntrinsicType::from_c(param.type_data.as_str(), target);
94+
fn parse_observable(param: &Parameter) -> Result<X86IntrinsicType, String> {
95+
let ty = X86IntrinsicType::from_c(param.type_data.as_str());
9796

9897
if let Err(_) = ty {
9998
return ty;
@@ -118,12 +117,11 @@ fn parse_observable(param: &Parameter, target: &String) -> Result<X86IntrinsicTy
118117

119118
fn xml_to_intrinsic(
120119
intr: XMLIntrinsic,
121-
target: &String,
122120
) -> Result<Intrinsic<X86IntrinsicType>, Box<dyn std::error::Error>> {
123121
let name = intr.name;
124-
let result = parse_observable(&intr.return_data, target);
122+
let result = parse_observable(&intr.return_data);
125123
let args_check = intr.parameters.into_iter().enumerate().map(|(i, param)| {
126-
let ty = parse_observable(&param, target);
124+
let ty = parse_observable(&param);
127125
if let Err(_) = ty {
128126
return None;
129127
}

0 commit comments

Comments
 (0)