Skip to content

Commit 5ebb6a8

Browse files
authoredOct 9, 2021
Rollup merge of #89641 - asquared31415:asm-feature-attr-regs, r=oli-obk
make #[target_feature] work with `asm` register classes Fixes #89289
·
1.89.01.57.0
2 parents 9d14b65 + 271da7d commit 5ebb6a8

File tree

7 files changed

+213
-112
lines changed

7 files changed

+213
-112
lines changed
 

‎compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -202,39 +202,20 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
202202

203203
let mut used_input_regs = FxHashMap::default();
204204
let mut used_output_regs = FxHashMap::default();
205-
let mut required_features: Vec<&str> = vec![];
205+
206206
for (idx, &(ref op, op_sp)) in operands.iter().enumerate() {
207207
if let Some(reg) = op.reg() {
208-
// Make sure we don't accidentally carry features from the
209-
// previous iteration.
210-
required_features.clear();
211-
212208
let reg_class = reg.reg_class();
213209
if reg_class == asm::InlineAsmRegClass::Err {
214210
continue;
215211
}
216212

217-
// We ignore target feature requirements for clobbers: if the
218-
// feature is disabled then the compiler doesn't care what we
219-
// do with the registers.
220-
//
221-
// Note that this is only possible for explicit register
222-
// operands, which cannot be used in the asm string.
223-
let is_clobber = matches!(
224-
op,
225-
hir::InlineAsmOperand::Out {
226-
reg: asm::InlineAsmRegOrRegClass::Reg(_),
227-
late: _,
228-
expr: None
229-
}
230-
);
231-
232213
// Some register classes can only be used as clobbers. This
233214
// means that we disallow passing a value in/out of the asm and
234215
// require that the operand name an explicit register, not a
235216
// register class.
236217
if reg_class.is_clobber_only(asm_arch.unwrap())
237-
&& !(is_clobber && matches!(reg, asm::InlineAsmRegOrRegClass::Reg(_)))
218+
&& !(op.is_clobber() && matches!(reg, asm::InlineAsmRegOrRegClass::Reg(_)))
238219
{
239220
let msg = format!(
240221
"register class `{}` can only be used as a clobber, \
@@ -245,47 +226,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
245226
continue;
246227
}
247228

248-
if !is_clobber {
249-
// Validate register classes against currently enabled target
250-
// features. We check that at least one type is available for
251-
// the current target.
252-
for &(_, feature) in reg_class.supported_types(asm_arch.unwrap()) {
253-
if let Some(feature) = feature {
254-
if self.sess.target_features.contains(&Symbol::intern(feature)) {
255-
required_features.clear();
256-
break;
257-
} else {
258-
required_features.push(feature);
259-
}
260-
} else {
261-
required_features.clear();
262-
break;
263-
}
264-
}
265-
// We are sorting primitive strs here and can use unstable sort here
266-
required_features.sort_unstable();
267-
required_features.dedup();
268-
match &required_features[..] {
269-
[] => {}
270-
[feature] => {
271-
let msg = format!(
272-
"register class `{}` requires the `{}` target feature",
273-
reg_class.name(),
274-
feature
275-
);
276-
sess.struct_span_err(op_sp, &msg).emit();
277-
}
278-
features => {
279-
let msg = format!(
280-
"register class `{}` requires at least one target feature: {}",
281-
reg_class.name(),
282-
features.join(", ")
283-
);
284-
sess.struct_span_err(op_sp, &msg).emit();
285-
}
286-
}
287-
}
288-
289229
// Check for conflicts between explicit register operands.
290230
if let asm::InlineAsmRegOrRegClass::Reg(reg) = reg {
291231
let (input, output) = match op {

‎compiler/rustc_hir/src/hir.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,13 @@ impl<'hir> InlineAsmOperand<'hir> {
22932293
Self::Const { .. } | Self::Sym { .. } => None,
22942294
}
22952295
}
2296+
2297+
pub fn is_clobber(&self) -> bool {
2298+
matches!(
2299+
self,
2300+
InlineAsmOperand::Out { reg: InlineAsmRegOrRegClass::Reg(_), late: _, expr: None }
2301+
)
2302+
}
22962303
}
22972304

22982305
#[derive(Debug, HashStable_Generic)]

‎compiler/rustc_passes/src/intrinsicck.rs

Lines changed: 120 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl ExprVisitor<'tcx> {
141141
template: &[InlineAsmTemplatePiece],
142142
is_input: bool,
143143
tied_input: Option<(&hir::Expr<'tcx>, Option<InlineAsmType>)>,
144+
target_features: &[Symbol],
144145
) -> Option<InlineAsmType> {
145146
// Check the type against the allowed types for inline asm.
146147
let ty = self.typeck_results.expr_ty_adjusted(expr);
@@ -283,17 +284,20 @@ impl ExprVisitor<'tcx> {
283284
};
284285

285286
// Check whether the selected type requires a target feature. Note that
286-
// this is different from the feature check we did earlier in AST
287-
// lowering. While AST lowering checked that this register class is
288-
// usable at all with the currently enabled features, some types may
289-
// only be usable with a register class when a certain feature is
290-
// enabled. We check this here since it depends on the results of typeck.
287+
// this is different from the feature check we did earlier. While the
288+
// previous check checked that this register class is usable at all
289+
// with the currently enabled features, some types may only be usable
290+
// with a register class when a certain feature is enabled. We check
291+
// this here since it depends on the results of typeck.
291292
//
292293
// Also note that this check isn't run when the operand type is never
293-
// (!). In that case we still need the earlier check in AST lowering to
294-
// verify that the register class is usable at all.
294+
// (!). In that case we still need the earlier check to verify that the
295+
// register class is usable at all.
295296
if let Some(feature) = feature {
296-
if !self.tcx.sess.target_features.contains(&Symbol::intern(feature)) {
297+
let feat_sym = Symbol::intern(feature);
298+
if !self.tcx.sess.target_features.contains(&feat_sym)
299+
&& !target_features.contains(&feat_sym)
300+
{
297301
let msg = &format!("`{}` target feature is not enabled", feature);
298302
let mut err = self.tcx.sess.struct_span_err(expr.span, msg);
299303
err.note(&format!(
@@ -349,23 +353,122 @@ impl ExprVisitor<'tcx> {
349353
Some(asm_ty)
350354
}
351355

352-
fn check_asm(&self, asm: &hir::InlineAsm<'tcx>) {
353-
for (idx, (op, _)) in asm.operands.iter().enumerate() {
356+
fn check_asm(&self, asm: &hir::InlineAsm<'tcx>, hir_id: hir::HirId) {
357+
let hir = self.tcx.hir();
358+
let enclosing_id = hir.enclosing_body_owner(hir_id);
359+
let enclosing_def_id = hir.local_def_id(enclosing_id).to_def_id();
360+
let attrs = self.tcx.codegen_fn_attrs(enclosing_def_id);
361+
for (idx, (op, op_sp)) in asm.operands.iter().enumerate() {
362+
// Validate register classes against currently enabled target
363+
// features. We check that at least one type is available for
364+
// the enabled features.
365+
//
366+
// We ignore target feature requirements for clobbers: if the
367+
// feature is disabled then the compiler doesn't care what we
368+
// do with the registers.
369+
//
370+
// Note that this is only possible for explicit register
371+
// operands, which cannot be used in the asm string.
372+
if let Some(reg) = op.reg() {
373+
if !op.is_clobber() {
374+
let mut missing_required_features = vec![];
375+
let reg_class = reg.reg_class();
376+
for &(_, feature) in reg_class.supported_types(self.tcx.sess.asm_arch.unwrap())
377+
{
378+
match feature {
379+
Some(feature) => {
380+
let feat_sym = Symbol::intern(feature);
381+
if self.tcx.sess.target_features.contains(&feat_sym)
382+
|| attrs.target_features.contains(&feat_sym)
383+
{
384+
missing_required_features.clear();
385+
break;
386+
} else {
387+
missing_required_features.push(feature);
388+
}
389+
}
390+
None => {
391+
missing_required_features.clear();
392+
break;
393+
}
394+
}
395+
}
396+
397+
// We are sorting primitive strs here and can use unstable sort here
398+
missing_required_features.sort_unstable();
399+
missing_required_features.dedup();
400+
match &missing_required_features[..] {
401+
[] => {}
402+
[feature] => {
403+
let msg = format!(
404+
"register class `{}` requires the `{}` target feature",
405+
reg_class.name(),
406+
feature
407+
);
408+
self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
409+
// register isn't enabled, don't do more checks
410+
continue;
411+
}
412+
features => {
413+
let msg = format!(
414+
"register class `{}` requires at least one of the following target features: {}",
415+
reg_class.name(),
416+
features.join(", ")
417+
);
418+
self.tcx.sess.struct_span_err(*op_sp, &msg).emit();
419+
// register isn't enabled, don't do more checks
420+
continue;
421+
}
422+
}
423+
}
424+
}
425+
354426
match *op {
355427
hir::InlineAsmOperand::In { reg, ref expr } => {
356-
self.check_asm_operand_type(idx, reg, expr, asm.template, true, None);
428+
self.check_asm_operand_type(
429+
idx,
430+
reg,
431+
expr,
432+
asm.template,
433+
true,
434+
None,
435+
&attrs.target_features,
436+
);
357437
}
358438
hir::InlineAsmOperand::Out { reg, late: _, ref expr } => {
359439
if let Some(expr) = expr {
360-
self.check_asm_operand_type(idx, reg, expr, asm.template, false, None);
440+
self.check_asm_operand_type(
441+
idx,
442+
reg,
443+
expr,
444+
asm.template,
445+
false,
446+
None,
447+
&attrs.target_features,
448+
);
361449
}
362450
}
363451
hir::InlineAsmOperand::InOut { reg, late: _, ref expr } => {
364-
self.check_asm_operand_type(idx, reg, expr, asm.template, false, None);
452+
self.check_asm_operand_type(
453+
idx,
454+
reg,
455+
expr,
456+
asm.template,
457+
false,
458+
None,
459+
&attrs.target_features,
460+
);
365461
}
366462
hir::InlineAsmOperand::SplitInOut { reg, late: _, ref in_expr, ref out_expr } => {
367-
let in_ty =
368-
self.check_asm_operand_type(idx, reg, in_expr, asm.template, true, None);
463+
let in_ty = self.check_asm_operand_type(
464+
idx,
465+
reg,
466+
in_expr,
467+
asm.template,
468+
true,
469+
None,
470+
&attrs.target_features,
471+
);
369472
if let Some(out_expr) = out_expr {
370473
self.check_asm_operand_type(
371474
idx,
@@ -374,6 +477,7 @@ impl ExprVisitor<'tcx> {
374477
asm.template,
375478
false,
376479
Some((in_expr, in_ty)),
480+
&attrs.target_features,
377481
);
378482
}
379483
}
@@ -422,7 +526,7 @@ impl Visitor<'tcx> for ExprVisitor<'tcx> {
422526
}
423527
}
424528

425-
hir::ExprKind::InlineAsm(asm) => self.check_asm(asm),
529+
hir::ExprKind::InlineAsm(asm) => self.check_asm(asm, expr.hir_id),
426530

427531
_ => {}
428532
}

‎src/test/ui/asm/x86_64/bad-reg.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ fn main() {
2121
//~^ ERROR asm template modifiers are not allowed for `const` arguments
2222
asm!("{:a}", sym main);
2323
//~^ ERROR asm template modifiers are not allowed for `sym` arguments
24-
asm!("{}", in(zmm_reg) foo);
25-
//~^ ERROR register class `zmm_reg` requires the `avx512f` target feature
26-
asm!("", in("zmm0") foo);
27-
//~^ ERROR register class `zmm_reg` requires the `avx512f` target feature
2824
asm!("", in("ebp") foo);
2925
//~^ ERROR invalid register `ebp`: the frame pointer cannot be used as an operand
3026
asm!("", in("rsp") foo);

‎src/test/ui/asm/x86_64/bad-reg.stderr

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,127 +46,115 @@ LL | asm!("{:a}", sym main);
4646
| |
4747
| template modifier
4848

49-
error: register class `zmm_reg` requires the `avx512f` target feature
50-
--> $DIR/bad-reg.rs:24:20
51-
|
52-
LL | asm!("{}", in(zmm_reg) foo);
53-
| ^^^^^^^^^^^^^^^
54-
55-
error: register class `zmm_reg` requires the `avx512f` target feature
56-
--> $DIR/bad-reg.rs:26:18
57-
|
58-
LL | asm!("", in("zmm0") foo);
59-
| ^^^^^^^^^^^^^^
60-
6149
error: invalid register `ebp`: the frame pointer cannot be used as an operand for inline asm
62-
--> $DIR/bad-reg.rs:28:18
50+
--> $DIR/bad-reg.rs:24:18
6351
|
6452
LL | asm!("", in("ebp") foo);
6553
| ^^^^^^^^^^^^^
6654

6755
error: invalid register `rsp`: the stack pointer cannot be used as an operand for inline asm
68-
--> $DIR/bad-reg.rs:30:18
56+
--> $DIR/bad-reg.rs:26:18
6957
|
7058
LL | asm!("", in("rsp") foo);
7159
| ^^^^^^^^^^^^^
7260

7361
error: invalid register `ip`: the instruction pointer cannot be used as an operand for inline asm
74-
--> $DIR/bad-reg.rs:32:18
62+
--> $DIR/bad-reg.rs:28:18
7563
|
7664
LL | asm!("", in("ip") foo);
7765
| ^^^^^^^^^^^^
7866

7967
error: invalid register `k0`: the k0 AVX mask register cannot be used as an operand for inline asm
80-
--> $DIR/bad-reg.rs:34:18
68+
--> $DIR/bad-reg.rs:30:18
8169
|
8270
LL | asm!("", in("k0") foo);
8371
| ^^^^^^^^^^^^
8472

8573
error: invalid register `ah`: high byte registers cannot be used as an operand on x86_64
86-
--> $DIR/bad-reg.rs:36:18
74+
--> $DIR/bad-reg.rs:32:18
8775
|
8876
LL | asm!("", in("ah") foo);
8977
| ^^^^^^^^^^^^
9078

9179
error: register class `x87_reg` can only be used as a clobber, not as an input or output
92-
--> $DIR/bad-reg.rs:39:18
80+
--> $DIR/bad-reg.rs:35:18
9381
|
9482
LL | asm!("", in("st(2)") foo);
9583
| ^^^^^^^^^^^^^^^
9684

9785
error: register class `mmx_reg` can only be used as a clobber, not as an input or output
98-
--> $DIR/bad-reg.rs:41:18
86+
--> $DIR/bad-reg.rs:37:18
9987
|
10088
LL | asm!("", in("mm0") foo);
10189
| ^^^^^^^^^^^^^
10290

10391
error: register class `x87_reg` can only be used as a clobber, not as an input or output
104-
--> $DIR/bad-reg.rs:45:20
92+
--> $DIR/bad-reg.rs:41:20
10593
|
10694
LL | asm!("{}", in(x87_reg) foo);
10795
| ^^^^^^^^^^^^^^^
10896

10997
error: register class `mmx_reg` can only be used as a clobber, not as an input or output
110-
--> $DIR/bad-reg.rs:47:20
98+
--> $DIR/bad-reg.rs:43:20
11199
|
112100
LL | asm!("{}", in(mmx_reg) foo);
113101
| ^^^^^^^^^^^^^^^
114102

115103
error: register class `x87_reg` can only be used as a clobber, not as an input or output
116-
--> $DIR/bad-reg.rs:49:20
104+
--> $DIR/bad-reg.rs:45:20
117105
|
118106
LL | asm!("{}", out(x87_reg) _);
119107
| ^^^^^^^^^^^^^^
120108

121109
error: register class `mmx_reg` can only be used as a clobber, not as an input or output
122-
--> $DIR/bad-reg.rs:51:20
110+
--> $DIR/bad-reg.rs:47:20
123111
|
124112
LL | asm!("{}", out(mmx_reg) _);
125113
| ^^^^^^^^^^^^^^
126114

127115
error: register `al` conflicts with register `ax`
128-
--> $DIR/bad-reg.rs:57:33
116+
--> $DIR/bad-reg.rs:53:33
129117
|
130118
LL | asm!("", in("eax") foo, in("al") bar);
131119
| ------------- ^^^^^^^^^^^^ register `al`
132120
| |
133121
| register `ax`
134122

135123
error: register `ax` conflicts with register `ax`
136-
--> $DIR/bad-reg.rs:59:33
124+
--> $DIR/bad-reg.rs:55:33
137125
|
138126
LL | asm!("", in("rax") foo, out("rax") bar);
139127
| ------------- ^^^^^^^^^^^^^^ register `ax`
140128
| |
141129
| register `ax`
142130
|
143131
help: use `lateout` instead of `out` to avoid conflict
144-
--> $DIR/bad-reg.rs:59:18
132+
--> $DIR/bad-reg.rs:55:18
145133
|
146134
LL | asm!("", in("rax") foo, out("rax") bar);
147135
| ^^^^^^^^^^^^^
148136

149137
error: register `ymm0` conflicts with register `xmm0`
150-
--> $DIR/bad-reg.rs:62:34
138+
--> $DIR/bad-reg.rs:58:34
151139
|
152140
LL | asm!("", in("xmm0") foo, in("ymm0") bar);
153141
| -------------- ^^^^^^^^^^^^^^ register `ymm0`
154142
| |
155143
| register `xmm0`
156144

157145
error: register `ymm0` conflicts with register `xmm0`
158-
--> $DIR/bad-reg.rs:64:34
146+
--> $DIR/bad-reg.rs:60:34
159147
|
160148
LL | asm!("", in("xmm0") foo, out("ymm0") bar);
161149
| -------------- ^^^^^^^^^^^^^^^ register `ymm0`
162150
| |
163151
| register `xmm0`
164152
|
165153
help: use `lateout` instead of `out` to avoid conflict
166-
--> $DIR/bad-reg.rs:64:18
154+
--> $DIR/bad-reg.rs:60:18
167155
|
168156
LL | asm!("", in("xmm0") foo, out("ymm0") bar);
169157
| ^^^^^^^^^^^^^^
170158

171-
error: aborting due to 23 previous errors
159+
error: aborting due to 21 previous errors
172160

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// only-x86_64
2+
3+
#![feature(asm, avx512_target_feature)]
4+
5+
#[target_feature(enable = "avx")]
6+
unsafe fn foo() {
7+
let mut x = 1;
8+
let y = 2;
9+
asm!("vaddps {2:y}, {0:y}, {1:y}", in(ymm_reg) x, in(ymm_reg) y, lateout(ymm_reg) x);
10+
assert_eq!(x, 3);
11+
}
12+
13+
unsafe fn bar() {
14+
let mut x = 1;
15+
let y = 2;
16+
asm!("vaddps {2:y}, {0:y}, {1:y}", in(ymm_reg) x, in(ymm_reg) y, lateout(ymm_reg) x);
17+
//~^ ERROR: register class `ymm_reg` requires the `avx` target feature
18+
//~| ERROR: register class `ymm_reg` requires the `avx` target feature
19+
//~| ERROR: register class `ymm_reg` requires the `avx` target feature
20+
assert_eq!(x, 3);
21+
}
22+
23+
#[target_feature(enable = "avx512bw")]
24+
unsafe fn baz() {
25+
let x = 1;
26+
asm!("/* {0} */", in(kreg) x);
27+
}
28+
29+
unsafe fn baz2() {
30+
let x = 1;
31+
asm!("/* {0} */", in(kreg) x);
32+
//~^ ERROR: register class `kreg` requires at least one of the following target features: avx512bw, avx512f
33+
}
34+
35+
fn main() {
36+
unsafe {
37+
foo();
38+
bar();
39+
}
40+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: register class `ymm_reg` requires the `avx` target feature
2+
--> $DIR/target-feature-attr.rs:16:40
3+
|
4+
LL | asm!("vaddps {2:y}, {0:y}, {1:y}", in(ymm_reg) x, in(ymm_reg) y, lateout(ymm_reg) x);
5+
| ^^^^^^^^^^^^^
6+
7+
error: register class `ymm_reg` requires the `avx` target feature
8+
--> $DIR/target-feature-attr.rs:16:55
9+
|
10+
LL | asm!("vaddps {2:y}, {0:y}, {1:y}", in(ymm_reg) x, in(ymm_reg) y, lateout(ymm_reg) x);
11+
| ^^^^^^^^^^^^^
12+
13+
error: register class `ymm_reg` requires the `avx` target feature
14+
--> $DIR/target-feature-attr.rs:16:70
15+
|
16+
LL | asm!("vaddps {2:y}, {0:y}, {1:y}", in(ymm_reg) x, in(ymm_reg) y, lateout(ymm_reg) x);
17+
| ^^^^^^^^^^^^^^^^^^
18+
19+
error: register class `kreg` requires at least one of the following target features: avx512bw, avx512f
20+
--> $DIR/target-feature-attr.rs:31:23
21+
|
22+
LL | asm!("/* {0} */", in(kreg) x);
23+
| ^^^^^^^^^^
24+
25+
error: aborting due to 4 previous errors
26+

0 commit comments

Comments
 (0)
Please sign in to comment.