Skip to content

Commit 50fde10

Browse files
Fix: removed BaseIntrinsicTypeDefinition + code cleanup
1. Removed default implementation of traits that are compulsorily implemented 2. Replaced BaseIntrinsicTypeDefinition with Deref<Target = IntrinsicType>
1 parent 66a88fe commit 50fde10

File tree

5 files changed

+49
-152
lines changed

5 files changed

+49
-152
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
use crate::base_intrinsictype_trait_def_macro;
21
use crate::common::argument::ArgumentList;
3-
use crate::common::cli::Language;
42
use crate::common::indentation::Indentation;
53
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
6-
use crate::common::intrinsic_helpers::{
7-
BaseIntrinsicTypeDefinition, IntrinsicTypeDefinition, TypeKind,
8-
};
4+
use crate::common::intrinsic_helpers::{IntrinsicType, IntrinsicTypeDefinition, TypeKind};
5+
use std::ops::Deref;
96

10-
base_intrinsictype_trait_def_macro! {ArmIntrinsicType}
7+
#[derive(Debug, Clone, PartialEq)]
8+
pub struct ArmIntrinsicType(pub IntrinsicType);
9+
10+
impl Deref for ArmIntrinsicType {
11+
type Target = IntrinsicType;
12+
13+
fn deref(&self) -> &Self::Target {
14+
&self.0
15+
}
16+
}
1117

