Skip to content

Commit 5597116

Browse files
committed
Combining C files is very effective
1 parent 0a59398 commit 5597116

File tree

12 files changed

+210
-171
lines changed

12 files changed

+210
-171
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ members = [
55
"examples",
66
]
77
exclude = [
8-
"crates/wasm-assert-instr-tests"
8+
"crates/wasm-assert-instr-tests",
9+
"rust_programs"
910
]
1011

1112
[profile.release]

ci/run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ esac
169169
# Arm specific
170170
case "${TARGET}" in
171171
aarch64-unknown-linux-gnu*|armv7-unknown-linux-gnueabihf*)
172-
CPPFLAGS="${TEST_CPPFLAGS}" RUSTFLAGS="${HOST_RUSTFLAGS}" RUST_LOG=warn \
172+
CPPFLAGS="${TEST_CPPFLAGS}" RUSTFLAGS="${HOST_RUSTFLAGS}" RUST_LOG=trace \
173173
cargo run "${INTRINSIC_TEST}" "${PROFILE}" \
174174
--bin intrinsic-test -- intrinsics_data/arm_intrinsics.json \
175175
--runner "${TEST_RUNNER}" \
@@ -179,7 +179,7 @@ case "${TARGET}" in
179179
;;
180180

181181
aarch64_be-unknown-linux-gnu*)
182-
CPPFLAGS="${TEST_CPPFLAGS}" RUSTFLAGS="${HOST_RUSTFLAGS}" RUST_LOG=warn \
182+
CPPFLAGS="${TEST_CPPFLAGS}" RUSTFLAGS="${HOST_RUSTFLAGS}" RUST_LOG=trace \
183183
cargo run "${INTRINSIC_TEST}" "${PROFILE}" \
184184
--bin intrinsic-test -- intrinsics_data/arm_intrinsics.json \
185185
--runner "${TEST_RUNNER}" \

crates/intrinsic-test/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ license = "MIT OR Apache-2.0"
1111
edition = "2024"
1212

1313
[dependencies]
14-
lazy_static = "1.4.0"
1514
serde = { version = "1", features = ["derive"] }
1615
serde_json = "1.0"
1716
csv = "1.1"

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

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::common::compile_c::CompilationCommandBuilder;
2-
use crate::common::gen_c::compile_c_programs;
32

43
pub fn compile_c_arm(
5-
intrinsics_name_list: &[String],
64
compiler: &str,
75
target: &str,
86
cxx_toolchain_dir: Option<&str>,
7+
inputs: &[String],
8+
output: Option<&str>,
99
) -> bool {
1010
// -ffp-contract=off emulates Rust's approach of not fusing separate mul-add operations
1111
let mut command = CompilationCommandBuilder::new()
@@ -49,16 +49,52 @@ pub fn compile_c_arm(
4949
command = command.add_extra_flag("-flax-vector-conversions");
5050
}
5151

52-
let compiler_commands = intrinsics_name_list
53-
.iter()
54-
.map(|intrinsic_name| {
55-
command
56-
.clone()
57-
.set_input_name(intrinsic_name)
58-
.set_output_name(intrinsic_name)
59-
.make_string()
60-
})
61-
.collect::<Vec<_>>();
52+
let mut command = command.into_command();
53+
command.command_mut().current_dir("c_programs");
6254

63-
compile_c_programs(&compiler_commands)
55+
for input in inputs {
56+
assert!(
57+
std::path::Path::new("c_programs").join(input).exists(),
58+
"{}",
59+
input
60+
);
61+
}
62+
command.command_mut().args(inputs);
63+
64+
if let Some(output) = output {
65+
trace!("running {compiler} to produce {output}");
66+
if output.ends_with(".o") {
67+
command.command_mut().arg("-c");
68+
}
69+
command.command_mut().args(["-o", output]);
70+
} else {
71+
trace!("running {compiler}");
72+
}
73+
74+
trace!("running {compiler}\n{:?}", &command);
75+
76+
if log::log_enabled!(log::Level::Trace) {
77+
command.command_mut().stdout(std::process::Stdio::inherit());
78+
command.command_mut().stderr(std::process::Stdio::inherit());
79+
}
80+
81+
let output = command.output();
82+
83+
trace!("{compiler} is done");
84+
85+
if let Ok(output) = output {
86+
if output.status.success() {
87+
true
88+
} else {
89+
error!(
90+
"Failed to compile code for intrinsics: \n\nstdout:\n{}\n\nstderr:\n{}",
91+
std::str::from_utf8(&output.stdout).unwrap_or(""),
92+
std::str::from_utf8(&output.stderr).unwrap_or("")
93+
);
94+
false
95+
}
96+
} else {
97+
error!("Command failed: {output:#?}");
98+
false
99+
}
64100
}

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

