LLVM 24.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"
16#include "AMDGPUMemoryUtils.h"
21#include "llvm/IR/InstVisitor.h"
23
24#define DEBUG_TYPE "amdgpu-annotate-uniform"
25
26using namespace llvm;
27
28namespace {
29
30class AMDGPUAnnotateUniformValues
31 : public InstVisitor<AMDGPUAnnotateUniformValues> {
33 MemorySSA *MSSA;
35 bool isEntryFunc;
36 bool Changed = false;
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 AMDGPUAnnotateUniformValues(UniformityInfo &UA, MemorySSA &MSSA,
50 AliasAnalysis &AA, const Function &F)
51 : UA(&UA), MSSA(&MSSA), AA(&AA),
52 isEntryFunc(AMDGPU::isEntryFunctionCC(F.getCallingConv())) {}
53
54 void visitCondBrInst(CondBrInst &I);
55 void visitLoadInst(LoadInst &I);
56
57 bool changed() const { return Changed; }
58};
59
60} // End anonymous namespace
61
62void AMDGPUAnnotateUniformValues::visitCondBrInst(CondBrInst &I) {
63 if (UA->isUniformTerminator(&I))
64 setUniformMetadata(&I);
65}
66
67void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
68 Value *Ptr = I.getPointerOperand();
69 if (UA->isDivergentAtDef(Ptr))
70 return;
72 if (PtrI)
73 setUniformMetadata(PtrI);
74
75 // We're tracking up to the Function boundaries, and cannot go beyond because
76 // of FunctionPass restrictions. We can ensure that is memory not clobbered
77 // for memory operations that are live in to entry points only.
78 if (!isEntryFunc)
79 return;
80
81 // If I is atomic or volatile, it might see the effects of concurrent
82 // clobbering accesses, so local clobber analysis is not sufficient to ensure
83 // that it sees the initial value.
84 if (!I.isSimple())
85 return;
86
87 bool GlobalLoad = I.getPointerAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
88 if (GlobalLoad && !AMDGPU::isClobberedInFunction(&I, MSSA, AA))
89 setNoClobberMetadata(&I);
90}
91
92PreservedAnalyses
96 MemorySSA &MSSA = FAM.getResult<MemorySSAAnalysis>(F).getMSSA();
97 AAResults &AA = FAM.getResult<AAManager>(F);
98
99 AMDGPUAnnotateUniformValues Impl(UI, MSSA, AA, F);
100 Impl.visit(F);
101
102 if (!Impl.changed())
103 return PreservedAnalyses::all();
104
106 // TODO: Should preserve nearly everything
108 return PA;
109}
110
112public:
113 static char ID;
114
116
117 bool doInitialization(Module &M) override { return false; }
118
119 bool runOnFunction(Function &F) override;
120 StringRef getPassName() const override {
121 return "AMDGPU Annotate Uniform Values";
122 }
123
130};
131
133 if (skipFunction(F))
134 return false;
135
136 UniformityInfo &UI =
137 getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
140
141 AMDGPUAnnotateUniformValues Impl(UI, MSSA, AA, F);
142 Impl.visit(F);
143 return Impl.changed();
144}
145
147 "Add AMDGPU uniform metadata", false, false)
152 "Add AMDGPU uniform metadata", false, false)
153
155
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
LLVM IR instance of the generic uniformity analysis.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
A manager for alias analyses.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Conditional Branch instruction.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
FunctionPass(char &pid)
Definition Pass.h:314
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition Pass.cpp:197
bool isDivergentAtDef(ConstValueRefT V) const
Whether V is divergent at its definition.
bool isUniformTerminator(const InstructionT *I) const
Base class for instruction visitors.
Definition InstVisitor.h:78
An instruction for reading from memory.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1588
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
Legacy analysis pass which computes MemorySSA.
Definition MemorySSA.h:989
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition MemorySSA.h:689
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:152
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Analysis pass which computes UniformityInfo.
Legacy analysis pass which computes a CycleInfo.
Abstract Attribute helper functions.
Definition Attributor.h:166
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA, AAResults *AA)
Check is a Load is clobbered in its function.
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< SSAContext > UniformityInfo
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
FunctionPass * createAMDGPUAnnotateUniformValuesLegacy()
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.