Skip to content

Commit 1d12831

Browse files
authored
Rollup merge of rust-lang#137225 - RalfJung:vectorcall, r=nnethercote
vectorcall ABI: require SSE2 According to the official docs at https://learn.microsoft.com/en-us/cpp/cpp/vectorcall, SSE2 is required for this ABI. Add a check that enforces this. I put this together with the other checks ensuring the target features required for a function are present... however, since the ABI is known pre-monomorphization, it would be possible to do this check earlier, which would have the advantage of checking even in `cargo check`. It would have the disadvantage of spreading this code in yet more places. The first commit just does a little refactoring of the mono-time ABI check to make it easier to add the new check. Cc `@workingjubilee`
2 parents a3b1e03 + cd9b5c1 commit 1d12831

15 files changed

+193
-83
lines changed

compiler/rustc_monomorphize/messages.ftl

+35-13
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
1-
monomorphize_abi_error_disabled_vector_type_call =
2-
this function call uses SIMD vector type `{$ty}` which (with the chosen ABI) requires the `{$required_feature}` target feature, which is not enabled in the caller
3-
.label = function called here
4-
.help = consider enabling it globally (`-C target-feature=+{$required_feature}`) or locally (`#[target_feature(enable="{$required_feature}")]`)
5-
monomorphize_abi_error_disabled_vector_type_def =
6-
this function definition uses SIMD vector type `{$ty}` which (with the chosen ABI) requires the `{$required_feature}` target feature, which is not enabled
7-
.label = function defined here
1+
monomorphize_abi_error_disabled_vector_type =
2+
this function {$is_call ->
3+
[true] call
4+
*[false] definition
5+
} uses SIMD vector type `{$ty}` which (with the chosen ABI) requires the `{$required_feature}` target feature, which is not enabled{$is_call ->
6+
[true] {" "}in the caller
7+
*[false] {""}
8+
}
9+
.label = function {$is_call ->
10+
[true] called
11+
*[false] defined
12+
} here
813
.help = consider enabling it globally (`-C target-feature=+{$required_feature}`) or locally (`#[target_feature(enable="{$required_feature}")]`)
914
10-
monomorphize_abi_error_unsupported_vector_type_call =
11-
this function call uses SIMD vector type `{$ty}` which is not currently supported with the chosen ABI
12-
.label = function called here
13-
monomorphize_abi_error_unsupported_vector_type_def =
14-
this function definition uses SIMD vector type `{$ty}` which is not currently supported with the chosen ABI
15-
.label = function defined here
15+
monomorphize_abi_error_unsupported_vector_type =
16+
this function {$is_call ->
17+
[true] call
18+
*[false] definition
19+
} uses SIMD vector type `{$ty}` which is not currently supported with the chosen ABI
20+
.label = function {$is_call ->
21+
[true] called
22+
*[false] defined
23+
} here
24+
25+
monomorphize_abi_required_target_feature =
26+
this function {$is_call ->
27+
[true] call
28+
*[false] definition
29+
} uses ABI "{$abi}" which requires the `{$required_feature}` target feature, which is not enabled{$is_call ->
30+
[true] {" "}in the caller
31+
*[false] {""}
32+
}
33+
.label = function {$is_call ->
34+
[true] called
35+
*[false] defined
36+
} here
37+
.help = consider enabling it globally (`-C target-feature=+{$required_feature}`) or locally (`#[target_feature(enable="{$required_feature}")]`)
1638
1739
monomorphize_couldnt_dump_mono_stats =
1840
unexpected error occurred while dumping monomorphization stats: {$error}

compiler/rustc_monomorphize/src/errors.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,36 @@ pub(crate) struct UnknownCguCollectionMode<'a> {
7070
}
7171

7272
#[derive(LintDiagnostic)]
73-
#[diag(monomorphize_abi_error_disabled_vector_type_def)]
73+
#[diag(monomorphize_abi_error_disabled_vector_type)]
7474
#[help]
75-
pub(crate) struct AbiErrorDisabledVectorTypeDef<'a> {
75+
pub(crate) struct AbiErrorDisabledVectorType<'a> {
7676
#[label]
7777
pub span: Span,
7878
pub required_feature: &'a str,
7979
pub ty: Ty<'a>,
80+
/// Whether this is a problem at a call site or at a declaration.
81+
pub is_call: bool,
8082
}
8183

