Paul Vario
2014-Feb-18 01:29 UTC
[LLVMdev] 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140217/249b89be/attachment.html>
Owen Anderson
2014-Feb-18 02:19 UTC
[LLVMdev] How to codegen an LLVM-IR that has dynamic arrays in it?
Call malloc()? —Owen On Feb 17, 2014, at 5:29 PM, Paul Vario <paul.paul.mit at gmail.com> wrote:> 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 > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Tim Northover
2014-Feb-18 10:24 UTC
[LLVMdev] How to codegen an LLVM-IR that has dynamic arrays in it?
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.
Paul Vario
2014-Feb-19 23:19 UTC
[LLVMdev] How to codegen an LLVM-IR that has dynamic arrays in it?
I see. Problem solved! Thank you all for the clarification. Best, Paul On Tue, Feb 18, 2014 at 4:24 AM, Tim Northover <t.p.northover at gmail.com>wrote:> 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. >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140219/bc3fdb8a/attachment.html>
Reasonably Related Threads
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] Adding function call in LLVM IR using IRBuilder causes assertion error
- [LLVMdev] Retrieving Underlying Type from AllocaInst
- [LLVMdev] LLVM Type Int32Ty Problems & LLVMContextImpl.h Problems