Skip to content

Commit a3fc99e

Browse files
hanhanWTensorFlow MLIR Team
authored andcommitted
Add support for lowering mhlo.dynamic_slice to Linalg ops.
PiperOrigin-RevId: 368033540
1 parent 0ec0a23 commit a3fc99e

2 files changed

Lines changed: 94 additions & 0 deletions

File tree

‎lib/Dialect/mhlo/transforms/legalize_to_linalg.cc‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,70 @@ class SliceConverter : public OpConversionPattern<OpTy> {
11341134
}
11351135
};
11361136

1137+
class DynamicSliceConverter : public OpConversionPattern<mhlo::DynamicSliceOp> {
1138+
public:
1139+
using OpConversionPattern<mhlo::DynamicSliceOp>::OpConversionPattern;
1140+
1141+
LogicalResult matchAndRewrite(
1142+
mhlo::DynamicSliceOp dynamic_slice_op, ArrayRef<Value> args,
1143+
ConversionPatternRewriter& rewriter) const final {
1144+
auto loc = dynamic_slice_op.getLoc();
1145+
mhlo::DynamicSliceOp::Adaptor adaptor(args);
1146+
auto arg_type = adaptor.operand().getType().dyn_cast<ShapedType>();
1147+
if (!arg_type || !arg_type.hasRank()) {
1148+
return rewriter.notifyMatchFailure(dynamic_slice_op,
1149+
"require known-rank args");
1150+
}
1151+
1152+
auto index_type = rewriter.getIndexType();
1153+
SmallVector<OpFoldResult, 3> start_indices, sizes;
1154+
Value zero = rewriter.create<ConstantOp>(
1155+
loc, rewriter.getZeroAttr(adaptor.start_indices()[0]
1156+
.getType()
1157+
.cast<RankedTensorType>()
1158+
.getElementType()));
1159+
for (auto en : llvm::enumerate(
1160+
llvm::zip(adaptor.start_indices(),
1161+
dynamic_slice_op.slice_sizes().getValues<int64_t>()))) {
1162+
int64_t size = std::get<1>(en.value());
1163+
sizes.push_back(rewriter.getI64IntegerAttr(size));
1164+
1165+
// By mhlo.DynamicSlice definition:
1166+
// `start_indices[i] = clamp(start_indices[i],
1167+
// 0, operand.dimension_size[i] - size_indices[i])`
1168+
Value start_index =
1169+
rewriter.create<tensor::ExtractOp>(loc, std::get<0>(en.value()));
1170+
Value ub = rewriter.createOrFold<memref::DimOp>(loc, adaptor.operand(),
1171+
en.index());
1172+
// ClampOp lowering does not support index type, so cast it into integer
1173+
// type.
1174+
ub = rewriter.createOrFold<IndexCastOp>(loc, start_index.getType(), ub);
1175+
ub = rewriter.createOrFold<SubIOp>(
1176+
loc, ub,
1177+
rewriter.create<ConstantOp>(
1178+
loc, rewriter.getIntegerAttr(start_index.getType(), size)));
1179+
// TODO(hanchung): This is a workaround to use the method because only
1180+
// lmhlo version is defined. The implementation in
1181+
// map_lmhlo_to_scalar_op.h requires to pass a mhlo op. It will convert it
1182+
// to an lmhlo op and call the lmhlo implementation.
1183+
start_index = lmhlo::HloOpToStdScalarOp::map<lmhlo::ClampOp>(
1184+
loc, start_index.getType(), ArrayRef<Value>{zero, start_index, ub},
1185+
&rewriter);
1186+
start_indices.push_back(
1187+
rewriter.create<IndexCastOp>(loc, index_type, start_index)
1188+
.getResult());
1189+
}
1190+
1191+
int64_t rank = arg_type.getRank();
1192+
SmallVector<OpFoldResult, 3> strides(rank, rewriter.getI64IntegerAttr(1));
1193+
1194+
rewriter.replaceOpWithNewOp<SubTensorOp>(
1195+
dynamic_slice_op, dynamic_slice_op.getType().cast<RankedTensorType>(),
1196+
adaptor.operand(), start_indices, sizes, strides);
1197+
return success();
1198+
}
1199+
};
1200+
11371201
enum class DotOperationType {
11381202
kVectorDot = 0,
11391203
kMatrixVector = 1,
@@ -2090,6 +2154,7 @@ void populateHLOToLinalgConversionPattern(MLIRContext* context,
20902154
ReshapeOpConverter<mhlo::ReshapeOp, false>,
20912155
ReverseConverter<mhlo::ReverseOp, false>,
20922156
SliceConverter<mhlo::SliceOp, false>,
2157+
DynamicSliceConverter,
20932158
TransposeConverter<mhlo::TransposeOp, false>,
20942159
DotOpOnTensorsConversion<DotOperationType::kMatrixMatrix,
20952160
linalg::MatmulOp>,

‎tests/hlo-legalize-to-linalg.mlir‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,35 @@ func @slice_stride_part(%arg0: tensor<3x4xi32>) -> tensor<1x2xi32> {
15321532

15331533
// -----
15341534

1535+
func @dynamic_slice(%arg: tensor<3x4xf32>, %start1: tensor<i64>, %start2: tensor<i64>) -> tensor<1x4xf32> {
1536+
%0 = "mhlo.dynamic-slice"(%arg, %start1, %start2) {
1537+
slice_sizes = dense<[1, 4]> : tensor<2xi64>
1538+
} : (tensor<3x4xf32>, tensor<i64>, tensor<i64>) -> tensor<1x4xf32>
1539+
return %0 : tensor<1x4xf32>
1540+
}
1541+
// CHECK-LABEL: func @dynamic_slice
1542+
// CHECK-SAME: %[[ARG0:[a-zA-Z0-9_]*]]
1543+
// CHECK-SAME: %[[ARG1:[a-zA-Z0-9_]*]]
1544+
// CHECK-SAME: %[[ARG2:[a-zA-Z0-9_]*]]
1545+
// CHECK: %[[C0:.*]] = constant 0 : i64
1546+
// CHECK: %[[SCALAR1:.*]] = tensor.extract %[[ARG1]][] : tensor<i64>
1547+
// CHECK: %[[UB1:.*]] = constant 2 : i64
1548+
// CHECK: %[[COND1:.*]] = cmpi slt, %[[SCALAR1]], %[[UB1]] : i64
1549+
// CHECK: %[[T1:.*]] = select %[[COND1]], %[[SCALAR1]], %[[UB1]] : i64
1550+
// CHECK: %[[COND2:.*]] = cmpi sgt, %[[T1]], %[[C0]] : i64
1551+
// CHECK: %[[CLAMPED1:.*]] = select %[[COND2]], %[[T1]], %[[C0]] : i64
1552+
// CHECK: %[[START1:.*]] = index_cast %[[CLAMPED1]] : i64 to index
1553+
// CHECK: %[[SCALAR2:.*]] = tensor.extract %[[ARG2]][] : tensor<i64>
1554+
// CHECK: %[[UB2:.*]] = constant 0 : i64
1555+
// CHECK: %[[COND3:.*]] = cmpi slt, %[[SCALAR2]], %[[UB2]] : i64
1556+
// CHECK: %[[T2:.*]] = select %[[COND3]], %[[SCALAR2]], %[[UB2]] : i64
1557+
// CHECK: %[[COND4:.*]] = cmpi sgt, %[[T2]], %[[C0]] : i64
1558+
// CHECK: %[[CLAMPED2:.*]] = select %[[COND4]], %[[T2]], %[[C0]] : i64
1559+
// CHECK: %[[START2:.*]] = index_cast %[[CLAMPED2]] : i64 to index
1560+
// CHECK: subtensor %[[ARG0]][%[[START1]], %[[START2]]] [1, 4] [1, 1]
1561+
1562+
// -----
1563+
15351564
func @pad_cst(%arg0: tensor<12x4xf32>) -> tensor<18x12xf32> {
15361565
%0 = constant dense<0.0> : tensor<f32>
15371566
%1 = "mhlo.pad"(%arg0, %0) {

0 commit comments

Comments
 (0)