Skip to content

Commit b73535f

Browse files
committed
Auto merge of #53031 - michaelwoerister:cross-lto, r=alexcrichton
Apply some fixes to cross-language LTO (especially when targeting MSVC) This PR contains a few fixes that were needed in order to get Firefox compiling with Rust/C++ cross-language ThinLTO on Windows. The commits are self-contained and should be self-explanatory. r? @alexcrichton
2 parents fbb6275 + 49972e9 commit b73535f

File tree

15 files changed

+203
-24
lines changed

15 files changed

+203
-24
lines changed

src/librustc/session/config.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ macro_rules! options {
831831
pub const parse_lto: Option<&'static str> =
832832
Some("one of `thin`, `fat`, or omitted");
833833
pub const parse_cross_lang_lto: Option<&'static str> =
834-
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), `no-link`, \
834+
Some("either a boolean (`yes`, `no`, `on`, `off`, etc), \
835835
or the path to the linker plugin");
836836
}
837837

@@ -2006,13 +2006,6 @@ pub fn build_session_options_and_crate_config(
20062006
(&None, &None) => None,
20072007
}.map(|m| PathBuf::from(m));
20082008

2009-
if cg.lto != Lto::No && incremental.is_some() {
2010-
early_error(
2011-
error_format,
2012-
"can't perform LTO when compiling incrementally",
2013-
);
2014-
}
2015-
20162009
if debugging_opts.profile && incremental.is_some() {
20172010
early_error(
20182011
error_format,

src/librustc/session/mod.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use lint::builtin::BuiltinLintDiagnostics;
2020
use middle::allocator::AllocatorKind;
2121
use middle::dependency_format;
2222
use session::search_paths::PathKind;
23-
use session::config::{OutputType};
23+
use session::config::{OutputType, Lto};
2424
use ty::tls;
2525
use util::nodemap::{FxHashMap, FxHashSet};
2626
use util::common::{duration_to_secs_str, ErrorReported};
@@ -1189,9 +1189,34 @@ pub fn build_session_(
11891189
driver_lint_caps: FxHashMap(),
11901190
};
11911191

1192+
validate_commandline_args_with_session_available(&sess);
1193+
11921194
sess
11931195
}
11941196

1197+
// If it is useful to have a Session available already for validating a
1198+
// commandline argument, you can do so here.
1199+
fn validate_commandline_args_with_session_available(sess: &Session) {
1200+
1201+
if sess.lto() != Lto::No && sess.opts.incremental.is_some() {
1202+
sess.err("can't perform LTO when compiling incrementally");
1203+
}
1204+
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.
1212+
if sess.opts.debugging_opts.cross_lang_lto.enabled() &&
1213+
sess.opts.cg.prefer_dynamic &&
1214+
sess.target.target.options.is_like_msvc {
1215+
sess.err("Linker plugin based LTO is not supported together with \
1216+
`-C prefer-dynamic` when targeting MSVC");
1217+
}
1218+
}
1219+
11951220
/// Hash value constructed out of all the `-C metadata` arguments passed to the
11961221
/// compiler. Together with the crate-name forms a unique global identifier for
11971222
/// the crate.

src/librustc_codegen_llvm/attributes.rs

+18
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ pub fn llvm_target_features(sess: &Session) -> impl Iterator<Item = &str> {
123123
.filter(|l| !l.is_empty())
124124
}
125125

126+
pub fn apply_target_cpu_attr(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value) {
127+
let target_cpu = CString::new(cx.tcx.sess.target_cpu().to_string()).unwrap();
128+
llvm::AddFunctionAttrStringValue(
129+
llfn,
130+
llvm::AttributePlace::Function,
131+
cstr("target-cpu\0"),
132+
target_cpu.as_c_str());
133+
}
134+
126135
/// Composite function which sets LLVM attributes for function depending on its AST (#[attribute])
127136
/// attributes.
128137
pub fn from_fn_attrs(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value, id: DefId) {
@@ -167,6 +176,15 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, '_>, llfn: &'ll Value, id: DefId) {
167176
Some(true) | None => {}
168177
}
169178

179+
// Always annotate functions with the target-cpu they are compiled for.
180+
// Without this, ThinLTO won't inline Rust functions into Clang generated
181+
// functions (because Clang annotates functions this way too).
182+
// NOTE: For now we just apply this if -Zcross-lang-lto is specified, since
183+
// it introduce a little overhead and isn't really necessary otherwise.
184+
if cx.tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() {
185+
apply_target_cpu_attr(cx, llfn);
186+
}
187+
170188
let features = llvm_target_features(cx.tcx.sess)
171189
.map(|s| s.to_string())
172190
.chain(

src/librustc_codegen_llvm/back/link.rs

+10-6
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,11 +1623,15 @@ 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 |
1629-
Lto::Thin |
16301629
Lto::Fat => true,
1630+
Lto::Thin => {
1631+
// If we defer LTO to the linker, we haven't run LTO ourselves, so
1632+
// any upstream object files have not been copied yet.
1633+
!sess.opts.debugging_opts.cross_lang_lto.enabled()
1634+
}
16311635
Lto::No |
16321636
Lto::ThinLocal => false,
16331637
}

src/librustc_codegen_llvm/back/lto.rs

+4
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ pub(crate) fn run(cgcx: &CodegenContext,
195195
}
196196
Lto::Thin |
197197
Lto::ThinLocal => {
198+
if cgcx.opts.debugging_opts.cross_lang_lto.enabled() {
199+
unreachable!("We should never reach this case if the LTO step \
200+
is deferred to the linker");
201+
}
198202
thin_lto(&diag_handler, modules, upstream_modules, &arr, timeline)
199203
}
200204
Lto::No => unreachable!(),

src/librustc_codegen_llvm/back/write.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,8 @@ unsafe fn optimize(cgcx: &CodegenContext,
552552
llvm::LLVMRustAddAnalysisPasses(tm, fpm, llmod);
553553
llvm::LLVMRustAddAnalysisPasses(tm, mpm, llmod);
554554
let opt_level = config.opt_level.unwrap_or(llvm::CodeGenOptLevel::None);
555-
let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal;
555+
let prepare_for_thin_lto = cgcx.lto == Lto::Thin || cgcx.lto == Lto::ThinLocal ||
556+
(cgcx.lto != Lto::Fat && cgcx.opts.debugging_opts.cross_lang_lto.enabled());
556557
have_name_anon_globals_pass = have_name_anon_globals_pass || prepare_for_thin_lto;
557558
if using_thin_buffers && !prepare_for_thin_lto {
558559
assert!(addpass("name-anon-globals"));
@@ -1351,6 +1352,8 @@ fn execute_work_item(cgcx: &CodegenContext,
13511352
unsafe {
13521353
optimize(cgcx, &diag_handler, &module, config, timeline)?;
13531354

1355+
let linker_does_lto = cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1356+
13541357
// After we've done the initial round of optimizations we need to
13551358
// decide whether to synchronously codegen this module or ship it
13561359
// back to the coordinator thread for further LTO processing (which
@@ -1361,6 +1364,11 @@ fn execute_work_item(cgcx: &CodegenContext,
13611364
let needs_lto = match cgcx.lto {
13621365
Lto::No => false,
13631366

1367+
// If the linker does LTO, we don't have to do it. Note that we
1368+
// keep doing full LTO, if it is requested, as not to break the
1369+
// assumption that the output will be a single module.
1370+
Lto::Thin | Lto::ThinLocal if linker_does_lto => false,
1371+
13641372
// Here we've got a full crate graph LTO requested. We ignore
13651373
// this, however, if the crate type is only an rlib as there's
13661374
// no full crate graph to process, that'll happen later.
@@ -1391,11 +1399,6 @@ fn execute_work_item(cgcx: &CodegenContext,
13911399
// settings.
13921400
let needs_lto = needs_lto && module.kind != ModuleKind::Metadata;
13931401

1394-
// Don't run LTO passes when cross-lang LTO is enabled. The linker
1395-
// will do that for us in this case.
1396-
let needs_lto = needs_lto &&
1397-
!cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1398-
13991402
if needs_lto {
14001403
Ok(WorkItemResult::NeedsLTO(module))
14011404
} else {
@@ -2375,8 +2378,18 @@ pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
23752378
}
23762379

23772380
fn msvc_imps_needed(tcx: TyCtxt) -> bool {
2381+
// This should never be true (because it's not supported). If it is true,
2382+
// something is wrong with commandline arg validation.
2383+
assert!(!(tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() &&
2384+
tcx.sess.target.target.options.is_like_msvc &&
2385+
tcx.sess.opts.cg.prefer_dynamic));
2386+
23782387
tcx.sess.target.target.options.is_like_msvc &&
2379-
tcx.sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateType::Rlib)
2388+
tcx.sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateType::Rlib) &&
2389+
// ThinLTO can't handle this workaround in all cases, so we don't
2390+
// emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
2391+
// dynamic linking when cross-language LTO is enabled.
2392+
!tcx.sess.opts.debugging_opts.cross_lang_lto.enabled()
23802393
}
23812394

23822395
// Create a `__imp_<symbol> = &symbol` global for every public static `symbol`.

src/librustc_codegen_llvm/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx) {
596596

597597
// `main` should respect same config for frame pointer elimination as rest of code
598598
attributes::set_frame_pointer_elimination(cx, llfn);
599+
attributes::apply_target_cpu_attr(cx, llfn);
599600

600601
let bx = Builder::new_block(cx, llfn, "top");
601602

src/librustc_codegen_llvm/consts.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,20 @@ pub fn get_static(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll Value {
189189
llvm::set_thread_local_mode(g, cx.tls_model);
190190
}
191191

192-
if cx.use_dll_storage_attrs && !cx.tcx.is_foreign_item(def_id) {
192+
let needs_dll_storage_attr =
193+
cx.use_dll_storage_attrs && !cx.tcx.is_foreign_item(def_id) &&
194+
// ThinLTO can't handle this workaround in all cases, so we don't
195+
// emit the attrs. Instead we make them unnecessary by disallowing
196+
// dynamic linking when cross-language LTO is enabled.
197+
!cx.tcx.sess.opts.debugging_opts.cross_lang_lto.enabled();
198+
199+
// If this assertion triggers, there's something wrong with commandline
200+
// argument validation.
201+
debug_assert!(!(cx.tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() &&
202+
cx.tcx.sess.target.target.options.is_like_msvc &&
203+
cx.tcx.sess.opts.cg.prefer_dynamic));
204+
205+
if needs_dll_storage_attr {
193206
// This item is external but not foreign, i.e. it originates from an external Rust
194207
// crate. Since we don't know whether this crate will be linked dynamically or
195208
// statically in the final application, we always mark such symbols as 'dllimport'.

src/librustc_codegen_llvm/context.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use attributes;
1112
use common;
1213
use llvm;
1314
use rustc::dep_graph::DepGraphSafe;
@@ -381,6 +382,7 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
381382
declare::declare_cfn(self, name, fty)
382383
}
383384
};
385+
attributes::apply_target_cpu_attr(self, llfn);
384386
self.eh_personality.set(Some(llfn));
385387
llfn
386388
}
@@ -412,6 +414,7 @@ impl<'b, 'tcx> CodegenCx<'b, 'tcx> {
412414

413415
let llfn = declare::declare_fn(self, "rust_eh_unwind_resume", ty);
414416
attributes::unwind(llfn, true);
417+
attributes::apply_target_cpu_attr(self, llfn);
415418
unwresume.set(Some(llfn));
416419
llfn
417420
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test makes sure that functions get annotated with the proper
12+
// "target-cpu" attribute in LLVM.
13+
14+
// no-prefer-dynamic
15+
// only-msvc
16+
// compile-flags: -Z cross-lang-lto
17+
18+
#![crate_type = "rlib"]
19+
20+
// CHECK-NOT: @{{.*}}__imp_{{.*}}GLOBAL{{.*}} = global i8*
21+
22+
pub static GLOBAL: u32 = 0;
23+
pub static mut GLOBAL2: u32 = 0;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// This test makes sure that functions get annotated with the proper
12+
// "target-cpu" attribute in LLVM.
13+
14+
// no-prefer-dynamic
15+
// ignore-tidy-linelength
16+
// compile-flags: -C no-prepopulate-passes -C panic=abort -Z cross-lang-lto -Cpasses=name-anon-globals
17+
18+
#![crate_type = "staticlib"]
19+
20+
// CHECK-LABEL: define {{.*}} @exported() {{.*}} #0
21+
#[no_mangle]
22+
pub extern fn exported() {
23+
not_exported();
24+
}
25+
26+
// CHECK-LABEL: define {{.*}} @_ZN23target_cpu_on_functions12not_exported{{.*}}() {{.*}} #0
27+
fn not_exported() {}
28+
29+
// CHECK: attributes #0 = {{.*}} "target-cpu"="{{.*}}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
-include ../tools.mk
3+
4+
# This test makes sure that we don't loose upstream object files when compiling
5+
# staticlibs with -Zcross-lang-lto
6+
7+
all: staticlib.rs upstream.rs
8+
$(RUSTC) upstream.rs -Z cross-lang-lto -Ccodegen-units=1
9+
10+
# Check No LTO
11+
$(RUSTC) staticlib.rs -Z cross-lang-lto -Ccodegen-units=1 -L. -o $(TMPDIR)/staticlib.a
12+
(cd $(TMPDIR); llvm-ar x ./staticlib.a)
13+
# Make sure the upstream object file was included
14+
ls $(TMPDIR)/upstream.*.rcgu.o
15+
16+
# Cleanup
17+
rm $(TMPDIR)/*
18+
19+
# Check ThinLTO
20+
$(RUSTC) upstream.rs -Z cross-lang-lto -Ccodegen-units=1 -Clto=thin
21+
$(RUSTC) staticlib.rs -Z cross-lang-lto -Ccodegen-units=1 -Clto=thin -L. -o $(TMPDIR)/staticlib.a
22+
(cd $(TMPDIR); llvm-ar x ./staticlib.a)
23+
ls $(TMPDIR)/upstream.*.rcgu.o
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type="staticlib"]
12+
13+
extern crate upstream;
14+
15+
#[no_mangle]
16+
pub extern fn bar() {
17+
upstream::foo();
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_type = "rlib"]
12+
13+
pub fn foo() {}

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

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# ignore-msvc
21

32
-include ../tools.mk
43

0 commit comments

Comments
 (0)