Skip to content

[cppia] Load this for member increments in JIT code - #1459

Merged
Simn merged 1 commit into
masterfrom
cppia-jit-this-crement
Nov 1, 2026
Merged

Simn merged 1 commit into
masterfrom
cppia-jit-this-crement

Conversation

@Simn

@Simn Simn commented Nov 1, 2026

Copy link
Copy Markdown
Member

In JIT-compiled cppia code, ++/-- on a member field is lost if it is the only access to this in the function:

class Counter {
	public var n = 0;
	public function new() {}
	public function inc() {
		n++; // no effect with -jit
	}
}

MemReferenceCrement::genCode addressed the field as sJitThis.star() + offset. That reads through the this register directly, but only jposThis operands (JitThisPos) set usesThis, and the prologue only loads the register if (usesThis). So when nothing else in the function touches this, the increment goes through a register that was never set up. n += 1, n = n + 1 and increments on other objects are not affected, and neither is the interpreter.

The fix uses JitThisPos(offset) like the other member references do. This adds a client test case covering pre/post increment and decrement, as statements and as values.

Found via a new Haxe unit test in HaxeFoundation/haxe#13111, which fails on the cppia JIT run on all platforms without this change.

🤖 Generated with Claude Code

MemReferenceCrement addressed `this` fields through the raw `this`
register, which doesn't mark the function as using `this`. If the
increment was the only access to `this`, the register was never
loaded in the function prologue and the increment was lost.

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

Simn commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

That repro looks a bit too simple to be real...

@tobil4sk

tobil4sk commented Nov 1, 2026

Copy link
Copy Markdown
Member

That repro looks a bit too simple to be real...

It seems it does actually fail on master:

cases.client.MemberCrements
  testVoid: FAILURE FFF.
    line: 39, expected 1 but it is 0
    line: 41, expected 2 but it is 0
    line: 43, expected 1 but it is 0
  testValue: FAILURE FFFF
    line: 50, expected 0 but it is -470161736
    line: 51, expected 1 but it is 0
    line: 52, expected 2 but it is -470161734
    line: 53, expected 2 but it is 0

I don't know about the implications of using JitThisPos but it seems at least like a minimal change that addresses the bug. I wonder however if there is much point in having both postInc and postIncValue in the tests.

@Simn

Simn commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

I'm mildly shocked that our tests miss this...

And in theory the value and block-level could take quite different code paths, so it won't hurt to test that.

@Simn
Simn merged commit a04a2c4 into master Nov 1, 2026
63 checks passed
@Simn
Simn deleted the cppia-jit-this-crement branch November 1, 2026 21:17
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.

2 participants