Skip to content

Commit ac84eb0

Browse files
authored
Rollup merge of rust-lang#63352 - jgalenson:reproducible-lto, r=alexcrichton
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.
2 parents 8abf29f + 3e6a927 commit ac84eb0

File tree

2 files changed

+15
-3
lines changed
  • src
    • librustc_codegen_llvm/back
    • test/run-make-fulldeps/reproducible-build

2 files changed

+15
-3
lines changed

src/librustc_codegen_llvm/back/lto.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fn fat_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
265265
// and we want to move everything to the same LLVM context. Currently the
266266
// way we know of to do that is to serialize them to a string and them parse
267267
// them later. Not great but hey, that's why it's "fat" LTO, right?
268-
serialized_modules.extend(modules.into_iter().map(|module| {
268+
let mut new_modules = modules.into_iter().map(|module| {
269269
match module {
270270
FatLTOInput::InMemory(module) => {
271271
let buffer = ModuleBuffer::new(module.module_llvm.llmod());
@@ -277,7 +277,10 @@ fn fat_lto(cgcx: &CodegenContext<LlvmCodegenBackend>,
277277
(SerializedModule::Local(buffer), llmod_id)
278278
}
279279
}
280-
}));
280+
}).collect::<Vec<_>>();
281+
// Sort the modules to ensure we produce deterministic results.
282+
new_modules.sort_by(|module1, module2| module1.1.partial_cmp(&module2.1).unwrap());
283+
serialized_modules.extend(new_modules);
281284
serialized_modules.extend(cached_modules.into_iter().map(|(buffer, wp)| {
282285
(buffer, CString::new(wp.cgu_name).unwrap())
283286
}));

src/test/run-make-fulldeps/reproducible-build/Makefile

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ all: \
1010
link_paths \
1111
remap_paths \
1212
different_source_dirs \
13-
extern_flags
13+
extern_flags \
14+
fat_lto
1415

1516
smoke:
1617
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
@@ -76,3 +77,11 @@ extern_flags:
7677
--extern reproducible_build_aux=$(TMPDIR)/libbar.rlib \
7778
--crate-type rlib
7879
cmp "$(TMPDIR)/libreproducible_build.rlib" "$(TMPDIR)/libfoo.rlib" || exit 1
80+
81+
fat_lto:
82+
rm -rf $(TMPDIR) && mkdir $(TMPDIR)
83+
$(RUSTC) reproducible-build-aux.rs
84+
$(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1
85+
cp $(TMPDIR)/reproducible-build $(TMPDIR)/reproducible-build-a
86+
$(RUSTC) reproducible-build.rs -C lto=fat -C opt-level=1
87+
cmp "$(TMPDIR)/reproducible-build-a" "$(TMPDIR)/reproducible-build" || exit 1

0 commit comments

Comments
 (0)