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