clspv icon indicating copy to clipboard operation
clspv copied to clipboard

long vector alignment

Open rjodinchr opened this issue 3 years ago • 3 comments

Long vectors (vectors of 8 or 16 elements) are not natively supported in SPIRV. To support them, we have a pass in clspv lowering them into a different form supported in SPIRV.

At the beginning we used to lower them into a struct of 8 or 16 scalar elements. But this solution prevents llvm from being able to access an element at a dynamic index.

So we changed it to be an array of 8 or 16 scalar elements. It worked pretty well for the previous issue. But I recently realized that it does not keep the same alignment as the one of the long vectors, which is problematic.

A solution could be to lower long vectors into a struct of one array of 8 or 16 scalar elements. I think llvm should still be able to access a dynamic index, and it should keep the alignment of the original long vector.

rjodinchr avatar Jul 28 '22 09:07 rjodinchr

Lowering long vectors to struct of one array does not work as the alignment of the structure is the alignment of its component having the bigger alignment.

It means that we need to do it "by hand". I have a mock up that is taking care of the structure in the long vectors pass. I also need to modify the clusterconstant pass, as the long vectors does not appear there anymore, but still, we need to keep the right alignment (maybe other pass needs to rework for that as well, clusterpodargs?).

Finally I have an issue with the ArrayStride in the generated SPIRV code with packed structures. Here is an example:

typedef struct __attribute__ ((packed)) myPackedStruct {
    char2 vec;
    char cPost;
} testStruct;

My only idea to support those at the moment, is to lower all vectors inside packed structures into arrays.

rjodinchr avatar Aug 03 '22 16:08 rjodinchr

here is an even harder structure to have packed with correct stride and alignment:

typedef struct __attribute__ ((packed)) myPackedStruct { 
    short2 vec;
    char cPost;
} testStruct;

rjodinchr avatar Aug 03 '22 17:08 rjodinchr

The alignment of long vectors should be fixed by #906 and #907. For the packing issue, I will open a new issue

rjodinchr avatar Aug 08 '22 12:08 rjodinchr