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/IR/Module.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Support/CommandLine.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
{
38 AMDGPUAlwaysInline(bool GlobalOpt
= false) :
39 ModulePass(ID
), GlobalOpt(GlobalOpt
) { }
40 bool runOnModule(Module
&M
) override
;
42 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
47 } // End anonymous namespace
49 INITIALIZE_PASS(AMDGPUAlwaysInline
, "amdgpu-always-inline",
50 "AMDGPU Inline All Functions", false, false)
52 char AMDGPUAlwaysInline::ID
= 0;
55 recursivelyVisitUsers(GlobalValue
&GV
,
56 SmallPtrSetImpl
<Function
*> &FuncsToAlwaysInline
) {
57 SmallVector
<User
*, 16> Stack(GV
.users());
59 SmallPtrSet
<const Value
*, 8> Visited
;
61 while (!Stack
.empty()) {
62 User
*U
= Stack
.pop_back_val();
63 if (!Visited
.insert(U
).second
)
66 if (Instruction
*I
= dyn_cast
<Instruction
>(U
)) {
67 Function
*F
= I
->getParent()->getParent();
68 if (!AMDGPU::isEntryFunctionCC(F
->getCallingConv())) {
69 // FIXME: This is a horrible hack. We should always respect noinline,
70 // and just let us hit the error when we can't handle this.
72 // Unfortunately, clang adds noinline to all functions at -O0. We have
73 // to override this here. until that's fixed.
74 F
->removeFnAttr(Attribute::NoInline
);
76 FuncsToAlwaysInline
.insert(F
);
80 // No need to look at further users, but we do need to inline any callers.
84 append_range(Stack
, U
->users());
88 static bool alwaysInlineImpl(Module
&M
, bool GlobalOpt
) {
89 std::vector
<GlobalAlias
*> AliasesToRemove
;
91 SmallPtrSet
<Function
*, 8> FuncsToAlwaysInline
;
92 SmallPtrSet
<Function
*, 8> FuncsToNoInline
;
94 for (GlobalAlias
&A
: M
.aliases()) {
95 if (Function
* F
= dyn_cast
<Function
>(A
.getAliasee())) {
96 A
.replaceAllUsesWith(F
);
97 AliasesToRemove
.push_back(&A
);
100 // FIXME: If the aliasee isn't a function, it's some kind of constant expr
101 // cast that won't be inlined through.
105 for (GlobalAlias
* A
: AliasesToRemove
) {
106 A
->eraseFromParent();
110 // Always force inlining of any function that uses an LDS global address. This
111 // is something of a workaround because we don't have a way of supporting LDS
112 // objects defined in functions. LDS is always allocated by a kernel, and it
113 // is difficult to manage LDS usage if a function may be used by multiple
116 // OpenCL doesn't allow declaring LDS in non-kernels, so in practice this
117 // should only appear when IPO passes manages to move LDs defined in a kernel
118 // into a single user function.
120 for (GlobalVariable
&GV
: M
.globals()) {
121 // TODO: Region address
122 unsigned AS
= GV
.getAddressSpace();
123 if ((AS
== AMDGPUAS::REGION_ADDRESS
) ||
124 (AS
== AMDGPUAS::LOCAL_ADDRESS
&&
125 !AMDGPUTargetMachine::EnableLowerModuleLDS
))
126 recursivelyVisitUsers(GV
, FuncsToAlwaysInline
);
129 if (!AMDGPUTargetMachine::EnableFunctionCalls
|| StressCalls
) {
131 = StressCalls
? Attribute::AlwaysInline
: Attribute::NoInline
;
133 for (Function
&F
: M
) {
134 if (!F
.isDeclaration() && !F
.use_empty() &&
135 !F
.hasFnAttribute(IncompatAttr
)) {
137 if (!FuncsToAlwaysInline
.count(&F
))
138 FuncsToNoInline
.insert(&F
);
140 FuncsToAlwaysInline
.insert(&F
);
145 for (Function
*F
: FuncsToAlwaysInline
)
146 F
->addFnAttr(Attribute::AlwaysInline
);
148 for (Function
*F
: FuncsToNoInline
)
149 F
->addFnAttr(Attribute::NoInline
);
151 return !FuncsToAlwaysInline
.empty() || !FuncsToNoInline
.empty();
154 bool AMDGPUAlwaysInline::runOnModule(Module
&M
) {
155 return alwaysInlineImpl(M
, GlobalOpt
);
158 ModulePass
*llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt
) {
159 return new AMDGPUAlwaysInline(GlobalOpt
);
162 PreservedAnalyses
AMDGPUAlwaysInlinePass::run(Module
&M
,
163 ModuleAnalysisManager
&AM
) {
164 alwaysInlineImpl(M
, GlobalOpt
);
165 return PreservedAnalyses::all();