26 #define DEBUG_TYPE "amdgpu-atomic-optimizer" 33 struct ReplacementInfo {
51 Value *
const Identity)
const;
54 bool ValDivergent)
const;
80 if (skipFunction(F)) {
84 DA = &getAnalysis<LegacyDivergenceAnalysis>();
87 getAnalysisIfAvailable<DominatorTreeWrapperPass>();
96 const bool Changed = !ToReplace.empty();
98 for (ReplacementInfo &
Info : ToReplace) {
107 void AMDGPUAtomicOptimizer::visitAtomicRMWInst(
AtomicRMWInst &
I) {
134 const unsigned PtrIdx = 0;
135 const unsigned ValIdx = 1;
143 const bool ValDivergent = DA->isDivergentUse(&I.
getOperandUse(ValIdx));
150 (!
ST->hasDPP() || DL->getTypeSizeInBits(I.
getType()) != 32)) {
157 const ReplacementInfo
Info = {&
I,
Op, ValIdx, ValDivergent};
159 ToReplace.push_back(Info);
162 void AMDGPUAtomicOptimizer::visitIntrinsicInst(
IntrinsicInst &I) {
168 case Intrinsic::amdgcn_buffer_atomic_add:
169 case Intrinsic::amdgcn_struct_buffer_atomic_add:
170 case Intrinsic::amdgcn_raw_buffer_atomic_add:
173 case Intrinsic::amdgcn_buffer_atomic_sub:
174 case Intrinsic::amdgcn_struct_buffer_atomic_sub:
175 case Intrinsic::amdgcn_raw_buffer_atomic_sub:
178 case Intrinsic::amdgcn_buffer_atomic_and:
179 case Intrinsic::amdgcn_struct_buffer_atomic_and:
180 case Intrinsic::amdgcn_raw_buffer_atomic_and:
183 case Intrinsic::amdgcn_buffer_atomic_or:
184 case Intrinsic::amdgcn_struct_buffer_atomic_or:
185 case Intrinsic::amdgcn_raw_buffer_atomic_or:
188 case Intrinsic::amdgcn_buffer_atomic_xor:
189 case Intrinsic::amdgcn_struct_buffer_atomic_xor:
190 case Intrinsic::amdgcn_raw_buffer_atomic_xor:
193 case Intrinsic::amdgcn_buffer_atomic_smin:
194 case Intrinsic::amdgcn_struct_buffer_atomic_smin:
195 case Intrinsic::amdgcn_raw_buffer_atomic_smin:
198 case Intrinsic::amdgcn_buffer_atomic_umin:
199 case Intrinsic::amdgcn_struct_buffer_atomic_umin:
200 case Intrinsic::amdgcn_raw_buffer_atomic_umin:
203 case Intrinsic::amdgcn_buffer_atomic_smax:
204 case Intrinsic::amdgcn_struct_buffer_atomic_smax:
205 case Intrinsic::amdgcn_raw_buffer_atomic_smax:
208 case Intrinsic::amdgcn_buffer_atomic_umax:
209 case Intrinsic::amdgcn_struct_buffer_atomic_umax:
210 case Intrinsic::amdgcn_raw_buffer_atomic_umax:
215 const unsigned ValIdx = 0;
217 const bool ValDivergent = DA->isDivergentUse(&I.
getOperandUse(ValIdx));
224 (!
ST->hasDPP() || DL->getTypeSizeInBits(I.
getType()) != 32)) {
239 const ReplacementInfo
Info = {&
I,
Op, ValIdx, ValDivergent};
241 ToReplace.push_back(Info);
294 for (
unsigned Idx = 0; Idx < 4; Idx++) {
298 {Identity, V, B.getInt32(DPP::ROW_SHR0 | 1 << Idx),
299 B.getInt32(0xf), B.getInt32(0xf), B.getFalse()}));
301 if (
ST->hasDPPBroadcasts()) {
306 {Identity, V, B.getInt32(DPP::BCAST15), B.getInt32(0xa),
307 B.getInt32(0xf), B.getFalse()}));
311 {Identity, V, B.getInt32(DPP::BCAST31), B.getInt32(0xc),
312 B.getInt32(0xf), B.getFalse()}));
325 {Identity, PermX, B.getInt32(DPP::QUAD_PERM_ID),
326 B.getInt32(0xa), B.getInt32(0xf), B.getFalse()}));
327 if (!
ST->isWave32()) {
333 {Identity, Lane31, B.getInt32(DPP::QUAD_PERM_ID),
334 B.getInt32(0xc), B.getInt32(0xf), B.getFalse()}));
343 Value *
const Identity)
const {
353 if (
ST->hasDPPWavefrontShifts()) {
370 if (!
ST->isWave32()) {
407 void AMDGPUAtomicOptimizer::optimizeAtomic(
Instruction &I,
410 bool ValDivergent)
const {
440 const unsigned TyBitWidth = DL->getTypeSizeInBits(Ty);
451 Intrinsic::amdgcn_icmp, {WaveTy, B.
getInt32Ty()},
459 if (
ST->isWave32()) {
475 Value *ExclScan =
nullptr;
476 Value *NewV =
nullptr;
483 NewV = B.
CreateIntrinsic(Intrinsic::amdgcn_set_inactive, Ty, {V, Identity});
487 NewV = buildScan(B, ScanOp, NewV, Identity);
488 ExclScan = buildShiftRight(B, NewV, Identity);
494 if (TyBitWidth == 64) {
496 Value *
const ExtractHi =
499 Intrinsic::amdgcn_readlane, {}, {ExtractLo, LastLaneIdx});
501 Intrinsic::amdgcn_readlane, {}, {ExtractHi, LastLaneIdx});
504 Value *
const Insert =
507 }
else if (TyBitWidth == 32) {
509 {NewV, LastLaneIdx});
591 Value *BroadcastI =
nullptr;
593 if (TyBitWidth == 64) {
595 Value *
const ExtractHi =
603 Value *
const Insert =
606 }
else if (TyBitWidth == 32) {
608 BroadcastI = B.
CreateIntrinsic(Intrinsic::amdgcn_readfirstlane, {}, PHI);
617 Value *LaneOffset =
nullptr;
662 "AMDGPU atomic optimizations",
false,
false)
669 return new AMDGPUAtomicOptimizer();
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A parsed version of the target data layout string in and methods for querying it. ...
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
*p = old <signed v ? old : v
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Base class for instruction visitors.
AMDGPU specific subclass of TargetSubtarget.
This class represents lattice values for constants.
A Module instance is used to store all the information related to an LLVM module. ...
This class represents a function call, abstracting a target machine's calling convention.
Address space for global memory (RAT0, VTX0).
*p = old <unsigned v ? old : v
*p = old >unsigned v ? old : v
const Use & getOperandUse(unsigned i) const
TMC & getTM() const
Get the right type of TargetMachine for this target.
an instruction that atomically reads a memory location, combines it with another value, and then stores the result back.
*p = old >signed v ? old : v
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
char & AMDGPUAtomicOptimizerID
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
AnalysisUsage & addRequired()
const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
#define INITIALIZE_PASS_DEPENDENCY(depName)
static Value * buildNonAtomicBinOp(IRBuilder<> &B, AtomicRMWInst::BinOp Op, Value *LHS, Value *RHS)
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
BinOp getOperation() const
DominatorTree & getDomTree()
Target-Independent Code Generator Pass Configuration Options.
Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following: ...
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Type * getType() const
All values are typed, get the type of this value.
BinOp
This enumeration lists the possible modifications atomicrmw can make.
BasicBlock * GetInsertBlock() const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
CallInst * CreateUnaryIntrinsic(Intrinsic::ID ID, Value *V, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with 1 operand which is mangled on its type.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Function * getDeclaration(Module *M, ID id, ArrayRef< Type *> Tys=None)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block...
Value * getOperand(unsigned i) const
Analysis containing CSE Info
ConstantInt * getIntN(unsigned N, uint64_t C)
Get a constant N-bit value, zero extended or truncated from a 64-bit value.
static bool runOnFunction(Function &F, bool PostInlining)
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Represent the analysis usage information of a pass.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
FunctionPass class - This class is used to implement most global optimizations.
Value * CreateICmpEQ(Value *LHS, Value *RHS, const Twine &Name="")
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
PHINode * CreatePHI(Type *Ty, unsigned NumReservedValues, const Twine &Name="")
static APInt getIdentityValueForAtomicOp(AtomicRMWInst::BinOp Op, unsigned BitWidth)
unsigned getNumOperands() const
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Value * CreateIntCast(Value *V, Type *DestTy, bool isSigned, const Twine &Name="")
Address space for local memory.
Calling convention used for Mesa/AMDPAL pixel shaders.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type *> Types, ArrayRef< Value *> Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with args, mangled using Types.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
FunctionPass * createAMDGPUAtomicOptimizerPass()
AMDGPU atomic optimizations
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
void setOperand(unsigned i, Value *Val)
Class for arbitrary precision integers.
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
ConstantInt * getFalse()
Get the constant value for i1 false.
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value *> Args=None, const Twine &Name="", MDNode *FPMathTag=nullptr)
INITIALIZE_PASS_BEGIN(AMDGPUAtomicOptimizer, DEBUG_TYPE, "AMDGPU atomic optimizations", false, false) INITIALIZE_PASS_END(AMDGPUAtomicOptimizer
Value * CreateAnd(Value *LHS, Value *RHS, const Twine &Name="")
InstTy * Insert(InstTy *I, const Twine &Name="") const
Insert and return the specified instruction.
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
void moveBefore(Instruction *MovePos)
Unlink this instruction from its current basic block and insert it into the basic block that MovePos ...
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Primary interface to the complete machine description for the target machine.
Legacy analysis pass which computes a DominatorTree.
ConstantInt * getInt(const APInt &AI)
Get a constant integer value.
Instruction * SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, bool Unreachable, MDNode *BranchWeights=nullptr, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, BasicBlock *ThenBlock=nullptr)
Split the containing block at the specified instruction - everything before SplitBefore stays in the ...
A wrapper class for inspecting calls to intrinsic functions.
const BasicBlock * getParent() const