[Beignet] [PATCH] clean llvm resource in compiler (libgbe.so)

Guo Yejun yejun.guo at intel.com
Thu Jul 17 16:16:34 PDT 2014


since we have separated the compiler (libgbe.so) and the interpreter
(libgbeinterp.so), the LLVM resource cleanup task should be done in
the compiler instead of the GenProgram::~GenProgram which has no way
to clean llvm resources in libgbeinterp.so

Signed-off-by: Guo Yejun <yejun.guo at intel.com>
---
 backend/src/backend/gen_program.cpp | 2 +-
 backend/src/backend/gen_program.hpp | 4 +++-
 backend/src/backend/program.cpp     | 9 ++++++++-
 backend/src/backend/program.h       | 4 ++++
 backend/src/backend/program.hpp     | 2 ++
 src/cl_gbe_loader.cpp               | 5 +++++
 src/cl_gbe_loader.h                 | 1 +
 src/cl_program.c                    | 6 +++++-
 8 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index c846786..3e16fd6 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -112,7 +112,7 @@ namespace gbe {
 #endif
   }
 
-  GenProgram::~GenProgram(void){
+  void GenProgram::CleanLlvmResource(void){
 #ifdef GBE_COMPILER_AVAILABLE
     if(module){
       delete (llvm::Module*)module;
diff --git a/backend/src/backend/gen_program.hpp b/backend/src/backend/gen_program.hpp
index d5c9079..1b5136e 100644
--- a/backend/src/backend/gen_program.hpp
+++ b/backend/src/backend/gen_program.hpp
@@ -64,7 +64,9 @@ namespace gbe
     /*! Current device ID*/
     uint32_t deviceID;
     /*! Destroy the program */
-    virtual ~GenProgram(void);
+    virtual ~GenProgram(void) {};
+    /*! Clean LLVM resource */
+    virtual void CleanLlvmResource(void);
     /*! Implements base class */
     virtual Kernel *compileKernel(const ir::Unit &unit, const std::string &name, bool relaxMath);
     /*! Allocate an empty kernel. */
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index c35db1b..57ef515 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -528,6 +528,11 @@ namespace gbe {
     GBE_SAFE_DELETE(program);
   }
 
+  static void programCleanLlvmResource(gbe_program gbeProgram) {
+    gbe::Program *program = (gbe::Program*)(gbeProgram);
+    program->CleanLlvmResource();
+  }
+
 #ifdef GBE_COMPILER_AVAILABLE
   BVAR(OCL_OUTPUT_BUILD_LOG, false);
   SVAR(OCL_PCH_PATH, PCH_OBJECT_DIR);
@@ -838,7 +843,7 @@ namespace gbe {
     processSourceAndOption(source, options, NULL, clOpt, clName, optLevel);
 
     gbe_program p;
-    // will delete the module and act in the destructor of GenProgram.
+    // will delete the module and act in GenProgram::CleanLlvmResource().
     llvm::Module * out_module;
     llvm::LLVMContext* llvm_ctx = new llvm::LLVMContext;
     if (buildModuleFromSource(clName.c_str(), &out_module, llvm_ctx, clOpt.c_str(),
@@ -1165,6 +1170,7 @@ GBE_EXPORT_SYMBOL gbe_program_link_from_llvm_cb *gbe_program_link_from_llvm = NU
 GBE_EXPORT_SYMBOL gbe_program_build_from_llvm_cb *gbe_program_build_from_llvm = NULL;
 GBE_EXPORT_SYMBOL gbe_program_get_global_constant_size_cb *gbe_program_get_global_constant_size = NULL;
 GBE_EXPORT_SYMBOL gbe_program_get_global_constant_data_cb *gbe_program_get_global_constant_data = NULL;
+GBE_EXPORT_SYMBOL gbe_program_clean_llvm_resource_cb *gbe_program_clean_llvm_resource = NULL;
 GBE_EXPORT_SYMBOL gbe_program_delete_cb *gbe_program_delete = NULL;
 GBE_EXPORT_SYMBOL gbe_program_get_kernel_num_cb *gbe_program_get_kernel_num = NULL;
 GBE_EXPORT_SYMBOL gbe_program_get_kernel_by_name_cb *gbe_program_get_kernel_by_name = NULL;
@@ -1210,6 +1216,7 @@ namespace gbe
       gbe_program_link_program = gbe::programLinkProgram;
       gbe_program_get_global_constant_size = gbe::programGetGlobalConstantSize;
       gbe_program_get_global_constant_data = gbe::programGetGlobalConstantData;
+      gbe_program_clean_llvm_resource = gbe::programCleanLlvmResource;
       gbe_program_delete = gbe::programDelete;
       gbe_program_get_kernel_num = gbe::programGetKernelNum;
       gbe_program_get_kernel_by_name = gbe::programGetKernelByName;
diff --git a/backend/src/backend/program.h b/backend/src/backend/program.h
index c56b94a..2a3841a 100644
--- a/backend/src/backend/program.h
+++ b/backend/src/backend/program.h
@@ -239,6 +239,10 @@ extern gbe_kernel_get_sampler_data_cb *gbe_kernel_get_sampler_data;
 typedef void (gbe_kernel_get_compile_wg_size_cb)(gbe_kernel gbeKernel, size_t wg_sz[3]);
 extern gbe_kernel_get_compile_wg_size_cb *gbe_kernel_get_compile_wg_size;
 
+/*! Clean LLVM resource of the given program */
+typedef void (gbe_program_clean_llvm_resource_cb)(gbe_program);
+extern gbe_program_clean_llvm_resource_cb *gbe_program_clean_llvm_resource;
+
 /*! Destroy and deallocate the given program */
 typedef void (gbe_program_delete_cb)(gbe_program);
 extern gbe_program_delete_cb *gbe_program_delete;
diff --git a/backend/src/backend/program.hpp b/backend/src/backend/program.hpp
index d8a9d22..b780c42 100644
--- a/backend/src/backend/program.hpp
+++ b/backend/src/backend/program.hpp
@@ -225,6 +225,8 @@ namespace gbe {
     Program(void);
     /*! Destroy the program */
     virtual ~Program(void);
+    /*! Clean LLVM resource of the program */
+    virtual void CleanLlvmResource() = 0;
     /*! Get the number of kernels in the program */
     uint32_t getKernelNum(void) const { return kernels.size(); }
     /*! Get the kernel from its name */
diff --git a/src/cl_gbe_loader.cpp b/src/cl_gbe_loader.cpp
index 2fda50c..c2a61c4 100644
--- a/src/cl_gbe_loader.cpp
+++ b/src/cl_gbe_loader.cpp
@@ -32,6 +32,7 @@ gbe_program_new_from_llvm_binary_cb *compiler_program_new_from_llvm_binary = NUL
 gbe_program_serialize_to_binary_cb *compiler_program_serialize_to_binary = NULL;
 gbe_program_new_from_llvm_cb *compiler_program_new_from_llvm = NULL;
 gbe_set_image_base_index_cb *compiler_set_image_base_index = NULL;
+gbe_program_clean_llvm_resource_cb *compiler_program_clean_llvm_resource = NULL;
 
 //function pointer from libgbeinterp.so
 gbe_program_new_from_binary_cb *interp_program_new_from_binary = NULL;
@@ -289,6 +290,10 @@ struct GbeLoaderInitializer
       if (compiler_set_image_base_index == NULL)
         return;
 
+      compiler_program_clean_llvm_resource = *(gbe_program_clean_llvm_resource_cb **)dlsym(dlhCompiler, "gbe_program_clean_llvm_resource");
+      if (compiler_program_clean_llvm_resource == NULL)
+        return;
+
       compilerLoaded = true;
     }
   }
diff --git a/src/cl_gbe_loader.h b/src/cl_gbe_loader.h
index 632163b..f855390 100644
--- a/src/cl_gbe_loader.h
+++ b/src/cl_gbe_loader.h
@@ -33,6 +33,7 @@ extern gbe_program_new_from_llvm_binary_cb *compiler_program_new_from_llvm_binar
 extern gbe_program_serialize_to_binary_cb *compiler_program_serialize_to_binary;
 extern gbe_program_new_from_llvm_cb *compiler_program_new_from_llvm;
 extern gbe_set_image_base_index_cb *compiler_set_image_base_index;
+extern gbe_program_clean_llvm_resource_cb *compiler_program_clean_llvm_resource;
 
 extern gbe_program_new_from_binary_cb *interp_program_new_from_binary;
 extern gbe_program_get_global_constant_size_cb *interp_program_get_global_constant_size;
diff --git a/src/cl_program.c b/src/cl_program.c
index 7d33f2a..c141a35 100644
--- a/src/cl_program.c
+++ b/src/cl_program.c
@@ -101,7 +101,11 @@ cl_program_delete(cl_program p)
   cl_context_delete(p->ctx);
 
   /* Free the program as allocated by the compiler */
-  if (p->opaque) interp_program_delete(p->opaque);
+  if (p->opaque) {
+    if (CompilerSupported())
+      compiler_program_clean_llvm_resource(p->opaque);
+    interp_program_delete(p->opaque);
+  }
 
   p->magic = CL_MAGIC_DEAD_HEADER; /* For safety */
   cl_free(p);
-- 
1.8.3.2



More information about the Beignet mailing list