Skip to content

Commit cd48b10

Browse files
authored
[NFC] Delete dead ops after cloning (#18669)
This helps increase the readability of IR dumps when looking at `flow.dispatch.region`s because cloneable ops with no consumers were being wrapped in a `flow.dispatch.region` instead of deleted.
1 parent 7a2705d commit cd48b10

File tree

4 files changed

+11
-5
lines changed

4 files changed

+11
-5
lines changed

compiler/src/iree/compiler/DispatchCreation/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ iree_compiler_cc_library(
8989
"@llvm-project//mlir:Pass",
9090
"@llvm-project//mlir:SCFDialect",
9191
"@llvm-project//mlir:SCFTransforms",
92+
"@llvm-project//mlir:SideEffectInterfaces",
9293
"@llvm-project//mlir:Support",
9394
"@llvm-project//mlir:TensorDialect",
9495
"@llvm-project//mlir:TensorTransforms",

compiler/src/iree/compiler/DispatchCreation/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ iree_cc_library(
6969
MLIRPass
7070
MLIRSCFDialect
7171
MLIRSCFTransforms
72+
MLIRSideEffectInterfaces
7273
MLIRSupport
7374
MLIRTensorDialect
7475
MLIRTensorTransforms

compiler/src/iree/compiler/DispatchCreation/CloneProducersIntoDispatchRegions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "mlir/Dialect/Linalg/IR/Linalg.h"
1111
#include "mlir/IR/PatternMatch.h"
1212
#include "mlir/Interfaces/FunctionInterfaces.h"
13+
#include "mlir/Interfaces/SideEffectInterfaces.h"
1314

1415
#define DEBUG_TYPE \
1516
"iree-dispatch-creation-clone-producers-into-dispatch-regions"
@@ -34,6 +35,9 @@ struct CloneProducersIntoDispatchRegionsPass final
3435
});
3536

3637
funcOp->walk([&](Operation *op) {
38+
if (isOpTriviallyDead(op)) {
39+
return rewriter.eraseOp(op);
40+
}
3741
if (!IREE::Flow::isNonNullAndOutsideDispatch(op) ||
3842
!isa<linalg::GenericOp>(op)) {
3943
return;

compiler/src/iree/compiler/DispatchCreation/test/clone_producers_into_dispatch_regions.mlir

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ util.func public @complex_create(%real : f32, %imag : f32, %input: tensor<4x2xco
8181
} -> tensor<4x2xcomplex<f32>>
8282
flow.return %generic : tensor<4x2xcomplex<f32>>
8383
}
84-
util.return %0 : tensor<4x2xcomplex<f32>>
84+
util.return %1 : tensor<4x2xcomplex<f32>>
8585
}
8686

8787
// CHECK-LABEL: @complex_create
@@ -330,13 +330,13 @@ util.func public @clone_broadcast_dequant_op(
330330
// -----
331331

332332
// Do no clone index cast operations when they are operands to the dispatch
333-
util.func public @dont_clone_index_type_op(%arg0 : i64) {
333+
util.func public @dont_clone_index_type_op(%arg0 : i64) -> tensor<?xf32> {
334334
%0 = arith.index_cast %arg0 : i64 to index
335335
%1 = flow.dispatch.region[] -> (tensor<?xf32>{%0}) {
336336
%2 = tensor.empty(%0) : tensor<?xf32>
337337
flow.return %2 : tensor<?xf32>
338338
}
339-
util.return
339+
util.return %1 : tensor<?xf32>
340340
}
341341
// CHECK-LABEL: func public @dont_clone_index_type_op
342342
// CHECK: arith.index_cast
@@ -346,14 +346,14 @@ util.func public @dont_clone_index_type_op(%arg0 : i64) {
346346
// -----
347347
// Do no clone index cast operations when they are in-direct operands to the dispatch
348348
#map = affine_map<()[s0] -> (s0 * 12)>
349-
util.func public @dont_clone_index_type_op_2(%arg0: i64) {
349+
util.func public @dont_clone_index_type_op_2(%arg0: i64) -> tensor<?xf32> {
350350
%0 = arith.index_cast %arg0 : i64 to index
351351
%1 = affine.apply #map()[%0]
352352
%2 = flow.dispatch.region -> (tensor<?xf32>{%1}) {
353353
%3 = tensor.empty(%1) : tensor<?xf32>
354354
flow.return %3 : tensor<?xf32>
355355
}
356-
util.return
356+
util.return %2 : tensor<?xf32>
357357
}
358358
// CHECK-LABEL: func public @dont_clone_index_type_op_2
359359
// CHECK: arith.index_cast

0 commit comments

Comments
 (0)