LLVM 20.0.0git
JITLoaderVTune.cpp
Go to the documentation of this file.
1//===------- JITLoaderVTune.cpp - Register profiler objects -----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Register objects for access by profilers via the VTune JIT interface.
10//===----------------------------------------------------------------------===//
11
14
15#if LLVM_USE_INTEL_JITEVENTS
17#include "ittnotify.h"
18
19using namespace llvm;
20using namespace llvm::orc;
21
22namespace {
23class JITEventWrapper {
24public:
25 static std::unique_ptr<IntelJITEventsWrapper> Wrapper;
26};
27std::unique_ptr<IntelJITEventsWrapper> JITEventWrapper::Wrapper;
28} // namespace
29
30static Error registerJITLoaderVTuneRegisterImpl(const VTuneMethodBatch &MB) {
31 const size_t StringsSize = MB.Strings.size();
32
33 for (const auto &MethodInfo : MB.Methods) {
34 iJIT_Method_Load MethodMessage;
35 memset(&MethodMessage, 0, sizeof(iJIT_Method_Load));
36
37 MethodMessage.method_id = MethodInfo.MethodID;
38 if (MethodInfo.NameSI != 0 && MethodInfo.NameSI < StringsSize) {
39 MethodMessage.method_name =
40 const_cast<char *>(MB.Strings.at(MethodInfo.NameSI).data());
41 } else {
42 MethodMessage.method_name = NULL;
43 }
44 if (MethodInfo.ClassFileSI != 0 && MethodInfo.ClassFileSI < StringsSize) {
45 MethodMessage.class_file_name =
46 const_cast<char *>(MB.Strings.at(MethodInfo.ClassFileSI).data());
47 } else {
48 MethodMessage.class_file_name = NULL;
49 }
50 if (MethodInfo.SourceFileSI != 0 && MethodInfo.SourceFileSI < StringsSize) {
51 MethodMessage.source_file_name =
52 const_cast<char *>(MB.Strings.at(MethodInfo.SourceFileSI).data());
53 } else {
54 MethodMessage.source_file_name = NULL;
55 }
56
57 MethodMessage.method_load_address = MethodInfo.LoadAddr.toPtr<void *>();
58 MethodMessage.method_size = MethodInfo.LoadSize;
59 MethodMessage.class_id = 0;
60
61 MethodMessage.user_data = NULL;
62 MethodMessage.user_data_size = 0;
63 MethodMessage.env = iJDE_JittingAPI;
64
65 std::vector<LineNumberInfo> LineInfo;
66 for (const auto &LInfo : MethodInfo.LineTable) {
67 LineInfo.push_back(LineNumberInfo{LInfo.first, LInfo.second});
68 }
69
70 if (LineInfo.size() == 0) {
71 MethodMessage.line_number_size = 0;
72 MethodMessage.line_number_table = 0;
73 } else {
74 MethodMessage.line_number_size = LineInfo.size();
75 MethodMessage.line_number_table = &*LineInfo.begin();
76 }
77 JITEventWrapper::Wrapper->iJIT_NotifyEvent(
79 }
80
81 return Error::success();
82}
83
84static void registerJITLoaderVTuneUnregisterImpl(
85 const std::vector<std::pair<uint64_t, uint64_t>> &UM) {
86 for (auto &Method : UM) {
87 JITEventWrapper::Wrapper->iJIT_NotifyEvent(
89 const_cast<uint64_t *>(&Method.first));
90 }
91}
92
94llvm_orc_registerVTuneImpl(const char *Data, uint64_t Size) {
95 using namespace orc::shared;
96 if (!JITEventWrapper::Wrapper)
97 JITEventWrapper::Wrapper.reset(new IntelJITEventsWrapper);
98
99 return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
100 Data, Size, registerJITLoaderVTuneRegisterImpl)
101 .release();
102}
103
105llvm_orc_unregisterVTuneImpl(const char *Data, uint64_t Size) {
106 using namespace orc::shared;
107 return WrapperFunction<void(SPSVTuneUnloadedMethodIDs)>::handle(
108 Data, Size, registerJITLoaderVTuneUnregisterImpl)
109 .release();
110}
111
112// For Testing: following code comes from llvm-jitlistener.cpp in llvm tools
113namespace {
114using SourceLocations = std::vector<std::pair<std::string, unsigned int>>;
115using NativeCodeMap = std::map<uint64_t, SourceLocations>;
116NativeCodeMap ReportedDebugFuncs;
117} // namespace
118
119static int NotifyEvent(iJIT_JVM_EVENT EventType, void *EventSpecificData) {
120 switch (EventType) {
122 if (!EventSpecificData) {
123 errs() << "Error: The JIT event listener did not provide a event data.";
124 return -1;
125 }
126 iJIT_Method_Load *msg = static_cast<iJIT_Method_Load *>(EventSpecificData);
127
128 ReportedDebugFuncs[msg->method_id];
129
130 outs() << "Method load [" << msg->method_id << "]: " << msg->method_name
131 << ", Size = " << msg->method_size << "\n";
132
133 for (unsigned int i = 0; i < msg->line_number_size; ++i) {
134 if (!msg->line_number_table) {
135 errs() << "A function with a non-zero line count had no line table.";
136 return -1;
137 }
138 std::pair<std::string, unsigned int> loc(
139 std::string(msg->source_file_name),
141 ReportedDebugFuncs[msg->method_id].push_back(loc);
142 outs() << " Line info @ " << msg->line_number_table[i].Offset << ": "
143 << msg->source_file_name << ", line "
144 << msg->line_number_table[i].LineNumber << "\n";
145 }
146 outs() << "\n";
147 } break;
149 if (!EventSpecificData) {
150 errs() << "Error: The JIT event listener did not provide a event data.";
151 return -1;
152 }
153 unsigned int UnloadId =
154 *reinterpret_cast<unsigned int *>(EventSpecificData);
155 assert(1 == ReportedDebugFuncs.erase(UnloadId));
156 outs() << "Method unload [" << UnloadId << "]\n";
157 } break;
158 default:
159 break;
160 }
161 return 0;
162}
163
164static iJIT_IsProfilingActiveFlags IsProfilingActive(void) {
165 // for testing, pretend we have an Intel Parallel Amplifier XE 2011
166 // instance attached
167 return iJIT_SAMPLING_ON;
168}
169
170static unsigned int GetNewMethodID(void) {
171 static unsigned int id = 0;
172 return ++id;
173}
174
177 using namespace orc::shared;
178 JITEventWrapper::Wrapper.reset(new IntelJITEventsWrapper(
179 NotifyEvent, NULL, NULL, IsProfilingActive, 0, 0, GetNewMethodID));
180 return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
181 Data, Size, registerJITLoaderVTuneRegisterImpl)
182 .release();
183}
184
185#else
186
187using namespace llvm;
188using namespace llvm::orc;
189
191 return llvm::make_error<StringError>("unsupported for Intel VTune",
193}
194
195static void unsuppported(const std::vector<std::pair<uint64_t, uint64_t>> &UM) {
196
197}
198
201 using namespace orc::shared;
202 return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
203 Data, Size, unsupportedBatch)
204 .release();
205}
206
209 using namespace orc::shared;
210 return WrapperFunction<void(SPSVTuneUnloadedMethodIDs)>::handle(Data, Size,
212 .release();
213}
214
217 using namespace orc::shared;
218 return WrapperFunction<SPSError(SPSVTuneMethodBatch)>::handle(
219 Data, Size, unsupportedBatch)
220 .release();
221}
222
223#endif
amdgpu aa AMDGPU Address space based Alias Analysis Wrapper
uint64_t Size
static Error unsupportedBatch(const VTuneMethodBatch &MB)
llvm::orc::shared::CWrapperFunctionResult llvm_orc_registerVTuneImpl(const char *Data, uint64_t Size)
llvm::orc::shared::CWrapperFunctionResult llvm_orc_unregisterVTuneImpl(const char *Data, uint64_t Size)
llvm::orc::shared::CWrapperFunctionResult llvm_orc_test_registerVTuneImpl(const char *Data, uint64_t Size)
static void unsuppported(const std::vector< std::pair< uint64_t, uint64_t > > &UM)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
@ iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED
Definition: jitprofiling.h:42
@ iJVM_EVENT_TYPE_METHOD_UNLOAD_START
Definition: jitprofiling.h:48
@ iJIT_SAMPLING_ON
Definition: jitprofiling.h:111
@ iJDE_JittingAPI
Definition: jitprofiling.h:121
enum iJIT_jvm_event iJIT_JVM_EVENT
enum _iJIT_IsProfilingActiveFlags iJIT_IsProfilingActiveFlags
std::vector< SourceLocation > SourceLocations
Definition: LookupResult.h:36
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
unsigned int Offset
Definition: jitprofiling.h:167
unsigned int LineNumber
Definition: jitprofiling.h:170
unsigned int line_number_size
Definition: jitprofiling.h:193
iJDEnvironmentType env
Definition: jitprofiling.h:214
unsigned int method_id
Definition: jitprofiling.h:177
unsigned int class_id
Definition: jitprofiling.h:199
pLineNumberInfo line_number_table
Definition: jitprofiling.h:196
unsigned int user_data_size
Definition: jitprofiling.h:211
unsigned int method_size
Definition: jitprofiling.h:190
void * method_load_address
Definition: jitprofiling.h:187