Skip to content
Permalink
Browse files

Do not generate allocations for zero sized allocations

  • Loading branch information...
oli-obk committed Aug 16, 2019
1 parent f7af19c commit 1fb854a0f5ec500414f7e4fd4eac30357a9dd666
Showing with 13 additions and 8 deletions.
  1. +13 −8 src/librustc_codegen_llvm/common.rs
@@ -333,14 +333,19 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
offset: Size,
) -> PlaceRef<'tcx, &'ll Value> {
assert_eq!(alloc.align, layout.align.abi);
let init = const_alloc_to_llvm(self, alloc);
let base_addr = self.static_addr_of(init, alloc.align, None);

let llval = unsafe { llvm::LLVMConstInBoundsGEP(
self.const_bitcast(base_addr, self.type_i8p()),
&self.const_usize(offset.bytes()),
1,
)};
let llval = if layout.size == Size::ZERO {
let llval = self.const_usize(alloc.align.bytes());
unsafe { llvm::LLVMConstIntToPtr(llval, self.type_ptr_to(self.type_i8p())) }
} else {
let init = const_alloc_to_llvm(self, alloc);
let base_addr = self.static_addr_of(init, alloc.align, None);

unsafe { llvm::LLVMConstInBoundsGEP(
self.const_bitcast(base_addr, self.type_i8p()),
&self.const_usize(offset.bytes()),
1,
)}
};
let llval = self.const_bitcast(llval, self.type_ptr_to(layout.llvm_type(self)));
PlaceRef::new_sized(llval, layout, alloc.align)
}

0 comments on commit 1fb854a

Please sign in to comment.
You can’t perform that action at this time.