LLVM 19.0.0git
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"
21#include "llvm/IR/InstVisitor.h"
23
24#define DEBUG_TYPE "amdgpu-annotate-uniform"
25
26using namespace llvm;
27
28namespace {
29
30class AMDGPUAnnotateUniformValues : public FunctionPass,
31 public InstVisitor<AMDGPUAnnotateUniformValues> {
33 MemorySSA *MSSA;
34 AliasAnalysis *AA;
35 bool isEntryFunc;
36 bool Changed;
37
38 void setUniformMetadata(Instruction *I) {
39 I->setMetadata("amdgpu.uniform", MDNode::get(I->getContext(), {}));
40 Changed = true;
41 }
42
43 void setNoClobberMetadata(Instruction *I) {
44 I->setMetadata("amdgpu.noclobber", MDNode::get(I->getContext(), {}));
45 Changed = true;
46 }
47
48public:
49 static char ID;
50 AMDGPUAnnotateUniformValues() :
51 FunctionPass(ID) { }
52 bool doInitialization(Module &M) override;
53 bool runOnFunction(Function &F) override;
54 StringRef getPassName() const override {
55 return "AMDGPU Annotate Uniform Values";
56 }
57 void getAnalysisUsage(AnalysisUsage &AU) const override {
61 AU.setPreservesAll();
62 }
63
66};
67
68} // End anonymous namespace
69
70INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
71 "Add AMDGPU uniform metadata", false, false)
75INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
77
78char AMDGPUAnnotateUniformValues::ID = 0;
79
80void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst &I) {
81 if (UA->isUniform(&I))
82 setUniformMetadata(&I);
83}
84
85void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
86 Value *Ptr = I.getPointerOperand();
87 if (!UA->isUniform(Ptr))
88 return;
89 Instruction *PtrI = dyn_cast<Instruction>(Ptr);
90 if (PtrI)
91 setUniformMetadata(PtrI);
92
93 // We're tracking up to the Function boundaries, and cannot go beyond because
94 // of FunctionPass restrictions. We can ensure that is memory not clobbered
95 // for memory operations that are live in to entry points only.
96 if (!isEntryFunc)
97 return;
98 bool GlobalLoad = I.getPointerAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
99 if (GlobalLoad && !AMDGPU::isClobberedInFunction(&I, MSSA, AA))
100 setNoClobberMetadata(&I);
101}
102
103bool AMDGPUAnnotateUniformValues::doInitialization(Module &M) {
104 return false;
105}
106
107bool AMDGPUAnnotateUniformValues::runOnFunction(Function &F) {
108 if (skipFunction(F))
109 return false;
110
111 UA = &getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
112 MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
113 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
114 isEntryFunc = AMDGPU::isEntryFunctionCC(F.getCallingConv());
115
116 Changed = false;
117 visit(F);
118 return Changed;
119}
120
123 return new AMDGPUAnnotateUniformValues();
124}
Add AMDGPU uniform metadata
#define DEBUG_TYPE
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
LLVM IR instance of the generic uniformity analysis.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Conditional or Unconditional Branch instruction.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Base class for instruction visitors.
Definition: InstVisitor.h:78
RetTy visitBranchInst(BranchInst &I)
Definition: InstVisitor.h:229
RetTy visitLoadInst(LoadInst &I)
Definition: InstVisitor.h:169
An instruction for reading from memory.
Definition: Instructions.h:184
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:980
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:700
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
Definition: Pass.h:119
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Legacy analysis pass which computes a CycleInfo.
LLVM Value Representation.
Definition: Value.h:74
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
bool isEntryFunctionCC(CallingConv::ID CC)
bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA, AAResults *AA)
Check is a Load is clobbered in its function.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createAMDGPUAnnotateUniformValues()
@ Add
Sum of integers.
T uniform(GenT &Gen, T Min, T Max)
Return a uniformly distributed random value between Min and Max.
Definition: Random.h:21