Skip to content

Commit 9c372d8

Browse files
Prepend temp files with a string per invocation of rustc
1 parent effef88 commit 9c372d8

File tree

17 files changed

+260
-50
lines changed

17 files changed

+260
-50
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4407,6 +4407,7 @@ dependencies = [
44074407
"bitflags",
44084408
"getopts",
44094409
"libc",
4410+
"rand 0.9.0",
44104411
"rustc_abi",
44114412
"rustc_ast",
44124413
"rustc_data_structures",

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+27-8
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,11 @@ fn produce_final_output_artifacts(
169169
if codegen_results.modules.len() == 1 {
170170
// 1) Only one codegen unit. In this case it's no difficulty
171171
// to copy `foo.0.x` to `foo.x`.
172-
let path =
173-
crate_output.temp_path_for_cgu(output_type, &codegen_results.modules[0].name);
172+
let path = crate_output.temp_path_for_cgu(
173+
output_type,
174+
&codegen_results.modules[0].name,
175+
sess.invocation_temp.as_deref(),
176+
);
174177
let output = crate_output.path(output_type);
175178
if !output_type.is_text_output() && output.is_tty() {
176179
sess.dcx()
@@ -345,6 +348,7 @@ fn make_module(sess: &Session, name: String) -> UnwindModule<ObjectModule> {
345348

346349
fn emit_cgu(
347350
output_filenames: &OutputFilenames,
351+
invocation_temp: Option<&str>,
348352
prof: &SelfProfilerRef,
349353
name: String,
350354
module: UnwindModule<ObjectModule>,
@@ -360,6 +364,7 @@ fn emit_cgu(
360364

361365
let module_regular = emit_module(
362366
output_filenames,
367+
invocation_temp,
363368
prof,
364369
product.object,
365370
ModuleKind::Regular,
@@ -385,6 +390,7 @@ fn emit_cgu(
385390

386391
fn emit_module(
387392
output_filenames: &OutputFilenames,
393+
invocation_temp: Option<&str>,
388394
prof: &SelfProfilerRef,
389395
mut object: cranelift_object::object::write::Object<'_>,
390396
kind: ModuleKind,
@@ -403,7 +409,7 @@ fn emit_module(
403409
object.set_section_data(comment_section, producer, 1);
404410
}
405411

406-
let tmp_file = output_filenames.temp_path_for_cgu(OutputType::Object, &name);
412+
let tmp_file = output_filenames.temp_path_for_cgu(OutputType::Object, &name, invocation_temp);
407413
let file = match File::create(&tmp_file) {
408414
Ok(file) => file,
409415
Err(err) => return Err(format!("error creating object file: {}", err)),
@@ -443,8 +449,11 @@ fn reuse_workproduct_for_cgu(
443449
cgu: &CodegenUnit<'_>,
444450
) -> Result<ModuleCodegenResult, String> {
445451
let work_product = cgu.previous_work_product(tcx);
446-
let obj_out_regular =
447-
tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, cgu.name().as_str());
452+
let obj_out_regular = tcx.output_filenames(()).temp_path_for_cgu(
453+
OutputType::Object,
454+
cgu.name().as_str(),
455+
tcx.sess.invocation_temp.as_deref(),
456+
);
448457
let source_file_regular = rustc_incremental::in_incr_comp_dir_sess(
449458
&tcx.sess,
450459
&work_product.saved_files.get("o").expect("no saved object file in work product"),
@@ -589,13 +598,19 @@ fn module_codegen(
589598

590599
let global_asm_object_file =
591600
profiler.generic_activity_with_arg("compile assembly", &*cgu_name).run(|| {
592-
crate::global_asm::compile_global_asm(&global_asm_config, &cgu_name, &cx.global_asm)
601+
crate::global_asm::compile_global_asm(
602+
&global_asm_config,
603+
&cgu_name,
604+
&cx.global_asm,
605+
cx.invocation_temp.as_deref(),
606+
)
593607
})?;
594608

595609
let codegen_result =
596610
profiler.generic_activity_with_arg("write object file", &*cgu_name).run(|| {
597611
emit_cgu(
598612
&global_asm_config.output_filenames,
613+
cx.invocation_temp.as_deref(),
599614
&profiler,
600615
cgu_name,
601616
module,
@@ -620,8 +635,11 @@ fn emit_metadata_module(tcx: TyCtxt<'_>, metadata: &EncodedMetadata) -> Compiled
620635
.as_str()
621636
.to_string();
622637

623-
let tmp_file =
624-
tcx.output_filenames(()).temp_path_for_cgu(OutputType::Metadata, &metadata_cgu_name);
638+
let tmp_file = tcx.output_filenames(()).temp_path_for_cgu(
639+
OutputType::Metadata,
640+
&metadata_cgu_name,
641+
tcx.sess.invocation_temp.as_deref(),
642+
);
625643

626644
let symbol_name = rustc_middle::middle::exported_symbols::metadata_symbol_name(tcx);
627645
let obj = create_compressed_metadata_file(tcx.sess, metadata, &symbol_name);
@@ -651,6 +669,7 @@ fn emit_allocator_module(tcx: TyCtxt<'_>) -> Option<CompiledModule> {
651669

652670
match emit_module(
653671
tcx.output_filenames(()),
672+
tcx.sess.invocation_temp.as_deref(),
654673
&tcx.sess.prof,
655674
product.object,
656675
ModuleKind::Allocator,

compiler/rustc_codegen_cranelift/src/global_asm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub(crate) fn compile_global_asm(
132132
config: &GlobalAsmConfig,
133133
cgu_name: &str,
134134
global_asm: &str,
135+
invocation_temp: Option<&str>,
135136
) -> Result<Option<PathBuf>, String> {
136137
if global_asm.is_empty() {
137138
return Ok(None);
@@ -146,7 +147,7 @@ pub(crate) fn compile_global_asm(
146147
global_asm.push('\n');
147148

148149
let global_asm_object_file = add_file_stem_postfix(
149-
config.output_filenames.temp_path_for_cgu(OutputType::Object, cgu_name),
150+
config.output_filenames.temp_path_for_cgu(OutputType::Object, cgu_name, invocation_temp),
150151
".asm",
151152
);
152153

compiler/rustc_codegen_cranelift/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl<F: Fn() -> String> Drop for PrintOnPanic<F> {
124124
/// inside a single codegen unit with the exception of the Cranelift [`Module`](cranelift_module::Module).
125125
struct CodegenCx {
126126
output_filenames: Arc<OutputFilenames>,
127+
invocation_temp: Option<String>,
127128
should_write_ir: bool,
128129
global_asm: String,
129130
inline_asm_index: usize,
@@ -142,6 +143,7 @@ impl CodegenCx {
142143
};
143144
CodegenCx {
144145
output_filenames: tcx.output_filenames(()).clone(),
146+
invocation_temp: tcx.sess.invocation_temp.clone(),
145147
should_write_ir: crate::pretty_clif::should_write_ir(tcx),
146148
global_asm: String::new(),
147149
inline_asm_index: 0,

compiler/rustc_codegen_gcc/src/back/write.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,16 @@ pub(crate) unsafe fn codegen(
3131
// TODO(antoyo): remove this environment variable.
3232
let fat_lto = env::var("EMBED_LTO_BITCODE").as_deref() == Ok("1");
3333

34-
let bc_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Bitcode, &module.name);
35-
let obj_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Object, &module.name);
34+
let bc_out = cgcx.output_filenames.temp_path_for_cgu(
35+
OutputType::Bitcode,
36+
&module.name,
37+
cgcx.invocation_temp.as_deref(),
38+
);
39+
let obj_out = cgcx.output_filenames.temp_path_for_cgu(
40+
OutputType::Object,
41+
&module.name,
42+
cgcx.invocation_temp.as_deref(),
43+
);
3644

3745
if config.bitcode_needed() {
3846
if fat_lto {
@@ -113,15 +121,22 @@ pub(crate) unsafe fn codegen(
113121
}
114122

115123
if config.emit_ir {
116-
let out =
117-
cgcx.output_filenames.temp_path_for_cgu(OutputType::LlvmAssembly, &module.name);
124+
let out = cgcx.output_filenames.temp_path_for_cgu(
125+
OutputType::LlvmAssembly,
126+
&module.name,
127+
cgcx.invocation_temp.as_deref(),
128+
);
118129
std::fs::write(out, "").expect("write file");
119130
}
120131

121132
if config.emit_asm {
122133
let _timer =
123134
cgcx.prof.generic_activity_with_arg("GCC_module_codegen_emit_asm", &*module.name);
124-
let path = cgcx.output_filenames.temp_path_for_cgu(OutputType::Assembly, &module.name);
135+
let path = cgcx.output_filenames.temp_path_for_cgu(
136+
OutputType::Assembly,
137+
&module.name,
138+
cgcx.invocation_temp.as_deref(),
139+
);
125140
context.compile_to_file(OutputKind::Assembler, path.to_str().expect("path to str"));
126141
}
127142

@@ -235,6 +250,7 @@ pub(crate) unsafe fn codegen(
235250
config.emit_asm,
236251
config.emit_ir,
237252
&cgcx.output_filenames,
253+
cgcx.invocation_temp.as_deref(),
238254
))
239255
}
240256

compiler/rustc_codegen_llvm/src/back/write.rs

+45-12
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,17 @@ pub(crate) fn create_target_machine(tcx: TyCtxt<'_>, mod_name: &str) -> OwnedTar
120120
tcx.sess.split_debuginfo(),
121121
tcx.sess.opts.unstable_opts.split_dwarf_kind,
122122
mod_name,
123+
tcx.sess.invocation_temp.as_deref(),
123124
)
124125
} else {
125126
None
126127
};
127128

128-
let output_obj_file =
129-
Some(tcx.output_filenames(()).temp_path_for_cgu(OutputType::Object, mod_name));
129+
let output_obj_file = Some(tcx.output_filenames(()).temp_path_for_cgu(
130+
OutputType::Object,
131+
mod_name,
132+
tcx.sess.invocation_temp.as_deref(),
133+
));
130134
let config = TargetMachineFactoryConfig { split_dwarf_file, output_obj_file };
131135

132136
target_machine_factory(
@@ -330,7 +334,11 @@ pub(crate) fn save_temp_bitcode(
330334
return;
331335
}
332336
let ext = format!("{name}.bc");
333-
let path = cgcx.output_filenames.temp_path_ext_for_cgu(&ext, &module.name);
337+
let path = cgcx.output_filenames.temp_path_ext_for_cgu(
338+
&ext,
339+
&module.name,
340+
cgcx.invocation_temp.as_deref(),
341+
);
334342
write_bitcode_to_file(module, &path)
335343
}
336344

@@ -694,7 +702,11 @@ pub(crate) unsafe fn optimize(
694702
let _handlers = DiagnosticHandlers::new(cgcx, dcx, llcx, module, CodegenDiagnosticsStage::Opt);
695703

696704
if config.emit_no_opt_bc {
697-
let out = cgcx.output_filenames.temp_path_ext_for_cgu("no-opt.bc", &module.name);
705+
let out = cgcx.output_filenames.temp_path_ext_for_cgu(
706+
"no-opt.bc",
707+
&module.name,
708+
cgcx.invocation_temp.as_deref(),
709+
);
698710
write_bitcode_to_file(module, &out)
699711
}
700712

@@ -739,8 +751,11 @@ pub(crate) unsafe fn optimize(
739751
if let Some(thin_lto_buffer) = thin_lto_buffer {
740752
let thin_lto_buffer = unsafe { ThinBuffer::from_raw_ptr(thin_lto_buffer) };
741753
module.thin_lto_buffer = Some(thin_lto_buffer.data().to_vec());
742-
let bc_summary_out =
743-
cgcx.output_filenames.temp_path_for_cgu(OutputType::ThinLinkBitcode, &module.name);
754+
let bc_summary_out = cgcx.output_filenames.temp_path_for_cgu(
755+
OutputType::ThinLinkBitcode,
756+
&module.name,
757+
cgcx.invocation_temp.as_deref(),
758+
);
744759
if config.emit_thin_lto_summary
745760
&& let Some(thin_link_bitcode_filename) = bc_summary_out.file_name()
746761
{
@@ -808,8 +823,16 @@ pub(crate) unsafe fn codegen(
808823
// copy it to the .o file, and delete the bitcode if it wasn't
809824
// otherwise requested.
810825

811-
let bc_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Bitcode, &module.name);
812-
let obj_out = cgcx.output_filenames.temp_path_for_cgu(OutputType::Object, &module.name);
826+
let bc_out = cgcx.output_filenames.temp_path_for_cgu(
827+
OutputType::Bitcode,
828+
&module.name,
829+
cgcx.invocation_temp.as_deref(),
830+
);
831+
let obj_out = cgcx.output_filenames.temp_path_for_cgu(
832+
OutputType::Object,
833+
&module.name,
834+
cgcx.invocation_temp.as_deref(),
835+
);
813836

814837
if config.bitcode_needed() {
815838
if config.emit_bc || config.emit_obj == EmitObj::Bitcode {
@@ -851,8 +874,11 @@ pub(crate) unsafe fn codegen(
851874
if config.emit_ir {
852875
let _timer =
853876
cgcx.prof.generic_activity_with_arg("LLVM_module_codegen_emit_ir", &*module.name);
854-
let out =
855-
cgcx.output_filenames.temp_path_for_cgu(OutputType::LlvmAssembly, &module.name);
877+
let out = cgcx.output_filenames.temp_path_for_cgu(
878+
OutputType::LlvmAssembly,
879+
&module.name,
880+
cgcx.invocation_temp.as_deref(),
881+
);
856882
let out_c = path_to_c_string(&out);
857883

858884
extern "C" fn demangle_callback(
@@ -894,7 +920,11 @@ pub(crate) unsafe fn codegen(
894920
if config.emit_asm {
895921
let _timer =
896922
cgcx.prof.generic_activity_with_arg("LLVM_module_codegen_emit_asm", &*module.name);
897-
let path = cgcx.output_filenames.temp_path_for_cgu(OutputType::Assembly, &module.name);
923+
let path = cgcx.output_filenames.temp_path_for_cgu(
924+
OutputType::Assembly,
925+
&module.name,
926+
cgcx.invocation_temp.as_deref(),
927+
);
898928

899929
// We can't use the same module for asm and object code output,
900930
// because that triggers various errors like invalid IR or broken
@@ -924,7 +954,9 @@ pub(crate) unsafe fn codegen(
924954
.prof
925955
.generic_activity_with_arg("LLVM_module_codegen_emit_obj", &*module.name);
926956

927-
let dwo_out = cgcx.output_filenames.temp_path_dwo_for_cgu(&module.name);
957+
let dwo_out = cgcx
958+
.output_filenames
959+
.temp_path_dwo_for_cgu(&module.name, cgcx.invocation_temp.as_deref());
928960
let dwo_out = match (cgcx.split_debuginfo, cgcx.split_dwarf_kind) {
929961
// Don't change how DWARF is emitted when disabled.
930962
(SplitDebuginfo::Off, _) => None,
@@ -989,6 +1021,7 @@ pub(crate) unsafe fn codegen(
9891021
config.emit_asm,
9901022
config.emit_ir,
9911023
&cgcx.output_filenames,
1024+
cgcx.invocation_temp.as_deref(),
9921025
))
9931026
}
9941027

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,7 @@ pub(crate) fn build_compile_unit_di_node<'ll, 'tcx>(
911911
tcx.sess.split_debuginfo(),
912912
tcx.sess.opts.unstable_opts.split_dwarf_kind,
913913
codegen_unit_name,
914+
tcx.sess.invocation_temp.as_deref(),
914915
) {
915916
// We get a path relative to the working directory from split_dwarf_path
916917
Some(tcx.sess.source_map().path_mapping().to_real_filename(f))

compiler/rustc_codegen_ssa/src/back/link.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ pub fn link_binary(
112112
codegen_results.crate_info.local_crate_name,
113113
);
114114
let crate_name = format!("{}", codegen_results.crate_info.local_crate_name);
115-
let out_filename = output.file_for_writing(outputs, OutputType::Exe, &crate_name);
115+
let out_filename = output.file_for_writing(
116+
outputs,
117+
OutputType::Exe,
118+
&crate_name,
119+
sess.invocation_temp.as_deref(),
120+
);
116121
match crate_type {
117122
CrateType::Rlib => {
118123
let _timer = sess.timer("link_rlib");

0 commit comments

Comments
 (0)