[PR #78] [MERGED] spirv_asm: float literals and DebugPrintf as spirv-as reads them (OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330) #130

Closed
opened 2026-11-26 20:16:53 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/78
Author: @ItsMrDubey
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: spirv-asm-float-fixes


📝 Commits (5)

  • ba49ba2 spirv_asm: a hex-float constant is truncated toward zero, as spirv-as reads it
  • 4e8aa91 spirv_asm: a decimal f32 constant is rounded once, and an f16 one is that f32 cut toward zero, as spirv-as reads them
  • 1f11a0f spirv_asm: a decimal float constant that overflows its type, or spells inf or nan, is refused as spirv-as refuses it
  • 3d0664a spirv_asm: a BFloat16KHR constant is encoded as bfloat16, not as an IEEE half
  • c389dbc spirv_asm: OpExtInst takes DebugPrintf by name from the NonSemantic.DebugPrintf set, as spirv-dis writes it

📊 Changes

3 files changed (+208 additions, -37 deletions)

View changed files

📝 tests/test_spirv_asm.py (+104 -0)
📝 tools/spirv_asm.py (+97 -35)
📝 tools/spirv_asm_tables.py (+7 -2)

📄 Description

Claimed in the coordination issue as: OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7119646964)

What and why

Five float-literal and extended-instruction differences between tools/spirv_asm.py and spirv-as. There is one commit per issue, and each comes with a regression test in tests/test_spirv_asm.py. I reproduced all five on origin/main before fixing them. No open PR or old-pr branch addressed them (the only spirv_asm branch in #17, old-pr/10025, is about output aliasing), so nothing was cherry-picked.

spirv-as is not installed here (where spirv-as finds nothing), so the reference behaviour comes from reading SPIRV-Tools' source/util/parse_number.cpp (ParseAndEncodeFloatingPointNumber) and source/util/hex_float.h (operator>> for HexFloat, ParseNormalFloat and its Float16 and BFloat16 specialisations, and castTo). The tests assert the values that code produces.

  • old #4330 (@mrbean56): hex-floats are truncated, not rounded. I checked the claim against HexFloat's operator>> and it is correct. That parser drops the significand bits that don't fit, both normal and subnormal, so the result is truncated toward zero. A value too large for the type becomes infinity, and the exponent-all-ones spelling still carries a NaN payload. spirv_asm used to round to nearest through float.fromhex and struct.pack. For example, f32 0x1.0000018p0 gave 0x3F800001 and now gives 0x3F800000. f32 0x1.fffffffp127 used to round up to +Inf and now gives 0x7F7FFFFF.
  • old #3911 (@lumenpearson): decimal f32 and f16 bits. An f32 decimal used to be rounded twice, first to a double and then to f32. It is now rounded once, to nearest-even, as strtof does. Where the double lands exactly on an f32 tie, the exact decimal decides the direction. For example, 1.00000005960464477539062500000001 now gives 0x3F800001; it used to give 0x3F800000.
    Note that f16 is not round-to-nearest in spirv-as, even though the issue says it is. ParseNormalFloat<Float16> reads the decimal as an f32 and then narrows it with castTo(..., round_direction::kToZero). spirv_asm now does the same thing, so 1.0006 gives 0x3C00, not the nearer 0x3C01, and 65535 gives 0x7BFF instead of +Inf.
  • old #4286: a non-finite decimal is now refused. spirv-as fails a decimal that overflows its type: the stream sets failbit, and the f16 and bf16 overflow checks do the same. spirv_asm used to emit infinity instead, for example f64 1e400, f32 1e39 and f16 65536. It also took Python-only spellings such as inf, nan, infinity and 1_0. All of these are now refused, and the error names the line. Underflow still rounds to zero, as strtod does. A hex-float can still spell infinity, as it can in spirv-as.
  • old #4237: BF16 constants. spirv_asm ignored OpTypeFloat 16 BFloat16KHR and encoded its constants as IEEE halves, so 1.5 gave 0x3E00. They now use the bfloat16 layout (8 exponent bits, 7 fraction bits), so 1.5 gives 0x3FC0. As SPIRV-Tools does, a decimal is read as an f32 and truncated, and a hex-float is truncated.
  • old #4322: DebugPrintf by name. OpExtInst %void %set DebugPrintf ..., the spelling spirv-dis prints for NonSemantic.DebugPrintf, used to fail with "invalid integer literal 'DebugPrintf'". The set now has a one-entry table (NONSEMANTIC_DEBUGPRINTF, from extinst.nonsemantic.debugprintf.grammar.json), and the numeric form 1 still works.

Tested

Windows 11, Python 3.11.9, no GPU needed. spirv-as and spirv-val are not installed, so the tests that compare against spirv-as, and the _validate call, skip.

  • python -I tests/test_spirv_asm.py: 36 tests OK (1 skipped: the comparison against spirv-as). With tools/ reverted to origin/main, the new tests fail: 10 of the hex-float cases, 8 decimal cases, all 12 overflow and non-finite cases, 10 BF16 cases plus its overflow check, and the DebugPrintf test with an error.
  • python -I tests/test_spirv_asm_tables.py: OK
  • python -I tests/test_desc_cache.py, and python -m unittest discover -s tests/semantic -p test_<name>.py for each semantic test that imports spirv_asm (alu_div_fmas, alu_ldexp_f64, global_sub_dword, lds_atomics_cas, scmp_u64_mask_pairs, wave64_emulate_opt, wave64_mov_b32_cross_half): all OK
  • python -I scripts/run_semantic_tests.py tests/semantic: 463 files, 6075 tests; 4 files fail (test_exec_computed 2, test_image_atomics_guard 47, test_sopc_validation 1, test_unary_f16_validation 15 failures), and with tools/ reverted to origin/main the same 4 files fail with the same counts, so the failures were already on main
  • python -m compileall -q make_game.py tools tests, and python -I tests/test_text_write_line_endings.py: OK
  • python -I scripts/check_no_game_data.py tools/spirv_asm.py tools/spirv_asm_tables.py tests/test_spirv_asm.py: clean

AI-assisted: yes

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • No new translator rule, so nothing to add to the contract table of docs/design.md (assembler only).

🔄 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/78 **Author:** [@ItsMrDubey](https://github.com/ItsMrDubey) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `spirv-asm-float-fixes` --- ### 📝 Commits (5) - [`ba49ba2`](https://github.com/ubiquitarianism/leonida/commit/ba49ba26a1013c5977c11037457a05bccbe203f0) spirv_asm: a hex-float constant is truncated toward zero, as spirv-as reads it - [`4e8aa91`](https://github.com/ubiquitarianism/leonida/commit/4e8aa910feb63fbf2d9beb621bbd7bae2fae51b2) spirv_asm: a decimal f32 constant is rounded once, and an f16 one is that f32 cut toward zero, as spirv-as reads them - [`1f11a0f`](https://github.com/ubiquitarianism/leonida/commit/1f11a0fd5ca3de724d75ab4c4e1a6d8edbeef763) spirv_asm: a decimal float constant that overflows its type, or spells inf or nan, is refused as spirv-as refuses it - [`3d0664a`](https://github.com/ubiquitarianism/leonida/commit/3d0664ab906d70b1b19cca1475c85a617251c8f3) spirv_asm: a BFloat16KHR constant is encoded as bfloat16, not as an IEEE half - [`c389dbc`](https://github.com/ubiquitarianism/leonida/commit/c389dbc95e0dc5c1ded530e5e3b0bade86becb8f) spirv_asm: OpExtInst takes DebugPrintf by name from the NonSemantic.DebugPrintf set, as spirv-dis writes it ### 📊 Changes **3 files changed** (+208 additions, -37 deletions) <details> <summary>View changed files</summary> 📝 `tests/test_spirv_asm.py` (+104 -0) 📝 `tools/spirv_asm.py` (+97 -35) 📝 `tools/spirv_asm_tables.py` (+7 -2) </details> ### 📄 Description Claimed in the coordination issue as: OLD-3911, OLD-4237, OLD-4286, OLD-4322, OLD-4330 (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7119646964) ### What and why Five float-literal and extended-instruction differences between `tools/spirv_asm.py` and spirv-as. There is one commit per issue, and each comes with a regression test in `tests/test_spirv_asm.py`. I reproduced all five on origin/main before fixing them. No open PR or old-pr branch addressed them (the only spirv_asm branch in #17, `old-pr/10025`, is about output aliasing), so nothing was cherry-picked. spirv-as is not installed here (`where spirv-as` finds nothing), so the reference behaviour comes from reading SPIRV-Tools' `source/util/parse_number.cpp` (`ParseAndEncodeFloatingPointNumber`) and `source/util/hex_float.h` (`operator>>` for `HexFloat`, `ParseNormalFloat` and its `Float16` and `BFloat16` specialisations, and `castTo`). The tests assert the values that code produces. - **old #4330 (@mrbean56): hex-floats are truncated, not rounded.** I checked the claim against `HexFloat`'s `operator>>` and it is correct. That parser drops the significand bits that don't fit, both normal and subnormal, so the result is truncated toward zero. A value too large for the type becomes infinity, and the exponent-all-ones spelling still carries a NaN payload. spirv_asm used to round to nearest through `float.fromhex` and `struct.pack`. For example, f32 `0x1.0000018p0` gave 0x3F800001 and now gives 0x3F800000. f32 `0x1.fffffffp127` used to round up to +Inf and now gives 0x7F7FFFFF. - **old #3911 (@lumenpearson): decimal f32 and f16 bits.** An f32 decimal used to be rounded twice, first to a double and then to f32. It is now rounded once, to nearest-even, as strtof does. Where the double lands exactly on an f32 tie, the exact decimal decides the direction. For example, `1.00000005960464477539062500000001` now gives 0x3F800001; it used to give 0x3F800000. Note that **f16 is not round-to-nearest in spirv-as**, even though the issue says it is. `ParseNormalFloat<Float16>` reads the decimal as an f32 and then narrows it with `castTo(..., round_direction::kToZero)`. spirv_asm now does the same thing, so `1.0006` gives 0x3C00, not the nearer 0x3C01, and `65535` gives 0x7BFF instead of +Inf. - **old #4286: a non-finite decimal is now refused.** spirv-as fails a decimal that overflows its type: the stream sets failbit, and the f16 and bf16 overflow checks do the same. spirv_asm used to emit infinity instead, for example f64 `1e400`, f32 `1e39` and f16 `65536`. It also took Python-only spellings such as `inf`, `nan`, `infinity` and `1_0`. All of these are now refused, and the error names the line. Underflow still rounds to zero, as strtod does. A hex-float can still spell infinity, as it can in spirv-as. - **old #4237: BF16 constants.** spirv_asm ignored `OpTypeFloat 16 BFloat16KHR` and encoded its constants as IEEE halves, so `1.5` gave 0x3E00. They now use the bfloat16 layout (8 exponent bits, 7 fraction bits), so `1.5` gives 0x3FC0. As SPIRV-Tools does, a decimal is read as an f32 and truncated, and a hex-float is truncated. - **old #4322: DebugPrintf by name.** `OpExtInst %void %set DebugPrintf ...`, the spelling spirv-dis prints for `NonSemantic.DebugPrintf`, used to fail with "invalid integer literal 'DebugPrintf'". The set now has a one-entry table (`NONSEMANTIC_DEBUGPRINTF`, from `extinst.nonsemantic.debugprintf.grammar.json`), and the numeric form `1` still works. ### Tested Windows 11, Python 3.11.9, no GPU needed. spirv-as and spirv-val are not installed, so the tests that compare against spirv-as, and the `_validate` call, skip. - `python -I tests/test_spirv_asm.py`: 36 tests OK (1 skipped: the comparison against spirv-as). With `tools/` reverted to origin/main, the new tests fail: 10 of the hex-float cases, 8 decimal cases, all 12 overflow and non-finite cases, 10 BF16 cases plus its overflow check, and the DebugPrintf test with an error. - `python -I tests/test_spirv_asm_tables.py`: OK - `python -I tests/test_desc_cache.py`, and `python -m unittest discover -s tests/semantic -p test_<name>.py` for each semantic test that imports spirv_asm (alu_div_fmas, alu_ldexp_f64, global_sub_dword, lds_atomics_cas, scmp_u64_mask_pairs, wave64_emulate_opt, wave64_mov_b32_cross_half): all OK - `python -I scripts/run_semantic_tests.py tests/semantic`: 463 files, 6075 tests; 4 files fail (test_exec_computed 2, test_image_atomics_guard 47, test_sopc_validation 1, test_unary_f16_validation 15 failures), and with `tools/` reverted to origin/main the same 4 files fail with the same counts, so the failures were already on main - `python -m compileall -q make_game.py tools tests`, and `python -I tests/test_text_write_line_endings.py`: OK - `python -I scripts/check_no_game_data.py tools/spirv_asm.py tools/spirv_asm_tables.py tests/test_spirv_asm.py`: clean AI-assisted: yes ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] No new translator rule, so nothing to add to the contract table of `docs/design.md` (assembler only). --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:16:53 +01:00
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#130
No description provided.