Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[mypyc] Provide an easier way to define IR-to-IR transforms#16998

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 17 commits intomasterfrommypyc-ir-transform
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
17 commits
Select commitHold shift + click to select a range
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
PrevPrevious commit
NextNext commit
Fix issues
  • Loading branch information
@JukkaL
JukkaL committedMar 2, 2024
commitdc7f55c65d4f1eb4a8b7c5e7ccf30febd0253f3f
47 changes: 43 additions & 4 deletionsmypyc/test-data/opt-copy-propagation.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -42,8 +42,6 @@ L1:
L2:
y = 2
return y
L3:
unreachable

[case testCopyPropagationArg]
def f(x: int) -> int:
Expand All@@ -56,6 +54,31 @@ L0:
x = 4
return x

[case testCopyPropagationPartiallyDefined1]
def f(b: bool) -> int:
if b:
x = 1
y = x
return y
[out]
def f(b):
b :: bool
r0, x :: int
r1 :: bool
L0:
r0 = <error> :: int
x = r0
if b goto L1 else goto L2 :: bool
L1:
x = 2
L2:
if is_error(x) goto L3 else goto L4
L3:
r1 = raise UnboundLocalError('local variable "x" referenced before assignment')
unreachable
L4:
return x

-- The remaining test cases test basic IRTransform functionality and are not
-- all needed for testing copy propagation as such.

Expand All@@ -77,8 +100,24 @@ L1:
return 2
L2:
return 4
L3:
unreachable

[case testIRTransformAssignment]
def f(b: bool, x: int) -> int:
y = x
if b:
return y
else:
return 1
[out]
def f(b, x):
b :: bool
x :: int
L0:
if b goto L1 else goto L2 :: bool
L1:
return x
L2:
return 2

[case testIRTransformRegisterOps1]
from __future__ import annotations
Expand Down
2 changes: 2 additions & 0 deletionsmypyc/test/test_copy_propagation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@
use_custom_builtins,
)
from mypyc.transform.copy_propagation import do_copy_propagation
from mypyc.transform.uninit import insert_uninit_checks

files = ["opt-copy-propagation.test"]

Expand All@@ -39,6 +40,7 @@ def run_case(self, testcase: DataDrivenTestCase) -> None:
for fn in ir:
if fn.name == TOP_LEVEL_NAME and not testcase.name.endswith("_toplevel"):
continue
insert_uninit_checks(fn)
do_copy_propagation(fn, CompilerOptions())
actual.extend(format_func(fn))

Expand Down
5 changes: 3 additions & 2 deletionsmypyc/transform/copy_propagation.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -19,6 +19,7 @@
from mypyc.irbuild.ll_builder import LowLevelIRBuilder
from mypyc.options import CompilerOptions
from mypyc.transform.ir_transform import IRTransform
from mypyc.sametype import is_same_type


def do_copy_propagation(fn: FuncIR, options: CompilerOptions) -> None:
Expand All@@ -39,10 +40,10 @@ def do_copy_propagation(fn: FuncIR, options: CompilerOptions) -> None:
if isinstance(op, Assign):
c = counts.get(op.dest, 0)
counts[op.dest] = c + 1
if c == 0:
if c == 0 and is_same_type(op.dest.type, op.src.type):
replacements[op.dest] = op.src
elif c == 1:
delreplacements[op.dest]
replacements.pop(op.dest, 0)
elif isinstance(op, AssignMulti):
# Copy propagation not supported for AssignMulti destinations
counts[op.dest] = 2
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp