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
Avoid temp variable assignments #6575
Conversation
@ehsantn thanks for submitting this, I have marked it as ready for review. Although, it looks like all the public CI tests failed. |
Looking at the CI failures. I am guessing a lot of the IR analysis/transformation has assumed the temp variables to exist. |
With the large amount of statement rewriting needed in the interpreter for python 3.9, might be a good idea to wait until those are in before looking at a change along these lines. |
@sklam The issues in CI seem to be expected and straightforward to work through. |
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.
I'm curious the motivation to make these changes. Assuming this passes the tests then the concept behind the changes seems fine.
The motivation is compilation time and IR readability as in description. The rest of changes are just adapting to the new IR. |
stupid question: the |
@luk-f-a This can only decrease compilation time even without parfors. The small difference here is probably just random variation in runtime (need to average several runs to see small differences). |
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.
I have verified the changes. I was also hoping to see some time reduction in LLVM SROA pass but that didn't happen. Nonetheless, I can confirm the overall compile-time improvements.
There is just two minor fixes needed.
- Please remove the unrelated file
algebra.log
. - And, I suggested additional comments to help find the reason for a change in typeinfer.
Regarding the CI failure:
it is caused by arrayexpr fusing the subexpression into a monolithic one. |
The arrayexpr test can be fixed by: diff --git a/numba/np/ufunc/array_exprs.py b/numba/np/ufunc/array_exprs.py
index 8cf5779b3..0b747616d 100644
--- a/numba/np/ufunc/array_exprs.py
+++ b/numba/np/ufunc/array_exprs.py
@@ -158,7 +158,7 @@ class RewriteArrayExprs(rewrites.Rewrite):
self.array_assigns[instr.target.name] = new_instr
for operand in self._get_operands(expr):
operand_name = operand.name
- if operand_name in self.array_assigns:
+ if operand.is_temp and operand_name in self.array_assigns:
child_assign = self.array_assigns[operand_name]
child_expr = child_assign.value
child_operands = child_expr.list_vars() This would retain the original behavior and not fuse array-expressions that are stored in user variable. |
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.
Thanks for implementing this improvement to code generation. I've manually looked at impact of the changes and it does appear make the IR clearer and more concise. My main concern with this patch as-is is that there are no tests that validate the transforms being made are producing the expected result.
numba/core/interpreter.py
Outdated
# the same temporary is assigned to multiple variables in cases | ||
# like a = b[i] = 1, so need to handle replaced temporaries in | ||
# later setitem/setattr nodes | ||
if (isinstance(inst, (ir.SetItem, ir.SetItem)) |
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.
Both items in the class_or_tuple
arg are the same.
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.
Good catch. Fixed.
Co-authored-by: Siu Kwan Lam <1929845+sklam@users.noreply.github.com>
@sklam @stuartarchibald thanks for the detailed review and suggested changes. I think I incorporated all of them. Let me know if anything else is needed. @stuartarchibald added a test in ca58e53. This PR also changes some other tests that check the IR structure to expect the new format. |
@stuartarchibald, do you want to verify this? |
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.
Thanks for the patch and all the fixes. The change-set looks good.
Avoids temporary variable assignment in bytecode processing if possible. This is possible since the bytecode is stack-based and not possible for temporaries to be reused after pop (@sklam @stuartarchibald are there corner cases to the contrary?).
This has several benefits:
Here is a benchmark that demonstrates the compile time benefit:
Output without the change (on a 2019 MacBook Pro):
Output with the change:
Parfor pass is significantly impacted since these extra copies fill up copy propagation data structures and slow them down I believe.