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

Open
opened 2026-11-26 21:19:45 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2013
Author: @ShotTechOp
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/shader-ir-lit-neg


📝 Commits (1)

  • 9b7af80 shader_ir: preserve sign inside lit(-V) instead of turning it into VOP3 neg(V)

📊 Changes

3 files changed (+106 additions, -5 deletions)

View changed files

➕ tests/semantic/test_shader_ir_lit_neg.py (+85 -0)
📝 tests/test_shader_ir.py (+14 -2)
📝 tools/shader_ir.py (+7 -3)

📄 Description

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2013 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/shader-ir-lit-neg` --- ### 📝 Commits (1) - [`9b7af80`](https://github.com/ubiquitarianism/leonida/commit/9b7af801d9f0df7c2e1f035414cd88656a838d63) shader_ir: preserve sign inside lit(-V) instead of turning it into VOP3 neg(V) ### 📊 Changes **3 files changed** (+106 additions, -5 deletions) <details> <summary>View changed files</summary> ➕ `tests/semantic/test_shader_ir_lit_neg.py` (+85 -0) 📝 `tests/test_shader_ir.py` (+14 -2) 📝 `tools/shader_ir.py` (+7 -3) </details> ### 📄 Description 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` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2020
No description provided.