Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34.3k
gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only)#146397
Draft
eendebakpt wants to merge 2 commits intopython:mainfrom
Draft
gh-146393: Optimize float division operations by mutating uniquely-referenced operands in place (JIT only)#146397eendebakpt wants to merge 2 commits intopython:mainfrom
eendebakpt wants to merge 2 commits intopython:mainfrom
Conversation
…izerAdd inplace float true division ops that the tier 2 optimizer emitswhen at least one operand is a known float:- _BINARY_OP_TRUEDIV_FLOAT_INPLACE (unique LHS)- _BINARY_OP_TRUEDIV_FLOAT_INPLACE_RIGHT (unique RHS)The optimizer inserts _GUARD_TOS_FLOAT / _GUARD_NOS_FLOAT foroperands not yet known to be float, enabling specialization inexpressions like `(a + b) / c`.Also marks the result of all NB_TRUE_DIVIDE operations as uniquefloat in the abstract interpreter, enabling downstream inplace opseven for generic `a / b` (the `+=` can reuse the division result).Speeds up chain division patterns by ~2.3x and simple `total += a/b`by ~1.5x.Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Operations that always return a new float (true division, float**int,int**negative_int, mixed int/float arithmetic) now mark their resultas PyJitRef_MakeUnique. This enables downstream operations to mutatethe result in place instead of allocating a new float.Int results are NOT marked unique because small ints are cached/immortal.Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading.Please reload this page.
We optimize float divisions for the case where one of the operands is a unique reference. This is similar to#146307, but with a guard for division by zero.
_BINARY_TRUEDIV_FLOATopcode for tier 2 in this PR.Micro-benchmarks (min of 3 runs, 2M iterations)
(a+b) * ca / b(a+b) / cc(2.0+x) / yyc / (a+b)c(a/b) / (c/d)(a/b) + (c/d)Benchmark script
Analysis
The inplace truediv kicks in when at least one operand is a uniquely-referenced float (e.g. the result of a prior add/multiply). The optimizer emits
_BINARY_OP_TRUEDIV_FLOAT_INPLACEor_INPLACE_RIGHT, saving onePyFloat_FromDoubleallocation + deallocation per iteration.The optimization works well for several cases. For some (e.g.
(a/b) + (c/d)) the performance gain is not due to an inplace division, but by better type propagation allowing the+to be specialized inplace. Thea / bis also faster because of better type propagation and a+=in the test script.