[Beignet] [Printf v2][PATCH 01/12] Change printf data structure and remove old code.

yan.wang at linux.intel.com yan.wang at linux.intel.com
Sun Jan 31 23:34:28 PST 2016


From: Yan Wang <yan.wang at linux.intel.com>

Contributor: Junyan He <junyan.he at linux.intel.com>
Signed-off-by: Yan Wang <yan.wang at linux.intel.com>
---
 backend/src/backend/program.cpp         |  14 --
 backend/src/backend/program.hpp         |  10 +-
 backend/src/gbe_bin_interpreter.cpp     |   2 -
 backend/src/ir/printf.cpp               | 168 -----------------
 backend/src/ir/printf.hpp               |  76 +++-----
 backend/src/llvm/llvm_gen_backend.cpp   |  13 +-
 backend/src/llvm/llvm_printf_parser.cpp | 313 ++------------------------------
 7 files changed, 36 insertions(+), 560 deletions(-)

diff --git a/backend/src/backend/program.cpp b/backend/src/backend/program.cpp
index 7d12f73..4eca9f1 100644
--- a/backend/src/backend/program.cpp
+++ b/backend/src/backend/program.cpp
@@ -1265,24 +1265,12 @@ namespace gbe {
     return ps->getBufBTI();
   }
 
-  static uint8_t kernelGetPrintfIndexBufBTI(void * printf_info) {
-    if (printf_info == NULL) return 0;
-    const ir::PrintfSet *ps = (ir::PrintfSet *)printf_info;
-    return ps->getIndexBufBTI();
-  }
-
   static void kernelReleasePrintfSet(void * printf_info) {
     if (printf_info == NULL) return;
     ir::PrintfSet *ps = (ir::PrintfSet *)printf_info;
     delete ps;
   }
 
