53f11d66db1932e195904776c193d9aef51efa7b
1 /****************************************************************************
2 * Copyright (C) 2014-2015 Intel Corporation. All Rights Reserved.
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:
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
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
25 * @brief Includes all the builder related functionality
29 ******************************************************************************/
31 #include "jit_pch.hpp"
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)
43 mVWidth
= pJitMgr
->mVWidth
;
46 mpIRBuilder
= &pJitMgr
->mBuilder
;
48 // Built in types: scalar
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);
65 mSimd4FP64Ty
= VectorType::get(mDoubleTy
, 4);
67 // Built in types: target simd
68 SetTargetWidth(pJitMgr
->mVWidth
);
70 // Built in types: simd16
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);
81 mSimd32Int8Ty
= VectorType::get(mInt8Ty
, 32);
83 if (sizeof(uint32_t*) == 4)
86 mSimdIntPtrTy
= mSimdInt32Ty
;
87 mSimd16IntPtrTy
= mSimd16Int32Ty
;
91 SWR_ASSERT(sizeof(uint32_t*) == 8);
94 mSimdIntPtrTy
= mSimdInt64Ty
;
95 mSimd16IntPtrTy
= mSimd16Int64Ty
;
99 void Builder::SetTargetWidth(uint32_t width
)
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);
114 /// @brief Mark this alloca as temporary to avoid hoisting later on
115 void Builder::SetTempAlloca(Value
* inst
)
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
);
123 bool Builder::IsTempAlloca(Value
* inst
)
125 AllocaInst
* pAlloca
= dyn_cast
<AllocaInst
>(inst
);
126 SWR_ASSERT(pAlloca
, "Unexpected non-alloca instruction");
128 return (pAlloca
->getMetadata("is_temp_alloca") != nullptr);
131 // Returns true if able to find a call instruction to mark
132 bool Builder::SetNamedMetaDataOnCallInstr(Instruction
* inst
, StringRef mdName
)
134 CallInst
* pCallInstr
= dyn_cast
<CallInst
>(inst
);
137 MDNode
* N
= MDNode::get(JM()->mContext
, MDString::get(JM()->mContext
, mdName
));
138 pCallInstr
->setMetadata(mdName
, N
);
143 // Follow use def chain back up
144 for (Use
& u
: inst
->operands())
146 Instruction
* srcInst
= dyn_cast
<Instruction
>(u
.get());
149 if (SetNamedMetaDataOnCallInstr(srcInst
, mdName
))
160 bool Builder::HasNamedMetaDataOnCallInstr(Instruction
* inst
, StringRef mdName
)
162 CallInst
* pCallInstr
= dyn_cast
<CallInst
>(inst
);
169 return (pCallInstr
->getMetadata(mdName
) != nullptr);
172 //////////////////////////////////////////////////////////////////////////
173 /// @brief Packetizes the type. Assumes SOA conversion.
174 Type
* Builder::GetVectorType(Type
* pType
)
176 if (pType
->isVectorTy())
181 // [N x float] should packetize to [N x <8 x float>]
182 if (pType
->isArrayTy())
184 uint32_t arraySize
= pType
->getArrayNumElements();
185 Type
* pArrayType
= pType
->getArrayElementType();
186 Type
* pVecArrayType
= GetVectorType(pArrayType
);
187 Type
* pVecType
= ArrayType::get(pVecArrayType
, arraySize
);
191 // {float,int} should packetize to {<8 x float>, <8 x int>}
192 if (pType
->isAggregateType())
194 uint32_t numElems
= pType
->getStructNumElements();
195 SmallVector
<Type
*, 8> vecTypes
;
196 for (uint32_t i
= 0; i
< numElems
; ++i
)
198 Type
* pElemType
= pType
->getStructElementType(i
);
199 Type
* pVecElemType
= GetVectorType(pElemType
);
200 vecTypes
.push_back(pVecElemType
);
202 Type
* pVecType
= StructType::get(JM()->mContext
, vecTypes
);
206 // [N x float]* should packetize to [N x <8 x float>]*
207 if (pType
->isPointerTy() && pType
->getPointerElementType()->isArrayTy())
209 return PointerType::get(GetVectorType(pType
->getPointerElementType()),
210 pType
->getPointerAddressSpace());
213 // <ty> should packetize to <8 x <ty>>
214 Type
* vecType
= VectorType::get(pType
, JM()->mVWidth
);
217 } // namespace SwrJit