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