42#define DEBUG_TYPE "si-insert-hard-clauses"
46 cl::desc(
"Maximum number of memory instructions to "
47 "place in the same hard clause"),
66 HARDCLAUSE_MIMG_STORE,
67 HARDCLAUSE_MIMG_ATOMIC,
68 HARDCLAUSE_MIMG_SAMPLE,
71 HARDCLAUSE_VMEM_STORE,
72 HARDCLAUSE_VMEM_ATOMIC,
75 HARDCLAUSE_FLAT_STORE,
76 HARDCLAUSE_FLAT_ATOMIC,
88 LAST_REAL_HARDCLAUSE_TYPE = HARDCLAUSE_VALU,
100class SIInsertHardClauses {
102 const GCNSubtarget *ST =
nullptr;
104 HardClauseType getHardClauseType(
const MachineInstr &
MI) {
105 if (
MI.mayLoad() || (
MI.mayStore() && ST->shouldClusterStores())) {
106 if (ST->hasMixedLoadStoreClauses()) {
109 if (ST->hasNSAClauseBug()) {
111 if (Info &&
Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA)
112 return HARDCLAUSE_ILLEGAL;
114 return HARDCLAUSE_VMEM;
117 return HARDCLAUSE_FLAT;
121 const AMDGPU::MIMGBaseOpcodeInfo *BaseInfo =
124 return HARDCLAUSE_BVH;
126 return HARDCLAUSE_MIMG_SAMPLE;
127 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_MIMG_ATOMIC
128 : HARDCLAUSE_MIMG_LOAD
129 : HARDCLAUSE_MIMG_STORE;
133 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_VMEM_ATOMIC
134 : HARDCLAUSE_VMEM_LOAD
135 : HARDCLAUSE_VMEM_STORE;
138 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_FLAT_ATOMIC
139 : HARDCLAUSE_FLAT_LOAD
140 : HARDCLAUSE_FLAT_STORE;
145 return HARDCLAUSE_SMEM;
152 if (
MI.getOpcode() == AMDGPU::S_NOP)
153 return HARDCLAUSE_INTERNAL;
154 if (
MI.isMetaInstruction())
155 return HARDCLAUSE_IGNORE;
156 return HARDCLAUSE_ILLEGAL;
162 HardClauseType Type = HARDCLAUSE_ILLEGAL;
164 MachineInstr *First =
nullptr;
166 MachineInstr *Last =
nullptr;
173 unsigned TrailingInternalLength = 0;
178 bool emitClause(
const ClauseInfo &CI,
const SIInstrInfo *SII) {
179 if (CI.First == CI.Last)
181 assert(CI.Length <= ST->maxHardClauseLength() &&
182 "Hard clause is too long!");
184 auto &
MBB = *CI.First->getParent();
189 std::next(CI.Last->getIterator()));
194 bool isIncompatibleScope(
const MachineInstr &MI1,
const MachineInstr &MI2,
195 const SIInstrInfo *SII)
const {
196 assert(ST->getGeneration() == AMDGPUSubtarget::GFX12 &&
197 ST->hasGFX1250Insts());
198 int CPol1 = 0, CPol2 = 0;
199 if (
const MachineOperand *
Op =
203 if (
const MachineOperand *
Op =
207 return CPol1 != CPol2;
212 if (!ST->hasHardClauses())
216 "amdgpu-hard-clause-length-limit", 255);
219 MaxClauseLength = std::min(MaxClauseLength, ST->maxHardClauseLength());
220 if (MaxClauseLength <= 1)
223 const SIInstrInfo *SII = ST->getInstrInfo();
226 for (
auto &
MBB : MF) {
228 unsigned ExistingClauseRemaining = 0;
229 for (
auto &
MI :
MBB) {
231 if (ExistingClauseRemaining) {
232 if (!
MI.isMetaInstruction())
233 ExistingClauseRemaining--;
234 Type = HARDCLAUSE_ILLEGAL;
235 }
else if (
MI.getOpcode() == AMDGPU::S_CLAUSE) {
238 ExistingClauseRemaining = (
MI.getOperand(0).
getImm() & 63) + 1;
239 Type = HARDCLAUSE_ILLEGAL;
241 Type = getHardClauseType(
MI);
248 if (
Type <= LAST_REAL_HARDCLAUSE_TYPE) {
253 Type = HARDCLAUSE_ILLEGAL;
257 if (CI.Length == MaxClauseLength ||
258 (CI.Length &&
Type != HARDCLAUSE_INTERNAL &&
259 Type != HARDCLAUSE_IGNORE &&
265 (CI.Length && ST->hasGFX1250_STRICT() &&
266 isIncompatibleScope(
MI, *CI.Last, SII))) {
268 Changed |= emitClause(CI, SII);
274 if (
Type != HARDCLAUSE_IGNORE) {
275 if (
Type == HARDCLAUSE_INTERNAL) {
276 ++CI.TrailingInternalLength;
279 CI.Length += CI.TrailingInternalLength;
280 CI.TrailingInternalLength = 0;
282 CI.BaseOps = std::move(BaseOps);
285 }
else if (
Type <= LAST_REAL_HARDCLAUSE_TYPE) {
287 CI = ClauseInfo{
Type, &
MI, &
MI, 1, 0, std::move(BaseOps)};
293 Changed |= emitClause(CI, SII);
303 SIInsertHardClausesLegacy() : MachineFunctionPass(ID) {}
309 return SIInsertHardClauses().run(MF);
312 void getAnalysisUsage(AnalysisUsage &AU)
const override {
323 if (!SIInsertHardClauses().
run(MF))
331char SIInsertHardClausesLegacy::ID = 0;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMD GCN specific subclass of TargetSubtarget.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
static cl::opt< unsigned > HardClauseLengthLimit("amdgpu-hard-clause-length-limit", cl::desc("Maximum number of memory instructions to " "place in the same hard clause"), cl::Hidden)
This file defines the SmallVector class.
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.
uint64_t getFnAttributeAsParsedInteger(StringRef Kind, uint64_t Default=0) const
For a string attribute Kind, parse attribute as an integer.
static LocationSize precise(uint64_t Value)
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.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
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 run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static bool isVMEM(const MachineInstr &MI)
static bool isSMRD(const MachineInstr &MI)
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width) const final
static bool isSegmentSpecificFLAT(const MachineInstr &MI)
static bool isMIMG(const MachineInstr &MI)
bool memOpsHaveSameBase(ArrayRef< const MachineOperand * > BaseOps1, ArrayRef< const MachineOperand * > BaseOps2) const
Return true if the memory operations described by BaseOps1 and BaseOps2 (as returned by getMemOperand...
static bool isFLAT(const MachineInstr &MI)
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, AMDGPU::OpName OperandName) const
Returns the operand named Op.
int getNumOccurrences() const
self_iterator getIterator()
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
DWARFExpression::Operation Op
char & SIInsertHardClausesID