Question

I have an array and a vector, and both of them hold the same data, like 0, 1, 2, 3, 4. Then I use GEP to get the ptr of array,

%0 = getelementptr [5 x i32]* %arr, i32 0, i32 3
%1 = load i32* %0

so, %0 is the pointer of the 4th element in the array pointed by %arr, and the value of %1 is 3.

But now, I bitcast the pointer to the vector into a pointer to i32:

%2 = bitcast <5 x i32>* %Vec to i32*

and:

%3 = getelementptr i32* %2, i32 3
%4 = load i32* %3

I don't know exactly if there is any difference of the layout in the memory between array and vector. If there is no difference, I think that way to get the element from a vector is ok. So, am I on the right way to do like that?

Was it helpful?

Solution 2

You can use a bitcast followed by a gep to get the 4th item in a vector, but it's redundant - you can just use a gep by itself, in exactly the same way as you have done with the array.

When using gep, you don't need to know anything about the memory layouts. In any case the memory for vectors is always laid out sequentially, as can be inferred from how bitcast behaves between vectors and integers; and since you can't bitcast arrays, their memory layout is immaterial.

OTHER TIPS

According to "The Often Misunderstood GEP Instruction" (http://llvm.org/docs/GetElementPtr.html) question "Can GEP index into vector elements?", "This hasn’t always been forcefully disallowed, though it’s not recommended. It leads to awkward special cases in the optimizers, and fundamental inconsistency in the IR. In the future, it will probably be outright disallowed."

So it's probably not a good idea to use GEP against vectors, but it's doable.

And in http://llvm.org/docs/doxygen/html/classllvm_1_1SequentialType.html, it says "All of these represent "arrays" in memory. The array type represents a specifically sized array, pointer types are unsized/unknown size arrays, vector types represent specifically sized arrays that allow for use of SIMD instructions. "

So it's better to decide whether or not vector is desired. If it does be the case, then 'extractelement' instruction is probably better (http://llvm.org/docs/LangRef.html#extractelement-instruction).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top