Debian Codegraph @

mypyc/test-data/opt-copy-propagation.test

raw
1 -- Test cases for copy propagation optimization. This also tests IR transforms in general,
2 -- as copy propagation was the first IR transform that was implemented.
3
4 [case testCopyPropagationSimple]
5 def g() -> int:
6 return 1
7
8 def f() -> int:
9 y = g()
10 return y
11 [out]
12 def g():
13 L0:
14 return 2
15 def f():
16 r0 :: int
17 L0:
18 r0 = g()
19 return r0
20
21 [case testCopyPropagationChain]
22 def f(x: int) -> int:
23 y = x
24 z = y
25 return z
26 [out]
27 def f(x):
28 x :: int
29 L0:
30 return x
31
32 [case testCopyPropagationChainPartial]
33 def f(x: int) -> int:
34 y = x
35 z = y
36 x = 2
37 return z
38 [out]
39 def f(x):
40 x, y :: int
41 L0:
42 y = x
43 x = 4
44 return y
45
46 [case testCopyPropagationChainBad]
47 def f(x: int) -> int:
48 y = x
49 z = y
50 y = 2
51 return z
52 [out]
53 def f(x):
54 x, y, z :: int
55 L0:
56 y = x
57 z = y
58 y = 4
59 return z
60
61 [case testCopyPropagationMutatedSource1]
62 def f(x: int) -> int:
63 y = x
64 x = 1
65 return y
66 [out]
67 def f(x):
68 x, y :: int
69 L0:
70 y = x
71 x = 2
72 return y
73
74 [case testCopyPropagationMutatedSource2]
75 def f() -> int:
76 z = 1
77 y = z
78 z = 2
79 return y
80 [out]
81 def f():
82 z, y :: int
83 L0:
84 z = 2
85 y = z
86 z = 4
87 return y
88
89 [case testCopyPropagationTooComplex]
90 def f(b: bool, x: int) -> int:
91 if b:
92 y = x
93 return y
94 else:
95 y = 1
96 return y
97 [out]
98 def f(b, x):
99 b :: bool
100 x, y :: int
101 L0:
102 if b goto L1 else goto L2 :: bool
103 L1:
104 y = x
105 return y
106 L2:
107 y = 2
108 return y
109
110 [case testCopyPropagationArg]
111 def f(x: int) -> int:
112 x = 2
113 return x
114 [out]
115 def f(x):
116 x :: int
117 L0:
118 x = 4
119 return x
120
121 [case testCopyPropagationPartiallyDefined1]
122 def f(b: bool) -> int:
123 if b:
124 x = 1
125 y = x
126 return y
127 [out]
128 def f(b):
129 b :: bool
130 r0, x :: int
131 r1 :: bool
132 y :: int
133 L0:
134 r0 = <error> :: int
135 x = r0
136 if b goto L1 else goto L2 :: bool
137 L1:
138 x = 2
139 L2:
140 if is_error(x) goto L3 else goto L4
141 L3:
142 r1 = raise UnboundLocalError('local variable "x" referenced before assignment')
143 unreachable
144 L4:
145 y = x
146 return y
147
148 -- The remaining test cases test basic IRTransform functionality and are not
149 -- all needed for testing copy propagation as such.
150
151 [case testIRTransformBranch]
152 from mypy_extensions import i64
153
154 def f(x: bool) -> int:
155 y = x
156 if y:
157 return 1
158 else:
159 return 2
160 [out]
161 def f(x):
162 x :: bool
163 L0:
164 if x goto L1 else goto L2 :: bool
165 L1:
166 return 2
167 L2:
168 return 4
169
170 [case testIRTransformAssignment]
171 def f(b: bool, x: int) -> int:
172 y = x
173 if b:
174 return y
175 else:
176 return 1
177 [out]
178 def f(b, x):
179 b :: bool
180 x :: int
181 L0:
182 if b goto L1 else goto L2 :: bool
183 L1:
184 return x
185 L2:
186 return 2
187
188 [case testIRTransformRegisterOps1_withgil]
189 from __future__ import annotations
190 from typing import cast
191
192 class C:
193 a: int
194
195 def m(self, x: int) -> None: pass
196
197 def get_attr(x: C) -> int:
198 y = x
199 return y.a
200
201 def set_attr(x: C) -> None:
202 y = x
203 y.a = 1
204
205 def tuple_get(x: tuple[int, int]) -> int:
206 y = x
207 return y[0]
208
209 def tuple_set(x: int, xx: int) -> tuple[int, int]:
210 y = x
211 z = xx
212 return y, z
213
214 def call(x: int) -> int:
215 y = x
216 return call(y)
217
218 def method_call(c: C, x: int) -> None:
219 y = x
220 c.m(y)
221
222 def cast_op(x: object) -> str:
223 y = x
224 return cast(str, y)
225
226 def box(x: int) -> object:
227 y = x
228 return y
229
230 def unbox(x: object) -> int:
231 y = x
232 return cast(int, y)
233
234 def call_c(x: list[str]) -> None:
235 y = x
236 y.append("x")
237
238 def keep_alive(x: C) -> int:
239 y = x
240 return y.a + 1
241 [out]
242 def C.m(self, x):
243 self :: __main__.C
244 x :: int
245 L0:
246 return 1
247 def get_attr(x):
248 x :: __main__.C
249 r0 :: int
250 L0:
251 r0 = x.a
252 return r0
253 def set_attr(x):
254 x :: __main__.C
255 r0 :: bool
256 L0:
257 x.a = 2; r0 = is_error
258 return 1
259 def tuple_get(x):
260 x :: tuple[int, int]
261 r0 :: int
262 L0:
263 r0 = x[0]
264 return r0
265 def tuple_set(x, xx):
266 x, xx :: int
267 r0 :: tuple[int, int]
268 L0:
269 r0 = (x, xx)
270 return r0
271 def call(x):
272 x, r0 :: int
273 L0:
274 r0 = call(x)
275 return r0
276 def method_call(c, x):
277 c :: __main__.C
278 x :: int
279 r0 :: None
280 L0:
281 r0 = c.m(x)
282 return 1
283 def cast_op(x):
284 x :: object
285 r0 :: str
286 L0:
287 r0 = cast(str, x)
288 return r0
289 def box(x):
290 x :: int
291 r0 :: object
292 L0:
293 r0 = box(int, x)
294 return r0
295 def unbox(x):
296 x :: object
297 r0 :: int
298 L0:
299 r0 = unbox(int, x)
300 return r0
301 def call_c(x):
302 x :: list
303 r0 :: str
304 r1 :: i32
305 r2 :: bit
306 L0:
307 r0 = 'x'
308 r1 = PyList_Append(x, r0)
309 r2 = r1 >= 0 :: signed
310 return 1
311 def keep_alive(x):
312 x :: __main__.C
313 r0, r1 :: int
314 L0:
315 r0 = borrow x.a
316 r1 = CPyTagged_Add(r0, 2)
317 keep_alive x
318 return r1
319
320 [case testIRTransformRegisterOps2]
321 from mypy_extensions import i32, i64
322
323 def truncate(x: i64) -> i32:
324 y = x
325 return i32(y)
326
327 def extend(x: i32) -> i64:
328 y = x
329 return i64(y)
330
331 def int_op(x: i64, xx: i64) -> i64:
332 y = x
333 z = xx
334 return y + z
335
336 def comparison_op(x: i64, xx: i64) -> bool:
337 y = x
338 z = xx
339 return y == z
340
341 def float_op(x: float, xx: float) -> float:
342 y = x
343 z = xx
344 return y + z
345
346 def float_neg(x: float) -> float:
347 y = x
348 return -y
349
350 def float_comparison_op(x: float, xx: float) -> bool:
351 y = x
352 z = xx
353 return y == z
354 [out]
355 def truncate(x):
356 x :: i64
357 r0 :: i32
358 L0:
359 r0 = truncate x: i64 to i32
360 return r0
361 def extend(x):
362 x :: i32
363 r0 :: i64
364 L0:
365 r0 = extend signed x: i32 to i64
366 return r0
367 def int_op(x, xx):
368 x, xx, r0 :: i64
369 L0:
370 r0 = x + xx
371 return r0
372 def comparison_op(x, xx):
373 x, xx :: i64
374 r0 :: bit
375 L0:
376 r0 = x == xx
377 return r0
378 def float_op(x, xx):
379 x, xx, r0 :: float
380 L0:
381 r0 = x + xx
382 return r0
383 def float_neg(x):
384 x, r0 :: float
385 L0:
386 r0 = -x
387 return r0
388 def float_comparison_op(x, xx):
389 x, xx :: float
390 r0 :: bit
391 L0:
392 r0 = x == xx
393 return r0
394
395 -- Note that transforms of these ops aren't tested here:
396 -- * LoadMem
397 -- * SetMem
398 -- * GetElementPtr
399 -- * LoadAddress
400 -- * Unborrow