Skip to content

Commit 2f7b0a6

Browse files
committed
optimize Rust file creation
1 parent 345d17a commit 2f7b0a6

File tree

3 files changed

+37
-49
lines changed

3 files changed

+37
-49
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
5757
let c_target = "aarch64";
5858

5959
let intrinsics_name_list = write_c_testfiles(
60-
&self
61-
.intrinsics
60+
self.intrinsics
6261
.iter()
63-
.map(|i| i as &dyn IntrinsicDefinition<_>)
64-
.collect::<Vec<_>>(),
62+
.map(|i| i as &dyn IntrinsicDefinition<_>),
6563
target,
6664
c_target,
6765
&["arm_neon.h", "arm_acle.h", "arm_fp16.h"],
@@ -92,15 +90,14 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
9290
let intrinsics_name_list = write_rust_testfiles(
9391
self.intrinsics
9492
.iter()
95-
.map(|i| i as &dyn IntrinsicDefinition<_>)
96-
.collect::<Vec<_>>(),
93+
.map(|i| i as &dyn IntrinsicDefinition<_>),
9794
rust_target,
9895
&build_notices("// "),
9996
F16_FORMATTING_DEF,
10097
AARCH_CONFIGURATIONS,
10198
);
10299

103-
compile_rust_programs(intrinsics_name_list, toolchain, target, linker)
100+
compile_rust_programs(intrinsics_name_list.unwrap(), toolchain, target, linker)
104101
}
105102

106103
fn compare_outputs(&self) -> bool {

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

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use itertools::Itertools;
2-
use rayon::prelude::*;
3-
use std::collections::BTreeMap;
42
use std::fs::File;
53
use std::process::Command;
64

@@ -124,20 +122,6 @@ pub fn compile_rust_programs(
124122
}
125123
}
126124

127-
// Creates directory structure and file path mappings
128-
pub fn setup_rust_file_paths(identifiers: &Vec<String>) -> BTreeMap<&String, String> {
129-
identifiers
130-
.par_iter()
131-
.map(|identifier| {
132-
let rust_dir = format!("rust_programs/{identifier}");
133-
let _ = std::fs::create_dir_all(&rust_dir);
134-
let rust_filename = format!("{rust_dir}/main.rs");
135-
136-
(identifier, rust_filename)
137-
})
138-
.collect::<BTreeMap<&String, String>>()
139-
}
140-
141125
pub fn generate_rust_test_loop<T: IntrinsicTypeDefinition>(
142126
intrinsic: &dyn IntrinsicDefinition<T>,
143127
indentation: Indentation,
Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
use std::fs::File;
22
use std::io::Write;
33

4-
use super::gen_rust::{create_rust_test_program, setup_rust_file_paths};
4+
use super::gen_rust::create_rust_test_program;
55
use super::intrinsic::IntrinsicDefinition;
66
use super::intrinsic_helpers::IntrinsicTypeDefinition;
77

8-
pub fn write_file(filename: &String, code: String) {
9-
let mut file = File::create(filename).unwrap();
10-
file.write_all(code.into_bytes().as_slice()).unwrap();
11-
}
12-
13-
pub fn write_c_testfiles<T: IntrinsicTypeDefinition + Sized>(
14-
intrinsics: &[&dyn IntrinsicDefinition<T>],
8+
pub fn write_c_testfiles<'a, T, I>(
9+
intrinsics: I,
1510
target: &str,
1611
c_target: &str,
1712
headers: &[&str],
1813
notice: &str,
1914
arch_specific_definitions: &[&str],
20-
) -> std::io::Result<Vec<String>> {
15+
) -> std::io::Result<Vec<String>>
16+
where
17+
T: IntrinsicTypeDefinition + Sized + 'a,
18+
I: Iterator<Item = &'a dyn IntrinsicDefinition<T>>,
19+
{
2120
std::fs::create_dir_all("c_programs")?;
2221

2322
intrinsics
24-
.iter()
2523
.map(|intrinsic| {
2624
let identifier = intrinsic.name().to_owned();
2725
let mut file = File::create(format!("c_programs/{identifier}.cpp")).unwrap();
2826

2927
// write_c_test_program(&mut file, intrinsic)?;
3028
let c_code = crate::common::gen_c::create_c_test_program(
31-
*intrinsic,
29+
intrinsic,
3230
headers,
3331
target,
3432
c_target,
@@ -43,25 +41,34 @@ pub fn write_c_testfiles<T: IntrinsicTypeDefinition + Sized>(
4341
.collect()
4442
}
4543

46-
pub fn write_rust_testfiles<T: IntrinsicTypeDefinition>(
47-
intrinsics: Vec<&dyn IntrinsicDefinition<T>>,
44+
pub fn write_rust_testfiles<'a, T, I>(
45+
intrinsics: I,
4846
rust_target: &str,
4947
notice: &str,
5048
definitions: &str,
5149
cfg: &str,
52-
) -> Vec<String> {
53-
let intrinsics_name_list = intrinsics
54-
.iter()
55-
.map(|i| i.name().clone())
56-
.collect::<Vec<_>>();
57-
let filename_mapping = setup_rust_file_paths(&intrinsics_name_list);
50+
) -> std::io::Result<Vec<String>>
51+
where
52+
T: IntrinsicTypeDefinition + Sized + 'a,
53+
I: Iterator<Item = &'a dyn IntrinsicDefinition<T>>,
54+
{
55+
std::fs::create_dir_all("rust_programs")?;
56+
57+
intrinsics
58+
.map(|intrinsic| {
59+
let identifier = intrinsic.name().to_owned();
5860

59-
intrinsics.iter().for_each(|&i| {
60-
let rust_code = create_rust_test_program(i, rust_target, notice, definitions, cfg);
61-
if let Some(filename) = filename_mapping.get(&i.name()) {
62-
write_file(filename, rust_code)
63-
}
64-
});
61+
let rust_dir = format!("rust_programs/{identifier}");
62+
std::fs::create_dir_all(&rust_dir)?;
63+
let rust_filename = format!("{rust_dir}/main.rs");
64+
let mut file = File::create(rust_filename).unwrap();
6565

66-
intrinsics_name_list
66+
let rust_code =
67+
create_rust_test_program(intrinsic, rust_target, notice, definitions, cfg);
68+
69+
file.write_all(rust_code.as_bytes())?;
70+
71+
Ok(identifier)
72+
})
73+
.collect()
6774
}

0 commit comments

Comments
 (0)