-
Notifications
You must be signed in to change notification settings - Fork 50
fix: handle co_freevars mismatch in ASTRewriter for closure kernels #474
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,11 +116,37 @@ def transform(cls, f): | |
| log().warning("could not find rewritten function %s in code object", f.__name__) | ||
| return f | ||
|
|
||
| f.__code__ = new_f_code_o | ||
|
|
||
| for name, val in cls.rewrite_globals.items(): | ||
| f.__globals__[name] = val | ||
|
|
||
| # AST transformers may inject references to new names (e.g. | ||
| # scf_if_dispatch, scf_if_collect_results) inside the kernel or | ||
| # its rewriter-generated sub-functions (__then_N, __else_N). | ||
| # Because enclosing_mod creates a closure layer, these unresolved | ||
| # names become free vars (LOAD_DEREF) rather than globals. This | ||
| # causes new_f_code_o.co_freevars to have more entries than the | ||
| # original f.__closure__. Direct f.__code__ assignment would | ||
| # raise ValueError, so we build a new function with a matching | ||
| # closure instead. | ||
| if f.__closure__ and new_f_code_o.co_freevars != f.__code__.co_freevars: | ||
| old_cells = {name: cell for name, cell | ||
| in zip(f.__code__.co_freevars, f.__closure__)} | ||
| new_closure = [] | ||
| for var in new_f_code_o.co_freevars: | ||
| if var in old_cells: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assuming that 'var' is in 'f.globals' simply because it's not in 'new_f_code_o.co_freevars' is incorrect, as this can lead to situations where it can't be found even if 'f.globals' is ignored. 'f.code.co_freevars' should be directly replaced with 'new_f_code_o.co_freevars' . Due to AST processing, some 'co_freevars' can be optimized away. |
||
| new_closure.append(old_cells[var]) | ||
| else: | ||
| # Create a cell whose value comes from globals | ||
| cell = (lambda v: lambda: v)(f.__globals__.get(var)).__closure__[0] | ||
|
|
||
| new_closure.append(cell) | ||
| new_f = types.FunctionType( | ||
| new_f_code_o, f.__globals__, f.__name__, | ||
| f.__defaults__, tuple(new_closure), | ||
| ) | ||
| new_f.__kwdefaults__ = f.__kwdefaults__ | ||
|
|
||
| return new_f | ||
|
Comment on lines
+131
to
+147
Comment on lines
+122
to
+147
|
||
|
|
||
| f.__code__ = new_f_code_o | ||
| return f | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The line 'f.globals[name] = val' can be moved before the 'f.closure' check, which makes it easier to merge the two 'f.closure' code sections.