shader_ir: preserve sign inside lit(-V) instead of turning it into VOP3 neg(V) (SHADER-IR-LIT-NEGATIVE-SIGN) #2013

Merged
ShotTechOp merged 1 commit from upstream-pr/2013 into main 2026-11-28 17:58:16 +00:00
ShotTechOp commented 2026-11-25 18:10:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ShotTechOp on 2026-11-25T18:10:48Z.


Claimed in #1608 as SHADER-IR-LIT-NEGATIVE-SIGN.

What and why

  • In tools/shader_ir.py::operand, the lit(...) unwrap checked if t.startswith("-"): after substituting lit(V) -> V:
    t = _LIT.sub(r"\1", t)
    if t.startswith("-"):
        t = "neg(" + t[1:] + ")"
    
  • As a result, a negative constant inside lit(...) such as lit(-1) or lit(-0x10) became "-1" / "-0x10" and was immediately rewritten to "neg(1)" / "neg(0x10)" — i.e., {"k": "imm", "v": 1, "neg": True} instead of {"k": "imm", "v": -1}!
  • In tools/rdna2spirv.py, "neg": True applies a VOP3 sign-bit XOR (^ 0x80000000), so s_mov_b32 s4, lit(-1) and v_add_nc_u32_e32 v2, lit(-0x10), v0 silently produced 0x80000001 and v0 + 0x80000010 instead of 0xFFFFFFFF and v0 - 16.
  • Checking outer_neg = t.startswith("-") before _LIT.sub(r"\1", t) keeps a leading - on lit(...) itself as the VOP3 neg(...) modifier (e.g., -lit(0x3f800000), -lit(-1.5), -|lit(-1.5)|) while preserving the negative value inside lit(-V) (matching operand("-1"), operand("-0x10"), operand("-1.5")).

