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