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 "Utils/AMDGPUMemoryUtils.h"
18 #include "llvm/Analysis/AliasAnalysis.h"
19 #include "llvm/Analysis/MemorySSA.h"
20 #include "llvm/Analysis/UniformityAnalysis.h"
21 #include "llvm/IR/InstVisitor.h"
22 #include "llvm/InitializePasses.h"
24 #define DEBUG_TYPE "amdgpu-annotate-uniform"
30 class AMDGPUAnnotateUniformValues
: public FunctionPass
,
31 public InstVisitor
<AMDGPUAnnotateUniformValues
> {
38 void setUniformMetadata(Instruction
*I
) {
39 I
->setMetadata("amdgpu.uniform", MDNode::get(I
->getContext(), {}));
43 void setNoClobberMetadata(Instruction
*I
) {
44 I
->setMetadata("amdgpu.noclobber", MDNode::get(I
->getContext(), {}));
50 AMDGPUAnnotateUniformValues() :
52 bool doInitialization(Module
&M
) override
;
53 bool runOnFunction(Function
&F
) override
;
54 StringRef
getPassName() const override
{
55 return "AMDGPU Annotate Uniform Values";
57 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
58 AU
.addRequired
<UniformityInfoWrapperPass
>();
59 AU
.addRequired
<MemorySSAWrapperPass
>();
60 AU
.addRequired
<AAResultsWrapperPass
>();
64 void visitBranchInst(BranchInst
&I
);
65 void visitLoadInst(LoadInst
&I
);
68 } // End anonymous namespace
70 INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues
, DEBUG_TYPE
,
71 "Add AMDGPU uniform metadata", false, false)
72 INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass
)
73 INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass
)
74 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass
)
75 INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues
, DEBUG_TYPE
,
76 "Add AMDGPU uniform metadata", false, false)
78 char AMDGPUAnnotateUniformValues::ID
= 0;
80 void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst
&I
) {
81 if (UA
->isUniform(&I
))
82 setUniformMetadata(&I
);
85 void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst
&I
) {
86 Value
*Ptr
= I
.getPointerOperand();
87 if (!UA
->isUniform(Ptr
))
89 Instruction
*PtrI
= dyn_cast
<Instruction
>(Ptr
);
91 setUniformMetadata(PtrI
);
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.
98 bool GlobalLoad
= I
.getPointerAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS
;
99 if (GlobalLoad
&& !AMDGPU::isClobberedInFunction(&I
, MSSA
, AA
))
100 setNoClobberMetadata(&I
);
103 bool AMDGPUAnnotateUniformValues::doInitialization(Module
&M
) {
107 bool AMDGPUAnnotateUniformValues::runOnFunction(Function
&F
) {
111 UA
= &getAnalysis
<UniformityInfoWrapperPass
>().getUniformityInfo();
112 MSSA
= &getAnalysis
<MemorySSAWrapperPass
>().getMSSA();
113 AA
= &getAnalysis
<AAResultsWrapperPass
>().getAAResults();
114 isEntryFunc
= AMDGPU::isEntryFunctionCC(F
.getCallingConv());
122 llvm::createAMDGPUAnnotateUniformValues() {
123 return new AMDGPUAnnotateUniformValues();