diff options
author | Pan Xiuli <xiuli.pan@intel.com> | 2015-11-20 15:07:42 +0800 |
---|---|---|
committer | Yang Rong <rong.r.yang@intel.com> | 2015-11-25 11:45:18 +0800 |
commit | 3bda30a2d5705fea64568bd4f39367754364bc8d (patch) | |
tree | a50573feb9ac68ef1c90128a264b529007cf5735 | |
parent | 06b0298cb481f936f84e9e9af8fae0763574d3fd (diff) |
Backend: Refine printfs into ir unit
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.
V2:
Rebase the patch
Signed-off-by: Pan Xiuli <xiuli.pan@intel.com>
Reviewed-by: Yang Rong <rong.r.yang@intel.com>
-rw-r--r-- | backend/src/ir/printf.cpp | 1 | ||||
-rw-r--r-- | backend/src/ir/printf.hpp | 2 | ||||
-rw-r--r-- | backend/src/ir/unit.cpp | 1 | ||||
-rw-r--r-- | backend/src/ir/unit.hpp | 5 | ||||
-rw-r--r-- | backend/src/llvm/llvm_gen_backend.cpp | 6 | ||||
-rw-r--r-- | backend/src/llvm/llvm_gen_backend.hpp | 2 | ||||
-rw-r--r-- | backend/src/llvm/llvm_printf_parser.cpp | 35 | ||||
-rw-r--r-- | backend/src/llvm/llvm_to_gen.cpp | 2 |
8 files changed, 25 insertions, 29 deletions
diff --git a/backend/src/ir/printf.cpp b/backend/src/ir/printf.cpp index eb1c1990..2e082484 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 df58437b..85153a5f 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 56042446..a350c602 100644 --- a/backend/src/ir/unit.cpp +++ b/backend/src/ir/unit.cpp @@ -34,6 +34,7 @@ namespace ir { Unit::~Unit(void) { for (const auto &pair : functions) GBE_DELETE(pair.second); delete profilingInfo; + 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 41dc1aee..10a1af69 100644 --- a/backend/src/ir/unit.hpp +++ b/backend/src/ir/unit.hpp @@ -27,8 +27,11 @@ #include "ir/constant.hpp" #include "ir/register.hpp" #include "ir/profiling.hpp" +#include "ir/printf.hpp" #include "sys/map.hpp" +#include "llvm/IR/Instructions.h" + namespace gbe { namespace ir { @@ -43,6 +46,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 a0b22626..33ab8711 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 cf601d37..23688f51 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); /*! Insert the time stamp for profiling. */ llvm::FunctionPass* createProfilingInserterPass(int profilingType, ir::Unit &unit); diff --git a/backend/src/llvm/llvm_printf_parser.cpp b/backend/src/llvm/llvm_printf_parser.cpp index e2adcd87..422f16bc 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 732278ef..b8ab1ddd 100644 --- a/backend/src/llvm/llvm_to_gen.cpp +++ b/backend/src/llvm/llvm_to_gen.cpp @@ -275,7 +275,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 |