LLVM  14.0.0git
SIInsertHardClauses.cpp
Go to the documentation of this file.
1 //===- SIInsertHardClauses.cpp - Insert Hard Clauses ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// Insert s_clause instructions to form hard clauses.
11 ///
12 /// Clausing load instructions can give cache coherency benefits. Before gfx10,
13 /// the hardware automatically detected "soft clauses", which were sequences of
14 /// memory instructions of the same type. In gfx10 this detection was removed,
15 /// and the s_clause instruction was introduced to explicitly mark "hard
16 /// clauses".
17 ///
18 /// It's the scheduler's job to form the clauses by putting similar memory
19 /// instructions next to each other. Our job is just to insert an s_clause
20 /// instruction to mark the start of each clause.
21 ///
22 /// Note that hard clauses are very similar to, but logically distinct from, the
23 /// groups of instructions that have to be restartable when XNACK is enabled.
24 /// The rules are slightly different in each case. For example an s_nop
25 /// instruction breaks a restartable group, but can appear in the middle of a
26 /// hard clause. (Before gfx10 there wasn't a distinction, and both were called
27 /// "soft clauses" or just "clauses".)
28 ///
29 /// The SIFormMemoryClauses pass and GCNHazardRecognizer deal with restartable
30 /// groups, not hard clauses.
31 //
32 //===----------------------------------------------------------------------===//
33 
34 #include "AMDGPU.h"
35 #include "GCNSubtarget.h"
37 #include "llvm/ADT/SmallVector.h"
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "si-insert-hard-clauses"
42 
43 namespace {
44 
45 enum HardClauseType {
46  // Texture, buffer, global or scratch memory instructions.
47  HARDCLAUSE_VMEM,
48  // Flat (not global or scratch) memory instructions.
49  HARDCLAUSE_FLAT,
50  // Instructions that access LDS.
51  HARDCLAUSE_LDS,
52  // Scalar memory instructions.
53  HARDCLAUSE_SMEM,
54  // VALU instructions.
55  HARDCLAUSE_VALU,
56  LAST_REAL_HARDCLAUSE_TYPE = HARDCLAUSE_VALU,
57 
58  // Internal instructions, which are allowed in the middle of a hard clause,
59  // except for s_waitcnt.
60  HARDCLAUSE_INTERNAL,
61  // Meta instructions that do not result in any ISA like KILL.
62  HARDCLAUSE_IGNORE,
63  // Instructions that are not allowed in a hard clause: SALU, export, branch,
64  // message, GDS, s_waitcnt and anything else not mentioned above.
65  HARDCLAUSE_ILLEGAL,
66 };
67 
68 class SIInsertHardClauses : public MachineFunctionPass {
69 public:
70  static char ID;
71  const GCNSubtarget *ST = nullptr;
72 
73  SIInsertHardClauses() : MachineFunctionPass(ID) {}
74 
75  void getAnalysisUsage(AnalysisUsage &AU) const override {
76  AU.setPreservesCFG();
78  }
79 
80  HardClauseType getHardClauseType(const MachineInstr &MI) {
81 
82  // On current architectures we only get a benefit from clausing loads.
83  if (MI.mayLoad()) {
85  if (ST->hasNSAClauseBug()) {
86  const AMDGPU::MIMGInfo *Info = AMDGPU::getMIMGInfo(MI.getOpcode());
87  if (Info && Info->MIMGEncoding == AMDGPU::MIMGEncGfx10NSA)
88  return HARDCLAUSE_ILLEGAL;
89  }
90  return HARDCLAUSE_VMEM;
91  }
93  return HARDCLAUSE_FLAT;
94  // TODO: LDS
96  return HARDCLAUSE_SMEM;
97  }
98 
99  // Don't form VALU clauses. It's not clear what benefit they give, if any.
100 
101  // In practice s_nop is the only internal instruction we're likely to see.
102  // It's safe to treat the rest as illegal.
103  if (MI.getOpcode() == AMDGPU::S_NOP)
104  return HARDCLAUSE_INTERNAL;
105  if (MI.isMetaInstruction())
106  return HARDCLAUSE_IGNORE;
107  return HARDCLAUSE_ILLEGAL;
108  }
109 
110  // Track information about a clause as we discover it.
111  struct ClauseInfo {
112  // The type of all (non-internal) instructions in the clause.
113  HardClauseType Type = HARDCLAUSE_ILLEGAL;
114  // The first (necessarily non-internal) instruction in the clause.
115  MachineInstr *First = nullptr;
116  // The last non-internal instruction in the clause.
117  MachineInstr *Last = nullptr;
118  // The length of the clause including any internal instructions in the
119  // middle (but not at the end) of the clause.
120  unsigned Length = 0;
121  // Internal instructions at the and of a clause should not be included in
122  // the clause. Count them in TrailingInternalLength until a new memory
123  // instruction is added.
124  unsigned TrailingInternalLength = 0;
125  // The base operands of *Last.
127  };
128 
129  bool emitClause(const ClauseInfo &CI, const SIInstrInfo *SII) {
130  if (CI.First == CI.Last)
131  return false;
132  assert(CI.Length <= 64 && "Hard clause is too long!");
133 
134  auto &MBB = *CI.First->getParent();
135  auto ClauseMI =
136  BuildMI(MBB, *CI.First, DebugLoc(), SII->get(AMDGPU::S_CLAUSE))
137  .addImm(CI.Length - 1);
138  finalizeBundle(MBB, ClauseMI->getIterator(),
139  std::next(CI.Last->getIterator()));
140  return true;
141  }
142 
143  bool runOnMachineFunction(MachineFunction &MF) override {
144  if (skipFunction(MF.getFunction()))
145  return false;
146 
147  ST = &MF.getSubtarget<GCNSubtarget>();
148  if (!ST->hasHardClauses())
149  return false;
150 
151  const SIInstrInfo *SII = ST->getInstrInfo();
152  const TargetRegisterInfo *TRI = ST->getRegisterInfo();
153 
154  bool Changed = false;
155  for (auto &MBB : MF) {
156  ClauseInfo CI;
157  for (auto &MI : MBB) {
158  HardClauseType Type = getHardClauseType(MI);
159 
160  int64_t Dummy1;
161  bool Dummy2;
162  unsigned Dummy3;
164  if (Type <= LAST_REAL_HARDCLAUSE_TYPE) {
165  if (!SII->getMemOperandsWithOffsetWidth(MI, BaseOps, Dummy1, Dummy2,
166  Dummy3, TRI)) {
167  // We failed to get the base operands, so we'll never clause this
168  // instruction with any other, so pretend it's illegal.
169  Type = HARDCLAUSE_ILLEGAL;
170  }
171  }
172 
173  if (CI.Length == 64 ||
174  (CI.Length && Type != HARDCLAUSE_INTERNAL &&
175  Type != HARDCLAUSE_IGNORE &&
176  (Type != CI.Type ||
177  // Note that we lie to shouldClusterMemOps about the size of the
178  // cluster. When shouldClusterMemOps is called from the machine
179  // scheduler it limits the size of the cluster to avoid increasing
180  // register pressure too much, but this pass runs after register
181  // allocation so there is no need for that kind of limit.
182  !SII->shouldClusterMemOps(CI.BaseOps, BaseOps, 2, 2)))) {
183  // Finish the current clause.
184  Changed |= emitClause(CI, SII);
185  CI = ClauseInfo();
186  }
187 
188  if (CI.Length) {
189  // Extend the current clause.
190  if (Type != HARDCLAUSE_IGNORE) {
191  if (Type == HARDCLAUSE_INTERNAL) {
192  ++CI.TrailingInternalLength;
193  } else {
194  ++CI.Length;
195  CI.Length += CI.TrailingInternalLength;
196  CI.TrailingInternalLength = 0;
197  CI.Last = &MI;
198  CI.BaseOps = std::move(BaseOps);
199  }
200  }
201  } else if (Type <= LAST_REAL_HARDCLAUSE_TYPE) {
202  // Start a new clause.
203  CI = ClauseInfo{Type, &MI, &MI, 1, 0, std::move(BaseOps)};
204  }
205  }
206 
207  // Finish the last clause in the basic block if any.
208  if (CI.Length)
209  Changed |= emitClause(CI, SII);
210  }
211 
212  return Changed;
213  }
214 };
215 
216 } // namespace
217 
218 char SIInsertHardClauses::ID = 0;
219 
221 
222 INITIALIZE_PASS(SIInsertHardClauses, DEBUG_TYPE, "SI Insert Hard Clauses",
223  false, false)
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:105
llvm::MachineInstrBuilder::addImm
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Definition: MachineInstrBuilder.h:131
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1168
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::GCNSubtarget
Definition: GCNSubtarget.h:31
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition: MachineFunctionPass.cpp:102
llvm::finalizeBundle
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...
Definition: MachineInstrBundle.cpp:123
GCNSubtarget.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:630
DEBUG_TYPE
#define DEBUG_TYPE
Definition: SIInsertHardClauses.cpp:41
AMDGPUMCTargetDesc.h
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::ARM_MB::ST
@ ST
Definition: ARMBaseInfo.h:73
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::SIInstrInfo::isSMRD
static bool isSMRD(const MachineInstr &MI)
Definition: SIInstrInfo.h:479
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::SIInsertHardClausesID
char & SIInsertHardClausesID
Definition: SIInsertHardClauses.cpp:220
llvm::AMDGPU::getMIMGInfo
const LLVM_READONLY MIMGInfo * getMIMGInfo(unsigned Opc)
llvm::MachineFunction
Definition: MachineFunction.h:234
llvm::SIInstrInfo::shouldClusterMemOps
bool shouldClusterMemOps(ArrayRef< const MachineOperand * > BaseOps1, ArrayRef< const MachineOperand * > BaseOps2, unsigned NumLoads, unsigned NumBytes) const override
Definition: SIInstrInfo.cpp:432
llvm::AnalysisUsage::setPreservesCFG
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:253
AMDGPU.h
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition: ilist_node.h:81
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::SIInstrInfo::getMemOperandsWithOffsetWidth
bool getMemOperandsWithOffsetWidth(const MachineInstr &LdSt, SmallVectorImpl< const MachineOperand * > &BaseOps, int64_t &Offset, bool &OffsetIsScalable, unsigned &Width, const TargetRegisterInfo *TRI) const final
Definition: SIInstrInfo.cpp:249
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:596
llvm::SIInstrInfo::isSegmentSpecificFLAT
static bool isSegmentSpecificFLAT(const MachineInstr &MI)
Definition: SIInstrInfo.h:521
llvm::SIInstrInfo::isVMEM
static bool isVMEM(const MachineInstr &MI)
Definition: SIInstrInfo.h:367
llvm::AMDGPU::MIMGInfo
Definition: AMDGPUBaseInfo.h:359
llvm::SIInstrInfo
Definition: SIInstrInfo.h:38
SmallVector.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::SIInstrInfo::isFLAT
static bool isFLAT(const MachineInstr &MI)
Definition: SIInstrInfo.h:515
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37