LLVM  15.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 "Utils/AMDGPUBaseInfo.h"
21 #include "llvm/IR/InstVisitor.h"
22 #include "llvm/InitializePasses.h"
23 
24 #define DEBUG_TYPE "amdgpu-annotate-uniform"
25 
26 using namespace llvm;
27 
28 namespace {
29 
30 class AMDGPUAnnotateUniformValues : public FunctionPass,
31  public InstVisitor<AMDGPUAnnotateUniformValues> {
33  MemorySSA *MSSA;
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 
48 public:
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 
64  void visitBranchInst(BranchInst &I);
65  void visitLoadInst(LoadInst &I);
66 };
67 
68 } // End anonymous namespace
69 
70 INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
71  "Add AMDGPU uniform metadata", false, false)
75 INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues, DEBUG_TYPE,
77 
78 char AMDGPUAnnotateUniformValues::ID = 0;
79 
80 void AMDGPUAnnotateUniformValues::visitBranchInst(BranchInst &I) {
81  if (DA->isUniform(&I))
82  setUniformMetadata(&I);
83 }
84 
85 void AMDGPUAnnotateUniformValues::visitLoadInst(LoadInst &I) {
86  Value *Ptr = I.getPointerOperand();
87  if (!DA->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 
103 bool AMDGPUAnnotateUniformValues::doInitialization(Module &M) {
104  return false;
105 }
106 
108  if (skipFunction(F))
109  return false;
110 
111  DA = &getAnalysis<LegacyDivergenceAnalysis>();
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 
121 FunctionPass *
123  return new AMDGPUAnnotateUniformValues();
124 }
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::Function
Definition: Function.h:60
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1300
F
#define F(x, y, z)
Definition: MD5.cpp:55
AliasAnalysis.h
AMDGPUMemoryUtils.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(AMDGPUAnnotateUniformValues, DEBUG_TYPE, "Add AMDGPU uniform metadata", false, false) INITIALIZE_PASS_END(AMDGPUAnnotateUniformValues
llvm::MemorySSAWrapperPass
Legacy analysis pass which computes MemorySSA.
Definition: MemorySSA.h:998
llvm::AAResults
Definition: AliasAnalysis.h:511
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::createAMDGPUAnnotateUniformValues
FunctionPass * createAMDGPUAnnotateUniformValues()
Definition: AMDGPUAnnotateUniformValues.cpp:122
false
Definition: StackSlotColoring.cpp:141
llvm::M68kBeads::DA
@ DA
Definition: M68kBaseInfo.h:59
metadata
Add AMDGPU uniform metadata
Definition: AMDGPUAnnotateUniformValues.cpp:76
AMDGPU
Definition: AMDGPUReplaceLDSUseWithPointer.cpp:114
llvm::Instruction
Definition: Instruction.h:42
llvm::AMDGPU::isClobberedInFunction
bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA, AAResults *AA)
Check is a Load is clobbered in its function.
Definition: AMDGPUMemoryUtils.cpp:171
llvm::LegacyDivergenceAnalysis
Definition: LegacyDivergenceAnalysis.h:31
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::AMDGPU::isEntryFunctionCC
bool isEntryFunctionCC(CallingConv::ID CC)
Definition: AMDGPUBaseInfo.cpp:1639
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::uniform
T uniform(GenT &Gen, T Min, T Max)
Return a uniformly distributed random value between Min and Max.
Definition: Random.h:21
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
llvm::MemorySSA
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:714
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::AMDGPUAS::GLOBAL_ADDRESS
@ GLOBAL_ADDRESS
Address space for global memory (RAT0, VTX0).
Definition: AMDGPU.h:359
AMDGPU.h
InstVisitor.h
llvm::InstVisitor
Base class for instruction visitors.
Definition: InstVisitor.h:78
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:173
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
AA
MemorySSA.h
LegacyDivergenceAnalysis.h
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:1351
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition: Instructions.h:3086
InitializePasses.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
DEBUG_TYPE
#define DEBUG_TYPE
Definition: AMDGPUAnnotateUniformValues.cpp:24
AMDGPUBaseInfo.h
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38