Skip to content

Commit c7995b1

Browse files
committed
Add thinlto support to codegen, assembly and coverage tests
1 parent e1e60b6 commit c7995b1

File tree

5 files changed

+79
-15
lines changed

5 files changed

+79
-15
lines changed

src/tools/compiletest/src/runtest.rs

+54-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,53 @@ 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 file_stem = self.testpaths.file.file_stem().unwrap().to_str().unwrap();
2803+
let mut output_file = None;
2804+
for entry in self.output_base_dir().read_dir().unwrap() {
2805+
if let Ok(entry) = entry {
2806+
let entry_path = entry.path();
2807+
let entry_file = entry_path.file_name().unwrap().to_str().unwrap();
2808+
if entry_file.starts_with(&format!("{}.{}", file_stem, file_stem))
2809+
&& entry_file.ends_with(extension)
2810+
{
2811+
assert!(
2812+
output_file.is_none(),
2813+
"thinlto doesn't support multiple cgu tests"
2814+
);
2815+
output_file = Some(entry_file.to_string());
2816+
}
2817+
}
2818+
}
2819+
if let Some(output_file) = output_file {
2820+
self.output_base_dir().join(output_file)
2821+
} else {
2822+
self.output_base_name().with_extension(extension)
2823+
}
2824+
} else {
2825+
self.output_base_name().with_extension(extension)
2826+
}
2827+
}
2828+
27882829
// codegen tests (using FileCheck)
27892830

2790-
fn compile_test_and_save_ir(&self) -> ProcRes {
2791-
let output_file = TargetLocation::ThisDirectory(self.output_base_dir());
2831+
fn compile_test_and_save_ir(&self) -> (ProcRes, PathBuf) {
2832+
let output_file = self.get_output_file("ll");
27922833
let input_file = &self.testpaths.file;
27932834
let rustc = self.make_compile_args(
27942835
input_file,
@@ -2799,15 +2840,13 @@ impl<'test> TestCx<'test> {
27992840
Vec::new(),
28002841
);
28012842

2802-
self.compose_and_run_compiler(rustc, None)
2843+
let proc_res = self.compose_and_run_compiler(rustc, None);
2844+
let output_path = self.get_filecheck_file("ll");
2845+
(proc_res, output_path)
28032846
}
28042847

28052848
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());
2849+
let output_file = self.get_output_file("s");
28112850
let input_file = &self.testpaths.file;
28122851

28132852
let mut emit = Emit::None;
@@ -2837,7 +2876,9 @@ impl<'test> TestCx<'test> {
28372876
Vec::new(),
28382877
);
28392878

2840-
(self.compose_and_run_compiler(rustc, None), output_path)
2879+
let proc_res = self.compose_and_run_compiler(rustc, None);
2880+
let output_path = self.get_filecheck_file("s");
2881+
(proc_res, output_path)
28412882
}
28422883

28432884
fn verify_with_filecheck(&self, output: &Path) -> ProcRes {
@@ -2870,16 +2911,14 @@ impl<'test> TestCx<'test> {
28702911
self.fatal("missing --llvm-filecheck");
28712912
}
28722913

2873-
let proc_res = self.compile_test_and_save_ir();
2914+
let (proc_res, output_path) = self.compile_test_and_save_ir();
28742915
if !proc_res.status.success() {
28752916
self.fatal_proc_rec("compilation failed!", &proc_res);
28762917
}
28772918

28782919
if let Some(PassMode::Build) = self.pass_mode() {
28792920
return;
28802921
}
2881-
2882-
let output_path = self.output_base_name().with_extension("ll");
28832922
let proc_res = self.verify_with_filecheck(&output_path);
28842923
if !proc_res.status.success() {
28852924
self.fatal_proc_rec("verification with 'FileCheck' failed", &proc_res);

tests/assembly/thinlto.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/thinlto.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/thinlto.cov-map

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function name: thinlto::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/thinlto.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)