From af8e9cc3bbc11d313393a26358105497d41b6fc3 Mon Sep 17 00:00:00 2001
From: foreman
Date: Wed, 22 Jun 2016 12:40:45 -0400
Subject: [PATCH] P4 to Git Change 1282811 by gandryey@gera-w8 on 2016/06/22
12:06:40
SWDEV-91794 - Memory leak when looping BuildProgram
- Release binary raw inside runtime and compiler library
Affected files ...
... //depot/stg/opencl/drivers/opencl/compiler/lib/api/v0_8/acl.cpp#43 edit
... //depot/stg/opencl/drivers/opencl/compiler/lib/loaders/bif/bifbase.cpp#57 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpucompiler.cpp#155 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprogram.cpp#227 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/gpu/gpuprogram.hpp#68 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palcompiler.cpp#3 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprogram.cpp#8 edit
... //depot/stg/opencl/drivers/opencl/runtime/device/pal/palprogram.hpp#6 edit
---
rocclr/runtime/device/gpu/gpucompiler.cpp | 11 ++------
rocclr/runtime/device/gpu/gpuprogram.cpp | 34 +++++++++++++++--------
rocclr/runtime/device/gpu/gpuprogram.hpp | 8 +-----
rocclr/runtime/device/pal/palcompiler.cpp | 11 ++------
rocclr/runtime/device/pal/palprogram.cpp | 34 +++++++++++++++--------
rocclr/runtime/device/pal/palprogram.hpp | 14 +++-------
6 files changed, 57 insertions(+), 55 deletions(-)
diff --git a/rocclr/runtime/device/gpu/gpucompiler.cpp b/rocclr/runtime/device/gpu/gpucompiler.cpp
index a541dbbb5c..edad392eef 100644
--- a/rocclr/runtime/device/gpu/gpucompiler.cpp
+++ b/rocclr/runtime/device/gpu/gpucompiler.cpp
@@ -447,15 +447,10 @@ HSAILProgram::compileImpl(
}
clBinary()->storeCompileOptions(compileOptions_);
- // Save the binary in the interface class
- size_t size = 0;
- void* mem = NULL;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
- // Save the binary inside the program
- // The FSAILProgram will be responsible to free it during destruction
- rawBinary_ = mem;
+ // Save the binary in the interface class
+ saveBinaryAndSetType(TYPE_COMPILED);
+
return true;
}
diff --git a/rocclr/runtime/device/gpu/gpuprogram.cpp b/rocclr/runtime/device/gpu/gpuprogram.cpp
index 84153ea6a0..fe774f2d25 100644
--- a/rocclr/runtime/device/gpu/gpuprogram.cpp
+++ b/rocclr/runtime/device/gpu/gpuprogram.cpp
@@ -1715,7 +1715,7 @@ HSAILProgram::~HSAILProgram()
delete it;
}
if (rawBinary_ != NULL) {
- free(rawBinary_);
+ aclFreeMem(binaryElf_, rawBinary_);
}
acl_error error;
// Free the elf binary
@@ -1863,12 +1863,8 @@ HSAILProgram::linkImpl(
aclBinaryFini(binaries_to_link[i]);
}
if (createLibrary) {
- size_t size = 0;
- void *mem = NULL;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
+ saveBinaryAndSetType(TYPE_LIBRARY);
buildLog_ += aclGetCompilerLog(dev().hsaCompiler());
- setType(TYPE_LIBRARY);
return true;
}
// Now call linkImpl with the new options
@@ -2227,12 +2223,8 @@ HSAILProgram::linkImpl(amd::option::Options* options)
}
}
// Save the binary in the interface class
- size_t size = 0;
- void *mem = NULL;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
+ saveBinaryAndSetType(TYPE_EXECUTABLE);
buildLog_ += aclGetCompilerLog(dev().hsaCompiler());
- setType(TYPE_EXECUTABLE);
return true;
}
@@ -2345,6 +2337,26 @@ HSAILProgram::info(const char * str) {
return info_;
}
+bool
+HSAILProgram::saveBinaryAndSetType(type_t type)
+{
+ //Write binary to memory
+ if (rawBinary_ != NULL) {
+ //Free memory containing rawBinary
+ aclFreeMem(binaryElf_, rawBinary_);
+ rawBinary_ = NULL;
+ }
+ size_t size = 0;
+ if (aclWriteToMem(binaryElf_, &rawBinary_, &size) != ACL_SUCCESS) {
+ buildLog_ += "Failed to write binary to memory \n";
+ return false;
+ }
+ setBinary(static_cast(rawBinary_), size);
+ //Set the type of binary
+ setType(type);
+ return true;
+}
+
hsa_isa_t ORCAHSALoaderContext::IsaFromName(const char *name) {
hsa_isa_t isa = {0};
if (!strcmp(Gfx700, name)) { isa.handle = gfx700; return isa; }
diff --git a/rocclr/runtime/device/gpu/gpuprogram.hpp b/rocclr/runtime/device/gpu/gpuprogram.hpp
index f5ec739c06..a1e2a185a4 100644
--- a/rocclr/runtime/device/gpu/gpuprogram.hpp
+++ b/rocclr/runtime/device/gpu/gpuprogram.hpp
@@ -559,13 +559,7 @@ protected:
*/
aclType getNextCompilationStageFromBinary(amd::option::Options* options);
- /*! \brief Compiles LLVM binary to FSAIL code (compiler backend: link+opt+codegen)
- *
- * \return The build error code
- */
- int compileBinaryToFSAIL(
- amd::option::Options* options //!< options for compilation
- );
+ bool saveBinaryAndSetType(type_t type);
virtual bool linkImpl(amd::option::Options* options);
diff --git a/rocclr/runtime/device/pal/palcompiler.cpp b/rocclr/runtime/device/pal/palcompiler.cpp
index 24b1b27750..b463d5a29c 100644
--- a/rocclr/runtime/device/pal/palcompiler.cpp
+++ b/rocclr/runtime/device/pal/palcompiler.cpp
@@ -132,15 +132,10 @@ HSAILProgram::compileImpl(
}
clBinary()->storeCompileOptions(compileOptions_);
- // Save the binary in the interface class
- size_t size = 0;
- void* mem = nullptr;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
- // Save the binary inside the program
- // The FSAILProgram will be responsible to free it during destruction
- rawBinary_ = mem;
+ // Save the binary in the interface class
+ saveBinaryAndSetType(TYPE_COMPILED);
+
return true;
}
diff --git a/rocclr/runtime/device/pal/palprogram.cpp b/rocclr/runtime/device/pal/palprogram.cpp
index ba094d83d9..6c3ec5029b 100644
--- a/rocclr/runtime/device/pal/palprogram.cpp
+++ b/rocclr/runtime/device/pal/palprogram.cpp
@@ -68,7 +68,7 @@ HSAILProgram::~HSAILProgram()
delete it;
}
if (rawBinary_ != nullptr) {
- free(rawBinary_);
+ aclFreeMem(binaryElf_, rawBinary_);
}
acl_error error;
// Free the elf binary
@@ -216,12 +216,8 @@ HSAILProgram::linkImpl(
aclBinaryFini(binaries_to_link[i]);
}
if (createLibrary) {
- size_t size = 0;
- void *mem = NULL;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
+ saveBinaryAndSetType(TYPE_LIBRARY);
buildLog_ += aclGetCompilerLog(dev().compiler());
- setType(TYPE_LIBRARY);
return true;
}
// Now call linkImpl with the new options
@@ -578,12 +574,8 @@ HSAILProgram::linkImpl(amd::option::Options* options)
}
}
// Save the binary in the interface class
- size_t size = 0;
- void *mem = nullptr;
- aclWriteToMem(binaryElf_, &mem, &size);
- setBinary(static_cast(mem), size);
+ saveBinaryAndSetType(TYPE_EXECUTABLE);
buildLog_ += aclGetCompilerLog(dev().compiler());
- setType(TYPE_EXECUTABLE);
return true;
}
@@ -696,6 +688,26 @@ HSAILProgram::info(const char * str) {
return info_;
}
+bool
+HSAILProgram::saveBinaryAndSetType(type_t type)
+{
+ //Write binary to memory
+ if (rawBinary_ != nullptr) {
+ //Free memory containing rawBinary
+ aclFreeMem(binaryElf_, rawBinary_);
+ rawBinary_ = nullptr;
+ }
+ size_t size = 0;
+ if (aclWriteToMem(binaryElf_, &rawBinary_, &size) != ACL_SUCCESS) {
+ buildLog_ += "Failed to write binary to memory \n";
+ return false;
+ }
+ setBinary(static_cast(rawBinary_), size);
+ //Set the type of binary
+ setType(type);
+ return true;
+}
+
hsa_isa_t ORCAHSALoaderContext::IsaFromName(const char *name) {
hsa_isa_t isa = {0};
if (!strcmp(Gfx700, name)) { isa.handle = gfx700; return isa; }
diff --git a/rocclr/runtime/device/pal/palprogram.hpp b/rocclr/runtime/device/pal/palprogram.hpp
index fb0aade8cf..719bb3f8e3 100644
--- a/rocclr/runtime/device/pal/palprogram.hpp
+++ b/rocclr/runtime/device/pal/palprogram.hpp
@@ -208,14 +208,8 @@ protected:
/* \brief Returns the next stage to compile from, based on sections and options in binary
*/
aclType getNextCompilationStageFromBinary(amd::option::Options* options);
-
- /*! \brief Compiles LLVM binary to FSAIL code (compiler backend: link+opt+codegen)
- *
- * \return The build error code
- */
- int compileBinaryToFSAIL(
- amd::option::Options* options //!< options for compilation
- );
+
+ bool saveBinaryAndSetType(type_t type);
virtual bool linkImpl(amd::option::Options* options);
@@ -269,10 +263,10 @@ private:
aclBinary* binaryElf_; //!< Binary for the new compiler library
void* rawBinary_; //!< Pointer to the raw binary
aclBinaryOptions binOpts_; //!< Binary options to create aclBinary
- std::vector globalStores_; //!< Global memory for the program
+ std::vector globalStores_; //!< Global memory for the program
Memory* kernels_; //!< Table with kernel object pointers
uint maxScratchRegs_; //!< Maximum number of scratch regs used in the program by individual kernel
- std::list staticSamplers_; //!< List od internal static samplers
+ std::list staticSamplers_; //!< List od internal static samplers
bool isNull_; //!< Null program no memory allocations
amd::hsa::loader::Loader* loader_; //!< Loader object
amd::hsa::loader::Executable* executable_; //!< Executable for HSA Loader