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