-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GPU] Add pattern to fuse tensor.extract_slice into forall producer
Signed-off-by: Max Dawkins <[email protected]>
- Loading branch information
Max Dawkins
committed
Nov 26, 2024
1 parent
4eb7167
commit 2f5056e
Showing
8 changed files
with
409 additions
and
0 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
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
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
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
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
60 changes: 60 additions & 0 deletions
60
...odegen/Dialect/GPU/TransformExtensions/test/transform_fuse_extract_slice_with_forall.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// RUN: iree-opt %s -iree-transform-dialect-interpreter -transform-dialect-drop-schedule -canonicalize -cse --split-input-file | FileCheck %s | ||
|
||
#map = affine_map<(d0) -> (d0 * 2)> | ||
module { | ||
func.func @fuse_extract_slice_with_forall(%arg0: tensor<8xf32>, %arg1: index) -> tensor<?xf32> { | ||
%0 = tensor.empty() : tensor<8xf32> | ||
%1 = scf.forall (%arg2) in (4) shared_outs(%arg3 = %0) -> (tensor<8xf32>) { | ||
%2 = affine.apply #map(%arg2) | ||
%extracted_slice_0 = tensor.extract_slice %arg0[%2] [2] [1] : tensor<8xf32> to tensor<2xf32> | ||
%extracted_slice_1 = tensor.extract_slice %arg3[%2] [2] [1] : tensor<8xf32> to tensor<2xf32> | ||
%3 = linalg.copy ins(%extracted_slice_0 : tensor<2xf32>) outs(%extracted_slice_1 : tensor<2xf32>) -> tensor<2xf32> | ||
scf.forall.in_parallel { | ||
tensor.parallel_insert_slice %3 into %arg3[%2] [2] [1] : tensor<2xf32> into tensor<8xf32> | ||
} | ||
} {mapping = [#gpu.thread<x>]} | ||
%extracted_slice = tensor.extract_slice %1[0] [%arg1] [1] : tensor<8xf32> to tensor<?xf32> | ||
return %extracted_slice : tensor<?xf32> | ||
} | ||
} | ||
|
||
module attributes {transform.with_named_sequence} { | ||
transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) { | ||
%producer = transform.structured.match ops{["scf.forall"]} in %arg0 : (!transform.any_op) -> !transform.any_op | ||
%consumer = transform.get_consumers_of_result %producer[0] : (!transform.any_op) -> !transform.any_op | ||
%2 = transform.iree.fuse_extract_slice_with_forall %consumer into %producer | ||
: (!transform.any_op, !transform.any_op) -> !transform.any_op | ||
transform.yield | ||
} | ||
} | ||
|
||
// CHECK-DAG: #[[$MAP:.+]] = affine_map<(d0) -> (d0 * 2)> | ||
// CHECK-DAG: #[[$MAP1:.+]] = affine_map<(d0)[s0] -> (d0 * -2 + s0, 2)> | ||
// CHECK-DAG: #[[$MAP2:.+]] = affine_map<(d0) -> (0, d0)> | ||
// CHECK-DAG: #[[$MAP3:.+]] = affine_map<(d0) -> (-d0 + 2)> | ||
|
||
// CHECK-LABEL: func @fuse_extract_slice_with_forall | ||
// CHECK-SAME: %[[ARG0:[A-Za-z0-9]+]]: tensor<8xf32> | ||
// CHECK-SAME: %[[ARG1:[A-Za-z0-9]+]]: index | ||
|
||
// CHECK-DAG: %[[ZERO:.+]] = arith.constant 0.000000e+00 : f32 | ||
// CHECK-DAG: %[[EMPTY:.+]] = tensor.empty() : tensor<8xf32> | ||
// CHECK-DAG: %[[SLICED_OUT:.+]] = tensor.extract_slice %[[EMPTY]][0] [%[[ARG1]]] [1] : tensor<8xf32> to tensor<?xf32> | ||
// CHECK: %[[FORALL_RESULT:.+]] = scf.forall (%[[IDX:.+]]) in (4) shared_outs(%[[SLICED_BBARG:.+]] = %[[SLICED_OUT]]) -> (tensor<?xf32>) { | ||
// CHECK-DAG: %[[SLICE_IDX:.+]] = affine.apply #[[$MAP]](%[[IDX]]) | ||
// CHECK-DAG: %[[SIZE_CLAMPED_HIGH:.+]] = affine.min #[[$MAP1]](%[[IDX]])[%[[ARG1]]] | ||
// CHECK-DAG: %[[SIZE_CLAMPED_LOW:.+]] = affine.max #[[$MAP2]](%[[SIZE_CLAMPED_HIGH]]) | ||
// CHECK-DAG: %[[IN_SLICE:.+]] = tensor.extract_slice %[[ARG0]][%[[SLICE_IDX]]] [2] [1] : tensor<8xf32> to tensor<2xf32> | ||
// CHECK-DAG: %[[OUT_SLICE:.+]] = tensor.extract_slice %[[SLICED_BBARG]][%[[SLICE_IDX]]] [%[[SIZE_CLAMPED_LOW]]] [1] : tensor<?xf32> to tensor<?xf32> | ||
// CHECK-DAG: %[[PAD_HIGH:.+]] = affine.apply #[[$MAP3]](%[[SIZE_CLAMPED_LOW]]) | ||
// CHECK: %[[PADDED_OUT_SLICE:.+]] = tensor.pad %[[OUT_SLICE]] low[0] high[%[[PAD_HIGH]]] { | ||
// CHECK: ^bb0({{.*}}): | ||
// CHECK: tensor.yield %[[ZERO]] : f32 | ||
// CHECK: } : tensor<?xf32> to tensor<2xf32> | ||
// CHECK: %[[COPY:.+]] = linalg.copy ins(%[[IN_SLICE]] : tensor<2xf32>) outs(%[[PADDED_OUT_SLICE]] : tensor<2xf32>) -> tensor<2xf32> | ||
// CHECK: %[[SLICED_COPY:.+]] = tensor.extract_slice %[[COPY]][0] [%[[SIZE_CLAMPED_LOW]]] [1] : tensor<2xf32> to tensor<?xf32> | ||
// CHECK: scf.forall.in_parallel { | ||
// CHECK: tensor.parallel_insert_slice %[[SLICED_COPY]] into %[[SLICED_BBARG]][%[[SLICE_IDX]]] [%[[SIZE_CLAMPED_LOW]]] [1] : tensor<?xf32> into tensor<?xf32> | ||
// CHECK: } | ||
// CHECK: } {mapping = [#gpu.thread<x>]} | ||
// CHECK: return %[[FORALL_RESULT]] |
Oops, something went wrong.