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