gallivm: Fix build against LLVM 3.7 SVN r235265
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_debug.cpp
index be8d7884bca13f2ad232ae57e4c6a9623e35a472..be3e834b774877d35fa2f35b32876c0fdf722208 100644 (file)
 #include <llvm/Target/TargetMachine.h>
 #include <llvm/Target/TargetInstrInfo.h>
 #include <llvm/Support/raw_ostream.h>
+#include <llvm/Support/Format.h>
+
+#if HAVE_LLVM >= 0x0306
+#include <llvm/Target/TargetSubtargetInfo.h>
+#else
 #include <llvm/Support/MemoryObject.h>
+#endif
 
-#if HAVE_LLVM >= 0x0300
 #include <llvm/Support/TargetRegistry.h>
-#include <llvm/Support/TargetSelect.h>
-#else /* HAVE_LLVM < 0x0300 */
-#include <llvm/Target/TargetRegistry.h>
-#include <llvm/Target/TargetSelect.h>
-#endif /* HAVE_LLVM < 0x0300 */
+#include <llvm/MC/MCSubtargetInfo.h>
 
-#if HAVE_LLVM >= 0x0209
 #include <llvm/Support/Host.h>
-#else /* HAVE_LLVM < 0x0209 */
-#include <llvm/System/Host.h>
-#endif /* HAVE_LLVM < 0x0209 */
 
-#if HAVE_LLVM >= 0x0207
+#include <llvm/IR/Module.h>
+
 #include <llvm/MC/MCDisassembler.h>
 #include <llvm/MC/MCAsmInfo.h>
 #include <llvm/MC/MCInst.h>
 #include <llvm/MC/MCInstPrinter.h>
-#endif /* HAVE_LLVM >= 0x0207 */
+#include <llvm/MC/MCRegisterInfo.h>
+
+#if HAVE_LLVM >= 0x0305
+#define OwningPtr std::unique_ptr
+#else
+#include <llvm/ADT/OwningPtr.h>
+#endif
+
+#if HAVE_LLVM >= 0x0305
+#include <llvm/MC/MCContext.h>
+#endif
 
 #include "util/u_math.h"
 #include "util/u_debug.h"
 
 #include "lp_bld_debug.h"
 
+#ifdef __linux__
+#include <sys/stat.h>
+#include <fcntl.h>
+#endif
+
 
 
 /**
@@ -80,17 +93,16 @@ lp_check_alignment(const void *ptr, unsigned alignment)
 class raw_debug_ostream :
    public llvm::raw_ostream
 {
+private:
    uint64_t pos;
 
+public:
+   raw_debug_ostream() : pos(0) { }
+
    void write_impl(const char *Ptr, size_t Size);
-   uint64_t current_pos() { return pos; }
-   uint64_t current_pos() const { return pos; }
 
-#if HAVE_LLVM >= 0x207
-   uint64_t preferred_buffer_size() { return 512; }
-#else
-   size_t preferred_buffer_size() { return 512; }
-#endif
+   uint64_t current_pos() const { return pos; }
+   size_t preferred_buffer_size() const { return 512; }
 };
 
 
@@ -108,6 +120,13 @@ raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
 }
 
 
+extern "C" const char *
+lp_get_module_id(LLVMModuleRef module)
+{
+   return llvm::unwrap(module)->getModuleIdentifier().c_str();
+}
+
+
 /**
  * Same as LLVMDumpValue, but through our debugging channels.
  */
@@ -124,7 +143,8 @@ lp_debug_dump_value(LLVMValueRef value)
 }
 
 
-#if HAVE_LLVM >= 0x0207
+#if HAVE_LLVM < 0x0306
+
 /*
  * MemoryObject wrapper around a buffer of memory, to be used by MC
  * disassembler.
@@ -159,7 +179,8 @@ public:
       return 0;
    }
 };
-#endif /* HAVE_LLVM >= 0x0207 */
+
+#endif /* HAVE_LLVM < 0x0306 */
 
 
 /*
@@ -169,10 +190,9 @@ public:
  * - http://blog.llvm.org/2010/01/x86-disassembler.html
  * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
  */
