From 7bbb487ec00e1d2db6186bf77bc0d06d2b0ff3db Mon Sep 17 00:00:00 2001 From: "Yang, Rong R" Date: Mon, 28 Nov 2016 19:32:36 +0800 Subject: OCL20: enable -cl-std=CL2.0. When build from source, get the OpenCL version from the option. Use spir64 triple if it is OpenCL 2.0. Get the OpenCL version for llvm module's meta. If OpenCL version is 2.0, set the unit's point size to 64 bits before using unit.getPointerSize(). Signed-off-by: Yang Rong Reviewed-by: Ruiling Song --- backend/src/llvm/llvm_bitcode_link.cpp | 19 ++++++++++++++----- backend/src/llvm/llvm_gen_backend.cpp | 3 +++ backend/src/llvm/llvm_gen_backend.hpp | 4 +++- backend/src/llvm/llvm_passes.cpp | 25 +++++++++++++++++++++++-- backend/src/llvm/llvm_to_gen.cpp | 5 +++-- 5 files changed, 46 insertions(+), 10 deletions(-) (limited to 'backend/src/llvm') diff --git a/backend/src/llvm/llvm_bitcode_link.cpp b/backend/src/llvm/llvm_bitcode_link.cpp index 0b01929f..87cefb51 100644 --- a/backend/src/llvm/llvm_bitcode_link.cpp +++ b/backend/src/llvm/llvm_bitcode_link.cpp @@ -26,18 +26,23 @@ #include "src/GBEConfig.h" #include "llvm_includes.hpp" #include "llvm/llvm_gen_backend.hpp" +#include "ir/unit.hpp" using namespace llvm; SVAR(OCL_BITCODE_LIB_PATH, OCL_BITCODE_BIN); +SVAR(OCL_BITCODE_LIB_20_PATH, OCL_BITCODE_BIN_20); namespace gbe { - static Module* createOclBitCodeModule(LLVMContext& ctx, bool strictMath) + static Module* createOclBitCodeModule(LLVMContext& ctx, + bool strictMath, + uint32_t oclVersion) { - std::string bitCodeFiles = OCL_BITCODE_LIB_PATH; + std::string bitCodeFiles = oclVersion >= 200 ? + OCL_BITCODE_LIB_20_PATH : OCL_BITCODE_LIB_PATH; if(bitCodeFiles == "") - bitCodeFiles = OCL_BITCODE_BIN; + bitCodeFiles = oclVersion >= 200 ? OCL_BITCODE_BIN_20 : OCL_BITCODE_BIN; std::istringstream bitCodeFilePath(bitCodeFiles); std::string FilePath; bool findBC = false; @@ -135,12 +140,16 @@ namespace gbe } - Module* runBitCodeLinker(Module *mod, bool strictMath) + Module* runBitCodeLinker(Module *mod, bool strictMath, ir::Unit &unit) { LLVMContext& ctx = mod->getContext(); std::set materializedFuncs; std::vector Gvs; - Module* clonedLib = createOclBitCodeModule(ctx, strictMath); + + uint32_t oclVersion = getModuleOclVersion(mod); + ir::PointerSize size = oclVersion >= 200 ? ir::POINTER_64_BITS : ir::POINTER_32_BITS; + unit.setPointerSize(size); + Module* clonedLib = createOclBitCodeModule(ctx, strictMath, oclVersion); if (clonedLib == NULL) return NULL; diff --git a/backend/src/llvm/llvm_gen_backend.cpp b/backend/src/llvm/llvm_gen_backend.cpp index d7bdcf04..8c7a230c 100644 --- a/backend/src/llvm/llvm_gen_backend.cpp +++ b/backend/src/llvm/llvm_gen_backend.cpp @@ -1697,6 +1697,9 @@ namespace gbe // Initialize TheModule = &M; + uint32_t oclVersion = getModuleOclVersion(TheModule); + legacyMode = oclVersion >= 200 ? false : true; + unit.setOclVersion(oclVersion); collectGlobalConstant(); return false; } diff --git a/backend/src/llvm/llvm_gen_backend.hpp b/backend/src/llvm/llvm_gen_backend.hpp index f2a278e6..1fecc739 100644 --- a/backend/src/llvm/llvm_gen_backend.hpp +++ b/backend/src/llvm/llvm_gen_backend.hpp @@ -153,7 +153,9 @@ namespace gbe llvm::FunctionPass* createSamplerFixPass(); /*! Add all the function call of ocl to our bitcode. */ - llvm::Module* runBitCodeLinker(llvm::Module *mod, bool strictMath); + llvm::Module* runBitCodeLinker(llvm::Module *mod, bool strictMath, ir::Unit &unit); + + uint32_t getModuleOclVersion(const llvm::Module *M); void* getPrintfInfo(llvm::CallInst* inst); } /* namespace gbe */ diff --git a/backend/src/llvm/llvm_passes.cpp b/backend/src/llvm/llvm_passes.cpp index 02dd4bfb..f5d90521 100644 --- a/backend/src/llvm/llvm_passes.cpp +++ b/backend/src/llvm/llvm_passes.cpp @@ -65,6 +65,27 @@ namespace gbe return bKernel; } + uint32_t getModuleOclVersion(const llvm::Module *M) { + uint32_t oclVersion = 120; + NamedMDNode *version = M->getNamedMetadata("opencl.ocl.version"); + if (version == NULL) + return oclVersion; + uint32_t ops = version->getNumOperands(); + if(ops > 0) { + uint32_t major = 0, minor = 0; + MDNode* node = version->getOperand(0); +#if LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 6 + major = mdconst::extract(node->getOperand(0))->getZExtValue(); + minor = mdconst::extract(node->getOperand(1))->getZExtValue(); +#else + major = cast(MD->getOperand(0))->getZExtValue(); + minor = cast(MD->getOperand(1))->getZExtValue(); +#endif + oclVersion = major * 100 + minor * 10; + } + return oclVersion; + } + int32_t getPadding(int32_t offset, int32_t align) { return (align - (offset % align)) % align; } @@ -262,7 +283,7 @@ namespace gbe if(!operand) continue; - +#if 0 //HACK TODO: Inserted by type replacement.. this code could break something???? if(getTypeByteSize(unit, operand->getType())>4) { @@ -286,7 +307,7 @@ namespace gbe "", GEPInst); } } - +#endif Value* tmpMul = operand; if (size != 1) { tmpMul = BinaryOperator::Create(Instruction::Mul, newConstSize, operand, diff --git a/backend/src/llvm/llvm_to_gen.cpp b/backend/src/llvm/llvm_to_gen.cpp index a889c56a..b58dfb72 100644 --- a/backend/src/llvm/llvm_to_gen.cpp +++ b/backend/src/llvm/llvm_to_gen.cpp @@ -321,8 +321,9 @@ namespace gbe std::unique_ptr M; - /* Before do any thing, we first filter in all CL functions in bitcode. */ - M.reset(runBitCodeLinker(cl_mod, strictMath)); + /* Before do any thing, we first filter in all CL functions in bitcode. */ + /* Also set unit's pointer size in runBitCodeLinker */ + M.reset(runBitCodeLinker(cl_mod, strictMath, unit)); if (!module) delete cl_mod; if (M.get() == 0) -- cgit v1.2.1