🚧 Infer / propagate alignment attributes #21991
Draft
+296
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a work in progress PR that propagates alignment attributes.
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
Computing the new address looks like the following
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:
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.