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