forked from KhronosGroup/SPIRV-LLVM-Translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SPIRVWriter.h
222 lines (191 loc) · 9.1 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
//===- 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 "SPIRVFunction.h"
#include "SPIRVInstruction.h"
#include "SPIRVModule.h"
#include "SPIRVType.h"
#include "SPIRVValue.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Analysis/CallGraph.h"
#include "llvm/IR/IntrinsicInst.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 runOnModule(Module &Mod) override;
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<OCLTypeToSPIRV>();
}
static char ID;
// This enum sets the mode used to translate the value which is
// a function, that is necessary for a convenient function pointers handling.
// By default transValue uses 'Decl' mode, which means every function
// we meet during the translation should result in its declaration generated.
// In 'Pointer' mode we generate OpConstFunctionPointerINTEL constant instead.
enum class FuncTransMode { Decl, Pointer };
SPIRVType *transType(Type *T);
SPIRVType *transSPIRVOpaqueType(Type *T);
SPIRVValue *getTranslatedValue(const Value *) const;
spv::LoopControlMask getLoopControl(const BranchInst *Branch,
std::vector<SPIRVWord> &Parameters);
// Translation functions
bool transAddressingMode();
bool transAlign(Value *V, SPIRVValue *BV);
std::vector<SPIRVWord> transArguments(CallInst *, SPIRVBasicBlock *,
SPIRVEntry *);
bool transSourceLanguage();
bool transExtension();
bool transBuiltinSet();
bool isKnownIntrinsic(Intrinsic::ID Id);
SPIRVValue *transIntrinsicInst(IntrinsicInst *Intrinsic, SPIRVBasicBlock *BB);
SPIRVValue *transCallInst(CallInst *Call, SPIRVBasicBlock *BB);
SPIRVValue *transDirectCallInst(CallInst *Call, SPIRVBasicBlock *BB);
SPIRVValue *transIndirectCallInst(CallInst *Call, SPIRVBasicBlock *BB);
SPIRVValue *transAsmINTEL(InlineAsm *Asm);
SPIRVValue *transAsmCallINTEL(CallInst *Call, SPIRVBasicBlock *BB);
bool transDecoration(Value *V, SPIRVValue *BV);
SPIRVWord transFunctionControlMask(Function *);
SPIRVFunction *transFunctionDecl(Function *F);
void transVectorComputeMetadata(Function *F);
void transFPGAFunctionMetadata(SPIRVFunction *BF, 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();
void transFPContract();
SPIRVValue *transConstant(Value *V);
SPIRVValue *transValue(Value *V, SPIRVBasicBlock *BB,
bool CreateForward = true,
FuncTransMode FuncTrans = FuncTransMode::Decl);
void transGlobalAnnotation(GlobalVariable *V);
SPIRVValue *
transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
bool CreateForward = true,
FuncTransMode FuncTrans = FuncTransMode::Decl);
void transGlobalIOPipeStorage(GlobalVariable *V, MDNode *IO);
static SPIRVInstruction *applyRoundingModeConstraint(Value *V,
SPIRVInstruction *I);
typedef DenseMap<Type *, SPIRVType *> LLVMToSPIRVTypeMap;
typedef DenseMap<Value *, SPIRVValue *> LLVMToSPIRVValueMap;
typedef DenseMap<MDNode *, SmallSet<SPIRVId, 2>> LLVMToSPIRVMetadataMap;
private:
Module *M;
LLVMContext *Ctx;
SPIRVModule *BM;
LLVMToSPIRVTypeMap TypeMap;
LLVMToSPIRVValueMap ValueMap;
LLVMToSPIRVMetadataMap IndexGroupArrayMap;
SPIRVWord SrcLang;
SPIRVWord SrcLangVer;
std::unique_ptr<LLVMToSPIRVDbgTran> DbgTran;
std::unique_ptr<CallGraph> CG;
enum class FPContract { UNDEF, DISABLED, ENABLED };
DenseMap<Function *, FPContract> FPContractMap;
FPContract getFPContract(Function *F);
bool joinFPContract(Function *F, FPContract C);
void fpContractUpdateRecursive(Function *F, FPContract FPC);
SPIRVType *mapType(Type *T, SPIRVType *BT);
SPIRVValue *mapValue(Value *V, SPIRVValue *BV);
SPIRVType *getSPIRVType(Type *T) { return TypeMap[T]; }
SPIRVErrorLog &getErrorLog() { return BM->getErrorLog(); }
llvm::IntegerType *getSizetType(unsigned AS = 0);
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);
SPIRVValue *transAtomicStore(StoreInst *ST, SPIRVBasicBlock *BB);
SPIRVValue *transAtomicLoad(LoadInst *LD, 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 isKernel(Function *F);
bool transMetadata();
bool transOCLMetadata();
SPIRVInstruction *transBuiltinToInst(StringRef DemangledName, CallInst *CI,
SPIRVBasicBlock *BB);
SPIRVValue *transBuiltinToConstant(StringRef DemangledName, CallInst *CI);
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);
void transFunction(Function *I);
SPIRV::SPIRVLinkageTypeKind transLinkageType(const GlobalValue *GV);
bool isAnyFunctionReachableFromFunction(
const Function *FS,
const std::unordered_set<const Function *> Funcs) const;
void collectInputOutputVariables(SPIRVFunction *SF, Function *F);
};
} // namespace SPIRV
#endif // SPIRVWRITER_H