Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 650b401

Browse files
committedNov 21, 2023
Add thinlto support to codegen, assembly and coverage tests
1 parent e1e60b6 commit 650b401

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed
 

‎src/tools/compiletest/src/runtest.rs

+55-15
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,12 @@ impl<'test> TestCx<'test> {
474474
self.fatal("missing --coverage-dump");
475475
};
476476

477-
let proc_res = self.compile_test_and_save_ir();
477+
let (proc_res, llvm_ir_path) = self.compile_test_and_save_ir();
478478
if !proc_res.status.success() {
479479
self.fatal_proc_rec("compilation failed!", &proc_res);
480480
}
481481
drop(proc_res);
482482

483-
let llvm_ir_path = self.output_base_name().with_extension("ll");
484-
485483
let mut dump_command = Command::new(coverage_dump_path);
486484
dump_command.arg(llvm_ir_path);
487485
let proc_res = self.run_command_to_procres(&mut dump_command);
@@ -2785,10 +2783,54 @@ impl<'test> TestCx<'test> {
27852783
proc_res.fatal(None, || on_failure(*self));
27862784
}
27872785

2786+
fn get_output_file(&self, extension: &str) -> TargetLocation {
2787+
let thin_lto = self.props.compile_flags.iter().any(|s| s.ends_with("lto=thin"));
2788+
if thin_lto {
2789+
TargetLocation::ThisDirectory(self.output_base_dir())
2790+
} else {
2791+
// This works with both `--emit asm` (as default output name for the assembly)
2792+
// and `ptx-linker` because the latter can write output at requested location.
2793+
let output_path = self.output_base_name().with_extension(extension);
2794+
let output_file = TargetLocation::ThisFile(output_path.clone());
2795+
output_file
2796+
}
2797+
}
2798+
2799+
fn get_filecheck_file(&self, extension: &str) -> PathBuf {
2800+
let thin_lto = self.props.compile_flags.iter().any(|s| s.ends_with("lto=thin"));
2801+
if thin_lto {
2802+
let name = self.testpaths.file.file_stem().unwrap().to_str().unwrap();
2803+
let canonical_name = name.replace('-', "_");
2804+
let mut output_file = None;
2805+
for entry in self.output_base_dir().read_dir().unwrap() {
2806+
if let Ok(entry) = entry {
2807+
let entry_path = entry.path();
2808+
let entry_file = entry_path.file_name().unwrap().to_str().unwrap();
2809+
if entry_file.starts_with(&format!("{}.{}", name, canonical_name))
2810+
&& entry_file.ends_with(extension)
2811+
{
2812+
assert!(
2813+
output_file.is_none(),
2814+
"thinlto doesn't support multiple cgu tests"
2815+
);
2816+
output_file = Some(entry_file.to_string());
2817+
}
2818+
}
2819+
}
2820+
if let Some(output_file) = output_file {
2821+
self.output_base_dir().join(output_file)
2822+
} else {
2823+
self.output_base_name().with_extension(extension)
2824+
}
2825+
} else {
2826+
self.output_base_name().with_extension(extension)
2827+
}
2828+
}
2829+
27882830
// codegen tests (using FileCheck)
27892831

2790-
fn compile_test_and_save_ir(&self) -> ProcRes {
2791-
let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
2832+
fn compile_test_and_save_ir(&self) -> (ProcRes, PathBuf) {
2833+
let output_file = self.get_output_file("ll");
27922834
let input_file = &self.testpaths.file;
27932835
let rustc = self.make_compile_args(
27942836
input_file,
@@ -2799,15 +2841,13 @@ impl<'test> TestCx<'test> {
27992841
Vec::new(),
28002842
);
28012843

2802-
self.compose_and_run_compiler(rustc, None)
2844+
let proc_res = self.compose_and_run_compiler(rustc, None);
2845+
let output_path = self.get_filecheck_file("ll");
2846+
(proc_res, output_path)
28032847
}
28042848

28052849
fn compile_test_and_save_assembly(&self) -> (ProcRes, PathBuf) {
2806-
// This works with both `--emit asm` (as default output name for the assembly)
2807-
// and `ptx-linker` because the latter can write output at requested location.
2808-
let output_path = self.output_base_name().with_extension("s");
2809-
2810-
let output_file = TargetLocation::ThisFile(output_path.clone());
2850+
let output_file = self.get_output_file("s");
28112851
let input_file = &self.testpaths.file;
28122852

28132853
let mut emit = Emit::None;
@@ -2837,7 +2877,9 @@ impl<'test> TestCx<'test> {
28372877
Vec::new(),
28382878
);
28392879

2840-
(self.compose_and_run_compiler(rustc, None), output_path)
2880+
let proc_res = self.compose_and_run_compiler(rustc, None);
2881+
let output_path = self.get_filecheck_file("s");
2882+
(proc_res, output_path)
28412883
}
28422884

28432885
fn verify_with_filecheck(&self, output: &Path) -> ProcRes {
@@ -2870,16 +2912,14 @@ impl<'test> TestCx<'test> {
28702912
self.fatal("missing --llvm-filecheck");
28712913
}
28722914

2873-
let proc_res = self.compile_test_and_save_ir();
2915+
let (proc_res, output_path) = self.compile_test_and_save_ir();
28742916
if !proc_res.status.success() {
28752917
self.fatal_proc_rec("compilation failed!", &proc_res);
28762918
}
28772919

28782920
if let Some(PassMode::Build) = self.pass_mode() {
28792921
return;
28802922
}
2881-
2882-
let output_path = self.output_base_name().with_extension("ll");
28832923
let proc_res = self.verify_with_filecheck(&output_path);
28842924
if !proc_res.status.success() {
28852925
self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);

‎tests/assembly/thin-lto.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: -O -C lto=thin -C prefer-dynamic=no
2+
// assembly-output: emit-asm
3+
4+
// CHECK: main
5+
6+
pub fn main() {
7+
}

‎tests/codegen/thin-lto.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// compile-flags: -O -C lto=thin -C prefer-dynamic=no
2+
3+
// CHECK: main
4+
5+
pub fn main() {
6+
}

‎tests/coverage/thin-lto.cov-map

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function name: thin_lto::main
2+
Raw bytes (9): 0x[01, 01, 00, 01, 01, 03, 01, 01, 02]
3+
Number of files: 1
4+
- file 0 => global file 1
5+
Number of expressions: 0
6+
Number of file 0 mappings: 1
7+
- Code(Counter(0)) at (prev + 3, 1) to (start + 1, 2)
8+

‎tests/coverage/thin-lto.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -O -C lto=thin -C prefer-dynamic=no
2+
3+
pub fn main() {
4+
}

0 commit comments

Comments
 (0)
Please sign in to comment.