canonicalize-block-merge.mlir
5.4 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline='func(canonicalize)' -split-input-file | FileCheck %s
// Check the simple case of single operation blocks with a return.
// CHECK-LABEL: func @return_blocks(
func @return_blocks() {
// CHECK: "foo.cond_br"()[^bb1, ^bb1]
// CHECK: ^bb1:
// CHECK-NEXT: return
// CHECK-NOT: ^bb2
"foo.cond_br"() [^bb1, ^bb2] : () -> ()
^bb1:
return
^bb2:
return
}
// Check the case of identical blocks with matching arguments.
// CHECK-LABEL: func @matching_arguments(
func @matching_arguments() -> i32 {
// CHECK: "foo.cond_br"()[^bb1, ^bb1]
// CHECK: ^bb1(%{{.*}}: i32):
// CHECK-NEXT: return
// CHECK-NOT: ^bb2
"foo.cond_br"() [^bb1, ^bb2] : () -> ()
^bb1(%arg0 : i32):
return %arg0 : i32
^bb2(%arg1 : i32):
return %arg1 : i32
}
// Check that no merging occurs if there is an operand mismatch and we can't
// update th predecessor.
// CHECK-LABEL: func @mismatch_unknown_terminator
func @mismatch_unknown_terminator(%arg0 : i32, %arg1 : i32) -> i32 {
// CHECK: "foo.cond_br"()[^bb1, ^bb2]
"foo.cond_br"() [^bb1, ^bb2] : () -> ()
^bb1:
return %arg0 : i32
^bb2:
return %arg1 : i32
}
// Check that merging does occurs if there is an operand mismatch and we can
// update th predecessor.
// CHECK-LABEL: func @mismatch_operands
// CHECK-SAME: %[[COND:.*]]: i1, %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
func @mismatch_operands(%cond : i1, %arg0 : i32, %arg1 : i32) -> i32 {
// CHECK: %[[RES:.*]] = select %[[COND]], %[[ARG0]], %[[ARG1]]
// CHECK: return %[[RES]]
cond_br %cond, ^bb1, ^bb2
^bb1:
return %arg0 : i32
^bb2:
return %arg1 : i32
}
// Check the same as above, but with pre-existing arguments.
// CHECK-LABEL: func @mismatch_operands_matching_arguments(
// CHECK-SAME: %[[COND:.*]]: i1, %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
func @mismatch_operands_matching_arguments(%cond : i1, %arg0 : i32, %arg1 : i32) -> (i32, i32) {
// CHECK: %[[RES0:.*]] = select %[[COND]], %[[ARG1]], %[[ARG0]]
// CHECK: %[[RES1:.*]] = select %[[COND]], %[[ARG0]], %[[ARG1]]
// CHECK: return %[[RES1]], %[[RES0]]
cond_br %cond, ^bb1(%arg1 : i32), ^bb2(%arg0 : i32)
^bb1(%arg2 : i32):
return %arg0, %arg2 : i32, i32
^bb2(%arg3 : i32):
return %arg1, %arg3 : i32, i32
}
// Check that merging does not occur if the uses of the arguments differ.
// CHECK-LABEL: func @mismatch_argument_uses(
func @mismatch_argument_uses(%cond : i1, %arg0 : i32, %arg1 : i32) -> (i32, i32) {
// CHECK: cond_br %{{.*}}, ^bb1(%{{.*}}), ^bb2
cond_br %cond, ^bb1(%arg1 : i32), ^bb2(%arg0 : i32)
^bb1(%arg2 : i32):
return %arg0, %arg2 : i32, i32
^bb2(%arg3 : i32):
return %arg3, %arg1 : i32, i32
}
// Check that merging does not occur if the types of the arguments differ.
// CHECK-LABEL: func @mismatch_argument_types(
func @mismatch_argument_types(%cond : i1, %arg0 : i32, %arg1 : i16) {
// CHECK: cond_br %{{.*}}, ^bb1(%{{.*}}), ^bb2
cond_br %cond, ^bb1(%arg0 : i32), ^bb2(%arg1 : i16)
^bb1(%arg2 : i32):
"foo.return"(%arg2) : (i32) -> ()
^bb2(%arg3 : i16):
"foo.return"(%arg3) : (i16) -> ()
}
// Check that merging does not occur if the number of the arguments differ.
// CHECK-LABEL: func @mismatch_argument_count(
func @mismatch_argument_count(%cond : i1, %arg0 : i32) {
// CHECK: cond_br %{{.*}}, ^bb1(%{{.*}}), ^bb2
cond_br %cond, ^bb1(%arg0 : i32), ^bb2
^bb1(%arg2 : i32):
"foo.return"(%arg2) : (i32) -> ()
^bb2:
"foo.return"() : () -> ()
}
// Check that merging does not occur if the operations differ.
// CHECK-LABEL: func @mismatch_operations(
func @mismatch_operations(%cond : i1) {
// CHECK: cond_br %{{.*}}, ^bb1, ^bb2
cond_br %cond, ^bb1, ^bb2
^bb1:
"foo.return"() : () -> ()
^bb2:
return
}
// Check that merging does not occur if the number of operations differ.
// CHECK-LABEL: func @mismatch_operation_count(
func @mismatch_operation_count(%cond : i1) {
// CHECK: cond_br %{{.*}}, ^bb1, ^bb2
cond_br %cond, ^bb1, ^bb2
^bb1:
"foo.op"() : () -> ()
return
^bb2:
return
}
// Check that merging does not occur if the blocks contain regions.
// CHECK-LABEL: func @contains_regions(
func @contains_regions(%cond : i1) {
// CHECK: cond_br %{{.*}}, ^bb1, ^bb2
cond_br %cond, ^bb1, ^bb2
^bb1:
scf.if %cond {
"foo.op"() : () -> ()
}
return
^bb2:
scf.if %cond {
"foo.op"() : () -> ()
}
return
}
// Check that properly handles back edges and the case where a value from one
// block is used in another.
// CHECK-LABEL: func @mismatch_loop(
// CHECK-SAME: %[[ARG:.*]]: i1
func @mismatch_loop(%cond : i1) {
// CHECK: cond_br %{{.*}}, ^bb1(%[[ARG]] : i1), ^bb2
cond_br %cond, ^bb2, ^bb3
^bb1:
// CHECK: ^bb1(%[[ARG2:.*]]: i1):
// CHECK-NEXT: %[[LOOP_CARRY:.*]] = "foo.op"
// CHECK-NEXT: cond_br %[[ARG2]], ^bb1(%[[LOOP_CARRY]] : i1), ^bb2
%ignored = "foo.op"() : () -> (i1)
cond_br %cond2, ^bb1, ^bb3
^bb2:
%cond2 = "foo.op"() : () -> (i1)
cond_br %cond, ^bb1, ^bb3
^bb3:
// CHECK: ^bb2:
// CHECK-NEXT: return
return
}
// Check that blocks are not merged if the types of the operands differ.
// CHECK-LABEL: func @mismatch_operand_types(
func @mismatch_operand_types(%arg0 : i1, %arg1 : memref<i32>, %arg2 : memref<i1>) {
%c0_i32 = constant 0 : i32
%true = constant true
br ^bb1
^bb1:
cond_br %arg0, ^bb2, ^bb3
^bb2:
// CHECK: store %{{.*}}, %{{.*}} : memref<i32>
store %c0_i32, %arg1[] : memref<i32>
br ^bb1
^bb3:
// CHECK: store %{{.*}}, %{{.*}} : memref<i1>
store %true, %arg2[] : memref<i1>
br ^bb1
}