Skip to content

Commit e7911cb

Browse files
committed
[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.
1 parent b3a53cc commit e7911cb

File tree

3 files changed

+83
-72
lines changed

3 files changed

+83
-72
lines changed

llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.cpp

Lines changed: 77 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,46 @@ static DecodeStatus decodeVersionImm(MCInst &Inst, unsigned Imm,
487487
//
488488
//===----------------------------------------------------------------------===//
489489

490+
template <typename InsnType>
491+
static DecodeStatus
492+
tryDecodeInst(const AMDGPUDisassembler *Asm, const uint8_t *Table, MCInst &MI,
493+
InsnType Inst, uint64_t Address, raw_ostream &Comments) {
494+
assert(MI.getOpcode() == 0);
495+
assert(MI.getNumOperands() == 0);
496+
MCInst TmpInst;
497+
Asm->setHasLiteral(false);
498+
const auto SavedBytes = Asm->getBytes();
499+
500+
SmallString<64> LocalComments;
501+
raw_svector_ostream LocalCommentStream(LocalComments);
502+
Asm->CommentStream = &LocalCommentStream;
503+
504+
DecodeStatus Res = decodeInstruction(Table, TmpInst, Inst, Address, Asm,
505+
Asm->getSubtargetInfo());
506+
507+
Asm->CommentStream = nullptr;
508+
509+
if (Res != MCDisassembler::Fail) {
510+
MI = TmpInst;
511+
Comments << LocalComments;
512+
return MCDisassembler::Success;
513+
}
514+
Asm->setBytes(SavedBytes);
515+
return MCDisassembler::Fail;
516+
}
517+
518+
template <typename InsnType>
519+
static DecodeStatus tryDecodeInst(const AMDGPUDisassembler *Asm,
520+
const uint8_t *Table1, const uint8_t *Table2,
521+
MCInst &MI, InsnType Inst, uint64_t Address,
522+
raw_ostream &Comments) {
523+
for (const uint8_t *T : {Table1, Table2}) {
524+
if (DecodeStatus Res = tryDecodeInst(Asm, T, MI, Inst, Address, Comments))
525+
return Res;
526+
}
527+
return MCDisassembler::Fail;
528+
}
529+
490530
template <typename T> static inline T eatBytes(ArrayRef<uint8_t>& Bytes) {
491531
assert(Bytes.size() >= sizeof(T));
492532
const auto Res =
@@ -538,17 +578,17 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
538578
DecoderUInt128 DecW = eat12Bytes(Bytes);
539579

540580
if (isGFX11() &&
541-
tryDecodeInst(DecoderTableGFX1196, DecoderTableGFX11_FAKE1696, MI,
542-
DecW, Address, CS))
581+
tryDecodeInst(this, DecoderTableGFX1196, DecoderTableGFX11_FAKE1696,
582+
MI, DecW, Address, CS))
543583
break;
544584

545585
if (isGFX12() &&
546-
tryDecodeInst(DecoderTableGFX1296, DecoderTableGFX12_FAKE1696, MI,
547-
DecW, Address, CS))
586+
tryDecodeInst(this, DecoderTableGFX1296, DecoderTableGFX12_FAKE1696,
587+
MI, DecW, Address, CS))
548588
break;
549589

550590
if (isGFX12() &&
551-
tryDecodeInst(DecoderTableGFX12W6496, MI, DecW, Address, CS))
591+
tryDecodeInst(this, DecoderTableGFX12W6496, MI, DecW, Address, CS))
552592
break;
553593

554594
// Reinitialize Bytes
@@ -557,7 +597,7 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
557597
} else if (Bytes.size() >= 16 &&
558598
STI.hasFeature(AMDGPU::FeatureGFX950Insts)) {
559599
DecoderUInt128 DecW = eat16Bytes(Bytes);
560-
if (tryDecodeInst(DecoderTableGFX940128, MI, DecW, Address, CS))
600+
if (tryDecodeInst(this, DecoderTableGFX940128, MI, DecW, Address, CS))
561601
break;
562602

563603
// Reinitialize Bytes
@@ -568,58 +608,61 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
568608
const uint64_t QW = eatBytes<uint64_t>(Bytes);
569609

570610
if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
571-
tryDecodeInst(DecoderTableGFX10_B64, MI, QW, Address, CS))
611+
tryDecodeInst(this, DecoderTableGFX10_B64, MI, QW, Address, CS))
572612
break;
573613

