[Beignet] [PATCH 1/2] add pci info in the gen binary code.

xionghu.luo at intel.com xionghu.luo at intel.com
Mon Jul 7 21:39:06 PDT 2014


From: LuoXionghu <xionghu.luo at intel.com>

the size of the pci info is 4 bytes, right after the '/0GENC'.
check the header magic number and pci info before deserializeFromBin.

Signed-off-by: LuoXionghu <xionghu.luo at intel.com>
---
 backend/src/backend/gen_program.cpp | 34 +++++++++++++++++++++++++---------
 backend/src/gbe_bin_generater.cpp   |  2 ++
 2 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 84e8c2a..fc50344 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -196,15 +196,29 @@ namespace gbe {
 #endif
   }
 
+#define IS_GEN_BINARY(binary) (*binary == '\0' && *(binary+1) == 'G'&& *(binary+2) == 'E' &&*(binary+3) == 'N' &&*(binary+4) == 'C')
+#define BINARY_MATCH(typeA, typeB) ((IS_IVYBRIDGE(typeA) && IS_IVYBRIDGE(typeB)) || (IS_HASWELL(typeA) && IS_HASWELL(typeB)) )
+#define FILL_GEN_BINARY(binary) do{*binary = '\0'; *(binary+1) = 'G'; *(binary+2) = 'E'; *(binary+3) = 'N'; *(binary+4) = 'C';}while(0)
+#define FILL_DEVICE_ID(binary, devID) do {*(binary+5) = devID; *(binary+6) = devID >>8; *(binary+7) = devID >>16; *(binary+8) = devID >>24;}while(0)
+
   static gbe_program genProgramNewFromBinary(uint32_t deviceID, const char *binary, size_t size) {
     using namespace gbe;
     std::string binary_content;
-    //the first 5 bytes are header to differentiate from llvm bitcode binary.
-    binary_content.assign(binary+5, size-5);
+    //the header length is 9 bytes: 1 byte is binary type, 4 bytes are bitcode header, 4  bytes are device id info.
+    uint32_t bin_deviceID = *(binary+5)|*(binary+6)<<8|*(binary+7)<<16|*(binary+8)<<24;
+
+    // check whether is gen binary ('/0GENC')
+    if(!IS_GEN_BINARY(binary)){
+        return NULL;
+    }
+    // check the whether the current device ID match the binary file's.
+    if(!BINARY_MATCH(bin_deviceID, deviceID)){
+      return NULL;
+    }
+
+    binary_content.assign(binary+9, size-9);
     GenProgram *program = GBE_NEW(GenProgram, deviceID);
     std::istringstream ifs(binary_content, std::ostringstream::binary);
-    // FIXME we need to check the whether the current device ID match the binary file's.
-    deviceID = deviceID;
 
     if (!program->deserializeFromBin(ifs)) {
       delete program;
@@ -255,11 +269,13 @@ namespace gbe {
       }
 
       //add header to differetiate from llvm bitcode binary.
-      //the header length is 5 bytes: 1 binary type, 4 bitcode header.
-      *binary = (char *)malloc(sizeof(char) * (sz+5) );
-      memset(*binary, 0, sizeof(char) * (sz+5) );
-      memcpy(*binary+5, oss.str().c_str(), sz*sizeof(char));
-      return sz+5;
+      //the header length is 9 bytes: 1 byte is binary type, 4 bytes are bitcode header, 4  bytes are device id info.
+      *binary = (char *)malloc(sizeof(char) * (sz+9) );
+      memset(*binary, 0, sizeof(char) * (sz+9) );
+      FILL_GEN_BINARY(*binary);
+      FILL_DEVICE_ID(*binary, prog->deviceID);
+      memcpy(*binary+9, oss.str().c_str(), sz*sizeof(char));
+      return sz+9;
     }else{
 #ifdef GBE_COMPILER_AVAILABLE
       std::string str;
diff --git a/backend/src/gbe_bin_generater.cpp b/backend/src/gbe_bin_generater.cpp
index d9ae946..e7a5b97 100644
--- a/backend/src/gbe_bin_generater.cpp
+++ b/backend/src/gbe_bin_generater.cpp
@@ -170,6 +170,7 @@ void program_build_instance::serialize_program(void) throw(int)
         OUTS_UPDATE_SZ(gen_header[2]);
         OUTS_UPDATE_SZ(gen_header[3]);
         OUTS_UPDATE_SZ(gen_header[4]);
+        OUTS_UPDATE_SZ(gen_pci_id);
       }
 
       string array_name = "Unknown_name_array";
@@ -213,6 +214,7 @@ void program_build_instance::serialize_program(void) throw(int)
         OUTF_UPDATE_SZ(gen_header[2]);
         OUTF_UPDATE_SZ(gen_header[3]);
         OUTF_UPDATE_SZ(gen_header[4]);
+        OUTF_UPDATE_SZ(gen_pci_id);
         sz = gbe_prog->serializeToBin(ofs);
       }else{
         char *llvm_binary;
-- 
1.8.1.2



More information about the Beignet mailing list