@@ -1622,7 +1622,27 @@ struct ChainedReduction final : OpRewritePattern<vector::ReductionOp> {
1622
1622
}
1623
1623
};
1624
1624
1625
- // / For vectors with either leading or trailing unit dim, replaces:
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:
1626
1646
// / elementwise(a, b)
1627
1647
// / with:
1628
1648
// / sc_a = shape_cast(a)
@@ -1634,20 +1654,16 @@ struct ChainedReduction final : OpRewritePattern<vector::ReductionOp> {
1634
1654
// / required to be rank > 1.
1635
1655
// /
1636
1656
// / Ex:
1637
- // / ```
1638
1657
// / %mul = arith.mulf %B_row, %A_row : vector<1x[4]xf32>
1639
1658
// / %cast = vector.shape_cast %mul : vector<1x[4]xf32> to vector<[4]xf32>
1640
- // / ```
1641
1659
// /
1642
1660
// / gets converted to:
1643
1661
// /
1644
- // / ```
1645
1662
// / %B_row_sc = vector.shape_cast %B_row : vector<1x[4]xf32> to vector<[4]xf32>
1646
1663
// / %A_row_sc = vector.shape_cast %A_row : vector<1x[4]xf32> to vector<[4]xf32>
1647
1664
// / %mul = arith.mulf %B_row_sc, %A_row_sc : vector<[4]xf32>
1648
1665
// / %cast_new = vector.shape_cast %mul : vector<[4]xf32> to vector<1x[4]xf32>
1649
1666
// / %cast = vector.shape_cast %cast_new : vector<1x[4]xf32> to vector<[4]xf32>
1650
- // / ```
1651
1667
// /
1652
1668
// / Patterns for folding shape_casts should instantly eliminate `%cast_new` and
1653
1669
// / `%cast`.
@@ -1667,42 +1683,29 @@ struct DropUnitDimFromElementwiseOps final
1667
1683
// guaranteed to have identical shapes (with some exceptions such as
1668
1684
// `arith.select`) and it suffices to only check one of them.
1669
1685
auto sourceVectorType = dyn_cast<VectorType>(op->getOperand (0 ).getType ());
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)
1686
+ if (!sourceVectorType || sourceVectorType.getRank () < 2 )
1682
1687
return failure ();
1683
1688
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
-
1688
1689
SmallVector<Value> newOperands;
1689
1690
auto loc = op->getLoc ();
1690
1691
for (auto operand : op->getOperands ()) {
1691
1692
auto opVectorType = cast<VectorType>(operand.getType ());
1692
- VectorType newVType = VectorType::Builder (opVectorType).dropDim (dim);
1693
+ auto newVType = dropNonScalableUnitDimFromType (opVectorType);
1694
+ if (newVType == opVectorType)
1695
+ return rewriter.notifyMatchFailure (op, " No unit dimension to remove." );
1696
+
1693
1697
auto opSC = rewriter.create <vector::ShapeCastOp>(loc, newVType, operand);
1694
1698
newOperands.push_back (opSC);
1695
1699
}
1696
1700
1697
1701
VectorType newResultVectorType =
1698
- VectorType::Builder (resultVectorType). dropDim (dim );
1699
- // Create an updated elementwise Op without leading/trailing unit dim
1702
+ dropNonScalableUnitDimFromType (resultVectorType);
1703
+ // Create an updated elementwise Op without unit dim.
1700
1704
Operation *elementwiseOp =
1701
1705
rewriter.create (loc, op->getName ().getIdentifier (), newOperands,
1702
1706
newResultVectorType, op->getAttrs ());
1703
1707
1704
- // Restore the leading/trailing unit dim by applying vector.shape_cast
1705
- // to the result
1708
+ // Restore the unit dim by applying vector.shape_cast to the result.
1706
1709
rewriter.replaceOpWithNewOp <ShapeCastOp>(op, resultVectorType,
1707
1710
elementwiseOp->getResult (0 ));
1708
1711
0 commit comments