54#define DEBUG_TYPE "amdgpu-lower-vgpr-encoding"
58class AMDGPULowerVGPREncoding {
59 static constexpr unsigned OpNum = 4;
60 static constexpr unsigned BitsPerField = 2;
61 static constexpr unsigned NumFields = 4;
62 static constexpr unsigned ModeWidth = NumFields * BitsPerField;
63 static constexpr unsigned ModeMask = (1 << ModeWidth) - 1;
64 static constexpr unsigned VGPRMSBShift =
69 std::optional<unsigned> MSBits;
71 bool update(
const OpMode &New,
bool &Rewritten) {
74 if (*New.MSBits != MSBits.value_or(0)) {
76 Rewritten |= MSBits.has_value();
87 bool update(
const ModeTy &New,
bool &Rewritten) {
89 for (
unsigned I :
seq(OpNum))
90 Updated |=
Ops[
I].update(New.Ops[
I], Rewritten);
98 V |=
Op.MSBits.value_or(0) << (
I * 2);
103 static const char *FieldNames[] = {
"src0",
"src1",
"src2",
"dst"};
108 OS << FieldNames[
I] <<
'=';
119 bool isCompatible(
const ModeTy NewMode)
const {
120 for (
unsigned I :
seq(OpNum)) {
121 if (!NewMode.Ops[
I].MSBits.has_value())
123 if (
Ops[
I].MSBits.value_or(0) != NewMode.Ops[
I].MSBits.value_or(0))
150 unsigned ClauseRemaining;
153 unsigned ClauseBreaks;
161 bool NeedNopBeforeSetVGPRMSB;
169 for (OpMode &
Op :
Mode.Ops)
185 const AMDGPU::OpName
Ops[OpNum],
186 const AMDGPU::OpName *Ops2 =
nullptr);
211bool AMDGPULowerVGPREncoding::setMode(ModeTy NewMode,
214 dbgs() <<
" setMode: NewMode=";
215 NewMode.print(
dbgs());
216 dbgs() <<
" CurrentMode=";
217 CurrentMode.print(
dbgs());
218 dbgs() <<
" MostRecentModeSet=" << (MostRecentModeSet ?
"yes" :
"null");
219 if (
I !=
MBB->instr_end())
220 dbgs() <<
" before: " << *
I;
222 dbgs() <<
" at end\n";
226 int64_t OldModeBits = CurrentMode.encode() << ModeWidth;
228 bool Rewritten =
false;
229 if (!CurrentMode.update(NewMode, Rewritten)) {
234 LLVM_DEBUG(
dbgs() <<
" Rewritten=" << Rewritten <<
" after update\n");
236 if (MostRecentModeSet && !Rewritten) {
239 if (MostRecentModeSet->
getOpcode() == AMDGPU::S_SET_VGPR_MSB) {
242 int64_t OldModeBits =
Op.getImm() & (ModeMask << ModeWidth);
243 Op.setImm(CurrentMode.encode() | OldModeBits);
245 << *MostRecentModeSet);
248 "unexpected MostRecentModeSet opcode");
249 updateSetregModeImm(*MostRecentModeSet, CurrentMode.encode());
251 << *MostRecentModeSet);
258 I = handleCoissue(
I);
263 if (NeedNopBeforeSetVGPRMSB) {
265 NeedNopBeforeSetVGPRMSB =
false;
267 MostRecentModeSet =
BuildMI(*
MBB,
I, {},
TII->get(AMDGPU::S_SET_VGPR_MSB))
268 .
addImm(NewMode.encode() | OldModeBits);
270 << *MostRecentModeSet);
272 CurrentMode = NewMode;
276std::optional<unsigned>
283 if (!RC || !
TRI->isVGPRClass(RC))
286 unsigned Idx =
TRI->getHWRegIndex(
Reg);
290void AMDGPULowerVGPREncoding::computeMode(ModeTy &NewMode,
292 const AMDGPU::OpName
Ops[OpNum],
293 const AMDGPU::OpName *Ops2) {
296 for (
unsigned I = 0;
I < OpNum; ++
I) {
299 std::optional<unsigned> MSBits;
301 MSBits = getMSBs(*
Op);
304 if (MSBits.has_value() && Ops2) {
307 std::optional<unsigned> MSBits2;
308 MSBits2 = getMSBs(*Op2);
309 if (MSBits2.has_value() && MSBits != MSBits2)
315 if (!MSBits.has_value() && Ops2) {
316 Op =
TII->getNamedOperand(
MI, Ops2[
I]);
318 MSBits = getMSBs(*
Op);
321 if (!MSBits.has_value())
327 if (
Ops[
I] == AMDGPU::OpName::src2 && !
Op->isDef() &&
Op->isTied() &&
330 TII->hasVALU32BitEncoding(
MI.getOpcode()))))
333 NewMode.Ops[
I].MSBits = MSBits.value();
337bool AMDGPULowerVGPREncoding::runOnMachineInstr(
MachineInstr &
MI) {
341 computeMode(NewMode,
MI,
Ops.first,
Ops.second);
343 dbgs() <<
" runOnMachineInstr: ";
345 dbgs() <<
" computed NewMode=";
346 NewMode.print(
dbgs());
347 dbgs() <<
" compatible=" << CurrentMode.isCompatible(NewMode) <<
'\n';
349 if (!CurrentMode.isCompatible(NewMode) &&
MI.isCommutable() &&
350 TII->commuteInstruction(
MI)) {
351 ModeTy NewModeCommuted;
352 computeMode(NewModeCommuted,
MI,
Ops.first,
Ops.second);
354 dbgs() <<
" commuted NewMode=";
355 NewModeCommuted.print(
dbgs());
356 dbgs() <<
" compatible=" << CurrentMode.isCompatible(NewModeCommuted)
359 if (CurrentMode.isCompatible(NewModeCommuted)) {
364 CurrentMode.update(NewModeCommuted, Unused);
369 if (!
TII->commuteInstruction(
MI))
372 return setMode(NewMode,
MI.getIterator());
374 assert(!
TII->hasVGPRUses(
MI) ||
MI.isMetaInstruction() ||
MI.isPseudo());
381 if (!ClauseRemaining)
386 if (ClauseRemaining == ClauseLen) {
387 I =
Clause->getPrevNode()->getIterator();
395 Clause->eraseFromBundle();
405 Clause->getOperand(0).setImm(ClauseLen | (ClauseBreaks << 8));
421 unsigned Opc =
MI->getOpcode();
423 Opc == AMDGPU::S_DELAY_ALU;
426 while (!
I.isEnd() &&
I !=
I->getParent()->begin()) {
427 auto Prev = std::prev(
I);
428 if (!isProgramStateInstr(&*Prev))
440static int64_t convertModeToSetregFormat(int64_t
Mode) {
445bool AMDGPULowerVGPREncoding::updateSetregModeImm(
MachineInstr &
MI,
447 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32);
450 int64_t SetregMode = convertModeToSetregFormat(ModeValue);
453 int64_t OldImm = ImmOp->
getImm();
455 (OldImm &
~AMDGPU::Hwreg::VGPR_MSB_MASK) | (SetregMode << VGPRMSBShift);
457 return NewImm != OldImm;
463 assert(
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
464 "only S_SETREG_IMM32_B32 needs to be handled");
469 assert(SIMM16Op &&
"SIMM16Op must be present");
474 <<
" Size=" <<
Size <<
'\n');
475 if (HwRegId != ID_MODE) {
480 int64_t ModeValue = CurrentMode.encode();
482 dbgs() <<
" CurrentMode=";
483 CurrentMode.print(
dbgs());
485 <<
" VGPRMSBShift=" << VGPRMSBShift <<
'\n';
491 if (
Size <= VGPRMSBShift) {
494 <<
"), treating as mode scope boundary\n");
498 MostRecentModeSet = &
MI;
500 bool Changed = updateSetregModeImm(
MI, 0);
501 LLVM_DEBUG(
dbgs() <<
" -> reset CurrentMode, cleared bits[12:19]: "
511 assert(ImmOp &&
"ImmOp must be present");
512 int64_t ImmBits12To19 = (ImmOp->
getImm() & VGPR_MSB_MASK) >> VGPRMSBShift;
513 int64_t SetregModeValue = convertModeToSetregFormat(ModeValue);
516 <<
" SetregModeValue=0x"
518 if (ImmBits12To19 == SetregModeValue) {
523 MostRecentModeSet =
nullptr;
524 NeedNopBeforeSetVGPRMSB =
true;
526 "invalidated MostRecentModeSet\n");
536 MostRecentModeSet =
BuildMI(*
MBB, InsertPt,
MI.getDebugLoc(),
537 TII->get(AMDGPU::S_SET_VGPR_MSB))
540 << *MostRecentModeSet);
546 if (!ST.has1024AddressableVGPRs())
549 TII = ST.getInstrInfo();
550 TRI = ST.getRegisterInfo();
556 ClauseLen = ClauseRemaining = 0;
558 for (
auto &
MBB : MF) {
559 MostRecentModeSet =
nullptr;
560 NeedNopBeforeSetVGPRMSB =
false;
567 if (
MI.isMetaInstruction())
570 if (
MI.isTerminator() ||
MI.isCall()) {
572 if (
MI.getOpcode() == AMDGPU::S_ENDPGM ||
573 MI.getOpcode() == AMDGPU::S_ENDPGM_SAVED)
576 resetMode(
MI.getIterator());
577 NeedNopBeforeSetVGPRMSB =
false;
581 if (
MI.isInlineAsm()) {
583 if (
TII->hasVGPRUses(
MI))
584 resetMode(
MI.getIterator());
585 NeedNopBeforeSetVGPRMSB =
false;
589 if (
MI.getOpcode() == AMDGPU::S_CLAUSE) {
590 assert(!ClauseRemaining &&
"Nested clauses are not supported");
591 ClauseLen =
MI.getOperand(0).getImm();
592 ClauseBreaks = (ClauseLen >> 8) & 15;
593 ClauseLen = ClauseRemaining = (ClauseLen & 63) + 1;
596 <<
" breaks=" << ClauseBreaks <<
'\n');
600 if (
MI.getOpcode() == AMDGPU::S_SETREG_IMM32_B32 &&
601 ST.hasSetregVGPRMSBFixup()) {
607 NeedNopBeforeSetVGPRMSB =
false;
615 resetMode(
MBB.instr_end());
628 return AMDGPULowerVGPREncoding().run(MF);
639char AMDGPULowerVGPREncodingLegacy::ID = 0;
644 "AMDGPU Lower VGPR Encoding",
false,
false)
649 if (!AMDGPULowerVGPREncoding().run(MF))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
Interface definition for SIInstrInfo.
This file implements the C++20 <bit> header.
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Represents analyses that only rely on functions' control flow.
Wrapper class representing physical registers. Should be passed by value.
Instructions::iterator instr_iterator
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
static bool isVOP2(const MachineInstr &MI)
static bool isVOP3(const MCInstrDesc &Desc)
static Twine utohexstr(uint64_t Val)
This class implements an extremely fast bulk output stream that can only output to a stream.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::pair< const AMDGPU::OpName *, const AMDGPU::OpName * > getVGPRLoweringOperandTables(const MCInstrDesc &Desc)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
This is an optimization pass for GlobalISel generic memory operations.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
unsigned encode(MaybeAlign A)
Returns a representation of the alignment that encodes undefined as 0.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
DWARFExpression::Operation Op
constexpr int countr_zero_constexpr(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
char & AMDGPULowerVGPREncodingLegacyID
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
constexpr T rotl(T V, int R)