File: JITLoaderVTune.cpp

package info (click to toggle)
llvm-toolchain-20 1%3A20.1.6-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,111,304 kB
  • sloc: cpp: 7,438,677; ansic: 1,393,822; asm: 1,012,926; python: 241,650; f90: 86,635; objc: 75,479; lisp: 42,144; pascal: 17,286; sh: 10,027; ml: 5,082; perl: 4,730; awk: 3,523; makefile: 3,349; javascript: 2,251; xml: 892; fortran: 672
file content (224 lines) | stat: -rw-r--r-- 7,691 bytes parent folder | download
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
213
214
215
216
217
218
219
220
221
222
223
224
//===------- JITLoaderVTune.cpp - Register profiler objects -----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Register objects for access by profilers via the VTune JIT interface.
//===----------------------------------------------------------------------===//

#include "llvm/ExecutionEngine/Orc/TargetProcess/JITLoaderVTune.h"
#include "llvm/ExecutionEngine/Orc/Shared/VTuneSharedStructs.h"

#if LLVM_USE_INTEL_JITEVENTS
#include "IntelJITEventsWrapper.h"
#include "ittnotify.h"
#include <map>

using namespace llvm;
using namespace llvm::orc;

namespace {
class JITEventWrapper {
public:
  static std::unique_ptr<IntelJITEventsWrapper> Wrapper;
};
std::unique_ptr<IntelJITEventsWrapper> JITEventWrapper::Wrapper;
} // namespace

static Error registerJITLoaderVTuneRegisterImpl(const VTuneMethodBatch &MB) {
  const size_t StringsSize = MB.Strings.size();

  for (const auto &MethodInfo : MB.Methods) {
    iJIT_Method_Load MethodMessage;
    memset(&MethodMessage, 0, sizeof(iJIT_Method_Load));

    MethodMessage.method_id = MethodInfo.MethodID;
    if (MethodInfo.NameSI != 0 && MethodInfo.NameSI < StringsSize) {
      MethodMessage.method_name =
          const_cast<char *>(MB.Strings.at(MethodInfo.NameSI).data());
    } else {
      MethodMessage.method_name = NULL;
    }
    if (MethodInfo.ClassFileSI != 0 && MethodInfo.ClassFileSI < StringsSize) {
      MethodMessage.class_file_name =
          const_cast<char *>(MB.Strings.at(MethodInfo.ClassFileSI).data());
    } else {
      MethodMessage.class_file_name = NULL;
    }
    if (MethodInfo.SourceFileSI != 0 && MethodInfo.SourceFileSI < StringsSize) {
      MethodMessage.source_file_name =
          const_cast<char *>(MB.Strings.at(MethodInfo.SourceFileSI).data());
    } else {
      MethodMessage.source_file_name = NULL;
    }

    MethodMessage.method_load_address = MethodInfo.LoadAddr.toPtr<void *>();
    MethodMessage.method_size = MethodInfo.LoadSize;
    MethodMessage.class_id = 0;

    MethodMessage.user_data = NULL;
    MethodMessage.user_data_size = 0;
    MethodMessage.env = iJDE_JittingAPI;

    std::vector<LineNumberInfo> LineInfo;
    for (const auto &LInfo : MethodInfo.LineTable) {
      LineInfo.push_back(LineNumberInfo{LInfo.first, LInfo.second});
    }

    if (LineInfo.size() == 0) {
      MethodMessage.line_number_size = 0;
      MethodMessage.line_number_table = 0;
    } else {
      MethodMessage.line_number_size = LineInfo.size();
      MethodMessage.line_number_table = &*LineInfo.begin();
    }
    JITEventWrapper::Wrapper->iJIT_NotifyEvent(
        iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED, &MethodMessage);
  }

  return Error::success();
}

