-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] parameterize -C prefer-dynamic
#88101
Closed
pnkfelix
wants to merge
10
commits into
rust-lang:master
from
pnkfelix:issue-82151-parameterize-prefer-dynamic
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a8ed3a4
extra details about library mismatches.
pnkfelix dbe4d27
improve debugging experience slightly.
pnkfelix bf817f6
Generalize `-C prefer-dyanmic` to carry an optional subset of the cra…
pnkfelix 5d77479
Guard new variants of `-C prefer-dynamic=...` via `-Z` flags.
pnkfelix 0099f6b
Add some simple tests of how `-C prefer-dynamic` interacts with depen…
pnkfelix 8d63ba2
Some tests of the current behavior, including the elaborated output w…
pnkfelix 172ba70
A collection of test cases showing how the new `-C prefer-dynamic=cra…
pnkfelix a2231a4
Variations on the dylibs/diamonds tests inspired by issue 82151.
pnkfelix 5509ab2
Update a comment to reflect the new status quo established in this PR.
pnkfelix 21778cb
run-make test exploring behavior of -C prefer-dynamic.
pnkfelix File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,22 +46,26 @@ | |
//! all dynamic. This isn't currently very well battle tested, so it will likely | ||
//! fall short in some use cases. | ||
//! | ||
//! Currently, there is no way to specify the preference of linkage with a | ||
//! particular library (other than a global dynamic/static switch). | ||
//! Additionally, the algorithm is geared towards finding *any* solution rather | ||
//! than finding a number of solutions (there are normally quite a few). | ||
//! The algorithm is geared towards finding *any* solution rather than finding a | ||
//! number of solutions (there are normally quite a few). One can specify the | ||
//! preference of linkage for a particular list of crates by specifying that | ||
//! list via `-C prefer-dynamic=crate1,crate2,...`. | ||
|
||
use crate::creader::CStore; | ||
|
||
use rustc_data_structures::fx::FxHashMap; | ||
use rustc_hir::def_id::CrateNum; | ||
use rustc_middle::middle::cstore::CrateDepKind; | ||
use rustc_hir::def_id::{CrateNum, LOCAL_CRATE}; | ||
use rustc_middle::middle::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic}; | ||
use rustc_middle::middle::cstore::{CrateDepKind, CrateSource}; | ||
use rustc_middle::middle::dependency_format::{Dependencies, DependencyList, Linkage}; | ||
use rustc_middle::ty::TyCtxt; | ||
use rustc_session::config::CrateType; | ||
use rustc_target::spec::PanicStrategy; | ||
|
||
type RevDeps = FxHashMap<(CrateNum, LinkagePreference), Vec<CrateNum>>; | ||
|
||
use rustc_data_structures::sync::Lrc; | ||
|
||
crate fn calculate(tcx: TyCtxt<'_>) -> Dependencies { | ||
tcx.sess | ||
.crate_types() | ||
|
@@ -81,23 +85,43 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
return Vec::new(); | ||
} | ||
|
||
let prefer_dynamic = &sess.opts.cg.prefer_dynamic; | ||
|
||
// traverses all crates to see if any are in the prefer-dynamic set. | ||
let prefer_dynamic_for_any_crate = || { | ||
if prefer_dynamic.is_empty() { | ||
// skip traversal if we know it cannot ever bear fruit. | ||
false | ||
} else { | ||
tcx.crates(()).iter().any(|cnum| prefer_dynamic.contains_crate(tcx.crate_name(*cnum))) | ||
} | ||
}; | ||
|
||
let preferred_linkage = match ty { | ||
// Generating a dylib without `-C prefer-dynamic` means that we're going | ||
// to try to eagerly statically link all dependencies. This is normally | ||
// done for end-product dylibs, not intermediate products. | ||
// If `-C prefer-dynamic` is not set for any crate, then means that we're | ||
// going to try to eagerly statically link all dependencies into the dylib. | ||
// This is normally done for end-product dylibs, not intermediate products. | ||
// | ||
// Treat cdylibs similarly. If `-C prefer-dynamic` is set, the caller may | ||
// be code-size conscious, but without it, it makes sense to statically | ||
// link a cdylib. | ||
CrateType::Dylib | CrateType::Cdylib if !sess.opts.cg.prefer_dynamic => Linkage::Static, | ||
CrateType::Dylib | CrateType::Cdylib => Linkage::Dynamic, | ||
// Treat cdylibs similarly. If `-C prefer-dynamic` is set for any given | ||
// crate, the caller may be code-size conscious, but without it, it | ||
// makes sense to statically link a cdylib. | ||
CrateType::Dylib | CrateType::Cdylib => { | ||
if prefer_dynamic_for_any_crate() { | ||
Linkage::Dynamic | ||
} else { | ||
Linkage::Static | ||
} | ||
} | ||
|
||
// If the global prefer_dynamic switch is turned off, or the final | ||
// If `prefer_dynamic` is not set for any crate, or the final | ||
// executable will be statically linked, prefer static crate linkage. | ||
CrateType::Executable if !sess.opts.cg.prefer_dynamic || sess.crt_static(Some(ty)) => { | ||
Linkage::Static | ||
CrateType::Executable => { | ||
if !prefer_dynamic_for_any_crate() || sess.crt_static(Some(ty)) { | ||
Linkage::Static | ||
} else { | ||
Linkage::Dynamic | ||
} | ||
} | ||
CrateType::Executable => Linkage::Dynamic, | ||
|
||
// proc-macro crates are mostly cdylibs, but we also need metadata. | ||
CrateType::ProcMacro => Linkage::Static, | ||
|
@@ -133,7 +157,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
if tcx.dep_kind(cnum).macros_only() { | ||
continue; | ||
} | ||
let src = tcx.used_crate_source(cnum); | ||
let src: Lrc<CrateSource> = tcx.used_crate_source(cnum); | ||
if src.rlib.is_some() { | ||
continue; | ||
} | ||
|
@@ -144,10 +168,16 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
)); | ||
} | ||
return Vec::new(); | ||
} else { | ||
tracing::info!( | ||
"calculate_type {} attempt_static failed; falling through to dynamic linkage", | ||
tcx.crate_name(LOCAL_CRATE) | ||
); | ||
} | ||
} | ||
|
||
let mut formats = FxHashMap::default(); | ||
let mut rev_deps: RevDeps = FxHashMap::default(); | ||
|
||
// Sweep all crates for found dylibs. Add all dylibs, as well as their | ||
// dependencies, ensuring there are no conflicts. The only valid case for a | ||
|
@@ -157,14 +187,27 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
continue; | ||
} | ||
let name = tcx.crate_name(cnum); | ||
let src = tcx.used_crate_source(cnum); | ||
if src.dylib.is_some() { | ||
tracing::info!("adding dylib: {}", name); | ||
add_library(tcx, cnum, RequireDynamic, &mut formats); | ||
let src: Lrc<CrateSource> = tcx.used_crate_source(cnum); | ||
// We should prefer the dylib, when available, if either: | ||
// 1. the user has requested that dylib (or all dylibs) via `-C prefer-dynamic`, or | ||
// 2. for backwards compatibility: if the prefer-dynamic subset is unset, then we *still* | ||
// favor dylibs here. This way, if static linking fails above, we might still hope to | ||
// succeed at linking here. | ||
let prefer_dylib = | ||
|name| -> bool { prefer_dynamic.is_unset() || prefer_dynamic.contains_crate(name) }; | ||
if src.dylib.is_some() && prefer_dylib(name) { | ||
tracing::info!("calculate_type {} adding dylib: {}", tcx.crate_name(LOCAL_CRATE), name); | ||
add_library(tcx, cnum, RequireDynamic, &mut formats, &mut rev_deps, LOCAL_CRATE); | ||
let deps = tcx.dylib_dependency_formats(cnum); | ||
for &(depnum, style) in deps.iter() { | ||
tracing::info!("adding {:?}: {}", style, tcx.crate_name(depnum)); | ||
add_library(tcx, depnum, style, &mut formats); | ||
tracing::info!( | ||
"calculate_type {} adding dep of {}, {:?}: {}", | ||
tcx.crate_name(LOCAL_CRATE), | ||
name, | ||
style, | ||
tcx.crate_name(depnum) | ||
); | ||
add_library(tcx, depnum, style, &mut formats, &mut rev_deps, cnum); | ||
} | ||
} | ||
} | ||
|
@@ -185,14 +228,17 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
// If the crate hasn't been included yet and it's not actually required | ||
// (e.g., it's an allocator) then we skip it here as well. | ||
for &cnum in tcx.crates(()).iter() { | ||
let src = tcx.used_crate_source(cnum); | ||
if src.dylib.is_none() | ||
&& !formats.contains_key(&cnum) | ||
&& tcx.dep_kind(cnum) == CrateDepKind::Explicit | ||
{ | ||
let name = tcx.crate_name(cnum); | ||
let src: Lrc<CrateSource> = tcx.used_crate_source(cnum); | ||
let static_available = src.rlib.is_some() || src.rmeta.is_some(); | ||
let prefer_static = | ||
src.dylib.is_none() || (static_available && !prefer_dynamic.contains_crate(name)); | ||
let missing = !formats.contains_key(&cnum); | ||
let actually_required = tcx.dep_kind(cnum) == CrateDepKind::Explicit; | ||
if prefer_static && missing && actually_required { | ||
assert!(src.rlib.is_some() || src.rmeta.is_some()); | ||
tracing::info!("adding staticlib: {}", tcx.crate_name(cnum)); | ||
add_library(tcx, cnum, RequireStatic, &mut formats); | ||
tracing::info!("adding staticlib: {}", name); | ||
add_library(tcx, cnum, RequireStatic, &mut formats, &mut rev_deps, LOCAL_CRATE); | ||
ret[cnum.as_usize() - 1] = Linkage::Static; | ||
} | ||
} | ||
|
@@ -215,7 +261,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList { | |
// making sure that everything is available in the requested format. | ||
for (cnum, kind) in ret.iter().enumerate() { | ||
let cnum = CrateNum::new(cnum + 1); | ||
let src = tcx.used_crate_source(cnum); | ||
let src: Lrc<CrateSource> = tcx.used_crate_source(cnum); | ||
match *kind { | ||
Linkage::NotLinked | Linkage::IncludedFromDylib => {} | ||
Linkage::Static if src.rlib.is_some() => continue, | ||
|
@@ -243,7 +289,10 @@ fn add_library( | |
cnum: CrateNum, | ||
link: LinkagePreference, | ||
m: &mut FxHashMap<CrateNum, LinkagePreference>, | ||
rev_deps: &mut RevDeps, | ||
parent_cnum: CrateNum, | ||
) { | ||
rev_deps.entry((cnum, link)).or_insert(Vec::new()).push(parent_cnum); | ||
match m.get(&cnum) { | ||
Some(&link2) => { | ||
// If the linkages differ, then we'd have two copies of the library | ||
|
@@ -254,11 +303,44 @@ fn add_library( | |
// This error is probably a little obscure, but I imagine that it | ||
// can be refined over time. | ||
if link2 != link || link == RequireStatic { | ||
let parent_names = |link| { | ||
let mut names = String::new(); | ||
let mut saw_one = false; | ||
for name in rev_deps[&(cnum, link)].iter().map(|pcnum| tcx.crate_name(*pcnum)) { | ||
if saw_one { | ||
names.push_str(", "); | ||
} | ||
names.push_str(&format!("`{}`", name)); | ||
saw_one = true; | ||
} | ||
names | ||
}; | ||
let link_parent_names = parent_names(link); | ||
let link2_parent_names = parent_names(link2); | ||
|
||
let details = match (link2, link) { | ||
(RequireDynamic, RequireStatic) => format!( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💖 |
||
"previously required dynamic, via [{}], \ | ||
and now also requires static, via [{}]", | ||
link2_parent_names, link_parent_names | ||
), | ||
(RequireStatic, RequireDynamic) => format!( | ||
"previously required static, via [{}], \ | ||
and now also requires dynamic, via [{}]", | ||
link2_parent_names, link_parent_names | ||
), | ||
(RequireStatic, RequireStatic) => format!( | ||
"two static copies from multiple different locations, via [{}]", | ||
link_parent_names | ||
), | ||
(RequireDynamic, RequireDynamic) => unreachable!("not a problem"), | ||
}; | ||
tcx.sess | ||
.struct_err(&format!( | ||
"cannot satisfy dependencies so `{}` only \ | ||
shows up once", | ||
tcx.crate_name(cnum) | ||
shows up once ({})", | ||
tcx.crate_name(cnum), | ||
details, | ||
)) | ||
.help( | ||
"having upstream crates all available in one format \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should skip non-linkable crates like proc-macros and their dependencies I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I'll look into that.