Skip to content

Commit f687f80

Browse files
Auto merge of #146421 - mati865:experiments, r=<try>
[nothing to see here yet] Experiments
2 parents 8e2ed71 + 405e404 commit f687f80

File tree

13 files changed

+695
-42
lines changed

13 files changed

+695
-42
lines changed

Cargo.lock

Lines changed: 433 additions & 33 deletions
Large diffs are not rendered by default.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ members = [
4545
"src/tools/unicode-table-generator",
4646
"src/tools/unstable-book-gen",
4747
"src/tools/wasm-component-ld",
48+
"src/tools/wild-linker",
4849
"src/tools/x",
4950
# tidy-alphabetical-end
5051
]
@@ -92,4 +93,3 @@ codegen-units = 1
9293
# If you want to use a crate with local modifications, you can set a path or git dependency here.
9394
# For git dependencies, also add your source to ALLOWED_SOURCES in src/tools/tidy/src/extdeps.rs.
9495
#[patch.crates-io]
95-

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,8 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
13261326
LinkerFlavor::Gnu(Cc::Yes, _)
13271327
| LinkerFlavor::Darwin(Cc::Yes, _)
13281328
| LinkerFlavor::WasmLld(Cc::Yes)
1329-
| LinkerFlavor::Unix(Cc::Yes) => {
1329+
| LinkerFlavor::Unix(Cc::Yes)
1330+
| LinkerFlavor::Wild => {
13301331
if cfg!(any(target_os = "solaris", target_os = "illumos")) {
13311332
// On historical Solaris systems, "cc" may have
13321333
// been Sun Studio, which is not flag-compatible
@@ -1366,6 +1367,8 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) {
13661367
});
13671368
let flavor = sess.target.linker_flavor.with_linker_hints(stem);
13681369
let flavor = adjust_flavor_to_features(flavor, features);
1370+
let linker =
1371+
if flavor == LinkerFlavor::Wild { PathBuf::from("cc") } else { linker };
13691372
Some((linker, flavor))
13701373
}
13711374
(None, None) => None,
@@ -2482,6 +2485,8 @@ fn add_order_independent_options(
24822485
// Take care of the flavors and CLI options requesting the `lld` linker.
24832486
add_lld_args(cmd, sess, flavor, self_contained_components);
24842487

2488+
add_wild_args(cmd, sess, flavor, self_contained_components);
2489+
24852490
add_apple_link_args(cmd, sess, flavor);
24862491

24872492
let apple_sdk_root = add_apple_sdk(cmd, sess, flavor);
@@ -3403,6 +3408,57 @@ fn add_lld_args(
34033408
}
34043409
}
34053410

3411+
fn add_wild_args(
3412+
cmd: &mut dyn Linker,
3413+
sess: &Session,
3414+
flavor: LinkerFlavor,
3415+
self_contained_components: LinkSelfContainedComponents,
3416+
) {
3417+
// Either Wild or LLD to make it work with CI
3418+
if flavor != LinkerFlavor::Wild || std::env::var_os("BUILDING_RUSTC").is_some() {
3419+
let self_contained_cli = sess.opts.cg.link_self_contained.is_linker_enabled();
3420+
let self_contained_target = self_contained_components.is_linker_enabled();
3421+
3422+
let self_contained_linker = self_contained_cli || self_contained_target;
3423+
if self_contained_linker && !sess.opts.cg.link_self_contained.is_linker_disabled() {
3424+
let mut linker_path_exists = false;
3425+
for path in sess.get_tools_search_paths(false) {
3426+
let linker_path = path.join("gcc-ld");
3427+
linker_path_exists |= linker_path.exists();
3428+
cmd.cc_arg({
3429+
let mut arg = OsString::from("-B");
3430+
arg.push(linker_path);
3431+
arg
3432+
});
3433+
}
3434+
if !linker_path_exists {
3435+
sess.dcx().emit_fatal(errors::SelfContainedLinkerMissing);
3436+
}
3437+
}
3438+
3439+
if !sess.target.is_like_wasm {
3440+
cmd.cc_arg("-fuse-ld=lld");
3441+
}
3442+
return ();
3443+
}
3444+
3445+
let mut linker_path_exists = false;
3446+
for path in sess.get_tools_search_paths(false) {
3447+
let linker_path = path.join("wild-gcc-ld");
3448+
linker_path_exists |= linker_path.exists();
3449+
cmd.cc_arg({
3450+
let mut arg = OsString::from("-B");
3451+
arg.push(linker_path);
3452+
arg
3453+
});
3454+
}
3455+
if !linker_path_exists {
3456+
// As a sanity check, we emit an error if none of these paths exist: we want
3457+
// self-contained linking and have no linker.
3458+
sess.dcx().emit_fatal(errors::SelfContainedLinkerMissing);
3459+
}
3460+
}
3461+
34063462
// gold has been deprecated with binutils 2.44
34073463
// and is known to behave incorrectly around Rust programs.
34083464
// There have been reports of being unable to bootstrap with gold:

