42#define DEBUG_TYPE "si-insert-hard-clauses"
46 cl::desc(
"Maximum number of memory instructions to "
47 "place in the same hard clause"),
64 HARDCLAUSE_MIMG_STORE,
65 HARDCLAUSE_MIMG_ATOMIC,
66 HARDCLAUSE_MIMG_SAMPLE,
69 HARDCLAUSE_VMEM_STORE,
70 HARDCLAUSE_VMEM_ATOMIC,
73 HARDCLAUSE_FLAT_STORE,
74 HARDCLAUSE_FLAT_ATOMIC,
86 LAST_REAL_HARDCLAUSE_TYPE = HARDCLAUSE_VALU,
98class SIInsertHardClauses {
100 const GCNSubtarget *ST =
nullptr;
102 HardClauseType getHardClauseType(
const MachineInstr &
MI) {
103 if (
MI.mayLoad() || (
MI.mayStore() && ST->shouldClusterStores())) {
104 if (ST->getGeneration() == AMDGPUSubtarget::GFX10 ||
105 ST->hasGFX1250Insts()) {
108 if (ST->hasNSAClauseBug()) {
110 if (Info &&
Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA)
111 return HARDCLAUSE_ILLEGAL;
113 return HARDCLAUSE_VMEM;
116 return HARDCLAUSE_FLAT;
120 const AMDGPU::MIMGBaseOpcodeInfo *BaseInfo =
123 return HARDCLAUSE_BVH;
125 return HARDCLAUSE_MIMG_SAMPLE;
126 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_MIMG_ATOMIC
127 : HARDCLAUSE_MIMG_LOAD
128 : HARDCLAUSE_MIMG_STORE;
132 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_VMEM_ATOMIC
133 : HARDCLAUSE_VMEM_LOAD
134 : HARDCLAUSE_VMEM_STORE;
137 return MI.mayLoad() ?
MI.mayStore() ? HARDCLAUSE_FLAT_ATOMIC
138 : HARDCLAUSE_FLAT_LOAD
139 : HARDCLAUSE_FLAT_STORE;
144 return HARDCLAUSE_SMEM;
151 if (
MI.getOpcode() == AMDGPU::S_NOP)
152 return HARDCLAUSE_INTERNAL;
153 if (
MI.isMetaInstruction())
154 return HARDCLAUSE_IGNORE;
155 return HARDCLAUSE_ILLEGAL;
161 HardClauseType Type = HARDCLAUSE_ILLEGAL;
163 MachineInstr *First =
nullptr;
165 MachineInstr *Last =
nullptr;
172 unsigned TrailingInternalLength = 0;
177 bool emitClause(
const ClauseInfo &CI,
const SIInstrInfo *SII) {
178 if (CI.First == CI.Last)
180 assert(CI.Length <= ST->maxHardClauseLength() &&
181 "Hard clause is too long!");
183 auto &
MBB = *CI.First->getParent();
188 std::next(CI.Last->getIterator()));
193 bool isIncompatibleScope(
const MachineInstr &MI1,
const MachineInstr &MI2,
194 const SIInstrInfo *SII)
const {
195 assert(ST->getGeneration() == AMDGPUSubtarget::GFX12 &&
196 ST->hasGFX1250Insts());
197 int CPol1 = 0, CPol2 = 0;
198 if (
const MachineOperand *
Op =
202 if (
const MachineOperand *
Op =
206 return CPol1 != CPol2;
211 if (!ST->hasHardClauses())
215 "amdgpu-hard-clause-length-limit", 255);
218 MaxClauseLength = std::min(MaxClauseLength, ST->maxHardClauseLength());
219 if (MaxClauseLength <= 1)
222 const SIInstrInfo *SII = ST->getInstrInfo();
223 const TargetRegisterInfo *
TRI = ST->getRegisterInfo();
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 &&
270 (CI.Length && ST->hasGFX1250_STRICT() &&
271 isIncompatibleScope(
MI, *CI.Last, SII))) {
273 Changed |= emitClause(CI, SII);
279 if (
Type != HARDCLAUSE_IGNORE) {
280 if (
Type == HARDCLAUSE_INTERNAL) {
281 ++CI.TrailingInternalLength;
284 CI.Length += CI.TrailingInternalLength;
285 CI.TrailingInternalLength = 0;
287 CI.BaseOps = std::move(BaseOps);
290 }
else if (
Type <= LAST_REAL_HARDCLAUSE_TYPE) {
292 CI = ClauseInfo{
Type, &
MI, &
MI, 1, 0, std::move(BaseOps)};
298 Changed |= emitClause(CI, SII);
308 SIInsertHardClausesLegacy() : MachineFunctionPass(ID) {}
314 return SIInsertHardClauses().run(MF);
317 void getAnalysisUsage(AnalysisUsage &AU)
const override {
328 if (!SIInsertHardClauses().
run(MF))
336char SIInsertHardClausesLegacy::ID = 0;
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMD GCN specific subclass of TargetSubtarget.
Register const TargetRegisterInfo * TRI
#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)
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, LocationSize &Width, const TargetRegisterInfo *TRI) const final
static bool isSMRD(const MachineInstr &MI)
bool shouldClusterMemOps(ArrayRef< const MachineOperand * > BaseOps1, int64_t Offset1, bool OffsetIsScalable1, ArrayRef< const MachineOperand * > BaseOps2, int64_t Offset2, bool OffsetIsScalable2, unsigned ClusterSize, unsigned NumBytes) const override
static bool isSegmentSpecificFLAT(const MachineInstr &MI)
static bool isMIMG(const MachineInstr &MI)
static bool isFLAT(const MachineInstr &MI)
LLVM_READONLY MachineOperand * getNamedOperand(MachineInstr &MI, AMDGPU::OpName OperandName) const
Returns the operand named Op.
self_iterator getIterator()
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
DXILDebugInfoMap run(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