Skip to content

Commit a475a9a

Browse files
authored
[mypyc] Resolve type aliases in function specialization (#21233)
Attempt to resolve aliased types when applying function specializations, allowing for some optimizations that wouldn't be caught otherwise. Still acclimating to the codebase, so if this makes sense to implement somewhere else let me know.
1 parent d0f5550 commit a475a9a

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

mypyc/irbuild/specialize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
string_writer_rprimitive,
8585
uint8_rprimitive,
8686
)
87-
from mypyc.irbuild.builder import IRBuilder
87+
from mypyc.irbuild.builder import IRBuilder, get_call_target_fullname
8888
from mypyc.irbuild.constant_fold import constant_fold_expr
8989
from mypyc.irbuild.for_helpers import (
9090
comprehension_helper,
@@ -198,7 +198,7 @@ def apply_function_specialization(
198198
builder: IRBuilder, expr: CallExpr, callee: RefExpr
199199
) -> Value | None:
200200
"""Invoke the Specializer callback for a function if one has been registered"""
201-
return _apply_specialization(builder, expr, callee, callee.fullname)
201+
return _apply_specialization(builder, expr, callee, get_call_target_fullname(callee))
202202

203203

204204
def apply_method_specialization(

mypyc/test-data/irbuild-tuple.test

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,71 @@ L2:
11771177
r3 = 1
11781178
L3:
11791179
return r3
1180+
1181+
[case testTupleTypeAliasFromGenerator]
1182+
X = tuple[str, ...]
1183+
1184+
def f() -> tuple[str, ...]:
1185+
return X(str(x) for x in range(5))
1186+
1187+
def f2() -> tuple[str, ...]:
1188+
return tuple(str(x) for x in range(5))
1189+
[out]
1190+
def f():
1191+
r0 :: list
1192+
r1 :: short_int
1193+
x :: int
1194+
r2 :: bit
1195+
r3 :: str
1196+
r4 :: i32
1197+
r5 :: bit
1198+
r6 :: short_int
1199+
r7 :: tuple
1200+
L0:
1201+
r0 = PyList_New(0)
1202+
r1 = 0
1203+
x = r1
1204+
L1:
1205+
r2 = int_lt r1, 10
1206+
if r2 goto L2 else goto L4 :: bool
1207+
L2:
1208+
x = r1
1209+
r3 = CPyTagged_Str(x)
1210+
r4 = PyList_Append(r0, r3)
1211+
r5 = r4 >= 0 :: signed
1212+
L3:
1213+
r6 = r1 + 2
1214+
r1 = r6
1215+
goto L1
1216+
L4:
1217+
r7 = PyList_AsTuple(r0)
1218+
return r7
1219+
def f2():
1220+
r0 :: list
1221+
r1 :: short_int
1222+
x :: int
1223+
r2 :: bit
1224+
r3 :: str
1225+
r4 :: i32
1226+
r5 :: bit
1227+
r6 :: short_int
1228+
r7 :: tuple
1229+
L0:
1230+
r0 = PyList_New(0)
1231+
r1 = 0
1232+
x = r1
1233+
L1:
1234+
r2 = int_lt r1, 10
1235+
if r2 goto L2 else goto L4 :: bool
1236+
L2:
1237+
x = r1
1238+
r3 = CPyTagged_Str(x)
1239+
r4 = PyList_Append(r0, r3)
1240+
r5 = r4 >= 0 :: signed
1241+
L3:
1242+
r6 = r1 + 2
1243+
r1 = r6
1244+
goto L1
1245+
L4:
1246+
r7 = PyList_AsTuple(r0)
1247+
return r7

0 commit comments

Comments
 (0)