-extern "C" void
-lp_disassemble(const void* func)
+static size_t
+disassemble(const void* func, llvm::raw_ostream & Out)
 {
-#if HAVE_LLVM >= 0x0207
    using namespace llvm;
 
    const uint8_t *bytes = (const uint8_t *)func;
@@ -180,7 +200,7 @@ lp_disassemble(const void* func)
    /*
     * Limit disassembly to this extent
     */
-   const uint64_t extent = 0x10000;
+   const uint64_t extent = 96 * 1024;
 
    uint64_t max_pc = 0;
 
@@ -188,77 +208,69 @@ lp_disassemble(const void* func)
     * Initialize all used objects.
     */
 
-#if HAVE_LLVM >= 0x0301
    std::string Triple = sys::getDefaultTargetTriple();
-#else
-   std::string Triple = sys::getHostTriple();
-#endif
 
    std::string Error;
    const Target *T = TargetRegistry::lookupTarget(Triple, Error);
 
-#if HAVE_LLVM >= 0x0208
-   InitializeNativeTargetAsmPrinter();
-#elif LLVM_NATIVE_ARCH == X86Target
-   LLVMInitializeX86AsmPrinter();
-#elif LLVM_NATIVE_ARCH == ARMTarget
-   LLVMInitializeARMAsmPrinter();
-#endif
-
-#if (LLVM_NATIVE_ARCH == X86 || LLVM_NATIVE_ARCH == X86Target)
-   LLVMInitializeX86Disassembler();
-#elif (LLVM_NATIVE_ARCH == ARM || LLVM_NATIVE_ARCH == ARMTarget)
-   LLVMInitializeARMDisassembler();
-#endif
-
-#if HAVE_LLVM >= 0x0300
-   OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
+#if HAVE_LLVM >= 0x0304
+   OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(*T->createMCRegInfo(Triple), Triple));
 #else
-   OwningPtr<const MCAsmInfo> AsmInfo(T->createAsmInfo(Triple));
+   OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
 #endif
 
    if (!AsmInfo) {
-      debug_printf("error: no assembly info for target %s\n", Triple.c_str());
-      return;
+      Out << "error: no assembly info for target " << Triple << "\n";
+      Out.flush();
+      return 0;
    }
 
-#if HAVE_LLVM >= 0x0300
-   const MCSubtargetInfo *STI = T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), "");
-   OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
-#else 
-   OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler());
-#endif 
-   if (!DisAsm) {
-      debug_printf("error: no disassembler for target %s\n", Triple.c_str());
-      return;
+   unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
+
+   OwningPtr<const MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
+   if (!MRI) {
+      Out << "error: no register info for target " << Triple.c_str() << "\n";
+      Out.flush();
+      return 0;
    }
 
-   raw_debug_ostream Out;
+   OwningPtr<const MCInstrInfo> MII(T->createMCInstrInfo());
+   if (!MII) {
+      Out << "error: no instruction info for target " << Triple.c_str() << "\n";
+      Out.flush();
+      return 0;
+   }
 
-#if HAVE_LLVM >= 0x0300
-   unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
+#if HAVE_LLVM >= 0x0305
+   OwningPtr<const MCSubtargetInfo> STI(T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), ""));
+   OwningPtr<MCContext> MCCtx(new MCContext(AsmInfo.get(), MRI.get(), 0));
+   OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI, *MCCtx));
 #else
-   int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
+   OwningPtr<const MCSubtargetInfo> STI(T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), ""));
+   OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
 #endif
+   if (!DisAsm) {
+      Out << "error: no disassembler for target " << Triple << "\n";
+      Out.flush();
+      return 0;
+   }
 
-#if HAVE_LLVM >= 0x0300
-   OwningPtr<MCInstPrinter> Printer(
-         T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *STI));
-#elif HAVE_LLVM >= 0x0208
+
+#if HAVE_LLVM >= 0x0307
    OwningPtr<MCInstPrinter> Printer(
-         T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo));
+         T->createMCInstPrinter(llvm::Triple(Triple), AsmPrinterVariant, *AsmInfo, *MII, *MRI));
 #else
    OwningPtr<MCInstPrinter> Printer(
-         T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out));
+         T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
 #endif
    if (!Printer) {
-      debug_printf("error: no instruction printer for target %s\n", Triple.c_str());
-      return;
+      Out << "error: no instruction printer for target " << Triple.c_str() << "\n";
+      Out.flush();
+      return 0;
    }
 
-#if HAVE_LLVM >= 0x0301
    TargetOptions options;
-#if defined(DEBUG)
+#if defined(DEBUG) && HAVE_LLVM < 0x0307
    options.JITEmitDebugInfo = true;
 #endif
 #if defined(PIPE_ARCH_X86)
@@ -267,19 +279,16 @@ lp_disassemble(const void* func)
 #if defined(DEBUG) || defined(PROFILE)
    options.NoFramePointerElim = true;
 #endif
-   TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "", options);
-#elif HAVE_LLVM == 0x0300
-   TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "");
-#else
-   TargetMachine *TM = T->createTargetMachine(Triple, "");
-#endif
-
-   const TargetInstrInfo *TII = TM->getInstrInfo();
+   OwningPtr<TargetMachine> TM(T->createTargetMachine(Triple, sys::getHostCPUName(), "", options));
 
    /*
     * Wrap the data in a MemoryObject
     */
+#if HAVE_LLVM >= 0x0306
+   ArrayRef<uint8_t> memoryObject((const uint8_t *)bytes, extent);
+#else
    BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
+#endif
 
    uint64_t pc;
    pc = 0;
@@ -292,16 +301,12 @@ lp_disassemble(const void* func)
        * so that between runs.
        */
 
