diff --git a/docs/shader-translation.md b/docs/shader-translation.md index a8b3ac02..8cd4ea1e 100644 --- a/docs/shader-translation.md +++ b/docs/shader-translation.md @@ -26,7 +26,9 @@ The container is read from `/../dxbc/.dxbc` (see Disassembly text, so the decoder prints every instruction exactly as llvm-objdump does for gfx1013 (wave64 lane masks as with `+wavefrontsize64`), branch targets appended as `@T0x`. It also keeps the listing's quirks: a dword that is no instruction is `.long 0x...`, a run of 8 or more zero bytes is left out, and an instruction llvm-objdump lists with an -alignment warning is dropped, as the listing parser drops that line. Opcode numbers and field layouts come from AMD's +alignment warning is dropped, as the listing parser drops that line. A VOP3 source the instruction does not read +may hold 0 or the inline constant 0 (llvm-mc 23 writes the latter, older releases the former); any other value makes the +dword `.long`. Opcode numbers and field layouts come from AMD's "RDNA 2 Instruction Set Architecture" reference guide. `LlvmDisassembler` (llvm-mc wraps the bytes in an object file, llvm-objdump lists it, `ObjdumpParser` reads the diff --git a/src/FiveStar.Shaders/Disassembly/Rdna/Vop3Decoder.cs b/src/FiveStar.Shaders/Disassembly/Rdna/Vop3Decoder.cs index 7d20a0af..61480d0d 100644 --- a/src/FiveStar.Shaders/Disassembly/Rdna/Vop3Decoder.cs +++ b/src/FiveStar.Shaders/Disassembly/Rdna/Vop3Decoder.cs @@ -17,6 +17,9 @@ internal static class Vop3Decoder private const int TiedSources = 2; private const int PermlaneSelects = 3; + /// The source code of the inline constant 0, which current llvm-mc writes in the sources an instruction does not read. + private const int InlineZero = 0x80; + public static Decoded? Decode(CodeWords code, int at) { if (!code.Has(at, Words)) @@ -51,7 +54,7 @@ internal static class Vop3Decoder var count = SourceCount(info, fields.Opcode); var words = Words; - if (fields.SourceCodes.Skip(count).Any(source => source != 0) || !ModifiersFit(info, fields, count)) + if (fields.SourceCodes.Skip(count).Any(source => source is not (0 or InlineZero)) || !ModifiersFit(info, fields, count)) { return null; } diff --git a/tests/FiveStar.Shaders.Tests/Disassembly/RdnaDecoderTests.cs b/tests/FiveStar.Shaders.Tests/Disassembly/RdnaDecoderTests.cs index 4623e5a0..e51f9f5c 100644 --- a/tests/FiveStar.Shaders.Tests/Disassembly/RdnaDecoderTests.cs +++ b/tests/FiveStar.Shaders.Tests/Disassembly/RdnaDecoderTests.cs @@ -52,6 +52,18 @@ public void Lane_masks_follow_the_wave_size(int waveSize, string text) Assert.Equal(text, Assert.Single(new RdnaDecoder().Disassemble(code, waveSize)).Text); } + /// + /// llvm-mc 23 writes the inline constant 0 into the source a VOP3 compare does not read (d4c40028 02020084, where + /// older releases wrote 0): llvm-objdump lists it as the same compare. + /// + [Fact] + public void An_unread_vop3_source_may_be_the_inline_zero() + { + byte[] code = [0x28, 0x00, 0xC4, 0xD4, 0x84, 0x00, 0x02, 0x02]; + + Assert.Equal("v_cmp_gt_u32_e64 s40, 4, v0", Assert.Single(new RdnaDecoder().Disassemble(code, Wave32)).Text); + } + /// The smallest programs of each stage of the dump decode exactly as with llvm-mc/llvm-objdump. [LlvmProgramTheory] [InlineData(8)]