Skip to content

Commit 1826030

Browse files
authored
Rollup merge of #89316 - asquared31415:multiple-clobber-abi, r=Amanieu
Add support for specifying multiple clobber_abi in `asm!` r? `@Amanieu` cc #72016 `@rustbot` label: +A-inline-assembly +F-asm
2 parents 13c8c32 + b159d95 commit 1826030

File tree

10 files changed

+136
-93
lines changed

10 files changed

+136
-93
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2056,7 +2056,7 @@ pub struct InlineAsm {
20562056
pub template: Vec<InlineAsmTemplatePiece>,
20572057
pub template_strs: Box<[(Symbol, Option<Symbol>, Span)]>,
20582058
pub operands: Vec<(InlineAsmOperand, Span)>,
2059-
pub clobber_abi: Option<(Symbol, Span)>,
2059+
pub clobber_abis: Vec<(Symbol, Span)>,
20602060
pub options: InlineAsmOptions,
20612061
pub line_spans: Vec<Span>,
20622062
}
@@ -2705,7 +2705,7 @@ pub enum ItemKind {
27052705
/// E.g., `extern {}` or `extern "C" {}`.
27062706
ForeignMod(ForeignMod),
27072707
/// Module-level inline assembly (from `global_asm!()`).
2708-
GlobalAsm(InlineAsm),
2708+
GlobalAsm(Box<InlineAsm>),
27092709
/// A type alias (`type`).
27102710
///
27112711
/// E.g., `type Foo = Bar<u8>;`.

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::LoweringContext;
22

33
use rustc_ast::*;
44
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_data_structures::stable_set::FxHashSet;
56
use rustc_errors::struct_span_err;
67
use rustc_hir as hir;
78
use rustc_span::{Span, Symbol};
@@ -27,11 +28,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2728
.emit();
2829
}
2930

