From fa36f8ec9ce492d65eab8ad599897b3e3045094a Mon Sep 17 00:00:00 2001 From: Tim Rowley Date: Thu, 21 Apr 2016 14:37:19 -0600 Subject: [PATCH] swr: [rasterizer jitter] support for dumping x86 asm Reviewed-by: Bruce Cherniak --- .../swr/rasterizer/jitter/JitManager.cpp | 49 +++++++++++++++++++ .../swr/rasterizer/jitter/JitManager.h | 1 + 2 files changed, 50 insertions(+) diff --git a/src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp b/src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp index 271c1969be6..d6755edf817 100644 --- a/src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp +++ b/src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp @@ -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) diff --git a/src/gallium/drivers/swr/rasterizer/jitter/JitManager.h b/src/gallium/drivers/swr/rasterizer/jitter/JitManager.h index 4ffb0fbee01..9861eae78aa 100644 --- a/src/gallium/drivers/swr/rasterizer/jitter/JitManager.h +++ b/src/gallium/drivers/swr/rasterizer/jitter/JitManager.h @@ -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); }; -- 2.30.2