forked from KhronosGroup/SPIRV-LLVM-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIRVWriter.h
230 lines (197 loc) · 9.26 KB
/
SPIRVWriter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//===- SPIRVWriter.h - Converts LLVM to SPIR-V ------------------*- C++ -*-===//
//
// The LLVM/SPIR-V Translator
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal with the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimers.
// Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimers in the documentation
// and/or other materials provided with the distribution.
// Neither the names of Advanced Micro Devices, Inc., nor the names of its
// contributors may be used to endorse or promote products derived from this
// Software without specific prior written permission.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
// THE SOFTWARE.
//
//===----------------------------------------------------------------------===//
/// \file
///
/// This file contains declaration of LLVMToSPIRV class which implements
/// conversion of LLVM intermediate language to SPIR-V
/// binary.
///
//===----------------------------------------------------------------------===//
#ifndef SPIRVWRITER_H
#define SPIRVWRITER_H
#include "OCLTypeToSPIRV.h"
#include "OCLUtil.h"
#include "SPIRVBasicBlock.h"
#include "SPIRVEntry.h"
#include "SPIRVEnum.h"
#include "SPIRVExtInst.h"
#include "SPIRVFunction.h"
#include "SPIRVInstruction.h"
#include "SPIRVInternal.h"
#include "SPIRVMDWalker.h"
#include "SPIRVModule.h"
#include "SPIRVType.h"
#include "SPIRVUtil.h"
#include "SPIRVValue.h"
#include "llvm/ADT/Optional.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/PostDominators.h"
#include <memory>
using namespace llvm;
using namespace SPIRV;
using namespace OCLUtil;
namespace SPIRV {
class LLVMToSPIRVDbgTran;
class LLVMToSPIRV : public ModulePass {
public:
LLVMToSPIRV(SPIRVModule *SMod = nullptr);
virtual StringRef getPassName() const override { return "LLVMToSPIRV"; }
bool doInitialization(Module &) override;
bool runOnModule(Module &Mod) override;
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<OCLTypeToSPIRV>();
AU.addRequired<LoopInfoWrapperPass>();
AU.addRequired<PostDominatorTreeWrapperPass>();
}
static char ID;
SPIRVType *getSPIRVImageSampledType(StringRef S);
typedef SmallDenseMap<std::pair<Type*, const Metadata*>, SPIRVType*, 16> TypeSpecVisitedMD;
SPIRVType *decodeMDTypeSpec(StorageClass SC,
const Metadata* This,
MDNode::op_range operands,
TypeSpecVisitedMD& Visited,
Optional<StringRef> RequiredType = None);
void decodeMDDecorations(const Metadata *MD, SPIRVEntry* Target,
Optional<SPIRVWord> Member = None,
SmallVectorImpl<std::unique_ptr<SPIRVMemberDecorate>>* MemberDecs = nullptr);
void decodeMDDecoration(const Metadata *MD, SPIRVEntry* Target,
Optional<SPIRVWord> Member = None,
SmallVectorImpl<std::unique_ptr<SPIRVMemberDecorate>>* MemberDecs = nullptr);
StorageClass globalObjectStorageClass(GlobalObject* GO) const;
void addStorageClassCapabilities(StorageClass SC);
SPIRVType *transTypeSpecMDInner(GlobalObject *Root,
StorageClass RootSC,
Type* Ty,
const Metadata* Spec,
TypeSpecVisitedMD& Visited);
SPIRVType *transTypeSpecMD(GlobalObject *GO);
SPIRVType *transType(Type *T, GlobalObject *GO = nullptr);
SPIRVType *transSPIRVOpaqueType(Type *T);
SPIRVValue *getTranslatedValue(const Value *) const;
// Translation functions
bool transAddressingMode();
bool transAlign(Value *V, SPIRVValue *BV);
std::vector<SPIRVValue *> transArguments(CallInst *, SPIRVBasicBlock *);
std::vector<SPIRVWord> transArguments(CallInst *, SPIRVBasicBlock *,
SPIRVEntry *);
bool transSourceLanguage();
bool transExtension();
bool transBuiltinSet();
SPIRVValue *transGlSlIntrinsic(IntrinsicInst *II,
SPIRVBasicBlock *BB);
SPIRVValue *transVectorReduce(IntrinsicInst *II,
SPIRVBasicBlock *BB);
SPIRVValue *transIntrinsicInst(IntrinsicInst *Intrinsic, SPIRVBasicBlock *BB);
SPIRVValue *transCallInst(CallInst *Call, SPIRVBasicBlock *BB);
bool transDecoration(Value *V, SPIRVValue *BV);
SPIRVWord transFunctionControlMask(CallInst *);
SPIRVWord transFunctionControlMask(Function *);
SPIRVFunction *transFunctionDecl(Function *F);
bool transGlobalVariables();
Op transBoolOpCode(SPIRVValue *Opn, Op OC);
// Translate LLVM module to SPIR-V module.
// Returns true if succeeds.
bool translate();
bool transExecutionMode();
SPIRVValue *transConstant(Value *V);
SPIRVValue *transValue(Value *V, SPIRVBasicBlock *BB,
bool CreateForward = true);
SPIRVValue *transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
bool CreateForward = true);
typedef DenseMap<std::pair<Type *, const Metadata *>, SPIRVType *> LLVMToSPIRVTypeMap;
typedef DenseMap<Value *, SPIRVValue *> LLVMToSPIRVValueMap;
private:
Module *M;
LLVMContext *Ctx;
SPIRVModule *BM;
LLVMToSPIRVTypeMap TypeMap;
LLVMToSPIRVValueMap ValueMap;
SPIRVWord SrcLang;
SPIRVWord SrcLangVer;
std::unique_ptr<LLVMToSPIRVDbgTran> DbgTran;
unsigned GlobalStorageClassMDKindID = 0;
unsigned GlobalDecorationMDKindID = 0;
unsigned GlobalTypeSpecMDKindID = 0;
unsigned EntryExeModelMDKindID = 0;
unsigned EntryExeModeMDKindID = 0;
SPIRVType *mapType(Type *T, SPIRVType *BT,
const Metadata* Root = nullptr);
SPIRVValue *mapValue(Value *V, SPIRVValue *BV);
SPIRVErrorLog &getErrorLog() { return BM->getErrorLog(); }
llvm::IntegerType *getSizetType();
std::vector<SPIRVValue *> transValue(const std::vector<Value *> &Values,
SPIRVBasicBlock *BB);
std::vector<SPIRVWord> transValue(const std::vector<Value *> &Values,
SPIRVBasicBlock *BB, SPIRVEntry *Entry);
SPIRVInstruction *transBinaryInst(BinaryOperator *B, SPIRVBasicBlock *BB);
SPIRVInstruction *transCmpInst(CmpInst *Cmp, SPIRVBasicBlock *BB);
SPIRVInstruction *transLifetimeIntrinsicInst(Op OC, IntrinsicInst *Intrinsic,
SPIRVBasicBlock *BB);
void dumpUsers(Value *V);
template <class ExtInstKind>
bool oclGetExtInstIndex(const std::string &MangledName,
const std::string &DemangledName,
SPIRVWord *EntryPoint);
void
oclGetMutatedArgumentTypesByBuiltin(llvm::FunctionType *FT,
std::map<unsigned, Type *> &ChangedType,
Function *F);
bool isBuiltinTransToInst(Function *F);
bool isBuiltinTransToExtInst(Function *F,
SPIRVExtInstSetKind *BuiltinSet = nullptr,
SPIRVWord *EntryPoint = nullptr,
SmallVectorImpl<std::string> *Dec = nullptr);
bool oclIsKernel(Function *F);
bool transOCLKernelMetadata();
SPIRVInstruction *transBuiltinToInst(const std::string &DemangledName,
const std::string &MangledName,
CallInst *CI, SPIRVBasicBlock *BB);
SPIRVInstruction *transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI,
SPIRVBasicBlock *BB);
void mutateFuncArgType(const std::map<unsigned, Type *> &ChangedType,
Function *F);
SPIRVValue *transSpcvCast(CallInst *CI, SPIRVBasicBlock *BB);
SPIRVValue *oclTransSpvcCastSampler(CallInst *CI, SPIRVBasicBlock *BB);
SPIRV::SPIRVInstruction *transUnaryInst(UnaryInstruction *U,
SPIRVBasicBlock *BB);
/// Add a 32 bit integer constant.
/// \return Id of the constant.
SPIRVId addInt32(int);
void transFunction(Function *I);
SPIRV::SPIRVLinkageTypeKind transLinkageType(const GlobalValue *GV);
};
} // namespace SPIRV
#endif // SPIRVWRITER_H