Skip to content

Commit

Permalink
Auto merge of rust-lang#118127 - RalfJung:unadjusted-abi, r=compiler-…
Browse files Browse the repository at this point in the history
…errors

the unadjusted ABI needs to pass aggregates by-value

Fixes rust-lang#118124, a regression introduced in rust-lang#117500
  • Loading branch information
bors committed Nov 25, 2023
2 parents fe3038f + ebfb95a commit 16087ee
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_target/src/abi/call/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ impl HomogeneousAggregate {
}

impl<'a, Ty> TyAndLayout<'a, Ty> {
/// Returns `true` if this is an aggregate type (including a ScalarPair!)
fn is_aggregate(&self) -> bool {
match self.abi {
Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } => false,
Expand Down
35 changes: 29 additions & 6 deletions compiler/rustc_ty_utils/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,15 @@ fn adjust_for_rust_scalar<'tcx>(
}

/// Ensure that the ABI makes basic sense.
fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) {
fn fn_abi_sanity_check<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
spec_abi: SpecAbi,
) {
fn fn_arg_sanity_check<'tcx>(
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
spec_abi: SpecAbi,
arg: &ArgAbi<'tcx, Ty<'tcx>>,
) {
match &arg.mode {
Expand Down Expand Up @@ -398,8 +403,8 @@ fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'
// (See issue: https://github.com/rust-lang/rust/issues/117271)
assert!(
matches!(&*cx.tcx.sess.target.arch, "wasm32" | "wasm64")
|| fn_abi.conv == Conv::PtxKernel,
"`PassMode::Direct` for aggregates only allowed on wasm and `extern \"ptx-kernel\"` fns\nProblematic type: {:#?}",
|| matches!(spec_abi, SpecAbi::PtxKernel | SpecAbi::Unadjusted),
r#"`PassMode::Direct` for aggregates only allowed for "unadjusted" and "ptx-kernel" functions and on wasm\nProblematic type: {:#?}"#,
arg.layout,
);
}
Expand Down Expand Up @@ -429,9 +434,9 @@ fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'
}

for arg in fn_abi.args.iter() {
fn_arg_sanity_check(cx, fn_abi, arg);
fn_arg_sanity_check(cx, fn_abi, spec_abi, arg);
}
fn_arg_sanity_check(cx, fn_abi, &fn_abi.ret);
fn_arg_sanity_check(cx, fn_abi, spec_abi, &fn_abi.ret);
}

// FIXME(eddyb) perhaps group the signature/type-containing (or all of them?)
Expand Down Expand Up @@ -560,7 +565,7 @@ fn fn_abi_new_uncached<'tcx>(
};
fn_abi_adjust_for_abi(cx, &mut fn_abi, sig.abi, fn_def_id)?;
debug!("fn_abi_new_uncached = {:?}", fn_abi);
fn_abi_sanity_check(cx, &fn_abi);
fn_abi_sanity_check(cx, &fn_abi, sig.abi);
Ok(cx.tcx.arena.alloc(fn_abi))
}

Expand All @@ -572,6 +577,24 @@ fn fn_abi_adjust_for_abi<'tcx>(
fn_def_id: Option<DefId>,
) -> Result<(), &'tcx FnAbiError<'tcx>> {
if abi == SpecAbi::Unadjusted {
// The "unadjusted" ABI passes aggregates in "direct" mode. That's fragile but needed for
// some LLVM intrinsics.
fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
// This still uses `PassMode::Pair` for ScalarPair types. That's unlikely to be intended,
// but who knows what breaks if we change this now.
if matches!(arg.layout.abi, Abi::Aggregate { .. }) {
assert!(
arg.layout.abi.is_sized(),
"'unadjusted' ABI does not support unsized arguments"
);
}
arg.make_direct_deprecated();
}

unadjust(&mut fn_abi.ret);
for arg in fn_abi.args.iter_mut() {
unadjust(arg);
}
return Ok(());
}

Expand Down
54 changes: 54 additions & 0 deletions tests/ui/abi/arm-unadjusted-intrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// build-pass
// revisions: arm
//[arm] compile-flags: --target arm-unknown-linux-gnueabi
//[arm] needs-llvm-components: arm
// revisions: aarch64
//[aarch64] compile-flags: --target aarch64-unknown-linux-gnu
//[aarch64] needs-llvm-components: aarch64
#![feature(
no_core, lang_items, link_llvm_intrinsics,
abi_unadjusted, repr_simd, arm_target_feature,
)]
#![no_std]
#![no_core]
#![crate_type = "lib"]
#![allow(non_camel_case_types)]

/// To work cross-target this test must be no_core.
/// This little prelude supplies what we need.
#[lang = "sized"]
pub trait Sized {}

#[lang = "copy"]
pub trait Copy: Sized {}
impl Copy for i8 {}
impl<T: ?Sized> Copy for *const T {}
impl<T: ?Sized> Copy for *mut T {}


// Regression test for https://github.com/rust-lang/rust/issues/118124.

#[repr(simd)]
pub struct int8x16_t(
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
);
impl Copy for int8x16_t {}

#[repr(C)]
pub struct int8x16x4_t(pub int8x16_t, pub int8x16_t, pub int8x16_t, pub int8x16_t);
impl Copy for int8x16x4_t {}

#[target_feature(enable = "neon")]
#[cfg_attr(target_arch = "arm", target_feature(enable = "v7"))]
pub unsafe fn vld1q_s8_x4(a: *const i8) -> int8x16x4_t {
#[allow(improper_ctypes)]
extern "unadjusted" {
#[cfg_attr(target_arch = "arm", link_name = "llvm.arm.neon.vld1x4.v16i8.p0i8")]
#[cfg_attr(target_arch = "aarch64", link_name = "llvm.aarch64.neon.ld1x4.v16i8.p0i8")]
fn vld1q_s8_x4_(a: *const i8) -> int8x16x4_t;
}
vld1q_s8_x4_(a)
}

0 comments on commit 16087ee

Please sign in to comment.