@@ -1623,27 +1623,7 @@ struct ChainedReduction final : OpRewritePattern<vector::ReductionOp> {
1623
1623
}
1624
1624
};
1625
1625
1626
- // Scalable unit dimensions are not supported. Folding such dimensions would
1627
- // require "shifting" the scalable flag onto some other fixed-width dim (e.g.
1628
- // vector<[1]x4xf32> -> vector<[4]xf32>). This could be implemented in the
1629
- // future.
1630
- static VectorType dropNonScalableUnitDimFromType (VectorType inVecTy) {
1631
- auto inVecShape = inVecTy.getShape ();
1632
- SmallVector<int64_t > newShape;
1633
- SmallVector<bool > newScalableDims;
1634
- for (auto [dim, isScalable] :
1635
- llvm::zip_equal (inVecShape, inVecTy.getScalableDims ())) {
1636
- if (dim == 1 && !isScalable)
1637
- continue ;
1638
-
1639
- newShape.push_back (dim);
1640
- newScalableDims.push_back (isScalable);
1641
- }
1642
-
1643
- return VectorType::get (newShape, inVecTy.getElementType (), newScalableDims);
1644
- }
1645
-
1646
- // / For vectors with at least an unit dim, replaces:
1626
+ // / For vectors with either leading or trailing unit dim, replaces:
1647
1627
// / elementwise(a, b)
1648
1628
// / with:
1649
1629
// / sc_a = shape_cast(a)
@@ -1655,16 +1635,20 @@ static VectorType dropNonScalableUnitDimFromType(VectorType inVecTy) {
1655
1635
// / required to be rank > 1.
1656
1636
// /
1657
1637
// / Ex:
1638
+ // / ```
1658
1639
// / %mul = arith.mulf %B_row, %A_row : vector<1x[4]xf32>
1659
1640
// / %cast = vector.shape_cast %mul : vector<1x[4]xf32> to vector<[4]xf32>
1641
+ // / ```
1660
1642
// /
1661
1643
// / gets converted to:
1662
1644
// /
1645
+ // / ```
1663
1646
// / %B_row_sc = vector.shape_cast %B_row : vector<1x[4]xf32> to vector<[4]xf32>
1664
1647
// / %A_row_sc = vector.shape_cast %A_row : vector<1x[4]xf32> to vector<[4]xf32>
1665
1648
// / %mul = arith.mulf %B_row_sc, %A_row_sc : vector<[4]xf32>
1666
1649
// / %cast_new = vector.shape_cast %mul : vector<[4]xf32> to vector<1x[4]xf32>
1667
1650
// / %cast = vector.shape_cast %cast_new : vector<1x[4]xf32> to vector<[4]xf32>
1651
+ // / ```
1668
1652
// /
1669
1653
// / Patterns for folding shape_casts should instantly eliminate `%cast_new` and
1670
1654
// / `%cast`.
@@ -1684,29 +1668,42 @@ struct DropUnitDimFromElementwiseOps final
1684
1668
// guaranteed to have identical shapes (with some exceptions such as
1685
1669
// `arith.select`) and it suffices to only check one of them.
1686
1670
auto sourceVectorType = dyn_cast<VectorType>(op->getOperand (0 ).getType ());
1687
- if (!sourceVectorType || sourceVectorType.getRank () < 2 )
1671
+ if (!sourceVectorType)
1672
+ return failure ();
1673
+ if (sourceVectorType.getRank () < 2 )
1674
+ return failure ();
1675
+
1676
+ bool hasTrailingDimUnitFixed =
1677
+ ((sourceVectorType.getShape ().back () == 1 ) &&
1678
+ (!sourceVectorType.getScalableDims ().back ()));
1679
+ bool hasLeadingDimUnitFixed =
1680
+ ((sourceVectorType.getShape ().front () == 1 ) &&
1681
+ (!sourceVectorType.getScalableDims ().front ()));
1682
+ if (!hasLeadingDimUnitFixed && !hasTrailingDimUnitFixed)
1688
1683
return failure ();
1689
1684
1685
+ // Drop leading/trailing unit dim by applying vector.shape_cast to all
1686
+ // operands
1687
+ int64_t dim = hasLeadingDimUnitFixed ? 0 : sourceVectorType.getRank () - 1 ;
1688
+
1690
1689
SmallVector<Value> newOperands;
1691
1690
auto loc = op->getLoc ();
1692
1691
for (auto operand : op->getOperands ()) {
1693
1692
auto opVectorType = cast<VectorType>(operand.getType ());
1694
- auto newVType = dropNonScalableUnitDimFromType (opVectorType);
1695
- if (newVType == opVectorType)
1696
- return rewriter.notifyMatchFailure (op, " No unit dimension to remove." );
1697
-
1693
+ VectorType newVType = VectorType::Builder (opVectorType).dropDim (dim);
1698
1694
auto opSC = rewriter.create <vector::ShapeCastOp>(loc, newVType, operand);
1699
1695
newOperands.push_back (opSC);
1700
1696
}
1701
1697
1702
1698
VectorType newResultVectorType =
1703
- dropNonScalableUnitDimFromType (resultVectorType);
1704
- // Create an updated elementwise Op without unit dim.
1699
+ VectorType::Builder (resultVectorType). dropDim (dim );
1700
+ // Create an updated elementwise Op without leading/trailing unit dim
1705
1701
Operation *elementwiseOp =
1706
1702
rewriter.create (loc, op->getName ().getIdentifier (), newOperands,
1707
1703
newResultVectorType, op->getAttrs ());
1708
1704
1709
- // Restore the unit dim by applying vector.shape_cast to the result.
1705
+ // Restore the leading/trailing unit dim by applying vector.shape_cast
1706
+ // to the result
1710
1707
rewriter.replaceOpWithNewOp <ShapeCastOp>(op, resultVectorType,
1711
1708
elementwiseOp->getResult (0 ));
1712
1709
0 commit comments