Skip to content

Commit 7f7103c

Browse files
author
gysit
committed
[mlir][linalg] Use top down traversal for padding.
Pad the operation using a top down traversal. The top down traversal unlocks folding opportunities and dim op canonicalizations due to the introduced extract slice operation after the padded operation. Depends On D114585 Reviewed By: nicolasvasilache Differential Revision: https://reviews.llvm.org/D114689
1 parent 9e8a71c commit 7f7103c

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

mlir/lib/Dialect/Linalg/Transforms/LinalgStrategyPasses.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ struct LinalgStrategyTileAndFusePass
6565
funcOp.getContext(), options, filter);
6666
}
6767
// Search the root operation using bottom up traversal.
68-
GreedyRewriteConfig grc;
69-
grc.useTopDownTraversal = false;
70-
(void)applyPatternsAndFoldGreedily(funcOp,
71-
std::move(tilingAndFusionPattern), grc);
68+
GreedyRewriteConfig config;
69+
config.useTopDownTraversal = false;
70+
(void)applyPatternsAndFoldGreedily(
71+
funcOp, std::move(tilingAndFusionPattern), config);
7272
}
7373

7474
LinalgTilingAndFusionOptions options;
@@ -132,7 +132,18 @@ struct LinalgStrategyPadPass
132132
paddingPattern.add<LinalgPaddingPattern>(funcOp.getContext(), options,
133133
filter);
134134
}
135-
if (failed(applyPatternsAndFoldGreedily(funcOp, std::move(paddingPattern))))
135+
// Traverse the operations top down to pad producers before consumers. The
136+
// extract slice operation introduced after every padded operation enables
137+
// padding its consumers. Padding an operation whose producers have not been
138+
// padded before fails due to the missing extract slice operations. In this
139+
// case, the padding pattern increments the transformation marker without
140+
// padding the operation. The top down traversal is thus not only a
141+
// performance optimization but also needed to pad all operations along the
142+
// use-def chains.
143+
GreedyRewriteConfig config;
144+
config.useTopDownTraversal = true;
145+
if (failed(applyPatternsAndFoldGreedily(funcOp, std::move(paddingPattern),
146+
config)))
136147
signalPassFailure();
137148
}
138149

mlir/test/Dialect/Linalg/pad.mlir

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ func @dynamic_sizes(%arg0: tensor<?x?xf32>,
163163

164164
#map0 = affine_map<()[s0] -> (64, s0)>
165165

166+
// FILL: pad_multiple
167+
// FILL-SAME: %[[ARG0:[0-9a-zA-Z]*]]: tensor<64x64xf32>
168+
func @pad_multiple(%arg0: tensor<64x64xf32>,
169+
%iv0 : index) -> tensor<?x?xf32> {
170+
%cst = arith.constant 0.0 : f32
171+
%size = affine.min #map0()[%iv0]
172+
%0 = tensor.extract_slice %arg0[0, 0] [%size, %size] [1, 1] : tensor<64x64xf32> to tensor<?x?xf32>
173+
174+
// Check both fill operations are padded by the same pad tensor operation.
175+
// FILL: %[[T0:.*]] = linalg.pad_tensor
176+
// FILL: %[[T1:.*]] = linalg.fill(%{{.*}}, %[[T0]])
177+
// FILL: %[[T2:.*]] = linalg.fill(%{{.*}}, %[[T1]])
178+
// FILL: = tensor.extract_slice %[[T2]]
179+
%1 = linalg.fill(%cst, %0) : f32, tensor<?x?xf32> -> tensor<?x?xf32>
180+
%2 = linalg.fill(%cst, %1) : f32, tensor<?x?xf32> -> tensor<?x?xf32>
181+
return %2 : tensor<?x?xf32>
182+
}
183+
184+
// -----
185+
186+
#map0 = affine_map<()[s0] -> (64, s0)>
187+
166188
// MATMUL: compose_padding
167189
// MATMUL-SAME: %[[ARG0:[0-9a-zA-Z]*]]: tensor<64x64xf32>
168190
func @compose_padding(%arg0: tensor<64x64xf32>,

0 commit comments

Comments
 (0)