blob: 4c1dbd4c53046f44dd4ace78f9d529e553b52892 [file] [log] [blame]
Tom Stellard5cbb53c2014-11-03 19:49:05 +00001//===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tom Stellard5cbb53c2014-11-03 19:49:05 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This pass marks all internal functions as always_inline and creates
Alfred Huangf9b521f2017-06-15 23:02:55 +000011/// duplicates of all other functions and marks the duplicates as always_inline.
Tom Stellard5cbb53c2014-11-03 19:49:05 +000012//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
Matt Arsenaulta6801992018-07-10 14:03:41 +000016#include "AMDGPUTargetMachine.h"
17#include "Utils/AMDGPUBaseInfo.h"
18#include "llvm/ADT/SmallPtrSet.h"
Tom Stellard5cbb53c2014-11-03 19:49:05 +000019#include "llvm/IR/Module.h"
20#include "llvm/Transforms/Utils/Cloning.h"
21
22using namespace llvm;
23
24namespace {
25
Matt Arsenault1390af22017-09-21 07:00:48 +000026static cl::opt<bool> StressCalls(
27 "amdgpu-stress-function-calls",
28 cl::Hidden,
29 cl::desc("Force all functions to be noinline"),
30 cl::init(false));
31
Tom Stellard5cbb53c2014-11-03 19:49:05 +000032class AMDGPUAlwaysInline : public ModulePass {
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +000033 bool GlobalOpt;
34
Matt Arsenaulta6801992018-07-10 14:03:41 +000035 void recursivelyVisitUsers(GlobalValue &GV,
36 SmallPtrSetImpl<Function *> &FuncsToAlwaysInline);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000037public:
Matt Arsenault746e0652017-06-02 18:02:42 +000038 static char ID;
39
40 AMDGPUAlwaysInline(bool GlobalOpt = false) :
41 ModulePass(ID), GlobalOpt(GlobalOpt) { }
Tom Stellard5cbb53c2014-11-03 19:49:05 +000042 bool runOnModule(Module &M) override;
Matt Arsenaulta6801992018-07-10 14:03:41 +000043
44 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesAll();
46 }
Tom Stellard5cbb53c2014-11-03 19:49:05 +000047};
48
49} // End anonymous namespace
50
Matt Arsenault746e0652017-06-02 18:02:42 +000051INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline",
52 "AMDGPU Inline All Functions", false, false)
53
Tom Stellard5cbb53c2014-11-03 19:49:05 +000054char AMDGPUAlwaysInline::ID = 0;
55
Matt Arsenaulta6801992018-07-10 14:03:41 +000056void AMDGPUAlwaysInline::recursivelyVisitUsers(
57 GlobalValue &GV,
58 SmallPtrSetImpl<Function *> &FuncsToAlwaysInline) {
59 SmallVector<User *, 16> Stack;
60
61 SmallPtrSet<const Value *, 8> Visited;
62
63 for (User *U : GV.users())
64 Stack.push_back(U);
65
66 while (!Stack.empty()) {
67 User *U = Stack.pop_back_val();
68 if (!Visited.insert(U).second)
69 continue;
70
71 if (Instruction *I = dyn_cast<Instruction>(U)) {
72 Function *F = I->getParent()->getParent();
73 if (!AMDGPU::isEntryFunctionCC(F->getCallingConv())) {
74 FuncsToAlwaysInline.insert(F);
75 Stack.push_back(F);
76 }
77
78 // No need to look at further users, but we do need to inline any callers.
79 continue;
80 }
81
82 for (User *UU : U->users())
83 Stack.push_back(UU);
84 }
85}
86
Tom Stellard5cbb53c2014-11-03 19:49:05 +000087bool AMDGPUAlwaysInline::runOnModule(Module &M) {
Nikolay Haustoveba80892016-08-31 11:18:33 +000088 std::vector<GlobalAlias*> AliasesToRemove;
Matt Arsenaulta6801992018-07-10 14:03:41 +000089
90 SmallPtrSet<Function *, 8> FuncsToAlwaysInline;
91 SmallPtrSet<Function *, 8> FuncsToNoInline;
Tom Stellard5cbb53c2014-11-03 19:49:05 +000092
Nikolay Haustoveba80892016-08-31 11:18:33 +000093 for (GlobalAlias &A : M.aliases()) {
94 if (Function* F = dyn_cast<Function>(A.getAliasee())) {
95 A.replaceAllUsesWith(F);
96 AliasesToRemove.push_back(&A);
97 }
Matt Arsenaulta6801992018-07-10 14:03:41 +000098
99 // FIXME: If the aliasee isn't a function, it's some kind of constant expr
100 // cast that won't be inlined through.
Nikolay Haustoveba80892016-08-31 11:18:33 +0000101 }
102
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +0000103 if (GlobalOpt) {
104 for (GlobalAlias* A : AliasesToRemove) {
105 A->eraseFromParent();
106 }
Nikolay Haustoveba80892016-08-31 11:18:33 +0000107 }
108
Matt Arsenaulta6801992018-07-10 14:03:41 +0000109 // Always force inlining of any function that uses an LDS global address. This
110 // is something of a workaround because we don't have a way of supporting LDS
111 // objects defined in functions. LDS is always allocated by a kernel, and it
112 // is difficult to manage LDS usage if a function may be used by multiple
113 // kernels.
114 //
115 // OpenCL doesn't allow declaring LDS in non-kernels, so in practice this
116 // should only appear when IPO passes manages to move LDs defined in a kernel
117 // into a single user function.
Matt Arsenault1390af22017-09-21 07:00:48 +0000118
Matt Arsenaulta6801992018-07-10 14:03:41 +0000119 for (GlobalVariable &GV : M.globals()) {
120 // TODO: Region address
121 unsigned AS = GV.getType()->getAddressSpace();
Matt Arsenault0da63502018-08-31 05:49:54 +0000122 if (AS != AMDGPUAS::LOCAL_ADDRESS && AS != AMDGPUAS::REGION_ADDRESS)
Matt Arsenaulta6801992018-07-10 14:03:41 +0000123 continue;
124
125 recursivelyVisitUsers(GV, FuncsToAlwaysInline);
Tom Stellard5cbb53c2014-11-03 19:49:05 +0000126 }
127
Matt Arsenaulta6801992018-07-10 14:03:41 +0000128 if (!AMDGPUTargetMachine::EnableFunctionCalls || StressCalls) {
129 auto IncompatAttr
130 = StressCalls ? Attribute::AlwaysInline : Attribute::NoInline;
Tom Stellard5cbb53c2014-11-03 19:49:05 +0000131
Matt Arsenaulta6801992018-07-10 14:03:41 +0000132 for (Function &F : M) {
133 if (!F.isDeclaration() && !F.use_empty() &&
134 !F.hasFnAttribute(IncompatAttr)) {
135 if (StressCalls) {
136 if (!FuncsToAlwaysInline.count(&F))
137 FuncsToNoInline.insert(&F);
138 } else
139 FuncsToAlwaysInline.insert(&F);
140 }
Tom Stellard5cbb53c2014-11-03 19:49:05 +0000141 }
142 }
Matt Arsenaulta6801992018-07-10 14:03:41 +0000143
144 for (Function *F : FuncsToAlwaysInline)
145 F->addFnAttr(Attribute::AlwaysInline);
146
147 for (Function *F : FuncsToNoInline)
148 F->addFnAttr(Attribute::NoInline);
149
150 return !FuncsToAlwaysInline.empty() || !FuncsToNoInline.empty();
Tom Stellard5cbb53c2014-11-03 19:49:05 +0000151}
152
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +0000153ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) {
154 return new AMDGPUAlwaysInline(GlobalOpt);
Tom Stellard5cbb53c2014-11-03 19:49:05 +0000155}
Matt Arsenaulta6801992018-07-10 14:03:41 +0000156