aboutsummaryrefslogtreecommitdiff
path: root/mlir/test/Transforms/sccp-callgraph.mlir
blob: f31f749b6e75a361b11234e8392901cd9f82516b (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// RUN: mlir-opt -allow-unregistered-dialect %s -sccp -split-input-file | FileCheck %s
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(builtin.module(sccp))" -split-input-file | FileCheck %s --check-prefix=NESTED
// RUN: mlir-opt -allow-unregistered-dialect %s -pass-pipeline="builtin.module(func.func(sccp))" -split-input-file | FileCheck %s --check-prefix=FUNC

/// Check that a constant is properly propagated through the arguments and
/// results of a private function.

// CHECK-LABEL: func private @private(
func.func private @private(%arg0 : i32) -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: return %[[CST]] : i32

  return %arg0 : i32
}

// CHECK-LABEL: func @simple_private(
func.func @simple_private() -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: return %[[CST]] : i32

  %1 = arith.constant 1 : i32
  %result = call @private(%1) : (i32) -> i32
  return %result : i32
}

// -----

/// Check that a constant is properly propagated through the arguments and
/// results of a visible nested function.

// CHECK: func nested @nested(
func.func nested @nested(%arg0 : i32) -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: return %[[CST]] : i32

  return %arg0 : i32
}

// CHECK-LABEL: func @simple_nested(
func.func @simple_nested() -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: return %[[CST]] : i32

  %1 = arith.constant 1 : i32
  %result = call @nested(%1) : (i32) -> i32
  return %result : i32
}

// -----

/// Check that non-visible nested functions do not track arguments.
module {
  // NESTED-LABEL: module @nested_module
  module @nested_module attributes { sym_visibility = "public" } {

    // NESTED: func nested @nested(
    func.func nested @nested(%arg0 : i32) -> (i32, i32) {
      // NESTED: %[[CST:.*]] = arith.constant 1 : i32
      // NESTED: return %[[CST]], %arg0 : i32, i32

      %1 = arith.constant 1 : i32
      return %1, %arg0 : i32, i32
    }

    // NESTED: func @nested_not_all_uses_visible(
    func.func @nested_not_all_uses_visible() -> (i32, i32) {
      // NESTED: %[[CST:.*]] = arith.constant 1 : i32
      // NESTED: %[[CALL:.*]]:2 = call @nested
      // NESTED: return %[[CST]], %[[CALL]]#1 : i32, i32

      %1 = arith.constant 1 : i32
      %result:2 = call @nested(%1) : (i32) -> (i32, i32)
      return %result#0, %result#1 : i32, i32
    }
  }
}

// -----

/// Check that public functions do not track arguments.

// CHECK-LABEL: func @public(
func.func @public(%arg0 : i32) -> (i32, i32) {
  %1 = arith.constant 1 : i32
  return %1, %arg0 : i32, i32
}

// CHECK-LABEL: func @simple_public(
func.func @simple_public() -> (i32, i32) {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: %[[CALL:.*]]:2 = call @public
  // CHECK: return %[[CST]], %[[CALL]]#1 : i32, i32

  %1 = arith.constant 1 : i32
  %result:2 = call @public(%1) : (i32) -> (i32, i32)
  return %result#0, %result#1 : i32, i32
}

// -----

/// Check that functions with non-call users don't have arguments tracked.

func.func private @callable(%arg0 : i32) -> (i32, i32) {
  %1 = arith.constant 1 : i32
  return %1, %arg0 : i32, i32
}

// CHECK-LABEL: func @non_call_users(
func.func @non_call_users() -> (i32, i32) {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: %[[CALL:.*]]:2 = call @callable
  // CHECK: return %[[CST]], %[[CALL]]#1 : i32, i32

  %1 = arith.constant 1 : i32
  %result:2 = call @callable(%1) : (i32) -> (i32, i32)
  return %result#0, %result#1 : i32, i32
}

"live.user"() {uses = [@callable]} : () -> ()

// -----

/// Check that return values are overdefined in the presence of an unknown terminator.

func.func private @callable(%arg0 : i32) -> i32 {
  "unknown.return"(%arg0) : (i32) -> ()
}

// CHECK-LABEL: func @unknown_terminator(
func.func @unknown_terminator() -> i32 {
  // CHECK: %[[CALL:.*]] = call @callable
  // CHECK: return %[[CALL]] : i32

  %1 = arith.constant 1 : i32
  %result = call @callable(%1) : (i32) -> i32
  return %result : i32
}

// -----

/// Check that return values are overdefined when the constant conflicts.

func.func private @callable(%arg0 : i32) -> i32 {
  return %arg0 : i32
}

// CHECK-LABEL: func @conflicting_constant(
func.func @conflicting_constant() -> (i32, i32) {
  // CHECK: %[[CALL1:.*]] = call @callable
  // CHECK: %[[CALL2:.*]] = call @callable
  // CHECK: return %[[CALL1]], %[[CALL2]] : i32, i32

  %1 = arith.constant 1 : i32
  %2 = arith.constant 2 : i32
  %result = call @callable(%1) : (i32) -> i32
  %result2 = call @callable(%2) : (i32) -> i32
  return %result, %result2 : i32, i32
}

// -----

