LLVM  4.0.0
AMDGPUAnnotateUniformValues.cpp
Go to the documentation of this file.
1 //===-- AMDGPUAnnotateUniformValues.cpp - ---------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// This pass adds amdgpu.uniform metadata to IR values so this information
12 /// can be used during instruction selection.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPU.h"
17 #include "AMDGPUIntrinsicInfo.h"
18 #include "llvm/ADT/SetVector.h"
20 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/IR/IRBuilder.h"
24 #include "llvm/Support/Debug.h"
26 
27 #define DEBUG_TYPE "amdgpu-annotate-uniform"
28 
29 using namespace llvm;
30 
31 namespace {
32 
33 class AMDGPUAnnotateUniformValues : public FunctionPass,
34  public InstVisitor<AMDGPUAnnotateUniformValues> {
37  LoopInfo *LI;
39  bool isKernelFunc;
40 
41 public:
42  static char ID;
43  AMDGPUAnnotateUniformValues() :
44  FunctionPass(ID) { }
45  bool doInitialization(Module &M) override;
46  bool runOnFunction(Function &F) override;
47  StringRef getPassName() const override {
48  return "AMDGPU Annotate Uniform Values";
49  }
50  void getAnalysisUsage(AnalysisUsage &AU) const override {
54  AU.setPreservesAll();
55  }
56 
57  void visitBranchInst(BranchInst &I);
58  void visitLoadInst(LoadInst &I);
59  bool isClobberedInFunction(LoadInst * Load);
60 };
61 
62 } // End anonymous namespace
63 
64 INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
65  "Add AMDGPU uniform metadata", false, false)
69 INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
70  "Add AMDGPU uniform metadata", false, false)
71 
72 char AMDGPUAnnotateUniformValues::ID = 0;
73 
75  I->setMetadata("amdgpu.uniform", MDNode::get(I->getContext(), {}));
76 }
78  I->setMetadata("amdgpu.noclobber", MDNode::get(I->getContext(), {}));
79 }
80 
81 static void DFS(BasicBlock *Root, SetVector<BasicBlock*> & Set) {
82  for (auto I : predecessors(Root))
83  if (Set.insert(I))
84  DFS(I, Set);
85 }
86 
87 bool AMDGPUAnnotateUniformValues::isClobberedInFunction(LoadInst * Load) {
88  // 1. get Loop for the Load->getparent();
89  // 2. if it exists, collect all the BBs from the most outer
90  // loop and check for the writes. If NOT - start DFS over all preds.
91  // 3. Start DFS over all preds from the most outer loop header.
92  SetVector<BasicBlock *> Checklist;
93  BasicBlock *Start = Load->getParent();
94  Checklist.insert(Start);
95  const Value *Ptr = Load->getPointerOperand();
96  const Loop *L = LI->getLoopFor(Start);
97  if (L) {
98  const Loop *P = L;
99  do {
100  L = P;
101  P = P->getParentLoop();
102  } while (P);
103  Checklist.insert(L->block_begin(), L->block_end());
104  Start = L->getHeader();
105  }
106 
107  DFS(Start, Checklist);
108  for (auto &BB : Checklist) {
109  BasicBlock::iterator StartIt = (BB == Load->getParent()) ?
110  BasicBlock::iterator(Load) : BB->end();
111  if (MDR->getPointerDependencyFrom(MemoryLocation(Ptr),
112  true, StartIt, BB, Load).isClobber())
113  return true;
114  }
115  return false;
116 }
117 
118 void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst &I) {
119  if (I.isUnconditional())
120  return;
121 
122  Value *Cond = I.getCondition();
123  if (!DA->isUniform(Cond))
124  return;
125 
127 }
128 
129 void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
130  Value *Ptr = I.getPointerOperand();
131  if (!DA->isUniform(Ptr))
132  return;
133  auto isGlobalLoad = [](LoadInst &Load)->bool {
135  };
136  // We're tracking up to the Function boundaries
137  // We cannot go beyond because of FunctionPass restrictions
138  // Thus we can ensure that memory not clobbered for memory
139  // operations that live in kernel only.
140  bool NotClobbered = isKernelFunc && !isClobberedInFunction(&I);
142  if (!PtrI && NotClobbered && isGlobalLoad(I)) {
143  if (isa<Argument>(Ptr) || isa<GlobalValue>(Ptr)) {
144  // Lookup for the existing GEP
145  if (noClobberClones.count(Ptr)) {
146  PtrI = noClobberClones[Ptr];
147  } else {
148  // Create GEP of the Value
149  Function *F = I.getParent()->getParent();
151  Type::getInt32Ty(Ptr->getContext()), APInt(64, 0));
152  // Insert GEP at the entry to make it dominate all uses
154  Ptr->getType()->getPointerElementType(), Ptr,
156  }
157  I.replaceUsesOfWith(Ptr, PtrI);
158  }
159  }
160 
161  if (PtrI) {
162  setUniformMetadata(PtrI);
163  if (NotClobbered)
164  setNoClobberMetadata(PtrI);
165  }
166 }
167 
168 bool AMDGPUAnnotateUniformValues::doInitialization(Module &M) {
169  return false;
170 }
171 
172 bool AMDGPUAnnotateUniformValues::runOnFunction(Function &F) {
173  if (skipFunction(F))
174  return false;
175 
176  DA = &getAnalysis<DivergenceAnalysis>();
177  MDR = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
178  LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
179  isKernelFunc = F.getCallingConv() == CallingConv::AMDGPU_KERNEL;
180 
181  visit(F);
182  noClobberClones.clear();
183  return true;
184 }
185 
186 FunctionPass *
188  return new AMDGPUAnnotateUniformValues();
189 }
MachineLoop * L
Provides a lazy, caching interface for making common memory aliasing information queries, backed by LLVM's alias analysis passes.
Base class for instruction visitors.
Definition: InstVisitor.h:81
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
LoopT * getParentLoop() const
Definition: LoopInfo.h:103
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:100
An instruction for reading from memory.
Definition: Instructions.h:164
BlockT * getHeader() const
Definition: LoopInfo.h:102
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:165
Type * getPointerElementType() const
Definition: Type.h:358
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:53
bool isUnconditional() const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:180
#define F(x, y, z)
Definition: MD5.cpp:51
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:136
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition: User.cpp:24
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:51
Conditional or Unconditional Branch instruction.
Calling convention for AMDGPU code object kernels.
Definition: CallingConv.h:194
Represent the analysis usage information of a pass.
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE,"Assign register bank of generic virtual registers", false, false) RegBankSelect
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:298
Value * getPointerOperand()
Definition: Instructions.h:270
static void setNoClobberMetadata(Instruction *I)
#define DEBUG_TYPE
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:654
A wrapper analysis pass for the legacy pass manager that exposes a MemoryDepnedenceResults instance...
Add AMDGPU uniform false
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:857
Add AMDGPU uniform metadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1183
FunctionPass * createAMDGPUAnnotateUniformValues()
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:232
static void DFS(BasicBlock *Root, SetVector< BasicBlock * > &Set)
Representation for a specific memory location.
Iterator for intrusive lists based on ilist_node.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:230
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:110
const BasicBlock & getEntryBlock() const
Definition: Function.h:519
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:275
Class for arbitrary precision integers.
Definition: APInt.h:77
Interface for the AMDGPU Implementation of the Intrinsic Info class.
void setPreservesAll()
Set by analyses that do not transform their input at all.
block_iterator block_end() const
Definition: LoopInfo.h:142
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1132
Value * getCondition() const
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:169
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:368
#define I(x, y, z)
Definition: MD5.cpp:54
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.cpp:124
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:287
LLVM Value Representation.
Definition: Value.h:71
A vector that has set insertion semantics.
Definition: SetVector.h:41
block_iterator block_begin() const
Definition: LoopInfo.h:141
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:831
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
int * Ptr
INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,"Add AMDGPU uniform metadata", false, false) INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues
const BasicBlock * getParent() const
Definition: Instruction.h:62
static void setUniformMetadata(Instruction *I)
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:139