GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — gh-130415: Narrowing to constants in branches involving `is` comparisons with a constant by reidenong · Pull Request #143895 · python/cpython Conversation
|
|
This is pretty cool! Thanks for doing this, I'll review it soon. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I like the idea, it provides some more reasoning about values and fits nicely into the existing framework.
Thanks for doing this.
I would recommend changing the construction of predicates to be more relaxed and only check for constants when the predicate is resolved. It would be more powerful, easier to reason about and maybe faster.
As an example:
p0 = a is b p1 = b is None exit_if(not p1) exit_if(not p0) # a must be None
By restricting p0 to cases where a is a const or b is a const, we cannot infer that a is None after the second exit.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx); | ||
| extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym); | ||
| extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef subject_ref, JitOptRef constant_ref, JitOptPredicateKind kind, bool invert); | ||
| extern bool _Py_uop_sym_is_known_singleton(JitOptContext *ctx, JitOptRef sym); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matters whether something is a singleton or not, so you can drop this.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| typedef enum { | ||
| JIT_PRED_IS, | ||
| // JIT_PRED_EQ, |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Either remove this, or add a TODO note that we'll be adding EQ, NE, etc.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| op(_IS_OP, (left, right -- b, l, r)) { | ||
| b = sym_new_type(ctx, &PyBool_Type); | ||
| bool invert = (oparg != 0); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It would be both more powerful and result in cleaner code, if we left all analysis until the predicate is resolved.
We will often have more information then.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return; | ||
| case JIT_SYM_PREDICATE_TAG: | ||
| if (!PyBool_Check(const_val) || | ||
| (_Py_uop_sym_is_const(ctx, ref) && |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Drop these extra checks. I'm not sure they are necessary and may be creating a contradiction where none exists.
If const_val is a boolean, then we can resolve the predicate, sym_apply_predicate_narrowing(ctx, flag, as_bool(const_val))
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| } | ||
|
|
||
| JitOptRef | ||
| _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef subject_ref, JitOptRef constant_ref, JitOptPredicateKind kind, bool invert) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
We don't need to restrict this to the case where one of the values is a constant. If a is b is true, then a and b refer to the same object regardless of whether one is constant.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
- Delay analysis until predicate resolves - Generalize predicate to lhs, rhs - Expand to accept all safe constants (not just singletons)
|
Hi @markshannon I think I've addressed the stated problems, a summary of changes is that
Additionally, you mentioned that for Another implementation I initially considered would be to move the Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
We need a test in the optimizer_symbols.c file (at the bottom, the last function). Also CI is failing, can you please investigate? Thanks! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
CI failing due to #142911 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
ok, will take a look and get back asap |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@reidenong you should be able to pull in main now and it will build. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@Fidget-Spinner I have added the unit tests, ptal. thanks!
btw it builds now but what was the issue? I'm a bit confused on what happened. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks correct, and thanks for updating the ascii art.
I've a couple of suggestions that should help long term maintenance, otherwise LGTM.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| extern bool _Py_uop_sym_is_compact_int(JitOptRef sym); | ||
| extern JitOptRef _Py_uop_sym_new_compact_int(JitOptContext *ctx); | ||
| extern void _Py_uop_sym_set_compact_int(JitOptContext *ctx, JitOptRef sym); | ||
| extern JitOptRef _Py_uop_sym_new_predicate(JitOptContext *ctx, JitOptRef lhs_ref, JitOptRef rhs_ref, JitOptPredicateKind kind, bool invert); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Now that the predicate is symmetrical (meaning that we treat the lhs and rhs the same, not that it is commutative), I think we should drop the invert parameter and field and only use the kind, adding an IS_NOT kind.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| bool lhs_is_const = _Py_uop_sym_is_safe_const(ctx, lhs_ref); | ||
| bool rhs_is_const = _Py_uop_sym_is_safe_const(ctx, rhs_ref); | ||
|
|
||
| if (pred.kind == JIT_PRED_IS && (lhs_is_const || rhs_is_const)) { |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Can you use a switch, that way the compiler will complain about missing cases as we add more kinds.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
I have made the requested changes; please review again |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Thanks for making the requested changes! @markshannon: please review the changes made to this pull request. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Please fix the merge conflicts, thank you! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This looks good now. Thanks for doing this.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
…mparisons with a constant (pythonGH-143895)
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.