1218
impl IntrinsicDefinition<ArmIntrinsicType> for Intrinsic<ArmIntrinsicType> {
1319
fn arguments(&self) -> ArgumentList<ArmIntrinsicType> {

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::common::cli::ProcessedCli;
1111
use crate::common::compare::compare_outputs;
1212
use crate::common::gen_rust::compile_rust;
1313
use crate::common::intrinsic::{Intrinsic, IntrinsicDefinition};
14-
use crate::common::intrinsic_helpers::{BaseIntrinsicTypeDefinition, TypeKind};
14+
use crate::common::intrinsic_helpers::TypeKind;
1515
use crate::common::write_file::{write_c_testfiles, write_rust_testfiles};
1616
use config::{AARCH_CONFIGURATIONS, F16_FORMATTING_DEF, POLY128_OSTREAM_DEF, build_notices};
1717
use json_parser::get_neon_intrinsics;
@@ -54,6 +54,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
5454
let compiler = self.cli_options.cpp_compiler.as_deref();
5555
let target = &self.cli_options.target;
5656
let cxx_toolchain_dir = self.cli_options.cxx_toolchain_dir.as_deref();
57+
let c_target = "aarch64";
5758

5859
let intrinsics_name_list = write_c_testfiles(
5960
&self
@@ -62,6 +63,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
6263
.map(|i| i as &dyn IntrinsicDefinition<_>)
6364
.collect::<Vec<_>>(),
6465
target,
66+
c_target,
6567
&["arm_neon.h", "arm_acle.h", "arm_fp16.h"],
6668
&build_notices("// "),
6769
&[POLY128_OSTREAM_DEF],
@@ -76,7 +78,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
7678
}
7779

7880
fn build_rust_file(&self) -> bool {
79-
let final_target = if self.cli_options.target.contains("v7") {
81+
let rust_target = if self.cli_options.target.contains("v7") {
8082
"arm"
8183
} else {
8284
"aarch64"
@@ -89,7 +91,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
8991
.iter()
9092
.map(|i| i as &dyn IntrinsicDefinition<_>)
9193
.collect::<Vec<_>>(),
92-
final_target,
94+
rust_target,
9395
&build_notices("// "),
9496
F16_FORMATTING_DEF,
9597
AARCH_CONFIGURATIONS,

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ where
3939
/// Generates a std::cout for the intrinsics results that will match the
4040
/// rust debug output format for the return type. The generated line assumes
4141
/// there is an int i in scope which is the current pass number.
42-
fn print_result_c(&self, _indentation: Indentation, _additional: &str) -> String {
43-
unimplemented!("Architectures need to implement print_result_c!")
44-
}
42+
fn print_result_c(&self, _indentation: Indentation, _additional: &str) -> String;
4543

4644
fn generate_loop_c(
4745
&self,
@@ -137,6 +135,7 @@ where
137135
&self,
138136
header_files: &[&str],
139137
target: &str,
138+
c_target: &str,
140139
notices: &str,
141140
arch_specific_definitions: &[&str],
142141
) -> String {
@@ -150,7 +149,7 @@ where
150149
generate_c_program(
151150
notices,
152151
header_files,
153-
"aarch64",
152+
c_target,
154153
arch_specific_definitions,
155154
self.arguments()
156155
.gen_arglists_c(indentation, PASSES)

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

Lines changed: 26 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt;
2+
use std::ops::Deref;
23
use std::str::FromStr;
34

45
use itertools::Itertools as _;
@@ -102,102 +103,52 @@ pub struct IntrinsicType {
102103
pub target: String,
103104
}
104105

105-
pub trait BaseIntrinsicTypeDefinition {
106-
/// Get the TypeKind for this type, recursing into pointers.
107-
fn kind(&self) -> TypeKind;
108-
109-
/// Get the size of a single element inside this type, recursing into
110-
/// pointers, i.e. a pointer to a u16 would be 16 rather than the size
111-
/// of a pointer.
112-
fn inner_size(&self) -> u32;
113-
114-
fn num_lanes(&self) -> u32;
115-
116-
fn num_vectors(&self) -> u32;
117-
118-
/// Determine if the type is a simd type, this will treat a type such as
119-
/// `uint64x1` as simd.
120-
fn is_simd(&self) -> bool;
121-
122-
fn is_ptr(&self) -> bool;
123-
124-
fn c_scalar_type(&self) -> String;
125-
126-
fn rust_scalar_type(&self) -> String;
127-
128-
/// Gets a cast for this type if needs promotion.
129-
/// This is required for 8 bit types due to printing as the 8 bit types use
130-
/// a char and when using that in `std::cout` it will print as a character,
131-
/// which means value of 0 will be printed as a null byte.
132-
///
133-
/// This is also needed for polynomial types because we want them to be
134-
/// printed as unsigned integers to match Rust's `Debug` impl.
135-
fn c_promotion(&self) -> &str;
136-
137-
/// Generates an initialiser for an array, which can be used to initialise an argument for the
138-
/// intrinsic call.
139-
///
140-
/// This is determistic based on the pass number.
141-
///
142-
/// * `loads`: The number of values that need to be loaded from the argument array
143-
/// * e.g for argument type uint32x2, loads=2 results in a string representing 4 32-bit values
144-
///
145-
/// Returns a string such as
146-
/// * `{0x1, 0x7F, 0xFF}` if `language` is `Language::C`
147-
/// * `[0x1 as _, 0x7F as _, 0xFF as _]` if `language` is `Language::Rust`
148-
fn populate_random(&self, indentation: Indentation, loads: u32, language: &Language) -> String;
149-
150-
fn is_rust_vals_array_const(&self) -> bool;
151-
152-
fn as_call_param_c(&self, name: &String) -> String;
153-
}
154-
155-
impl BaseIntrinsicTypeDefinition for IntrinsicType {
156-
fn kind(&self) -> TypeKind {
106+
impl IntrinsicType {
107+
pub fn kind(&self) -> TypeKind {
157108
self.kind
158109
}
159110

160-
fn inner_size(&self) -> u32 {
111+
pub fn inner_size(&self) -> u32 {
161112
if let Some(bl) = self.bit_len {
162113
bl
163114
} else {
164115
unreachable!("")
165116
}
166117
}
167118

168-
fn num_lanes(&self) -> u32 {
119+
pub fn num_lanes(&self) -> u32 {
169120
if let Some(sl) = self.simd_len { sl } else { 1 }
170121
}
171122

172-
fn num_vectors(&self) -> u32 {
123+
pub fn num_vectors(&self) -> u32 {
173124
if let Some(vl) = self.vec_len { vl } else { 1 }
174125
}
175126

176-
fn is_simd(&self) -> bool {
127+
pub fn is_simd(&self) -> bool {
177128
self.simd_len.is_some() || self.vec_len.is_some()
178129
}
179130

180-
fn is_ptr(&self) -> bool {
131+
pub fn is_ptr(&self) -> bool {
181132
self.ptr
182133
}
183134

184-
fn c_scalar_type(&self) -> String {
135+
pub fn c_scalar_type(&self) -> String {
185136
format!(
186137
"{prefix}{bits}_t",
187138
prefix = self.kind().c_prefix(),
188139
bits = self.inner_size()
189140
)
190141
}
191142

192-
fn rust_scalar_type(&self) -> String {
143+
pub fn rust_scalar_type(&self) -> String {
193144
format!(
194145
"{prefix}{bits}",
195146
prefix = self.kind().rust_prefix(),
196147
bits = self.inner_size()
197148
)
198149
}
199150

200-
fn c_promotion(&self) -> &str {
151+
pub fn c_promotion(&self) -> &str {
201152
match *self {
202153
IntrinsicType {
203154
kind,
@@ -225,7 +176,12 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
225176
}
226177
}
227178

228-
fn populate_random(&self, indentation: Indentation, loads: u32, language: &Language) -> String {
179+
pub fn populate_random(
180+
&self,
181+
indentation: Indentation,
182+
loads: u32,
183+
language: &Language,
184+
) -> String {
229185
match self {
230186
IntrinsicType {
231187
bit_len: Some(bit_len @ (8 | 16 | 32 | 64)),
@@ -293,7 +249,7 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
293249
}
294250
}
295251

296-
fn is_rust_vals_array_const(&self) -> bool {
252+
pub fn is_rust_vals_array_const(&self) -> bool {
297253
match self {
298254
// Floats have to be loaded at runtime for stable NaN conversion.
299255
IntrinsicType {
@@ -308,7 +264,7 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
308264
}
309265
}
310266

311-
fn as_call_param_c(&self, name: &String) -> String {
267+
pub fn as_call_param_c(&self, name: &String) -> String {
312268
if self.ptr {
313269
format!("&{}", name)
314270
} else {
@@ -317,92 +273,24 @@ impl BaseIntrinsicTypeDefinition for IntrinsicType {
317273
}
318274
}
319275

320-
pub trait IntrinsicTypeDefinition: BaseIntrinsicTypeDefinition {
276+
pub trait IntrinsicTypeDefinition: Deref<Target = IntrinsicType> {
321277
/// Determines the load function for this type.
322278
/// can be implemented in an `impl` block
323-
fn get_load_function(&self, _language: Language) -> String {
324-
unimplemented!("Different architectures must implement get_load_function!")
325-
}
279+
fn get_load_function(&self, _language: Language) -> String;
326280

327281
/// can be implemented in an `impl` block
328-
fn get_lane_function(&self) -> String {
329-
unimplemented!("Different architectures must implement get_lane_function!")
330-
}
282+
fn get_lane_function(&self) -> String;
331283

332284
/// can be implemented in an `impl` block
333-
fn from_c(_s: &str, _target: &String) -> Result<Box<Self>, String> {
334-
unimplemented!("Different architectures must implement from_c!")
335-
}
285+
fn from_c(_s: &str, _target: &String) -> Result<Box<Self>, String>;
336286

337287
/// Gets a string containing the typename for this type in C format.
338288
/// can be directly defined in `impl` blocks
339-
fn c_type(&self) -> String {
340-
unimplemented!("Different architectures must implement c_type!")
341-
}
289+
fn c_type(&self) -> String;
342290

343291
/// can be directly defined in `impl` blocks
344-
fn c_single_vector_type(&self) -> String {
345-
unimplemented!("Different architectures must implement c_single_vector_type!")
346-
}
292+
fn c_single_vector_type(&self) -> String;
347293

348294
/// can be defined in `impl` blocks
349-
fn rust_type(&self) -> String {
350-
unimplemented!("Different architectures must implement rust_type!")
351-
}
352-
}
353-
354-
/// Defines the basic structure of achitecture-specific derivatives
355-
/// of IntrinsicType.
356-
#[macro_export]
357-
macro_rules! base_intrinsictype_trait_def_macro {
358-
($T:ident) => {
359-
use crate::common::intrinsic_helpers::IntrinsicType;
360-
361-
#[derive(Debug, Clone, PartialEq)]
362-
pub struct $T(pub IntrinsicType);
363-
364-
impl BaseIntrinsicTypeDefinition for $T {
365-
fn kind(&self) -> TypeKind {
366-
self.0.kind()
367-
}
368-
fn inner_size(&self) -> u32 {
369-
self.0.inner_size()
370-
}
371-
fn num_lanes(&self) -> u32 {
372-
self.0.num_lanes()
373-
}
374-
fn num_vectors(&self) -> u32 {
375-
self.0.num_vectors()
376-
}
377-
fn is_simd(&self) -> bool {
378-
self.0.is_simd()
379-
}
380-
fn is_ptr(&self) -> bool {
381-
self.0.is_ptr()
382-
}
383-
fn c_scalar_type(&self) -> String {
384-
self.0.c_scalar_type()
385-
}
386-
fn rust_scalar_type(&self) -> String {
387-
self.0.rust_scalar_type()
388-
}
389-
fn c_promotion(&self) -> &str {
390-
self.0.c_promotion()
391-
}
392-
fn populate_random(
393-
&self,
394-
indentation: Indentation,
395-
loads: u32,
396-
language: &Language,
397-
) -> String {
398-
self.0.populate_random(indentation, loads, language)
399-
}
400-
fn is_rust_vals_array_const(&self) -> bool {
401-
self.0.is_rust_vals_array_const()
402-
}
403-
fn as_call_param_c(&self, name: &String) -> String {
404-
self.0.as_call_param_c(name)
405-
}
406-
}
407-
};
295+
fn rust_type(&self) -> String;
408296
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::common::write_file;
77
pub fn write_c_testfiles<T: IntrinsicTypeDefinition + Sized>(
88
intrinsics: &Vec<&dyn IntrinsicDefinition<T>>,
99
target: &str,
10+
c_target: &str,
1011
headers: &[&str],
1112
notice: &str,
1213
arch_specific_definitions: &[&str],
@@ -18,7 +19,8 @@ pub fn write_c_testfiles<T: IntrinsicTypeDefinition + Sized>(
1819
let filename_mapping = create_c_filenames(&intrinsics_name_list);
1920

2021
intrinsics.iter().for_each(|i| {
21-
let c_code = i.generate_c_program(headers, target, notice, arch_specific_definitions);
22+
let c_code =
23+
i.generate_c_program(headers, target, c_target, notice, arch_specific_definitions);
2224
match filename_mapping.get(&i.name()) {
2325
Some(filename) => write_file(filename, c_code),
2426
None => {}

0 commit comments

Comments
 (0)