• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- SPIRVWriter.cpp - Converts LLVM to SPIR-V ----------------*- 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 implements conversion of LLVM intermediate language to SPIR-V
37 /// binary.
38 ///
39 //===----------------------------------------------------------------------===//
40 
41 #include "SPIRVModule.h"
42 #include "SPIRVEnum.h"
43 #include "SPIRVEntry.h"
44 #include "SPIRVType.h"
45 #include "SPIRVValue.h"
46 #include "SPIRVFunction.h"
47 #include "SPIRVBasicBlock.h"
48 #include "SPIRVInstruction.h"
49 #include "SPIRVExtInst.h"
50 #include "SPIRVUtil.h"
51 #include "SPIRVInternal.h"
52 #include "SPIRVMDWalker.h"
53 #include "OCLTypeToSPIRV.h"
54 #include "OCLUtil.h"
55 
56 #include "llvm/ADT/DenseMap.h"
57 #include "llvm/ADT/SetVector.h"
58 #include "llvm/ADT/StringSwitch.h"
59 #include "llvm/ADT/Triple.h"
60 #include "llvm/Bitcode/ReaderWriter.h"
61 #include "llvm/IR/Constants.h"
62 #include "llvm/IR/DerivedTypes.h"
63 #include "llvm/IR/DebugInfo.h"
64 #include "llvm/IR/Function.h"
65 #include "llvm/IR/InstrTypes.h"
66 #include "llvm/IR/Instructions.h"
67 #include "llvm/IR/Module.h"
68 #include "llvm/IR/Operator.h"
69 #include "llvm/IR/Verifier.h"
70 #include "llvm/Pass.h"
71 #include "llvm/PassSupport.h"
72 #include "llvm/IR/LegacyPassManager.h"
73 #include "llvm/Support/Casting.h"
74 #include "llvm/Support/CommandLine.h"
75 #include "llvm/Support/Debug.h"
76 #include "llvm/Support/raw_ostream.h"
77 #include "llvm/Support/ToolOutputFile.h"
78 #include "llvm/Transforms/IPO.h"
79 
80 #include <iostream>
81 #include <list>
82 #include <memory>
83 #include <set>
84 #include <sstream>
85 #include <vector>
86 #include <functional>
87 #include <cstdlib>
88 
89 #define DEBUG_TYPE "spirv"
90 
91 using namespace llvm;
92 using namespace SPIRV;
93 using namespace OCLUtil;
94 
95 namespace llvm {
96   FunctionPass *createPromoteMemoryToRegisterPass();
97 }
98 
99 namespace SPIRV{
100 
101 cl::opt<bool> SPIRVMemToReg("spirv-mem2reg", cl::init(true),
102     cl::desc("LLVM/SPIR-V translation enable mem2reg"));
103 
104 
105 static void
foreachKernelArgMD(MDNode * MD,SPIRVFunction * BF,std::function<void (const std::string & Str,SPIRVFunctionParameter * BA)> Func)106 foreachKernelArgMD(MDNode *MD, SPIRVFunction *BF,
107     std::function<void(const std::string& Str,
108         SPIRVFunctionParameter *BA)>Func) {
109   for (unsigned I = 1, E = MD->getNumOperands(); I != E; ++I) {
110     SPIRVFunctionParameter *BA = BF->getArgument(I-1);
111     Func(getMDOperandAsString(MD, I), BA);
112   }
113 }
114 
115 /// Information for translating OCL builtin.
116 struct OCLBuiltinSPIRVTransInfo {
117   std::string UniqName;
118   /// Postprocessor of operands
119   std::function<void(std::vector<SPIRVWord>&)> PostProc;
OCLBuiltinSPIRVTransInfoSPIRV::OCLBuiltinSPIRVTransInfo120   OCLBuiltinSPIRVTransInfo(){
121     PostProc = [](std::vector<SPIRVWord>&){};
122   }
123 };
124 
125 class LLVMToSPIRVDbgTran {
126 public:
LLVMToSPIRVDbgTran(Module * TM=nullptr,SPIRVModule * TBM=nullptr)127   LLVMToSPIRVDbgTran(Module *TM = nullptr, SPIRVModule *TBM = nullptr)
128   :BM(TBM), M(TM){
129   }
130 
setModule(Module * Mod)131   void setModule(Module *Mod) { M = Mod;}
setSPIRVModule(SPIRVModule * SMod)132   void setSPIRVModule(SPIRVModule *SMod) { BM = SMod;}
133 
transDbgInfo(Value * V,SPIRVValue * BV)134   void transDbgInfo(Value *V, SPIRVValue *BV) {
135     if (auto I = dyn_cast<Instruction>(V)) {
136       auto DL = I->getDebugLoc();
137       if (DL.get() != nullptr) {
138         DILocation* DIL = DL.get();
139         auto File = BM->getString(DIL->getFilename().str());
140         // ToDo: SPIR-V rev.31 cannot add debug info for instructions without ids.
141         // This limitation needs to be addressed.
142         if (!BV->hasId())
143           return;
144         BM->addLine(BV, File, DL.getLine(), DL.getCol());
145       }
146     } else if (auto F = dyn_cast<Function>(V)) {
147       if (auto DIS = F->getSubprogram()) {
148         auto File = BM->getString(DIS->getFilename().str());
149         BM->addLine(BV, File, DIS->getLine(), 0);
150       }
151     }
152   }
153 
154 private:
155   SPIRVModule *BM;
156   Module *M;
157 };
158 
159 class LLVMToSPIRV: public ModulePass {
160 public:
LLVMToSPIRV(SPIRVModule * SMod=nullptr)161   LLVMToSPIRV(SPIRVModule *SMod = nullptr)
162       : ModulePass(ID),
163         M(nullptr),
164         Ctx(nullptr),
165         BM(SMod),
166         ExtSetId(SPIRVID_INVALID),
167         SrcLang(0),
168         SrcLangVer(0),
169         DbgTran(nullptr, SMod){
170   }
171 
runOnModule(Module & Mod)172   bool runOnModule(Module &Mod) override {
173     M = &Mod;
174     Ctx = &M->getContext();
175     DbgTran.setModule(M);
176     assert(BM && "SPIR-V module not initialized");
177     translate();
178     return true;
179   }
180 
getAnalysisUsage(AnalysisUsage & AU) const181   void getAnalysisUsage(AnalysisUsage &AU) const {
182     AU.addRequired<OCLTypeToSPIRV>();
183   }
184 
185   static char ID;
186 
187   SPIRVType *transType(Type *T);
188   SPIRVType *transSPIRVOpaqueType(Type *T);
189 
190   SPIRVValue *getTranslatedValue(Value *);
191 
192   // Translation functions
193   bool transAddressingMode();
194   bool transAlign(Value *V, SPIRVValue *BV);
195   std::vector<SPIRVValue *> transArguments(CallInst *, SPIRVBasicBlock *);
196   std::vector<SPIRVWord> transArguments(CallInst *, SPIRVBasicBlock *,
197       SPIRVEntry *);
198   bool transSourceLanguage();
199   bool transExtension();
200   bool transBuiltinSet();
201   SPIRVValue *transCallInst(CallInst *Call, SPIRVBasicBlock *BB);
202   bool transDecoration(Value *V, SPIRVValue *BV);
203   SPIRVWord transFunctionControlMask(CallInst *);
204   SPIRVWord transFunctionControlMask(Function *);
205   SPIRVFunction *transFunctionDecl(Function *F);
206   bool transGlobalVariables();
207 
208   Op transBoolOpCode(SPIRVValue *Opn, Op OC);
209   // Translate LLVM module to SPIR-V module.
210   // Returns true if succeeds.
211   bool translate();
212   bool transExecutionMode();
213   SPIRVValue *transConstant(Value *V);
214   SPIRVValue *transValue(Value *V, SPIRVBasicBlock *BB,
215       bool CreateForward = true);
216   SPIRVValue *transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
217       bool CreateForward = true);
218 
219   typedef DenseMap<Type *, SPIRVType *> LLVMToSPIRVTypeMap;
220   typedef DenseMap<Value *, SPIRVValue *> LLVMToSPIRVValueMap;
221 private:
222   Module *M;
223   LLVMContext *Ctx;
224   SPIRVModule *BM;
225   LLVMToSPIRVTypeMap TypeMap;
226   LLVMToSPIRVValueMap ValueMap;
227   //ToDo: support multiple builtin sets. Currently assume one builtin set.
228   SPIRVId ExtSetId;
229   SPIRVWord SrcLang;
230   SPIRVWord SrcLangVer;
231   LLVMToSPIRVDbgTran DbgTran;
232 
mapType(Type * T,SPIRVType * BT)233   SPIRVType *mapType(Type *T, SPIRVType *BT) {
234     TypeMap[T] = BT;
235     SPIRVDBG(dbgs() << "[mapType] " << *T << " => ";
236              spvdbgs() << *BT << '\n');
237     return BT;
238   }
239 
mapValue(Value * V,SPIRVValue * BV)240   SPIRVValue *mapValue(Value *V, SPIRVValue *BV) {
241     auto Loc = ValueMap.find(V);
242     if (Loc != ValueMap.end()) {
243       if (Loc->second == BV)
244         return BV;
245       assert (Loc->second->isForward() &&
246           "LLVM Value is mapped to different SPIRV Values");
247       auto Forward = static_cast<SPIRVForward *>(Loc->second);
248       BV->setId(Forward->getId());
249       BM->replaceForward(Forward, BV);
250     }
251     ValueMap[V] = BV;
252     SPIRVDBG(dbgs() << "[mapValue] " << *V << " => ";
253              spvdbgs() << *BV << "\n");
254     return BV;
255   }
256 
getSPIRVType(Type * T)257   SPIRVType *getSPIRVType(Type *T) {
258       return TypeMap[T];
259   }
260 
getSPIRVValue(Value * V)261   SPIRVValue *getSPIRVValue(Value *V) {
262       return ValueMap[V];
263   }
264 
getErrorLog()265   SPIRVErrorLog &getErrorLog() {
266     return BM->getErrorLog();
267   }
268 
269   llvm::IntegerType* getSizetType();
270   std::vector<SPIRVValue*> transValue(const std::vector<Value *> &Values,
271       SPIRVBasicBlock* BB);
272   std::vector<SPIRVWord> transValue(const std::vector<Value *> &Values,
273       SPIRVBasicBlock* BB, SPIRVEntry *Entry);
274 
275   SPIRVInstruction* transBinaryInst(BinaryOperator* B, SPIRVBasicBlock* BB);
276   SPIRVInstruction* transCmpInst(CmpInst* Cmp, SPIRVBasicBlock* BB);
277 
278   void dumpUsers(Value *V);
279 
280   template<class ExtInstKind>
281   bool oclGetExtInstIndex(const std::string &MangledName,
282       const std::string& DemangledName, SPIRVWord* EntryPoint);
283   void oclGetMutatedArgumentTypesByBuiltin(llvm::FunctionType* FT,
284       std::map<unsigned, Type*>& ChangedType, Function* F);
285 
286   bool isBuiltinTransToInst(Function *F);
287   bool isBuiltinTransToExtInst(Function *F,
288       SPIRVExtInstSetKind *BuiltinSet = nullptr,
289       SPIRVWord *EntryPoint = nullptr,
290       SmallVectorImpl<std::string> *Dec = nullptr);
291   bool oclIsKernel(Function *F);
292 
293   bool transOCLKernelMetadata();
294 
295   SPIRVInstruction *transBuiltinToInst(const std::string& DemangledName,
296       const std::string &MangledName, CallInst* CI, SPIRVBasicBlock* BB);
297   SPIRVInstruction *transBuiltinToInstWithoutDecoration(Op OC,
298       CallInst* CI, SPIRVBasicBlock* BB);
299   void mutateFuncArgType(const std::map<unsigned, Type*>& ChangedType,
300       Function* F);
301 
302   SPIRVValue *transSpcvCast(CallInst* CI, SPIRVBasicBlock *BB);
303   SPIRVValue *oclTransSpvcCastSampler(CallInst* CI, SPIRVBasicBlock *BB);
304 
305   SPIRV::SPIRVInstruction* transUnaryInst(UnaryInstruction* U,
306       SPIRVBasicBlock* BB);
307 
308   /// Add a 32 bit integer constant.
309   /// \return Id of the constant.
310   SPIRVId addInt32(int);
311   void transFunction(Function *I);
312   SPIRV::SPIRVLinkageTypeKind transLinkageType(const GlobalValue* GV);
313 };
314 
315 
316 SPIRVValue *
getTranslatedValue(Value * V)317 LLVMToSPIRV::getTranslatedValue(Value *V) {
318   LLVMToSPIRVValueMap::iterator Loc = ValueMap.find(V);
319   if (Loc != ValueMap.end())
320     return Loc->second;
321   return nullptr;
322 }
323 
324 bool
oclIsKernel(Function * F)325 LLVMToSPIRV::oclIsKernel(Function *F) {
326   if (F->getCallingConv() == CallingConv::SPIR_KERNEL)
327     return true;
328   return false;
329 }
330 
331 bool
isBuiltinTransToInst(Function * F)332 LLVMToSPIRV::isBuiltinTransToInst(Function *F) {
333   std::string DemangledName;
334   if (!oclIsBuiltin(F->getName(), &DemangledName) &&
335       !isDecoratedSPIRVFunc(F, &DemangledName))
336     return false;
337   SPIRVDBG(spvdbgs() << "CallInst: demangled name: " << DemangledName << '\n');
338   return getSPIRVFuncOC(DemangledName) != OpNop;
339 }
340 
341 bool
isBuiltinTransToExtInst(Function * F,SPIRVExtInstSetKind * ExtSet,SPIRVWord * ExtOp,SmallVectorImpl<std::string> * Dec)342 LLVMToSPIRV::isBuiltinTransToExtInst(Function *F,
343     SPIRVExtInstSetKind *ExtSet,
344     SPIRVWord *ExtOp,
345     SmallVectorImpl<std::string> *Dec) {
346   std::string OrigName = F->getName();
347   std::string DemangledName;
348   if (!oclIsBuiltin(OrigName, &DemangledName))
349     return false;
350   DEBUG(dbgs() << "[oclIsBuiltinTransToExtInst] CallInst: demangled name: "
351       << DemangledName << '\n');
352   StringRef S = DemangledName;
353   if (!S.startswith(kSPIRVName::Prefix))
354     return false;
355   S = S.drop_front(strlen(kSPIRVName::Prefix));
356   auto Loc = S.find(kSPIRVPostfix::Divider);
357   auto ExtSetName = S.substr(0, Loc);
358   SPIRVExtInstSetKind Set = SPIRVEIS_Count;
359   if (!SPIRVExtSetShortNameMap::rfind(ExtSetName, &Set))
360     return false;
361   assert(Set == BM->getBuiltinSet(ExtSetId) &&
362       "Invalid extended instruction set");
363   assert(Set == SPIRVEIS_OpenCL && "Unsupported extended instruction set");
364 
365   auto ExtOpName = S.substr(Loc + 1);
366   auto Splited = ExtOpName.split(kSPIRVPostfix::ExtDivider);
367   OCLExtOpKind EOC;
368   if (!OCLExtOpMap::rfind(Splited.first, &EOC))
369     return false;
370 
371   if (ExtSet)
372     *ExtSet = Set;
373   if (ExtOp)
374     *ExtOp = EOC;
375   if (Dec) {
376     SmallVector<StringRef, 2> P;
377     Splited.second.split(P, kSPIRVPostfix::Divider);
378     for (auto &I:P)
379       Dec->push_back(I.str());
380   }
381   return true;
382 }
383 
384 /// Decode SPIR-V type name in the format spirv.{TypeName}._{Postfixes}
385 /// where Postfixes are strings separated by underscores.
386 /// \return TypeName.
387 /// \param Ops contains the integers decoded from postfixes.
388 static std::string
decodeSPIRVTypeName(StringRef Name,SmallVectorImpl<std::string> & Strs)389  decodeSPIRVTypeName(StringRef Name,
390     SmallVectorImpl<std::string>& Strs) {
391   SmallVector<StringRef, 4> SubStrs;
392   const char Delim[] = { kSPIRVTypeName::Delimiter, 0 };
393   Name.split(SubStrs, Delim, -1, true);
394   assert(SubStrs.size() >= 2 && "Invalid SPIRV type name");
395   assert(SubStrs[0] == kSPIRVTypeName::Prefix && "Invalid prefix");
396   assert((SubStrs.size() == 2 || !SubStrs[2].empty()) && "Invalid postfix");
397 
398   if (SubStrs.size() > 2) {
399     const char PostDelim[] = { kSPIRVTypeName::PostfixDelim, 0 };
400     SmallVector<StringRef, 4> Postfixes;
401     SubStrs[2].split(Postfixes, PostDelim, -1, true);
402     assert(Postfixes.size() > 1 && Postfixes[0].empty() && "Invalid postfix");
403     for (unsigned I = 1, E = Postfixes.size(); I != E; ++I)
404       Strs.push_back(std::string(Postfixes[I]).c_str());
405   }
406   return SubStrs[1].str();
407 }
408 
recursiveType(const StructType * ST,const Type * Ty)409 static bool recursiveType(const StructType *ST, const Type *Ty) {
410   SmallPtrSet<const StructType *, 4> Seen;
411 
412   std::function<bool(const Type *Ty)> Run = [&](const Type *Ty) {
413     if (!isa<CompositeType>(Ty))
414       return false;
415 
416     if (auto *StructTy = dyn_cast<StructType>(Ty)) {
417       if (StructTy == ST)
418         return true;
419 
420       if (Seen.count(StructTy))
421         return false;
422 
423       Seen.insert(StructTy);
424 
425       return find_if(StructTy->subtype_begin(), StructTy->subtype_end(), Run) !=
426              StructTy->subtype_end();
427     }
428 
429     if (auto *PtrTy = dyn_cast<PointerType>(Ty))
430       return Run(PtrTy->getPointerElementType());
431 
432     if (auto *ArrayTy = dyn_cast<ArrayType>(Ty))
433       return Run(ArrayTy->getArrayElementType());
434 
435     return false;
436   };
437 
438   return Run(Ty);
439 }
440 
441 SPIRVType *
transType(Type * T)442 LLVMToSPIRV::transType(Type *T) {
443   LLVMToSPIRVTypeMap::iterator Loc = TypeMap.find(T);
444   if (Loc != TypeMap.end())
445     return Loc->second;
446 
447   SPIRVDBG(dbgs() << "[transType] " << *T << '\n');
448   if (T->isVoidTy())
449     return mapType(T, BM->addVoidType());
450 
451   if (T->isIntegerTy(1))
452     return mapType(T, BM->addBoolType());
453 
454   if (T->isIntegerTy())
455     return mapType(T, BM->addIntegerType(T->getIntegerBitWidth()));
456 
457   if (T->isFloatingPointTy())
458     return mapType(T, BM->addFloatType(T->getPrimitiveSizeInBits()));
459 
460   // A pointer to image or pipe type in LLVM is translated to a SPIRV
461   // sampler or pipe type.
462   if (T->isPointerTy()) {
463     auto ET = T->getPointerElementType();
464     assert(!ET->isFunctionTy() && "Function pointer type is not allowed");
465     auto ST = dyn_cast<StructType>(ET);
466     auto AddrSpc = T->getPointerAddressSpace();
467     if (ST && !ST->isSized()) {
468       Op OpCode;
469       StringRef STName = ST->getName();
470       // Workaround for non-conformant SPIR binary
471       if (STName == "struct._event_t") {
472         STName = kSPR2TypeName::Event;
473         ST->setName(STName);
474       }
475       assert (!STName.startswith(kSPR2TypeName::Pipe) &&
476               "OpenCL type names should be translated to SPIR-V type names");
477       // ToDo: For SPIR1.2/2.0 there may still be load/store or bitcast
478       // instructions using opencl.* type names. We need to handle these
479       // type names until they are all mapped or FE generates SPIR-V type
480       // names.
481       if (STName.find(kSPR2TypeName::Pipe) == 0) {
482         assert(AddrSpc == SPIRAS_Global);
483         SmallVector<StringRef, 4> SubStrs;
484         const char Delims[] = {kSPR2TypeName::Delimiter, 0};
485         STName.split(SubStrs, Delims);
486         std::string Acc = kAccessQualName::ReadOnly;
487         if (SubStrs.size() > 2) {
488           Acc = SubStrs[2];
489         }
490         auto PipeT = BM->addPipeType();
491         PipeT->setPipeAcessQualifier(SPIRSPIRVAccessQualifierMap::map(Acc));
492         return mapType(T, PipeT);
493       } else if (STName.find(kSPR2TypeName::ImagePrefix) == 0) {
494         assert(AddrSpc == SPIRAS_Global);
495         auto SPIRVImageTy = getSPIRVImageTypeFromOCL(M, T);
496         return mapType(T, transSPIRVOpaqueType(SPIRVImageTy));
497       } else if (STName.startswith(kSPIRVTypeName::PrefixAndDelim))
498         return transSPIRVOpaqueType(T);
499       else if (OCLOpaqueTypeOpCodeMap::find(STName, &OpCode)) {
500         switch (OpCode) {
501         default:
502           return mapType(T, BM->addOpaqueGenericType(OpCode));
503         case OpTypePipe:
504           return mapType(T, BM->addPipeType());
505         case OpTypeDeviceEvent:
506           return mapType(T, BM->addDeviceEventType());
507         case OpTypeQueue:
508           return mapType(T, BM->addQueueType());
509         }
510       } else if (isPointerToOpaqueStructType(T)) {
511         return mapType(T, BM->addPointerType(SPIRSPIRVAddrSpaceMap::map(
512           static_cast<SPIRAddressSpace>(AddrSpc)),
513           transType(ET)));
514       }
515     } else  {
516       return mapType(T, BM->addPointerType(SPIRSPIRVAddrSpaceMap::map(
517         static_cast<SPIRAddressSpace>(AddrSpc)),
518         transType(ET)));
519     }
520   }
521 
522   if (T->isVectorTy())
523     return mapType(T, BM->addVectorType(transType(T->getVectorElementType()),
524         T->getVectorNumElements()));
525 
526   if (T->isArrayTy())
527     return mapType(T, BM->addArrayType(transType(T->getArrayElementType()),
528         static_cast<SPIRVConstant*>(transValue(ConstantInt::get(getSizetType(),
529             T->getArrayNumElements(), false), nullptr))));
530 
531   if (T->isStructTy() && !T->isSized()) {
532     auto ST = dyn_cast<StructType>(T);
533     (void) ST;
534     assert(!ST->getName().startswith(kSPR2TypeName::Pipe));
535     assert(!ST->getName().startswith(kSPR2TypeName::ImagePrefix));
536     return mapType(T, BM->addOpaqueType(T->getStructName()));
537   }
538 
539   if (auto ST = dyn_cast<StructType>(T)) {
540     assert(ST->isSized());
541 
542     std::string Name;
543     if (ST->hasName())
544       Name = ST->getName();
545 
546     if(Name == getSPIRVTypeName(kSPIRVTypeName::ConstantSampler))
547       return transType(getSamplerType(M));
548     if (Name == getSPIRVTypeName(kSPIRVTypeName::ConstantPipeStorage))
549       return transType(getPipeStorageType(M));
550 
551     auto *Struct = BM->openStructType(T->getStructNumElements(), Name);
552     mapType(T, Struct);
553 
554     SmallVector<unsigned, 4> ForwardRefs;
555 
556     for (unsigned I = 0, E = T->getStructNumElements(); I != E; ++I) {
557       auto *ElemTy = ST->getElementType(I);
558       if (isa<CompositeType>(ElemTy) && recursiveType(ST, ElemTy))
559         ForwardRefs.push_back(I);
560       else
561         Struct->setMemberType(I, transType(ST->getElementType(I)));
562     }
563 
564     BM->closeStructType(Struct, ST->isPacked());
565 
566     for (auto I : ForwardRefs)
567       Struct->setMemberType(I, transType(ST->getElementType(I)));
568 
569     return Struct;
570   }
571 
572   if (FunctionType *FT = dyn_cast<FunctionType>(T)) {
573     SPIRVType *RT = transType(FT->getReturnType());
574     std::vector<SPIRVType *> PT;
575     for (FunctionType::param_iterator I = FT->param_begin(),
576         E = FT->param_end(); I != E; ++I)
577       PT.push_back(transType(*I));
578     return mapType(T, BM->addFunctionType(RT, PT));
579   }
580 
581   llvm_unreachable("Not implemented!");
582   return 0;
583 }
584 
585 SPIRVType *
transSPIRVOpaqueType(Type * T)586 LLVMToSPIRV::transSPIRVOpaqueType(Type *T) {
587   auto ET = T->getPointerElementType();
588   auto ST = cast<StructType>(ET);
589   auto AddrSpc = T->getPointerAddressSpace();
590   (void)AddrSpc;  // prevent warning about unused variable in NDEBUG build
591   auto STName = ST->getStructName();
592   assert (STName.startswith(kSPIRVTypeName::PrefixAndDelim) &&
593     "Invalid SPIR-V opaque type name");
594   SmallVector<std::string, 8> Postfixes;
595   auto TN = decodeSPIRVTypeName(STName, Postfixes);
596   if (TN == kSPIRVTypeName::Pipe) {
597     assert(AddrSpc == SPIRAS_Global);
598     assert(Postfixes.size() == 1 && "Invalid pipe type ops");
599     auto PipeT = BM->addPipeType();
600     PipeT->setPipeAcessQualifier(static_cast<spv::AccessQualifier>(
601       atoi(Postfixes[0].c_str())));
602     return mapType(T, PipeT);
603   } else if (TN == kSPIRVTypeName::Image) {
604     assert(AddrSpc == SPIRAS_Global);
605     // The sampled type needs to be translated through LLVM type to guarantee
606     // uniqueness.
607     auto SampledT = transType(getLLVMTypeForSPIRVImageSampledTypePostfix(
608       Postfixes[0], *Ctx));
609     SmallVector<int, 7> Ops;
610     for (unsigned I = 1; I < 8; ++I)
611       Ops.push_back(atoi(Postfixes[I].c_str()));
612     SPIRVTypeImageDescriptor Desc(static_cast<SPIRVImageDimKind>(Ops[0]),
613         Ops[1], Ops[2], Ops[3], Ops[4], Ops[5]);
614     return mapType(T, BM->addImageType(SampledT, Desc,
615                    static_cast<spv::AccessQualifier>(Ops[6])));
616   } else if (TN == kSPIRVTypeName::SampledImg) {
617     return mapType(T, BM->addSampledImageType(
618         static_cast<SPIRVTypeImage *>(
619             transType(getSPIRVTypeByChangeBaseTypeName(M,
620                 T, kSPIRVTypeName::SampledImg,
621                 kSPIRVTypeName::Image)))));
622   } else if(TN == kSPIRVTypeName::Sampler)
623     return mapType(T, BM->addSamplerType());
624   else if (TN == kSPIRVTypeName::DeviceEvent)
625     return mapType(T, BM->addDeviceEventType());
626   else if (TN == kSPIRVTypeName::Queue)
627     return mapType(T, BM->addQueueType());
628   else if (TN == kSPIRVTypeName::PipeStorage)
629     return mapType(T, BM->addPipeStorageType());
630   else
631     return mapType(T, BM->addOpaqueGenericType(
632       SPIRVOpaqueTypeOpCodeMap::map(TN)));
633 }
634 
635 SPIRVFunction *
transFunctionDecl(Function * F)636 LLVMToSPIRV::transFunctionDecl(Function *F) {
637   if (auto BF= getTranslatedValue(F))
638     return static_cast<SPIRVFunction *>(BF);
639 
640   SPIRVTypeFunction *BFT = static_cast<SPIRVTypeFunction *>(transType(
641       getAnalysis<OCLTypeToSPIRV>().getAdaptedType(F)));
642   SPIRVFunction *BF = static_cast<SPIRVFunction *>(mapValue(F,
643       BM->addFunction(BFT)));
644   BF->setFunctionControlMask(transFunctionControlMask(F));
645   if (F->hasName())
646     BM->setName(BF, F->getName());
647   if (oclIsKernel(F))
648     BM->addEntryPoint(ExecutionModelKernel, BF->getId());
649   else if (F->getLinkage() != GlobalValue::InternalLinkage)
650     BF->setLinkageType(transLinkageType(F));
651   auto Attrs = F->getAttributes();
652   for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
653       ++I) {
654     auto ArgNo = I->getArgNo();
655     SPIRVFunctionParameter *BA = BF->getArgument(ArgNo);
656     if (I->hasName())
657       BM->setName(BA, I->getName());
658     if (I->hasByValAttr())
659       BA->addAttr(FunctionParameterAttributeByVal);
660     if (I->hasNoAliasAttr())
661       BA->addAttr(FunctionParameterAttributeNoAlias);
662     if (I->hasNoCaptureAttr())
663       BA->addAttr(FunctionParameterAttributeNoCapture);
664     if (I->hasStructRetAttr())
665       BA->addAttr(FunctionParameterAttributeSret);
666     if (Attrs.hasAttribute(ArgNo + 1, Attribute::ZExt))
667       BA->addAttr(FunctionParameterAttributeZext);
668     if (Attrs.hasAttribute(ArgNo + 1, Attribute::SExt))
669       BA->addAttr(FunctionParameterAttributeSext);
670     if (Attrs.hasAttribute(ArgNo + 1, Attribute::Dereferenceable))
671       BA->addDecorate(DecorationMaxByteOffset,
672                       Attrs.getAttribute(ArgNo + 1, Attribute::Dereferenceable)
673                         .getDereferenceableBytes());
674   }
675   if (Attrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt))
676     BF->addDecorate(DecorationFuncParamAttr, FunctionParameterAttributeZext);
677   if (Attrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
678     BF->addDecorate(DecorationFuncParamAttr, FunctionParameterAttributeSext);
679   DbgTran.transDbgInfo(F, BF);
680   SPIRVDBG(dbgs() << "[transFunction] " << *F << " => ";
681     spvdbgs() << *BF << '\n';)
682   return BF;
683 }
684 
685 #define _SPIRV_OPL(x) OpLogical##x
686 
687 #define _SPIRV_OPB(x) OpBitwise##x
688 
689 SPIRVValue *
transConstant(Value * V)690 LLVMToSPIRV::transConstant(Value *V) {
691   if (auto CPNull = dyn_cast<ConstantPointerNull>(V))
692     return BM->addNullConstant(bcast<SPIRVTypePointer>(transType(
693         CPNull->getType())));
694 
695   if (auto CAZero = dyn_cast<ConstantAggregateZero>(V)) {
696     Type *AggType = CAZero->getType();
697     if (const StructType* ST = dyn_cast<StructType>(AggType))
698       if (ST->getName() == getSPIRVTypeName(kSPIRVTypeName::ConstantSampler))
699         return BM->addSamplerConstant(transType(AggType), 0,0,0);
700 
701     return BM->addNullConstant(transType(AggType));
702   }
703 
704   if (auto ConstI = dyn_cast<ConstantInt>(V))
705     return BM->addConstant(transType(V->getType()), ConstI->getZExtValue());
706 
707   if (auto ConstFP = dyn_cast<ConstantFP>(V)) {
708     auto BT = static_cast<SPIRVType *>(transType(V->getType()));
709     return BM->addConstant(BT,
710         ConstFP->getValueAPF().bitcastToAPInt().getZExtValue());
711   }
712 
713   if (auto ConstDA = dyn_cast<ConstantDataArray>(V)) {
714     std::vector<SPIRVValue *> BV;
715     for (unsigned I = 0, E = ConstDA->getNumElements(); I != E; ++I)
716       BV.push_back(transValue(ConstDA->getElementAsConstant(I), nullptr));
717     return BM->addCompositeConstant(transType(V->getType()), BV);
718   }
719 
720   if (auto ConstA = dyn_cast<ConstantArray>(V)) {
721     std::vector<SPIRVValue *> BV;
722     for (auto I = ConstA->op_begin(), E = ConstA->op_end(); I != E; ++I)
723       BV.push_back(transValue(*I, nullptr));
724     return BM->addCompositeConstant(transType(V->getType()), BV);
725   }
726 
727   if (auto ConstDV = dyn_cast<ConstantDataVector>(V)) {
728     std::vector<SPIRVValue *> BV;
729     for (unsigned I = 0, E = ConstDV->getNumElements(); I != E; ++I)
730       BV.push_back(transValue(ConstDV->getElementAsConstant(I), nullptr));
731     return BM->addCompositeConstant(transType(V->getType()), BV);
732   }
733 
734   if (auto ConstV = dyn_cast<ConstantVector>(V)) {
735     std::vector<SPIRVValue *> BV;
736     for (auto I = ConstV->op_begin(), E = ConstV->op_end(); I != E; ++I)
737       BV.push_back(transValue(*I, nullptr));
738     return BM->addCompositeConstant(transType(V->getType()), BV);
739   }
740 
741   if (auto ConstV = dyn_cast<ConstantStruct>(V)) {
742     if (ConstV->getType()->getName() ==
743         getSPIRVTypeName(kSPIRVTypeName::ConstantSampler)) {
744       assert(ConstV->getNumOperands() == 3);
745       SPIRVWord
746         AddrMode   = ConstV->getOperand(0)->getUniqueInteger().getZExtValue(),
747         Normalized = ConstV->getOperand(1)->getUniqueInteger().getZExtValue(),
748         FilterMode = ConstV->getOperand(2)->getUniqueInteger().getZExtValue();
749       assert(AddrMode < 5 && "Invalid addressing mode");
750       assert(Normalized < 2 && "Invalid value of normalized coords");
751       assert(FilterMode < 2 && "Invalid filter mode");
752       SPIRVType* SamplerTy = transType(ConstV->getType());
753       return BM->addSamplerConstant(SamplerTy,
754                                     AddrMode, Normalized, FilterMode);
755     }
756     if (ConstV->getType()->getName() ==
757       getSPIRVTypeName(kSPIRVTypeName::ConstantPipeStorage)) {
758       assert(ConstV->getNumOperands() == 3);
759       SPIRVWord
760         PacketSize = ConstV->getOperand(0)->getUniqueInteger().getZExtValue(),
761         PacketAlign = ConstV->getOperand(1)->getUniqueInteger().getZExtValue(),
762         Capacity = ConstV->getOperand(2)->getUniqueInteger().getZExtValue();
763       assert(PacketAlign >= 1 && "Invalid packet alignment");
764       assert(PacketSize >= PacketAlign && PacketSize % PacketAlign == 0 &&
765         "Invalid packet size and/or alignment.");
766       SPIRVType* PipeStorageTy = transType(ConstV->getType());
767       return BM->addPipeStorageConstant(PipeStorageTy, PacketSize, PacketAlign,
768                                         Capacity);
769     }
770     std::vector<SPIRVValue *> BV;
771     for (auto I = ConstV->op_begin(), E = ConstV->op_end(); I != E; ++I)
772       BV.push_back(transValue(*I, nullptr));
773     return BM->addCompositeConstant(transType(V->getType()), BV);
774   }
775 
776   if (auto ConstUE = dyn_cast<ConstantExpr>(V)) {
777     auto Inst = ConstUE->getAsInstruction();
778     SPIRVDBG(dbgs() << "ConstantExpr: " << *ConstUE << '\n';
779       dbgs() << "Instruction: " << *Inst << '\n';)
780     auto BI = transValue(Inst, nullptr, false);
781     Inst->dropAllReferences();
782     return BI;
783   }
784 
785   if (isa<UndefValue>(V)) {
786     return BM->addUndef(transType(V->getType()));
787   }
788 
789   return nullptr;
790 }
791 
792 SPIRVValue *
transValue(Value * V,SPIRVBasicBlock * BB,bool CreateForward)793 LLVMToSPIRV::transValue(Value *V, SPIRVBasicBlock *BB, bool CreateForward) {
794   LLVMToSPIRVValueMap::iterator Loc = ValueMap.find(V);
795   if (Loc != ValueMap.end() && (!Loc->second->isForward() || CreateForward))
796     return Loc->second;
797 
798   SPIRVDBG(dbgs() << "[transValue] " << *V << '\n');
799   assert ((!isa<Instruction>(V) || isa<GetElementPtrInst>(V) ||
800       isa<CastInst>(V) || BB) &&
801       "Invalid SPIRV BB");
802 
803   auto BV = transValueWithoutDecoration(V, BB, CreateForward);
804   std::string name = V->getName();
805   if (!name.empty()) // Don't erase the name, which BM might already have
806     BM->setName(BV, name);
807   if(!transDecoration(V, BV))
808     return nullptr;
809   return BV;
810 }
811 
812 SPIRVInstruction*
transBinaryInst(BinaryOperator * B,SPIRVBasicBlock * BB)813 LLVMToSPIRV::transBinaryInst(BinaryOperator* B, SPIRVBasicBlock* BB) {
814   unsigned LLVMOC = B->getOpcode();
815   auto Op0 = transValue(B->getOperand(0), BB);
816   SPIRVInstruction* BI = BM->addBinaryInst(
817       transBoolOpCode(Op0, OpCodeMap::map(LLVMOC)),
818       transType(B->getType()), Op0, transValue(B->getOperand(1), BB), BB);
819   return BI;
820 }
821 
822 SPIRVInstruction*
transCmpInst(CmpInst * Cmp,SPIRVBasicBlock * BB)823 LLVMToSPIRV::transCmpInst(CmpInst* Cmp, SPIRVBasicBlock* BB) {
824   auto Op0 = transValue(Cmp->getOperand(0), BB);
825   SPIRVInstruction* BI = BM->addCmpInst(
826       transBoolOpCode(Op0, CmpMap::map(Cmp->getPredicate())),
827       transType(Cmp->getType()), Op0,
828       transValue(Cmp->getOperand(1), BB), BB);
829   return BI;
830 }
831 
transUnaryInst(UnaryInstruction * U,SPIRVBasicBlock * BB)832 SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
833                                                   SPIRVBasicBlock *BB) {
834   Op BOC = OpNop;
835   if (auto Cast = dyn_cast<AddrSpaceCastInst>(U)) {
836     if (Cast->getDestTy()->getPointerAddressSpace() == SPIRAS_Generic) {
837       assert(Cast->getSrcTy()->getPointerAddressSpace() != SPIRAS_Constant &&
838              "Casts from constant address space to generic are illegal");
839       BOC = OpPtrCastToGeneric;
840     } else {
841       assert(Cast->getDestTy()->getPointerAddressSpace() != SPIRAS_Constant &&
842              "Casts from generic address space to constant are illegal");
843       assert(Cast->getSrcTy()->getPointerAddressSpace() == SPIRAS_Generic);
844       BOC = OpGenericCastToPtr;
845     }
846   } else {
847     auto OpCode = U->getOpcode();
848     BOC = OpCodeMap::map(OpCode);
849   }
850 
851   auto Op = transValue(U->getOperand(0), BB);
852   return BM->addUnaryInst(transBoolOpCode(Op, BOC),
853       transType(U->getType()), Op, BB);
854 }
855 
856 /// An instruction may use an instruction from another BB which has not been
857 /// translated. SPIRVForward should be created as place holder for these
858 /// instructions and replaced later by the real instructions.
859 /// Use CreateForward = true to indicate such situation.
860 SPIRVValue *
transValueWithoutDecoration(Value * V,SPIRVBasicBlock * BB,bool CreateForward)861 LLVMToSPIRV::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB,
862     bool CreateForward) {
863   if (auto LBB = dyn_cast<BasicBlock>(V)) {
864     auto BF = static_cast<SPIRVFunction *>(getTranslatedValue(LBB->getParent()));
865     assert (BF && "Function not translated");
866     BB = static_cast<SPIRVBasicBlock *>(mapValue(V, BM->addBasicBlock(BF)));
867     BM->setName(BB, LBB->getName());
868     return BB;
869   }
870 
871   if (auto F = dyn_cast<Function>(V))
872     return transFunctionDecl(F);
873 
874   if (auto GV = dyn_cast<GlobalVariable>(V)) {
875     llvm::PointerType * Ty = GV->getType();
876     // Though variables with common linkage type are initialized by 0,
877     // they can be represented in SPIR-V as uninitialized variables with
878     // 'Export' linkage type, just as tentative definitions look in C
879     llvm::Value *Init = GV->hasInitializer() && !GV->hasCommonLinkage() ?
880       GV->getInitializer() : nullptr;
881     StructType *ST = Init ? dyn_cast<StructType>(Init->getType()) : nullptr;
882     if (ST && ST->hasName() && isSPIRVConstantName(ST->getName())) {
883       auto BV = transConstant(Init);
884       assert(BV);
885       return mapValue(V, BV);
886     } else if (ConstantExpr *ConstUE = dyn_cast_or_null<ConstantExpr>(Init)) {
887       Instruction * Inst = ConstUE->getAsInstruction();
888       if (isSpecialTypeInitializer(Inst)) {
889         Init = Inst->getOperand(0);
890         Ty = static_cast<PointerType*>(Init->getType());
891       }
892       Inst->dropAllReferences();
893     }
894     auto BVar = static_cast<SPIRVVariable *>(BM->addVariable(
895       transType(Ty), GV->isConstant(),
896       transLinkageType(GV),
897       Init ? transValue(Init, nullptr) : nullptr,
898       GV->getName(),
899       SPIRSPIRVAddrSpaceMap::map(
900         static_cast<SPIRAddressSpace>(Ty->getAddressSpace())),
901       nullptr
902       ));
903     mapValue(V, BVar);
904     spv::BuiltIn Builtin = spv::BuiltInPosition;
905     if (!GV->hasName() || !getSPIRVBuiltin(GV->getName().str(), Builtin))
906       return BVar;
907     BVar->setBuiltin(Builtin);
908     return BVar;
909   }
910 
911   if (isa<Constant>(V)) {
912     auto BV = transConstant(V);
913     assert(BV);
914     return mapValue(V, BV);
915   }
916 
917   if (auto Arg = dyn_cast<Argument>(V)) {
918     unsigned ArgNo = Arg->getArgNo();
919     SPIRVFunction *BF = BB->getParent();
920     //assert(BF->existArgument(ArgNo));
921     return mapValue(V, BF->getArgument(ArgNo));
922   }
923 
924   if (CreateForward)
925     return mapValue(V, BM->addForward(transType(V->getType())));
926 
927   if (StoreInst *ST = dyn_cast<StoreInst>(V)) {
928     std::vector<SPIRVWord> MemoryAccess(1,0);
929     if (ST->isVolatile())
930       MemoryAccess[0] |= MemoryAccessVolatileMask;
931     if (ST->getAlignment()) {
932       MemoryAccess[0] |= MemoryAccessAlignedMask;
933       MemoryAccess.push_back(ST->getAlignment());
934     }
935     if (ST->getMetadata(LLVMContext::MD_nontemporal))
936       MemoryAccess[0] |= MemoryAccessNontemporalMask;
937     if (MemoryAccess.front() == 0)
938       MemoryAccess.clear();
939     return mapValue(V, BM->addStoreInst(
940         transValue(ST->getPointerOperand(), BB),
941         transValue(ST->getValueOperand(), BB),
942         MemoryAccess, BB));
943   }
944 
945   if (LoadInst *LD = dyn_cast<LoadInst>(V)) {
946     std::vector<SPIRVWord> MemoryAccess(1,0);
947     if (LD->isVolatile())
948       MemoryAccess[0] |= MemoryAccessVolatileMask;
949     if (LD->getAlignment()) {
950       MemoryAccess[0] |= MemoryAccessAlignedMask;
951       MemoryAccess.push_back(LD->getAlignment());
952     }
953     if (LD->getMetadata(LLVMContext::MD_nontemporal))
954       MemoryAccess[0] |= MemoryAccessNontemporalMask;
955     if (MemoryAccess.front() == 0)
956       MemoryAccess.clear();
957     return mapValue(V, BM->addLoadInst(
958         transValue(LD->getPointerOperand(), BB),
959         MemoryAccess, BB));
960   }
961 
962   if (BinaryOperator *B = dyn_cast<BinaryOperator>(V)) {
963     SPIRVInstruction* BI = transBinaryInst(B, BB);
964     return mapValue(V, BI);
965   }
966 
967   if (auto RI = dyn_cast<ReturnInst>(V)) {
968     if (auto RV = RI->getReturnValue())
969       return mapValue(V, BM->addReturnValueInst(
970           transValue(RV, BB), BB));
971     return mapValue(V, BM->addReturnInst(BB));
972   }
973 
974   if (CmpInst *Cmp = dyn_cast<CmpInst>(V)) {
975     SPIRVInstruction* BI = transCmpInst(Cmp, BB);
976     return mapValue(V, BI);
977   }
978 
979   if (SelectInst *Sel = dyn_cast<SelectInst>(V))
980     return mapValue(V, BM->addSelectInst(
981         transValue(Sel->getCondition(), BB),
982         transValue(Sel->getTrueValue(), BB),
983         transValue(Sel->getFalseValue(), BB),BB));
984 
985   if (AllocaInst *Alc = dyn_cast<AllocaInst>(V))
986     return mapValue(V, BM->addVariable(
987       transType(Alc->getType()), false,
988       SPIRVLinkageTypeKind::LinkageTypeInternal,
989       nullptr, Alc->getName(),
990       StorageClassFunction, BB));
991 
992   if (auto *Switch = dyn_cast<SwitchInst>(V)) {
993     std::vector<std::pair<SPIRVWord, SPIRVBasicBlock *>> Pairs;
994     for (auto I = Switch->case_begin(), E = Switch->case_end(); I != E; ++I)
995       Pairs.push_back(std::make_pair(I.getCaseValue()->getZExtValue(),
996           static_cast<SPIRVBasicBlock*>(transValue(I.getCaseSuccessor(),
997               nullptr))));
998     return mapValue(V, BM->addSwitchInst(
999         transValue(Switch->getCondition(), BB),
1000         static_cast<SPIRVBasicBlock*>(transValue(Switch->getDefaultDest(),
1001             nullptr)), Pairs, BB));
1002   }
1003 
1004   if (auto Branch = dyn_cast<BranchInst>(V)) {
1005     if (Branch->isUnconditional())
1006       return mapValue(V, BM->addBranchInst(
1007           static_cast<SPIRVLabel*>(transValue(Branch->getSuccessor(0), BB)),
1008           BB));
1009     return mapValue(V, BM->addBranchConditionalInst(
1010         transValue(Branch->getCondition(), BB),
1011         static_cast<SPIRVLabel*>(transValue(Branch->getSuccessor(0), BB)),
1012         static_cast<SPIRVLabel*>(transValue(Branch->getSuccessor(1), BB)),
1013         BB));
1014   }
1015 
1016   if (auto Phi = dyn_cast<PHINode>(V)) {
1017     std::vector<SPIRVValue *> IncomingPairs;
1018     for (size_t I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1019       IncomingPairs.push_back(transValue(Phi->getIncomingValue(I), BB));
1020       IncomingPairs.push_back(transValue(Phi->getIncomingBlock(I), nullptr));
1021     }
1022     return mapValue(V, BM->addPhiInst(transType(Phi->getType()), IncomingPairs,
1023         BB));
1024   }
1025 
1026   if (auto Ext = dyn_cast<ExtractValueInst>(V)) {
1027     return mapValue(V, BM->addCompositeExtractInst(
1028         transType(Ext->getType()),
1029         transValue(Ext->getAggregateOperand(), BB),
1030         Ext->getIndices(), BB));
1031   }
1032 
1033   if (auto Ins = dyn_cast<InsertValueInst>(V)) {
1034     return mapValue(V, BM->addCompositeInsertInst(
1035         transValue(Ins->getInsertedValueOperand(), BB),
1036         transValue(Ins->getAggregateOperand(), BB),
1037         Ins->getIndices(), BB));
1038   }
1039 
1040   if (UnaryInstruction *U = dyn_cast<UnaryInstruction>(V)) {
1041     if (isSpecialTypeInitializer(U))
1042       return mapValue(V, transValue(U->getOperand(0), BB));
1043     return mapValue(V, transUnaryInst(U, BB));
1044   }
1045 
1046   if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
1047     std::vector<SPIRVValue *> Indices;
1048     for (unsigned i = 0, e = GEP->getNumIndices(); i != e; ++i)
1049       Indices.push_back(transValue(GEP->getOperand(i+1), BB));
1050     return mapValue(V, BM->addPtrAccessChainInst(
1051         transType(GEP->getType()),
1052         transValue(GEP->getPointerOperand(), BB),
1053         Indices, BB, GEP->isInBounds()));
1054   }
1055 
1056   if (auto Ext = dyn_cast<ExtractElementInst>(V)) {
1057     auto Index = Ext->getIndexOperand();
1058     if (auto Const = dyn_cast<ConstantInt>(Index))
1059       return mapValue(V, BM->addCompositeExtractInst(
1060         transType(Ext->getType()),
1061         transValue(Ext->getVectorOperand(), BB),
1062         std::vector<SPIRVWord>(1, Const->getZExtValue()),
1063         BB));
1064     else
1065       return mapValue(V, BM->addVectorExtractDynamicInst(
1066           transValue(Ext->getVectorOperand(), BB),
1067           transValue(Index, BB),
1068           BB));
1069   }
1070 
1071   if (auto Ins = dyn_cast<InsertElementInst>(V)) {
1072     auto Index = Ins->getOperand(2);
1073     if (auto Const = dyn_cast<ConstantInt>(Index))
1074       return mapValue(V, BM->addCompositeInsertInst(
1075       transValue(Ins->getOperand(1), BB),
1076       transValue(Ins->getOperand(0), BB),
1077       std::vector<SPIRVWord>(1, Const->getZExtValue()),
1078       BB));
1079     else
1080       return mapValue(V, BM->addVectorInsertDynamicInst(
1081       transValue(Ins->getOperand(0), BB),
1082       transValue(Ins->getOperand(1), BB),
1083       transValue(Index, BB),
1084       BB));
1085   }
1086 
1087   if (auto SF = dyn_cast<ShuffleVectorInst>(V)) {
1088     std::vector<SPIRVWord> Comp;
1089     for (auto &I:SF->getShuffleMask())
1090       Comp.push_back(I);
1091     return mapValue(V, BM->addVectorShuffleInst(
1092         transType(SF->getType()),
1093         transValue(SF->getOperand(0), BB),
1094         transValue(SF->getOperand(1), BB),
1095         Comp,
1096         BB));
1097   }
1098 
1099   if (CallInst *CI = dyn_cast<CallInst>(V))
1100     return mapValue(V, transCallInst(CI, BB));
1101 
1102   llvm_unreachable("Not implemented");
1103   return nullptr;
1104 }
1105 
1106 bool
transDecoration(Value * V,SPIRVValue * BV)1107 LLVMToSPIRV::transDecoration(Value *V, SPIRVValue *BV) {
1108   if (!transAlign(V, BV))
1109     return false;
1110   if ((isa<AtomicCmpXchgInst>(V) &&
1111       cast<AtomicCmpXchgInst>(V)->isVolatile()) ||
1112       (isa<AtomicRMWInst>(V) && cast<AtomicRMWInst>(V)->isVolatile()))
1113     BV->setVolatile(true);
1114   DbgTran.transDbgInfo(V, BV);
1115   return true;
1116 }
1117 
1118 bool
transAlign(Value * V,SPIRVValue * BV)1119 LLVMToSPIRV::transAlign(Value *V, SPIRVValue *BV) {
1120   if (auto AL = dyn_cast<AllocaInst>(V)) {
1121     BM->setAlignment(BV, AL->getAlignment());
1122     return true;
1123   }
1124   if (auto GV = dyn_cast<GlobalVariable>(V)) {
1125     BM->setAlignment(BV, GV->getAlignment());
1126     return true;
1127   }
1128   return true;
1129 }
1130 
1131 /// Do this after source language is set.
1132 bool
transBuiltinSet()1133 LLVMToSPIRV::transBuiltinSet() {
1134   SPIRVWord Ver = 0;
1135   SourceLanguage Kind = BM->getSourceLanguage(&Ver);
1136   (void) Kind;
1137   assert((Kind == SourceLanguageOpenCL_C ||
1138       Kind == SourceLanguageOpenCL_CPP ) && "not supported");
1139   std::stringstream SS;
1140   SS << "OpenCL.std";
1141   return BM->importBuiltinSet(SS.str(), &ExtSetId);
1142 }
1143 
1144 /// Transform sampler* spcv.cast(i32 arg)
1145 /// Only two cases are possible:
1146 ///   arg = ConstantInt x -> SPIRVConstantSampler
1147 ///   arg = i32 argument -> transValue(arg)
1148 ///   arg = load from sampler -> look through load
1149 SPIRVValue *
oclTransSpvcCastSampler(CallInst * CI,SPIRVBasicBlock * BB)1150 LLVMToSPIRV::oclTransSpvcCastSampler(CallInst* CI, SPIRVBasicBlock *BB) {
1151   llvm::Function* F = CI->getCalledFunction();
1152   auto FT = F->getFunctionType();
1153   auto RT = FT->getReturnType();
1154   assert(FT->getNumParams() == 1);
1155   assert(isSPIRVType(RT, kSPIRVTypeName::Sampler) &&
1156     FT->getParamType(0)->isIntegerTy() && "Invalid sampler type");
1157   auto Arg = CI->getArgOperand(0);
1158 
1159   auto GetSamplerConstant = [&](uint64_t SamplerValue) {
1160     auto AddrMode = (SamplerValue & 0xE) >> 1;
1161     auto Param = SamplerValue & 0x1;
1162     auto Filter = ((SamplerValue & 0x30) >> 4) - 1;
1163     auto BV = BM->addSamplerConstant(transType(RT), AddrMode, Param, Filter);
1164     return BV;
1165   };
1166 
1167   if (auto Const = dyn_cast<ConstantInt>(Arg)) {
1168     // Sampler is declared as a kernel scope constant
1169     return GetSamplerConstant(Const->getZExtValue());
1170   } else if (auto Load = dyn_cast<LoadInst>(Arg)) {
1171     // If value of the sampler is loaded from a global constant, use its
1172     // initializer for initialization of the sampler.
1173     auto Op = Load->getPointerOperand();
1174     assert(isa<GlobalVariable>(Op) && "Unknown sampler pattern!");
1175     auto GV = cast<GlobalVariable>(Op);
1176     assert(GV->isConstant() ||
1177       GV->getType()->getPointerAddressSpace() == SPIRAS_Constant);
1178     auto Initializer = GV->getInitializer();
1179     assert(isa<ConstantInt>(Initializer) && "sampler not constant int?");
1180     return GetSamplerConstant(cast<ConstantInt>(Initializer)->getZExtValue());
1181   }
1182   // Sampler is a function argument
1183   auto BV = transValue(Arg, BB);
1184   assert(BV && BV->getType() == transType(RT));
1185   return BV;
1186 }
1187 
1188 SPIRVValue *
transSpcvCast(CallInst * CI,SPIRVBasicBlock * BB)1189 LLVMToSPIRV::transSpcvCast(CallInst* CI, SPIRVBasicBlock *BB) {
1190   return oclTransSpvcCastSampler(CI, BB);
1191 }
1192 
1193 SPIRVValue *
transCallInst(CallInst * CI,SPIRVBasicBlock * BB)1194 LLVMToSPIRV::transCallInst(CallInst *CI, SPIRVBasicBlock *BB) {
1195   SPIRVExtInstSetKind ExtSetKind = SPIRVEIS_Count;
1196   SPIRVWord ExtOp = SPIRVWORD_MAX;
1197   llvm::Function* F = CI->getCalledFunction();
1198   auto MangledName = F->getName();
1199   std::string DemangledName;
1200 
1201   if (MangledName.startswith(SPCV_CAST))
1202     return transSpcvCast(CI, BB);
1203 
1204   if (MangledName.startswith("llvm.memcpy")) {
1205     std::vector<SPIRVWord> MemoryAccess;
1206 
1207     if (isa<ConstantInt>(CI->getOperand(4)) &&
1208       dyn_cast<ConstantInt>(CI->getOperand(4))
1209       ->getZExtValue() == 1)
1210       MemoryAccess.push_back(MemoryAccessVolatileMask);
1211     if (isa<ConstantInt>(CI->getOperand(3))) {
1212         MemoryAccess.push_back(MemoryAccessAlignedMask);
1213         MemoryAccess.push_back(dyn_cast<ConstantInt>(CI->getOperand(3))
1214           ->getZExtValue());
1215     }
1216 
1217     return BM->addCopyMemorySizedInst(
1218       transValue(CI->getOperand(0), BB),
1219       transValue(CI->getOperand(1), BB),
1220       transValue(CI->getOperand(2), BB),
1221       MemoryAccess,
1222       BB);
1223   }
1224 
1225   if (oclIsBuiltin(MangledName, &DemangledName) ||
1226       isDecoratedSPIRVFunc(F, &DemangledName))
1227     if (auto BV = transBuiltinToInst(DemangledName, MangledName, CI, BB))
1228       return BV;
1229 
1230   SmallVector<std::string, 2> Dec;
1231   if (isBuiltinTransToExtInst(CI->getCalledFunction(), &ExtSetKind,
1232       &ExtOp, &Dec))
1233     return addDecorations(BM->addExtInst(
1234       transType(CI->getType()),
1235       ExtSetId,
1236       ExtOp,
1237       transArguments(CI, BB, SPIRVEntry::create_unique(ExtSetKind, ExtOp).get()),
1238       BB), Dec);
1239 
1240   return BM->addCallInst(
1241     transFunctionDecl(CI->getCalledFunction()),
1242     transArguments(CI, BB, SPIRVEntry::create_unique(OpFunctionCall).get()),
1243     BB);
1244 }
1245 
1246 bool
transAddressingMode()1247 LLVMToSPIRV::transAddressingMode() {
1248   Triple TargetTriple(M->getTargetTriple());
1249   Triple::ArchType Arch = TargetTriple.getArch();
1250 
1251   SPIRVCKRT(Arch == Triple::spir || Arch == Triple::spir64,
1252       InvalidTargetTriple,
1253       "Actual target triple is " + M->getTargetTriple());
1254 
1255   if (Arch == Triple::spir)
1256     BM->setAddressingModel(AddressingModelPhysical32);
1257   else
1258     BM->setAddressingModel(AddressingModelPhysical64);
1259   // Physical addressing model requires Addresses capability
1260   BM->addCapability(CapabilityAddresses);
1261   return true;
1262 }
1263 std::vector<SPIRVValue*>
transValue(const std::vector<Value * > & Args,SPIRVBasicBlock * BB)1264 LLVMToSPIRV::transValue(const std::vector<Value *> &Args, SPIRVBasicBlock* BB) {
1265   std::vector<SPIRVValue*> BArgs;
1266   for (auto &I: Args)
1267     BArgs.push_back(transValue(I, BB));
1268   return BArgs;
1269 }
1270 
1271 std::vector<SPIRVValue*>
transArguments(CallInst * CI,SPIRVBasicBlock * BB)1272 LLVMToSPIRV::transArguments(CallInst *CI, SPIRVBasicBlock *BB) {
1273   return transValue(getArguments(CI), BB);
1274 }
1275 
1276 std::vector<SPIRVWord>
transValue(const std::vector<Value * > & Args,SPIRVBasicBlock * BB,SPIRVEntry * Entry)1277 LLVMToSPIRV::transValue(const std::vector<Value *> &Args, SPIRVBasicBlock* BB,
1278     SPIRVEntry *Entry) {
1279   std::vector<SPIRVWord> Operands;
1280   for (size_t I = 0, E = Args.size(); I != E; ++I) {
1281     Operands.push_back(Entry->isOperandLiteral(I) ?
1282         cast<ConstantInt>(Args[I])->getZExtValue() :
1283         transValue(Args[I], BB)->getId());
1284   }
1285   return Operands;
1286 }
1287 
1288 std::vector<SPIRVWord>
transArguments(CallInst * CI,SPIRVBasicBlock * BB,SPIRVEntry * Entry)1289 LLVMToSPIRV::transArguments(CallInst *CI, SPIRVBasicBlock *BB, SPIRVEntry *Entry) {
1290   return transValue(getArguments(CI), BB, Entry);
1291 }
1292 
1293 SPIRVWord
transFunctionControlMask(CallInst * CI)1294 LLVMToSPIRV::transFunctionControlMask(CallInst *CI) {
1295   SPIRVWord FCM = 0;
1296   SPIRSPIRVFuncCtlMaskMap::foreach([&](Attribute::AttrKind Attr,
1297       SPIRVFunctionControlMaskKind Mask){
1298     if (CI->hasFnAttr(Attr))
1299       FCM |= Mask;
1300   });
1301   return FCM;
1302 }
1303 
1304 SPIRVWord
transFunctionControlMask(Function * F)1305 LLVMToSPIRV::transFunctionControlMask(Function *F) {
1306   SPIRVWord FCM = 0;
1307   SPIRSPIRVFuncCtlMaskMap::foreach([&](Attribute::AttrKind Attr,
1308       SPIRVFunctionControlMaskKind Mask){
1309     if (F->hasFnAttribute(Attr))
1310       FCM |= Mask;
1311   });
1312   return FCM;
1313 }
1314 
1315 bool
transGlobalVariables()1316 LLVMToSPIRV::transGlobalVariables() {
1317   for (auto I = M->global_begin(),
1318             E = M->global_end(); I != E; ++I) {
1319     if (!transValue(static_cast<GlobalVariable*>(I), nullptr))
1320       return false;
1321   }
1322   return true;
1323 }
1324 
1325 void
mutateFuncArgType(const std::map<unsigned,Type * > & ChangedType,Function * F)1326 LLVMToSPIRV::mutateFuncArgType(const std::map<unsigned, Type*>& ChangedType,
1327     Function* F) {
1328   for (auto &I : ChangedType) {
1329     for (auto UI = F->user_begin(), UE = F->user_end(); UI != UE; ++UI) {
1330       auto Call = dyn_cast<CallInst>(*UI);
1331       if (!Call)
1332         continue;
1333       auto Arg = Call->getArgOperand(I.first);
1334       auto OrigTy = Arg->getType();
1335       if (OrigTy == I.second)
1336         continue;
1337       SPIRVDBG(dbgs() << "[mutate arg type] " << *Call << ", " << *Arg << '\n');
1338       auto CastF = M->getOrInsertFunction(SPCV_CAST, I.second, OrigTy, nullptr);
1339       std::vector<Value *> Args;
1340       Args.push_back(Arg);
1341       auto Cast = CallInst::Create(CastF, Args, "", Call);
1342       Call->replaceUsesOfWith(Arg, Cast);
1343       SPIRVDBG(dbgs() << "[mutate arg type] -> " << *Cast << '\n');
1344     }
1345   }
1346 }
1347 
1348 void
transFunction(Function * I)1349 LLVMToSPIRV::transFunction(Function *I) {
1350   transFunctionDecl(I);
1351   // Creating all basic blocks before creating any instruction.
1352   for (Function::iterator FI = I->begin(), FE = I->end(); FI != FE; ++FI) {
1353     transValue(static_cast<BasicBlock*>(FI), nullptr);
1354   }
1355   for (Function::iterator FI = I->begin(), FE = I->end(); FI != FE; ++FI) {
1356     SPIRVBasicBlock* BB = static_cast<SPIRVBasicBlock*>(transValue(static_cast<BasicBlock*>(FI), nullptr));
1357     for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1358         ++BI) {
1359       transValue(static_cast<Instruction*>(BI), BB, false);
1360     }
1361   }
1362 }
1363 
1364 bool
translate()1365 LLVMToSPIRV::translate() {
1366   BM->setGeneratorVer(kTranslatorVer);
1367 
1368   if (!transSourceLanguage())
1369     return false;
1370   if (!transExtension())
1371     return false;
1372   if (!transBuiltinSet())
1373     return false;
1374   if (!transAddressingMode())
1375     return false;
1376   if (!transGlobalVariables())
1377     return false;
1378 
1379   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) {
1380     Function *F = static_cast<Function*>(I);
1381     auto FT = F->getFunctionType();
1382     std::map<unsigned, Type *> ChangedType;
1383     oclGetMutatedArgumentTypesByBuiltin(FT, ChangedType, F);
1384     mutateFuncArgType(ChangedType, F);
1385   }
1386 
1387   // SPIR-V logical layout requires all function declarations go before
1388   // function definitions.
1389   std::vector<Function *> Decls, Defs;
1390   for (Module::iterator I1 = M->begin(), E = M->end(); I1 != E; ++I1) {
1391     auto I = static_cast<Function*>(I1);
1392     if (isBuiltinTransToInst(I) || isBuiltinTransToExtInst(I)
1393         || I->getName().startswith(SPCV_CAST) ||
1394         I->getName().startswith(LLVM_MEMCPY))
1395       continue;
1396     if (I->isDeclaration())
1397       Decls.push_back(I);
1398     else
1399       Defs.push_back(I);
1400   }
1401   for (auto I:Decls)
1402     transFunctionDecl(I);
1403   for (auto I:Defs)
1404     transFunction(I);
1405 
1406   if (!transOCLKernelMetadata())
1407     return false;
1408   if (!transExecutionMode())
1409     return false;
1410 
1411   BM->optimizeDecorates();
1412   BM->resolveUnknownStructFields();
1413   BM->createForwardPointers();
1414   return true;
1415 }
1416 
getSizetType()1417 llvm::IntegerType* LLVMToSPIRV::getSizetType() {
1418   return IntegerType::getIntNTy(M->getContext(),
1419     M->getDataLayout().getPointerSizeInBits());
1420 }
1421 
1422 void
oclGetMutatedArgumentTypesByBuiltin(llvm::FunctionType * FT,std::map<unsigned,Type * > & ChangedType,Function * F)1423 LLVMToSPIRV::oclGetMutatedArgumentTypesByBuiltin(
1424     llvm::FunctionType* FT, std::map<unsigned, Type*>& ChangedType,
1425     Function* F) {
1426   auto Name = F->getName();
1427   std::string Demangled;
1428   if (!oclIsBuiltin(Name, &Demangled))
1429     return;
1430   if (Demangled.find(kSPIRVName::SampledImage) == std::string::npos)
1431     return;
1432   if (FT->getParamType(1)->isIntegerTy())
1433     ChangedType[1] = getSamplerType(F->getParent());
1434 }
1435 
1436 SPIRVInstruction *
transBuiltinToInst(const std::string & DemangledName,const std::string & MangledName,CallInst * CI,SPIRVBasicBlock * BB)1437 LLVMToSPIRV::transBuiltinToInst(const std::string& DemangledName,
1438     const std::string &MangledName, CallInst* CI, SPIRVBasicBlock* BB) {
1439   SmallVector<std::string, 2> Dec;
1440   auto OC = getSPIRVFuncOC(DemangledName, &Dec);
1441 
1442   if (OC == OpNop)
1443     return nullptr;
1444 
1445   auto Inst = transBuiltinToInstWithoutDecoration(OC, CI, BB);
1446   addDecorations(Inst, Dec);
1447   return Inst;
1448 }
1449 
1450 bool
transExecutionMode()1451 LLVMToSPIRV::transExecutionMode() {
1452   if (auto NMD = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::ExecutionMode)) {
1453     while (!NMD.atEnd()) {
1454       unsigned EMode = ~0U;
1455       Function *F = nullptr;
1456       auto N = NMD.nextOp(); /* execution mode MDNode */
1457       N.get(F).get(EMode);
1458 
1459       SPIRVFunction *BF = static_cast<SPIRVFunction *>(getTranslatedValue(F));
1460       assert(BF && "Invalid kernel function");
1461       if (!BF)
1462         return false;
1463 
1464       switch (EMode) {
1465       case spv::ExecutionModeContractionOff:
1466       case spv::ExecutionModeInitializer:
1467       case spv::ExecutionModeFinalizer:
1468         BF->addExecutionMode(new SPIRVExecutionMode(BF,
1469             static_cast<ExecutionMode>(EMode)));
1470         break;
1471       case spv::ExecutionModeLocalSize:
1472       case spv::ExecutionModeLocalSizeHint: {
1473         unsigned X, Y, Z;
1474         N.get(X).get(Y).get(Z);
1475         BF->addExecutionMode(new SPIRVExecutionMode(BF,
1476             static_cast<ExecutionMode>(EMode), X, Y, Z));
1477       }
1478       break;
1479       case spv::ExecutionModeVecTypeHint:
1480       case spv::ExecutionModeSubgroupSize:
1481       case spv::ExecutionModeSubgroupsPerWorkgroup: {
1482         unsigned X;
1483         N.get(X);
1484         BF->addExecutionMode(new SPIRVExecutionMode(BF,
1485             static_cast<ExecutionMode>(EMode), X));
1486       }
1487       break;
1488       default:
1489         llvm_unreachable("invalid execution mode");
1490       }
1491     }
1492   }
1493   return true;
1494 }
1495 
1496 bool
transOCLKernelMetadata()1497 LLVMToSPIRV::transOCLKernelMetadata() {
1498   NamedMDNode *KernelMDs = M->getNamedMetadata(SPIR_MD_KERNELS);
1499   std::vector<std::string> argAccessQual;
1500   if (!KernelMDs)
1501     return true;
1502 
1503   for (unsigned I = 0, E = KernelMDs->getNumOperands(); I < E; ++I) {
1504     MDNode *KernelMD = KernelMDs->getOperand(I);
1505     if (KernelMD->getNumOperands() == 0)
1506       continue;
1507     Function *Kernel = mdconst::dyn_extract<Function>(KernelMD->getOperand(0));
1508 
1509     SPIRVFunction *BF = static_cast<SPIRVFunction *>(getTranslatedValue(Kernel));
1510     assert(BF && "Kernel function should be translated first");
1511     assert(Kernel && oclIsKernel(Kernel)
1512             && "Invalid kernel calling convention or metadata");
1513     for (unsigned MI = 1, ME = KernelMD->getNumOperands(); MI < ME; ++MI) {
1514       MDNode *MD = dyn_cast<MDNode>(KernelMD->getOperand(MI));
1515       if (!MD)
1516         continue;
1517       MDString *NameMD = dyn_cast<MDString>(MD->getOperand(0));
1518       if (!NameMD)
1519         continue;
1520       StringRef Name = NameMD->getString();
1521       if (Name == SPIR_MD_KERNEL_ARG_TYPE_QUAL) {
1522         foreachKernelArgMD(MD, BF,
1523             [](const std::string &Str, SPIRVFunctionParameter *BA){
1524           if (Str.find("volatile") != std::string::npos)
1525             BA->addDecorate(new SPIRVDecorate(DecorationVolatile, BA));
1526           if (Str.find("restrict") != std::string::npos)
1527             BA->addDecorate(new SPIRVDecorate(DecorationFuncParamAttr,
1528                 BA, FunctionParameterAttributeNoAlias));
1529           if (Str.find("const") != std::string::npos)
1530             BA->addDecorate(new SPIRVDecorate(DecorationFuncParamAttr,
1531                 BA, FunctionParameterAttributeNoWrite));
1532           });
1533       } else if (Name == SPIR_MD_KERNEL_ARG_NAME) {
1534         foreachKernelArgMD(MD, BF,
1535             [=](const std::string &Str, SPIRVFunctionParameter *BA){
1536             BM->setName(BA, Str);
1537           });
1538       }
1539     }
1540   }
1541   return true;
1542 }
1543 
1544 bool
transSourceLanguage()1545 LLVMToSPIRV::transSourceLanguage() {
1546   auto Src = getSPIRVSource(M);
1547   SrcLang = std::get<0>(Src);
1548   SrcLangVer = std::get<1>(Src);
1549   BM->setSourceLanguage(static_cast<SourceLanguage>(SrcLang), SrcLangVer);
1550   return true;
1551 }
1552 
1553 bool
transExtension()1554 LLVMToSPIRV::transExtension() {
1555   if (auto N = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::Extension)) {
1556     while (!N.atEnd()) {
1557       std::string S;
1558       N.nextOp().get(S);
1559       assert(!S.empty() && "Invalid extension");
1560       BM->getExtension().insert(S);
1561     }
1562   }
1563   if (auto N = SPIRVMDWalker(*M).getNamedMD(kSPIRVMD::SourceExtension)) {
1564     while (!N.atEnd()) {
1565       std::string S;
1566       N.nextOp().get(S);
1567       assert(!S.empty() && "Invalid extension");
1568       BM->getSourceExtension().insert(S);
1569     }
1570   }
1571   for (auto &I:map<SPIRVCapabilityKind>(rmap<OclExt::Kind>(BM->getExtension())))
1572     BM->addCapability(I);
1573 
1574   return true;
1575 }
1576 
1577 void
dumpUsers(Value * V)1578 LLVMToSPIRV::dumpUsers(Value* V) {
1579   SPIRVDBG(dbgs() << "Users of " << *V << " :\n");
1580   for (auto UI = V->user_begin(), UE = V->user_end();
1581       UI != UE; ++UI)
1582     SPIRVDBG(dbgs() << "  " << **UI << '\n');
1583 }
1584 
1585 Op
transBoolOpCode(SPIRVValue * Opn,Op OC)1586 LLVMToSPIRV::transBoolOpCode(SPIRVValue* Opn, Op OC) {
1587   if (!Opn->getType()->isTypeVectorOrScalarBool())
1588     return OC;
1589   IntBoolOpMap::find(OC, &OC);
1590   return OC;
1591 }
1592 
1593 SPIRVInstruction *
transBuiltinToInstWithoutDecoration(Op OC,CallInst * CI,SPIRVBasicBlock * BB)1594 LLVMToSPIRV::transBuiltinToInstWithoutDecoration(Op OC,
1595     CallInst* CI, SPIRVBasicBlock* BB) {
1596   if (isGroupOpCode(OC))
1597     BM->addCapability(CapabilityGroups);
1598   switch (OC) {
1599   case OpControlBarrier: {
1600     auto BArgs = transValue(getArguments(CI), BB);
1601     return BM->addControlBarrierInst(
1602       BArgs[0], BArgs[1], BArgs[2], BB);
1603     }
1604     break;
1605   case OpGroupAsyncCopy: {
1606     auto BArgs = transValue(getArguments(CI), BB);
1607     return BM->addAsyncGroupCopy(BArgs[0], BArgs[1], BArgs[2], BArgs[3],
1608                                  BArgs[4], BArgs[5], BB);
1609     }
1610     break;
1611   default: {
1612     if (isCvtOpCode(OC) && OC != OpGenericCastToPtrExplicit) {
1613       return BM->addUnaryInst(OC, transType(CI->getType()),
1614         transValue(CI->getArgOperand(0), BB), BB);
1615     } else if (isCmpOpCode(OC)) {
1616       assert(CI && CI->getNumArgOperands() == 2 && "Invalid call inst");
1617       auto ResultTy = CI->getType();
1618       Type *BoolTy = IntegerType::getInt1Ty(M->getContext());
1619       auto IsVector = ResultTy->isVectorTy();
1620       if (IsVector)
1621         BoolTy = VectorType::get(BoolTy, ResultTy->getVectorNumElements());
1622       auto BBT = transType(BoolTy);
1623       auto Cmp = BM->addCmpInst(OC, BBT,
1624         transValue(CI->getArgOperand(0), BB),
1625         transValue(CI->getArgOperand(1), BB), BB);
1626       auto Zero = transValue(Constant::getNullValue(ResultTy), BB);
1627       auto One = transValue(
1628           IsVector ? Constant::getAllOnesValue(ResultTy) : getInt32(M, 1), BB);
1629       return BM->addSelectInst(Cmp, One, Zero, BB);
1630     } else if (isBinaryOpCode(OC)) {
1631       assert(CI && CI->getNumArgOperands() == 2 && "Invalid call inst");
1632       return BM->addBinaryInst(OC, transType(CI->getType()),
1633         transValue(CI->getArgOperand(0), BB),
1634         transValue(CI->getArgOperand(1), BB), BB);
1635     } else if (CI->getNumArgOperands() == 1 &&
1636         !CI->getType()->isVoidTy() &&
1637         !hasExecScope(OC) &&
1638         !isAtomicOpCode(OC)) {
1639       return BM->addUnaryInst(OC, transType(CI->getType()),
1640         transValue(CI->getArgOperand(0), BB), BB);
1641     } else {
1642       auto Args = getArguments(CI);
1643       SPIRVType *SPRetTy = nullptr;
1644       Type *RetTy = CI->getType();
1645       auto F = CI->getCalledFunction();
1646       if (!RetTy->isVoidTy()) {
1647         SPRetTy = transType(RetTy);
1648       } else if (Args.size() > 0 && F->arg_begin()->hasStructRetAttr()) {
1649         SPRetTy = transType(F->arg_begin()->getType()->getPointerElementType());
1650         Args.erase(Args.begin());
1651       }
1652       auto SPI = BM->addInstTemplate(OC, BB, SPRetTy);
1653       std::vector<SPIRVWord> SPArgs;
1654       for (size_t I = 0, E = Args.size(); I != E; ++I) {
1655         assert((!isFunctionPointerType(Args[I]->getType()) ||
1656                isa<Function>(Args[I])) &&
1657                "Invalid function pointer argument");
1658         SPArgs.push_back(SPI->isOperandLiteral(I) ?
1659             cast<ConstantInt>(Args[I])->getZExtValue() :
1660             transValue(Args[I], BB)->getId());
1661       }
1662       SPI->setOpWordsAndValidate(SPArgs);
1663       if (!SPRetTy || !SPRetTy->isTypeStruct())
1664         return SPI;
1665       std::vector<SPIRVWord> Mem;
1666       SPIRVDBG(spvdbgs() << *SPI << '\n');
1667       return BM->addStoreInst(transValue(CI->getArgOperand(0), BB), SPI,
1668           Mem, BB);
1669     }
1670   }
1671   }
1672   return nullptr;
1673 }
1674 
1675 
1676 SPIRVId
addInt32(int I)1677 LLVMToSPIRV::addInt32(int I) {
1678   return transValue(getInt32(M, I), nullptr, false)->getId();
1679 }
1680 
1681 SPIRV::SPIRVLinkageTypeKind
transLinkageType(const GlobalValue * GV)1682 LLVMToSPIRV::transLinkageType(const GlobalValue* GV) {
1683   if(GV->isDeclarationForLinker())
1684     return SPIRVLinkageTypeKind::LinkageTypeImport;
1685   if(GV->hasInternalLinkage() || GV->hasPrivateLinkage())
1686     return SPIRVLinkageTypeKind::LinkageTypeInternal;
1687   return SPIRVLinkageTypeKind::LinkageTypeExport;
1688 }
1689 } // end of SPIRV namespace
1690 
1691 char LLVMToSPIRV::ID = 0;
1692 
1693 INITIALIZE_PASS_BEGIN(LLVMToSPIRV, "llvmtospv", "Translate LLVM to SPIR-V",
1694     false, false)
INITIALIZE_PASS_DEPENDENCY(OCLTypeToSPIRV)1695 INITIALIZE_PASS_DEPENDENCY(OCLTypeToSPIRV)
1696 INITIALIZE_PASS_END(LLVMToSPIRV, "llvmtospv", "Translate LLVM to SPIR-V",
1697     false, false)
1698 
1699 ModulePass *llvm::createLLVMToSPIRV(SPIRVModule *SMod) {
1700   return new LLVMToSPIRV(SMod);
1701 }
1702 
1703 void
addPassesForSPIRV(legacy::PassManager & PassMgr)1704 addPassesForSPIRV(legacy::PassManager &PassMgr) {
1705   if (SPIRVMemToReg)
1706     PassMgr.add(createPromoteMemoryToRegisterPass());
1707   PassMgr.add(createTransOCLMD());
1708   PassMgr.add(createOCL21ToSPIRV());
1709   PassMgr.add(createSPIRVLowerOCLBlocks());
1710   PassMgr.add(createOCLTypeToSPIRV());
1711   PassMgr.add(createOCL20ToSPIRV());
1712   PassMgr.add(createSPIRVRegularizeLLVM());
1713   PassMgr.add(createSPIRVLowerConstExpr());
1714   PassMgr.add(createSPIRVLowerBool());
1715 }
1716 
1717 bool
WriteSPIRV(Module * M,llvm::raw_ostream & OS,std::string & ErrMsg)1718 llvm::WriteSPIRV(Module *M, llvm::raw_ostream &OS, std::string &ErrMsg) {
1719   std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
1720   legacy::PassManager PassMgr;
1721   addPassesForSPIRV(PassMgr);
1722   PassMgr.add(createLLVMToSPIRV(BM.get()));
1723   PassMgr.run(*M);
1724 
1725   if (BM->getError(ErrMsg) != SPIRVEC_Success)
1726     return false;
1727   OS << *BM;
1728   return true;
1729 }
1730 
1731 bool
RegularizeLLVMForSPIRV(Module * M,std::string & ErrMsg)1732 llvm::RegularizeLLVMForSPIRV(Module *M, std::string &ErrMsg) {
1733   std::unique_ptr<SPIRVModule> BM(SPIRVModule::createSPIRVModule());
1734   legacy::PassManager PassMgr;
1735   addPassesForSPIRV(PassMgr);
1736   PassMgr.run(*M);
1737   return true;
1738 }
1739