blob: ac62480362d2da60420ceae5e9d3075db08725a0 [file] [log] [blame]
David Neto22f144c2017-06-12 14:26:21 -04001// Copyright 2017 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifdef _MSC_VER
16#pragma warning(push, 0)
17#endif
18
David Neto156783e2017-07-05 15:39:41 -040019#include <cassert>
David Neto22f144c2017-06-12 14:26:21 -040020#include <clspv/Passes.h>
21
22#include <llvm/ADT/StringSwitch.h>
23#include <llvm/ADT/UniqueVector.h>
24#include <llvm/Analysis/LoopInfo.h>
25#include <llvm/IR/Constants.h>
26#include <llvm/IR/Dominators.h>
27#include <llvm/IR/Instructions.h>
28#include <llvm/IR/Metadata.h>
29#include <llvm/IR/Module.h>
30#include <llvm/Pass.h>
31#include <llvm/Support/raw_ostream.h>
32#include <llvm/Transforms/Utils/Cloning.h>
33
34#include <spirv/1.0/spirv.hpp>
35#include <clspv/AddressSpace.h>
36#include <clspv/spirv_c_strings.hpp>
37#include <clspv/spirv_glsl.hpp>
38
39#include <list>
David Neto0676e6f2017-07-11 18:47:44 -040040#include <iomanip>
41#include <sstream>
David Neto44795152017-07-13 15:45:28 -040042#include <utility>
David Neto22f144c2017-06-12 14:26:21 -040043
44#if defined(_MSC_VER)
45#pragma warning(pop)
46#endif
47
48using namespace llvm;
49using namespace clspv;
David Neto156783e2017-07-05 15:39:41 -040050using namespace mdconst;
David Neto22f144c2017-06-12 14:26:21 -040051
52namespace {
53enum SPIRVOperandType {
54 NUMBERID,
55 LITERAL_INTEGER,
56 LITERAL_STRING,
57 LITERAL_FLOAT
58};
59
60struct SPIRVOperand {
61 explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num)
62 : Type(Ty), LiteralNum(1, Num) {}
63 explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str)
64 : Type(Ty), LiteralStr(Str) {}
65 explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str)
66 : Type(Ty), LiteralStr(Str) {}
67 explicit SPIRVOperand(SPIRVOperandType Ty, ArrayRef<uint32_t> NumVec)
68 : Type(Ty), LiteralNum(NumVec.begin(), NumVec.end()) {}
69
70 SPIRVOperandType getType() { return Type; };
71 uint32_t getNumID() { return LiteralNum[0]; };
72 std::string getLiteralStr() { return LiteralStr; };
73 ArrayRef<uint32_t> getLiteralNum() { return LiteralNum; };
74
75private:
76 SPIRVOperandType Type;
77 std::string LiteralStr;
78 SmallVector<uint32_t, 4> LiteralNum;
79};
80
81struct SPIRVInstruction {
82 explicit SPIRVInstruction(uint16_t WCount, spv::Op Opc, uint32_t ResID,
83 ArrayRef<SPIRVOperand *> Ops)
84 : WordCount(WCount), Opcode(static_cast<uint16_t>(Opc)), ResultID(ResID),
85 Operands(Ops.begin(), Ops.end()) {}
86
87 uint16_t getWordCount() const { return WordCount; }
88 uint16_t getOpcode() const { return Opcode; }
89 uint32_t getResultID() const { return ResultID; }
90 ArrayRef<SPIRVOperand *> getOperands() const { return Operands; }
91
92private:
93 uint16_t WordCount;
94 uint16_t Opcode;
95 uint32_t ResultID;
96 SmallVector<SPIRVOperand *, 4> Operands;
97};
98
99struct SPIRVProducerPass final : public ModulePass {
100 typedef std::vector<SPIRVOperand *> SPIRVOperandList;
101 typedef DenseMap<Type *, uint32_t> TypeMapType;
102 typedef UniqueVector<Type *> TypeList;
103 typedef DenseMap<Value *, uint32_t> ValueMapType;
104 typedef std::vector<Value *> ValueList;
105 typedef std::vector<std::pair<Value *, uint32_t>> EntryPointVecType;
106 typedef std::list<SPIRVInstruction *> SPIRVInstructionList;
107 typedef std::vector<
108 std::tuple<Value *, SPIRVInstructionList::iterator, uint32_t>>
109 DeferredInstVecType;
110 typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>>
111 GlobalConstFuncMapType;
112
David Neto44795152017-07-13 15:45:28 -0400113 explicit SPIRVProducerPass(
114 raw_pwrite_stream &out, raw_ostream &descriptor_map_out,
115 ArrayRef<std::pair<unsigned, std::string>> samplerMap, bool outputAsm,
116 bool outputCInitList)
David Netoc2c368d2017-06-30 16:50:17 -0400117 : ModulePass(ID), samplerMap(samplerMap), out(out),
David Neto0676e6f2017-07-11 18:47:44 -0400118 binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out),
David Netoc2c368d2017-06-30 16:50:17 -0400119 descriptorMapOut(descriptor_map_out), outputAsm(outputAsm),
David Neto0676e6f2017-07-11 18:47:44 -0400120 outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1),
121 OpExtInstImportID(0), HasVariablePointers(false), SamplerTy(nullptr) {}
David Neto22f144c2017-06-12 14:26:21 -0400122
123 void getAnalysisUsage(AnalysisUsage &AU) const override {
124 AU.addRequired<DominatorTreeWrapperPass>();
125 AU.addRequired<LoopInfoWrapperPass>();
126 }
127
128 virtual bool runOnModule(Module &module) override;
129
130 // output the SPIR-V header block
131 void outputHeader();
132
133 // patch the SPIR-V header block
134 void patchHeader();
135
136 uint32_t lookupType(Type *Ty) {
137 if (Ty->isPointerTy() &&
138 (Ty->getPointerAddressSpace() != AddressSpace::UniformConstant)) {
139 auto PointeeTy = Ty->getPointerElementType();
140 if (PointeeTy->isStructTy() &&
141 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
142 Ty = PointeeTy;
143 }
144 }
145
146 if (0 == TypeMap.count(Ty)) {
147 Ty->print(errs());
148 llvm_unreachable("Unhandled type!");
149 }
150
151 return TypeMap[Ty];
152 }
153 TypeMapType &getImageTypeMap() { return ImageTypeMap; }
154 TypeList &getTypeList() { return Types; };
155 ValueList &getConstantList() { return Constants; };
156 ValueMapType &getValueMap() { return ValueMap; }
157 ValueMapType &getAllocatedValueMap() { return AllocatedValueMap; }
158 SPIRVInstructionList &getSPIRVInstList() { return SPIRVInsts; };
159 ValueToValueMapTy &getArgumentGVMap() { return ArgumentGVMap; };
160 ValueMapType &getArgumentGVIDMap() { return ArgumentGVIDMap; };
161 EntryPointVecType &getEntryPointVec() { return EntryPointVec; };
162 DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; };
163 ValueList &getEntryPointInterfacesVec() { return EntryPointInterfacesVec; };
164 uint32_t &getOpExtInstImportID() { return OpExtInstImportID; };
165 std::vector<uint32_t> &getBuiltinDimVec() { return BuiltinDimensionVec; };
166 bool hasVariablePointers() { return true; /* We use StorageBuffer everywhere */ };
167 void setVariablePointers(bool Val) { HasVariablePointers = Val; };
David Neto44795152017-07-13 15:45:28 -0400168 ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() { return samplerMap; }
David Neto22f144c2017-06-12 14:26:21 -0400169 GlobalConstFuncMapType &getGlobalConstFuncTypeMap() {
170 return GlobalConstFuncTypeMap;
171 }
172 SmallPtrSet<Value *, 16> &getGlobalConstArgSet() {
173 return GlobalConstArgumentSet;
174 }
David Neto1a1a0582017-07-07 12:01:44 -0400175 TypeList &getPointerTypesNeedingArrayStride() {
176 return PointerTypesNeedingArrayStride;
177 }
David Neto22f144c2017-06-12 14:26:21 -0400178
179 void GenerateLLVMIRInfo(Module &M);
180 bool FindExtInst(Module &M);
181 void FindTypePerGlobalVar(GlobalVariable &GV);
182 void FindTypePerFunc(Function &F);
183 void FindType(Type *Ty);
184 void FindConstantPerGlobalVar(GlobalVariable &GV);
185 void FindConstantPerFunc(Function &F);
186 void FindConstant(Value *V);
187 void GenerateExtInstImport();
188 void GenerateSPIRVTypes(const DataLayout &DL);
189 void GenerateSPIRVConstants();
190 void GenerateModuleInfo();
191 void GenerateGlobalVar(GlobalVariable &GV);
192 void GenerateSamplers(Module &M);
193 void GenerateFuncPrologue(Function &F);
194 void GenerateFuncBody(Function &F);
195 void GenerateInstForArg(Function &F);
196 spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI);
197 spv::Op GetSPIRVCastOpcode(Instruction &I);
198 spv::Op GetSPIRVBinaryOpcode(Instruction &I);
199 void GenerateInstruction(Instruction &I);
200 void GenerateFuncEpilogue();
201 void HandleDeferredInstruction();
David Neto1a1a0582017-07-07 12:01:44 -0400202 void HandleDeferredDecorations(const DataLayout& DL);
David Neto22f144c2017-06-12 14:26:21 -0400203 bool is4xi8vec(Type *Ty) const;
204 spv::StorageClass GetStorageClass(unsigned AddrSpace) const;
205 spv::BuiltIn GetBuiltin(StringRef globalVarName) const;
206 glsl::ExtInst getExtInstEnum(StringRef Name);
207 void PrintResID(SPIRVInstruction *Inst);
208 void PrintOpcode(SPIRVInstruction *Inst);
209 void PrintOperand(SPIRVOperand *Op);
210 void PrintCapability(SPIRVOperand *Op);
211 void PrintExtInst(SPIRVOperand *Op);
212 void PrintAddrModel(SPIRVOperand *Op);
213 void PrintMemModel(SPIRVOperand *Op);
214 void PrintExecModel(SPIRVOperand *Op);
215 void PrintExecMode(SPIRVOperand *Op);
216 void PrintSourceLanguage(SPIRVOperand *Op);
217 void PrintFuncCtrl(SPIRVOperand *Op);
218 void PrintStorageClass(SPIRVOperand *Op);
219 void PrintDecoration(SPIRVOperand *Op);
220 void PrintBuiltIn(SPIRVOperand *Op);
221 void PrintSelectionControl(SPIRVOperand *Op);
222 void PrintLoopControl(SPIRVOperand *Op);
223 void PrintDimensionality(SPIRVOperand *Op);
224 void PrintImageFormat(SPIRVOperand *Op);
225 void PrintMemoryAccess(SPIRVOperand *Op);
226 void PrintImageOperandsType(SPIRVOperand *Op);
227 void WriteSPIRVAssembly();
228 void WriteOneWord(uint32_t Word);
229 void WriteResultID(SPIRVInstruction *Inst);
230 void WriteWordCountAndOpcode(SPIRVInstruction *Inst);
231 void WriteOperand(SPIRVOperand *Op);
232 void WriteSPIRVBinary();
233
234private:
235 static char ID;
David Neto44795152017-07-13 15:45:28 -0400236 ArrayRef<std::pair<unsigned, std::string>> samplerMap;
David Neto22f144c2017-06-12 14:26:21 -0400237 raw_pwrite_stream &out;
David Neto0676e6f2017-07-11 18:47:44 -0400238
239 // TODO(dneto): Wouldn't it be better to always just emit a binary, and then
240 // convert to other formats on demand?
241
242 // When emitting a C initialization list, the WriteSPIRVBinary method
243 // will actually write its words to this vector via binaryTempOut.
244 SmallVector<char, 100> binaryTempUnderlyingVector;
245 raw_svector_ostream binaryTempOut;
246
247 // Binary output writes to this stream, which might be |out| or
248 // |binaryTempOut|. It's the latter when we really want to write a C
249 // initializer list.
250 raw_pwrite_stream* binaryOut;
David Netoc2c368d2017-06-30 16:50:17 -0400251 raw_ostream &descriptorMapOut;
David Neto22f144c2017-06-12 14:26:21 -0400252 const bool outputAsm;
David Neto0676e6f2017-07-11 18:47:44 -0400253 const bool outputCInitList; // If true, output look like {0x7023, ... , 5}
David Neto22f144c2017-06-12 14:26:21 -0400254 uint64_t patchBoundOffset;
255 uint32_t nextID;
256
257 TypeMapType TypeMap;
258 TypeMapType ImageTypeMap;
259 TypeList Types;
260 ValueList Constants;
261 ValueMapType ValueMap;
262 ValueMapType AllocatedValueMap;
263 SPIRVInstructionList SPIRVInsts;
264 ValueToValueMapTy ArgumentGVMap;
265 ValueMapType ArgumentGVIDMap;
266 EntryPointVecType EntryPointVec;
267 DeferredInstVecType DeferredInstVec;
268 ValueList EntryPointInterfacesVec;
269 uint32_t OpExtInstImportID;
270 std::vector<uint32_t> BuiltinDimensionVec;
271 bool HasVariablePointers;
272 Type *SamplerTy;
273 GlobalConstFuncMapType GlobalConstFuncTypeMap;
274 SmallPtrSet<Value *, 16> GlobalConstArgumentSet;
David Neto1a1a0582017-07-07 12:01:44 -0400275 // An ordered set of pointer types of Base arguments to OpPtrAccessChain,
276 // and which point into transparent memory (StorageBuffer storage class).
277 // These will require an ArrayStride decoration.
278 // See SPV_KHR_variable_pointers rev 13.
279 TypeList PointerTypesNeedingArrayStride;
David Neto22f144c2017-06-12 14:26:21 -0400280};
281
282char SPIRVProducerPass::ID;
283}
284
285namespace clspv {
David Neto44795152017-07-13 15:45:28 -0400286ModulePass *
287createSPIRVProducerPass(raw_pwrite_stream &out, raw_ostream &descriptor_map_out,
288 ArrayRef<std::pair<unsigned, std::string>> samplerMap,
289 bool outputAsm, bool outputCInitList) {
290 return new SPIRVProducerPass(out, descriptor_map_out, samplerMap, outputAsm,
291 outputCInitList);
David Neto22f144c2017-06-12 14:26:21 -0400292}
David Netoc2c368d2017-06-30 16:50:17 -0400293} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -0400294
295bool SPIRVProducerPass::runOnModule(Module &module) {
David Neto0676e6f2017-07-11 18:47:44 -0400296 binaryOut = outputCInitList ? &binaryTempOut : &out;
297
David Neto22f144c2017-06-12 14:26:21 -0400298 // SPIR-V always begins with its header information
299 outputHeader();
300
301 // Gather information from the LLVM IR that we require.
302 GenerateLLVMIRInfo(module);
303
304 // If we are using a sampler map, find the type of the sampler.
305 if (0 < getSamplerMap().size()) {
306 auto SamplerStructTy = module.getTypeByName("opencl.sampler_t");
307 if (!SamplerStructTy) {
308 SamplerStructTy =
309 StructType::create(module.getContext(), "opencl.sampler_t");
310 }
311
312 SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant);
313
314 FindType(SamplerTy);
315 }
316
317 // Collect information on global variables too.
318 for (GlobalVariable &GV : module.globals()) {
319 // If the GV is one of our special __spirv_* variables, remove the
320 // initializer as it was only placed there to force LLVM to not throw the
321 // value away.
322 if (GV.getName().startswith("__spirv_")) {
323 GV.setInitializer(nullptr);
324 }
325
326 // Collect types' information from global variable.
327 FindTypePerGlobalVar(GV);
328
329 // Collect constant information from global variable.
330 FindConstantPerGlobalVar(GV);
331
332 // If the variable is an input, entry points need to know about it.
333 if (AddressSpace::Input == GV.getType()->getPointerAddressSpace()) {
334 getEntryPointInterfacesVec().push_back(&GV);
335 }
336 }
337
338 // If there are extended instructions, generate OpExtInstImport.
339 if (FindExtInst(module)) {
340 GenerateExtInstImport();
341 }
342
343 // Generate SPIRV instructions for types.
344 const DataLayout &DL = module.getDataLayout();
345 GenerateSPIRVTypes(DL);
346
347 // Generate SPIRV constants.
348 GenerateSPIRVConstants();
349
350 // If we have a sampler map, we might have literal samplers to generate.
351 if (0 < getSamplerMap().size()) {
352 GenerateSamplers(module);
353 }
354
355 // Generate SPIRV variables.
356 for (GlobalVariable &GV : module.globals()) {
357 GenerateGlobalVar(GV);
358 }
359
360 // Generate SPIRV instructions for each function.
361 for (Function &F : module) {
362 if (F.isDeclaration()) {
363 continue;
364 }
365
366 // Generate Function Prologue.
367 GenerateFuncPrologue(F);
368
369 // Generate SPIRV instructions for function body.
370 GenerateFuncBody(F);
371
372 // Generate Function Epilogue.
373 GenerateFuncEpilogue();
374 }
375
376 HandleDeferredInstruction();
David Neto1a1a0582017-07-07 12:01:44 -0400377 HandleDeferredDecorations(DL);
David Neto22f144c2017-06-12 14:26:21 -0400378
379 // Generate SPIRV module information.
380 GenerateModuleInfo();
381
382 if (outputAsm) {
383 WriteSPIRVAssembly();
384 } else {
385 WriteSPIRVBinary();
386 }
387
388 // We need to patch the SPIR-V header to set bound correctly.
389 patchHeader();
David Neto0676e6f2017-07-11 18:47:44 -0400390
391 if (outputCInitList) {
392 bool first = true;
David Neto0676e6f2017-07-11 18:47:44 -0400393 std::ostringstream os;
394
David Neto57fb0b92017-08-04 15:35:09 -0400395 auto emit_word = [&os, &first](uint32_t word) {
David Neto0676e6f2017-07-11 18:47:44 -0400396 if (!first)
David Neto57fb0b92017-08-04 15:35:09 -0400397 os << ",\n";
398 os << word;
David Neto0676e6f2017-07-11 18:47:44 -0400399 first = false;
400 };
401
402 os << "{";
David Neto57fb0b92017-08-04 15:35:09 -0400403 const std::string str(binaryTempOut.str());
404 for (unsigned i = 0; i < str.size(); i += 4) {
405 const uint32_t a = static_cast<unsigned char>(str[i]);
406 const uint32_t b = static_cast<unsigned char>(str[i + 1]);
407 const uint32_t c = static_cast<unsigned char>(str[i + 2]);
408 const uint32_t d = static_cast<unsigned char>(str[i + 3]);
409 emit_word(a | (b << 8) | (c << 16) | (d << 24));
David Neto0676e6f2017-07-11 18:47:44 -0400410 }
411 os << "}\n";
412 out << os.str();
413 }
414
David Neto22f144c2017-06-12 14:26:21 -0400415 return false;
416}
417
418void SPIRVProducerPass::outputHeader() {
419 if (outputAsm) {
420 // for ASM output the header goes into 5 comments at the beginning of the
421 // file
422 out << "; SPIR-V\n";
423
424 // the major version number is in the 2nd highest byte
425 const uint32_t major = (spv::Version >> 16) & 0xFF;
426
427 // the minor version number is in the 2nd lowest byte
428 const uint32_t minor = (spv::Version >> 8) & 0xFF;
429 out << "; Version: " << major << "." << minor << "\n";
430
431 // use Codeplay's vendor ID
432 out << "; Generator: Codeplay; 0\n";
433
434 out << "; Bound: ";
435
436 // we record where we need to come back to and patch in the bound value
437 patchBoundOffset = out.tell();
438
439 // output one space per digit for the max size of a 32 bit unsigned integer
440 // (which is the maximum ID we could possibly be using)
441 for (uint32_t i = std::numeric_limits<uint32_t>::max(); 0 != i; i /= 10) {
442 out << " ";
443 }
444
445 out << "\n";
446
447 out << "; Schema: 0\n";
448 } else {
David Neto0676e6f2017-07-11 18:47:44 -0400449 binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber),
David Neto22f144c2017-06-12 14:26:21 -0400450 sizeof(spv::MagicNumber));
David Neto0676e6f2017-07-11 18:47:44 -0400451 binaryOut->write(reinterpret_cast<const char *>(&spv::Version),
David Neto22f144c2017-06-12 14:26:21 -0400452 sizeof(spv::Version));
453
454 // use Codeplay's vendor ID
455 const uint32_t vendor = 3 << 16;
David Neto0676e6f2017-07-11 18:47:44 -0400456 binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor));
David Neto22f144c2017-06-12 14:26:21 -0400457
458 // we record where we need to come back to and patch in the bound value
David Neto0676e6f2017-07-11 18:47:44 -0400459 patchBoundOffset = binaryOut->tell();
David Neto22f144c2017-06-12 14:26:21 -0400460
461 // output a bad bound for now
David Neto0676e6f2017-07-11 18:47:44 -0400462 binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID));
David Neto22f144c2017-06-12 14:26:21 -0400463
464 // output the schema (reserved for use and must be 0)
465 const uint32_t schema = 0;
David Neto0676e6f2017-07-11 18:47:44 -0400466 binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema));
David Neto22f144c2017-06-12 14:26:21 -0400467 }
468}
469
470void SPIRVProducerPass::patchHeader() {
471 if (outputAsm) {
472 // get the string representation of the max bound used (nextID will be the
473 // max ID used)
474 auto asString = std::to_string(nextID);
475 out.pwrite(asString.c_str(), asString.size(), patchBoundOffset);
476 } else {
477 // for a binary we just write the value of nextID over bound
David Neto0676e6f2017-07-11 18:47:44 -0400478 binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID),
479 patchBoundOffset);
David Neto22f144c2017-06-12 14:26:21 -0400480 }
481}
482
483void SPIRVProducerPass::GenerateLLVMIRInfo(Module &M) {
484 // This function generates LLVM IR for function such as global variable for
485 // argument, constant and pointer type for argument access. These information
486 // is artificial one because we need Vulkan SPIR-V output. This function is
487 // executed ahead of FindType and FindConstant.
488 ValueToValueMapTy &ArgGVMap = getArgumentGVMap();
489 LLVMContext &Context = M.getContext();
490
491 // Map for avoiding to generate struct type with same fields.
492 DenseMap<Type *, Type *> ArgTyMap;
493
494 // Collect global constant variables.
495 SmallVector<GlobalVariable *, 8> GVList;
496 for (GlobalVariable &GV : M.globals()) {
497 if (GV.getType()->getAddressSpace() == AddressSpace::Constant) {
498 GVList.push_back(&GV);
499 }
500 }
501
502 // Change global constant variable's address space to ModuleScopePrivate.
503 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
504 for (auto GV : GVList) {
505 // If there is no user of gv, delete gv.
506 if (GV->use_empty()) {
507 GV->eraseFromParent();
508 continue;
509 }
510
511 // Create new gv with ModuleScopePrivate address space.
512 Type *NewGVTy = GV->getType()->getPointerElementType();
513 GlobalVariable *NewGV = new GlobalVariable(
514 M, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "", nullptr,
515 GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate);
516 NewGV->takeName(GV);
517
518 const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end());
519 SmallVector<User*, 8> CandidateUsers;
520
521 for (User *GVU : GVUsers) {
522 if (CallInst *Call = dyn_cast<CallInst>(GVU)) {
523 // Find argument index.
524 unsigned GVCstArgIdx = 0;
525 for (unsigned i = 0; i < Call->getNumArgOperands(); i++) {
526 if (GV == Call->getOperand(i)) {
527 GVCstArgIdx = i;
528 }
529 }
530
531 // Record function with global constant.
532 GlobalConstFuncTyMap[Call->getFunctionType()] =
533 std::make_pair(Call->getFunctionType(), GVCstArgIdx);
534 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) {
535 // Check GEP users.
536 for (User *GEPU : GEP->users()) {
537 if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) {
538 // Find argument index.
539 unsigned GVCstArgIdx = 0;
540 for (unsigned i = 0; i < GEPCall->getNumArgOperands(); i++) {
541 if (GEP == GEPCall->getOperand(i)) {
542 GVCstArgIdx = i;
543 }
544 }
545
546 // Record function with global constant.
547 GlobalConstFuncTyMap[GEPCall->getFunctionType()] =
548 std::make_pair(GEPCall->getFunctionType(), GVCstArgIdx);
549 }
550 }
551 }
552
553 CandidateUsers.push_back(GVU);
554 }
555
556 for (User *U : CandidateUsers) {
557 // Update users of gv with new gv.
558 U->replaceUsesOfWith(GV, NewGV);
559 }
560
561 // Delete original gv.
562 GV->eraseFromParent();
563 }
564
565 bool HasWorkGroupBuiltin = false;
566 for (GlobalVariable &GV : M.globals()) {
567 const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName());
568 if (spv::BuiltInWorkgroupSize == BuiltinType) {
569 HasWorkGroupBuiltin = true;
570 }
571 }
572
573
574 for (Function &F : M) {
575 // Handle kernel function first.
576 if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) {
577 continue;
578 }
579
580 for (BasicBlock &BB : F) {
581 for (Instruction &I : BB) {
582 if (I.getOpcode() == Instruction::ZExt ||
583 I.getOpcode() == Instruction::SExt ||
584 I.getOpcode() == Instruction::UIToFP) {
585 // If there is zext with i1 type, it will be changed to OpSelect. The
586 // OpSelect needs constant 0 and 1 so the constants are added here.
587
588 auto OpTy = I.getOperand(0)->getType();
589
590 if (OpTy->isIntegerTy(1) ||
591 (OpTy->isVectorTy() &&
592 OpTy->getVectorElementType()->isIntegerTy(1))) {
593 if (I.getOpcode() == Instruction::ZExt) {
594 APInt One(32, 1);
595 FindConstant(Constant::getNullValue(I.getType()));
596 FindConstant(Constant::getIntegerValue(I.getType(), One));
597 } else if (I.getOpcode() == Instruction::SExt) {
598 APInt MinusOne(32, UINT64_MAX, true);
599 FindConstant(Constant::getNullValue(I.getType()));
600 FindConstant(Constant::getIntegerValue(I.getType(), MinusOne));
601 } else {
602 FindConstant(ConstantFP::get(Context, APFloat(0.0f)));
603 FindConstant(ConstantFP::get(Context, APFloat(1.0f)));
604 }
605 }
606 } else if (CallInst *Call = dyn_cast<CallInst>(&I)) {
607 Function *Callee = Call->getCalledFunction();
608
609 // Handle image type specially.
610 if (Callee->getName().equals(
611 "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") ||
612 Callee->getName().equals(
613 "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) {
614 TypeMapType &OpImageTypeMap = getImageTypeMap();
615 Type *ImageTy =
616 Call->getArgOperand(0)->getType()->getPointerElementType();
617 OpImageTypeMap[ImageTy] = 0;
618
619 FindConstant(ConstantFP::get(Context, APFloat(0.0f)));
620 }
621 }
622 }
623 }
624
625 if (M.getTypeByName("opencl.image2d_ro_t") ||
626 M.getTypeByName("opencl.image2d_wo_t") ||
627 M.getTypeByName("opencl.image3d_ro_t") ||
628 M.getTypeByName("opencl.image3d_wo_t")) {
629 // Assume Image type's sampled type is float type.
630 FindType(Type::getFloatTy(Context));
631 }
632
633 if (const MDNode *MD =
634 dyn_cast<Function>(&F)->getMetadata("reqd_work_group_size")) {
635 // We generate constants if the WorkgroupSize builtin is being used.
636 if (HasWorkGroupBuiltin) {
637 // Collect constant information for work group size.
638 FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(0)));
639 FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(1)));
640 FindConstant(mdconst::extract<ConstantInt>(MD->getOperand(2)));
641 }
642 }
643
644 // Wrap up all argument types with struct type and create global variables
645 // with them.
646 bool HasArgUser = false;
647 unsigned Idx = 0;
648
649 for (const Argument &Arg : F.args()) {
650 Type *ArgTy = Arg.getType();
651 Type *GVTy = nullptr;
652
653 // Check argument type whether it is pointer type or not. If it is
654 // pointer type, add its address space to new global variable for
655 // argument.
656 unsigned AddrSpace = AddressSpace::Global;
657 if (PointerType *ArgPTy = dyn_cast<PointerType>(ArgTy)) {
658 AddrSpace = ArgPTy->getAddressSpace();
659 }
660
661 Type *TmpArgTy = ArgTy;
662
663 // sampler_t and image types have pointer type of struct type with
664 // opaque
665 // type as field. Extract the struct type. It will be used by global
666 // variable for argument.
667 bool IsSamplerType = false;
668 bool IsImageType = false;
669 if (PointerType *TmpArgPTy = dyn_cast<PointerType>(TmpArgTy)) {
670 if (StructType *STy =
671 dyn_cast<StructType>(TmpArgPTy->getElementType())) {
672 if (STy->isOpaque()) {
673 if (STy->getName().equals("opencl.sampler_t")) {
674 AddrSpace = AddressSpace::UniformConstant;
675 IsSamplerType = true;
676 TmpArgTy = STy;
677 } else if (STy->getName().equals("opencl.image2d_ro_t") ||
678 STy->getName().equals("opencl.image2d_wo_t") ||
679 STy->getName().equals("opencl.image3d_ro_t") ||
680 STy->getName().equals("opencl.image3d_wo_t")) {
681 AddrSpace = AddressSpace::UniformConstant;
682 IsImageType = true;
683 TmpArgTy = STy;
684 } else {
685 llvm_unreachable("Argument has opaque type unsupported???");
686 }
687 }
688 }
689 }
690
691 // LLVM's pointer type is distinguished by address space but we need to
692 // regard constant and global address space as same here. If pointer
693 // type has constant address space, generate new pointer type
694 // temporarily to check previous struct type for argument.
695 if (PointerType *TmpArgPTy = dyn_cast<PointerType>(TmpArgTy)) {
696 AddrSpace = TmpArgPTy->getAddressSpace();
697 if (AddrSpace == AddressSpace::Constant) {
698 TmpArgTy = PointerType::get(TmpArgPTy->getElementType(),
699 AddressSpace::Global);
700 }
701 }
702
703 if (IsSamplerType || IsImageType) {
704 GVTy = TmpArgTy;
705 } else if (ArgTyMap.count(TmpArgTy)) {
706 // If there are arguments handled previously, use its type.
707 GVTy = ArgTyMap[TmpArgTy];
708 } else {
709 // Wrap up argument type with struct type.
710 StructType *STy = StructType::create(Context);
711
712 SmallVector<Type *, 8> EltTys;
713 EltTys.push_back(ArgTy);
714
715 STy->setBody(EltTys, false);
716
717 GVTy = STy;
718 ArgTyMap[TmpArgTy] = STy;
719 }
720
721 // In order to build type map between llvm type and spirv id, LLVM
722 // global variable is needed. It has llvm type and other instructions
723 // can access it with its type.
724 GlobalVariable *NewGV = new GlobalVariable(
725 M, GVTy, false, GlobalValue::ExternalLinkage, UndefValue::get(GVTy),
726 F.getName() + ".arg." + std::to_string(Idx++), nullptr,
727 GlobalValue::ThreadLocalMode::NotThreadLocal, AddrSpace);
728
729 // Generate type info for argument global variable.
730 FindType(NewGV->getType());
731
732 ArgGVMap[&Arg] = NewGV;
733
734 // Generate pointer type of argument type for OpAccessChain of argument.
735 if (!Arg.use_empty()) {
736 if (!isa<PointerType>(ArgTy)) {
737 FindType(PointerType::get(ArgTy, AddrSpace));
738 }
739 HasArgUser = true;
740 }
741 }
742
743 if (HasArgUser) {
744 // Generate constant 0 for OpAccessChain of argument.
745 Type *IdxTy = Type::getInt32Ty(Context);
746 FindConstant(ConstantInt::get(IdxTy, 0));
747 FindType(IdxTy);
748 }
749
750 // Collect types' information from function.
751 FindTypePerFunc(F);
752
753 // Collect constant information from function.
754 FindConstantPerFunc(F);
755 }
756
757 for (Function &F : M) {
758 // Handle non-kernel functions.
759 if (F.isDeclaration() || F.getCallingConv() == CallingConv::SPIR_KERNEL) {
760 continue;
761 }
762
763 for (BasicBlock &BB : F) {
764 for (Instruction &I : BB) {
765 if (I.getOpcode() == Instruction::ZExt ||
766 I.getOpcode() == Instruction::SExt ||
767 I.getOpcode() == Instruction::UIToFP) {
768 // If there is zext with i1 type, it will be changed to OpSelect. The
769 // OpSelect needs constant 0 and 1 so the constants are added here.
770
771 auto OpTy = I.getOperand(0)->getType();
772
773 if (OpTy->isIntegerTy(1) ||
774 (OpTy->isVectorTy() &&
775 OpTy->getVectorElementType()->isIntegerTy(1))) {
776 if (I.getOpcode() == Instruction::ZExt) {
777 APInt One(32, 1);
778 FindConstant(Constant::getNullValue(I.getType()));
779 FindConstant(Constant::getIntegerValue(I.getType(), One));
780 } else if (I.getOpcode() == Instruction::SExt) {
781 APInt MinusOne(32, UINT64_MAX, true);
782 FindConstant(Constant::getNullValue(I.getType()));
783 FindConstant(Constant::getIntegerValue(I.getType(), MinusOne));
784 } else {
785 FindConstant(ConstantFP::get(Context, APFloat(0.0f)));
786 FindConstant(ConstantFP::get(Context, APFloat(1.0f)));
787 }
788 }
789 } else if (CallInst *Call = dyn_cast<CallInst>(&I)) {
790 Function *Callee = Call->getCalledFunction();
791
792 // Handle image type specially.
793 if (Callee->getName().equals(
794 "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") ||
795 Callee->getName().equals(
796 "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) {
797 TypeMapType &OpImageTypeMap = getImageTypeMap();
798 Type *ImageTy =
799 Call->getArgOperand(0)->getType()->getPointerElementType();
800 OpImageTypeMap[ImageTy] = 0;
801
802 FindConstant(ConstantFP::get(Context, APFloat(0.0f)));
803 }
804 }
805 }
806 }
807
808 if (M.getTypeByName("opencl.image2d_ro_t") ||
809 M.getTypeByName("opencl.image2d_wo_t") ||
810 M.getTypeByName("opencl.image3d_ro_t") ||
811 M.getTypeByName("opencl.image3d_wo_t")) {
812 // Assume Image type's sampled type is float type.
813 FindType(Type::getFloatTy(Context));
814 }
815
816 // Collect types' information from function.
817 FindTypePerFunc(F);
818
819 // Collect constant information from function.
820 FindConstantPerFunc(F);
821 }
822}
823
824bool SPIRVProducerPass::FindExtInst(Module &M) {
825 LLVMContext &Context = M.getContext();
826 bool HasExtInst = false;
827
828 for (Function &F : M) {
829 for (BasicBlock &BB : F) {
830 for (Instruction &I : BB) {
831 if (CallInst *Call = dyn_cast<CallInst>(&I)) {
832 Function *Callee = Call->getCalledFunction();
833 // Check whether this call is for extend instructions.
834 glsl::ExtInst EInst = getExtInstEnum(Callee->getName());
835 if (EInst) {
836 // clz needs OpExtInst and OpISub with constant 31. Add constant 31
837 // to constant list here.
838 if (EInst == glsl::ExtInstFindUMsb) {
839 Type *IdxTy = Type::getInt32Ty(Context);
840 FindConstant(ConstantInt::get(IdxTy, 31));
841 FindType(IdxTy);
842 }
843
844 HasExtInst = true;
845 }
846 }
847 }
848 }
849 }
850
851 return HasExtInst;
852}
853
854void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) {
855 // Investigate global variable's type.
856 FindType(GV.getType());
857}
858
859void SPIRVProducerPass::FindTypePerFunc(Function &F) {
860 // Investigate function's type.
861 FunctionType *FTy = F.getFunctionType();
862
863 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
864 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
865 // Handle function with global constant parameters.
866 if (GlobalConstFuncTyMap.count(FTy)) {
867 uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second;
868 SmallVector<Type *, 4> NewFuncParamTys;
869 for (unsigned i = 0; i < FTy->getNumParams(); i++) {
870 Type *ParamTy = FTy->getParamType(i);
871 if (i == GVCstArgIdx) {
872 Type *EleTy = ParamTy->getPointerElementType();
873 ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
874 }
875
876 NewFuncParamTys.push_back(ParamTy);
877 }
878
879 FunctionType *NewFTy =
880 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
881 GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx);
882 FTy = NewFTy;
883 }
884
885 FindType(FTy);
886 } else {
887 // As kernel functions do not have parameters, create new function type and
888 // add it to type map.
889 SmallVector<Type *, 4> NewFuncParamTys;
890 FunctionType *NewFTy =
891 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
892 FindType(NewFTy);
893 }
894
895 // Investigate instructions' type in function body.
896 for (BasicBlock &BB : F) {
897 for (Instruction &I : BB) {
898 if (isa<ShuffleVectorInst>(I)) {
899 for (unsigned i = 0; i < I.getNumOperands(); i++) {
900 // Ignore type for mask of shuffle vector instruction.
901 if (i == 2) {
902 continue;
903 }
904
905 Value *Op = I.getOperand(i);
906 if (!isa<MetadataAsValue>(Op)) {
907 FindType(Op->getType());
908 }
909 }
910
911 FindType(I.getType());
912 continue;
913 }
914
915 // Work through the operands of the instruction.
916 for (unsigned i = 0; i < I.getNumOperands(); i++) {
917 Value *const Op = I.getOperand(i);
918 // If any of the operands is a constant, find the type!
919 if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) {
920 FindType(Op->getType());
921 }
922 }
923
924 for (Use &Op : I.operands()) {
925 if (CallInst *Call = dyn_cast<CallInst>(&I)) {
926 // Avoid to check call instruction's type.
927 break;
928 }
929 if (!isa<MetadataAsValue>(&Op)) {
930 FindType(Op->getType());
931 continue;
932 }
933 }
934
935 CallInst *Call = dyn_cast<CallInst>(&I);
936
937 // We don't want to track the type of this call as we are going to replace
938 // it.
939 if (Call && ("__translate_sampler_initializer" ==
940 Call->getCalledFunction()->getName())) {
941 continue;
942 }
943
944 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
945 // If gep's base operand has ModuleScopePrivate address space, make gep
946 // return ModuleScopePrivate address space.
947 if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) {
948 // Add pointer type with private address space for global constant to
949 // type list.
950 Type *EleTy = I.getType()->getPointerElementType();
951 Type *NewPTy =
952 PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
953
954 FindType(NewPTy);
955 continue;
956 }
957 }
958
959 FindType(I.getType());
960 }
961 }
962}
963
964void SPIRVProducerPass::FindType(Type *Ty) {
965 TypeList &TyList = getTypeList();
966
967 if (0 != TyList.idFor(Ty)) {
968 return;
969 }
970
971 if (Ty->isPointerTy()) {
972 auto AddrSpace = Ty->getPointerAddressSpace();
973 if ((AddressSpace::Constant == AddrSpace) ||
974 (AddressSpace::Global == AddrSpace)) {
975 auto PointeeTy = Ty->getPointerElementType();
976
977 if (PointeeTy->isStructTy() &&
978 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
979 FindType(PointeeTy);
980 auto ActualPointerTy =
981 PointeeTy->getPointerTo(AddressSpace::UniformConstant);
982 FindType(ActualPointerTy);
983 return;
984 }
985 }
986 }
987
988 // OpTypeArray has constant and we need to support type of the constant.
989 if (isa<ArrayType>(Ty)) {
990 LLVMContext &Context = Ty->getContext();
991 FindType(Type::getInt32Ty(Context));
992 }
993
994 for (Type *SubTy : Ty->subtypes()) {
995 FindType(SubTy);
996 }
997
998 TyList.insert(Ty);
999}
1000
1001void SPIRVProducerPass::FindConstantPerGlobalVar(GlobalVariable &GV) {
1002 // If the global variable has a (non undef) initializer.
1003 if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) {
1004 FindConstant(GV.getInitializer());
1005 }
1006}
1007
1008void SPIRVProducerPass::FindConstantPerFunc(Function &F) {
1009 // Investigate constants in function body.
1010 for (BasicBlock &BB : F) {
1011 for (Instruction &I : BB) {
1012 CallInst *Call = dyn_cast<CallInst>(&I);
1013
1014 if (Call && ("__translate_sampler_initializer" ==
1015 Call->getCalledFunction()->getName())) {
1016 // We've handled these constants elsewhere, so skip it.
1017 continue;
1018 }
1019
1020 if (isa<AllocaInst>(I)) {
1021 // Alloca instruction has constant for the number of element. Ignore it.
1022 continue;
1023 } else if (isa<ShuffleVectorInst>(I)) {
1024 for (unsigned i = 0; i < I.getNumOperands(); i++) {
1025 // Ignore constant for mask of shuffle vector instruction.
1026 if (i == 2) {
1027 continue;
1028 }
1029
1030 if (isa<Constant>(I.getOperand(i)) &&
1031 !isa<GlobalValue>(I.getOperand(i))) {
1032 FindConstant(I.getOperand(i));
1033 }
1034 }
1035
1036 continue;
1037 } else if (isa<InsertElementInst>(I)) {
1038 // Handle InsertElement with <4 x i8> specially.
1039 Type *CompositeTy = I.getOperand(0)->getType();
1040 if (is4xi8vec(CompositeTy)) {
1041 LLVMContext &Context = CompositeTy->getContext();
1042 if (isa<Constant>(I.getOperand(0))) {
1043 FindConstant(I.getOperand(0));
1044 }
1045
1046 if (isa<Constant>(I.getOperand(1))) {
1047 FindConstant(I.getOperand(1));
1048 }
1049
1050 // Add mask constant 0xFF.
1051 Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF);
1052 FindConstant(CstFF);
1053
1054 // Add shift amount constant.
1055 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) {
1056 uint64_t Idx = CI->getZExtValue();
1057 Constant *CstShiftAmount =
1058 ConstantInt::get(Type::getInt32Ty(Context), Idx * 8);
1059 FindConstant(CstShiftAmount);
1060 }
1061
1062 continue;
1063 }
1064
1065 for (unsigned i = 0; i < I.getNumOperands(); i++) {
1066 // Ignore constant for index of InsertElement instruction.
1067 if (i == 2) {
1068 continue;
1069 }
1070
1071 if (isa<Constant>(I.getOperand(i)) &&
1072 !isa<GlobalValue>(I.getOperand(i))) {
1073 FindConstant(I.getOperand(i));
1074 }
1075 }
1076
1077 continue;
1078 } else if (isa<ExtractElementInst>(I)) {
1079 // Handle ExtractElement with <4 x i8> specially.
1080 Type *CompositeTy = I.getOperand(0)->getType();
1081 if (is4xi8vec(CompositeTy)) {
1082 LLVMContext &Context = CompositeTy->getContext();
1083 if (isa<Constant>(I.getOperand(0))) {
1084 FindConstant(I.getOperand(0));
1085 }
1086
1087 // Add mask constant 0xFF.
1088 Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF);
1089 FindConstant(CstFF);
1090
1091 // Add shift amount constant.
1092 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
1093 uint64_t Idx = CI->getZExtValue();
1094 Constant *CstShiftAmount =
1095 ConstantInt::get(Type::getInt32Ty(Context), Idx * 8);
1096 FindConstant(CstShiftAmount);
1097 } else {
1098 ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8);
1099 FindConstant(Cst8);
1100 }
1101
1102 continue;
1103 }
1104
1105 for (unsigned i = 0; i < I.getNumOperands(); i++) {
1106 // Ignore constant for index of ExtractElement instruction.
1107 if (i == 1) {
1108 continue;
1109 }
1110
1111 if (isa<Constant>(I.getOperand(i)) &&
1112 !isa<GlobalValue>(I.getOperand(i))) {
1113 FindConstant(I.getOperand(i));
1114 }
1115 }
1116
1117 continue;
1118 } else if ((Instruction::Xor == I.getOpcode()) && I.getType()->isIntegerTy(1)) {
1119 // We special case for Xor where the type is i1 and one of the arguments is a constant 1 (true), this is an OpLogicalNot in SPIR-V, and we don't need the constant
1120 bool foundConstantTrue = false;
1121 for (Use &Op : I.operands()) {
1122 if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) {
1123 auto CI = cast<ConstantInt>(Op);
1124
1125 if (CI->isZero() || foundConstantTrue) {
1126 // If we already found the true constant, we might (probably only on -O0) have an OpLogicalNot which is taking a constant argument, so discover it anyway.
1127 FindConstant(Op);
1128 } else {
1129 foundConstantTrue = true;
1130 }
1131 }
1132 }
1133
1134 continue;
1135 }
1136
1137 for (Use &Op : I.operands()) {
1138 if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) {
1139 FindConstant(Op);
1140 }
1141 }
1142 }
1143 }
1144}
1145
1146void SPIRVProducerPass::FindConstant(Value *V) {
1147 ValueMapType &VMap = getValueMap();
1148 ValueList &CstList = getConstantList();
1149
1150 // If V is already in VMap, ignore it.
1151 if (VMap.find_as(V) != VMap.end()) {
1152 return;
1153 }
1154
1155 Constant *Cst = cast<Constant>(V);
1156
1157 // Handle constant with <4 x i8> type specially.
1158 Type *CstTy = Cst->getType();
1159 if (is4xi8vec(CstTy)) {
1160 if (!isa<GlobalValue>(V)) {
1161 CstList.push_back(V);
1162 VMap[V] = static_cast<uint32_t>(CstList.size());
1163 }
1164 }
1165
1166 if (Cst->getNumOperands()) {
1167 for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end(); I != E;
1168 ++I) {
1169 FindConstant(*I);
1170 }
1171
1172 CstList.push_back(Cst);
1173 VMap[V] = static_cast<uint32_t>(CstList.size());
1174 return;
1175 } else if (const ConstantDataSequential *CDS =
1176 dyn_cast<ConstantDataSequential>(Cst)) {
1177 // Add constants for each element to constant list.
1178 for (unsigned i = 0; i < CDS->getNumElements(); i++) {
1179 Constant *EleCst = CDS->getElementAsConstant(i);
1180 FindConstant(EleCst);
1181 }
1182 }
1183
1184 if (!isa<GlobalValue>(V)) {
1185 CstList.push_back(V);
1186 VMap[V] = static_cast<uint32_t>(CstList.size());
1187 }
1188}
1189
1190spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const {
1191 switch (AddrSpace) {
1192 default:
1193 llvm_unreachable("Unsupported OpenCL address space");
1194 case AddressSpace::Private:
1195 return spv::StorageClassFunction;
1196 case AddressSpace::Global:
1197 case AddressSpace::Constant:
1198 return spv::StorageClassStorageBuffer;
1199 case AddressSpace::Input:
1200 return spv::StorageClassInput;
1201 case AddressSpace::Local:
1202 return spv::StorageClassWorkgroup;
1203 case AddressSpace::UniformConstant:
1204 return spv::StorageClassUniformConstant;
1205 case AddressSpace::ModuleScopePrivate:
1206 return spv::StorageClassPrivate;
1207 }
1208}
1209
1210spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const {
1211 return StringSwitch<spv::BuiltIn>(Name)
1212 .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId)
1213 .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId)
1214 .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize)
1215 .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups)
1216 .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId)
1217 .Default(spv::BuiltInMax);
1218}
1219
1220void SPIRVProducerPass::GenerateExtInstImport() {
1221 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
1222 uint32_t &ExtInstImportID = getOpExtInstImportID();
1223
1224 //
1225 // Generate OpExtInstImport.
1226 //
1227 // Ops[0] ... Ops[n] = Name (Literal String)
1228 SPIRVOperandList Ops;
1229
1230 SPIRVOperand *Name =
1231 new SPIRVOperand(SPIRVOperandType::LITERAL_STRING, "GLSL.std.450");
1232 Ops.push_back(Name);
1233
1234 size_t NameWordSize = (Name->getLiteralStr().size() + 1) / 4;
1235 assert(NameWordSize < (UINT16_MAX - 2));
1236 if ((Name->getLiteralStr().size() + 1) % 4) {
1237 NameWordSize += 1;
1238 }
1239
1240 uint16_t WordCount = static_cast<uint16_t>(2 + NameWordSize);
1241 ExtInstImportID = nextID;
1242
1243 SPIRVInstruction *Inst =
1244 new SPIRVInstruction(WordCount, spv::OpExtInstImport, nextID++, Ops);
1245 SPIRVInstList.push_back(Inst);
1246}
1247
1248void SPIRVProducerPass::GenerateSPIRVTypes(const DataLayout &DL) {
1249 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
1250 ValueMapType &VMap = getValueMap();
1251 ValueMapType &AllocatedVMap = getAllocatedValueMap();
1252 ValueToValueMapTy &ArgGVMap = getArgumentGVMap();
1253
1254 // Map for OpTypeRuntimeArray. If argument has pointer type, 2 spirv type
1255 // instructions are generated. They are OpTypePointer and OpTypeRuntimeArray.
1256 DenseMap<Type *, uint32_t> OpRuntimeTyMap;
1257
1258 for (Type *Ty : getTypeList()) {
1259 // Update TypeMap with nextID for reference later.
1260 TypeMap[Ty] = nextID;
1261
1262 switch (Ty->getTypeID()) {
1263 default: {
1264 Ty->print(errs());
1265 llvm_unreachable("Unsupported type???");
1266 break;
1267 }
1268 case Type::MetadataTyID:
1269 case Type::LabelTyID: {
1270 // Ignore these types.
1271 break;
1272 }
1273 case Type::PointerTyID: {
1274 PointerType *PTy = cast<PointerType>(Ty);
1275 unsigned AddrSpace = PTy->getAddressSpace();
1276
1277 // For the purposes of our Vulkan SPIR-V type system, constant and global
1278 // are conflated.
1279 bool UseExistingOpTypePointer = false;
1280 if (AddressSpace::Constant == AddrSpace) {
1281 AddrSpace = AddressSpace::Global;
1282
1283 // Check to see if we already created this type (for instance, if we had
1284 // a constant <type>* and a global <type>*, the type would be created by
1285 // one of these types, and shared by both).
1286 auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
1287 if (0 < TypeMap.count(GlobalTy)) {
1288 TypeMap[PTy] = TypeMap[GlobalTy];
1289 break;
1290 }
1291 } else if (AddressSpace::Global == AddrSpace) {
1292 AddrSpace = AddressSpace::Constant;
1293
1294 // Check to see if we already created this type (for instance, if we had
1295 // a constant <type>* and a global <type>*, the type would be created by
1296 // one of these types, and shared by both).
1297 auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
1298 if (0 < TypeMap.count(ConstantTy)) {
1299 TypeMap[PTy] = TypeMap[ConstantTy];
1300 UseExistingOpTypePointer = true;
1301 }
1302 }
1303
1304 bool IsOpTypeRuntimeArray = false;
1305 bool HasArgUser = false;
1306
1307 for (auto ArgGV : ArgGVMap) {
1308 auto Arg = ArgGV.first;
1309
1310 Type *ArgTy = Arg->getType();
1311 if (ArgTy == PTy) {
1312 if (AddrSpace != AddressSpace::UniformConstant) {
1313 IsOpTypeRuntimeArray = true;
1314 }
1315
1316 for (auto U : Arg->users()) {
1317 if (!isa<GetElementPtrInst>(U) || (U->getType() == PTy)) {
1318 HasArgUser = true;
1319 break;
1320 }
1321 }
1322 }
1323 }
1324
1325 if ((!IsOpTypeRuntimeArray || HasArgUser) && !UseExistingOpTypePointer) {
1326 //
1327 // Generate OpTypePointer.
1328 //
1329
1330 // OpTypePointer
1331 // Ops[0] = Storage Class
1332 // Ops[1] = Element Type ID
1333 SPIRVOperandList Ops;
1334
1335 spv::StorageClass StorageClass = GetStorageClass(AddrSpace);
1336
1337 SPIRVOperand *StorageClassOp =
1338 new SPIRVOperand(SPIRVOperandType::NUMBERID, StorageClass);
1339 Ops.push_back(StorageClassOp);
1340
1341 uint32_t EleTyID = lookupType(PTy->getElementType());
1342 SPIRVOperand *EleTyOp =
1343 new SPIRVOperand(SPIRVOperandType::NUMBERID, EleTyID);
1344 Ops.push_back(EleTyOp);
1345
1346 spv::Op Opcode = spv::OpTypePointer;
1347 uint16_t WordCount = 4;
1348
1349 SPIRVInstruction *Inst =
1350 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
1351 SPIRVInstList.push_back(Inst);
1352 }
1353
1354 if (IsOpTypeRuntimeArray) {
1355 //
1356 // Generate OpTypeRuntimeArray.
1357 //
1358
1359 // OpTypeRuntimeArray
1360 // Ops[0] = Element Type ID
1361 SPIRVOperandList Ops;
1362
1363 uint32_t EleTyID = lookupType(PTy->getElementType());
1364 SPIRVOperand *EleTyOp =
1365 new SPIRVOperand(SPIRVOperandType::NUMBERID, EleTyID);
1366 Ops.push_back(EleTyOp);
1367
1368 spv::Op Opcode = spv::OpTypeRuntimeArray;
1369 uint16_t WordCount = 3;
1370
1371 uint32_t OpTypeRuntimeArrayID = nextID;
1372 assert(0 == OpRuntimeTyMap.count(Ty));
1373 OpRuntimeTyMap[Ty] = nextID;
1374
1375 SPIRVInstruction *Inst =
1376 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
1377 SPIRVInstList.push_back(Inst);
1378
1379 // Generate OpDecorate.
1380 auto DecoInsertPoint =
1381 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
1382 [](SPIRVInstruction *Inst) -> bool {
1383 return Inst->getOpcode() != spv::OpDecorate &&
1384 Inst->getOpcode() != spv::OpMemberDecorate &&
1385 Inst->getOpcode() != spv::OpExtInstImport;
1386 });
1387
1388 // Ops[0] = Target ID
1389 // Ops[1] = Decoration (ArrayStride)
1390 // Ops[2] = Stride Number(Literal Number)
1391 Ops.clear();
1392
1393 SPIRVOperand *PTyIDOp =
1394 new SPIRVOperand(SPIRVOperandType::NUMBERID, OpTypeRuntimeArrayID);
1395 Ops.push_back(PTyIDOp);
1396
1397 SPIRVOperand *DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID,
1398 spv::DecorationArrayStride);
1399 Ops.push_back(DecoOp);
1400
1401 std::vector<uint32_t> LiteralNum;
1402 Type *EleTy = PTy->getElementType();
David Neto25018082017-07-07 13:21:46 -04001403 const unsigned ArrayStride = DL.getTypeAllocSize(EleTy);
David Neto22f144c2017-06-12 14:26:21 -04001404 LiteralNum.push_back(ArrayStride);
1405 SPIRVOperand *ArrayStrideOp =
1406 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1407 Ops.push_back(ArrayStrideOp);
1408
1409 SPIRVInstruction *DecoInst =
1410 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
1411 SPIRVInstList.insert(DecoInsertPoint, DecoInst);
1412 }
1413 break;
1414 }
1415 case Type::StructTyID: {
1416 LLVMContext &Context = Ty->getContext();
1417
1418 StructType *STy = cast<StructType>(Ty);
1419
1420 // Handle sampler type.
1421 if (STy->isOpaque()) {
1422 if (STy->getName().equals("opencl.sampler_t")) {
1423 //
1424 // Generate OpTypeSampler
1425 //
1426 // Empty Ops.
1427 SPIRVOperandList Ops;
1428
1429 SPIRVInstruction *Inst =
1430 new SPIRVInstruction(2, spv::OpTypeSampler, nextID++, Ops);
1431 SPIRVInstList.push_back(Inst);
1432 break;
1433 } else if (STy->getName().equals("opencl.image2d_ro_t") ||
1434 STy->getName().equals("opencl.image2d_wo_t") ||
1435 STy->getName().equals("opencl.image3d_ro_t") ||
1436 STy->getName().equals("opencl.image3d_wo_t")) {
1437 //
1438 // Generate OpTypeImage
1439 //
1440 // Ops[0] = Sampled Type ID
1441 // Ops[1] = Dim ID
1442 // Ops[2] = Depth (Literal Number)
1443 // Ops[3] = Arrayed (Literal Number)
1444 // Ops[4] = MS (Literal Number)
1445 // Ops[5] = Sampled (Literal Number)
1446 // Ops[6] = Image Format ID
1447 //
1448 SPIRVOperandList Ops;
1449
1450 // TODO: Changed Sampled Type according to situations.
1451 uint32_t SampledTyID = lookupType(Type::getFloatTy(Context));
1452 SPIRVOperand *SampledTyIDOp =
1453 new SPIRVOperand(SPIRVOperandType::NUMBERID, SampledTyID);
1454 Ops.push_back(SampledTyIDOp);
1455
1456 spv::Dim DimID = spv::Dim2D;
1457 if (STy->getName().equals("opencl.image3d_ro_t") ||
1458 STy->getName().equals("opencl.image3d_wo_t")) {
1459 DimID = spv::Dim3D;
1460 }
1461 SPIRVOperand *DimIDOp =
1462 new SPIRVOperand(SPIRVOperandType::NUMBERID, DimID);
1463 Ops.push_back(DimIDOp);
1464
1465 // TODO: Set up Depth.
1466 std::vector<uint32_t> LiteralNum;
1467 LiteralNum.push_back(0);
1468 SPIRVOperand *DepthOp =
1469 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1470 Ops.push_back(DepthOp);
1471
1472 // TODO: Set up Arrayed.
1473 LiteralNum.clear();
1474 LiteralNum.push_back(0);
1475 SPIRVOperand *ArrayedOp =
1476 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1477 Ops.push_back(ArrayedOp);
1478
1479 // TODO: Set up MS.
1480 LiteralNum.clear();
1481 LiteralNum.push_back(0);
1482 SPIRVOperand *MSOp =
1483 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1484 Ops.push_back(MSOp);
1485
1486 // TODO: Set up Sampled.
1487 //
1488 // From Spec
1489 //
1490 // 0 indicates this is only known at run time, not at compile time
1491 // 1 indicates will be used with sampler
1492 // 2 indicates will be used without a sampler (a storage image)
1493 uint32_t Sampled = 1;
1494 if (STy->getName().equals("opencl.image2d_wo_t") ||
1495 STy->getName().equals("opencl.image3d_wo_t")) {
1496 Sampled = 2;
1497 }
1498 LiteralNum.clear();
1499 LiteralNum.push_back(Sampled);
1500 SPIRVOperand *SampledOp =
1501 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1502 Ops.push_back(SampledOp);
1503
1504 // TODO: Set up Image Format.
1505 SPIRVOperand *ImageFormatOp = new SPIRVOperand(
1506 SPIRVOperandType::NUMBERID, spv::ImageFormatUnknown);
1507 Ops.push_back(ImageFormatOp);
1508
1509 SPIRVInstruction *Inst =
1510 new SPIRVInstruction(9, spv::OpTypeImage, nextID++, Ops);
1511 SPIRVInstList.push_back(Inst);
1512 break;
1513 }
1514 }
1515
1516 //
1517 // Generate OpTypeStruct
1518 //
1519 // Ops[0] ... Ops[n] = Member IDs
1520 SPIRVOperandList Ops;
1521
1522 for (auto *EleTy : STy->elements()) {
1523 uint32_t EleTyID = lookupType(EleTy);
1524
1525 // Check OpTypeRuntimeArray.
1526 if (isa<PointerType>(EleTy)) {
1527 for (auto ArgGV : ArgGVMap) {
1528 Type *ArgTy = ArgGV.first->getType();
1529 if (ArgTy == EleTy) {
1530 assert(0 != OpRuntimeTyMap.count(EleTy));
1531 EleTyID = OpRuntimeTyMap[EleTy];
1532 }
1533 }
1534 }
1535
1536 SPIRVOperand *EleTyOp =
1537 new SPIRVOperand(SPIRVOperandType::NUMBERID, EleTyID);
1538 Ops.push_back(EleTyOp);
1539 }
1540
1541 uint16_t WordCount = static_cast<uint16_t>(2 + Ops.size());
1542 uint32_t STyID = nextID;
1543
1544 SPIRVInstruction *Inst =
1545 new SPIRVInstruction(WordCount, spv::OpTypeStruct, nextID++, Ops);
1546 SPIRVInstList.push_back(Inst);
1547
1548 // Generate OpMemberDecorate.
1549 auto DecoInsertPoint =
1550 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
1551 [](SPIRVInstruction *Inst) -> bool {
1552 return Inst->getOpcode() != spv::OpDecorate &&
1553 Inst->getOpcode() != spv::OpMemberDecorate &&
1554 Inst->getOpcode() != spv::OpExtInstImport;
1555 });
1556
David Netoc463b372017-08-10 15:32:21 -04001557 const auto StructLayout = DL.getStructLayout(STy);
1558
David Neto22f144c2017-06-12 14:26:21 -04001559 for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements();
1560 MemberIdx++) {
1561 // Ops[0] = Structure Type ID
1562 // Ops[1] = Member Index(Literal Number)
1563 // Ops[2] = Decoration (Offset)
1564 // Ops[3] = Byte Offset (Literal Number)
1565 Ops.clear();
1566
1567 SPIRVOperand *STyIDOp =
1568 new SPIRVOperand(SPIRVOperandType::NUMBERID, STyID);
1569 Ops.push_back(STyIDOp);
1570
1571 std::vector<uint32_t> LiteralNum;
1572 LiteralNum.push_back(MemberIdx);
1573 SPIRVOperand *MemberIdxOp =
1574 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1575 Ops.push_back(MemberIdxOp);
1576
1577 SPIRVOperand *DecoOp =
1578 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::DecorationOffset);
1579 Ops.push_back(DecoOp);
1580
1581 LiteralNum.clear();
David Netoc463b372017-08-10 15:32:21 -04001582 const auto ByteOffset =
1583 uint32_t(StructLayout->getElementOffset(MemberIdx));
David Neto22f144c2017-06-12 14:26:21 -04001584 LiteralNum.push_back(ByteOffset);
1585 SPIRVOperand *ByteOffsetOp =
1586 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1587 Ops.push_back(ByteOffsetOp);
1588
1589 SPIRVInstruction *DecoInst =
1590 new SPIRVInstruction(5, spv::OpMemberDecorate, 0 /* No id */, Ops);
1591 SPIRVInstList.insert(DecoInsertPoint, DecoInst);
David Neto22f144c2017-06-12 14:26:21 -04001592 }
1593
1594 // Generate OpDecorate.
1595 for (auto ArgGV : ArgGVMap) {
1596 Type *ArgGVTy = ArgGV.second->getType();
1597 PointerType *PTy = cast<PointerType>(ArgGVTy);
1598 Type *ArgTy = PTy->getElementType();
1599
1600 // Struct type from argument is already distinguished with the other
1601 // struct types on llvm types. As a result, if current processing struct
1602 // type is same with argument type, we can generate OpDecorate with
1603 // Block or BufferBlock.
1604 if (ArgTy == STy) {
1605 // Ops[0] = Target ID
1606 // Ops[1] = Decoration (Block or BufferBlock)
1607 Ops.clear();
1608
1609 SPIRVOperand *STyIDOp =
1610 new SPIRVOperand(SPIRVOperandType::NUMBERID, STyID);
1611 Ops.push_back(STyIDOp);
1612
David Neto6e392822017-08-04 14:06:10 -04001613 // Use Block decorations with StorageBuffer storage class.
1614 const spv::Decoration Deco = spv::DecorationBlock;
David Neto22f144c2017-06-12 14:26:21 -04001615
1616 SPIRVOperand *DecoOp =
1617 new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
1618 Ops.push_back(DecoOp);
1619
1620 SPIRVInstruction *DecoInst =
1621 new SPIRVInstruction(3, spv::OpDecorate, 0 /* No id */, Ops);
1622 SPIRVInstList.insert(DecoInsertPoint, DecoInst);
1623 break;
1624 }
1625 }
1626 break;
1627 }
1628 case Type::IntegerTyID: {
1629 unsigned BitWidth = Ty->getPrimitiveSizeInBits();
1630
1631 if (BitWidth == 1) {
1632 SPIRVInstruction *Inst =
1633 new SPIRVInstruction(2, spv::OpTypeBool, nextID++, {});
1634 SPIRVInstList.push_back(Inst);
1635 } else {
1636 // i8 is added to TypeMap as i32.
1637 if (BitWidth == 8) {
1638 BitWidth = 32;
1639 }
1640
1641 SPIRVOperand *Ops[2] = {
1642 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, BitWidth),
1643 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, 0u)};
1644
1645 SPIRVInstList.push_back(
1646 new SPIRVInstruction(4, spv::OpTypeInt, nextID++, Ops));
1647 }
1648 break;
1649 }
1650 case Type::HalfTyID:
1651 case Type::FloatTyID:
1652 case Type::DoubleTyID: {
1653 SPIRVOperand *WidthOp = new SPIRVOperand(
1654 SPIRVOperandType::LITERAL_INTEGER, Ty->getPrimitiveSizeInBits());
1655
1656 SPIRVInstList.push_back(
1657 new SPIRVInstruction(3, spv::OpTypeFloat, nextID++, WidthOp));
1658 break;
1659 }
1660 case Type::ArrayTyID: {
1661 LLVMContext &Context = Ty->getContext();
1662 ArrayType *ArrTy = cast<ArrayType>(Ty);
1663 //
1664 // Generate OpConstant and OpTypeArray.
1665 //
1666
1667 //
1668 // Generate OpConstant for array length.
1669 //
1670 // Ops[0] = Result Type ID
1671 // Ops[1] .. Ops[n] = Values LiteralNumber
1672 SPIRVOperandList Ops;
1673
1674 Type *LengthTy = Type::getInt32Ty(Context);
1675 uint32_t ResTyID = lookupType(LengthTy);
1676 SPIRVOperand *ResTyOp =
1677 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
1678 Ops.push_back(ResTyOp);
1679
1680 uint64_t Length = ArrTy->getArrayNumElements();
1681 assert(Length < UINT32_MAX);
1682 std::vector<uint32_t> LiteralNum;
1683 LiteralNum.push_back(static_cast<uint32_t>(Length));
1684 SPIRVOperand *ValOp =
1685 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1686 Ops.push_back(ValOp);
1687
1688 // Add constant for length to constant list.
1689 Constant *CstLength = ConstantInt::get(LengthTy, Length);
1690 AllocatedVMap[CstLength] = nextID;
1691 VMap[CstLength] = nextID;
1692 uint32_t LengthID = nextID;
1693
1694 SPIRVInstruction *CstInst =
1695 new SPIRVInstruction(4, spv::OpConstant, nextID++, Ops);
1696 SPIRVInstList.push_back(CstInst);
1697
1698 //
1699 // Generate OpTypeArray.
1700 //
1701 // Ops[0] = Element Type ID
1702 // Ops[1] = Array Length Constant ID
1703 Ops.clear();
1704
1705 uint32_t EleTyID = lookupType(ArrTy->getElementType());
1706 SPIRVOperand *EleTyOp =
1707 new SPIRVOperand(SPIRVOperandType::NUMBERID, EleTyID);
1708 Ops.push_back(EleTyOp);
1709
1710 SPIRVOperand *LengthOp =
1711 new SPIRVOperand(SPIRVOperandType::NUMBERID, LengthID);
1712 Ops.push_back(LengthOp);
1713
1714 // Update TypeMap with nextID.
1715 TypeMap[Ty] = nextID;
1716
1717 SPIRVInstruction *ArrayInst =
1718 new SPIRVInstruction(4, spv::OpTypeArray, nextID++, Ops);
1719 SPIRVInstList.push_back(ArrayInst);
1720 break;
1721 }
1722 case Type::VectorTyID: {
1723 // <4 x i8> is changed to i32.
1724 LLVMContext &Context = Ty->getContext();
1725 if (Ty->getVectorElementType() == Type::getInt8Ty(Context)) {
1726 if (Ty->getVectorNumElements() == 4) {
1727 TypeMap[Ty] = lookupType(Ty->getVectorElementType());
1728 break;
1729 } else {
1730 Ty->print(errs());
1731 llvm_unreachable("Support above i8 vector type");
1732 }
1733 }
1734
1735 // Ops[0] = Component Type ID
1736 // Ops[1] = Component Count (Literal Number)
1737 SPIRVOperand *Ops[2] = {
1738 new SPIRVOperand(SPIRVOperandType::NUMBERID,
1739 lookupType(Ty->getVectorElementType())),
1740 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER,
1741 Ty->getVectorNumElements())};
1742
1743 SPIRVInstList.push_back(
1744 new SPIRVInstruction(4, spv::OpTypeVector, nextID++, Ops));
1745 break;
1746 }
1747 case Type::VoidTyID: {
1748 SPIRVInstruction *Inst =
1749 new SPIRVInstruction(2, spv::OpTypeVoid, nextID++, {});
1750 SPIRVInstList.push_back(Inst);
1751 break;
1752 }
1753 case Type::FunctionTyID: {
1754 // Generate SPIRV instruction for function type.
1755 FunctionType *FTy = cast<FunctionType>(Ty);
1756
1757 // Ops[0] = Return Type ID
1758 // Ops[1] ... Ops[n] = Parameter Type IDs
1759 SPIRVOperandList Ops;
1760
1761 // Find SPIRV instruction for return type
1762 uint32_t RetTyID = lookupType(FTy->getReturnType());
1763
1764 SPIRVOperand *RetTyOp =
1765 new SPIRVOperand(SPIRVOperandType::NUMBERID, RetTyID);
1766 Ops.push_back(RetTyOp);
1767
1768 // Find SPIRV instructions for parameter types
1769 for (unsigned k = 0; k < FTy->getNumParams(); k++) {
1770 // Find SPIRV instruction for parameter type.
1771 auto ParamTy = FTy->getParamType(k);
1772 if (ParamTy->isPointerTy()) {
1773 auto PointeeTy = ParamTy->getPointerElementType();
1774 if (PointeeTy->isStructTy() &&
1775 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
1776 ParamTy = PointeeTy;
1777 }
1778 }
1779
1780 uint32_t ParamTyID = lookupType(ParamTy);
1781 SPIRVOperand *ParamTyOp =
1782 new SPIRVOperand(SPIRVOperandType::NUMBERID, ParamTyID);
1783 Ops.push_back(ParamTyOp);
1784 }
1785
1786 // Return type id is included in operand list.
1787 uint16_t WordCount = static_cast<uint16_t>(2 + Ops.size());
1788
1789 SPIRVInstruction *Inst =
1790 new SPIRVInstruction(WordCount, spv::OpTypeFunction, nextID++, Ops);
1791 SPIRVInstList.push_back(Inst);
1792 break;
1793 }
1794 }
1795 }
1796
1797 // Generate OpTypeSampledImage.
1798 TypeMapType &OpImageTypeMap = getImageTypeMap();
1799 for (auto &ImageType : OpImageTypeMap) {
1800 //
1801 // Generate OpTypeSampledImage.
1802 //
1803 // Ops[0] = Image Type ID
1804 //
1805 SPIRVOperandList Ops;
1806
1807 Type *ImgTy = ImageType.first;
1808 uint32_t ImgTyID = TypeMap[ImgTy];
1809 SPIRVOperand *ImgTyOp =
1810 new SPIRVOperand(SPIRVOperandType::NUMBERID, ImgTyID);
1811 Ops.push_back(ImgTyOp);
1812
1813 // Update OpImageTypeMap.
1814 ImageType.second = nextID;
1815
1816 SPIRVInstruction *Inst =
1817 new SPIRVInstruction(3, spv::OpTypeSampledImage, nextID++, Ops);
1818 SPIRVInstList.push_back(Inst);
1819 }
1820}
1821
1822void SPIRVProducerPass::GenerateSPIRVConstants() {
1823 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
1824 ValueMapType &VMap = getValueMap();
1825 ValueMapType &AllocatedVMap = getAllocatedValueMap();
1826 ValueList &CstList = getConstantList();
1827
1828 for (uint32_t i = 0; i < CstList.size(); i++) {
1829 Constant *Cst = cast<Constant>(CstList[i]);
1830
1831 // OpTypeArray's constant was already generated.
1832 if (AllocatedVMap[Cst]) {
1833 continue;
1834 }
1835
1836 // Update TypeMap with nextID for reference later.
1837 VMap[Cst] = nextID;
1838
1839 //
1840 // Generate OpConstant.
1841 //
1842
1843 // Ops[0] = Result Type ID
1844 // Ops[1] .. Ops[n] = Values LiteralNumber
1845 SPIRVOperandList Ops;
1846
1847 uint32_t ResTyID = lookupType(Cst->getType());
1848 SPIRVOperand *ResTyIDOp =
1849 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
1850 Ops.push_back(ResTyIDOp);
1851
1852 std::vector<uint32_t> LiteralNum;
1853 uint16_t WordCount = 0;
1854 spv::Op Opcode = spv::OpNop;
1855
1856 if (isa<UndefValue>(Cst)) {
1857 // Ops[0] = Result Type ID
1858 Opcode = spv::OpUndef;
1859 WordCount = 3;
1860 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) {
1861 unsigned BitWidth = CI->getBitWidth();
1862 if (BitWidth == 1) {
1863 // If the bitwidth of constant is 1, generate OpConstantTrue or
1864 // OpConstantFalse.
1865 if (CI->getZExtValue()) {
1866 // Ops[0] = Result Type ID
1867 Opcode = spv::OpConstantTrue;
1868 } else {
1869 // Ops[0] = Result Type ID
1870 Opcode = spv::OpConstantFalse;
1871 }
1872 WordCount = 3;
1873 } else {
1874 auto V = CI->getZExtValue();
1875 LiteralNum.push_back(V & 0xFFFFFFFF);
1876
1877 if (BitWidth > 32) {
1878 LiteralNum.push_back(V >> 32);
1879 }
1880
1881 Opcode = spv::OpConstant;
1882 WordCount = static_cast<uint16_t>(3 + LiteralNum.size());
1883
1884 SPIRVOperand *CstValue =
1885 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1886 Ops.push_back(CstValue);
1887 }
1888 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) {
1889 uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
1890 Type *CFPTy = CFP->getType();
1891 if (CFPTy->isFloatTy()) {
1892 LiteralNum.push_back(FPVal & 0xFFFFFFFF);
1893 } else {
1894 CFPTy->print(errs());
1895 llvm_unreachable("Implement this ConstantFP Type");
1896 }
1897
1898 Opcode = spv::OpConstant;
1899 WordCount = static_cast<uint16_t>(3 + LiteralNum.size());
1900
1901 SPIRVOperand *CstValue =
1902 new SPIRVOperand(SPIRVOperandType::LITERAL_FLOAT, LiteralNum);
1903 Ops.push_back(CstValue);
1904 } else if (isa<ConstantDataSequential>(Cst) &&
1905 cast<ConstantDataSequential>(Cst)->isString()) {
1906 Cst->print(errs());
1907 llvm_unreachable("Implement this Constant");
1908
1909 } else if (const ConstantDataSequential *CDS =
1910 dyn_cast<ConstantDataSequential>(Cst)) {
1911 for (unsigned k = 0; k < CDS->getNumElements(); k++) {
1912 Constant *EleCst = CDS->getElementAsConstant(k);
1913 uint32_t EleCstID = VMap[EleCst];
1914 SPIRVOperand *EleCstIDOp =
1915 new SPIRVOperand(SPIRVOperandType::NUMBERID, EleCstID);
1916 Ops.push_back(EleCstIDOp);
1917 }
1918
1919 Opcode = spv::OpConstantComposite;
1920 WordCount = static_cast<uint16_t>(3 + CDS->getNumElements());
1921 } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) {
1922 // Let's convert <4 x i8> constant to int constant specially.
1923 Type *CstTy = Cst->getType();
1924 if (is4xi8vec(CstTy)) {
1925 LLVMContext &Context = CstTy->getContext();
1926
1927 //
1928 // Generate OpConstant with OpTypeInt 32 0.
1929 //
1930 uint64_t IntValue = 0;
1931 uint32_t Idx = 0;
1932 for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end();
1933 I != E; ++I) {
1934 uint64_t Val = 0;
1935 if (ConstantInt *CI2 = dyn_cast<ConstantInt>(I)) {
1936 Val = CI2->getZExtValue();
1937 }
1938 IntValue = (IntValue << Idx) | Val;
1939 }
1940
1941 ConstantInt *CstInt =
1942 ConstantInt::get(Type::getInt32Ty(Context), IntValue);
1943 // If this constant is already registered on VMap, use it.
1944 if (VMap.count(CstInt)) {
1945 uint32_t CstID = VMap[CstInt];
1946 VMap[Cst] = CstID;
1947 return;
1948 }
1949
1950 LiteralNum.push_back(IntValue & 0xFFFFFFFF);
1951 SPIRVOperand *CstValue =
1952 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
1953 Ops.push_back(CstValue);
1954
1955 SPIRVInstruction *CstInst =
1956 new SPIRVInstruction(4, spv::OpConstant, nextID++, Ops);
1957 SPIRVInstList.push_back(CstInst);
1958
1959 return;
1960 }
1961
1962 // We use a constant composite in SPIR-V for our constant aggregate in
1963 // LLVM.
1964 Opcode = spv::OpConstantComposite;
1965 WordCount = static_cast<uint16_t>(3 + CA->getNumOperands());
1966
1967 for (unsigned k = 0; k < CA->getNumOperands(); k++) {
1968 // Look up the ID of the element of this aggregate (which we will
1969 // previously have created a constant for).
1970 uint32_t ElementConstantID = VMap[CA->getAggregateElement(k)];
1971
1972 // And add an operand to the composite we are constructing
1973 Ops.push_back(
1974 new SPIRVOperand(SPIRVOperandType::NUMBERID, ElementConstantID));
1975 }
1976 } else if (Cst->isNullValue()) {
1977 Opcode = spv::OpConstantNull;
1978 WordCount = 3;
1979 } else {
1980 Cst->print(errs());
1981 llvm_unreachable("Unsupported Constant???");
1982 }
1983
1984 SPIRVInstruction *CstInst =
1985 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
1986 SPIRVInstList.push_back(CstInst);
1987 }
1988}
1989
1990void SPIRVProducerPass::GenerateSamplers(Module &M) {
1991 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
1992 ValueMapType &VMap = getValueMap();
1993
1994 DenseMap<unsigned, unsigned> SamplerLiteralToIDMap;
1995
1996 unsigned BindingIdx = 0;
1997
1998 // Generate the sampler map.
1999 for (auto SamplerLiteral : getSamplerMap()) {
2000 // Generate OpVariable.
2001 //
2002 // GIDOps[0] : Result Type ID
2003 // GIDOps[1] : Storage Class
2004 SPIRVOperandList Ops;
2005
2006 Ops.push_back(
2007 new SPIRVOperand(SPIRVOperandType::NUMBERID, lookupType(SamplerTy)));
2008
2009 spv::StorageClass StorageClass = spv::StorageClassUniformConstant;
2010 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, StorageClass));
2011
2012 SPIRVInstruction *Inst = new SPIRVInstruction(
2013 static_cast<uint16_t>(2 + Ops.size()), spv::OpVariable, nextID, Ops);
2014 SPIRVInstList.push_back(Inst);
2015
David Neto44795152017-07-13 15:45:28 -04002016 SamplerLiteralToIDMap[SamplerLiteral.first] = nextID++;
David Neto22f144c2017-06-12 14:26:21 -04002017
2018 // Find Insert Point for OpDecorate.
2019 auto DecoInsertPoint =
2020 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
2021 [](SPIRVInstruction *Inst) -> bool {
2022 return Inst->getOpcode() != spv::OpDecorate &&
2023 Inst->getOpcode() != spv::OpMemberDecorate &&
2024 Inst->getOpcode() != spv::OpExtInstImport;
2025 });
2026
2027 // Ops[0] = Target ID
2028 // Ops[1] = Decoration (DescriptorSet)
2029 // Ops[2] = LiteralNumber according to Decoration
2030 Ops.clear();
2031
David Neto44795152017-07-13 15:45:28 -04002032 SPIRVOperand *ArgIDOp =
2033 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2034 SamplerLiteralToIDMap[SamplerLiteral.first]);
David Neto22f144c2017-06-12 14:26:21 -04002035 Ops.push_back(ArgIDOp);
2036
2037 spv::Decoration Deco = spv::DecorationDescriptorSet;
2038 SPIRVOperand *DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2039 Ops.push_back(DecoOp);
2040
David Neto44795152017-07-13 15:45:28 -04002041 descriptorMapOut << "sampler," << SamplerLiteral.first << ",samplerExpr,\""
2042 << SamplerLiteral.second << "\",descriptorSet,0,binding,"
David Netoc2c368d2017-06-30 16:50:17 -04002043 << BindingIdx << "\n";
2044
David Neto22f144c2017-06-12 14:26:21 -04002045 std::vector<uint32_t> LiteralNum;
2046 LiteralNum.push_back(0);
2047 SPIRVOperand *DescSet =
2048 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2049 Ops.push_back(DescSet);
2050
2051 SPIRVInstruction *DescDecoInst =
2052 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2053 SPIRVInstList.insert(DecoInsertPoint, DescDecoInst);
2054
2055 // Ops[0] = Target ID
2056 // Ops[1] = Decoration (Binding)
2057 // Ops[2] = LiteralNumber according to Decoration
2058 Ops.clear();
2059
2060 Ops.push_back(ArgIDOp);
2061
2062 Deco = spv::DecorationBinding;
2063 DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2064 Ops.push_back(DecoOp);
2065
2066 LiteralNum.clear();
2067 LiteralNum.push_back(BindingIdx++);
2068 SPIRVOperand *Binding =
2069 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2070 Ops.push_back(Binding);
2071
2072 SPIRVInstruction *BindDecoInst =
2073 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2074 SPIRVInstList.insert(DecoInsertPoint, BindDecoInst);
2075 }
2076
2077 const char *TranslateSamplerFunctionName = "__translate_sampler_initializer";
2078
2079 auto SamplerFunction = M.getFunction(TranslateSamplerFunctionName);
2080
2081 // If there are no uses of the sampler function, no work to do!
2082 if (!SamplerFunction) {
2083 return;
2084 }
2085
2086 // Iterate through the users of the sampler function.
2087 for (auto User : SamplerFunction->users()) {
2088 if (auto CI = dyn_cast<CallInst>(User)) {
2089 // Get the literal used to initialize the sampler.
2090 auto Constant = dyn_cast<ConstantInt>(CI->getArgOperand(0));
2091
2092 if (!Constant) {
2093 CI->getArgOperand(0)->print(errs());
2094 llvm_unreachable("Argument of sampler initializer was non-constant!");
2095 }
2096
2097 auto SamplerLiteral = static_cast<unsigned>(Constant->getZExtValue());
2098
2099 if (0 == SamplerLiteralToIDMap.count(SamplerLiteral)) {
2100 Constant->print(errs());
2101 llvm_unreachable("Sampler literal was not found in sampler map!");
2102 }
2103
2104 // Calls to the sampler literal function to initialize a sampler are
2105 // re-routed to the global variables declared for the sampler.
2106 VMap[CI] = SamplerLiteralToIDMap[SamplerLiteral];
2107 }
2108 }
2109}
2110
2111void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) {
2112 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
2113 ValueMapType &VMap = getValueMap();
2114 std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec();
2115
2116 const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName());
2117 Type *Ty = GV.getType();
2118 PointerType *PTy = cast<PointerType>(Ty);
2119
2120 uint32_t InitializerID = 0;
2121
2122 // Workgroup size is handled differently (it goes into a constant)
2123 if (spv::BuiltInWorkgroupSize == BuiltinType) {
2124 std::vector<bool> HasMDVec;
2125 uint32_t PrevXDimCst = 0xFFFFFFFF;
2126 uint32_t PrevYDimCst = 0xFFFFFFFF;
2127 uint32_t PrevZDimCst = 0xFFFFFFFF;
2128 for (Function &Func : *GV.getParent()) {
2129 if (Func.isDeclaration()) {
2130 continue;
2131 }
2132
2133 // We only need to check kernels.
2134 if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) {
2135 continue;
2136 }
2137
2138 if (const MDNode *MD =
2139 dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) {
2140 uint32_t CurXDimCst = static_cast<uint32_t>(
2141 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
2142 uint32_t CurYDimCst = static_cast<uint32_t>(
2143 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue());
2144 uint32_t CurZDimCst = static_cast<uint32_t>(
2145 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue());
2146
2147 if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF &&
2148 PrevZDimCst == 0xFFFFFFFF) {
2149 PrevXDimCst = CurXDimCst;
2150 PrevYDimCst = CurYDimCst;
2151 PrevZDimCst = CurZDimCst;
2152 } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst ||
2153 CurZDimCst != PrevZDimCst) {
2154 llvm_unreachable(
2155 "reqd_work_group_size must be the same across all kernels");
2156 } else {
2157 continue;
2158 }
2159
2160 //
2161 // Generate OpConstantComposite.
2162 //
2163 // Ops[0] : Result Type ID
2164 // Ops[1] : Constant size for x dimension.
2165 // Ops[2] : Constant size for y dimension.
2166 // Ops[3] : Constant size for z dimension.
2167 SPIRVOperandList Ops;
2168
2169 uint32_t XDimCstID =
2170 VMap[mdconst::extract<ConstantInt>(MD->getOperand(0))];
2171 uint32_t YDimCstID =
2172 VMap[mdconst::extract<ConstantInt>(MD->getOperand(1))];
2173 uint32_t ZDimCstID =
2174 VMap[mdconst::extract<ConstantInt>(MD->getOperand(2))];
2175
2176 InitializerID = nextID;
2177
2178 Ops.push_back(
2179 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2180 lookupType(Ty->getPointerElementType())));
2181
2182 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, XDimCstID));
2183 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, YDimCstID));
2184 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, ZDimCstID));
2185
2186 SPIRVInstruction *Inst =
2187 new SPIRVInstruction(6, spv::OpConstantComposite, nextID++, Ops);
2188 SPIRVInstList.push_back(Inst);
2189
2190 HasMDVec.push_back(true);
2191 } else {
2192 HasMDVec.push_back(false);
2193 }
2194 }
2195
2196 // Check all kernels have same definitions for work_group_size.
2197 bool HasMD = false;
2198 if (!HasMDVec.empty()) {
2199 HasMD = HasMDVec[0];
2200 for (uint32_t i = 1; i < HasMDVec.size(); i++) {
2201 if (HasMD != HasMDVec[i]) {
2202 llvm_unreachable(
2203 "Kernels should have consistent work group size definition");
2204 }
2205 }
2206 }
2207
2208 // If all kernels do not have metadata for reqd_work_group_size, generate
2209 // OpSpecConstants for x/y/z dimension.
2210 if (!HasMD) {
2211 //
2212 // Generate OpSpecConstants for x/y/z dimension.
2213 //
2214 // Ops[0] : Result Type ID
2215 // Ops[1] : Constant size for x/y/z dimension (Literal Number).
2216 uint32_t XDimCstID = 0;
2217 uint32_t YDimCstID = 0;
2218 uint32_t ZDimCstID = 0;
2219
2220 // X Dimension
2221 SPIRVOperandList Ops;
2222
2223 Ops.push_back(new SPIRVOperand(
2224 SPIRVOperandType::NUMBERID,
2225 lookupType(Ty->getPointerElementType()->getSequentialElementType())));
2226
2227 std::vector<uint32_t> LiteralNum;
2228 LiteralNum.push_back(1);
2229 SPIRVOperand *XDim =
2230 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2231 Ops.push_back(XDim);
2232
2233 XDimCstID = nextID;
2234 BuiltinDimVec.push_back(XDimCstID);
2235
2236 SPIRVInstruction *XDimCstInst =
2237 new SPIRVInstruction(4, spv::OpSpecConstant, nextID++, Ops);
2238 SPIRVInstList.push_back(XDimCstInst);
2239
2240 // Y Dimension
2241 Ops.clear();
2242
2243 Ops.push_back(new SPIRVOperand(
2244 SPIRVOperandType::NUMBERID,
2245 lookupType(Ty->getPointerElementType()->getSequentialElementType())));
2246
2247 LiteralNum.clear();
2248 LiteralNum.push_back(1);
2249 SPIRVOperand *YDim =
2250 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2251 Ops.push_back(YDim);
2252
2253 YDimCstID = nextID;
2254 BuiltinDimVec.push_back(YDimCstID);
2255
2256 SPIRVInstruction *YDimCstInst =
2257 new SPIRVInstruction(4, spv::OpSpecConstant, nextID++, Ops);
2258 SPIRVInstList.push_back(YDimCstInst);
2259
2260 // Z Dimension
2261 Ops.clear();
2262
2263 Ops.push_back(new SPIRVOperand(
2264 SPIRVOperandType::NUMBERID,
2265 lookupType(Ty->getPointerElementType()->getSequentialElementType())));
2266
2267 LiteralNum.clear();
2268 LiteralNum.push_back(1);
2269 SPIRVOperand *ZDim =
2270 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2271 Ops.push_back(ZDim);
2272
2273 ZDimCstID = nextID;
2274 BuiltinDimVec.push_back(ZDimCstID);
2275
2276 SPIRVInstruction *ZDimCstInst =
2277 new SPIRVInstruction(4, spv::OpSpecConstant, nextID++, Ops);
2278 SPIRVInstList.push_back(ZDimCstInst);
2279
2280 //
2281 // Generate OpSpecConstantComposite.
2282 //
2283 // Ops[0] : Result Type ID
2284 // Ops[1] : Constant size for x dimension.
2285 // Ops[2] : Constant size for y dimension.
2286 // Ops[3] : Constant size for z dimension.
2287 InitializerID = nextID;
2288
2289 Ops.clear();
2290
2291 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
2292 lookupType(Ty->getPointerElementType())));
2293
2294 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, XDimCstID));
2295 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, YDimCstID));
2296 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, ZDimCstID));
2297
2298 SPIRVInstruction *Inst =
2299 new SPIRVInstruction(6, spv::OpSpecConstantComposite, nextID++, Ops);
2300 SPIRVInstList.push_back(Inst);
2301 }
2302 }
2303
2304 if (GV.hasInitializer()) {
2305 InitializerID = VMap[GV.getInitializer()];
2306 }
2307
2308 VMap[&GV] = nextID;
2309
2310 //
2311 // Generate OpVariable.
2312 //
2313 // GIDOps[0] : Result Type ID
2314 // GIDOps[1] : Storage Class
2315 SPIRVOperandList Ops;
2316
2317 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, lookupType(Ty)));
2318
2319 spv::StorageClass StorageClass = GetStorageClass(PTy->getAddressSpace());
2320 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, StorageClass));
2321
2322 if (0 != InitializerID) {
2323 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, InitializerID));
2324 }
2325
2326 SPIRVInstruction *Inst = new SPIRVInstruction(
2327 static_cast<uint16_t>(2 + Ops.size()), spv::OpVariable, nextID++, Ops);
2328 SPIRVInstList.push_back(Inst);
2329
2330 // If we have a builtin.
2331 if (spv::BuiltInMax != BuiltinType) {
2332 // Find Insert Point for OpDecorate.
2333 auto DecoInsertPoint =
2334 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
2335 [](SPIRVInstruction *Inst) -> bool {
2336 return Inst->getOpcode() != spv::OpDecorate &&
2337 Inst->getOpcode() != spv::OpMemberDecorate &&
2338 Inst->getOpcode() != spv::OpExtInstImport;
2339 });
2340 //
2341 // Generate OpDecorate.
2342 //
2343 // DOps[0] = Target ID
2344 // DOps[1] = Decoration (Builtin)
2345 // DOps[2] = BuiltIn ID
2346 uint32_t ResultID;
2347
2348 // WorkgroupSize is different, we decorate the constant composite that has
2349 // its value, rather than the variable that we use to access the value.
2350 if (spv::BuiltInWorkgroupSize == BuiltinType) {
2351 ResultID = InitializerID;
2352 } else {
2353 ResultID = VMap[&GV];
2354 }
2355
2356 SPIRVOperandList DOps;
2357 SPIRVOperand *ResultIDOp =
2358 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResultID);
2359 DOps.push_back(ResultIDOp);
2360
2361 spv::Decoration Deco = spv::DecorationBuiltIn;
2362 SPIRVOperand *DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2363 DOps.push_back(DecoOp);
2364
2365 SPIRVOperand *Builtin =
2366 new SPIRVOperand(SPIRVOperandType::NUMBERID, BuiltinType);
2367 DOps.push_back(Builtin);
2368
2369 SPIRVInstruction *DescDecoInst =
2370 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, DOps);
2371 SPIRVInstList.insert(DecoInsertPoint, DescDecoInst);
2372 }
2373}
2374
2375void SPIRVProducerPass::GenerateFuncPrologue(Function &F) {
2376 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
2377 ValueMapType &VMap = getValueMap();
2378 EntryPointVecType &EntryPoints = getEntryPointVec();
2379 ValueToValueMapTy &ArgGVMap = getArgumentGVMap();
2380 ValueMapType &ArgGVIDMap = getArgumentGVIDMap();
2381 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
2382 auto &GlobalConstArgSet = getGlobalConstArgSet();
David Netoc2c368d2017-06-30 16:50:17 -04002383 const DataLayout& dataLayout(F.getParent()->getDataLayout());
David Neto22f144c2017-06-12 14:26:21 -04002384
2385 FunctionType *FTy = F.getFunctionType();
2386
2387 //
2388 // Generate OpVariable and OpDecorate for kernel function with arguments.
2389 //
2390 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
2391
2392 // Find Insert Point for OpDecorate.
2393 auto DecoInsertPoint =
2394 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
2395 [](SPIRVInstruction *Inst) -> bool {
2396 return Inst->getOpcode() != spv::OpDecorate &&
2397 Inst->getOpcode() != spv::OpMemberDecorate &&
2398 Inst->getOpcode() != spv::OpExtInstImport;
2399 });
2400
2401 uint32_t DescriptorSetIdx = (0 < getSamplerMap().size()) ? 1u : 0u;
2402 for (Function &Func : *F.getParent()) {
2403 if (Func.isDeclaration()) {
2404 continue;
2405 }
2406
2407 if (Func.getCallingConv() == CallingConv::SPIR_KERNEL) {
2408 if (&Func == &F) {
2409 break;
2410 }
2411 DescriptorSetIdx++;
2412 }
2413 }
2414
David Neto156783e2017-07-05 15:39:41 -04002415 const auto *ArgMap = F.getMetadata("kernel_arg_map");
2416 // Emit descriptor map entries, if there was explicit metadata
2417 // attached.
2418 if (ArgMap) {
2419 for (const auto &arg : ArgMap->operands()) {
2420 const MDNode *arg_node = dyn_cast<MDNode>(arg.get());
2421 assert(arg_node->getNumOperands() == 4);
2422 const auto name =
2423 dyn_cast<MDString>(arg_node->getOperand(0))->getString();
2424 const auto old_index =
2425 dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue();
2426 const auto new_index =
2427 dyn_extract<ConstantInt>(arg_node->getOperand(2))->getZExtValue();
2428 const auto offset =
2429 dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue();
2430 descriptorMapOut << "kernel," << F.getName() << ",arg," << name
2431 << ",argOrdinal," << old_index << ",descriptorSet,"
2432 << DescriptorSetIdx << ",binding," << new_index
2433 << ",offset," << offset << "\n";
2434 }
2435 }
2436
David Neto22f144c2017-06-12 14:26:21 -04002437 uint32_t BindingIdx = 0;
2438 for (auto &Arg : F.args()) {
2439 Value *NewGV = ArgGVMap[&Arg];
2440 VMap[&Arg] = VMap[NewGV];
2441 ArgGVIDMap[&Arg] = VMap[&Arg];
2442
David Neto156783e2017-07-05 15:39:41 -04002443 // Emit a descriptor map entry for this arg, in case there was no explicit
2444 // kernel arg mapping metadata.
2445 if (!ArgMap) {
2446 descriptorMapOut << "kernel," << F.getName() << ",arg," << Arg.getName()
2447 << ",argOrdinal," << BindingIdx << ",descriptorSet,"
2448 << DescriptorSetIdx << ",binding," << BindingIdx
2449 << ",offset,0\n";
David Netoc2c368d2017-06-30 16:50:17 -04002450 }
2451
David Neto22f144c2017-06-12 14:26:21 -04002452 // Ops[0] = Target ID
2453 // Ops[1] = Decoration (DescriptorSet)
2454 // Ops[2] = LiteralNumber according to Decoration
2455 SPIRVOperandList Ops;
2456
2457 SPIRVOperand *ArgIDOp =
2458 new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[&Arg]);
2459 Ops.push_back(ArgIDOp);
2460
2461 spv::Decoration Deco = spv::DecorationDescriptorSet;
2462 SPIRVOperand *DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2463 Ops.push_back(DecoOp);
2464
2465 std::vector<uint32_t> LiteralNum;
2466 LiteralNum.push_back(DescriptorSetIdx);
2467 SPIRVOperand *DescSet =
2468 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2469 Ops.push_back(DescSet);
2470
2471 SPIRVInstruction *DescDecoInst =
2472 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2473 SPIRVInstList.insert(DecoInsertPoint, DescDecoInst);
2474
2475 // Ops[0] = Target ID
2476 // Ops[1] = Decoration (Binding)
2477 // Ops[2] = LiteralNumber according to Decoration
2478 Ops.clear();
2479
2480 Ops.push_back(ArgIDOp);
2481
2482 Deco = spv::DecorationBinding;
2483 DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2484 Ops.push_back(DecoOp);
2485
2486 LiteralNum.clear();
2487 LiteralNum.push_back(BindingIdx++);
2488 SPIRVOperand *Binding =
2489 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2490 Ops.push_back(Binding);
2491
2492 SPIRVInstruction *BindDecoInst =
2493 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2494 SPIRVInstList.insert(DecoInsertPoint, BindDecoInst);
2495
2496 // Handle image type argument.
2497 bool HasReadOnlyImageType = false;
2498 bool HasWriteOnlyImageType = false;
2499 if (PointerType *ArgPTy = dyn_cast<PointerType>(Arg.getType())) {
2500 if (StructType *STy = dyn_cast<StructType>(ArgPTy->getElementType())) {
2501 if (STy->isOpaque()) {
2502 if (STy->getName().equals("opencl.image2d_ro_t") ||
2503 STy->getName().equals("opencl.image3d_ro_t")) {
2504 HasReadOnlyImageType = true;
2505 } else if (STy->getName().equals("opencl.image2d_wo_t") ||
2506 STy->getName().equals("opencl.image3d_wo_t")) {
2507 HasWriteOnlyImageType = true;
2508 }
2509 }
2510 }
2511 }
2512
2513 if (HasReadOnlyImageType || HasWriteOnlyImageType) {
2514 // Ops[0] = Target ID
2515 // Ops[1] = Decoration (NonReadable or NonWritable)
2516 Ops.clear();
2517
2518 ArgIDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[&Arg]);
2519 Ops.push_back(ArgIDOp);
2520
2521 Deco = spv::DecorationNonReadable;
2522 if (HasReadOnlyImageType) {
2523 Deco = spv::DecorationNonWritable;
2524 }
2525 DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2526 Ops.push_back(DecoOp);
2527
2528 DescDecoInst =
2529 new SPIRVInstruction(3, spv::OpDecorate, 0 /* No id */, Ops);
2530 SPIRVInstList.insert(DecoInsertPoint, DescDecoInst);
2531 }
2532
2533 // Handle const address space.
2534 if (NewGV->getType()->getPointerAddressSpace() ==
2535 AddressSpace::Constant) {
2536 // Ops[0] = Target ID
2537 // Ops[1] = Decoration (NonWriteable)
2538 Ops.clear();
2539
2540 Ops.push_back(ArgIDOp);
2541
2542 Deco = spv::DecorationNonWritable;
2543 DecoOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Deco);
2544 Ops.push_back(DecoOp);
2545
2546 BindDecoInst =
2547 new SPIRVInstruction(3, spv::OpDecorate, 0 /* No id */, Ops);
2548 SPIRVInstList.insert(DecoInsertPoint, BindDecoInst);
2549 }
2550 }
2551 }
2552
2553 //
2554 // Generate OPFunction.
2555 //
2556
2557 // FOps[0] : Result Type ID
2558 // FOps[1] : Function Control
2559 // FOps[2] : Function Type ID
2560 SPIRVOperandList FOps;
2561
2562 // Find SPIRV instruction for return type.
2563 uint32_t RetTyID = lookupType(FTy->getReturnType());
2564 SPIRVOperand *RetTyOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, RetTyID);
2565 FOps.push_back(RetTyOp);
2566
2567 // Check function attributes for SPIRV Function Control.
2568 uint32_t FuncControl = spv::FunctionControlMaskNone;
2569 if (F.hasFnAttribute(Attribute::AlwaysInline)) {
2570 FuncControl |= spv::FunctionControlInlineMask;
2571 }
2572 if (F.hasFnAttribute(Attribute::NoInline)) {
2573 FuncControl |= spv::FunctionControlDontInlineMask;
2574 }
2575 // TODO: Check llvm attribute for Function Control Pure.
2576 if (F.hasFnAttribute(Attribute::ReadOnly)) {
2577 FuncControl |= spv::FunctionControlPureMask;
2578 }
2579 // TODO: Check llvm attribute for Function Control Const.
2580 if (F.hasFnAttribute(Attribute::ReadNone)) {
2581 FuncControl |= spv::FunctionControlConstMask;
2582 }
2583
2584 SPIRVOperand *FunctionControlOp =
2585 new SPIRVOperand(SPIRVOperandType::NUMBERID, FuncControl);
2586 FOps.push_back(FunctionControlOp);
2587
2588 uint32_t FTyID;
2589 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
2590 SmallVector<Type *, 4> NewFuncParamTys;
2591 FunctionType *NewFTy =
2592 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
2593 FTyID = lookupType(NewFTy);
2594 } else {
2595 // Handle function with global constant parameters.
2596 if (GlobalConstFuncTyMap.count(FTy)) {
2597 FTyID = lookupType(GlobalConstFuncTyMap[FTy].first);
2598 } else {
2599 FTyID = lookupType(FTy);
2600 }
2601 }
2602
2603 SPIRVOperand *FTyOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, FTyID);
2604 FOps.push_back(FTyOp);
2605
2606 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
2607 EntryPoints.push_back(std::make_pair(&F, nextID));
2608 }
2609
2610 VMap[&F] = nextID;
2611
2612 // Generate SPIRV instruction for function.
2613 SPIRVInstruction *FuncInst =
2614 new SPIRVInstruction(5, spv::OpFunction, nextID++, FOps);
2615 SPIRVInstList.push_back(FuncInst);
2616
2617 //
2618 // Generate OpFunctionParameter for Normal function.
2619 //
2620
2621 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
2622 // Iterate Argument for name instead of param type from function type.
2623 unsigned ArgIdx = 0;
2624 for (Argument &Arg : F.args()) {
2625 VMap[&Arg] = nextID;
2626
2627 // ParamOps[0] : Result Type ID
2628 SPIRVOperandList ParamOps;
2629
2630 // Find SPIRV instruction for parameter type.
2631 uint32_t ParamTyID = lookupType(Arg.getType());
2632 if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) {
2633 if (GlobalConstFuncTyMap.count(FTy)) {
2634 if (ArgIdx == GlobalConstFuncTyMap[FTy].second) {
2635 Type *EleTy = PTy->getPointerElementType();
2636 Type *ArgTy =
2637 PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
2638 ParamTyID = lookupType(ArgTy);
2639 GlobalConstArgSet.insert(&Arg);
2640 }
2641 }
2642 }
2643 SPIRVOperand *ParamTyOp =
2644 new SPIRVOperand(SPIRVOperandType::NUMBERID, ParamTyID);
2645 ParamOps.push_back(ParamTyOp);
2646
2647 // Generate SPIRV instruction for parameter.
2648 SPIRVInstruction *ParamInst =
2649 new SPIRVInstruction(3, spv::OpFunctionParameter, nextID++, ParamOps);
2650 SPIRVInstList.push_back(ParamInst);
2651
2652 ArgIdx++;
2653 }
2654 }
2655}
2656
2657void SPIRVProducerPass::GenerateModuleInfo() {
2658 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
2659 EntryPointVecType &EntryPoints = getEntryPointVec();
2660 ValueMapType &VMap = getValueMap();
2661 ValueList &EntryPointInterfaces = getEntryPointInterfacesVec();
2662 uint32_t &ExtInstImportID = getOpExtInstImportID();
2663 std::vector<uint32_t> &BuiltinDimVec = getBuiltinDimVec();
2664
2665 // Set up insert point.
2666 auto InsertPoint = SPIRVInstList.begin();
2667
2668 //
2669 // Generate OpCapability
2670 //
2671 // TODO: Which llvm information is mapped to SPIRV Capapbility?
2672
2673 // Ops[0] = Capability
2674 SPIRVOperandList Ops;
2675
2676 SPIRVInstruction *CapInst = new SPIRVInstruction(
2677 2, spv::OpCapability, 0 /* No id */,
2678 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::CapabilityShader));
2679 SPIRVInstList.insert(InsertPoint, CapInst);
2680
2681 for (Type *Ty : getTypeList()) {
2682 // Find the i16 type.
2683 if (Ty->isIntegerTy(16)) {
2684 // Generate OpCapability for i16 type.
2685 SPIRVInstList.insert(
2686 InsertPoint,
2687 new SPIRVInstruction(2, spv::OpCapability, 0 /* No id */,
2688 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2689 spv::CapabilityInt16)));
2690 } else if (Ty->isIntegerTy(64)) {
2691 // Generate OpCapability for i64 type.
2692 SPIRVInstList.insert(
2693 InsertPoint,
2694 new SPIRVInstruction(2, spv::OpCapability, 0 /* No id */,
2695 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2696 spv::CapabilityInt64)));
2697 } else if (Ty->isHalfTy()) {
2698 // Generate OpCapability for half type.
2699 SPIRVInstList.insert(
2700 InsertPoint,
2701 new SPIRVInstruction(2, spv::OpCapability, 0 /* No id */,
2702 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2703 spv::CapabilityFloat16)));
2704 } else if (Ty->isDoubleTy()) {
2705 // Generate OpCapability for double type.
2706 SPIRVInstList.insert(
2707 InsertPoint,
2708 new SPIRVInstruction(2, spv::OpCapability, 0 /* No id */,
2709 new SPIRVOperand(SPIRVOperandType::NUMBERID,
2710 spv::CapabilityFloat64)));
2711 } else if (auto *STy = dyn_cast<StructType>(Ty)) {
2712 if (STy->isOpaque()) {
2713 if (STy->getName().equals("opencl.image2d_ro_t") ||
2714 STy->getName().equals("opencl.image3d_ro_t")) {
2715 // Generate OpCapability for read only image type.
2716 SPIRVInstList.insert(
2717 InsertPoint,
2718 new SPIRVInstruction(
2719 2, spv::OpCapability, 0 /* No id */,
2720 new SPIRVOperand(
2721 SPIRVOperandType::NUMBERID,
2722 spv::CapabilityStorageImageReadWithoutFormat)));
2723 } else if (STy->getName().equals("opencl.image2d_wo_t") ||
2724 STy->getName().equals("opencl.image3d_wo_t")) {
2725 // Generate OpCapability for write only image type.
2726 SPIRVInstList.insert(
2727 InsertPoint,
2728 new SPIRVInstruction(
2729 2, spv::OpCapability, 0 /* No id */,
2730 new SPIRVOperand(
2731 SPIRVOperandType::NUMBERID,
2732 spv::CapabilityStorageImageWriteWithoutFormat)));
2733 }
2734 }
2735 }
2736 }
2737
2738 if (hasVariablePointers()) {
2739 //
2740 // Generate OpCapability and OpExtension
2741 //
2742
2743 //
2744 // Generate OpCapability.
2745 //
2746 // Ops[0] = Capability
2747 //
2748 Ops.clear();
2749
2750 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
2751 spv::CapabilityVariablePointers));
2752
2753 SPIRVInstList.insert(InsertPoint, new SPIRVInstruction(2, spv::OpCapability,
2754 0 /* No id */, Ops));
2755
2756 //
2757 // Generate OpExtension.
2758 //
2759 // Ops[0] = Name (Literal String)
2760 //
David Netoa772fd12017-08-04 14:17:33 -04002761 for (auto extension : {"SPV_KHR_storage_buffer_storage_class",
2762 "SPV_KHR_variable_pointers"}) {
2763 Ops.clear();
David Neto22f144c2017-06-12 14:26:21 -04002764
David Netoa772fd12017-08-04 14:17:33 -04002765 SPIRVOperand *Name =
2766 new SPIRVOperand(SPIRVOperandType::LITERAL_STRING, extension);
2767 Ops.push_back(Name);
David Neto22f144c2017-06-12 14:26:21 -04002768
David Netoa772fd12017-08-04 14:17:33 -04002769 size_t NameWordSize = (Name->getLiteralStr().size() + 1) / 4;
2770 if ((Name->getLiteralStr().size() + 1) % 4) {
2771 NameWordSize += 1;
2772 }
2773
2774 assert((NameWordSize + 1) < UINT16_MAX);
2775 uint16_t WordCount = static_cast<uint16_t>(1 + NameWordSize);
2776
2777 SPIRVInstruction *ExtensionInst =
2778 new SPIRVInstruction(WordCount, spv::OpExtension, 0 /* No id */, Ops);
2779 SPIRVInstList.insert(InsertPoint, ExtensionInst);
David Neto22f144c2017-06-12 14:26:21 -04002780 }
David Neto22f144c2017-06-12 14:26:21 -04002781 }
2782
2783 if (ExtInstImportID) {
2784 ++InsertPoint;
2785 }
2786
2787 //
2788 // Generate OpMemoryModel
2789 //
2790 // Memory model for Vulkan will always be GLSL450.
2791
2792 // Ops[0] = Addressing Model
2793 // Ops[1] = Memory Model
2794 Ops.clear();
2795 SPIRVOperand *AddrModel =
2796 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::AddressingModelLogical);
2797 Ops.push_back(AddrModel);
2798
2799 SPIRVOperand *MemModel =
2800 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::MemoryModelGLSL450);
2801 Ops.push_back(MemModel);
2802
2803 SPIRVInstruction *MemModelInst =
2804 new SPIRVInstruction(3, spv::OpMemoryModel, 0 /* No id */, Ops);
2805 SPIRVInstList.insert(InsertPoint, MemModelInst);
2806
2807 //
2808 // Generate OpEntryPoint
2809 //
2810 for (auto EntryPoint : EntryPoints) {
2811 // Ops[0] = Execution Model
2812 // Ops[1] = EntryPoint ID
2813 // Ops[2] = Name (Literal String)
2814 // ...
2815 //
2816 // TODO: Do we need to consider Interface ID for forward references???
2817 Ops.clear();
2818 SPIRVOperand *ExecModel = new SPIRVOperand(SPIRVOperandType::NUMBERID,
2819 spv::ExecutionModelGLCompute);
2820 Ops.push_back(ExecModel);
2821
2822 SPIRVOperand *EntryPointID =
2823 new SPIRVOperand(SPIRVOperandType::NUMBERID, EntryPoint.second);
2824 Ops.push_back(EntryPointID);
2825
2826 SPIRVOperand *Name = new SPIRVOperand(SPIRVOperandType::LITERAL_STRING,
2827 EntryPoint.first->getName());
2828 Ops.push_back(Name);
2829
2830 size_t NameWordSize = (Name->getLiteralStr().size() + 1) / 4;
2831 if ((Name->getLiteralStr().size() + 1) % 4) {
2832 NameWordSize += 1;
2833 }
2834
2835 assert((3 + NameWordSize) < UINT16_MAX);
2836 uint16_t WordCount = static_cast<uint16_t>(3 + NameWordSize);
2837
2838 for (Value *Interface : EntryPointInterfaces) {
2839 SPIRVOperand *GIDOp =
2840 new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[Interface]);
2841 Ops.push_back(GIDOp);
2842 WordCount++;
2843 }
2844
2845 SPIRVInstruction *EntryPointInst =
2846 new SPIRVInstruction(WordCount, spv::OpEntryPoint, 0 /* No id */, Ops);
2847 SPIRVInstList.insert(InsertPoint, EntryPointInst);
2848 }
2849
2850 for (auto EntryPoint : EntryPoints) {
2851 if (const MDNode *MD = dyn_cast<Function>(EntryPoint.first)
2852 ->getMetadata("reqd_work_group_size")) {
2853
2854 if (!BuiltinDimVec.empty()) {
2855 llvm_unreachable(
2856 "Kernels should have consistent work group size definition");
2857 }
2858
2859 //
2860 // Generate OpExecutionMode
2861 //
2862
2863 // Ops[0] = Entry Point ID
2864 // Ops[1] = Execution Mode
2865 // Ops[2] ... Ops[n] = Optional literals according to Execution Mode
2866 Ops.clear();
2867 SPIRVOperand *EntryPointID =
2868 new SPIRVOperand(SPIRVOperandType::NUMBERID, EntryPoint.second);
2869 Ops.push_back(EntryPointID);
2870
2871 SPIRVOperand *ExecMode = new SPIRVOperand(SPIRVOperandType::NUMBERID,
2872 spv::ExecutionModeLocalSize);
2873 Ops.push_back(ExecMode);
2874
2875 uint32_t XDim = static_cast<uint32_t>(
2876 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
2877 uint32_t YDim = static_cast<uint32_t>(
2878 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue());
2879 uint32_t ZDim = static_cast<uint32_t>(
2880 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue());
2881
2882 std::vector<uint32_t> LiteralNum;
2883 LiteralNum.push_back(XDim);
2884 SPIRVOperand *XDimOp =
2885 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2886 Ops.push_back(XDimOp);
2887
2888 LiteralNum.clear();
2889 LiteralNum.push_back(YDim);
2890 SPIRVOperand *YDimOp =
2891 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2892 Ops.push_back(YDimOp);
2893
2894 LiteralNum.clear();
2895 LiteralNum.push_back(ZDim);
2896 SPIRVOperand *ZDimOp =
2897 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2898 Ops.push_back(ZDimOp);
2899
2900 SPIRVInstruction *ExecModeInst =
2901 new SPIRVInstruction(static_cast<uint16_t>(1 + Ops.size()),
2902 spv::OpExecutionMode, 0 /* No id */, Ops);
2903 SPIRVInstList.insert(InsertPoint, ExecModeInst);
2904 }
2905 }
2906
2907 //
2908 // Generate OpSource.
2909 //
2910 // Ops[0] = SourceLanguage ID
2911 // Ops[1] = Version (LiteralNum)
2912 //
2913 Ops.clear();
2914 SPIRVOperand *SourceLanguage =
2915 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::SourceLanguageOpenCL_C);
2916 Ops.push_back(SourceLanguage);
2917
2918 std::vector<uint32_t> LiteralNum;
2919 LiteralNum.push_back(120);
2920 SPIRVOperand *Version =
2921 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2922 Ops.push_back(Version);
2923
2924 SPIRVInstruction *OpenSourceInst =
2925 new SPIRVInstruction(3, spv::OpSource, 0 /* No id */, Ops);
2926 SPIRVInstList.insert(InsertPoint, OpenSourceInst);
2927
2928 if (!BuiltinDimVec.empty()) {
2929 //
2930 // Generate OpDecorates for x/y/z dimension.
2931 //
2932 // Ops[0] = Target ID
2933 // Ops[1] = Decoration (SpecId)
2934 // Ops[2] = Specialization Cosntant ID (Literal Number)
2935
2936 // X Dimension
2937 Ops.clear();
2938
2939 SPIRVOperand *TargetID =
2940 new SPIRVOperand(SPIRVOperandType::NUMBERID, BuiltinDimVec[0]);
2941 Ops.push_back(TargetID);
2942
2943 SPIRVOperand *DecoOp =
2944 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::DecorationSpecId);
2945 Ops.push_back(DecoOp);
2946
2947 LiteralNum.clear();
2948 LiteralNum.push_back(0);
2949 SPIRVOperand *XDim =
2950 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2951 Ops.push_back(XDim);
2952
2953 SPIRVInstruction *XDimDecoInst =
2954 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2955 SPIRVInstList.insert(InsertPoint, XDimDecoInst);
2956
2957 // Y Dimension
2958 Ops.clear();
2959
2960 TargetID = new SPIRVOperand(SPIRVOperandType::NUMBERID, BuiltinDimVec[1]);
2961 Ops.push_back(TargetID);
2962
2963 DecoOp =
2964 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::DecorationSpecId);
2965 Ops.push_back(DecoOp);
2966
2967 LiteralNum.clear();
2968 LiteralNum.push_back(1);
2969 SPIRVOperand *YDim =
2970 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2971 Ops.push_back(YDim);
2972
2973 SPIRVInstruction *YDimDecoInst =
2974 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2975 SPIRVInstList.insert(InsertPoint, YDimDecoInst);
2976
2977 // Z Dimension
2978 Ops.clear();
2979
2980 TargetID = new SPIRVOperand(SPIRVOperandType::NUMBERID, BuiltinDimVec[2]);
2981 Ops.push_back(TargetID);
2982
2983 DecoOp =
2984 new SPIRVOperand(SPIRVOperandType::NUMBERID, spv::DecorationSpecId);
2985 Ops.push_back(DecoOp);
2986
2987 LiteralNum.clear();
2988 LiteralNum.push_back(2);
2989 SPIRVOperand *ZDim =
2990 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
2991 Ops.push_back(ZDim);
2992
2993 SPIRVInstruction *ZDimDecoInst =
2994 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
2995 SPIRVInstList.insert(InsertPoint, ZDimDecoInst);
2996 }
2997}
2998
2999void SPIRVProducerPass::GenerateInstForArg(Function &F) {
3000 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
3001 ValueMapType &VMap = getValueMap();
3002 Module *Module = F.getParent();
3003 LLVMContext &Context = Module->getContext();
3004 ValueToValueMapTy &ArgGVMap = getArgumentGVMap();
3005
3006 for (Argument &Arg : F.args()) {
3007 if (Arg.use_empty()) {
3008 continue;
3009 }
3010
3011 // Check the type of users of arguments.
3012 bool HasOnlyGEPUse = true;
3013 for (auto *U : Arg.users()) {
3014 if (!isa<GetElementPtrInst>(U) && isa<Instruction>(U)) {
3015 HasOnlyGEPUse = false;
3016 break;
3017 }
3018 }
3019
3020 Type *ArgTy = Arg.getType();
3021
3022 if (PointerType *PTy = dyn_cast<PointerType>(ArgTy)) {
3023 if (StructType *STy = dyn_cast<StructType>(PTy->getElementType())) {
3024 if (STy->isOpaque()) {
3025 // Generate OpLoad for sampler and image types.
3026 if (STy->getName().equals("opencl.sampler_t") ||
3027 STy->getName().equals("opencl.image2d_ro_t") ||
3028 STy->getName().equals("opencl.image2d_wo_t") ||
3029 STy->getName().equals("opencl.image3d_ro_t") ||
3030 STy->getName().equals("opencl.image3d_wo_t")) {
3031 //
3032 // Generate OpLoad.
3033 //
3034 // Ops[0] = Result Type ID
3035 // Ops[1] = Pointer ID
3036 // Ops[2] ... Ops[n] = Optional Memory Access
3037 //
3038 // TODO: Do we need to implement Optional Memory Access???
3039 SPIRVOperandList Ops;
3040
3041 // Use type with address space modified.
3042 ArgTy = ArgGVMap[&Arg]->getType()->getPointerElementType();
3043
3044 uint32_t ResTyID = lookupType(ArgTy);
3045 SPIRVOperand *ResTyIDOp =
3046 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3047 Ops.push_back(ResTyIDOp);
3048
3049 uint32_t PointerID = VMap[&Arg];
3050 SPIRVOperand *PointerIDOp =
3051 new SPIRVOperand(SPIRVOperandType::NUMBERID, PointerID);
3052 Ops.push_back(PointerIDOp);
3053
3054 VMap[&Arg] = nextID;
3055 SPIRVInstruction *Inst =
3056 new SPIRVInstruction(4, spv::OpLoad, nextID++, Ops);
3057 SPIRVInstList.push_back(Inst);
3058 continue;
3059 }
3060 }
3061 }
3062
3063 if (!HasOnlyGEPUse) {
3064 //
3065 // Generate OpAccessChain.
3066 //
3067 // Ops[0] = Result Type ID
3068 // Ops[1] = Base ID
3069 // Ops[2] ... Ops[n] = Indexes ID
3070 SPIRVOperandList Ops;
3071
3072 uint32_t ResTyID = lookupType(ArgTy);
3073 if (!isa<PointerType>(ArgTy)) {
3074 ResTyID = lookupType(PointerType::get(ArgTy, AddressSpace::Global));
3075 }
3076 SPIRVOperand *ResTyOp =
3077 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3078 Ops.push_back(ResTyOp);
3079
3080 uint32_t BaseID = VMap[&Arg];
3081 SPIRVOperand *BaseOp =
3082 new SPIRVOperand(SPIRVOperandType::NUMBERID, BaseID);
3083 Ops.push_back(BaseOp);
3084
3085 Type *IdxTy = Type::getInt32Ty(Context);
3086 uint32_t IndexID = VMap[ConstantInt::get(IdxTy, 0)];
3087 SPIRVOperand *IndexIDOp =
3088 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3089 Ops.push_back(IndexIDOp);
3090 Ops.push_back(IndexIDOp);
3091
3092 // Generate SPIRV instruction for argument.
3093 VMap[&Arg] = nextID;
3094 SPIRVInstruction *ArgInst =
3095 new SPIRVInstruction(6, spv::OpAccessChain, nextID++, Ops);
3096 SPIRVInstList.push_back(ArgInst);
3097 } else {
3098 // For GEP uses, generate OpAccessChain with folding GEP ahead of GEP.
3099 // Nothing to do here.
3100 }
3101 } else {
3102 //
3103 // Generate OpAccessChain and OpLoad for non-pointer type argument.
3104 //
3105
3106 //
3107 // Generate OpAccessChain.
3108 //
3109 // Ops[0] = Result Type ID
3110 // Ops[1] = Base ID
3111 // Ops[2] ... Ops[n] = Indexes ID
3112 SPIRVOperandList Ops;
3113
3114 uint32_t ResTyID = lookupType(ArgTy);
3115 if (!isa<PointerType>(ArgTy)) {
3116 ResTyID = lookupType(PointerType::get(ArgTy, AddressSpace::Global));
3117 }
3118 SPIRVOperand *ResTyIDOp =
3119 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3120 Ops.push_back(ResTyIDOp);
3121
3122 uint32_t BaseID = VMap[&Arg];
3123 SPIRVOperand *BaseOp =
3124 new SPIRVOperand(SPIRVOperandType::NUMBERID, BaseID);
3125 Ops.push_back(BaseOp);
3126
3127 Type *IdxTy = Type::getInt32Ty(Context);
3128 uint32_t IndexID = VMap[ConstantInt::get(IdxTy, 0)];
3129 SPIRVOperand *IndexIDOp =
3130 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3131 Ops.push_back(IndexIDOp);
3132
3133 // Generate SPIRV instruction for argument.
3134 uint32_t PointerID = nextID;
3135 VMap[&Arg] = nextID;
3136 SPIRVInstruction *ArgInst =
3137 new SPIRVInstruction(5, spv::OpAccessChain, nextID++, Ops);
3138 SPIRVInstList.push_back(ArgInst);
3139
3140 //
3141 // Generate OpLoad.
3142 //
3143
3144 // Ops[0] = Result Type ID
3145 // Ops[1] = Pointer ID
3146 // Ops[2] ... Ops[n] = Optional Memory Access
3147 //
3148 // TODO: Do we need to implement Optional Memory Access???
3149 Ops.clear();
3150
3151 ResTyID = lookupType(ArgTy);
3152 ResTyIDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3153 Ops.push_back(ResTyIDOp);
3154
3155 SPIRVOperand *PointerIDOp =
3156 new SPIRVOperand(SPIRVOperandType::NUMBERID, PointerID);
3157 Ops.push_back(PointerIDOp);
3158
3159 VMap[&Arg] = nextID;
3160 SPIRVInstruction *Inst =
3161 new SPIRVInstruction(4, spv::OpLoad, nextID++, Ops);
3162 SPIRVInstList.push_back(Inst);
3163 }
3164 }
3165}
3166
3167void SPIRVProducerPass::GenerateFuncBody(Function &F) {
3168 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
3169 ValueMapType &VMap = getValueMap();
3170
3171 bool IsKernel = false;
3172 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
3173 IsKernel = true;
3174 }
3175
3176 for (BasicBlock &BB : F) {
3177 // Register BasicBlock to ValueMap.
3178 VMap[&BB] = nextID;
3179
3180 //
3181 // Generate OpLabel for Basic Block.
3182 //
3183 SPIRVOperandList Ops;
3184 SPIRVInstruction *Inst =
3185 new SPIRVInstruction(2, spv::OpLabel, nextID++, Ops);
3186 SPIRVInstList.push_back(Inst);
3187
David Neto6dcd4712017-06-23 11:06:47 -04003188 // OpVariable instructions must come first.
3189 for (Instruction &I : BB) {
3190 if (isa<AllocaInst>(I)) {
3191 GenerateInstruction(I);
3192 }
3193 }
3194
David Neto22f144c2017-06-12 14:26:21 -04003195 if (&BB == &F.getEntryBlock() && IsKernel) {
3196 GenerateInstForArg(F);
3197 }
3198
3199 for (Instruction &I : BB) {
David Neto6dcd4712017-06-23 11:06:47 -04003200 if (!isa<AllocaInst>(I)) {
3201 GenerateInstruction(I);
3202 }
David Neto22f144c2017-06-12 14:26:21 -04003203 }
3204 }
3205}
3206
3207spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) {
3208 const std::map<CmpInst::Predicate, spv::Op> Map = {
3209 {CmpInst::ICMP_EQ, spv::OpIEqual},
3210 {CmpInst::ICMP_NE, spv::OpINotEqual},
3211 {CmpInst::ICMP_UGT, spv::OpUGreaterThan},
3212 {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual},
3213 {CmpInst::ICMP_ULT, spv::OpULessThan},
3214 {CmpInst::ICMP_ULE, spv::OpULessThanEqual},
3215 {CmpInst::ICMP_SGT, spv::OpSGreaterThan},
3216 {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual},
3217 {CmpInst::ICMP_SLT, spv::OpSLessThan},
3218 {CmpInst::ICMP_SLE, spv::OpSLessThanEqual},
3219 {CmpInst::FCMP_OEQ, spv::OpFOrdEqual},
3220 {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan},
3221 {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual},
3222 {CmpInst::FCMP_OLT, spv::OpFOrdLessThan},
3223 {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual},
3224 {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual},
3225 {CmpInst::FCMP_UEQ, spv::OpFUnordEqual},
3226 {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan},
3227 {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual},
3228 {CmpInst::FCMP_ULT, spv::OpFUnordLessThan},
3229 {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual},
3230 {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}};
3231
3232 assert(0 != Map.count(I->getPredicate()));
3233
3234 return Map.at(I->getPredicate());
3235}
3236
3237spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) {
3238 const std::map<unsigned, spv::Op> Map{
3239 {Instruction::Trunc, spv::OpUConvert},
3240 {Instruction::ZExt, spv::OpUConvert},
3241 {Instruction::SExt, spv::OpSConvert},
3242 {Instruction::FPToUI, spv::OpConvertFToU},
3243 {Instruction::FPToSI, spv::OpConvertFToS},
3244 {Instruction::UIToFP, spv::OpConvertUToF},
3245 {Instruction::SIToFP, spv::OpConvertSToF},
3246 {Instruction::FPTrunc, spv::OpFConvert},
3247 {Instruction::FPExt, spv::OpFConvert},
3248 {Instruction::BitCast, spv::OpBitcast}};
3249
3250 assert(0 != Map.count(I.getOpcode()));
3251
3252 return Map.at(I.getOpcode());
3253}
3254
3255spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) {
3256 if (I.getType()->isIntegerTy(1)) {
3257 switch (I.getOpcode()) {
3258 default:
3259 break;
3260 case Instruction::Or:
3261 return spv::OpLogicalOr;
3262 case Instruction::And:
3263 return spv::OpLogicalAnd;
3264 case Instruction::Xor:
3265 return spv::OpLogicalNotEqual;
3266 }
3267 }
3268
3269 const std::map<unsigned, spv::Op> Map {
3270 {Instruction::Add, spv::OpIAdd},
3271 {Instruction::FAdd, spv::OpFAdd},
3272 {Instruction::Sub, spv::OpISub},
3273 {Instruction::FSub, spv::OpFSub},
3274 {Instruction::Mul, spv::OpIMul},
3275 {Instruction::FMul, spv::OpFMul},
3276 {Instruction::UDiv, spv::OpUDiv},
3277 {Instruction::SDiv, spv::OpSDiv},
3278 {Instruction::FDiv, spv::OpFDiv},
3279 {Instruction::URem, spv::OpUMod},
3280 {Instruction::SRem, spv::OpSRem},
3281 {Instruction::FRem, spv::OpFRem},
3282 {Instruction::Or, spv::OpBitwiseOr},
3283 {Instruction::Xor, spv::OpBitwiseXor},
3284 {Instruction::And, spv::OpBitwiseAnd},
3285 {Instruction::Shl, spv::OpShiftLeftLogical},
3286 {Instruction::LShr, spv::OpShiftRightLogical},
3287 {Instruction::AShr, spv::OpShiftRightArithmetic}};
3288
3289 assert(0 != Map.count(I.getOpcode()));
3290
3291 return Map.at(I.getOpcode());
3292}
3293
3294void SPIRVProducerPass::GenerateInstruction(Instruction &I) {
3295 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
3296 ValueMapType &VMap = getValueMap();
3297 ValueToValueMapTy &ArgGVMap = getArgumentGVMap();
3298 ValueMapType &ArgGVIDMap = getArgumentGVIDMap();
3299 DeferredInstVecType &DeferredInsts = getDeferredInstVec();
3300 LLVMContext &Context = I.getParent()->getParent()->getParent()->getContext();
3301
3302 // Register Instruction to ValueMap.
3303 if (0 == VMap[&I]) {
3304 VMap[&I] = nextID;
3305 }
3306
3307 switch (I.getOpcode()) {
3308 default: {
3309 if (Instruction::isCast(I.getOpcode())) {
3310 //
3311 // Generate SPIRV instructions for cast operators.
3312 //
3313
3314 auto OpTy = I.getOperand(0)->getType();
3315 // Handle zext, sext and uitofp with i1 type specially.
3316 if ((I.getOpcode() == Instruction::ZExt ||
3317 I.getOpcode() == Instruction::SExt ||
3318 I.getOpcode() == Instruction::UIToFP) &&
3319 (OpTy->isIntegerTy(1) ||
3320 (OpTy->isVectorTy() &&
3321 OpTy->getVectorElementType()->isIntegerTy(1)))) {
3322 //
3323 // Generate OpSelect.
3324 //
3325
3326 // Ops[0] = Result Type ID
3327 // Ops[1] = Condition ID
3328 // Ops[2] = True Constant ID
3329 // Ops[3] = False Constant ID
3330 SPIRVOperandList Ops;
3331
3332 uint32_t ResTyID = lookupType(I.getType());
3333 SPIRVOperand *ResTyIDOp =
3334 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3335 Ops.push_back(ResTyIDOp);
3336
3337 // TODO: zext's first operand should be compare instructions???
3338 uint32_t CondID = VMap[I.getOperand(0)];
3339 SPIRVOperand *CondIDOp =
3340 new SPIRVOperand(SPIRVOperandType::NUMBERID, CondID);
3341 Ops.push_back(CondIDOp);
3342
3343 uint32_t TrueID = 0;
3344 if (I.getOpcode() == Instruction::ZExt) {
3345 APInt One(32, 1);
3346 TrueID = VMap[Constant::getIntegerValue(I.getType(), One)];
3347 } else if (I.getOpcode() == Instruction::SExt) {
3348 APInt MinusOne(32, UINT64_MAX, true);
3349 TrueID = VMap[Constant::getIntegerValue(I.getType(), MinusOne)];
3350 } else {
3351 TrueID = VMap[ConstantFP::get(Context, APFloat(1.0f))];
3352 }
3353 SPIRVOperand *TrueIDOp =
3354 new SPIRVOperand(SPIRVOperandType::NUMBERID, TrueID);
3355 Ops.push_back(TrueIDOp);
3356
3357 uint32_t FalseID = 0;
3358 if (I.getOpcode() == Instruction::ZExt) {
3359 FalseID = VMap[Constant::getNullValue(I.getType())];
3360 } else if (I.getOpcode() == Instruction::SExt) {
3361 FalseID = VMap[Constant::getNullValue(I.getType())];
3362 } else {
3363 FalseID = VMap[ConstantFP::get(Context, APFloat(0.0f))];
3364 }
3365 SPIRVOperand *FalseIDOp =
3366 new SPIRVOperand(SPIRVOperandType::NUMBERID, FalseID);
3367 Ops.push_back(FalseIDOp);
3368
3369 SPIRVInstruction *Inst =
3370 new SPIRVInstruction(6, spv::OpSelect, nextID++, Ops);
3371 SPIRVInstList.push_back(Inst);
3372 } else {
3373 // Ops[0] = Result Type ID
3374 // Ops[1] = Source Value ID
3375 SPIRVOperandList Ops;
3376
3377 uint32_t ResTyID = lookupType(I.getType());
3378 SPIRVOperand *ResTyIDOp =
3379 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3380 Ops.push_back(ResTyIDOp);
3381
3382 uint32_t SrcID = VMap[I.getOperand(0)];
3383 SPIRVOperand *SrcIDOp =
3384 new SPIRVOperand(SPIRVOperandType::NUMBERID, SrcID);
3385 Ops.push_back(SrcIDOp);
3386
3387 SPIRVInstruction *Inst =
3388 new SPIRVInstruction(4, GetSPIRVCastOpcode(I), nextID++, Ops);
3389 SPIRVInstList.push_back(Inst);
3390 }
3391 } else if (isa<BinaryOperator>(I)) {
3392 //
3393 // Generate SPIRV instructions for binary operators.
3394 //
3395
3396 // Handle xor with i1 type specially.
3397 if (I.getOpcode() == Instruction::Xor &&
3398 I.getType() == Type::getInt1Ty(Context) &&
3399 (isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) {
3400 //
3401 // Generate OpLogicalNot.
3402 //
3403 // Ops[0] = Result Type ID
3404 // Ops[1] = Operand
3405 SPIRVOperandList Ops;
3406
3407 uint32_t ResTyID = lookupType(I.getType());
3408 SPIRVOperand *ResTyIDOp =
3409 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3410 Ops.push_back(ResTyIDOp);
3411
3412 Value *CondV = I.getOperand(0);
3413 if (isa<Constant>(I.getOperand(0))) {
3414 CondV = I.getOperand(1);
3415 }
3416 uint32_t CondID = VMap[CondV];
3417 SPIRVOperand *CondIDOp =
3418 new SPIRVOperand(SPIRVOperandType::NUMBERID, CondID);
3419 Ops.push_back(CondIDOp);
3420
3421 SPIRVInstruction *Inst =
3422 new SPIRVInstruction(4, spv::OpLogicalNot, nextID++, Ops);
3423 SPIRVInstList.push_back(Inst);
3424 } else {
3425 // Ops[0] = Result Type ID
3426 // Ops[1] = Operand 0
3427 // Ops[2] = Operand 1
3428 SPIRVOperandList Ops;
3429
3430 uint32_t ResTyID = lookupType(I.getType());
3431 SPIRVOperand *ResTyIDOp =
3432 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3433 Ops.push_back(ResTyIDOp);
3434
3435 uint32_t Op0ID = VMap[I.getOperand(0)];
3436 SPIRVOperand *Op0IDOp =
3437 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3438 Ops.push_back(Op0IDOp);
3439
3440 uint32_t Op1ID = VMap[I.getOperand(1)];
3441 SPIRVOperand *Op1IDOp =
3442 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3443 Ops.push_back(Op1IDOp);
3444
3445 SPIRVInstruction *Inst =
3446 new SPIRVInstruction(5, GetSPIRVBinaryOpcode(I), nextID++, Ops);
3447 SPIRVInstList.push_back(Inst);
3448 }
3449 } else {
3450 I.print(errs());
3451 llvm_unreachable("Unsupported instruction???");
3452 }
3453 break;
3454 }
3455 case Instruction::GetElementPtr: {
3456 auto &GlobalConstArgSet = getGlobalConstArgSet();
3457
3458 //
3459 // Generate OpAccessChain.
3460 //
3461 GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I);
3462
3463 //
3464 // Generate OpAccessChain.
3465 //
3466
3467 // Ops[0] = Result Type ID
3468 // Ops[1] = Base ID
3469 // Ops[2] ... Ops[n] = Indexes ID
3470 SPIRVOperandList Ops;
3471
David Neto1a1a0582017-07-07 12:01:44 -04003472 PointerType* ResultType = cast<PointerType>(GEP->getType());
David Neto22f144c2017-06-12 14:26:21 -04003473 if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate ||
3474 GlobalConstArgSet.count(GEP->getPointerOperand())) {
3475 // Use pointer type with private address space for global constant.
3476 Type *EleTy = I.getType()->getPointerElementType();
David Neto1a1a0582017-07-07 12:01:44 -04003477 ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
David Neto22f144c2017-06-12 14:26:21 -04003478 }
David Neto1a1a0582017-07-07 12:01:44 -04003479 const uint32_t ResTyID = lookupType(ResultType);
David Neto22f144c2017-06-12 14:26:21 -04003480 SPIRVOperand *ResTyIDOp =
3481 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3482 Ops.push_back(ResTyIDOp);
3483
3484 // Check whether GEP's pointer operand is pointer argument.
3485 bool HasArgBasePointer = false;
3486 for (auto ArgGV : ArgGVMap) {
3487 if (ArgGV.first == GEP->getPointerOperand()) {
3488 if (isa<PointerType>(ArgGV.first->getType())) {
3489 HasArgBasePointer = true;
3490 } else {
3491 llvm_unreachable(
3492 "GEP's pointer operand is argument of non-poninter type???");
3493 }
3494 }
3495 }
3496
3497 uint32_t BaseID;
3498 if (HasArgBasePointer) {
3499 // Point to global variable for argument directly.
3500 BaseID = ArgGVIDMap[GEP->getPointerOperand()];
3501 } else {
3502 BaseID = VMap[GEP->getPointerOperand()];
3503 }
3504
3505 SPIRVOperand *BaseIDOp =
3506 new SPIRVOperand(SPIRVOperandType::NUMBERID, BaseID);
3507 Ops.push_back(BaseIDOp);
3508
3509 uint16_t WordCount = 4;
3510
3511 if (HasArgBasePointer) {
3512 // If GEP's pointer operand is argument, add one more index for struct
3513 // type to wrap up argument type.
3514 Type *IdxTy = Type::getInt32Ty(Context);
3515 uint32_t IndexID = VMap[ConstantInt::get(IdxTy, 0)];
3516 SPIRVOperand *IndexIDOp =
3517 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3518 Ops.push_back(IndexIDOp);
3519
3520 WordCount++;
3521 }
3522
3523 //
3524 // Follows below rules for gep.
3525 //
3526 // 1. If gep's first index is 0 and gep's base is not kernel function's
3527 // argument, generate OpAccessChain and ignore gep's first index.
3528 // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's
3529 // first index.
3530 // 3. If gep's first index is not constant, generate OpPtrAccessChain and
3531 // use gep's first index.
3532 // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use
3533 // gep's first index.
3534 //
3535 spv::Op Opcode = spv::OpAccessChain;
3536 unsigned offset = 0;
3537 if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
3538 if (CstInt->getZExtValue() == 0 && !HasArgBasePointer) {
3539 offset = 1;
3540 } else if (CstInt->getZExtValue() != 0 && !HasArgBasePointer) {
3541 Opcode = spv::OpPtrAccessChain;
David Neto22f144c2017-06-12 14:26:21 -04003542 }
3543 } else if (!HasArgBasePointer) {
3544 Opcode = spv::OpPtrAccessChain;
David Neto1a1a0582017-07-07 12:01:44 -04003545 }
3546
3547 if (Opcode == spv::OpPtrAccessChain) {
David Neto22f144c2017-06-12 14:26:21 -04003548 setVariablePointers(true);
David Neto1a1a0582017-07-07 12:01:44 -04003549 // Do we need to generate ArrayStride? Check against the GEP result type
3550 // rather than the pointer type of the base because when indexing into
3551 // an OpenCL program-scope constant, we'll swap out the LLVM base pointer
3552 // for something else in the SPIR-V.
3553 // E.g. see test/PointerAccessChain/pointer_index_is_constant_1.cl
3554 if (GetStorageClass(ResultType->getAddressSpace()) ==
3555 spv::StorageClassStorageBuffer) {
3556 // Save the need to generate an ArrayStride decoration. But defer
3557 // generation until later, so we only make one decoration.
3558 getPointerTypesNeedingArrayStride().insert(ResultType);
3559 }
David Neto22f144c2017-06-12 14:26:21 -04003560 }
3561
3562 for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) {
3563 uint32_t IndexID = VMap[*II];
3564 SPIRVOperand *IndexIDOp =
3565 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3566 Ops.push_back(IndexIDOp);
3567
3568 WordCount++;
3569 }
3570
3571 SPIRVInstruction *Inst =
3572 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
3573 SPIRVInstList.push_back(Inst);
3574 break;
3575 }
3576 case Instruction::ExtractValue: {
3577 ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
3578 // Ops[0] = Result Type ID
3579 // Ops[1] = Composite ID
3580 // Ops[2] ... Ops[n] = Indexes (Literal Number)
3581 SPIRVOperandList Ops;
3582
3583 uint32_t ResTyID = lookupType(I.getType());
3584 SPIRVOperand *ResTyIDOp =
3585 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3586 Ops.push_back(ResTyIDOp);
3587
3588 uint32_t CompositeID = VMap[EVI->getAggregateOperand()];
3589 SPIRVOperand *CompositeIDOp =
3590 new SPIRVOperand(SPIRVOperandType::NUMBERID, CompositeID);
3591 Ops.push_back(CompositeIDOp);
3592
3593 for (auto &Index : EVI->indices()) {
3594 Ops.push_back(new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, Index));
3595 }
3596
3597 uint16_t WordCount = static_cast<uint16_t>(2 + Ops.size());
3598 SPIRVInstruction *Inst =
3599 new SPIRVInstruction(WordCount, spv::OpCompositeExtract, nextID++, Ops);
3600 SPIRVInstList.push_back(Inst);
3601 break;
3602 }
3603 case Instruction::InsertValue: {
3604 InsertValueInst *IVI = cast<InsertValueInst>(&I);
3605 // Ops[0] = Result Type ID
3606 // Ops[1] = Object ID
3607 // Ops[2] = Composite ID
3608 // Ops[3] ... Ops[n] = Indexes (Literal Number)
3609 SPIRVOperandList Ops;
3610
3611 uint32_t ResTyID = lookupType(I.getType());
3612 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID));
3613
3614 uint32_t ObjectID = VMap[IVI->getInsertedValueOperand()];
3615 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, ObjectID));
3616
3617 uint32_t CompositeID = VMap[IVI->getAggregateOperand()];
3618 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, CompositeID));
3619
3620 for (auto &Index : IVI->indices()) {
3621 Ops.push_back(new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, Index));
3622 }
3623
3624 uint16_t WordCount = static_cast<uint16_t>(2 + Ops.size());
3625 SPIRVInstruction *Inst =
3626 new SPIRVInstruction(WordCount, spv::OpCompositeInsert, nextID++, Ops);
3627 SPIRVInstList.push_back(Inst);
3628 break;
3629 }
3630 case Instruction::Select: {
3631 //
3632 // Generate OpSelect.
3633 //
3634
3635 // Ops[0] = Result Type ID
3636 // Ops[1] = Condition ID
3637 // Ops[2] = True Constant ID
3638 // Ops[3] = False Constant ID
3639 SPIRVOperandList Ops;
3640
3641 // Find SPIRV instruction for parameter type.
3642 auto Ty = I.getType();
3643 if (Ty->isPointerTy()) {
3644 auto PointeeTy = Ty->getPointerElementType();
3645 if (PointeeTy->isStructTy() &&
3646 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
3647 Ty = PointeeTy;
3648 }
3649 }
3650
3651 uint32_t ResTyID = lookupType(Ty);
3652 SPIRVOperand *ResTyIDOp =
3653 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3654 Ops.push_back(ResTyIDOp);
3655
3656 uint32_t CondID = VMap[I.getOperand(0)];
3657 SPIRVOperand *CondIDOp =
3658 new SPIRVOperand(SPIRVOperandType::NUMBERID, CondID);
3659 Ops.push_back(CondIDOp);
3660
3661 uint32_t TrueID = VMap[I.getOperand(1)];
3662 SPIRVOperand *TrueIDOp =
3663 new SPIRVOperand(SPIRVOperandType::NUMBERID, TrueID);
3664 Ops.push_back(TrueIDOp);
3665
3666 uint32_t FalseID = VMap[I.getOperand(2)];
3667 SPIRVOperand *FalseIDOp =
3668 new SPIRVOperand(SPIRVOperandType::NUMBERID, FalseID);
3669 Ops.push_back(FalseIDOp);
3670
3671 SPIRVInstruction *Inst =
3672 new SPIRVInstruction(6, spv::OpSelect, nextID++, Ops);
3673 SPIRVInstList.push_back(Inst);
3674 break;
3675 }
3676 case Instruction::ExtractElement: {
3677 // Handle <4 x i8> type manually.
3678 Type *CompositeTy = I.getOperand(0)->getType();
3679 if (is4xi8vec(CompositeTy)) {
3680 //
3681 // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with
3682 // <4 x i8>.
3683 //
3684
3685 //
3686 // Generate OpShiftRightLogical
3687 //
3688 // Ops[0] = Result Type ID
3689 // Ops[1] = Operand 0
3690 // Ops[2] = Operand 1
3691 //
3692 SPIRVOperandList Ops;
3693
3694 uint32_t ResTyID = lookupType(CompositeTy);
3695 SPIRVOperand *ResTyIDOp =
3696 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3697 Ops.push_back(ResTyIDOp);
3698
3699 uint32_t Op0ID = VMap[I.getOperand(0)];
3700 SPIRVOperand *Op0IDOp =
3701 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3702 Ops.push_back(Op0IDOp);
3703
3704 uint32_t Op1ID = 0;
3705 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
3706 // Handle constant index.
3707 uint64_t Idx = CI->getZExtValue();
3708 Value *ShiftAmount =
3709 ConstantInt::get(Type::getInt32Ty(Context), Idx * 8);
3710 Op1ID = VMap[ShiftAmount];
3711 } else {
3712 // Handle variable index.
3713 SPIRVOperandList TmpOps;
3714
3715 uint32_t TmpResTyID = lookupType(Type::getInt32Ty(Context));
3716 SPIRVOperand *TmpResTyIDOp =
3717 new SPIRVOperand(SPIRVOperandType::NUMBERID, TmpResTyID);
3718 TmpOps.push_back(TmpResTyIDOp);
3719
3720 uint32_t IdxID = VMap[I.getOperand(1)];
3721 SPIRVOperand *TmpOp0IDOp =
3722 new SPIRVOperand(SPIRVOperandType::NUMBERID, IdxID);
3723 TmpOps.push_back(TmpOp0IDOp);
3724
3725 ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8);
3726 uint32_t Cst8ID = VMap[Cst8];
3727 SPIRVOperand *TmpOp1IDOp =
3728 new SPIRVOperand(SPIRVOperandType::NUMBERID, Cst8ID);
3729 TmpOps.push_back(TmpOp1IDOp);
3730
3731 Op1ID = nextID;
3732
3733 SPIRVInstruction *TmpInst =
3734 new SPIRVInstruction(5, spv::OpIMul, nextID++, TmpOps);
3735 SPIRVInstList.push_back(TmpInst);
3736 }
3737 SPIRVOperand *Op1IDOp =
3738 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3739 Ops.push_back(Op1IDOp);
3740
3741 uint32_t ShiftID = nextID;
3742
3743 SPIRVInstruction *Inst =
3744 new SPIRVInstruction(5, spv::OpShiftRightLogical, nextID++, Ops);
3745 SPIRVInstList.push_back(Inst);
3746
3747 //
3748 // Generate OpBitwiseAnd
3749 //
3750 // Ops[0] = Result Type ID
3751 // Ops[1] = Operand 0
3752 // Ops[2] = Operand 1
3753 //
3754 Ops.clear();
3755
3756 ResTyID = lookupType(CompositeTy);
3757 ResTyIDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3758 Ops.push_back(ResTyIDOp);
3759
3760 Op0ID = ShiftID;
3761 Op0IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3762 Ops.push_back(Op0IDOp);
3763
3764 Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF);
3765 Op1ID = VMap[CstFF];
3766 Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3767 Ops.push_back(Op1IDOp);
3768
3769 Inst = new SPIRVInstruction(5, spv::OpBitwiseAnd, nextID++, Ops);
3770 SPIRVInstList.push_back(Inst);
3771 break;
3772 }
3773
3774 // Ops[0] = Result Type ID
3775 // Ops[1] = Composite ID
3776 // Ops[2] ... Ops[n] = Indexes (Literal Number)
3777 SPIRVOperandList Ops;
3778
3779 uint32_t ResTyID = lookupType(I.getType());
3780 SPIRVOperand *ResTyIDOp =
3781 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3782 Ops.push_back(ResTyIDOp);
3783
3784 uint32_t CompositeID = VMap[I.getOperand(0)];
3785 SPIRVOperand *CompositeIDOp =
3786 new SPIRVOperand(SPIRVOperandType::NUMBERID, CompositeID);
3787 Ops.push_back(CompositeIDOp);
3788
3789 spv::Op Opcode = spv::OpCompositeExtract;
3790 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
3791 std::vector<uint32_t> LiteralNum;
3792 assert(CI->getZExtValue() < UINT32_MAX);
3793 LiteralNum.push_back(static_cast<uint32_t>(CI->getZExtValue()));
3794 SPIRVOperand *Indexes =
3795 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
3796 Ops.push_back(Indexes);
3797 } else {
3798 uint32_t IndexID = VMap[I.getOperand(1)];
3799 SPIRVOperand *IndexIDOp =
3800 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3801 Ops.push_back(IndexIDOp);
3802 Opcode = spv::OpVectorExtractDynamic;
3803 }
3804
3805 uint16_t WordCount = 5;
3806 SPIRVInstruction *Inst =
3807 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
3808 SPIRVInstList.push_back(Inst);
3809 break;
3810 }
3811 case Instruction::InsertElement: {
3812 // Handle <4 x i8> type manually.
3813 Type *CompositeTy = I.getOperand(0)->getType();
3814 if (is4xi8vec(CompositeTy)) {
3815 Constant *CstFF = ConstantInt::get(Type::getInt32Ty(Context), 0xFF);
3816 uint32_t CstFFID = VMap[CstFF];
3817
3818 uint32_t ShiftAmountID = 0;
3819 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) {
3820 // Handle constant index.
3821 uint64_t Idx = CI->getZExtValue();
3822 Value *ShiftAmount =
3823 ConstantInt::get(Type::getInt32Ty(Context), Idx * 8);
3824 ShiftAmountID = VMap[ShiftAmount];
3825 } else {
3826 // Handle variable index.
3827 SPIRVOperandList TmpOps;
3828
3829 uint32_t TmpResTyID = lookupType(Type::getInt32Ty(Context));
3830 SPIRVOperand *TmpResTyIDOp =
3831 new SPIRVOperand(SPIRVOperandType::NUMBERID, TmpResTyID);
3832 TmpOps.push_back(TmpResTyIDOp);
3833
3834 uint32_t IdxID = VMap[I.getOperand(2)];
3835 SPIRVOperand *TmpOp0IDOp =
3836 new SPIRVOperand(SPIRVOperandType::NUMBERID, IdxID);
3837 TmpOps.push_back(TmpOp0IDOp);
3838
3839 ConstantInt *Cst8 = ConstantInt::get(Type::getInt32Ty(Context), 8);
3840 uint32_t Cst8ID = VMap[Cst8];
3841 SPIRVOperand *TmpOp1IDOp =
3842 new SPIRVOperand(SPIRVOperandType::NUMBERID, Cst8ID);
3843 TmpOps.push_back(TmpOp1IDOp);
3844
3845 ShiftAmountID = nextID;
3846
3847 SPIRVInstruction *TmpInst =
3848 new SPIRVInstruction(5, spv::OpIMul, nextID++, TmpOps);
3849 SPIRVInstList.push_back(TmpInst);
3850 }
3851
3852 //
3853 // Generate mask operations.
3854 //
3855
3856 // ShiftLeft mask according to index of insertelement.
3857 SPIRVOperandList Ops;
3858
3859 uint32_t ResTyID = lookupType(CompositeTy);
3860 SPIRVOperand *ResTyIDOp =
3861 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3862 Ops.push_back(ResTyIDOp);
3863
3864 uint32_t Op0ID = CstFFID;
3865 SPIRVOperand *Op0IDOp =
3866 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3867 Ops.push_back(Op0IDOp);
3868
3869 uint32_t Op1ID = ShiftAmountID;
3870 SPIRVOperand *Op1IDOp =
3871 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3872 Ops.push_back(Op1IDOp);
3873
3874 uint32_t MaskID = nextID;
3875
3876 SPIRVInstruction *Inst =
3877 new SPIRVInstruction(5, spv::OpShiftLeftLogical, nextID++, Ops);
3878 SPIRVInstList.push_back(Inst);
3879
3880 // Inverse mask.
3881 Ops.clear();
3882
3883 Ops.push_back(ResTyIDOp);
3884
3885 Op0ID = MaskID;
3886 Op0IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3887 Ops.push_back(Op0IDOp);
3888
3889 uint32_t InvMaskID = nextID;
3890
3891 Inst = new SPIRVInstruction(4, spv::OpLogicalNot, nextID++, Ops);
3892 SPIRVInstList.push_back(Inst);
3893
3894 // Apply mask.
3895 Ops.clear();
3896
3897 Ops.push_back(ResTyIDOp);
3898
3899 Op0ID = VMap[I.getOperand(0)];
3900 Op0IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3901 Ops.push_back(Op0IDOp);
3902
3903 Op1ID = InvMaskID;
3904 Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3905 Ops.push_back(Op1IDOp);
3906
3907 uint32_t OrgValID = nextID;
3908
3909 Inst = new SPIRVInstruction(5, spv::OpBitwiseAnd, nextID++, Ops);
3910 SPIRVInstList.push_back(Inst);
3911
3912 // Create correct value according to index of insertelement.
3913 Ops.clear();
3914
3915 Ops.push_back(ResTyIDOp);
3916
3917 Op0ID = VMap[I.getOperand(1)];
3918 Op0IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3919 Ops.push_back(Op0IDOp);
3920
3921 Op1ID = ShiftAmountID;
3922 Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3923 Ops.push_back(Op1IDOp);
3924
3925 uint32_t InsertValID = nextID;
3926
3927 Inst = new SPIRVInstruction(5, spv::OpShiftLeftLogical, nextID++, Ops);
3928 SPIRVInstList.push_back(Inst);
3929
3930 // Insert value to original value.
3931 Ops.clear();
3932
3933 Ops.push_back(ResTyIDOp);
3934
3935 Op0ID = OrgValID;
3936 Op0IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
3937 Ops.push_back(Op0IDOp);
3938
3939 Op1ID = InsertValID;
3940 Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
3941 Ops.push_back(Op1IDOp);
3942
3943 Inst = new SPIRVInstruction(5, spv::OpBitwiseOr, nextID++, Ops);
3944 SPIRVInstList.push_back(Inst);
3945
3946 break;
3947 }
3948
3949 // Ops[0] = Result Type ID
3950 // Ops[1] = Object ID
3951 // Ops[2] = Composite ID
3952 // Ops[3] ... Ops[n] = Indexes (Literal Number)
3953 SPIRVOperandList Ops;
3954
3955 uint32_t ResTyID = lookupType(I.getType());
3956 SPIRVOperand *ResTyIDOp =
3957 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
3958 Ops.push_back(ResTyIDOp);
3959
3960 uint32_t ObjectID = VMap[I.getOperand(1)];
3961 SPIRVOperand *ObjectIDOp =
3962 new SPIRVOperand(SPIRVOperandType::NUMBERID, ObjectID);
3963 Ops.push_back(ObjectIDOp);
3964
3965 uint32_t CompositeID = VMap[I.getOperand(0)];
3966 SPIRVOperand *CompositeIDOp =
3967 new SPIRVOperand(SPIRVOperandType::NUMBERID, CompositeID);
3968 Ops.push_back(CompositeIDOp);
3969
3970 spv::Op Opcode = spv::OpCompositeInsert;
3971 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) {
3972 std::vector<uint32_t> LiteralNum;
3973 assert(CI->getZExtValue() < UINT32_MAX);
3974 LiteralNum.push_back(static_cast<uint32_t>(CI->getZExtValue()));
3975 SPIRVOperand *Indexes =
3976 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
3977 Ops.push_back(Indexes);
3978 } else {
3979 uint32_t IndexID = VMap[I.getOperand(1)];
3980 SPIRVOperand *IndexIDOp =
3981 new SPIRVOperand(SPIRVOperandType::NUMBERID, IndexID);
3982 Ops.push_back(IndexIDOp);
3983 Opcode = spv::OpVectorInsertDynamic;
3984 }
3985
3986 uint16_t WordCount = 6;
3987 SPIRVInstruction *Inst =
3988 new SPIRVInstruction(WordCount, Opcode, nextID++, Ops);
3989 SPIRVInstList.push_back(Inst);
3990 break;
3991 }
3992 case Instruction::ShuffleVector: {
3993 // Ops[0] = Result Type ID
3994 // Ops[1] = Vector 1 ID
3995 // Ops[2] = Vector 2 ID
3996 // Ops[3] ... Ops[n] = Components (Literal Number)
3997 SPIRVOperandList Ops;
3998
3999 uint32_t ResTyID = lookupType(I.getType());
4000 SPIRVOperand *ResTyIDOp =
4001 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4002 Ops.push_back(ResTyIDOp);
4003
4004 uint32_t Vec1ID = VMap[I.getOperand(0)];
4005 SPIRVOperand *Vec1IDOp =
4006 new SPIRVOperand(SPIRVOperandType::NUMBERID, Vec1ID);
4007 Ops.push_back(Vec1IDOp);
4008
4009 uint32_t Vec2ID = VMap[I.getOperand(1)];
4010 SPIRVOperand *Vec2IDOp =
4011 new SPIRVOperand(SPIRVOperandType::NUMBERID, Vec2ID);
4012 Ops.push_back(Vec2IDOp);
4013
4014 uint64_t NumElements = 0;
4015 if (Constant *Cst = dyn_cast<Constant>(I.getOperand(2))) {
4016 NumElements = cast<VectorType>(Cst->getType())->getNumElements();
4017
4018 if (Cst->isNullValue()) {
4019 for (unsigned i = 0; i < NumElements; i++) {
4020 std::vector<uint32_t> LiteralNum;
4021 LiteralNum.push_back(0);
4022 SPIRVOperand *Component =
4023 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
4024 Ops.push_back(Component);
4025 }
4026 } else if (const ConstantDataSequential *CDS =
4027 dyn_cast<ConstantDataSequential>(Cst)) {
4028 for (unsigned i = 0; i < CDS->getNumElements(); i++) {
4029 std::vector<uint32_t> LiteralNum;
4030 assert(CDS->getElementAsInteger(i) < UINT32_MAX);
4031 LiteralNum.push_back(
4032 static_cast<uint32_t>(CDS->getElementAsInteger(i)));
4033 SPIRVOperand *Component =
4034 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
4035 Ops.push_back(Component);
4036 }
4037 } else if (const ConstantVector *CV = dyn_cast<ConstantVector>(Cst)) {
4038 for (unsigned i = 0; i < CV->getNumOperands(); i++) {
4039 auto Op = CV->getOperand(i);
4040
4041 uint32_t literal = 0;
4042
4043 if (auto CI = dyn_cast<ConstantInt>(Op)) {
4044 literal = static_cast<uint32_t>(CI->getZExtValue());
4045 } else if (auto UI = dyn_cast<UndefValue>(Op)) {
4046 literal = 0xFFFFFFFFu;
4047 } else {
4048 Op->print(errs());
4049 llvm_unreachable("Unsupported element in ConstantVector!");
4050 }
4051
4052 std::vector<uint32_t> LiteralNum;
4053 LiteralNum.push_back(literal);
4054 SPIRVOperand *Component =
4055 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
4056 Ops.push_back(Component);
4057 }
4058 } else {
4059 Cst->print(errs());
4060 llvm_unreachable("Unsupported constant mask in ShuffleVector!");
4061 }
4062 }
4063
4064 uint16_t WordCount = static_cast<uint16_t>(5 + NumElements);
4065 SPIRVInstruction *Inst =
4066 new SPIRVInstruction(WordCount, spv::OpVectorShuffle, nextID++, Ops);
4067 SPIRVInstList.push_back(Inst);
4068 break;
4069 }
4070 case Instruction::ICmp:
4071 case Instruction::FCmp: {
4072 CmpInst *CmpI = cast<CmpInst>(&I);
4073
4074 // Ops[0] = Result Type ID
4075 // Ops[1] = Operand 1 ID
4076 // Ops[2] = Operand 2 ID
4077 SPIRVOperandList Ops;
4078
4079 uint32_t ResTyID = lookupType(CmpI->getType());
4080 SPIRVOperand *ResTyIDOp =
4081 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4082 Ops.push_back(ResTyIDOp);
4083
David Netod4ca2e62017-07-06 18:47:35 -04004084 // Pointer equality is invalid.
4085 Type* ArgTy = CmpI->getOperand(0)->getType();
4086 if (isa<PointerType>(ArgTy)) {
4087 CmpI->print(errs());
4088 std::string name = I.getParent()->getParent()->getName();
4089 errs()
4090 << "\nPointer equality test is not supported by SPIR-V for Vulkan, "
4091 << "in function " << name << "\n";
4092 llvm_unreachable("Pointer equality check is invalid");
4093 break;
4094 }
4095
David Neto22f144c2017-06-12 14:26:21 -04004096 uint32_t Op1ID = VMap[CmpI->getOperand(0)];
4097 SPIRVOperand *Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op1ID);
4098 Ops.push_back(Op1IDOp);
4099
4100 uint32_t Op2ID = VMap[CmpI->getOperand(1)];
4101 SPIRVOperand *Op2IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, Op2ID);
4102 Ops.push_back(Op2IDOp);
4103
4104 spv::Op Opcode = GetSPIRVCmpOpcode(CmpI);
4105 SPIRVInstruction *Inst = new SPIRVInstruction(5, Opcode, nextID++, Ops);
4106 SPIRVInstList.push_back(Inst);
4107 break;
4108 }
4109 case Instruction::Br: {
4110 // Branch instrucion is deferred because it needs label's ID. Record slot's
4111 // location on SPIRVInstructionList.
4112 DeferredInsts.push_back(
4113 std::make_tuple(&I, --SPIRVInstList.end(), 0 /* No id */));
4114 break;
4115 }
4116 case Instruction::Switch: {
4117 I.print(errs());
4118 llvm_unreachable("Unsupported instruction???");
4119 break;
4120 }
4121 case Instruction::IndirectBr: {
4122 I.print(errs());
4123 llvm_unreachable("Unsupported instruction???");
4124 break;
4125 }
4126 case Instruction::PHI: {
4127 // Branch instrucion is deferred because it needs label's ID. Record slot's
4128 // location on SPIRVInstructionList.
4129 DeferredInsts.push_back(
4130 std::make_tuple(&I, --SPIRVInstList.end(), nextID++));
4131 break;
4132 }
4133 case Instruction::Alloca: {
4134 //
4135 // Generate OpVariable.
4136 //
4137 // Ops[0] : Result Type ID
4138 // Ops[1] : Storage Class
4139 SPIRVOperandList Ops;
4140
4141 Type *ResTy = I.getType();
4142 uint32_t ResTyID = lookupType(ResTy);
4143 SPIRVOperand *ResTyOp =
4144 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4145 Ops.push_back(ResTyOp);
4146
4147 spv::StorageClass StorageClass = spv::StorageClassFunction;
4148 SPIRVOperand *StorageClassOp =
4149 new SPIRVOperand(SPIRVOperandType::NUMBERID, StorageClass);
4150 Ops.push_back(StorageClassOp);
4151
4152 SPIRVInstruction *Inst =
4153 new SPIRVInstruction(4, spv::OpVariable, nextID++, Ops);
4154 SPIRVInstList.push_back(Inst);
4155 break;
4156 }
4157 case Instruction::Load: {
4158 LoadInst *LD = cast<LoadInst>(&I);
4159 //
4160 // Generate OpLoad.
4161 //
4162
4163 // Ops[0] = Result Type ID
4164 // Ops[1] = Pointer ID
4165 // Ops[2] ... Ops[n] = Optional Memory Access
4166 //
4167 // TODO: Do we need to implement Optional Memory Access???
4168 SPIRVOperandList Ops;
4169
4170 uint32_t ResTyID = lookupType(LD->getType());
4171 SPIRVOperand *ResTyIDOp =
4172 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4173 Ops.push_back(ResTyIDOp);
4174
4175 uint32_t PointerID = VMap[LD->getPointerOperand()];
4176 SPIRVOperand *PointerIDOp =
4177 new SPIRVOperand(SPIRVOperandType::NUMBERID, PointerID);
4178 Ops.push_back(PointerIDOp);
4179
4180 SPIRVInstruction *Inst =
4181 new SPIRVInstruction(4, spv::OpLoad, nextID++, Ops);
4182 SPIRVInstList.push_back(Inst);
4183 break;
4184 }
4185 case Instruction::Store: {
4186 StoreInst *ST = cast<StoreInst>(&I);
4187 //
4188 // Generate OpStore.
4189 //
4190
4191 // Ops[0] = Pointer ID
4192 // Ops[1] = Object ID
4193 // Ops[2] ... Ops[n] = Optional Memory Access (later???)
4194 //
4195 // TODO: Do we need to implement Optional Memory Access???
4196 SPIRVOperand *Ops[2] = {new SPIRVOperand(SPIRVOperandType::NUMBERID,
4197 VMap[ST->getPointerOperand()]),
4198 new SPIRVOperand(SPIRVOperandType::NUMBERID,
4199 VMap[ST->getValueOperand()])};
4200
4201 SPIRVInstruction *Inst =
4202 new SPIRVInstruction(3, spv::OpStore, 0 /* No id */, Ops);
4203 SPIRVInstList.push_back(Inst);
4204 break;
4205 }
4206 case Instruction::AtomicCmpXchg: {
4207 I.print(errs());
4208 llvm_unreachable("Unsupported instruction???");
4209 break;
4210 }
4211 case Instruction::AtomicRMW: {
4212 I.print(errs());
4213 llvm_unreachable("Unsupported instruction???");
4214 break;
4215 }
4216 case Instruction::Fence: {
4217 I.print(errs());
4218 llvm_unreachable("Unsupported instruction???");
4219 break;
4220 }
4221 case Instruction::Call: {
4222 CallInst *Call = dyn_cast<CallInst>(&I);
4223 Function *Callee = Call->getCalledFunction();
4224
4225 // Sampler initializers become a load of the corresponding sampler.
4226 if (Callee->getName().equals("__translate_sampler_initializer")) {
4227 // Check that the sampler map was definitely used though.
4228 if (0 == getSamplerMap().size()) {
4229 llvm_unreachable("Sampler literal in source without sampler map!");
4230 }
4231
4232 SPIRVOperandList Ops;
4233
4234 uint32_t ResTyID = lookupType(SamplerTy->getPointerElementType());
4235 SPIRVOperand *ResTyIDOp =
4236 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4237 Ops.push_back(ResTyIDOp);
4238
4239 uint32_t PointerID = VMap[Call];
4240 SPIRVOperand *PointerIDOp =
4241 new SPIRVOperand(SPIRVOperandType::NUMBERID, PointerID);
4242 Ops.push_back(PointerIDOp);
4243
4244 VMap[Call] = nextID;
4245 SPIRVInstruction *Inst =
4246 new SPIRVInstruction(4, spv::OpLoad, nextID++, Ops);
4247 SPIRVInstList.push_back(Inst);
4248
4249 break;
4250 }
4251
4252 if (Callee->getName().startswith("spirv.atomic")) {
4253 spv::Op opcode = StringSwitch<spv::Op>(Callee->getName())
4254 .Case("spirv.atomic_add", spv::OpAtomicIAdd)
4255 .Case("spirv.atomic_sub", spv::OpAtomicISub)
4256 .Case("spirv.atomic_exchange", spv::OpAtomicExchange)
4257 .Case("spirv.atomic_inc", spv::OpAtomicIIncrement)
4258 .Case("spirv.atomic_dec", spv::OpAtomicIDecrement)
4259 .Case("spirv.atomic_compare_exchange",
4260 spv::OpAtomicCompareExchange)
4261 .Case("spirv.atomic_umin", spv::OpAtomicUMin)
4262 .Case("spirv.atomic_smin", spv::OpAtomicSMin)
4263 .Case("spirv.atomic_umax", spv::OpAtomicUMax)
4264 .Case("spirv.atomic_smax", spv::OpAtomicSMax)
4265 .Case("spirv.atomic_and", spv::OpAtomicAnd)
4266 .Case("spirv.atomic_or", spv::OpAtomicOr)
4267 .Case("spirv.atomic_xor", spv::OpAtomicXor)
4268 .Default(spv::OpNop);
4269
4270 //
4271 // Generate OpAtomic*.
4272 //
4273 SPIRVOperandList Ops;
4274
4275 uint32_t TyID = lookupType(I.getType());
4276 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID));
4277
4278 for (unsigned i = 0; i < Call->getNumArgOperands(); i++) {
4279 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
4280 VMap[Call->getArgOperand(i)]));
4281 }
4282
4283 VMap[&I] = nextID;
4284
4285 SPIRVInstruction *Inst = new SPIRVInstruction(
4286 static_cast<uint16_t>(2 + Ops.size()), opcode, nextID++, Ops);
4287 SPIRVInstList.push_back(Inst);
4288 break;
4289 }
4290
4291 if (Callee->getName().startswith("_Z3dot")) {
4292 // If the argument is a vector type, generate OpDot
4293 if (Call->getArgOperand(0)->getType()->isVectorTy()) {
4294 //
4295 // Generate OpDot.
4296 //
4297 SPIRVOperandList Ops;
4298
4299 uint32_t TyID = lookupType(I.getType());
4300 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID));
4301
4302 for (unsigned i = 0; i < Call->getNumArgOperands(); i++) {
4303 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
4304 VMap[Call->getArgOperand(i)]));
4305 }
4306
4307 VMap[&I] = nextID;
4308
4309 SPIRVInstruction *Inst = new SPIRVInstruction(
4310 static_cast<uint16_t>(2 + Ops.size()), spv::OpDot, nextID++, Ops);
4311 SPIRVInstList.push_back(Inst);
4312 } else {
4313 //
4314 // Generate OpFMul.
4315 //
4316 SPIRVOperandList Ops;
4317
4318 uint32_t TyID = lookupType(I.getType());
4319 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID));
4320
4321 for (unsigned i = 0; i < Call->getNumArgOperands(); i++) {
4322 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
4323 VMap[Call->getArgOperand(i)]));
4324 }
4325
4326 VMap[&I] = nextID;
4327
4328 SPIRVInstruction *Inst = new SPIRVInstruction(
4329 static_cast<uint16_t>(2 + Ops.size()), spv::OpFMul, nextID++, Ops);
4330 SPIRVInstList.push_back(Inst);
4331 }
4332 break;
4333 }
4334
4335 // spirv.store_null.* intrinsics become OpStore's.
4336 if (Callee->getName().startswith("spirv.store_null")) {
4337 //
4338 // Generate OpStore.
4339 //
4340
4341 // Ops[0] = Pointer ID
4342 // Ops[1] = Object ID
4343 // Ops[2] ... Ops[n]
4344 SPIRVOperandList Ops;
4345
4346 uint32_t PointerID = VMap[Call->getArgOperand(0)];
4347 SPIRVOperand *PointerIDOp =
4348 new SPIRVOperand(SPIRVOperandType::NUMBERID, PointerID);
4349 Ops.push_back(PointerIDOp);
4350
4351 uint32_t ObjectID = VMap[Call->getArgOperand(1)];
4352 SPIRVOperand *ObjectIDOp =
4353 new SPIRVOperand(SPIRVOperandType::NUMBERID, ObjectID);
4354 Ops.push_back(ObjectIDOp);
4355
4356 SPIRVInstruction *Inst =
4357 new SPIRVInstruction(3, spv::OpStore, 0 /* No id */, Ops);
4358 SPIRVInstList.push_back(Inst);
4359
4360 break;
4361 }
4362
4363 // spirv.copy_memory.* intrinsics become OpMemoryMemory's.
4364 if (Callee->getName().startswith("spirv.copy_memory")) {
4365 //
4366 // Generate OpCopyMemory.
4367 //
4368
4369 // Ops[0] = Dst ID
4370 // Ops[1] = Src ID
4371 // Ops[2] = Memory Access
4372 // Ops[3] = Alignment
4373
4374 auto IsVolatile =
4375 dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0;
4376
4377 auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask
4378 : spv::MemoryAccessMaskNone;
4379
4380 auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask;
4381
4382 auto Alignment =
4383 dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue();
4384
4385 SPIRVOperand *Ops[4] = {
4386 new SPIRVOperand(SPIRVOperandType::NUMBERID,
4387 VMap[Call->getArgOperand(0)]),
4388 new SPIRVOperand(SPIRVOperandType::NUMBERID,
4389 VMap[Call->getArgOperand(1)]),
4390 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, MemoryAccess),
4391 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER,
4392 static_cast<uint32_t>(Alignment))};
4393
4394 SPIRVInstruction *Inst =
4395 new SPIRVInstruction(5, spv::OpCopyMemory, 0 /* No id */, Ops);
4396
4397 SPIRVInstList.push_back(Inst);
4398
4399 break;
4400 }
4401
4402 // Nothing to do for abs with uint. Map abs's operand ID to VMap for abs
4403 // with unit.
4404 if (Callee->getName().equals("_Z3absj") ||
4405 Callee->getName().equals("_Z3absDv2_j") ||
4406 Callee->getName().equals("_Z3absDv3_j") ||
4407 Callee->getName().equals("_Z3absDv4_j")) {
4408 VMap[&I] = VMap[Call->getOperand(0)];
4409 break;
4410 }
4411
4412 // barrier is converted to OpControlBarrier
4413 if (Callee->getName().equals("__spirv_control_barrier")) {
4414 //
4415 // Generate OpControlBarrier.
4416 //
4417 // Ops[0] = Execution Scope ID
4418 // Ops[1] = Memory Scope ID
4419 // Ops[2] = Memory Semantics ID
4420 //
4421 Value *ExecutionScope = Call->getArgOperand(0);
4422 Value *MemoryScope = Call->getArgOperand(1);
4423 Value *MemorySemantics = Call->getArgOperand(2);
4424
4425 SPIRVOperand *Ops[3] = {
4426 new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[ExecutionScope]),
4427 new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[MemoryScope]),
4428 new SPIRVOperand(SPIRVOperandType::NUMBERID, VMap[MemorySemantics])};
4429
4430 SPIRVInstList.push_back(
4431 new SPIRVInstruction(4, spv::OpControlBarrier, 0 /* No id */, Ops));
4432 break;
4433 }
4434
4435 // memory barrier is converted to OpMemoryBarrier
4436 if (Callee->getName().equals("__spirv_memory_barrier")) {
4437 //
4438 // Generate OpMemoryBarrier.
4439 //
4440 // Ops[0] = Memory Scope ID
4441 // Ops[1] = Memory Semantics ID
4442 //
4443 SPIRVOperandList Ops;
4444
4445 Value *MemoryScope = Call->getArgOperand(0);
4446 Value *MemorySemantics = Call->getArgOperand(1);
4447
4448 uint32_t MemoryScopeID = VMap[MemoryScope];
4449 Ops.push_back(
4450 new SPIRVOperand(SPIRVOperandType::NUMBERID, MemoryScopeID));
4451
4452 uint32_t MemorySemanticsID = VMap[MemorySemantics];
4453 Ops.push_back(
4454 new SPIRVOperand(SPIRVOperandType::NUMBERID, MemorySemanticsID));
4455
4456 SPIRVInstruction *Inst =
4457 new SPIRVInstruction(3, spv::OpMemoryBarrier, 0 /* No id */, Ops);
4458 SPIRVInstList.push_back(Inst);
4459 break;
4460 }
4461
4462 // isinf is converted to OpIsInf
4463 if (Callee->getName().equals("__spirv_isinff") ||
4464 Callee->getName().equals("__spirv_isinfDv2_f") ||
4465 Callee->getName().equals("__spirv_isinfDv3_f") ||
4466 Callee->getName().equals("__spirv_isinfDv4_f")) {
4467 //
4468 // Generate OpIsInf.
4469 //
4470 // Ops[0] = Result Type ID
4471 // Ops[1] = X ID
4472 //
4473 SPIRVOperandList Ops;
4474
4475 uint32_t TyID = lookupType(I.getType());
4476 SPIRVOperand *ResTyIDOp =
4477 new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID);
4478 Ops.push_back(ResTyIDOp);
4479
4480 uint32_t XID = VMap[Call->getArgOperand(0)];
4481 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, XID));
4482
4483 VMap[&I] = nextID;
4484
4485 SPIRVInstruction *Inst =
4486 new SPIRVInstruction(4, spv::OpIsInf, nextID++, Ops);
4487 SPIRVInstList.push_back(Inst);
4488 break;
4489 }
4490
4491 // isnan is converted to OpIsNan
4492 if (Callee->getName().equals("__spirv_isnanf") ||
4493 Callee->getName().equals("__spirv_isnanDv2_f") ||
4494 Callee->getName().equals("__spirv_isnanDv3_f") ||
4495 Callee->getName().equals("__spirv_isnanDv4_f")) {
4496 //
4497 // Generate OpIsInf.
4498 //
4499 // Ops[0] = Result Type ID
4500 // Ops[1] = X ID
4501 //
4502 SPIRVOperandList Ops;
4503
4504 uint32_t TyID = lookupType(I.getType());
4505 SPIRVOperand *ResTyIDOp =
4506 new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID);
4507 Ops.push_back(ResTyIDOp);
4508
4509 uint32_t XID = VMap[Call->getArgOperand(0)];
4510 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, XID));
4511
4512 VMap[&I] = nextID;
4513
4514 SPIRVInstruction *Inst =
4515 new SPIRVInstruction(4, spv::OpIsNan, nextID++, Ops);
4516 SPIRVInstList.push_back(Inst);
4517 break;
4518 }
4519
4520 // all is converted to OpAll
4521 if (Callee->getName().equals("__spirv_allDv2_i") ||
4522 Callee->getName().equals("__spirv_allDv3_i") ||
4523 Callee->getName().equals("__spirv_allDv4_i")) {
4524 //
4525 // Generate OpAll.
4526 //
4527 // Ops[0] = Result Type ID
4528 // Ops[1] = Vector ID
4529 //
4530 SPIRVOperandList Ops;
4531
4532 uint32_t TyID = lookupType(I.getType());
4533 SPIRVOperand *ResTyIDOp =
4534 new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID);
4535 Ops.push_back(ResTyIDOp);
4536
4537 uint32_t VectorID = VMap[Call->getArgOperand(0)];
4538 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, VectorID));
4539
4540 VMap[&I] = nextID;
4541
4542 SPIRVInstruction *Inst =
4543 new SPIRVInstruction(4, spv::OpAll, nextID++, Ops);
4544 SPIRVInstList.push_back(Inst);
4545 break;
4546 }
4547
4548 // any is converted to OpAny
4549 if (Callee->getName().equals("__spirv_anyDv2_i") ||
4550 Callee->getName().equals("__spirv_anyDv3_i") ||
4551 Callee->getName().equals("__spirv_anyDv4_i")) {
4552 //
4553 // Generate OpAny.
4554 //
4555 // Ops[0] = Result Type ID
4556 // Ops[1] = Vector ID
4557 //
4558 SPIRVOperandList Ops;
4559
4560 uint32_t TyID = lookupType(I.getType());
4561 SPIRVOperand *ResTyIDOp =
4562 new SPIRVOperand(SPIRVOperandType::NUMBERID, TyID);
4563 Ops.push_back(ResTyIDOp);
4564
4565 uint32_t VectorID = VMap[Call->getArgOperand(0)];
4566 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID, VectorID));
4567
4568 VMap[&I] = nextID;
4569
4570 SPIRVInstruction *Inst =
4571 new SPIRVInstruction(4, spv::OpAny, nextID++, Ops);
4572 SPIRVInstList.push_back(Inst);
4573 break;
4574 }
4575
4576 // read_image is converted to OpSampledImage and OpImageSampleExplicitLod.
4577 // Additionally, OpTypeSampledImage is generated.
4578 if (Callee->getName().equals(
4579 "_Z11read_imagef14ocl_image2d_ro11ocl_samplerDv2_f") ||
4580 Callee->getName().equals(
4581 "_Z11read_imagef14ocl_image3d_ro11ocl_samplerDv4_f")) {
4582 //
4583 // Generate OpSampledImage.
4584 //
4585 // Ops[0] = Result Type ID
4586 // Ops[1] = Image ID
4587 // Ops[2] = Sampler ID
4588 //
4589 SPIRVOperandList Ops;
4590
4591 Value *Image = Call->getArgOperand(0);
4592 Value *Sampler = Call->getArgOperand(1);
4593 Value *Coordinate = Call->getArgOperand(2);
4594
4595 TypeMapType &OpImageTypeMap = getImageTypeMap();
4596 Type *ImageTy = Image->getType()->getPointerElementType();
4597 uint32_t ImageTyID = OpImageTypeMap[ImageTy];
4598 SPIRVOperand *ResTyIDOp =
4599 new SPIRVOperand(SPIRVOperandType::NUMBERID, ImageTyID);
4600 Ops.push_back(ResTyIDOp);
4601
4602 uint32_t ImageID = VMap[Image];
4603 SPIRVOperand *ImageIDOp =
4604 new SPIRVOperand(SPIRVOperandType::NUMBERID, ImageID);
4605 Ops.push_back(ImageIDOp);
4606
4607 uint32_t SamplerID = VMap[Sampler];
4608 SPIRVOperand *SamplerIDOp =
4609 new SPIRVOperand(SPIRVOperandType::NUMBERID, SamplerID);
4610 Ops.push_back(SamplerIDOp);
4611
4612 uint32_t SampledImageID = nextID;
4613
4614 SPIRVInstruction *Inst =
4615 new SPIRVInstruction(5, spv::OpSampledImage, nextID++, Ops);
4616 SPIRVInstList.push_back(Inst);
4617
4618 //
4619 // Generate OpImageSampleExplicitLod.
4620 //
4621 // Ops[0] = Result Type ID
4622 // Ops[1] = Sampled Image ID
4623 // Ops[2] = Coordinate ID
4624 // Ops[3] = Image Operands Type ID
4625 // Ops[4] ... Ops[n] = Operands ID
4626 //
4627 Ops.clear();
4628
4629 uint32_t RetTyID = lookupType(Call->getType());
4630 ResTyIDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID, RetTyID);
4631 Ops.push_back(ResTyIDOp);
4632
4633 SPIRVOperand *SampledImageIDOp =
4634 new SPIRVOperand(SPIRVOperandType::NUMBERID, SampledImageID);
4635 Ops.push_back(SampledImageIDOp);
4636
4637 uint32_t CoordinateID = VMap[Coordinate];
4638 SPIRVOperand *CoordinateIDOp =
4639 new SPIRVOperand(SPIRVOperandType::NUMBERID, CoordinateID);
4640 Ops.push_back(CoordinateIDOp);
4641
4642 std::vector<uint32_t> LiteralNum;
4643 LiteralNum.push_back(spv::ImageOperandsLodMask);
4644 SPIRVOperand *ImageOperandTyIDOp =
4645 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
4646 Ops.push_back(ImageOperandTyIDOp);
4647
4648 Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f));
4649 uint32_t OperandID = VMap[CstFP0];
4650 SPIRVOperand *OperandIDOp =
4651 new SPIRVOperand(SPIRVOperandType::NUMBERID, OperandID);
4652 Ops.push_back(OperandIDOp);
4653
4654 VMap[&I] = nextID;
4655
4656 Inst =
4657 new SPIRVInstruction(7, spv::OpImageSampleExplicitLod, nextID++, Ops);
4658 SPIRVInstList.push_back(Inst);
4659 break;
4660 }
4661
4662 // write_imagef is mapped to OpImageWrite.
4663 if (Callee->getName().equals(
4664 "_Z12write_imagef14ocl_image2d_woDv2_iDv4_f") ||
4665 Callee->getName().equals(
4666 "_Z12write_imagef14ocl_image3d_woDv4_iDv4_f")) {
4667 //
4668 // Generate OpImageWrite.
4669 //
4670 // Ops[0] = Image ID
4671 // Ops[1] = Coordinate ID
4672 // Ops[2] = Texel ID
4673 // Ops[3] = (Optional) Image Operands Type (Literal Number)
4674 // Ops[4] ... Ops[n] = (Optional) Operands ID
4675 //
4676 SPIRVOperandList Ops;
4677
4678 Value *Image = Call->getArgOperand(0);
4679 Value *Coordinate = Call->getArgOperand(1);
4680 Value *Texel = Call->getArgOperand(2);
4681
4682 uint32_t ImageID = VMap[Image];
4683 SPIRVOperand *ImageIDOp =
4684 new SPIRVOperand(SPIRVOperandType::NUMBERID, ImageID);
4685 Ops.push_back(ImageIDOp);
4686
4687 uint32_t CoordinateID = VMap[Coordinate];
4688 SPIRVOperand *CoordinateIDOp =
4689 new SPIRVOperand(SPIRVOperandType::NUMBERID, CoordinateID);
4690 Ops.push_back(CoordinateIDOp);
4691
4692 uint32_t TexelID = VMap[Texel];
4693 SPIRVOperand *TexelIDOp =
4694 new SPIRVOperand(SPIRVOperandType::NUMBERID, TexelID);
4695 Ops.push_back(TexelIDOp);
4696
4697 SPIRVInstruction *Inst =
4698 new SPIRVInstruction(4, spv::OpImageWrite, 0 /* No id */, Ops);
4699 SPIRVInstList.push_back(Inst);
4700 break;
4701 }
4702
4703 // Call instrucion is deferred because it needs function's ID. Record
4704 // slot's location on SPIRVInstructionList.
4705 DeferredInsts.push_back(
4706 std::make_tuple(&I, --SPIRVInstList.end(), nextID++));
4707
4708 // Check whether this call is for extend instructions.
4709 glsl::ExtInst EInst = getExtInstEnum(Callee->getName());
4710 if (EInst == glsl::ExtInstFindUMsb) {
4711 // clz needs OpExtInst and OpISub with constant 31. Increase nextID.
4712 VMap[&I] = nextID;
4713 nextID++;
4714 }
4715 break;
4716 }
4717 case Instruction::Ret: {
4718 unsigned NumOps = I.getNumOperands();
4719 if (NumOps == 0) {
4720 //
4721 // Generate OpReturn.
4722 //
4723
4724 // Empty Ops
4725 SPIRVOperandList Ops;
4726 SPIRVInstruction *Inst =
4727 new SPIRVInstruction(1, spv::OpReturn, 0 /* No id */, Ops);
4728 SPIRVInstList.push_back(Inst);
4729 } else {
4730 //
4731 // Generate OpReturnValue.
4732 //
4733
4734 // Ops[0] = Return Value ID
4735 SPIRVOperandList Ops;
4736 uint32_t RetValID = VMap[I.getOperand(0)];
4737 SPIRVOperand *RetValIDOp =
4738 new SPIRVOperand(SPIRVOperandType::NUMBERID, RetValID);
4739 Ops.push_back(RetValIDOp);
4740
4741 SPIRVInstruction *Inst =
4742 new SPIRVInstruction(2, spv::OpReturnValue, 0 /* No id */, Ops);
4743 SPIRVInstList.push_back(Inst);
4744 break;
4745 }
4746 break;
4747 }
4748 }
4749}
4750
4751void SPIRVProducerPass::GenerateFuncEpilogue() {
4752 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
4753
4754 //
4755 // Generate OpFunctionEnd
4756 //
4757
4758 // Empty Ops
4759 SPIRVOperandList Ops;
4760 SPIRVInstruction *Inst =
4761 new SPIRVInstruction(1, spv::OpFunctionEnd, 0 /* No id */, Ops);
4762 SPIRVInstList.push_back(Inst);
4763}
4764
4765bool SPIRVProducerPass::is4xi8vec(Type *Ty) const {
4766 LLVMContext &Context = Ty->getContext();
4767 if (Ty->isVectorTy()) {
4768 if (Ty->getVectorElementType() == Type::getInt8Ty(Context) &&
4769 Ty->getVectorNumElements() == 4) {
4770 return true;
4771 }
4772 }
4773
4774 return false;
4775}
4776
4777void SPIRVProducerPass::HandleDeferredInstruction() {
4778 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
4779 ValueMapType &VMap = getValueMap();
4780 DeferredInstVecType &DeferredInsts = getDeferredInstVec();
4781
4782 for (auto DeferredInst = DeferredInsts.rbegin();
4783 DeferredInst != DeferredInsts.rend(); ++DeferredInst) {
4784 Value *Inst = std::get<0>(*DeferredInst);
4785 SPIRVInstructionList::iterator InsertPoint = ++std::get<1>(*DeferredInst);
4786 if (InsertPoint != SPIRVInstList.end()) {
4787 while ((*InsertPoint)->getOpcode() == spv::OpPhi) {
4788 ++InsertPoint;
4789 }
4790 }
4791
4792 if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) {
4793 // Check whether basic block, which has this branch instruction, is loop
4794 // header or not. If it is loop header, generate OpLoopMerge and
4795 // OpBranchConditional.
4796 Function *Func = Br->getParent()->getParent();
4797 DominatorTree &DT =
4798 getAnalysis<DominatorTreeWrapperPass>(*Func).getDomTree();
4799 const LoopInfo &LI =
4800 getAnalysis<LoopInfoWrapperPass>(*Func).getLoopInfo();
4801
4802 BasicBlock *BrBB = Br->getParent();
4803 if (LI.isLoopHeader(BrBB)) {
4804 Value *ContinueBB = nullptr;
4805 Value *MergeBB = nullptr;
4806
4807 Loop *L = LI.getLoopFor(BrBB);
4808 MergeBB = L->getExitBlock();
4809 if (!MergeBB) {
4810 // StructurizeCFG pass converts CFG into triangle shape and the cfg
4811 // has regions with single entry/exit. As a result, loop should not
4812 // have multiple exits.
4813 llvm_unreachable("Loop has multiple exits???");
4814 }
4815
4816 if (L->isLoopLatch(BrBB)) {
4817 ContinueBB = BrBB;
4818 } else {
4819 // From SPIR-V spec 2.11, Continue Target must dominate that back-edge
4820 // block.
4821 BasicBlock *Header = L->getHeader();
4822 BasicBlock *Latch = L->getLoopLatch();
4823 for (BasicBlock *BB : L->blocks()) {
4824 if (BB == Header) {
4825 continue;
4826 }
4827
4828 // Check whether block dominates block with back-edge.
4829 if (DT.dominates(BB, Latch)) {
4830 ContinueBB = BB;
4831 }
4832 }
4833
4834 if (!ContinueBB) {
4835 llvm_unreachable("Wrong continue block from loop");
4836 }
4837 }
4838
4839 //
4840 // Generate OpLoopMerge.
4841 //
4842 // Ops[0] = Merge Block ID
4843 // Ops[1] = Continue Target ID
4844 // Ops[2] = Selection Control
4845 SPIRVOperandList Ops;
4846
4847 // StructurizeCFG pass already manipulated CFG. Just use false block of
4848 // branch instruction as merge block.
4849 uint32_t MergeBBID = VMap[MergeBB];
4850 SPIRVOperand *MergeBBIDOp =
4851 new SPIRVOperand(SPIRVOperandType::NUMBERID, MergeBBID);
4852 Ops.push_back(MergeBBIDOp);
4853
4854 uint32_t ContinueBBID = VMap[ContinueBB];
4855 SPIRVOperand *ContinueBBIDOp =
4856 new SPIRVOperand(SPIRVOperandType::NUMBERID, ContinueBBID);
4857 Ops.push_back(ContinueBBIDOp);
4858
4859 SPIRVOperand *SelectionControlOp = new SPIRVOperand(
4860 SPIRVOperandType::NUMBERID, spv::SelectionControlMaskNone);
4861 Ops.push_back(SelectionControlOp);
4862
4863 SPIRVInstruction *MergeInst =
4864 new SPIRVInstruction(4, spv::OpLoopMerge, 0 /* No id */, Ops);
4865 SPIRVInstList.insert(InsertPoint, MergeInst);
4866
4867 } else if (Br->isConditional()) {
4868 bool HasBackEdge = false;
4869
4870 for (unsigned i = 0; i < Br->getNumSuccessors(); i++) {
4871 if (LI.isLoopHeader(Br->getSuccessor(i))) {
4872 HasBackEdge = true;
4873 }
4874 }
4875 if (!HasBackEdge) {
4876 //
4877 // Generate OpSelectionMerge.
4878 //
4879 // Ops[0] = Merge Block ID
4880 // Ops[1] = Selection Control
4881 SPIRVOperandList Ops;
4882
4883 // StructurizeCFG pass already manipulated CFG. Just use false block
4884 // of branch instruction as merge block.
4885 uint32_t MergeBBID = VMap[Br->getSuccessor(1)];
4886 SPIRVOperand *MergeBBIDOp =
4887 new SPIRVOperand(SPIRVOperandType::NUMBERID, MergeBBID);
4888 Ops.push_back(MergeBBIDOp);
4889
4890 SPIRVOperand *SelectionControlOp = new SPIRVOperand(
4891 SPIRVOperandType::NUMBERID, spv::SelectionControlMaskNone);
4892 Ops.push_back(SelectionControlOp);
4893
4894 SPIRVInstruction *MergeInst = new SPIRVInstruction(
4895 3, spv::OpSelectionMerge, 0 /* No id */, Ops);
4896 SPIRVInstList.insert(InsertPoint, MergeInst);
4897 }
4898 }
4899
4900 if (Br->isConditional()) {
4901 //
4902 // Generate OpBranchConditional.
4903 //
4904 // Ops[0] = Condition ID
4905 // Ops[1] = True Label ID
4906 // Ops[2] = False Label ID
4907 // Ops[3] ... Ops[n] = Branch weights (Literal Number)
4908 SPIRVOperandList Ops;
4909
4910 uint32_t CondID = VMap[Br->getCondition()];
4911 SPIRVOperand *CondIDOp =
4912 new SPIRVOperand(SPIRVOperandType::NUMBERID, CondID);
4913 Ops.push_back(CondIDOp);
4914
4915 uint32_t TrueBBID = VMap[Br->getSuccessor(0)];
4916 SPIRVOperand *TrueBBIDOp =
4917 new SPIRVOperand(SPIRVOperandType::NUMBERID, TrueBBID);
4918 Ops.push_back(TrueBBIDOp);
4919
4920 uint32_t FalseBBID = VMap[Br->getSuccessor(1)];
4921 SPIRVOperand *FalseBBIDOp =
4922 new SPIRVOperand(SPIRVOperandType::NUMBERID, FalseBBID);
4923 Ops.push_back(FalseBBIDOp);
4924
4925 SPIRVInstruction *BrInst = new SPIRVInstruction(
4926 4, spv::OpBranchConditional, 0 /* No id */, Ops);
4927 SPIRVInstList.insert(InsertPoint, BrInst);
4928 } else {
4929 //
4930 // Generate OpBranch.
4931 //
4932 // Ops[0] = Target Label ID
4933 SPIRVOperandList Ops;
4934
4935 uint32_t TargetID = VMap[Br->getSuccessor(0)];
4936 SPIRVOperand *TargetIDOp =
4937 new SPIRVOperand(SPIRVOperandType::NUMBERID, TargetID);
4938 Ops.push_back(TargetIDOp);
4939
4940 SPIRVInstList.insert(
4941 InsertPoint,
4942 new SPIRVInstruction(2, spv::OpBranch, 0 /* No id */, Ops));
4943 }
4944 } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) {
4945 //
4946 // Generate OpPhi.
4947 //
4948 // Ops[0] = Result Type ID
4949 // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs
4950 SPIRVOperandList Ops;
4951
4952 uint32_t ResTyID = lookupType(PHI->getType());
4953 SPIRVOperand *ResTyIDOp =
4954 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4955 Ops.push_back(ResTyIDOp);
4956
4957 uint16_t WordCount = 3;
4958 for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
4959 uint32_t VarID = VMap[PHI->getIncomingValue(i)];
4960 SPIRVOperand *VarIDOp =
4961 new SPIRVOperand(SPIRVOperandType::NUMBERID, VarID);
4962 Ops.push_back(VarIDOp);
4963
4964 uint32_t ParentID = VMap[PHI->getIncomingBlock(i)];
4965 SPIRVOperand *ParentIDOp =
4966 new SPIRVOperand(SPIRVOperandType::NUMBERID, ParentID);
4967 Ops.push_back(ParentIDOp);
4968
4969 WordCount += 2;
4970 }
4971
4972 SPIRVInstList.insert(
4973 InsertPoint, new SPIRVInstruction(WordCount, spv::OpPhi,
4974 std::get<2>(*DeferredInst), Ops));
4975 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) {
4976 Function *Callee = Call->getCalledFunction();
4977 glsl::ExtInst EInst = getExtInstEnum(Callee->getName());
4978
4979 if (EInst) {
4980 uint32_t &ExtInstImportID = getOpExtInstImportID();
4981
4982 //
4983 // Generate OpExtInst.
4984 //
4985
4986 // Ops[0] = Result Type ID
4987 // Ops[1] = Set ID (OpExtInstImport ID)
4988 // Ops[2] = Instruction Number (Literal Number)
4989 // Ops[3] ... Ops[n] = Operand 1, ... , Operand n
4990 SPIRVOperandList Ops;
4991
4992 uint32_t ResTyID = lookupType(Call->getType());
4993 SPIRVOperand *ResTyIDOp =
4994 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
4995 Ops.push_back(ResTyIDOp);
4996
4997 SPIRVOperand *SetIDOp =
4998 new SPIRVOperand(SPIRVOperandType::NUMBERID, ExtInstImportID);
4999 Ops.push_back(SetIDOp);
5000
5001 std::vector<uint32_t> LiteralNum;
5002 LiteralNum.push_back(EInst);
5003 SPIRVOperand *InstructionOp =
5004 new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, LiteralNum);
5005 Ops.push_back(InstructionOp);
5006
5007 uint16_t WordCount = 5;
5008
5009 FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType());
5010 for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) {
5011 uint32_t ArgID = VMap[Call->getOperand(i)];
5012 SPIRVOperand *ArgIDOp =
5013 new SPIRVOperand(SPIRVOperandType::NUMBERID, ArgID);
5014 Ops.push_back(ArgIDOp);
5015 WordCount++;
5016 }
5017
5018 SPIRVInstruction *ExtInst = new SPIRVInstruction(
5019 WordCount, spv::OpExtInst, std::get<2>(*DeferredInst), Ops);
5020 SPIRVInstList.insert(InsertPoint, ExtInst);
5021
5022 // clz needs OpExtInst and OpISub with constant 31.
5023 if (EInst == glsl::ExtInstFindUMsb) {
5024 LLVMContext &Context =
5025 Call->getParent()->getParent()->getParent()->getContext();
5026 //
5027 // Generate OpISub with constant 31.
5028 //
5029 // Ops[0] = Result Type ID
5030 // Ops[1] = Operand 0
5031 // Ops[2] = Operand 1
5032 Ops.clear();
5033
5034 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
5035 lookupType(Call->getType())));
5036
5037 Type *IdxTy = Type::getInt32Ty(Context);
5038 Constant *Cst31 = ConstantInt::get(IdxTy, 31);
5039 uint32_t Op0ID = VMap[Cst31];
5040 SPIRVOperand *Op0IDOp =
5041 new SPIRVOperand(SPIRVOperandType::NUMBERID, Op0ID);
5042 Ops.push_back(Op0IDOp);
5043
5044 SPIRVOperand *Op1IDOp = new SPIRVOperand(SPIRVOperandType::NUMBERID,
5045 std::get<2>(*DeferredInst));
5046 Ops.push_back(Op1IDOp);
5047
5048 SPIRVInstList.insert(
5049 InsertPoint,
5050 new SPIRVInstruction(5, spv::OpISub,
5051 std::get<2>(*DeferredInst) + 1, Ops));
5052 }
5053 } else if (Callee->getName().equals("_Z8popcounti") ||
5054 Callee->getName().equals("_Z8popcountj") ||
5055 Callee->getName().equals("_Z8popcountDv2_i") ||
5056 Callee->getName().equals("_Z8popcountDv3_i") ||
5057 Callee->getName().equals("_Z8popcountDv4_i") ||
5058 Callee->getName().equals("_Z8popcountDv2_j") ||
5059 Callee->getName().equals("_Z8popcountDv3_j") ||
5060 Callee->getName().equals("_Z8popcountDv4_j")) {
5061 //
5062 // Generate OpBitCount
5063 //
5064 // Ops[0] = Result Type ID
5065 // Ops[1] = Base ID
5066 SPIRVOperand *Ops[2]{new SPIRVOperand(SPIRVOperandType::NUMBERID,
5067 lookupType(Call->getType())),
5068 new SPIRVOperand(SPIRVOperandType::NUMBERID,
5069 VMap[Call->getOperand(0)])};
5070
5071 SPIRVInstList.insert(
5072 InsertPoint, new SPIRVInstruction(4, spv::OpBitCount,
5073 std::get<2>(*DeferredInst), Ops));
5074 } else {
5075 //
5076 // Generate OpFunctionCall.
5077 //
5078
5079 // Ops[0] = Result Type ID
5080 // Ops[1] = Callee Function ID
5081 // Ops[2] ... Ops[n] = Argument 0, ... , Argument n
5082 SPIRVOperandList Ops;
5083
5084 uint32_t ResTyID = lookupType(Call->getType());
5085 SPIRVOperand *ResTyIDOp =
5086 new SPIRVOperand(SPIRVOperandType::NUMBERID, ResTyID);
5087 Ops.push_back(ResTyIDOp);
5088
5089 uint32_t CalleeID = VMap[Callee];
5090
5091 SPIRVOperand *CalleeIDOp =
5092 new SPIRVOperand(SPIRVOperandType::NUMBERID, CalleeID);
5093 Ops.push_back(CalleeIDOp);
5094
5095 uint16_t WordCount = 4;
5096
5097 FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType());
5098 for (unsigned i = 0; i < CalleeFTy->getNumParams(); i++) {
5099 uint32_t ArgID = VMap[Call->getOperand(i)];
5100 SPIRVOperand *ArgIDOp =
5101 new SPIRVOperand(SPIRVOperandType::NUMBERID, ArgID);
5102 Ops.push_back(ArgIDOp);
5103 WordCount++;
5104 }
5105
5106 SPIRVInstruction *CallInst = new SPIRVInstruction(
5107 WordCount, spv::OpFunctionCall, std::get<2>(*DeferredInst), Ops);
5108 SPIRVInstList.insert(InsertPoint, CallInst);
5109 }
5110 }
5111 }
5112}
5113
David Neto1a1a0582017-07-07 12:01:44 -04005114void SPIRVProducerPass::HandleDeferredDecorations(const DataLayout &DL) {
5115 // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain
5116 // instructions we generated earlier.
5117 if (getPointerTypesNeedingArrayStride().empty())
5118 return;
5119
5120 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
5121 ValueMapType &VMap = getValueMap();
5122
5123 // Find an iterator pointing just past the last decoration.
5124 bool seen_decorations = false;
5125 auto DecoInsertPoint =
5126 std::find_if(SPIRVInstList.begin(), SPIRVInstList.end(),
5127 [&seen_decorations](SPIRVInstruction *Inst) -> bool {
5128 const bool is_decoration =
5129 Inst->getOpcode() == spv::OpDecorate ||
5130 Inst->getOpcode() == spv::OpMemberDecorate;
5131 if (is_decoration) {
5132 seen_decorations = true;
5133 return false;
5134 } else {
5135 return seen_decorations;
5136 }
5137 });
5138
5139 for (auto *type : getPointerTypesNeedingArrayStride()) {
5140 auto *ptrType = cast<PointerType>(type);
5141
5142 // Ops[0] = Target ID
5143 // Ops[1] = Decoration (ArrayStride)
5144 // Ops[2] = Stride number (Literal Number)
5145 SPIRVOperandList Ops;
5146
5147 Ops.push_back(
5148 new SPIRVOperand(SPIRVOperandType::NUMBERID, lookupType(ptrType)));
5149 Ops.push_back(new SPIRVOperand(SPIRVOperandType::NUMBERID,
5150 spv::DecorationArrayStride));
5151 Type *elemTy = ptrType->getElementType();
5152 // Same as DL.getIndexedOfffsetInType( elemTy, { 1 } );
5153 const unsigned stride = DL.getTypeAllocSize(elemTy);
5154 Ops.push_back(new SPIRVOperand(SPIRVOperandType::LITERAL_INTEGER, stride));
5155
5156 SPIRVInstruction *DecoInst =
5157 new SPIRVInstruction(4, spv::OpDecorate, 0 /* No id */, Ops);
5158 SPIRVInstList.insert(DecoInsertPoint, DecoInst);
5159 }
5160}
5161
David Neto22f144c2017-06-12 14:26:21 -04005162glsl::ExtInst SPIRVProducerPass::getExtInstEnum(StringRef Name) {
5163 return StringSwitch<glsl::ExtInst>(Name)
5164 .Case("_Z3absi", glsl::ExtInst::ExtInstSAbs)
5165 .Case("_Z3absDv2_i", glsl::ExtInst::ExtInstSAbs)
5166 .Case("_Z3absDv3_i", glsl::ExtInst::ExtInstSAbs)
5167 .Case("_Z3absDv4_i", glsl::ExtInst::ExtInstSAbs)
5168 .Case("_Z5clampiii", glsl::ExtInst::ExtInstSClamp)
5169 .Case("_Z5clampDv2_iS_S_", glsl::ExtInst::ExtInstSClamp)
5170 .Case("_Z5clampDv3_iS_S_", glsl::ExtInst::ExtInstSClamp)
5171 .Case("_Z5clampDv4_iS_S_", glsl::ExtInst::ExtInstSClamp)
5172 .Case("_Z5clampjjj", glsl::ExtInst::ExtInstUClamp)
5173 .Case("_Z5clampDv2_jS_S_", glsl::ExtInst::ExtInstUClamp)
5174 .Case("_Z5clampDv3_jS_S_", glsl::ExtInst::ExtInstUClamp)
5175 .Case("_Z5clampDv4_jS_S_", glsl::ExtInst::ExtInstUClamp)
5176 .Case("_Z5clampfff", glsl::ExtInst::ExtInstFClamp)
5177 .Case("_Z5clampDv2_fS_S_", glsl::ExtInst::ExtInstFClamp)
5178 .Case("_Z5clampDv3_fS_S_", glsl::ExtInst::ExtInstFClamp)
5179 .Case("_Z5clampDv4_fS_S_", glsl::ExtInst::ExtInstFClamp)
5180 .StartsWith("_Z3clz", glsl::ExtInst::ExtInstFindUMsb)
5181 .Case("_Z3maxii", glsl::ExtInst::ExtInstSMax)
5182 .Case("_Z3maxDv2_iS_", glsl::ExtInst::ExtInstSMax)
5183 .Case("_Z3maxDv3_iS_", glsl::ExtInst::ExtInstSMax)
5184 .Case("_Z3maxDv4_iS_", glsl::ExtInst::ExtInstSMax)
5185 .Case("_Z3maxjj", glsl::ExtInst::ExtInstUMax)
5186 .Case("_Z3maxDv2_jS_", glsl::ExtInst::ExtInstUMax)
5187 .Case("_Z3maxDv3_jS_", glsl::ExtInst::ExtInstUMax)
5188 .Case("_Z3maxDv4_jS_", glsl::ExtInst::ExtInstUMax)
5189 .Case("_Z3maxff", glsl::ExtInst::ExtInstFMax)
5190 .Case("_Z3maxDv2_fS_", glsl::ExtInst::ExtInstFMax)
5191 .Case("_Z3maxDv3_fS_", glsl::ExtInst::ExtInstFMax)
5192 .Case("_Z3maxDv4_fS_", glsl::ExtInst::ExtInstFMax)
5193 .StartsWith("_Z4fmax", glsl::ExtInst::ExtInstFMax)
5194 .Case("_Z3minii", glsl::ExtInst::ExtInstSMin)
5195 .Case("_Z3minDv2_iS_", glsl::ExtInst::ExtInstSMin)
5196 .Case("_Z3minDv3_iS_", glsl::ExtInst::ExtInstSMin)
5197 .Case("_Z3minDv4_iS_", glsl::ExtInst::ExtInstSMin)
5198 .Case("_Z3minjj", glsl::ExtInst::ExtInstUMin)
5199 .Case("_Z3minDv2_jS_", glsl::ExtInst::ExtInstUMin)
5200 .Case("_Z3minDv3_jS_", glsl::ExtInst::ExtInstUMin)
5201 .Case("_Z3minDv4_jS_", glsl::ExtInst::ExtInstUMin)
5202 .Case("_Z3minff", glsl::ExtInst::ExtInstFMin)
5203 .Case("_Z3minDv2_fS_", glsl::ExtInst::ExtInstFMin)
5204 .Case("_Z3minDv3_fS_", glsl::ExtInst::ExtInstFMin)
5205 .Case("_Z3minDv4_fS_", glsl::ExtInst::ExtInstFMin)
5206 .StartsWith("_Z4fmin", glsl::ExtInst::ExtInstFMin)
5207 .StartsWith("_Z7degrees", glsl::ExtInst::ExtInstDegrees)
5208 .StartsWith("_Z7radians", glsl::ExtInst::ExtInstRadians)
5209 .StartsWith("_Z3mix", glsl::ExtInst::ExtInstFMix)
5210 .StartsWith("_Z4acos", glsl::ExtInst::ExtInstAcos)
5211 .StartsWith("_Z5acosh", glsl::ExtInst::ExtInstAcosh)
5212 .StartsWith("_Z4asin", glsl::ExtInst::ExtInstAsin)
5213 .StartsWith("_Z5asinh", glsl::ExtInst::ExtInstAsinh)
5214 .StartsWith("_Z4atan", glsl::ExtInst::ExtInstAtan)
5215 .StartsWith("_Z5atan2", glsl::ExtInst::ExtInstAtan2)
5216 .StartsWith("_Z5atanh", glsl::ExtInst::ExtInstAtanh)
5217 .StartsWith("_Z4ceil", glsl::ExtInst::ExtInstCeil)
5218 .StartsWith("_Z3sin", glsl::ExtInst::ExtInstSin)
5219 .StartsWith("_Z4sinh", glsl::ExtInst::ExtInstSinh)
5220 .StartsWith("_Z8half_sin", glsl::ExtInst::ExtInstSin)
5221 .StartsWith("_Z10native_sin", glsl::ExtInst::ExtInstSin)
5222 .StartsWith("_Z3cos", glsl::ExtInst::ExtInstCos)
5223 .StartsWith("_Z4cosh", glsl::ExtInst::ExtInstCosh)
5224 .StartsWith("_Z8half_cos", glsl::ExtInst::ExtInstCos)
5225 .StartsWith("_Z10native_cos", glsl::ExtInst::ExtInstCos)
5226 .StartsWith("_Z3tan", glsl::ExtInst::ExtInstTan)
5227 .StartsWith("_Z4tanh", glsl::ExtInst::ExtInstTanh)
5228 .StartsWith("_Z8half_tan", glsl::ExtInst::ExtInstTan)
5229 .StartsWith("_Z10native_tan", glsl::ExtInst::ExtInstTan)
5230 .StartsWith("_Z3exp", glsl::ExtInst::ExtInstExp)
5231 .StartsWith("_Z8half_exp", glsl::ExtInst::ExtInstExp)
5232 .StartsWith("_Z10native_exp", glsl::ExtInst::ExtInstExp)
5233 .StartsWith("_Z4exp2", glsl::ExtInst::ExtInstExp2)
5234 .StartsWith("_Z9half_exp2", glsl::ExtInst::ExtInstExp2)
5235 .StartsWith("_Z11native_exp2", glsl::ExtInst::ExtInstExp2)
5236 .StartsWith("_Z3log", glsl::ExtInst::ExtInstLog)
5237 .StartsWith("_Z8half_log", glsl::ExtInst::ExtInstLog)
5238 .StartsWith("_Z10native_log", glsl::ExtInst::ExtInstLog)
5239 .StartsWith("_Z4log2", glsl::ExtInst::ExtInstLog2)
5240 .StartsWith("_Z9half_log2", glsl::ExtInst::ExtInstLog2)
5241 .StartsWith("_Z11native_log2", glsl::ExtInst::ExtInstLog2)
5242 .StartsWith("_Z4fabs", glsl::ExtInst::ExtInstFAbs)
5243 .StartsWith("_Z5floor", glsl::ExtInst::ExtInstFloor)
5244 .StartsWith("_Z5ldexp", glsl::ExtInst::ExtInstLdexp)
5245 .StartsWith("_Z3pow", glsl::ExtInst::ExtInstPow)
5246 .StartsWith("_Z4powr", glsl::ExtInst::ExtInstPow)
5247 .StartsWith("_Z9half_powr", glsl::ExtInst::ExtInstPow)
5248 .StartsWith("_Z11native_powr", glsl::ExtInst::ExtInstPow)
5249 .StartsWith("_Z5round", glsl::ExtInst::ExtInstRound)
5250 .StartsWith("_Z4sqrt", glsl::ExtInst::ExtInstSqrt)
5251 .StartsWith("_Z9half_sqrt", glsl::ExtInst::ExtInstSqrt)
5252 .StartsWith("_Z11native_sqrt", glsl::ExtInst::ExtInstSqrt)
5253 .StartsWith("_Z5rsqrt", glsl::ExtInst::ExtInstInverseSqrt)
5254 .StartsWith("_Z10half_rsqrt", glsl::ExtInst::ExtInstInverseSqrt)
5255 .StartsWith("_Z12native_rsqrt", glsl::ExtInst::ExtInstInverseSqrt)
5256 .StartsWith("_Z5trunc", glsl::ExtInst::ExtInstTrunc)
5257 .StartsWith("_Z5frexp", glsl::ExtInst::ExtInstFrexp)
5258 .StartsWith("_Z4sign", glsl::ExtInst::ExtInstFSign)
5259 .StartsWith("_Z6length", glsl::ExtInst::ExtInstLength)
5260 .StartsWith("_Z8distance", glsl::ExtInst::ExtInstDistance)
5261 .Case("_Z5crossDv3_fS_", glsl::ExtInst::ExtInstCross)
5262 .StartsWith("_Z9normalize", glsl::ExtInst::ExtInstNormalize)
5263 .StartsWith("llvm.fmuladd.", glsl::ExtInst::ExtInstFma)
5264 .Case("spirv.unpack.v2f16", glsl::ExtInst::ExtInstUnpackHalf2x16)
5265 .Case("spirv.pack.v2f16", glsl::ExtInst::ExtInstPackHalf2x16)
5266 .Default(static_cast<glsl::ExtInst>(0));
5267}
5268
5269void SPIRVProducerPass::PrintResID(SPIRVInstruction *Inst) {
5270 out << "%" << Inst->getResultID();
5271}
5272
5273void SPIRVProducerPass::PrintOpcode(SPIRVInstruction *Inst) {
5274 spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode());
5275 out << "\t" << spv::getOpName(Opcode);
5276}
5277
5278void SPIRVProducerPass::PrintOperand(SPIRVOperand *Op) {
5279 SPIRVOperandType OpTy = Op->getType();
5280 switch (OpTy) {
5281 default: {
5282 llvm_unreachable("Unsupported SPIRV Operand Type???");
5283 break;
5284 }
5285 case SPIRVOperandType::NUMBERID: {
5286 out << "%" << Op->getNumID();
5287 break;
5288 }
5289 case SPIRVOperandType::LITERAL_STRING: {
5290 out << "\"" << Op->getLiteralStr() << "\"";
5291 break;
5292 }
5293 case SPIRVOperandType::LITERAL_INTEGER: {
5294 // TODO: Handle LiteralNum carefully.
5295 for (auto Word : Op->getLiteralNum()) {
5296 out << Word;
5297 }
5298 break;
5299 }
5300 case SPIRVOperandType::LITERAL_FLOAT: {
5301 // TODO: Handle LiteralNum carefully.
5302 for (auto Word : Op->getLiteralNum()) {
5303 APFloat APF = APFloat(APFloat::IEEEsingle(), APInt(32, Word));
5304 SmallString<8> Str;
5305 APF.toString(Str, 6, 2);
5306 out << Str;
5307 }
5308 break;
5309 }
5310 }
5311}
5312
5313void SPIRVProducerPass::PrintCapability(SPIRVOperand *Op) {
5314 spv::Capability Cap = static_cast<spv::Capability>(Op->getNumID());
5315 out << spv::getCapabilityName(Cap);
5316}
5317
5318void SPIRVProducerPass::PrintExtInst(SPIRVOperand *Op) {
5319 auto LiteralNum = Op->getLiteralNum();
5320 glsl::ExtInst Ext = static_cast<glsl::ExtInst>(LiteralNum[0]);
5321 out << glsl::getExtInstName(Ext);
5322}
5323
5324void SPIRVProducerPass::PrintAddrModel(SPIRVOperand *Op) {
5325 spv::AddressingModel AddrModel =
5326 static_cast<spv::AddressingModel>(Op->getNumID());
5327 out << spv::getAddressingModelName(AddrModel);
5328}
5329
5330void SPIRVProducerPass::PrintMemModel(SPIRVOperand *Op) {
5331 spv::MemoryModel MemModel = static_cast<spv::MemoryModel>(Op->getNumID());
5332 out << spv::getMemoryModelName(MemModel);
5333}
5334
5335void SPIRVProducerPass::PrintExecModel(SPIRVOperand *Op) {
5336 spv::ExecutionModel ExecModel =
5337 static_cast<spv::ExecutionModel>(Op->getNumID());
5338 out << spv::getExecutionModelName(ExecModel);
5339}
5340
5341void SPIRVProducerPass::PrintExecMode(SPIRVOperand *Op) {
5342 spv::ExecutionMode ExecMode = static_cast<spv::ExecutionMode>(Op->getNumID());
5343 out << spv::getExecutionModeName(ExecMode);
5344}
5345
5346void SPIRVProducerPass::PrintSourceLanguage(SPIRVOperand *Op) {
5347 spv::SourceLanguage SourceLang = static_cast<spv::SourceLanguage>(Op->getNumID());
5348 out << spv::getSourceLanguageName(SourceLang);
5349}
5350
5351void SPIRVProducerPass::PrintFuncCtrl(SPIRVOperand *Op) {
5352 spv::FunctionControlMask FuncCtrl =
5353 static_cast<spv::FunctionControlMask>(Op->getNumID());
5354 out << spv::getFunctionControlName(FuncCtrl);
5355}
5356
5357void SPIRVProducerPass::PrintStorageClass(SPIRVOperand *Op) {
5358 spv::StorageClass StClass = static_cast<spv::StorageClass>(Op->getNumID());
5359 out << getStorageClassName(StClass);
5360}
5361
5362void SPIRVProducerPass::PrintDecoration(SPIRVOperand *Op) {
5363 spv::Decoration Deco = static_cast<spv::Decoration>(Op->getNumID());
5364 out << getDecorationName(Deco);
5365}
5366
5367void SPIRVProducerPass::PrintBuiltIn(SPIRVOperand *Op) {
5368 spv::BuiltIn BIn = static_cast<spv::BuiltIn>(Op->getNumID());
5369 out << getBuiltInName(BIn);
5370}
5371
5372void SPIRVProducerPass::PrintSelectionControl(SPIRVOperand *Op) {
5373 spv::SelectionControlMask BIn =
5374 static_cast<spv::SelectionControlMask>(Op->getNumID());
5375 out << getSelectionControlName(BIn);
5376}
5377
5378void SPIRVProducerPass::PrintLoopControl(SPIRVOperand *Op) {
5379 spv::LoopControlMask BIn = static_cast<spv::LoopControlMask>(Op->getNumID());
5380 out << getLoopControlName(BIn);
5381}
5382
5383void SPIRVProducerPass::PrintDimensionality(SPIRVOperand *Op) {
5384 spv::Dim DIM = static_cast<spv::Dim>(Op->getNumID());
5385 out << getDimName(DIM);
5386}
5387
5388void SPIRVProducerPass::PrintImageFormat(SPIRVOperand *Op) {
5389 spv::ImageFormat Format = static_cast<spv::ImageFormat>(Op->getNumID());
5390 out << getImageFormatName(Format);
5391}
5392
5393void SPIRVProducerPass::PrintMemoryAccess(SPIRVOperand *Op) {
5394 out << spv::getMemoryAccessName(
5395 static_cast<spv::MemoryAccessMask>(Op->getNumID()));
5396}
5397
5398void SPIRVProducerPass::PrintImageOperandsType(SPIRVOperand *Op) {
5399 auto LiteralNum = Op->getLiteralNum();
5400 spv::ImageOperandsMask Type =
5401 static_cast<spv::ImageOperandsMask>(LiteralNum[0]);
5402 out << getImageOperandsName(Type);
5403}
5404
5405void SPIRVProducerPass::WriteSPIRVAssembly() {
5406 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
5407
5408 for (auto Inst : SPIRVInstList) {
5409 SPIRVOperandList Ops = Inst->getOperands();
5410 spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode());
5411
5412 switch (Opcode) {
5413 default: {
5414 llvm_unreachable("Unsupported SPIRV instruction");
5415 break;
5416 }
5417 case spv::OpCapability: {
5418 // Ops[0] = Capability
5419 PrintOpcode(Inst);
5420 out << " ";
5421 PrintCapability(Ops[0]);
5422 out << "\n";
5423 break;
5424 }
5425 case spv::OpMemoryModel: {
5426 // Ops[0] = Addressing Model
5427 // Ops[1] = Memory Model
5428 PrintOpcode(Inst);
5429 out << " ";
5430 PrintAddrModel(Ops[0]);
5431 out << " ";
5432 PrintMemModel(Ops[1]);
5433 out << "\n";
5434 break;
5435 }
5436 case spv::OpEntryPoint: {
5437 // Ops[0] = Execution Model
5438 // Ops[1] = EntryPoint ID
5439 // Ops[2] = Name (Literal String)
5440 // Ops[3] ... Ops[n] = Interface ID
5441 PrintOpcode(Inst);
5442 out << " ";
5443 PrintExecModel(Ops[0]);
5444 for (uint32_t i = 1; i < Ops.size(); i++) {
5445 out << " ";
5446 PrintOperand(Ops[i]);
5447 }
5448 out << "\n";
5449 break;
5450 }
5451 case spv::OpExecutionMode: {
5452 // Ops[0] = Entry Point ID
5453 // Ops[1] = Execution Mode
5454 // Ops[2] ... Ops[n] = Optional literals according to Execution Mode
5455 PrintOpcode(Inst);
5456 out << " ";
5457 PrintOperand(Ops[0]);
5458 out << " ";
5459 PrintExecMode(Ops[1]);
5460 for (uint32_t i = 2; i < Ops.size(); i++) {
5461 out << " ";
5462 PrintOperand(Ops[i]);
5463 }
5464 out << "\n";
5465 break;
5466 }
5467 case spv::OpSource: {
5468 // Ops[0] = SourceLanguage ID
5469 // Ops[1] = Version (LiteralNum)
5470 PrintOpcode(Inst);
5471 out << " ";
5472 PrintSourceLanguage(Ops[0]);
5473 out << " ";
5474 PrintOperand(Ops[1]);
5475 out << "\n";
5476 break;
5477 }
5478 case spv::OpDecorate: {
5479 // Ops[0] = Target ID
5480 // Ops[1] = Decoration (Block or BufferBlock)
5481 // Ops[2] ... Ops[n] = Optional literals according to Decoration
5482 PrintOpcode(Inst);
5483 out << " ";
5484 PrintOperand(Ops[0]);
5485 out << " ";
5486 PrintDecoration(Ops[1]);
5487 // Handle BuiltIn OpDecorate specially.
5488 if (Ops[1]->getNumID() == spv::DecorationBuiltIn) {
5489 out << " ";
5490 PrintBuiltIn(Ops[2]);
5491 } else {
5492 for (uint32_t i = 2; i < Ops.size(); i++) {
5493 out << " ";
5494 PrintOperand(Ops[i]);
5495 }
5496 }
5497 out << "\n";
5498 break;
5499 }
5500 case spv::OpMemberDecorate: {
5501 // Ops[0] = Structure Type ID
5502 // Ops[1] = Member Index(Literal Number)
5503 // Ops[2] = Decoration
5504 // Ops[3] ... Ops[n] = Optional literals according to Decoration
5505 PrintOpcode(Inst);
5506 out << " ";
5507 PrintOperand(Ops[0]);
5508 out << " ";
5509 PrintOperand(Ops[1]);
5510 out << " ";
5511 PrintDecoration(Ops[2]);
5512 for (uint32_t i = 3; i < Ops.size(); i++) {
5513 out << " ";
5514 PrintOperand(Ops[i]);
5515 }
5516 out << "\n";
5517 break;
5518 }
5519 case spv::OpTypePointer: {
5520 // Ops[0] = Storage Class
5521 // Ops[1] = Element Type ID
5522 PrintResID(Inst);
5523 out << " = ";
5524 PrintOpcode(Inst);
5525 out << " ";
5526 PrintStorageClass(Ops[0]);
5527 out << " ";
5528 PrintOperand(Ops[1]);
5529 out << "\n";
5530 break;
5531 }
5532 case spv::OpTypeImage: {
5533 // Ops[0] = Sampled Type ID
5534 // Ops[1] = Dim ID
5535 // Ops[2] = Depth (Literal Number)
5536 // Ops[3] = Arrayed (Literal Number)
5537 // Ops[4] = MS (Literal Number)
5538 // Ops[5] = Sampled (Literal Number)
5539 // Ops[6] = Image Format ID
5540 PrintResID(Inst);
5541 out << " = ";
5542 PrintOpcode(Inst);
5543 out << " ";
5544 PrintOperand(Ops[0]);
5545 out << " ";
5546 PrintDimensionality(Ops[1]);
5547 out << " ";
5548 PrintOperand(Ops[2]);
5549 out << " ";
5550 PrintOperand(Ops[3]);
5551 out << " ";
5552 PrintOperand(Ops[4]);
5553 out << " ";
5554 PrintOperand(Ops[5]);
5555 out << " ";
5556 PrintImageFormat(Ops[6]);
5557 out << "\n";
5558 break;
5559 }
5560 case spv::OpFunction: {
5561 // Ops[0] : Result Type ID
5562 // Ops[1] : Function Control
5563 // Ops[2] : Function Type ID
5564 PrintResID(Inst);
5565 out << " = ";
5566 PrintOpcode(Inst);
5567 out << " ";
5568 PrintOperand(Ops[0]);
5569 out << " ";
5570 PrintFuncCtrl(Ops[1]);
5571 out << " ";
5572 PrintOperand(Ops[2]);
5573 out << "\n";
5574 break;
5575 }
5576 case spv::OpSelectionMerge: {
5577 // Ops[0] = Merge Block ID
5578 // Ops[1] = Selection Control
5579 PrintOpcode(Inst);
5580 out << " ";
5581 PrintOperand(Ops[0]);
5582 out << " ";
5583 PrintSelectionControl(Ops[1]);
5584 out << "\n";
5585 break;
5586 }
5587 case spv::OpLoopMerge: {
5588 // Ops[0] = Merge Block ID
5589 // Ops[1] = Continue Target ID
5590 // Ops[2] = Selection Control
5591 PrintOpcode(Inst);
5592 out << " ";
5593 PrintOperand(Ops[0]);
5594 out << " ";
5595 PrintOperand(Ops[1]);
5596 out << " ";
5597 PrintLoopControl(Ops[2]);
5598 out << "\n";
5599 break;
5600 }
5601 case spv::OpImageSampleExplicitLod: {
5602 // Ops[0] = Result Type ID
5603 // Ops[1] = Sampled Image ID
5604 // Ops[2] = Coordinate ID
5605 // Ops[3] = Image Operands Type ID
5606 // Ops[4] ... Ops[n] = Operands ID
5607 PrintResID(Inst);
5608 out << " = ";
5609 PrintOpcode(Inst);
5610 for (uint32_t i = 0; i < 3; i++) {
5611 out << " ";
5612 PrintOperand(Ops[i]);
5613 }
5614 out << " ";
5615 PrintImageOperandsType(Ops[3]);
5616 for (uint32_t i = 4; i < Ops.size(); i++) {
5617 out << " ";
5618 PrintOperand(Ops[i]);
5619 }
5620 out << "\n";
5621 break;
5622 }
5623 case spv::OpVariable: {
5624 // Ops[0] : Result Type ID
5625 // Ops[1] : Storage Class
5626 // Ops[2] ... Ops[n] = Initializer IDs
5627 PrintResID(Inst);
5628 out << " = ";
5629 PrintOpcode(Inst);
5630 out << " ";
5631 PrintOperand(Ops[0]);
5632 out << " ";
5633 PrintStorageClass(Ops[1]);
5634 for (uint32_t i = 2; i < Ops.size(); i++) {
5635 out << " ";
5636 PrintOperand(Ops[i]);
5637 }
5638 out << "\n";
5639 break;
5640 }
5641 case spv::OpExtInst: {
5642 // Ops[0] = Result Type ID
5643 // Ops[1] = Set ID (OpExtInstImport ID)
5644 // Ops[2] = Instruction Number (Literal Number)
5645 // Ops[3] ... Ops[n] = Operand 1, ... , Operand n
5646 PrintResID(Inst);
5647 out << " = ";
5648 PrintOpcode(Inst);
5649 out << " ";
5650 PrintOperand(Ops[0]);
5651 out << " ";
5652 PrintOperand(Ops[1]);
5653 out << " ";
5654 PrintExtInst(Ops[2]);
5655 for (uint32_t i = 3; i < Ops.size(); i++) {
5656 out << " ";
5657 PrintOperand(Ops[i]);
5658 }
5659 out << "\n";
5660 break;
5661 }
5662 case spv::OpCopyMemory: {
5663 // Ops[0] = Addressing Model
5664 // Ops[1] = Memory Model
5665 PrintOpcode(Inst);
5666 out << " ";
5667 PrintOperand(Ops[0]);
5668 out << " ";
5669 PrintOperand(Ops[1]);
5670 out << " ";
5671 PrintMemoryAccess(Ops[2]);
5672 out << " ";
5673 PrintOperand(Ops[3]);
5674 out << "\n";
5675 break;
5676 }
5677 case spv::OpExtension:
5678 case spv::OpControlBarrier:
5679 case spv::OpMemoryBarrier:
5680 case spv::OpBranch:
5681 case spv::OpBranchConditional:
5682 case spv::OpStore:
5683 case spv::OpImageWrite:
5684 case spv::OpReturnValue:
5685 case spv::OpReturn:
5686 case spv::OpFunctionEnd: {
5687 PrintOpcode(Inst);
5688 for (uint32_t i = 0; i < Ops.size(); i++) {
5689 out << " ";
5690 PrintOperand(Ops[i]);
5691 }
5692 out << "\n";
5693 break;
5694 }
5695 case spv::OpExtInstImport:
5696 case spv::OpTypeRuntimeArray:
5697 case spv::OpTypeStruct:
5698 case spv::OpTypeSampler:
5699 case spv::OpTypeSampledImage:
5700 case spv::OpTypeInt:
5701 case spv::OpTypeFloat:
5702 case spv::OpTypeArray:
5703 case spv::OpTypeVector:
5704 case spv::OpTypeBool:
5705 case spv::OpTypeVoid:
5706 case spv::OpTypeFunction:
5707 case spv::OpFunctionParameter:
5708 case spv::OpLabel:
5709 case spv::OpPhi:
5710 case spv::OpLoad:
5711 case spv::OpSelect:
5712 case spv::OpAccessChain:
5713 case spv::OpPtrAccessChain:
5714 case spv::OpInBoundsAccessChain:
5715 case spv::OpUConvert:
5716 case spv::OpSConvert:
5717 case spv::OpConvertFToU:
5718 case spv::OpConvertFToS:
5719 case spv::OpConvertUToF:
5720 case spv::OpConvertSToF:
5721 case spv::OpFConvert:
5722 case spv::OpConvertPtrToU:
5723 case spv::OpConvertUToPtr:
5724 case spv::OpBitcast:
5725 case spv::OpIAdd:
5726 case spv::OpFAdd:
5727 case spv::OpISub:
5728 case spv::OpFSub:
5729 case spv::OpIMul:
5730 case spv::OpFMul:
5731 case spv::OpUDiv:
5732 case spv::OpSDiv:
5733 case spv::OpFDiv:
5734 case spv::OpUMod:
5735 case spv::OpSRem:
5736 case spv::OpFRem:
5737 case spv::OpBitwiseOr:
5738 case spv::OpBitwiseXor:
5739 case spv::OpBitwiseAnd:
5740 case spv::OpShiftLeftLogical:
5741 case spv::OpShiftRightLogical:
5742 case spv::OpShiftRightArithmetic:
5743 case spv::OpBitCount:
5744 case spv::OpCompositeExtract:
5745 case spv::OpVectorExtractDynamic:
5746 case spv::OpCompositeInsert:
5747 case spv::OpVectorInsertDynamic:
5748 case spv::OpVectorShuffle:
5749 case spv::OpIEqual:
5750 case spv::OpINotEqual:
5751 case spv::OpUGreaterThan:
5752 case spv::OpUGreaterThanEqual:
5753 case spv::OpULessThan:
5754 case spv::OpULessThanEqual:
5755 case spv::OpSGreaterThan:
5756 case spv::OpSGreaterThanEqual:
5757 case spv::OpSLessThan:
5758 case spv::OpSLessThanEqual:
5759 case spv::OpFOrdEqual:
5760 case spv::OpFOrdGreaterThan:
5761 case spv::OpFOrdGreaterThanEqual:
5762 case spv::OpFOrdLessThan:
5763 case spv::OpFOrdLessThanEqual:
5764 case spv::OpFOrdNotEqual:
5765 case spv::OpFUnordEqual:
5766 case spv::OpFUnordGreaterThan:
5767 case spv::OpFUnordGreaterThanEqual:
5768 case spv::OpFUnordLessThan:
5769 case spv::OpFUnordLessThanEqual:
5770 case spv::OpFUnordNotEqual:
5771 case spv::OpSampledImage:
5772 case spv::OpFunctionCall:
5773 case spv::OpConstantTrue:
5774 case spv::OpConstantFalse:
5775 case spv::OpConstant:
5776 case spv::OpSpecConstant:
5777 case spv::OpConstantComposite:
5778 case spv::OpSpecConstantComposite:
5779 case spv::OpConstantNull:
5780 case spv::OpLogicalOr:
5781 case spv::OpLogicalAnd:
5782 case spv::OpLogicalNot:
5783 case spv::OpLogicalNotEqual:
5784 case spv::OpUndef:
5785 case spv::OpIsInf:
5786 case spv::OpIsNan:
5787 case spv::OpAny:
5788 case spv::OpAll:
5789 case spv::OpAtomicIAdd:
5790 case spv::OpAtomicISub:
5791 case spv::OpAtomicExchange:
5792 case spv::OpAtomicIIncrement:
5793 case spv::OpAtomicIDecrement:
5794 case spv::OpAtomicCompareExchange:
5795 case spv::OpAtomicUMin:
5796 case spv::OpAtomicSMin:
5797 case spv::OpAtomicUMax:
5798 case spv::OpAtomicSMax:
5799 case spv::OpAtomicAnd:
5800 case spv::OpAtomicOr:
5801 case spv::OpAtomicXor:
5802 case spv::OpDot: {
5803 PrintResID(Inst);
5804 out << " = ";
5805 PrintOpcode(Inst);
5806 for (uint32_t i = 0; i < Ops.size(); i++) {
5807 out << " ";
5808 PrintOperand(Ops[i]);
5809 }
5810 out << "\n";
5811 break;
5812 }
5813 }
5814 }
5815}
5816
5817void SPIRVProducerPass::WriteOneWord(uint32_t Word) {
David Neto0676e6f2017-07-11 18:47:44 -04005818 binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t));
David Neto22f144c2017-06-12 14:26:21 -04005819}
5820
5821void SPIRVProducerPass::WriteResultID(SPIRVInstruction *Inst) {
5822 WriteOneWord(Inst->getResultID());
5823}
5824
5825void SPIRVProducerPass::WriteWordCountAndOpcode(SPIRVInstruction *Inst) {
5826 // High 16 bit : Word Count
5827 // Low 16 bit : Opcode
5828 uint32_t Word = Inst->getOpcode();
5829 Word |= Inst->getWordCount() << 16;
5830 WriteOneWord(Word);
5831}
5832
5833void SPIRVProducerPass::WriteOperand(SPIRVOperand *Op) {
5834 SPIRVOperandType OpTy = Op->getType();
5835 switch (OpTy) {
5836 default: {
5837 llvm_unreachable("Unsupported SPIRV Operand Type???");
5838 break;
5839 }
5840 case SPIRVOperandType::NUMBERID: {
5841 WriteOneWord(Op->getNumID());
5842 break;
5843 }
5844 case SPIRVOperandType::LITERAL_STRING: {
5845 std::string Str = Op->getLiteralStr();
5846 const char *Data = Str.c_str();
5847 size_t WordSize = Str.size() / 4;
5848 for (unsigned Idx = 0; Idx < WordSize; Idx++) {
5849 WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx]));
5850 }
5851
5852 uint32_t Remainder = Str.size() % 4;
5853 uint32_t LastWord = 0;
5854 if (Remainder) {
5855 for (unsigned Idx = 0; Idx < Remainder; Idx++) {
5856 LastWord |= Data[4 * WordSize + Idx] << 8 * Idx;
5857 }
5858 }
5859
5860 WriteOneWord(LastWord);
5861 break;
5862 }
5863 case SPIRVOperandType::LITERAL_INTEGER:
5864 case SPIRVOperandType::LITERAL_FLOAT: {
5865 auto LiteralNum = Op->getLiteralNum();
5866 // TODO: Handle LiteranNum carefully.
5867 for (auto Word : LiteralNum) {
5868 WriteOneWord(Word);
5869 }
5870 break;
5871 }
5872 }
5873}
5874
5875void SPIRVProducerPass::WriteSPIRVBinary() {
5876 SPIRVInstructionList &SPIRVInstList = getSPIRVInstList();
5877
5878 for (auto Inst : SPIRVInstList) {
5879 SPIRVOperandList Ops = Inst->getOperands();
5880 spv::Op Opcode = static_cast<spv::Op>(Inst->getOpcode());
5881
5882 switch (Opcode) {
5883 default: {
5884 llvm_unreachable("Unsupported SPIRV instruction");
5885 break;
5886 }
5887 case spv::OpCapability:
5888 case spv::OpExtension:
5889 case spv::OpMemoryModel:
5890 case spv::OpEntryPoint:
5891 case spv::OpExecutionMode:
5892 case spv::OpSource:
5893 case spv::OpDecorate:
5894 case spv::OpMemberDecorate:
5895 case spv::OpBranch:
5896 case spv::OpBranchConditional:
5897 case spv::OpSelectionMerge:
5898 case spv::OpLoopMerge:
5899 case spv::OpStore:
5900 case spv::OpImageWrite:
5901 case spv::OpReturnValue:
5902 case spv::OpControlBarrier:
5903 case spv::OpMemoryBarrier:
5904 case spv::OpReturn:
5905 case spv::OpFunctionEnd:
5906 case spv::OpCopyMemory: {
5907 WriteWordCountAndOpcode(Inst);
5908 for (uint32_t i = 0; i < Ops.size(); i++) {
5909 WriteOperand(Ops[i]);
5910 }
5911 break;
5912 }
5913 case spv::OpTypeBool:
5914 case spv::OpTypeVoid:
5915 case spv::OpTypeSampler:
5916 case spv::OpLabel:
5917 case spv::OpExtInstImport:
5918 case spv::OpTypePointer:
5919 case spv::OpTypeRuntimeArray:
5920 case spv::OpTypeStruct:
5921 case spv::OpTypeImage:
5922 case spv::OpTypeSampledImage:
5923 case spv::OpTypeInt:
5924 case spv::OpTypeFloat:
5925 case spv::OpTypeArray:
5926 case spv::OpTypeVector:
5927 case spv::OpTypeFunction: {
5928 WriteWordCountAndOpcode(Inst);
5929 WriteResultID(Inst);
5930 for (uint32_t i = 0; i < Ops.size(); i++) {
5931 WriteOperand(Ops[i]);
5932 }
5933 break;
5934 }
5935 case spv::OpFunction:
5936 case spv::OpFunctionParameter:
5937 case spv::OpAccessChain:
5938 case spv::OpPtrAccessChain:
5939 case spv::OpInBoundsAccessChain:
5940 case spv::OpUConvert:
5941 case spv::OpSConvert:
5942 case spv::OpConvertFToU:
5943 case spv::OpConvertFToS:
5944 case spv::OpConvertUToF:
5945 case spv::OpConvertSToF:
5946 case spv::OpFConvert:
5947 case spv::OpConvertPtrToU:
5948 case spv::OpConvertUToPtr:
5949 case spv::OpBitcast:
5950 case spv::OpIAdd:
5951 case spv::OpFAdd:
5952 case spv::OpISub:
5953 case spv::OpFSub:
5954 case spv::OpIMul:
5955 case spv::OpFMul:
5956 case spv::OpUDiv:
5957 case spv::OpSDiv:
5958 case spv::OpFDiv:
5959 case spv::OpUMod:
5960 case spv::OpSRem:
5961 case spv::OpFRem:
5962 case spv::OpBitwiseOr:
5963 case spv::OpBitwiseXor:
5964 case spv::OpBitwiseAnd:
5965 case spv::OpShiftLeftLogical:
5966 case spv::OpShiftRightLogical:
5967 case spv::OpShiftRightArithmetic:
5968 case spv::OpBitCount:
5969 case spv::OpCompositeExtract:
5970 case spv::OpVectorExtractDynamic:
5971 case spv::OpCompositeInsert:
5972 case spv::OpVectorInsertDynamic:
5973 case spv::OpVectorShuffle:
5974 case spv::OpIEqual:
5975 case spv::OpINotEqual:
5976 case spv::OpUGreaterThan:
5977 case spv::OpUGreaterThanEqual:
5978 case spv::OpULessThan:
5979 case spv::OpULessThanEqual:
5980 case spv::OpSGreaterThan:
5981 case spv::OpSGreaterThanEqual:
5982 case spv::OpSLessThan:
5983 case spv::OpSLessThanEqual:
5984 case spv::OpFOrdEqual:
5985 case spv::OpFOrdGreaterThan:
5986 case spv::OpFOrdGreaterThanEqual:
5987 case spv::OpFOrdLessThan:
5988 case spv::OpFOrdLessThanEqual:
5989 case spv::OpFOrdNotEqual:
5990 case spv::OpFUnordEqual:
5991 case spv::OpFUnordGreaterThan:
5992 case spv::OpFUnordGreaterThanEqual:
5993 case spv::OpFUnordLessThan:
5994 case spv::OpFUnordLessThanEqual:
5995 case spv::OpFUnordNotEqual:
5996 case spv::OpExtInst:
5997 case spv::OpIsInf:
5998 case spv::OpIsNan:
5999 case spv::OpAny:
6000 case spv::OpAll:
6001 case spv::OpUndef:
6002 case spv::OpConstantNull:
6003 case spv::OpLogicalOr:
6004 case spv::OpLogicalAnd:
6005 case spv::OpLogicalNot:
6006 case spv::OpLogicalNotEqual:
6007 case spv::OpConstantComposite:
6008 case spv::OpSpecConstantComposite:
6009 case spv::OpConstantTrue:
6010 case spv::OpConstantFalse:
6011 case spv::OpConstant:
6012 case spv::OpSpecConstant:
6013 case spv::OpVariable:
6014 case spv::OpFunctionCall:
6015 case spv::OpSampledImage:
6016 case spv::OpImageSampleExplicitLod:
6017 case spv::OpSelect:
6018 case spv::OpPhi:
6019 case spv::OpLoad:
6020 case spv::OpAtomicIAdd:
6021 case spv::OpAtomicISub:
6022 case spv::OpAtomicExchange:
6023 case spv::OpAtomicIIncrement:
6024 case spv::OpAtomicIDecrement:
6025 case spv::OpAtomicCompareExchange:
6026 case spv::OpAtomicUMin:
6027 case spv::OpAtomicSMin:
6028 case spv::OpAtomicUMax:
6029 case spv::OpAtomicSMax:
6030 case spv::OpAtomicAnd:
6031 case spv::OpAtomicOr:
6032 case spv::OpAtomicXor:
6033 case spv::OpDot: {
6034 WriteWordCountAndOpcode(Inst);
6035 WriteOperand(Ops[0]);
6036 WriteResultID(Inst);
6037 for (uint32_t i = 1; i < Ops.size(); i++) {
6038 WriteOperand(Ops[i]);
6039 }
6040 break;
6041 }
6042 }
6043 }
6044}