-  static uint32_t kernelGetPrintfSizeOfSize(void * printf_info) {
-    if (printf_info == NULL) return 0;
-    const ir::PrintfSet *ps = (ir::PrintfSet *)printf_info;
-    return ps->getPrintfSizeOfSize();
-  }
-
   static void kernelOutputPrintf(void * printf_info, void* index_addr,
                                  void* buf_addr, size_t global_wk_sz0,
                                  size_t global_wk_sz1, size_t global_wk_sz2,
@@ -1426,9 +1414,7 @@ namespace gbe
       gbe_dup_profiling = gbe::kernelDupProfiling;
       gbe_output_profiling = gbe::kernelOutputProfiling;
       gbe_get_printf_buf_bti = gbe::kernelGetPrintfBufBTI;
-      gbe_get_printf_indexbuf_bti = gbe::kernelGetPrintfIndexBufBTI;
       gbe_dup_printfset = gbe::kernelDupPrintfSet;
-      gbe_get_printf_sizeof_size = gbe::kernelGetPrintfSizeOfSize;
       gbe_release_printf_info = gbe::kernelReleasePrintfSet;
       gbe_output_printf = gbe::kernelOutputPrintf;
       genSetupCallBacks();
diff --git a/backend/src/backend/program.hpp b/backend/src/backend/program.hpp
index 775e560..e5c4b95 100644
--- a/backend/src/backend/program.hpp
+++ b/backend/src/backend/program.hpp
@@ -152,10 +152,7 @@ namespace gbe {
     void setPrintfSet(ir::PrintfSet * from) {
       printfSet = from;
     }
-    /* ! Return the offset in the sizeof(xxx). */
-    uint32_t getPrintfSizeOfSize(void) const {
-      return printfSet ? printfSet->getPrintfSizeOfSize() : 0;
-    }
+
     uint32_t getPrintfNum() const {
       return printfSet ? printfSet->getPrintfNum() : 0;
     }
@@ -169,11 +166,6 @@ namespace gbe {
       return printfSet->getBufBTI();
     }
 
-    uint8_t getPrintfIndexBufBTI() const {
-      GBE_ASSERT(printfSet);
-      return printfSet->getIndexBufBTI();
-    }
-
     void outputPrintf(void* index_addr, void* buf_addr, size_t global_wk_sz0,
                       size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz) {
       if(printfSet)
diff --git a/backend/src/gbe_bin_interpreter.cpp b/backend/src/gbe_bin_interpreter.cpp
index 4756842..34d04dd 100644
--- a/backend/src/gbe_bin_interpreter.cpp
+++ b/backend/src/gbe_bin_interpreter.cpp
@@ -70,9 +70,7 @@ struct BinInterpCallBackInitializer
     gbe_output_profiling = gbe::kernelOutputProfiling;
     gbe_get_printf_num = gbe::kernelGetPrintfNum;
     gbe_get_printf_buf_bti = gbe::kernelGetPrintfBufBTI;
-    gbe_get_printf_indexbuf_bti = gbe::kernelGetPrintfIndexBufBTI;
     gbe_dup_printfset = gbe::kernelDupPrintfSet;
-    gbe_get_printf_sizeof_size = gbe::kernelGetPrintfSizeOfSize;
     gbe_release_printf_info = gbe::kernelReleasePrintfSet;
     gbe_output_printf = gbe::kernelOutputPrintf;
   }
diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index 2e08248..3873ca9 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -32,43 +32,6 @@ namespace gbe
 
     pthread_mutex_t PrintfSet::lock = PTHREAD_MUTEX_INITIALIZER;
 
-    PrintfSlot::~PrintfSlot(void)
-    {
-        if (ptr)
-        {
-          if (type == PRINTF_SLOT_TYPE_STRING) {
-            free(ptr);
-            ptr = NULL;
-          } else if (type == PRINTF_SLOT_TYPE_STATE) {
-            delete state;
-            state = NULL;
-          } else {
-            type = PRINTF_SLOT_TYPE_NONE;
-            ptr = NULL;
-          }
-        }
-    }
-
-    uint32_t PrintfSet::append(PrintfFmt* fmt, Unit& unit)
-    {
-      fmts.push_back(*fmt);
-      vector<PrintfSlot>& vp = fmts.back().first;
-
-      for (vector<PrintfSlot>::iterator f = vp.begin(); f !=  vp.end(); ++f) {
-        if (f->type == PRINTF_SLOT_TYPE_STRING)
-          continue;
-
-        slots.push_back(*f);
-      }
-
-      /* Update the total size of size. */
-      if (slots.size() > 0)
-        sizeOfSize = slots.back().state->out_buf_sizeof_offset
-                     + getPrintfBufferElementSize(slots.size() - 1);
-
-      return (uint32_t)fmts.size();
-    }
-
     static void generatePrintfFmtString(PrintfState& state, std::string& str)
     {
       char num_str[16];
@@ -140,137 +103,6 @@ namespace gbe
                                  size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz)
     {
       LockOutput lock;
-      size_t i, j, k;
-      std::string pf_str;
-      int stmt = 0;
-
-      for (size_t count = 0; count < fmts.size(); ++count) {
-        for (i = 0; i < global_wk_sz0; i++) {
-          for (j = 0; j < global_wk_sz1; j++) {
-            for (k = 0; k < global_wk_sz2; k++) {
-              int loop_num = ((int *)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
-                                                 + k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2];
-              int printf_num = ((int *)index_addr)[(stmt*global_wk_sz0*global_wk_sz1*global_wk_sz2
-                                                 + k*global_wk_sz0*global_wk_sz1 + j*global_wk_sz0 + i)*2 + 1];
-              if (!loop_num) continue;
-
-              PrintfFmt* ppf = NULL;
-              for (auto& f : fmts) {
-                if (f.second == printf_num) {
-                  ppf = &f;
-                  break;
-                }
-              }
-
-              PrintfFmt& pf = *ppf;
-
-              for (int n = 0; n < loop_num; n++) {
-                for (vector<PrintfSlot>::iterator pfit = pf.first.begin(); pfit != pf.first.end(); ++pfit) {
-                  PrintfSlot& slot = *pfit;
-                  pf_str = "";
-                  int vec_num;
-
-                  if (slot.type == PRINTF_SLOT_TYPE_STRING) {
-                    printf("%s", slot.str);
-                    continue;
-                  }
-                  assert(slot.type == PRINTF_SLOT_TYPE_STATE);
-
-                  generatePrintfFmtString(*slot.state, pf_str);
-
-
-                  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;
-                    }
-                  }
-
-                  pf_str = "";
-                }
-
-              }
-            }
-          }
-        }
-        stmt++;
-      }
     }
   } /* namespace ir */
 } /* namespace gbe */
diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp
index 85153a5..def6331 100644
--- a/backend/src/ir/printf.hpp
+++ b/backend/src/ir/printf.hpp
@@ -111,55 +111,37 @@ namespace gbe
     };
 
     struct PrintfSlot {
-      int type;
-      union {
-        char* str;
-        PrintfState* state;
-        void *ptr;
-      };
+      uint32_t type;
+      std::string str;
+      PrintfState state;
 
       PrintfSlot(void) {
         type = PRINTF_SLOT_TYPE_NONE;
-        ptr = NULL;
       }
 
-      PrintfSlot(const char * s) {
+      PrintfSlot(std::string& s) : str(s) {
         type = PRINTF_SLOT_TYPE_STRING;
-        int len = strlen(s);
-        str = (char*)malloc((len + 1) * sizeof(char));
-        memcpy(str, s, (len + 1) * sizeof(char));
-        str[len] = 0;
       }
 
-      PrintfSlot(PrintfState * st) {
+      PrintfSlot(PrintfState& st) {
         type = PRINTF_SLOT_TYPE_STATE;
-        state = new PrintfState(*st);
+        state = st;
       }
 
       PrintfSlot(const PrintfSlot & other) {
         if (other.type == PRINTF_SLOT_TYPE_STRING) {
-          int len = strlen(other.str);
-          str = (char*)malloc((len + 1) * sizeof(char));
-          memcpy(str, other.str, (len + 1) * sizeof(char));
-          str[len] = 0;
           type = PRINTF_SLOT_TYPE_STRING;
+          str = other.str;
         } else if (other.type == PRINTF_SLOT_TYPE_STATE) {
           type = PRINTF_SLOT_TYPE_STATE;
-          state = new PrintfState(*other.state);
+          state = other.state;
         } else {
           type = PRINTF_SLOT_TYPE_NONE;
-          ptr = NULL;
         }
       }
 
-      PrintfSlot(PrintfSlot && other) {
-        void *p = other.ptr;
-        type = other.type;
-        other.ptr = ptr;
-        ptr = p;
+      ~PrintfSlot(void) {
       }
-
-      ~PrintfSlot(void);
     };
 
     class Context;
@@ -168,19 +150,8 @@ namespace gbe
     {
     public:
       PrintfSet(const PrintfSet& other) {
-        for (size_t i = 0; i < other.fmts.size(); ++i) {
-          const PrintfFmt& f = other.fmts[i];
-          fmts.push_back(f);
-        }
-
-        for (size_t i = 0; i < other.slots.size(); ++i) {
-          PrintfSlot s = other.slots[i];
-          slots.push_back(s);
-        }
-
-        sizeOfSize = other.sizeOfSize;
+        fmts = other.fmts;
         btiBuf = other.btiBuf;
-        btiIndexBuf = other.btiIndexBuf;
       }
 
       PrintfSet(void) = default;
@@ -195,32 +166,30 @@ namespace gbe
         }
       };
 
-      typedef std::pair<vector<PrintfSlot>, int> PrintfFmt;
-      uint32_t append(PrintfFmt* fmt, Unit &unit);
+      typedef vector<PrintfSlot> PrintfFmt;
 
-      uint32_t getPrintfNum(void) const {
-        return fmts.size();
+      void append(uint32_t num, PrintfFmt* fmt) {
+        GBE_ASSERT(fmts.find(num) == fmts.end());
+        fmts.insert(std::pair<uint32_t, PrintfFmt>(num, *fmt));
       }
 
-      uint32_t getPrintfSizeOfSize(void) const {
-        return sizeOfSize;
+      uint32_t getPrintfNum(void) const {
+        return fmts.size();
       }
 
       void setBufBTI(uint8_t b)      { btiBuf = b; }
-      void setIndexBufBTI(uint8_t b) { btiIndexBuf = b; }
       uint8_t getBufBTI() const      { return btiBuf; }
-      uint8_t getIndexBufBTI() const { return btiIndexBuf; }
 
       uint32_t getPrintfBufferElementSize(uint32_t i) {
-        PrintfSlot& slot = slots[i];
+        PrintfSlot slot;
         int vec_num = 1;
-        if (slot.state->vector_n > 0) {
-          vec_num = slot.state->vector_n;
+        if (slot.state.vector_n > 0) {
+          vec_num = slot.state.vector_n;
         }
 
         assert(vec_num > 0 && vec_num <= 16);
 
-        switch (slot.state->conversion_specifier) {
+        switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_I:
           case PRINTF_CONVERSION_D:
           case PRINTF_CONVERSION_O:
@@ -253,12 +222,9 @@ namespace gbe
                         size_t global_wk_sz1, size_t global_wk_sz2, size_t output_sz);
 
     private:
-      vector<PrintfFmt> fmts;
-      vector<PrintfSlot> slots;
-      uint32_t sizeOfSize; // Total sizeof size.
+      std::map<uint32_t, PrintfFmt> fmts;
       friend struct LockOutput;
       uint8_t btiBuf;
-      uint8_t btiIndexBuf;
       static pthread_mutex_t lock;
       GBE_CLASS(PrintfSet);
     };
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index 886b6f3..dec023c 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -2676,15 +2676,7 @@ namespace gbe
         ir::Constant &con = unit.getConstantSet().getConstant(v.getName());
         ctx.LOADI(ir::TYPE_S32, reg, ctx.newIntegerImmediate(con.getOffset(), ir::TYPE_S32));
       } else {
-        if(v.getName().equals(StringRef("__gen_ocl_printf_buf"))) {
-          ctx.getFunction().getPrintfSet()->setBufBTI(BtiMap.find(const_cast<GlobalVariable*>(&v))->second);
-          this->newRegister(const_cast<GlobalVariable*>(&v), NULL, true);
-          ctx.CVT(ir::TYPE_U32, ir::TYPE_U64, getRegister(const_cast<GlobalVariable*>(&v)), ir::ocl::printfbptr);
-        } else if(v.getName().equals(StringRef("__gen_ocl_printf_index_buf"))) {
-          ctx.getFunction().getPrintfSet()->setIndexBufBTI(BtiMap.find(const_cast<GlobalVariable*>(&v))->second);
-          this->newRegister(const_cast<GlobalVariable*>(&v), NULL, true);
-          ctx.CVT(ir::TYPE_U32, ir::TYPE_U64, getRegister(const_cast<GlobalVariable*>(&v)), ir::ocl::printfiptr);
-        } else if(v.getName().equals(StringRef("__gen_ocl_profiling_buf"))) {
+        if(v.getName().equals(StringRef("__gen_ocl_profiling_buf"))) {
           ctx.getUnit().getProfilingInfo()->setBTI(BtiMap.find(const_cast<GlobalVariable*>(&v))->second);
           regTranslator.newScalarProxy(ir::ocl::profilingbptr, const_cast<GlobalVariable*>(&v));
         } else if(v.getName().str().substr(0, 4) == ".str") {
@@ -4535,9 +4527,6 @@ namespace gbe
 
           case GEN_OCL_PRINTF:
           {
-            ir::PrintfSet::PrintfFmt* fmt = (ir::PrintfSet::PrintfFmt*)getPrintfInfo(&I);
-            ctx.getFunction().getPrintfSet()->append(fmt, unit);
-            assert(fmt);
             break;
           }
           case GEN_OCL_CALC_TIMESTAMP:
diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp
index 422f16b..1c88981 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -236,8 +236,8 @@ again:
       }
 
       if (p != begin) {
-        std::string s = std::string(begin, size_t(p - begin));
-        printf_fmt->first.push_back(PrintfSlot(s.c_str()));
+        std::string s(begin, size_t(p - begin));
+        printf_fmt->push_back(PrintfSlot(s));
       }
 
       if (p == end) // finish
@@ -248,7 +248,7 @@ again:
       if (ret_char < 0)
         goto error;
 
-      printf_fmt->first.push_back(&state);
+      printf_fmt->push_back(state);
       num++;
 
       if (rend == end)
@@ -328,7 +328,6 @@ error:
 
     bool parseOnePrintfInstruction(CallInst * call, PrintfParserInfo& info, int& sizeof_size);
     bool generateOneParameterInst(PrintfSlot& slot, Value*& arg, Type*& dst_type, int& sizeof_size);
-    bool generateOnePrintfInstruction(PrintfParserInfo& pInfo);
 
     virtual const char *getPassName() const
     {
@@ -338,95 +337,6 @@ error:
     virtual bool runOnFunction(llvm::Function &F);
   };
 
-  bool PrintfParser::generateOnePrintfInstruction(PrintfParserInfo& pInfo)
-  {
-    Value* op0 = NULL;
-    Value* val = NULL;
-
-    /////////////////////////////////////////////////////
-    /* calculate index address.
-       index_addr = (index_offset + wg_offset )* sizeof(int) * 2 + index_buf_ptr
-       index_offset = global_size2 * global_size1 * global_size0 * printf_num */
-
-    Value* index_offset = builder->CreateMul(g1Xg2Xg3, ConstantInt::get(intTy, printf_num));
-    // index_offset + offset
-    op0 = builder->CreateAdd(index_offset, wg_offset);
-    // (index_offset + offset)* sizeof(int) * 2
-    op0 = builder->CreateMul(op0, ConstantInt::get(intTy, sizeof(int)*2));
-    // Final index address = index_buf_ptr + (index_offset + offset)* sizeof(int)
-    op0 = builder->CreateAdd(index_buf_ptr, op0);
-    Value* index_addr = builder->CreateIntToPtr(op0, Type::getInt32PtrTy(module->getContext(), 1));
-    // Load the printf num first, printf may be in loop.
-    Value* loop_num = builder->CreateLoad(index_addr);
-    val = builder->CreateAdd(loop_num, ConstantInt::get(intTy, 1));
-    builder->CreateStore(val, index_addr);// The loop number.
-
-    op0 = builder->CreateAdd(op0, ConstantInt::get(intTy, sizeof(int)));
-    index_addr = builder->CreateIntToPtr(op0, Type::getInt32PtrTy(module->getContext(), 1));
-    builder->CreateStore(ConstantInt::get(intTy, printf_num), index_addr);// The printf number.
-
-    int i = 1;
-    Value* data_addr = NULL;
-    for (auto &s : (*pInfo.printf_fmt).first) {
-      if (s.type == PRINTF_SLOT_TYPE_STRING)
-        continue;
-
-      assert(i < static_cast<int>(pInfo.call->getNumOperands()) - 1);
-
-      Value *out_arg = pInfo.call->getOperand(i);
-      Type *dst_type = NULL;
-      int sizeof_size = 0;
-      if (!generateOneParameterInst(s, out_arg, dst_type, sizeof_size)) {
-        printf("Printf: %d, parameter %d may have no result because some error\n",
-               printf_num, i - 1);
-        i++;
-        continue;
-      }
-
-      s.state->out_buf_sizeof_offset = out_buf_sizeof_offset;
-      if (!sizeof_size) {
-        i++;
-        continue;
-      }
-
-      assert(dst_type);
-
-      /////////////////////////////////////////////////////
-      /* Calculate the data address.
-      data_addr = (data_offset + pbuf_ptr + offset * sizeof(specify)) +
-               totalSizeofSize * global_size2 * global_size1 * global_size0 * loop_num
-      data_offset = global_size2 * global_size1 * global_size0 * out_buf_sizeof_offset
-
-      //global_size2 * global_size1 * global_size0 * out_buf_sizeof_offset */
-      op0 = builder->CreateMul(g1Xg2Xg3, ConstantInt::get(intTy, out_buf_sizeof_offset));
-      //offset * sizeof(specify)
-      val = builder->CreateMul(wg_offset, ConstantInt::get(intTy, sizeof_size));
-      //data_offset + pbuf_ptr
-      op0 = builder->CreateAdd(pbuf_ptr, op0);
-      op0 = builder->CreateAdd(op0, val);
-      //totalSizeofSize * global_size2 * global_size1 * global_size0
-      val = builder->CreateMul(g1Xg2Xg3, ConstantInt::get(intTy, totalSizeofSize));
-      //totalSizeofSize * global_size2 * global_size1 * global_size0 * loop_num
-      val = builder->CreateMul(val, loop_num);
-      //final
-      op0 = builder->CreateAdd(op0, val);
-      data_addr = builder->CreateIntToPtr(op0, dst_type);
-      builder->CreateStore(out_arg, data_addr);
-
-      out_buf_sizeof_offset += ((sizeof_size + 3) / 4) * 4;
-      i++;
-    }
-
-    CallInst* printf_inst = builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
-                              "__gen_ocl_printf", Type::getVoidTy(module->getContext()),
-                              NULL)));
-    assert(unit.printfs[printf_inst] == NULL);
-    unit.printfs[printf_inst] = pInfo.printf_fmt;
-    unit.printfs[printf_inst]->second = printf_num;
-    printf_num++;
-    return true;
-  }
-
   bool PrintfParser::parseOnePrintfInstruction(CallInst * call, PrintfParserInfo& info, int& sizeof_size)
   {
     CallSite CS(call);
@@ -459,62 +369,11 @@ error:
       return false;
     }
 
-    info.call = call;
-    info.printf_fmt = printf_fmt;
-
-    sizeof_size = 0;
-    int i = 1;
-    for (auto &s : (*printf_fmt).first) {
-      int sz = 0;
-      if (s.type == PRINTF_SLOT_TYPE_STRING)
-        continue;
-
-      assert(i < static_cast<int>(call->getNumOperands()) - 1);
-
-      switch (s.state->conversion_specifier) {
-        case PRINTF_CONVERSION_I:
-        case PRINTF_CONVERSION_D:
-        case PRINTF_CONVERSION_O:
-        case PRINTF_CONVERSION_U:
-        case PRINTF_CONVERSION_x:
-        case PRINTF_CONVERSION_X:
-        case PRINTF_CONVERSION_P:
-          if (s.state->length_modifier == PRINTF_LM_L)
-            sz = sizeof(int64_t);
-          else
-            sz = sizeof(int);
-          break;
-        case PRINTF_CONVERSION_C:
-          sz = sizeof(char);
-          break;
-        case PRINTF_CONVERSION_F:
-        case PRINTF_CONVERSION_f:
-        case PRINTF_CONVERSION_E:
-        case PRINTF_CONVERSION_e:
-        case PRINTF_CONVERSION_G:
-        case PRINTF_CONVERSION_g:
-        case PRINTF_CONVERSION_A:
-        case PRINTF_CONVERSION_a:
-          sz = sizeof(float);
-          break;
-        default:
-          sz = 0;
-          break;
-      }
-
-      if (s.state->vector_n) {
-        sz = sz * s.state->vector_n;
-      }
-
-      sizeof_size += ((sz + 3) / 4) * 4;
-    }
-
     return true;
   }
 
   bool PrintfParser::runOnFunction(llvm::Function &F)
   {
-    bool changed = false;
     bool hasPrintf = false;
     switch (F.getCallingConv()) {
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR <= 2
@@ -531,8 +390,6 @@ error:
         GBE_ASSERTM(false, "Unsupported calling convention");
     }
 
-    std::vector<PrintfParserInfo> infoVect;
-    totalSizeofSize = 0;
     module = F.getParent();
     intTy = IntegerType::get(module->getContext(), 32);
 
@@ -556,9 +413,6 @@ error:
       for (BasicBlock::iterator instI = B->begin(),
            instE = B->end(); instI != instE; ++instI) {
 
-        PrintfParserInfo pInfo;
-        int sizeof_size = 0;
-
         llvm::CallInst* call = dyn_cast<llvm::CallInst>(instI);
         if (!call) {
           continue;
@@ -573,152 +427,11 @@ error:
         if (fnName != "__gen_ocl_printf_stub" && fnName != "__gen_ocl_puts_stub")
           continue;
 
-        if (!parseOnePrintfInstruction(call, pInfo, sizeof_size)) {
-          printf("Parse One printf inst failed, may have some error\n");
-          // Just kill this printf instruction.
-          deadprintfs.push_back(PrintfInst(cast<Instruction>(call),0));
-          continue;
-        }
-
         hasPrintf = true;
-
-        infoVect.push_back(pInfo);
-        totalSizeofSize += sizeof_size;
       }
     }
 
-    if (!hasPrintf)
-      return changed;
-
-    if (!pbuf_ptr) {
-      /* alloc a new buffer ptr to collect the print output. */
-      Type *ptrTy = Type::getInt32PtrTy(module->getContext(), 1);
-      llvm::Constant *pBuf = new GlobalVariable(*module, ptrTy, false,
-                                GlobalVariable::ExternalLinkage,
-                                nullptr,
-                                StringRef("__gen_ocl_printf_buf"),
-                                nullptr,
-                                GlobalVariable::NotThreadLocal,
-                                1);
-      pbuf_ptr = builder->CreatePtrToInt(pBuf, Type::getInt32Ty(module->getContext()));
-    }
-    if (!index_buf_ptr) {
-      Type *ptrTy = Type::getInt32PtrTy(module->getContext(), 1);
-      llvm::Constant *pBuf = new GlobalVariable(*module, ptrTy, false,
-                                GlobalVariable::ExternalLinkage,
-                                nullptr,
-                                StringRef("__gen_ocl_printf_index_buf"),
-                                nullptr,
-                                GlobalVariable::NotThreadLocal,
-                                1);
-      index_buf_ptr = builder->CreatePtrToInt(pBuf, Type::getInt32Ty(module->getContext()));
-    }
-
-    if (!wg_offset || !g1Xg2Xg3) {
-      Value* op0 = NULL;
-      Value* val = NULL;
-
-      builder->SetInsertPoint(F.begin()->begin());// Insert the common var in the begin.
-
-      /* FIXME: Because the OpenCL language do not support va macro, and we do not want
-         to introduce the va_list, va_start and va_end into our code, we just simulate
-         the function calls to caculate the offset caculation here. */
-#define BUILD_CALL_INST(name) \
-	CallInst* name = builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction( \
-				 "__gen_ocl_get_"#name, 					\
-				 IntegerType::getInt32Ty(module->getContext()), 		\
-				 NULL)))
-
-      BUILD_CALL_INST(group_id2);
-      BUILD_CALL_INST(group_id1);
-      BUILD_CALL_INST(group_id0);
-      BUILD_CALL_INST(global_size2);
-      BUILD_CALL_INST(global_size1);
-      BUILD_CALL_INST(global_size0);
-      BUILD_CALL_INST(local_id2);
-      BUILD_CALL_INST(local_id1);
-      BUILD_CALL_INST(local_id0);
-      BUILD_CALL_INST(local_size2);
-      BUILD_CALL_INST(local_size1);
-      BUILD_CALL_INST(local_size0);
-
-#undef BUILD_CALL_INST
-
-      /* calculate offset for later usage.
-         wg_offset = ((local_id2 + local_size2 * group_id2) * (global_size1 * global_size0)
-         + (local_id1 + local_size1 * group_id1) * global_size0
-         + (local_id0 + local_size0 * group_id0))  */
-
-      // local_size2 * group_id2
-      val = builder->CreateMul(local_size2, group_id2);
-      // local_id2 + local_size2 * group_id2
-      val = builder->CreateAdd(local_id2, val);
-      // global_size1 * global_size0
-      op0 = builder->CreateMul(global_size1, global_size0);
-      // (local_id2 + local_size2 * group_id2) * (global_size1 * global_size0)
-      Value* offset1 = builder->CreateMul(val, op0);
-      // local_size1 * group_id1
-      val = builder->CreateMul(local_size1, group_id1);
-      // local_id1 + local_size1 * group_id1
-      val = builder->CreateAdd(local_id1, val);
-      // (local_id1 + local_size1 * group_id1) * global_size_0
-      Value* offset2 = builder->CreateMul(val, global_size0);
-      // local_size0 * group_id0
-      val = builder->CreateMul(local_size0, group_id0);
-      // local_id0 + local_size0 * group_id0
-      val = builder->CreateAdd(local_id0, val);
-      // The total sum
-      val = builder->CreateAdd(val, offset1);
-      wg_offset = builder->CreateAdd(val, offset2);
-
-      // global_size2 * global_size1
-      op0 = builder->CreateMul(global_size2, global_size1);
-      // global_size2 * global_size1 * global_size0
-      g1Xg2Xg3 = builder->CreateMul(op0, global_size0);
-    }
-
-
-    /* Now generate the instructions. */
-    for (auto pInfo : infoVect) {
-      builder->SetInsertPoint(pInfo.call);
-      deadprintfs.push_back(PrintfInst(cast<Instruction>(pInfo.call), generateOnePrintfInstruction(pInfo)));
-    }
-
-    assert(out_buf_sizeof_offset == totalSizeofSize);
-
-    /* Replace the instruction's operand if using printf's return value. */
-    for (llvm::Function::iterator B = F.begin(), BE = F.end(); B != BE; B++) {
-      for (BasicBlock::iterator instI = B->begin(),
-           instE = B->end(); instI != instE; ++instI) {
-
-        for (unsigned i = 0; i < instI->getNumOperands(); i++) {
-          for (auto &prf : deadprintfs) {
-            if (instI->getOperand(i) == prf.first) {
-
-              if (prf.second == true) {
-                instI->setOperand(i, ConstantInt::get(intTy, 0));
-              } else {
-                instI->setOperand(i, ConstantInt::get(intTy, -1));
-              }
-            }
-          }
-        }
-      }
-    }
-
-    /* Kill the dead printf instructions. */
-    for (auto &prf : deadprintfs) {
-      prf.first->dropAllReferences();
-      if (prf.first->use_empty())
-        prf.first->eraseFromParent();
-    }
-
-    deadprintfs.clear();
-    delete builder;
-    g1Xg2Xg3 = NULL;
-    wg_offset = NULL;
-
-    return changed;
+    return hasPrintf;
   }
 
   bool PrintfParser::generateOneParameterInst(PrintfSlot& slot, Value*& arg, Type*& dst_type, int& sizeof_size)