8284
#[derive(LintDiagnostic)]
83-
#[diag(monomorphize_abi_error_disabled_vector_type_call)]
84-
#[help]
85-
pub(crate) struct AbiErrorDisabledVectorTypeCall<'a> {
86-
#[label]
87-
pub span: Span,
88-
pub required_feature: &'a str,
89-
pub ty: Ty<'a>,
90-
}
91-
92-
#[derive(LintDiagnostic)]
93-
#[diag(monomorphize_abi_error_unsupported_vector_type_def)]
94-
pub(crate) struct AbiErrorUnsupportedVectorTypeDef<'a> {
85+
#[diag(monomorphize_abi_error_unsupported_vector_type)]
86+
pub(crate) struct AbiErrorUnsupportedVectorType<'a> {
9587
#[label]
9688
pub span: Span,
9789
pub ty: Ty<'a>,
90+
/// Whether this is a problem at a call site or at a declaration.
91+
pub is_call: bool,
9892
}
9993

100-
#[derive(LintDiagnostic)]
101-
#[diag(monomorphize_abi_error_unsupported_vector_type_call)]
102-
pub(crate) struct AbiErrorUnsupportedVectorTypeCall<'a> {
94+
#[derive(Diagnostic)]
95+
#[diag(monomorphize_abi_required_target_feature)]
96+
#[help]
97+
pub(crate) struct AbiRequiredTargetFeature<'a> {
98+
#[primary_span]
10399
#[label]
104100
pub span: Span,
105-
pub ty: Ty<'a>,
101+
pub required_feature: &'a str,
102+
pub abi: &'a str,
103+
/// Whether this is a problem at a call site or at a declaration.
104+
pub is_call: bool,
106105
}

compiler/rustc_monomorphize/src/mono_checks/abi_check.rs

+55-52
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@ use rustc_middle::ty::inherent::*;
77
use rustc_middle::ty::{self, Instance, InstanceKind, Ty, TyCtxt};
88
use rustc_session::lint::builtin::ABI_UNSUPPORTED_VECTOR_TYPES;
99
use rustc_span::def_id::DefId;
10-
use rustc_span::{DUMMY_SP, Span, Symbol};
11-
use rustc_target::callconv::{FnAbi, PassMode};
10+
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
11+
use rustc_target::callconv::{Conv, FnAbi, PassMode};
1212

13-
use crate::errors::{
14-
AbiErrorDisabledVectorTypeCall, AbiErrorDisabledVectorTypeDef,
15-
AbiErrorUnsupportedVectorTypeCall, AbiErrorUnsupportedVectorTypeDef,
16-
};
13+
use crate::errors;
1714

