From 43f2055af5a0897876f25b6bdfa821dea8d5e4aa Mon Sep 17 00:00:00 2001 From: Kjetil Kjeka Date: Tue, 6 Feb 2024 19:46:12 +0100 Subject: [PATCH] LLVM Bitcode Linker: Add as a linker known to the compiler --- compiler/rustc_codegen_ssa/src/back/link.rs | 23 +++- compiler/rustc_codegen_ssa/src/back/linker.rs | 101 +++++++++++++++++- compiler/rustc_target/src/spec/mod.rs | 22 +++- .../rustc_target/src/spec/tests/tests_impl.rs | 5 +- 4 files changed, 142 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index fcb3602b734..e70cc9b6216 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -24,6 +24,7 @@ use rustc_span::symbol::Symbol; use rustc_target::spec::crt_objects::CrtObjects; use rustc_target::spec::LinkSelfContainedComponents; use rustc_target::spec::LinkSelfContainedDefault; +use rustc_target::spec::LinkerFlavorCli; use rustc_target::spec::{Cc, LinkOutputKind, LinkerFlavor, Lld, PanicStrategy}; use rustc_target::spec::{RelocModel, RelroLevel, SanitizerSet, SplitDebuginfo}; @@ -1350,6 +1351,7 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { } } LinkerFlavor::Bpf => "bpf-linker", + LinkerFlavor::Llbc => "llvm-bitcode-linker", LinkerFlavor::Ptx => "rust-ptx-linker", }), flavor, @@ -1367,8 +1369,17 @@ pub fn linker_and_flavor(sess: &Session) -> (PathBuf, LinkerFlavor) { // linker and linker flavor specified via command line have precedence over what the target // specification specifies - let linker_flavor = - sess.opts.cg.linker_flavor.map(|flavor| sess.target.linker_flavor.with_cli_hints(flavor)); + let linker_flavor = match sess.opts.cg.linker_flavor { + // The linker flavors that are non-target specific can be directly translated to LinkerFlavor + Some(LinkerFlavorCli::Llbc) => Some(LinkerFlavor::Llbc), + Some(LinkerFlavorCli::Ptx) => Some(LinkerFlavor::Ptx), + // The linker flavors that corresponds to targets needs logic that keeps the base LinkerFlavor + _ => sess + .opts + .cg + .linker_flavor + .map(|flavor| sess.target.linker_flavor.with_cli_hints(flavor)), + }; if let Some(ret) = infer_from(sess, sess.opts.cg.linker.clone(), linker_flavor) { return ret; } @@ -2338,8 +2349,12 @@ fn add_order_independent_options( }); } - if flavor == LinkerFlavor::Ptx { - // Provide the linker with fallback to internal `target-cpu`. + if flavor == LinkerFlavor::Llbc { + cmd.arg("--target"); + cmd.arg(sess.target.llvm_target.as_ref()); + cmd.arg("--target-cpu"); + cmd.arg(&codegen_results.crate_info.target_cpu); + } else if flavor == LinkerFlavor::Ptx { cmd.arg("--fallback-arch"); cmd.arg(&codegen_results.crate_info.target_cpu); } else if flavor == LinkerFlavor::Bpf { diff --git a/compiler/rustc_codegen_ssa/src/back/linker.rs b/compiler/rustc_codegen_ssa/src/back/linker.rs index e52efd86955..b4e054417f3 100644 --- a/compiler/rustc_codegen_ssa/src/back/linker.rs +++ b/compiler/rustc_codegen_ssa/src/back/linker.rs @@ -153,6 +153,7 @@ pub fn get_linker<'a>( LinkerFlavor::Msvc(..) => Box::new(MsvcLinker { cmd, sess }) as Box, LinkerFlavor::EmCc => Box::new(EmLinker { cmd, sess }) as Box, LinkerFlavor::Bpf => Box::new(BpfLinker { cmd, sess }) as Box, + LinkerFlavor::Llbc => Box::new(LlbcLinker { cmd, sess }) as Box, LinkerFlavor::Ptx => Box::new(PtxLinker { cmd, sess }) as Box, } } @@ -1824,7 +1825,7 @@ impl<'a> Linker for PtxLinker<'a> { } Lto::No => {} - }; + } } fn output_filename(&mut self, path: &Path) { @@ -1862,6 +1863,104 @@ impl<'a> Linker for PtxLinker<'a> { fn linker_plugin_lto(&mut self) {} } +/// The `self-contained` LLVM bitcode linker +pub struct LlbcLinker<'a> { + cmd: Command, + sess: &'a Session, +} + +impl<'a> Linker for LlbcLinker<'a> { + fn cmd(&mut self) -> &mut Command { + &mut self.cmd + } + + fn set_output_kind(&mut self, _output_kind: LinkOutputKind, _out_filename: &Path) {} + + fn link_dylib_by_name(&mut self, _name: &str, _verbatim: bool, _as_needed: bool) { + panic!("external dylibs not supported") + } + + fn link_staticlib_by_name( + &mut self, + _name: &str, + _verbatim: bool, + _whole_archive: bool, + _search_paths: &SearchPaths, + ) { + panic!("staticlibs not supported") + } + + fn link_staticlib_by_path(&mut self, path: &Path, _whole_archive: bool) { + self.cmd.arg(path); + } + + fn include_path(&mut self, path: &Path) { + self.cmd.arg("-L").arg(path); + } + + fn debuginfo(&mut self, _strip: Strip, _: &[PathBuf]) { + self.cmd.arg("--debug"); + } + + fn add_object(&mut self, path: &Path) { + self.cmd.arg(path); + } + + fn optimize(&mut self) { + match self.sess.opts.optimize { + OptLevel::No => "-O0", + OptLevel::Less => "-O1", + OptLevel::Default => "-O2", + OptLevel::Aggressive => "-O3", + OptLevel::Size => "-Os", + OptLevel::SizeMin => "-Oz", + }; + } + + fn output_filename(&mut self, path: &Path) { + self.cmd.arg("-o").arg(path); + } + + fn framework_path(&mut self, _path: &Path) { + panic!("frameworks not supported") + } + + fn full_relro(&mut self) {} + + fn partial_relro(&mut self) {} + + fn no_relro(&mut self) {} + + fn gc_sections(&mut self, _keep_metadata: bool) {} + + fn no_gc_sections(&mut self) {} + + fn pgo_gen(&mut self) {} + + fn no_crt_objects(&mut self) {} + + fn no_default_libraries(&mut self) {} + + fn control_flow_guard(&mut self) {} + + fn ehcont_guard(&mut self) {} + + fn export_symbols(&mut self, _tmpdir: &Path, _crate_type: CrateType, symbols: &[String]) { + match _crate_type { + CrateType::Cdylib => { + for sym in symbols { + self.cmd.arg("--export-symbol").arg(sym); + } + } + _ => (), + } + } + + fn subsystem(&mut self, _subsystem: &str) {} + + fn linker_plugin_lto(&mut self) {} +} + pub struct BpfLinker<'a> { cmd: Command, sess: &'a Session, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index 3b6e15fad46..7d2a6d3d861 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -123,6 +123,8 @@ pub enum LinkerFlavor { Bpf, /// Linker tool for Nvidia PTX. Ptx, + /// LLVM bitcode linker that can be used as a `self-contained` linker + Llbc, } /// Linker flavors available externally through command line (`-Clinker-flavor`) @@ -141,6 +143,7 @@ pub enum LinkerFlavorCli { EmCc, Bpf, Ptx, + Llbc, // Legacy stable values Gcc, @@ -160,6 +163,7 @@ impl LinkerFlavorCli { | LinkerFlavorCli::Msvc(Lld::Yes) | LinkerFlavorCli::EmCc | LinkerFlavorCli::Bpf + | LinkerFlavorCli::Llbc | LinkerFlavorCli::Ptx => true, LinkerFlavorCli::Gcc | LinkerFlavorCli::Ld @@ -219,6 +223,7 @@ impl LinkerFlavor { LinkerFlavorCli::Msvc(lld) => LinkerFlavor::Msvc(lld), LinkerFlavorCli::EmCc => LinkerFlavor::EmCc, LinkerFlavorCli::Bpf => LinkerFlavor::Bpf, + LinkerFlavorCli::Llbc => LinkerFlavor::Llbc, LinkerFlavorCli::Ptx => LinkerFlavor::Ptx, // Below: legacy stable values @@ -258,6 +263,7 @@ impl LinkerFlavor { LinkerFlavor::Msvc(..) => LinkerFlavorCli::Msvc(Lld::No), LinkerFlavor::EmCc => LinkerFlavorCli::Em, LinkerFlavor::Bpf => LinkerFlavorCli::Bpf, + LinkerFlavor::Llbc => LinkerFlavorCli::Llbc, LinkerFlavor::Ptx => LinkerFlavorCli::Ptx, } } @@ -272,6 +278,7 @@ impl LinkerFlavor { LinkerFlavor::Msvc(lld) => LinkerFlavorCli::Msvc(lld), LinkerFlavor::EmCc => LinkerFlavorCli::EmCc, LinkerFlavor::Bpf => LinkerFlavorCli::Bpf, + LinkerFlavor::Llbc => LinkerFlavorCli::Llbc, LinkerFlavor::Ptx => LinkerFlavorCli::Ptx, } } @@ -286,6 +293,7 @@ impl LinkerFlavor { LinkerFlavorCli::Msvc(lld) => (Some(Cc::No), Some(lld)), LinkerFlavorCli::EmCc => (Some(Cc::Yes), Some(Lld::Yes)), LinkerFlavorCli::Bpf | LinkerFlavorCli::Ptx => (None, None), + LinkerFlavorCli::Llbc => (None, None), // Below: legacy stable values LinkerFlavorCli::Gcc => (Some(Cc::Yes), None), @@ -340,7 +348,7 @@ impl LinkerFlavor { LinkerFlavor::WasmLld(cc) => LinkerFlavor::WasmLld(cc_hint.unwrap_or(cc)), LinkerFlavor::Unix(cc) => LinkerFlavor::Unix(cc_hint.unwrap_or(cc)), LinkerFlavor::Msvc(lld) => LinkerFlavor::Msvc(lld_hint.unwrap_or(lld)), - LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Ptx => self, + LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Llbc | LinkerFlavor::Ptx => self, } } @@ -355,8 +363,8 @@ impl LinkerFlavor { pub fn check_compatibility(self, cli: LinkerFlavorCli) -> Option { let compatible = |cli| { // The CLI flavor should be compatible with the target if: - // 1. they are counterparts: they have the same principal flavor. match (self, cli) { + // 1. they are counterparts: they have the same principal flavor. (LinkerFlavor::Gnu(..), LinkerFlavorCli::Gnu(..)) | (LinkerFlavor::Darwin(..), LinkerFlavorCli::Darwin(..)) | (LinkerFlavor::WasmLld(..), LinkerFlavorCli::WasmLld(..)) @@ -364,11 +372,14 @@ impl LinkerFlavor { | (LinkerFlavor::Msvc(..), LinkerFlavorCli::Msvc(..)) | (LinkerFlavor::EmCc, LinkerFlavorCli::EmCc) | (LinkerFlavor::Bpf, LinkerFlavorCli::Bpf) + | (LinkerFlavor::Llbc, LinkerFlavorCli::Llbc) | (LinkerFlavor::Ptx, LinkerFlavorCli::Ptx) => return true, + // 2. The linker flavor is independent of target and compatible + (LinkerFlavor::Ptx, LinkerFlavorCli::Llbc) => return true, _ => {} } - // 2. or, the flavor is legacy and survives this roundtrip. + // 3. or, the flavor is legacy and survives this roundtrip. cli == self.with_cli_hints(cli).to_cli() }; (!compatible(cli)).then(|| { @@ -387,6 +398,7 @@ impl LinkerFlavor { | LinkerFlavor::Unix(..) | LinkerFlavor::EmCc | LinkerFlavor::Bpf + | LinkerFlavor::Llbc | LinkerFlavor::Ptx => LldFlavor::Ld, LinkerFlavor::Darwin(..) => LldFlavor::Ld64, LinkerFlavor::WasmLld(..) => LldFlavor::Wasm, @@ -412,6 +424,7 @@ impl LinkerFlavor { | LinkerFlavor::Msvc(_) | LinkerFlavor::Unix(_) | LinkerFlavor::Bpf + | LinkerFlavor::Llbc | LinkerFlavor::Ptx => false, } } @@ -431,6 +444,7 @@ impl LinkerFlavor { | LinkerFlavor::Msvc(_) | LinkerFlavor::Unix(_) | LinkerFlavor::Bpf + | LinkerFlavor::Llbc | LinkerFlavor::Ptx => false, } } @@ -480,6 +494,7 @@ linker_flavor_cli_impls! { (LinkerFlavorCli::Msvc(Lld::No)) "msvc" (LinkerFlavorCli::EmCc) "em-cc" (LinkerFlavorCli::Bpf) "bpf" + (LinkerFlavorCli::Llbc) "llbc" (LinkerFlavorCli::Ptx) "ptx" // Legacy stable flavors @@ -2205,6 +2220,7 @@ fn add_link_args_iter( | LinkerFlavor::Unix(..) | LinkerFlavor::EmCc | LinkerFlavor::Bpf + | LinkerFlavor::Llbc | LinkerFlavor::Ptx => {} } } diff --git a/compiler/rustc_target/src/spec/tests/tests_impl.rs b/compiler/rustc_target/src/spec/tests/tests_impl.rs index 3fefd60f7cd..3be18ef3127 100644 --- a/compiler/rustc_target/src/spec/tests/tests_impl.rs +++ b/compiler/rustc_target/src/spec/tests/tests_impl.rs @@ -56,7 +56,10 @@ impl Target { LinkerFlavor::Msvc(..) => { assert_matches!(flavor, LinkerFlavor::Msvc(..)) } - LinkerFlavor::EmCc | LinkerFlavor::Bpf | LinkerFlavor::Ptx => { + LinkerFlavor::EmCc + | LinkerFlavor::Bpf + | LinkerFlavor::Ptx + | LinkerFlavor::Llbc => { assert_eq!(flavor, self.linker_flavor) } }