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