Skip to content

Commit 2b4d881

Browse files
committed
Add test to ensure implied target features work with asm, and fix failing tests
1 parent 9710acb commit 2b4d881

File tree

6 files changed

+45
-50
lines changed

6 files changed

+45
-50
lines changed

compiler/rustc_codegen_ssa/src/target_features.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::errors;
22
use rustc_ast::ast;
33
use rustc_attr::InstructionSetAttr;
44
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
5-
use rustc_data_structures::unord::{UnordMap, UnordSet};
5+
use rustc_data_structures::unord::{ExtendUnord, UnordMap, UnordSet};
66
use rustc_errors::Applicability;
77
use rustc_hir::def::DefKind;
88
use rustc_hir::def_id::DefId;
@@ -100,12 +100,14 @@ pub fn from_target_feature(
100100
}));
101101
}
102102

103-
// Add implied features
103+
// Add both explicit and implied target features, using a set to deduplicate
104+
let mut target_features_set = UnordSet::new();
104105
for feature in added_target_features.iter() {
105-
target_features
106-
.extend(tcx.implied_target_features(*feature).clone().into_sorted_stable_ord());
106+
target_features_set
107+
.extend_unord(tcx.implied_target_features(*feature).clone().into_items());
107108
}
108-
target_features.extend(added_target_features)
109+
target_features_set.extend(added_target_features);
110+
target_features.extend(target_features_set.into_sorted_stable_ord())
109111
}
110112

111113
/// Computes the set of target features used in a function for the purposes of

tests/ui/consts/const-eval/const_fn_target_feature.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/const_fn_target_feature.rs:11:24
33
|
44
LL | const B: () = unsafe { avx2_fn() };
5-
| ^^^^^^^^^ calling a function that requires unavailable target features: avx2
5+
| ^^^^^^^^^ calling a function that requires unavailable target features: avx, avx2, sse4.1, sse4.2
66

77
error: aborting due to 1 previous error
88

