Skip to content

Commit 6fa32f1

Browse files
Adapt backend to trans::partitioning dictating the codegen-unit setup.
1 parent c115ad2 commit 6fa32f1

File tree

8 files changed

+181
-95
lines changed

8 files changed

+181
-95
lines changed

src/librustc/session/config.rs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,23 +197,70 @@ pub struct OutputFilenames {
197197
pub outputs: HashMap<OutputType, Option<PathBuf>>,
198198
}
199199

200+
/// Codegen unit names generated by the numbered naming scheme will contain this
201+
/// marker right before the index of the codegen unit.
202+
pub const NUMBERED_CODEGEN_UNIT_MARKER: &'static str = ".cgu-";
203+
200204
impl OutputFilenames {
201205
pub fn path(&self, flavor: OutputType) -> PathBuf {
202206
self.outputs.get(&flavor).and_then(|p| p.to_owned())
203207
.or_else(|| self.single_output_file.clone())
204-
.unwrap_or_else(|| self.temp_path(flavor))
208+
.unwrap_or_else(|| self.temp_path(flavor, None))
205209
}
206210

207-
pub fn temp_path(&self, flavor: OutputType) -> PathBuf {
211+
/// Get the path where a compilation artifact of the given type for the
212+
/// given codegen unit should be placed on disk. If codegen_unit_name is
213+
/// None, a path distinct from those of any codegen unit will be generated.
214+
pub fn temp_path(&self,
215+
flavor: OutputType,
216+
codegen_unit_name: Option<&str>)
217+
-> PathBuf {
218+
let extension = match flavor {
219+
OutputType::Bitcode => "bc",
220+
OutputType::Assembly => "s",
221+
OutputType::LlvmAssembly => "ll",
222+
OutputType::Object => "o",
223+
OutputType::DepInfo => "d",
224+
OutputType::Exe => "",
225+
};
226+
227+
self.temp_path_ext(extension, codegen_unit_name)
228+
}
229+
230+
/// Like temp_path, but also supports things where there is no corresponding
231+
/// OutputType, like no-opt-bitcode or lto-bitcode.
232+
pub fn temp_path_ext(&self,
233+
ext: &str,
234+
codegen_unit_name: Option<&str>)
235+
-> PathBuf {
208236
let base = self.out_directory.join(&self.filestem());
209-
match flavor {
210-
OutputType::Bitcode => base.with_extension("bc"),
211-
OutputType::Assembly => base.with_extension("s"),
212-
OutputType::LlvmAssembly => base.with_extension("ll"),
213-
OutputType::Object => base.with_extension("o"),
214-
OutputType::DepInfo => base.with_extension("d"),
215-
OutputType::Exe => base,
237+
238+
let mut extension = String::new();
239+
240+
if let Some(codegen_unit_name) = codegen_unit_name {
241+
if codegen_unit_name.contains(NUMBERED_CODEGEN_UNIT_MARKER) {
242+
// If we use the numbered naming scheme for modules, we don't want
243+
// the files to look like <crate-name><extra>.<crate-name>.<index>.<ext>
244+
// but simply <crate-name><extra>.<index>.<ext>
245+
let marker_offset = codegen_unit_name.rfind(NUMBERED_CODEGEN_UNIT_MARKER)
246+
.unwrap();
247+
let index_offset = marker_offset + NUMBERED_CODEGEN_UNIT_MARKER.len();
248+
extension.push_str(&codegen_unit_name[index_offset .. ]);
249+
} else {
250+
extension.push_str(codegen_unit_name);
251+
};
252+
}
253+
254+
if !ext.is_empty() {
255+
if !extension.is_empty() {
256+
extension.push_str(".");
257+
}
258+
259+
extension.push_str(ext);
216260
}
261+
262+
let path = base.with_extension(&extension[..]);
263+
path
217264
}
218265

219266
pub fn with_extension(&self, extension: &str) -> PathBuf {

src/librustc_driver/driver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session,
10711071

10721072
// Remove assembly source, unless --save-temps was specified
10731073
if !sess.opts.cg.save_temps {
1074-
fs::remove_file(&outputs.temp_path(OutputType::Assembly)).unwrap();
1074+
fs::remove_file(&outputs.temp_path(OutputType::Assembly, None)).unwrap();
10751075
}
10761076
} else {
10771077
time(sess.time_passes(),

src/librustc_trans/back/link.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub fn link_binary(sess: &Session,
204204

205205
// Remove the temporary object file and metadata if we aren't saving temps
206206
if !sess.opts.cg.save_temps {
207-
for obj in object_filenames(sess, outputs) {
207+
for obj in object_filenames(trans, outputs) {
208208
remove(sess, &obj);
209209
}
210210
remove(sess, &outputs.with_extension("metadata.o"));
@@ -315,7 +315,7 @@ fn link_binary_output(sess: &Session,
315315
crate_type: config::CrateType,
316316
outputs: &OutputFilenames,
317317
crate_name: &str) -> PathBuf {
318-
let objects = object_filenames(sess, outputs);
318+
let objects = object_filenames(trans, outputs);
319319
let default_filename = filename_for_input(sess, crate_type, crate_name,
320320
outputs);
321321
let out_filename = outputs.outputs.get(&OutputType::Exe)
@@ -355,10 +355,11 @@ fn link_binary_output(sess: &Session,
355355
out_filename
356356
}
357357

358-
fn object_filenames(sess: &Session, outputs: &OutputFilenames) -> Vec<PathBuf> {
359-
(0..sess.opts.cg.codegen_units).map(|i| {
360-
let ext = format!("{}.o", i);
361-
outputs.temp_path(OutputType::Object).with_extension(&ext)
358+
fn object_filenames(trans: &CrateTranslation,
359+
outputs: &OutputFilenames)
360+
-> Vec<PathBuf> {
361+
trans.modules.iter().map(|module| {
362+
outputs.temp_path(OutputType::Object, Some(&module.name[..]))
362363
}).collect()
363364
}
364365

@@ -503,7 +504,7 @@ fn link_rlib<'a>(sess: &'a Session,
503504
ab.add_file(&bc_deflated_filename);
504505

505506
// See the bottom of back::write::run_passes for an explanation
506-
// of when we do and don't keep .0.bc files around.
507+
// of when we do and don't keep .#module-name#.bc files around.
507508
let user_wants_numbered_bitcode =
508509
sess.opts.output_types.contains_key(&OutputType::Bitcode) &&
509510
sess.opts.cg.codegen_units > 1;

src/librustc_trans/back/lto.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ use libc;
2222
use flate;
2323

2424
use std::ffi::CString;
25+
use std::path::Path;
2526

2627
pub fn run(sess: &session::Session, llmod: ModuleRef,
2728
tm: TargetMachineRef, reachable: &[String],
2829
config: &ModuleConfig,
29-
name_extra: &str,
30-
output_names: &config::OutputFilenames) {
30+
temp_no_opt_bc_filename: &Path) {
3131
if sess.opts.cg.prefer_dynamic {
3232
sess.struct_err("cannot prefer dynamic linking when performing LTO")
3333
.note("only 'staticlib', 'bin', and 'cdylib' outputs are \
@@ -132,8 +132,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef,
132132
}
133133

134134
if sess.opts.cg.save_temps {
135-
let path = output_names.with_extension(&format!("{}.no-opt.lto.bc", name_extra));
136-
let cstr = path2cstr(&path);
135+
let cstr = path2cstr(temp_no_opt_bc_filename);
137136
unsafe {
138137
llvm::LLVMWriteBitcodeToFile(llmod, cstr.as_ptr());
139138
}

0 commit comments

Comments
 (0)