Skip to content

Commit

Permalink
Distribute LLVM bitcode linker as a preview component
Browse files Browse the repository at this point in the history
  • Loading branch information
Kjetil Kjeka committed Apr 15, 2024
1 parent d493fd1 commit 235d45e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 7 deletions.
48 changes: 48 additions & 0 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,7 @@ impl Step for Extended {
compiler: builder.compiler(stage, target),
backend: "cranelift".to_string(),
});
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {compiler, target});

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

Expand Down Expand Up @@ -2224,6 +2225,53 @@ impl Step for LlvmTools {
}
}

#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
pub struct LlvmBitcodeLinker {
pub compiler: Compiler,
pub target: TargetSelection,
}

impl Step for LlvmBitcodeLinker {
type Output = Option<GeneratedTarball>;
const DEFAULT: bool = true;
const ONLY_HOSTS: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let default = should_build_extended_tool(run.builder, "llvm-bitcode-linker");
run.alias("llvm-bitcode-linker").default_condition(default)
}

fn make_run(run: RunConfig<'_>) {
run.builder.ensure(LlvmBitcodeLinker {
compiler: run.builder.compiler_for(
run.builder.top_stage,
run.builder.config.build,
run.target,
),
target: run.target,
});
}

fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
let compiler = self.compiler;
let target = self.target;

let llbc_linker =
builder.ensure(tool::LlvmBitcodeLinker { compiler, target, extra_features: vec![] });

let self_contained_bin_dir = format!("lib/rustlib/{}/bin/self-contained", target.triple);

// Prepare the image directory
let mut tarball = Tarball::new(builder, "llvm-bitcode-linker", &target.triple);
tarball.set_overlay(OverlayKind::LlvmBitcodeLinker);
tarball.is_preview(true);

tarball.add_file(llbc_linker, self_contained_bin_dir, 0o755);

Some(tarball.generate())
}
}

// Tarball intended for internal consumption to ease rustc/std development.
//
// Should not be considered stable by end users.
Expand Down
9 changes: 9 additions & 0 deletions src/bootstrap/src/core/build_steps/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ install!((self, builder, _config),
);
}
};
LlvmBitcodeLinker, alias = "llvm-bitcode-linker", Self::should_build(_config), only_hosts: true, {
if let Some(tarball) = builder.ensure(dist::LlvmBitcodeLinker { compiler: self.compiler, target: self.target }) {
install_sh(builder, "llvm-bitcode-linker", self.compiler.stage, Some(self.target), &tarball);
} else {
builder.info(
&format!("skipping llvm-bitcode-linker stage{} ({})", self.compiler.stage, self.target),
);
}
};
);

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down
8 changes: 5 additions & 3 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,11 @@ impl Step for LlvmBitcodeLinker {
.join(exe(bin_name, self.compiler.host));

if self.compiler.stage > 0 {
let bindir = builder.sysroot(self.compiler).join("bin");
t!(fs::create_dir_all(&bindir));
let bin_destination = bindir.join(exe(bin_name, self.compiler.host));
let bindir_self_contained = builder
.sysroot(self.compiler)
.join(format!("lib/rustlib/{}/bin/self-contained", self.target.triple));
t!(fs::create_dir_all(&bindir_self_contained));
let bin_destination = bindir_self_contained.join(exe(bin_name, self.compiler.host));
builder.copy_link(&tool_out, &bin_destination);
bin_destination
} else {
Expand Down
1 change: 1 addition & 0 deletions src/bootstrap/src/core/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ impl<'a> Builder<'a> {
dist::Clippy,
dist::Miri,
dist::LlvmTools,
dist::LlvmBitcodeLinker,
dist::RustDev,
dist::Bootstrap,
dist::Extended,
Expand Down
8 changes: 8 additions & 0 deletions src/bootstrap/src/utils/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub(crate) enum OverlayKind {
RLS,
RustAnalyzer,
RustcCodegenCranelift,
LlvmBitcodeLinker,
}

impl OverlayKind {
Expand Down Expand Up @@ -64,6 +65,12 @@ impl OverlayKind {
"compiler/rustc_codegen_cranelift/LICENSE-APACHE",
"compiler/rustc_codegen_cranelift/LICENSE-MIT",
],
OverlayKind::LlvmBitcodeLinker => &[
"COPYRIGHT",
"LICENSE-APACHE",
"LICENSE-MIT",
"src/tools/llvm-bitcode-linker/README.md",
],
}
}

Expand All @@ -87,6 +94,7 @@ impl OverlayKind {
.rust_analyzer_info
.version(builder, &builder.release_num("rust-analyzer/crates/rust-analyzer")),
OverlayKind::RustcCodegenCranelift => builder.rust_version(),
OverlayKind::LlvmBitcodeLinker => builder.rust_version(),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/doc/unstable-book/src/compiler-flags/codegen-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ In addition to the stable set of linker flavors, the following unstable values a
- `ptx`: use [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker)
for Nvidia NVPTX GPGPU support.
- `bpf`: use [`bpf-linker`](https://github.com/alessandrod/bpf-linker) for eBPF support.
- `llbc`: for linking in llvm bitcode. Install the preview rustup components`llvm-bitcode-linker`
and `llvm-tools` to use as a self-contained linker by passing
`-Zunstable-options -Clink-self-contained=+linker` together with `-Clinker-flavor=llbc`.
Can currently only be used for Nvidia NVPTX targets (`nvptx64-nvidia-cuda`).

Additionally, a set of more precise linker flavors also exists, for example allowing targets to
declare that they use the LLD linker by default. The following values are currently unstable, and
Expand Down
13 changes: 9 additions & 4 deletions src/tools/llvm-bitcode-linker/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Session {
.arg("-o")
.arg(&self.link_path)
.output()
.unwrap();
.context("An error occured when calling llvm-link. Make sure the llvm-tools component is installed.")?;

if !llvm_link_output.status.success() {
tracing::error!(
Expand Down Expand Up @@ -108,7 +108,9 @@ impl Session {
opt_cmd.arg("--strip-debug");
}

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

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

let lcc_output =
lcc_command.arg(&self.opt_path).arg("-o").arg(&self.out_path).output().unwrap();
let lcc_output = lcc_command
.arg(&self.opt_path)
.arg("-o").arg(&self.out_path)
.output()
.context("An error occured when calling llc. Make sure the llvm-tools component is installed.")?;

if !lcc_output.status.success() {
tracing::error!(
Expand Down

0 comments on commit 235d45e

Please sign in to comment.