Skip to content

Commit d28b44b

Browse files
committed
intrinsic test: deduplicate rust constants
1 parent d6ba187 commit d28b44b

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

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

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,15 @@ where
6060
}
6161

6262
/// The name (e.g. "A_VALS" or "a_vals") for the array of possible test inputs.
63-
fn rust_vals_array_name(&self) -> impl std::fmt::Display {
63+
pub(crate) fn rust_vals_array_name(&self) -> impl std::fmt::Display {
6464
if self.ty.is_rust_vals_array_const() {
65-
format!("{}_VALS", self.name.to_uppercase())
65+
let loads = crate::common::gen_rust::PASSES;
66+
format!(
67+
"{}_{ty}_{load_size}",
68+
self.name.to_uppercase(),
69+
ty = self.ty.rust_scalar_type(),
70+
load_size = self.ty.num_lanes() * self.ty.num_vectors() + loads - 1,
71+
)
6672
} else {
6773
format!("{}_vals", self.name.to_lowercase())
6874
}
@@ -134,20 +140,34 @@ where
134140
loads: u32,
135141
) -> std::io::Result<()> {
136142
for arg in self.iter().filter(|&arg| !arg.has_constraint()) {
137-
writeln!(
138-
w,
139-
"{indentation}{bind} {name}: [{ty}; {load_size}] = {values};",
140-
bind = arg.rust_vals_array_binding(),
141-
name = arg.rust_vals_array_name(),
142-
ty = arg.ty.rust_scalar_type(),
143-
load_size = arg.ty.num_lanes() * arg.ty.num_vectors() + loads - 1,
144-
values = arg.ty.populate_random(indentation, loads, &Language::Rust)
145-
)?
143+
// Constants are defined globally.
144+
if arg.ty.is_rust_vals_array_const() {
145+
continue;
146+
}
147+
148+
Self::gen_arg_rust(arg, w, indentation, loads)?;
146149
}
147150

148151
Ok(())
149152
}
150153

154+
pub fn gen_arg_rust(
155+
arg: &Argument<T>,
156+
w: &mut impl std::io::Write,
157+
indentation: Indentation,
158+
loads: u32,
159+
) -> std::io::Result<()> {
160+
writeln!(
161+
w,
162+
"{indentation}{bind} {name}: [{ty}; {load_size}] = {values};\n",
163+
bind = arg.rust_vals_array_binding(),
164+
name = arg.rust_vals_array_name(),
165+
ty = arg.ty.rust_scalar_type(),
166+
load_size = arg.ty.num_lanes() * arg.ty.num_vectors() + loads - 1,
167+
values = arg.ty.populate_random(indentation, loads, &Language::Rust)
168+
)
169+
}
170+
151171
/// Creates a line for each argument that initializes the argument from an array `[arg]_vals` at
152172
/// an offset `i` using a load intrinsic, in C.
153173
/// e.g `uint8x8_t a = vld1_u8(&a_vals[i]);`

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
use itertools::Itertools;
22
use std::process::Command;
33

4+
use crate::common::argument::ArgumentList;
45
use crate::common::intrinsic::Intrinsic;
56

67
use super::indentation::Indentation;
78
use super::intrinsic_helpers::IntrinsicTypeDefinition;
89

910
// The number of times each intrinsic will be called.
10-
const PASSES: u32 = 20;
11+
pub(crate) const PASSES: u32 = 20;
1112

1213
fn write_cargo_toml_header(w: &mut impl std::io::Write, name: &str) -> std::io::Result<()> {
1314
writeln!(
@@ -118,6 +119,20 @@ pub fn write_lib_rs<T: IntrinsicTypeDefinition>(
118119

119120
writeln!(w, "{definitions}")?;
120121

122+
let mut seen = std::collections::HashSet::new();
123+
124+
for intrinsic in intrinsics {
125+
for arg in &intrinsic.arguments.args {
126+
if !arg.has_constraint() && arg.ty.is_rust_vals_array_const() {
127+
let name = arg.rust_vals_array_name().to_string();
128+
129+
if seen.insert(name) {
130+
ArgumentList::gen_arg_rust(arg, w, Indentation::default(), PASSES)?;
131+
}
132+
}
133+
}
134+
}
135+
121136
for intrinsic in intrinsics {
122137
crate::common::gen_rust::create_rust_test_module(w, intrinsic)?;
123138
}

0 commit comments

Comments
 (0)