Skip to content

Commit

Permalink
Rollup merge of rust-lang#63352 - jgalenson:reproducible-lto, r=alexc…
Browse files Browse the repository at this point in the history
…richton

Sort the fat LTO modules to produce deterministic output.

Some projects that use LTO for their release builds are not reproducible.  We can fix this by sorting the fat LTO modules before using them.

It might also be useful to do this for thin LTO, but I couldn't get that to work to test it so I didn't do it.
  • Loading branch information
Centril committed Aug 8, 2019
2 parents 8abf29f + 3e6a927 commit ac84eb0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/librustc_codegen_llvm/back/lto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ fn fat_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
// and we want to move everything to the same LLVM context. Currently the
// way we know of to do that is to serialize them to a string and them parse
// them later. Not great but hey, that's why it's "fat" LTO, right?
serialized_modules.extend(modules.into_iter().map(|module| {
let mut new_modules = modules.into_iter().map(|module| {
match module {
FatLTOInput::InMemory(module) => {
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
Expand All @@ -277,7 +277,10 @@ fn fat_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
(SerializedModule::Local(buffer), llmod_id)
}
}
}));
}).collect::<Vec<_>>();
// Sort the modules to ensure we produce deterministic results.
new_modules.sort_by(|module1, module2| module1.1.partial_cmp(&module2.1).unwrap());
serialized_modules.extend(new_modules);
serialized_modules.extend(cached_modules.into_iter().map(|(buffer, wp)| {
(buffer, CString::new(wp.cgu_name).unwrap())
}));
Expand Down
11 changes: 10 additions & 1 deletion src/test/run-make-fulldeps/reproducible-build/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ all: \
link_paths \
remap_paths \
different_source_dirs \
extern_flags
extern_flags \
fat_lto

smoke:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
Expand Down Expand Up @@ -76,3 +77,11 @@ extern_flags:
--extern reproducible_build_aux=$(TMPDIR)/libbar.rlib \
--crate-type rlib
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1

fat_lto:
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
$(RUSTC) reproducible-build-aux.rs
$(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1
cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a
$(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1
cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1

0 comments on commit ac84eb0

Please sign in to comment.