Skip to content

Commit a5b0838

Browse files
committed
Add an attribute to check the number of lanes in a SIMD vector after monomorphization
Unify zero-length and oversized SIMD errors
1 parent 9d82de1 commit a5b0838

34 files changed

+241
-77
lines changed

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,4 @@
1-
use std::num::IntErrorKind;
2-
3-
use rustc_hir::limit::Limit;
4-
51
use super::prelude::*;
6-
use crate::session_diagnostics::LimitInvalid;
7-
8-
impl<S: Stage> AcceptContext<'_, '_, S> {
9-
fn parse_limit_int(&self, nv: &NameValueParser) -> Option<Limit> {
10-
let Some(limit) = nv.value_as_str() else {
11-
self.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
12-
return None;
13-
};
14-
15-
let error_str = match limit.as_str().parse() {
16-
Ok(i) => return Some(Limit::new(i)),
17-
Err(e) => match e.kind() {
18-
IntErrorKind::PosOverflow => "`limit` is too large",
19-
IntErrorKind::Empty => "`limit` must be a non-negative integer",
20-
IntErrorKind::InvalidDigit => "not a valid integer",
21-
IntErrorKind::NegOverflow => {
22-
panic!(
23-
"`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead"
24-
)
25-
}
26-
IntErrorKind::Zero => {
27-
panic!("zero is a valid `limit` so should have returned Ok() when parsing")
28-
}
29-
kind => panic!("unimplemented IntErrorKind variant: {:?}", kind),
30-
},
31-
};
32-
33-
self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str });
34-
35-
None
36-
}
37-
}
382

393
pub(crate) struct CrateNameParser;
404

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,21 @@ impl<S: Stage> SingleAttributeParser<S> for RustcObjectLifetimeDefaultParser {
4949
Some(AttributeKind::RustcObjectLifetimeDefault)
5050
}
5151
}
52+
53+
pub(crate) struct RustcSimdMonomorphizeLaneLimitParser;
54+
55+
impl<S: Stage> SingleAttributeParser<S> for RustcSimdMonomorphizeLaneLimitParser {
56+
const PATH: &[Symbol] = &[sym::rustc_simd_monomorphize_lane_limit];
57+
const ATTRIBUTE_ORDER: AttributeOrder = AttributeOrder::KeepInnermost;
58+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Error;
59+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Struct)]);
60+
const TEMPLATE: AttributeTemplate = template!(NameValueStr: "N");
61+
62+
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser<'_>) -> Option<AttributeKind> {
63+
let ArgParser::NameValue(nv) = args else {
64+
cx.expected_name_value(cx.attr_span, None);
65+
return None;
66+
};
67+
Some(AttributeKind::RustcSimdMonomorphizeLaneLimit(cx.parse_limit_int(nv)?))
68+
}
69+
}

compiler/rustc_attr_parsing/src/attributes/util.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::num::IntErrorKind;
2+
13
use rustc_ast::LitKind;
24
use rustc_ast::attr::AttributeExt;
35
use rustc_feature::is_builtin_attr_name;
46
use rustc_hir::RustcVersion;
7+
use rustc_hir::limit::Limit;
58
use rustc_span::{Symbol, sym};
69

710
use crate::context::{AcceptContext, Stage};
8-
use crate::parser::ArgParser;
11+
use crate::parser::{ArgParser, NameValueParser};
12+
use crate::session_diagnostics::LimitInvalid;
913

