Skip to content

Commit 94200f6

Browse files
committed
default Aggregate ABI to Indirect, and make sure it's never used for unsized
1 parent 7b0df22 commit 94200f6

File tree

13 files changed

+102
-13
lines changed

13 files changed

+102
-13
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_ty_utils/src/abi.rs

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

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

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

tests/ui/abi/compatibility.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
//[wasi] compile-flags: --target wasm32-wasi
3939
//[wasi] needs-llvm-components: webassembly
4040
// FIXME: disabled on nvptx64 since the target ABI fails the sanity check
41+
// see https://github.com/rust-lang/rust/issues/117480
4142
/* revisions: nvptx64
4243
[nvptx64] compile-flags: --target nvptx64-nvidia-cuda
4344
[nvptx64] needs-llvm-components: nvptx

0 commit comments

Comments
 (0)