[LLVMdev] How to codegen an LLVM-IR that has dynamic arrays in it?

Tim Northover t.p.northover at gmail.com
Tue Feb 18 02:24:31 PST 2014


Hi Paul,

>                     Value *sz = Mod->getOrInsertGlobal("SIZE", Int32Ty);
>                     Type *ArrayTy = ArrayType::get(FloatTy, sz)
>                     AllocaInst *AI = CreateAlloca(ArrayTy, 0, "");

You can't create dynamically varying types like that in LLVM, but an
AllocaInst can have a parameter saying how many elements to allocate,
so instead of trying to write:

   %numElts = load i32* @SIZE
   %val = alloca [@var x float] ; %val would have dynamic type
[%numElts x float]*. Not good.

You'd want:
    %numElts = load i32* @SIZE
    %val = alloca float, i32 %numElts ; %val now has type float*, but
%numElts elements can be accessed.

Roughly the same would happen if you took Owen up on using malloc
(%val would have to have type "float*" rather than a static array
type).

Cheers.

Tim.



More information about the llvm-dev mailing list