diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h --- a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h +++ b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h @@ -115,14 +115,25 @@ template DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst, - uint64_t Address) const { + uint64_t Address, raw_ostream &Comments) const { assert(MI.getOpcode() == 0); assert(MI.getNumOperands() == 0); MCInst TmpInst; HasLiteral = false; const auto SavedBytes = Bytes; - if (decodeInstruction(Table, TmpInst, Inst, Address, this, STI)) { + + SmallString<64> LocalComments; + raw_svector_ostream LocalCommentStream(LocalComments); + CommentStream = &LocalCommentStream; + + DecodeStatus Res = + decodeInstruction(Table, TmpInst, Inst, Address, this, STI); + + CommentStream = nullptr; + + if (Res != Fail) { MI = TmpInst; + Comments << LocalComments; return MCDisassembler::Success; } Bytes = SavedBytes; diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp --- a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp +++ b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp @@ -413,7 +413,6 @@ ArrayRef Bytes_, uint64_t Address, raw_ostream &CS) const { - CommentStream = &CS; bool IsSDWA = false; unsigned MaxInstBytesNum = std::min((size_t)TargetMaxInstBytes, Bytes_.size()); @@ -428,13 +427,11 @@ // encodings if (isGFX11Plus() && Bytes.size() >= 12 ) { DecoderUInt128 DecW = eat12Bytes(Bytes); - Res = tryDecodeInst(DecoderTableDPP8GFX1196, MI, DecW, - Address); + Res = tryDecodeInst(DecoderTableDPP8GFX1196, MI, DecW, Address, CS); if (Res && convertDPP8Inst(MI) == MCDisassembler::Success) break; MI = MCInst(); // clear - Res = tryDecodeInst(DecoderTableDPPGFX1196, MI, DecW, - Address); + Res = tryDecodeInst(DecoderTableDPPGFX1196, MI, DecW, Address, CS); if (Res) { if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VOP3P) convertVOP3PDPPInst(MI); @@ -446,7 +443,7 @@ } break; } - Res = tryDecodeInst(DecoderTableGFX1196, MI, DecW, Address); + Res = tryDecodeInst(DecoderTableGFX1196, MI, DecW, Address, CS); if (Res) break; } @@ -457,7 +454,7 @@ const uint64_t QW = eatBytes(Bytes); if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding)) { - Res = tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address, CS); if (Res) { if (AMDGPU::getNamedOperandIdx(MI.getOpcode(), AMDGPU::OpName::dpp8) == -1) @@ -468,37 +465,37 @@ } } - Res = tryDecodeInst(DecoderTableDPP864, MI, QW, Address); + Res = tryDecodeInst(DecoderTableDPP864, MI, QW, Address, CS); if (Res && convertDPP8Inst(MI) == MCDisassembler::Success) break; MI = MCInst(); // clear - Res = tryDecodeInst(DecoderTableDPP8GFX1164, MI, QW, Address); + Res = tryDecodeInst(DecoderTableDPP8GFX1164, MI, QW, Address, CS); if (Res && convertDPP8Inst(MI) == MCDisassembler::Success) break; MI = MCInst(); // clear - Res = tryDecodeInst(DecoderTableDPP64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableDPP64, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableDPPGFX1164, MI, QW, Address); + Res = tryDecodeInst(DecoderTableDPPGFX1164, MI, QW, Address, CS); if (Res) { if (MCII->get(MI.getOpcode()).TSFlags & SIInstrFlags::VOPC) convertVOPCDPPInst(MI); break; } - Res = tryDecodeInst(DecoderTableSDWA64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableSDWA64, MI, QW, Address, CS); if (Res) { IsSDWA = true; break; } - Res = tryDecodeInst(DecoderTableSDWA964, MI, QW, Address); + Res = tryDecodeInst(DecoderTableSDWA964, MI, QW, Address, CS); if (Res) { IsSDWA = true; break; } - Res = tryDecodeInst(DecoderTableSDWA1064, MI, QW, Address); + Res = tryDecodeInst(DecoderTableSDWA1064, MI, QW, Address, CS); if (Res) { IsSDWA = true; break; } if (STI.hasFeature(AMDGPU::FeatureUnpackedD16VMem)) { - Res = tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS); if (Res) break; } @@ -507,7 +504,7 @@ // v_mad_mixhi_f16 for FMA variants. Try to decode using this special // table first so we print the correct name. if (STI.hasFeature(AMDGPU::FeatureFmaMixInsts)) { - Res = tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS); if (Res) break; } @@ -519,64 +516,64 @@ // Try decode 32-bit instruction if (Bytes.size() < 4) break; const uint32_t DW = eatBytes(Bytes); - Res = tryDecodeInst(DecoderTableGFX832, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address); + Res = tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableGFX932, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS); if (Res) break; if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts)) { - Res = tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS); if (Res) break; } if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding)) { - Res = tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS); if (Res) break; } - Res = tryDecodeInst(DecoderTableGFX1032, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableGFX1132, MI, DW, Address); + Res = tryDecodeInst(DecoderTableGFX1132, MI, DW, Address, CS); if (Res) break; if (Bytes.size() < 4) break; const uint64_t QW = ((uint64_t)eatBytes(Bytes) << 32) | DW; if (STI.hasFeature(AMDGPU::FeatureGFX940Insts)) { - Res = tryDecodeInst(DecoderTableGFX94064, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS); if (Res) break; } if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts)) { - Res = tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS); if (Res) break; } - Res = tryDecodeInst(DecoderTableGFX864, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableAMDGPU64, MI, QW, Address); + Res = tryDecodeInst(DecoderTableAMDGPU64, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableGFX964, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableGFX1064, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableGFX1164, MI, QW, Address); + Res = tryDecodeInst(DecoderTableGFX1164, MI, QW, Address, CS); if (Res) break; - Res = tryDecodeInst(DecoderTableWMMAGFX1164, MI, QW, Address); + Res = tryDecodeInst(DecoderTableWMMAGFX1164, MI, QW, Address, CS); } while (false); if (Res && AMDGPU::isMAC(MI.getOpcode())) { diff --git a/llvm/test/MC/Disassembler/AMDGPU/comments.txt b/llvm/test/MC/Disassembler/AMDGPU/comments.txt --- a/llvm/test/MC/Disassembler/AMDGPU/comments.txt +++ b/llvm/test/MC/Disassembler/AMDGPU/comments.txt @@ -1,5 +1,8 @@ # RUN: llvm-mc -assemble -triple=amdgcn--amdhsa -mcpu=gfx1100 -filetype=obj %s -o - | \ # RUN: llvm-objdump -d - | FileCheck %s -; CHECK: v_perm_b32 v14, v52, v51, 0x5040100 // 000000000000: D644000E 03FE6734 05040100 ; Error: cannot read literal, inst bytes left 0{{$}} +; Make sure disassembling of this instruction does not cause any errors +; generated in the comment. For this test to do its job, the instruction +; has to be the last or the only one in the object file. +; CHECK: v_perm_b32 v14, v52, v51, 0x5040100 // 000000000000: D644000E 03FE6734 05040100{{$}} v_perm_b32 v14, v52, v51, 0x5040100