[Beignet] [PATCH 2/2] GBE: warning when the GEN binary version mismatch.

Yang Rong rong.r.yang at intel.com
Mon Jun 6 09:48:32 UTC 2016


GENC is indicate the binary type, so use the first byte of GEN binary
as binary version, if mismatch, output a warning.

Signed-off-by: Yang Rong <rong.r.yang at intel.com>
---
 backend/src/backend/gen_program.cpp | 24 +++++++++++++++---------
 backend/src/gbe_bin_generater.cpp   |  8 ++++----
 src/cl_program.c                    | 12 ++++++++++--
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/backend/src/backend/gen_program.cpp b/backend/src/backend/gen_program.cpp
index 69ddb22..88010c2 100644
--- a/backend/src/backend/gen_program.cpp
+++ b/backend/src/backend/gen_program.cpp
@@ -235,15 +235,15 @@ namespace gbe {
     GBHI_BXT = 6,
     GBHI_MAX,
   };
-
+#define GEN_BINARY_VERSION  1
   static const unsigned char gen_binary_header[GBHI_MAX][GEN_BINARY_HEADER_LENGTH]= \
-                                             {{0, 'G','E', 'N', 'C', 'B', 'Y', 'T'},
-                                              {0, 'G','E', 'N', 'C', 'I', 'V', 'B'},
-                                              {0, 'G','E', 'N', 'C', 'H', 'S', 'W'},
-                                              {0, 'G','E', 'N', 'C', 'C', 'H', 'V'},
-                                              {0, 'G','E', 'N', 'C', 'B', 'D', 'W'},
-                                              {0, 'G','E', 'N', 'C', 'S', 'K', 'L'},
-                                              {0, 'G','E', 'N', 'C', 'B', 'X', 'T'}
+                                             {{GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'B', 'Y', 'T'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'I', 'V', 'B'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'H', 'S', 'W'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'C', 'H', 'V'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'B', 'D', 'W'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'S', 'K', 'L'},
+                                              {GEN_BINARY_VERSION, 'G','E', 'N', 'C', 'B', 'X', 'T'}
                                               };
 
 #define FILL_GEN_HEADER(binary, index)  do {int i = 0; do {*(binary+i) = gen_binary_header[index][i]; i++; }while(i < GEN_BINARY_HEADER_LENGTH);}while(0)
@@ -258,10 +258,16 @@ namespace gbe {
   static bool genHeaderCompare(const unsigned char *BufPtr, GEN_BINARY_HEADER_INDEX index)
   {
     bool matched = true;
-    for (int i =0; i < GEN_BINARY_HEADER_LENGTH; ++i)
+    for (int i = 1; i < GEN_BINARY_HEADER_LENGTH; ++i)
     {
       matched = matched && (BufPtr[i] == gen_binary_header[index][i]);
     }
+    if(matched) {
+      if(BufPtr[0] != gen_binary_header[index][0]) {
+        std::cout << "Beignet binary format have been changed, please generate binary again.\n";
+        matched = false;
+      }
+    }
     return matched;
   }
 
diff --git a/backend/src/gbe_bin_generater.cpp b/backend/src/gbe_bin_generater.cpp
index 7ed353a..8e42891 100644
--- a/backend/src/gbe_bin_generater.cpp
+++ b/backend/src/gbe_bin_generater.cpp
@@ -196,8 +196,8 @@ void program_build_instance::serialize_program(void) throw(int)
 
       if(gen_pci_id){
         //add header to differeciate from llvm bitcode binary.
-        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
-        char gen_header[6] = "\0GENC";
+        // (5 bytes: 1 byte for binary version, 4 byte for bc code, 'GENC' is for gen binary.)
+        char gen_header[6] = "\1GENC";
         OUTS_UPDATE_SZ(gen_header[0]);
         OUTS_UPDATE_SZ(gen_header[1]);
         OUTS_UPDATE_SZ(gen_header[2]);
@@ -243,8 +243,8 @@ void program_build_instance::serialize_program(void) throw(int)
     } else {
       if(gen_pci_id){
         //add header to differeciate from llvm bitcode binary.
-        // (5 bytes: 1 byte for binary type, 4 byte for bc code, 'GENC' is for gen binary.)
-        char gen_header[6] = "\0GENC";
+        // (5 bytes: 1 byte for binary version, 4 byte for bc code, 'GENC' is for gen binary.)
+        char gen_header[6] = "\1GENC";
         OUTF_UPDATE_SZ(gen_header[0]);
         OUTF_UPDATE_SZ(gen_header[1]);
         OUTF_UPDATE_SZ(gen_header[2]);
diff --git a/src/cl_program.c b/src/cl_program.c
index 93eba03..7e1ffda 100644
--- a/src/cl_program.c
+++ b/src/cl_program.c
@@ -183,7 +183,7 @@ static const unsigned char binary_type_header[BHI_MAX][BINARY_HEADER_LENGTH]=  \
                                               {{'B','C', 0xC0, 0xDE},
                                                {1, 'B', 'C', 0xC0, 0xDE},
                                                {2, 'B', 'C', 0xC0, 0xDE},
-                                               {0, 'G','E', 'N', 'C'},
+                                               {1, 'G','E', 'N', 'C'},
                                                {'C','I', 'S', 'A'},
                                                };
 
@@ -192,10 +192,18 @@ LOCAL cl_bool headerCompare(const unsigned char *BufPtr, BINARY_HEADER_INDEX ind
   bool matched = true;
   int length = (index == BHI_SPIR || index == BHI_CMRT) ? BINARY_HEADER_LENGTH -1 :BINARY_HEADER_LENGTH;
   int i = 0;
-  for (i = 0; i < length; ++i)
+  if(index == BHI_GEN_BINARY)
+    i = 1;
+  for (; i < length; ++i)
   {
     matched = matched && (BufPtr[i] == binary_type_header[index][i]);
   }
+  if(index == BHI_GEN_BINARY && matched) {
+    if(BufPtr[0] != binary_type_header[index][0]) {
+      DEBUGP(DL_WARNING, "Beignet binary format have been changed, please generate binary again.\n");
+      matched = false;
+    }
+  }
   return matched;
 }
 
-- 
2.1.4



More information about the Beignet mailing list