forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Flow] Add pattern to canonicalize away full tensor.insert_slice ops (i…
…ree-org#18941) Additionally drops the pad combining pattern from this pass because it was upstreamed.
- Loading branch information
Showing
2 changed files
with
92 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 39 additions & 67 deletions
106
compiler/src/iree/compiler/Dialect/Flow/Transforms/test/flow_canonicalize.mlir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,56 @@ | ||
// RUN: iree-opt --iree-flow-canonicalize %s --split-input-file --mlir-print-local-scope | FileCheck %s | ||
|
||
util.func public @merge_constant_padding(%arg0: tensor<2x3xf32>, %pad_value: f32) -> tensor<7x8xf32> { | ||
%pad0 = tensor.pad %arg0 low[1, 1] high[1, 0] { | ||
^bb0(%b0: index, %b1 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<2x3xf32> to tensor<4x4xf32> | ||
%pad1 = tensor.pad %pad0 low[0, 2] high[3, 2] { | ||
^bb0(%b2: index, %b3 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<4x4xf32> to tensor<7x8xf32> | ||
util.return %pad1 : tensor<7x8xf32> | ||
util.func public @fold_full_insert_into_extract( | ||
%source: tensor<8x?xf32>, | ||
%dest: tensor<10x?xf32>, | ||
%size: index) -> tensor<8x?xf32> { | ||
%extract = tensor.extract_slice %dest [1, 1] [8, %size] [1, 1] : tensor<10x?xf32> to tensor<8x?xf32> | ||
%insert = tensor.insert_slice %source into %extract [0, 0] [8, %size] [1, 1] : tensor<8x?xf32> into tensor<8x?xf32> | ||
util.return %insert : tensor<8x?xf32> | ||
} | ||
// CHECK-LABEL: util.func public @merge_constant_padding | ||
// CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<2x3xf32> | ||
// CHECK-SAME: %[[PADVAL:[A-Za-z0-9]+]]: f32 | ||
// CHECK: %[[PAD:.+]] = tensor.pad %[[ARG0]] low[1, 3] high[4, 2] | ||
// CHECK: tensor.yield %[[PADVAL]] | ||
// CHECK: util.return %[[PAD]] | ||
|
||
// CHECK-LABEL: util.func public @fold_full_insert_into_extract | ||
// CHECK-SAME: %[[SOURCE:.+]]: tensor<8x?xf32> | ||
// CHECK: util.return %[[SOURCE]] | ||
|
||
// ----- | ||
|
||
util.func public @merge_constant_padding_dynamic(%arg0: tensor<?x?xf32>, %idx: index, %pad_value: f32) -> tensor<?x?xf32> { | ||
%pad0 = tensor.pad %arg0 low[%idx, 1] high[1, 0] { | ||
^bb0(%b0: index, %b1 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<?x?xf32> to tensor<?x?xf32> | ||
%pad1 = tensor.pad %pad0 low[0, 2] high[%idx, 2] { | ||
^bb0(%b2: index, %b3 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<?x?xf32> to tensor<?x?xf32> | ||
util.return %pad1 : tensor<?x?xf32> | ||
util.func public @fold_full_insert_into_empty( | ||
%source: tensor<8x?xf32>, | ||
%size: index) -> tensor<8x?xf32> { | ||
%empty = tensor.empty(%size) : tensor<8x?xf32> | ||
%insert = tensor.insert_slice %source into %empty [0, 0] [8, %size] [1, 1] : tensor<8x?xf32> into tensor<8x?xf32> | ||
util.return %insert : tensor<8x?xf32> | ||
} | ||
// CHECK-LABEL: util.func public @merge_constant_padding_dynamic | ||
// CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<?x?xf32> | ||
// CHECK-SAME: %[[IDX:[A-Za-z0-9]+]]: index | ||
// CHECK-SAME: %[[PADVAL:[A-Za-z0-9]+]]: f32 | ||
// CHECK: %[[HIGH:.+]] = affine.apply affine_map<()[s0] -> (s0 + 1)>()[%[[IDX]]] | ||
// CHECK: %[[PAD:.+]] = tensor.pad %[[ARG0]] low[%[[IDX]], 3] high[%[[HIGH]], 2] | ||
// CHECK: tensor.yield %[[PADVAL]] | ||
// CHECK: util.return %[[PAD]] | ||
|
||
// CHECK-LABEL: util.func public @fold_full_insert_into_empty | ||
// CHECK-SAME: %[[SOURCE:.+]]: tensor<8x?xf32> | ||
// CHECK: util.return %[[SOURCE]] | ||
|
||
// ----- | ||
|
||
util.func public @dont_merge_constant_padding_nofold(%arg0: tensor<2x3xf32>, %pad_value: f32) -> tensor<7x8xf32> { | ||
%pad0 = tensor.pad %arg0 low[1, 1] high[1, 0] { | ||
^bb0(%b0: index, %b1 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<2x3xf32> to tensor<4x4xf32> | ||
%pad1 = tensor.pad %pad0 nofold low[0, 2] high[3, 2] { | ||
^bb0(%b2: index, %b3 : index): | ||
tensor.yield %pad_value : f32 | ||
} : tensor<4x4xf32> to tensor<7x8xf32> | ||
util.return %pad1 : tensor<7x8xf32> | ||
util.func public @dont_fold_not_full_insert_into_empty( | ||
%source: tensor<8x?xf32>, | ||
%size1: index, %size2: index) -> tensor<8x?xf32> { | ||
%empty = tensor.empty(%size1) : tensor<8x?xf32> | ||
%insert = tensor.insert_slice %source into %empty [0, 0] [8, %size2] [1, 1] : tensor<8x?xf32> into tensor<8x?xf32> | ||
util.return %insert : tensor<8x?xf32> | ||
} | ||
|
||
// Verify that folding does not happen if it would drop a nofold attribute | ||
|
||
// CHECK-LABEL: util.func public @dont_merge_constant_padding_nofold | ||
// CHECK: tensor.pad | ||
// CHECK: tensor.pad {{.*}} nofold | ||
// CHECK-LABEL: util.func public @dont_fold_not_full_insert_into_empty | ||
// CHECK: %[[INSERT:.+]] = tensor.insert_slice | ||
// CHECK: util.return %[[INSERT]] | ||
|
||
// ----- | ||
|
||
util.func public @dont_merge_constant_padding_different_vals( | ||
%arg0: tensor<2x3xf32>, | ||
%pad_value0: f32, | ||
%pad_value1: f32) -> tensor<7x8xf32> { | ||
%pad0 = tensor.pad %arg0 low[1, 1] high[1, 0] { | ||
^bb0(%b0: index, %b1 : index): | ||
tensor.yield %pad_value0 : f32 | ||
} : tensor<2x3xf32> to tensor<4x4xf32> | ||
%pad1 = tensor.pad %pad0 nofold low[0, 2] high[3, 2] { | ||
^bb0(%b2: index, %b3 : index): | ||
tensor.yield %pad_value1 : f32 | ||
} : tensor<4x4xf32> to tensor<7x8xf32> | ||
util.return %pad1 : tensor<7x8xf32> | ||
util.func public @dont_fold_not_full_static_insert_into_empty( | ||
%source: tensor<8x?xf32>, | ||
%size: index) -> tensor<10x?xf32> { | ||
%empty = tensor.empty(%size) : tensor<10x?xf32> | ||
%insert = tensor.insert_slice %source into %empty [0, 0] [8, %size] [1, 1] : tensor<8x?xf32> into tensor<10x?xf32> | ||
util.return %insert : tensor<10x?xf32> | ||
} | ||
|
||
// Verify that folding does not happen if it would drop a nofold attribute | ||
|
||
// CHECK-LABEL: util.func public @dont_merge_constant_padding_different_vals | ||
// CHECK: tensor.pad | ||
// CHECK: tensor.pad | ||
// CHECK-LABEL: util.func public @dont_fold_not_full_static_insert_into_empty | ||
// CHECK: %[[INSERT:.+]] = tensor.insert_slice | ||
// CHECK: util.return %[[INSERT]] |