[AMDGPU] Incorrect outputs from tiling kernel after PR #129464
To reproduce, grab https://github.com/user-attachments/files/19236058/129464-reproducer.zip and, on a gfx942/MI-300, make test after (if necessary) editing the Makefile to point at the LLVM build you'd like to test.
The IR that triggers the miscompile is in input.ll within that attached file, and compiles correctly under -O1 but does not compile correctly under -O3.
The test harness, which demonstrates the expected array packing the program performs, is in driver.cpp in the attached reproducer. The code takes a 100x250 array of 32-bit ints an input and produces a 16x4x16x32 array, where out[i][j][k][l] == in[j * 32 + l][i * 16 + k], defaulting to 42 for elements that would be out of bounds in in.
The failure is
Mismatch at (0, 1, 14, 4) got 11014 but wanted 9014
Mismatch at (0, 2, 14, 4) got 19014 but wanted 17014
Mismatch at (1, 0, 14, 4) got 3030 but wanted 1030
Mismatch at (1, 1, 14, 4) got 11030 but wanted 9030
Mismatch at (1, 2, 14, 4) got 19030 but wanted 17030
Mismatch at (2, 0, 14, 4) got 3046 but wanted 1046
Mismatch at (2, 1, 14, 4) got 11046 but wanted 9046
Mismatch at (2, 2, 14, 4) got 19046 but wanted 17046
...
Mismatch at (12, 0, 14, 4) got 3206 but wanted 1206
Mismatch at (12, 1, 14, 4) got 11206 but wanted 9206
Mismatch at (12, 2, 14, 4) got 19206 but wanted 17206
Mismatch at (13, 0, 14, 4) got 3222 but wanted 1222
Mismatch at (13, 1, 14, 4) got 11222 but wanted 9222
Mismatch at (13, 2, 14, 4) got 19222 but wanted 17222
Mismatch at (14, 0, 14, 4) got 3238 but wanted 1238
Mismatch at (14, 1, 14, 4) got 11238 but wanted 9238
Mismatch at (14, 2, 14, 4) got 19238 but wanted 17238
as if some particular register were being dropped.
I bisected this failure to #129464 - I don't know if this PR caused the failure or exposed some other underlying bug.
@llvm/issue-subscribers-backend-amdgpu
Author: Krzysztof Drewniak (krzysz00)
The IR that triggers the miscompile is in input.ll within that attached file, and compiles correctly under -O1 but does not compile correctly under -O3.
The test harness, which demonstrates the expected array packing the program performs, is in driver.cpp in the attached reproducer. The code takes a 100x250 array of 32-bit ints an input and produces a 16x4x16x32 array, where out[i][j][k][l] == in[j * 32 + l][i * 16 + k], defaulting to 42 for elements that would be out of bounds in in.
The failure is
Mismatch at (0, 1, 14, 4) got 11014 but wanted 9014
Mismatch at (0, 2, 14, 4) got 19014 but wanted 17014
Mismatch at (1, 0, 14, 4) got 3030 but wanted 1030
Mismatch at (1, 1, 14, 4) got 11030 but wanted 9030
Mismatch at (1, 2, 14, 4) got 19030 but wanted 17030
Mismatch at (2, 0, 14, 4) got 3046 but wanted 1046
Mismatch at (2, 1, 14, 4) got 11046 but wanted 9046
Mismatch at (2, 2, 14, 4) got 19046 but wanted 17046
...
Mismatch at (12, 0, 14, 4) got 3206 but wanted 1206
Mismatch at (12, 1, 14, 4) got 11206 but wanted 9206
Mismatch at (12, 2, 14, 4) got 19206 but wanted 17206
Mismatch at (13, 0, 14, 4) got 3222 but wanted 1222
Mismatch at (13, 1, 14, 4) got 11222 but wanted 9222
Mismatch at (13, 2, 14, 4) got 19222 but wanted 17222
Mismatch at (14, 0, 14, 4) got 3238 but wanted 1238
Mismatch at (14, 1, 14, 4) got 11238 but wanted 9238
Mismatch at (14, 2, 14, 4) got 19238 but wanted 17238
as if some particular register were being dropped.
I bisected this failure to #129464 - I don't know if this PR caused the failure or exposed some other underlying bug.
@krzysz00 @EugeneZelenko do you know if anyone is already working on this, if not can I get assigned?
@bababuck You're welcome to have this one as far as I'm concerned
Update, still am working towards resolving this, I wrote a simulator so that I could easily get an instruction trace and am able to recreate the failure in my simulator. Will pin-point the cause next.
@krzysz00 Looks like this issue is related to the implicit operands inserted during the Prologue/Epilogue Insertion & Frame Finalization pass. In this particular case, that pass performs teh following lowering:
renamable $agpr16_agpr17_agpr18_agpr19_agpr20_agpr21_agpr22_agpr23_agpr24_agpr25_agpr26_agpr27_agpr28_agpr29_agpr30_agpr31 = SI_SPILL_AV512_RESTORE %stack.17, $sgpr32, 0, implicit $exec :: (load (s512) from %stack.17, align 4, addrspace 5)
to this
$agpr16_agpr17_agpr18_agpr19 = SCRATCH_LOAD_DWORDX4_ST 64, 0, implicit $exec, implicit $flat_scr, implicit-def $agpr16_agpr17_agpr18_agpr19_agpr20_agpr21_agpr22_agpr23_agpr24_agpr25_agpr26_agpr27_agpr28_agpr29_agpr30_agpr31 :: (load (s128) from %stack.17, align 4, addrspace 5)
$agpr20_agpr21_agpr22_agpr23 = SCRATCH_LOAD_DWORDX4_ST 80, 0, implicit $exec, implicit $flat_scr :: (load (s128) from %stack.17 + 16, align 4, addrspace 5)
$agpr24_agpr25_agpr26_agpr27 = SCRATCH_LOAD_DWORDX4_ST 96, 0, implicit $exec, implicit $flat_scr :: (load (s128) from %stack.17 + 32, align 4, addrspace 5)
$agpr31 = COPY $agpr112, implicit $agpr16_agpr17_agpr18_agpr19_agpr20_agpr21_agpr22_agpr23_agpr24_agpr25_agpr26_agpr27_agpr28_agpr29_agpr30_agpr31
$agpr30 = COPY $agpr208, implicit $agpr16_agpr17_agpr18_agpr19_agpr20_agpr21_agpr22_agpr23_agpr24_agpr25_agpr26_agpr27_agpr28_agpr29_agpr30_agpr31
$agpr28_agpr29 = SCRATCH_LOAD_DWORDX2_ST 112, 0, implicit $exec, implicit $flat_scr, implicit-def $agpr16_agpr17_agpr18_agpr19_agpr20_agpr21_agpr22_agpr23_agpr24_agpr25_agpr26_agpr27_agpr28_agpr29_agpr30_agpr31 :: (load (s64) from %stack.17 + 48, align 4, addrspace 5)
During the proceeding machine-cp pass, $agpr30 = COPY $agpr208 is optimized away since due to the following $agpr28_agpr29 = SCRATCH_LOAD_DWORDX2_ST 112, 0, implicit $exec instruction's implicit-def, the machine-cp pass considers the $agpr30 as unused.
There seems to have been issues related to this in the past. From the source code (llvm/lib/Target/AMDGPU/SIRegisterInfo.cpp), it seems this has been a recurring issue. Also see 1f08d3bc3.
The simple fix would be the following and this appears to fix the failing case you attached (when I run it on the emulator I wrote, I don't have access to a gfx942/MI-300). I don't 100% understand why IsLastSubReg was included in this clause in the first place (git history did not provide any answers), but logically it seems like it would be a bug to define the entire register group on the stack restoration.
- if (NeedSuperRegImpOperand && (IsFirstSubReg || IsLastSubReg))
+ if (NeedSuperRegImpOperand && IsFirstSubReg)
MIB.addReg(ValueReg, RegState::Implicit | SrcDstRegState);