/// Check that return values are overdefined when the constant conflicts with a
/// non-constant.

func.func private @callable(%arg0 : i32) -> i32 {
  "unknown.return"(%arg0) : (i32) -> ()
}

// CHECK-LABEL: func @conflicting_constant(
func.func @conflicting_constant(%arg0 : i32) -> (i32, i32) {
  // CHECK: %[[CALL1:.*]] = call @callable
  // CHECK: %[[CALL2:.*]] = call @callable
  // CHECK: return %[[CALL1]], %[[CALL2]] : i32, i32

  %1 = arith.constant 1 : i32
  %result = call @callable(%1) : (i32) -> i32
  %result2 = call @callable(%arg0) : (i32) -> i32
  return %result, %result2 : i32, i32
}

// -----

/// Check a more complex interaction with calls and control flow.

// CHECK-LABEL: func private @complex_inner_if(
func.func private @complex_inner_if(%arg0 : i32) -> i32 {
  // CHECK-DAG: %[[TRUE:.*]] = arith.constant true
  // CHECK-DAG: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: cf.cond_br %[[TRUE]], ^bb1

  %cst_20 = arith.constant 20 : i32
  %cond = arith.cmpi ult, %arg0, %cst_20 : i32
  cf.cond_br %cond, ^bb1, ^bb2

^bb1:
  // CHECK: ^bb1:
  // CHECK: return %[[CST]] : i32

  %cst_1 = arith.constant 1 : i32
  return %cst_1 : i32

^bb2:
  %cst_1_2 = arith.constant 1 : i32
  %arg_inc = arith.addi %arg0, %cst_1_2 : i32
  return %arg_inc : i32
}

func.func private @complex_cond() -> i1

// CHECK-LABEL: func private @complex_callee(
func.func private @complex_callee(%arg0 : i32) -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32

  %loop_cond = call @complex_cond() : () -> i1
  cf.cond_br %loop_cond, ^bb1, ^bb2

^bb1:
  // CHECK: ^bb1:
  // CHECK-NEXT: return %[[CST]] : i32
  return %arg0 : i32

^bb2:
  // CHECK: ^bb2:
  // CHECK: call @complex_inner_if(%[[CST]]) : (i32) -> i32
  // CHECK: call @complex_callee(%[[CST]]) : (i32) -> i32
  // CHECK: return %[[CST]] : i32

  %updated_arg = call @complex_inner_if(%arg0) : (i32) -> i32
  %res = call @complex_callee(%updated_arg) : (i32) -> i32
  return %res : i32
}

// CHECK-LABEL: func @complex_caller(
func.func @complex_caller(%arg0 : i32) -> i32 {
  // CHECK: %[[CST:.*]] = arith.constant 1 : i32
  // CHECK: return %[[CST]] : i32

  %1 = arith.constant 1 : i32
  %result = call @complex_callee(%1) : (i32) -> i32
  return %result : i32
}

// -----

/// Check that non-symbol defining callables currently go to overdefined.

// CHECK-LABEL: func @non_symbol_defining_callable
func.func @non_symbol_defining_callable() -> i32 {
  // CHECK: %[[RES:.*]] = call_indirect
  // CHECK: return %[[RES]] : i32

  %fn = "test.functional_region_op"() ({
    %1 = arith.constant 1 : i32
    "test.return"(%1) : (i32) -> ()
  }) : () -> (() -> i32)
  %res = call_indirect %fn() : () -> (i32)
  return %res : i32
}

// -----

/// Check that private callables don't get processed if they have no uses.

// CHECK-LABEL: func private @unreferenced_private_function
func.func private @unreferenced_private_function() -> i32 {
  // CHECK: %[[RES:.*]] = arith.select
  // CHECK: return %[[RES]] : i32
  %true = arith.constant true
  %cst0 = arith.constant 0 : i32
  %cst1 = arith.constant 1 : i32
  %result = arith.select %true, %cst0, %cst1 : i32
  return %result : i32
}

// -----

/// Check that callables outside the analysis scope are marked as external.

func.func private @foo() -> index {
  %0 = arith.constant 10 : index
  return %0 : index
}

// CHECK-LABEL: func @bar
// FUNC-LABEL: func @bar
func.func @bar(%arg0: index) -> index {
  // CHECK: %[[C10:.*]] = arith.constant 10
  %c0 = arith.constant 0 : index
  %1 = arith.constant 420 : index
  %7 = arith.cmpi eq, %arg0, %c0 : index
  cf.cond_br %7, ^bb1(%1 : index), ^bb2

// CHECK: ^bb1(%[[ARG:.*]]: index):
// FUNC: ^bb1(%[[ARG:.*]]: index):
^bb1(%8: index):  // 2 preds: ^bb0, ^bb4
  // CHECK-NEXT: return %[[ARG]]
  // FUNC-NEXT: return %[[ARG]]
  return %8 : index

// CHECK: ^bb2
// FUNC: ^bb2
^bb2:
  // FUNC-NEXT: %[[FOO:.*]] = call @foo
  %13 = call @foo() : () -> index
  // CHECK: cf.br ^bb1(%[[C10]]
  // FUNC: cf.br ^bb1(%[[FOO]]
  cf.br ^bb1(%13 : index)
}