From 77134db336f71be88552a189357275ce346c7fc9 Mon Sep 17 00:00:00 2001 From: foreman Date: Tue, 30 Aug 2016 17:47:57 -0400 Subject: [PATCH] P4 to Git Change 1308882 by lmoriche@lmoriche_opencl_dev on 2016/08/30 17:43:27 SWDEV-101678 - Create a new instance of the ROCm-OpenCL-Driver for each call to compileImpl and linkImpl. Affected files ... ... //depot/stg/opencl/drivers/opencl/runtime/device/device.cpp#202 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/roccompiler.cpp#11 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocdevice.cpp#12 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocdevice.hpp#5 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprogram.cpp#18 edit ... //depot/stg/opencl/drivers/opencl/runtime/device/rocm/rocprogram.hpp#9 edit [ROCm/clr commit: bac3dbc7a81c165ee8d81b48b4d8972b4d20ae6d] --- projects/clr/rocclr/runtime/device/device.cpp | 12 ++--- .../runtime/device/rocm/roccompiler.cpp | 2 +- .../rocclr/runtime/device/rocm/rocdevice.cpp | 39 +------------- .../rocclr/runtime/device/rocm/rocdevice.hpp | 2 +- .../rocclr/runtime/device/rocm/rocprogram.cpp | 51 ++++++++++++++++++- .../rocclr/runtime/device/rocm/rocprogram.hpp | 2 + 6 files changed, 59 insertions(+), 49 deletions(-) diff --git a/projects/clr/rocclr/runtime/device/device.cpp b/projects/clr/rocclr/runtime/device/device.cpp index da8b95710a..943dc516de 100644 --- a/projects/clr/rocclr/runtime/device/device.cpp +++ b/projects/clr/rocclr/runtime/device/device.cpp @@ -140,21 +140,19 @@ Device::BlitProgram::create(amd::Device* device, } // Build all kernels -#if defined(WITH_LIGHTNING_COMPILER) - std::string opt = ""; -#else // !defined(WITH_LIGHTNING_COMPILER) - std::string opt = "-Wf,--force_disable_spir -fno-lib-no-inline "\ - "-fno-sc-keep-calls -cl-internal-kernel "; + std::string opt = "-cl-internal-kernel " +#if !defined(WITH_LIGHTNING_COMPILER) + "-Wf,--force_disable_spir -fno-lib-no-inline " \ + "-fno-sc-keep-calls " #endif // !defined(WITH_LIGHTNING_COMPILER) + ; if (extraOptions != NULL) { opt += extraOptions; } -#if !defined(WITH_LIGHTNING_COMPILER) if (!GPU_DUMP_BLIT_KERNELS) { opt += " -fno-enable-dump"; } -#endif // !defined(WITH_LIGHTNING_COMPILER) if (CL_SUCCESS != program_->build(devices, opt.c_str(), NULL, NULL, GPU_DUMP_BLIT_KERNELS)) { return false; diff --git a/projects/clr/rocclr/runtime/device/rocm/roccompiler.cpp b/projects/clr/rocclr/runtime/device/rocm/roccompiler.cpp index d536137a39..ad733f1475 100644 --- a/projects/clr/rocclr/runtime/device/rocm/roccompiler.cpp +++ b/projects/clr/rocclr/runtime/device/rocm/roccompiler.cpp @@ -41,7 +41,7 @@ HSAILProgram::compileImpl_LC(const std::string& sourceCode, amd::option::Options* options) { using namespace amd::opencl_driver; - Compiler* C = device().compiler(); + std::auto_ptr C(newCompilerInstance()); std::vector inputs; if (options->isDumpFlagSet(amd::option::DUMP_CL)) { diff --git a/projects/clr/rocclr/runtime/device/rocm/rocdevice.cpp b/projects/clr/rocclr/runtime/device/rocm/rocdevice.cpp index a7d57d3e7f..60cbb5788b 100644 --- a/projects/clr/rocclr/runtime/device/rocm/rocdevice.cpp +++ b/projects/clr/rocclr/runtime/device/rocm/rocdevice.cpp @@ -37,11 +37,6 @@ #include #endif // WITHOUT_HSA_BACKEND -#if defined(ATI_OS_LINUX) -#include -#include -#endif // defined(ATI_OS_LINUX) - #define OPENCL_VERSION_STR XSTR(OPENCL_MAJOR) "." XSTR(OPENCL_MINOR) #ifndef WITHOUT_HSA_BACKEND @@ -235,39 +230,7 @@ Device::~Device() } } bool NullDevice::initCompiler(bool isOffline) { -#if defined(WITH_LIGHTNING_COMPILER) - if (!compilerHandle_) { - std::string llvmbin = amd::Os::getEnvironment("LLVM_BIN"); -#if defined(ATI_OS_LINUX) - // FIXME_Wilkin: When no LLVM_BIN defined, use the default path - if (llvmbin.empty()) { - Dl_info info; - if (dladdr((const void*)&roc::NullDevice::initCompiler, &info)) { - llvmbin = dirname(strdup(info.dli_fname)); - size_t pos = llvmbin.rfind("lib"); - if (pos != std::string::npos) { - llvmbin.replace(pos, 3, "bin"); - } - } - } -#if defined(DEBUG) - std::string clangbin(llvmbin); - clangbin.append("/clang"); - struct stat buf; - if (stat(clangbin.c_str(), &buf)) { - std::string msg("Could not find the Clang binary in "); - msg.append(llvmbin); - LogWarning(msg.c_str()); - } -#endif // defined(DEBUG) -#endif // defined(ATI_OS_LINUX) - compilerHandle_ = amd::opencl_driver::CompilerFactory() - .CreateAMDGPUCompiler(llvmbin); - if (!compilerHandle_) { - return false; - } - } -#else // !defined(WITH_LIGHTNING_COMPILER) +#if !defined(WITH_LIGHTNING_COMPILER) // Initializes g_complibModule and g_complibApi if they were not initialized if( g_complibModule == NULL ){ if (!LoadCompLib(isOffline)) { diff --git a/projects/clr/rocclr/runtime/device/rocm/rocdevice.hpp b/projects/clr/rocclr/runtime/device/rocm/rocdevice.hpp index 71d4a06d59..96b0e29339 100644 --- a/projects/clr/rocclr/runtime/device/rocm/rocdevice.hpp +++ b/projects/clr/rocclr/runtime/device/rocm/rocdevice.hpp @@ -77,7 +77,7 @@ public: //! Destructor for the Null device virtual ~NullDevice(); - Compiler *compiler() const { return compilerHandle_; } + Compiler* compiler() const { return compilerHandle_; } //! Construct an HSAIL program object from the ELF assuming it is valid virtual device::Program *createProgram(amd::option::Options* options = NULL); diff --git a/projects/clr/rocclr/runtime/device/rocm/rocprogram.cpp b/projects/clr/rocclr/runtime/device/rocm/rocprogram.cpp index 60d935184e..74d287ea9a 100644 --- a/projects/clr/rocclr/runtime/device/rocm/rocprogram.cpp +++ b/projects/clr/rocclr/runtime/device/rocm/rocprogram.cpp @@ -44,6 +44,10 @@ #include #include +#if defined(ATI_OS_LINUX) +#include +#include +#endif // defined(ATI_OS_LINUX) #endif // WITHOUT_HSA_BACKEND @@ -485,7 +489,7 @@ namespace roc { bool createLibrary) { using namespace amd::opencl_driver; - Compiler* C = device().compiler(); + std::auto_ptr C(newCompilerInstance()); std::vector inputs; for (auto program : (const std::vector&)inputPrograms) { @@ -727,7 +731,7 @@ namespace roc { bool HSAILProgram::linkImpl_LC(amd::option::Options *options) { using namespace amd::opencl_driver; - Compiler* C = device().compiler(); + std::auto_ptr C(newCompilerInstance()); // call LinkLLVMBitcode std::vector inputs; @@ -1463,6 +1467,49 @@ namespace roc { } return hsailOptions; } + +#if defined(WITH_LIGHTNING_COMPILER) + +static std::string llvmBin_(amd::Os::getEnvironment("LLVM_BIN")); + +#if defined(ATI_OS_LINUX) +static pthread_once_t once = PTHREAD_ONCE_INIT; + +static void +checkLLVM_BIN() +{ + if (llvmBin_.empty()) { + Dl_info info; + if (dladdr((const void*)&amd::Device::init, &info)) { + llvmBin_ = dirname(strdup(info.dli_fname)); + size_t pos = llvmBin_.rfind("lib"); + if (pos != std::string::npos) { + llvmBin_.replace(pos, 3, "bin"); + } + } + } +#if defined(DEBUG) + std::string clangExe(llvmBin_ + "/clang"); + struct stat buf; + if (stat(clangExe.c_str(), &buf)) { + std::string msg("Could not find the Clang binary in " + llvmBin_); + LogWarning(msg.c_str()); + } +#endif // defined(DEBUG) +} +#endif // defined(ATI_OS_LINUX) + +std::auto_ptr +HSAILProgram::newCompilerInstance() +{ +#if defined(ATI_OS_LINUX) + pthread_once(&once, checkLLVM_BIN); +#endif // defined(ATI_OS_LINUX) + return std::auto_ptr( + amd::opencl_driver::CompilerFactory().CreateAMDGPUCompiler(llvmBin_)); +} +#endif // defined(WITH_LIGHTNING_COMPILER) + #endif // WITHOUT_HSA_BACKEND } // namespace roc diff --git a/projects/clr/rocclr/runtime/device/rocm/rocprogram.hpp b/projects/clr/rocclr/runtime/device/rocm/rocprogram.hpp index 2422cbc0c0..4e76ff855e 100644 --- a/projects/clr/rocclr/runtime/device/rocm/rocprogram.hpp +++ b/projects/clr/rocclr/runtime/device/rocm/rocprogram.hpp @@ -173,6 +173,8 @@ namespace roc { #if defined(WITH_LIGHTNING_COMPILER) RuntimeMD::Program::Metadata* metadata_; //!< Runtime metadata + //! Return a new transient compiler instance. + static std::auto_ptr newCompilerInstance(); #endif // defined(WITH_LIGHTNING_COMPILER) };