Skip to content

Commit 2ef8280

Browse files
committed
Use the aligned size for alloca at ret when the pass mode is cast.
1 parent c453dcd commit 2ef8280

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

+14-4
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,20 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
230230
let layout = start_bx.layout_of(fx.monomorphize(decl.ty));
231231
assert!(!layout.ty.has_erasable_regions());
232232

233-
if local == mir::RETURN_PLACE && fx.fn_abi.ret.is_indirect() {
234-
debug!("alloc: {:?} (return place) -> place", local);
235-
let llretptr = start_bx.get_param(0);
236-
return LocalRef::Place(PlaceRef::new_sized(llretptr, layout));
233+
if local == mir::RETURN_PLACE {
234+
match fx.fn_abi.ret.mode {
235+
PassMode::Indirect { .. } => {
236+
debug!("alloc: {:?} (return place) -> place", local);
237+
let llretptr = start_bx.get_param(0);
238+
return LocalRef::Place(PlaceRef::new_sized(llretptr, layout));
239+
}
240+
PassMode::Cast { ref cast, .. } => {
241+
debug!("alloc: {:?} (return place) -> place", local);
242+
let size = cast.size(&start_bx);
243+
return LocalRef::Place(PlaceRef::alloca_size(&mut start_bx, size, layout));
244+
}
245+
_ => {}
246+
};
237247
}
238248

239249
if memory_locals.contains(local) {

compiler/rustc_codegen_ssa/src/mir/place.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,17 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
108108
pub fn alloca<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
109109
bx: &mut Bx,
110110
layout: TyAndLayout<'tcx>,
111+
) -> Self {
112+
Self::alloca_size(bx, layout.size, layout)
113+
}
114+
115+
pub fn alloca_size<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
116+
bx: &mut Bx,
117+
size: Size,
118+
layout: TyAndLayout<'tcx>,
111119
) -> Self {
112120
assert!(layout.is_sized(), "tried to statically allocate unsized place");
113-
PlaceValue::alloca(bx, layout.size, layout.align.abi).with_type(layout)
121+
PlaceValue::alloca(bx, size, layout.align.abi).with_type(layout)
114122
}
115123

116124
/// Returns a place for an indirect reference to an unsized place.

tests/codegen/cast-target-abi.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ pub extern "C" fn returns_twou16s() -> TwoU16s {
102102
// powerpc returns this struct via sret pointer, it doesn't use the cast ABI.
103103
// The other targets copy the cast ABI type to an alloca.
104104

105-
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [4 x i8], align [[ABI_ALIGN:2]]
106-
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [4 x i8], align [[ABI_ALIGN:2]]
107-
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [4 x i8], align [[ABI_ALIGN:2]]
105+
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [8 x i8], align [[ABI_ALIGN:2]]
106+
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [8 x i8], align [[ABI_ALIGN:2]]
107+
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [8 x i8], align [[ABI_ALIGN:2]]
108108
// x86_64: [[ABI_ALLOCA:%.+]] = alloca [4 x i8], align [[ABI_ALIGN:2]]
109109

110110
// aarch64: [[ABI_VALUE:%.+]] = load [[ABI_TYPE:i64]], ptr [[ABI_ALLOCA]], align [[ABI_ALIGN]]
@@ -145,10 +145,10 @@ pub extern "C" fn returns_fiveu16s() -> FiveU16s {
145145
// powerpc returns this struct via sret pointer, it doesn't use the cast ABI.
146146
// The other targets copy the cast ABI type to an alloca.
147147

148-
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [10 x i8], align [[ABI_ALIGN:2]]
149-
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [10 x i8], align [[ABI_ALIGN:2]]
150-
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [10 x i8], align [[ABI_ALIGN:2]]
151-
// x86_64: [[ABI_ALLOCA:%.+]] = alloca [10 x i8], align [[ABI_ALIGN:2]]
148+
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:2]]
149+
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:2]]
150+
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:2]]
151+
// x86_64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:2]]
152152

153153
// aarch64: [[ABI_VALUE:%.+]] = load [[ABI_TYPE:\[2 x i64\]]], ptr [[ABI_ALLOCA]], align [[ABI_ALIGN]]
154154
// loongarch64: [[ABI_VALUE:%.+]] = load [[ABI_TYPE:\[2 x i64\]]], ptr [[ABI_ALLOCA]], align [[ABI_ALIGN]]
@@ -231,10 +231,10 @@ pub extern "C" fn returns_three32s() -> Three32s {
231231
// powerpc returns this struct via sret pointer, it doesn't use the cast ABI.
232232
// The other targets copy the cast ABI type to an alloca.
233233

234-
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [12 x i8], align [[ABI_ALIGN:4]]
235-
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [12 x i8], align [[ABI_ALIGN:4]]
236-
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [12 x i8], align [[ABI_ALIGN:4]]
237-
// x86_64: [[ABI_ALLOCA:%.+]] = alloca [12 x i8], align [[ABI_ALIGN:4]]
234+
// aarch64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:4]]
235+
// loongarch64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:4]]
236+
// sparc64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:4]]
237+
// x86_64: [[ABI_ALLOCA:%.+]] = alloca [16 x i8], align [[ABI_ALIGN:4]]
238238

239239
// aarch64: [[ABI_VALUE:%.+]] = load [[ABI_TYPE:\[2 x i64\]]], ptr [[ABI_ALLOCA]], align [[ABI_ALIGN]]
240240
// loongarch64: [[ABI_VALUE:%.+]] = load [[ABI_TYPE:\[2 x i64\]]], ptr [[ABI_ALLOCA]], align [[ABI_ALIGN]]

0 commit comments

Comments
 (0)