Skip to content

Commit 3a3b331

Browse files
Fix issue around dllimport and ThinLTO as LLD runs it.
1 parent d5f8edf commit 3a3b331

File tree

5 files changed

+55
-15
lines changed

5 files changed

+55
-15
lines changed

src/librustc/session/config.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 19 additions & 1 deletion
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,27 @@ 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+
if sess.opts.debugging_opts.cross_lang_lto.enabled() &&
1206+
sess.opts.cg.prefer_dynamic &&
1207+
sess.target.target.options.is_like_msvc {
1208+
sess.err("Linker plugin based LTO is not supported together with \
1209+
`-C prefer-dynamic` when targeting MSVC");
1210+
}
1211+
}
1212+
11951213
/// Hash value constructed out of all the `-C metadata` arguments passed to the
11961214
/// compiler. Together with the crate-name forms a unique global identifier for
11971215
/// the crate.

src/librustc_codegen_llvm/back/lto.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,8 @@ fn execute_work_item(cgcx: &CodegenContext,
13511351
unsafe {
13521352
optimize(cgcx, &diag_handler, &module, config, timeline)?;
13531353

1354+
let linker_does_lto = cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1355+
13541356
// After we've done the initial round of optimizations we need to
13551357
// decide whether to synchronously codegen this module or ship it
13561358
// back to the coordinator thread for further LTO processing (which
@@ -1361,6 +1363,11 @@ fn execute_work_item(cgcx: &CodegenContext,
13611363
let needs_lto = match cgcx.lto {
13621364
Lto::No => false,
13631365

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

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-
13991401
if needs_lto {
14001402
Ok(WorkItemResult::NeedsLTO(module))
14011403
} else {
@@ -2375,8 +2377,18 @@ pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
23752377
}
23762378

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

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

src/librustc_codegen_llvm/consts.rs

Lines changed: 14 additions & 1 deletion
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'.

0 commit comments

Comments
 (0)