-
Notifications
You must be signed in to change notification settings - Fork 24
fix(compiler): move register tracking side-effect out of ZEN_ASSERT in copyParam #474
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ys8888john
wants to merge
2
commits into
DTVMStack:main
Choose a base branch
from
ys8888john:fix-p2-h09-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
docs/changes/2026-04-14-copyparam-register-tracking/README.md
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Change: Move register tracking side-effect out of ZEN_ASSERT in copyParam | ||
|
|
||
| - **Status**: Implemented | ||
| - **Date**: 2026-04-14 | ||
| - **Tier**: Light | ||
|
|
||
| ## Overview | ||
|
|
||
| Move the `GpRegUsed` and `FpRegUsed` register tracking bit-or assignments out of `ZEN_ASSERT()` macros in `OnePassCodeGen::copyParam()`, so they execute in both debug and release builds. | ||
|
|
||
| ## Motivation | ||
|
|
||
| The original code placed side-effect expressions inside `ZEN_ASSERT`: | ||
|
|
||
| ```cpp | ||
| ZEN_ASSERT(GpRegUsed |= (1 << Reg)); | ||
| ZEN_ASSERT(FpRegUsed |= (1 << Reg)); | ||
| ``` | ||
|
|
||
| In release builds (`NDEBUG`), `ZEN_ASSERT` is compiled out entirely, which means the register tracking updates are silently skipped. This causes the singlepass JIT to lose track of which registers are in use during parameter copying, potentially leading to register allocation conflicts and incorrect code generation. | ||
|
|
||
| ## Impact | ||
|
|
||
| - **Module**: `src/singlepass/common/codegen.h` — `OnePassCodeGen::copyParam()` | ||
| - **Contracts affected**: None (internal implementation detail; no API change) | ||
| - **Behavior change**: Register tracking now works correctly in release builds. No change in debug builds. | ||
|
|
||
| ## Checklist | ||
|
|
||
| - [x] Implementation complete | ||
| - [x] Tests added/updated | ||
| - [x] Module specs in `docs/modules/` updated (if affected) | ||
| - [x] Build and tests pass |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| ;; Test that register tracking in copyParam works correctly. | ||
| ;; This exercises the code path where multiple register parameters | ||
| ;; are copied during a function call, ensuring GpRegUsed/FpRegUsed | ||
| ;; tracking is not lost (regression test for side-effect inside ZEN_ASSERT). | ||
|
|
||
| (module | ||
| ;; Function with many integer parameters to exercise GP register tracking | ||
| (func $sum_i32_6 (param i32 i32 i32 i32 i32 i32) (result i32) | ||
| local.get 0 | ||
| local.get 1 | ||
| i32.add | ||
| local.get 2 | ||
| i32.add | ||
| local.get 3 | ||
| i32.add | ||
| local.get 4 | ||
| i32.add | ||
| local.get 5 | ||
| i32.add) | ||
|
|
||
| ;; Function with many f64 parameters to exercise FP register tracking | ||
| (func $sum_f64_4 (param f64 f64 f64 f64) (result f64) | ||
| local.get 0 | ||
| local.get 1 | ||
| f64.add | ||
| local.get 2 | ||
| f64.add | ||
| local.get 3 | ||
| f64.add) | ||
|
|
||
| ;; Caller that passes distinct values to verify correct register assignment | ||
| (func (export "test_gp_reg_tracking") (result i32) | ||
| i32.const 1 | ||
| i32.const 2 | ||
| i32.const 3 | ||
| i32.const 4 | ||
| i32.const 5 | ||
| i32.const 6 | ||
| call $sum_i32_6) | ||
|
|
||
| (func (export "test_fp_reg_tracking") (result f64) | ||
| f64.const 1.5 | ||
| f64.const 2.5 | ||
| f64.const 3.5 | ||
| f64.const 4.5 | ||
| call $sum_f64_4) | ||
|
|
||
| ;; Mixed integer and float parameters | ||
| (func $mixed (param i32 f64 i32 f64) (result f64) | ||
| local.get 0 | ||
| f64.convert_i32_s | ||
| local.get 1 | ||
| f64.add | ||
| local.get 2 | ||
| f64.convert_i32_s | ||
| f64.add | ||
| local.get 3 | ||
| f64.add) | ||
|
|
||
| (func (export "test_mixed_reg_tracking") (result f64) | ||
| i32.const 10 | ||
| f64.const 20.5 | ||
| i32.const 30 | ||
| f64.const 40.5 | ||
| call $mixed) | ||
| ) | ||
|
|
||
| (assert_return (invoke "test_gp_reg_tracking") (i32.const 21)) | ||
| (assert_return (invoke "test_fp_reg_tracking") (f64.const 12.0)) | ||
| (assert_return (invoke "test_mixed_reg_tracking") (f64.const 101.0)) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add change doc and test cases for this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change doc added. The original ZEN_ASSERT(GpRegUsed |= (1 << Reg)) was always true so bitwise OR with (1 << Reg) always yields a non-zero result so the assert had no real checking value. The fix simply moves the side-effect out of the assert. Since GpRegUsed is only consumed by another assert (the conflict check above), there's no observable difference in generated code between the buggy and fixed versions, so no test case can meaningfully validate this change.