LLVM 20.0.0git
AMDGPUAlwaysInlinePass.cpp
Go to the documentation of this file.
1//===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===//
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
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.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
16#include "AMDGPUTargetMachine.h"
19#include "llvm/IR/Module.h"
20#include "llvm/Pass.h"
22
23using namespace llvm;
24
25namespace {
26
27static cl::opt<bool> StressCalls(
28 "amdgpu-stress-function-calls",
30 cl::desc("Force all functions to be noinline"),
31 cl::init(false));
32
33class AMDGPUAlwaysInline : public ModulePass {
34 bool GlobalOpt;
35
36public:
37 static char ID;
38
39 AMDGPUAlwaysInline(bool GlobalOpt = false) :
40 ModulePass(ID), GlobalOpt(GlobalOpt) { }
41 bool runOnModule(Module &M) override;
42
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.setPreservesAll();
45 }
46};
47
48} // End anonymous namespace
49
50INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline",
51 "AMDGPU Inline All Functions", false, false)
52
53char AMDGPUAlwaysInline::ID = 0;
54
55static void
57 SmallPtrSetImpl<Function *> &FuncsToAlwaysInline) {
58 SmallVector<User *, 16> Stack(GV.users());
59
61
62 while (!Stack.empty()) {
63 User *U = Stack.pop_back_val();
64 if (!Visited.insert(U).second)
65 continue;
66
67 if (Instruction *I = dyn_cast<Instruction>(U)) {
68 Function *F = I->getParent()->getParent();
69 if (!AMDGPU::isEntryFunctionCC(F->getCallingConv())) {
70 // FIXME: This is a horrible hack. We should always respect noinline,
71 // and just let us hit the error when we can't handle this.
72 //
73 // Unfortunately, clang adds noinline to all functions at -O0. We have
74 // to override this here until that's fixed.
75 F->removeFnAttr(Attribute::NoInline);
76
77 FuncsToAlwaysInline.insert(F);
78 Stack.push_back(F);
79 }
80
81 // No need to look at further users, but we do need to inline any callers.
82 continue;
83 }
84
85 append_range(Stack, U->users());
86 }
87}
88
89static bool alwaysInlineImpl(Module &M, bool GlobalOpt) {
90 std::vector<GlobalAlias*> AliasesToRemove;
91
92 bool Changed = false;
93 SmallPtrSet<Function *, 8> FuncsToAlwaysInline;
94 SmallPtrSet<Function *, 8> FuncsToNoInline;
95 Triple TT(M.getTargetTriple());
96
97 for (GlobalAlias &A : M.aliases()) {
98 if (Function* F = dyn_cast<Function>(A.getAliasee())) {
99 if (TT.getArch() == Triple::amdgcn &&
100 A.getLinkage() != GlobalValue::InternalLinkage)
101 continue;
102 Changed = true;
103 A.replaceAllUsesWith(F);
104 AliasesToRemove.push_back(&A);
105 }
106
107 // FIXME: If the aliasee isn't a function, it's some kind of constant expr
108 // cast that won't be inlined through.
109 }
110
111 if (GlobalOpt) {
112 for (GlobalAlias* A : AliasesToRemove) {
113 A->eraseFromParent();
114 }
115 }
116
117 // Always force inlining of any function that uses an LDS global address. This
118 // is something of a workaround because we don't have a way of supporting LDS
119 // objects defined in functions. LDS is always allocated by a kernel, and it
120 // is difficult to manage LDS usage if a function may be used by multiple
121 // kernels.
122 //
123 // OpenCL doesn't allow declaring LDS in non-kernels, so in practice this
124 // should only appear when IPO passes manages to move LDs defined in a kernel
125 // into a single user function.
126
127 for (GlobalVariable &GV : M.globals()) {
128 // TODO: Region address
129 unsigned AS = GV.getAddressSpace();
130 if ((AS == AMDGPUAS::REGION_ADDRESS) ||
133 recursivelyVisitUsers(GV, FuncsToAlwaysInline);
134 }
135
136 if (!AMDGPUTargetMachine::EnableFunctionCalls || StressCalls) {
137 auto IncompatAttr
138 = StressCalls ? Attribute::AlwaysInline : Attribute::NoInline;
139
140 for (Function &F : M) {
141 if (!F.isDeclaration() && !F.use_empty() &&
142 !F.hasFnAttribute(IncompatAttr)) {
143 if (StressCalls) {
144 if (!FuncsToAlwaysInline.count(&F))
145 FuncsToNoInline.insert(&F);
146 } else
147 FuncsToAlwaysInline.insert(&F);
148 }
149 }
150 }
151
152 for (Function *F : FuncsToAlwaysInline)
153 F->addFnAttr(Attribute::AlwaysInline);
154
155 for (Function *F : FuncsToNoInline)
156 F->addFnAttr(Attribute::NoInline);
157
158 return Changed || !FuncsToAlwaysInline.empty() || !FuncsToNoInline.empty();
159}
160
161bool AMDGPUAlwaysInline::runOnModule(Module &M) {
162 return alwaysInlineImpl(M, GlobalOpt);
163}
164
166 return new AMDGPUAlwaysInline(GlobalOpt);
167}
168
171 const bool Changed = alwaysInlineImpl(M, GlobalOpt);
172 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
173}
static bool alwaysInlineImpl(Module &M, bool GlobalOpt)
static INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline", "AMDGPU Inline All Functions", false, false) char AMDGPUAlwaysInline void recursivelyVisitUsers(GlobalValue &GV, SmallPtrSetImpl< Function * > &FuncsToAlwaysInline)
The AMDGPU TargetMachine interface definition for hw codegen targets.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition: Analysis.h:114
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
@ REGION_ADDRESS
Address space for region memory. (GDS)
@ LOCAL_ADDRESS
Address space for local memory.
bool isEntryFunctionCC(CallingConv::ID CC)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
ModulePass * createAMDGPUAlwaysInlinePass(bool GlobalOpt=true)
PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)