I'm trying to create a global variable initialized to zero, and return its value from a newly created function, in JIT context. I'm keeping all types as i32 for the moment, and I only have the one module object. This is the code I have for creating the global variable: const Type *type = Type::getInt32Ty(getGlobalContext()); // Constant *zerov = Constant::getNullValue(type); Constant *zerov = Constant::getIntegerValue(type, APInt(32, 0)); V = new GlobalVariable(getGlobalContext(), type, 0, GlobalValue::PrivateLinkage, zerov, name); And these are the error messages given by verifyFunction: Function return type does not match operand type of return inst! ret i32* @x i32Referencing global in another module! ret i32* @xBroken module found, compilation aborted! Any idea of what I might be doing wrong? Is this the right way to be going about it in the first place?
You have not specified/attached the module. Use: GlobalVariable::GlobalVariable (Module &, const Type *, bool, LinkageTypes, Constant *, const Twine &, GlobalVariable*InsertBefore=0, bool ThreadLocal=false, unsigned AddressSpace=0) as described in: http://llvm.org/doxygen/classllvm_1_1GlobalVariable.html Garrison On Feb 16, 2010, at 6:26, Russell Wallace wrote:> I'm trying to create a global variable initialized to zero, and return > its value from a newly created function, in JIT context. I'm keeping > all types as i32 for the moment, and I only have the one module > object. > > This is the code I have for creating the global variable: > > const Type *type = Type::getInt32Ty(getGlobalContext()); > // Constant *zerov = Constant::getNullValue(type); > Constant *zerov = Constant::getIntegerValue(type, APInt(32, 0)); > V = new GlobalVariable(getGlobalContext(), type, 0, > GlobalValue::PrivateLinkage, zerov, name); > > And these are the error messages given by verifyFunction: > > Function return type does not match operand type of return inst! > ret i32* @x i32Referencing global in another module! > ret i32* @xBroken module found, compilation aborted! > > Any idea of what I might be doing wrong? Is this the right way to be > going about it in the first place? > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Victor Zverovich
2010-Feb-16 13:09 UTC
[LLVMdev] Creating a global variable in JIT context
Do you use load to get the value of V? If not then you probably return i32* instead of i32. Victor On 16 February 2010 11:26, Russell Wallace <russell.wallace at gmail.com>wrote:> I'm trying to create a global variable initialized to zero, and return > its value from a newly created function, in JIT context. I'm keeping > all types as i32 for the moment, and I only have the one module > object. > > This is the code I have for creating the global variable: > > const Type *type = Type::getInt32Ty(getGlobalContext()); > // Constant *zerov = Constant::getNullValue(type); > Constant *zerov = Constant::getIntegerValue(type, APInt(32, 0)); > V = new GlobalVariable(getGlobalContext(), type, 0, > GlobalValue::PrivateLinkage, zerov, name); > > And these are the error messages given by verifyFunction: > > Function return type does not match operand type of return inst! > ret i32* @x i32Referencing global in another module! > ret i32* @xBroken module found, compilation aborted! > > Any idea of what I might be doing wrong? Is this the right way to be > going about it in the first place? > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20100216/f3495c34/attachment.html>
Yes, the module specification and load instruction were exactly the things I was missing. Thanks! On Tue, Feb 16, 2010 at 1:09 PM, Victor Zverovich <victor.zverovich at googlemail.com> wrote:> Do you use load to get the value of V? If not then you probably return i32* > instead of i32. > Victor > On 16 February 2010 11:26, Russell Wallace <russell.wallace at gmail.com> > wrote: >> >> I'm trying to create a global variable initialized to zero, and return >> its value from a newly created function, in JIT context. I'm keeping >> all types as i32 for the moment, and I only have the one module >> object. >> >> This is the code I have for creating the global variable: >> >> const Type *type = Type::getInt32Ty(getGlobalContext()); >> // Constant *zerov = Constant::getNullValue(type); >> Constant *zerov = Constant::getIntegerValue(type, APInt(32, 0)); >> V = new GlobalVariable(getGlobalContext(), type, 0, >> GlobalValue::PrivateLinkage, zerov, name); >> >> And these are the error messages given by verifyFunction: >> >> Function return type does not match operand type of return inst! >> ret i32* @x i32Referencing global in another module! >> ret i32* @xBroken module found, compilation aborted! >> >> Any idea of what I might be doing wrong? Is this the right way to be >> going about it in the first place? >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > >