Skip to content

Keep literal identity when a local is copy-propagated in the JIT - #10224

Open
zev wants to merge 2 commits into
jruby:jruby-10.0from
zev:jit-literal-identity
Open

zev wants to merge 2 commits into
jruby:jruby-10.0from
zev:jit-literal-identity

Conversation

@zev

@zev zev commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

In compiled code, a literal assigned to a local variable could turn into two different objects:

r = /hay/
[r, r].map(&:object_id).uniq.size  # => 2 with -X+C (or jit.threshold=0), 1 interpreted and in CRuby
'haystack'.match(r).regexp.equal?(r)  # => false with -X+C

The IR copy propagation pass replaces a use of a local with its value when canCopyPropagate() is true. ImmutableLiteral returns true, so one use of r became the Regexp operand itself, while the other kept reading r. The interpreter caches the object in the operand, so both uses still get the same object. The JIT compiles each literal operand to its own site, and each site makes its own object.

This affects the literal operands that make a new object: Regexp, Range, Bignum, Complex and Rational. They now return false from canCopyPropagate().

It also affects Fixnum literals outside CRuby's fixnum range (±2**62). There Integer#equal? compares identity, as for a Bignum in CRuby, so i = 4_611_686_018_427_387_905; i.dup.equal?(i) was false. The Fixnum operand now returns false from canCopyPropagate() for those values, using RubyFixnum.fixnumable (now public), the same check equal? uses. Smaller Fixnums, Float, Symbol, nil, booleans and frozen Strings are still propagated.

This fixes MatchData#regexp returns the same Regexp used to match and Integer#dup returns self for large integers under -X+C.

Testing

  • New example in spec/compiler/general_spec.rb: each of these literals (including 4611686018427387905), assigned to a local at the top level and in a method, is the same object at both uses. It fails in the jit and aot modes without the change.
  • spec/compiler: 510 examples, 0 failures.
  • mspec ci on language, core/integer, core/matchdata, core/range, core/regexp: 0 failures interpreted, with -X+C and with -Xjit.threshold=0. On core/complex and core/rational: no new failures.
  • test/mri test_regexp, test_range, test_rational, test_complex, test_bignum, test_integer, test_integer_comb, test_fixnum, interpreted and -X+C: 0 failures.
  • Checked with -Xcompile.invokedynamic=true and false.

🤖 Generated with Claude Code

The IR copy propagation pass could replace one use of a local variable
with the literal it was assigned. The JIT compiles each literal operand
to its own site, so for Regexp, Range, Bignum, Complex and Rational
literals the two uses of the local became different objects:
`r = /a/; [r, r]` gave two Regexps, and MatchData#regexp did not return
the Regexp used to match. These operands are no longer propagated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zev

zev commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

@headius I found this while looking at the MatchData#regexp spec that fails under -X+C. Do you think this should be merged?

The fix stops copy propagation for Regexp, Range, Bignum, Complex and Rational literals, so those literals are no longer substituted into later uses of the local. I don't think that loses much, but you know better whether any JIT optimization depends on it. The other option I see is to have the JIT share one site per literal operand, which would be a bigger change.

@zev
zev marked this pull request as ready for review December 6, 2026 05:08
Integer#equal? compares values only inside CRuby's fixnum range, as in
CRuby. Outside it, a copy-propagated Fixnum literal became a different
object at each JIT site, so `i = 4_611_686_018_427_387_905; i.dup`
was not equal? to i. Such literals are no longer propagated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant