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 #pragma once
31 
32 #include "JitManager.h"
33 #include "common/formats.h"
34 
35 namespace SwrJit
36 {
37     ///@todo Move this to better place
38     enum SHADER_STATS_COUNTER_TYPE
39     {
40         STATS_INST_EXECUTED           = 0,
41         STATS_SAMPLE_EXECUTED         = 1,
42         STATS_SAMPLE_L_EXECUTED       = 2,
43         STATS_SAMPLE_B_EXECUTED       = 3,
44         STATS_SAMPLE_C_EXECUTED       = 4,
45         STATS_SAMPLE_C_LZ_EXECUTED    = 5,
46         STATS_SAMPLE_C_D_EXECUTED     = 6,
47         STATS_LOD_EXECUTED            = 7,
48         STATS_GATHER4_EXECUTED        = 8,
49         STATS_GATHER4_C_EXECUTED      = 9,
50         STATS_GATHER4_C_PO_EXECUTED   = 10,
51         STATS_GATHER4_C_PO_C_EXECUTED = 11,
52         STATS_LOAD_RAW_UAV            = 12,
53         STATS_LOAD_RAW_RESOURCE       = 13,
54         STATS_STORE_RAW_UAV           = 14,
55         STATS_STORE_TGSM              = 15,
56         STATS_DISCARD                 = 16,
57         STATS_BARRIER                 = 17,
58 
59         // ------------------
60         STATS_TOTAL_COUNTERS
61     };
62 
63     using namespace llvm;
64     struct Builder
65     {
66         Builder(JitManager* pJitMgr);
~BuilderBuilder67         virtual ~Builder() {}
68 
IRBBuilder69         IRBuilder<>* IRB() { return mpIRBuilder; };
JMBuilder70         JitManager*  JM() { return mpJitMgr; }
71 
72         JitManager*  mpJitMgr;
73         IRBuilder<>* mpIRBuilder;
74 
75         uint32_t mVWidth;   // vector width target simd
76         uint32_t mVWidth16; // vector width simd16
77 
78         // Built in types: scalar
79 
80         Type* mVoidTy;
81         Type* mHandleTy;
82         Type* mInt1Ty;
83         Type* mInt8Ty;
84         Type* mInt16Ty;
85         Type* mInt32Ty;
86         Type* mInt64Ty;
87         Type* mIntPtrTy;
88         Type* mFP16Ty;
89         Type* mFP32Ty;
90         Type* mFP32PtrTy;
91         Type* mDoubleTy;
92         Type* mInt8PtrTy;
93         Type* mInt16PtrTy;
94         Type* mInt32PtrTy;
95         Type* mInt64PtrTy;
96 
97         Type* mSimd4FP64Ty;
98 
99         // Built in types: target SIMD
100 
101         Type* mSimdFP16Ty;
102         Type* mSimdFP32Ty;
103         Type* mSimdInt1Ty;
104         Type* mSimdInt16Ty;
105         Type* mSimdInt32Ty;
106         Type* mSimdInt64Ty;
107         Type* mSimdIntPtrTy;
108         Type* mSimdVectorTy;
109         Type* mSimdVectorTRTy;
110         Type* mSimdVectorIntTy;
111         Type* mSimdVectorTRIntTy;
112 
113         // Built in types: simd16
114 
115         Type* mSimd16FP16Ty;
116         Type* mSimd16FP32Ty;
117         Type* mSimd16Int1Ty;
118         Type* mSimd16Int16Ty;
119         Type* mSimd16Int32Ty;
120         Type* mSimd16Int64Ty;
121         Type* mSimd16IntPtrTy;
122         Type* mSimd16VectorTy;
123         Type* mSimd16VectorTRTy;
124 
125         Type* mSimd32Int8Ty;
126 
127         void  SetTargetWidth(uint32_t width);
128         void  SetTempAlloca(Value* inst);
129         bool  IsTempAlloca(Value* inst);
130         bool  SetNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName);
131         bool  HasNamedMetaDataOnCallInstr(Instruction* inst, StringRef mdName);
132         Type* GetVectorType(Type* pType);
SetMetadataBuilder133         void  SetMetadata(StringRef s, uint32_t val)
134         {
135             llvm::NamedMDNode* metaData = mpJitMgr->mpCurrentModule->getOrInsertNamedMetadata(s);
136             Constant*          cval     = mpIRBuilder->getInt32(val);
137             llvm::MDNode*      mdNode   = llvm::MDNode::get(mpJitMgr->mpCurrentModule->getContext(),
138                                                      llvm::ConstantAsMetadata::get(cval));
139             if (metaData->getNumOperands())
140             {
141                 metaData->setOperand(0, mdNode);
142             }
143             else
144             {
145                 metaData->addOperand(mdNode);
146             }
147         }
GetMetadataBuilder148         uint32_t GetMetadata(StringRef s)
149         {
150             NamedMDNode* metaData = mpJitMgr->mpCurrentModule->getNamedMetadata(s);
151             if (metaData)
152             {
153                 MDNode*   mdNode = metaData->getOperand(0);
154                 Metadata* val    = mdNode->getOperand(0);
155                 return mdconst::dyn_extract<ConstantInt>(val)->getZExtValue();
156             }
157             else
158             {
159                 return 0;
160             }
161         }
162 
163 #include "gen_builder.hpp"
164 #include "gen_builder_meta.hpp"
165 #include "gen_builder_intrin.hpp"
166 #include "builder_misc.h"
167 #include "builder_math.h"
168 #include "builder_mem.h"
169 
SetPrivateContextBuilder170         void SetPrivateContext(Value* pPrivateContext)
171         {
172             mpPrivateContext = pPrivateContext;
173             NotifyPrivateContextSet();
174         }
NotifyPrivateContextSetBuilder175         virtual void  NotifyPrivateContextSet() {}
GetPrivateContextBuilder176         inline Value* GetPrivateContext() { return mpPrivateContext; }
177 
178     private:
179         Value* mpPrivateContext;
180     };
181 } // namespace SwrJit
182