summaryrefslogtreecommitdiff
path: root/backend/src/llvm/llvm_profiling.cpp
blob: 2d2ee1196c3a62295f2f445d3c4bcbf16731db73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright © 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * \file llvm_profiling.cpp
 * This file will insert some instructions for each profiling point.
 *
 */

#include <stdio.h>
#include <stdlib.h>

#include "llvm/Config/llvm-config.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/IR/IRBuilder.h"

#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 35
#include "llvm/IR/CallSite.h"
#include "llvm/IR/CFG.h"
#else
#include "llvm/Support/CallSite.h"
#include "llvm/Support/CFG.h"
#endif

#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Attributes.h"

#include "llvm/llvm_gen_backend.hpp"
#include "sys/map.hpp"
#include "ir/unit.hpp"

#include <iostream>
#include <vector>


using namespace llvm;
using std::vector;


namespace gbe
{
  using namespace ir;

  class ProfilingInserter : public FunctionPass
  {
  public:
    static char ID;
    Module* module;
    IRBuilder<>* builder;
    Type* intTy;
    Type *ptrTy;
    int profilingType;

    ProfilingInserter(int profiling) : FunctionPass(ID), profilingType(profiling)
    {
      module = NULL;
      builder = NULL;
      intTy = NULL;
      ptrTy = NULL;
    }

    ~ProfilingInserter(void)
    {
    }

#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 40
    virtual StringRef getPassName() const
#else
    virtual const char *getPassName() const
#endif
    {
      return "Timestamp Parser";
    }

    virtual bool runOnFunction(llvm::Function &F);
  };

  bool ProfilingInserter::runOnFunction(llvm::Function &F)
  {
    bool changed = false;
    int pointNum = 0;

    switch (F.getCallingConv()) {
      case CallingConv::C:
      case CallingConv::Fast:
      case CallingConv::SPIR_KERNEL:
        break;
      default:
        GBE_ASSERTM(false, "Unsupported calling convention");
    }

    // As we inline all function calls, so skip non-kernel functions
    bool bKernel = isKernelFunction(F);
    if (!bKernel) return changed;

    module = F.getParent();
    intTy = IntegerType::get(module->getContext(), 32);
    ptrTy = Type::getInt32PtrTy(module->getContext(), 1);
    builder = new IRBuilder<>(module->getContext());

    /* alloc a new buffer ptr to collect the timestamps. */
    builder->SetInsertPoint(&*F.begin()->begin());
    llvm::Constant *profilingBuf = module->getGlobalVariable("__gen_ocl_profiling_buf");
    if (!profilingBuf) {
      profilingBuf = new GlobalVariable(*module, intTy, false,
          GlobalVariable::ExternalLinkage, nullptr, StringRef("__gen_ocl_profiling_buf"),
          nullptr, GlobalVariable::NotThreadLocal, 1);
    }

    changed = true;

    for (llvm::Function::iterator B = F.begin(), BE = F.end(); B != BE; B++) {
      /* Skip the empty blocks. */
      if (B->empty())
        continue;

      BasicBlock::iterator instI = B->begin();
      for ( ; instI != B->end(); instI++) {
        if (dyn_cast<llvm::PHINode>(instI))
          continue;
        if (dyn_cast<llvm::ReturnInst>(instI)) {
          instI++;
          GBE_ASSERT(instI == B->end());
          break;
        }
        if (dyn_cast<llvm::BranchInst>(instI)) {
          instI++;
          GBE_ASSERT(instI == B->end());
          break;
        }
        break;
      }

      if (instI == B->end())
        continue;

      if (pointNum >= 20) // To many timestamp.
        continue;

      // Insert the first one at beginning of not PHI.
      builder->SetInsertPoint(&*instI);
      /* Add the timestamp store function call. */
      // __gen_ocl_store_timestamp(int nth, int type);
      Value *Args[2] = {ConstantInt::get(intTy, pointNum++), ConstantInt::get(intTy, profilingType)};
#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 50
      builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
              "__gen_ocl_calc_timestamp", Type::getVoidTy(module->getContext()),
              IntegerType::getInt32Ty(module->getContext()),
              IntegerType::getInt32Ty(module->getContext()))),
              ArrayRef<Value*>(Args));
#else
      builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
              "__gen_ocl_calc_timestamp", Type::getVoidTy(module->getContext()),
              IntegerType::getInt32Ty(module->getContext()),
              IntegerType::getInt32Ty(module->getContext()), nullptr)),
              ArrayRef<Value*>(Args));
#endif
    }
    /* We insert one store_profiling at the end of the last block to hold the place. */
    llvm::Function::iterator BE = F.end();
    BE--;
    BasicBlock::iterator retInst = BE->end();
    retInst--;
    builder->SetInsertPoint(&*retInst);
    Value *Args2[2] = {profilingBuf, ConstantInt::get(intTy, profilingType)};

#if LLVM_VERSION_MAJOR * 10 + LLVM_VERSION_MINOR >= 50
    builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
            "__gen_ocl_store_profiling", Type::getVoidTy(module->getContext()),
            ptrTy,
            IntegerType::getInt32Ty(module->getContext()))),
            ArrayRef<Value*>(Args2));
#else
    builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
            "__gen_ocl_store_profiling", Type::getVoidTy(module->getContext()),
            ptrTy,
            IntegerType::getInt32Ty(module->getContext()), nullptr)),
            ArrayRef<Value*>(Args2));
#endif

    delete builder;
    return changed;
  }

  FunctionPass* createProfilingInserterPass(int profilingType, ir::Unit &unit)
  {
    unit.setInProfilingMode(true);
    return new ProfilingInserter(profilingType);
  }
  char ProfilingInserter::ID = 0;

} // end namespace