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

Open
github-bot wants to merge 1 commit from github-bot/gh-2013 into main AGit
Owner

@⁠ShotTechOp on GitHub · 2026-11-25 18:10 UTC · original
ShotTechOp:fix/shader-ir-lit-neg → main · +106 −5 · 3 files · 1 commits · open

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
**@⁠ShotTechOp** on GitHub · 2026-11-25 18:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2013) `ShotTechOp:fix/shader-ir-lit-neg` → `main` · +106 −5 · 3 files · 1 commits · open 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`
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:46 +00:00
Author
Owner

@⁠xel1uz on GitHub · 2026-11-25 21:14 UTC · original · approved these changes

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.

**@⁠xel1uz** on GitHub · 2026-11-25 21:14 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2013#pullrequestreview-6065070675) · **approved these changes** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2013/head:github-bot/gh-2013'
git switch 'github-bot/gh-2013'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2013'
git switch 'github-bot/gh-2013'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2013'
git switch 'github-bot/gh-2013'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2013'
git switch 'main'
git merge --squash 'github-bot/gh-2013'
git switch 'main'
git merge --ff-only 'github-bot/gh-2013'
git switch 'main'
git merge 'github-bot/gh-2013'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.