574614
if (STI.hasFeature(AMDGPU::FeatureUnpackedD16VMem) &&
575-
tryDecodeInst(DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
615+
tryDecodeInst(this, DecoderTableGFX80_UNPACKED64, MI, QW, Address,
616+
CS))
576617
break;
577618

578619
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
579-
tryDecodeInst(DecoderTableGFX95064, MI, QW, Address, CS))
620+
tryDecodeInst(this, DecoderTableGFX95064, MI, QW, Address, CS))
580621
break;
581622

582623
// Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
583624
// v_mad_mixhi_f16 for FMA variants. Try to decode using this special
584625
// table first so we print the correct name.
585626
if (STI.hasFeature(AMDGPU::FeatureFmaMixInsts) &&
586-
tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS))
627+
tryDecodeInst(this, DecoderTableGFX9_DL64, MI, QW, Address, CS))
587628
break;
588629

589630
if (STI.hasFeature(AMDGPU::FeatureGFX940Insts) &&
590-
tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS))
631+
tryDecodeInst(this, DecoderTableGFX94064, MI, QW, Address, CS))
591632
break;
592633

593634
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
594-
tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS))
635+
tryDecodeInst(this, DecoderTableGFX90A64, MI, QW, Address, CS))
595636
break;
596637

597638
if ((isVI() || isGFX9()) &&
598-
tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS))
639+
tryDecodeInst(this, DecoderTableGFX864, MI, QW, Address, CS))
599640
break;
600641

601-
if (isGFX9() && tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS))
642+
if (isGFX9() &&
643+
tryDecodeInst(this, DecoderTableGFX964, MI, QW, Address, CS))
602644
break;
603645

604-
if (isGFX10() && tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS))
646+
if (isGFX10() &&
647+
tryDecodeInst(this, DecoderTableGFX1064, MI, QW, Address, CS))
605648
break;
606649

607650
if (isGFX12() &&
608-
tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
609-
Address, CS))
651+
tryDecodeInst(this, DecoderTableGFX1264, DecoderTableGFX12_FAKE1664,
652+
MI, QW, Address, CS))
610653
break;
611654

612655
if (isGFX11() &&
613-
tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
614-
Address, CS))
656+
tryDecodeInst(this, DecoderTableGFX1164, DecoderTableGFX11_FAKE1664,
657+
MI, QW, Address, CS))
615658
break;
616659

617660
if (isGFX11() &&
618-
tryDecodeInst(DecoderTableGFX11W6464, MI, QW, Address, CS))
661+
tryDecodeInst(this, DecoderTableGFX11W6464, MI, QW, Address, CS))
619662
break;
620663

621664
if (isGFX12() &&
622-
tryDecodeInst(DecoderTableGFX12W6464, MI, QW, Address, CS))
665+
tryDecodeInst(this, DecoderTableGFX12W6464, MI, QW, Address, CS))
623666
break;
624667

625668
// Reinitialize Bytes
@@ -631,38 +674,40 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
631674
const uint32_t DW = eatBytes<uint32_t>(Bytes);
632675

633676
if ((isVI() || isGFX9()) &&
634-
tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS))
677+
tryDecodeInst(this, DecoderTableGFX832, MI, DW, Address, CS))
635678
break;
636679

637-
if (tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS))
680+
if (tryDecodeInst(this, DecoderTableAMDGPU32, MI, DW, Address, CS))
638681
break;
639682

640-
if (isGFX9() && tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS))
683+
if (isGFX9() &&
684+
tryDecodeInst(this, DecoderTableGFX932, MI, DW, Address, CS))
641685
break;
642686

643687
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
644-
tryDecodeInst(DecoderTableGFX95032, MI, DW, Address, CS))
688+
tryDecodeInst(this, DecoderTableGFX95032, MI, DW, Address, CS))
645689
break;
646690

647691
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
648-
tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS))
692+
tryDecodeInst(this, DecoderTableGFX90A32, MI, DW, Address, CS))
649693
break;
650694