-      debug_printf("%6lu:\t", (unsigned long)pc);
+      Out << llvm::format("%6lu:\t", (unsigned long)pc);
 
       if (!DisAsm->getInstruction(Inst, Size, memoryObject,
                                  pc,
-#if HAVE_LLVM >= 0x0300
                                  nulls(), nulls())) {
-#else
-                                 nulls())) {
-#endif
-         debug_printf("invalid\n");
+         Out << "invalid";
          pc += 1;
       }
 
@@ -312,25 +317,21 @@ lp_disassemble(const void* func)
       if (0) {
          unsigned i;
          for (i = 0; i < Size; ++i) {
-            debug_printf("%02x ", ((const uint8_t*)bytes)[pc + i]);
+            Out << llvm::format("%02x ", ((const uint8_t*)bytes)[pc + i]);
          }
          for (; i < 16; ++i) {
-            debug_printf("   ");
+            Out << "   ";
          }
       }
 
       /*
        * Print the instruction.
        */
-
-#if HAVE_LLVM >= 0x0300
-      Printer->printInst(&Inst, Out, "");
-#elif HAVE_LLVM >= 0x208
-      Printer->printInst(&Inst, Out);
+#if HAVE_LLVM >= 0x0307
+      Printer->printInst(&Inst, Out, "", *STI);
 #else
-      Printer->printInst(&Inst);
+      Printer->printInst(&Inst, Out, "");
 #endif
-      Out.flush();
 
       /*
        * Advance.
@@ -338,11 +339,7 @@ lp_disassemble(const void* func)
 
       pc += Size;
 
-#if HAVE_LLVM >= 0x0300
-      const MCInstrDesc &TID = TII->get(Inst.getOpcode());
-#else
-      const TargetInstrDesc &TID = TII->get(Inst.getOpcode());
-#endif
+      const MCInstrDesc &TID = MII->get(Inst.getOpcode());
 
       /*
        * Keep track of forward jumps to a nearby address.
@@ -378,7 +375,7 @@ lp_disassemble(const void* func)
                 * Output the address relative to the function start, given
                 * that MC will print the addresses relative the current pc.
                 */
-               debug_printf("\t\t; %lu", (unsigned long)jump);
+               Out << "\t\t; " << jump;
 
                /*
                 * Ignore far jumps given it could be actually a tail return to
@@ -393,7 +390,7 @@ lp_disassemble(const void* func)
          }
       }
 
-      debug_printf("\n");
+      Out << "\n";
 
       /*
        * Stop disassembling on return statements, if there is no record of a
@@ -405,19 +402,81 @@ lp_disassemble(const void* func)
             break;
          }
       }
+
+      if (pc >= extent) {
+         Out << "disassembly larger than " << extent << "bytes, aborting\n";
+         break;
+      }
    }
 
+   Out << "\n";
+   Out.flush();
+
    /*
     * Print GDB command, useful to verify output.
     */
-
    if (0) {
-      debug_printf("disassemble %p %p\n", bytes, bytes + pc);
+      _debug_printf("disassemble %p %p\n", bytes, bytes + pc);
    }
 
-   debug_printf("\n");
-#else /* HAVE_LLVM < 0x0207 */
+   return pc;
+}
+
+
+extern "C" void
+lp_disassemble(LLVMValueRef func, const void *code) {
+   raw_debug_ostream Out;
+   Out << LLVMGetValueName(func) << ":\n";
+   disassemble(code, Out);
+}
+
+
+/*
+ * Linux perf profiler integration.
+ *
+ * See also:
+ * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
+ * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
+ * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
+ */
+extern "C" void
+lp_profile(LLVMValueRef func, const void *code)
+{
+#if defined(__linux__) && (defined(DEBUG) || defined(PROFILE))
+   static boolean first_time = TRUE;
+   static FILE *perf_map_file = NULL;
+   static int perf_asm_fd = -1;
+   if (first_time) {
+      /*
+       * We rely on the disassembler for determining a function's size, but
+       * the disassembly is a leaky and slow operation, so avoid running
+       * this except when running inside linux perf, which can be inferred
+       * by the PERF_BUILDID_DIR environment variable.
+       */
+      if (getenv("PERF_BUILDID_DIR")) {
+         pid_t pid = getpid();
+         char filename[256];
+         util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid);
+         perf_map_file = fopen(filename, "wt");
+         util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid);
+         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
+         perf_asm_fd = open(filename, O_WRONLY | O_CREAT, mode);
+      }
+      first_time = FALSE;
+   }
+   if (perf_map_file) {
+      const char *symbol = LLVMGetValueName(func);
+      unsigned long addr = (uintptr_t)code;
+      llvm::raw_fd_ostream Out(perf_asm_fd, false);
+      Out << symbol << ":\n";
+      unsigned long size = disassemble(code, Out);
+      fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);
+      fflush(perf_map_file);
+   }
+#else
    (void)func;
-#endif /* HAVE_LLVM < 0x0207 */
+   (void)code;
+#endif
 }
 
+