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 "MCTargetDesc/AMDGPUMCTargetDesc.h"
18 #include "R600Defines.h"
19 #include "R600Subtarget.h"
25 void initializeR600EmitClauseMarkersPass(PassRegistry
&);
27 } // end namespace llvm
31 class R600EmitClauseMarkers
: public MachineFunctionPass
{
33 const R600InstrInfo
*TII
= nullptr;
36 unsigned OccupiedDwords(MachineInstr
&MI
) const {
37 switch (MI
.getOpcode()) {
38 case R600::INTERP_PAIR_XY
:
39 case R600::INTERP_PAIR_ZW
:
40 case R600::INTERP_VEC_LOAD
:
49 // These will be expanded to two ALU instructions in the
50 // ExpandSpecialInstructions pass.
51 if (TII
->isLDSRetInstr(MI
.getOpcode()))
54 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()) ||
55 TII
->isReductionOp(MI
.getOpcode()))
58 unsigned NumLiteral
= 0;
59 for (MachineInstr::mop_iterator It
= MI
.operands_begin(),
60 E
= MI
.operands_end();
62 MachineOperand
&MO
= *It
;
63 if (MO
.isReg() && MO
.getReg() == R600::ALU_LITERAL_X
)
66 return 1 + NumLiteral
;
69 bool isALU(const MachineInstr
&MI
) const {
70 if (TII
->isALUInstr(MI
.getOpcode()))
72 if (TII
->isVector(MI
) || TII
->isCubeOp(MI
.getOpcode()))
74 switch (MI
.getOpcode()) {
76 case R600::INTERP_PAIR_XY
:
77 case R600::INTERP_PAIR_ZW
:
78 case R600::INTERP_VEC_LOAD
:
87 bool IsTrivialInst(MachineInstr
&MI
) const {
88 switch (MI
.getOpcode()) {
91 case R600::IMPLICIT_DEF
:
98 std::pair
<unsigned, unsigned> getAccessedBankLine(unsigned Sel
) const {
99 // Sel is (512 + (kc_bank << 12) + ConstIndex) << 2
100 // (See also R600ISelLowering.cpp)
101 // ConstIndex value is in [0, 4095];
102 return std::pair
<unsigned, unsigned>(
103 ((Sel
>> 2) - 512) >> 12, // KC_BANK
104 // Line Number of ConstIndex
105 // A line contains 16 constant registers however KCX bank can lock
106 // two line at the same time ; thus we want to get an even line number.
107 // Line number can be retrieved with (>>4), using (>>5) <<1 generates
109 ((((Sel
>> 2) - 512) & 4095) >> 5) << 1);
113 SubstituteKCacheBank(MachineInstr
&MI
,
114 std::vector
<std::pair
<unsigned, unsigned>> &CachedConsts
,
115 bool UpdateInstr
= true) const {
116 std::vector
<std::pair
<unsigned, unsigned>> UsedKCache
;
118 if (!TII
->isALUInstr(MI
.getOpcode()) && MI
.getOpcode() != R600::DOT_4
)
121 const SmallVectorImpl
<std::pair
<MachineOperand
*, int64_t>> &Consts
=
124 (TII
->isALUInstr(MI
.getOpcode()) || MI
.getOpcode() == R600::DOT_4
) &&
125 "Can't assign Const");
126 for (unsigned i
= 0, n
= Consts
.size(); i
< n
; ++i
) {
127 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
129 unsigned Sel
= Consts
[i
].second
;
130 unsigned Chan
= Sel
& 3, Index
= ((Sel
>> 2) - 512) & 31;
131 unsigned KCacheIndex
= Index
* 4 + Chan
;
132 const std::pair
<unsigned, unsigned> &BankLine
= getAccessedBankLine(Sel
);
133 if (CachedConsts
.empty()) {
134 CachedConsts
.push_back(BankLine
);
135 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
138 if (CachedConsts
[0] == BankLine
) {
139 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(0, KCacheIndex
));
142 if (CachedConsts
.size() == 1) {
143 CachedConsts
.push_back(BankLine
);
144 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
147 if (CachedConsts
[1] == BankLine
) {
148 UsedKCache
.push_back(std::pair
<unsigned, unsigned>(1, KCacheIndex
));
157 for (unsigned i
= 0, j
= 0, n
= Consts
.size(); i
< n
; ++i
) {
158 if (Consts
[i
].first
->getReg() != R600::ALU_CONST
)
160 switch(UsedKCache
[j
].first
) {
162 Consts
[i
].first
->setReg(
163 R600::R600_KC0RegClass
.getRegister(UsedKCache
[j
].second
));
166 Consts
[i
].first
->setReg(
167 R600::R600_KC1RegClass
.getRegister(UsedKCache
[j
].second
));
170 llvm_unreachable("Wrong Cache Line");
177 bool canClauseLocalKillFitInClause(
178 unsigned AluInstCount
,
179 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
,
180 MachineBasicBlock::iterator Def
,
181 MachineBasicBlock::iterator BBEnd
) {
182 const R600RegisterInfo
&TRI
= TII
->getRegisterInfo();
183 //TODO: change this to defs?
184 for (MachineInstr::const_mop_iterator
185 MOI
= Def
->operands_begin(),
186 MOE
= Def
->operands_end(); MOI
!= MOE
; ++MOI
) {
187 if (!MOI
->isReg() || !MOI
->isDef() ||
188 TRI
.isPhysRegLiveAcrossClauses(MOI
->getReg()))
191 // Def defines a clause local register, so check that its use will fit
193 unsigned LastUseCount
= 0;
194 for (MachineBasicBlock::iterator UseI
= Def
; UseI
!= BBEnd
; ++UseI
) {
195 AluInstCount
+= OccupiedDwords(*UseI
);
196 // Make sure we won't need to end the clause due to KCache limitations.
197 if (!SubstituteKCacheBank(*UseI
, KCacheBanks
, false))
200 // We have reached the maximum instruction limit before finding the
201 // use that kills this register, so we cannot use this def in the
203 if (AluInstCount
>= TII
->getMaxAlusPerClause())
206 // TODO: Is this true? kill flag appears to work OK below
207 // Register kill flags have been cleared by the time we get to this
208 // pass, but it is safe to assume that all uses of this register
209 // occur in the same basic block as its definition, because
210 // it is illegal for the scheduler to schedule them in
212 if (UseI
->readsRegister(MOI
->getReg(), &TRI
))
213 LastUseCount
= AluInstCount
;
215 // Exit early if the current use kills the register
216 if (UseI
!= Def
&& UseI
->killsRegister(MOI
->getReg(), &TRI
))
220 return LastUseCount
<= TII
->getMaxAlusPerClause();
221 llvm_unreachable("Clause local register live at end of clause.");
226 MachineBasicBlock::iterator
227 MakeALUClause(MachineBasicBlock
&MBB
, MachineBasicBlock::iterator I
) {
228 MachineBasicBlock::iterator ClauseHead
= I
;
229 std::vector
<std::pair
<unsigned, unsigned>> KCacheBanks
;
230 bool PushBeforeModifier
= false;
231 unsigned AluInstCount
= 0;
232 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
; ++I
) {
233 if (IsTrivialInst(*I
))
237 if (AluInstCount
> TII
->getMaxAlusPerClause())
239 if (I
->getOpcode() == R600::PRED_X
) {
240 // We put PRED_X in its own clause to ensure that ifcvt won't create
241 // clauses with more than 128 insts.
242 // IfCvt is indeed checking that "then" and "else" branches of an if
243 // statement have less than ~60 insts thus converted clauses can't be
244 // bigger than ~121 insts (predicate setter needs to be in the same
245 // clause as predicated alus).
246 if (AluInstCount
> 0)
248 if (TII
->getFlagOp(*I
).getImm() & MO_FLAG_PUSH
)
249 PushBeforeModifier
= true;
253 // XXX: GROUP_BARRIER instructions cannot be in the same ALU clause as:
255 // * KILL or INTERP instructions
256 // * Any instruction that sets UPDATE_EXEC_MASK or UPDATE_PRED bits
257 // * Uses waterfalling (i.e. INDEX_MODE = AR.X)
259 // XXX: These checks have not been implemented yet.
260 if (TII
->mustBeLastInClause(I
->getOpcode())) {
265 // If this instruction defines a clause local register, make sure
266 // its use can fit in this clause.
267 if (!canClauseLocalKillFitInClause(AluInstCount
, KCacheBanks
, I
, E
))
270 if (!SubstituteKCacheBank(*I
, KCacheBanks
))
272 AluInstCount
+= OccupiedDwords(*I
);
274 unsigned Opcode
= PushBeforeModifier
?
275 R600::CF_ALU_PUSH_BEFORE
: R600::CF_ALU
;
276 BuildMI(MBB
, ClauseHead
, MBB
.findDebugLoc(ClauseHead
), TII
->get(Opcode
))
277 // We don't use the ADDR field until R600ControlFlowFinalizer pass, where
278 // it is safe to assume it is 0. However if we always put 0 here, the ifcvt
279 // pass may assume that identical ALU clause starter at the beginning of a
280 // true and false branch can be factorized which is not the case.
281 .addImm(Address
++) // ADDR
282 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].first
) // KB0
283 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].first
) // KB1
284 .addImm(KCacheBanks
.empty()?0:2) // KM0
285 .addImm((KCacheBanks
.size() < 2)?0:2) // KM1
286 .addImm(KCacheBanks
.empty()?0:KCacheBanks
[0].second
) // KLINE0
287 .addImm((KCacheBanks
.size() < 2)?0:KCacheBanks
[1].second
) // KLINE1
288 .addImm(AluInstCount
) // COUNT
289 .addImm(1); // Enabled
296 R600EmitClauseMarkers() : MachineFunctionPass(ID
) {
297 initializeR600EmitClauseMarkersPass(*PassRegistry::getPassRegistry());
300 bool runOnMachineFunction(MachineFunction
&MF
) override
{
301 const R600Subtarget
&ST
= MF
.getSubtarget
<R600Subtarget
>();
302 TII
= ST
.getInstrInfo();
304 for (MachineFunction::iterator BB
= MF
.begin(), BB_E
= MF
.end();
306 MachineBasicBlock
&MBB
= *BB
;
307 MachineBasicBlock::iterator I
= MBB
.begin();
308 if (I
!= MBB
.end() && I
->getOpcode() == R600::CF_ALU
)
309 continue; // BB was already parsed
310 for (MachineBasicBlock::iterator E
= MBB
.end(); I
!= E
;) {
312 auto next
= MakeALUClause(MBB
, I
);
322 StringRef
getPassName() const override
{
323 return "R600 Emit Clause Markers Pass";
327 char R600EmitClauseMarkers::ID
= 0;
329 } // end anonymous namespace
331 INITIALIZE_PASS_BEGIN(R600EmitClauseMarkers
, "emitclausemarkers",
332 "R600 Emit Clause Markters", false, false)
333 INITIALIZE_PASS_END(R600EmitClauseMarkers
, "emitclausemarkers",
334 "R600 Emit Clause Markters", false, false)
336 FunctionPass
*llvm::createR600EmitClauseMarkers() {
337 return new R600EmitClauseMarkers();