1 //===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This pass marks all internal functions as always_inline and creates
11 /// duplicates of all other functions and marks the duplicates as always_inline.
13 //===----------------------------------------------------------------------===//
16 #include "AMDGPUTargetMachine.h"
17 #include "Utils/AMDGPUBaseInfo.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Transforms/Utils/Cloning.h"
26 static cl::opt
<bool> StressCalls(
27 "amdgpu-stress-function-calls",
29 cl::desc("Force all functions to be noinline"),
32 class AMDGPUAlwaysInline
: public ModulePass
{
35 void recursivelyVisitUsers(GlobalValue
&GV
,
36 SmallPtrSetImpl
<Function
*> &FuncsToAlwaysInline
);
40 AMDGPUAlwaysInline(bool GlobalOpt
= false) :
41 ModulePass(ID
), GlobalOpt(GlobalOpt
) { }
42 bool runOnModule(Module
&M
) override
;
44 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
49 } // End anonymous namespace
51 INITIALIZE_PASS(AMDGPUAlwaysInline
, "amdgpu-always-inline",
52 "AMDGPU Inline All Functions", false, false)
54 char AMDGPUAlwaysInline::ID
= 0;
56 void AMDGPUAlwaysInline::recursivelyVisitUsers(
58 SmallPtrSetImpl
<Function
*> &FuncsToAlwaysInline
) {
59 SmallVector
<User
*, 16> Stack
;
61 SmallPtrSet
<const Value
*, 8> Visited
;
63 for (User
*U
: GV
.users())
66 while (!Stack
.empty()) {
67 User
*U
= Stack
.pop_back_val();
68 if (!Visited
.insert(U
).second
)
71 if (Instruction
*I
= dyn_cast
<Instruction
>(U
)) {
72 Function
*F
= I
->getParent()->getParent();
73 if (!AMDGPU::isEntryFunctionCC(F
->getCallingConv())) {
74 FuncsToAlwaysInline
.insert(F
);
78 // No need to look at further users, but we do need to inline any callers.
82 for (User
*UU
: U
->users())
87 bool AMDGPUAlwaysInline::runOnModule(Module
&M
) {
88 std::vector
<GlobalAlias
*> AliasesToRemove
;
90 SmallPtrSet
<Function
*, 8> FuncsToAlwaysInline
;
91 SmallPtrSet
<Function
*, 8> FuncsToNoInline
;
93 for (GlobalAlias
&A
: M
.aliases()) {
94 if (Function
* F
= dyn_cast
<Function
>(A
.getAliasee())) {
95 A
.replaceAllUsesWith(F
);
96 AliasesToRemove
.push_back(&A
);
99 // FIXME: If the aliasee isn't a function, it's some kind of constant expr
100 // cast that won't be inlined through.
104 for (GlobalAlias
* A
: AliasesToRemove
) {
105 A
->eraseFromParent();
109 // 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
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.
119 for (GlobalVariable
&GV
: M
.globals()) {
120 // TODO: Region address
121 unsigned AS
= GV
.getType()->getAddressSpace();
122 if (AS
!= AMDGPUAS::LOCAL_ADDRESS
&& AS
!= AMDGPUAS::REGION_ADDRESS
)
125 recursivelyVisitUsers(GV
, FuncsToAlwaysInline
);
128 if (!AMDGPUTargetMachine::EnableFunctionCalls
|| StressCalls
) {
130 = StressCalls
? Attribute::AlwaysInline
: Attribute::NoInline
;
132 for (Function
&F
: M
) {
133 if (!F
.isDeclaration() && !F
.use_empty() &&
134 !F
.hasFnAttribute(IncompatAttr
)) {
136 if (!FuncsToAlwaysInline
.count(&F
))
137 FuncsToNoInline
.insert(&F
);
139 FuncsToAlwaysInline
.insert(&F
);
144 for (Function
*F
: FuncsToAlwaysInline
)
145 F
->addFnAttr(Attribute::AlwaysInline
);
147 for (Function
*F
: FuncsToNoInline
)
148 F
->addFnAttr(Attribute::NoInline
);
150 return !FuncsToAlwaysInline
.empty() || !FuncsToNoInline
.empty();
153 ModulePass
*llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt
) {
154 return new AMDGPUAlwaysInline(GlobalOpt
);