Add -dump-opt-asm support to the clLinkProgram() API

Submitted by Manasi Navare on Oct. 12, 2015, 8:35 a.m.

Details

Message ID 1444638957-15738-1-git-send-email-manasi.d.navare@intel.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Manasi Navare Oct. 12, 2015, 8:35 a.m.
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@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(-)

Patch hide | download patch | download mbox

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 04da692..625a097 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 *options_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
+      memcpy(options_str, options, strlen(options) + 1);
+      std::string optionStr(options_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(options_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 de892b5..86b3177 100644
--- a/backend/src/backend/program.h
+++ b/backend/src/backend/program.h
@@ -192,7 +192,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])

Comments

LGTM

Thanks!
Ruiling

> -----Original Message-----
> From: Navare, Manasi D
> Sent: Monday, October 12, 2015 4:36 PM
> To: beignet@lists.freedesktop.org; Song, Ruiling
> Cc: Navare, Manasi D
> Subject: [PATCH] Add -dump-opt-asm support to the clLinkProgram() API
> 
> 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@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..625a097 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 *options_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));
> +      memcpy(options_str, options, strlen(options) + 1);
> +      std::string optionStr(options_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(options_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 de892b5..86b3177 100644
> --- a/backend/src/backend/program.h
> +++ b/backend/src/backend/program.h
> @@ -192,7 +192,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
Pushed.

> -----Original Message-----

> From: Beignet [mailto:beignet-bounces@lists.freedesktop.org] On Behalf Of

> Song, Ruiling

> Sent: Tuesday, October 13, 2015 10:23

> To: Navare, Manasi D; beignet@lists.freedesktop.org

> Subject: Re: [Beignet] [PATCH] Add -dump-opt-asm support to the

> clLinkProgram() API

> 

> LGTM

> 

> Thanks!

> Ruiling

> 

> > -----Original Message-----

> > From: Navare, Manasi D

> > Sent: Monday, October 12, 2015 4:36 PM

> > To: beignet@lists.freedesktop.org; Song, Ruiling

> > Cc: Navare, Manasi D

> > Subject: [PATCH] Add -dump-opt-asm support to the clLinkProgram() API

> >

> > 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@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..625a097 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 *options_str = (char *)malloc(sizeof(char) * (strlen(options) + 1));

> > +      memcpy(options_str, options, strlen(options) + 1);

> > +      std::string optionStr(options_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(options_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 de892b5..86b3177 100644

> > --- a/backend/src/backend/program.h

> > +++ b/backend/src/backend/program.h

> > @@ -192,7 +192,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

> 

> _______________________________________________

> Beignet mailing list

> Beignet@lists.freedesktop.org

> http://lists.freedesktop.org/mailman/listinfo/beignet