tests/ui/rfcs/rfc-2396-target_feature-11/safe-calls.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn foo() {
3434

3535
#[target_feature(enable = "sse2")]
3636
fn bar() {
37+
sse2();
3738
avx_bmi2();
3839
//~^ ERROR call to function `avx_bmi2` with `#[target_feature]` is unsafe
3940
Quux.avx_bmi2();
@@ -43,7 +44,6 @@ fn bar() {
4344
#[target_feature(enable = "avx")]
4445
fn baz() {
4546
sse2();
46-
//~^ ERROR call to function `sse2` with `#[target_feature]` is unsafe
4747
avx_bmi2();
4848
//~^ ERROR call to function `avx_bmi2` with `#[target_feature]` is unsafe
4949
Quux.avx_bmi2();
@@ -54,7 +54,8 @@ fn baz() {
5454
#[target_feature(enable = "bmi2")]
5555
fn qux() {
5656
sse2();
57-
//~^ ERROR call to function `sse2` with `#[target_feature]` is unsafe
57+
avx_bmi2();
58+
Quux.avx_bmi2();
5859
}
5960

6061
const _: () = sse2();
@@ -64,11 +65,9 @@ const _: () = sse2_and_fxsr();
6465
//~^ ERROR call to function `sse2_and_fxsr` with `#[target_feature]` is unsafe
6566

6667
#[deny(unsafe_op_in_unsafe_fn)]
67-
#[target_feature(enable = "avx")]
68-
#[target_feature(enable = "bmi2")]
6968
unsafe fn needs_unsafe_block() {
7069
sse2();
71-
//~^ ERROR call to function `sse2` with `#[target_feature]` is unsafe
70+
//~^ call to function `sse2` with `#[target_feature]` is unsafe and requires unsafe block
7271
}
7372

7473
fn main() {}

tests/ui/rfcs/rfc-2396-target_feature-11/safe-calls.stderr

+22-38
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,42 @@ error[E0133]: call to function `sse2` with `#[target_feature]` is unsafe and req
44
LL | sse2();
55
| ^^^^^^ call to function with `#[target_feature]`
66
|
7-
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
8-
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
7+
= help: in order for the call to be safe, the context requires the following additional target features: sse and sse2
8+
= note: the sse and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
99

1010
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
1111
--> $DIR/safe-calls.rs:29:5
1212
|
1313
LL | avx_bmi2();
1414
| ^^^^^^^^^^ call to function with `#[target_feature]`
1515
|
16-
= help: in order for the call to be safe, the context requires the following additional target features: avx and bmi2
16+
= help: in order for the call to be safe, the context requires the following additional target features: avx, sse, sse2, sse3, sse4.1, sse4.2, ssse3, and bmi2
17+
= note: the sse and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
1718

1819
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
1920
--> $DIR/safe-calls.rs:31:5
2021
|
2122
LL | Quux.avx_bmi2();
2223
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
2324
|
24-
= help: in order for the call to be safe, the context requires the following additional target features: avx and bmi2
25+
= help: in order for the call to be safe, the context requires the following additional target features: avx, sse, sse2, sse3, sse4.1, sse4.2, ssse3, and bmi2
26+
= note: the sse and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
2527

2628
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
27-
--> $DIR/safe-calls.rs:37:5
29+
--> $DIR/safe-calls.rs:38:5
2830
|
2931
LL | avx_bmi2();
3032
| ^^^^^^^^^^ call to function with `#[target_feature]`
3133
|
32-
= help: in order for the call to be safe, the context requires the following additional target features: avx and bmi2
34+
= help: in order for the call to be safe, the context requires the following additional target features: avx, sse3, sse4.1, sse4.2, ssse3, and bmi2
3335

3436
error[E0133]: call to function `Quux::avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
35-
--> $DIR/safe-calls.rs:39:5
37+
--> $DIR/safe-calls.rs:40:5
3638
|
3739
LL | Quux.avx_bmi2();
3840
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
3941
|
40-
= help: in order for the call to be safe, the context requires the following additional target features: avx and bmi2
41-
42-
error[E0133]: call to function `sse2` with `#[target_feature]` is unsafe and requires unsafe function or block
43-
--> $DIR/safe-calls.rs:45:5
44-
|
45-
LL | sse2();
46-
| ^^^^^^ call to function with `#[target_feature]`
47-
|
48-
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
49-
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
42+
= help: in order for the call to be safe, the context requires the following additional target features: avx, sse3, sse4.1, sse4.2, ssse3, and bmi2
5043

5144
error[E0133]: call to function `avx_bmi2` with `#[target_feature]` is unsafe and requires unsafe function or block
5245
--> $DIR/safe-calls.rs:47:5
@@ -65,52 +58,43 @@ LL | Quux.avx_bmi2();
6558
= help: in order for the call to be safe, the context requires the following additional target feature: bmi2
6659

6760
error[E0133]: call to function `sse2` with `#[target_feature]` is unsafe and requires unsafe function or block
68-
--> $DIR/safe-calls.rs:56:5
69-
|
70-
LL | sse2();
71-
| ^^^^^^ call to function with `#[target_feature]`
72-
|
73-
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
74-
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
75-
76-
error[E0133]: call to function `sse2` with `#[target_feature]` is unsafe and requires unsafe function or block
77-
--> $DIR/safe-calls.rs:60:15
61+
--> $DIR/safe-calls.rs:61:15
7862
|
7963
LL | const _: () = sse2();
8064
| ^^^^^^ call to function with `#[target_feature]`
8165
|
82-
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
83-
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
66+
= help: in order for the call to be safe, the context requires the following additional target features: sse and sse2
67+
= note: the sse and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
8468

8569
error[E0133]: call to function `sse2_and_fxsr` with `#[target_feature]` is unsafe and requires unsafe function or block
86-
--> $DIR/safe-calls.rs:63:15
70+
--> $DIR/safe-calls.rs:64:15
8771
|
8872
LL | const _: () = sse2_and_fxsr();
8973
| ^^^^^^^^^^^^^^^ call to function with `#[target_feature]`
9074
|
91-
= help: in order for the call to be safe, the context requires the following additional target features: sse2 and fxsr
92-
= note: the fxsr and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
75+
= help: in order for the call to be safe, the context requires the following additional target features: sse, sse2, and fxsr
76+
= note: the fxsr, sse, and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
9377

9478
error[E0133]: call to function `sse2` with `#[target_feature]` is unsafe and requires unsafe block
95-
--> $DIR/safe-calls.rs:70:5
79+
--> $DIR/safe-calls.rs:69:5
9680
|
9781
LL | sse2();
9882
| ^^^^^^ call to function with `#[target_feature]`
9983
|
10084
= note: for more information, see issue #71668 <https://github.com/rust-lang/rust/issues/71668>
101-
= help: in order for the call to be safe, the context requires the following additional target feature: sse2
102-
= note: the sse2 target feature being enabled in the build configuration does not remove the requirement to list it in `#[target_feature]`
85+
= help: in order for the call to be safe, the context requires the following additional target features: sse and sse2
86+
= note: the sse and sse2 target features being enabled in the build configuration does not remove the requirement to list them in `#[target_feature]`
10387
note: an unsafe function restricts its caller, but its body is safe by default
104-
--> $DIR/safe-calls.rs:69:1
88+
--> $DIR/safe-calls.rs:68:1
10589
|
10690
LL | unsafe fn needs_unsafe_block() {
10791
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10892
note: the lint level is defined here
109-
--> $DIR/safe-calls.rs:66:8
93+
--> $DIR/safe-calls.rs:67:8
11094
|
11195
LL | #[deny(unsafe_op_in_unsafe_fn)]
11296
| ^^^^^^^^^^^^^^^^^^^^^^
11397

114-
error: aborting due to 12 previous errors
98+
error: aborting due to 10 previous errors
11599

116100
For more information about this error, try `rustc --explain E0133`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//@ only-x86_64
2+
//@ build-pass
3+
#![allow(dead_code)]
4+
5+
#[target_feature(enable = "avx2")]
6+
unsafe fn demo(v: std::arch::x86_64::__m256i) {
7+
std::arch::asm!("/* {v} */", v = in(ymm_reg) v);
8+
}
9+
10+
fn main() {}

tests/ui/target-feature/implied-features.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@ only-x86_64
2-
//@ run-pass
2+
//@ build-pass
33
#![feature(target_feature_11)]
44
#![allow(dead_code)]
55

0 commit comments

Comments
 (0)