1 //===-- R600EmitClauseMarkers.cpp - Emit CF_ALU ---------------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// Add CF_ALU. R600 Alu instructions are grouped in clause which can hold
11 /// 128 Alu instructions ; these instructions can access up to 4 prefetched
12 /// 4 lines of 16 registers from constant buffers. Such ALU clauses are
13 /// initiated by CF_ALU instructions.
14 //===----------------------------------------------------------------------===//
17 #include "AMDGPUSubtarget.h"
18 #include "R600Defines.h"
19 #include "R600InstrInfo.h"
20 #include "R600RegisterInfo.h"
21 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/CodeGen/MachineBasicBlock.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineFunctionPass.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineOperand.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Support/ErrorHandling.h"
41 void initializeR600EmitClauseMarkersPass(PassRegistry
&);
43 } // end namespace llvm
47 class R600EmitClauseMarkers
: public MachineFunctionPass
{
49 const R600InstrInfo
*TII
= nullptr;
52 unsigned OccupiedDwords(MachineInstr
&MI
) const {
53 switch (MI
.getOpcode()) {
54 case R600::INTERP_PAIR_XY
:
55 case R600::INTERP_PAIR_ZW
:
56 case R600::INTERP_VEC_LOAD
:
65 // These will be expanded to two ALU instructions in the
66 // ExpandSpecialInstructions pass.
67 if (TII
->isLDSRetInstr(MI
.getOpcode()))
70 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()) ||
71 TII
->isReductionOp(MI
.getOpcode()))
74 unsigned NumLiteral
= 0;
75 for (MachineInstr::mop_iterator It
= MI
.operands_begin(),
76 E
= MI
.operands_end();
78 MachineOperand
&MO
= *It
;
79 if (MO
.isReg() && MO
.getReg() == R600::ALU_LITERAL_X
)
82 return 1 + NumLiteral
;
85 bool isALU(const MachineInstr
&MI
) const {
86 if (TII
->isALUInstr(MI
.getOpcode()))
88 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()))
90 switch (MI
.getOpcode()) {
92 case R600::INTERP_PAIR_XY
:
93 case R600::INTERP_PAIR_ZW
:
94 case R600::INTERP_VEC_LOAD
:
103 bool IsTrivialInst(MachineInstr
&MI
) const {
104 switch (MI
.getOpcode()) {
107 case R600::IMPLICIT_DEF
:
114 std::pair
<unsigned, unsigned> getAccessedBankLine(unsigned Sel
) const {
115 // Sel is (512 + (kc_bank << 12) + ConstIndex) << 2
116 // (See also R600ISelLowering.cpp)
117 // ConstIndex value is in [0, 4095];
118 return std::pair
<unsigned, unsigned>(
119 ((Sel
>> 2) - 512) >> 12, // KC_BANK
120 // Line Number of ConstIndex
121 // A line contains 16 constant registers however KCX bank can lock
122 // two line at the same time ; thus we want to get an even line number.
123 // Line number can be retrieved with (>>4), using (>>5) <<1 generates
125 ((((Sel
>> 2) - 512) & 4095) >> 5) << 1);
129 SubstituteKCacheBank(MachineInstr
&MI
,
130 std::vector
<std::pair
<unsigned, unsigned>> &CachedConsts
,
131 bool UpdateInstr
= true) const {
132 std::vector
<std::pair
<unsigned, unsigned>> UsedKCache
;
134 if (!TII
->isALUInstr(MI
.getOpcode()) && MI
.getOpcode() != R600::DOT_4
)
137 const SmallVectorImpl
<std::pair
<MachineOperand
*, int64_t>> &Consts
=
140 (TII
->isALUInstr(MI
.getOpcode()) || MI
.getOpcode() == R600::DOT_4
) &&
141 "Can't assign Const");
142 for (unsigned i
= 0, n
= Consts
.size(); i
< n
; ++i
) {
143 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
145 unsigned Sel
= Consts
[i
].second
;
146 unsigned Chan
= Sel
& 3, Index
= ((Sel
>> 2) - 512) & 31;
147 unsigned KCacheIndex
= Index
* 4 + Chan
;
148 const std::pair
<unsigned, unsigned> &BankLine
= getAccessedBankLine(Sel
);
149 if (CachedConsts
.empty()) {
150 CachedConsts
.push_back(BankLine
);
151 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
154 if (CachedConsts
[0] == BankLine
) {
155 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
158 if (CachedConsts
.size() == 1) {
159 CachedConsts
.push_back(BankLine
);
160 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
163 if (CachedConsts
[1] == BankLine
) {
164 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
173 for (unsigned i
= 0, j
= 0, n
= Consts
.size(); i
< n
; ++i
) {
174 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
176 switch(UsedKCache
[j
].first
) {
178 Consts
[i
].first
->setReg(
179 R600::R600_KC0RegClass
.getRegister(UsedKCache
[j
].second
));
182 Consts
[i
].first
->setReg(
183 R600::R600_KC1RegClass
.getRegister(UsedKCache
[j
].second
));
186 llvm_unreachable("Wrong Cache Line");
193 bool canClauseLocalKillFitInClause(
194 unsigned AluInstCount
,
195 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
,
196 MachineBasicBlock::iterator Def
,
197 MachineBasicBlock::iterator BBEnd
) {
198 const R600RegisterInfo
&TRI
= TII
->getRegisterInfo();
199 //TODO: change this to defs?
200 for (MachineInstr::const_mop_iterator
201 MOI
= Def
->operands_begin(),
202 MOE
= Def
->operands_end(); MOI
!= MOE
; ++MOI
) {
203 if (!MOI
->isReg() || !MOI
->isDef() ||
204 TRI
.isPhysRegLiveAcrossClauses(MOI
->getReg()))
207 // Def defines a clause local register, so check that its use will fit
209 unsigned LastUseCount
= 0;
210 for (MachineBasicBlock::iterator UseI
= Def
; UseI
!= BBEnd
; ++UseI
) {
211 AluInstCount
+= OccupiedDwords(*UseI
);
212 // Make sure we won't need to end the clause due to KCache limitations.
213 if (!SubstituteKCacheBank(*UseI
, KCacheBanks
, false))
216 // We have reached the maximum instruction limit before finding the
217 // use that kills this register, so we cannot use this def in the
219 if (AluInstCount
>= TII
->getMaxAlusPerClause())
222 // TODO: Is this true? kill flag appears to work OK below
223 // Register kill flags have been cleared by the time we get to this
224 // pass, but it is safe to assume that all uses of this register
225 // occur in the same basic block as its definition, because
226 // it is illegal for the scheduler to schedule them in
228 if (UseI
->readsRegister(MOI
->getReg(), &TRI
))
229 LastUseCount
= AluInstCount
;
231 // Exit early if the current use kills the register
232 if (UseI
!= Def
&& UseI
->killsRegister(MOI
->getReg(), &TRI
))
236 return LastUseCount
<= TII
->getMaxAlusPerClause();
237 llvm_unreachable("Clause local register live at end of clause.");
242 MachineBasicBlock::iterator
243 MakeALUClause(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
) {
244 MachineBasicBlock::iterator ClauseHead
= I
;
245 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
;
246 bool PushBeforeModifier
= false;
247 unsigned AluInstCount
= 0;
248 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
; ++I
) {
249 if (IsTrivialInst(*I
))
253 if (AluInstCount
> TII
->getMaxAlusPerClause())
255 if (I
->getOpcode() == R600::PRED_X
) {
256 // We put PRED_X in its own clause to ensure that ifcvt won't create
257 // clauses with more than 128 insts.
258 // IfCvt is indeed checking that "then" and "else" branches of an if
259 // statement have less than ~60 insts thus converted clauses can't be
260 // bigger than ~121 insts (predicate setter needs to be in the same
261 // clause as predicated alus).
262 if (AluInstCount
> 0)
264 if (TII
->getFlagOp(*I
).getImm() & MO_FLAG_PUSH
)
265 PushBeforeModifier
= true;
269 // XXX: GROUP_BARRIER instructions cannot be in the same ALU clause as:
271 // * KILL or INTERP instructions
272 // * Any instruction that sets UPDATE_EXEC_MASK or UPDATE_PRED bits
273 // * Uses waterfalling (i.e. INDEX_MODE = AR.X)
275 // XXX: These checks have not been implemented yet.
276 if (TII
->mustBeLastInClause(I
->getOpcode())) {
281 // If this instruction defines a clause local register, make sure
282 // its use can fit in this clause.
283 if (!canClauseLocalKillFitInClause(AluInstCount
, KCacheBanks
, I
, E
))
286 if (!SubstituteKCacheBank(*I
, KCacheBanks
))
288 AluInstCount
+= OccupiedDwords(*I
);
290 unsigned Opcode
= PushBeforeModifier
?
291 R600::CF_ALU_PUSH_BEFORE
: R600::CF_ALU
;
292 BuildMI(MBB
, ClauseHead
, MBB
.findDebugLoc(ClauseHead
), TII
->get(Opcode
))
293 // We don't use the ADDR field until R600ControlFlowFinalizer pass, where
294 // it is safe to assume it is 0. However if we always put 0 here, the ifcvt
295 // pass may assume that identical ALU clause starter at the beginning of a
296 // true and false branch can be factorized which is not the case.
297 .addImm(Address
++) // ADDR
298 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].first
) // KB0
299 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].first
) // KB1
300 .addImm(KCacheBanks
.empty()?0:2) // KM0
301 .addImm((KCacheBanks
.size() < 2)?0:2) // KM1
302 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].second
) // KLINE0
303 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].second
) // KLINE1
304 .addImm(AluInstCount
) // COUNT
305 .addImm(1); // Enabled
312 R600EmitClauseMarkers() : MachineFunctionPass(ID
) {
313 initializeR600EmitClauseMarkersPass(*PassRegistry::getPassRegistry());
316 bool runOnMachineFunction(MachineFunction
&MF
) override
{
317 const R600Subtarget
&ST
= MF
.getSubtarget
<R600Subtarget
>();
318 TII
= ST
.getInstrInfo();
320 for (MachineFunction::iterator BB
= MF
.begin(), BB_E
= MF
.end();
322 MachineBasicBlock
&MBB
= *BB
;
323 MachineBasicBlock::iterator I
= MBB
.begin();
324 if (I
!= MBB
.end() && I
->getOpcode() == R600::CF_ALU
)
325 continue; // BB was already parsed
326 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
;) {
328 auto next
= MakeALUClause(MBB
, I
);
338 StringRef
getPassName() const override
{
339 return "R600 Emit Clause Markers Pass";
343 char R600EmitClauseMarkers::ID
= 0;
345 } // end anonymous namespace
347 INITIALIZE_PASS_BEGIN(R600EmitClauseMarkers
, "emitclausemarkers",
348 "R600 Emit Clause Markters", false, false)
349 INITIALIZE_PASS_END(R600EmitClauseMarkers
, "emitclausemarkers",
350 "R600 Emit Clause Markters", false, false)
352 FunctionPass
*llvm::createR600EmitClauseMarkers() {
353 return new R600EmitClauseMarkers();