651695
if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
652-
tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS))
696+
tryDecodeInst(this, DecoderTableGFX10_B32, MI, DW, Address, CS))
653697
break;
654698

655-
if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
699+
if (isGFX10() &&
700+
tryDecodeInst(this, DecoderTableGFX1032, MI, DW, Address, CS))
656701
break;
657702

658703
if (isGFX11() &&
659-
tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
660-
Address, CS))
704+
tryDecodeInst(this, DecoderTableGFX1132, DecoderTableGFX11_FAKE1632,
705+
MI, DW, Address, CS))
661706
break;
662707

663708
if (isGFX12() &&
664-
tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
665-
Address, CS))
709+
tryDecodeInst(this, DecoderTableGFX1232, DecoderTableGFX12_FAKE1632,
710+
MI, DW, Address, CS))
666711
break;
667712
}
668713

llvm/lib/Target/AMDGPU/Disassembler/AMDGPUDisassembler.h

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -128,44 +128,6 @@ class AMDGPUDisassembler : public MCDisassembler {
128128

129129
MCOperand errOperand(unsigned V, const Twine& ErrMsg) const;
130130

131-
template <typename InsnType>
132-
DecodeStatus tryDecodeInst(const uint8_t *Table, MCInst &MI, InsnType Inst,
133-
uint64_t Address, raw_ostream &Comments) const {
134-
assert(MI.getOpcode() == 0);
135-
assert(MI.getNumOperands() == 0);
136-
MCInst TmpInst;
137-
HasLiteral = false;
138-
const auto SavedBytes = Bytes;
139-
140-
SmallString<64> LocalComments;
141-
raw_svector_ostream LocalCommentStream(LocalComments);
142-
CommentStream = &LocalCommentStream;
143-
144-
DecodeStatus Res =
145-
decodeInstruction(Table, TmpInst, Inst, Address, this, STI);
146-
147-
CommentStream = nullptr;
148-
149-
if (Res != Fail) {
150-
MI = TmpInst;
151-
Comments << LocalComments;
152-
return MCDisassembler::Success;
153-
}
154-
Bytes = SavedBytes;
155-
return MCDisassembler::Fail;
156-
}
157-
158-
template <typename InsnType>
159-
DecodeStatus tryDecodeInst(const uint8_t *Table1, const uint8_t *Table2,
160-
MCInst &MI, InsnType Inst, uint64_t Address,
161-
raw_ostream &Comments) const {
162-
for (const uint8_t *T : {Table1, Table2}) {
163-
if (DecodeStatus Res = tryDecodeInst(T, MI, Inst, Address, Comments))
164-
return Res;
165-
}
166-
return MCDisassembler::Fail;
167-
}
168-
169131
Expected<bool> onSymbolStart(SymbolInfoTy &Symbol, uint64_t &Size,
170132
ArrayRef<uint8_t> Bytes,
171133
uint64_t Address) const override;
@@ -294,6 +256,10 @@ class AMDGPUDisassembler : public MCDisassembler {
294256
bool hasKernargPreload() const;
295257

296258
bool isMacDPP(MCInst &MI) const;
259+
260+
void setHasLiteral(bool Val) const { HasLiteral = Val; }
261+
void setBytes(ArrayRef<uint8_t> Val) const { Bytes = Val; }
262+
ArrayRef<uint8_t> getBytes() const { return Bytes; }
297263
};
298264

299265
//===----------------------------------------------------------------------===//

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,7 +2417,7 @@ void DecoderEmitter::run(raw_ostream &o) {
24172417
#include "llvm/TargetParser/SubtargetFeature.h"
24182418
#include <assert.h>
24192419
2420-
namespace llvm {
2420+
namespace {
24212421
)";
24222422

24232423
emitFieldFromInstruction(OS);
@@ -2561,7 +2561,7 @@ namespace llvm {
25612561
// Emit the main entry point for the decoder, decodeInstruction().
25622562
emitDecodeInstruction(OS, IsVarLenInst);
25632563

2564-
OS << "\n} // end namespace llvm\n";
2564+
OS << "\n} // end anonymous namespace\n";
25652565
}
25662566

25672567
void llvm::EmitDecoder(const RecordKeeper &RK, raw_ostream &OS,

0 commit comments

Comments
 (0)