Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/AMDGPU/AMDGPUAlwaysInlinePass.cpp
Line
Count
Source
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"
17
#include "Utils/AMDGPUBaseInfo.h"
18
#include "llvm/ADT/SmallPtrSet.h"
19
#include "llvm/IR/Module.h"
20
#include "llvm/Transforms/Utils/Cloning.h"
21
22
using namespace llvm;
23
24
namespace {
25
26
static 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
32
class AMDGPUAlwaysInline : public ModulePass {
33
  bool GlobalOpt;
34
35
  void recursivelyVisitUsers(GlobalValue &GV,
36
                             SmallPtrSetImpl<Function *> &FuncsToAlwaysInline);
37
public:
38
  static char ID;
39
40
  AMDGPUAlwaysInline(bool GlobalOpt = false) :
41
2.72k
    ModulePass(ID), GlobalOpt(GlobalOpt) { }
42
  bool runOnModule(Module &M) override;
43
44
2.70k
  void getAnalysisUsage(AnalysisUsage &AU) const override {
45
2.70k
    AU.setPreservesAll();
46
2.70k
 }
47
};
48
49
} // End anonymous namespace
50
51
INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline",
52
                "AMDGPU Inline All Functions", false, false)
53
54
char AMDGPUAlwaysInline::ID = 0;
55
56
void AMDGPUAlwaysInline::recursivelyVisitUsers(
57
  GlobalValue &GV,
58
188
  SmallPtrSetImpl<Function *> &FuncsToAlwaysInline) {
59
188
  SmallVector<User *, 16> Stack;
60
188
61
188
  SmallPtrSet<const Value *, 8> Visited;
62
188
63
188
  for (User *U : GV.users())
64
585
    Stack.push_back(U);
65
188
66
864
  while (!Stack.empty()) {
67
676
    User *U = Stack.pop_back_val();
68
676
    if (!Visited.insert(U).second)
69
6
      continue;
70
670
71
670
    if (Instruction *I = dyn_cast<Instruction>(U)) {
72
598
      Function *F = I->getParent()->getParent();
73
598
      if (!AMDGPU::isEntryFunctionCC(F->getCallingConv())) {
74
26
        FuncsToAlwaysInline.insert(F);
75
26
        Stack.push_back(F);
76
26
      }
77
598
78
598
      // No need to look at further users, but we do need to inline any callers.
79
598
      continue;
80
598
    }
81
72
82
72
    for (User *UU : U->users())
83
65
      Stack.push_back(UU);
84
72
  }
85
188
}
86
87
2.70k
bool AMDGPUAlwaysInline::runOnModule(Module &M) {
88
2.70k
  std::vector<GlobalAlias*> AliasesToRemove;
89
2.70k
90
2.70k
  SmallPtrSet<Function *, 8> FuncsToAlwaysInline;
91
2.70k
  SmallPtrSet<Function *, 8> FuncsToNoInline;
92
2.70k
93
2.70k
  for (GlobalAlias &A : M.aliases()) {
94
5
    if (Function* F = dyn_cast<Function>(A.getAliasee())) {
95
3
      A.replaceAllUsesWith(F);
96
3
      AliasesToRemove.push_back(&A);
97
3
    }
98
5
99
5
    // FIXME: If the aliasee isn't a function, it's some kind of constant expr
100
5
    // cast that won't be inlined through.
101
5
  }
102
2.70k
103
2.70k
  if (GlobalOpt) {
104
2.70k
    for (GlobalAlias* A : AliasesToRemove) {
105
3
      A->eraseFromParent();
106
3
    }
107
2.70k
  }
108
2.70k
109
2.70k
  // Always force inlining of any function that uses an LDS global address. This
110
2.70k
  // is something of a workaround because we don't have a way of supporting LDS
111
2.70k
  // objects defined in functions. LDS is always allocated by a kernel, and it
112
2.70k
  // is difficult to manage LDS usage if a function may be used by multiple
113
2.70k
  // kernels.
114
2.70k
  //
115
2.70k
  // OpenCL doesn't allow declaring LDS in non-kernels, so in practice this
116
2.70k
  // should only appear when IPO passes manages to move LDs defined in a kernel
117
2.70k
  // into a single user function.
118
2.70k
119
2.70k
  for (GlobalVariable &GV : M.globals()) {
120
346
    // TODO: Region address
121
346
    unsigned AS = GV.getType()->getAddressSpace();
122
346
    if (AS != AMDGPUAS::LOCAL_ADDRESS && 
AS != AMDGPUAS::REGION_ADDRESS160
)
123
158
      continue;
124
188
125
188
    recursivelyVisitUsers(GV, FuncsToAlwaysInline);
126
188
  }
127
2.70k
128
2.70k
  if (!AMDGPUTargetMachine::EnableFunctionCalls || 
StressCalls2.42k
) {
129
284
    auto IncompatAttr
130
284
      = StressCalls ? 
Attribute::AlwaysInline4
:
Attribute::NoInline280
;
131
284
132
2.65k
    for (Function &F : M) {
133
2.65k
      if (!F.isDeclaration() && 
!F.use_empty()2.31k
&&
134
2.65k
          
!F.hasFnAttribute(IncompatAttr)14
) {
135
10
        if (StressCalls) {
136
6
          if (!FuncsToAlwaysInline.count(&F))
137
2
            FuncsToNoInline.insert(&F);
138
6
        } else
139
4
          FuncsToAlwaysInline.insert(&F);
140
10
      }
141
2.65k
    }
142
284
  }
143
2.70k
144
2.70k
  for (Function *F : FuncsToAlwaysInline)
145
28
    F->addFnAttr(Attribute::AlwaysInline);
146
2.70k
147
2.70k
  for (Function *F : FuncsToNoInline)
148
2
    F->addFnAttr(Attribute::NoInline);
149
2.70k
150
2.70k
  return !FuncsToAlwaysInline.empty() || 
!FuncsToNoInline.empty()2.69k
;
151
2.70k
}
152
153
2.72k
ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) {
154
2.72k
  return new AMDGPUAlwaysInline(GlobalOpt);
155
2.72k
}
156