Debian Codegraph @

llvm-project/llvm/projects/llvm-spirv/lib/SPIRV/SPIRVReader.h

raw
1 //===- SPIRVReader.h - Converts SPIR-V to LLVM ------------------*- C++ -*-===//
2 //
3 // The LLVM/SPIR-V Translator
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 // Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal with the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // Redistributions of source code must retain the above copyright notice,
18 // this list of conditions and the following disclaimers.
19 // Redistributions in binary form must reproduce the above copyright notice,
20 // this list of conditions and the following disclaimers in the documentation
21 // and/or other materials provided with the distribution.
22 // Neither the names of Advanced Micro Devices, Inc., nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // Software without specific prior written permission.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 // CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
31 // THE SOFTWARE.
32 //
33 //===----------------------------------------------------------------------===//
34 /// \file
35 ///
36 /// This file contains declaration of SPIRVToLLVM class which implements
37 /// conversion of SPIR-V binary to LLVM IR.
38 ///
39 //===----------------------------------------------------------------------===//
40
41 #ifndef SPIRVREADER_H
42 #define SPIRVREADER_H
43
44 #include "SPIRVBuiltinHelper.h"
45 #include "SPIRVInternal.h"
46 #include "SPIRVModule.h"
47
48 #include "llvm/ADT/DenseMap.h"
49 #include "llvm/ADT/StringSet.h"
50 #include "llvm/IR/GlobalValue.h" // llvm::GlobalValue::LinkageTypes
51
52 namespace llvm {
53 class Metadata;
54 class Module;
55 class Type;
56 class Instruction;
57 class CallInst;
58 class BasicBlock;
59 class Loop;
60 class Function;
61 class GlobalVariable;
62 class LLVMContext;
63 class MDString;
64 class IntrinsicInst;
65 class LoadInst;
66 class BranchInst;
67 class BinaryOperator;
68 class Value;
69 } // namespace llvm
70 using namespace llvm;
71
72 namespace SPIRV {
73 class SPIRVFunctionParameter;
74 class SPIRVConstantSampler;
75 class SPIRVConstantPipeStorage;
76 class SPIRVLoopMerge;
77 class SPIRVToLLVMDbgTran;
78 class SPIRVToLLVM : private BuiltinCallHelper {
79 public:
80 SPIRVToLLVM(Module *LLVMModule, SPIRVModule *TheSPIRVModule);
81
82 static const StringSet<> BuiltInConstFunc;
83
84 /// Translate the SPIR-V type into an LLVM type. If UseTypedPointerTypes is
85 /// true, then generate a TypedPointerType instead of a PointerType. The
86 /// intended use of TypedPointerTypes is for name mangling, so pointer types
87 /// that occur as array members or struct members will not be represented with
88 /// TypedPointerType, even when UseTypedPointerTypes is true.
89 Type *transType(SPIRVType *BT, bool UseTypedPointerTypes = false);
90 std::string transTypeToOCLTypeName(SPIRVType *BT, bool IsSigned = true);
91 std::vector<Type *> transTypeVector(const std::vector<SPIRVType *> &,
92 bool UseTypedPointerTypes = false);
93 bool translate();
94 bool transAddressingModel();
95
96 Value *transValue(SPIRVValue *, Function *F, BasicBlock *,
97 bool CreatePlaceHolder = true);
98 Value *transValueWithoutDecoration(SPIRVValue *, Function *F, BasicBlock *,
99 bool CreatePlaceHolder = true);
100 bool transDecoration(SPIRVValue *, Value *);
101 bool transAlign(SPIRVValue *, Value *);
102 Instruction *transOCLBuiltinFromExtInst(SPIRVExtInst *BC, BasicBlock *BB);
103 void transAuxDataInst(SPIRVExtInst *BC);
104 std::vector<Value *> transValue(const std::vector<SPIRVValue *> &,
105 Function *F, BasicBlock *);
106 Function *transFunction(SPIRVFunction *F, unsigned AS = SPIRAS_Private);
107 void transFunctionAttrs(SPIRVFunction *BF, Function *F);
108 Value *transBlockInvoke(SPIRVValue *Invoke, BasicBlock *BB);
109 Instruction *transWGSizeQueryBI(SPIRVInstruction *BI, BasicBlock *BB);
110 Instruction *transSGSizeQueryBI(SPIRVInstruction *BI, BasicBlock *BB);
111 bool transFPContractMetadata();
112 bool transMetadata();
113 bool transOCLMetadata(SPIRVFunction *BF);
114 bool transVectorComputeMetadata(SPIRVFunction *BF);
115 bool transFPGAFunctionMetadata(SPIRVFunction *BF, Function *F);
116 Value *transAsmINTEL(SPIRVAsmINTEL *BA);
117 CallInst *transAsmCallINTEL(SPIRVAsmCallINTEL *BI, Function *F,
118 BasicBlock *BB);
119 Value *transFixedPointInst(SPIRVInstruction *BI, BasicBlock *BB);
120 Value *transArbFloatInst(SPIRVInstruction *BI, BasicBlock *BB,
121 bool IsBinaryInst = false);
122 bool transNonTemporalMetadata(Instruction *I);
123 template <typename SPIRVInstType>
124 void transAliasingMemAccess(SPIRVInstType *BI, Instruction *I);
125 void addMemAliasMetadata(Instruction *I, SPIRVId AliasListId,
126 uint32_t AliasMDKind);
127 void transSourceLanguage();
128 bool transSourceExtension();
129 void transGeneratorMD();
130 Value *transConvertInst(SPIRVValue *BV, Function *F, BasicBlock *BB);
131 Instruction *transBuiltinFromInst(const std::string &FuncName,
132 SPIRVInstruction *BI, BasicBlock *BB);
133 Instruction *transSPIRVBuiltinFromInst(SPIRVInstruction *BI, BasicBlock *BB);
134
135 /// \brief Expand OCL builtin functions with scalar argument, e.g.
136 /// step, smoothstep.
137 /// gentype func (fp edge, gentype x)
138 /// =>
139 /// gentype func (gentype edge, gentype x)
140 /// \return transformed call instruction.
141 CallInst *expandOCLBuiltinWithScalarArg(CallInst *CI,
142 const std::string &FuncName);
143
144 typedef DenseMap<SPIRVType *, Type *> SPIRVToLLVMTypeMap;
145 typedef DenseMap<SPIRVValue *, Value *> SPIRVToLLVMValueMap;
146 typedef DenseMap<SPIRVValue *, Value *> SPIRVBlockToLLVMStructMap;
147 typedef DenseMap<SPIRVFunction *, Function *> SPIRVToLLVMFunctionMap;
148 typedef DenseMap<GlobalVariable *, SPIRVBuiltinVariableKind> BuiltinVarMap;
149 typedef std::unordered_map<SPIRVId, MDNode *> SPIRVToLLVMMDAliasInstMap;
150
151 // A SPIRV value may be translated to a load instruction of a placeholder
152 // global variable. This map records load instruction of these placeholders
153 // which are supposed to be replaced by the real values later.
154 typedef std::map<SPIRVValue *, LoadInst *> SPIRVToLLVMPlaceholderMap;
155
156 typedef std::map<const BasicBlock *, const SPIRVValue *>
157 SPIRVToLLVMLoopMetadataMap;
158
159 // Store all the allocations to Struct Types that are further
160 // accessed inside GetElementPtr instruction or in ptr.annotation intrinsics.
161 // For every structure we save the accessed structure field index and the
162 // last corresponding translated LLVM instruction.
163 typedef std::unordered_map<Value *,
164 std::unordered_map<SPIRVWord, Instruction *>>
165 TypeToGEPOrUseMap;
166
167 private:
168 Module *M;
169 LLVMContext *Context;
170 SPIRVModule *BM;
171 SPIRVToLLVMTypeMap TypeMap;
172 SPIRVToLLVMValueMap ValueMap;
173 SPIRVToLLVMFunctionMap FuncMap;
174 SPIRVBlockToLLVMStructMap BlockMap;
175 SPIRVToLLVMPlaceholderMap PlaceholderMap;
176 std::unique_ptr<SPIRVToLLVMDbgTran> DbgTran;
177 // GlobalAnnotations collects array of annotation entries for global variables
178 // and functions. They are used in translation of llvm.global.annotations
179 // instruction.
180 std::vector<Constant *> GlobalAnnotations;
181 // AnnotationsMap helps to translate annotation strings for local variables.
182 // Map values are pointers on global strings in LLVM-IR. It is used to avoid
183 // duplication of these annotation strings in LLVM-IR, which can be caused by
184 // multiple translation of UserSemantic decorations with the same literal.
185 std::unordered_map<std::string, Constant *> AnnotationsMap;
186
187 // Loops metadata is translated in the end of a function translation.
188 // This storage contains pairs of translated loop header basic block and loop
189 // metadata SPIR-V instruction in SPIR-V representation of this basic block.
190 SPIRVToLLVMLoopMetadataMap FuncLoopMetadataMap;
191
192 // These storages are used to prevent duplication of alias.scope/noalias
193 // metadata
194 SPIRVToLLVMMDAliasInstMap MDAliasDomainMap;
195 SPIRVToLLVMMDAliasInstMap MDAliasScopeMap;
196 SPIRVToLLVMMDAliasInstMap MDAliasListMap;
197
198 TypeToGEPOrUseMap GEPOrUseMap;
199
200 Type *mapType(SPIRVType *BT, Type *T);
201
202 // If a value is mapped twice, the existing mapped value is a placeholder,
203 // which must be a load instruction of a global variable whose name starts
204 // with kPlaceholderPrefix.
205 Value *mapValue(SPIRVValue *BV, Value *V);
206
207 // OpenCL function always has NoUnwind attribute.
208 // Change this if it is no longer true.
209 bool isFuncNoUnwind() const { return true; }
210
211 bool isFuncReadNone(const std::string &Name) const {
212 return BuiltInConstFunc.count(Name);
213 }
214
215 bool isDirectlyTranslatedToOCL(Op OpCode) const;
216 MDString *transOCLKernelArgTypeName(SPIRVFunctionParameter *);
217
218 // Attempt to translate Id as a (specialization) constant.
219 std::optional<uint64_t> transIdAsConstant(SPIRVId Id);
220
221 // Return the value of an Alignment or AlignmentId decoration for V.
222 std::optional<uint64_t> getAlignment(SPIRVValue *V);
223
224 Value *mapFunction(SPIRVFunction *BF, Function *F);
225 Value *getTranslatedValue(SPIRVValue *BV);
226 IntrinsicInst *getLifetimeStartIntrinsic(Instruction *I);
227 SPIRVErrorLog &getErrorLog();
228 void setCallingConv(CallInst *Call);
229 Type *transFPType(SPIRVType *T);
230 Value *transShiftLogicalBitwiseInst(SPIRVValue *BV, BasicBlock *BB,
231 Function *F);
232 Value *transCmpInst(SPIRVValue *BV, BasicBlock *BB, Function *F);
233 void transOCLBuiltinFromInstPreproc(SPIRVInstruction *BI, Type *&RetTy,
234 std::vector<SPIRVValue *> &Args);
235 Instruction *transOCLBuiltinPostproc(SPIRVInstruction *BI, CallInst *CI,
236 BasicBlock *BB,
237 const std::string &DemangledName);
238 std::string transOCLImageTypeAccessQualifier(SPIRV::SPIRVTypeImage *ST);
239 std::string transOCLPipeTypeAccessQualifier(SPIRV::SPIRVTypePipe *ST);
240 std::string transVCTypeName(SPIRVTypeBufferSurfaceINTEL *PST);
241
242 Value *oclTransConstantSampler(SPIRV::SPIRVConstantSampler *BCS,
243 BasicBlock *BB);
244 Value *oclTransConstantPipeStorage(SPIRV::SPIRVConstantPipeStorage *BCPS);
245 void setName(llvm::Value *V, SPIRVValue *BV);
246 template <typename LoopInstType>
247 void setLLVMLoopMetadata(const LoopInstType *LM, const Loop *LoopObj);
248 void transLLVMLoopMetadata(const Function *F);
249 inline llvm::Metadata *getMetadataFromName(std::string Name);
250 inline std::vector<llvm::Metadata *>
251 getMetadataFromNameAndParameter(std::string Name, SPIRVWord Parameter);
252 inline MDNode *getMetadataFromNameAndParameter(std::string Name,
253 int64_t Parameter);
254 template <class Source, class Func> bool foreachFuncCtlMask(Source, Func);
255 llvm::GlobalValue::LinkageTypes transLinkageType(const SPIRVValue *V);
256 Instruction *transAllAny(SPIRVInstruction *BI, BasicBlock *BB);
257 Instruction *transRelational(SPIRVInstruction *BI, BasicBlock *BB);
258
259 void transUserSemantic(SPIRV::SPIRVFunction *Fun);
260 void transGlobalAnnotations();
261 void transGlobalCtorDtors(SPIRVVariable *BV);
262 void createCXXStructor(const char *ListName,
263 SmallVectorImpl<Function *> &Funcs);
264 void transIntelFPGADecorations(SPIRVValue *BV, Value *V);
265 void transMemAliasingINTELDecorations(SPIRVValue *BV, Value *V);
266 void transDecorationsToMetadata(SPIRVValue *BV, Value *V);
267 void transFunctionDecorationsToMetadata(SPIRVFunction *BF, Function *F);
268 void
269 transFunctionPointerCallArgumentAttributes(SPIRVValue *BV, CallInst *CI,
270 SPIRVTypeFunction *CalledFnTy);
271 }; // class SPIRVToLLVM
272
273 } // namespace SPIRV
274
275 #endif // SPIRVREADER_H