compiler/rustc_codegen_ssa/src/back/linker.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ pub(crate) fn get_linker<'a>(
161161
LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box<dyn Linker>,
162162
LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box<dyn Linker>,
163163
LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box<dyn Linker>,
164+
LinkerFlavor::Wild => Box::new(GccLinker {
165+
cmd,
166+
sess,
167+
target_cpu,
168+
hinted_static: None,
169+
is_ld: false,
170+
is_gnu: flavor.is_gnu(),
171+
uses_lld: flavor.uses_lld(),
172+
}),
164173
}
165174
}
166175

compiler/rustc_session/src/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ mod desc {
862862
pub(crate) const parse_link_self_contained: &str = "one of: `y`, `yes`, `on`, `n`, `no`, `off`, or a list of enabled (`+` prefix) and disabled (`-` prefix) \
863863
components: `crto`, `libc`, `unwind`, `linker`, `sanitizers`, `mingw`";
864864
pub(crate) const parse_linker_features: &str =
865-
"a list of enabled (`+` prefix) and disabled (`-` prefix) features: `lld`";
865+
"a list of enabled (`+` prefix) and disabled (`-` prefix) features: `lld`, `wild`";
866866
pub(crate) const parse_polonius: &str = "either no value or `legacy` (the default), or `next`";
867867
pub(crate) const parse_stack_protector: &str =
868868
"one of (`none` (default), `basic`, `strong`, or `all`)";

compiler/rustc_target/src/spec/mod.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ pub enum LinkerFlavor {
127127
/// Emscripten Compiler Frontend, a wrapper around `WasmLld(Cc::Yes)` that has a different
128128
/// interface and produces some additional JavaScript output.
129129
EmCc,
130+
// TODO: This needs some design on how to proceed
131+
Wild,
130132
// Below: other linker-like tools with unique interfaces for exotic targets.
131133
/// Linker tool for BPF.
132134
Bpf,
@@ -153,6 +155,7 @@ pub enum LinkerFlavorCli {
153155
Bpf,
154156
Ptx,
155157
Llbc,
158+
Wild,
156159

157160
// Legacy stable values
158161
Gcc,
@@ -178,7 +181,8 @@ impl LinkerFlavorCli {
178181
| LinkerFlavorCli::Ld
179182
| LinkerFlavorCli::Lld(..)
180183
| LinkerFlavorCli::Msvc(Lld::No)
181-
| LinkerFlavorCli::Em => false,
184+
| LinkerFlavorCli::Em
185+
| LinkerFlavorCli::Wild => false,
182186
}
183187
}
184188
}
@@ -245,6 +249,7 @@ impl LinkerFlavor {
245249
LinkerFlavorCli::Bpf => LinkerFlavor::Bpf,
246250
LinkerFlavorCli::Llbc => LinkerFlavor::Llbc,
247251
LinkerFlavorCli::Ptx => LinkerFlavor::Ptx,
252+
LinkerFlavorCli::Wild => LinkerFlavor::Wild,
248253

249254
// Below: legacy stable values
250255
LinkerFlavorCli::Gcc => match lld_flavor {
@@ -285,6 +290,7 @@ impl LinkerFlavor {
285290
LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
286291
LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
287292
LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
293+
LinkerFlavor::Wild => LinkerFlavorCli::Wild,
288294
}
289295
}
290296

@@ -300,6 +306,7 @@ impl LinkerFlavor {
300306
LinkerFlavor::Bpf => LinkerFlavorCli::Bpf,
301307
LinkerFlavor::Llbc => LinkerFlavorCli::Llbc,
302308
LinkerFlavor::Ptx => LinkerFlavorCli::Ptx,
309+
LinkerFlavor::Wild => LinkerFlavorCli::Wild,
303310
}
304311
}
305312

@@ -314,6 +321,7 @@ impl LinkerFlavor {
314321
LinkerFlavorCli::EmCc => (Some(Cc::Yes), Some(Lld::Yes)),
315322
LinkerFlavorCli::Bpf | LinkerFlavorCli::Ptx => (None, None),
316323
LinkerFlavorCli::Llbc => (None, None),
324+
LinkerFlavorCli::Wild => (None, None),
317325

318326
// Below: legacy stable values
319327
LinkerFlavorCli::Gcc => (Some(Cc::Yes), None),
@@ -332,6 +340,8 @@ impl LinkerFlavor {
332340

333341
if stem == "llvm-bitcode-linker" {
334342
Ok(Self::Llbc)
343+
} else if stem == "wild" {
344+
Ok(Self::Wild)
335345
} else if stem == "emcc" // GCC/Clang can have an optional target prefix.
336346
|| stem == "gcc"
337347
|| stem.ends_with("-gcc")
@@ -369,7 +379,11 @@ impl LinkerFlavor {
369379
LinkerFlavor::WasmLld(cc) => LinkerFlavor::WasmLld(cc_hint.unwrap_or(cc)),
370380
LinkerFlavor::Unix(cc) => LinkerFlavor::Unix(cc_hint.unwrap_or(cc)),
371381
LinkerFlavor::Msvc(lld) => LinkerFlavor::Msvc(lld_hint.unwrap_or(lld)),
372-
LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Llbc | LinkerFlavor::Ptx => self,
382+
LinkerFlavor::EmCc
383+
| LinkerFlavor::Bpf
384+
| LinkerFlavor::Llbc
385+
| LinkerFlavor::Ptx
386+
| LinkerFlavor::Wild => self,
373387
}
374388
}
375389

@@ -397,7 +411,8 @@ impl LinkerFlavor {
397411
| (LinkerFlavor::EmCc, LinkerFlavorCli::EmCc)
398412
| (LinkerFlavor::Bpf, LinkerFlavorCli::Bpf)
399413
| (LinkerFlavor::Llbc, LinkerFlavorCli::Llbc)
400-
| (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx) => return true,
414+
| (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx)
415+
| (LinkerFlavor::Wild, LinkerFlavorCli::Wild) => return true,
401416
// 2. The linker flavor is independent of target and compatible
402417
(LinkerFlavor::Ptx, LinkerFlavorCli::Llbc) => return true,
403418
_ => {}
@@ -427,6 +442,7 @@ impl LinkerFlavor {
427442
LinkerFlavor::Darwin(..) => LldFlavor::Ld64,
428443
LinkerFlavor::WasmLld(..) => LldFlavor::Wasm,
429444
LinkerFlavor::Msvc(..) => LldFlavor::Link,
445+
LinkerFlavor::Wild => todo!(),
430446
}
431447
}
432448

@@ -449,7 +465,8 @@ impl LinkerFlavor {
449465
| LinkerFlavor::Unix(_)
450466
| LinkerFlavor::Bpf
451467
| LinkerFlavor::Llbc
452-
| LinkerFlavor::Ptx => false,
468+
| LinkerFlavor::Ptx
469+
| LinkerFlavor::Wild => false,
453470
}
454471
}
455472

@@ -461,7 +478,8 @@ impl LinkerFlavor {
461478
| LinkerFlavor::Darwin(Cc::Yes, _)
462479
| LinkerFlavor::WasmLld(Cc::Yes)
463480
| LinkerFlavor::Unix(Cc::Yes)
464-
| LinkerFlavor::EmCc => true,
481+
| LinkerFlavor::EmCc
482+
| LinkerFlavor::Wild => true,
465483
LinkerFlavor::Gnu(..)
466484
| LinkerFlavor::Darwin(..)
467485
| LinkerFlavor::WasmLld(_)
@@ -546,6 +564,7 @@ linker_flavor_cli_impls! {
546564
(LinkerFlavorCli::Bpf) "bpf"
547565
(LinkerFlavorCli::Llbc) "llbc"
548566
(LinkerFlavorCli::Ptx) "ptx"
567+
(LinkerFlavorCli::Wild) "wild"
549568

550569
// Legacy stable flavors
551570
(LinkerFlavorCli::Gcc) "gcc"
@@ -2847,6 +2866,7 @@ fn add_link_args_iter(
28472866
assert_eq!(lld, Lld::No);
28482867
insert(LinkerFlavor::Msvc(Lld::Yes));
28492868
}
2869+
LinkerFlavor::Wild => insert(LinkerFlavor::Wild),
28502870
LinkerFlavor::WasmLld(..)
28512871
| LinkerFlavor::Unix(..)
28522872
| LinkerFlavor::EmCc
@@ -3237,6 +3257,7 @@ impl Target {
32373257
| LinkerFlavor::Llbc => {
32383258
check_eq!(flavor, self.linker_flavor, "mixing different linker flavors")
32393259
}
3260+
LinkerFlavor::Wild => todo!(),
32403261
}
32413262

32423263
// Check that link args for cc and non-cc versions of flavors are consistent.

compiler/rustc_target/src/spec/targets/x86_64_unknown_linux_gnu.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub(crate) fn target() -> Target {
2727
base.link_self_contained = crate::spec::LinkSelfContainedDefault::with_linker();
2828
}
2929

30+
base.linker_flavor = LinkerFlavor::Wild;
31+
3032
Target {
3133
llvm_target: "x86_64-unknown-linux-gnu".into(),
3234
metadata: TargetMetadata {

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use serde_derive::Deserialize;
2020
use tracing::span;
2121

2222
use crate::core::build_steps::gcc::{Gcc, GccOutput, add_cg_gcc_cargo_flags};
23-
use crate::core::build_steps::tool::{RustcPrivateCompilers, SourceType, copy_lld_artifacts};
23+
use crate::core::build_steps::tool::{
24+
RustcPrivateCompilers, SourceType, copy_lld_artifacts, copy_wild_artifacts,
25+
};
2426
use crate::core::build_steps::{dist, llvm};
2527
use crate::core::builder;
2628
use crate::core::builder::{
@@ -2312,6 +2314,15 @@ impl Step for Assemble {
23122314
copy_lld_artifacts(builder, lld_wrapper, target_compiler);
23132315
}
23142316

2317+
if builder.host_target.triple == "x86_64-unknown-linux-gnu" {
2318+
let wild_wrapper =
2319+
builder.ensure(crate::core::build_steps::tool::WildLinker::for_use_by_compiler(
2320+
builder,
2321+
target_compiler,
2322+
));
2323+
copy_wild_artifacts(builder, wild_wrapper, target_compiler);
2324+
}
2325+
23152326
if builder.config.llvm_enabled(target_compiler.host) && builder.config.llvm_tools_enabled {
23162327
debug!(
23172328
"llvm and llvm tools enabled; copying `llvm-objcopy` as `rust-objcopy` to \

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,33 @@ impl Step for Rustc {
552552
}
553553
}
554554

555+
if builder.host_target.triple == "x86_64-unknown-linux-gnu" {
556+
let src_dir = builder.sysroot_target_bindir(target_compiler, target);
557+
let rust_wild = exe("rust-wild", target_compiler.host);
558+
builder.copy_link(
559+
&src_dir.join(&rust_wild),
560+
&dst_dir.join(&rust_wild),
561+
FileType::Executable,
562+
);
563+
let self_contained_wild_src_dir = src_dir.join("wild-gcc-ld");
564+
let self_contained_wild_dst_dir = dst_dir.join("wild-gcc-ld");
565+
t!(fs::create_dir(&self_contained_wild_dst_dir));
566+
let wild_name = "wild";
567+
let exe_name = exe(wild_name, target_compiler.host);
568+
builder.copy_link(
569+
&self_contained_wild_src_dir.join(&exe_name),
570+
&self_contained_wild_dst_dir.join(&exe_name),
571+
FileType::Executable,
572+
);
573+
// Pretend Wild is LD so the compiler can pick it up
574+
let exe_name = exe("ld", target_compiler.host);
575+
builder.copy_link(
576+
&self_contained_wild_src_dir.join(&exe_name),
577+
&self_contained_wild_dst_dir.join(&exe_name),
578+
FileType::Executable,
579+
);
580+
}
581+
555582
if builder.config.llvm_enabled(target_compiler.host)
556583
&& builder.config.llvm_tools_enabled
557584
{

0 commit comments

Comments
 (0)