xref: /llvm-project/mlir/test/Dialect/Tosa/transpose-fold.mlir (revision da4d191ffe76efdfc3a1c67c438cb721fad83511)
1// RUN: mlir-opt %s --canonicalize -split-input-file | FileCheck %s
2
3// CHECK-LABEL:   func.func @test_cancel_transpose_transpose(
4// CHECK-SAME:                                               %[[VAL_0:.*]]: tensor<1x2x3xi32>) -> tensor<1x2x3xi32> {
5// CHECK:           return %[[VAL_0]] : tensor<1x2x3xi32>
6// CHECK:         }
7
8func.func @test_cancel_transpose_transpose(%arg0: tensor<1x2x3xi32>) -> (tensor<1x2x3xi32>) {
9	%0 = arith.constant dense<[1, 2, 0]> : tensor<3xi32>
10	%1 = tosa.transpose %arg0, %0 : (tensor<1x2x3xi32>, tensor<3xi32>) -> tensor<2x3x1xi32>
11	%2 = arith.constant dense<[2, 0, 1]> : tensor<3xi32>
12	%3 = tosa.transpose %1, %2 : (tensor<2x3x1xi32>, tensor<3xi32>) -> tensor<1x2x3xi32>
13  return %3 : tensor<1x2x3xi32>
14}
15
16// -----
17
18// CHECK-LABEL:   func.func @test_remove_identity_transpose(
19// CHECK-SAME:                                              %[[VAL_0:.*]]: tensor<1x2x3xi32>) -> tensor<1x2x3xi32> {
20// CHECK:           return %[[VAL_0]] : tensor<1x2x3xi32>
21// CHECK:         }
22
23func.func @test_remove_identity_transpose(%arg0: tensor<1x2x3xi32>) -> (tensor<1x2x3xi32>) {
24	%0 = arith.constant dense<[0, 1, 2]> : tensor<3xi32>
25	%1 = tosa.transpose %arg0, %0 : (tensor<1x2x3xi32>, tensor<3xi32>) -> tensor<1x2x3xi32>
26  return %1 : tensor<1x2x3xi32>
27}
28
29// -----
30
31// CHECK-LABEL:   func.func @test_do_not_cancel_different_transpose(
32// CHECK-SAME:                                                      %[[VAL_0:.*]]: tensor<2x3x4x5xi32>) -> tensor<5x4x3x2xi32> {
33// CHECK:           %[[VAL_1:.*]] = arith.constant dense<[3, 2, 1, 0]> : tensor<4xi32>
34// CHECK:           %[[VAL_2:.*]] = tosa.transpose %[[VAL_0]], %[[VAL_1]] : (tensor<2x3x4x5xi32>, tensor<4xi32>) -> tensor<5x4x3x2xi32>
35// CHECK:           return %[[VAL_2]] : tensor<5x4x3x2xi32>
36// CHECK:         }
37
38func.func @test_do_not_cancel_different_transpose(%arg0: tensor<2x3x4x5xi32>) -> (tensor<5x4x3x2xi32>) {
39	%0 = arith.constant dense<[1, 2, 0, 3]> : tensor<4xi32>
40	%1 = tosa.transpose %arg0, %0 : (tensor<2x3x4x5xi32>, tensor<4xi32>) -> tensor<3x4x2x5xi32>
41	%2 = arith.constant dense<[3, 1, 0, 2]> : tensor<4xi32>
42	%3 = tosa.transpose %1, %2 : (tensor<3x4x2x5xi32>, tensor<4xi32>) -> tensor<5x4x3x2xi32>
43  return %3 : tensor<5x4x3x2xi32>
44}
45
46// -----
47
48// CHECK-LABEL:   func.func @test_prefer_compose_transpose(
49// CHECK-SAME:                                                      %[[VAL_0:.*]]: tensor<1x2x3x4xi32>) -> tensor<4x3x2x1xi32> {
50// CHECK:           %[[VAL_1:.*]] = arith.constant dense<[3, 2, 1, 0]> : tensor<4xi32>
51// CHECK:           %[[VAL_2:.*]] = tosa.transpose %[[VAL_0]], %[[VAL_1]] : (tensor<1x2x3x4xi32>, tensor<4xi32>) -> tensor<4x3x2x1xi32>
52// CHECK:           return %[[VAL_2]] : tensor<4x3x2x1xi32>
53// CHECK:         }
54
55func.func @test_prefer_compose_transpose(%arg0: tensor<1x2x3x4xi32>) -> (tensor<4x3x2x1xi32>) {
56	%0 = arith.constant dense<[1, 2, 0, 3]> : tensor<4xi32>
57	%1 = tosa.transpose %arg0, %0 : (tensor<1x2x3x4xi32>, tensor<4xi32>) -> tensor<2x3x1x4xi32>
58	%2 = arith.constant dense<[3, 1, 0, 2]> : tensor<4xi32>
59	%3 = tosa.transpose %1, %2 : (tensor<2x3x1x4xi32>, tensor<4xi32>) -> tensor<4x3x2x1xi32>
60  return %3 : tensor<4x3x2x1xi32>
61}
62