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