swr: [rasterizer jitter] support for dumping x86 asm
authorTim Rowley <timothy.o.rowley@intel.com>
Thu, 21 Apr 2016 20:37:19 +0000 (14:37 -0600)
committerTim Rowley <timothy.o.rowley@intel.com>
Wed, 27 Apr 2016 15:41:32 +0000 (10:41 -0500)
Reviewed-by: Bruce Cherniak <bruce.cherniak@intel.com>
src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp
src/gallium/drivers/swr/rasterizer/jitter/JitManager.h

index 271c1969be6d6bc4f627edad6ad7ea560f82f1a3..d6755edf817d9b3fb449e8d51a6ad527b22ddbd7 100644 (file)
@@ -46,6 +46,8 @@
 
 #include "llvm/Analysis/CFGPrinter.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Target/TargetMachine.h"
+#include "llvm/Support/FormattedStream.h"
 
 #if LLVM_USE_INTEL_JITEVENTS
 #include "llvm/ExecutionEngine/JITEventListener.h"
@@ -250,6 +252,53 @@ bool JitManager::SetupModuleFromIR(const uint8_t *pIR)
     return true;
 }
 
+
+//////////////////////////////////////////////////////////////////////////
+/// @brief Dump function x86 assembly to file.
+/// @note This should only be called after the module has been jitted to x86 and the
+///       module will not be further accessed.
+void JitManager::DumpAsm(Function* pFunction, const char* fileName)
+{
+    if (KNOB_DUMP_SHADER_IR)
+    {
+
+#if defined(_WIN32)
+        DWORD pid = GetCurrentProcessId();
+        TCHAR procname[MAX_PATH];
+        GetModuleFileName(NULL, procname, MAX_PATH);
+        const char* pBaseName = strrchr(procname, '\\');
+        std::stringstream outDir;
+        outDir << JITTER_OUTPUT_DIR << pBaseName << "_" << pid << std::ends;
+        CreateDirectory(outDir.str().c_str(), NULL);
+#endif
+
+        std::error_code EC;
+        Module* pModule = pFunction->getParent();
+        const char *funcName = pFunction->getName().data();
+        char fName[256];
+#if defined(_WIN32)
+        sprintf(fName, "%s\\%s.%s.asm", outDir.str().c_str(), funcName, fileName);
+#else
+        sprintf(fName, "%s.%s.asm", funcName, fileName);
+#endif
+
+#if HAVE_LLVM == 0x306
+        raw_fd_ostream fd(fName, EC, llvm::sys::fs::F_None);
+        formatted_raw_ostream filestream(fd);
+#else
+        raw_fd_ostream filestream(fName, EC, llvm::sys::fs::F_None);
+#endif
+
+        legacy::PassManager* pMPasses = new legacy::PassManager();
+        auto* pTarget = mpExec->getTargetMachine();
+        pTarget->Options.MCOptions.AsmVerbose = true;
+        pTarget->addPassesToEmitFile(*pMPasses, filestream, TargetMachine::CGFT_AssemblyFile);
+        pMPasses->run(*pModule);
+        delete pMPasses;
+        pTarget->Options.MCOptions.AsmVerbose = false;
+    }
+}
+
 //////////////////////////////////////////////////////////////////////////
 /// @brief Dump function to file.
 void JitManager::DumpToFile(Function *f, const char *fileName)
index 4ffb0fbee0181b1a8e10413626e286e137eabeeb..9861eae78aa4f817d70afb533b123bbd3fdfd998 100644 (file)
@@ -184,5 +184,6 @@ struct JitManager
     void SetupNewModule();
     bool SetupModuleFromIR(const uint8_t *pIR);
 
+    void DumpAsm(Function* pFunction, const char* fileName);
     static void DumpToFile(Function *f, const char *fileName);
 };