1/*
2 * Copyright 2016, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "InlinePreparationPass.h"
18
19#include "llvm/IR/Module.h"
20#include "llvm/Pass.h"
21#include "llvm/Support/Debug.h"
22#include "llvm/Support/raw_ostream.h"
23
24#include "Context.h"
25
26#define DEBUG_TYPE "rs2spirv-inline"
27
28using namespace llvm;
29
30namespace rs2spirv {
31
32namespace {
33
34class InlinePreparationPass : public ModulePass {
35public:
36  static char ID;
37  explicit InlinePreparationPass() : ModulePass(ID) {}
38
39  const char *getPassName() const override { return "InlinePreparationPass"; }
40
41  bool runOnModule(Module &M) override {
42    DEBUG(dbgs() << "InlinePreparationPass\n");
43
44    rs2spirv::Context &Ctxt = rs2spirv::Context::getInstance();
45
46    for (auto &F : M.functions()) {
47      if (F.isDeclaration()) {
48        continue;
49      }
50
51      if (Ctxt.isForEachKernel(F.getName())) {
52        continue; // Skip kernels.
53      }
54
55      F.addFnAttr(Attribute::AlwaysInline);
56      F.setLinkage(GlobalValue::InternalLinkage);
57
58      DEBUG(dbgs() << "Marked as alwaysinline:\t" << F.getName() << '\n');
59    }
60
61    // Returns true, because this pass modifies the Module.
62    return true;
63  }
64};
65
66} // namespace
67
68char InlinePreparationPass::ID = 0;
69
70ModulePass *createInlinePreparationPass() {
71  return new InlinePreparationPass();
72}
73
74} // namespace rs2spirv
75