Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f4426c1

Browse files
use [N x i8] for alloca types
1 parent a07f3eb commit f4426c1

33 files changed

+203
-148
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -898,26 +898,14 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
898898
self.gcc_checked_binop(oop, typ, lhs, rhs)
899899
}
900900

901-
fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
902-
// FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
903-
// Ideally, we shouldn't need to do this check.
904-
let aligned_type = if ty == self.cx.u128_type || ty == self.cx.i128_type {
905-
ty
906-
} else {
907-
ty.get_aligned(align.bytes())
908-
};
901+
fn alloca(&mut self, size: Size, align: Align) -> RValue<'gcc> {
902+
let ty = self.cx.type_array(self.cx.type_i8(), size.bytes()).get_aligned(align.bytes());
909903
// TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
910904
self.stack_var_count.set(self.stack_var_count.get() + 1);
911-
self.current_func()
912-
.new_local(
913-
self.location,
914-
aligned_type,
915-
&format!("stack_var_{}", self.stack_var_count.get()),
916-
)
917-
.get_address(self.location)
905+
self.current_func().new_local(None, ty, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
918906
}
919907

920-
fn byte_array_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
908+
fn dynamic_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
921909
unimplemented!();
922910
}
923911

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
531531
// We instead thus allocate some scratch space...
532532
let scratch_size = cast.size(bx);
533533
let scratch_align = cast.align(bx);
534-
let llscratch = bx.alloca(cast.gcc_type(bx), scratch_align);
534+
let llscratch = bx.alloca(scratch_size, scratch_align);
535535
bx.lifetime_start(llscratch, scratch_size);
536536

537537
// ... where we first store the value...

compiler/rustc_codegen_gcc/src/intrinsic/simd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::span_bug;
1818
use rustc_middle::ty::layout::HasTyCtxt;
1919
use rustc_middle::ty::{self, Ty};
2020
use rustc_span::{sym, Span, Symbol};
21-
use rustc_target::abi::Align;
21+
use rustc_target::abi::{Align, Size};
2222

2323
use crate::builder::Builder;
2424
#[cfg(not(feature = "master"))]
@@ -558,7 +558,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
558558
let ze = bx.zext(result, bx.type_ix(expected_bytes * 8));
559559

560560
// Convert the integer to a byte array
561-
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
561+
let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
562562
bx.store(ze, ptr, Align::ONE);
563563
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
564564
let ptr = bx.pointercast(ptr, bx.cx.type_ptr_to(array_ty));

compiler/rustc_codegen_llvm/src/abi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
227227
// when passed by value, making it larger.
228228
let copy_bytes = cmp::min(scratch_size.bytes(), self.layout.size.bytes());
229229
// Allocate some scratch space...
230-
let llscratch = bx.alloca(cast.llvm_type(bx), scratch_align);
230+
let llscratch = bx.alloca(scratch_size, scratch_align);
231231
bx.lifetime_start(llscratch, scratch_size);
232232
// ...store the value...
233233
bx.store(val, llscratch, scratch_align);

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -468,20 +468,21 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
468468
val
469469
}
470470

471-
fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
471+
fn alloca(&mut self, size: Size, align: Align) -> &'ll Value {
472472
let mut bx = Builder::with_cx(self.cx);
473473
bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
474+
let ty = self.cx().type_array(self.cx().type_i8(), size.bytes());
474475
unsafe {
475476
let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
476477
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
477478
alloca
478479
}
479480
}
480481

481-
fn byte_array_alloca(&mut self, len: &'ll Value, align: Align) -> &'ll Value {
482+
fn dynamic_alloca(&mut self, size: &'ll Value, align: Align) -> &'ll Value {
482483
unsafe {
483484
let alloca =
484-
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), len, UNNAMED);
485+
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), size, UNNAMED);
485486
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
486487
alloca
487488
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, LayoutOf};
1818
use rustc_middle::ty::{self, GenericArgsRef, Ty};
1919
use rustc_middle::{bug, span_bug};
2020
use rustc_span::{sym, Span, Symbol};
21-
use rustc_target::abi::{self, Align, HasDataLayout, Primitive};
21+
use rustc_target::abi::{self, Align, HasDataLayout, Primitive, Size};
2222
use rustc_target::spec::{HasTargetSpec, PanicStrategy};
2323

2424
use std::cmp::Ordering;
@@ -638,8 +638,9 @@ fn codegen_msvc_try<'ll>(
638638
// }
639639
//
640640
// More information can be found in libstd's seh.rs implementation.
641+
let ptr_size = bx.tcx().data_layout.pointer_size;
641642
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
642-
let slot = bx.alloca(bx.type_ptr(), ptr_align);
643+
let slot = bx.alloca(ptr_size, ptr_align);
643644
let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
644645
bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None, None);
645646

