Skip to content

Commit 9f8d4f8

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 9f8d4f8

File tree

3 files changed

+82
-72
lines changed

3 files changed

+82
-72
lines changed

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

Lines changed: 76 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 =
505+
decodeInstruction(Table, TmpInst, Inst, Address, this, STI);
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(Asm, 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(Asm, DecoderTableGFX1296, DecoderTableGFX12_FAKE1696,
587+
MI, DecW, Address, CS))
548588
break;
549589

550590
if (isGFX12() &&
551-
tryDecodeInst(DecoderTableGFX12W6496, MI, DecW, Address, CS))
591+
tryDecodeInst(Asm, 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(Asm, DecoderTableGFX940128, MI, DecW, Address, CS))
561601
break;
562602

563603
// Reinitialize Bytes
@@ -568,58 +608,60 @@ 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(Asm, 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(Asm, DecoderTableGFX80_UNPACKED64, MI, QW, Address, CS))
576616
break;
577617

578618
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
579-
tryDecodeInst(DecoderTableGFX95064, MI, QW, Address, CS))
619+
tryDecodeInst(Asm, DecoderTableGFX95064, MI, QW, Address, CS))
580620
break;
581621

582622
// Some GFX9 subtargets repurposed the v_mad_mix_f32, v_mad_mixlo_f16 and
583623
// v_mad_mixhi_f16 for FMA variants. Try to decode using this special
584624
// table first so we print the correct name.
585625
if (STI.hasFeature(AMDGPU::FeatureFmaMixInsts) &&
586-
tryDecodeInst(DecoderTableGFX9_DL64, MI, QW, Address, CS))
626+
tryDecodeInst(Asm, DecoderTableGFX9_DL64, MI, QW, Address, CS))
587627
break;
588628

589629
if (STI.hasFeature(AMDGPU::FeatureGFX940Insts) &&
590-
tryDecodeInst(DecoderTableGFX94064, MI, QW, Address, CS))
630+
tryDecodeInst(Asm, DecoderTableGFX94064, MI, QW, Address, CS))
591631
break;
592632

593633
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
594-
tryDecodeInst(DecoderTableGFX90A64, MI, QW, Address, CS))
634+
tryDecodeInst(Asm, DecoderTableGFX90A64, MI, QW, Address, CS))
595635
break;
596636

597637
if ((isVI() || isGFX9()) &&
598-
tryDecodeInst(DecoderTableGFX864, MI, QW, Address, CS))
638+
tryDecodeInst(Asm, DecoderTableGFX864, MI, QW, Address, CS))
599639
break;
600640

601-
if (isGFX9() && tryDecodeInst(DecoderTableGFX964, MI, QW, Address, CS))
641+
if (isGFX9() &&
642+
tryDecodeInst(Asm, DecoderTableGFX964, MI, QW, Address, CS))
602643
break;
603644

604-
if (isGFX10() && tryDecodeInst(DecoderTableGFX1064, MI, QW, Address, CS))
645+
if (isGFX10() &&
646+
tryDecodeInst(Asm, DecoderTableGFX1064, MI, QW, Address, CS))
605647
break;
606648

607649
if (isGFX12() &&
608-
tryDecodeInst(DecoderTableGFX1264, DecoderTableGFX12_FAKE1664, MI, QW,
609-
Address, CS))
650+
tryDecodeInst(Asm, DecoderTableGFX1264, DecoderTableGFX12_FAKE1664,
651+
MI, QW, Address, CS))
610652
break;
611653

612654
if (isGFX11() &&
613-
tryDecodeInst(DecoderTableGFX1164, DecoderTableGFX11_FAKE1664, MI, QW,
614-
Address, CS))
655+
tryDecodeInst(Asm, DecoderTableGFX1164, DecoderTableGFX11_FAKE1664,
656+
MI, QW, Address, CS))
615657
break;
616658

617659
if (isGFX11() &&
618-
tryDecodeInst(DecoderTableGFX11W6464, MI, QW, Address, CS))
660+
tryDecodeInst(Asm, DecoderTableGFX11W6464, MI, QW, Address, CS))
619661
break;
620662

621663
if (isGFX12() &&
622-
tryDecodeInst(DecoderTableGFX12W6464, MI, QW, Address, CS))
664+
tryDecodeInst(Asm, DecoderTableGFX12W6464, MI, QW, Address, CS))
623665
break;
624666

625667
// Reinitialize Bytes
@@ -631,38 +673,40 @@ DecodeStatus AMDGPUDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
631673
const uint32_t DW = eatBytes<uint32_t>(Bytes);
632674

633675
if ((isVI() || isGFX9()) &&
634-
tryDecodeInst(DecoderTableGFX832, MI, DW, Address, CS))
676+
tryDecodeInst(Asm, DecoderTableGFX832, MI, DW, Address, CS))
635677
break;
636678

637-
if (tryDecodeInst(DecoderTableAMDGPU32, MI, DW, Address, CS))
679+
if (tryDecodeInst(Asm, DecoderTableAMDGPU32, MI, DW, Address, CS))
638680
break;
639681

640-
if (isGFX9() && tryDecodeInst(DecoderTableGFX932, MI, DW, Address, CS))
682+
if (isGFX9() &&
683+
tryDecodeInst(Asm, DecoderTableGFX932, MI, DW, Address, CS))
641684
break;
642685

643686
if (STI.hasFeature(AMDGPU::FeatureGFX950Insts) &&
644-
tryDecodeInst(DecoderTableGFX95032, MI, DW, Address, CS))
687+
tryDecodeInst(Asm, DecoderTableGFX95032, MI, DW, Address, CS))
645688
break;
646689

647690
if (STI.hasFeature(AMDGPU::FeatureGFX90AInsts) &&
648-
tryDecodeInst(DecoderTableGFX90A32, MI, DW, Address, CS))
691+
tryDecodeInst(Asm, DecoderTableGFX90A32, MI, DW, Address, CS))
649692
break;
650693

651694
if (STI.hasFeature(AMDGPU::FeatureGFX10_BEncoding) &&
652-
tryDecodeInst(DecoderTableGFX10_B32, MI, DW, Address, CS))
695+
tryDecodeInst(Asm, DecoderTableGFX10_B32, MI, DW, Address, CS))
653696
break;
654697

655-
if (isGFX10() && tryDecodeInst(DecoderTableGFX1032, MI, DW, Address, CS))
698+
if (isGFX10() &&
699+
tryDecodeInst(Asm, DecoderTableGFX1032, MI, DW, Address, CS))
656700
break;
657701

658702
if (isGFX11() &&
659-
tryDecodeInst(DecoderTableGFX1132, DecoderTableGFX11_FAKE1632, MI, DW,
660-
Address, CS))
703+
tryDecodeInst(Asm, DecoderTableGFX1132, DecoderTableGFX11_FAKE1632,
704+
MI, DW, Address, CS))
661705
break;
662706

663707
if (isGFX12() &&
664-
tryDecodeInst(DecoderTableGFX1232, DecoderTableGFX12_FAKE1632, MI, DW,
665-
Address, CS))
708+
tryDecodeInst(Asm, DecoderTableGFX1232, DecoderTableGFX12_FAKE1632,
709+
MI, DW, Address, CS))
666710
break;
667711
}
668712

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)