InlinePreparationPass.cpp revision b83f1f4d77c7624b9e8dc11beec8f796ff841918
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/ADT/StringSet.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24
25#include "bcinfo/MetadataExtractor.h"
26
27#define DEBUG_TYPE "rs2spirv-inline"
28
29using namespace llvm;
30
31namespace rs2spirv {
32
33namespace {
34
35class InlinePreparationPass : public ModulePass {
36  bcinfo::MetadataExtractor &ME;
37
38public:
39  static char ID;
40  explicit InlinePreparationPass(bcinfo::MetadataExtractor &Extractor)
41      : ModulePass(ID), ME(Extractor) {}
42
43  const char *getPassName() const override { return "InlinePreparationPass"; }
44
45  bool runOnModule(Module &M) override {
46    DEBUG(dbgs() << "InlinePreparationPass\n");
47
48    const size_t RSKernelNum = ME.getExportForEachSignatureCount();
49    const char **RSKernelNames = ME.getExportForEachNameList();
50    if (RSKernelNum == 0)
51      DEBUG(dbgs() << "InlinePreparationPass detected no kernel\n");
52
53    StringSet<> KNames;
54    for (size_t i = 0; i < RSKernelNum; ++i)
55      KNames.insert(RSKernelNames[i]);
56
57    for (auto &F : M.functions()) {
58      if (F.isDeclaration())
59        continue;
60
61      const auto FName = F.getName();
62
63      // TODO: Consider inlining kernels (i.e. kernels calling other kernels)
64      // when multi-kernel module support is ready.
65      if (KNames.count(FName) != 0)
66        continue; // Skip kernels.
67
68      F.addFnAttr(Attribute::AlwaysInline);
69      F.setLinkage(GlobalValue::InternalLinkage);
70      DEBUG(dbgs() << "Marked as alwaysinline:\t" << FName << '\n');
71    }
72
73    // Return true, as the pass modifies module.
74    return true;
75  }
76};
77} // namespace
78
79char InlinePreparationPass::ID = 0;
80
81ModulePass *createInlinePreparationPass(bcinfo::MetadataExtractor &ME) {
82  return new InlinePreparationPass(ME);
83}
84
85} // namespace rs2spirv
86