Opaque pointers and LLVM alloca


The LLVM documentation mentions example alloca instructions:
%ptr = alloca i32 ; yields i32*:ptr
%ptr = alloca i32, i32 4 ; yields i32*:ptr
%ptr = alloca i32, i32 4, align 1024 ; yields i32*:ptr
%ptr = alloca i32, align 1024 ; yields i32*:ptr

With opaque pointers, surely one does not need any types associated with alloca.
One might want to know how many bytes to allocate, address space, and alignment, but I don’t see the need for the type?

Kind Regards


It’s really a convenience, in effect. Much of the time you only want to allocate enough memory to hold a single instance of a given type; in this case, alloca is already specified the way you want, since you can just give the type. Other times you might want an arbitrarily-sized allocation of bytes; in this case, you can give i8 as the type and a count of whatever size you want.

In other words, the type is sometimes useful but you are by no means restricted by it.

It seems feasible to use alloca i8, i8 {numbytes}, align {align} for all fixed-size alloca.
(Though, I’m not sure that will be optimal in practice, currently, since there are a bunch of calls to AllocaInst::getAllocatedType. Someone should perhaps double-check whether all of those are only required for non-opaque-pointers support.)

However, we do also support alloca of scalable vector types e.g. alloca <vscale x 2 x i64>, which can’t be represented as a constant number of bytes.