Skip to content

Commit 1fb854a

Browse files
committed
Do not generate allocations for zero sized allocations
1 parent f7af19c commit 1fb854a

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/librustc_codegen_llvm/common.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,19 @@ impl ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
333333
offset: Size,
334334
) -> PlaceRef<'tcx, &'ll Value> {
335335
assert_eq!(alloc.align, layout.align.abi);
336-
let init = const_alloc_to_llvm(self, alloc);
337-
let base_addr = self.static_addr_of(init, alloc.align, None);
338-
339-
let llval = unsafe { llvm::LLVMConstInBoundsGEP(
340-
self.const_bitcast(base_addr, self.type_i8p()),
341-
&self.const_usize(offset.bytes()),
342-
1,
343-
)};
336+
let llval = if layout.size == Size::ZERO {
337+
let llval = self.const_usize(alloc.align.bytes());
338+
unsafe { llvm::LLVMConstIntToPtr(llval, self.type_ptr_to(self.type_i8p())) }
339+
} else {
340+
let init = const_alloc_to_llvm(self, alloc);
341+
let base_addr = self.static_addr_of(init, alloc.align, None);
342+
343+
unsafe { llvm::LLVMConstInBoundsGEP(
344+
self.const_bitcast(base_addr, self.type_i8p()),
345+
&self.const_usize(offset.bytes()),
346+
1,
347+
)}
348+
};
344349
let llval = self.const_bitcast(llval, self.type_ptr_to(layout.llvm_type(self)));
345350
PlaceRef::new_sized(llval, layout, alloc.align)
346351
}

0 commit comments

Comments
 (0)