Testing

  • AI-assisted: yes.
  • Environment: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally).
  • Tests:
    • Extended tests/test_shader_ir.py::OperandTests::test_llvm23_lit_is_the_constant with lit(-1), lit(-0x10), lit(-1.5), -lit(-1), -lit(-1.5), |lit(-1.5)|, -|lit(-1.5)|, neg(lit(-1)).
    • Added tests/semantic/test_shader_ir_lit_neg.py end-to-end through tools/rdna2spirv.py + spirv_mini_interp.py.
  • RED control on origin/main: tests/semantic/test_shader_ir_lit_neg.py fails on origin/main with 2147483649 (0x80000001) != 4294967295 (0xFFFFFFFF).
  • GREEN checks on branch:
    • python -I tests/test_shader_ir.py -v (39/39 OK)
    • python -I tests/test_shader_ir_parse.py -v (3/3 OK)
    • python -I tests/semantic/test_shader_ir_lit_neg.py -v (1/1 OK)
    • python -I tests/test_text_write_line_endings.py (3/3 OK)
    • python -m compileall -q tools/shader_ir.py tests/test_shader_ir.py tests/semantic/test_shader_ir_lit_neg.py
    • python -I scripts/check_no_game_data.py tools/shader_ir.py tests/test_shader_ir.py tests/semantic/test_shader_ir_lit_neg.py
    • git diff --check
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5496649146 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2013). Originally posted by [ShotTechOp](https://github.com/ShotTechOp) on 2026-11-25T18:10:48Z. --- Claimed in #1608 as `SHADER-IR-LIT-NEGATIVE-SIGN`. ## What and why - In `tools/shader_ir.py::operand`, the `lit(...)` unwrap checked `if t.startswith("-"):` **after** substituting `lit(V)` -> `V`: ```python t = _LIT.sub(r"\1", t) if t.startswith("-"): t = "neg(" + t[1:] + ")" ``` - As a result, a negative constant inside `lit(...)` such as `lit(-1)` or `lit(-0x10)` became `"-1"` / `"-0x10"` and was immediately rewritten to `"neg(1)"` / `"neg(0x10)"` — i.e., `{"k": "imm", "v": 1, "neg": True}` instead of `{"k": "imm", "v": -1}`! - In `tools/rdna2spirv.py`, `"neg": True` applies a VOP3 sign-bit XOR (`^ 0x80000000`), so `s_mov_b32 s4, lit(-1)` and `v_add_nc_u32_e32 v2, lit(-0x10), v0` silently produced `0x80000001` and `v0 + 0x80000010` instead of `0xFFFFFFFF` and `v0 - 16`. - Checking `outer_neg = t.startswith("-")` **before** `_LIT.sub(r"\1", t)` keeps a leading `-` on `lit(...)` itself as the VOP3 `neg(...)` modifier (e.g., `-lit(0x3f800000)`, `-lit(-1.5)`, `-|lit(-1.5)|`) while preserving the negative value inside `lit(-V)` (matching `operand("-1")`, `operand("-0x10")`, `operand("-1.5")`). ## Testing - **AI-assisted**: yes. - **Environment**: Windows 11, Python 3.12 (no GPU / LLVM / SPIRV-Tools installed locally). - **Tests**: - Extended `tests/test_shader_ir.py::OperandTests::test_llvm23_lit_is_the_constant` with `lit(-1)`, `lit(-0x10)`, `lit(-1.5)`, `-lit(-1)`, `-lit(-1.5)`, `|lit(-1.5)|`, `-|lit(-1.5)|`, `neg(lit(-1))`. - Added `tests/semantic/test_shader_ir_lit_neg.py` end-to-end through `tools/rdna2spirv.py` + `spirv_mini_interp.py`. - **RED control on `origin/main`**: `tests/semantic/test_shader_ir_lit_neg.py` fails on `origin/main` with `2147483649 (0x80000001) != 4294967295 (0xFFFFFFFF)`. - **GREEN checks on branch**: - `python -I tests/test_shader_ir.py -v` (39/39 OK) - `python -I tests/test_shader_ir_parse.py -v` (3/3 OK) - `python -I tests/semantic/test_shader_ir_lit_neg.py -v` (1/1 OK) - `python -I tests/test_text_write_line_endings.py` (3/3 OK) - `python -m compileall -q tools/shader_ir.py tests/test_shader_ir.py tests/semantic/test_shader_ir_lit_neg.py` - `python -I scripts/check_no_game_data.py tools/shader_ir.py tests/test_shader_ir.py tests/semantic/test_shader_ir_lit_neg.py` - `git diff --check`
xel1uz commented 2026-11-25 21:14:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by xel1uz on 2026-11-25T21:14:04Z.

Review: APPROVED


Approving 9b7af801. I verified it on main 747809e7 (merged cleanly).

  • Reading: only a - in front of lit(...) is the VOP3 neg modifier now. A - inside lit(-V) stays part of the constant, so lit(-1) is no longer read as neg(1). The fix is a small, local change in operand().
  • Own tests: tests/semantic/test_shader_ir_lit_neg.py is OK, and pytest tests/test_shader_ir.py gives 39 passed.
  • Bite check: with main's tools/shader_ir.py the new semantic test fails, and 3 test_shader_ir.py cases fail.
  • Whole translator suite: 492 files, 6,313 tests, 0 files failed.

Ubuntu 26.04, Python 3.14, LLVM 21.1.8, SPIRV-Tools 2026.1.

AI-assisted review, CPU only.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6065070675 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2013#pullrequestreview-6065070675). Originally posted by [xel1uz](https://github.com/xel1uz) on 2026-11-25T21:14:04Z. **Review:** APPROVED --- Approving `9b7af801`. I verified it on `main` 747809e7 (merged cleanly). - **Reading:** only a `-` in front of `lit(...)` is the VOP3 neg modifier now. A `-` inside `lit(-V)` stays part of the constant, so `lit(-1)` is no longer read as `neg(1)`. The fix is a small, local change in `operand()`. - **Own tests:** `tests/semantic/test_shader_ir_lit_neg.py` is OK, and `pytest tests/test_shader_ir.py` gives 39 passed. - **Bite check:** with `main`'s `tools/shader_ir.py` the new semantic test fails, and 3 `test_shader_ir.py` cases fail. - **Whole translator suite:** 492 files, 6,313 tests, 0 files failed. Ubuntu 26.04, Python 3.14, LLVM 21.1.8, SPIRV-Tools 2026.1. AI-assisted review, CPU only.
zmasi commented 2026-11-28 11:09:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:09:02Z.


#2013 | head 9b7af801d9 (merged cleanly with main 039870ea2) | python -I tests/test_shader_ir.py && python -I tests/semantic/test_shader_ir_lit_neg.py | red on main: FAILED (failures=3) in test_shader_ir.py (lit(-1) rewritten to neg(1) 0x80000001 instead of 0xFFFFFFFF) and FAILED (failures=1) in test_shader_ir_lit_neg.py | green on PR+main: 39 tests OK and 1 test OK | Windows 11, Python 3.12.10 | AI-assisted: yes

Audited outer_neg handling in shader_ir.operand (tools/shader_ir.py) so lit(-V) preserves negative integer/float constants while leading -lit(...) still emits VOP3 neg(...). Also passed tests/test_ci_registration.py (18 OK), tests/test_text_write_line_endings.py (3 OK), tests/test_runner_calls_all_tests.py (3 OK), and scripts/check_no_game_data.py.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238359667 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2013#issuecomment-7238359667). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:09:02Z. --- #2013 | head 9b7af801d9f0df7c2e1f035414cd88656a838d63 (merged cleanly with main 039870ea2) | `python -I tests/test_shader_ir.py && python -I tests/semantic/test_shader_ir_lit_neg.py` | red on main: `FAILED (failures=3)` in `test_shader_ir.py` (`lit(-1)` rewritten to `neg(1)` `0x80000001` instead of `0xFFFFFFFF`) and `FAILED (failures=1)` in `test_shader_ir_lit_neg.py` | green on PR+main: `39 tests OK` and `1 test OK` | Windows 11, Python 3.12.10 | AI-assisted: yes Audited `outer_neg` handling in `shader_ir.operand` (`tools/shader_ir.py`) so `lit(-V)` preserves negative integer/float constants while leading `-lit(...)` still emits VOP3 `neg(...)`. Also passed `tests/test_ci_registration.py` (`18 OK`), `tests/test_text_write_line_endings.py` (`3 OK`), `tests/test_runner_calls_all_tests.py` (`3 OK`), and `scripts/check_no_game_data.py`.
MoonEyes commented 2026-11-28 17:58:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:58:12Z.


Red/green: tests/test_shader_ir.py fails 3 assertions and tests/semantic/test_shader_ir_lit_neg.py fails 1 on main because lit(-1) is read as neg(1), and both pass with the PR.
Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243427180 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2013#issuecomment-7243427180). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:58:12Z. --- Red/green: tests/test_shader_ir.py fails 3 assertions and tests/semantic/test_shader_ir_lit_neg.py fails 1 on main because lit(-1) is read as neg(1), and both pass with the PR. Targeted batch (PR tests, tests importing the changed modules, docs tests, translator when touched) merged on main cb7a239cc in GitHub Actions: no new failure. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!2013
No description provided.