1 //===-- AMDGPUAnnotateUniformValues.cpp - ---------------------------------===//
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 /// This pass adds amdgpu.uniform metadata to IR values so this information
11 /// can be used during instruction selection.
13 //===----------------------------------------------------------------------===//
16 #include "Utils/AMDGPUBaseInfo.h"
17 #include "llvm/ADT/SetVector.h"
18 #include "llvm/Analysis/LegacyDivergenceAnalysis.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/MemoryDependenceAnalysis.h"
21 #include "llvm/IR/IRBuilder.h"
22 #include "llvm/IR/InstVisitor.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "amdgpu-annotate-uniform"
33 class AMDGPUAnnotateUniformValues : public FunctionPass,
34 public InstVisitor<AMDGPUAnnotateUniformValues> {
35 LegacyDivergenceAnalysis *DA;
36 MemoryDependenceResults *MDR;
38 DenseMap<Value*, GetElementPtrInst*> noClobberClones;
43 AMDGPUAnnotateUniformValues() :
45 bool doInitialization(Module &M) override;
46 bool runOnFunction(Function &F) override;
47 StringRef getPassName() const override {
48 return "AMDGPU Annotate Uniform Values";
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.addRequired<LegacyDivergenceAnalysis>();
52 AU.addRequired<MemoryDependenceWrapperPass>();
53 AU.addRequired<LoopInfoWrapperPass>();
57 void visitBranchInst(BranchInst &I);
58 void visitLoadInst(LoadInst &I);
59 bool isClobberedInFunction(LoadInst * Load);
62 } // End anonymous namespace
64 INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
65 "Add AMDGPU uniform metadata", false, false)
66 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis)
67 INITIALIZE_PASS_DEPENDENCY(MemoryDependenceWrapperPass)
68 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
69 INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
70 "Add AMDGPU uniform metadata", false, false)
72 char AMDGPUAnnotateUniformValues::ID = 0;
74 static void setUniformMetadata(Instruction *I) {
75 I->setMetadata("amdgpu.uniform", MDNode::get(I->getContext(), {}));
77 static void setNoClobberMetadata(Instruction *I) {
78 I->setMetadata("amdgpu.noclobber", MDNode::get(I->getContext(), {}));
81 static void DFS(BasicBlock *Root, SetVector<BasicBlock*> & Set) {
82 for (auto I : predecessors(Root))
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);
101 P = P->getParentLoop();
103 Checklist.insert(L->block_begin(), L->block_end());
104 Start = L->getHeader();
107 DFS(Start, Checklist);
108 for (auto &BB : Checklist) {
109 BasicBlock::iterator StartIt = (!L && (BB == Load->getParent())) ?
110 BasicBlock::iterator(Load) : BB->end();
111 auto Q = MDR->getPointerDependencyFrom(MemoryLocation(Ptr), true,
113 if (Q.isClobber() || Q.isUnknown())
119 void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst &I) {
120 if (DA->isUniform(&I))
121 setUniformMetadata(I.getParent()->getTerminator());
124 void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
125 Value *Ptr = I.getPointerOperand();
126 if (!DA->isUniform(Ptr))
128 auto isGlobalLoad = [&](LoadInst &Load)->bool {
129 return Load.getPointerAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
131 // We're tracking up to the Function boundaries, and cannot go beyond because
132 // of FunctionPass restrictions. We can ensure that is memory not clobbered
133 // for memory operations that are live in to entry points only.
134 Instruction *PtrI = dyn_cast<Instruction>(Ptr);
138 setUniformMetadata(PtrI);
142 bool NotClobbered = false;
144 NotClobbered = !isClobberedInFunction(&I);
145 else if (isa<Argument>(Ptr) || isa<GlobalValue>(Ptr)) {
146 if (isGlobalLoad(I) && !isClobberedInFunction(&I)) {
148 // Lookup for the existing GEP
149 if (noClobberClones.count(Ptr)) {
150 PtrI = noClobberClones[Ptr];
152 // Create GEP of the Value
153 Function *F = I.getParent()->getParent();
154 Value *Idx = Constant::getIntegerValue(
155 Type::getInt32Ty(Ptr->getContext()), APInt(64, 0));
156 // Insert GEP at the entry to make it dominate all uses
157 PtrI = GetElementPtrInst::Create(
158 Ptr->getType()->getPointerElementType(), Ptr,
159 ArrayRef<Value*>(Idx), Twine(""), F->getEntryBlock().getFirstNonPHI());
161 I.replaceUsesOfWith(Ptr, PtrI);
166 setUniformMetadata(PtrI);
168 setNoClobberMetadata(PtrI);
172 bool AMDGPUAnnotateUniformValues::doInitialization(Module &M) {
176 bool AMDGPUAnnotateUniformValues::runOnFunction(Function &F) {
180 DA = &getAnalysis<LegacyDivergenceAnalysis>();
181 MDR = &getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
182 LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
183 isEntryFunc = AMDGPU::isEntryFunctionCC(F.getCallingConv());
186 noClobberClones.clear();
191 llvm::createAMDGPUAnnotateUniformValues() {
192 return new AMDGPUAnnotateUniformValues();