LLVM 17.0.0git
AMDGPULowerIntrinsics.cpp
Go to the documentation of this file.
1//===-- AMDGPULowerIntrinsics.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#include "AMDGPU.h"
10#include "AMDGPUSubtarget.h"
13#include "llvm/IR/Constants.h"
16#include "llvm/IR/IntrinsicsR600.h"
17#include "llvm/IR/Module.h"
21
22#define DEBUG_TYPE "amdgpu-lower-intrinsics"
23
24using namespace llvm;
25
26namespace {
27
28static int MaxStaticSize;
29
30static cl::opt<int, true> MemIntrinsicExpandSizeThresholdOpt(
31 "amdgpu-mem-intrinsic-expand-size",
32 cl::desc("Set minimum mem intrinsic size to expand in IR"),
33 cl::location(MaxStaticSize),
34 cl::init(1024),
36
37
38class AMDGPULowerIntrinsics : public ModulePass {
39private:
40 bool makeLIDRangeMetadata(Function &F) const;
41
42public:
43 static char ID;
44
45 AMDGPULowerIntrinsics() : ModulePass(ID) {}
46
47 bool runOnModule(Module &M) override;
48 bool expandMemIntrinsicUses(Function &F);
49 StringRef getPassName() const override {
50 return "AMDGPU Lower Intrinsics";
51 }
52
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 }
56};
57
58}
59
60char AMDGPULowerIntrinsics::ID = 0;
61
62char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID;
63
64INITIALIZE_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE, "Lower intrinsics", false,
65 false)
66
67// TODO: Should refine based on estimated number of accesses (e.g. does it
68// require splitting based on alignment)
69static bool shouldExpandOperationWithSize(Value *Size) {
70 ConstantInt *CI = dyn_cast<ConstantInt>(Size);
71 return !CI || (CI->getSExtValue() > MaxStaticSize);
72}
73
74bool AMDGPULowerIntrinsics::expandMemIntrinsicUses(Function &F) {
75 Intrinsic::ID ID = F.getIntrinsicID();
76 bool Changed = false;
77
78 for (User *U : llvm::make_early_inc_range(F.users())) {
79 Instruction *Inst = cast<Instruction>(U);
80
81 switch (ID) {
82 case Intrinsic::memcpy: {
83 auto *Memcpy = cast<MemCpyInst>(Inst);
84 if (shouldExpandOperationWithSize(Memcpy->getLength())) {
85 Function *ParentFunc = Memcpy->getParent()->getParent();
87 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*ParentFunc);
88 expandMemCpyAsLoop(Memcpy, TTI);
89 Changed = true;
90 Memcpy->eraseFromParent();
91 }
92
93 break;
94 }
95 case Intrinsic::memmove: {
96 auto *Memmove = cast<MemMoveInst>(Inst);
97 if (shouldExpandOperationWithSize(Memmove->getLength())) {
98 expandMemMoveAsLoop(Memmove);
99 Changed = true;
100 Memmove->eraseFromParent();
101 }
102
103 break;
104 }
105 case Intrinsic::memset: {
106 auto *Memset = cast<MemSetInst>(Inst);
107 if (shouldExpandOperationWithSize(Memset->getLength())) {
108 expandMemSetAsLoop(Memset);
109 Changed = true;
110 Memset->eraseFromParent();
111 }
112
113 break;
114 }
115 default:
116 break;
117 }
118 }
119
120 return Changed;
121}
122
123bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const {
124 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
125 if (!TPC)
126 return false;
127
128 const TargetMachine &TM = TPC->getTM<TargetMachine>();
129 bool Changed = false;
130
131 for (auto *U : F.users()) {
132 auto *CI = dyn_cast<CallInst>(U);
133 if (!CI)
134 continue;
135
136 Function *Caller = CI->getParent()->getParent();
137 const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, *Caller);
138 Changed |= ST.makeLIDRangeMetadata(CI);
139 }
140 return Changed;
141}
142
143bool AMDGPULowerIntrinsics::runOnModule(Module &M) {
144 bool Changed = false;
145
146 for (Function &F : M) {
147 if (!F.isDeclaration())
148 continue;
149
150 switch (F.getIntrinsicID()) {
151 case Intrinsic::memcpy:
152 case Intrinsic::memmove:
153 case Intrinsic::memset:
154 if (expandMemIntrinsicUses(F))
155 Changed = true;
156 break;
157
158 case Intrinsic::r600_read_tidig_x:
159 case Intrinsic::r600_read_tidig_y:
160 case Intrinsic::r600_read_tidig_z:
161 case Intrinsic::r600_read_local_size_x:
162 case Intrinsic::r600_read_local_size_y:
163 case Intrinsic::r600_read_local_size_z:
164 Changed |= makeLIDRangeMetadata(F);
165 break;
166
167 default:
168 break;
169 }
170 }
171
172 return Changed;
173}
174
176 return new AMDGPULowerIntrinsics();
177}
#define DEBUG_TYPE
Base class for AMDGPU specific classes of TargetSubtarget.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
uint64_t Size
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Target-Independent Code Generator Pass Configuration Options pass.
This pass exposes codegen information to IR-level passes.
static const AMDGPUSubtarget & get(const MachineFunction &MF)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
This is the shared class of boolean and integer constants.
Definition: Constants.h:78
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:151
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:652
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
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 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
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
Wrapper pass for TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM Value Representation.
Definition: Value.h:74
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
LocationClass< Ty > location(Ty &L)
Definition: CommandLine.h:465
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:748
ModulePass * createAMDGPULowerIntrinsicsPass()
void expandMemMoveAsLoop(MemMoveInst *MemMove)
Expand MemMove as a loop. MemMove is not deleted.
char & AMDGPULowerIntrinsicsID
void expandMemCpyAsLoop(MemCpyInst *MemCpy, const TargetTransformInfo &TTI, ScalarEvolution *SE=nullptr)
Expand MemCpy as a loop. MemCpy is not deleted.
void expandMemSetAsLoop(MemSetInst *MemSet)
Expand MemSet as a loop. MemSet is not deleted.