diff --git a/rocclr/device/devprogram.cpp b/rocclr/device/devprogram.cpp index 4a2145c56c..6338b25ae9 100644 --- a/rocclr/device/devprogram.cpp +++ b/rocclr/device/devprogram.cpp @@ -74,6 +74,10 @@ inline static std::vector splitSpaceSeparatedString(const char *str return vec; } +#if defined(WITH_COMPILER_LIB) +amd::Monitor Program::buildLock_("HSAIL build lock", true); +#endif + // ================================================================================================ Program::Program(amd::Device& device, amd::Program& owner) : device_(device), @@ -785,6 +789,8 @@ bool Program::compileImplHSAIL(const std::string& sourceCode, const std::vector& headers, const char** headerIncludeNames, amd::option::Options* options) { #if defined(WITH_COMPILER_LIB) + amd::ScopedLock sl(&buildLock_); + acl_error errorCode; aclTargetInfo target; @@ -1009,6 +1015,8 @@ bool Program::linkImplLC(const std::vector& inputPrograms, bool Program::linkImplHSAIL(const std::vector& inputPrograms, amd::option::Options* options, bool createLibrary) { #if defined(WITH_COMPILER_LIB) + amd::ScopedLock sl(&buildLock_); + acl_error errorCode; // For each program we need to extract the LLVMIR and create @@ -1317,6 +1325,8 @@ bool Program::linkImplLC(amd::option::Options* options) { // ================================================================================================ bool Program::linkImplHSAIL(amd::option::Options* options) { #if defined(WITH_COMPILER_LIB) + amd::ScopedLock sl(&buildLock_); + acl_error errorCode; bool finalize = true; internal_ = (compileOptions_.find("-cl-internal-kernel") != std::string::npos) ? true : false; @@ -1870,6 +1880,8 @@ int32_t Program::build(const std::string& sourceCode, const char* origOptions, // ================================================================================================ bool Program::loadHSAIL() { #if defined(WITH_COMPILER_LIB) + amd::ScopedLock sl(&buildLock_); + acl_error errorCode; size_t binSize; void* bin = const_cast(amd::Hsail::ExtractSection(device().compiler(), binaryElf_, diff --git a/rocclr/device/devprogram.hpp b/rocclr/device/devprogram.hpp index b326974977..55eac9984e 100644 --- a/rocclr/device/devprogram.hpp +++ b/rocclr/device/devprogram.hpp @@ -119,6 +119,10 @@ class Program : public amd::HeapObject { typedef enum { InitKernel = 0, FiniKernel } kernel_kind_t; //!< Kernel kind bool runInitFiniKernel(kernel_kind_t) const; +#if defined(WITH_COMPILER_LIB) + static amd::Monitor buildLock_; //!< Global build lock for HSAIL which isn't thread-safe +#endif + protected: union { struct { diff --git a/rocclr/platform/program.cpp b/rocclr/platform/program.cpp index c21b58451a..e36e16c9bc 100644 --- a/rocclr/platform/program.cpp +++ b/rocclr/platform/program.cpp @@ -227,10 +227,6 @@ device::Program* Program::getDeviceProgram(const Device& device) const { return it->second; } -#if defined(WITH_COMPILER_LIB) -Monitor Program::buildLock_("OCL build program", true); -#endif - static bool adjustOptionsOnIgnoreEnv(std::string &cppstr) { // if there is a -ignore-env, adjust options. bool optionChangable = true; @@ -252,9 +248,8 @@ int32_t Program::compile(const std::vector& devices, size_t numHeaders, const char** headerIncludeNames, const char* options, void(CL_CALLBACK* notifyFptr)(cl_program, void*), void* data, bool optionChangable) { -#if defined(WITH_COMPILER_LIB) - ScopedLock sl(!amd::IS_HIP && useHsail(devices) ? &buildLock_ : nullptr); -#endif + ScopedLock sl(&programLock_); + int32_t retval = CL_SUCCESS; // Clear the program object @@ -324,9 +319,8 @@ int32_t Program::link(const std::vector& devices, size_t numInputs, const std::vector& inputPrograms, const char* options, void(CL_CALLBACK* notifyFptr)(cl_program, void*), void* data, bool optionChangable) { -#if defined(WITH_COMPILER_LIB) - ScopedLock sl(!amd::IS_HIP && useHsail(devices) ? &buildLock_ : nullptr); -#endif + ScopedLock sl(&programLock_); + int32_t retval = CL_SUCCESS; if (symbolTable_ == NULL) { @@ -494,9 +488,8 @@ void Program::StubProgramSource(const std::string& app_name) { int32_t Program::build(const std::vector& devices, const char* options, void(CL_CALLBACK* notifyFptr)(cl_program, void*), void* data, bool optionChangable, bool newDevProg) { -#if defined(WITH_COMPILER_LIB) - ScopedLock sl(!amd::IS_HIP && useHsail(devices) ? &buildLock_ : nullptr); -#endif + ScopedLock sl(&programLock_); + int32_t retval = CL_SUCCESS; if (symbolTable_ == NULL) { @@ -603,9 +596,7 @@ int32_t Program::build(const std::vector& devices, const char* options, } bool Program::load(const std::vector& devices) { -#if defined(WITH_COMPILER_LIB) - ScopedLock sl(!amd::IS_HIP && useHsail(devices) ? &buildLock_ : nullptr); -#endif + ScopedLock sl(&programLock_); for (const auto& it : devicePrograms_) { const Device& device = *(it.first); diff --git a/rocclr/platform/program.hpp b/rocclr/platform/program.hpp index 47f64886b7..e6914575ce 100644 --- a/rocclr/platform/program.hpp +++ b/rocclr/platform/program.hpp @@ -119,6 +119,8 @@ class Program : public RuntimeObject { std::string programLog_; //!< Log for parsing options, etc. + Monitor programLock_; //!< Lock to protect program data structure + protected: //! Destroy this program. ~Program(); @@ -126,19 +128,6 @@ class Program : public RuntimeObject { //! Clears the program object if the app attempts to rebuild the program void clear(); -#if defined(WITH_COMPILER_LIB) - //! Global HSAIL build lock (remove when HSAIL is thread-safe). - static Monitor buildLock_; - - //! Check if any device uses HSAIL - bool useHsail(const std::vector& devices) const { - for (const auto& it : devices) { - if (!it->settings().useLightning_) return true; - } - return false; - } -#endif - public: //! Construct a new program to be compiled from the given source code. Program(Context& context, const std::string& sourceCode, Language language, @@ -147,7 +136,8 @@ class Program : public RuntimeObject { sourceCode_(sourceCode), language_(language), symbolTable_(NULL), - programLog_() { + programLog_(), + programLock_("Program lock", true) { for (auto i = 0; i != numHeaders; ++i) { headers_.emplace_back(headers[i]); headerNames_.emplace_back(headerNames[i]); @@ -157,7 +147,8 @@ class Program : public RuntimeObject { //! Construct a new program associated with a context. Program(Context& context, Language language = Binary) : context_(context), language_(language), - symbolTable_(NULL) {} + symbolTable_(NULL), + programLock_("Program lock", true) {} //! Returns context, associated with the current program. const Context& context() const { return context_(); }