[llvm] [LoadStoreVectorizer] Propagate alignment through contiguous chain (PR #145733)
Artem Belevich via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 10 10:59:07 PDT 2025
================
@@ -1634,3 +1638,32 @@ std::optional<APInt> Vectorizer::getConstantOffset(Value *PtrA, Value *PtrB,
.sextOrTrunc(OrigBitWidth);
return std::nullopt;
}
+
+void Vectorizer::propagateBestAlignmentsInChain(ArrayRef<ChainElem> C) const {
+ auto PropagateAlignments = [](auto ChainIt) {
+ ChainElem BestAlignedElem = *ChainIt.begin();
+ Align BestAlignSoFar = getLoadStoreAlignment(BestAlignedElem.Inst);
+
+ for (const ChainElem &E : ChainIt) {
+ Align OrigAlign = getLoadStoreAlignment(E.Inst);
+ if (OrigAlign > BestAlignSoFar) {
+ BestAlignedElem = E;
+ BestAlignSoFar = OrigAlign;
+ continue;
+ }
+
+ APInt DeltaFromBestAlignedElem =
+ APIntOps::abdu(E.OffsetFromLeader, BestAlignedElem.OffsetFromLeader);
+ // commonAlignment is equivalent to a greatest common power-of-two
+ // divisor; it returns the largest power of 2 that divides both A and B.
+ Align NewAlign = commonAlignment(
+ BestAlignSoFar, DeltaFromBestAlignedElem.getLimitedValue());
+ if (NewAlign > OrigAlign)
+ setLoadStoreAlignment(E.Inst, NewAlign);
+ }
+ };
+
+ // Propagate forwards and backwards.
+ PropagateAlignments(C);
+ PropagateAlignments(reverse(C));
----------------
Artem-B wrote:
Thank you for the explanation.
So, this case, the combination of forward and backward propagation is equivalent to finding the largest alignment at a known offset, and then applying that known alignment to other operations in the chain. Forward pass applies alignment to the operations from the max alignment point to the subsequent operations, and the reverse pass will apply it to the preceding ones.
We may be doing somewhat unnecessary work (we will be applying smaller alignment until we reach the max point and we'll eventually apply the max alignment on the reverse pass), but it should be valid.
Perhaps it would make sense to separate the find the max alignment from upgrading the alignment. Finding max alignment may be constrained on finding the large-enough value, so we can stop the search early, as we don't need alignments larger than the size of the largest aligned load. It also makes the forward/backward distinction moot.
https://github.com/llvm/llvm-project/pull/145733
More information about the llvm-commits
mailing list