1815
fn uses_vector_registers(mode: &PassMode, repr: &BackendRepr) -> bool {
1916
match mode {
@@ -28,35 +25,68 @@ fn uses_vector_registers(mode: &PassMode, repr: &BackendRepr) -> bool {
2825

2926
/// Checks whether a certain function ABI is compatible with the target features currently enabled
3027
/// for a certain function.
31-
/// If not, `emit_err` is called, with `Some(feature)` if a certain feature should be enabled and
32-
/// with `None` if no feature is known that would make the ABI compatible.
28+
/// `is_call` indicates whether this is a call-site check or a definition-site check;
29+
/// this is only relevant for the wording in the emitted error.
3330
fn do_check_abi<'tcx>(
3431
tcx: TyCtxt<'tcx>,
3532
abi: &FnAbi<'tcx, Ty<'tcx>>,
36-
target_feature_def: DefId,
37-
mut emit_err: impl FnMut(Ty<'tcx>, Option<&'static str>),
33+
def_id: DefId,
34+
is_call: bool,
35+
span: impl Fn() -> Span,
3836
) {
3937
let feature_def = tcx.sess.target.features_for_correct_vector_abi();
40-
let codegen_attrs = tcx.codegen_fn_attrs(target_feature_def);
38+
let codegen_attrs = tcx.codegen_fn_attrs(def_id);
39+
let have_feature = |feat: Symbol| {
40+
tcx.sess.unstable_target_features.contains(&feat)
41+
|| codegen_attrs.target_features.iter().any(|x| x.name == feat)
42+
};
4143
for arg_abi in abi.args.iter().chain(std::iter::once(&abi.ret)) {
4244
let size = arg_abi.layout.size;
4345
if uses_vector_registers(&arg_abi.mode, &arg_abi.layout.backend_repr) {
4446
// Find the first feature that provides at least this vector size.
4547
let feature = match feature_def.iter().find(|(bits, _)| size.bits() <= *bits) {
4648
Some((_, feature)) => feature,
4749
None => {
48-
emit_err(arg_abi.layout.ty, None);
50+
let span = span();
51+
tcx.emit_node_span_lint(
52+
ABI_UNSUPPORTED_VECTOR_TYPES,
53+
CRATE_HIR_ID,
54+
span,
55+
errors::AbiErrorUnsupportedVectorType {
56+
span,
57+
ty: arg_abi.layout.ty,
58+
is_call,
59+
},
60+
);
4961
continue;
5062
}
5163
};
52-
let feature_sym = Symbol::intern(feature);
53-
if !tcx.sess.unstable_target_features.contains(&feature_sym)
54-
&& !codegen_attrs.target_features.iter().any(|x| x.name == feature_sym)
55-
{
56-
emit_err(arg_abi.layout.ty, Some(&feature));
64+
if !have_feature(Symbol::intern(feature)) {
65+
// Emit error.
66+
let span = span();
67+
tcx.emit_node_span_lint(
68+
ABI_UNSUPPORTED_VECTOR_TYPES,
69+
CRATE_HIR_ID,
70+
span,
71+
errors::AbiErrorDisabledVectorType {
72+
span,
73+
required_feature: feature,
74+
ty: arg_abi.layout.ty,
75+
is_call,
76+
},
77+
);
5778
}
5879
}
5980
}
81+
// The `vectorcall` ABI is special in that it requires SSE2 no matter which types are being passed.
82+
if abi.conv == Conv::X86VectorCall && !have_feature(sym::sse2) {
83+
tcx.dcx().emit_err(errors::AbiRequiredTargetFeature {
84+
span: span(),
85+
required_feature: "sse2",
86+
abi: "vectorcall",
87+
is_call,
88+
});
89+
}
6090
}
6191

6292
/// Checks that the ABI of a given instance of a function does not contain vector-passed arguments
@@ -69,24 +99,13 @@ fn check_instance_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) {
6999
// function.
70100
return;
71101
};
72-
do_check_abi(tcx, abi, instance.def_id(), |ty, required_feature| {
73-
let span = tcx.def_span(instance.def_id());
74-
if let Some(required_feature) = required_feature {
75-
tcx.emit_node_span_lint(
76-
ABI_UNSUPPORTED_VECTOR_TYPES,
77-
CRATE_HIR_ID,
78-
span,
79-
AbiErrorDisabledVectorTypeDef { span, required_feature, ty },
80-
);
81-
} else {
82-
tcx.emit_node_span_lint(
83-
ABI_UNSUPPORTED_VECTOR_TYPES,
84-
CRATE_HIR_ID,
85-
span,
86-
AbiErrorUnsupportedVectorTypeDef { span, ty },
87-
);
88-
}
89-
})
102+
do_check_abi(
103+
tcx,
104+
abi,
105+
instance.def_id(),
106+
/*is_call*/ false,
107+
|| tcx.def_span(instance.def_id()),
108+
)
90109
}
91110

92111
/// Checks that a call expression does not try to pass a vector-passed argument which requires a
@@ -123,23 +142,7 @@ fn check_call_site_abi<'tcx>(
123142
// ABI failed to compute; this will not get through codegen.
124143
return;
125144
};
126-
do_check_abi(tcx, callee_abi, caller.def_id(), |ty, required_feature| {
127-
if let Some(required_feature) = required_feature {
128-
tcx.emit_node_span_lint(
129-
ABI_UNSUPPORTED_VECTOR_TYPES,
130-
CRATE_HIR_ID,
131-
span,
132-
AbiErrorDisabledVectorTypeCall { span, required_feature, ty },
133-
);
134-
} else {
135-
tcx.emit_node_span_lint(
136-
ABI_UNSUPPORTED_VECTOR_TYPES,
137-
CRATE_HIR_ID,
138-
span,
139-
AbiErrorUnsupportedVectorTypeCall { span, ty },
140-
);
141-
}
142-
});
145+
do_check_abi(tcx, callee_abi, caller.def_id(), /*is_call*/ true, || span);
143146
}
144147

