Skip to content

Commit 2e34599

Browse files
committed
[mlir][tensor] Remove folding of tensor.extract_slice during tiling
Blindly folding tensor.extract_slice makes the bufferization transformation harder. This kind of transformation should happen separatley if needed rather than doing it within makeShape that is called during tiling. Also removed makeComposedExtractSliceOp as it is not tested outside of this code. Differential Revision: https://reviews.llvm.org/D132666
1 parent e84784e commit 2e34599

7 files changed

Lines changed: 22 additions & 79 deletions

File tree

‎mlir/include/mlir/Dialect/Linalg/Utils/Utils.h‎

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,6 @@ void getUpperBoundForIndex(Value value, AffineMap &boundMap,
9393
/// (boundsMap = affine.map<() -> (42)>)
9494
FailureOr<int64_t> getConstantUpperBoundForIndex(Value value);
9595

96-
/// Create an ExtractSliceOp and, if `source` is defined by an ExtractSliceOp,
97-
/// fold it by adding the offsets.
98-
///
99-
/// Example:
100-
/// ```
101-
/// %0 = tensor.extract_slice %arg0[3, 4][3, 32][1, 1] : tensor<64x64xf32> to
102-
/// tensor<3x32xf32>
103-
/// %1 = tensor.extract_slice %0[0, 5][3, 4][1, 1] : tensor<3x32xf32> to
104-
/// tensor<3x4xf32>
105-
/// ```
106-
/// folds into:
107-
/// ```
108-
/// %1 = tensor.extract_slice %arg0[3, 9][3, 4][1, 1] : tensor<64x64xf32> to
109-
/// tensor<3x4xf32>
110-
/// ```
111-
tensor::ExtractSliceOp makeComposedExtractSliceOp(
112-
OpBuilder &b, Location loc, Value source, ArrayRef<OpFoldResult> offsets,
113-
ArrayRef<OpFoldResult> sizes, ArrayRef<OpFoldResult> strides);
114-
11596
/// Create a tensor::PadOp that pads `source` to the size of the statically
11697
/// sized `type` whose static sizes are assumed to be greater than the dynamic
11798
/// `source` size. The padding introduces trailing `pad` values until the target

‎mlir/lib/Dialect/Linalg/Utils/Utils.cpp‎

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -346,48 +346,6 @@ FailureOr<int64_t> getConstantUpperBoundForIndex(Value value) {
346346
return *std::min_element(constantBounds.begin(), constantBounds.end());
347347
}
348348

349-
tensor::ExtractSliceOp makeComposedExtractSliceOp(
350-
OpBuilder &b, Location loc, Value source, ArrayRef<OpFoldResult> offsets,
351-
ArrayRef<OpFoldResult> sizes, ArrayRef<OpFoldResult> strides) {
352-
assert(source && "expect source to be nonzero");
353-
354-
// Do not fold if the producer is not an ExtractSliceOp.
355-
auto producerOp = source.getDefiningOp<tensor::ExtractSliceOp>();
356-
if (!producerOp)
357-
return b.create<tensor::ExtractSliceOp>(loc, source, offsets, sizes,
358-
strides);
359-
360-
// Do not fold if the producer is rank reducing or if there are any non-unit
361-
// strides. Supporting non-unit strides complicates the offset computation
362-
// since the consumer offsets need to be multiplied by the producer strides.
363-
// TODO: support non-unit strides once there are use cases.
364-
SmallVector<OpFoldResult> allStrides = producerOp.getMixedStrides();
365-
allStrides.append(strides.begin(), strides.end());
366-
bool hasNonUnitStride = any_of(allStrides, [](OpFoldResult ofr) {
367-
return getConstantIntValue(ofr) != static_cast<int64_t>(1);
368-
});
369-
if (hasNonUnitStride ||
370-
producerOp.getSourceType().getRank() !=
371-
producerOp.getResult().getType().cast<ShapedType>().getRank())
372-
return b.create<tensor::ExtractSliceOp>(loc, source, offsets, sizes,
373-
strides);
374-
375-
// Fold the producer by adding the offests and extracting the slice directly
376-
// from the producer source tensor.
377-
SmallVector<OpFoldResult> foldedOffsets(offsets.begin(), offsets.end());
378-
AffineExpr dim1, dim2;
379-
bindDims(b.getContext(), dim1, dim2);
380-
for (const auto &en : enumerate(producerOp.getMixedOffsets())) {
381-
SmallVector<Value> offsetValues = {
382-
getValueOrCreateConstantIndexOp(b, loc, foldedOffsets[en.index()]),
383-
getValueOrCreateConstantIndexOp(b, loc, en.value())};
384-
foldedOffsets[en.index()] =
385-
makeComposedAffineApply(b, loc, dim1 + dim2, offsetValues).getResult();
386-
}
387-
return b.create<tensor::ExtractSliceOp>(loc, producerOp.getSource(),
388-
foldedOffsets, sizes, strides);
389-
}
390-
391349
Value makeComposedPadHighOp(OpBuilder &b, Location loc, RankedTensorType type,
392350
Value source, Value pad, bool nofold) {
393351
// Exit if `source` is not defined by an ExtractSliceOp.
@@ -777,8 +735,8 @@ static Value materializeTiledShape(OpBuilder &builder, Location loc,
777735
sliceParams.sizes, sliceParams.strides);
778736
})
779737
.Case([&](RankedTensorType) {
780-
return makeComposedExtractSliceOp(
781-
builder, loc, valueToTile, sliceParams.offsets,
738+
return builder.create<tensor::ExtractSliceOp>(
739+
loc, valueToTile, sliceParams.offsets,
782740
sliceParams.sizes, sliceParams.strides);
783741
})
784742
.Default([](ShapedType) -> Operation * {

‎mlir/test/Dialect/Linalg/multisize-tiling-full.mlir‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,16 @@ func.func @two_d(%arg0: tensor<10x34xf32>,
4747
// The canonicalizer is able to recover static shapes of for linalg.generic
4848
// instances, use those to differentiate the quadrants.
4949

50+
// CHECK: %[[SLICE_1_IN:.+]] = tensor.extract_slice %[[IN]][0, 0] [4, 34] [1, 1]
5051
// CHECK: %[[SLICE_1:.+]] = tensor.extract_slice %[[OUT]][0, 0] [4, 34] [1, 1]
5152
// CHECK: scf.for %[[I1:.+]] = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ITERARG_1:.+]] = %[[SLICE_1]])
53+
// CHECK: %[[OUTSLICE_1_IN:.+]] = tensor.extract_slice %[[SLICE_1_IN]][%[[I1]], 0] [2, 34] [1, 1]
5254
// CHECK: %[[OUTSLICE_1:.+]] = tensor.extract_slice %[[ITERARG_1]][%[[I1]], 0] [2, 34] [1, 1]
5355

54-
// CHECK: %[[SLICE_2:.+]] = tensor.extract_slice %[[ITERARG_1]][%[[I1]], 0] [2, 16] [1, 1]
56+
// CHECK: %[[SLICE_2_IN:.+]] = tensor.extract_slice %[[OUTSLICE_1_IN]][0, 0] [2, 16] [1, 1]
57+
// CHECK: %[[SLICE_2:.+]] = tensor.extract_slice %[[OUTSLICE_1]][0, 0] [2, 16] [1, 1]
5558
// CHECK: %[[LOOPRES:.+]] = scf.for %[[I2:.+]] = %{{.*}} to %{{.*}} step %{{.*}} iter_args(%[[ITERARG_2:.+]] = %[[SLICE_2]])
56-
// CHECK: %[[INSLICE_2:.+]] = tensor.extract_slice %[[IN]][%[[I1]], %[[I2]]] [2, 8] [1, 1]
59+
// CHECK: %[[INSLICE_2:.+]] = tensor.extract_slice %[[SLICE_2_IN]][0, %[[I2]]] [2, 8] [1, 1]
5760
// CHECK: %[[OUTSLICE_2:.+]] = tensor.extract_slice %[[ITERARG_2]][0, %[[I2]]] [2, 8] [1, 1]
5861
// CHECK: %[[RESSLICE_1:.+]] = linalg.generic {{.*}} ins(%[[INSLICE_2]] : tensor<2x8xf32>) outs(%[[OUTSLICE_2]] : tensor<2x8xf32>)
5962
// CHECK: %[[RESPARTIAL:.+]] = tensor.insert_slice %[[RESSLICE_1]] into %[[ITERARG_2]]

‎mlir/test/Dialect/Linalg/tile-fuse-and-distribute.mlir‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ func.func @fill_matmul_tensors(
2323
// CHECK: %[[LBX:.+]] = affine.apply #[[ADDMAP]]()[%[[MUL]], %[[C0]]]
2424
// CHECK: %[[STEPX:.+]] = affine.apply #[[MULMAP]]()[%[[NBLOCKSX]], %[[C8]]]
2525
// CHECK: %[[TD1:.*]] = scf.for {{.*}} to {{.*}} step {{.*}} iter_args(%[[TC1:.*]] = %[[TC0]]) -> (tensor<?x?xf32>) {
26+
// CHECK: %[[OUTSLICEA:.+]] = tensor.extract_slice %{{.*}}[%{{.*}}, 0] [%{{.*}}, %{{.*}}] [1, 1] : tensor<?x?xf32> to tensor<?x?xf32>
27+
// CHECK: %[[OUTSLICEB:.+]] = tensor.extract_slice %{{.*}}[0, %{{.*}}] [%{{.*}}, %{{.*}}] [1, 1] : tensor<?x?xf32> to tensor<?x?xf32>
2628
// CHECK: %[[SLICE:.+]] = tensor.extract_slice %[[TC1]]
2729
// CHECK: %[[FILL:.+]] = linalg.fill ins(%{{.+}}{{.*}}outs(%[[SLICE]]
2830
// CHECK: %[[sTD2:.*]] = scf.for {{.*}} to {{.*}} step {{.*}} iter_args(%[[TC2:.*]] = %[[FILL]]) -> (tensor<?x?xf32>) {
29-
// CHECK: %[[sTA:.*]] = tensor.extract_slice %[[TA]][{{.*}}] : tensor<?x?xf32> to tensor<?x?xf32>
30-
// CHECK: %[[sTB:.*]] = tensor.extract_slice %[[TB]][{{.*}}] : tensor<?x?xf32> to tensor<?x?xf32>
31+
// CHECK: %[[sTA:.*]] = tensor.extract_slice %[[OUTSLICEA]][{{.*}}] : tensor<?x?xf32> to tensor<?x?xf32>
32+
// CHECK: %[[sTB:.*]] = tensor.extract_slice %[[OUTSLICEB]][{{.*}}] : tensor<?x?xf32> to tensor<?x?xf32>
3133
// CHECK: %[[sTC:.*]] = tensor.extract_slice %[[TC2]][{{.*}}] : tensor<?x?xf32> to tensor<?x?xf32>
3234
// CHECK: %[[sTD:.*]] = linalg.matmul ins(%[[sTA]], %[[sTB]] : tensor<?x?xf32>, tensor<?x?xf32>)
3335
// CHECK-SAME: outs(%[[sTC]] : tensor<?x?xf32>) -> tensor<?x?xf32>

‎mlir/test/Dialect/Linalg/tile-tensors.mlir‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ func.func @generic_op_tensors(
7777
// -----
7878

7979
// CHECK-DAG: #[[MAP0:.*]] = affine_map<(d0)[s0] -> (-d0 + s0, 2)>
80-
// CHECK-DAG: #[[MAP1:.*]] = affine_map<(d0) -> (d0 + 3)>
81-
// CHECK-DAG: #[[MAP2:.*]] = affine_map<(d0) -> (d0 + 4)>
8280

8381
// CHECK: fold_extract_slice
8482
// CHECK-SAME: %[[ARG0:[0-9a-zA-Z]*]]: tensor<?x128xf32>
@@ -93,15 +91,15 @@ func.func @fold_extract_slice(
9391
%0 = tensor.dim %arg1, %c0 : tensor<?x42xf32>
9492
%1 = tensor.extract_slice %arg0[3, 4] [%0, 42] [1, 1] : tensor<?x128xf32> to tensor<?x42xf32>
9593

94+
// CHECK: %[[E:.*]] = tensor.extract_slice %[[ARG0]][3, 4] [%[[DIM]], 42] [1, 1] : tensor<?x128xf32> to tensor<?x42xf32>
95+
9696
// CHECK: scf.for %[[IV0:[0-9a-zA-Z]*]] =
9797
// CHECK: scf.for %[[IV1:[0-9a-zA-Z]*]] =
9898

9999
// Fold the existing extract slice op into the one created by the tiling.
100100
// CHECK: %[[SIZE0:.*]] = affine.min #[[MAP0]](%[[IV0]])[%[[DIM]]
101-
// CHECK: %[[OFF0:.*]] = affine.apply #[[MAP1]](%[[IV0]]
102-
// CHECK: %[[OFF1:.*]] = affine.apply #[[MAP2]](%[[IV1]]
103-
// CHECK: %[[T0:.*]] = tensor.extract_slice %[[ARG0]]
104-
// CHECK-SAME: %[[OFF0]], %[[OFF1]]
101+
// CHECK: %[[T0:.*]] = tensor.extract_slice %[[E]]
102+
// CHECK-SAME: %[[IV0]], %[[IV1]]
105103
// CHECK-SAME: %[[SIZE0]], 3
106104
// CHECK-SAME: 1, 1
107105
// CHECK: {{.*}} = linalg.generic {{.*}} ins(%[[T0]]

‎mlir/test/Dialect/Linalg/transform-op-fuse.mlir‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@ func.func @interchange_reduction(%input: tensor<12x7x25xf32>) -> tensor<12x25xf3
6666
// CHECK-DAG: %[[C7:.+]] = arith.constant 7 : index
6767
// CHECK: scf.for %[[IV0:.+]] = %{{.+}} to %{{.+}} step %[[C5]] iter_args(%[[FOR_ARG0:.+]] = %[[INIT]])
6868
// CHECK: scf.for %[[IV1:.+]] = %{{.+}} to %{{.+}} step %[[C7]] iter_args(%[[FOR_ARG1:.+]] = %[[FOR_ARG0]])
69-
// CHECK: %[[OUT_SLICE0:.+]] = tensor.extract_slice %[[FOR_ARG1]][%[[IV0]], %[[IV1]]]
70-
// CHECK: %[[FILL:.+]] = linalg.fill {{.+}} outs(%[[OUT_SLICE0]] : tensor<?x?xf32>)
69+
// CHECK: %[[OUT_SLICE0:.+]] = tensor.extract_slice %[[INPUT]][%[[IV0]], 0, %[[IV1]]]
70+
// CHECK: %[[OUT_SLICE1:.+]] = tensor.extract_slice %[[FOR_ARG1]][%[[IV0]], %[[IV1]]]
71+
// CHECK: %[[FILL:.+]] = linalg.fill {{.+}} outs(%[[OUT_SLICE1]] : tensor<?x?xf32>)
7172
// CHECK: %[[C4:.+]] = arith.constant 4 : index
7273
// CHECK: scf.for %[[IV2:.+]] = %{{.+}} to %{{.+}} step %[[C4]] iter_args(%[[FOR_ARG2:.+]] = %[[FILL]])
73-
// CHECK: %[[IN_SLICE:.+]] = tensor.extract_slice %[[INPUT]]
74+
// CHECK: %[[IN_SLICE:.+]] = tensor.extract_slice %[[OUT_SLICE0]]
7475
// CHECK: %[[OUT_SLICE2:.+]] = tensor.extract_slice %[[FOR_ARG2]][0, 0]
7576
// CHECK: linalg.generic {{.+}} ins(%[[IN_SLICE]] : tensor<?x?x?xf32>) outs(%[[OUT_SLICE2]] : tensor<?x?xf32>)
7677

‎mlir/test/Dialect/Linalg/transform-op-split.mlir‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,14 @@ func.func @two_d(%arg0: tensor<10x34xf32>,
153153
// CHECK: %[[OUT_2:.+]] = tensor.extract_slice %[[PARTIAL_1]]
154154
// Note that `extract_slice` taking a slice from another `extract_slice` result
155155
// is folded to use the operand of the first `extract_slice`.
156-
// CHECK: %[[IN_21:.+]] = tensor.extract_slice %[[IN]]
157-
// CHECK: %[[OUT_21:.+]] = tensor.extract_slice %[[PARTIAL_1]]
156+
// CHECK: %[[IN_21:.+]] = tensor.extract_slice %[[IN_2]]
157+
// CHECK: %[[OUT_21:.+]] = tensor.extract_slice %[[OUT_2]]
158158
// CHECK: %[[RES_21:.+]] = linalg.generic
159159
// CHECK-SAME: ins(%[[IN_21]] : tensor<6x16xf32>)
160160
// CHECK-SAME: outs(%[[OUT_21]] : tensor<6x16xf32>)
161161
// CHECK: %[[PARTIAL_21:.+]] = tensor.insert_slice %[[RES_21]] into %[[OUT_2]]
162162
//
163-
// CHECK: %[[IN_22:.+]] = tensor.extract_slice %[[IN]]
163+
// CHECK: %[[IN_22:.+]] = tensor.extract_slice %[[IN_2]]
164164
// CHECK: %[[OUT_22:.+]] = tensor.extract_slice %[[PARTIAL_21]]
165165
// CHECK: %[[RES_22:.+]] = linalg.generic
166166
// CHECK-SAME: ins(%[[IN_22]] : tensor<6x18xf32>)

0 commit comments

Comments
 (0)