@@ -909,15 +910,14 @@ fn codegen_emcc_try<'ll>(
909910

910911
// We need to pass two values to catch_func (ptr and is_rust_panic), so
911912
// create an alloca and pass a pointer to that.
913+
let ptr_size = bx.tcx().data_layout.pointer_size;
912914
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
913915
let i8_align = bx.tcx().data_layout.i8_align.abi;
914-
let catch_data_type = bx.type_struct(&[bx.type_ptr(), bx.type_bool()], false);
915-
let catch_data = bx.alloca(catch_data_type, ptr_align);
916-
let catch_data_0 =
917-
bx.inbounds_gep(catch_data_type, catch_data, &[bx.const_usize(0), bx.const_usize(0)]);
918-
bx.store(ptr, catch_data_0, ptr_align);
919-
let catch_data_1 =
920-
bx.inbounds_gep(catch_data_type, catch_data, &[bx.const_usize(0), bx.const_usize(1)]);
916+
// Required in order for there to be no padding between the fields.
917+
assert!(i8_align <= ptr_align);
918+
let catch_data = bx.alloca(2 * ptr_size, ptr_align);
919+
bx.store(ptr, catch_data, ptr_align);
920+
let catch_data_1 = bx.inbounds_ptradd(catch_data, bx.const_usize(ptr_size.bytes()));
921921
bx.store(is_rust_panic, catch_data_1, i8_align);
922922

923923
let catch_ty = bx.type_func(&[bx.type_ptr(), bx.type_ptr()], bx.type_void());
@@ -1363,7 +1363,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
13631363
let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));
13641364

13651365
// Convert the integer to a byte array
1366-
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
1366+
let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
13671367
bx.store(ze, ptr, Align::ONE);
13681368
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
13691369
return Ok(bx.load(array_ty, ptr, Align::ONE));

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
508508
let ptr_size = bx.tcx().data_layout.pointer_size;
509509
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
510510
let arg_argc = bx.const_int(cx.type_isize(), 2);
511-
let arg_argv = bx.alloca(cx.type_array(cx.type_ptr(), 2), ptr_align);
511+
let arg_argv = bx.alloca(2 * ptr_size, ptr_align);
512512
bx.store(param_handle, arg_argv, ptr_align);
513513
let arg_argv_el1 = bx.inbounds_ptradd(arg_argv, bx.const_usize(ptr_size.bytes()));
514514
bx.store(param_system_table, arg_argv_el1, ptr_align);

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
15121512
// when passed by value, making it larger.
15131513
let copy_bytes = cmp::min(scratch_size.bytes(), arg.layout.size.bytes());
15141514
// Allocate some scratch space...
1515-
let llscratch = bx.alloca(bx.cast_backend_type(cast), scratch_align);
1515+
let llscratch = bx.alloca(scratch_size, scratch_align);
15161516
bx.lifetime_start(llscratch, scratch_size);
15171517
// ...memcpy the value...
15181518
bx.memcpy(

compiler/rustc_codegen_ssa/src/mir/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
323323
let llfield_ty = bx.cx().backend_type(field);
324324

325325
// Can't bitcast an aggregate, so round trip through memory.
326-
let llptr = bx.alloca(llfield_ty, field.align.abi);
326+
let llptr = bx.alloca(field.size, field.align.abi);
327327
bx.store(*llval, llptr, field.align.abi);
328328
*llval = bx.load(llfield_ty, llptr, field.align.abi);
329329
}
@@ -466,7 +466,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
466466
let align_minus_1 = bx.sub(align, one);
467467
let size_extra = bx.add(size, align_minus_1);
468468
let min_align = Align::ONE;
469-
let alloca = bx.byte_array_alloca(size_extra, min_align);
469+
let alloca = bx.dynamic_alloca(size_extra, min_align);
470470
let address = bx.ptrtoint(alloca, bx.type_isize());
471471
let neg_address = bx.neg(address);
472472
let offset = bx.and(neg_address, align_minus_1);

compiler/rustc_codegen_ssa/src/mir/place.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
5757
align: Align,
5858
) -> Self {
5959
assert!(layout.is_sized(), "tried to statically allocate unsized place");
60-
let tmp = bx.alloca(bx.cx().backend_type(layout), align);
60+
let tmp = bx.alloca(layout.size, align);
6161
Self::new_sized_aligned(tmp, layout, align)
6262
}
6363

0 commit comments

Comments
 (0)