File: transform-op-hoist-pad-build-packing-loop-nest.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (174 lines) | stat: -rw-r--r-- 8,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// BUILD-PACKING-LOOP-NEST only checks the creation of packing code but does not connect it.
// Do not run canonicalization as it would be DCE'd away.
// RUN: mlir-opt --test-transform-dialect-interpreter -split-input-file --verify-diagnostics %s | FileCheck %s --check-prefix=BUILD-PACKING-LOOP-NEST

func.func @pad_and_hoist_rhs(
  %arg0: tensor<24x12xf32>, %arg1: tensor<12x25xf32>, %arg2: tensor<24x25xf32>)
     -> tensor<24x25xf32>
{
  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<24x12xf32>, tensor<12x25xf32>) outs(%arg2 : tensor<24x25xf32>) -> tensor<24x25xf32>
  func.return %0 : tensor<24x25xf32>
}

transform.sequence failures(propagate) {
^bb1(%arg1: !transform.any_op):
  %matmul = transform.structured.match ops{["linalg.matmul"]} in %arg1
    : (!transform.any_op) -> !transform.any_op

  %matmul_l1, %loops_l1 = transform.structured.tile_to_scf_for %matmul [5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %matmul_padded, %0 = transform.structured.pad %matmul_l1 {
    padding_values=[0.0: f32, 0.0 : f32, 0.0 : f32],
    padding_dimensions=[0, 1, 2]
  } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  // In this case, the pad op is actually empty: we only tile the first dimension
  // and it does not have an impact on the RHS operand.
  %pad = transform.get_producer_of_operand %matmul_padded[1]
    : (!transform.any_op) -> !transform.any_op

  // expected-error @below {{requires exactly 2 non-null handles}}
  transform.structured.hoist_pad.build_packing_loop_nest %pad above %loops_l1
     : (!transform.any_op, !transform.any_op) -> !transform.any_op
}

// -----

func.func @pad_and_hoist_init(
  %arg0: tensor<24x12xf32>, %arg1: tensor<12x25xf32>, %arg2: tensor<24x25xf32>)
     -> tensor<24x25xf32>
{
  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<24x12xf32>, tensor<12x25xf32>) outs(%arg2 : tensor<24x25xf32>) -> tensor<24x25xf32>
  func.return %0 : tensor<24x25xf32>
}

transform.sequence failures(propagate) {
^bb1(%arg1: !transform.any_op):
  %matmul = transform.structured.match ops{["linalg.matmul"]} in %arg1
    : (!transform.any_op) -> !transform.any_op

  %matmul_l1, %loops_l1 = transform.structured.tile_to_scf_for %matmul [5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %matmul_padded, %0 = transform.structured.pad %matmul_l1 {
    padding_values=[0.0: f32, 0.0 : f32, 0.0 : f32],
    padding_dimensions=[0, 1, 2]
  } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %pad = transform.get_producer_of_operand %matmul_padded[2]
    : (!transform.any_op) -> !transform.any_op

  // We do not know yet how to hoist the init.
  // expected-error @below {{could not build packing loop nest}}
  transform.structured.hoist_pad.build_packing_loop_nest %pad above %loops_l1
     : (!transform.any_op, !transform.any_op) -> !transform.any_op
}

// -----

//     BUILD-PACKING-LOOP-NEST-LABEL: pad_and_hoist_lhs
func.func @pad_and_hoist_lhs(
  %arg0: tensor<24x12xf32>, %arg1: tensor<12x25xf32>, %arg2: tensor<24x25xf32>)
     -> tensor<24x25xf32>
{
  //     BUILD-PACKING-LOOP-NEST: %[[PACKED:.*]] = scf.for %{{.*}} -> (tensor<?x5x12xf32>) {
  //     BUILD-PACKING-LOOP-NEST:   tensor.pad %{{.*}} 
  //     BUILD-PACKING-LOOP-NEST:     : tensor<?x12xf32> to tensor<5x12xf32>
  //     BUILD-PACKING-LOOP-NEST:   tensor.insert_slice %{{.*}} into %{{.*}}[%{{.*}}, 0, 0] [1, 5, 12] [1, 1, 1]
  // BUILD-PACKING-LOOP-NEST-SAME:   : tensor<5x12xf32> into tensor<?x5x12xf32>
  //     BUILD-PACKING-LOOP-NEST: scf.for %{{.*}} -> (tensor<24x25xf32>)
  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<24x12xf32>, tensor<12x25xf32>) outs(%arg2 : tensor<24x25xf32>) -> tensor<24x25xf32>
  func.return %0 : tensor<24x25xf32>
}

