Path: blob/21.2-virgl/src/gallium/drivers/swr/rasterizer/jitter/builder.cpp
4574 views
/****************************************************************************1* Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* @file builder.h23*24* @brief Includes all the builder related functionality25*26* Notes:27*28******************************************************************************/2930#include "jit_pch.hpp"31#include "builder.h"3233namespace SwrJit34{35using namespace llvm;3637//////////////////////////////////////////////////////////////////////////38/// @brief Contructor for Builder.39/// @param pJitMgr - JitManager which contains modules, function passes, etc.40Builder::Builder(JitManager* pJitMgr) : mpJitMgr(pJitMgr), mpPrivateContext(nullptr)41{42mVWidth = pJitMgr->mVWidth;43mVWidth16 = 16;4445mpIRBuilder = &pJitMgr->mBuilder;4647// Built in types: scalar4849mVoidTy = Type::getVoidTy(pJitMgr->mContext);50mFP16Ty = Type::getHalfTy(pJitMgr->mContext);51mFP32Ty = Type::getFloatTy(pJitMgr->mContext);52mFP32PtrTy = PointerType::get(mFP32Ty, 0);53mDoubleTy = Type::getDoubleTy(pJitMgr->mContext);54mInt1Ty = Type::getInt1Ty(pJitMgr->mContext);55mInt8Ty = Type::getInt8Ty(pJitMgr->mContext);56mInt16Ty = Type::getInt16Ty(pJitMgr->mContext);57mInt32Ty = Type::getInt32Ty(pJitMgr->mContext);58mInt64Ty = Type::getInt64Ty(pJitMgr->mContext);59mInt8PtrTy = PointerType::get(mInt8Ty, 0);60mInt16PtrTy = PointerType::get(mInt16Ty, 0);61mInt32PtrTy = PointerType::get(mInt32Ty, 0);62mInt64PtrTy = PointerType::get(mInt64Ty, 0);63mHandleTy = mInt8PtrTy;6465mSimd4FP64Ty = getVectorType(mDoubleTy, 4);6667// Built in types: target simd68SetTargetWidth(pJitMgr->mVWidth);6970// Built in types: simd167172mSimd16Int1Ty = getVectorType(mInt1Ty, mVWidth16);73mSimd16Int16Ty = getVectorType(mInt16Ty, mVWidth16);74mSimd16Int32Ty = getVectorType(mInt32Ty, mVWidth16);75mSimd16Int64Ty = getVectorType(mInt64Ty, mVWidth16);76mSimd16FP16Ty = getVectorType(mFP16Ty, mVWidth16);77mSimd16FP32Ty = getVectorType(mFP32Ty, mVWidth16);78mSimd16VectorTy = ArrayType::get(mSimd16FP32Ty, 4);79mSimd16VectorTRTy = ArrayType::get(mSimd16FP32Ty, 5);8081mSimd32Int8Ty = getVectorType(mInt8Ty, 32);8283if (sizeof(uint32_t*) == 4)84{85mIntPtrTy = mInt32Ty;86mSimdIntPtrTy = mSimdInt32Ty;87mSimd16IntPtrTy = mSimd16Int32Ty;88}89else90{91SWR_ASSERT(sizeof(uint32_t*) == 8);9293mIntPtrTy = mInt64Ty;94mSimdIntPtrTy = mSimdInt64Ty;95mSimd16IntPtrTy = mSimd16Int64Ty;96}97}9899void Builder::SetTargetWidth(uint32_t width)100{101mVWidth = width;102103mSimdInt1Ty = getVectorType(mInt1Ty, mVWidth);104mSimdInt16Ty = getVectorType(mInt16Ty, mVWidth);105mSimdInt32Ty = getVectorType(mInt32Ty, mVWidth);106mSimdInt64Ty = getVectorType(mInt64Ty, mVWidth);107mSimdFP16Ty = getVectorType(mFP16Ty, mVWidth);108mSimdFP32Ty = getVectorType(mFP32Ty, mVWidth);109mSimdVectorTy = ArrayType::get(mSimdFP32Ty, 4);110mSimdVectorIntTy = ArrayType::get(mSimdInt32Ty, 4);111mSimdVectorTRTy = ArrayType::get(mSimdFP32Ty, 5);112mSimdVectorTRIntTy = ArrayType::get(mSimdInt32Ty, 5);113}114115/// @brief Mark this alloca as temporary to avoid hoisting later on116void Builder::SetTempAlloca(Value* inst)117{118AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst);119SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction");120MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, "is_temp_alloca"));121pAlloca->setMetadata("is_temp_alloca", N);122}123124bool Builder::IsTempAlloca(Value* inst)125{126AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst);127SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction");128129return (pAlloca->getMetadata("is_temp_alloca") != nullptr);130}131132// Returns true if able to find a call instruction to mark133bool Builder::SetNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName)134{135CallInst* pCallInstr = dyn_cast<CallInst>(inst);136if (pCallInstr)137{138MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, mdName));139pCallInstr->setMetadata(mdName, N);140return true;141}142else143{144// Follow use def chain back up145for (Use& u : inst->operands())146{147Instruction* srcInst = dyn_cast<Instruction>(u.get());148if (srcInst)149{150if (SetNamedMetaDataOnCallInstr(srcInst, mdName))151{152return true;153}154}155}156}157158return false;159}160161bool Builder::HasNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName)162{163CallInst* pCallInstr = dyn_cast<CallInst>(inst);164165if (!pCallInstr)166{167return false;168}169170return (pCallInstr->getMetadata(mdName) != nullptr);171}172173//////////////////////////////////////////////////////////////////////////174/// @brief Packetizes the type. Assumes SOA conversion.175Type* Builder::GetVectorType(Type* pType)176{177if (pType->isVectorTy())178{179return pType;180}181182// [N x float] should packetize to [N x <8 x float>]183if (pType->isArrayTy())184{185uint32_t arraySize = pType->getArrayNumElements();186Type* pArrayType = pType->getArrayElementType();187Type* pVecArrayType = GetVectorType(pArrayType);188Type* pVecType = ArrayType::get(pVecArrayType, arraySize);189return pVecType;190}191192// {float,int} should packetize to {<8 x float>, <8 x int>}193if (pType->isAggregateType())194{195uint32_t numElems = pType->getStructNumElements();196SmallVector<Type*, 8> vecTypes;197for (uint32_t i = 0; i < numElems; ++i)198{199Type* pElemType = pType->getStructElementType(i);200Type* pVecElemType = GetVectorType(pElemType);201vecTypes.push_back(pVecElemType);202}203Type* pVecType = StructType::get(JM()->mContext, vecTypes);204return pVecType;205}206207// [N x float]* should packetize to [N x <8 x float>]*208if (pType->isPointerTy() && pType->getPointerElementType()->isArrayTy())209{210return PointerType::get(GetVectorType(pType->getPointerElementType()),211pType->getPointerAddressSpace());212}213214// <ty> should packetize to <8 x <ty>>215Type* vecType = getVectorType(pType, JM()->mVWidth);216return vecType;217}218} // namespace SwrJit219220221