xref: /llvm-project/mlir/test/Dialect/Linalg/flatten-elementwise.mlir (revision 97069a86193a617a9e4cf742a29db6116b2bf449)
1// RUN: mlir-opt %s -transform-interpreter -split-input-file | FileCheck %s
2
3// CHECK-LABEL: func.func @fill(
4// CHECK-SAME:                  %[[ARG0:.*]]: f32,
5// CHECK-SAME:                  %[[ARG1:.*]]: memref<32x7xf32>
6// CHECK-NEXT:    %[[FLATTENED:.*]] = memref.collapse_shape %[[ARG1]] {{\[}}[0, 1]]
7// CHECK-NEXT:    linalg.fill ins(%[[ARG0]] : f32) outs(%[[FLATTENED]] : memref<224xf32>)
8func.func @fill(%cst: f32, %arg: memref<32x7xf32>) {
9    linalg.fill ins(%cst: f32) outs(%arg: memref<32x7xf32>)
10    return
11}
12
13module attributes {transform.with_named_sequence} {
14  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
15    %0 = transform.structured.match interface{LinalgOp} in %arg1 : (!transform.any_op) -> !transform.any_op
16    %flattened = transform.structured.flatten_elementwise %0
17      : (!transform.any_op) -> !transform.any_op
18    transform.yield
19  }
20}
21
22// -----
23
24// CHECK-LABEL: func.func @fill_tensor(
25// CHECK-SAME:                         %[[ARG0:.*]]: f32,
26// CHECK-SAME:                         %[[ARG1:.*]]: tensor<32x7xf32>
27// CHECK-NEXT:    %[[FLATTENED:.*]] = tensor.collapse_shape %[[ARG1]] {{\[}}[0, 1]]
28// CHECK-NEXT:    %[[FLATTENED_RESULT:.*]] = linalg.fill ins(%[[ARG0]] : f32) outs(%[[FLATTENED]] : tensor<224xf32>)
29// CHECK-NEXT:    %[[RESULT:.*]] = tensor.expand_shape %[[FLATTENED_RESULT]] {{\[}}[0, 1]] output_shape [32, 7] : tensor<224xf32> into tensor<32x7xf32>
30func.func @fill_tensor(%cst: f32, %arg: tensor<32x7xf32>) -> tensor<32x7xf32> {
31    %0 = linalg.fill ins(%cst: f32) outs(%arg: tensor<32x7xf32>) ->  tensor<32x7xf32>
32    return %0 :  tensor<32x7xf32>
33}
34
35module attributes {transform.with_named_sequence} {
36  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
37    %0 = transform.structured.match interface{LinalgOp} in %arg1 : (!transform.any_op) -> !transform.any_op
38    %flattened = transform.structured.flatten_elementwise %0
39      : (!transform.any_op) -> !transform.any_op
40    transform.yield
41  }
42}
43
44// -----
45
46// CHECK-LABEL: func.func @map(
47// CHECK-SAME:                 %[[ARG0:[a-zA-Z0-9_]*]]: memref<32x7xf32>
48// CHECK-SAME:                 %[[ARG1:[a-zA-Z0-9_]*]]: memref<32x7xf32>
49// CHECK-SAME:                 %[[ARG2:[a-zA-Z0-9_]*]]: memref<32x7xf32>
50// CHECK-NEXT:    %[[FLATTENED_0:.*]] = memref.collapse_shape %[[ARG0]] {{\[}}[0, 1]]
51// CHECK-NEXT:    %[[FLATTENED_1:.*]] = memref.collapse_shape %[[ARG1]] {{\[}}[0, 1]]
52// CHECK-NEXT:    %[[FLATTENED_2:.*]] = memref.collapse_shape %[[ARG2]] {{\[}}[0, 1]]
53// CHECK-NEXT:    linalg.map { arith.addf } ins(%[[FLATTENED_0]], %[[FLATTENED_1]] : memref<224xf32>, memref<224xf32>) outs(%[[FLATTENED_2]] : memref<224xf32>)
54func.func @map(%arg0: memref<32x7xf32>, %arg1: memref<32x7xf32>, %arg2: memref<32x7xf32>) {
55    linalg.map {arith.addf} ins(%arg0, %arg1: memref<32x7xf32>, memref<32x7xf32>) outs(%arg2: memref<32x7xf32>)
56    return
57}
58
59module attributes {transform.with_named_sequence} {
60  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
61    %0 = transform.structured.match interface{LinalgOp} in %arg1 : (!transform.any_op) -> !transform.any_op
62    %flattened = transform.structured.flatten_elementwise %0
63      : (!transform.any_op) -> !transform.any_op
64    transform.yield
65  }
66}
67
68// -----
69
70// CHECK-LABEL: func.func @map_already_flat(
71// CHECK-SAME:                 %[[ARG0:[a-zA-Z0-9_]*]]: memref<32xf32>
72// CHECK-SAME:                 %[[ARG1:[a-zA-Z0-9_]*]]: memref<32xf32>
73// CHECK-SAME:                 %[[ARG2:[a-zA-Z0-9_]*]]: memref<32xf32>
74// CHECK-NEXT:    linalg.map { arith.addf } ins(%[[ARG0]], %[[ARG1]] : memref<32xf32>, memref<32xf32>) outs(%[[ARG2]] : memref<32xf32>)
75func.func @map_already_flat(%arg0: memref<32xf32>, %arg1: memref<32xf32>, %arg2: memref<32xf32>) {
76    linalg.map {arith.addf} ins(%arg0, %arg1: memref<32xf32>, memref<32xf32>) outs(%arg2: memref<32xf32>)
77    return
78}
79
80module attributes {transform.with_named_sequence} {
81  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
82    %0 = transform.structured.match interface{LinalgOp} in %arg1 : (!transform.any_op) -> !transform.any_op
83    %flattened = transform.structured.flatten_elementwise %0
84      : (!transform.any_op) -> !transform.any_op
85    transform.yield
86  }
87}
88
89// -----
90
91// CHECK: #[[$MAP0:.*]] = affine_map<(d0) -> (d0)>
92// CHECK-LABEL: func.func @generic
93// CHECK-SAME:                 %[[ARG0:[a-zA-Z0-9_]*]]: memref<32x7xf32>
94// CHECK-SAME:                 %[[ARG1:[a-zA-Z0-9_]*]]: memref<32x7xf32>
95// CHECK-SAME:                 %[[ARG2:[a-zA-Z0-9_]*]]: memref<32x7xf32>
96// CHECK-NEXT:    %[[FLATTENED_0:.*]] = memref.collapse_shape %[[ARG0]] {{\[}}[0, 1]]
97// CHECK-NEXT:    %[[FLATTENED_1:.*]] = memref.collapse_shape %[[ARG1]] {{\[}}[0, 1]]
98// CHECK-NEXT:    %[[FLATTENED_2:.*]] = memref.collapse_shape %[[ARG2]] {{\[}}[0, 1]]
99// CHECK-NEXT:    linalg.generic {indexing_maps = [#[[$MAP0]], #[[$MAP0]], #[[$MAP0]]], iterator_types = ["parallel"]} ins(%[[FLATTENED_0]], %[[FLATTENED_1]] : memref<224xf32>, memref<224xf32>) outs(%[[FLATTENED_2]] : memref<224xf32>)
100// CHECK-NEXT:       ^bb0(%[[A:.*]]: f32, %[[B:.*]]: f32, %[[C:.*]]: f32)
101// CHECK-NEXT:         %[[SUM:.*]] = arith.addf %[[A]], %[[B]]
102// CHECK-NEXT:         linalg.yield %[[SUM]]
103#map = affine_map<(d0, d1) -> (d0, d1)>
104func.func @generic( %arg0: memref<32x7xf32>, %arg1: memref<32x7xf32>, %arg2: memref<32x7xf32>) {
105    linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel"]} ins(%arg0, %arg1: memref<32x7xf32>, memref<32x7xf32>) outs(%arg2: memref<32x7xf32>) {
106        ^bb0(%a: f32, %b: f32, %c: f32):
107            %0 = arith.addf %a, %b : f32
108            linalg.yield %0 : f32
109    }
110    return
111}
112
113module attributes {transform.with_named_sequence} {
114  transform.named_sequence @__transform_main(%arg1: !transform.any_op {transform.readonly}) {
115    %0 = transform.structured.match interface{LinalgOp} in %arg1 : (!transform.any_op) -> !transform.any_op
116    %flattened = transform.structured.flatten_elementwise %0
117      : (!transform.any_op) -> !transform.any_op
118    transform.yield
119  }
120}
121