1//===- AMDGPURewriteOutArgumentsPass.cpp - Create struct returns ----------===//
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 This pass attempts to replace out argument usage with a return of a
10/// struct.
11///
12/// We can support returning a lot of values directly in registers, but
13/// idiomatic C code frequently uses a pointer argument to return a second value
14/// rather than returning a struct by value. GPU stack access is also quite
15/// painful, so we want to avoid that if possible. Passing a stack object
16/// pointer to a function also requires an additional address expansion code
17/// sequence to convert the pointer to be relative to the kernel's scratch wave
18/// offset register since the callee doesn't know what stack frame the incoming
19/// pointer is relative to.
20///
21/// The goal is to try rewriting code that looks like this:
22///
23/// int foo(int a, int b, int* out) {
24/// *out = bar();
25/// return a + b;
26/// }
27///
28/// into something like this:
29///
30/// std::pair<int, int> foo(int a, int b) {
31/// return std::pair(a + b, bar());
32/// }
33///
34/// Typically the incoming pointer is a simple alloca for a temporary variable
35/// to use the API, which if replaced with a struct return will be easily SROA'd
36/// out when the stub function we create is inlined
37///
38/// This pass introduces the struct return, but leaves the unused pointer
39/// arguments and introduces a new stub function calling the struct returning
40/// body. DeadArgumentElimination should be run after this to clean these up.
41//
42//===----------------------------------------------------------------------===//
43
44#include "AMDGPU.h"
45#include "Utils/AMDGPUBaseInfo.h"
46#include "llvm/ADT/Statistic.h"
47#include "llvm/Analysis/MemorySSA.h"
48#include "llvm/Analysis/MemorySSAUpdater.h"
49#include "llvm/IR/AttributeMask.h"
50#include "llvm/IR/Attributes.h"
51#include "llvm/IR/IRBuilder.h"
52#include "llvm/IR/Instructions.h"
53#include "llvm/InitializePasses.h"
54#include "llvm/Pass.h"
55#include "llvm/Support/CommandLine.h"
56#include "llvm/Support/Debug.h"
57#include "llvm/Support/raw_ostream.h"
58
59#define DEBUG_TYPE "amdgpu-rewrite-out-arguments"
60
61using namespace llvm;
62
63static cl::opt<bool> AnyAddressSpace(
64 "amdgpu-any-address-space-out-arguments",
65 cl::desc("Replace pointer out arguments with "
66 "struct returns for non-private address space"),
67 cl::Hidden,
68 cl::init(Val: false));
69
70static cl::opt<unsigned> MaxNumRetRegs(
71 "amdgpu-max-return-arg-num-regs",
72 cl::desc("Approximately limit number of return registers for replacing out arguments"),
73 cl::Hidden,
74 cl::init(Val: 16));
75
76STATISTIC(NumOutArgumentsReplaced,
77 "Number out arguments moved to struct return values");
78STATISTIC(NumOutArgumentFunctionsReplaced,
79 "Number of functions with out arguments moved to struct return values");
80
81namespace {
82
83class AMDGPURewriteOutArguments : public FunctionPass {
84private:
85 const DataLayout *DL = nullptr;
86 MemorySSA *MSSA = nullptr;
87 MemorySSAUpdater *MSSAU = nullptr;
88 AAResults *AA = nullptr;
89
90 Type *getStoredType(Value &Arg) const;
91 Type *getOutArgumentType(Argument &Arg) const;
92
93public:
94 static char ID;
95
96 AMDGPURewriteOutArguments() : FunctionPass(ID) {}
97
98 void getAnalysisUsage(AnalysisUsage &AU) const override {
99 AU.addRequired<MemorySSAWrapperPass>();
100 AU.addRequired<AAResultsWrapperPass>();
101 FunctionPass::getAnalysisUsage(AU);
102 }
103
104 bool doInitialization(Module &M) override;
105 bool runOnFunction(Function &F) override;
106};
107
108} // end anonymous namespace
109
110INITIALIZE_PASS_BEGIN(AMDGPURewriteOutArguments, DEBUG_TYPE,
111 "AMDGPU Rewrite Out Arguments", false, false)
112INITIALIZE_PASS_DEPENDENCY(MemorySSAWrapperPass)
113INITIALIZE_PASS_END(AMDGPURewriteOutArguments, DEBUG_TYPE,
114 "AMDGPU Rewrite Out Arguments", false, false)
115
116char AMDGPURewriteOutArguments::ID = 0;
117
118Type *AMDGPURewriteOutArguments::getStoredType(Value &Arg) const {
119 const int MaxUses = 10;
120 int UseCount = 0;
121
122 SmallVector<Use *> Worklist(llvm::make_pointer_range(Range: Arg.uses()));
123
124 Type *StoredType = nullptr;
125 while (!Worklist.empty()) {
126 Use *U = Worklist.pop_back_val();
127
128 if (auto *BCI = dyn_cast<BitCastInst>(Val: U->getUser())) {
129 for (Use &U : BCI->uses())
130 Worklist.push_back(Elt: &U);
131 continue;
132 }
133
134 if (auto *SI = dyn_cast<StoreInst>(Val: U->getUser())) {
135 if (UseCount++ > MaxUses)
136 return nullptr;
137
138 if (!SI->isSimple() ||
139 U->getOperandNo() != StoreInst::getPointerOperandIndex())
140 return nullptr;
141
142 if (StoredType && StoredType != SI->getValueOperand()->getType())
143 return nullptr; // More than one type.
144 StoredType = SI->getValueOperand()->getType();
145 continue;
146 }
147
148 // Unsupported user.
149 return nullptr;
150 }
151
152 return StoredType;
153}
154
155Type *AMDGPURewriteOutArguments::getOutArgumentType(Argument &Arg) const {
156 const unsigned MaxOutArgSizeBytes = 4 * MaxNumRetRegs;
157 PointerType *ArgTy = dyn_cast<PointerType>(Val: Arg.getType());
158
159 // TODO: It might be useful for any out arguments, not just privates.
160 if (!ArgTy || (ArgTy->getAddressSpace() != DL->getAllocaAddrSpace() &&
161 !AnyAddressSpace) ||
162 Arg.hasByValAttr() || Arg.hasStructRetAttr()) {
163 return nullptr;
164 }
165
166 Type *StoredType = getStoredType(Arg);
167 if (!StoredType || DL->getTypeStoreSize(Ty: StoredType) > MaxOutArgSizeBytes)
168 return nullptr;
169
170 return StoredType;
171}
172
173bool AMDGPURewriteOutArguments::doInitialization(Module &M) {
174 DL = &M.getDataLayout();
175 return false;
176}
177
178static StoreInst *findStoreForOutArgument(BasicBlock *BB, Argument *OutArg,
179 MemorySSA &MSSA,
180 BatchAAResults &BAA) {
181 MemoryLocation ArgLoc = MemoryLocation::getBeforeOrAfter(Ptr: OutArg);
182 const auto *Accesses = MSSA.getBlockAccesses(BB);
183 if (!Accesses)
184 return nullptr;
185
186 for (const MemoryAccess &Access : reverse(C: *Accesses)) {
187 const auto *UseOrDef = dyn_cast<MemoryUseOrDef>(Val: &Access);
188 if (!UseOrDef)
189 continue;
190
191 Instruction *I = UseOrDef->getMemoryInst();
192
193 // Return the must-alias store to the out argument.
194 if (auto *Store = dyn_cast<StoreInst>(Val: I))
195 if (Store->getPointerOperand() == OutArg)
196 return Store;
197
198 if (auto *FI = dyn_cast<FenceInst>(Val: I))
199 if (FI->getOrdering() == AtomicOrdering::Release)
200 continue;
201
202 if (auto *LI = dyn_cast<LoadInst>(Val: I)) {
203 if (LI->isAtomic()) {
204 // May-alias reads with monotonic ordering are ignored.
205 if (isStrongerThan(AO: LI->getOrdering(), Other: AtomicOrdering::Monotonic))
206 return nullptr;
207 continue;
208 }
209 }
210
211 // Any other memory access that may read or write the location prevents the
212 // rewrite.
213 if (isModOrRefSet(MRI: BAA.getModRefInfo(I, OptLoc: ArgLoc)))
214 return nullptr;
215 }
216
217 return nullptr;
218}
219
220bool AMDGPURewriteOutArguments::runOnFunction(Function &F) {
221 if (skipFunction(F))
222 return false;
223
224 // TODO: Could probably handle variadic functions.
225 if (F.isVarArg() || F.hasStructRetAttr() ||
226 AMDGPU::isEntryFunctionCC(CC: F.getCallingConv()))
227 return false;
228
229 unsigned ReturnNumRegs = 0;
230 // Maps an out-argument number to its field index in the return struct.
231 // Fields are in processing order, which the retry loop below can reorder
232 // relative to argument order, so the index must be tracked, not recomputed.
233 SmallDenseMap<unsigned, unsigned, 4> OutArgIndexes;
234 SmallVector<Type *, 4> ReturnTypes;
235 Type *RetTy = F.getReturnType();
236 if (!RetTy->isVoidTy()) {
237 ReturnNumRegs = DL->getTypeStoreSize(Ty: RetTy) / 4;
238
239 if (ReturnNumRegs >= MaxNumRetRegs)
240 return false;
241
242 ReturnTypes.push_back(Elt: RetTy);
243 }
244
245 SmallVector<std::pair<Argument *, Type *>, 4> OutArgs;
246 for (Argument &Arg : F.args()) {
247 if (Type *Ty = getOutArgumentType(Arg)) {
248 LLVM_DEBUG(dbgs() << "Found possible out argument " << Arg
249 << " in function " << F.getName() << '\n');
250 OutArgs.push_back(Elt: {&Arg, Ty});
251 }
252 }
253
254 if (OutArgs.empty())
255 return false;
256
257 using ReplacementVec = SmallVector<std::pair<Argument *, Value *>, 4>;
258
259 DenseMap<ReturnInst *, ReplacementVec> Replacements;
260
261 SmallVector<ReturnInst *, 4> Returns;
262 for (BasicBlock &BB : F) {
263 if (ReturnInst *RI = dyn_cast<ReturnInst>(Val: &BB.back()))
264 Returns.push_back(Elt: RI);
265 }
266
267 if (Returns.empty())
268 return false;
269
270 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
271 MSSA = &getAnalysis<MemorySSAWrapperPass>().getMSSA();
272
273 BatchAAResults BatchAA(*AA);
274 MemorySSAUpdater MSSAUpdater(MSSA);
275 MSSAU = &MSSAUpdater;
276
277 bool Changing;
278
279 do {
280 Changing = false;
281
282 // Keep retrying if we are able to successfully eliminate an argument. This
283 // helps with cases with multiple arguments which may alias, such as in a
284 // sincos implementation. With 2 stores to may-aliasing arguments, MDA
285 // returns the second store for the first argument too; the identity guard
286 // below rejects it, and a later iteration folds the first argument once the
287 // second store has been removed.
288 for (const auto &Pair : OutArgs) {
289 bool ThisReplaceable = true;
290 SmallVector<std::pair<ReturnInst *, StoreInst *>, 4> ReplaceableStores;
291
292 Argument *OutArg = Pair.first;
293 Type *ArgTy = Pair.second;
294
295 // Skip this argument if converting it will push us over the register
296 // count to return limit.
297
298 // TODO: This is an approximation. When legalized this could be more. We
299 // can ask TLI for exactly how many.
300 unsigned ArgNumRegs = DL->getTypeStoreSize(Ty: ArgTy) / 4;
301 if (ArgNumRegs + ReturnNumRegs > MaxNumRetRegs)
302 continue;
303
304 // An argument is convertible only if all exit blocks are able to replace
305 // it.
306 for (ReturnInst *RI : Returns) {
307 BasicBlock *BB = RI->getParent();
308
309 StoreInst *SI = findStoreForOutArgument(BB, OutArg, MSSA&: *MSSA, BAA&: BatchAA);
310 if (SI) {
311 LLVM_DEBUG(dbgs() << "Found out argument store: " << *SI << '\n');
312 ReplaceableStores.emplace_back(Args&: RI, Args&: SI);
313 } else {
314 ThisReplaceable = false;
315 break;
316 }
317 }
318
319 if (!ThisReplaceable)
320 continue; // Try the next argument candidate.
321
322 for (std::pair<ReturnInst *, StoreInst *> Store : ReplaceableStores) {
323 Value *ReplVal = Store.second->getValueOperand();
324
325 auto &ValVec = Replacements[Store.first];
326 if (llvm::is_contained(Range: llvm::make_first_range(c&: ValVec), Element: OutArg)) {
327 LLVM_DEBUG(dbgs()
328 << "Saw multiple out arg stores" << *OutArg << '\n');
329 // It is possible to see stores to the same argument multiple times,
330 // but we expect these would have been optimized out already.
331 ThisReplaceable = false;
332 break;
333 }
334
335 ValVec.emplace_back(Args&: OutArg, Args&: ReplVal);
336 MSSAU->removeMemoryAccess(I: Store.second);
337 Store.second->eraseFromParent();
338 }
339
340 if (ThisReplaceable) {
341 OutArgIndexes.insert(KV: {OutArg->getArgNo(), ReturnTypes.size()});
342 ReturnTypes.push_back(Elt: ArgTy);
343 ++NumOutArgumentsReplaced;
344 Changing = true;
345 }
346 }
347 } while (Changing);
348
349 if (Replacements.empty())
350 return false;
351
352 LLVMContext &Ctx = F.getContext();
353 StructType *NewRetTy = StructType::create(Context&: Ctx, Elements: ReturnTypes, Name: F.getName());
354
355 FunctionType *NewFuncTy = FunctionType::get(Result: NewRetTy,
356 Params: F.getFunctionType()->params(),
357 isVarArg: F.isVarArg());
358
359 LLVM_DEBUG(dbgs() << "Computed new return type: " << *NewRetTy << '\n');
360
361 Function *NewFunc = Function::Create(Ty: NewFuncTy, Linkage: Function::PrivateLinkage,
362 N: F.getName() + ".body");
363 F.getParent()->getFunctionList().insert(where: F.getIterator(), New: NewFunc);
364 NewFunc->copyAttributesFrom(Src: &F);
365 NewFunc->setComdat(F.getComdat());
366
367 // We want to preserve the function and param attributes, but need to strip
368 // off any return attributes, e.g. zeroext doesn't make sense with a struct.
369 NewFunc->stealArgumentListFrom(Src&: F);
370
371 NewFunc->removeRetAttrs(Attrs: AttributeFuncs::typeIncompatible(
372 Ty: NewRetTy, AS: NewFunc->getAttributes().getRetAttrs()));
373 // TODO: How to preserve metadata?
374
375 // Move the body of the function into the new rewritten function, and replace
376 // this function with a stub.
377 NewFunc->splice(ToIt: NewFunc->begin(), FromF: &F);
378
379 for (auto &Replacement : Replacements) {
380 ReturnInst *RI = Replacement.first;
381 IRBuilder<> B(RI);
382 B.SetCurrentDebugLocation(RI->getDebugLoc());
383
384 Value *NewRetVal = PoisonValue::get(T: NewRetTy);
385
386 Value *RetVal = RI->getReturnValue();
387 if (RetVal)
388 NewRetVal = B.CreateInsertValue(Agg: NewRetVal, Val: RetVal, Idxs: 0);
389
390 // Use OutArgIndexes so body and stub agree on the field for each argument.
391 for (std::pair<Argument *, Value *> ReturnPoint : Replacement.second) {
392 unsigned FieldIdx = OutArgIndexes.lookup(Val: ReturnPoint.first->getArgNo());
393 NewRetVal = B.CreateInsertValue(Agg: NewRetVal, Val: ReturnPoint.second, Idxs: FieldIdx);
394 }
395
396 if (RetVal)
397 RI->setOperand(i_nocapture: 0, Val_nocapture: NewRetVal);
398 else {
399 B.CreateRet(V: NewRetVal);
400 RI->eraseFromParent();
401 }
402 }
403
404 SmallVector<Value *, 16> StubCallArgs;
405 for (Argument &Arg : F.args()) {
406 if (OutArgIndexes.count(Val: Arg.getArgNo())) {
407 // It's easier to preserve the type of the argument list. We rely on
408 // DeadArgumentElimination to take care of these.
409 StubCallArgs.push_back(Elt: PoisonValue::get(T: Arg.getType()));
410 } else {
411 StubCallArgs.push_back(Elt: &Arg);
412 }
413 }
414
415 BasicBlock *StubBB = BasicBlock::Create(Context&: Ctx, Name: "", Parent: &F);
416 IRBuilder<> B(StubBB);
417 CallInst *StubCall = B.CreateCall(Callee: NewFunc, Args: StubCallArgs);
418
419 for (Argument &Arg : F.args()) {
420 auto It = OutArgIndexes.find(Val: Arg.getArgNo());
421 if (It == OutArgIndexes.end())
422 continue;
423
424 unsigned FieldIdx = It->second;
425 Type *EltTy = NewRetTy->getElementType(N: FieldIdx);
426 const auto Align =
427 DL->getValueOrABITypeAlignment(Alignment: Arg.getParamAlign(), Ty: EltTy);
428
429 Value *Val = B.CreateExtractValue(Agg: StubCall, Idxs: FieldIdx);
430 B.CreateAlignedStore(Val, Ptr: &Arg, Align);
431 }
432
433 if (!RetTy->isVoidTy()) {
434 B.CreateRet(V: B.CreateExtractValue(Agg: StubCall, Idxs: 0));
435 } else {
436 B.CreateRetVoid();
437 }
438
439 // The function is now a stub we want to inline.
440 F.addFnAttr(Kind: Attribute::AlwaysInline);
441
442 ++NumOutArgumentFunctionsReplaced;
443 return true;
444}
445
446FunctionPass *llvm::createAMDGPURewriteOutArgumentsPass() {
447 return new AMDGPURewriteOutArguments();
448}
449