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