Skip to content

Commit 92f7cb4

Browse files
authored
Merge pull request rust-lang#1946 from folkertdev/intrinsic-test-dedup-constants
`intrinsic-test`: dedup constants
2 parents 8ca1329 + f4d4218 commit 92f7cb4

File tree

3 files changed

+78
-33
lines changed

3 files changed

+78
-33
lines changed

library/stdarch/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]);`

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

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
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;
12+
13+
macro_rules! concatln {
14+
($($lines:expr),* $(,)?) => {
15+
concat!($( $lines, "\n" ),*)
16+
};
17+
}
1118

1219
fn write_cargo_toml_header(w: &mut impl std::io::Write, name: &str) -> std::io::Result<()> {
1320
writeln!(
1421
w,
15-
concat!(
16-
"[package]\n",
17-
"name = \"{name}\"\n",
18-
"version = \"{version}\"\n",
19-
"authors = [{authors}]\n",
20-
"license = \"{license}\"\n",
21-
"edition = \"2018\"\n",
22+
concatln!(
23+
"[package]",
24+
"name = \"{name}\"",
25+
"version = \"{version}\"",
26+
"authors = [{authors}]",
27+
"license = \"{license}\"",
28+
"edition = \"2018\"",
2229
),
2330
name = name,
2431
version = env!("CARGO_PKG_VERSION"),
@@ -118,6 +125,20 @@ pub fn write_lib_rs<T: IntrinsicTypeDefinition>(
118125

119126
writeln!(w, "{definitions}")?;
120127

128+
let mut seen = std::collections::HashSet::new();
129+
130+
for intrinsic in intrinsics {
131+
for arg in &intrinsic.arguments.args {
132+
if !arg.has_constraint() && arg.ty.is_rust_vals_array_const() {
133+
let name = arg.rust_vals_array_name().to_string();
134+
135+
if seen.insert(name) {
136+
ArgumentList::gen_arg_rust(arg, w, Indentation::default(), PASSES)?;
137+
}
138+
}
139+
}
140+
}
141+
121142
for intrinsic in intrinsics {
122143
crate::common::gen_rust::create_rust_test_module(w, intrinsic)?;
123144
}
@@ -232,23 +253,23 @@ pub fn generate_rust_test_loop<T: IntrinsicTypeDefinition>(
232253
}
233254
}
234255

235-
let indentation2 = indentation.nested();
236-
let indentation3 = indentation2.nested();
237-
writeln!(
256+
write!(
238257
w,
239-
"\
240-
for (id, f) in specializations {{\n\
241-
for i in 0..{passes} {{\n\
242-
unsafe {{\n\
243-
{loaded_args}\
244-
let __return_value = f({args});\n\
245-
println!(\"Result {{id}}-{{}}: {{:?}}\", i + 1, {return_value});\n\
246-
}}\n\
247-
}}\n\
248-
}}",
249-
loaded_args = intrinsic.arguments.load_values_rust(indentation3),
258+
concatln!(
259+
" for (id, f) in specializations {{",
260+
" for i in 0..{passes} {{",
261+
" unsafe {{",
262+
"{loaded_args}",
263+
" let __return_value = f({args});",
264+
" println!(\"Result {{id}}-{{}}: {{:?}}\", i + 1, {return_value});",
265+
" }}",
266+
" }}",
267+
" }}",
268+
),
269+
loaded_args = intrinsic.arguments.load_values_rust(indentation.nest_by(4)),
250270
args = intrinsic.arguments.as_call_param_rust(),
251271
return_value = intrinsic.results.print_result_rust(),
272+
passes = passes,
252273
)
253274
}
254275

library/stdarch/crates/intrinsic-test/src/common/indentation.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ impl Indentation {
1010
pub fn nested(self) -> Self {
1111
Self(self.0 + 1)
1212
}
13+
14+
pub fn nest_by(&self, additional_levels: u32) -> Self {
15+
Self(self.0 + additional_levels)
16+
}
1317
}
1418

1519
impl std::fmt::Display for Indentation {

0 commit comments

Comments
 (0)