How to create vector pointer type?


Say the original type is Integer i16*, I want to create a v16i16* type to replace it.

static Type *getVectorPtr(Type *Ty) {
PointerType *PointerTy = dyn_cast(Ty);
assert(PointerTy && “PointerType expected”);

unsigned addSpace = PointerTy->getAddressSpace();
Type *ScalarType = PointerTy->getElementType();
VectorType *VectorType = VectorType::get(ScalarType, 16);
return PointerType::get(VectorType, addSpace);

There’s no isVectorPtr() check, so I add

static bool isVectorPtr(Type *Ty) {
bool a = false;
if(Ty->isPointerTy() ){
PointerType *PointerTy = dyn_cast(Ty);
Type *vt = PointerTy->getElementType();
a = vt->isVectorTy();
return a;

Then meet the following error:

Assertion failed: ResultElementType == cast(getType()->getScalarType())->getElementType(), file D:\cygwin64\home\celine\clang-102\llvm\include\llvm/IR/Instructions.h, line 958

I wonder how to create vector pointer type correctly?

Hi Celine,

Then meet the following error:
Assertion failed: ResultElementType == cast<PointerType>(getType()->getScalarType())->getElementType(), file D:\cygwin64\home\celine\clang-102\llvm\include\llvm/IR/Instructions.h, line 958

Both of your functions look sensible to me, but this assertion is from
within GetElementPtrInst, so it's happening after you've done
something with these types. You probably replaced a Value with a
different one with this new type (or mutated it in place). That leaves
the IR in an inconsistent state E.g. one operand of an add could be
i16 and the other i32, or in this case a GEP thinks it's operating on
an i16* to produce another i16*, but it's actually operating on a <16
x i16>*, which would produce another <16 x i16>*.

If you're trying to change the type of a Value you generally have to
recreate its whole chain of users until you can reunify the types. If
this is vectorizing a loop, for example, the whole loop data-path
would probably need to be recreated with the new vector type, and
surrounded by pointer bitcasts ot <16 x i16>* at the beginning (and
back to i16* at the end if anyone cares about the final pointer).



Thank you very much for this detail information~