[llvm] [AMDGPU][X86][DAG] Avoid duplicate BinOp result from narrowing insert-extract sub-vector (PR #201056)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 17 05:12:47 PDT 2026
================
@@ -27457,36 +27498,108 @@ static SDValue narrowInsertExtractVectorBinOp(EVT SubVT, SDValue BinOp,
EVT VecVT = BinOp.getValueType();
SDValue Bop0 = BinOp.getOperand(0), Bop1 = BinOp.getOperand(1);
- if (VecVT != Bop0.getValueType() || VecVT != Bop1.getValueType())
+ if (VecVT != Bop0.getValueType() || VecVT != Bop1.getValueType() ||
+ VecVT.isScalableVT())
return SDValue();
+
+ // This fold only pays off when the wide binop disappears completely, so every
+ // user must be an extract_subvector. Require them all to extract N's type so
+ // a single chain scan serves every extract.
+ EVT SubVT = N->getValueType(0);
+ if (VecVT.getSizeInBits() <= SubVT.getSizeInBits())
+ return SDValue();
+
if (!TLI.isOperationLegalOrCustom(BinOpcode, SubVT, LegalOperations))
return SDValue();
- SDValue Sub0 = getSubVectorSrc(Bop0, Index, SubVT);
- SDValue Sub1 = getSubVectorSrc(Bop1, Index, SubVT);
+ // The wide binop splits into a small, dense set of SubVT-sized slots, so a
+ // flat table indexed by subvector position is all we need.
+ unsigned NumSubElts = SubVT.getVectorMinNumElements();
+ unsigned NumParts = VecVT.getVectorMinNumElements() / NumSubElts;
+ SmallVector<std::tuple<SDNode *, SDValue, SDValue>, 4> Slots(NumParts);
+ // Scan each wide operand's chain once, filling each slot's source.
+ collectSubVectorSrcs(Bop0, SubVT, /*OpNo=*/0, Slots);
+ collectSubVectorSrcs(Bop1, SubVT, /*OpNo=*/1, Slots);
+
+ bool HasNonZeroExt = false;
+ bool HasNonExtUser = false;
+ bool AllExtractsCheap = true;
+ for (SDNode *User : BinOp->users()) {
+ if (User->getOpcode() != ISD::EXTRACT_SUBVECTOR) {
+ HasNonExtUser = true;
+ continue;
+ }
+ if (User->getValueType(0) != SubVT ||
+ (User->getCombinerWorklistIndex() < 0 && User != N))
+ return SDValue();
+ unsigned Idx = User->getConstantOperandVal(1);
+ if (Idx % NumSubElts != 0 || Idx / NumSubElts >= NumParts)
+ return SDValue();
+ auto &[ExtSubVec, Sub0, Sub1] = Slots[Idx / NumSubElts];
+ if (!Sub0 || !Sub1)
+ return SDValue();
+ if (!ExtSubVec) {
+ ExtSubVec = User;
+ AllExtractsCheap &= TLI.isExtractSubvectorCheap(SubVT, VecVT, Idx);
+ if (Idx != 0)
+ HasNonZeroExt = true;
+ } else {
+ llvm_unreachable("Duplicate extract subvector");
+ }
+ }
- // TODO: We could handle the case where only 1 operand is being inserted by
- // creating an extract of the other operand, but that requires checking
- // number of uses and/or costs.
- if (!Sub0 || !Sub1)
+ if (TLI.isTypeLegal(VecVT) && AllExtractsCheap &&
+ !TLI.isNarrowingProfitable(BinOp.getNode(), VecVT, SubVT))
----------------
arsenm wrote:
I still don't think isNarrowingProfitable should be repurposed for vector element trimming
https://github.com/llvm/llvm-project/pull/201056
More information about the llvm-commits
mailing list