ZJIT: Forward guarded values to branch-edge args to dedup CFG-join guards#16828
Open
dak2 wants to merge 1 commit intoruby:masterfrom
Open
ZJIT: Forward guarded values to branch-edge args to dedup CFG-join guards#16828dak2 wants to merge 1 commit intoruby:masterfrom
dak2 wants to merge 1 commit intoruby:masterfrom
Conversation
d0302e0 to
815a087
Compare
…ards When a value is type-guarded in predecessor blocks before a CFG join, the merge block parameter inherits only the widened union type of its inputs. The narrower type proven on each edge is lost at the merge, forcing the same value to be guarded again after the join. Code such as `if cond; a = n + 1; else; a = n + 2; end; n + a` ends up emitting a redundant Fixnum guard on `n` after the merge, bloating compiled code and adding runtime overhead. Add a new HIR pass `forward_guarded_values_to_jumps` that rewrites branch-edge arguments to the most recent `GuardType` of that value within the same block. A subsequent `infer_types` run can then narrow merge-block parameter types, letting `fold_constants` drop redundant guards at CFG joins. Fixes: Shopify#978
815a087 to
df9e51e
Compare
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 free
to 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.
Summary
When a value is type-guarded in predecessor blocks before a CFG join, the merge block parameter inherits only the widened union type of its inputs. The narrower type proven on each edge is lost at the merge, forcing the same value to be guarded again after the join. Code such as
if cond; a = n + 1; else; a = n + 2; end; n + aends up emitting a redundant Fixnum guard onnafter the merge, bloating compiled code and adding runtime overhead.Add a new HIR pass
forward_guarded_values_to_jumpsthat rewrites branch-edge arguments to the most recentGuardTypeof that value within the same block. A subsequentinfer_typesrun can then narrow merge-block parameter types, lettingfold_constantsdrop redundant guards at CFG joins.Fixes: Shopify#978
Benchmark
Microbenchmark targeting the optimization (a value type-guarded on both branches before a CFG join, then used after the merge):
Run with ./miniruby --zjit bench_cfg_join.rb (10 trials, N=100M):
The benchmark calls test 100M times with cond alternating via i.even?, so both branches are exercised and the post-merge guard fires on every call before the fix.
Removing that one guard per call saves ~1% of total wall time on this microbenchmark.
Real workloads will see the benefit only where guarded values flow through CFG joins, but I believe the saving is purely additive (one fewer GuardType execution per affected join per call)