blob: 8a92e7d923fbc5e40b825625689fe33227ab69e2 [file] [log] [blame]
Neil Henning66416572018-10-08 15:49:19 +00001//===-- AMDGPUAtomicOptimizer.cpp -----------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Neil Henning66416572018-10-08 15:49:19 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This pass optimizes atomic operations by using a single lane of a wavefront
11/// to perform the atomic operation, thus reducing contention on that memory
12/// location.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AMDGPU.h"
17#include "AMDGPUSubtarget.h"
18#include "llvm/Analysis/LegacyDivergenceAnalysis.h"
19#include "llvm/CodeGen/TargetPassConfig.h"
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/InstVisitor.h"
22#include "llvm/Transforms/Utils/BasicBlockUtils.h"
23
24#define DEBUG_TYPE "amdgpu-atomic-optimizer"
25
26using namespace llvm;
27
28namespace {
29
30enum DPP_CTRL {
31 DPP_ROW_SR1 = 0x111,
32 DPP_ROW_SR2 = 0x112,
Neil Henning8c10fa12019-02-11 14:44:14 +000033 DPP_ROW_SR3 = 0x113,
Neil Henning66416572018-10-08 15:49:19 +000034 DPP_ROW_SR4 = 0x114,
35 DPP_ROW_SR8 = 0x118,
36 DPP_WF_SR1 = 0x138,
37 DPP_ROW_BCAST15 = 0x142,
38 DPP_ROW_BCAST31 = 0x143
39};
40
41struct ReplacementInfo {
42 Instruction *I;
Jay Foad17060f02019-07-16 17:44:54 +000043 AtomicRMWInst::BinOp Op;
Neil Henning66416572018-10-08 15:49:19 +000044 unsigned ValIdx;
45 bool ValDivergent;
46};
47
48class AMDGPUAtomicOptimizer : public FunctionPass,
49 public InstVisitor<AMDGPUAtomicOptimizer> {
50private:
51 SmallVector<ReplacementInfo, 8> ToReplace;
52 const LegacyDivergenceAnalysis *DA;
53 const DataLayout *DL;
54 DominatorTree *DT;
55 bool HasDPP;
Neil Henning233a02d2018-11-05 12:04:48 +000056 bool IsPixelShader;
Neil Henning66416572018-10-08 15:49:19 +000057
Jay Foad17060f02019-07-16 17:44:54 +000058 void optimizeAtomic(Instruction &I, AtomicRMWInst::BinOp Op, unsigned ValIdx,
59 bool ValDivergent) const;
Neil Henning66416572018-10-08 15:49:19 +000060
Neil Henning66416572018-10-08 15:49:19 +000061public:
62 static char ID;
63
64 AMDGPUAtomicOptimizer() : FunctionPass(ID) {}
65
66 bool runOnFunction(Function &F) override;
67
68 void getAnalysisUsage(AnalysisUsage &AU) const override {
69 AU.addPreserved<DominatorTreeWrapperPass>();
70 AU.addRequired<LegacyDivergenceAnalysis>();
71 AU.addRequired<TargetPassConfig>();
72 }
73
74 void visitAtomicRMWInst(AtomicRMWInst &I);
75 void visitIntrinsicInst(IntrinsicInst &I);
76};
77
78} // namespace
79
80char AMDGPUAtomicOptimizer::ID = 0;
81
82char &llvm::AMDGPUAtomicOptimizerID = AMDGPUAtomicOptimizer::ID;
83
84bool AMDGPUAtomicOptimizer::runOnFunction(Function &F) {
85 if (skipFunction(F)) {
86 return false;
87 }
88
89 DA = &getAnalysis<LegacyDivergenceAnalysis>();
90 DL = &F.getParent()->getDataLayout();
91 DominatorTreeWrapperPass *const DTW =
92 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
93 DT = DTW ? &DTW->getDomTree() : nullptr;
94 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
95 const TargetMachine &TM = TPC.getTM<TargetMachine>();
96 const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
97 HasDPP = ST.hasDPP();
Neil Henning233a02d2018-11-05 12:04:48 +000098 IsPixelShader = F.getCallingConv() == CallingConv::AMDGPU_PS;
Neil Henning66416572018-10-08 15:49:19 +000099
100 visit(F);
101
102 const bool Changed = !ToReplace.empty();
103
104 for (ReplacementInfo &Info : ToReplace) {
105 optimizeAtomic(*Info.I, Info.Op, Info.ValIdx, Info.ValDivergent);
106 }
107
108 ToReplace.clear();
109
110 return Changed;
111}
112
113void AMDGPUAtomicOptimizer::visitAtomicRMWInst(AtomicRMWInst &I) {
114 // Early exit for unhandled address space atomic instructions.
115 switch (I.getPointerAddressSpace()) {
116 default:
117 return;
118 case AMDGPUAS::GLOBAL_ADDRESS:
119 case AMDGPUAS::LOCAL_ADDRESS:
120 break;
121 }
122
Jay Foad17060f02019-07-16 17:44:54 +0000123 AtomicRMWInst::BinOp Op = I.getOperation();
Neil Henning66416572018-10-08 15:49:19 +0000124
Jay Foad17060f02019-07-16 17:44:54 +0000125 switch (Op) {
Neil Henning66416572018-10-08 15:49:19 +0000126 default:
127 return;
128 case AtomicRMWInst::Add:
Neil Henning66416572018-10-08 15:49:19 +0000129 case AtomicRMWInst::Sub:
Jay Foad70235c62019-07-17 13:40:03 +0000130 case AtomicRMWInst::And:
131 case AtomicRMWInst::Or:
132 case AtomicRMWInst::Xor:
Jay Foad17060f02019-07-16 17:44:54 +0000133 case AtomicRMWInst::Max:
134 case AtomicRMWInst::Min:
135 case AtomicRMWInst::UMax:
136 case AtomicRMWInst::UMin:
Neil Henning66416572018-10-08 15:49:19 +0000137 break;
138 }
139
140 const unsigned PtrIdx = 0;
141 const unsigned ValIdx = 1;
142
143 // If the pointer operand is divergent, then each lane is doing an atomic
144 // operation on a different address, and we cannot optimize that.
145 if (DA->isDivergent(I.getOperand(PtrIdx))) {
146 return;
147 }
148
149 const bool ValDivergent = DA->isDivergent(I.getOperand(ValIdx));
150
151 // If the value operand is divergent, each lane is contributing a different
152 // value to the atomic calculation. We can only optimize divergent values if
153 // we have DPP available on our subtarget, and the atomic operation is 32
154 // bits.
155 if (ValDivergent && (!HasDPP || (DL->getTypeSizeInBits(I.getType()) != 32))) {
156 return;
157 }
158
159 // If we get here, we can optimize the atomic using a single wavefront-wide
160 // atomic operation to do the calculation for the entire wavefront, so
161 // remember the instruction so we can come back to it.
162 const ReplacementInfo Info = {&I, Op, ValIdx, ValDivergent};
163
164 ToReplace.push_back(Info);
165}
166
167void AMDGPUAtomicOptimizer::visitIntrinsicInst(IntrinsicInst &I) {
Jay Foad17060f02019-07-16 17:44:54 +0000168 AtomicRMWInst::BinOp Op;
Neil Henning66416572018-10-08 15:49:19 +0000169
170 switch (I.getIntrinsicID()) {
171 default:
172 return;
173 case Intrinsic::amdgcn_buffer_atomic_add:
174 case Intrinsic::amdgcn_struct_buffer_atomic_add:
175 case Intrinsic::amdgcn_raw_buffer_atomic_add:
Jay Foad17060f02019-07-16 17:44:54 +0000176 Op = AtomicRMWInst::Add;
Neil Henning66416572018-10-08 15:49:19 +0000177 break;
178 case Intrinsic::amdgcn_buffer_atomic_sub:
179 case Intrinsic::amdgcn_struct_buffer_atomic_sub:
180 case Intrinsic::amdgcn_raw_buffer_atomic_sub:
Jay Foad17060f02019-07-16 17:44:54 +0000181 Op = AtomicRMWInst::Sub;
182 break;
Jay Foad70235c62019-07-17 13:40:03 +0000183 case Intrinsic::amdgcn_buffer_atomic_and:
184 case Intrinsic::amdgcn_struct_buffer_atomic_and:
185 case Intrinsic::amdgcn_raw_buffer_atomic_and:
186 Op = AtomicRMWInst::And;
187 break;
188 case Intrinsic::amdgcn_buffer_atomic_or:
189 case Intrinsic::amdgcn_struct_buffer_atomic_or:
190 case Intrinsic::amdgcn_raw_buffer_atomic_or:
191 Op = AtomicRMWInst::Or;
192 break;
193 case Intrinsic::amdgcn_buffer_atomic_xor:
194 case Intrinsic::amdgcn_struct_buffer_atomic_xor:
195 case Intrinsic::amdgcn_raw_buffer_atomic_xor:
196 Op = AtomicRMWInst::Xor;
197 break;
Jay Foad17060f02019-07-16 17:44:54 +0000198 case Intrinsic::amdgcn_buffer_atomic_smin:
199 case Intrinsic::amdgcn_struct_buffer_atomic_smin:
200 case Intrinsic::amdgcn_raw_buffer_atomic_smin:
201 Op = AtomicRMWInst::Min;
202 break;
203 case Intrinsic::amdgcn_buffer_atomic_umin:
204 case Intrinsic::amdgcn_struct_buffer_atomic_umin:
205 case Intrinsic::amdgcn_raw_buffer_atomic_umin:
206 Op = AtomicRMWInst::UMin;
207 break;
208 case Intrinsic::amdgcn_buffer_atomic_smax:
209 case Intrinsic::amdgcn_struct_buffer_atomic_smax:
210 case Intrinsic::amdgcn_raw_buffer_atomic_smax:
211 Op = AtomicRMWInst::Max;
212 break;
213 case Intrinsic::amdgcn_buffer_atomic_umax:
214 case Intrinsic::amdgcn_struct_buffer_atomic_umax:
215 case Intrinsic::amdgcn_raw_buffer_atomic_umax:
216 Op = AtomicRMWInst::UMax;
Neil Henning66416572018-10-08 15:49:19 +0000217 break;
218 }
219
220 const unsigned ValIdx = 0;
221
222 const bool ValDivergent = DA->isDivergent(I.getOperand(ValIdx));
223
224 // If the value operand is divergent, each lane is contributing a different
225 // value to the atomic calculation. We can only optimize divergent values if
226 // we have DPP available on our subtarget, and the atomic operation is 32
227 // bits.
228 if (ValDivergent && (!HasDPP || (DL->getTypeSizeInBits(I.getType()) != 32))) {
229 return;
230 }
231
232 // If any of the other arguments to the intrinsic are divergent, we can't
233 // optimize the operation.
234 for (unsigned Idx = 1; Idx < I.getNumOperands(); Idx++) {
235 if (DA->isDivergent(I.getOperand(Idx))) {
236 return;
237 }
238 }
239
240 // If we get here, we can optimize the atomic using a single wavefront-wide
241 // atomic operation to do the calculation for the entire wavefront, so
242 // remember the instruction so we can come back to it.
243 const ReplacementInfo Info = {&I, Op, ValIdx, ValDivergent};
244
245 ToReplace.push_back(Info);
246}
247
Jay Foad17060f02019-07-16 17:44:54 +0000248// Use the builder to create the non-atomic counterpart of the specified
249// atomicrmw binary op.
250static Value *buildNonAtomicBinOp(IRBuilder<> &B, AtomicRMWInst::BinOp Op,
251 Value *LHS, Value *RHS) {
252 CmpInst::Predicate Pred;
253
254 switch (Op) {
255 default:
256 llvm_unreachable("Unhandled atomic op");
257 case AtomicRMWInst::Add:
258 return B.CreateBinOp(Instruction::Add, LHS, RHS);
259 case AtomicRMWInst::Sub:
260 return B.CreateBinOp(Instruction::Sub, LHS, RHS);
Jay Foad70235c62019-07-17 13:40:03 +0000261 case AtomicRMWInst::And:
262 return B.CreateBinOp(Instruction::And, LHS, RHS);
263 case AtomicRMWInst::Or:
264 return B.CreateBinOp(Instruction::Or, LHS, RHS);
265 case AtomicRMWInst::Xor:
266 return B.CreateBinOp(Instruction::Xor, LHS, RHS);
Jay Foad17060f02019-07-16 17:44:54 +0000267
268 case AtomicRMWInst::Max:
269 Pred = CmpInst::ICMP_SGT;
270 break;
271 case AtomicRMWInst::Min:
272 Pred = CmpInst::ICMP_SLT;
273 break;
274 case AtomicRMWInst::UMax:
275 Pred = CmpInst::ICMP_UGT;
276 break;
277 case AtomicRMWInst::UMin:
278 Pred = CmpInst::ICMP_ULT;
279 break;
280 }
281 Value *Cond = B.CreateICmp(Pred, LHS, RHS);
282 return B.CreateSelect(Cond, LHS, RHS);
283}
284
285static APInt getIdentityValueForAtomicOp(AtomicRMWInst::BinOp Op,
286 unsigned BitWidth) {
287 switch (Op) {
288 default:
289 llvm_unreachable("Unhandled atomic op");
290 case AtomicRMWInst::Add:
291 case AtomicRMWInst::Sub:
Jay Foad70235c62019-07-17 13:40:03 +0000292 case AtomicRMWInst::Or:
293 case AtomicRMWInst::Xor:
Jay Foad17060f02019-07-16 17:44:54 +0000294 case AtomicRMWInst::UMax:
295 return APInt::getMinValue(BitWidth);
Jay Foad70235c62019-07-17 13:40:03 +0000296 case AtomicRMWInst::And:
Jay Foad17060f02019-07-16 17:44:54 +0000297 case AtomicRMWInst::UMin:
298 return APInt::getMaxValue(BitWidth);
299 case AtomicRMWInst::Max:
300 return APInt::getSignedMinValue(BitWidth);
301 case AtomicRMWInst::Min:
302 return APInt::getSignedMaxValue(BitWidth);
303 }
304}
305
Neil Henning66416572018-10-08 15:49:19 +0000306void AMDGPUAtomicOptimizer::optimizeAtomic(Instruction &I,
Jay Foad17060f02019-07-16 17:44:54 +0000307 AtomicRMWInst::BinOp Op,
Neil Henning66416572018-10-08 15:49:19 +0000308 unsigned ValIdx,
309 bool ValDivergent) const {
Neil Henning66416572018-10-08 15:49:19 +0000310 // Start building just before the instruction.
311 IRBuilder<> B(&I);
312
Neil Henning233a02d2018-11-05 12:04:48 +0000313 // If we are in a pixel shader, because of how we have to mask out helper
314 // lane invocations, we need to record the entry and exit BB's.
315 BasicBlock *PixelEntryBB = nullptr;
316 BasicBlock *PixelExitBB = nullptr;
317
318 // If we're optimizing an atomic within a pixel shader, we need to wrap the
319 // entire atomic operation in a helper-lane check. We do not want any helper
320 // lanes that are around only for the purposes of derivatives to take part
321 // in any cross-lane communication, and we use a branch on whether the lane is
322 // live to do this.
323 if (IsPixelShader) {
324 // Record I's original position as the entry block.
325 PixelEntryBB = I.getParent();
326
327 Value *const Cond = B.CreateIntrinsic(Intrinsic::amdgcn_ps_live, {}, {});
328 Instruction *const NonHelperTerminator =
329 SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, DT, nullptr);
330
331 // Record I's new position as the exit block.
332 PixelExitBB = I.getParent();
333
334 I.moveBefore(NonHelperTerminator);
335 B.SetInsertPoint(&I);
336 }
337
Neil Henning66416572018-10-08 15:49:19 +0000338 Type *const Ty = I.getType();
339 const unsigned TyBitWidth = DL->getTypeSizeInBits(Ty);
340 Type *const VecTy = VectorType::get(B.getInt32Ty(), 2);
341
342 // This is the value in the atomic operation we need to combine in order to
343 // reduce the number of atomic operations.
344 Value *const V = I.getOperand(ValIdx);
345
346 // We need to know how many lanes are active within the wavefront, and we do
Neil Henning8c10fa12019-02-11 14:44:14 +0000347 // this by doing a ballot of active lanes.
Jay Foad38902352019-07-08 07:04:58 +0000348 CallInst *const Ballot = B.CreateIntrinsic(
349 Intrinsic::amdgcn_icmp, {B.getInt64Ty(), B.getInt32Ty()},
350 {B.getInt32(1), B.getInt32(0), B.getInt32(CmpInst::ICMP_NE)});
Neil Henning66416572018-10-08 15:49:19 +0000351
352 // We need to know how many lanes are active within the wavefront that are
353 // below us. If we counted each lane linearly starting from 0, a lane is
354 // below us only if its associated index was less than ours. We do this by
355 // using the mbcnt intrinsic.
Neil Henning8c10fa12019-02-11 14:44:14 +0000356 Value *const BitCast = B.CreateBitCast(Ballot, VecTy);
Neil Henning66416572018-10-08 15:49:19 +0000357 Value *const ExtractLo = B.CreateExtractElement(BitCast, B.getInt32(0));
358 Value *const ExtractHi = B.CreateExtractElement(BitCast, B.getInt32(1));
359 CallInst *const PartialMbcnt = B.CreateIntrinsic(
360 Intrinsic::amdgcn_mbcnt_lo, {}, {ExtractLo, B.getInt32(0)});
Jay Foad70235c62019-07-17 13:40:03 +0000361 Value *const Mbcnt =
362 B.CreateIntCast(B.CreateIntrinsic(Intrinsic::amdgcn_mbcnt_hi, {},
363 {ExtractHi, PartialMbcnt}),
364 Ty, false);
Neil Henning66416572018-10-08 15:49:19 +0000365
Jay Foad17060f02019-07-16 17:44:54 +0000366 Value *const Identity = B.getInt(getIdentityValueForAtomicOp(Op, TyBitWidth));
367
368 Value *ExclScan = nullptr;
Neil Henning66416572018-10-08 15:49:19 +0000369 Value *NewV = nullptr;
370
371 // If we have a divergent value in each lane, we need to combine the value
372 // using DPP.
373 if (ValDivergent) {
Jay Foad17060f02019-07-16 17:44:54 +0000374 // First we need to set all inactive invocations to the identity value, so
375 // that they can correctly contribute to the final result.
Neil Henning8c10fa12019-02-11 14:44:14 +0000376 CallInst *const SetInactive =
377 B.CreateIntrinsic(Intrinsic::amdgcn_set_inactive, Ty, {V, Identity});
Neil Henning66416572018-10-08 15:49:19 +0000378
Neil Henning8c10fa12019-02-11 14:44:14 +0000379 CallInst *const FirstDPP =
380 B.CreateIntrinsic(Intrinsic::amdgcn_update_dpp, Ty,
381 {Identity, SetInactive, B.getInt32(DPP_WF_SR1),
382 B.getInt32(0xf), B.getInt32(0xf), B.getFalse()});
Jay Foad17060f02019-07-16 17:44:54 +0000383 ExclScan = FirstDPP;
Neil Henning66416572018-10-08 15:49:19 +0000384
Neil Henning8c10fa12019-02-11 14:44:14 +0000385 const unsigned Iters = 7;
386 const unsigned DPPCtrl[Iters] = {
387 DPP_ROW_SR1, DPP_ROW_SR2, DPP_ROW_SR3, DPP_ROW_SR4,
388 DPP_ROW_SR8, DPP_ROW_BCAST15, DPP_ROW_BCAST31};
389 const unsigned RowMask[Iters] = {0xf, 0xf, 0xf, 0xf, 0xf, 0xa, 0xc};
390 const unsigned BankMask[Iters] = {0xf, 0xf, 0xf, 0xe, 0xc, 0xf, 0xf};
391
392 // This loop performs an exclusive scan across the wavefront, with all lanes
Neil Henning66416572018-10-08 15:49:19 +0000393 // active (by using the WWM intrinsic).
394 for (unsigned Idx = 0; Idx < Iters; Idx++) {
Jay Foad17060f02019-07-16 17:44:54 +0000395 Value *const UpdateValue = Idx < 3 ? FirstDPP : ExclScan;
Neil Henning8c10fa12019-02-11 14:44:14 +0000396 CallInst *const DPP = B.CreateIntrinsic(
397 Intrinsic::amdgcn_update_dpp, Ty,
398 {Identity, UpdateValue, B.getInt32(DPPCtrl[Idx]),
399 B.getInt32(RowMask[Idx]), B.getInt32(BankMask[Idx]), B.getFalse()});
Neil Henning66416572018-10-08 15:49:19 +0000400
Jay Foad17060f02019-07-16 17:44:54 +0000401 ExclScan = buildNonAtomicBinOp(B, Op, ExclScan, DPP);
Neil Henning66416572018-10-08 15:49:19 +0000402 }
403
Jay Foad17060f02019-07-16 17:44:54 +0000404 NewV = buildNonAtomicBinOp(B, Op, SetInactive, ExclScan);
Neil Henning66416572018-10-08 15:49:19 +0000405
406 // Read the value from the last lane, which has accumlated the values of
Jay Foad17060f02019-07-16 17:44:54 +0000407 // each active lane in the wavefront. This will be our new value which we
408 // will provide to the atomic operation.
Neil Henning66416572018-10-08 15:49:19 +0000409 if (TyBitWidth == 64) {
410 Value *const ExtractLo = B.CreateTrunc(NewV, B.getInt32Ty());
411 Value *const ExtractHi =
412 B.CreateTrunc(B.CreateLShr(NewV, B.getInt64(32)), B.getInt32Ty());
413 CallInst *const ReadLaneLo = B.CreateIntrinsic(
414 Intrinsic::amdgcn_readlane, {}, {ExtractLo, B.getInt32(63)});
Neil Henning66416572018-10-08 15:49:19 +0000415 CallInst *const ReadLaneHi = B.CreateIntrinsic(
416 Intrinsic::amdgcn_readlane, {}, {ExtractHi, B.getInt32(63)});
Neil Henning66416572018-10-08 15:49:19 +0000417 Value *const PartialInsert = B.CreateInsertElement(
418 UndefValue::get(VecTy), ReadLaneLo, B.getInt32(0));
419 Value *const Insert =
420 B.CreateInsertElement(PartialInsert, ReadLaneHi, B.getInt32(1));
421 NewV = B.CreateBitCast(Insert, Ty);
422 } else if (TyBitWidth == 32) {
Jay Foad17060f02019-07-16 17:44:54 +0000423 NewV = B.CreateIntrinsic(Intrinsic::amdgcn_readlane, {},
424 {NewV, B.getInt32(63)});
Neil Henning66416572018-10-08 15:49:19 +0000425 } else {
426 llvm_unreachable("Unhandled atomic bit width");
427 }
Neil Henning8c10fa12019-02-11 14:44:14 +0000428
429 // Finally mark the readlanes in the WWM section.
430 NewV = B.CreateIntrinsic(Intrinsic::amdgcn_wwm, Ty, NewV);
Neil Henning66416572018-10-08 15:49:19 +0000431 } else {
Jay Foad17060f02019-07-16 17:44:54 +0000432 switch (Op) {
433 default:
434 llvm_unreachable("Unhandled atomic op");
Neil Henning66416572018-10-08 15:49:19 +0000435
Jay Foad17060f02019-07-16 17:44:54 +0000436 case AtomicRMWInst::Add:
437 case AtomicRMWInst::Sub: {
Jay Foad70235c62019-07-17 13:40:03 +0000438 // The new value we will be contributing to the atomic operation is the
439 // old value times the number of active lanes.
440 Value *const Ctpop = B.CreateIntCast(
441 B.CreateUnaryIntrinsic(Intrinsic::ctpop, Ballot), Ty, false);
442 NewV = B.CreateMul(V, Ctpop);
Jay Foad17060f02019-07-16 17:44:54 +0000443 break;
444 }
445
Jay Foad70235c62019-07-17 13:40:03 +0000446 case AtomicRMWInst::And:
447 case AtomicRMWInst::Or:
Jay Foad17060f02019-07-16 17:44:54 +0000448 case AtomicRMWInst::Max:
449 case AtomicRMWInst::Min:
450 case AtomicRMWInst::UMax:
451 case AtomicRMWInst::UMin:
Jay Foad70235c62019-07-17 13:40:03 +0000452 // These operations with a uniform value are idempotent: doing the atomic
453 // operation multiple times has the same effect as doing it once.
Jay Foad17060f02019-07-16 17:44:54 +0000454 NewV = V;
455 break;
Jay Foad70235c62019-07-17 13:40:03 +0000456
457 case AtomicRMWInst::Xor:
458 // The new value we will be contributing to the atomic operation is the
459 // old value times the parity of the number of active lanes.
460 Value *const Ctpop = B.CreateIntCast(
461 B.CreateUnaryIntrinsic(Intrinsic::ctpop, Ballot), Ty, false);
462 NewV = B.CreateMul(V, B.CreateAnd(Ctpop, 1));
463 break;
Jay Foad17060f02019-07-16 17:44:54 +0000464 }
Neil Henning66416572018-10-08 15:49:19 +0000465 }
466
467 // We only want a single lane to enter our new control flow, and we do this
468 // by checking if there are any active lanes below us. Only one lane will
469 // have 0 active lanes below us, so that will be the only one to progress.
Jay Foad70235c62019-07-17 13:40:03 +0000470 Value *const Cond = B.CreateICmpEQ(Mbcnt, B.getIntN(TyBitWidth, 0));
Neil Henning66416572018-10-08 15:49:19 +0000471
472 // Store I's original basic block before we split the block.
473 BasicBlock *const EntryBB = I.getParent();
474
475 // We need to introduce some new control flow to force a single lane to be
476 // active. We do this by splitting I's basic block at I, and introducing the
477 // new block such that:
478 // entry --> single_lane -\
479 // \------------------> exit
480 Instruction *const SingleLaneTerminator =
481 SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, DT, nullptr);
482
483 // Move the IR builder into single_lane next.
484 B.SetInsertPoint(SingleLaneTerminator);
485
486 // Clone the original atomic operation into single lane, replacing the
487 // original value with our newly created one.
488 Instruction *const NewI = I.clone();
489 B.Insert(NewI);
490 NewI->setOperand(ValIdx, NewV);
491
492 // Move the IR builder into exit next, and start inserting just before the
493 // original instruction.
494 B.SetInsertPoint(&I);
495
496 // Create a PHI node to get our new atomic result into the exit block.
497 PHINode *const PHI = B.CreatePHI(Ty, 2);
498 PHI->addIncoming(UndefValue::get(Ty), EntryBB);
499 PHI->addIncoming(NewI, SingleLaneTerminator->getParent());
500
501 // We need to broadcast the value who was the lowest active lane (the first
502 // lane) to all other lanes in the wavefront. We use an intrinsic for this,
503 // but have to handle 64-bit broadcasts with two calls to this intrinsic.
504 Value *BroadcastI = nullptr;
505
506 if (TyBitWidth == 64) {
507 Value *const ExtractLo = B.CreateTrunc(PHI, B.getInt32Ty());
508 Value *const ExtractHi =
509 B.CreateTrunc(B.CreateLShr(PHI, B.getInt64(32)), B.getInt32Ty());
510 CallInst *const ReadFirstLaneLo =
511 B.CreateIntrinsic(Intrinsic::amdgcn_readfirstlane, {}, ExtractLo);
Neil Henning66416572018-10-08 15:49:19 +0000512 CallInst *const ReadFirstLaneHi =
513 B.CreateIntrinsic(Intrinsic::amdgcn_readfirstlane, {}, ExtractHi);
Neil Henning66416572018-10-08 15:49:19 +0000514 Value *const PartialInsert = B.CreateInsertElement(
515 UndefValue::get(VecTy), ReadFirstLaneLo, B.getInt32(0));
516 Value *const Insert =
517 B.CreateInsertElement(PartialInsert, ReadFirstLaneHi, B.getInt32(1));
518 BroadcastI = B.CreateBitCast(Insert, Ty);
519 } else if (TyBitWidth == 32) {
Matt Arsenault3a31b3f2019-03-14 13:46:09 +0000520
521 BroadcastI = B.CreateIntrinsic(Intrinsic::amdgcn_readfirstlane, {}, PHI);
Neil Henning66416572018-10-08 15:49:19 +0000522 } else {
523 llvm_unreachable("Unhandled atomic bit width");
524 }
525
526 // Now that we have the result of our single atomic operation, we need to
527 // get our individual lane's slice into the result. We use the lane offset we
528 // previously calculated combined with the atomic result value we got from the
529 // first lane, to get our lane's index into the atomic result.
Jay Foad17060f02019-07-16 17:44:54 +0000530 Value *LaneOffset = nullptr;
531 if (ValDivergent) {
532 LaneOffset = B.CreateIntrinsic(Intrinsic::amdgcn_wwm, Ty, ExclScan);
533 } else {
534 switch (Op) {
535 default:
536 llvm_unreachable("Unhandled atomic op");
537 case AtomicRMWInst::Add:
538 case AtomicRMWInst::Sub:
Jay Foad70235c62019-07-17 13:40:03 +0000539 LaneOffset = B.CreateMul(V, Mbcnt);
Jay Foad17060f02019-07-16 17:44:54 +0000540 break;
Jay Foad70235c62019-07-17 13:40:03 +0000541 case AtomicRMWInst::And:
542 case AtomicRMWInst::Or:
Jay Foad17060f02019-07-16 17:44:54 +0000543 case AtomicRMWInst::Max:
544 case AtomicRMWInst::Min:
545 case AtomicRMWInst::UMax:
546 case AtomicRMWInst::UMin:
547 LaneOffset = B.CreateSelect(Cond, Identity, V);
548 break;
Jay Foad70235c62019-07-17 13:40:03 +0000549 case AtomicRMWInst::Xor:
550 LaneOffset = B.CreateMul(V, B.CreateAnd(Mbcnt, 1));
551 break;
Jay Foad17060f02019-07-16 17:44:54 +0000552 }
553 }
554 Value *const Result = buildNonAtomicBinOp(B, Op, BroadcastI, LaneOffset);
Neil Henning66416572018-10-08 15:49:19 +0000555
Neil Henning233a02d2018-11-05 12:04:48 +0000556 if (IsPixelShader) {
557 // Need a final PHI to reconverge to above the helper lane branch mask.
558 B.SetInsertPoint(PixelExitBB->getFirstNonPHI());
559
560 PHINode *const PHI = B.CreatePHI(Ty, 2);
561 PHI->addIncoming(UndefValue::get(Ty), PixelEntryBB);
562 PHI->addIncoming(Result, I.getParent());
563 I.replaceAllUsesWith(PHI);
564 } else {
565 // Replace the original atomic instruction with the new one.
566 I.replaceAllUsesWith(Result);
567 }
Neil Henning66416572018-10-08 15:49:19 +0000568
569 // And delete the original.
570 I.eraseFromParent();
571}
572
Neil Henning66416572018-10-08 15:49:19 +0000573INITIALIZE_PASS_BEGIN(AMDGPUAtomicOptimizer, DEBUG_TYPE,
574 "AMDGPU atomic optimizations", false, false)
575INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
576INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
577INITIALIZE_PASS_END(AMDGPUAtomicOptimizer, DEBUG_TYPE,
578 "AMDGPU atomic optimizations", false, false)
579
580FunctionPass *llvm::createAMDGPUAtomicOptimizerPass() {
581 return new AMDGPUAtomicOptimizer();
582}