canonicalize-cf.mlir
4.2 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
// RUN: mlir-opt %s -allow-unregistered-dialect -pass-pipeline='func(canonicalize)' -split-input-file | FileCheck %s
/// Test the folding of BranchOp.
// CHECK-LABEL: func @br_folding(
func @br_folding() -> i32 {
// CHECK-NEXT: %[[CST:.*]] = constant 0 : i32
// CHECK-NEXT: return %[[CST]] : i32
%c0_i32 = constant 0 : i32
br ^bb1(%c0_i32 : i32)
^bb1(%x : i32):
return %x : i32
}
/// Test that pass-through successors of BranchOp get folded.
// CHECK-LABEL: func @br_passthrough(
// CHECK-SAME: %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
func @br_passthrough(%arg0 : i32, %arg1 : i32) -> (i32, i32) {
"foo.switch"() [^bb1, ^bb2, ^bb3] : () -> ()
^bb1:
// CHECK: ^bb1:
// CHECK-NEXT: br ^bb3(%[[ARG0]], %[[ARG1]] : i32, i32)
br ^bb2(%arg0 : i32)
^bb2(%arg2 : i32):
br ^bb3(%arg2, %arg1 : i32, i32)
^bb3(%arg4 : i32, %arg5 : i32):
return %arg4, %arg5 : i32, i32
}
/// Test the folding of CondBranchOp with a constant condition.
// CHECK-LABEL: func @cond_br_folding(
func @cond_br_folding(%cond : i1, %a : i32) {
// CHECK-NEXT: return
%false_cond = constant false
%true_cond = constant true
cond_br %cond, ^bb1, ^bb2(%a : i32)
^bb1:
cond_br %true_cond, ^bb3, ^bb2(%a : i32)
^bb2(%x : i32):
cond_br %false_cond, ^bb2(%x : i32), ^bb3
^bb3:
return
}
/// Test the folding of CondBranchOp when the successors are identical.
// CHECK-LABEL: func @cond_br_same_successor(
func @cond_br_same_successor(%cond : i1, %a : i32) {
// CHECK-NEXT: return
cond_br %cond, ^bb1(%a : i32), ^bb1(%a : i32)
^bb1(%result : i32):
return
}
/// Test the folding of CondBranchOp when the successors are identical, but the
/// arguments are different.
// CHECK-LABEL: func @cond_br_same_successor_insert_select(
// CHECK-SAME: %[[COND:.*]]: i1, %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32
// CHECK-SAME: %[[ARG2:.*]]: tensor<2xi32>, %[[ARG3:.*]]: tensor<2xi32>
func @cond_br_same_successor_insert_select(
%cond : i1, %a : i32, %b : i32, %c : tensor<2xi32>, %d : tensor<2xi32>
) -> (i32, tensor<2xi32>) {
// CHECK: %[[RES:.*]] = select %[[COND]], %[[ARG0]], %[[ARG1]]
// CHECK: %[[RES2:.*]] = select %[[COND]], %[[ARG2]], %[[ARG3]]
// CHECK: return %[[RES]], %[[RES2]]
cond_br %cond, ^bb1(%a, %c : i32, tensor<2xi32>), ^bb1(%b, %d : i32, tensor<2xi32>)
^bb1(%result : i32, %result2 : tensor<2xi32>):
return %result, %result2 : i32, tensor<2xi32>
}
/// Test the compound folding of BranchOp and CondBranchOp.
// CHECK-LABEL: func @cond_br_and_br_folding(
func @cond_br_and_br_folding(%a : i32) {
// CHECK-NEXT: return
%false_cond = constant false
%true_cond = constant true
cond_br %true_cond, ^bb2, ^bb1(%a : i32)
^bb1(%x : i32):
cond_br %false_cond, ^bb1(%x : i32), ^bb2
^bb2:
return
}
/// Test that pass-through successors of CondBranchOp get folded.
// CHECK-LABEL: func @cond_br_passthrough(
// CHECK-SAME: %[[ARG0:.*]]: i32, %[[ARG1:.*]]: i32, %[[ARG2:.*]]: i32, %[[COND:.*]]: i1
func @cond_br_passthrough(%arg0 : i32, %arg1 : i32, %arg2 : i32, %cond : i1) -> (i32, i32) {
// CHECK: %[[RES:.*]] = select %[[COND]], %[[ARG0]], %[[ARG2]]
// CHECK: %[[RES2:.*]] = select %[[COND]], %[[ARG1]], %[[ARG2]]
// CHECK: return %[[RES]], %[[RES2]]
cond_br %cond, ^bb1(%arg0 : i32), ^bb2(%arg2, %arg2 : i32, i32)
^bb1(%arg3: i32):
br ^bb2(%arg3, %arg1 : i32, i32)
^bb2(%arg4: i32, %arg5: i32):
return %arg4, %arg5 : i32, i32
}
/// Test the failure modes of collapsing CondBranchOp pass-throughs successors.
// CHECK-LABEL: func @cond_br_pass_through_fail(
func @cond_br_pass_through_fail(%cond : i1) {
// CHECK: cond_br %{{.*}}, ^bb1, ^bb2
cond_br %cond, ^bb1, ^bb2
^bb1:
// CHECK: ^bb1:
// CHECK: "foo.op"
// CHECK: br ^bb2
// Successors can't be collapsed if they contain other operations.
"foo.op"() : () -> ()
br ^bb2
^bb2:
return
}
// -----
// Erase assertion if condition is known to be true at compile time.
// CHECK-LABEL: @assert_true
func @assert_true() {
// CHECK-NOT: assert
%true = constant true
assert %true, "Computer says no"
return
}
// -----
// Keep assertion if condition unknown at compile time.
// CHECK-LABEL: @assert
// CHECK-SAME: (%[[ARG:.*]]: i1)
func @assert(%arg : i1) {
// CHECK: assert %[[ARG]], "Computer says no"
assert %arg, "Computer says no"
return
}