[Beignet] [PATCH] Backend: Refine printfs into ir unit

Pan Xiuli xiuli.pan at intel.com
Tue Nov 17 00:57:05 PST 2015


Move the printfs of PrintfParser into the ir::Unit to make the gbe
thread safe. The old static printfs will be cleared by othrer thread
when running in multithread.

Signed-off-by: Pan Xiuli <xiuli.pan at intel.com>
---
 backend/src/ir/printf.cpp               |  1 +
 backend/src/ir/printf.hpp               |  2 +-
 backend/src/ir/unit.cpp                 |  1 +
 backend/src/ir/unit.hpp                 |  6 ++++++
 backend/src/llvm/llvm_gen_backend.cpp   |  6 ++++++
 backend/src/llvm/llvm_gen_backend.hpp   |  2 +-
 backend/src/llvm/llvm_printf_parser.cpp | 35 +++++++++------------------------
 backend/src/llvm/llvm_to_gen.cpp        |  2 +-
 8 files changed, 26 insertions(+), 29 deletions(-)

diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp
index eb1c199..2e08248 100644
--- a/backend/src/ir/printf.cpp
+++ b/backend/src/ir/printf.cpp
@@ -23,6 +23,7 @@
 
 #include <stdarg.h>
 #include "printf.hpp"
+#include "ir/unit.hpp"
 
 namespace gbe
 {
diff --git a/backend/src/ir/printf.hpp b/backend/src/ir/printf.hpp
index df58437..85153a5 100644
--- a/backend/src/ir/printf.hpp
+++ b/backend/src/ir/printf.hpp
@@ -26,12 +26,12 @@
 #include <string.h>
 #include "sys/map.hpp"
 #include "sys/vector.hpp"
-#include "unit.hpp"
 
 namespace gbe
 {
   namespace ir
   {
+    class Unit;
 
     /* Things about printf info. */
     enum {
diff --git a/backend/src/ir/unit.cpp b/backend/src/ir/unit.cpp
index 84208e5..33d91c0 100644
--- a/backend/src/ir/unit.cpp
+++ b/backend/src/ir/unit.cpp
@@ -30,6 +30,7 @@ namespace ir {
   Unit::Unit(PointerSize pointerSize) : pointerSize(pointerSize), valid(true) {}
   Unit::~Unit(void) {
     for (const auto &pair : functions) GBE_DELETE(pair.second);
+    for (const auto &pair : printfs) GBE_DELETE(pair.second);
   }
   Function *Unit::getFunction(const std::string &name) const {
     auto it = functions.find(name);
diff --git a/backend/src/ir/unit.hpp b/backend/src/ir/unit.hpp
index 8ff858d..592e3a8 100644
--- a/backend/src/ir/unit.hpp
+++ b/backend/src/ir/unit.hpp
@@ -26,13 +26,17 @@
 
 #include "ir/constant.hpp"
 #include "ir/register.hpp"
+#include "ir/printf.hpp"
 #include "sys/map.hpp"
 
+#include "llvm/IR/Instructions.h"
+
 namespace gbe {
 namespace ir {
 
   // A unit contains a set of functions
   class Function;
+  class PrintfSet;
 
   /*! Complete unit of compilation. It contains a set of functions and a set of
    *  constant the functions may refer to.
@@ -41,6 +45,8 @@ namespace ir {
   {
   public:
     typedef map<std::string, Function*> FunctionSet;
+    /*! Moved from printf pass */
+    map<llvm::CallInst*, PrintfSet::PrintfFmt*> printfs;
     /*! Create an empty unit */
     Unit(PointerSize pointerSize = POINTER_32_BITS);
     /*! Release everything (*including* the function pointers) */
diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp
index 858bd49..d17e7d3 100644
--- a/backend/src/llvm/llvm_gen_backend.cpp
+++ b/backend/src/llvm/llvm_gen_backend.cpp
@@ -672,6 +672,12 @@ namespace gbe
     // handle load of dword/qword with unaligned address
     void emitUnalignedDQLoadStore(ir::Register ptr, Value *llvmValues, ir::AddressSpace addrSpace, ir::Register bti, bool isLoad, bool dwAligned, bool fixedBTI);
     void visitInstruction(Instruction &I) {NOT_SUPPORTED;}
+    void* getPrintfInfo(CallInst* inst)
+    {
+      if (unit.printfs[inst])
+        return (void*)unit.printfs[inst];
+      return NULL;
+    }
     private:
       ir::ImmediateIndex processConstantImmIndexImpl(Constant *CPV, int32_t index = 0u);
       template <typename T, typename P = T>
diff --git a/backend/src/llvm/llvm_gen_backend.hpp b/backend/src/llvm/llvm_gen_backend.hpp
index 94a377b..800f78f 100644
--- a/backend/src/llvm/llvm_gen_backend.hpp
+++ b/backend/src/llvm/llvm_gen_backend.hpp
@@ -140,7 +140,7 @@ namespace gbe
   llvm::BasicBlockPass *createIntrinsicLoweringPass();
 
   /*! Passer the printf function call. */
-  llvm::FunctionPass* createPrintfParserPass();
+  llvm::FunctionPass* createPrintfParserPass(ir::Unit &unit);
 
 #if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 5
   /* customized loop unrolling pass. */
diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp
index e2adcd8..422f16b 100644
--- a/backend/src/llvm/llvm_printf_parser.cpp
+++ b/backend/src/llvm/llvm_printf_parser.cpp
@@ -38,6 +38,7 @@
 #include "llvm/llvm_gen_backend.hpp"
 #include "sys/map.hpp"
 #include "ir/printf.hpp"
+#include "ir/unit.hpp"
 
 using namespace llvm;
 
@@ -301,7 +302,7 @@ error:
     Value* g1Xg2Xg3;
     Value* wg_offset;
     int out_buf_sizeof_offset;
-    static map<CallInst*, PrintfSet::PrintfFmt*> printfs;
+    ir::Unit &unit;
     int printf_num;
     int totalSizeofSize;
 
@@ -310,13 +311,13 @@ error:
       PrintfSet::PrintfFmt* printf_fmt;
     };
 
-    PrintfParser(void) : FunctionPass(ID)
+    PrintfParser(ir::Unit &unit) : FunctionPass(ID),
+    unit(unit)
     {
       module = NULL;
       builder = NULL;
       intTy = NULL;
       out_buf_sizeof_offset = 0;
-      printfs.clear();
       pbuf_ptr = NULL;
       index_buf_ptr = NULL;
       g1Xg2Xg3 = NULL;
@@ -325,15 +326,6 @@ error:
       totalSizeofSize = 0;
     }
 
-    ~PrintfParser(void)
-    {
-      for (auto &s : printfs) {
-        delete s.second;
-        s.second = NULL;
-      }
-      printfs.clear();
-    }
-
     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);
@@ -428,9 +420,9 @@ error:
     CallInst* printf_inst = builder->CreateCall(cast<llvm::Function>(module->getOrInsertFunction(
                               "__gen_ocl_printf", Type::getVoidTy(module->getContext()),
                               NULL)));
-    assert(printfs[printf_inst] == NULL);
-    printfs[printf_inst] = pInfo.printf_fmt;
-    printfs[printf_inst]->second = printf_num;
+    assert(unit.printfs[printf_inst] == NULL);
+    unit.printfs[printf_inst] = pInfo.printf_fmt;
+    unit.printfs[printf_inst]->second = printf_num;
     printf_num++;
     return true;
   }
@@ -972,18 +964,9 @@ error:
     return false;
   }
 
-  map<CallInst*, PrintfSet::PrintfFmt*> PrintfParser::printfs;
-
-  void* getPrintfInfo(CallInst* inst)
-  {
-    if (PrintfParser::printfs[inst])
-      return (void*)PrintfParser::printfs[inst];
-    return NULL;
-  }
-
-  FunctionPass* createPrintfParserPass()
+  FunctionPass* createPrintfParserPass(ir::Unit &unit)
   {
-    return new PrintfParser();
+    return new PrintfParser(unit);
   }
   char PrintfParser::ID = 0;
 
diff --git a/backend/src/llvm/llvm_to_gen.cpp b/backend/src/llvm/llvm_to_gen.cpp
index 24d4be7..2250f06 100644
--- a/backend/src/llvm/llvm_to_gen.cpp
+++ b/backend/src/llvm/llvm_to_gen.cpp
@@ -274,7 +274,7 @@ namespace gbe
     passes.add(createPromoteMemoryToRegisterPass());
     if(optLevel > 0)
       passes.add(createGVNPass());                 // Remove redundancies
-    passes.add(createPrintfParserPass());
+    passes.add(createPrintfParserPass(unit));
     passes.add(createExpandConstantExprPass());    // expand ConstantExpr
     passes.add(createScalarizePass());             // Expand all vector ops
     passes.add(createExpandLargeIntegersPass());   // legalize large integer operation
-- 
2.1.4



More information about the Beignet mailing list