Skip to content

Commit 6d74563

Browse files
Rollup merge of #136608 - kulst:ptx_target_features, r=bjorn3
Pass through of target features to llvm-bitcode-linker and handling them When using the llvm-bitcode-linker (`linker-flavor=llbc`) target-features are not passed through and are not handled by it. The llvm-bitcode-linker is mainly used as a self contained linker to link llvm bitcode for the nvptx64 target. It uses `llvm-link`, `opt` and `llc` internally. To produce a `.ptx` file of a specific ptx-version it is necessary to pass the version to llc with the `--mattr` option. Without explicitly setting it, the emitted `.ptx`-version is the minimum supported version of the `--target-cpu`. I would like to be able to explicitly set the ptx version as [some llvm problems only occur in earlier `.ptx`-versions](llvm/llvm-project#112998). Therefore this pull request adds support for passing target features to llvm-bitcode-linker and handling them. I was not quite sure if adding these features to `rustc_target/src/target_features.rs` is necessary or not. If so I will gladly add these. r? ``@kjetilkjeka``
2 parents 921ef32 + 831d9f3 commit 6d74563

File tree

5 files changed

+26
-2
lines changed

5 files changed

+26
-2
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,12 @@ fn add_order_independent_options(
25192519
"--target-cpu",
25202520
&codegen_results.crate_info.target_cpu,
25212521
]);
2522+
if codegen_results.crate_info.target_features.len() > 0 {
2523+
cmd.link_arg(&format!(
2524+
"--target-feature={}",
2525+
&codegen_results.crate_info.target_features.join(",")
2526+
));
2527+
}
25222528
} else if flavor == LinkerFlavor::Ptx {
25232529
cmd.link_args(&["--fallback-arch", &codegen_results.crate_info.target_cpu]);
25242530
} else if flavor == LinkerFlavor::Bpf {

compiler/rustc_codegen_ssa/src/base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,7 @@ impl CrateInfo {
915915
let n_crates = crates.len();
916916
let mut info = CrateInfo {
917917
target_cpu,
918+
target_features: tcx.global_backend_features(()).clone(),
918919
crate_types,
919920
exported_symbols,
920921
linked_symbols,

compiler/rustc_codegen_ssa/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ impl From<&cstore::NativeLib> for NativeLib {
190190
#[derive(Debug, Encodable, Decodable)]
191191
pub struct CrateInfo {
192192
pub target_cpu: String,
193+
pub target_features: Vec<String>,
193194
pub crate_types: Vec<CrateType>,
194195
pub exported_symbols: UnordMap<CrateType, Vec<String>>,
195196
pub linked_symbols: FxIndexMap<CrateType, Vec<(String, SymbolExportKind)>>,
@@ -230,6 +231,7 @@ pub fn provide(providers: &mut Providers) {
230231
crate::base::provide(providers);
231232
crate::target_features::provide(providers);
232233
crate::codegen_attrs::provide(providers);
234+
providers.queries.global_backend_features = |_tcx: TyCtxt<'_>, ()| vec![];
233235
}
234236

235237
/// Checks if the given filename ends with the `.rcgu.o` extension that `rustc`

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub struct Args {
2727
#[arg(long)]
2828
target_cpu: Option<String>,
2929

30+
/// The target features
31+
#[arg(long)]
32+
target_feature: Option<String>,
33+
3034
/// Write output to the filename
3135
#[arg(short, long)]
3236
output: PathBuf,
@@ -49,7 +53,7 @@ fn main() -> anyhow::Result<()> {
4953

5054
let args = Args::parse();
5155

52-
let mut linker = Session::new(args.target, args.target_cpu, args.output);
56+
let mut linker = Session::new(args.target, args.target_cpu, args.target_feature, args.output);
5357

5458
linker.add_exported_symbols(args.export_symbol);
5559

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{Optimization, Target};
88
pub struct Session {
99
target: Target,
1010
cpu: Option<String>,
11+
feature: Option<String>,
1112
symbols: Vec<String>,
1213

1314
/// A file that `llvm-link` supports, like a bitcode file or an archive.
@@ -21,14 +22,20 @@ pub struct Session {
2122
}
2223

2324
impl Session {
24-
pub fn new(target: crate::Target, cpu: Option<String>, out_path: PathBuf) -> Self {
25+
pub fn new(
26+
target: crate::Target,
27+
cpu: Option<String>,
28+
feature: Option<String>,
29+
out_path: PathBuf,
30+
) -> Self {
2531
let link_path = out_path.with_extension("o");
2632
let opt_path = out_path.with_extension("optimized.o");
2733
let sym_path = out_path.with_extension("symbols.txt");
2834

2935
Session {
3036
target,
3137
cpu,
38+
feature,
3239
symbols: Vec::new(),
3340
files: Vec::new(),
3441
link_path,
@@ -134,6 +141,10 @@ impl Session {
134141
lcc_command.arg("--mcpu").arg(mcpu);
135142
}
136143

144+
if let Some(mattr) = &self.feature {
145+
lcc_command.arg(&format!("--mattr={}", mattr));
146+
}
147+
137148
let lcc_output = lcc_command
138149
.arg(&self.opt_path)
139150
.arg("-o").arg(&self.out_path)

0 commit comments

Comments
 (0)