Try to fix the build of IntelJITEventListener.
[oota-llvm.git] / lib / ExecutionEngine / IntelJITEvents / IntelJITEventListener.cpp
1 //===-- IntelJITEventListener.cpp - Tell Intel profiler about JITed code --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a JITEventListener object to tell Intel(R) VTune(TM)
11 // Amplifier XE 2011 about JITted functions.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/Config/config.h"
16 #include "IntelJITEventsWrapper.h"
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/DebugInfo/DIContext.h"
20 #include "llvm/DebugInfo/DWARF/DWARFContext.h"
21 #include "llvm/ExecutionEngine/JITEventListener.h"
22 #include "llvm/IR/DebugInfo.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Metadata.h"
25 #include "llvm/IR/ValueHandle.h"
26 #include "llvm/Object/ObjectFile.h"
27 #include "llvm/Object/SymbolSize.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Errno.h"
30 #include "llvm/Support/raw_ostream.h"
31
32 using namespace llvm;
33 using namespace llvm::object;
34
35 #define DEBUG_TYPE "amplifier-jit-event-listener"
36
37 namespace {
38
39 class IntelJITEventListener : public JITEventListener {
40   typedef DenseMap<void*, unsigned int> MethodIDMap;
41
42   std::unique_ptr<IntelJITEventsWrapper> Wrapper;
43   MethodIDMap MethodIDs;
44
45   typedef SmallVector<const void *, 64> MethodAddressVector;
46   typedef DenseMap<const void *, MethodAddressVector>  ObjectMap;
47
48   ObjectMap  LoadedObjectMap;
49   std::map<const char*, OwningBinary<ObjectFile>> DebugObjects;
50
51 public:
52   IntelJITEventListener(IntelJITEventsWrapper* libraryWrapper) {
53       Wrapper.reset(libraryWrapper);
54   }
55
56   ~IntelJITEventListener() {
57   }
58
59   void NotifyObjectEmitted(const ObjectFile &Obj,
60                            const RuntimeDyld::LoadedObjectInfo &L) override;
61
62   void NotifyFreeingObject(const ObjectFile &Obj) override;
63 };
64
65 static LineNumberInfo DILineInfoToIntelJITFormat(uintptr_t StartAddress,
66                                                  uintptr_t Address,
67                                                  DILineInfo Line) {
68   LineNumberInfo Result;
69
70   Result.Offset = Address - StartAddress;
71   Result.LineNumber = Line.Line;
72
73   return Result;
74 }
75
76 static iJIT_Method_Load FunctionDescToIntelJITFormat(
77     IntelJITEventsWrapper& Wrapper,
78     const char* FnName,
79     uintptr_t FnStart,
80     size_t FnSize) {
81   iJIT_Method_Load Result;
82   memset(&Result, 0, sizeof(iJIT_Method_Load));
83
84   Result.method_id = Wrapper.iJIT_GetNewMethodID();
85   Result.method_name = const_cast<char*>(FnName);
86   Result.method_load_address = reinterpret_cast<void*>(FnStart);
87   Result.method_size = FnSize;
88
89   Result.class_id = 0;
90   Result.class_file_name = NULL;
91   Result.user_data = NULL;
92   Result.user_data_size = 0;
93   Result.env = iJDE_JittingAPI;
94
95   return Result;
96 }
97
98 void IntelJITEventListener::NotifyObjectEmitted(
99                                        const ObjectFile &Obj,
100                                        const RuntimeDyld::LoadedObjectInfo &L) {
101
102   OwningBinary<ObjectFile> DebugObjOwner = L.getObjectForDebug(Obj);
103   const ObjectFile &DebugObj = *DebugObjOwner.getBinary();
104
105   // Get the address of the object image for use as a unique identifier
106   const void* ObjData = DebugObj.getData().data();
107   DIContext* Context = new DWARFContextInMemory(DebugObj);
108   MethodAddressVector Functions;
109
110   // Use symbol info to iterate functions in the object.
111   for (const std::pair<SymbolRef, uint64_t> &P : computeSymbolSizes(DebugObj)) {
112     SymbolRef Sym = P.first;
113     std::vector<LineNumberInfo> LineInfo;
114     std::string SourceFileName;
115
116     if (Sym.getType() == SymbolRef::ST_Function) {
117       ErrorOr<StringRef> Name = Sym.getName();
118       if (!Name)
119         continue;
120
121       uint64_t Addr;
122       if (Sym.getAddress(Addr))
123         continue;
124       uint64_t Size = P.second;
125
126       // Record this address in a local vector
127       Functions.push_back((void*)Addr);
128
129       // Build the function loaded notification message
130       iJIT_Method_Load FunctionMessage =
131           FunctionDescToIntelJITFormat(*Wrapper, Name->data(), Addr, Size);
132       if (Context) {
133         DILineInfoTable  Lines = Context->getLineInfoForAddressRange(Addr, Size);
134         DILineInfoTable::iterator  Begin = Lines.begin();
135         DILineInfoTable::iterator  End = Lines.end();
136         for (DILineInfoTable::iterator It = Begin; It != End; ++It) {
137           LineInfo.push_back(DILineInfoToIntelJITFormat((uintptr_t)Addr,
138                                                         It->first,
139                                                         It->second));
140         }
141         if (LineInfo.size() == 0) {
142           FunctionMessage.source_file_name = 0;
143           FunctionMessage.line_number_size = 0;
144           FunctionMessage.line_number_table = 0;
145         } else {
146           // Source line information for the address range is provided as 
147           // a code offset for the start of the corresponding sub-range and
148           // a source line. JIT API treats offsets in LineNumberInfo structures
149           // as the end of the corresponding code region. The start of the code
150           // is taken from the previous element. Need to shift the elements.
151
152           LineNumberInfo last = LineInfo.back();
153           last.Offset = FunctionMessage.method_size;
154           LineInfo.push_back(last);
155           for (size_t i = LineInfo.size() - 2; i > 0; --i)
156             LineInfo[i].LineNumber = LineInfo[i - 1].LineNumber;
157
158           SourceFileName = Lines.front().second.FileName;
159           FunctionMessage.source_file_name = const_cast<char *>(SourceFileName.c_str());
160           FunctionMessage.line_number_size = LineInfo.size();
161           FunctionMessage.line_number_table = &*LineInfo.begin();
162         }
163       } else {
164         FunctionMessage.source_file_name = 0;
165         FunctionMessage.line_number_size = 0;
166         FunctionMessage.line_number_table = 0;
167       }
168
169       Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_LOAD_FINISHED,
170                                 &FunctionMessage);
171       MethodIDs[(void*)Addr] = FunctionMessage.method_id;
172     }
173   }
174
175   // To support object unload notification, we need to keep a list of
176   // registered function addresses for each loaded object.  We will
177   // use the MethodIDs map to get the registered ID for each function.
178   LoadedObjectMap[ObjData] = Functions;
179   DebugObjects[Obj.getData().data()] = std::move(DebugObjOwner);
180 }
181
182 void IntelJITEventListener::NotifyFreeingObject(const ObjectFile &Obj) {
183   // This object may not have been registered with the listener. If it wasn't,
184   // bail out.
185   if (DebugObjects.find(Obj.getData().data()) == DebugObjects.end())
186     return;
187
188   // Get the address of the object image for use as a unique identifier
189   const ObjectFile &DebugObj = *DebugObjects[Obj.getData().data()].getBinary();
190   const void* ObjData = DebugObj.getData().data();
191
192   // Get the object's function list from LoadedObjectMap
193   ObjectMap::iterator OI = LoadedObjectMap.find(ObjData);
194   if (OI == LoadedObjectMap.end())
195     return;
196   MethodAddressVector& Functions = OI->second;
197
198   // Walk the function list, unregistering each function
199   for (MethodAddressVector::iterator FI = Functions.begin(),
200                                      FE = Functions.end();
201        FI != FE;
202        ++FI) {
203     void* FnStart = const_cast<void*>(*FI);
204     MethodIDMap::iterator MI = MethodIDs.find(FnStart);
205     if (MI != MethodIDs.end()) {
206       Wrapper->iJIT_NotifyEvent(iJVM_EVENT_TYPE_METHOD_UNLOAD_START,
207                                 &MI->second);
208       MethodIDs.erase(MI);
209     }
210   }
211
212   // Erase the object from LoadedObjectMap
213   LoadedObjectMap.erase(OI);
214   DebugObjects.erase(Obj.getData().data());
215 }
216
217 }  // anonymous namespace.
218
219 namespace llvm {
220 JITEventListener *JITEventListener::createIntelJITEventListener() {
221   return new IntelJITEventListener(new IntelJITEventsWrapper);
222 }
223
224 // for testing
225 JITEventListener *JITEventListener::createIntelJITEventListener(
226                                       IntelJITEventsWrapper* TestImpl) {
227   return new IntelJITEventListener(TestImpl);
228 }
229
230 } // namespace llvm
231