xref: /llvm-project/mlir/test/Dialect/Linalg/transform-op-interchange.mlir (revision e4384149b58f7c3d19c5d38bc46038c660b77ca9)
1// RUN: mlir-opt %s -transform-interpreter -split-input-file -verify-diagnostics | FileCheck %s
2
3//       CHECK: #[[$MAP:.*]] = affine_map<(d0, d1) -> (d1, d0)>
4
5// CHECK-LABEL: @interchange_generic
6func.func @interchange_generic(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>) -> tensor<?x?xf32> {
7
8  //      CHECK:   linalg.generic
9  // CHECK-SAME:   indexing_maps = [#[[$MAP]], #[[$MAP]]
10  %0 = linalg.generic {
11    indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>],
12    iterator_types = ["parallel", "parallel"]
13  } ins(%arg0 : tensor<?x?xf32>) outs(%arg1 : tensor<?x?xf32>) {
14  ^bb0(%arg2: f32, %arg3: f32):
15    %1 = math.exp %arg2 : f32
16    linalg.yield %1 : f32
17  } -> tensor<?x?xf32>
18  return %0 : tensor<?x?xf32>
19}
20
21module attributes {transform.with_named_sequence} {
22  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
23    %0 = transform.structured.match ops{["linalg.generic"]} in %arg1 : (!transform.any_op) -> !transform.any_op
24    transform.structured.interchange %0 iterator_interchange = [1, 0] : (!transform.any_op) -> !transform.any_op
25    transform.yield
26  }
27}
28
29// -----
30
31func.func @interchange_matmul(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>, %arg2: tensor<?x?xf32>) -> tensor<?x?xf32> {
32  // expected-note @below {{when applied to this op}}
33  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%arg2 : tensor<?x?xf32>) -> tensor<?x?xf32>
34  return %0 : tensor<?x?xf32>
35}
36
37module attributes {transform.with_named_sequence} {
38  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
39    %0 = transform.structured.match ops{["linalg.matmul"]} in %arg1 : (!transform.any_op) -> !transform.any_op
40    // expected-error @below {{transform applied to the wrong op kind}}
41    transform.structured.interchange %0 iterator_interchange = [1, 0] : (!transform.any_op) -> !transform.any_op
42    transform.yield
43  }
44}
45
46// -----
47
48func.func @too_many_iters(%0: tensor<?x?xf32>, %1: tensor<?x?xf32>) -> tensor<?x?xf32> {
49  %r = linalg.generic {
50    indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>],
51    iterator_types = ["parallel", "parallel"]
52  } ins(%0: tensor<?x?xf32>) outs(%1: tensor<?x?xf32>) {
53  ^bb0(%2: f32, %3: f32):
54    %4 = arith.mulf %2, %2 : f32
55    linalg.yield %4 : f32
56  } -> tensor<?x?xf32>
57  return %r : tensor<?x?xf32>
58}
59
60module attributes {transform.with_named_sequence} {
61  transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) {
62    %0 = transform.structured.match ops{["linalg.generic"]} in %arg0 : (!transform.any_op) -> !transform.any_op
63    // expected-error @below {{"iterator_interchange" has length (3) different from the number of loops in the target operation (2)}}
64    transform.structured.interchange %0 iterator_interchange = [2,1,0] : (!transform.any_op) -> !transform.any_op
65    transform.yield
66  }
67}
68