53f11d66db1932e195904776c193d9aef51efa7b
[mesa.git] / src / gallium / drivers / swr / rasterizer / jitter / builder.cpp
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
34 namespace 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 mInt64Ty = Type::getInt64Ty(pJitMgr->mContext);
60 mInt8PtrTy = PointerType::get(mInt8Ty, 0);
61 mInt16PtrTy = PointerType::get(mInt16Ty, 0);
62 mInt32PtrTy = PointerType::get(mInt32Ty, 0);
63 mInt64PtrTy = PointerType::get(mInt64Ty, 0);
64
65 mSimd4FP64Ty = VectorType::get(mDoubleTy, 4);
66
67 // Built in types: target simd
68 SetTargetWidth(pJitMgr->mVWidth);
69
70 // Built in types: simd16
71
72 mSimd16Int1Ty = VectorType::get(mInt1Ty, mVWidth16);
73 mSimd16Int16Ty = VectorType::get(mInt16Ty, mVWidth16);
74 mSimd16Int32Ty = VectorType::get(mInt32Ty, mVWidth16);
75 mSimd16Int64Ty = VectorType::get(mInt64Ty, mVWidth16);
76 mSimd16FP16Ty = VectorType::get(mFP16Ty, mVWidth16);
77 mSimd16FP32Ty = VectorType::get(mFP32Ty, mVWidth16);
78 mSimd16VectorTy = ArrayType::get(mSimd16FP32Ty, 4);
79 mSimd16VectorTRTy = ArrayType::get(mSimd16FP32Ty, 5);
80
81 mSimd32Int8Ty = VectorType::get(mInt8Ty, 32);
82
83 if (sizeof(uint32_t*) == 4)
84 {
85 mIntPtrTy = mInt32Ty;
86 mSimdIntPtrTy = mSimdInt32Ty;
87 mSimd16IntPtrTy = mSimd16Int32Ty;
88 }
89 else
90 {
91 SWR_ASSERT(sizeof(uint32_t*) == 8);
92
93 mIntPtrTy = mInt64Ty;
94 mSimdIntPtrTy = mSimdInt64Ty;
95 mSimd16IntPtrTy = mSimd16Int64Ty;
96 }
97 }
98
99 void Builder::SetTargetWidth(uint32_t width)
100 {
101 mVWidth = width;
102
103 mSimdInt1Ty = VectorType::get(mInt1Ty, mVWidth);
104 mSimdInt16Ty = VectorType::get(mInt16Ty, mVWidth);
105 mSimdInt32Ty = VectorType::get(mInt32Ty, mVWidth);
106 mSimdInt64Ty = VectorType::get(mInt64Ty, mVWidth);
107 mSimdFP16Ty = VectorType::get(mFP16Ty, mVWidth);
108 mSimdFP32Ty = VectorType::get(mFP32Ty, mVWidth);
109 mSimdVectorTy = ArrayType::get(mSimdFP32Ty, 4);
110 mSimdVectorIntTy = ArrayType::get(mSimdInt32Ty, 4);
111 mSimdVectorTRTy = ArrayType::get(mSimdFP32Ty, 5);
112 }
113
114 /// @brief Mark this alloca as temporary to avoid hoisting later on
115 void Builder::SetTempAlloca(Value* inst)
116 {
117 AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst);
118 SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction");
119 MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, "is_temp_alloca"));
120 pAlloca->setMetadata("is_temp_alloca", N);
121 }
122
123 bool Builder::IsTempAlloca(Value* inst)
124 {
125 AllocaInst* pAlloca = dyn_cast<AllocaInst>(inst);
126 SWR_ASSERT(pAlloca, "Unexpected non-alloca instruction");
127
128 return (pAlloca->getMetadata("is_temp_alloca") != nullptr);
129 }
130
131 // Returns true if able to find a call instruction to mark
132 bool Builder::SetNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName)
133 {
134 CallInst* pCallInstr = dyn_cast<CallInst>(inst);
135 if (pCallInstr)
136 {
137 MDNode* N = MDNode::get(JM()->mContext, MDString::get(JM()->mContext, mdName));
138 pCallInstr->setMetadata(mdName, N);
139 return true;
140 }
141 else
142 {
143 // Follow use def chain back up
144 for (Use& u : inst->operands())
145 {
146 Instruction* srcInst = dyn_cast<Instruction>(u.get());
147 if (srcInst)
148 {
149 if (SetNamedMetaDataOnCallInstr(srcInst, mdName))
150 {
151 return true;
152 }
153 }
154 }
155 }
156
157 return false;
158 }
159
160 bool Builder::HasNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName)
161 {
162 CallInst* pCallInstr = dyn_cast<CallInst>(inst);
163
164 if (!pCallInstr)
165 {
166 return false;
167 }
168
169 return (pCallInstr->getMetadata(mdName) != nullptr);
170 }
171
172 //////////////////////////////////////////////////////////////////////////
173 /// @brief Packetizes the type. Assumes SOA conversion.
174 Type* Builder::GetVectorType(Type* pType)
175 {
176 if (pType->isVectorTy())
177 {
178 return pType;
179 }
180
181 // [N x float] should packetize to [N x <8 x float>]
182 if (pType->isArrayTy())
183 {
184 uint32_t arraySize = pType->getArrayNumElements();
185 Type* pArrayType = pType->getArrayElementType();
186 Type* pVecArrayType = GetVectorType(pArrayType);
187 Type* pVecType = ArrayType::get(pVecArrayType, arraySize);
188 return pVecType;
189 }
190
191 // {float,int} should packetize to {<8 x float>, <8 x int>}
192 if (pType->isAggregateType())
193 {
194 uint32_t numElems = pType->getStructNumElements();
195 SmallVector<Type*, 8> vecTypes;
196 for (uint32_t i = 0; i < numElems; ++i)
197 {
198 Type* pElemType = pType->getStructElementType(i);
199 Type* pVecElemType = GetVectorType(pElemType);
200 vecTypes.push_back(pVecElemType);
201 }
202 Type* pVecType = StructType::get(JM()->mContext, vecTypes);
203 return pVecType;
204 }
205
206 // [N x float]* should packetize to [N x <8 x float>]*
207 if (pType->isPointerTy() && pType->getPointerElementType()->isArrayTy())
208 {
209 return PointerType::get(GetVectorType(pType->getPointerElementType()),
210 pType->getPointerAddressSpace());
211 }
212
213 // <ty> should packetize to <8 x <ty>>
214 Type* vecType = VectorType::get(pType, JM()->mVWidth);
215 return vecType;
216 }
217 } // namespace SwrJit