Alexander Poddey
2015-May-26 18:16 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
Hi all, I'm still struggling with getElementPtr. I have a global variable (array of doubles). I can use ConstantExpr::getGetElementPtr(myGlobalVariable, indices) and use the result as argument of e.g. a function. I read the GEP docu and know that I need anextra leading '0' index to dereference the global's pointer. So far so good. But I can not succeed in using the getGetElementPtr result in constructing an initalizer for another global value (which expects a ConstantFP* and not a ConstantExpr*). I guess I have a missconception, but I can't figure it out... Alex
Tim Northover
2015-May-26 20:36 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
> But I can not succeed in using the getGetElementPtr result in constructing > an initalizer for another global value (which expects a ConstantFP* and not > a ConstantExpr*).The result of a Constant GEP is going to be the address of a floating-point value rather than the value itself, so you can't use it to initialise something that expects a float. For example: @arr = constant [2 x float] [float 0.0, float 1.0] @bad = global float getelementptr([2 x float], [2 x float]* @arr, i32 0, i32 1) @good = global float* getelementptr([2 x float], [2 x float]* @arr, i32 0, i32 1) LLVM doesn't really do value aliases, so you'd usually just write "@bad = global float 1.0" for the second line. You might extract that 1.0 value by looking into the @arr definition directly using the LLVM API -- if you can't do that (say because it's not a compile-time constant) then the chances are what you're trying to write wouldn't be valid anyway. Cheers. Tim.
Alexander Poddey
2015-May-27 03:56 UTC
[LLVMdev] convert GetElemtPtr result to pointer on element?
Hi Tim, I forgot to say that I try to do all this in the LLVM API, not IR. I read in a bc'ed program and try to edit it using the API. Alex Tim Northover wrote:>> But I can not succeed in using the getGetElementPtr result in >> constructing an initalizer for another global value (which expects a >> ConstantFP* and not a ConstantExpr*). > > The result of a Constant GEP is going to be the address of a > floating-point value rather than the value itself, so you can't use it > to initialise something that expects a float. For example: > > @arr = constant [2 x float] [float 0.0, float 1.0] > @bad = global float getelementptr([2 x float], [2 x float]* @arr, > i32 0, i32 1) > @good = global float* getelementptr([2 x float], [2 x float]* > @arr, i32 0, i32 1) > > LLVM doesn't really do value aliases, so you'd usually just write > "@bad = global float 1.0" for the second line. You might extract that > 1.0 value by looking into the @arr definition directly using the LLVM > API -- if you can't do that (say because it's not a compile-time > constant) then the chances are what you're trying to write wouldn't be > valid anyway. > > Cheers. > > Tim.
Apparently Analagous Threads
- [LLVMdev] copy value of a global's data field to another global
- [LLVMdev] convert GetElemtPtr result to pointer on element?
- [LLVMdev] copy value of a global's data field to another global
- [LLVMdev] casting Constant * to value *?
- [LLVMdev] array of pointers