transform.sequence failures(propagate) {
^bb1(%arg1: !transform.any_op):
  %matmul = transform.structured.match ops{["linalg.matmul"]} in %arg1
    : (!transform.any_op) -> !transform.any_op

  %matmul_l1, %loops_l1 = transform.structured.tile_to_scf_for %matmul [5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %matmul_padded, %0 = transform.structured.pad %matmul_l1 {
    padding_values=[0.0: f32, 0.0 : f32, 0.0 : f32],
    padding_dimensions=[0, 1, 2]
  } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %pad = transform.get_producer_of_operand %matmul_padded[0]
    : (!transform.any_op) -> !transform.any_op

  transform.structured.hoist_pad.build_packing_loop_nest %pad above %loops_l1
     : (!transform.any_op, !transform.any_op) -> !transform.any_op
}

// -----

//     BUILD-PACKING-LOOP-NEST-LABEL: pad_and_hoist_lhs_transpose
func.func @pad_and_hoist_lhs_transpose(
  %arg0: tensor<24x12xf32>, %arg1: tensor<12x25xf32>, %arg2: tensor<24x25xf32>)
     -> tensor<24x25xf32>
{
  //     BUILD-PACKING-LOOP-NEST: %[[PACKED:.*]] = scf.for %{{.*}} -> (tensor<?x12x5xf32>) {
  //     BUILD-PACKING-LOOP-NEST:   tensor.pad %{{.*}}
  //     BUILD-PACKING-LOOP-NEST:     : tensor<?x12xf32> to tensor<5x12xf32>
  //     BUILD-PACKING-LOOP-NEST:   linalg.generic
  //     BUILD-PACKING-LOOP-NEST:     -> tensor<12x5xf32>
  //     BUILD-PACKING-LOOP-NEST:   tensor.insert_slice %{{.*}} into %{{.*}}[%{{.*}}, 0, 0] [1, 12, 5] [1, 1, 1]
  // BUILD-PACKING-LOOP-NEST-SAME:   : tensor<12x5xf32> into tensor<?x12x5xf32>
  //     BUILD-PACKING-LOOP-NEST: scf.for %{{.*}} -> (tensor<24x25xf32>)
  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<24x12xf32>, tensor<12x25xf32>) outs(%arg2 : tensor<24x25xf32>) -> tensor<24x25xf32>
  func.return %0 : tensor<24x25xf32>
}

transform.sequence failures(propagate) {
^bb1(%arg1: !transform.any_op):
  %matmul = transform.structured.match ops{["linalg.matmul"]} in %arg1
    : (!transform.any_op) -> !transform.any_op

  %matmul_l1, %loops_l1 = transform.structured.tile_to_scf_for %matmul [5] : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %matmul_padded, %0 = transform.structured.pad %matmul_l1 {
    padding_values=[0.0: f32, 0.0 : f32, 0.0 : f32],
    padding_dimensions=[0, 1, 2]
  } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %pad = transform.get_producer_of_operand %matmul_padded[0]
    : (!transform.any_op) -> !transform.any_op

  transform.structured.hoist_pad.build_packing_loop_nest %pad above %loops_l1, transpose by [1, 0]
     : (!transform.any_op, !transform.any_op) -> !transform.any_op
}

// -----

//     BUILD-PACKING-LOOP-NEST-LABEL: pad_and_hoist_init
func.func @pad_and_hoist_init(
  %arg0: tensor<24x12xf32>, %arg1: tensor<12x25xf32>, %arg2: tensor<24x25xf32>)
     -> tensor<24x25xf32>
{

  //      BUILD-PACKING-LOOP-NEST: scf.for %{{.*}} -> (tensor<24x25xf32>) {
  //      BUILD-PACKING-LOOP-NEST:   %[[EXTRACTED_SLICE:.*]] = tensor.extract_slice
  //      BUILD-PACKING-LOOP-NEST:   %[[PADDED:.*]] = tensor.pad %[[EXTRACTED_SLICE]]
  //      BUILD-PACKING-LOOP-NEST:     : tensor<?x25xf32> to tensor<5x25xf32>
  //      BUILD-PACKING-LOOP-NEST:   scf.for %{{.*}} iter_args({{.*}} = %[[EXTRACTED_SLICE]]) -> (tensor<24x25xf32>, tensor<?x25xf32>) {
  %0 = linalg.matmul ins(%arg0, %arg1 : tensor<24x12xf32>, tensor<12x25xf32>) outs(%arg2 : tensor<24x25xf32>) -> tensor<24x25xf32>
  func.return %0 : tensor<24x25xf32>
}

transform.sequence failures(propagate) {
^bb1(%arg1: !transform.any_op):
  %matmul = transform.structured.match ops{["linalg.matmul"]} in %arg1
    : (!transform.any_op) -> !transform.any_op

  %matmul_l1, %loops_l1:2 = transform.structured.tile_to_scf_for %matmul [5, 0, 7] : (!transform.any_op) -> (!transform.any_op, !transform.any_op, !transform.any_op)

  %matmul_padded, %0 = transform.structured.pad %matmul_l1 {
    padding_values=[0.0: f32, 0.0 : f32, 0.0 : f32],
    padding_dimensions=[0, 1, 2]
  } : (!transform.any_op) -> (!transform.any_op, !transform.any_op)

  %pad = transform.get_producer_of_operand %matmul_padded[2]
    : (!transform.any_op) -> !transform.any_op

  transform.structured.hoist_pad.build_packing_loop_nest %pad above %loops_l1#1
     : (!transform.any_op, !transform.any_op) -> !transform.any_op
}