[llvm] [LLVM][TableGen] Move DecoderEmitter output to anonymous namespace (PR #136214)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 17 15:49:15 PDT 2025
https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/136214
>From e7911cbe2889c1a1428e9807dd9315b84d63a8eb Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
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 | 109 +++++++++++++-----
.../AMDGPU/Disassembler/AMDGPUDisassembler.h | 42 +------
llvm/utils/TableGen/DecoderEmitter.cpp | 4 +-
3 files changed, 83 insertions(+), 72 deletions(-)
diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp
index 847121f251361..edcd9890c8404 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 <typename InsnType>
+static DecodeStatus
+tryDecodeInst(const AMDGPUDisassembler *Asm, const uint8_t *Table, MCInst &MI,
+ InsnType Inst, uint64_t Address, raw_ostream &Comments) {
+ assert(MI.getOpcode() == 0);
+ assert(MI.getNumOperands() == 0);
+ MCInst TmpInst;
+ Asm->setHasLiteral(false);
+ const auto SavedBytes = Asm->getBytes();
+
+ SmallString<64> LocalComments;
+ raw_svector_ostream LocalCommentStream(LocalComments);
+ Asm->CommentStream = &LocalCommentStream;
+
+ DecodeStatus Res = decodeInstruction(Table, TmpInst, Inst, Address, Asm,
+ Asm->getSubtargetInfo());
+
+ Asm->CommentStream = nullptr;
+
+ if (Res != MCDisassembler::Fail) {
+ MI = TmpInst;
+ Comments << LocalComments;
+ return MCDisassembler::Success;
+ }
+ Asm->setBytes(SavedBytes);
+ return MCDisassembler::Fail;
+}
+
+template <typename InsnType>
+static DecodeStatus tryDecodeInst(const AMDGPUDisassembler *Asm,
+ const uint8_t *Table1, const uint8_t *Table2,
+ MCInst &MI, InsnType Inst, uint64_t Address,
+ raw_ostream &Comments) {
+ for (const uint8_t *T : {Table1, Table2}) {
+ if (DecodeStatus Res = tryDecodeInst(Asm, T, MI, Inst, Address, Comments))
+ return Res;
+ }
+ return MCDisassembler::Fail;
+}
+
template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
assert(Bytes.size() >= sizeof(T));
const auto Res =
@@ -538,17 +578,17 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
DecoderUInt128 DecW = eat12Bytes(Bytes);
if (isGFX11() &&
- tryDecodeInst(DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI,
- DecW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1196, DecoderTableGFX11_FAKE1696,
+ MI, DecW, Address, CS))
break;
if (isGFX12() &&
- tryDecodeInst(DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI,
- DecW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1296, DecoderTableGFX12_FAKE1696,
+ MI, DecW, Address, CS))
break;
if (isGFX12() &&
- tryDecodeInst(DecoderTableGFX12W6496, MI, DecW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX12W6496, MI, DecW, Address, CS))
break;
// Reinitialize Bytes
@@ -557,7 +597,7 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
} else if (Bytes.size() >= 16 &&
STI.hasFeature(AMDGPU::FeatureGFX950Insts)) {
DecoderUInt128 DecW = eat16Bytes(Bytes);
- if (tryDecodeInst(DecoderTableGFX940128, MI, DecW, Address, CS))
+ if (tryDecodeInst(this, DecoderTableGFX940128, MI, DecW, Address, CS))
break;
// Reinitialize Bytes
@@ -568,58 +608,61 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const uint64_t QW = eatBytes<uint64_t>(Bytes);
if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
- tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX10_B64, MI, QW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureUnpackedD16VMem) &&
- tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX80_UNPACKED64, MI, QW, Address,
+ CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
- tryDecodeInst(DecoderTableGFX95064, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX95064, MI, QW, Address, CS))
break;
// Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
// 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) &&
- tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX9_DL64, MI, QW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX940Insts) &&
- tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX94064, MI, QW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
- tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX90A64, MI, QW, Address, CS))
break;
if ((isVI() || isGFX9()) &&
- tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX864, MI, QW, Address, CS))
break;
- if (isGFX9() && tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS))
+ if (isGFX9() &&
+ tryDecodeInst(this, DecoderTableGFX964, MI, QW, Address, CS))
break;
- if (isGFX10() && tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS))
+ if (isGFX10() &&
+ tryDecodeInst(this, DecoderTableGFX1064, MI, QW, Address, CS))
break;
if (isGFX12() &&
- tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
- Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1264, DecoderTableGFX12_FAKE1664,
+ MI, QW, Address, CS))
break;
if (isGFX11() &&
- tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
- Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1164, DecoderTableGFX11_FAKE1664,
+ MI, QW, Address, CS))
break;
if (isGFX11() &&
- tryDecodeInst(DecoderTableGFX11W6464, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX11W6464, MI, QW, Address, CS))
break;
if (isGFX12() &&
- tryDecodeInst(DecoderTableGFX12W6464, MI, QW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX12W6464, MI, QW, Address, CS))
break;
// Reinitialize Bytes
@@ -631,38 +674,40 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
const uint32_t DW = eatBytes<uint32_t>(Bytes);
if ((isVI() || isGFX9()) &&
- tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX832, MI, DW, Address, CS))
break;
- if (tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS))
+ if (tryDecodeInst(this, DecoderTableAMDGPU32, MI, DW, Address, CS))
break;
- if (isGFX9() && tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS))
+ if (isGFX9() &&
+ tryDecodeInst(this, DecoderTableGFX932, MI, DW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
- tryDecodeInst(DecoderTableGFX95032, MI, DW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX95032, MI, DW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
- tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX90A32, MI, DW, Address, CS))
break;
if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
- tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS))
+ tryDecodeInst(this, DecoderTableGFX10_B32, MI, DW, Address, CS))
break;
- if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
+ if (isGFX10() &&
+ tryDecodeInst(this, DecoderTableGFX1032, MI, DW, Address, CS))
break;
if (isGFX11() &&
- tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
- Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1132, DecoderTableGFX11_FAKE1632,
+ MI, DW, Address, CS))
break;
if (isGFX12() &&
- tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
- Address, CS))
+ tryDecodeInst(this, DecoderTableGFX1232, DecoderTableGFX12_FAKE1632,
+ MI, DW, Address, CS))
break;
}
diff --git a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h
index 29452166e21a0..4f4bf6b2731c2 100644
--- a/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h
+++ b/llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h
@@ -128,44 +128,6 @@ class AMDGPUDisassembler : public MCDisassembler {
MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
- template <typename InsnType>
- 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;
- }
-
- template <typename InsnType>
- 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;
- }
-
Expected<bool> onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
ArrayRef<uint8_t> Bytes,
uint64_t Address) const override;
@@ -294,6 +256,10 @@ class AMDGPUDisassembler : public MCDisassembler {
bool hasKernargPreload() const;
bool isMacDPP(MCInst &MI) const;
+
+ void setHasLiteral(bool Val) const { HasLiteral = Val; }
+ void setBytes(ArrayRef<uint8_t> Val) const { Bytes = Val; }
+ ArrayRef<uint8_t> getBytes() const { return Bytes; }
};
//===----------------------------------------------------------------------===//
diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 9c6015cc24576..35c928fae7b2b 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 <assert.h>
-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} // end anonymous namespace\n";
}
void llvm::EmitDecoder(const RecordKeeper &RK, raw_ostream &OS,
More information about the llvm-commits
mailing list