gallivm: set mcpu when initializing llvm execution engine
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_debug.cpp
1 /**************************************************************************
2 *
3 * Copyright 2009-2011 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stddef.h>
29
30 #include <llvm-c/Core.h>
31 #include <llvm/Target/TargetMachine.h>
32 #include <llvm/Target/TargetInstrInfo.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/Support/Format.h>
35 #include <llvm/Support/MemoryObject.h>
36
37 #include <llvm/Support/TargetRegistry.h>
38 #include <llvm/MC/MCSubtargetInfo.h>
39
40 #include <llvm/Support/Host.h>
41
42 #if HAVE_LLVM >= 0x0303
43 #include <llvm/IR/Module.h>
44 #else
45 #include <llvm/Module.h>
46 #endif
47
48 #include <llvm/MC/MCDisassembler.h>
49 #include <llvm/MC/MCAsmInfo.h>
50 #include <llvm/MC/MCInst.h>
51 #include <llvm/MC/MCInstPrinter.h>
52 #include <llvm/MC/MCRegisterInfo.h>
53
54 #if HAVE_LLVM >= 0x0303
55 #include <llvm/ADT/OwningPtr.h>
56 #endif
57
58 #if HAVE_LLVM >= 0x0305
59 #include <llvm/MC/MCContext.h>
60 #endif
61
62 #include "util/u_math.h"
63 #include "util/u_debug.h"
64
65 #include "lp_bld_debug.h"
66
67 #ifdef __linux__
68 #include <sys/stat.h>
69 #include <fcntl.h>
70 #endif
71
72
73
74 /**
75 * Check alignment.
76 *
77 * It is important that this check is not implemented as a macro or inlined
78 * function, as the compiler assumptions in respect to alignment of global
79 * and stack variables would often make the check a no op, defeating the
80 * whole purpose of the exercise.
81 */
82 extern "C" boolean
83 lp_check_alignment(const void *ptr, unsigned alignment)
84 {
85 assert(util_is_power_of_two(alignment));
86 return ((uintptr_t)ptr & (alignment - 1)) == 0;
87 }
88
89
90 class raw_debug_ostream :
91 public llvm::raw_ostream
92 {
93 private:
94 uint64_t pos;
95
96 public:
97 raw_debug_ostream() : pos(0) { }
98
99 void write_impl(const char *Ptr, size_t Size);
100
101 uint64_t current_pos() const { return pos; }
102 size_t preferred_buffer_size() const { return 512; }
103 };
104
105
106 void
107 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
108 {
109 if (Size > 0) {
110 char *lastPtr = (char *)&Ptr[Size];
111 char last = *lastPtr;
112 *lastPtr = 0;
113 _debug_printf("%*s", Size, Ptr);
114 *lastPtr = last;
115 pos += Size;
116 }
117 }
118
119
120 extern "C" const char *
121 lp_get_module_id(LLVMModuleRef module)
122 {
123 return llvm::unwrap(module)->getModuleIdentifier().c_str();
124 }
125
126
127 /**
128 * Same as LLVMDumpValue, but through our debugging channels.
129 */
130 extern "C" void
131 lp_debug_dump_value(LLVMValueRef value)
132 {
133 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
134 raw_debug_ostream os;
135 llvm::unwrap(value)->print(os);
136 os.flush();
137 #else
138 LLVMDumpValue(value);
139 #endif
140 }
141
142
143 /*
144 * MemoryObject wrapper around a buffer of memory, to be used by MC
145 * disassembler.
146 */
147 class BufferMemoryObject:
148 public llvm::MemoryObject
149 {
150 private:
151 const uint8_t *Bytes;
152 uint64_t Length;
153 public:
154 BufferMemoryObject(const uint8_t *bytes, uint64_t length) :
155 Bytes(bytes), Length(length)
156 {
157 }
158
159 uint64_t getBase() const
160 {
161 return 0;
162 }
163
164 uint64_t getExtent() const
165 {
166 return Length;
167 }
168
169 int readByte(uint64_t addr, uint8_t *byte) const
170 {
171 if (addr > getExtent())
172 return -1;
173 *byte = Bytes[addr];
174 return 0;
175 }
176 };
177
178
179 /*
180 * Disassemble a function, using the LLVM MC disassembler.
181 *
182 * See also:
183 * - http://blog.llvm.org/2010/01/x86-disassembler.html
184 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
185 */
186 static size_t
187 disassemble(const void* func, llvm::raw_ostream & Out)
188 {
189 using namespace llvm;
190
191 const uint8_t *bytes = (const uint8_t *)func;
192
193 /*
194 * Limit disassembly to this extent
195 */
196 const uint64_t extent = 96 * 1024;
197
198 uint64_t max_pc = 0;
199
200 /*
201 * Initialize all used objects.
202 */
203
204 std::string Triple = sys::getDefaultTargetTriple();
205
206 std::string Error;
207 const Target *T = TargetRegistry::lookupTarget(Triple, Error);
208
209 #if HAVE_LLVM >= 0x0304
210 OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(*T->createMCRegInfo(Triple), Triple));
211 #else
212 OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
213 #endif
214
215 if (!AsmInfo) {
216 Out << "error: no assembly info for target " << Triple << "\n";
217 Out.flush();
218 return 0;
219 }
220
221 unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
222
223 OwningPtr<const MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
224 if (!MRI) {
225 Out << "error: no register info for target " << Triple.c_str() << "\n";
226 Out.flush();
227 return 0;
228 }
229
230 OwningPtr<const MCInstrInfo> MII(T->createMCInstrInfo());
231 if (!MII) {
232 Out << "error: no instruction info for target " << Triple.c_str() << "\n";
233 Out.flush();
234 return 0;
235 }
236
237 #if HAVE_LLVM >= 0x0305
238 OwningPtr<const MCSubtargetInfo> STI(T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), ""));
239 OwningPtr<MCContext> MCCtx(new MCContext(AsmInfo.get(), MRI.get(), 0));
240 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI, *MCCtx));
241 #else
242 OwningPtr<const MCSubtargetInfo> STI(T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), ""));
243 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
244 #endif
245 if (!DisAsm) {
246 Out << "error: no disassembler for target " << Triple << "\n";
247 Out.flush();
248 return 0;
249 }
250
251
252 OwningPtr<MCInstPrinter> Printer(
253 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
254 if (!Printer) {
255 Out << "error: no instruction printer for target " << Triple.c_str() << "\n";
256 Out.flush();
257 return 0;
258 }
259
260 TargetOptions options;
261 #if defined(DEBUG)
262 options.JITEmitDebugInfo = true;
263 #endif
264 #if defined(PIPE_ARCH_X86)
265 options.StackAlignmentOverride = 4;
266 #endif
267 #if defined(DEBUG) || defined(PROFILE)
268 options.NoFramePointerElim = true;
269 #endif
270 OwningPtr<TargetMachine> TM(T->createTargetMachine(Triple, sys::getHostCPUName(), "", options));
271
272 const TargetInstrInfo *TII = TM->getInstrInfo();
273
274 /*
275 * Wrap the data in a MemoryObject
276 */
277 BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
278
279 uint64_t pc;
280 pc = 0;
281 while (true) {
282 MCInst Inst;
283 uint64_t Size;
284
285 /*
286 * Print address. We use addresses relative to the start of the function,
287 * so that between runs.
288 */
289
290 Out << llvm::format("%6lu:\t", (unsigned long)pc);
291
292 if (!DisAsm->getInstruction(Inst, Size, memoryObject,
293 pc,
294 nulls(), nulls())) {
295 Out << "invalid";
296 pc += 1;
297 }
298
299 /*
300 * Output the bytes in hexidecimal format.
301 */
302
303 if (0) {
304 unsigned i;
305 for (i = 0; i < Size; ++i) {
306 Out << llvm::format("%02x ", ((const uint8_t*)bytes)[pc + i]);
307 }
308 for (; i < 16; ++i) {
309 Out << " ";
310 }
311 }
312
313 /*
314 * Print the instruction.
315 */
316 Printer->printInst(&Inst, Out, "");
317
318 /*
319 * Advance.
320 */
321
322 pc += Size;
323
324 const MCInstrDesc &TID = TII->get(Inst.getOpcode());
325
326 /*
327 * Keep track of forward jumps to a nearby address.
328 */
329
330 if (TID.isBranch()) {
331 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
332 const MCOperand &operand = Inst.getOperand(i);
333 if (operand.isImm()) {
334 uint64_t jump;
335
336 /*
337 * FIXME: Handle both relative and absolute addresses correctly.
338 * EDInstInfo actually has this info, but operandTypes and
339 * operandFlags enums are not exposed in the public interface.
340 */
341
342 if (1) {
343 /*
344 * PC relative addr.
345 */
346
347 jump = pc + operand.getImm();
348 } else {
349 /*
350 * Absolute addr.
351 */
352
353 jump = (uint64_t)operand.getImm();
354 }
355
356 /*
357 * Output the address relative to the function start, given
358 * that MC will print the addresses relative the current pc.
359 */
360 Out << "\t\t; " << jump;
361
362 /*
363 * Ignore far jumps given it could be actually a tail return to
364 * a random address.
365 */
366
367 if (jump > max_pc &&
368 jump < extent) {
369 max_pc = jump;
370 }
371 }
372 }
373 }
374
375 Out << "\n";
376
377 /*
378 * Stop disassembling on return statements, if there is no record of a
379 * jump to a successive address.
380 */
381
382 if (TID.isReturn()) {
383 if (pc > max_pc) {
384 break;
385 }
386 }
387 }
388
389 /*
390 * Print GDB command, useful to verify output.
391 */
392
393 if (0) {
394 _debug_printf("disassemble %p %p\n", bytes, bytes + pc);
395 }
396
397 Out << "\n";
398 Out.flush();
399
400 return pc;
401 }
402
403
404 extern "C" void
405 lp_disassemble(LLVMValueRef func, const void *code) {
406 raw_debug_ostream Out;
407 disassemble(code, Out);
408 }
409
410
411 /*
412 * Linux perf profiler integration.
413 *
414 * See also:
415 * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
416 * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
417 * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
418 */
419 extern "C" void
420 lp_profile(LLVMValueRef func, const void *code)
421 {
422 #if defined(__linux__) && (defined(DEBUG) || defined(PROFILE))
423 static boolean first_time = TRUE;
424 static FILE *perf_map_file = NULL;
425 static int perf_asm_fd = -1;
426 if (first_time) {
427 /*
428 * We rely on the disassembler for determining a function's size, but
429 * the disassembly is a leaky and slow operation, so avoid running
430 * this except when running inside linux perf, which can be inferred
431 * by the PERF_BUILDID_DIR environment variable.
432 */
433 if (getenv("PERF_BUILDID_DIR")) {
434 pid_t pid = getpid();
435 char filename[256];
436 util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid);
437 perf_map_file = fopen(filename, "wt");
438 util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid);
439 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
440 perf_asm_fd = open(filename, O_WRONLY | O_CREAT, mode);
441 }
442 first_time = FALSE;
443 }
444 if (perf_map_file) {
445 const char *symbol = LLVMGetValueName(func);
446 unsigned long addr = (uintptr_t)code;
447 llvm::raw_fd_ostream Out(perf_asm_fd, false);
448 Out << symbol << ":\n";
449 unsigned long size = disassemble(code, Out);
450 fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);
451 fflush(perf_map_file);
452 }
453 #else
454 (void)func;
455 (void)code;
456 #endif
457 }
458
459