Skip to content

Commit 38bf4f4

Browse files
use [N x i8] for alloca types
1 parent 58d7036 commit 38bf4f4

27 files changed

+92
-68
lines changed

compiler/rustc_codegen_gcc/src/builder.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,18 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
734734
self.gcc_checked_binop(oop, typ, lhs, rhs)
735735
}
736736

737-
fn alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
737+
fn alloca(&mut self, size: Size, align: Align) -> RValue<'gcc> {
738+
let ty = self.cx.type_array(self.cx.type_i8(), size.bytes()).get_aligned(align.bytes());
739+
// TODO(antoyo): It might be better to return a LValue, but fixing the rustc API is non-trivial.
740+
self.stack_var_count.set(self.stack_var_count.get() + 1);
741+
self.current_func().new_local(None, ty, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
742+
}
743+
744+
fn dynamic_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
745+
unimplemented!();
746+
}
747+
748+
fn typed_alloca(&mut self, ty: Type<'gcc>, align: Align) -> RValue<'gcc> {
738749
// FIXME(antoyo): this check that we don't call get_aligned() a second time on a type.
739750
// Ideally, we shouldn't need to do this check.
740751
let aligned_type =
@@ -749,10 +760,6 @@ impl<'a, 'gcc, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'gcc, 'tcx> {
749760
self.current_func().new_local(None, aligned_type, &format!("stack_var_{}", self.stack_var_count.get())).get_address(None)
750761
}
751762

752-
fn byte_array_alloca(&mut self, _len: RValue<'gcc>, _align: Align) -> RValue<'gcc> {
753-
unimplemented!();
754-
}
755-
756763
fn load(&mut self, pointee_ty: Type<'gcc>, ptr: RValue<'gcc>, align: Align) -> RValue<'gcc> {
757764
let block = self.llbb();
758765
let function = block.get_function();

compiler/rustc_codegen_gcc/src/intrinsic/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ impl<'gcc, 'tcx> ArgAbiExt<'gcc, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
490490
// We instead thus allocate some scratch space...
491491
let scratch_size = cast.size(bx);
492492
let scratch_align = cast.align(bx);
493-
let llscratch = bx.alloca(cast.gcc_type(bx), scratch_align);
493+
let llscratch = bx.alloca(scratch_size, scratch_align);
494494
bx.lifetime_start(llscratch, scratch_size);
495495

496496
// ... 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
@@ -16,7 +16,7 @@ use rustc_middle::span_bug;
1616
use rustc_middle::ty::layout::HasTyCtxt;
1717
use rustc_middle::ty::{self, Ty};
1818
use rustc_span::{sym, Span, Symbol};
19-
use rustc_target::abi::Align;
19+
use rustc_target::abi::{Align, Size};
2020

2121
use crate::builder::Builder;
2222
#[cfg(feature = "master")]
@@ -363,7 +363,7 @@ pub fn generic_simd_intrinsic<'a, 'gcc, 'tcx>(
363363
let ze = bx.zext(result, bx.type_ix(expected_bytes * 8));
364364

365365
// Convert the integer to a byte array
366-
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
366+
let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
367367
bx.store(ze, ptr, Align::ONE);
368368
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
369369
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
@@ -234,7 +234,7 @@ impl<'ll, 'tcx> ArgAbiExt<'ll, 'tcx> for ArgAbi<'tcx, Ty<'tcx>> {
234234
// We instead thus allocate some scratch space...
235235
let scratch_size = cast.size(bx);
236236
let scratch_align = cast.align(bx);
237-
let llscratch = bx.alloca(cast.llvm_type(bx), scratch_align);
237+
let llscratch = bx.alloca(scratch_size, scratch_align);
238238
bx.lifetime_start(llscratch, scratch_size);
239239

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

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,9 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
466466
val
467467
}
468468

469-
fn alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
469+
fn alloca(&mut self, size: Size, align: Align) -> &'ll Value {
470+
let ty = self.cx().type_array(self.cx().type_i8(), size.bytes());
471+
470472
let mut bx = Builder::with_cx(self.cx);
471473
bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
472474
unsafe {
@@ -476,10 +478,20 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
476478
}
477479
}
478480

479-
fn byte_array_alloca(&mut self, len: &'ll Value, align: Align) -> &'ll Value {
481+
fn dynamic_alloca(&mut self, size: &'ll Value, align: Align) -> &'ll Value {
480482
unsafe {
481483
let alloca =
482-
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), len, UNNAMED);
484+
llvm::LLVMBuildArrayAlloca(self.llbuilder, self.cx().type_i8(), size, UNNAMED);
485+
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
486+
alloca
487+
}
488+
}
489+
490+
fn typed_alloca(&mut self, ty: &'ll Type, align: Align) -> &'ll Value {
491+
let mut bx = Builder::with_cx(self.cx);
492+
bx.position_at_start(unsafe { llvm::LLVMGetFirstBasicBlock(self.llfn()) });
493+
unsafe {
494+
let alloca = llvm::LLVMBuildAlloca(bx.llbuilder, ty, UNNAMED);
483495
llvm::LLVMSetAlignment(alloca, align.bytes() as c_uint);
484496
alloca
485497
}

compiler/rustc_codegen_llvm/src/intrinsic.rs

Lines changed: 5 additions & 4 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, symbol::kw, 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;
@@ -565,7 +565,8 @@ fn codegen_msvc_try<'ll>(
565565
//
566566
// More information can be found in libstd's seh.rs implementation.
567567
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
568-
let slot = bx.alloca(bx.type_ptr(), ptr_align);
568+
let ptr_size = bx.tcx().data_layout.pointer_size;
569+
let slot = bx.alloca(ptr_size, ptr_align);
569570
let try_func_ty = bx.type_func(&[bx.type_ptr()], bx.type_void());
570571
bx.invoke(try_func_ty, None, None, try_func, &[data], normal, catchswitch, None);
571572

@@ -838,7 +839,7 @@ fn codegen_emcc_try<'ll>(
838839
let ptr_align = bx.tcx().data_layout.pointer_align.abi;
839840
let i8_align = bx.tcx().data_layout.i8_align.abi;
840841
let catch_data_type = bx.type_struct(&[bx.type_ptr(), bx.type_bool()], false);
841-
let catch_data = bx.alloca(catch_data_type, ptr_align);
842+
let catch_data = bx.typed_alloca(catch_data_type, ptr_align);
842843
let catch_data_0 =
843844
bx.inbounds_gep(catch_data_type, catch_data, &[bx.const_usize(0), bx.const_usize(0)]);
844845
bx.store(ptr, catch_data_0, ptr_align);
@@ -1289,7 +1290,7 @@ fn generic_simd_intrinsic<'ll, 'tcx>(
12891290
let ze = bx.zext(i_, bx.type_ix(expected_bytes * 8));
12901291

12911292
// Convert the integer to a byte array
1292-
let ptr = bx.alloca(bx.type_ix(expected_bytes * 8), Align::ONE);
1293+
let ptr = bx.alloca(Size::from_bytes(expected_bytes), Align::ONE);
12931294
bx.store(ze, ptr, Align::ONE);
12941295
let array_ty = bx.type_array(bx.type_i8(), expected_bytes);
12951296
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
@@ -511,7 +511,7 @@ fn get_argc_argv<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
511511
let param_handle = bx.get_param(0);
512512
let param_system_table = bx.get_param(1);
513513
let arg_argc = bx.const_int(cx.type_isize(), 2);
514-
let arg_argv = bx.alloca(cx.type_array(cx.type_ptr(), 2), Align::ONE);
514+
let arg_argv = bx.typed_alloca(cx.type_array(cx.type_ptr(), 2), Align::ONE);
515515
bx.store(param_handle, arg_argv, Align::ONE);
516516
let arg_argv_el1 = bx.gep(cx.type_ptr(), arg_argv, &[bx.const_int(cx.type_int(), 1)]);
517517
bx.store(param_system_table, arg_argv_el1, Align::ONE);

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
}
@@ -471,7 +471,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
471471
let align_minus_1 = bx.sub(align, one);
472472
let size_extra = bx.add(size, align_minus_1);
473473
let min_align = Align::ONE;
474-
let alloca = bx.byte_array_alloca(size_extra, min_align);
474+
let alloca = bx.dynamic_alloca(size_extra, min_align);
475475
let address = bx.ptrtoint(alloca, bx.type_isize());
476476
let neg_address = bx.neg(address);
477477
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

compiler/rustc_codegen_ssa/src/traits/builder.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ pub trait BuilderMethods<'a, 'tcx>:
141141
}
142142
fn to_immediate_scalar(&mut self, val: Self::Value, scalar: Scalar) -> Self::Value;
143143

144-
fn alloca(&mut self, ty: Self::Type, align: Align) -> Self::Value;
145-
fn byte_array_alloca(&mut self, len: Self::Value, align: Align) -> Self::Value;
144+
/// Used for all fixed-size Rust types.
145+
fn alloca(&mut self, size: Size, align: Align) -> Self::Value;
146+
/// Used for DSTs and unsized locals.
147+
fn dynamic_alloca(&mut self, size: Self::Value, align: Align) -> Self::Value;
148+
/// Should only be used for types without a Rust layout, e.g. C++ EH catch data.
149+
fn typed_alloca(&mut self, ty: Self::Type, align: Align) -> Self::Value;
146150

147151
fn load(&mut self, ty: Self::Type, ptr: Self::Value, align: Align) -> Self::Value;
148152
fn volatile_load(&mut self, ty: Self::Type, ptr: Self::Value) -> Self::Value;

0 commit comments

Comments
 (0)