Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 30e6af0

Browse files
author
Kjetil Kjeka
committedApr 15, 2024
Distribute LLVM bitcode linker as a preview component
1 parent a74a047 commit 30e6af0

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed
 

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,7 @@ impl Step for Extended {
15461546
compiler: builder.compiler(stage, target),
15471547
backend: "cranelift".to_string(),
15481548
});
1549+
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {compiler, target});
15491550

15501551
let etc = builder.src.join("src/etc/installer");
15511552

@@ -2222,6 +2223,53 @@ impl Step for LlvmTools {
22222223
}
22232224
}
22242225

2226+
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
2227+
pub struct LlvmBitcodeLinker {
2228+
pub compiler: Compiler,
2229+
pub target: TargetSelection,
2230+
}
2231+
2232+
impl Step for LlvmBitcodeLinker {
2233+
type Output = Option<GeneratedTarball>;
2234+
const DEFAULT: bool = true;
2235+
const ONLY_HOSTS: bool = true;
2236+
2237+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2238+
let default = should_build_extended_tool(run.builder, "llvm-bitcode-linker");
2239+
run.alias("llvm-bitcode-linker").default_condition(default)
2240+
}
2241+
2242+
fn make_run(run: RunConfig<'_>) {
2243+
run.builder.ensure(LlvmBitcodeLinker {
2244+
compiler: run.builder.compiler_for(
2245+
run.builder.top_stage,
2246+
run.builder.config.build,
2247+
run.target,
2248+
),
2249+
target: run.target,
2250+
});
2251+
}
2252+
2253+
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
2254+
let compiler = self.compiler;
2255+
let target = self.target;
2256+
2257+
let llbc_linker =
2258+
builder.ensure(tool::LlvmBitcodeLinker { compiler, target, extra_features: vec![] });
2259+
2260+
let self_contained_bin_dir = format!("lib/rustlib/{}/bin/self-contained", target.triple);
2261+
2262+
// Prepare the image directory
2263+
let mut tarball = Tarball::new(builder, "llvm-bitcode-linker", &target.triple);
2264+
tarball.set_overlay(OverlayKind::LlvmBitcodeLinker);
2265+
tarball.is_preview(true);
2266+
2267+
tarball.add_file(llbc_linker, self_contained_bin_dir, 0o755);
2268+
2269+
Some(tarball.generate())
2270+
}
2271+
}
2272+
22252273
// Tarball intended for internal consumption to ease rustc/std development.
22262274
//
22272275
// Should not be considered stable by end users.

‎src/bootstrap/src/core/build_steps/install.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ install!((self, builder, _config),
300300
);
301301
}
302302
};
303+
LlvmBitcodeLinker, alias = "llvm-bitcode-linker", Self::should_build(_config), only_hosts: true, {
304+
if let Some(tarball) = builder.ensure(dist::LlvmBitcodeLinker { compiler: self.compiler, target: self.target }) {
305+
install_sh(builder, "llvm-bitcode-linker", self.compiler.stage, Some(self.target), &tarball);
306+
} else {
307+
builder.info(
308+
&format!("skipping llvm-bitcode-linker stage{} ({})", self.compiler.stage, self.target),
309+
);
310+
}
311+
};
303312
);
304313

305314
#[derive(Debug, Clone, Hash, PartialEq, Eq)]

