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

Hi Fellows,

Is there a way to allocate dynamic array within an LLVM IR file from my code generator? Say., how to create an array type with a size determined through a global variable. Symbolically, something like below:

Value *sz = Mod->getOrInsertGlobal(“SIZE”, Int32Ty);

Type *ArrayTy = ArrayType::get(FloatTy, sz)

AllocaInst *AI = CreateAlloca(ArrayTy, 0, “”);

Thanks,
Paul

Call malloc()?

—Owen

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.

I see. Problem solved! Thank you all for the clarification.

Best,
Paul