Skip to content

Commit 16087ee

Browse files
committed
Auto merge of rust-lang#118127 - RalfJung:unadjusted-abi, r=compiler-errors
the unadjusted ABI needs to pass aggregates by-value Fixes rust-lang#118124, a regression introduced in rust-lang#117500
2 parents fe3038f + ebfb95a commit 16087ee

File tree

3 files changed

+84
-6
lines changed

3 files changed

+84
-6
lines changed

compiler/rustc_target/src/abi/call/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,7 @@ impl HomogeneousAggregate {
382382
}
383383

384384
impl<'a, Ty> TyAndLayout<'a, Ty> {
385+
/// Returns `true` if this is an aggregate type (including a ScalarPair!)
385386
fn is_aggregate(&self) -> bool {
386387
match self.abi {
387388
Abi::Uninhabited | Abi::Scalar(_) | Abi::Vector { .. } => false,

compiler/rustc_ty_utils/src/abi.rs

+29-6
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,15 @@ fn adjust_for_rust_scalar<'tcx>(
365365
}
366366

367367
/// Ensure that the ABI makes basic sense.
368-
fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'tcx, Ty<'tcx>>) {
368+
fn fn_abi_sanity_check<'tcx>(
369+
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
370+
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
371+
spec_abi: SpecAbi,
372+
) {
369373
fn fn_arg_sanity_check<'tcx>(
370374
cx: &LayoutCx<'tcx, TyCtxt<'tcx>>,
371375
fn_abi: &FnAbi<'tcx, Ty<'tcx>>,
376+
spec_abi: SpecAbi,
372377
arg: &ArgAbi<'tcx, Ty<'tcx>>,
373378
) {
374379
match &arg.mode {
@@ -398,8 +403,8 @@ fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'
398403
// (See issue: https://github.com/rust-lang/rust/issues/117271)
399404
assert!(
400405
matches!(&*cx.tcx.sess.target.arch, "wasm32" | "wasm64")
401-
|| fn_abi.conv == Conv::PtxKernel,
402-
"`PassMode::Direct` for aggregates only allowed on wasm and `extern \"ptx-kernel\"` fns\nProblematic type: {:#?}",
406+
|| matches!(spec_abi, SpecAbi::PtxKernel | SpecAbi::Unadjusted),
407+
r#"`PassMode::Direct` for aggregates only allowed for "unadjusted" and "ptx-kernel" functions and on wasm\nProblematic type: {:#?}"#,
403408
arg.layout,
404409
);
405410
}
@@ -429,9 +434,9 @@ fn fn_abi_sanity_check<'tcx>(cx: &LayoutCx<'tcx, TyCtxt<'tcx>>, fn_abi: &FnAbi<'
429434
}
430435

431436
for arg in fn_abi.args.iter() {
432-
fn_arg_sanity_check(cx, fn_abi, arg);
437+
fn_arg_sanity_check(cx, fn_abi, spec_abi, arg);
433438
}
434-
fn_arg_sanity_check(cx, fn_abi, &fn_abi.ret);
439+
fn_arg_sanity_check(cx, fn_abi, spec_abi, &fn_abi.ret);
435440
}
436441

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

@@ -572,6 +577,24 @@ fn fn_abi_adjust_for_abi<'tcx>(
572577
fn_def_id: Option<DefId>,
573578
) -> Result<(), &'tcx FnAbiError<'tcx>> {
574579
if abi == SpecAbi::Unadjusted {
580+
// The "unadjusted" ABI passes aggregates in "direct" mode. That's fragile but needed for
581+
// some LLVM intrinsics.
582+
fn unadjust<'tcx>(arg: &mut ArgAbi<'tcx, Ty<'tcx>>) {
583+
// This still uses `PassMode::Pair` for ScalarPair types. That's unlikely to be intended,
584+
// but who knows what breaks if we change this now.
585+
if matches!(arg.layout.abi, Abi::Aggregate { .. }) {
586+
assert!(
587+
arg.layout.abi.is_sized(),
588+
"'unadjusted' ABI does not support unsized arguments"
589+
);
590+
}
591+
arg.make_direct_deprecated();
592+
}
593+
594+
unadjust(&mut fn_abi.ret);
595+
for arg in fn_abi.args.iter_mut() {
596+
unadjust(arg);
597+
}
575598
return Ok(());
576599
}
577600

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// build-pass
2+
// revisions: arm
3+
//[arm] compile-flags: --target arm-unknown-linux-gnueabi
4+
//[arm] needs-llvm-components: arm
5+
// revisions: aarch64
6+
//[aarch64] compile-flags: --target aarch64-unknown-linux-gnu
7+
//[aarch64] needs-llvm-components: aarch64
8+
#![feature(
9+
no_core, lang_items, link_llvm_intrinsics,
10+
abi_unadjusted, repr_simd, arm_target_feature,
11+
)]
12+
#![no_std]
13+
#![no_core]
14+
#![crate_type = "lib"]
15+
#![allow(non_camel_case_types)]
16+
17+
/// To work cross-target this test must be no_core.
18+
/// This little prelude supplies what we need.
19+
#[lang = "sized"]
20+
pub trait Sized {}
21+
22+
#[lang = "copy"]
23+
pub trait Copy: Sized {}
24+
impl Copy for i8 {}
25+
impl<T: ?Sized> Copy for *const T {}
26+
impl<T: ?Sized> Copy for *mut T {}
27+
28+
29+
// Regression test for https://github.com/rust-lang/rust/issues/118124.
30+
31+
#[repr(simd)]
32+
pub struct int8x16_t(
33+
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
34+
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
35+
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
36+
pub(crate) i8, pub(crate) i8, pub(crate) i8, pub(crate) i8,
37+
);
38+
impl Copy for int8x16_t {}
39+
40+
#[repr(C)]
41+
pub struct int8x16x4_t(pub int8x16_t, pub int8x16_t, pub int8x16_t, pub int8x16_t);
42+
impl Copy for int8x16x4_t {}
43+
44+
#[target_feature(enable = "neon")]
45+
#[cfg_attr(target_arch = "arm", target_feature(enable = "v7"))]
46+
pub unsafe fn vld1q_s8_x4(a: *const i8) -> int8x16x4_t {
47+
#[allow(improper_ctypes)]
48+
extern "unadjusted" {
49+
#[cfg_attr(target_arch = "arm", link_name = "llvm.arm.neon.vld1x4.v16i8.p0i8")]
50+
#[cfg_attr(target_arch = "aarch64", link_name = "llvm.aarch64.neon.ld1x4.v16i8.p0i8")]
51+
fn vld1q_s8_x4_(a: *const i8) -> int8x16x4_t;
52+
}
53+
vld1q_s8_x4_(a)
54+
}

0 commit comments

Comments
 (0)