145148
fn check_callees_abi<'tcx>(tcx: TyCtxt<'tcx>, instance: Instance<'tcx>, body: &mir::Body<'tcx>) {
File renamed without changes.
File renamed without changes.

tests/ui/abi/sse-abi-checks.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Ensure we trigger abi_unsupported_vector_types for target features that are usually enabled
2+
//! on a target, but disabled in this file via a `-C` flag.
3+
//@ add-core-stubs
4+
//@ compile-flags: --crate-type=rlib --target=i586-unknown-linux-gnu -C target-feature=-sse,-sse2
5+
//@ build-pass
6+
//@ ignore-pass (test emits codegen-time warnings)
7+
//@ needs-llvm-components: x86
8+
#![feature(no_core, repr_simd)]
9+
#![no_core]
10+
#![allow(improper_ctypes_definitions)]
11+
12+
extern crate minicore;
13+
use minicore::*;
14+
15+
#[repr(simd)]
16+
pub struct SseVector([i64; 2]);
17+
18+
#[no_mangle]
19+
pub unsafe extern "C" fn f(_: SseVector) {
20+
//~^ this function definition uses SIMD vector type `SseVector` which (with the chosen ABI) requires the `sse` target feature, which is not enabled
21+
//~| WARNING this was previously accepted by the compiler
22+
}

tests/ui/abi/sse-abi-checks.stderr

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
warning: this function definition uses SIMD vector type `SseVector` which (with the chosen ABI) requires the `sse` target feature, which is not enabled
2+
--> $DIR/sse-abi-checks.rs:19:1
3+
|
4+
LL | pub unsafe extern "C" fn f(_: SseVector) {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
6+
|
7+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
8+
= note: for more information, see issue #116558 <https://github.com/rust-lang/rust/issues/116558>
9+
= help: consider enabling it globally (`-C target-feature=+sse`) or locally (`#[target_feature(enable="sse")]`)
10+
= note: `#[warn(abi_unsupported_vector_types)]` on by default
11+
12+
warning: 1 warning emitted
13+
14+
Future incompatibility report: Future breakage diagnostic:
15+
warning: this function definition uses SIMD vector type `SseVector` which (with the chosen ABI) requires the `sse` target feature, which is not enabled
16+
--> $DIR/sse-abi-checks.rs:19:1
17+
|
18+
LL | pub unsafe extern "C" fn f(_: SseVector) {
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
20+
|
21+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
22+
= note: for more information, see issue #116558 <https://github.com/rust-lang/rust/issues/116558>
23+
= help: consider enabling it globally (`-C target-feature=+sse`) or locally (`#[target_feature(enable="sse")]`)
24+
= note: `#[warn(abi_unsupported_vector_types)]` on by default
25+

tests/ui/abi/vectorcall-abi-checks.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ add-core-stubs
2+
//@ compile-flags: --crate-type=rlib --target=i586-unknown-linux-gnu -C target-feature=-sse,-sse2
3+
//@ build-fail
4+
//@ ignore-pass (test emits codegen-time errors)
5+
//@ needs-llvm-components: x86
6+
#![feature(no_core, abi_vectorcall)]
7+
#![no_core]
8+
9+
extern crate minicore;
10+
use minicore::*;
11+
12+
#[no_mangle]
13+
pub extern "vectorcall" fn f() {
14+
//~^ ABI "vectorcall" which requires the `sse2` target feature
15+
}
16+
17+
#[no_mangle]
18+
pub fn call_site() {
19+
f();
20+
//~^ ABI "vectorcall" which requires the `sse2` target feature
21+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: this function definition uses ABI "vectorcall" which requires the `sse2` target feature, which is not enabled
2+
--> $DIR/vectorcall-abi-checks.rs:13:1
3+
|
4+
LL | pub extern "vectorcall" fn f() {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function defined here
6+
|
7+
= help: consider enabling it globally (`-C target-feature=+sse2`) or locally (`#[target_feature(enable="sse2")]`)
8+
9+
error: this function call uses ABI "vectorcall" which requires the `sse2` target feature, which is not enabled in the caller
10+
--> $DIR/vectorcall-abi-checks.rs:19:5
11+
|
12+
LL | f();
13+
| ^^^ function called here
14+
|
15+
= help: consider enabling it globally (`-C target-feature=+sse2`) or locally (`#[target_feature(enable="sse2")]`)
16+
17+
error: aborting due to 2 previous errors
18+

0 commit comments

Comments
 (0)