[llvm] [X86] Add combineTruncateOfConcat and simplify EltsFromConsecutiveLoads strided truncation (PR #208869)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 12 23:39:16 PDT 2026


================
@@ -55868,6 +55844,70 @@ static SDValue detectPMADDUBSW(SDValue In, EVT VT, SelectionDAG &DAG,
                           PMADDBuilder);
 }
 
+// Fold trunc(concat(trunc(x0), trunc(x1), ...)) -> shuffle(vtrunc(x0), ...).
+// A truncate of the concat narrows twice. VTRUNC narrows each source straight
+// to its final sub-128-bit width leaving only the shuffles.
+static SDValue combineTruncateOfConcat(SDValue Src, EVT VT, const SDLoc &DL,
+                                       SelectionDAG &DAG,
+                                       const X86Subtarget &Subtarget) {
+  if (!Subtarget.hasAVX512() || !VT.isVector() || !Src.hasOneUse())
+    return SDValue();
+
+  SmallVector<SDValue, 4> Ops;
+  if (!collectConcatOps(Src.getNode(), Ops, DAG) || Ops.size() < 2 ||
+      !isPowerOf2_32(Ops.size()))
+    return SDValue();
+
+  // Every piece must be a truncate from one common source type.
+  using namespace SDPatternMatch;
+  EVT InVT = Ops[0].getOperand(0).getValueType();
+  if (!all_of(Ops, [InVT](SDValue Op) {
+        return sd_match(Op, m_OneUse(m_Trunc(m_SpecificVT(InVT))));
+      }))
+    return SDValue();
+
+  // VTRUNC writes to the low lanes of an xmm, so it only helps when a piece is
+  // narrower than that. It needs a 512-bit source (or 256 with VLX), and BWI
+  // for the i16 to i8 form.
+  unsigned InBits = InVT.getSizeInBits();
+  unsigned PieceLanes = InVT.getVectorNumElements();
+  MVT DstEltVT = MVT::getIntegerVT(VT.getScalarSizeInBits());
+  unsigned TruncDstLanes = 128 / DstEltVT.getSizeInBits();
+  if (PieceLanes >= TruncDstLanes ||
+      (InBits != 512 && !(InBits == 256 && Subtarget.hasVLX())) ||
+      (InVT.getScalarSizeInBits() == 16 && !Subtarget.hasBWI()))
+    return SDValue();
+
+  MVT TruncDstVT = MVT::getVectorVT(DstEltVT, TruncDstLanes);
+  SmallVector<SDValue, 8> Pieces;
+  for (SDValue Op : Ops)
+    Pieces.push_back(
+        DAG.getNode(X86ISD::VTRUNC, DL, TruncDstVT, Op.getOperand(0)));
+
+  // Pairwise shuffle the low halves until each piece is full.
+  unsigned GoodLanes = PieceLanes;
+  while (Pieces.size() > 1 && GoodLanes < TruncDstLanes) {
+    assert((TruncDstLanes % GoodLanes) == 0 && "Illegal VTRUNC packing");
+    SmallVector<SDValue, 8> Next;
+    SmallVector<int, 16> Mask(TruncDstLanes, -1);
+    for (unsigned I = 0; I != GoodLanes; ++I) {
+      Mask[I] = I;
+      Mask[GoodLanes + I] = TruncDstLanes + I;
+    }
+    for (unsigned J = 0, E = Pieces.size() - 1; J < E; J += 2)
+      Next.push_back(
+          DAG.getVectorShuffle(TruncDstVT, DL, Pieces[J], Pieces[J + 1], Mask));
+    Pieces = std::move(Next);
+    GoodLanes *= 2;
+  }
+  if (Pieces.size() == 1)
----------------
RKSimon wrote:

Technically we don't need this fast-path as getNode(ISD::CONCAT_VECTORS) checks for single operand concats.

https://github.com/llvm/llvm-project/pull/208869


More information about the llvm-commits mailing list