LLVM 20.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;
34 AliasAnalysis *AA;
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
56
57 bool changed() const { return Changed; }
58};
59
60} // End anonymous namespace
61
62void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst &I) {
63 if (UA->isUniform(&I))
64 setUniformMetadata(&I);
65}
66
67void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
68 Value *Ptr = I.getPointerOperand();
69 if (!UA->isUniform(Ptr))
70 return;
71 Instruction *PtrI = dyn_cast<Instruction>(Ptr);
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 bool GlobalLoad = I.getPointerAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS;
81 if (GlobalLoad && !AMDGPU::isClobberedInFunction(&I, MSSA, AA))
82 setNoClobberMetadata(&I);
83}
84
89 MemorySSA &MSSA = FAM.getResult<MemorySSAAnalysis>(F).getMSSA();
91
92 AMDGPUAnnotateUniformValues Impl(UI, MSSA, AA, F);
93 Impl.visit(F);
94
95 if (!Impl.changed())
97
99 // TODO: Should preserve nearly everything
101 return PA;
102}
103
105public:
106 static char ID;
107
109
110 bool doInitialization(Module &M) override { return false; }
111
112 bool runOnFunction(Function &F) override;
113 StringRef getPassName() const override {
114 return "AMDGPU Annotate Uniform Values";
115 }
116
117 void getAnalysisUsage(AnalysisUsage &AU) const override {
121 AU.setPreservesAll();
122 }
123};
124
126 if (skipFunction(F))
127 return false;
128
129 UniformityInfo &UI =
130 getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
131 MemorySSA &MSSA = getAnalysis<MemorySSAWrapperPass>().getMSSA();
132 AliasAnalysis &AA = getAnalysis<AAResultsWrapperPass>().getAAResults();
133
134 AMDGPUAnnotateUniformValues Impl(UI, MSSA, AA, F);
135 Impl.visit(F);
136 return Impl.changed();
137}
138
140 "Add AMDGPU uniform metadata", false, false)
146
148
151}
Add AMDGPU uniform metadata
static bool runOnFunction(Function &F, bool PostInlining)
#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...
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
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)
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
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.
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Base class for instruction visitors.
Definition: InstVisitor.h:78
RetTy visitBranchInst(BranchInst &I)
Definition: InstVisitor.h:232
RetTy visitLoadInst(LoadInst &I)
Definition: InstVisitor.h:169
An instruction for reading from memory.
Definition: Instructions.h:176
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1543
An analysis that produces MemorySSA for a function.
Definition: MemorySSA.h:928
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:985
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:701
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Analysis pass which computes UniformityInfo.
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.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Add
Sum of integers.
FunctionPass * createAMDGPUAnnotateUniformValuesLegacy()
T uniform(GenT &Gen, T Min, T Max)
Return a uniformly distributed random value between Min and Max.
Definition: Random.h:21