Squash-merged after diff review of PR #6923 - spirv_asm: refuse an OpSwitch whose selector has no known type, instead of guessing 32 bits (SHADER-1089)
#2776 (@Vonsek) added _encode_integer, which sign-extends any value in
[2^(w-1), 2^w) for a signed narrow type. _int_literal already does that for a
bare hex literal and refuses a decimal or signed one out of the signed range
(spirv-as refuses `OpConstant %short 32768`; _encode_integer would give
-32768), so the conflict keeps this branch's tools/spirv_asm.py. #2776's
test_subword_integer_literals_use_the_declared_signedness is kept and passes.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SPIRV-Tools reads a hex literal as a bit pattern only when it has no sign:
`OpConstant %short +0xFFFF` is 65535 and is refused for a 16-bit signed type,
while `0xFFFF` is still -1. _int_literal took `+0x...` as a bit pattern too, so
it assembled +0xFFFF (i16), +0x8000 (i16), +0x80 (i8), +0xFFFFFFFF (i32),
+0xFFFFFFFFFFFFFFFF (i64) and the same OpSwitch arms where spirv-as refuses
them. Found by @mrbean56.
Twelve more cases in IntegerLiteralRangeTests (77 in all, 40 refused),
measured with the Vulkan SDK 1.4.363.1 spirv-as; seven of them fail on the
previous commit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The Python assembler masked every integer literal to its word size, so
`OpConstant %uint 4294967296` assembled as 0, `OpConstant %uint -1` as
0xffffffff, and `OpDecorate %x Location 4294967296` as 0, where spirv-as
refuses all three. It also wrote a hex literal for a signed type narrower
than 32 bits without sign-extending it (`OpConstant %short 0xFFFF` gave
0x0000ffff where spirv-as gives 0xffffffff, as the spec requires).
_int_literal now reads a literal for a given width and signedness the way
SPIRV-Tools does: unsigned takes 0..2^w-1 and no minus sign; signed takes
-2^(w-1)..2^(w-1)-1 or a hex bit pattern up to 2^w-1; narrow types are
zero- or sign-extended into their word. It is used for OpConstant /
OpSpecConstant, OpSwitch case literals (by the selector's type, which now
carries its signedness), LiteralInteger and PairIdRefLiteralInteger operands,
and the numeric fallbacks for enum values, masks and OpExtInst numbers.
IntegerLiteralRangeTests pins 65 cases (33 refused) measured with the Vulkan SDK
1.4.363.1 spirv-as, and compares each with spirv-as when it is installed.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed narrow hexadecimal constants and case labels require sign extension to the SPIR-V word width. Emitting the unextended words produces binaries rejected by Vulkan validation.
Addresses standalone compatibility review findings on PR #2251 (@FreddyxS):
- Encode width-aware IEEE-754 hex-float literals (16/32/64-bit) directly into bit
patterns, including SPIRV-Tools Inf and NaN payload spellings (0x1p+128 -> 0x7f800000,
0x1.8p+128 -> 0x7fc00000, and f16/f64 equivalents) without OverflowError.
- Match SPIRV-Tools literal string decoding in _unquote_string: \<ch> yields <ch>
verbatim (e.g. \n -> 'n', not LF).
- Add regression test comparing assembled words against spirv-as in tests/test_spirv_asm.py.