|
| 1 | +//===- comgr-hotswap-patch-sethalt-fix.cpp - in-shader sethalt fix ------===// |
| 2 | +// |
| 3 | +// Part of Comgr, under the Apache License v2.0 with LLVM Exceptions. See |
| 4 | +// amd/comgr/LICENSE.TXT in this repository for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file |
| 10 | +/// Strong-symbol override for applySethaltFixPatch. Defensive hotswap |
| 11 | +/// pass for the GFX1250 A0 LD_SCALE+WMMA clause-break bug as triggered |
| 12 | +/// by the in-shader `s_sethalt` instruction. |
| 13 | +/// |
| 14 | +/// On GFX1250 A0, the SQ has an FSM bug where a wave halt issued just |
| 15 | +/// before LD_SCALE breaks the implicit LD_SCALE+WMMA clause: an |
| 16 | +/// asynchronous clause-break event during the halt allows other waves |
| 17 | +/// to interleave between this wave's LD_SCALE and its WMMA, which |
| 18 | +/// either picks up a leaked scale (corrupting another wave's WMMA) or |
| 19 | +/// loses its own scale (this wave's WMMA runs unscaled). |
| 20 | +/// |
| 21 | +/// "Software running on A0 cannot use SQ_CMD.HALT operations" covers |
| 22 | +/// two halt sources: |
| 23 | +/// 1. `s_sethalt` -- in-shader instruction. Reachable from compiler |
| 24 | +/// code objects; this pass handles it. |
| 25 | +/// 2. SQ_CMD.HALT (SETHALT) -- external register write from the host |
| 26 | +/// (debugger breakpoints, rocprofiler PC sampling, kernel CWSR |
| 27 | +/// paths). NOT reachable from COMGR; needs ROCdbgapi / |
| 28 | +/// rocprofiler / runtime changes to skip on GFX1250 A0. Out of |
| 29 | +/// scope for this pass. |
| 30 | +/// |
| 31 | +/// LLVM almost never emits `s_sethalt` in production code -- it appears |
| 32 | +/// only via the `int_amdgcn_s_sethalt` intrinsic, which is largely |
| 33 | +/// confined to debug builds. This pass exists for defense-in-depth |
| 34 | +/// against any input ELF (compiler-emitted or hand-written) that |
| 35 | +/// contains the instruction, and would otherwise hit the SQ FSM bug |
| 36 | +/// when the kernel also uses VOP3PX2. |
| 37 | +/// |
| 38 | +/// Strategy: detect every `s_sethalt` (mnemonic match against the MC |
| 39 | +/// printer output) and rewrite to `s_nop 0` in-place. Same 4 bytes, no |
| 40 | +/// relocation, no length change. Replacement bytes come from |
| 41 | +/// LS.SNopBytes (pre-encoded at initLLVM() time -- same source the |
| 42 | +/// splitter uses for trampoline padding). The shader proceeds without |
| 43 | +/// the halt; if the halt was for an in-shader debug breakpoint, that's |
| 44 | +/// the correct trade-off on A0 (the debugger should switch to |
| 45 | +/// trap-based breakpoints, which is the source-2 fix the debugger team |
| 46 | +/// owns). |
| 47 | +/// |
| 48 | +//===----------------------------------------------------------------------===// |
| 49 | + |
| 50 | +#include "comgr-hotswap-internal.h" |
| 51 | + |
| 52 | +#include "llvm/ADT/StringExtras.h" |
| 53 | +#include "llvm/ADT/StringRef.h" |
| 54 | + |
| 55 | +using namespace llvm; |
| 56 | + |
| 57 | +namespace COMGR { |
| 58 | +namespace hotswap { |
| 59 | +namespace { |
| 60 | + |
| 61 | +constexpr StringLiteral SSethaltMnemonic = "s_sethalt"; |
| 62 | + |
| 63 | +uint32_t applySethaltFixPatchImpl(PatchContext &Ctx) { |
| 64 | + if (Ctx.LS.SNopBytes.size() != MinInstSize) { |
| 65 | + log() << "hotswap: error: sethalt-fix: LS.SNopBytes is not " << MinInstSize |
| 66 | + << " bytes (got " << Ctx.LS.SNopBytes.size() << ")\n"; |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + uint32_t Patched = 0; |
| 71 | + for (const InternalDecodedInst &DI : Ctx.Decoded) { |
| 72 | + if (DI.Mnemonic != SSethaltMnemonic) |
| 73 | + continue; |
| 74 | + if (DI.Size != MinInstSize) { |
| 75 | + log() << "hotswap: error: sethalt-fix: s_sethalt at .text offset 0x" |
| 76 | + << utohexstr(DI.Offset) << " has unexpected size " << DI.Size |
| 77 | + << " (expected " << MinInstSize << ")\n"; |
| 78 | + continue; |
| 79 | + } |
| 80 | + // DI came from decodeTextSection over Ctx.Text, so this should only fire |
| 81 | + // if a future caller supplies an inconsistent decoded stream/text pair. |
| 82 | + if (DI.Offset + DI.Size > Ctx.TextSize) { |
| 83 | + log() << "hotswap: error: sethalt-fix: s_sethalt at .text offset 0x" |
| 84 | + << utohexstr(DI.Offset) << " extends past .text size 0x" |
| 85 | + << utohexstr(Ctx.TextSize) << "\n"; |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + std::memcpy(Ctx.Text + DI.Offset, Ctx.LS.SNopBytes.data(), MinInstSize); |
| 90 | + |
| 91 | + std::string KernelName = |
| 92 | + Ctx.Elf.findKernelAtOffset(DI.Offset + Ctx.Elf.textAddr()); |
| 93 | + StringRef KernelDisplay = |
| 94 | + KernelName.empty() ? StringRef("<unknown>") : StringRef(KernelName); |
| 95 | + log() << "hotswap: sethalt-fix: neutralized s_sethalt in kernel '" |
| 96 | + << KernelDisplay << "' at .text offset 0x" << utohexstr(DI.Offset) |
| 97 | + << "\n"; |
| 98 | + ++Patched; |
| 99 | + } |
| 100 | + |
| 101 | + return Patched; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace |
| 105 | + |
| 106 | +void registerSethaltFixPatch(HotswapPatchVTable &VT) { |
| 107 | + VT.applySethaltFixPatch = &applySethaltFixPatchImpl; |
| 108 | +} |
| 109 | + |
| 110 | +} // namespace hotswap |
| 111 | +} // namespace COMGR |
0 commit comments