Skip to content

Commit 935529b

Browse files
author
kulst
committed
Pass through of target features to llvm-bitcode-linker and handling them
The .ptx version produced by llc can be specified by passing it with --mattr. Currently it is not possible to specify the .ptx version with -Ctarget-feature because these are not passed through to llvm-bitcode-linker and handled by it. This commit adds both. --target-feature and -mattr are passed with equals to mitigate issues when the value starts with a - (minus).
1 parent 820bfff commit 935529b

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2518,6 +2518,12 @@ fn add_order_independent_options(
25182518
"--target-cpu",
25192519
&codegen_results.crate_info.target_cpu,
25202520
]);
2521+
if let Some(feat) = [sess.opts.cg.target_feature.as_str(), &sess.target.options.features]
2522+
.into_iter()
2523+
.find(|feat| !feat.is_empty())
2524+
{
2525+
cmd.link_arg(&format!("--target-feature={}", feat));
2526+
}
25212527
} else if flavor == LinkerFlavor::Ptx {
25222528
cmd.link_args(&["--fallback-arch", &codegen_results.crate_info.target_cpu]);
25232529
} else if flavor == LinkerFlavor::Bpf {

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)