Skip to content

Commit 603584d

Browse files
Address review comments for rust-lang#53031.
1 parent 28499b3 commit 603584d

File tree

3 files changed

+14
-7
lines changed
  • src
    • librustc/session
    • librustc_codegen_llvm/back
    • test/run-make-fulldeps/cross-lang-lto-upstream-rlibs

3 files changed

+14
-7
lines changed

src/librustc/session/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,13 @@ fn validate_commandline_args_with_session_available(sess: &Session) {
12021202
sess.err("can't perform LTO when compiling incrementally");
12031203
}
12041204

1205+
// Since we don't know if code in an rlib will be linked to statically or
1206+
// dynamically downstream, rustc generates `__imp_` symbols that help the
1207+
// MSVC linker deal with this lack of knowledge (#27438). Unfortunately,
1208+
// these manually generated symbols confuse LLD when it tries to merge
1209+
// bitcode during ThinLTO. Therefore we disallow dynamic linking on MSVC
1210+
// when compiling for LLD ThinLTO. This way we can validly just not generate
1211+
// the `dllimport` attributes and `__imp_` symbols in that case.
12051212
if sess.opts.debugging_opts.cross_lang_lto.enabled() &&
12061213
sess.opts.cg.prefer_dynamic &&
12071214
sess.target.target.options.is_like_msvc {

src/librustc_codegen_llvm/back/link.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ fn link_staticlib(sess: &Session,
563563
});
564564
ab.add_rlib(path,
565565
&name.as_str(),
566-
is_full_lto_enabled(sess) &&
566+
are_upstream_rust_objects_already_included(sess) &&
567567
!ignored_for_lto(sess, &codegen_results.crate_info, cnum),
568568
skip_object_files).unwrap();
569569

@@ -1446,7 +1446,7 @@ fn add_upstream_rust_crates(cmd: &mut dyn Linker,
14461446
lib.kind == NativeLibraryKind::NativeStatic && !relevant_lib(sess, lib)
14471447
});
14481448

1449-
if (!is_full_lto_enabled(sess) ||
1449+
if (!are_upstream_rust_objects_already_included(sess) ||
14501450
ignored_for_lto(sess, &codegen_results.crate_info, cnum)) &&
14511451
crate_type != config::CrateType::Dylib &&
14521452
!skip_native {
@@ -1500,7 +1500,7 @@ fn add_upstream_rust_crates(cmd: &mut dyn Linker,
15001500
// file, then we don't need the object file as it's part of the
15011501
// LTO module. Note that `#![no_builtins]` is excluded from LTO,
15021502
// though, so we let that object file slide.
1503-
let skip_because_lto = is_full_lto_enabled(sess) &&
1503+
let skip_because_lto = are_upstream_rust_objects_already_included(sess) &&
15041504
is_rust_object &&
15051505
(sess.target.target.options.no_builtins ||
15061506
!codegen_results.crate_info.is_no_builtins.contains(&cnum));
@@ -1537,7 +1537,7 @@ fn add_upstream_rust_crates(cmd: &mut dyn Linker,
15371537
fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) {
15381538
// If we're performing LTO, then it should have been previously required
15391539
// that all upstream rust dependencies were available in an rlib format.
1540-
assert!(!is_full_lto_enabled(sess));
1540+
assert!(!are_upstream_rust_objects_already_included(sess));
15411541

15421542
// Just need to tell the linker about where the library lives and
15431543
// what its name is
@@ -1623,7 +1623,7 @@ fn relevant_lib(sess: &Session, lib: &NativeLibrary) -> bool {
16231623
}
16241624
}
16251625

1626-
fn is_full_lto_enabled(sess: &Session) -> bool {
1626+
fn are_upstream_rust_objects_already_included(sess: &Session) -> bool {
16271627
match sess.lto() {
16281628
Lto::Yes |
16291629
Lto::Fat => true,

src/test/run-make-fulldeps/cross-lang-lto-upstream-rlibs/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ all: staticlib.rs upstream.rs
1111
$(RUSTC) staticlib.rs -Z cross-lang-lto -Ccodegen-units=1 -L. -o $(TMPDIR)/staticlib.a
1212
(cd $(TMPDIR); llvm-ar x ./staticlib.a)
1313
# Make sure the upstream object file was included
14-
ls upstream.*.rcgu.o
14+
ls $(TMPDIR)/upstream.*.rcgu.o
1515

1616
# Cleanup
1717
rm $(TMPDIR)/*
@@ -20,4 +20,4 @@ all: staticlib.rs upstream.rs
2020
$(RUSTC) upstream.rs -Z cross-lang-lto -Ccodegen-units=1 -Clto=thin
2121
$(RUSTC) staticlib.rs -Z cross-lang-lto -Ccodegen-units=1 -Clto=thin -L. -o $(TMPDIR)/staticlib.a
2222
(cd $(TMPDIR); llvm-ar x ./staticlib.a)
23-
ls upstream.*.rcgu.o
23+
ls $(TMPDIR)/upstream.*.rcgu.o

0 commit comments

Comments
 (0)