How allocate a multi-dimensional array

Hello,
I’m trying to allocate a multi-dimensional array with LLVM API. I need to generate something like this:
%a = alloca [4 x [2 x [3 x i32]]], align 16

I can allocate a one-dimension array with the CreateAlloca method, but I can’t figure out how to allocate a multi-dimensional one.

Thanks

CreateAlloca takes a type, which can itself be an ArrayType. What’s probably confusing you is that there’s also a separate ArraySize parameter, but that’s for alloca type, i64 N, align A, which is equivalent to alloca [type x N], align A when N is a constant but also lets you use a non-constant value (in order to implement variable-length arrays) for the outermost dimension.

Thank you! Yes, the ArraySize was a bit confusing. Nesting the ArraySize calls works.

llvm::Type* arrayType = getTypeFromDataType(dt);
for (auto szIt = dt.arraySize.rbegin(); szIt != dt.arraySize.rend(); ++szIt)
{
	arrayType = llvm::ArrayType::get(arrayType, *szIt);
}
alloca = tempBuilder.CreateAlloca(arrayType, nullptr, useNames ? varName : "");

Thanks
Alan