Skip to content

Conversation

amd-eochoalo
Copy link
Contributor

This is a work in progress PR that propagates alignment attributes.

%memref = memref.alloc() { alignment = 64 : i64 } : memref<8xf32>
// … to …
%vector = vector.load %memref[%cst0] : memref<8xf32> into vector<8xf32>

This is limited to the index in the vector.load operation and the definition of the base being annotated with alignment.

For the general case

%vector = vector.load %memref[%dyn, %dyn, %cst0, %cst16, %dyn, %cst0] : … into vector…

Computing the new address looks like the following

// pseudocode
// indices here correspond to the dimension being accessed. 
%new_address = %memref + %cst0 * strides_in_bytes[0] + %dyn * strides_in_bytes[1]  + %cst_16 * strides_in_bytes[2] …

Then I use the following:

If c | a (read a is divisible by c) and c | b, then c | (k_0a + k_1b) (read c divides any linear combination of a and b)

That looks a lot like the formula above for computing the new address. Maybe we can change into:

%new_address = %memref/alignment * alignment + %cst0 * strides_in_bytes[0] + %dyn * strides_in_bytes[1]  + %cst_16 * strides_in_bytes[2] …

And now what we are interested in finding is the greatest common denominator (which corresponds to the greatest possible alignment) which is also a power of two.

For gcd(a, b) = c => c | a and c | b then for all constant indices we can get the gcd for the concrete offset and unknown indices the gcd of the strides in bytes

C = gcd(alignment, %cst * strides_in_bytes[i], …, strides_in_bytes[1], …)

Then C is the gcd for all accesses. C is not guaranteed to be a power of two, but we just need to find out the highest power level of two that is a divisor of C and that is our alignment.

This also shows that in case when we don’t know the inner-most dimension, the best alignment we can possibly have is just the size of the element type in bytes, which matches reality.

@amd-eochoalo amd-eochoalo requested a review from kuhar September 15, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant