[Beignet] [Printf][PATCH 10/11] Output printf result.

Yan Wang yan.wang at linux.intel.com
Wed Jan 20 19:31:12 PST 2016


Contributor: Junyan He <junyan.he at linux.intel.com>
Signed-off-by: Yan Wang <yan.wang at linux.intel.com>
---
 backend/src/ir/printf.cpp | 122 +++++++++++++++++++++++++++++++++++++++++-----
 backend/src/ir/printf.hpp |   2 +-
 2 files changed, 112 insertions(+), 12 deletions(-)

diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index 19daa19..7ca127d 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -35,7 +35,7 @@ namespace gbe
     static void generatePrintfFmtString(PrintfState& state, std::string& str)
     {
       char num_str[16];
-      str += "%";
+      str = "%";
 
       if (state.left_justified) {
         str += "-";
@@ -87,21 +87,121 @@ namespace gbe
 #define PRINT_SOMETHING(target_ty, conv)  do {                          \
       if (!vec_i)                                                       \
         pf_str = pf_str + std::string(#conv);                           \
-      char *ptr = ((char *)buf_addr + sizeOfSize * global_wk_sz0 * global_wk_sz1 * global_wk_sz2 * n \
-                   + slot.state->out_buf_sizeof_offset *                \
-                   global_wk_sz0 * global_wk_sz1 * global_wk_sz2);      \
-      target_ty* obj_ptr = ((target_ty *)ptr) + (k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i) * vec_num + vec_i; \
-      if ((char *)obj_ptr + sizeof(target_ty) > (char *)buf_addr + output_sz) {            \
-        printf("\n\n!!!The printf message is out of range because of the limited buffer, ignore.\n"); \
-        return;                                                         \
-      }                                                                 \
-      printf(pf_str.c_str(),  *obj_ptr);                                \
+      printf(pf_str.c_str(), log.getData<target_ty>());                 \
     } while (0)
 
+    static void printOutOneStatement(PrintfSet::PrintfFmt& fmt, PrintfLog& log)
+    {
+      std::string pf_str = "";
+      for (auto& slot : fmt) {
+        if (slot.type == PRINTF_SLOT_TYPE_STRING) {
+          printf("%s", slot.str.c_str());
+          continue;
+        }
+        assert(slot.type == PRINTF_SLOT_TYPE_STATE);
+
+        generatePrintfFmtString(slot.state, pf_str);
+
+        int vec_num;
+        vec_num = slot.state.vector_n > 0 ? slot.state.vector_n : 1;
+
+        for (int vec_i = 0; vec_i < vec_num; vec_i++) {
+          if (vec_i)
+            printf(",");
+
+          switch (slot.state.conversion_specifier) {
+            case PRINTF_CONVERSION_D:
+            case PRINTF_CONVERSION_I:
+              if (slot.state.length_modifier == PRINTF_LM_L)
+                PRINT_SOMETHING(uint64_t, d);
+              else
+                PRINT_SOMETHING(int, d);
+              break;
+
+            case PRINTF_CONVERSION_O:
+              if (slot.state.length_modifier == PRINTF_LM_L)
+                PRINT_SOMETHING(uint64_t, o);
+              else
+                PRINT_SOMETHING(int, o);
+              break;
+            case PRINTF_CONVERSION_U:
+              if (slot.state.length_modifier == PRINTF_LM_L)
+                PRINT_SOMETHING(uint64_t, u);
+              else
+                PRINT_SOMETHING(int, u);
+              break;
+            case PRINTF_CONVERSION_X:
+              if (slot.state.length_modifier == PRINTF_LM_L)
+                PRINT_SOMETHING(uint64_t, X);
+              else
+                PRINT_SOMETHING(int, X);
+              break;
+            case PRINTF_CONVERSION_x:
+              if (slot.state.length_modifier == PRINTF_LM_L)
+                PRINT_SOMETHING(uint64_t, x);
+              else
+                PRINT_SOMETHING(int, x);
+              break;
+
+            case PRINTF_CONVERSION_C:
+              PRINT_SOMETHING(char, c);
+              break;
+
+            case PRINTF_CONVERSION_F:
+              PRINT_SOMETHING(float, F);
+              break;
+            case PRINTF_CONVERSION_f:
+              PRINT_SOMETHING(float, f);
+              break;
+            case PRINTF_CONVERSION_E:
+              PRINT_SOMETHING(float, E);
+              break;
+            case PRINTF_CONVERSION_e:
+              PRINT_SOMETHING(float, e);
+              break;
+            case PRINTF_CONVERSION_G:
+              PRINT_SOMETHING(float, G);
+              break;
+            case PRINTF_CONVERSION_g:
+              PRINT_SOMETHING(float, g);
+              break;
+            case PRINTF_CONVERSION_A:
+              PRINT_SOMETHING(float, A);
+              break;
+            case PRINTF_CONVERSION_a:
+              PRINT_SOMETHING(float, a);
+              break;
+            case PRINTF_CONVERSION_P:
+              PRINT_SOMETHING(int, p);
+              break;
+
+            case PRINTF_CONVERSION_S:
+              pf_str = pf_str + "s";
+              printf(pf_str.c_str(), slot.state.str.c_str());
+              break;
+
+            default:
+              assert(0);
+              return;
+          }
+        }
 
-    void PrintfSet::outputPrintf(void* index_addr)
+      }
+    }
+
+    void PrintfSet::outputPrintf(void* buf_addr)
     {
       LockOutput lock;
+      uint32_t totalSZ = ((uint32_t *)buf_addr)[0];
+      char* p = (char*)buf_addr + sizeof(uint32_t);
+
+      for (uint32_t parsed = 4; parsed < totalSZ; ) {
+        PrintfLog log(p);
+        GBE_ASSERT(fmts.find(log.statementNum) != fmts.end());
+        printOutOneStatement(fmts[log.statementNum], log);
+        parsed += log.size;
+        p += log.size;
+      }
     }
   } /* namespace ir */
 } /* namespace gbe */
diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp
index fc36283..728aa68 100644
--- a/backend/src/ir/printf.hpp
+++ b/backend/src/ir/printf.hpp
@@ -243,7 +243,7 @@ namespace gbe
         return 0;
       }
 
-      void outputPrintf(void* index_addr);
+      void outputPrintf(void* buf_addr);
 
     private:
       std::map<uint32_t, PrintfFmt> fmts;
-- 
2.4.3



More information about the Beignet mailing list