Skip to content

Commit

Permalink
[LegalizeDAG] Propagate alignment in ExpandExtractFromVectorThroughStack
Browse files Browse the repository at this point in the history
Unlike the name suggests this can reuse any store as a base for a
memory-based vector extract. If that store is underaligned the loads
created to extract will have an invalid alignment. Since most CPUs are
forgiving wrt alignment this is almost never an issue, on x86 this is
only reproducible by extracting a 128 bit vector out of a wider vector.

I tried making a test case in the context of
https://reviews.llvm.org/D127982 but it's really really fragile, as the
output pretty much looks like a missed optimization.
  • Loading branch information
d0k committed Jul 19, 2022
1 parent 6cb9529 commit 8aff88f
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp
Expand Up @@ -1404,17 +1404,21 @@ SDValue SelectionDAGLegalize::ExpandExtractFromVectorThroughStack(SDValue Op) {
}

SDValue NewLoad;
Align ElementAlignment =
std::min(cast<StoreSDNode>(Ch)->getAlign(),
DAG.getDataLayout().getPrefTypeAlign(
Op.getValueType().getTypeForEVT(*DAG.getContext())));

if (Op.getValueType().isVector()) {
StackPtr = TLI.getVectorSubVecPointer(DAG, StackPtr, VecVT,
Op.getValueType(), Idx);
NewLoad =
DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr, MachinePointerInfo());
NewLoad = DAG.getLoad(Op.getValueType(), dl, Ch, StackPtr,
MachinePointerInfo(), ElementAlignment);
} else {
StackPtr = TLI.getVectorElementPointer(DAG, StackPtr, VecVT, Idx);
NewLoad = DAG.getExtLoad(ISD::EXTLOAD, dl, Op.getValueType(), Ch, StackPtr,
MachinePointerInfo(),
VecVT.getVectorElementType());
MachinePointerInfo(), VecVT.getVectorElementType(),
ElementAlignment);
}

// Replace the chain going out of the store, by the one out of the load.
Expand Down

0 comments on commit 8aff88f

Please sign in to comment.