30-
let mut clobber_abi = None;
31+
let mut clobber_abis = FxHashMap::default();
3132
if let Some(asm_arch) = asm_arch {
32-
if let Some((abi_name, abi_span)) = asm.clobber_abi {
33+
for &(abi_name, abi_span) in &asm.clobber_abis {
3334
match asm::InlineAsmClobberAbi::parse(asm_arch, &self.sess.target, abi_name) {
34-
Ok(abi) => clobber_abi = Some((abi, abi_span)),
35+
Ok(abi) => {
36+
clobber_abis.insert(abi, abi_span);
37+
}
3538
Err(&[]) => {
3639
self.sess
3740
.struct_span_err(
@@ -368,8 +371,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
368371

369372
// If a clobber_abi is specified, add the necessary clobbers to the
370373
// operands list.
371-
if let Some((abi, abi_span)) = clobber_abi {
374+
let mut clobbered = FxHashSet::default();
375+
for (abi, abi_span) in clobber_abis {
372376
for &clobber in abi.clobbered_regs() {
377+
// Don't emit a clobber for a register already clobbered
378+
if clobbered.contains(&clobber) {
379+
continue;
380+
}
381+
373382
let mut output_used = false;
374383
clobber.overlapping_regs(|reg| {
375384
if used_output_regs.contains_key(&reg) {
@@ -386,6 +395,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
386395
},
387396
self.lower_span(abi_span),
388397
));
398+
clobbered.insert(clobber);
389399
}
390400
}
391401
}

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,8 +2199,8 @@ impl<'a> State<'a> {
21992199

22002200
let mut args = vec![AsmArg::Template(InlineAsmTemplatePiece::to_string(&asm.template))];
22012201
args.extend(asm.operands.iter().map(|(o, _)| AsmArg::Operand(o)));
2202-
if let Some((abi, _)) = asm.clobber_abi {
2203-
args.push(AsmArg::ClobberAbi(abi));
2202+
for (abi, _) in &asm.clobber_abis {
2203+
args.push(AsmArg::ClobberAbi(*abi));
22042204
}
22052205
if !asm.options.is_empty() {
22062206
args.push(AsmArg::Options(asm.options));

compiler/rustc_builtin_macros/src/asm.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct AsmArgs {
1919
operands: Vec<(ast::InlineAsmOperand, Span)>,
2020
named_args: FxHashMap<Symbol, usize>,
2121
reg_args: FxHashSet<usize>,
22-
clobber_abi: Option<(Symbol, Span)>,
22+
clobber_abis: Vec<(Symbol, Span)>,
2323
options: ast::InlineAsmOptions,
2424
options_spans: Vec<Span>,
2525
}
@@ -64,7 +64,7 @@ fn parse_args<'a>(
6464
operands: vec![],
6565
named_args: FxHashMap::default(),
6666
reg_args: FxHashSet::default(),
67-
clobber_abi: None,
67+
clobber_abis: Vec::new(),
6868
options: ast::InlineAsmOptions::empty(),
6969
options_spans: vec![],
7070
};
@@ -210,7 +210,7 @@ fn parse_args<'a>(
210210
.span_labels(args.options_spans.clone(), "previous options")
211211
.span_label(span, "argument")
212212
.emit();
213-
} else if let Some((_, abi_span)) = args.clobber_abi {
213+
} else if let Some(&(_, abi_span)) = args.clobber_abis.last() {
214214
ecx.struct_span_err(span, "arguments are not allowed after clobber_abi")
215215
.span_label(abi_span, "clobber_abi")
216216
.span_label(span, "argument")
@@ -322,10 +322,12 @@ fn parse_args<'a>(
322322
// Bail out now since this is likely to confuse MIR
323323
return Err(err);
324324
}
325-
if let Some((_, abi_span)) = args.clobber_abi {
325+
if args.clobber_abis.len() > 0 {
326326
if is_global_asm {
327-
let err =
328-
ecx.struct_span_err(abi_span, "`clobber_abi` cannot be used with `global_asm!`");
327+
let err = ecx.struct_span_err(
328+
args.clobber_abis.iter().map(|(_, sp)| *sp).collect::<Vec<Span>>(),
329+
"`clobber_abi` cannot be used with `global_asm!`",
330+
);
329331

330332
// Bail out now since this is likely to confuse later stages
331333
return Err(err);
@@ -335,7 +337,10 @@ fn parse_args<'a>(
335337
regclass_outputs.clone(),
336338
"asm with `clobber_abi` must specify explicit registers for outputs",
337339
)
338-
.span_label(abi_span, "clobber_abi")
340+
.span_labels(
341+
args.clobber_abis.iter().map(|(_, sp)| *sp).collect::<Vec<Span>>(),
342+
"clobber_abi",
343+
)
339344
.span_labels(regclass_outputs, "generic outputs")
340345
.emit();
341346
}
@@ -453,14 +458,7 @@ fn parse_clobber_abi<'a>(
453458

454459
let new_span = span_start.to(p.prev_token.span);
455460

456-
if let Some((_, prev_span)) = args.clobber_abi {
457-
let mut err = p
458-
.sess
459-
.span_diagnostic
460-
.struct_span_err(new_span, "clobber_abi specified multiple times");
461-
err.span_label(prev_span, "clobber_abi previously specified here");
462-
return Err(err);
463-
} else if !args.options_spans.is_empty() {
461+
if !args.options_spans.is_empty() {
464462
let mut err = p
465463
.sess
466464
.span_diagnostic
@@ -469,7 +467,7 @@ fn parse_clobber_abi<'a>(
469467
return Err(err);
470468
}
471469

472-
args.clobber_abi = Some((clobber_abi, new_span));
470+
args.clobber_abis.push((clobber_abi, new_span));
473471

474472
Ok(())
475473
}
@@ -770,7 +768,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
770768
template,
771769
template_strs: template_strs.into_boxed_slice(),
772770
operands: args.operands,
773-
clobber_abi: args.clobber_abi,
771+
clobber_abis: args.clobber_abis,
774772
options: args.options,
775773
line_spans,
776774
})
@@ -815,7 +813,7 @@ pub fn expand_global_asm<'cx>(
815813
ident: Ident::invalid(),
816814
attrs: Vec::new(),
817815
id: ast::DUMMY_NODE_ID,
818-
kind: ast::ItemKind::GlobalAsm(inline_asm),
816+
kind: ast::ItemKind::GlobalAsm(Box::new(inline_asm)),
819817
vis: ast::Visibility {
820818
span: sp.shrink_to_lo(),
821819
kind: ast::VisibilityKind::Inherited,

src/doc/unstable-book/src/library-features/asm.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ fn call_foo(arg: i32) -> i32 {
319319

320320
Note that the `fn` or `static` item does not need to be public or `#[no_mangle]`: the compiler will automatically insert the appropriate mangled symbol name into the assembly code.
321321

322-
By default, `asm!` assumes that any register not specified as an output will have its contents preserved by the assembly code. The [`clobber_abi`](#abi-clobbers) argument to `asm!` tells the compiler to automatically insert the necessary clobber operands according to the given calling convention ABI: any register which is not fully preserved in that ABI will be treated as clobbered.
322+
By default, `asm!` assumes that any register not specified as an output will have its contents preserved by the assembly code. The [`clobber_abi`](#abi-clobbers) argument to `asm!` tells the compiler to automatically insert the necessary clobber operands according to the given calling convention ABI: any register which is not fully preserved in that ABI will be treated as clobbered. Multiple `clobber_abi` arguments may be provided and all clobbers from all specified ABIs will be inserted.
323323

324324
## Register template modifiers
325325

@@ -456,7 +456,7 @@ operand := reg_operand / "const" const_expr / "sym" path
456456
clobber_abi := "clobber_abi(" <abi> ")"
457457
option := "pure" / "nomem" / "readonly" / "preserves_flags" / "noreturn" / "nostack" / "att_syntax" / "raw"
458458
options := "options(" option *["," option] [","] ")"
459-
asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) ["," clobber_abi] ["," options] [","] ")"
459+
asm := "asm!(" format_string *("," format_string) *("," [ident "="] operand) *("," clobber_abi) ["," options] [","] ")"
460460
```
461461

462462
Inline assembly is currently supported on the following architectures:
@@ -799,6 +799,8 @@ As stated in the previous section, passing an input value smaller than the regis
799799

800800
The `clobber_abi` keyword can be used to apply a default set of clobbers to an `asm` block. This will automatically insert the necessary clobber constraints as needed for calling a function with a particular calling convention: if the calling convention does not fully preserve the value of a register across a call then a `lateout("reg") _` is implicitly added to the operands list.
801801

802+
`clobber_abi` may be specified any number of times. It will insert a clobber for all unique registers in the union of all specified calling conventions.
803+
802804
Generic register class outputs are disallowed by the compiler when `clobber_abi` is used: all outputs must specify an explicit register. Explicit register outputs have precedence over the implicit clobbers inserted by `clobber_abi`: a clobber will only be inserted for a register if that register is not used as an output.
803805
The following ABIs can be used with `clobber_abi`:
804806

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// run-pass
2+
// needs-asm-support
3+
// only-x86_64
4+
5+
// Checks that multiple clobber_abi options can be used
6+
7+
#![feature(asm)]
8+
9+
extern "sysv64" fn foo(x: i32) -> i32 {
10+
x + 16
11+
}
12+
13+
extern "win64" fn bar(x: i32) -> i32 {
14+
x / 2
15+
}
16+
17+
fn main() {
18+
let x = 8;
19+
let y: i32;
20+
// call `foo` with `x` as the input, and then `bar` with the output of `foo`
21+
// and output that to `y`
22+
unsafe {
23+
asm!(
24+
"call {}; mov rcx, rax; call {}",
25+
sym foo,
26+
sym bar,
27+
in("rdi") x,
28+
out("rax") y,
29+
clobber_abi("sysv64"),
30+
clobber_abi("win64")
31+
);
32+
}
33+
assert_eq!((x, y), (8, 12));
34+
}

src/test/ui/asm/x86_64/bad-options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ fn main() {
2121
//~^ ERROR invalid ABI for `clobber_abi`
2222
asm!("{}", out(reg) foo, clobber_abi("C"));
2323
//~^ ERROR asm with `clobber_abi` must specify explicit registers for outputs
24+
asm!("{}", out(reg) foo, clobber_abi("C"), clobber_abi("C"));
25+
//~^ ERROR asm with `clobber_abi` must specify explicit registers for outputs
2426
asm!("", out("eax") foo, clobber_abi("C"));
2527
}
2628
}

src/test/ui/asm/x86_64/bad-options.stderr

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,47 @@ LL | asm!("{}", out(reg) foo, clobber_abi("C"));
3636
| |
3737
| generic outputs
3838

39+
error: asm with `clobber_abi` must specify explicit registers for outputs
40+
--> $DIR/bad-options.rs:24:20
41+
|
42+
LL | asm!("{}", out(reg) foo, clobber_abi("C"), clobber_abi("C"));
43+
| ^^^^^^^^^^^^ ---------------- ---------------- clobber_abi
44+
| | |
45+
| | clobber_abi
46+
| generic outputs
47+
3948
error: expected one of `)`, `att_syntax`, or `raw`, found `nomem`
40-
--> $DIR/bad-options.rs:28:25
49+
--> $DIR/bad-options.rs:30:25
4150
|
4251
LL | global_asm!("", options(nomem));
4352
| ^^^^^ expected one of `)`, `att_syntax`, or `raw`
4453

4554
error: expected one of `)`, `att_syntax`, or `raw`, found `readonly`
46-
--> $DIR/bad-options.rs:30:25
55+
--> $DIR/bad-options.rs:32:25
4756
|
4857
LL | global_asm!("", options(readonly));
4958
| ^^^^^^^^ expected one of `)`, `att_syntax`, or `raw`
5059

5160
error: expected one of `)`, `att_syntax`, or `raw`, found `noreturn`
52-
--> $DIR/bad-options.rs:32:25
61+
--> $DIR/bad-options.rs:34:25
5362
|
5463
LL | global_asm!("", options(noreturn));
5564
| ^^^^^^^^ expected one of `)`, `att_syntax`, or `raw`
5665

5766
error: expected one of `)`, `att_syntax`, or `raw`, found `pure`
58-
--> $DIR/bad-options.rs:34:25
67+
--> $DIR/bad-options.rs:36:25
5968
|
6069
LL | global_asm!("", options(pure));
6170
| ^^^^ expected one of `)`, `att_syntax`, or `raw`
6271

6372
error: expected one of `)`, `att_syntax`, or `raw`, found `nostack`
64-
--> $DIR/bad-options.rs:36:25
73+
--> $DIR/bad-options.rs:38:25
6574
|
6675
LL | global_asm!("", options(nostack));
6776
| ^^^^^^^ expected one of `)`, `att_syntax`, or `raw`
6877

6978
error: expected one of `)`, `att_syntax`, or `raw`, found `preserves_flags`
70-
--> $DIR/bad-options.rs:38:25
79+
--> $DIR/bad-options.rs:40:25
7180
|
7281
LL | global_asm!("", options(preserves_flags));
7382
| ^^^^^^^^^^^^^^^ expected one of `)`, `att_syntax`, or `raw`
@@ -80,5 +89,5 @@ LL | asm!("", clobber_abi("foo"));
8089
|
8190
= note: the following ABIs are supported on this target: `C`, `system`, `efiapi`, `win64`, `sysv64`
8291

83-
error: aborting due to 13 previous errors
92+
error: aborting due to 14 previous errors
8493

src/test/ui/asm/x86_64/parse-error.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ fn main() {
5050
//~^ ERROR clobber_abi is not allowed after options
5151
asm!("{}", options(), clobber_abi("C"), const foo);
5252
//~^ ERROR clobber_abi is not allowed after options
53-
asm!("", clobber_abi("C"), clobber_abi("C"));
54-
//~^ ERROR clobber_abi specified multiple times
5553
asm!("{a}", a = const foo, a = const bar);
5654
//~^ ERROR duplicate argument named `a`
5755
//~^^ ERROR argument never used
@@ -121,7 +119,7 @@ global_asm!("", options(), clobber_abi("C"));
121119
global_asm!("{}", options(), clobber_abi("C"), const FOO);
122120
//~^ ERROR clobber_abi is not allowed after options
123121
global_asm!("", clobber_abi("C"), clobber_abi("C"));
124-
//~^ ERROR clobber_abi specified multiple times
122+
//~^ ERROR `clobber_abi` cannot be used with `global_asm!`
125123
global_asm!("{a}", a = const FOO, a = const BAR);
126124
//~^ ERROR duplicate argument named `a`
127125
//~^^ ERROR argument never used

0 commit comments

Comments
 (0)