[Beignet] [PATCH 1/2] backend: enable option -dump-spir-binary to generate SPIR binary from beignet.

xionghu.luo at intel.com xionghu.luo at intel.com
Tue Nov 3 21:45:45 PST 2015


From: Luo Xionghu <xionghu.luo at intel.com>

allow user to pass "-dump-spir-binary=[file_name]" to generate the spir
binary to the file.

Signed-off-by: Luo Xionghu <xionghu.luo at intel.com>
---
 backend/src/backend/program.cpp | 27 ++++++++++++++++++++++-----
 1 file changed, 22 insertions(+), 5 deletions(-)

diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index 472734b..27d0bca 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -519,7 +519,7 @@ namespace gbe {
   BVAR(OCL_OUTPUT_BUILD_LOG, false);
 
   static bool buildModuleFromSource(const char *source, llvm::Module** out_module, llvm::LLVMContext* llvm_ctx,
-                                    std::string dumpLLVMFileName, std::vector<std::string>& options, size_t stringSize, char *err,
+                                    std::string dumpLLVMFileName, std::string dumpSPIRBinaryName, std::vector<std::string>& options, size_t stringSize, char *err,
                                     size_t *errSize) {
     // Arguments to pass to the clang frontend
     vector<const char *> args;
@@ -662,6 +662,14 @@ namespace gbe {
         (*out_module)->print(ostream, 0);
       } //Otherwise, you'll have to make do without the dump.
     }
+
+    if (!dumpSPIRBinaryName.empty()) {
+      std::error_code err;
+      llvm::raw_fd_ostream ostream (dumpSPIRBinaryName.c_str(),
+                                    err, llvm::sys::fs::F_None);
+      if (!err)
+        llvm::WriteBitcodeToFile(*out_module, ostream);
+    }
 #endif
     return true;
   }
@@ -677,6 +685,7 @@ namespace gbe {
                                      std::vector<std::string>& clOpt,
                                      std::string& dumpLLVMFileName,
                                      std::string& dumpASMFileName,
+                                     std::string& dumpSPIRBinaryName,
                                      int& optLevel,
                                      size_t stringSize,
                                      char *err,
@@ -765,6 +774,11 @@ namespace gbe {
           continue; // Don't push this str back; ignore it.
         }
 
+        if(str.find("-dump-spir-binary=") != std::string::npos) {
+          dumpSPIRBinaryName = str.substr(str.find("=") + 1);
+          continue; // Don't push this str back; ignore it.
+        }
+
         clOpt.push_back(str);
       }
       free(str);
@@ -809,8 +823,9 @@ namespace gbe {
     int optLevel = 1;
     std::vector<std::string> clOpt;
     std::string dumpLLVMFileName, dumpASMFileName;
+    std::string dumpSPIRBinaryName;
     if (!processSourceAndOption(source, options, NULL, clOpt,
-                                dumpLLVMFileName, dumpASMFileName,
+                                dumpLLVMFileName, dumpASMFileName, dumpSPIRBinaryName,
                                 optLevel,
                                 stringSize, err, errSize))
       return NULL;
@@ -823,7 +838,7 @@ namespace gbe {
     if (!llvm::llvm_is_multithreaded())
       llvm_mutex.lock();
 
-    if (buildModuleFromSource(source, &out_module, llvm_ctx, dumpLLVMFileName, clOpt,
+    if (buildModuleFromSource(source, &out_module, llvm_ctx, dumpLLVMFileName, dumpSPIRBinaryName, clOpt,
                               stringSize, err, errSize)) {
     // Now build the program from llvm
       size_t clangErrSize = 0;
@@ -839,6 +854,7 @@ namespace gbe {
         if (asmDumpStream)
           fclose(asmDumpStream);
       }
+
       p = gbe_program_new_from_llvm(deviceID, NULL, out_module, llvm_ctx,
                                     dumpASMFileName.empty() ? NULL : dumpASMFileName.c_str(),
                                     stringSize, err, errSize, optLevel);
@@ -869,8 +885,9 @@ namespace gbe {
     int optLevel = 1;
     std::vector<std::string> clOpt;
     std::string dumpLLVMFileName, dumpASMFileName;
+    std::string dumpSPIRBinaryName;
     if (!processSourceAndOption(source, options, temp_header_path, clOpt,
-                                dumpLLVMFileName, dumpASMFileName,
+                                dumpLLVMFileName, dumpASMFileName, dumpSPIRBinaryName,
                                 optLevel, stringSize, err, errSize))
       return NULL;
 
@@ -881,7 +898,7 @@ namespace gbe {
     llvm::Module * out_module;
     llvm::LLVMContext* llvm_ctx = &llvm::getGlobalContext();
 
-    if (buildModuleFromSource(source, &out_module, llvm_ctx, dumpLLVMFileName, clOpt,
+    if (buildModuleFromSource(source, &out_module, llvm_ctx, dumpLLVMFileName, dumpSPIRBinaryName, clOpt,
                               stringSize, err, errSize)) {
     // Now build the program from llvm
       if (err != NULL) {
-- 
1.9.1



More information about the Beignet mailing list