1/**************************************************************************** 2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 * 23 * @file builder.h 24 * 25 * @brief Includes all the builder related functionality 26 * 27 * Notes: 28 * 29 ******************************************************************************/ 30 31#include "jit_pch.hpp" 32#include "builder.h" 33 34namespace SwrJit 35{ 36 using namespace llvm; 37 38 ////////////////////////////////////////////////////////////////////////// 39 /// @brief Contructor for Builder. 40 /// @param pJitMgr - JitManager which contains modules, function passes, etc. 41 Builder::Builder(JitManager* pJitMgr) : mpJitMgr(pJitMgr), mpPrivateContext(nullptr) 42 { 43 mVWidth = pJitMgr->mVWidth; 44 mVWidth16 = 16; 45 46 mpIRBuilder = &pJitMgr->mBuilder; 47 48 // Built in types: scalar 49 50 mVoidTy = Type::getVoidTy(pJitMgr->mContext); 51 mFP16Ty = Type::getHalfTy(pJitMgr->mContext); 52 mFP32Ty = Type::getFloatTy(pJitMgr->mContext); 53 mFP32PtrTy = PointerType::get(mFP32Ty, 0); 54 mDoubleTy = Type::getDoubleTy(pJitMgr->mContext); 55 mInt1Ty = Type::getInt1Ty(pJitMgr->mContext); 56 mInt8Ty = Type::getInt8Ty(pJitMgr->mContext); 57 mInt16Ty = Type::getInt16Ty(pJitMgr->mContext); 58 mInt32Ty = Type::getInt32Ty(pJitMgr->mContext); 59 mInt8PtrTy = PointerType::get(mInt8Ty, 0); 60 mInt16PtrTy = PointerType::get(mInt16Ty, 0); 61 mInt32PtrTy = PointerType::get(mInt32Ty, 0); 62 mInt64Ty = Type::getInt64Ty(pJitMgr->mContext); 63 64 mSimd4FP64Ty = VectorType::get(mDoubleTy, 4); 65 66 // Built in types: target simd 67 SetTargetWidth(pJitMgr->mVWidth); 68 69 // Built in types: simd16 70 71 mSimd16Int1Ty = VectorType::get(mInt1Ty, mVWidth16); 72 mSimd16Int16Ty = VectorType::get(mInt16Ty, mVWidth16); 73 mSimd16Int32Ty = VectorType::get(mInt32Ty, mVWidth16); 74 mSimd16Int64Ty = VectorType::get(mInt64Ty, mVWidth16); 75 mSimd16FP16Ty = VectorType::get(mFP16Ty, mVWidth16); 76 mSimd16FP32Ty = VectorType::get(mFP32Ty, mVWidth16); 77 mSimd16VectorTy = ArrayType::get(mSimd16FP32Ty, 4); 78 mSimd16VectorTRTy = ArrayType::get(mSimd16FP32Ty, 5); 79 80 mSimd32Int8Ty = VectorType::get(mInt8Ty, 32); 81 82 if (sizeof(uint32_t*) == 4) 83 { 84 mIntPtrTy = mInt32Ty; 85 mSimdIntPtrTy = mSimdInt32Ty; 86 mSimd16IntPtrTy = mSimd16Int32Ty; 87 } 88 else 89 { 90 SWR_ASSERT(sizeof(uint32_t*) == 8); 91 92 mIntPtrTy = mInt64Ty; 93 mSimdIntPtrTy = mSimdInt64Ty; 94 mSimd16IntPtrTy = mSimd16Int64Ty; 95 } 96 } 97 98 void Builder::SetTargetWidth(uint32_t width) 99 { 100 mVWidth = width; 101 102 mSimdInt1Ty = VectorType::get(mInt1Ty, mVWidth); 103 mSimdInt16Ty = VectorType::get(mInt16Ty, mVWidth); 104 mSimdInt32Ty = VectorType::get(mInt32Ty, mVWidth); 105 mSimdInt64Ty = VectorType::get(mInt64Ty, mVWidth); 106 mSimdFP16Ty = VectorType::get(mFP16Ty, mVWidth); 107 mSimdFP32Ty = VectorType::get(mFP32Ty, mVWidth); 108 mSimdVectorTy = ArrayType::get(mSimdFP32Ty, 4); 109 mSimdVectorIntTy = ArrayType::get(mSimdInt32Ty, 4); 110 mSimdVectorTRTy = ArrayType::get(mSimdFP32Ty, 5); 111 } 112 113 /// @brief Mark this alloca as temporary to avoid hoisting later on 114 void Builder::SetTempAlloca(Value* inst) 115 { 116 AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst); 117 SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction"); 118 MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, "is_temp_alloca")); 119 pAlloca->setMetadata("is_temp_alloca", N); 120 } 121 122 bool Builder::IsTempAlloca(Value* inst) 123 { 124 AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst); 125 SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction"); 126 127 return (pAlloca->getMetadata("is_temp_alloca") != nullptr); 128 } 129 130 // Returns true if able to find a call instruction to mark 131 bool Builder::SetNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName) 132 { 133 CallInst* pCallInstr = dyn_cast<CallInst>(inst); 134 if (pCallInstr) 135 { 136 MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, mdName)); 137 pCallInstr->setMetadata(mdName, N); 138 return true; 139 } 140 else 141 { 142 // Follow use def chain back up 143 for (Use& u : inst->operands()) 144 { 145 Instruction* srcInst = dyn_cast<Instruction>(u.get()); 146 if (srcInst) 147 { 148 if (SetNamedMetaDataOnCallInstr(srcInst, mdName)) 149 { 150 return true; 151 } 152 } 153 } 154 } 155 156 return false; 157 } 158 159 bool Builder::HasNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName) 160 { 161 CallInst* pCallInstr = dyn_cast<CallInst>(inst); 162 163 if (!pCallInstr) 164 { 165 return false; 166 } 167 168 return (pCallInstr->getMetadata(mdName) != nullptr); 169 } 170 171 ////////////////////////////////////////////////////////////////////////// 172 /// @brief Packetizes the type. Assumes SOA conversion. 173 Type* Builder::GetVectorType(Type* pType) 174 { 175 if (pType->isVectorTy()) 176 { 177 return pType; 178 } 179 180 // [N x float] should packetize to [N x <8 x float>] 181 if (pType->isArrayTy()) 182 { 183 uint32_t arraySize = pType->getArrayNumElements(); 184 Type* pArrayType = pType->getArrayElementType(); 185 Type* pVecArrayType = GetVectorType(pArrayType); 186 Type* pVecType = ArrayType::get(pVecArrayType, arraySize); 187 return pVecType; 188 } 189 190 // {float,int} should packetize to {<8 x float>, <8 x int>} 191 if (pType->isAggregateType()) 192 { 193 uint32_t numElems = pType->getStructNumElements(); 194 SmallVector<Type*, 8> vecTypes; 195 for (uint32_t i = 0; i < numElems; ++i) 196 { 197 Type* pElemType = pType->getStructElementType(i); 198 Type* pVecElemType = GetVectorType(pElemType); 199 vecTypes.push_back(pVecElemType); 200 } 201 Type* pVecType = StructType::get(JM()->mContext, vecTypes); 202 return pVecType; 203 } 204 205 // [N x float]* should packetize to [N x <8 x float>]* 206 if (pType->isPointerTy() && pType->getPointerElementType()->isArrayTy()) 207 { 208 return PointerType::get(GetVectorType(pType->getPointerElementType()), 209 pType->getPointerAddressSpace()); 210 } 211 212 // <ty> should packetize to <8 x <ty>> 213 Type* vecType = VectorType::get(pType, JM()->mVWidth); 214 return vecType; 215 } 216} // namespace SwrJit 217