gongweixue gongweixue - 2 months ago 17
C++ Question

Any difference between vector and array in memory layout of LLVM?

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*


%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?

Oak Oak

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.