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