Skip to content

Commit e32b078

Browse files
chore: add better error handling when writing and compiling mod_{i}.cpp,
neatly organize C++ headers
1 parent 218c360 commit e32b078

File tree

4 files changed

+18
-34
lines changed

4 files changed

+18
-34
lines changed

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
3131

3232
const NOTICE: &str = config::NOTICE;
3333

34-
const PLATFORM_C_HEADERS: &[&str] = &[
35-
"iostream",
36-
"cstring",
37-
"iomanip",
38-
"sstream",
39-
"arm_neon.h",
40-
"arm_acle.h",
41-
"arm_fp16.h",
42-
];
34+
const PLATFORM_C_HEADERS: &[&str] = &["arm_neon.h", "arm_acle.h", "arm_fp16.h"];
4335
const PLATFORM_C_DEFINITIONS: &str = config::POLY128_OSTREAM_DEF;
4436
const PLATFORM_C_FORWARD_DECLARATIONS: &str = config::POLY128_OSTREAM_DECL;
4537

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use super::intrinsic_helpers::IntrinsicTypeDefinition;
66

77
// The number of times each intrinsic will be called.
88
const PASSES: u32 = 20;
9+
const COMMON_HEADERS: [&str; 5] = ["iostream", "string", "cstring", "iomanip", "sstream"];
910

1011
pub fn generate_c_test_loop<T: IntrinsicTypeDefinition + Sized>(
1112
w: &mut impl std::io::Write,
@@ -99,7 +100,7 @@ pub fn write_mod_cpp<T: IntrinsicTypeDefinition>(
99100
) -> std::io::Result<()> {
100101
write!(w, "{notice}")?;
101102

102-
for header in platform_headers {
103+
for header in COMMON_HEADERS.iter().chain(platform_headers.iter()) {
103104
writeln!(w, "#include <{header}>")?;
104105
}
105106

@@ -133,20 +134,13 @@ pub fn write_main_cpp<'a>(
133134
arch_specific_headers: &[&str],
134135
intrinsics: impl Iterator<Item = &'a str> + Clone,
135136
) -> std::io::Result<()> {
136-
writeln!(w, "#include <iostream>")?;
137-
writeln!(w, "#include <string>")?;
138-
139-
for header in arch_specific_headers {
137+
for header in COMMON_HEADERS.iter().chain(arch_specific_headers.iter()) {
140138
writeln!(w, "#include <{header}>")?;
141139
}
142140

143141
writeln!(
144142
w,
145143
r#"
146-
#include <cstring>
147-
#include <iomanip>
148-
#include <sstream>
149-
150144
std::ostream& operator<<(std::ostream& os, float16_t value) {{
151145
uint16_t temp = 0;
152146
memcpy(&temp, &value, sizeof(float16_t));

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,34 @@ pub trait SupportedArchitectureTest {
6060
.map(|(i, chunk)| {
6161
let c_filename = format!("c_programs/mod_{i}.cpp");
6262
let mut file = File::create(&c_filename).unwrap();
63-
write_mod_cpp(
63+
let mod_file_write_result = write_mod_cpp(
6464
&mut file,
6565
Self::NOTICE,
6666
Self::PLATFORM_C_HEADERS,
6767
Self::PLATFORM_C_FORWARD_DECLARATIONS,
6868
chunk,
69-
)
70-
.unwrap();
69+
);
70+
71+
if let Err(error) = mod_file_write_result {
72+
return Err(format!("Error writing to mod_{i}.cpp: {error:?}"));
73+
}
7174

7275
// compile this cpp file into a .o file.
7376
//
7477
// This is done because `cpp_compiler_wrapped` is None when
7578
// the --generate-only flag is passed
7679
if let Some(cpp_compiler) = cpp_compiler_wrapped.as_ref() {
77-
let output = cpp_compiler
78-
.compile_object_file(&format!("mod_{i}.cpp"), &format!("mod_{i}.o"))?;
79-
assert!(output.status.success(), "{output:?}");
80+
let compile_output = cpp_compiler
81+
.compile_object_file(&format!("mod_{i}.cpp"), &format!("mod_{i}.o"));
82+
83+
if let Err(compile_error) = compile_output {
84+
return Err(format!("Error compiling mod_{i}.cpp: {compile_error:?}"));
85+
}
8086
}
8187

8288
Ok(())
8389
})
84-
.collect::<Result<(), std::io::Error>>()
90+
.collect::<Result<(), String>>()
8591
.unwrap();
8692

8793
let mut file = File::create("c_programs/main.cpp").unwrap();

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ impl SupportedArchitectureTest for X86ArchitectureTest {
3535

3636
const NOTICE: &str = config::NOTICE;
3737

38-
const PLATFORM_C_HEADERS: &[&str] = &[
39-
"immintrin.h",
40-
"iostream",
41-
"cstring",
42-
"iomanip",
43-
"sstream",
44-
"cstddef",
45-
"cstdint",
46-
];
38+
const PLATFORM_C_HEADERS: &[&str] = &["immintrin.h", "cstddef", "cstdint"];
4739
const PLATFORM_C_DEFINITIONS: &str = config::LANE_FUNCTION_HELPERS;
4840
const PLATFORM_C_FORWARD_DECLARATIONS: &str = config::LANE_FUNCTION_HELPERS;
4941

0 commit comments

Comments
 (0)