[Beignet] [PATCH 1/2] Add -dump-opt-asm support to the clLinkProgram() API

Manasi Navare manasi.d.navare at intel.com
Thu Oct 1 04:34:48 PDT 2015


This will dump the Gen ASM output to the file specified in the
-dump-opt-asm Link option during the Link program step.

Signed-off-by: Manasi Navare <manasi.d.navare at intel.com>
---
 backend/src/backend/gen_program.cpp | 30 +++++++++++++++++++++++++++---
 backend/src/backend/program.cpp     |  6 +++++-
 backend/src/backend/program.h       |  3 ++-
 src/cl_program.c                    |  4 +---
 4 files changed, 35 insertions(+), 8 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 04da692..f07f131 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -379,9 +379,9 @@ namespace gbe {
   }
 
   static gbe_program genProgramNewGenProgram(uint32_t deviceID, const void* module,
-                                             const void* llvm_ctx)  {
+                                             const void* llvm_ctx,const char* asm_file_name)  {
     using namespace gbe;
-    GenProgram *program = GBE_NEW(GenProgram, deviceID, module, llvm_ctx);
+    GenProgram *program = GBE_NEW(GenProgram, deviceID, module, llvm_ctx, asm_file_name);
     // Everything run fine
     return (gbe_program) program;
   }
@@ -425,17 +425,41 @@ namespace gbe {
 #ifdef GBE_COMPILER_AVAILABLE
     using namespace gbe;
     std::string error;
-
     int optLevel = 1;
+    std::string dumpASMFileName;
+    size_t start = 0, end = 0;
 
     if(options) {
       char *p;
       p = strstr(const_cast<char *>(options), "-cl-opt-disable");
       if (p)
         optLevel = 0;
+
+    char *str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      memcpy(str, options, strlen(options) + 1);
+      std::string optionStr(str);
+      while (end != std::string::npos) {
+        end = optionStr.find(' ', start);
+        std::string str = optionStr.substr(start, end - start);
+        start = end + 1;
+        if(str.size() == 0)
+          continue;
+
+        if(str.find("-dump-opt-asm=") != std::string::npos) {
+          dumpASMFileName = str.substr(str.find("=") + 1);
+          continue; // Don't push this str back; ignore it.
+        }
+      }
+      free(str);
     }
 
     GenProgram* p = (GenProgram*) program;
+    if (!dumpASMFileName.empty()) {
+        p->asm_file_name = dumpASMFileName.c_str();
+        FILE *asmDumpStream = fopen(dumpASMFileName.c_str(), "w");
+        if (asmDumpStream)
+          fclose(asmDumpStream);
+      }
     // Try to compile the program
     acquireLLVMContextLock();
     llvm::Module* module = (llvm::Module*)p->module;
diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index 0ee76fc..472734b 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -890,7 +890,7 @@ namespace gbe {
         err += *errSize;
       }
 
-      p = gbe_program_new_gen_program(deviceID, out_module, NULL);
+      p = gbe_program_new_gen_program(deviceID, out_module, NULL, NULL);
 
       if (OCL_OUTPUT_BUILD_LOG && options)
         llvm::errs() << options;
@@ -936,6 +936,10 @@ namespace gbe {
       if(pos != std::string::npos) {
         s.erase(pos, strlen("-enable-link-options"));
       }
+      pos = s.find("-dump-opt-asm");
+      if(pos != std::string::npos) {
+        s.erase(pos, strlen("-dump-opt-asm"));
+      }
       args.push_back(s.c_str());
 
       // The compiler invocation needs a DiagnosticsEngine so it can report problems
diff --git a/backend/src/backend/program.h b/backend/src/backend/program.h
index 346d855..b3f90dd 100644
--- a/backend/src/backend/program.h
+++ b/backend/src/backend/program.h
@@ -196,7 +196,8 @@ extern gbe_program_check_opt_cb *gbe_program_check_opt;
 /*! create s new genprogram for link. */
 typedef gbe_program (gbe_program_new_gen_program_cb)(uint32_t deviceID,
                                                      const void *module,
-                                                     const void *act);
+                                                     const void *act,
+                                                     const char *asm_file_name);
 extern gbe_program_new_gen_program_cb *gbe_program_new_gen_program;
 
 /*! Create a new program from the given blob */
diff --git a/src/cl_program.c b/src/cl_program.c
index 82dd3e3..55c1ee8 100644
--- a/src/cl_program.c
+++ b/src/cl_program.c
@@ -620,13 +620,11 @@ cl_program_link(cl_context            context,
   int copyed = 0;
   cl_bool ret = 0;
   int avialable_program = 0;
-
   //Although we don't use options, but still need check options
   if(!compiler_program_check_opt(options)) {
     err = CL_INVALID_LINKER_OPTIONS;
     goto error;
   }
-
   for(i = 0; i < num_input_programs; i++) {
     //num_input_programs >0 and input_programs MUST not NULL, so compare with input_programs[0] directly.
     if(input_programs[i]->binary_type == CL_PROGRAM_BINARY_TYPE_LIBRARY ||
@@ -657,7 +655,7 @@ cl_program_link(cl_context            context,
     goto error;
   }
 
-  p->opaque = compiler_program_new_gen_program(context->device->device_id, NULL, NULL);
+  p->opaque = compiler_program_new_gen_program(context->device->device_id, NULL, NULL, NULL);
   for(i = 0; i < num_input_programs; i++) {
     // if program create with llvm binary, need deserilize first to get module.
     if(input_programs[i])
-- 
1.9.1



More information about the Beignet mailing list