Skip to content

Commit 282a7a7

Browse files
Fix issue around dllimport and ThinLTO as LLD runs it.
1 parent 20d3493 commit 282a7a7

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
@@ -1993,13 +1993,6 @@ pub fn build_session_options_and_crate_config(
19931993
(&None, &None) => None,
19941994
}.map(|m| PathBuf::from(m));
19951995

1996-
if cg.lto != Lto::No && incremental.is_some() {
1997-
early_error(
1998-
error_format,
1999-
"can't perform LTO when compiling incrementally",
2000-
);
2001-
}
2002-
20031996
if debugging_opts.profile && incremental.is_some() {
20041997
early_error(
20051998
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
@@ -1321,6 +1321,8 @@ fn execute_work_item(cgcx: &CodegenContext,
13211321
unsafe {
13221322
optimize(cgcx, &diag_handler, &module, config, timeline)?;
13231323

1324+
let linker_does_lto = cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1325+
13241326
// After we've done the initial round of optimizations we need to
13251327
// decide whether to synchronously codegen this module or ship it
13261328
// back to the coordinator thread for further LTO processing (which
@@ -1331,6 +1333,11 @@ fn execute_work_item(cgcx: &CodegenContext,
13311333
let needs_lto = match cgcx.lto {
13321334
Lto::No => false,
13331335

1336+
// If the linker does LTO, we don't have to do it. Note that we
1337+
// keep doing full LTO, if it is requested, as not to break the
1338+
// assumption that the output will be a single module.
1339+
Lto::Thin | Lto::ThinLocal if linker_does_lto => false,
1340+
13341341
// Here we've got a full crate graph LTO requested. We ignore
13351342
// this, however, if the crate type is only an rlib as there's
13361343
// no full crate graph to process, that'll happen later.
@@ -1361,11 +1368,6 @@ fn execute_work_item(cgcx: &CodegenContext,
13611368
// settings.
13621369
let needs_lto = needs_lto && module.kind != ModuleKind::Metadata;
13631370

1364-
// Don't run LTO passes when cross-lang LTO is enabled. The linker
1365-
// will do that for us in this case.
1366-
let needs_lto = needs_lto &&
1367-
!cgcx.opts.debugging_opts.cross_lang_lto.enabled();
1368-
13691371
if needs_lto {
13701372
Ok(WorkItemResult::NeedsLTO(module))
13711373
} else {
@@ -2345,8 +2347,18 @@ pub(crate) fn submit_codegened_module_to_llvm(tcx: TyCtxt,
23452347
}
23462348

23472349
fn msvc_imps_needed(tcx: TyCtxt) -> bool {
2350+
// This should never be true (because it's not supported). If it is true,
2351+
// something is wrong with commandline arg validation.
2352+
assert!(!(tcx.sess.opts.debugging_opts.cross_lang_lto.enabled() &&
2353+
tcx.sess.target.target.options.is_like_msvc &&
2354+
tcx.sess.opts.cg.prefer_dynamic));
2355+
23482356
tcx.sess.target.target.options.is_like_msvc &&
2349-
tcx.sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateTypeRlib)
2357+
tcx.sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateTypeRlib) &&
2358+
// ThinLTO can't handle this workaround in all cases, so we don't
2359+
// emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
2360+
// dynamic linking when cross-language LTO is enabled.
2361+
!tcx.sess.opts.debugging_opts.cross_lang_lto.enabled()
23502362
}
23512363

23522364
// 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
@@ -183,7 +183,20 @@ pub fn get_static(cx: &CodegenCx<'ll, '_>, def_id: DefId) -> &'ll Value {
183183
llvm::set_thread_local_mode(g, cx.tls_model);
184184
}
185185

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

0 commit comments

Comments
 (0)