8000 [mypyc] Inline increfs and decrefs in commonly executed blocks by JukkaL · Pull Request #11540 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

[mypyc] Inline increfs and decrefs in commonly executed blocks #11540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add test cases
  • Loading branch information
JukkaL committed Nov 13, 2021
commit dbd6b916b523c1f8f1279a425bc2a967fa90b413
124 changes: 124 additions & 0 deletions mypyc/test-data/exceptions-freq.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
-- Test cases for basic block execution frequency analysis.
--
-- These test cases are using exception transform test machinery for convenience.
--
-- NOTE: These must all have the _freq suffix

[case testSimpleError_freq]
from typing import List
def f(x: List[int]) -> int:
return x[0]
[out]
def f(x):
x :: list
r0 :: object
r1, r2 :: int
L0:
r0 = CPyList_GetItemShort(x, 0)
if is_error(r0) goto L3 (error at f:3) else goto L1
L1:
r1 = unbox(int, r0)
dec_ref r0
if is_error(r1) goto L3 (error at f:3) else goto L2
L2:
return r1
L3:
r2 = <error> :: int
return r2
hot blocks: [0, 1, 2]

[case testHotBranch_freq]
from typing import List
def f(x: bool) -> None:
if x:
y = 1
else:
y = 2
[out]
def f(x):
x :: bool
y :: int
L0:
if x goto L1 else goto L2 :: bool
L1:
y = 2
dec_ref y :: int
goto L3
L2:
y = 4
dec_ref y :: int
L3:
return 1
hot blocks: [0, 1, 2, 3]

[case testGoto_freq]
from typing import List
def f(x: bool) -> int:
if x:
y = 1
else:
return 2
return y
[out]
def f(x):
x :: bool
y :: int
L0:
if x goto L1 else goto L2 :: bool
L1:
y = 2
goto L3
L2:
return 4
L3:
return y
hot blocks: [0, 1, 2, 3]

[case testFalseOnError_freq]
from typing import List
def f(x: List[int]) -> None:
x[0] = 1
[out]
def f(x):
x :: list
r0 :: object
r1 :: bit
r2 :: None
L0:
r0 = box(short_int, 2)
r1 = CPyList_SetItem(x, 0, r0)
if not r1 goto L2 (error at f:3) else goto L1 :: bool
L1:
return 1
L2:
r2 = <error> :: None
return r2
hot blocks: [0, 1]

[case testRareBranch_freq]
from typing_extensions import Final

x: Final = str()

def f() -> str:
return x
[out]
def f():
r0 :: str
r1 :: bool
r2 :: str
L0:
r0 = __main__.x :: static
if is_error(r0) goto L1 else goto L3
L1:
r1 = raise NameError('value for final name "x" was not set')
if not r1 goto L4 (error at f:6) else goto L2 :: bool
L2:
unreachable
L3:
inc_ref r0
return r0
L4:
r2 = <error> :: str
return r2
hot blocks: [0, 3]
7 changes: 6 additions & 1 deletion mypyc/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
ICODE_GEN_BUILTINS, use_custom_builtins, MypycDataSuite, build_ir_for_single_file,
assert_test_output, remove_comment_lines
)
from mypyc.analysis.blockfreq import commonly_executed_blocks

files = [
'exceptions.test'
'exceptions.test',
'exceptions-freq.test',
]


Expand All @@ -46,6 +48,9 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
insert_exception_handling(fn)
insert_ref_count_opcodes(fn)
actual.extend(format_func(fn))
if testcase.name.endswith('_freq'):
common = commonly_executed_blocks(fn.blocks[0])
actual.append('hot blocks: %s' % sorted(b.label for b in common))

assert_test_output(testcase, actual, 'Invalid source code output',
expected_output)
0