static void registerJITLoaderVTuneUnregisterImpl(
    const std::vector<std::pair<uint64_t, uint64_t>> &UM) {
  for (auto &Method : UM) {
    JITEventWrapper::Wrapper->iJIT_NotifyEvent(
        iJVM_EVENT_TYPE_METHOD_UNLOAD_START,
        const_cast<uint64_t *>(&Method.first));
  }
}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_registerVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  if (!JITEventWrapper::Wrapper)
    JITEventWrapper::Wrapper.reset(new IntelJITEventsWrapper);

  return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
             Data, Size, registerJITLoaderVTuneRegisterImpl)
      .release();
}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_unregisterVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  return WrapperFunction<void(SPSVTuneUnloadedMethodIDs)>::handle(
             Data, Size, registerJITLoaderVTuneUnregisterImpl)
      .release();
}

// For Testing: following code comes from llvm-jitlistener.cpp in llvm tools
namespace {
using SourceLocations = std::vector<std::pair<std::string, unsigned int>>;
using NativeCodeMap = std::map<uint64_t, SourceLocations>;
NativeCodeMap ReportedDebugFuncs;
} // namespace

static int NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
  switch (EventType) {
  case iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED: {
    if (!EventSpecificData) {
      errs() << "Error: The JIT event listener did not provide a event data.";
      return -1;
    }
    iJIT_Method_Load *msg = static_cast<iJIT_Method_Load *>(EventSpecificData);

    ReportedDebugFuncs[msg->method_id];

    outs() << "Method load [" << msg->method_id << "]: " << msg->method_name
           << ", Size = " << msg->method_size << "\n";

    for (unsigned int i = 0; i < msg->line_number_size; ++i) {
      if (!msg->line_number_table) {
        errs() << "A function with a non-zero line count had no line table.";
        return -1;
      }
      std::pair<std::string, unsigned int> loc(
          std::string(msg->source_file_name),
          msg->line_number_table[i].LineNumber);
      ReportedDebugFuncs[msg->method_id].push_back(loc);
      outs() << "  Line info @ " << msg->line_number_table[i].Offset << ": "
             << msg->source_file_name << ", line "
             << msg->line_number_table[i].LineNumber << "\n";
    }
    outs() << "\n";
  } break;
  case iJVM_EVENT_TYPE_METHOD_UNLOAD_START: {
    if (!EventSpecificData) {
      errs() << "Error: The JIT event listener did not provide a event data.";
      return -1;
    }
    unsigned int UnloadId =
        *reinterpret_cast<unsigned int *>(EventSpecificData);
    assert(1 == ReportedDebugFuncs.erase(UnloadId));
    outs() << "Method unload [" << UnloadId << "]\n";
  } break;
  default:
    break;
  }
  return 0;
}

static iJIT_IsProfilingActiveFlags IsProfilingActive(void) {
  // for testing, pretend we have an Intel Parallel Amplifier XE 2011
  // instance attached
  return iJIT_SAMPLING_ON;
}

static unsigned int GetNewMethodID(void) {
  static unsigned int id = 0;
  return ++id;
}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_test_registerVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  JITEventWrapper::Wrapper.reset(new IntelJITEventsWrapper(
      NotifyEvent, NULL, NULL, IsProfilingActive, 0, 0, GetNewMethodID));
  return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
             Data, Size, registerJITLoaderVTuneRegisterImpl)
      .release();
}

#else

using namespace llvm;
using namespace llvm::orc;

static Error unsupportedBatch(const VTuneMethodBatch &MB) {
  return llvm::make_error<StringError>("unsupported for Intel VTune",
                                       inconvertibleErrorCode());
}

static void unsuppported(const std::vector<std::pair<uint64_t, uint64_t>> &UM) {

}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_registerVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
             Data, Size, unsupportedBatch)
      .release();
}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_unregisterVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  return WrapperFunction<void(SPSVTuneUnloadedMethodIDs)>::handle(Data, Size,
                                                                  unsuppported)
      .release();
}

extern "C" llvm::orc::shared::CWrapperFunctionResult
llvm_orc_test_registerVTuneImpl(const char *Data, uint64_t Size) {
  using namespace orc::shared;
  return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
             Data, Size, unsupportedBatch)
      .release();
}

#endif