Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -13793,6 +13793,8 @@ ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_cmp16_' -V
python -B tests/startup_gfx2hlsl_cmp16_check.py binary <llvm-mc> <llvm-objcopy>
```

`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)
Expand Down
112 changes: 112 additions & 0 deletions tests/startup_gfx2hlsl_refusals_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<I', literal))


def translate_one(code):
"""(decoded text, generated lines) of a single instruction, or the refusal text."""
try:
decoded = G.decode(code)
assert len(decoded) == 1 and decoded[0][1] == len(code), decoded
probe = Probe()
probe.one(decoded[0][2], decoded[0][3])
return decoded[0][3], probe.lines
except G.Unsupported as reason:
return str(reason)
except (AttributeError, KeyError, IndexError, ValueError) as error:
# A handler that needs operands the synthetic probe does not provide: not an accepted form.
return 'probe error %s' % type(error).__name__


def check_neg_integer_sources():
v = lambda n: 256 + n
sentinels = [v(10), v(12), v(14)]
inline = lambda n: 128 + n
names = {}
for op, entry in G.D.VOP3_NATIVE_OPS.items():
names[entry[0]] = op
cndmask = 0x101
accepted = [op for op in range(0x400) if not isinstance(translate_one(vop3(op, sentinels)), str)]
check('the sweep reaches the accepted VOP3 opcodes', len(accepted) > 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(
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('<I', code, off)[0] >> 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('<I', code, off + 4)
for i in range(3):
if name == 'v_cndmask_b32_e64' and i < 2 or not word1 & (1 << (29 + i)):
continue
source = (word1 >> (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


Expand Down
Loading