1014
/// Parse a rustc version number written inside string literal in an attribute,
1115
/// like appears in `since = "1.0.0"`. Suffixes like "-dev" and "-nightly" are
@@ -85,3 +89,34 @@ pub(crate) fn parse_single_integer<S: Stage>(
8589
};
8690
Some(num.0)
8791
}
92+
93+
impl<S: Stage> AcceptContext<'_, '_, S> {
94+
pub(crate) fn parse_limit_int(&self, nv: &NameValueParser) -> Option<Limit> {
95+
let Some(limit) = nv.value_as_str() else {
96+
self.expected_string_literal(nv.value_span, Some(nv.value_as_lit()));
97+
return None;
98+
};
99+
100+
let error_str = match limit.as_str().parse() {
101+
Ok(i) => return Some(Limit::new(i)),
102+
Err(e) => match e.kind() {
103+
IntErrorKind::PosOverflow => "`limit` is too large",
104+
IntErrorKind::Empty => "`limit` must be a non-negative integer",
105+
IntErrorKind::InvalidDigit => "not a valid integer",
106+
IntErrorKind::NegOverflow => {
107+
panic!(
108+
"`limit` should never negatively overflow since we're parsing into a usize and we'd get Empty instead"
109+
)
110+
}
111+
IntErrorKind::Zero => {
112+
panic!("zero is a valid `limit` so should have returned Ok() when parsing")
113+
}
114+
kind => panic!("unimplemented IntErrorKind variant: {:?}", kind),
115+
},
116+
};
117+
118+
self.emit_err(LimitInvalid { span: self.attr_span, value_span: nv.value_span, error_str });
119+
120+
None
121+
}
122+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::attributes::prototype::CustomMirParser;
5353
use crate::attributes::repr::{AlignParser, AlignStaticParser, ReprParser};
5454
use crate::attributes::rustc_internal::{
5555
RustcLayoutScalarValidRangeEnd, RustcLayoutScalarValidRangeStart,
56-
RustcObjectLifetimeDefaultParser,
56+
RustcObjectLifetimeDefaultParser, RustcSimdMonomorphizeLaneLimitParser,
5757
};
5858
use crate::attributes::semantics::MayDangleParser;
5959
use crate::attributes::stability::{
@@ -195,6 +195,7 @@ attribute_parsers!(
195195
Single<RustcLayoutScalarValidRangeEnd>,
196196
Single<RustcLayoutScalarValidRangeStart>,
197197
Single<RustcObjectLifetimeDefaultParser>,
198+
Single<RustcSimdMonomorphizeLaneLimitParser>,
198199
Single<SanitizeParser>,
199200
Single<ShouldPanicParser>,
200201
Single<SkipDuringMethodDispatchParser>,

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,10 @@ pub(crate) struct FullyMonomorphizedLayoutCx<'tcx>(pub(crate) TyCtxt<'tcx>);
439439
impl<'tcx> LayoutOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
440440
#[inline]
441441
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
442-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
442+
if let LayoutError::SizeOverflow(_)
443+
| LayoutError::InvalidSimd { .. }
444+
| LayoutError::ReferencesError(_) = err
445+
{
443446
self.0.sess.dcx().span_fatal(span, err.to_string())
444447
} else {
445448
self.0
@@ -458,7 +461,9 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for FullyMonomorphizedLayoutCx<'tcx> {
458461
span: Span,
459462
fn_abi_request: FnAbiRequest<'tcx>,
460463
) -> ! {
461-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
464+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
465+
err
466+
{
462467
self.0.sess.dcx().emit_fatal(Spanned { span, node: err })
463468
} else {
464469
match fn_abi_request {

compiler/rustc_codegen_gcc/src/context.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,10 @@ impl<'gcc, 'tcx> HasX86AbiOpt for CodegenCx<'gcc, 'tcx> {
529529
impl<'gcc, 'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
530530
#[inline]
531531
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
532-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
532+
if let LayoutError::SizeOverflow(_)
533+
| LayoutError::InvalidSimd { .. }
534+
| LayoutError::ReferencesError(_) = err
535+
{
533536
self.tcx.dcx().emit_fatal(respan(span, err.into_diagnostic()))
534537
} else {
535538
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
@@ -545,7 +548,9 @@ impl<'gcc, 'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'gcc, 'tcx> {
545548
span: Span,
546549
fn_abi_request: FnAbiRequest<'tcx>,
547550
) -> ! {
548-
if let FnAbiError::Layout(LayoutError::SizeOverflow(_)) = err {
551+
if let FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::InvalidSimd { .. }) =
552+
err
553+
{
549554
self.tcx.dcx().emit_fatal(respan(span, err))
550555
} else {
551556
match fn_abi_request {

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,10 @@ impl<'tcx, 'll> HasTypingEnv<'tcx> for CodegenCx<'ll, 'tcx> {
991991
impl<'tcx> LayoutOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
992992
#[inline]
993993
fn handle_layout_err(&self, err: LayoutError<'tcx>, span: Span, ty: Ty<'tcx>) -> ! {
994-
if let LayoutError::SizeOverflow(_) | LayoutError::ReferencesError(_) = err {
994+
if let LayoutError::SizeOverflow(_)
995+
| LayoutError::ReferencesError(_)
996+
| LayoutError::InvalidSimd { .. } = err
997+
{
995998
self.tcx.dcx().emit_fatal(Spanned { span, node: err.into_diagnostic() })
996999
} else {
9971000
self.tcx.dcx().emit_fatal(ssa_errors::FailedToGetLayout { span, ty, err })
@@ -1008,7 +1011,11 @@ impl<'tcx> FnAbiOfHelpers<'tcx> for CodegenCx<'_, 'tcx> {
10081011
fn_abi_request: FnAbiRequest<'tcx>,
10091012
) -> ! {
10101013
match err {
1011-
FnAbiError::Layout(LayoutError::SizeOverflow(_) | LayoutError::Cycle(_)) => {
1014+
FnAbiError::Layout(
1015+
LayoutError::SizeOverflow(_)
1016+
| LayoutError::Cycle(_)
1017+
| LayoutError::InvalidSimd { .. },
1018+
) => {
10121019
self.tcx.dcx().emit_fatal(Spanned { span, node: err });
10131020
}
10141021
_ => match fn_abi_request {

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,12 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
12031203
"the `#[rustc_layout_scalar_valid_range_end]` attribute is just used to enable \
12041204
niche optimizations in the standard library",
12051205
),
1206+
rustc_attr!(
1207+
rustc_simd_monomorphize_lane_limit, Normal, template!(NameValueStr: "N"), ErrorFollowing,
1208+
EncodeCrossCrate::Yes,
1209+
"the `#[rustc_simd_monomorphize_lane_limit]` attribute is just used by std::simd \
1210+
for better error messages",
1211+
),
12061212
rustc_attr!(
12071213
rustc_nonnull_optimization_guaranteed, Normal, template!(Word), WarnFollowing,
12081214
EncodeCrossCrate::Yes,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ pub enum AttributeKind {
642642
/// Represents `#[rustc_object_lifetime_default]`.
643643
RustcObjectLifetimeDefault,
644644

645+
/// Represents `#[rustc_simd_monomorphize_lane_limit = "N"]`.
646+
RustcSimdMonomorphizeLaneLimit(Limit),
647+
645648
/// Represents `#[sanitize]`
646649
///
647650
/// the on set and off set are distjoint since there's a third option: unset.

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl AttributeKind {
8585
RustcLayoutScalarValidRangeEnd(..) => Yes,
8686
RustcLayoutScalarValidRangeStart(..) => Yes,
8787
RustcObjectLifetimeDefault => No,
88+
RustcSimdMonomorphizeLaneLimit(..) => Yes, // Affects layout computation, which needs to work cross-crate
8889
Sanitize { .. } => No,
8990
ShouldPanic { .. } => No,
9091
SkipDuringMethodDispatch { .. } => No,

0 commit comments

Comments
 (0)