Skip to content

Commit e07d18f

Browse files
authored
Rollup merge of #123423 - kjetilkjeka:llvm_bitcode_linker_component, r=Mark-Simulacrum
Distribute LLVM bitcode linker as a preview component The self-contained LLVM bitcode linker was recently added in #117458. It is currently only in use to link the Nvidia ptx assembly tests when running rustc tests (local or CI). In fact, the linker itself is currently only usable for the `nvptx64-nvidia-cuda` target, but more targets will be supported in the future. The reason a new linker was needed for the ptx format is that the [old one](https://github.com/denzp/rust-ptx-linker) has not been updated the last few years. It worked fine for a while, but as LLVM changed it broke and the nvptx tests was [disabled in rustc back in 2019](f8f9a28). It was ad-hoc patched and have been used in a sub-optimal state by the community until now. If this PR is merged, the LLVM bitcode linker will be distributed as a preview component that can be used as a replacement for the old ptx-linker for development in addition to rustc tests. In addition to installing the `llvm-bitcode-linker` component, also the `llvm-tools` component must be installed as the `llvm-bitcode-linker` works by calling llvm tools. Even though the LLVM bitcode linker is in its early stages it already now provides a lot of value over the old ptx-linker just by working and using up-to-date llvm tooling. By shipping it as a component it will be easier to gather user experience and improving it. ``@petrochenkov`` when installing as a component it will be installed in the self-contained folder and will not work with `-Clink-self-contained=no` (although for some reason I expect to be a bug `-Clink-self-contained=-linker` doesn't properly disable it). However, when building using `x.py build` it will be placed in the `lib/rustlib/<target>/bin` directory and will be available for internal tests even if `-Clink-self-contained=no` is passed. CC: ``@Mark-Simulacrum`` as I very briefly discussed it with you some months ago https://rust-lang.zulipchat.com/#narrow/stream/242791-t-infra/topic/.E2.9C.94.20How.20to.20ship.20a.20new.20tool.20.28embedded.20linker.29.20to.20users.3F
2 parents 84e729a + 235d45e commit e07d18f

7 files changed

Lines changed: 84 additions & 7 deletions

File tree

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

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

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

@@ -2224,6 +2225,53 @@ impl Step for LlvmTools {
22242225
}
22252226
}
22262227

2228+
#[derive(Debug,PartialOrd,Ord,Clone,Hash,PartialEq,Eq)]
2229+
pubstructLlvmBitcodeLinker{
2230+
pubcompiler:Compiler,
2231+
pubtarget:TargetSelection,
2232+
}
2233+
2234+
implStepforLlvmBitcodeLinker{
2235+
typeOutput = Option<GeneratedTarball>;
2236+
constDEFAULT:bool = true;
2237+
constONLY_HOSTS:bool = true;
2238+
2239+
fnshould_run(run:ShouldRun<'_>) -> ShouldRun<'_>{
2240+
let default = should_build_extended_tool(run.builder,"llvm-bitcode-linker");
2241+
run.alias("llvm-bitcode-linker").default_condition(default)
2242+
}
2243+
2244+
fnmake_run(run:RunConfig<'_>){
2245+
run.builder.ensure(LlvmBitcodeLinker{
2246+
compiler: run.builder.compiler_for(
2247+
run.builder.top_stage,
2248+
run.builder.config.build,
2249+
run.target,
2250+
),
2251+
target: run.target,
2252+
});
2253+
}
2254+
2255+
fnrun(self,builder:&Builder<'_>) -> Option<GeneratedTarball>{
2256+
let compiler = self.compiler;
2257+
let target = self.target;
2258+
2259+
let llbc_linker =
2260+
builder.ensure(tool::LlvmBitcodeLinker{ compiler, target,extra_features:vec![]});
2261+
2262+
let self_contained_bin_dir = format!("lib/rustlib/{}/bin/self-contained", target.triple);
2263+
2264+
// Prepare the image directory
2265+
letmut tarball = Tarball::new(builder,"llvm-bitcode-linker",&target.triple);
2266+
tarball.set_overlay(OverlayKind::LlvmBitcodeLinker);
2267+
tarball.is_preview(true);
2268+
2269+
tarball.add_file(llbc_linker, self_contained_bin_dir,0o755);
2270+
2271+
Some(tarball.generate())
2272+
}
2273+
}
2274+
22272275
// Tarball intended for internal consumption to ease rustc/std development.
22282276
//
22292277
// 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+
ifletSome(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
ifself.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
@@ -853,6 +853,7 @@ impl<'a> Builder<'a> {
853853
dist::Clippy,
854854
dist::Miri,
855855
dist::LlvmTools,
856+
dist::LlvmBitcodeLinker,
856857
dist::RustDev,
857858
dist::Bootstrap,
858859
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
implOverlayKind{
@@ -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)