Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d442cf5
control libunwind linkage mode via `crt-static` on gnullvm targets
mati865 Sep 28, 2024
b440ef8
Move some alloc tests to the alloctests crate
bjorn3 Dec 4, 2024
3e3bcfa
[CI] Use a lockfile for installing the `datadog` package
Kobzol Dec 9, 2024
3b05779
Add feature gate, not working yet
compiler-errors Nov 16, 2024
a7fa4cb
Implement projection and shim for AFIDT
compiler-errors Nov 16, 2024
57e8a1c
Don't check RPITITs that are Self:Sized for PointerLike
compiler-errors Dec 10, 2024
67df7cb
Simplify `rustc_mir_dataflow::abs_domain`.
nnethercote Dec 8, 2024
f7ca820
Forbid unsafe_op_in_unsafe_fn in hurd-specific os and sys files
sthibaul Dec 10, 2024
014363e
Don't emit "field expressions may not have generic arguments" if it's…
Dec 10, 2024
c04b52a
Add regression tests
oli-obk Dec 9, 2024
f11edf7
allow `symbol_intern_string_literal` lint in test modules
onur-ozkan Dec 11, 2024
1268445
remove `Kind` check for `symbol_intern_string_literal`
onur-ozkan Dec 11, 2024
6a8bc4b
Remove consteval note from <*mut T>::align_offset docs.
zachs18 Dec 11, 2024
6d3d61f
Evaluate constants in SIMD vec lengths before rejecting them
oli-obk Dec 9, 2024
98edb8f
Clarify why a type is rejected for asm!
oli-obk Dec 9, 2024
1bc5897
Stabilize the Rust 2024 prelude
ehuss Dec 11, 2024
40c9645
Remove `PErr`.
nnethercote Dec 12, 2024
2caada1
Properly consider APITs for never type fallback ascription fix
compiler-errors Dec 10, 2024
2779de7
Rollup merge of #122003 - mati865:gnullvm-build-libunwind, r=petroche…
jieyouxu Dec 12, 2024
85ab5be
Rollup merge of #133122 - compiler-errors:afidt, r=oli-obk
jieyouxu Dec 12, 2024
65472d2
Rollup merge of #133859 - bjorn3:move_tests_to_alloctests, r=tgross35
jieyouxu Dec 12, 2024
9aef64f
Rollup merge of #134070 - oli-obk:push-nquzymupzlsq, r=jieyouxu
jieyouxu Dec 12, 2024
95125c1
Rollup merge of #134095 - Kobzol:datadog-lockfile, r=MarcoIeni
jieyouxu Dec 12, 2024
b2434ea
Rollup merge of #134144 - compiler-errors:fallback-apit, r=WaffleLapkin
jieyouxu Dec 12, 2024
41bec17
Rollup merge of #134152 - nnethercote:simplify-rustc_mir_dataflow-abs…
jieyouxu Dec 12, 2024
c5a1869
Rollup merge of #134154 - dev-ardi:field-expr-generics, r=compiler-er…
jieyouxu Dec 12, 2024
6daf30a
Rollup merge of #134155 - sthibaul:unsafe_op_in_unsafe_fn, r=tgross35
jieyouxu Dec 12, 2024
69d9a2a
Rollup merge of #134173 - onur-ozkan:allow-symbol-intern-string-liter…
jieyouxu Dec 12, 2024
3025feb
Rollup merge of #134178 - ehuss:stabilize-2024-prelude, r=amanieu,tra…
jieyouxu Dec 12, 2024
52919ed
Rollup merge of #134179 - zachs18:align_offset_mut_ptr_doc, r=working…
jieyouxu Dec 12, 2024
328b086
Rollup merge of #134187 - nnethercote:rm-PErr, r=jieyouxu
jieyouxu Dec 12, 2024
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 74 additions & 44 deletions compiler/rustc_hir_analysis/src/check/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::assert_matches::debug_assert_matches;
use rustc_abi::FieldIdx;
use rustc_ast::InlineAsmTemplatePiece;
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir::def_id::DefId;
use rustc_hir::{self as hir, LangItem};
use rustc_middle::bug;
use rustc_middle::ty::{self, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
Expand All @@ -21,6 +22,12 @@ pub struct InlineAsmCtxt<'a, 'tcx> {
get_operand_ty: Box<dyn Fn(&'tcx hir::Expr<'tcx>) -> Ty<'tcx> + 'a>,
}

enum NonAsmTypeReason<'tcx> {
UnevaluatedSIMDArrayLength(DefId, ty::Const<'tcx>),
Invalid(Ty<'tcx>),
InvalidElement(DefId, Ty<'tcx>),
}

impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
pub fn new_global_asm(tcx: TyCtxt<'tcx>) -> Self {
InlineAsmCtxt {
Expand Down Expand Up @@ -56,7 +63,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
false
}

fn get_asm_ty(&self, ty: Ty<'tcx>) -> Option<InlineAsmType> {
fn get_asm_ty(&self, ty: Ty<'tcx>) -> Result<InlineAsmType, NonAsmTypeReason<'tcx>> {
let asm_ty_isize = match self.tcx.sess.target.pointer_width {
16 => InlineAsmType::I16,
32 => InlineAsmType::I32,
Expand All @@ -65,64 +72,62 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
};

match *ty.kind() {
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::I8),
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Some(InlineAsmType::I16),
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Some(InlineAsmType::I32),
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Some(InlineAsmType::I64),
ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Some(InlineAsmType::I128),
ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Some(asm_ty_isize),
ty::Float(FloatTy::F16) => Some(InlineAsmType::F16),
ty::Float(FloatTy::F32) => Some(InlineAsmType::F32),
ty::Float(FloatTy::F64) => Some(InlineAsmType::F64),
ty::Float(FloatTy::F128) => Some(InlineAsmType::F128),
ty::FnPtr(..) => Some(asm_ty_isize),
ty::RawPtr(ty, _) if self.is_thin_ptr_ty(ty) => Some(asm_ty_isize),
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Ok(InlineAsmType::I8),
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Ok(InlineAsmType::I16),
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Ok(InlineAsmType::I32),
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Ok(InlineAsmType::I64),
ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Ok(InlineAsmType::I128),
ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Ok(asm_ty_isize),
ty::Float(FloatTy::F16) => Ok(InlineAsmType::F16),
ty::Float(FloatTy::F32) => Ok(InlineAsmType::F32),
ty::Float(FloatTy::F64) => Ok(InlineAsmType::F64),
ty::Float(FloatTy::F128) => Ok(InlineAsmType::F128),
ty::FnPtr(..) => Ok(asm_ty_isize),
ty::RawPtr(ty, _) if self.is_thin_ptr_ty(ty) => Ok(asm_ty_isize),
ty::Adt(adt, args) if adt.repr().simd() => {
let fields = &adt.non_enum_variant().fields;
let elem_ty = fields[FieldIdx::ZERO].ty(self.tcx, args);
let field = &fields[FieldIdx::ZERO];
let elem_ty = field.ty(self.tcx, args);

let (size, ty) = match elem_ty.kind() {
ty::Array(ty, len) => {
let len = self.tcx.normalize_erasing_regions(self.typing_env, *len);
if let Some(len) = len.try_to_target_usize(self.tcx) {
(len, *ty)
} else {
return None;
return Err(NonAsmTypeReason::UnevaluatedSIMDArrayLength(
field.did, len,
));
}
}
_ => (fields.len() as u64, elem_ty),
};

match ty.kind() {
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Some(InlineAsmType::VecI8(size)),
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => {
Some(InlineAsmType::VecI16(size))
}
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => {
Some(InlineAsmType::VecI32(size))
}
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => {
Some(InlineAsmType::VecI64(size))
}
ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Ok(InlineAsmType::VecI8(size)),
ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Ok(InlineAsmType::VecI16(size)),
ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Ok(InlineAsmType::VecI32(size)),
ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Ok(InlineAsmType::VecI64(size)),
ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
Some(InlineAsmType::VecI128(size))
Ok(InlineAsmType::VecI128(size))
}
ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
Some(match self.tcx.sess.target.pointer_width {
Ok(match self.tcx.sess.target.pointer_width {
16 => InlineAsmType::VecI16(size),
32 => InlineAsmType::VecI32(size),
64 => InlineAsmType::VecI64(size),
width => bug!("unsupported pointer width: {width}"),
})
}
ty::Float(FloatTy::F16) => Some(InlineAsmType::VecF16(size)),
ty::Float(FloatTy::F32) => Some(InlineAsmType::VecF32(size)),
ty::Float(FloatTy::F64) => Some(InlineAsmType::VecF64(size)),
ty::Float(FloatTy::F128) => Some(InlineAsmType::VecF128(size)),
_ => None,
ty::Float(FloatTy::F16) => Ok(InlineAsmType::VecF16(size)),
ty::Float(FloatTy::F32) => Ok(InlineAsmType::VecF32(size)),
ty::Float(FloatTy::F64) => Ok(InlineAsmType::VecF64(size)),
ty::Float(FloatTy::F128) => Ok(InlineAsmType::VecF128(size)),
_ => Err(NonAsmTypeReason::InvalidElement(field.did, ty)),
}
}
ty::Infer(_) => bug!("unexpected infer ty in asm operand"),
_ => None,
_ => Err(NonAsmTypeReason::Invalid(ty)),
}
}

Expand Down Expand Up @@ -163,17 +168,42 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}
_ => self.get_asm_ty(ty),
};
let Some(asm_ty) = asm_ty else {
let msg = format!("cannot use value of type `{ty}` for inline assembly");
self.tcx
.dcx()
.struct_span_err(expr.span, msg)
.with_note(
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
)
.emit();
return None;
let asm_ty = match asm_ty {
Ok(asm_ty) => asm_ty,
Err(reason) => {
match reason {
NonAsmTypeReason::UnevaluatedSIMDArrayLength(did, len) => {
let msg = format!("cannot evaluate SIMD vector length `{len}`");
self.tcx
.dcx()
.struct_span_err(self.tcx.def_span(did), msg)
.with_span_note(
expr.span,
"SIMD vector length needs to be known statically for use in `asm!`",
)
.emit();
}
NonAsmTypeReason::Invalid(ty) => {
let msg = format!("cannot use value of type `{ty}` for inline assembly");
self.tcx.dcx().struct_span_err(expr.span, msg).with_note(
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
).emit();
}
NonAsmTypeReason::InvalidElement(did, ty) => {
let msg = format!(
"cannot use SIMD vector with element type `{ty}` for inline assembly"
);
self.tcx.dcx()
.struct_span_err(self.tcx.def_span(did), msg).with_span_note(
expr.span,
"only integers, floats, SIMD vectors, pointers and function pointers \
can be used as arguments for inline assembly",
).emit();
}
}
return None;
}
};

// Check that the type implements Copy. The only case where this can
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/asm/generic_const_simd_vec_len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//! This is a regression test to ensure that we emit a diagnostic pointing to the
//! reason the type was rejected in inline assembly.

//@ only-x86_64

#![feature(repr_simd)]

#[repr(simd)]
#[derive(Copy, Clone)]
pub struct Foo<const C: usize>([u8; C]);
//~^ ERROR: cannot evaluate SIMD vector length

pub unsafe fn foo<const C: usize>(a: Foo<C>) {
std::arch::asm!(
"movaps {src}, {src}",
src = in(xmm_reg) a,
//~^ NOTE: SIMD vector length needs to be known statically
);
}

fn main() {}
14 changes: 14 additions & 0 deletions tests/ui/asm/generic_const_simd_vec_len.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: cannot evaluate SIMD vector length `C`
--> $DIR/generic_const_simd_vec_len.rs:10:32
|
LL | pub struct Foo<const C: usize>([u8; C]);
| ^^^^^^^
|
note: SIMD vector length needs to be known statically for use in `asm!`
--> $DIR/generic_const_simd_vec_len.rs:16:27
|
LL | src = in(xmm_reg) a,
| ^

error: aborting due to 1 previous error

22 changes: 22 additions & 0 deletions tests/ui/asm/named_const_simd_vec_len.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//! This is a regression test to ensure that we evaluate
//! SIMD vector length constants instead of assuming they are literals.

//@ only-x86_64
//@ check-pass

#![feature(repr_simd)]

const C: usize = 16;

#[repr(simd)]
#[derive(Copy, Clone)]
pub struct Foo([u8; C]);

pub unsafe fn foo(a: Foo) {
std::arch::asm!(
"movaps {src}, {src}",
src = in(xmm_reg) a,
);
}

fn main() {}