Motivation
This RFC proposes extending the alignment attribute to data sizes smaller than a byte. Types and data smaller than a byte are already being used in several dialects (e.g., vector, amdgpu, rocdl, spirv), in passes to emulate these types (–test-emulate-narrow-int), and being used in several targets. To support alignment for data sizes smaller than a byte, we propose changing the byte-alignment attribute to be a bit-alignment attribute.
Byte Alignment
We say that some data is n-byte aligned when it is stored at an address a and a is a multiple of n where n is also a power of two.
Some targets require data of certain size to be n-byte aligned. When loading or storing data of these sizes from or to addresses which are not a multiple of n, these targets may result in a performance penalty or an alignment fault. The alignment attribute is useful to allow compilers to properly meet alignment requirements to optimize and generate valid programs.
Bit Alignment
All byte-alignment attributes can already be expressed in terms of bit-alignment. The conversion of a byte-alignment attribute to a bit-alignment attribute is just the byte-alignment attribute multiplied by eight.
Element Alignment
An alternative to bit-alignment that would allow for sub-byte data to be annotated with an alignment attribute is to have the alignment be defined in units of the number of elements.
E.g., For a vector <N x i4> with element-align 2, then the bit alignment is 8, and so the byte alignment is 1.
Table of Pros and Cons
| Byte Alignment | Bit Alignment | Element Alignment | |
|---|---|---|---|
| Pros | Status Quo is preserved | Optimized lowerings for vector dialect. Easy conversion between byte and bit alignment. | Optimized lowerings for vector dialect. |
| Cons | No optimized lowerings for vector dialect | Not so easy conversion between byte and element alignment. |
Next Steps
- The LLVM dialect’s byte-aligned attributes remain unchanged and dialects lowering to the LLVM dialect will need to reconcile bit-alignment attributes that do not directly correspond to a byte-aligned attribute.
- Vector’s and MemRef’s lowerings will use these alignment attributes to determine the alignment instead of alignment attributes determined by the types. Currently, the lowerings of vector.load and vector.store to LLVM always assume that the pointer is aligned to the natural alignment of the vector element, not the vector. This is behavior users often want to override, either in general or on a case-by-case basis, and having an alignment attribute lets people do that.
- Alignment attributes should be added to other operations in the vector dialect.