1 //===-- IntelJITEventListener.cpp - Tell Intel profiler about JITed code --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a JITEventListener object to tell Intel(R) VTune(TM)
11 // Amplifier XE 2011 about JITted functions.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "llvm/ExecutionEngine/JITEventListener.h"
18 #define DEBUG_TYPE "amplifier-jit-event-listener"
19 #include "llvm/DebugInfo.h"
20 #include "llvm/Function.h"
21 #include "llvm/Metadata.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/OwningPtr.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Support/Errno.h"
28 #include "llvm/Support/ValueHandle.h"
29 #include "EventListenerCommon.h"
30 #include "IntelJITEventsWrapper.h"
33 using namespace llvm::jitprofiling;
37 class IntelJITEventListener : public JITEventListener {
38 typedef DenseMap<void*, unsigned int> MethodIDMap;
40 OwningPtr<IntelJITEventsWrapper> Wrapper;
41 MethodIDMap MethodIDs;
42 FilenameCache Filenames;
45 IntelJITEventListener(IntelJITEventsWrapper* libraryWrapper) {
46 Wrapper.reset(libraryWrapper);
49 ~IntelJITEventListener() {
52 virtual void NotifyFunctionEmitted(const Function &F,
53 void *FnStart, size_t FnSize,
54 const EmittedFunctionDetails &Details);
56 virtual void NotifyFreeingMachineCode(void *OldPtr);
59 static LineNumberInfo LineStartToIntelJITFormat(
60 uintptr_t StartAddress,
63 LineNumberInfo Result;
65 Result.Offset = Address - StartAddress;
66 Result.LineNumber = Loc.getLine();
71 static iJIT_Method_Load FunctionDescToIntelJITFormat(
72 IntelJITEventsWrapper& Wrapper,
76 iJIT_Method_Load Result;
77 memset(&Result, 0, sizeof(iJIT_Method_Load));
79 Result.method_id = Wrapper.iJIT_GetNewMethodID();
80 Result.method_name = const_cast<char*>(FnName);
81 Result.method_load_address = reinterpret_cast<void*>(FnStart);
82 Result.method_size = FnSize;
85 Result.class_file_name = NULL;
86 Result.user_data = NULL;
87 Result.user_data_size = 0;
88 Result.env = iJDE_JittingAPI;
93 // Adds the just-emitted function to the symbol table.
94 void IntelJITEventListener::NotifyFunctionEmitted(
95 const Function &F, void *FnStart, size_t FnSize,
96 const EmittedFunctionDetails &Details) {
97 iJIT_Method_Load FunctionMessage = FunctionDescToIntelJITFormat(*Wrapper,
99 reinterpret_cast<uint64_t>(FnStart),
102 std::vector<LineNumberInfo> LineInfo;
104 if (!Details.LineStarts.empty()) {
105 // Now convert the line number information from the address/DebugLoc
106 // format in Details to the offset/lineno in Intel JIT API format.
108 LineInfo.reserve(Details.LineStarts.size() + 1);
110 DebugLoc FirstLoc = Details.LineStarts[0].Loc;
111 assert(!FirstLoc.isUnknown()
112 && "LineStarts should not contain unknown DebugLocs");
114 MDNode *FirstLocScope = FirstLoc.getScope(F.getContext());
115 DISubprogram FunctionDI = getDISubprogram(FirstLocScope);
116 if (FunctionDI.Verify()) {
117 FunctionMessage.source_file_name = const_cast<char*>(
118 Filenames.getFullPath(FirstLocScope));
120 LineNumberInfo FirstLine;
121 FirstLine.Offset = 0;
122 FirstLine.LineNumber = FunctionDI.getLineNumber();
123 LineInfo.push_back(FirstLine);
126 for (std::vector<EmittedFunctionDetails::LineStart>::const_iterator I =
127 Details.LineStarts.begin(), E = Details.LineStarts.end();
129 // This implementation ignores the DebugLoc filename because the Intel
130 // JIT API does not support multiple source files associated with a single
132 LineInfo.push_back(LineStartToIntelJITFormat(
133 reinterpret_cast<uintptr_t>(FnStart),
137 // If we have no file name yet for the function, use the filename from
138 // the first instruction that has one
139 if (FunctionMessage.source_file_name == 0) {
140 MDNode *scope = I->Loc.getScope(
141 Details.MF->getFunction()->getContext());
142 FunctionMessage.source_file_name = const_cast<char*>(
143 Filenames.getFullPath(scope));
147 FunctionMessage.line_number_size = LineInfo.size();
148 FunctionMessage.line_number_table = &*LineInfo.begin();
150 FunctionMessage.line_number_size = 0;
151 FunctionMessage.line_number_table = 0;
154 Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED,
156 MethodIDs[FnStart] = FunctionMessage.method_id;
159 void IntelJITEventListener::NotifyFreeingMachineCode(void *FnStart) {
160 MethodIDMap::iterator I = MethodIDs.find(FnStart);
161 if (I != MethodIDs.end()) {
162 Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_UNLOAD_START, &I->second);
167 } // anonymous namespace.
170 JITEventListener *JITEventListener::createIntelJITEventListener() {
171 return new IntelJITEventListener(new IntelJITEventsWrapper);
175 JITEventListener *JITEventListener::createIntelJITEventListener(
176 IntelJITEventsWrapper* TestImpl) {
177 return new IntelJITEventListener(TestImpl);