From a0f717dc48b2db32b18b2abfbd5c82b4a8776949 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 17 Apr 2025 14:03:48 -0700 Subject: [PATCH] [LLVM][TableGen] Move DecoderEmitter output to anonymous namespace - Move the code generated by DecoderEmitter to anonymous nespace. - Move AMDGPU's usage of this code from header file to .cpp file. --- .../Disassembler/AMDGPUDisassembler.cpp | 40 +++++++++++++++++++ .../AMDGPU/Disassembler/AMDGPUDisassembler.h | 34 +--------------- llvm/utils/TableGen/DecoderEmitter.cpp | 4 +- 3 files changed, 44 insertions(+), 34 deletions(-) diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp index 847121f251361..3fbba17159375 100644 --- a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp +++ b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp @@ -487,6 +487,46 @@ static DecodeStatus decodeVersionImm(MCInst &Inst, unsigned Imm, // //===----------------------------------------------------------------------===// +template +DecodeStatus AMDGPUDisassembler::tryDecodeInst(const uint8_t *Table, MCInst &MI, + InsnType Inst, uint64_t Address, + raw_ostream &Comments) const { + assert(MI.getOpcode() == 0); + assert(MI.getNumOperands() == 0); + MCInst TmpInst; + HasLiteral = false; + const auto SavedBytes = Bytes; + + SmallString<64> LocalComments; + raw_svector_ostream LocalCommentStream(LocalComments); + CommentStream = &LocalCommentStream; + + DecodeStatus Res = + decodeInstruction(Table, TmpInst, Inst, Address, this, STI); + + CommentStream = nullptr; + + if (Res != MCDisassembler::Fail) { + MI = TmpInst; + Comments << LocalComments; + return MCDisassembler::Success; + } + Bytes = SavedBytes; + return MCDisassembler::Fail; +} + +template +DecodeStatus +AMDGPUDisassembler::tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2, + MCInst &MI, InsnType Inst, uint64_t Address, + raw_ostream &Comments) const { + for (const uint8_t *T : {Table1, Table2}) { + if (DecodeStatus Res = tryDecodeInst(T, MI, Inst, Address, Comments)) + return Res; + } + return MCDisassembler::Fail; +} + template static inline T eatBytes(ArrayRef& Bytes) { assert(Bytes.size() >= sizeof(T)); const auto Res = diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h index 29452166e21a0..498ef655b7f45 100644 --- a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h +++ b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h @@ -130,41 +130,11 @@ class AMDGPUDisassembler : public MCDisassembler { template DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst, - uint64_t Address, raw_ostream &Comments) const { - assert(MI.getOpcode() == 0); - assert(MI.getNumOperands() == 0); - MCInst TmpInst; - HasLiteral = false; - const auto SavedBytes = Bytes; - - 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; - return MCDisassembler::Fail; - } - + uint64_t Address, raw_ostream &Comments) const; template DecodeStatus tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2, MCInst &MI, InsnType Inst, uint64_t Address, - raw_ostream &Comments) const { - for (const uint8_t *T : {Table1, Table2}) { - if (DecodeStatus Res = tryDecodeInst(T, MI, Inst, Address, Comments)) - return Res; - } - return MCDisassembler::Fail; - } + raw_ostream &Comments) const; Expected onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size, ArrayRef Bytes, diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp index 9c6015cc24576..75c8c80aebd6d 100644 --- a/llvm/utils/TableGen/DecoderEmitter.cpp +++ b/llvm/utils/TableGen/DecoderEmitter.cpp @@ -2417,7 +2417,7 @@ void DecoderEmitter::run(raw_ostream &o) { #include "llvm/TargetParser/SubtargetFeature.h" #include -namespace llvm { +namespace { )"; emitFieldFromInstruction(OS); @@ -2561,7 +2561,7 @@ namespace llvm { // Emit the main entry point for the decoder, decodeInstruction(). emitDecodeInstruction(OS, IsVarLenInst); - OS << "\n} // end namespace llvm\n"; + OS << "\n} // namespace\n"; } void llvm::EmitDecoder(const RecordKeeper &RK, raw_ostream &OS,