‎src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,11 @@ impl Step for LlvmBitcodeLinker {
746746
.join(exe(bin_name, self.compiler.host));
747747

748748
if self.compiler.stage > 0 {
749-
let bindir = builder.sysroot(self.compiler).join("bin");
750-
t!(fs::create_dir_all(&bindir));
751-
let bin_destination = bindir.join(exe(bin_name, self.compiler.host));
749+
let bindir_self_contained = builder
750+
.sysroot(self.compiler)
751+
.join(format!("lib/rustlib/{}/bin/self-contained", self.target.triple));
752+
t!(fs::create_dir_all(&bindir_self_contained));
753+
let bin_destination = bindir_self_contained.join(exe(bin_name, self.compiler.host));
752754
builder.copy_link(&tool_out, &bin_destination);
753755
bin_destination
754756
} else {

‎src/bootstrap/src/core/builder.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,7 @@ impl<'a> Builder<'a> {
869869
dist::Clippy,
870870
dist::Miri,
871871
dist::LlvmTools,
872+
dist::LlvmBitcodeLinker,
872873
dist::RustDev,
873874
dist::Bootstrap,
874875
dist::Extended,

‎src/bootstrap/src/utils/tarball.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) enum OverlayKind {
2020
RLS,
2121
RustAnalyzer,
2222
RustcCodegenCranelift,
23+
LlvmBitcodeLinker,
2324
}
2425

2526
impl OverlayKind {
@@ -64,6 +65,12 @@ impl OverlayKind {
6465
"compiler/rustc_codegen_cranelift/LICENSE-APACHE",
6566
"compiler/rustc_codegen_cranelift/LICENSE-MIT",
6667
],
68+
OverlayKind::LlvmBitcodeLinker => &[
69+
"COPYRIGHT",
70+
"LICENSE-APACHE",
71+
"LICENSE-MIT",
72+
"src/tools/llvm-bitcode-linker/README.md",
73+
],
6774
}
6875
}
6976

@@ -87,6 +94,7 @@ impl OverlayKind {
8794
.rust_analyzer_info
8895
.version(builder, &builder.release_num("rust-analyzer/crates/rust-analyzer")),
8996
OverlayKind::RustcCodegenCranelift => builder.rust_version(),
97+
OverlayKind::LlvmBitcodeLinker => builder.rust_version(),
9098
}
9199
}
92100
}

‎src/doc/unstable-book/src/compiler-flags/codegen-options.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ In addition to the stable set of linker flavors, the following unstable values a
1010
- `ptx`: use [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker)
1111
for Nvidia NVPTX GPGPU support.
1212
- `bpf`: use [`bpf-linker`](https://github.com/alessandrod/bpf-linker) for eBPF support.
13+
- `llbc`: for linking in llvm bitcode. Install the preview rustup components`llvm-bitcode-linker`
14+
and `llvm-tools` to use as a self-contained linker by passing
15+
`-Zunstable-options -Clink-self-contained=+linker` together with `-Clinker-flavor=llbc`.
16+
Can currently only be used for Nvidia NVPTX targets (`nvptx64-nvidia-cuda`).
1317

1418
Additionally, a set of more precise linker flavors also exists, for example allowing targets to
1519
declare that they use the LLD linker by default. The following values are currently unstable, and

‎src/tools/llvm-bitcode-linker/src/linker.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Session {
6262
.arg("-o")
6363
.arg(&self.link_path)
6464
.output()
65-
.unwrap();
65+
.context("An error occured when calling llvm-link. Make sure the llvm-tools component is installed.")?;
6666

6767
if !llvm_link_output.status.success() {
6868
tracing::error!(
@@ -108,7 +108,9 @@ impl Session {
108108
opt_cmd.arg("--strip-debug");
109109
}
110110

111-
let opt_output = opt_cmd.output().unwrap();
111+
let opt_output = opt_cmd.output().context(
112+
"An error occured when calling opt. Make sure the llvm-tools component is installed.",
113+
)?;
112114

113115
if !opt_output.status.success() {
114116
tracing::error!(
@@ -133,8 +135,11 @@ impl Session {
133135
lcc_command.arg("--mcpu").arg(mcpu);
134136
}
135137

136-
let lcc_output =
137-
lcc_command.arg(&self.opt_path).arg("-o").arg(&self.out_path).output().unwrap();
138+
let lcc_output = lcc_command
139+
.arg(&self.opt_path)
140+
.arg("-o").arg(&self.out_path)
141+
.output()
142+
.context("An error occured when calling llc. Make sure the llvm-tools component is installed.")?;
138143

139144
if !lcc_output.status.success() {
140145
tracing::error!(

0 commit comments

Comments
 (0)
Please sign in to comment.