Lines changed: 28 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod intrinsic;
88
mod json_parser;
99
mod types;
1010

11+
use crate::arm::compile::compile_c_arm;
1112
use crate::common::SupportedArchitectureTest;
1213
use crate::common::cli::ProcessedCli;
1314
use crate::common::compare::compare_outputs;
@@ -54,7 +55,7 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
5455
}
5556

5657
fn build_c_file(&self) -> bool {
57-
let compiler = self.cli_options.cpp_compiler.as_deref();
58+
let compiler = self.cli_options.cpp_compiler.as_deref().unwrap();
5859
let target = &self.cli_options.target;
5960
let cxx_toolchain_dir = self.cli_options.cxx_toolchain_dir.as_deref();
6061
let c_target = "aarch64";
@@ -69,36 +70,24 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
6970
.map(|(i, chunk)| {
7071
let c_filename = format!("c_programs/mod_{i}.cpp");
7172
let mut file = File::create(&c_filename).unwrap();
72-
write_mod_cpp(&mut file, &notice, chunk).unwrap();
73+
write_mod_cpp(&mut file, notice, c_target, chunk).unwrap();
7374

7475
// compile this cpp file into a .o file
7576

76-
// clang++ -march=armv8.6-a+crypto+crc+dotprod+fp16+faminmax+lut+sha3 -O2 -ffp-contract=off -Wno-narrowing --target=aarch64-unknown-linux-gnu c_file_0.c -c
77-
let mut cmd = std::process::Command::new("clang++");
78-
cmd.current_dir("c_programs");
79-
80-
cmd.arg("-march=armv8.6-a+crypto+crc+dotprod+fp16+faminmax+lut+sha3");
81-
cmd.arg("-O2");
82-
cmd.arg("-ffp-contract=off");
83-
cmd.arg("-Wno-narrowing");
84-
cmd.arg("--target=aarch64-unknown-linux-gnu");
85-
cmd.arg("-c");
86-
cmd.arg(format!("mod_{i}.cpp"));
87-
88-
let output = cmd.output();
89-
eprintln!(
90-
"{}",
91-
String::from_utf8_lossy(&output.as_ref().unwrap().stderr)
77+
compile_c_arm(
78+
compiler,
79+
target,
80+
cxx_toolchain_dir,
81+
&[format!("mod_{i}.cpp")],
82+
Some(&format!("mod_{i}.o")),
9283
);
93-
assert!(output.unwrap().status.success());
9484

9585
Ok(())
9686
})
9787
.collect::<Result<(), std::io::Error>>()
9888
.unwrap();
9989

100-
let c_filename = format!("c_programs/main.cpp");
101-
let mut file = File::create(&c_filename).unwrap();
90+
let mut file = File::create("c_programs/main.cpp").unwrap();
10291
write_main_cpp(
10392
&mut file,
10493
c_target,
@@ -107,38 +96,18 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
10796
)
10897
.unwrap();
10998

110-
let mut cmd = std::process::Command::new("clang++");
111-
cmd.current_dir("c_programs");
112-
113-
cmd.arg("-march=armv8.6-a+crypto+crc+dotprod+fp16+faminmax+lut+sha3");
114-
cmd.arg("-O2");
115-
cmd.arg("-ffp-contract=off");
116-
cmd.arg("-Wno-narrowing");
117-
cmd.arg("--target=aarch64-unknown-linux-gnu");
118-
cmd.arg(format!("main.cpp"));
99+
let mut inputs = vec![format!("main.cpp")];
119100
for i in 0..Ord::min(available_parallelism, self.intrinsics.len()) {
120-
cmd.arg(format!("mod_{i}.o"));
101+
inputs.push(format!("mod_{i}.o"));
121102
}
122-
cmd.args(&["-o", "intrinsic-test-programs"]);
123103

124-
let output = cmd.output();
125-
eprintln!(
126-
"{}",
127-
String::from_utf8_lossy(&output.as_ref().unwrap().stderr)
128-
);
129-
assert!(output.unwrap().status.success());
130-
131-
// match compiler {
132-
// None => true,
133-
// Some(compiler) => compile_c_arm(
134-
// intrinsics_name_list.unwrap().as_slice(),
135-
// compiler,
136-
// target,
137-
// cxx_toolchain_dir,
138-
// ),
139-
// }
140-
141-
true
104+
compile_c_arm(
105+
compiler,
106+
target,
107+
cxx_toolchain_dir,
108+
&inputs,
109+
Some("intrinsic-test-programs"),
110+
)
142111
}
143112

144113
fn build_rust_file(&self) -> bool {
@@ -171,14 +140,23 @@ impl SupportedArchitectureTest for ArmArchitectureTest {
171140
let toolchain = self.cli_options.toolchain.as_deref();
172141
let linker = self.cli_options.linker.as_deref();
173142

143+
warn!(
144+
"available parallelism: {:?} {}",
145+
std::thread::available_parallelism(),
146+
rayon::current_num_threads(),
147+
);
148+
174149
let notice = &build_notices("// ");
175150
self.intrinsics
176151
.par_chunks(chunk_size)
177152
.enumerate()
178153
.map(|(i, chunk)| {
179154
use std::io::Write;
180155

156+
dbg!(chunk_size, chunk.len());
157+
181158
let rust_filename = format!("rust_programs/src/mod_{i}.rs");
159+
trace!("generating `{rust_filename}`");
182160
let mut file = File::create(rust_filename).unwrap();
183161

184162
write!(file, "{notice}")?;

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,10 @@ impl ProcessedCli {
9090
(None, None)
9191
} else {
9292
(
93-
Some(
94-
cli_options
95-
.toolchain
96-
.map_or_else(String::new, |t| format!("+{t}")),
97-
),
93+
match cli_options.toolchain {
94+
Some(t) => Some(format!("+{t}")),
95+
None => Some(String::new()), // NOTE this is confusing
96+
},
9897
Some(cli_options.cppcompiler),
9998
)
10099
};

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ pub fn compare_outputs(intrinsic_name_list: &Vec<String>, runner: &str, target:
2323
let rust = runner_command(runner)
2424
.arg(format!("target/{target}/release/intrinsic-test-programs"))
2525
.arg(intrinsic_name)
26-
.current_dir("rust_programs")
2726
.output();
2827

2928
let (c, rust) = match (c, rust) {
@@ -33,19 +32,19 @@ pub fn compare_outputs(intrinsic_name_list: &Vec<String>, runner: &str, target:
3332

3433
if !c.status.success() {
3534
error!(
36-
"Failed to run C program for intrinsic {intrinsic_name}\nstdout: {stdout}\nstderr: {stderr}",
35+
"Failed to run C program for intrinsic `{intrinsic_name}`\nstdout: {stdout}\nstderr: {stderr}",
3736
stdout = std::str::from_utf8(&c.stdout).unwrap_or(""),
3837
stderr = std::str::from_utf8(&c.stderr).unwrap_or(""),
39-
);
38+
);
4039
return Some(FailureReason::RunC(intrinsic_name.clone()));
4140
}
4241

4342
if !rust.status.success() {
4443
error!(
45-
"Failed to run Rust program for intrinsic {intrinsic_name}\nstdout: {stdout}\nstderr: {stderr}",
44+
"Failed to run Rust program for intrinsic `{intrinsic_name}`\nstdout: {stdout}\nstderr: {stderr}",
4645
stdout = String::from_utf8_lossy(&rust.stdout),
4746
stderr = String::from_utf8_lossy(&rust.stderr),
48-
);
47+
);
4948
return Some(FailureReason::RunRust(intrinsic_name.clone()));
5049
}
5150

0 commit comments

Comments
 (0)