@@ -731,7 +444,7 @@ error:
     switch (arg->getType()->getTypeID()) {
       case Type::IntegerTyID: {
         bool sign = false;
-        switch (slot.state->conversion_specifier) {
+        switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_I:
           case PRINTF_CONVERSION_D:
             sign = true;
@@ -739,7 +452,7 @@ error:
           case PRINTF_CONVERSION_U:
           case PRINTF_CONVERSION_x:
           case PRINTF_CONVERSION_X:
-            if (slot.state->length_modifier == PRINTF_LM_L) { /* we would rather print long. */
+            if (slot.state.length_modifier == PRINTF_LM_L) { /* we would rather print long. */
               if (arg->getType() != Type::getInt64Ty(module->getContext())) {
                 arg = builder->CreateIntCast(arg, Type::getInt64Ty(module->getContext()), sign);
               }
@@ -781,7 +494,7 @@ error:
           case PRINTF_CONVERSION_S:
             /* Here, the case is printf("xxx%s", 0); we should output the null. */
             sizeof_size = 0;
-            slot.state->str = "(null)";
+            slot.state.str = "(null)";
             return true;
 
           default:
@@ -802,7 +515,7 @@ error:
         /* Because the printf is a variable parameter function, it does not have the
            function prototype, so the compiler will always promote the arg to the
            longest precise type for float. So here, we can always find it is double. */
-        switch (slot.state->conversion_specifier) {
+        switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_I:
           case PRINTF_CONVERSION_D:
             /* Float to Int, add a conversion. */
@@ -845,7 +558,7 @@ error:
 
       /* %p and %s */
       case Type::PointerTyID:
-        switch (slot.state->conversion_specifier) {
+        switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_S: {
             llvm::Constant* arg0 = dyn_cast<llvm::ConstantExpr>(arg);
             llvm::Constant* arg0_ptr = dyn_cast<llvm::Constant>(arg0->getOperand(0));
@@ -858,7 +571,7 @@ error:
               return false;
             }
             sizeof_size = 0;
-            slot.state->str = fmt_arg->getAsCString();
+            slot.state.str = fmt_arg->getAsCString();
             return true;
           }
           case PRINTF_CONVERSION_P: {
@@ -879,12 +592,12 @@ error:
         int vec_num = vect_type->getVectorNumElements();
         bool sign = false;
 
-        if (vec_num != slot.state->vector_n) {
+        if (vec_num != slot.state.vector_n) {
           printf("Error The printf vector number is not match!\n");
           return false;
         }
 
-        switch (slot.state->conversion_specifier) {
+        switch (slot.state.conversion_specifier) {
           case PRINTF_CONVERSION_I:
           case PRINTF_CONVERSION_D:
             sign = true;
@@ -898,7 +611,7 @@ error:
             }
 
             Type* elt_dst_type = NULL;
-            if (slot.state->length_modifier == PRINTF_LM_L) {
+            if (slot.state.length_modifier == PRINTF_LM_L) {
               elt_dst_type = Type::getInt64Ty(elt_type->getContext());
             } else {
               elt_dst_type = Type::getInt32Ty(elt_type->getContext());
-- 
2.4.3



More information about the Beignet mailing list