diff --git a/docs/experiments.md b/docs/experiments.md index 6b9b1a1d02..2567d978e5 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -13793,6 +13793,8 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cmp16_' -V python -B tests/startup_gfx2hlsl_cmp16_check.py binary ``` +`tools/startup/gfx2hlsl.py` also refuses the NEG bit of a VOP3 source that is a non-negative inline integer (0 to 64) or a literal. The disassembler prints inline 1 with NEG as `-1`, the same text as inline -1, so the text cannot tell the two encodings apart. The exception is the two data operands of `v_cndmask_b32_e64`, which model NEG as a flip of bit 31. The hardware behavior of NEG on integer sources is not measured. Check: `tests/startup_gfx2hlsl_refusals_check.py`. + ## Startup translator: explicit-LOD image sampling `tools/startup/gfx2hlsl.py` now lowers the gfx1013 `image_sample_l` form of a 2D or 3D texture sample to `tex.SampleLevel(smp, uv, level)`. The level is the last address register: the third for a contiguous 2D address, the fourth for 3D, and the last element of an NSA list. `tests/startup_image_sample_l_check.py` translates source-built fixtures (the words are checked against `llvm-mc`) and requires the level to come from that register; an address list `[v0, v1, v3]` must use `v3`, not the third consecutive register. Replacing the level with 0, replacing the call with implicit `Sample`, or reading the third register in every form each make the check fail. Offset (`_o`), compare (`_c_l`), gather, 1D, array, cube and multisample dimensions, an empty `dmask`, and the `unorm`, `r128`, `tfe`, `lwe`, `a16` and `d16` modifiers or any set reserved MIMG bit are refused with an `Unsupported` message; only the cache-policy bits `glc`, `slc` and `dlc` are accepted because they do not change the sampled value. The disassembler previously read `lwe` from reserved bit 14 of word 0; `llvm-mc` (19, gfx1013) encodes it in bit 17, and the check now has one `llvm-mc`-built fixture per refused modifier plus patched reserved-bit fixtures. Not measured: no draw samples a mip level on WARP or hardware through this lowering, so the level selection is checked on the generated HLSL text only. [HLSL SampleLevel](https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-to-samplelevel) diff --git a/tests/startup_gfx2hlsl_refusals_check.py b/tests/startup_gfx2hlsl_refusals_check.py index 4f1f77b6df..f2f22d9662 100644 --- a/tests/startup_gfx2hlsl_refusals_check.py +++ b/tests/startup_gfx2hlsl_refusals_check.py @@ -72,6 +72,117 @@ def broken(self, ops, mods): found == preceding + [(label + ' in v_census_probe: TypeError', 'ps'), (SECOND, 'ps')]) +class Probe(G.Stage): + """A Stage with no pipeline: enough state for decoded instructions to be translated.""" + + def __init__(self): + self.lines, self.sym = [], {} + self.used_s, self.used_v, self.uniform_v = set(), set(), set() + self.masks = set() + self.exec_known = True + + +def vop3(op, srcs, vdst=4, sdst=0, neg=0, absolute=0, literal=None): + """One VOP3 instruction: w0 = 0b110101 | op | abs | SDST (VOP3b) or VDST, w1 = three 9-bit sources | NEG.""" + w0 = (0b110101 << 26) | (op << 16) | (absolute << 8) | (sdst << 8) | vdst + w1 = srcs[0] | (srcs[1] << 9) | (srcs[2] << 18) | (neg << 29) + return struct.pack('<2I', w0, w1) + (b'' if literal is None else struct.pack(' 100 and cndmask in accepted) + loose = [] + for op in accepted: + for slot in range(3): + srcs = list(sentinels) + srcs[slot] = inline(1) + plain = translate_one(vop3(op, srcs)) + result = translate_one(vop3(op, srcs, neg=1 << slot)) + modeled = op == cndmask and slot < 2 + if isinstance(plain, str) or modeled: + continue + # Ops with their own, earlier refusal of NEG and ABS (the 16-bit compares, v_sad_*) keep it. + if not (isinstance(result, str) and ('NEG on an integer or literal source %d' % slot in result + or ' with neg/abs' in result)): + loose.append((hex(op), slot, result)) + check('NEG on inline 1 is refused for every accepted opcode and slot except v_cndmask_b32_e64 data sources', + not loose) + if loose: + print(' first misses: %s' % loose[:4]) + + def refused(code, slot): + text = translate_one(code) + return isinstance(text, str) and 'NEG on an integer or literal source %d' % slot in text + + iadd, fadd, cmp_lt, add_co, shl64 = names['v_mad_u32_u24'], 0x103, 0x81, names['v_add_co_u32'], names['v_lshlrev_b64'] + for slot in range(3): + srcs = list(sentinels) + srcs[slot] = inline(1) + check('v_mad_u32_u24 slot %d: inline 1 with NEG refused' % slot, + refused(vop3(iadd, srcs, neg=1 << slot), slot)) + check('v_mad_u32_u24 slot %d: the same word without NEG still translates' % slot, + not isinstance(translate_one(vop3(iadd, srcs)), str)) + check('inline 0 with NEG refused', refused(vop3(iadd, [inline(0), sentinels[1], sentinels[2]], neg=1), 0)) + check('inline 64 with NEG refused', refused(vop3(iadd, [inline(64), sentinels[1], sentinels[2]], neg=1), 0)) + check('literal with NEG refused', refused(vop3(iadd, [255, sentinels[1], sentinels[2]], neg=1, literal=5), 0)) + check('literal without NEG translates', not isinstance( + translate_one(vop3(iadd, [255, sentinels[1], sentinels[2]], literal=5)), str)) + check('v_add_f32_e64 with an integer inline and NEG refused', refused(vop3(fadd, [inline(1), v(12), 0], neg=1), 0)) + check('compare with inline 1 and NEG refused', refused(vop3(cmp_lt, [inline(1), v(12), 0], sdst=4, neg=1), 0)) + check('compare slot 1 with literal and NEG refused', refused( + vop3(cmp_lt, [v(10), 255, 0], sdst=4, neg=2, literal=7), 1)) + check('v_add_co_u32 (carry out) with inline 1 and NEG refused', + refused(vop3(add_co, [inline(1), v(12), 0], sdst=4, neg=1), 0)) + check('v_lshlrev_b64 with NEG on the shift inline refused', refused(vop3(shl64, [inline(1), v(12), 0], neg=1), 0)) + + # Negative controls. + minus_one = translate_one(vop3(iadd, [193, sentinels[1], sentinels[2]])) + check('plain inline -1 still reads 0xffffffff', + not isinstance(minus_one, str) and '0xffffffffu' in ' '.join(minus_one[1])) + negative = translate_one(vop3(iadd, [193, sentinels[1], sentinels[2]], neg=1)) + check('inline -1 with NEG is not refused by this rule', + not (isinstance(negative, str) and 'NEG on an integer' in negative)) + for code in range(240, 249): + result = translate_one(vop3(fadd, [code, v(12), 0], neg=1)) + check('inline float %d with NEG is not refused by this rule' % code, + not (isinstance(result, str) and 'NEG on an integer' in result)) + check('v_add_co_u32 with sdst s4 and register sources translates', + not isinstance(translate_one(vop3(add_co, [v(10), v(12), 0], sdst=4)), str)) + check('v_add_co_u32 with sdst s4 and an inline source translates', + not isinstance(translate_one(vop3(add_co, [inline(1), v(12), 0], sdst=4)), str)) + for slot in range(2): + srcs = [v(10), v(12), 106] + srcs[slot] = inline(1) + result = translate_one(vop3(cndmask, srcs, neg=1 << slot)) + check('v_cndmask_b32_e64 slot %d: inline 1 with NEG still translates' % slot, + not isinstance(result, str) and '0x80000001u' in ' '.join(result[1])) + check('v_cndmask_b32_e64 mask slot: inline 1 with NEG is refused', + refused(vop3(cndmask, [v(10), v(12), inline(1)], neg=4), 2)) + + def check_decode(): code = struct.pack('<2I', 0, END) with patch.object(G.D, 'decode_instruction', decode_instruction), patch.object( @@ -314,6 +425,7 @@ def check_normalization(): with patch.dict(os.environ, {'X2W_FXC': ''}): check_stage() check_decode() + check_neg_integer_sources() check_cli() check_request_failures() check_pairs() diff --git a/tools/startup/gfx2hlsl.py b/tools/startup/gfx2hlsl.py index 34f5a14a33..0dc15d1bd9 100644 --- a/tools/startup/gfx2hlsl.py +++ b/tools/startup/gfx2hlsl.py @@ -401,6 +401,20 @@ def check_encoding(code, off, n, name, asm): operand = fields[i + 1] fields[i + 1] = operand[:len(operand) - len(operand.lstrip())] + '0x%08x' % (bits ^ 0x80000000) asm = ','.join(fields) + if n >= 8 and struct.unpack_from('> 26 == 0b110101: + # VOP3 NEG (w1[31:29], bit 29 + i for source i) on a non-negative inline integer (128..192) or a literal (255). + # The decoder prints inline 1 with NEG as `-1`, the text of the plain inline -1 (193), so both would translate to + # 0xffffffff, and a literal 5 would become 0xfffffffb. Only v_cndmask_b32_e64 models NEG on its two data + # sources (above, as the sign bit flipped). Inline floats, registers and negative inlines are not affected. + # ABS is not tested: on the VOP3b forms w0[10:8] is SDST, not a modifier. + word1, = struct.unpack_from('> (9 * i)) & 0x1ff + if 128 <= source <= 192 or source == 255: + raise Unsupported('%s with NEG on an integer or literal source %d: the printed sign would read as inline -N' + % (name, i)) return asm