Skip to content

Commit eaaa03f

Browse files
committed
default Aggregate ABI to Indirect, and make sure it's never used for unsized
1 parent 405e420 commit eaaa03f

File tree

16 files changed

+124
-23
lines changed

16 files changed

+124
-23
lines changed

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

+8
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ where
4040
Ty: TyAbiInterface<'a, C> + Copy,
4141
C: HasDataLayout,
4242
{
43+
if !ret.layout.is_sized() {
44+
// Not touching this...
45+
return;
46+
}
4347
if !ret.layout.is_aggregate() {
4448
if kind == AbiKind::DarwinPCS {
4549
// On Darwin, when returning an i8/i16, it must be sign-extended to 32 bits,
@@ -67,6 +71,10 @@ where
6771
Ty: TyAbiInterface<'a, C> + Copy,
6872
C: HasDataLayout,
6973
{
74+
if !arg.layout.is_sized() {
75+
// Not touching this...
76+
return;
77+
}
7078
if !arg.layout.is_aggregate() {
7179
if kind == AbiKind::DarwinPCS {
7280
// On Darwin, when passing an i8/i16, it must be sign-extended to 32 bits,

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

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ where
3030
Ty: TyAbiInterface<'a, C> + Copy,
3131
C: HasDataLayout,
3232
{
33+
if !ret.layout.is_sized() {
34+
// Not touching this...
35+
return;
36+
}
3337
if !ret.layout.is_aggregate() {
3438
ret.extend_integer_width_to(32);
3539
return;
@@ -56,6 +60,10 @@ where
5660
Ty: TyAbiInterface<'a, C> + Copy,
5761
C: HasDataLayout,
5862
{
63+
if !arg.layout.is_sized() {
64+
// Not touching this...
65+
return;
66+
}
5967
if !arg.layout.is_aggregate() {
6068
arg.extend_integer_width_to(32);
6169
return;

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

+8
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ fn classify_ret<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, xlen: u64, flen: u6
152152
where
153153
Ty: TyAbiInterface<'a, C> + Copy,
154154
{
155+
if !arg.layout.is_sized() {
156+
// Not touching this...
157+
return false; // I guess? return value of this function is not documented
158+
}
155159
if let Some(conv) = should_use_fp_conv(cx, &arg.layout, xlen, flen) {
156160
match conv {
157161
FloatConv::Float(f) => {
@@ -214,6 +218,10 @@ fn classify_arg<'a, Ty, C>(
214218
) where
215219
Ty: TyAbiInterface<'a, C> + Copy,
216220
{
221+
if !arg.layout.is_sized() {
222+
// Not touching this...
223+
return;
224+
}
217225
if !is_vararg {
218226
match should_use_fp_conv(cx, &arg.layout, xlen, flen) {
219227
Some(FloatConv::Float(f)) if *avail_fprs >= 1 => {

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn classify_arg<Ty, C>(cx: &C, arg: &mut ArgAbi<'_, Ty>, offset: &mut Size)
1717
where
1818
C: HasDataLayout,
1919
{
20+
if !arg.layout.is_sized() {
21+
// Not touching this...
22+
return;
23+
}
2024
let dl = cx.data_layout();
2125
let size = arg.layout.size;
2226
let align = arg.layout.align.max(dl.i32_align).min(dl.i64_align).abi;

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

+14-4
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
422422
}))
423423
}
424424

425-
Abi::ScalarPair(..) | Abi::Aggregate { .. } => {
425+
Abi::ScalarPair(..) | Abi::Aggregate { sized: true } => {
426426
// Helper for computing `homogeneous_aggregate`, allowing a custom
427427
// starting offset (used below for handling variants).
428428
let from_fields_at =
@@ -520,6 +520,7 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
520520
Ok(result)
521521
}
522522
}
523+
Abi::Aggregate { sized: false } => Err(Heterogeneous),
523524
}
524525
}
525526
}
@@ -555,8 +556,7 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
555556
scalar_attrs(&layout, b, a.size(cx).align_to(b.align(cx).abi)),
556557
),
557558
Abi::Vector { .. } => PassMode::Direct(ArgAttributes::new()),
558-
// The `Aggregate` ABI should always be adjusted later.
559-
Abi::Aggregate { .. } => PassMode::Direct(ArgAttributes::new()),
559+
Abi::Aggregate { .. } => Self::indirect_pass_mode(&layout),
560560
};
561561
ArgAbi { layout, mode }
562562
}
@@ -580,10 +580,20 @@ impl<'a, Ty> ArgAbi<'a, Ty> {
580580
PassMode::Indirect { attrs, meta_attrs, on_stack: false }
581581
}
582582

583+
/// Pass this argument directly instead. Should NOT be used!
584+
/// Only exists because of past ABI mistakes that will take time to fix
585+
/// (see <https://github.com/rust-lang/rust/issues/115666>).
586+
pub fn make_direct_deprecated(&mut self) {
587+
self.mode = PassMode::Direct(ArgAttributes::new());
588+
}
589+
583590
pub fn make_indirect(&mut self) {
584591
match self.mode {
585592
PassMode::Direct(_) | PassMode::Pair(_, _) => {}
586-
PassMode::Indirect { attrs: _, meta_attrs: None, on_stack: false } => return,
593+
PassMode::Indirect { attrs: _, meta_attrs: _, on_stack: false } => {
594+
// already indirect
595+
return;
596+
}
587597
_ => panic!("Tried to make {:?} indirect", self.mode),
588598
}
589599

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

+9
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ use crate::abi::{HasDataLayout, TyAbiInterface};
44
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
55
if ret.layout.is_aggregate() && ret.layout.size.bits() > 64 {
66
ret.make_indirect();
7+
} else {
8+
// FIXME: this is wrong! Need to decide which ABI we really want here.
9+
ret.make_direct_deprecated();
710
}
811
}
912

1013
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
1114
if arg.layout.is_aggregate() && arg.layout.size.bits() > 64 {
1215
arg.make_indirect();
16+
} else {
17+
// FIXME: this is wrong! Need to decide which ABI we really want here.
18+
arg.make_direct_deprecated();
1319
}
1420
}
1521

@@ -30,6 +36,9 @@ where
3036
_ => unreachable!("Align is given as power of 2 no larger than 16 bytes"),
3137
};
3238
arg.cast_to(Uniform { unit, total: Size::from_bytes(2 * align_bytes) });
39+
} else {
40+
// FIXME: find a better way to do this. See https://github.com/rust-lang/rust/issues/117271.
41+
arg.make_direct_deprecated();
3342
}
3443
}
3544

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

+8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ where
4646
Ty: TyAbiInterface<'a, C> + Copy,
4747
C: HasDataLayout,
4848
{
49+
if !ret.layout.is_sized() {
50+
// Not touching this...
51+
return;
52+
}
4953
if !ret.layout.is_aggregate() {
5054
ret.extend_integer_width_to(64);
5155
return;
@@ -89,6 +93,10 @@ where
8993
Ty: TyAbiInterface<'a, C> + Copy,
9094
C: HasDataLayout,
9195
{
96+
if !arg.layout.is_sized() {
97+
// Not touching this...
98+
return;
99+
}
92100
if !arg.layout.is_aggregate() {
93101
arg.extend_integer_width_to(64);
94102
return;

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

+8
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ fn classify_ret<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>, xlen: u64, flen: u6
158158
where
159159
Ty: TyAbiInterface<'a, C> + Copy,
160160
{
161+
if !arg.layout.is_sized() {
162+
// Not touching this...
163+
return false; // I guess? return value of this function is not documented
164+
}
161165
if let Some(conv) = should_use_fp_conv(cx, &arg.layout, xlen, flen) {
162166
match conv {
163167
FloatConv::Float(f) => {
@@ -220,6 +224,10 @@ fn classify_arg<'a, Ty, C>(
220224
) where
221225
Ty: TyAbiInterface<'a, C> + Copy,
222226
{
227+
if !arg.layout.is_sized() {
228+
// Not touching this...
229+
return;
230+
}
223231
if !is_vararg {
224232
match should_use_fp_conv(cx, &arg.layout, xlen, flen) {
225233
Some(FloatConv::Float(f)) if *avail_fprs >= 1 => {

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ where
1717
Ty: TyAbiInterface<'a, C> + Copy,
1818
C: HasDataLayout,
1919
{
20+
if !arg.layout.is_sized() {
21+
// Not touching this...
22+
return;
23+
}
2024
if !arg.layout.is_aggregate() && arg.layout.size.bits() <= 64 {
2125
arg.extend_integer_width_to(64);
2226
return;

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ fn classify_arg<Ty, C>(cx: &C, arg: &mut ArgAbi<'_, Ty>, offset: &mut Size)
1717
where
1818
C: HasDataLayout,
1919
{
20+
if !arg.layout.is_sized() {
21+
// Not touching this...
22+
return;
23+
}
2024
let dl = cx.data_layout();
2125
let size = arg.layout.size;
2226
let align = arg.layout.align.max(dl.i32_align).min(dl.i64_align).abi;

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ where
3434
Ty: TyAbiInterface<'a, C> + Copy,
3535
C: HasDataLayout,
3636
{
37+
if !arg.layout.is_sized() {
38+
// Not touching this...
39+
return;
40+
}
3741
arg.extend_integer_width_to(32);
3842
if arg.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, arg) {
3943
arg.make_indirect_byval(None);
@@ -67,21 +71,33 @@ where
6771
/// Also see <https://github.com/rust-lang/rust/issues/115666>.
6872
pub fn compute_wasm_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
6973
if !fn_abi.ret.is_ignore() {
70-
classify_ret(&mut fn_abi.ret);
74+
classify_ret_wasm_abi(&mut fn_abi.ret);
7175
}
7276

7377
for arg in fn_abi.args.iter_mut() {
7478
if arg.is_ignore() {
7579
continue;
7680
}
77-
classify_arg(arg);
81+
classify_arg_wasm_abi(arg);
7882
}
7983

80-
fn classify_ret<Ty>(ret: &mut ArgAbi<'_, Ty>) {
84+
fn classify_ret_wasm_abi<Ty>(ret: &mut ArgAbi<'_, Ty>) {
85+
if !ret.layout.is_sized() {
86+
// Not touching this...
87+
return;
88+
}
89+
// FIXME: this is bad! https://github.com/rust-lang/rust/issues/115666
90+
ret.make_direct_deprecated();
8191
ret.extend_integer_width_to(32);
8292
}
8393

84-
fn classify_arg<Ty>(arg: &mut ArgAbi<'_, Ty>) {
94+
fn classify_arg_wasm_abi<Ty>(arg: &mut ArgAbi<'_, Ty>) {
95+
if !arg.layout.is_sized() {
96+
// Not touching this...
97+
return;
98+
}
99+
// FIXME: this is bad! https://github.com/rust-lang/rust/issues/115666
100+
arg.make_direct_deprecated();
85101
arg.extend_integer_width_to(32);
86102
}
87103
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ where
1414
C: HasDataLayout + HasTargetSpec,
1515
{
1616
if !fn_abi.ret.is_ignore() {
17-
if fn_abi.ret.layout.is_aggregate() {
17+
if fn_abi.ret.layout.is_aggregate() && fn_abi.ret.layout.is_sized() {
1818
// Returning a structure. Most often, this will use
1919
// a hidden first argument. On some platforms, though,
2020
// small structs are returned as integers.
@@ -50,7 +50,7 @@ where
5050
}
5151

5252
for arg in fn_abi.args.iter_mut() {
53-
if arg.is_ignore() {
53+
if arg.is_ignore() || !arg.layout.is_sized() {
5454
continue;
5555
}
5656

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,9 @@ fn reg_component(cls: &[Option<Class>], i: &mut usize, size: Size) -> Option<Reg
153153
}
154154
}
155155

156-
fn cast_target(cls: &[Option<Class>], size: Size) -> Option<CastTarget> {
156+
fn cast_target(cls: &[Option<Class>], size: Size) -> CastTarget {
157157
let mut i = 0;
158-
let lo = reg_component(cls, &mut i, size)?;
158+
let lo = reg_component(cls, &mut i, size).unwrap();
159159
let offset = Size::from_bytes(8) * (i as u64);
160160
let mut target = CastTarget::from(lo);
161161
if size > offset {
@@ -164,7 +164,7 @@ fn cast_target(cls: &[Option<Class>], size: Size) -> Option<CastTarget> {
164164
}
165165
}
166166
assert_eq!(reg_component(cls, &mut i, Size::ZERO), None);
167-
Some(target)
167+
target
168168
}
169169

170170
const MAX_INT_REGS: usize = 6; // RDI, RSI, RDX, RCX, R8, R9
@@ -179,6 +179,10 @@ where
179179
let mut sse_regs = MAX_SSE_REGS;
180180

181181
let mut x86_64_arg_or_ret = |arg: &mut ArgAbi<'a, Ty>, is_arg: bool| {
182+
if !arg.layout.is_sized() {
183+
// Not touching this...
184+
return;
185+
}
182186
let mut cls_or_mem = classify_arg(cx, arg);
183187

184188
if is_arg {
@@ -227,9 +231,7 @@ where
227231
// split into sized chunks passed individually
228232
if arg.layout.is_aggregate() {
229233
let size = arg.layout.size;
230-
if let Some(cast_target) = cast_target(cls, size) {
231-
arg.cast_to(cast_target);
232-
}
234+
arg.cast_to(cast_target(cls, size));
233235
} else {
234236
arg.extend_integer_width_to(32);
235237
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::abi::Abi;
66
pub fn compute_abi_info<Ty>(fn_abi: &mut FnAbi<'_, Ty>) {
77
let fixup = |a: &mut ArgAbi<'_, Ty>| {
88
match a.layout.abi {
9-
Abi::Uninhabited => {}
10-
Abi::ScalarPair(..) | Abi::Aggregate { .. } => match a.layout.size.bits() {
9+
Abi::Uninhabited | Abi::Aggregate { sized: false } => {}
10+
Abi::ScalarPair(..) | Abi::Aggregate { sized: true } => match a.layout.size.bits() {
1111
8 => a.cast_to(Reg::i8()),
1212
16 => a.cast_to(Reg::i16()),
1313
32 => a.cast_to(Reg::i32()),

compiler/rustc_ty_utils/src/abi.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,14 @@ fn fn_abi_adjust_for_abi<'tcx>(
591591

592592
_ => return,
593593
}
594-
// `Aggregate` ABI must be adjusted to ensure that ABI-compatible Rust types are passed
595-
// the same way.
594+
// Compute `Aggregate` ABI.
595+
596+
let is_indirect_not_on_stack =
597+
matches!(arg.mode, PassMode::Indirect { on_stack: false, .. });
598+
assert!(is_indirect_not_on_stack, "{:?}", arg);
596599

597600
let size = arg.layout.size;
598-
if arg.layout.is_unsized() || size > Pointer(AddressSpace::DATA).size(cx) {
599-
arg.make_indirect();
600-
} else {
601+
if !arg.layout.is_unsized() && size <= Pointer(AddressSpace::DATA).size(cx) {
601602
// We want to pass small aggregates as immediates, but using
602603
// an LLVM aggregate type for this leads to bad optimizations,
603604
// so we pick an appropriately sized integer type instead.

tests/ui/abi/compatibility.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// check-pass
22
// revisions: host
3+
// revisions: i686
4+
//[i686] compile-flags: --target i686-unknown-linux-gnu
5+
//[i686] needs-llvm-components: x86
6+
// revisions: x86-64
7+
//[x86-64] compile-flags: --target x86_64-unknown-linux-gnu
8+
//[x86-64] needs-llvm-components: x86
9+
// revisions: x86-64-win
10+
//[x86-64-win] compile-flags: --target x86_64-pc-windows-msvc
11+
//[x86-64-win] needs-llvm-components: x86
312
// revisions: arm
413
//[arm] compile-flags: --target arm-unknown-linux-gnueabi
514
//[arm] needs-llvm-components: arm
@@ -38,6 +47,7 @@
3847
//[wasi] compile-flags: --target wasm32-wasi
3948
//[wasi] needs-llvm-components: webassembly
4049
// FIXME: disabled on nvptx64 since the target ABI fails the sanity check
50+
// see https://github.com/rust-lang/rust/issues/117480
4151
/* revisions: nvptx64
4252
[nvptx64] compile-flags: --target nvptx64-nvidia-cuda
4353
[nvptx64] needs-llvm-components: nvptx
@@ -326,6 +336,7 @@ mod unsized_ {
326336
use super::*;
327337
test_transparent_unsized!(str_, str);
328338
test_transparent_unsized!(slice, [u8]);
339+
test_transparent_unsized!(slice_with_prefix, (usize, [u8]));
329340
test_transparent_unsized!(dyn_trait, dyn Any);
330341
}
331342

0 commit comments

Comments
 (0)