blob: 9e3ca396c10b263ac3df89e5327e918be304563c [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 Neto257c3892018-04-11 13:19:45 -040020#include <cstring>
David Neto118188e2018-08-24 11:27:54 -040021#include <iomanip>
22#include <list>
David Neto862b7d82018-06-14 18:48:37 -040023#include <memory>
David Neto118188e2018-08-24 11:27:54 -040024#include <set>
25#include <sstream>
26#include <string>
27#include <tuple>
28#include <unordered_set>
29#include <utility>
David Neto862b7d82018-06-14 18:48:37 -040030
David Neto118188e2018-08-24 11:27:54 -040031#include "llvm/ADT/StringSwitch.h"
32#include "llvm/ADT/UniqueVector.h"
33#include "llvm/Analysis/LoopInfo.h"
34#include "llvm/IR/Constants.h"
35#include "llvm/IR/Dominators.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/Metadata.h"
38#include "llvm/IR/Module.h"
alan-bakerf67468c2019-11-25 15:51:49 -050039#include "llvm/IR/ValueSymbolTable.h"
David Neto118188e2018-08-24 11:27:54 -040040#include "llvm/Pass.h"
41#include "llvm/Support/CommandLine.h"
Kévin Petitbbbda972020-03-03 19:16:31 +000042#include "llvm/Support/MathExtras.h"
David Neto118188e2018-08-24 11:27:54 -040043#include "llvm/Support/raw_ostream.h"
44#include "llvm/Transforms/Utils/Cloning.h"
David Neto22f144c2017-06-12 14:26:21 -040045
SJWf93f5f32020-05-05 07:27:56 -050046// enable spv::HasResultAndType
47#define SPV_ENABLE_UTILITY_CODE
alan-bakere0902602020-03-23 08:43:40 -040048#include "spirv/unified1/spirv.hpp"
David Neto118188e2018-08-24 11:27:54 -040049
David Neto85082642018-03-24 06:55:20 -070050#include "clspv/AddressSpace.h"
David Neto118188e2018-08-24 11:27:54 -040051#include "clspv/Option.h"
alan-baker86ce19c2020-08-05 13:09:19 -040052#include "clspv/PushConstant.h"
53#include "clspv/SpecConstant.h"
David Neto85082642018-03-24 06:55:20 -070054#include "clspv/spirv_c_strings.hpp"
55#include "clspv/spirv_glsl.hpp"
alan-baker86ce19c2020-08-05 13:09:19 -040056#include "clspv/spirv_reflection.hpp"
David Neto22f144c2017-06-12 14:26:21 -040057
David Neto4feb7a42017-10-06 17:29:42 -040058#include "ArgKind.h"
alan-bakerf67468c2019-11-25 15:51:49 -050059#include "Builtins.h"
alan-baker06cad652019-12-03 17:56:47 -050060#include "ComputeStructuredOrder.h"
David Neto85082642018-03-24 06:55:20 -070061#include "ConstantEmitter.h"
Alan Baker202c8c72018-08-13 13:47:44 -040062#include "Constants.h"
David Neto78383442018-06-15 20:31:56 -040063#include "DescriptorCounter.h"
alan-bakerc4579bb2020-04-29 14:15:50 -040064#include "Layout.h"
alan-baker56f7aff2019-05-22 08:06:42 -040065#include "NormalizeGlobalVariable.h"
Diego Novilloa4c44fa2019-04-11 10:56:15 -040066#include "Passes.h"
alan-bakera1be3322020-04-20 12:48:18 -040067#include "SpecConstant.h"
alan-bakerce179f12019-12-06 19:02:22 -050068#include "Types.h"
David Neto48f56a42017-10-06 16:44:25 -040069
David Neto22f144c2017-06-12 14:26:21 -040070#if defined(_MSC_VER)
71#pragma warning(pop)
72#endif
73
74using namespace llvm;
75using namespace clspv;
SJW173c7e92020-03-16 08:44:47 -050076using namespace clspv::Builtins;
SJW806a5d82020-07-15 12:51:38 -050077using namespace clspv::Option;
David Neto156783e2017-07-05 15:39:41 -040078using namespace mdconst;
David Neto22f144c2017-06-12 14:26:21 -040079
80namespace {
David Netocd8ca5f2017-10-02 23:34:11 -040081
David Neto862b7d82018-06-14 18:48:37 -040082cl::opt<bool> ShowResourceVars("show-rv", cl::init(false), cl::Hidden,
83 cl::desc("Show resource variable creation"));
84
alan-baker5ed87542020-03-23 11:05:22 -040085cl::opt<bool>
86 ShowProducerIR("show-producer-ir", cl::init(false), cl::ReallyHidden,
87 cl::desc("Dump the IR at the start of SPIRVProducer"));
88
David Neto862b7d82018-06-14 18:48:37 -040089// These hacks exist to help transition code generation algorithms
90// without making huge noise in detailed test output.
91const bool Hack_generate_runtime_array_stride_early = true;
92
David Neto3fbb4072017-10-16 11:28:14 -040093// The value of 1/pi. This value is from MSDN
94// https://msdn.microsoft.com/en-us/library/4hwaceh6.aspx
95const double kOneOverPi = 0.318309886183790671538;
96const glsl::ExtInst kGlslExtInstBad = static_cast<glsl::ExtInst>(0);
97
alan-baker86ce19c2020-08-05 13:09:19 -040098// SPIRV Module Sections (per 2.4 of the SPIR-V spec)
SJW69939d52020-04-16 07:29:07 -050099// These are used to collect SPIRVInstructions by type on-the-fly.
100enum SPIRVSection {
101 kCapabilities,
102 kExtensions,
103 kImports,
104 kMemoryModel,
105 kEntryPoints,
106 kExecutionModes,
107
108 kDebug,
109 kAnnotations,
110
111 kTypes,
112 kConstants = kTypes,
113 kGlobalVariables,
114
115 kFunctions,
116
alan-baker86ce19c2020-08-05 13:09:19 -0400117 // This is not a section of the SPIR-V spec and should always immediately
118 // precede kSectionCount. It is a convenient place for the embedded
119 // reflection data.
120 kReflection,
SJW69939d52020-04-16 07:29:07 -0500121 kSectionCount
122};
123
SJW01901d92020-05-21 08:58:31 -0500124class SPIRVID {
125 uint32_t id;
126
127public:
128 SPIRVID(uint32_t _id = 0) : id(_id) {}
129 uint32_t get() const { return id; }
130 bool isValid() const { return id != 0; }
131 bool operator==(const SPIRVID &that) const { return id == that.id; }
SJW806a5d82020-07-15 12:51:38 -0500132 bool operator<(const SPIRVID &that) const { return id < that.id; }
SJW01901d92020-05-21 08:58:31 -0500133};
SJWf93f5f32020-05-05 07:27:56 -0500134
SJW88ed5fe2020-05-11 12:40:57 -0500135enum SPIRVOperandType { NUMBERID, LITERAL_WORD, LITERAL_DWORD, LITERAL_STRING };
David Neto22f144c2017-06-12 14:26:21 -0400136
137struct SPIRVOperand {
SJW88ed5fe2020-05-11 12:40:57 -0500138 explicit SPIRVOperand(SPIRVOperandType Ty, uint32_t Num) : Type(Ty) {
139 LiteralNum[0] = Num;
140 }
David Neto22f144c2017-06-12 14:26:21 -0400141 explicit SPIRVOperand(SPIRVOperandType Ty, const char *Str)
142 : Type(Ty), LiteralStr(Str) {}
143 explicit SPIRVOperand(SPIRVOperandType Ty, StringRef Str)
144 : Type(Ty), LiteralStr(Str) {}
SJW88ed5fe2020-05-11 12:40:57 -0500145 explicit SPIRVOperand(ArrayRef<uint32_t> NumVec) {
146 auto sz = NumVec.size();
147 assert(sz >= 1 && sz <= 2);
148 Type = sz == 1 ? LITERAL_WORD : LITERAL_DWORD;
149 LiteralNum[0] = NumVec[0];
150 if (sz == 2) {
151 LiteralNum[1] = NumVec[1];
152 }
153 }
David Neto22f144c2017-06-12 14:26:21 -0400154
alan-baker7506abb2020-09-10 15:02:55 -0400155 SPIRVOperandType getType() const { return Type; }
156 uint32_t getNumID() const { return LiteralNum[0]; }
157 std::string getLiteralStr() const { return LiteralStr; }
158 const uint32_t *getLiteralNum() const { return LiteralNum; }
David Neto22f144c2017-06-12 14:26:21 -0400159
David Neto87846742018-04-11 17:36:22 -0400160 uint32_t GetNumWords() const {
161 switch (Type) {
162 case NUMBERID:
SJW88ed5fe2020-05-11 12:40:57 -0500163 case LITERAL_WORD:
David Neto87846742018-04-11 17:36:22 -0400164 return 1;
SJW88ed5fe2020-05-11 12:40:57 -0500165 case LITERAL_DWORD:
166 return 2;
David Neto87846742018-04-11 17:36:22 -0400167 case LITERAL_STRING:
168 // Account for the terminating null character.
David Netoee2660d2018-06-28 16:31:29 -0400169 return uint32_t((LiteralStr.size() + 4) / 4);
David Neto87846742018-04-11 17:36:22 -0400170 }
171 llvm_unreachable("Unhandled case in SPIRVOperand::GetNumWords()");
172 }
173
David Neto22f144c2017-06-12 14:26:21 -0400174private:
175 SPIRVOperandType Type;
176 std::string LiteralStr;
SJW88ed5fe2020-05-11 12:40:57 -0500177 uint32_t LiteralNum[2];
David Neto22f144c2017-06-12 14:26:21 -0400178};
179
SJW88ed5fe2020-05-11 12:40:57 -0500180typedef SmallVector<SPIRVOperand, 4> SPIRVOperandVec;
David Netoc6f3ab22018-04-06 18:02:31 -0400181
David Neto22f144c2017-06-12 14:26:21 -0400182struct SPIRVInstruction {
SJWf93f5f32020-05-05 07:27:56 -0500183 // Primary constructor must have Opcode, initializes WordCount based on ResID.
184 SPIRVInstruction(spv::Op Opc, SPIRVID ResID = 0)
185 : Opcode(static_cast<uint16_t>(Opc)) {
186 setResult(ResID);
David Neto87846742018-04-11 17:36:22 -0400187 }
David Neto22f144c2017-06-12 14:26:21 -0400188
SJWf93f5f32020-05-05 07:27:56 -0500189 // Creates an instruction with an opcode and no result ID, and with the given
190 // operands. This calls primary constructor to initialize Opcode, WordCount.
191 // Takes ownership of the operands and clears |Ops|.
192 SPIRVInstruction(spv::Op Opc, SPIRVOperandVec &Ops) : SPIRVInstruction(Opc) {
193 setOperands(Ops);
David Netoef5ba2b2019-12-20 08:35:54 -0500194 }
SJWf93f5f32020-05-05 07:27:56 -0500195 // Creates an instruction with an opcode and no result ID, and with the given
196 // operands. This calls primary constructor to initialize Opcode, WordCount.
197 // Takes ownership of the operands and clears |Ops|.
198 SPIRVInstruction(spv::Op Opc, SPIRVID ResID, SPIRVOperandVec &Ops)
199 : SPIRVInstruction(Opc, ResID) {
200 setOperands(Ops);
David Netoef5ba2b2019-12-20 08:35:54 -0500201 }
David Netoef5ba2b2019-12-20 08:35:54 -0500202
David Netoee2660d2018-06-28 16:31:29 -0400203 uint32_t getWordCount() const { return WordCount; }
David Neto22f144c2017-06-12 14:26:21 -0400204 uint16_t getOpcode() const { return Opcode; }
SJW88ed5fe2020-05-11 12:40:57 -0500205 SPIRVID getResultID() const { return ResultID; }
206 const SPIRVOperandVec &getOperands() const { return Operands; }
David Neto22f144c2017-06-12 14:26:21 -0400207
208private:
SJW01901d92020-05-21 08:58:31 -0500209 void setResult(SPIRVID ResID = 0) {
210 WordCount = 1 + (ResID.isValid() ? 1 : 0);
SJWf93f5f32020-05-05 07:27:56 -0500211 ResultID = ResID;
212 }
213
214 void setOperands(SPIRVOperandVec &Ops) {
215 assert(Operands.empty());
216 Operands = std::move(Ops);
217 for (auto &opd : Operands) {
SJW88ed5fe2020-05-11 12:40:57 -0500218 WordCount += uint16_t(opd.GetNumWords());
SJWf93f5f32020-05-05 07:27:56 -0500219 }
220 }
221
222private:
David Netoee2660d2018-06-28 16:31:29 -0400223 uint32_t WordCount; // Check the 16-bit bound at code generation time.
David Neto22f144c2017-06-12 14:26:21 -0400224 uint16_t Opcode;
SJW88ed5fe2020-05-11 12:40:57 -0500225 SPIRVID ResultID;
SJWf93f5f32020-05-05 07:27:56 -0500226 SPIRVOperandVec Operands;
David Neto22f144c2017-06-12 14:26:21 -0400227};
228
229struct SPIRVProducerPass final : public ModulePass {
SJW01901d92020-05-21 08:58:31 -0500230 typedef DenseMap<Type *, SPIRVID> TypeMapType;
David Neto22f144c2017-06-12 14:26:21 -0400231 typedef UniqueVector<Type *> TypeList;
SJW88ed5fe2020-05-11 12:40:57 -0500232 typedef DenseMap<Value *, SPIRVID> ValueMapType;
SJW806a5d82020-07-15 12:51:38 -0500233 typedef std::list<SPIRVID> SPIRVIDListType;
SJW01901d92020-05-21 08:58:31 -0500234 typedef std::vector<std::pair<Value *, SPIRVID>> EntryPointVecType;
235 typedef std::set<uint32_t> CapabilitySetType;
SJW88ed5fe2020-05-11 12:40:57 -0500236 typedef std::list<SPIRVInstruction> SPIRVInstructionList;
SJW806a5d82020-07-15 12:51:38 -0500237 typedef std::map<spv::BuiltIn, SPIRVID> BuiltinConstantMapType;
SJW88ed5fe2020-05-11 12:40:57 -0500238 // A vector of pairs, each of which is:
David Neto87846742018-04-11 17:36:22 -0400239 // - the LLVM instruction that we will later generate SPIR-V code for
SJW88ed5fe2020-05-11 12:40:57 -0500240 // - the SPIR-V instruction placeholder that will be replaced
241 typedef std::vector<std::pair<Value *, SPIRVInstruction *>>
David Neto22f144c2017-06-12 14:26:21 -0400242 DeferredInstVecType;
243 typedef DenseMap<FunctionType *, std::pair<FunctionType *, uint32_t>>
244 GlobalConstFuncMapType;
245
David Neto44795152017-07-13 15:45:28 -0400246 explicit SPIRVProducerPass(
alan-bakerf5e5f692018-11-27 08:33:24 -0500247 raw_pwrite_stream &out,
alan-baker00e7a582019-06-07 12:54:21 -0400248 ArrayRef<std::pair<unsigned, std::string>> samplerMap,
David Neto44795152017-07-13 15:45:28 -0400249 bool outputCInitList)
SJW01901d92020-05-21 08:58:31 -0500250 : ModulePass(ID), module(nullptr), samplerMap(samplerMap), out(out),
David Neto0676e6f2017-07-11 18:47:44 -0400251 binaryTempOut(binaryTempUnderlyingVector), binaryOut(&out),
David Neto0676e6f2017-07-11 18:47:44 -0400252 outputCInitList(outputCInitList), patchBoundOffset(0), nextID(1),
alan-baker5b86ed72019-02-15 08:26:50 -0500253 OpExtInstImportID(0), HasVariablePointersStorageBuffer(false),
254 HasVariablePointers(false), SamplerTy(nullptr), WorkgroupSizeValueID(0),
SJW01901d92020-05-21 08:58:31 -0500255 WorkgroupSizeVarID(0) {
256 addCapability(spv::CapabilityShader);
257 Ptr = this;
258 }
David Neto22f144c2017-06-12 14:26:21 -0400259
James Price11010dc2019-12-19 13:53:09 -0500260 virtual ~SPIRVProducerPass() {
James Price11010dc2019-12-19 13:53:09 -0500261 }
262
David Neto22f144c2017-06-12 14:26:21 -0400263 void getAnalysisUsage(AnalysisUsage &AU) const override {
264 AU.addRequired<DominatorTreeWrapperPass>();
265 AU.addRequired<LoopInfoWrapperPass>();
266 }
267
268 virtual bool runOnModule(Module &module) override;
269
270 // output the SPIR-V header block
271 void outputHeader();
272
273 // patch the SPIR-V header block
274 void patchHeader();
275
SJW01901d92020-05-21 08:58:31 -0500276 CapabilitySetType &getCapabilitySet() { return CapabilitySet; }
David Neto22f144c2017-06-12 14:26:21 -0400277 TypeMapType &getImageTypeMap() { return ImageTypeMap; }
alan-baker7506abb2020-09-10 15:02:55 -0400278 TypeList &getTypeList() { return Types; }
David Neto22f144c2017-06-12 14:26:21 -0400279 ValueMapType &getValueMap() { return ValueMap; }
SJW69939d52020-04-16 07:29:07 -0500280 SPIRVInstructionList &getSPIRVInstList(SPIRVSection Section) {
281 return SPIRVSections[Section];
282 };
alan-baker7506abb2020-09-10 15:02:55 -0400283 EntryPointVecType &getEntryPointVec() { return EntryPointVec; }
284 DeferredInstVecType &getDeferredInstVec() { return DeferredInstVec; }
SJW806a5d82020-07-15 12:51:38 -0500285 SPIRVIDListType &getEntryPointInterfacesList() {
286 return EntryPointInterfacesList;
alan-baker7506abb2020-09-10 15:02:55 -0400287 }
SJW01901d92020-05-21 08:58:31 -0500288 SPIRVID getOpExtInstImportID();
alan-baker7506abb2020-09-10 15:02:55 -0400289 std::vector<SPIRVID> &getBuiltinDimVec() { return BuiltinDimensionVec; }
SJW2c317da2020-03-23 07:39:13 -0500290
alan-baker5b86ed72019-02-15 08:26:50 -0500291 bool hasVariablePointersStorageBuffer() {
292 return HasVariablePointersStorageBuffer;
293 }
SJW01901d92020-05-21 08:58:31 -0500294 void setVariablePointersStorageBuffer() {
295 if (!HasVariablePointersStorageBuffer) {
296 addCapability(spv::CapabilityVariablePointersStorageBuffer);
297 HasVariablePointersStorageBuffer = true;
298 }
alan-baker5b86ed72019-02-15 08:26:50 -0500299 }
alan-baker7506abb2020-09-10 15:02:55 -0400300 bool hasVariablePointers() { return HasVariablePointers; }
SJW01901d92020-05-21 08:58:31 -0500301 void setVariablePointers() {
302 if (!HasVariablePointers) {
303 addCapability(spv::CapabilityVariablePointers);
304 HasVariablePointers = true;
305 }
alan-baker7506abb2020-09-10 15:02:55 -0400306 }
alan-bakerb6b09dc2018-11-08 16:59:28 -0500307 ArrayRef<std::pair<unsigned, std::string>> &getSamplerMap() {
308 return samplerMap;
309 }
David Neto22f144c2017-06-12 14:26:21 -0400310 GlobalConstFuncMapType &getGlobalConstFuncTypeMap() {
311 return GlobalConstFuncTypeMap;
312 }
313 SmallPtrSet<Value *, 16> &getGlobalConstArgSet() {
314 return GlobalConstArgumentSet;
315 }
alan-bakerb6b09dc2018-11-08 16:59:28 -0500316 TypeList &getTypesNeedingArrayStride() { return TypesNeedingArrayStride; }
David Neto22f144c2017-06-12 14:26:21 -0400317
SJW77b87ad2020-04-21 14:37:52 -0500318 void GenerateLLVMIRInfo();
alan-bakerb6b09dc2018-11-08 16:59:28 -0500319 // Populate GlobalConstFuncTypeMap. Also, if module-scope __constant will
320 // *not* be converted to a storage buffer, replace each such global variable
321 // with one in the storage class expecgted by SPIR-V.
SJW77b87ad2020-04-21 14:37:52 -0500322 void FindGlobalConstVars();
David Neto862b7d82018-06-14 18:48:37 -0400323 // Populate ResourceVarInfoList, FunctionToResourceVarsMap, and
324 // ModuleOrderedResourceVars.
SJW77b87ad2020-04-21 14:37:52 -0500325 void FindResourceVars();
David Neto22f144c2017-06-12 14:26:21 -0400326 void FindTypePerGlobalVar(GlobalVariable &GV);
327 void FindTypePerFunc(Function &F);
SJW77b87ad2020-04-21 14:37:52 -0500328 void FindTypesForSamplerMap();
329 void FindTypesForResourceVars();
alan-bakerb6b09dc2018-11-08 16:59:28 -0500330 // Inserts |Ty| and relevant sub-types into the |Types| member, indicating
331 // that |Ty| and its subtypes will need a corresponding SPIR-V type.
David Neto22f144c2017-06-12 14:26:21 -0400332 void FindType(Type *Ty);
SJWf93f5f32020-05-05 07:27:56 -0500333
334 // Lookup or create Types, Constants.
335 // Returns SPIRVID once it has been created.
336 SPIRVID getSPIRVType(Type *Ty);
337 SPIRVID getSPIRVConstant(Constant *Cst);
SJW806a5d82020-07-15 12:51:38 -0500338 SPIRVID getSPIRVInt32Constant(uint32_t CstVal);
SJWf93f5f32020-05-05 07:27:56 -0500339 // Lookup SPIRVID of llvm::Value, may create Constant.
340 SPIRVID getSPIRVValue(Value *V);
341
SJW806a5d82020-07-15 12:51:38 -0500342 SPIRVID getSPIRVBuiltin(spv::BuiltIn BID, spv::Capability Cap);
343
David Neto19a1bad2017-08-25 15:01:41 -0400344 // Generates instructions for SPIR-V types corresponding to the LLVM types
345 // saved in the |Types| member. A type follows its subtypes. IDs are
346 // allocated sequentially starting with the current value of nextID, and
347 // with a type following its subtypes. Also updates nextID to just beyond
348 // the last generated ID.
SJW77b87ad2020-04-21 14:37:52 -0500349 void GenerateSPIRVTypes();
SJW77b87ad2020-04-21 14:37:52 -0500350 void GenerateModuleInfo();
David Neto22f144c2017-06-12 14:26:21 -0400351 void GenerateGlobalVar(GlobalVariable &GV);
SJW77b87ad2020-04-21 14:37:52 -0500352 void GenerateWorkgroupVars();
alan-baker86ce19c2020-08-05 13:09:19 -0400353 // Generate reflection instructions for resource variables associated with
David Neto862b7d82018-06-14 18:48:37 -0400354 // arguments to F.
SJW77b87ad2020-04-21 14:37:52 -0500355 void GenerateSamplers();
David Neto862b7d82018-06-14 18:48:37 -0400356 // Generate OpVariables for %clspv.resource.var.* calls.
SJW77b87ad2020-04-21 14:37:52 -0500357 void GenerateResourceVars();
David Neto22f144c2017-06-12 14:26:21 -0400358 void GenerateFuncPrologue(Function &F);
359 void GenerateFuncBody(Function &F);
David Netob6e2e062018-04-25 10:32:06 -0400360 void GenerateEntryPointInitialStores();
David Neto22f144c2017-06-12 14:26:21 -0400361 spv::Op GetSPIRVCmpOpcode(CmpInst *CmpI);
362 spv::Op GetSPIRVCastOpcode(Instruction &I);
363 spv::Op GetSPIRVBinaryOpcode(Instruction &I);
SJW806a5d82020-07-15 12:51:38 -0500364 SPIRVID GenerateClspvInstruction(CallInst *Call,
365 const FunctionInfo &FuncInfo);
366 SPIRVID GenerateImageInstruction(CallInst *Call,
367 const FunctionInfo &FuncInfo);
368 SPIRVID GenerateSubgroupInstruction(CallInst *Call,
369 const FunctionInfo &FuncInfo);
370 SPIRVID GenerateInstructionFromCall(CallInst *Call);
David Neto22f144c2017-06-12 14:26:21 -0400371 void GenerateInstruction(Instruction &I);
372 void GenerateFuncEpilogue();
373 void HandleDeferredInstruction();
SJW77b87ad2020-04-21 14:37:52 -0500374 void HandleDeferredDecorations();
David Neto22f144c2017-06-12 14:26:21 -0400375 bool is4xi8vec(Type *Ty) const;
376 spv::StorageClass GetStorageClass(unsigned AddrSpace) const;
David Neto862b7d82018-06-14 18:48:37 -0400377 spv::StorageClass GetStorageClassForArgKind(clspv::ArgKind arg_kind) const;
David Neto22f144c2017-06-12 14:26:21 -0400378 spv::BuiltIn GetBuiltin(StringRef globalVarName) const;
David Neto3fbb4072017-10-16 11:28:14 -0400379 // Returns the GLSL extended instruction enum that the given function
380 // call maps to. If none, then returns the 0 value, i.e. GLSLstd4580Bad.
SJW61531372020-06-09 07:31:08 -0500381 glsl::ExtInst getExtInstEnum(const Builtins::FunctionInfo &func_info);
David Neto3fbb4072017-10-16 11:28:14 -0400382 // Returns the GLSL extended instruction enum indirectly used by the given
383 // function. That is, to implement the given function, we use an extended
384 // instruction plus one more instruction. If none, then returns the 0 value,
385 // i.e. GLSLstd4580Bad.
SJW61531372020-06-09 07:31:08 -0500386 glsl::ExtInst getIndirectExtInstEnum(const Builtins::FunctionInfo &func_info);
David Neto3fbb4072017-10-16 11:28:14 -0400387 // Returns the single GLSL extended instruction used directly or
388 // indirectly by the given function call.
SJW61531372020-06-09 07:31:08 -0500389 glsl::ExtInst
390 getDirectOrIndirectExtInstEnum(const Builtins::FunctionInfo &func_info);
David Neto22f144c2017-06-12 14:26:21 -0400391 void WriteOneWord(uint32_t Word);
SJW88ed5fe2020-05-11 12:40:57 -0500392 void WriteResultID(const SPIRVInstruction &Inst);
393 void WriteWordCountAndOpcode(const SPIRVInstruction &Inst);
394 void WriteOperand(const SPIRVOperand &Op);
David Neto22f144c2017-06-12 14:26:21 -0400395 void WriteSPIRVBinary();
SJW69939d52020-04-16 07:29:07 -0500396 void WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList);
David Neto22f144c2017-06-12 14:26:21 -0400397
Alan Baker9bf93fb2018-08-28 16:59:26 -0400398 // Returns true if |type| is compatible with OpConstantNull.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500399 bool IsTypeNullable(const Type *type) const;
Alan Baker9bf93fb2018-08-28 16:59:26 -0400400
Alan Bakerfcda9482018-10-02 17:09:59 -0400401 // Populate UBO remapped type maps.
SJW77b87ad2020-04-21 14:37:52 -0500402 void PopulateUBOTypeMaps();
Alan Bakerfcda9482018-10-02 17:09:59 -0400403
alan-baker06cad652019-12-03 17:56:47 -0500404 // Populate the merge and continue block maps.
SJW77b87ad2020-04-21 14:37:52 -0500405 void PopulateStructuredCFGMaps();
alan-baker06cad652019-12-03 17:56:47 -0500406
Alan Bakerfcda9482018-10-02 17:09:59 -0400407 // Wrapped methods of DataLayout accessors. If |type| was remapped for UBOs,
408 // uses the internal map, otherwise it falls back on the data layout.
409 uint64_t GetTypeSizeInBits(Type *type, const DataLayout &DL);
410 uint64_t GetTypeStoreSize(Type *type, const DataLayout &DL);
411 uint64_t GetTypeAllocSize(Type *type, const DataLayout &DL);
Kévin Petitbbbda972020-03-03 19:16:31 +0000412 uint32_t GetExplicitLayoutStructMemberOffset(StructType *type,
413 unsigned member,
414 const DataLayout &DL);
Alan Bakerfcda9482018-10-02 17:09:59 -0400415
alan-baker5b86ed72019-02-15 08:26:50 -0500416 // Returns the base pointer of |v|.
417 Value *GetBasePointer(Value *v);
418
SJW01901d92020-05-21 08:58:31 -0500419 // Add Capability if not already (e.g. CapabilityGroupNonUniformBroadcast)
420 void addCapability(uint32_t c) { CapabilitySet.emplace(c); }
421
alan-baker5b86ed72019-02-15 08:26:50 -0500422 // Sets |HasVariablePointersStorageBuffer| or |HasVariablePointers| base on
423 // |address_space|.
424 void setVariablePointersCapabilities(unsigned address_space);
425
426 // Returns true if |lhs| and |rhs| represent the same resource or workgroup
427 // variable.
428 bool sameResource(Value *lhs, Value *rhs) const;
429
430 // Returns true if |inst| is phi or select that selects from the same
431 // structure (or null).
432 bool selectFromSameObject(Instruction *inst);
433
alan-bakere9308012019-03-15 10:25:13 -0400434 // Returns true if |Arg| is called with a coherent resource.
435 bool CalledWithCoherentResource(Argument &Arg);
436
SJWf93f5f32020-05-05 07:27:56 -0500437 //
438 // Primary interface for adding SPIRVInstructions to a SPIRVSection.
439 template <enum SPIRVSection TSection = kFunctions>
440 SPIRVID addSPIRVInst(spv::Op Opcode, SPIRVOperandVec &Operands) {
441 bool has_result, has_result_type;
442 spv::HasResultAndType(Opcode, &has_result, &has_result_type);
443 SPIRVID RID = has_result ? incrNextID() : 0;
SJW88ed5fe2020-05-11 12:40:57 -0500444 SPIRVSections[TSection].emplace_back(Opcode, RID, Operands);
SJWf93f5f32020-05-05 07:27:56 -0500445 return RID;
446 }
447 template <enum SPIRVSection TSection = kFunctions>
448 SPIRVID addSPIRVInst(spv::Op Op) {
449 SPIRVOperandVec Ops;
450 return addSPIRVInst<TSection>(Op, Ops);
451 }
452 template <enum SPIRVSection TSection = kFunctions>
453 SPIRVID addSPIRVInst(spv::Op Op, uint32_t V) {
454 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -0500455 Ops.emplace_back(LITERAL_WORD, V);
SJWf93f5f32020-05-05 07:27:56 -0500456 return addSPIRVInst<TSection>(Op, Ops);
457 }
458 template <enum SPIRVSection TSection = kFunctions>
459 SPIRVID addSPIRVInst(spv::Op Op, const char *V) {
460 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -0500461 Ops.emplace_back(LITERAL_STRING, V);
SJWf93f5f32020-05-05 07:27:56 -0500462 return addSPIRVInst<TSection>(Op, Ops);
463 }
464
SJW88ed5fe2020-05-11 12:40:57 -0500465 //
466 // Add placeholder for llvm::Value that references future values.
467 // Must have result ID just in case final SPIRVInstruction requires.
468 SPIRVID addSPIRVPlaceholder(Value *I) {
469 SPIRVID RID = incrNextID();
470 SPIRVOperandVec Ops;
471 SPIRVSections[kFunctions].emplace_back(spv::OpExtInst, RID, Ops);
472 DeferredInstVec.push_back({I, &SPIRVSections[kFunctions].back()});
473 return RID;
474 }
475 // Replace placeholder with actual SPIRVInstruction on the final pass
476 // (HandleDeferredInstruction).
477 SPIRVID replaceSPIRVInst(SPIRVInstruction *I, spv::Op Opcode,
478 SPIRVOperandVec &Operands) {
479 bool has_result, has_result_type;
480 spv::HasResultAndType(Opcode, &has_result, &has_result_type);
481 SPIRVID RID = has_result ? I->getResultID() : 0;
482 *I = SPIRVInstruction(Opcode, RID, Operands);
483 return RID;
484 }
485
SJW806a5d82020-07-15 12:51:38 -0500486 //
487 // Add global variable and capture entry point interface
488 SPIRVID addSPIRVGlobalVariable(const SPIRVID &TypeID, spv::StorageClass SC,
489 const SPIRVID &InitID = SPIRVID());
490
alan-baker86ce19c2020-08-05 13:09:19 -0400491 SPIRVID getReflectionImport();
492 void GenerateReflection();
493 void GenerateKernelReflection();
494 void GeneratePushConstantReflection();
495 void GenerateSpecConstantReflection();
496 void AddArgumentReflection(SPIRVID kernel_decl, const std::string &name,
497 clspv::ArgKind arg_kind, uint32_t ordinal,
498 uint32_t descriptor_set, uint32_t binding,
499 uint32_t offset, uint32_t size, uint32_t spec_id,
500 uint32_t elem_size);
501
David Neto22f144c2017-06-12 14:26:21 -0400502private:
503 static char ID;
SJW77b87ad2020-04-21 14:37:52 -0500504
505 Module *module;
506
SJW01901d92020-05-21 08:58:31 -0500507 // Set of Capabilities required
508 CapabilitySetType CapabilitySet;
509
SJW806a5d82020-07-15 12:51:38 -0500510 // Map from clspv::BuiltinType to SPIRV Global Variable
511 BuiltinConstantMapType BuiltinConstantMap;
512
David Neto44795152017-07-13 15:45:28 -0400513 ArrayRef<std::pair<unsigned, std::string>> samplerMap;
David Neto22f144c2017-06-12 14:26:21 -0400514 raw_pwrite_stream &out;
David Neto0676e6f2017-07-11 18:47:44 -0400515
516 // TODO(dneto): Wouldn't it be better to always just emit a binary, and then
517 // convert to other formats on demand?
518
519 // When emitting a C initialization list, the WriteSPIRVBinary method
520 // will actually write its words to this vector via binaryTempOut.
521 SmallVector<char, 100> binaryTempUnderlyingVector;
522 raw_svector_ostream binaryTempOut;
523
524 // Binary output writes to this stream, which might be |out| or
525 // |binaryTempOut|. It's the latter when we really want to write a C
526 // initializer list.
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400527 raw_pwrite_stream *binaryOut;
David Neto0676e6f2017-07-11 18:47:44 -0400528 const bool outputCInitList; // If true, output look like {0x7023, ... , 5}
David Neto22f144c2017-06-12 14:26:21 -0400529 uint64_t patchBoundOffset;
530 uint32_t nextID;
531
SJWf93f5f32020-05-05 07:27:56 -0500532 SPIRVID incrNextID() { return nextID++; }
533
alan-bakerf67468c2019-11-25 15:51:49 -0500534 // ID for OpTypeInt 32 1.
SJW01901d92020-05-21 08:58:31 -0500535 SPIRVID int32ID;
alan-bakerf67468c2019-11-25 15:51:49 -0500536 // ID for OpTypeVector %int 4.
SJW01901d92020-05-21 08:58:31 -0500537 SPIRVID v4int32ID;
alan-bakerf67468c2019-11-25 15:51:49 -0500538
David Neto19a1bad2017-08-25 15:01:41 -0400539 // Maps an LLVM Value pointer to the corresponding SPIR-V Id.
David Neto22f144c2017-06-12 14:26:21 -0400540 TypeMapType TypeMap;
David Neto19a1bad2017-08-25 15:01:41 -0400541 // Maps an LLVM image type to its SPIR-V ID.
David Neto22f144c2017-06-12 14:26:21 -0400542 TypeMapType ImageTypeMap;
David Neto19a1bad2017-08-25 15:01:41 -0400543 // A unique-vector of LLVM types that map to a SPIR-V type.
David Neto22f144c2017-06-12 14:26:21 -0400544 TypeList Types;
David Neto19a1bad2017-08-25 15:01:41 -0400545 // Maps an LLVM Value pointer to the corresponding SPIR-V Id.
David Neto22f144c2017-06-12 14:26:21 -0400546 ValueMapType ValueMap;
SJW69939d52020-04-16 07:29:07 -0500547 SPIRVInstructionList SPIRVSections[kSectionCount];
David Neto862b7d82018-06-14 18:48:37 -0400548
David Neto22f144c2017-06-12 14:26:21 -0400549 EntryPointVecType EntryPointVec;
550 DeferredInstVecType DeferredInstVec;
SJW806a5d82020-07-15 12:51:38 -0500551 SPIRVIDListType EntryPointInterfacesList;
SJW01901d92020-05-21 08:58:31 -0500552 SPIRVID OpExtInstImportID;
553 std::vector<SPIRVID> BuiltinDimensionVec;
alan-baker5b86ed72019-02-15 08:26:50 -0500554 bool HasVariablePointersStorageBuffer;
David Neto22f144c2017-06-12 14:26:21 -0400555 bool HasVariablePointers;
556 Type *SamplerTy;
SJW01901d92020-05-21 08:58:31 -0500557 DenseMap<unsigned, SPIRVID> SamplerLiteralToIDMap;
David Netoc77d9e22018-03-24 06:30:28 -0700558
559 // If a function F has a pointer-to-__constant parameter, then this variable
David Neto9ed8e2f2018-03-24 06:47:24 -0700560 // will map F's type to (G, index of the parameter), where in a first phase
561 // G is F's type. During FindTypePerFunc, G will be changed to F's type
562 // but replacing the pointer-to-constant parameter with
563 // pointer-to-ModuleScopePrivate.
David Netoc77d9e22018-03-24 06:30:28 -0700564 // TODO(dneto): This doesn't seem general enough? A function might have
565 // more than one such parameter.
David Neto22f144c2017-06-12 14:26:21 -0400566 GlobalConstFuncMapType GlobalConstFuncTypeMap;
567 SmallPtrSet<Value *, 16> GlobalConstArgumentSet;
David Neto1a1a0582017-07-07 12:01:44 -0400568 // An ordered set of pointer types of Base arguments to OpPtrAccessChain,
David Neto85082642018-03-24 06:55:20 -0700569 // or array types, and which point into transparent memory (StorageBuffer
570 // storage class). These will require an ArrayStride decoration.
David Neto1a1a0582017-07-07 12:01:44 -0400571 // See SPV_KHR_variable_pointers rev 13.
David Neto85082642018-03-24 06:55:20 -0700572 TypeList TypesNeedingArrayStride;
David Netoa60b00b2017-09-15 16:34:09 -0400573
574 // This is truly ugly, but works around what look like driver bugs.
575 // For get_local_size, an earlier part of the flow has created a module-scope
576 // variable in Private address space to hold the value for the workgroup
577 // size. Its intializer is a uint3 value marked as builtin WorkgroupSize.
578 // When this is present, save the IDs of the initializer value and variable
579 // in these two variables. We only ever do a vector load from it, and
580 // when we see one of those, substitute just the value of the intializer.
581 // This mimics what Glslang does, and that's what drivers are used to.
David Neto66cfe642018-03-24 06:13:56 -0700582 // TODO(dneto): Remove this once drivers are fixed.
SJW01901d92020-05-21 08:58:31 -0500583 SPIRVID WorkgroupSizeValueID;
584 SPIRVID WorkgroupSizeVarID;
David Neto26aaf622017-10-23 18:11:53 -0400585
David Neto862b7d82018-06-14 18:48:37 -0400586 // Bookkeeping for mapping kernel arguments to resource variables.
587 struct ResourceVarInfo {
588 ResourceVarInfo(int index_arg, unsigned set_arg, unsigned binding_arg,
alan-bakere9308012019-03-15 10:25:13 -0400589 Function *fn, clspv::ArgKind arg_kind_arg, int coherent_arg)
David Neto862b7d82018-06-14 18:48:37 -0400590 : index(index_arg), descriptor_set(set_arg), binding(binding_arg),
alan-bakere9308012019-03-15 10:25:13 -0400591 var_fn(fn), arg_kind(arg_kind_arg), coherent(coherent_arg),
David Neto862b7d82018-06-14 18:48:37 -0400592 addr_space(fn->getReturnType()->getPointerAddressSpace()) {}
593 const int index; // Index into ResourceVarInfoList
594 const unsigned descriptor_set;
595 const unsigned binding;
596 Function *const var_fn; // The @clspv.resource.var.* function.
597 const clspv::ArgKind arg_kind;
alan-bakere9308012019-03-15 10:25:13 -0400598 const int coherent;
David Neto862b7d82018-06-14 18:48:37 -0400599 const unsigned addr_space; // The LLVM address space
600 // The SPIR-V ID of the OpVariable. Not populated at construction time.
SJW01901d92020-05-21 08:58:31 -0500601 SPIRVID var_id;
David Neto862b7d82018-06-14 18:48:37 -0400602 };
603 // A list of resource var info. Each one correponds to a module-scope
604 // resource variable we will have to create. Resource var indices are
605 // indices into this vector.
606 SmallVector<std::unique_ptr<ResourceVarInfo>, 8> ResourceVarInfoList;
607 // This is a vector of pointers of all the resource vars, but ordered by
608 // kernel function, and then by argument.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500609 UniqueVector<ResourceVarInfo *> ModuleOrderedResourceVars;
David Neto862b7d82018-06-14 18:48:37 -0400610 // Map a function to the ordered list of resource variables it uses, one for
611 // each argument. If an argument does not use a resource variable, it
612 // will have a null pointer entry.
613 using FunctionToResourceVarsMapType =
614 DenseMap<Function *, SmallVector<ResourceVarInfo *, 8>>;
615 FunctionToResourceVarsMapType FunctionToResourceVarsMap;
616
617 // What LLVM types map to SPIR-V types needing layout? These are the
618 // arrays and structures supporting storage buffers and uniform buffers.
619 TypeList TypesNeedingLayout;
620 // What LLVM struct types map to a SPIR-V struct type with Block decoration?
621 UniqueVector<StructType *> StructTypesNeedingBlock;
622 // For a call that represents a load from an opaque type (samplers, images),
623 // map it to the variable id it should load from.
SJW01901d92020-05-21 08:58:31 -0500624 DenseMap<CallInst *, SPIRVID> ResourceVarDeferredLoadCalls;
David Neto85082642018-03-24 06:55:20 -0700625
David Netoc6f3ab22018-04-06 18:02:31 -0400626 // An ordered list of the kernel arguments of type pointer-to-local.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500627 using LocalArgList = SmallVector<Argument *, 8>;
David Netoc6f3ab22018-04-06 18:02:31 -0400628 LocalArgList LocalArgs;
629 // Information about a pointer-to-local argument.
630 struct LocalArgInfo {
631 // The SPIR-V ID of the array variable.
SJW01901d92020-05-21 08:58:31 -0500632 SPIRVID variable_id;
David Netoc6f3ab22018-04-06 18:02:31 -0400633 // The element type of the
alan-bakerb6b09dc2018-11-08 16:59:28 -0500634 Type *elem_type;
David Netoc6f3ab22018-04-06 18:02:31 -0400635 // The ID of the array type.
SJW01901d92020-05-21 08:58:31 -0500636 SPIRVID array_size_id;
David Netoc6f3ab22018-04-06 18:02:31 -0400637 // The ID of the array type.
SJW01901d92020-05-21 08:58:31 -0500638 SPIRVID array_type_id;
David Netoc6f3ab22018-04-06 18:02:31 -0400639 // The ID of the pointer to the array type.
SJW01901d92020-05-21 08:58:31 -0500640 SPIRVID ptr_array_type_id;
David Netoc6f3ab22018-04-06 18:02:31 -0400641 // The specialization constant ID of the array size.
642 int spec_id;
643 };
Alan Baker202c8c72018-08-13 13:47:44 -0400644 // A mapping from Argument to its assigned SpecId.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500645 DenseMap<const Argument *, int> LocalArgSpecIds;
Alan Baker202c8c72018-08-13 13:47:44 -0400646 // A mapping from SpecId to its LocalArgInfo.
647 DenseMap<int, LocalArgInfo> LocalSpecIdInfoMap;
Alan Bakerfcda9482018-10-02 17:09:59 -0400648 // A mapping from a remapped type to its real offsets.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500649 DenseMap<Type *, std::vector<uint32_t>> RemappedUBOTypeOffsets;
Alan Bakerfcda9482018-10-02 17:09:59 -0400650 // A mapping from a remapped type to its real sizes.
alan-bakerb6b09dc2018-11-08 16:59:28 -0500651 DenseMap<Type *, std::tuple<uint64_t, uint64_t, uint64_t>>
652 RemappedUBOTypeSizes;
alan-baker06cad652019-12-03 17:56:47 -0500653
654 // Maps basic block to its merge block.
655 DenseMap<BasicBlock *, BasicBlock *> MergeBlocks;
656 // Maps basic block to its continue block.
657 DenseMap<BasicBlock *, BasicBlock *> ContinueBlocks;
SJW01901d92020-05-21 08:58:31 -0500658
alan-baker86ce19c2020-08-05 13:09:19 -0400659 SPIRVID ReflectionID;
660 DenseMap<Function *, SPIRVID> KernelDeclarations;
661
SJW01901d92020-05-21 08:58:31 -0500662public:
663 static SPIRVProducerPass *Ptr;
David Neto22f144c2017-06-12 14:26:21 -0400664};
665
666char SPIRVProducerPass::ID;
SJW01901d92020-05-21 08:58:31 -0500667SPIRVProducerPass *SPIRVProducerPass::Ptr = nullptr;
David Netoc6f3ab22018-04-06 18:02:31 -0400668
alan-bakerb6b09dc2018-11-08 16:59:28 -0500669} // namespace
David Neto22f144c2017-06-12 14:26:21 -0400670
671namespace clspv {
alan-baker86ce19c2020-08-05 13:09:19 -0400672ModulePass *
673createSPIRVProducerPass(raw_pwrite_stream &out,
674 ArrayRef<std::pair<unsigned, std::string>> samplerMap,
675 bool outputCInitList) {
676 return new SPIRVProducerPass(out, samplerMap, outputCInitList);
David Neto22f144c2017-06-12 14:26:21 -0400677}
David Netoc2c368d2017-06-30 16:50:17 -0400678} // namespace clspv
David Neto22f144c2017-06-12 14:26:21 -0400679
SJW01901d92020-05-21 08:58:31 -0500680namespace {
681SPIRVOperandVec &operator<<(SPIRVOperandVec &list, uint32_t num) {
682 list.emplace_back(LITERAL_WORD, num);
683 return list;
684}
685
686SPIRVOperandVec &operator<<(SPIRVOperandVec &list, int32_t num) {
687 list.emplace_back(LITERAL_WORD, static_cast<uint32_t>(num));
688 return list;
689}
690
691SPIRVOperandVec &operator<<(SPIRVOperandVec &list, ArrayRef<uint32_t> num_vec) {
692 list.emplace_back(num_vec);
693 return list;
694}
695
696SPIRVOperandVec &operator<<(SPIRVOperandVec &list, StringRef str) {
697 list.emplace_back(LITERAL_STRING, str);
698 return list;
699}
700
701SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Type *t) {
702 list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVType(t).get());
703 return list;
704}
705
706SPIRVOperandVec &operator<<(SPIRVOperandVec &list, Value *v) {
707 list.emplace_back(NUMBERID, SPIRVProducerPass::Ptr->getSPIRVValue(v).get());
708 return list;
709}
710
SJW806a5d82020-07-15 12:51:38 -0500711SPIRVOperandVec &operator<<(SPIRVOperandVec &list, const SPIRVID &v) {
SJW01901d92020-05-21 08:58:31 -0500712 list.emplace_back(NUMBERID, v.get());
713 return list;
714}
715} // namespace
716
SJW77b87ad2020-04-21 14:37:52 -0500717bool SPIRVProducerPass::runOnModule(Module &M) {
SJW01901d92020-05-21 08:58:31 -0500718 // TODO(sjw): Need to reset all data members for each Module, or better
719 // yet create a new SPIRVProducer for every module.. For now only
720 // allow 1 call.
721 assert(module == nullptr);
SJW77b87ad2020-04-21 14:37:52 -0500722 module = &M;
alan-baker5ed87542020-03-23 11:05:22 -0400723 if (ShowProducerIR) {
SJW77b87ad2020-04-21 14:37:52 -0500724 llvm::outs() << *module << "\n";
alan-baker5ed87542020-03-23 11:05:22 -0400725 }
David Neto0676e6f2017-07-11 18:47:44 -0400726 binaryOut = outputCInitList ? &binaryTempOut : &out;
727
SJW77b87ad2020-04-21 14:37:52 -0500728 PopulateUBOTypeMaps();
729 PopulateStructuredCFGMaps();
Alan Bakerfcda9482018-10-02 17:09:59 -0400730
David Neto22f144c2017-06-12 14:26:21 -0400731 // SPIR-V always begins with its header information
732 outputHeader();
733
734 // Gather information from the LLVM IR that we require.
SJW77b87ad2020-04-21 14:37:52 -0500735 GenerateLLVMIRInfo();
David Neto22f144c2017-06-12 14:26:21 -0400736
David Neto22f144c2017-06-12 14:26:21 -0400737 // Collect information on global variables too.
SJW77b87ad2020-04-21 14:37:52 -0500738 for (GlobalVariable &GV : module->globals()) {
David Neto22f144c2017-06-12 14:26:21 -0400739 // If the GV is one of our special __spirv_* variables, remove the
740 // initializer as it was only placed there to force LLVM to not throw the
741 // value away.
Kévin Petitbbbda972020-03-03 19:16:31 +0000742 if (GV.getName().startswith("__spirv_") ||
743 GV.getAddressSpace() == clspv::AddressSpace::PushConstant) {
David Neto22f144c2017-06-12 14:26:21 -0400744 GV.setInitializer(nullptr);
745 }
746
747 // Collect types' information from global variable.
748 FindTypePerGlobalVar(GV);
David Neto22f144c2017-06-12 14:26:21 -0400749 }
750
David Neto22f144c2017-06-12 14:26:21 -0400751 // Generate SPIRV instructions for types.
SJW77b87ad2020-04-21 14:37:52 -0500752 GenerateSPIRVTypes();
David Neto22f144c2017-06-12 14:26:21 -0400753
alan-baker09cb9802019-12-10 13:16:27 -0500754 // Generate literal samplers if necessary.
SJW77b87ad2020-04-21 14:37:52 -0500755 GenerateSamplers();
David Neto22f144c2017-06-12 14:26:21 -0400756
757 // Generate SPIRV variables.
SJW77b87ad2020-04-21 14:37:52 -0500758 for (GlobalVariable &GV : module->globals()) {
David Neto22f144c2017-06-12 14:26:21 -0400759 GenerateGlobalVar(GV);
760 }
SJW77b87ad2020-04-21 14:37:52 -0500761 GenerateResourceVars();
762 GenerateWorkgroupVars();
David Neto22f144c2017-06-12 14:26:21 -0400763
764 // Generate SPIRV instructions for each function.
SJW77b87ad2020-04-21 14:37:52 -0500765 for (Function &F : *module) {
David Neto22f144c2017-06-12 14:26:21 -0400766 if (F.isDeclaration()) {
767 continue;
768 }
769
770 // Generate Function Prologue.
771 GenerateFuncPrologue(F);
772
773 // Generate SPIRV instructions for function body.
774 GenerateFuncBody(F);
775
776 // Generate Function Epilogue.
777 GenerateFuncEpilogue();
778 }
779
780 HandleDeferredInstruction();
SJW77b87ad2020-04-21 14:37:52 -0500781 HandleDeferredDecorations();
alan-bakera1be3322020-04-20 12:48:18 -0400782
David Neto22f144c2017-06-12 14:26:21 -0400783 // Generate SPIRV module information.
SJW77b87ad2020-04-21 14:37:52 -0500784 GenerateModuleInfo();
David Neto22f144c2017-06-12 14:26:21 -0400785
alan-baker86ce19c2020-08-05 13:09:19 -0400786 // Generate embedded reflection information.
787 GenerateReflection();
788
alan-baker00e7a582019-06-07 12:54:21 -0400789 WriteSPIRVBinary();
David Neto22f144c2017-06-12 14:26:21 -0400790
791 // We need to patch the SPIR-V header to set bound correctly.
792 patchHeader();
David Neto0676e6f2017-07-11 18:47:44 -0400793
794 if (outputCInitList) {
795 bool first = true;
David Neto0676e6f2017-07-11 18:47:44 -0400796 std::ostringstream os;
797
David Neto57fb0b92017-08-04 15:35:09 -0400798 auto emit_word = [&os, &first](uint32_t word) {
David Neto0676e6f2017-07-11 18:47:44 -0400799 if (!first)
David Neto57fb0b92017-08-04 15:35:09 -0400800 os << ",\n";
801 os << word;
David Neto0676e6f2017-07-11 18:47:44 -0400802 first = false;
803 };
804
805 os << "{";
David Neto57fb0b92017-08-04 15:35:09 -0400806 const std::string str(binaryTempOut.str());
807 for (unsigned i = 0; i < str.size(); i += 4) {
808 const uint32_t a = static_cast<unsigned char>(str[i]);
809 const uint32_t b = static_cast<unsigned char>(str[i + 1]);
810 const uint32_t c = static_cast<unsigned char>(str[i + 2]);
811 const uint32_t d = static_cast<unsigned char>(str[i + 3]);
812 emit_word(a | (b << 8) | (c << 16) | (d << 24));
David Neto0676e6f2017-07-11 18:47:44 -0400813 }
814 os << "}\n";
815 out << os.str();
816 }
817
David Neto22f144c2017-06-12 14:26:21 -0400818 return false;
819}
820
821void SPIRVProducerPass::outputHeader() {
alan-baker00e7a582019-06-07 12:54:21 -0400822 binaryOut->write(reinterpret_cast<const char *>(&spv::MagicNumber),
823 sizeof(spv::MagicNumber));
SJW806a5d82020-07-15 12:51:38 -0500824 uint32_t minor = 0;
825 if (SpvVersion() == SPIRVVersion::SPIRV_1_3) {
826 minor = 3;
827 }
828 uint32_t version = (1 << 16) | (minor << 8);
829 binaryOut->write(reinterpret_cast<const char *>(&version), sizeof(version));
David Neto22f144c2017-06-12 14:26:21 -0400830
alan-baker0c18ab02019-06-12 10:23:21 -0400831 // use Google's vendor ID
832 const uint32_t vendor = 21 << 16;
alan-baker00e7a582019-06-07 12:54:21 -0400833 binaryOut->write(reinterpret_cast<const char *>(&vendor), sizeof(vendor));
David Neto22f144c2017-06-12 14:26:21 -0400834
alan-baker00e7a582019-06-07 12:54:21 -0400835 // we record where we need to come back to and patch in the bound value
836 patchBoundOffset = binaryOut->tell();
David Neto22f144c2017-06-12 14:26:21 -0400837
alan-baker00e7a582019-06-07 12:54:21 -0400838 // output a bad bound for now
839 binaryOut->write(reinterpret_cast<const char *>(&nextID), sizeof(nextID));
David Neto22f144c2017-06-12 14:26:21 -0400840
alan-baker00e7a582019-06-07 12:54:21 -0400841 // output the schema (reserved for use and must be 0)
842 const uint32_t schema = 0;
843 binaryOut->write(reinterpret_cast<const char *>(&schema), sizeof(schema));
David Neto22f144c2017-06-12 14:26:21 -0400844}
845
846void SPIRVProducerPass::patchHeader() {
alan-baker00e7a582019-06-07 12:54:21 -0400847 // for a binary we just write the value of nextID over bound
848 binaryOut->pwrite(reinterpret_cast<char *>(&nextID), sizeof(nextID),
849 patchBoundOffset);
David Neto22f144c2017-06-12 14:26:21 -0400850}
851
SJW77b87ad2020-04-21 14:37:52 -0500852void SPIRVProducerPass::GenerateLLVMIRInfo() {
David Neto22f144c2017-06-12 14:26:21 -0400853 // This function generates LLVM IR for function such as global variable for
854 // argument, constant and pointer type for argument access. These information
855 // is artificial one because we need Vulkan SPIR-V output. This function is
856 // executed ahead of FindType and FindConstant.
David Neto22f144c2017-06-12 14:26:21 -0400857
SJW77b87ad2020-04-21 14:37:52 -0500858 FindGlobalConstVars();
David Neto5c22a252018-03-15 16:07:41 -0400859
SJW77b87ad2020-04-21 14:37:52 -0500860 FindResourceVars();
David Neto22f144c2017-06-12 14:26:21 -0400861
862 bool HasWorkGroupBuiltin = false;
SJW77b87ad2020-04-21 14:37:52 -0500863 for (GlobalVariable &GV : module->globals()) {
David Neto22f144c2017-06-12 14:26:21 -0400864 const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName());
865 if (spv::BuiltInWorkgroupSize == BuiltinType) {
866 HasWorkGroupBuiltin = true;
867 }
868 }
869
SJW77b87ad2020-04-21 14:37:52 -0500870 FindTypesForSamplerMap();
871 FindTypesForResourceVars();
David Neto22f144c2017-06-12 14:26:21 -0400872}
873
SJW77b87ad2020-04-21 14:37:52 -0500874void SPIRVProducerPass::FindGlobalConstVars() {
875 clspv::NormalizeGlobalVariables(*module);
876 const DataLayout &DL = module->getDataLayout();
alan-baker56f7aff2019-05-22 08:06:42 -0400877
David Neto862b7d82018-06-14 18:48:37 -0400878 SmallVector<GlobalVariable *, 8> GVList;
879 SmallVector<GlobalVariable *, 8> DeadGVList;
SJW77b87ad2020-04-21 14:37:52 -0500880 for (GlobalVariable &GV : module->globals()) {
David Neto862b7d82018-06-14 18:48:37 -0400881 if (GV.getType()->getAddressSpace() == AddressSpace::Constant) {
882 if (GV.use_empty()) {
883 DeadGVList.push_back(&GV);
884 } else {
885 GVList.push_back(&GV);
886 }
887 }
888 }
889
890 // Remove dead global __constant variables.
891 for (auto GV : DeadGVList) {
892 GV->eraseFromParent();
893 }
894 DeadGVList.clear();
895
896 if (clspv::Option::ModuleConstantsInStorageBuffer()) {
897 // For now, we only support a single storage buffer.
alan-baker7506abb2020-09-10 15:02:55 -0400898 if (!GVList.empty()) {
David Neto862b7d82018-06-14 18:48:37 -0400899 assert(GVList.size() == 1);
900 const auto *GV = GVList[0];
901 const auto constants_byte_size =
Alan Bakerfcda9482018-10-02 17:09:59 -0400902 (GetTypeSizeInBits(GV->getInitializer()->getType(), DL)) / 8;
David Neto862b7d82018-06-14 18:48:37 -0400903 const size_t kConstantMaxSize = 65536;
904 if (constants_byte_size > kConstantMaxSize) {
905 outs() << "Max __constant capacity of " << kConstantMaxSize
906 << " bytes exceeded: " << constants_byte_size << " bytes used\n";
907 llvm_unreachable("Max __constant capacity exceeded");
908 }
909 }
910 } else {
911 // Change global constant variable's address space to ModuleScopePrivate.
912 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
913 for (auto GV : GVList) {
914 // Create new gv with ModuleScopePrivate address space.
915 Type *NewGVTy = GV->getType()->getPointerElementType();
916 GlobalVariable *NewGV = new GlobalVariable(
SJW77b87ad2020-04-21 14:37:52 -0500917 *module, NewGVTy, false, GV->getLinkage(), GV->getInitializer(), "",
David Neto862b7d82018-06-14 18:48:37 -0400918 nullptr, GV->getThreadLocalMode(), AddressSpace::ModuleScopePrivate);
919 NewGV->takeName(GV);
920
921 const SmallVector<User *, 8> GVUsers(GV->user_begin(), GV->user_end());
922 SmallVector<User *, 8> CandidateUsers;
923
924 auto record_called_function_type_as_user =
925 [&GlobalConstFuncTyMap](Value *gv, CallInst *call) {
926 // Find argument index.
927 unsigned index = 0;
928 for (unsigned i = 0; i < call->getNumArgOperands(); i++) {
929 if (gv == call->getOperand(i)) {
930 // TODO(dneto): Should we break here?
931 index = i;
932 }
933 }
934
935 // Record function type with global constant.
936 GlobalConstFuncTyMap[call->getFunctionType()] =
937 std::make_pair(call->getFunctionType(), index);
938 };
939
940 for (User *GVU : GVUsers) {
941 if (CallInst *Call = dyn_cast<CallInst>(GVU)) {
942 record_called_function_type_as_user(GV, Call);
943 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(GVU)) {
944 // Check GEP users.
945 for (User *GEPU : GEP->users()) {
946 if (CallInst *GEPCall = dyn_cast<CallInst>(GEPU)) {
947 record_called_function_type_as_user(GEP, GEPCall);
948 }
949 }
950 }
951
952 CandidateUsers.push_back(GVU);
953 }
954
955 for (User *U : CandidateUsers) {
956 // Update users of gv with new gv.
alan-bakered80f572019-02-11 17:28:26 -0500957 if (!isa<Constant>(U)) {
958 // #254: Can't change operands of a constant, but this shouldn't be
959 // something that sticks around in the module.
960 U->replaceUsesOfWith(GV, NewGV);
961 }
David Neto862b7d82018-06-14 18:48:37 -0400962 }
963
964 // Delete original gv.
965 GV->eraseFromParent();
966 }
967 }
968}
969
SJW77b87ad2020-04-21 14:37:52 -0500970void SPIRVProducerPass::FindResourceVars() {
David Neto862b7d82018-06-14 18:48:37 -0400971 ResourceVarInfoList.clear();
972 FunctionToResourceVarsMap.clear();
973 ModuleOrderedResourceVars.reset();
974 // Normally, there is one resource variable per clspv.resource.var.*
975 // function, since that is unique'd by arg type and index. By design,
976 // we can share these resource variables across kernels because all
977 // kernels use the same descriptor set.
978 //
979 // But if the user requested distinct descriptor sets per kernel, then
980 // the descriptor allocator has made different (set,binding) pairs for
981 // the same (type,arg_index) pair. Since we can decorate a resource
982 // variable with only exactly one DescriptorSet and Binding, we are
983 // forced in this case to make distinct resource variables whenever
Kévin Petitbbbda972020-03-03 19:16:31 +0000984 // the same clspv.resource.var.X function is seen with disintct
David Neto862b7d82018-06-14 18:48:37 -0400985 // (set,binding) values.
986 const bool always_distinct_sets =
987 clspv::Option::DistinctKernelDescriptorSets();
SJW77b87ad2020-04-21 14:37:52 -0500988 for (Function &F : *module) {
David Neto862b7d82018-06-14 18:48:37 -0400989 // Rely on the fact the resource var functions have a stable ordering
990 // in the module.
SJW61531372020-06-09 07:31:08 -0500991 if (Builtins::Lookup(&F) == Builtins::kClspvResource) {
David Neto862b7d82018-06-14 18:48:37 -0400992 // Find all calls to this function with distinct set and binding pairs.
993 // Save them in ResourceVarInfoList.
994
995 // Determine uniqueness of the (set,binding) pairs only withing this
996 // one resource-var builtin function.
997 using SetAndBinding = std::pair<unsigned, unsigned>;
998 // Maps set and binding to the resource var info.
999 DenseMap<SetAndBinding, ResourceVarInfo *> set_and_binding_map;
1000 bool first_use = true;
1001 for (auto &U : F.uses()) {
1002 if (auto *call = dyn_cast<CallInst>(U.getUser())) {
1003 const auto set = unsigned(
1004 dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue());
1005 const auto binding = unsigned(
1006 dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue());
1007 const auto arg_kind = clspv::ArgKind(
1008 dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue());
1009 const auto arg_index = unsigned(
1010 dyn_cast<ConstantInt>(call->getArgOperand(3))->getZExtValue());
alan-bakere9308012019-03-15 10:25:13 -04001011 const auto coherent = unsigned(
1012 dyn_cast<ConstantInt>(call->getArgOperand(5))->getZExtValue());
David Neto862b7d82018-06-14 18:48:37 -04001013
1014 // Find or make the resource var info for this combination.
1015 ResourceVarInfo *rv = nullptr;
1016 if (always_distinct_sets) {
1017 // Make a new resource var any time we see a different
1018 // (set,binding) pair.
1019 SetAndBinding key{set, binding};
1020 auto where = set_and_binding_map.find(key);
1021 if (where == set_and_binding_map.end()) {
alan-baker7506abb2020-09-10 15:02:55 -04001022 rv = new ResourceVarInfo(
1023 static_cast<int>(ResourceVarInfoList.size()), set, binding,
1024 &F, arg_kind, coherent);
David Neto862b7d82018-06-14 18:48:37 -04001025 ResourceVarInfoList.emplace_back(rv);
1026 set_and_binding_map[key] = rv;
1027 } else {
1028 rv = where->second;
1029 }
1030 } else {
1031 // The default is to make exactly one resource for each
1032 // clspv.resource.var.* function.
1033 if (first_use) {
1034 first_use = false;
alan-baker7506abb2020-09-10 15:02:55 -04001035 rv = new ResourceVarInfo(
1036 static_cast<int>(ResourceVarInfoList.size()), set, binding,
1037 &F, arg_kind, coherent);
David Neto862b7d82018-06-14 18:48:37 -04001038 ResourceVarInfoList.emplace_back(rv);
1039 } else {
1040 rv = ResourceVarInfoList.back().get();
1041 }
1042 }
1043
1044 // Now populate FunctionToResourceVarsMap.
1045 auto &mapping =
1046 FunctionToResourceVarsMap[call->getParent()->getParent()];
1047 while (mapping.size() <= arg_index) {
1048 mapping.push_back(nullptr);
1049 }
1050 mapping[arg_index] = rv;
1051 }
1052 }
1053 }
1054 }
1055
1056 // Populate ModuleOrderedResourceVars.
SJW77b87ad2020-04-21 14:37:52 -05001057 for (Function &F : *module) {
David Neto862b7d82018-06-14 18:48:37 -04001058 auto where = FunctionToResourceVarsMap.find(&F);
1059 if (where != FunctionToResourceVarsMap.end()) {
1060 for (auto &rv : where->second) {
1061 if (rv != nullptr) {
1062 ModuleOrderedResourceVars.insert(rv);
1063 }
1064 }
1065 }
1066 }
1067 if (ShowResourceVars) {
1068 for (auto *info : ModuleOrderedResourceVars) {
1069 outs() << "MORV index " << info->index << " (" << info->descriptor_set
1070 << "," << info->binding << ") " << *(info->var_fn->getReturnType())
1071 << "\n";
1072 }
1073 }
1074}
1075
David Neto22f144c2017-06-12 14:26:21 -04001076void SPIRVProducerPass::FindTypePerGlobalVar(GlobalVariable &GV) {
1077 // Investigate global variable's type.
1078 FindType(GV.getType());
1079}
1080
1081void SPIRVProducerPass::FindTypePerFunc(Function &F) {
1082 // Investigate function's type.
1083 FunctionType *FTy = F.getFunctionType();
1084
1085 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
1086 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
David Neto9ed8e2f2018-03-24 06:47:24 -07001087 // Handle a regular function with global constant parameters.
David Neto22f144c2017-06-12 14:26:21 -04001088 if (GlobalConstFuncTyMap.count(FTy)) {
1089 uint32_t GVCstArgIdx = GlobalConstFuncTypeMap[FTy].second;
1090 SmallVector<Type *, 4> NewFuncParamTys;
1091 for (unsigned i = 0; i < FTy->getNumParams(); i++) {
1092 Type *ParamTy = FTy->getParamType(i);
1093 if (i == GVCstArgIdx) {
1094 Type *EleTy = ParamTy->getPointerElementType();
1095 ParamTy = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
1096 }
1097
1098 NewFuncParamTys.push_back(ParamTy);
1099 }
1100
1101 FunctionType *NewFTy =
1102 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
1103 GlobalConstFuncTyMap[FTy] = std::make_pair(NewFTy, GVCstArgIdx);
1104 FTy = NewFTy;
1105 }
1106
1107 FindType(FTy);
1108 } else {
1109 // As kernel functions do not have parameters, create new function type and
1110 // add it to type map.
1111 SmallVector<Type *, 4> NewFuncParamTys;
1112 FunctionType *NewFTy =
1113 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
1114 FindType(NewFTy);
1115 }
1116
1117 // Investigate instructions' type in function body.
1118 for (BasicBlock &BB : F) {
1119 for (Instruction &I : BB) {
1120 if (isa<ShuffleVectorInst>(I)) {
1121 for (unsigned i = 0; i < I.getNumOperands(); i++) {
1122 // Ignore type for mask of shuffle vector instruction.
1123 if (i == 2) {
1124 continue;
1125 }
1126
1127 Value *Op = I.getOperand(i);
1128 if (!isa<MetadataAsValue>(Op)) {
1129 FindType(Op->getType());
1130 }
1131 }
1132
1133 FindType(I.getType());
1134 continue;
1135 }
1136
David Neto862b7d82018-06-14 18:48:37 -04001137 CallInst *Call = dyn_cast<CallInst>(&I);
1138
SJW61531372020-06-09 07:31:08 -05001139 if (Call) {
1140 auto &func_info = Builtins::Lookup(Call->getCalledFunction());
1141 if (func_info.getType() == Builtins::kClspvResource ||
1142 func_info.getType() == Builtins::kClspvLocal) {
1143 // This is a fake call representing access to a resource/workgroup
1144 // variable. We handle that elsewhere.
1145 continue;
1146 }
Alan Baker202c8c72018-08-13 13:47:44 -04001147 }
1148
alan-bakerf083bed2020-01-29 08:15:42 -05001149 // #497: InsertValue and ExtractValue map to OpCompositeInsert and
1150 // OpCompositeExtract which takes literal values for indices. As a result
1151 // don't map the type of indices.
1152 if (I.getOpcode() == Instruction::ExtractValue) {
1153 FindType(I.getOperand(0)->getType());
1154 continue;
1155 }
1156 if (I.getOpcode() == Instruction::InsertValue) {
1157 FindType(I.getOperand(0)->getType());
1158 FindType(I.getOperand(1)->getType());
1159 continue;
1160 }
1161
1162 // #497: InsertElement and ExtractElement map to OpCompositeExtract if
1163 // the index is a constant. In such a case don't map the index type.
1164 if (I.getOpcode() == Instruction::ExtractElement) {
1165 FindType(I.getOperand(0)->getType());
1166 Value *op1 = I.getOperand(1);
1167 if (!isa<Constant>(op1) || isa<GlobalValue>(op1)) {
1168 FindType(op1->getType());
1169 }
1170 continue;
1171 }
1172 if (I.getOpcode() == Instruction::InsertElement) {
1173 FindType(I.getOperand(0)->getType());
1174 FindType(I.getOperand(1)->getType());
1175 Value *op2 = I.getOperand(2);
1176 if (!isa<Constant>(op2) || isa<GlobalValue>(op2)) {
1177 FindType(op2->getType());
1178 }
1179 continue;
1180 }
1181
David Neto22f144c2017-06-12 14:26:21 -04001182 // Work through the operands of the instruction.
1183 for (unsigned i = 0; i < I.getNumOperands(); i++) {
1184 Value *const Op = I.getOperand(i);
1185 // If any of the operands is a constant, find the type!
1186 if (isa<Constant>(Op) && !isa<GlobalValue>(Op)) {
1187 FindType(Op->getType());
1188 }
1189 }
1190
1191 for (Use &Op : I.operands()) {
Radek Szymanskibe4b0c42018-10-04 22:20:53 +01001192 if (isa<CallInst>(&I)) {
David Neto22f144c2017-06-12 14:26:21 -04001193 // Avoid to check call instruction's type.
1194 break;
1195 }
Alan Baker202c8c72018-08-13 13:47:44 -04001196 if (CallInst *OpCall = dyn_cast<CallInst>(Op)) {
SJW61531372020-06-09 07:31:08 -05001197 if (Builtins::Lookup(OpCall->getCalledFunction()) ==
1198 Builtins::kClspvLocal) {
Alan Baker202c8c72018-08-13 13:47:44 -04001199 // This is a fake call representing access to a workgroup variable.
1200 // We handle that elsewhere.
1201 continue;
1202 }
1203 }
David Neto22f144c2017-06-12 14:26:21 -04001204 if (!isa<MetadataAsValue>(&Op)) {
1205 FindType(Op->getType());
1206 continue;
1207 }
1208 }
1209
David Neto22f144c2017-06-12 14:26:21 -04001210 // We don't want to track the type of this call as we are going to replace
1211 // it.
SJW61531372020-06-09 07:31:08 -05001212 if (Call && Builtins::Lookup(Call->getCalledFunction()) ==
1213 Builtins::kClspvSamplerVarLiteral) {
David Neto22f144c2017-06-12 14:26:21 -04001214 continue;
1215 }
1216
1217 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(&I)) {
1218 // If gep's base operand has ModuleScopePrivate address space, make gep
1219 // return ModuleScopePrivate address space.
1220 if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate) {
1221 // Add pointer type with private address space for global constant to
1222 // type list.
1223 Type *EleTy = I.getType()->getPointerElementType();
1224 Type *NewPTy =
1225 PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
1226
1227 FindType(NewPTy);
1228 continue;
1229 }
1230 }
1231
1232 FindType(I.getType());
1233 }
1234 }
1235}
1236
SJW77b87ad2020-04-21 14:37:52 -05001237void SPIRVProducerPass::FindTypesForSamplerMap() {
David Neto862b7d82018-06-14 18:48:37 -04001238 // If we are using a sampler map, find the type of the sampler.
SJW77b87ad2020-04-21 14:37:52 -05001239 if (module->getFunction(clspv::LiteralSamplerFunction()) ||
alan-baker7506abb2020-09-10 15:02:55 -04001240 !getSamplerMap().empty()) {
SJW77b87ad2020-04-21 14:37:52 -05001241 auto SamplerStructTy = module->getTypeByName("opencl.sampler_t");
David Neto862b7d82018-06-14 18:48:37 -04001242 if (!SamplerStructTy) {
SJW77b87ad2020-04-21 14:37:52 -05001243 SamplerStructTy =
1244 StructType::create(module->getContext(), "opencl.sampler_t");
David Neto862b7d82018-06-14 18:48:37 -04001245 }
1246
1247 SamplerTy = SamplerStructTy->getPointerTo(AddressSpace::UniformConstant);
1248
1249 FindType(SamplerTy);
1250 }
1251}
1252
SJW77b87ad2020-04-21 14:37:52 -05001253void SPIRVProducerPass::FindTypesForResourceVars() {
David Neto862b7d82018-06-14 18:48:37 -04001254 // Record types so they are generated.
1255 TypesNeedingLayout.reset();
1256 StructTypesNeedingBlock.reset();
1257
1258 // To match older clspv codegen, generate the float type first if required
1259 // for images.
1260 for (const auto *info : ModuleOrderedResourceVars) {
alan-bakerf6bc8252020-09-23 14:58:55 -04001261 if (info->arg_kind == clspv::ArgKind::SampledImage ||
1262 info->arg_kind == clspv::ArgKind::StorageImage) {
alan-bakerf67468c2019-11-25 15:51:49 -05001263 if (IsIntImageType(info->var_fn->getReturnType())) {
1264 // Nothing for now...
1265 } else if (IsUintImageType(info->var_fn->getReturnType())) {
SJW77b87ad2020-04-21 14:37:52 -05001266 FindType(Type::getInt32Ty(module->getContext()));
alan-bakerf67468c2019-11-25 15:51:49 -05001267 }
1268
1269 // We need "float" either for the sampled type or for the Lod operand.
SJW77b87ad2020-04-21 14:37:52 -05001270 FindType(Type::getFloatTy(module->getContext()));
David Neto862b7d82018-06-14 18:48:37 -04001271 }
1272 }
1273
1274 for (const auto *info : ModuleOrderedResourceVars) {
1275 Type *type = info->var_fn->getReturnType();
1276
1277 switch (info->arg_kind) {
1278 case clspv::ArgKind::Buffer:
Alan Bakerfcda9482018-10-02 17:09:59 -04001279 case clspv::ArgKind::BufferUBO:
David Neto862b7d82018-06-14 18:48:37 -04001280 if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) {
1281 StructTypesNeedingBlock.insert(sty);
1282 } else {
1283 errs() << *type << "\n";
1284 llvm_unreachable("Buffer arguments must map to structures!");
1285 }
1286 break;
1287 case clspv::ArgKind::Pod:
alan-baker9b0ec3c2020-04-06 14:45:34 -04001288 case clspv::ArgKind::PodUBO:
1289 case clspv::ArgKind::PodPushConstant:
David Neto862b7d82018-06-14 18:48:37 -04001290 if (auto *sty = dyn_cast<StructType>(type->getPointerElementType())) {
1291 StructTypesNeedingBlock.insert(sty);
1292 } else {
1293 errs() << *type << "\n";
1294 llvm_unreachable("POD arguments must map to structures!");
1295 }
1296 break;
alan-bakerf6bc8252020-09-23 14:58:55 -04001297 case clspv::ArgKind::SampledImage:
1298 case clspv::ArgKind::StorageImage:
David Neto862b7d82018-06-14 18:48:37 -04001299 case clspv::ArgKind::Sampler:
1300 // Sampler and image types map to the pointee type but
1301 // in the uniform constant address space.
1302 type = PointerType::get(type->getPointerElementType(),
1303 clspv::AddressSpace::UniformConstant);
1304 break;
1305 default:
1306 break;
1307 }
1308
1309 // The converted type is the type of the OpVariable we will generate.
1310 // If the pointee type is an array of size zero, FindType will convert it
1311 // to a runtime array.
1312 FindType(type);
1313 }
1314
alan-bakerdcd97412019-09-16 15:32:30 -04001315 // If module constants are clustered in a storage buffer then that struct
1316 // needs layout decorations.
1317 if (clspv::Option::ModuleConstantsInStorageBuffer()) {
SJW77b87ad2020-04-21 14:37:52 -05001318 for (GlobalVariable &GV : module->globals()) {
alan-bakerdcd97412019-09-16 15:32:30 -04001319 PointerType *PTy = cast<PointerType>(GV.getType());
1320 const auto AS = PTy->getAddressSpace();
1321 const bool module_scope_constant_external_init =
1322 (AS == AddressSpace::Constant) && GV.hasInitializer();
1323 const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName());
1324 if (module_scope_constant_external_init &&
1325 spv::BuiltInMax == BuiltinType) {
1326 StructTypesNeedingBlock.insert(
1327 cast<StructType>(PTy->getPointerElementType()));
1328 }
1329 }
1330 }
1331
SJW77b87ad2020-04-21 14:37:52 -05001332 for (const GlobalVariable &GV : module->globals()) {
Kévin Petitbbbda972020-03-03 19:16:31 +00001333 if (GV.getAddressSpace() == clspv::AddressSpace::PushConstant) {
1334 auto Ty = cast<PointerType>(GV.getType())->getPointerElementType();
1335 assert(Ty->isStructTy() && "Push constants have to be structures.");
1336 auto STy = cast<StructType>(Ty);
1337 StructTypesNeedingBlock.insert(STy);
1338 }
1339 }
1340
David Neto862b7d82018-06-14 18:48:37 -04001341 // Traverse the arrays and structures underneath each Block, and
1342 // mark them as needing layout.
1343 std::vector<Type *> work_list(StructTypesNeedingBlock.begin(),
1344 StructTypesNeedingBlock.end());
1345 while (!work_list.empty()) {
1346 Type *type = work_list.back();
1347 work_list.pop_back();
1348 TypesNeedingLayout.insert(type);
1349 switch (type->getTypeID()) {
1350 case Type::ArrayTyID:
1351 work_list.push_back(type->getArrayElementType());
1352 if (!Hack_generate_runtime_array_stride_early) {
1353 // Remember this array type for deferred decoration.
1354 TypesNeedingArrayStride.insert(type);
1355 }
1356 break;
1357 case Type::StructTyID:
1358 for (auto *elem_ty : cast<StructType>(type)->elements()) {
1359 work_list.push_back(elem_ty);
1360 }
1361 default:
1362 // This type and its contained types don't get layout.
1363 break;
1364 }
1365 }
1366}
1367
SJWf93f5f32020-05-05 07:27:56 -05001368void SPIRVProducerPass::GenerateWorkgroupVars() {
Alan Baker202c8c72018-08-13 13:47:44 -04001369 // The SpecId assignment for pointer-to-local arguments is recorded in
1370 // module-level metadata. Translate that information into local argument
1371 // information.
SJWf93f5f32020-05-05 07:27:56 -05001372 LLVMContext &Context = module->getContext();
SJW77b87ad2020-04-21 14:37:52 -05001373 NamedMDNode *nmd = module->getNamedMetadata(clspv::LocalSpecIdMetadataName());
alan-bakerb6b09dc2018-11-08 16:59:28 -05001374 if (!nmd)
1375 return;
Alan Baker202c8c72018-08-13 13:47:44 -04001376 for (auto operand : nmd->operands()) {
1377 MDTuple *tuple = cast<MDTuple>(operand);
1378 ValueAsMetadata *fn_md = cast<ValueAsMetadata>(tuple->getOperand(0));
1379 Function *func = cast<Function>(fn_md->getValue());
alan-bakerb6b09dc2018-11-08 16:59:28 -05001380 ConstantAsMetadata *arg_index_md =
1381 cast<ConstantAsMetadata>(tuple->getOperand(1));
1382 int arg_index = static_cast<int>(
1383 cast<ConstantInt>(arg_index_md->getValue())->getSExtValue());
1384 Argument *arg = &*(func->arg_begin() + arg_index);
Alan Baker202c8c72018-08-13 13:47:44 -04001385
1386 ConstantAsMetadata *spec_id_md =
1387 cast<ConstantAsMetadata>(tuple->getOperand(2));
alan-bakerb6b09dc2018-11-08 16:59:28 -05001388 int spec_id = static_cast<int>(
1389 cast<ConstantInt>(spec_id_md->getValue())->getSExtValue());
Alan Baker202c8c72018-08-13 13:47:44 -04001390
Alan Baker202c8c72018-08-13 13:47:44 -04001391 LocalArgSpecIds[arg] = spec_id;
alan-bakerb6b09dc2018-11-08 16:59:28 -05001392 if (LocalSpecIdInfoMap.count(spec_id))
1393 continue;
Alan Baker202c8c72018-08-13 13:47:44 -04001394
SJWf93f5f32020-05-05 07:27:56 -05001395 // Generate the spec constant.
1396 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05001397 Ops << Type::getInt32Ty(Context) << 1;
SJWf93f5f32020-05-05 07:27:56 -05001398 SPIRVID ArraySizeID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
Alan Baker202c8c72018-08-13 13:47:44 -04001399
SJWf93f5f32020-05-05 07:27:56 -05001400 // Generate the array type.
1401 Type *ElemTy = arg->getType()->getPointerElementType();
1402 Ops.clear();
1403 // The element type must have been created.
SJW01901d92020-05-21 08:58:31 -05001404 Ops << ElemTy << ArraySizeID;
SJWf93f5f32020-05-05 07:27:56 -05001405
1406 SPIRVID ArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops);
1407
1408 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05001409 Ops << spv::StorageClassWorkgroup << ArrayTypeID;
SJWf93f5f32020-05-05 07:27:56 -05001410 SPIRVID PtrArrayTypeID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops);
1411
1412 // Generate OpVariable.
1413 //
1414 // Ops[0] : Result Type ID
1415 // Ops[1] : Storage Class
SJW806a5d82020-07-15 12:51:38 -05001416 SPIRVID VariableID =
1417 addSPIRVGlobalVariable(PtrArrayTypeID, spv::StorageClassWorkgroup);
SJWf93f5f32020-05-05 07:27:56 -05001418
1419 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05001420 Ops << ArraySizeID << spv::DecorationSpecId << spec_id;
SJWf93f5f32020-05-05 07:27:56 -05001421 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
1422
1423 LocalArgInfo info{VariableID, ElemTy, ArraySizeID,
1424 ArrayTypeID, PtrArrayTypeID, spec_id};
1425 LocalSpecIdInfoMap[spec_id] = info;
Alan Baker202c8c72018-08-13 13:47:44 -04001426 }
1427}
1428
David Neto22f144c2017-06-12 14:26:21 -04001429void SPIRVProducerPass::FindType(Type *Ty) {
1430 TypeList &TyList = getTypeList();
1431
1432 if (0 != TyList.idFor(Ty)) {
1433 return;
1434 }
1435
1436 if (Ty->isPointerTy()) {
1437 auto AddrSpace = Ty->getPointerAddressSpace();
1438 if ((AddressSpace::Constant == AddrSpace) ||
1439 (AddressSpace::Global == AddrSpace)) {
1440 auto PointeeTy = Ty->getPointerElementType();
1441
1442 if (PointeeTy->isStructTy() &&
1443 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
1444 FindType(PointeeTy);
1445 auto ActualPointerTy =
1446 PointeeTy->getPointerTo(AddressSpace::UniformConstant);
1447 FindType(ActualPointerTy);
1448 return;
1449 }
1450 }
1451 }
1452
David Neto862b7d82018-06-14 18:48:37 -04001453 // By convention, LLVM array type with 0 elements will map to
1454 // OpTypeRuntimeArray. Otherwise, it will map to OpTypeArray, which
1455 // has a constant number of elements. We need to support type of the
1456 // constant.
1457 if (auto *arrayTy = dyn_cast<ArrayType>(Ty)) {
1458 if (arrayTy->getNumElements() > 0) {
1459 LLVMContext &Context = Ty->getContext();
1460 FindType(Type::getInt32Ty(Context));
1461 }
David Neto22f144c2017-06-12 14:26:21 -04001462 }
1463
1464 for (Type *SubTy : Ty->subtypes()) {
1465 FindType(SubTy);
1466 }
1467
1468 TyList.insert(Ty);
1469}
1470
David Neto22f144c2017-06-12 14:26:21 -04001471spv::StorageClass SPIRVProducerPass::GetStorageClass(unsigned AddrSpace) const {
1472 switch (AddrSpace) {
1473 default:
1474 llvm_unreachable("Unsupported OpenCL address space");
1475 case AddressSpace::Private:
1476 return spv::StorageClassFunction;
1477 case AddressSpace::Global:
David Neto22f144c2017-06-12 14:26:21 -04001478 return spv::StorageClassStorageBuffer;
Alan Bakerfcda9482018-10-02 17:09:59 -04001479 case AddressSpace::Constant:
1480 return clspv::Option::ConstantArgsInUniformBuffer()
1481 ? spv::StorageClassUniform
1482 : spv::StorageClassStorageBuffer;
David Neto22f144c2017-06-12 14:26:21 -04001483 case AddressSpace::Input:
1484 return spv::StorageClassInput;
1485 case AddressSpace::Local:
1486 return spv::StorageClassWorkgroup;
1487 case AddressSpace::UniformConstant:
1488 return spv::StorageClassUniformConstant;
David Neto9ed8e2f2018-03-24 06:47:24 -07001489 case AddressSpace::Uniform:
David Netoe439d702018-03-23 13:14:08 -07001490 return spv::StorageClassUniform;
David Neto22f144c2017-06-12 14:26:21 -04001491 case AddressSpace::ModuleScopePrivate:
1492 return spv::StorageClassPrivate;
Kévin Petitbbbda972020-03-03 19:16:31 +00001493 case AddressSpace::PushConstant:
1494 return spv::StorageClassPushConstant;
David Neto22f144c2017-06-12 14:26:21 -04001495 }
1496}
1497
David Neto862b7d82018-06-14 18:48:37 -04001498spv::StorageClass
1499SPIRVProducerPass::GetStorageClassForArgKind(clspv::ArgKind arg_kind) const {
1500 switch (arg_kind) {
1501 case clspv::ArgKind::Buffer:
1502 return spv::StorageClassStorageBuffer;
Alan Bakerfcda9482018-10-02 17:09:59 -04001503 case clspv::ArgKind::BufferUBO:
1504 return spv::StorageClassUniform;
David Neto862b7d82018-06-14 18:48:37 -04001505 case clspv::ArgKind::Pod:
alan-baker9b0ec3c2020-04-06 14:45:34 -04001506 return spv::StorageClassStorageBuffer;
1507 case clspv::ArgKind::PodUBO:
1508 return spv::StorageClassUniform;
1509 case clspv::ArgKind::PodPushConstant:
1510 return spv::StorageClassPushConstant;
David Neto862b7d82018-06-14 18:48:37 -04001511 case clspv::ArgKind::Local:
1512 return spv::StorageClassWorkgroup;
alan-bakerf6bc8252020-09-23 14:58:55 -04001513 case clspv::ArgKind::SampledImage:
1514 case clspv::ArgKind::StorageImage:
David Neto862b7d82018-06-14 18:48:37 -04001515 case clspv::ArgKind::Sampler:
1516 return spv::StorageClassUniformConstant;
Radek Szymanskibe4b0c42018-10-04 22:20:53 +01001517 default:
1518 llvm_unreachable("Unsupported storage class for argument kind");
David Neto862b7d82018-06-14 18:48:37 -04001519 }
1520}
1521
David Neto22f144c2017-06-12 14:26:21 -04001522spv::BuiltIn SPIRVProducerPass::GetBuiltin(StringRef Name) const {
1523 return StringSwitch<spv::BuiltIn>(Name)
1524 .Case("__spirv_GlobalInvocationId", spv::BuiltInGlobalInvocationId)
1525 .Case("__spirv_LocalInvocationId", spv::BuiltInLocalInvocationId)
1526 .Case("__spirv_WorkgroupSize", spv::BuiltInWorkgroupSize)
1527 .Case("__spirv_NumWorkgroups", spv::BuiltInNumWorkgroups)
1528 .Case("__spirv_WorkgroupId", spv::BuiltInWorkgroupId)
alan-bakerbed3a882020-04-21 14:42:41 -04001529 .Case("__spirv_WorkDim", spv::BuiltInWorkDim)
alan-bakere1996972020-05-04 08:38:12 -04001530 .Case("__spirv_GlobalOffset", spv::BuiltInGlobalOffset)
David Neto22f144c2017-06-12 14:26:21 -04001531 .Default(spv::BuiltInMax);
1532}
1533
SJW01901d92020-05-21 08:58:31 -05001534SPIRVID SPIRVProducerPass::getOpExtInstImportID() {
1535 if (OpExtInstImportID == 0) {
1536 //
1537 // Generate OpExtInstImport.
1538 //
1539 // Ops[0] ... Ops[n] = Name (Literal String)
David Neto22f144c2017-06-12 14:26:21 -04001540
SJW01901d92020-05-21 08:58:31 -05001541 OpExtInstImportID =
1542 addSPIRVInst<kImports>(spv::OpExtInstImport, "GLSL.std.450");
1543 }
1544 return OpExtInstImportID;
SJWf93f5f32020-05-05 07:27:56 -05001545}
1546
SJW806a5d82020-07-15 12:51:38 -05001547SPIRVID SPIRVProducerPass::addSPIRVGlobalVariable(const SPIRVID &TypeID,
1548 spv::StorageClass SC,
1549 const SPIRVID &InitID) {
1550 // Generate OpVariable.
1551 //
1552 // Ops[0] : Result Type ID
1553 // Ops[1] : Storage Class
1554 // Ops[2] : Initialization Value ID (optional)
1555
1556 SPIRVOperandVec Ops;
1557 Ops << TypeID << SC;
1558 if (InitID.isValid()) {
1559 Ops << InitID;
1560 }
1561
1562 SPIRVID VID = addSPIRVInst<kGlobalVariables>(spv::OpVariable, Ops);
1563
1564 if (SC == spv::StorageClassInput) {
1565 getEntryPointInterfacesList().push_back(VID);
1566 }
1567
1568 return VID;
1569}
1570
SJW01901d92020-05-21 08:58:31 -05001571SPIRVID SPIRVProducerPass::getSPIRVType(Type *Ty) {
SJWf93f5f32020-05-05 07:27:56 -05001572 auto TI = TypeMap.find(Ty);
1573 if (TI != TypeMap.end()) {
SJW01901d92020-05-21 08:58:31 -05001574 assert(TI->second.isValid());
SJWf93f5f32020-05-05 07:27:56 -05001575 return TI->second;
1576 }
1577
1578 const auto &DL = module->getDataLayout();
1579
SJW01901d92020-05-21 08:58:31 -05001580 SPIRVID RID;
SJWf93f5f32020-05-05 07:27:56 -05001581
1582 switch (Ty->getTypeID()) {
1583 default: {
1584 Ty->print(errs());
1585 llvm_unreachable("Unsupported type???");
1586 break;
1587 }
1588 case Type::MetadataTyID:
1589 case Type::LabelTyID: {
1590 // Ignore these types.
1591 break;
1592 }
1593 case Type::PointerTyID: {
1594 PointerType *PTy = cast<PointerType>(Ty);
1595 unsigned AddrSpace = PTy->getAddressSpace();
1596
1597 if (AddrSpace != AddressSpace::UniformConstant) {
1598 auto PointeeTy = PTy->getElementType();
1599 if (PointeeTy->isStructTy() &&
1600 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
1601 // TODO(sjw): assert always an image?
1602 RID = getSPIRVType(PointeeTy);
1603 break;
1604 }
1605 }
1606
1607 // For the purposes of our Vulkan SPIR-V type system, constant and global
1608 // are conflated.
1609 if (AddressSpace::Constant == AddrSpace) {
1610 if (!clspv::Option::ConstantArgsInUniformBuffer()) {
1611 AddrSpace = AddressSpace::Global;
1612 // Check to see if we already created this type (for instance, if we
1613 // had a constant <type>* and a global <type>*, the type would be
1614 // created by one of these types, and shared by both).
1615 auto GlobalTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
1616 if (0 < TypeMap.count(GlobalTy)) {
1617 RID = TypeMap[GlobalTy];
1618 break;
1619 }
1620 }
1621 } else if (AddressSpace::Global == AddrSpace) {
1622 if (!clspv::Option::ConstantArgsInUniformBuffer()) {
1623 AddrSpace = AddressSpace::Constant;
1624
1625 // Check to see if we already created this type (for instance, if we
1626 // had a constant <type>* and a global <type>*, the type would be
1627 // created by one of these types, and shared by both).
1628 auto ConstantTy = PTy->getPointerElementType()->getPointerTo(AddrSpace);
1629 if (0 < TypeMap.count(ConstantTy)) {
1630 RID = TypeMap[ConstantTy];
1631 break;
1632 }
1633 }
1634 }
1635
1636 //
1637 // Generate OpTypePointer.
1638 //
1639
1640 // OpTypePointer
1641 // Ops[0] = Storage Class
1642 // Ops[1] = Element Type ID
1643 SPIRVOperandVec Ops;
1644
SJW01901d92020-05-21 08:58:31 -05001645 Ops << GetStorageClass(AddrSpace) << PTy->getElementType();
SJWf93f5f32020-05-05 07:27:56 -05001646
1647 RID = addSPIRVInst<kTypes>(spv::OpTypePointer, Ops);
1648 break;
1649 }
1650 case Type::StructTyID: {
1651 StructType *STy = cast<StructType>(Ty);
1652
1653 // Handle sampler type.
1654 if (STy->isOpaque()) {
1655 if (STy->getName().equals("opencl.sampler_t")) {
1656 //
1657 // Generate OpTypeSampler
1658 //
1659 // Empty Ops.
1660
1661 RID = addSPIRVInst<kTypes>(spv::OpTypeSampler);
1662 break;
1663 } else if (STy->getName().startswith("opencl.image1d_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001664 STy->getName().startswith("opencl.image1d_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001665 STy->getName().startswith("opencl.image1d_wo_t") ||
1666 STy->getName().startswith("opencl.image1d_array_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001667 STy->getName().startswith("opencl.image1d_array_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001668 STy->getName().startswith("opencl.image1d_array_wo_t") ||
1669 STy->getName().startswith("opencl.image2d_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001670 STy->getName().startswith("opencl.image2d_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001671 STy->getName().startswith("opencl.image2d_wo_t") ||
1672 STy->getName().startswith("opencl.image2d_array_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001673 STy->getName().startswith("opencl.image2d_array_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001674 STy->getName().startswith("opencl.image2d_array_wo_t") ||
1675 STy->getName().startswith("opencl.image3d_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001676 STy->getName().startswith("opencl.image3d_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001677 STy->getName().startswith("opencl.image3d_wo_t")) {
SJW01901d92020-05-21 08:58:31 -05001678 if (STy->getName().startswith("opencl.image1d_")) {
1679 if (STy->getName().contains(".sampled"))
1680 addCapability(spv::CapabilitySampled1D);
1681 else
1682 addCapability(spv::CapabilityImage1D);
1683 }
1684
SJWf93f5f32020-05-05 07:27:56 -05001685 //
1686 // Generate OpTypeImage
1687 //
1688 // Ops[0] = Sampled Type ID
1689 // Ops[1] = Dim ID
1690 // Ops[2] = Depth (Literal Number)
1691 // Ops[3] = Arrayed (Literal Number)
1692 // Ops[4] = MS (Literal Number)
1693 // Ops[5] = Sampled (Literal Number)
1694 // Ops[6] = Image Format ID
1695 //
1696 SPIRVOperandVec Ops;
1697
SJW01901d92020-05-21 08:58:31 -05001698 SPIRVID SampledTyID;
SJWf93f5f32020-05-05 07:27:56 -05001699 if (STy->getName().contains(".float")) {
1700 SampledTyID = getSPIRVType(Type::getFloatTy(Ty->getContext()));
1701 } else if (STy->getName().contains(".uint")) {
1702 SampledTyID = getSPIRVType(Type::getInt32Ty(Ty->getContext()));
1703 } else if (STy->getName().contains(".int")) {
1704 // Generate a signed 32-bit integer if necessary.
1705 if (int32ID == 0) {
1706 SPIRVOperandVec intOps;
SJW01901d92020-05-21 08:58:31 -05001707 intOps << 32 << 1;
SJWf93f5f32020-05-05 07:27:56 -05001708 int32ID = addSPIRVInst<kTypes>(spv::OpTypeInt, intOps);
1709 }
1710 SampledTyID = int32ID;
1711
1712 // Generate a vec4 of the signed int if necessary.
1713 if (v4int32ID == 0) {
1714 SPIRVOperandVec vecOps;
SJW01901d92020-05-21 08:58:31 -05001715 vecOps << int32ID << 4;
SJWf93f5f32020-05-05 07:27:56 -05001716 v4int32ID = addSPIRVInst<kTypes>(spv::OpTypeVector, vecOps);
1717 }
1718 } else {
1719 // This was likely an UndefValue.
1720 SampledTyID = getSPIRVType(Type::getFloatTy(Ty->getContext()));
1721 }
SJW01901d92020-05-21 08:58:31 -05001722 Ops << SampledTyID;
SJWf93f5f32020-05-05 07:27:56 -05001723
1724 spv::Dim DimID = spv::Dim2D;
1725 if (STy->getName().startswith("opencl.image1d_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001726 STy->getName().startswith("opencl.image1d_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001727 STy->getName().startswith("opencl.image1d_wo_t") ||
1728 STy->getName().startswith("opencl.image1d_array_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001729 STy->getName().startswith("opencl.image1d_array_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001730 STy->getName().startswith("opencl.image1d_array_wo_t")) {
1731 DimID = spv::Dim1D;
1732 } else if (STy->getName().startswith("opencl.image3d_ro_t") ||
alan-bakerf6bc8252020-09-23 14:58:55 -04001733 STy->getName().startswith("opencl.image3d_rw_t") ||
SJWf93f5f32020-05-05 07:27:56 -05001734 STy->getName().startswith("opencl.image3d_wo_t")) {
1735 DimID = spv::Dim3D;
1736 }
SJW01901d92020-05-21 08:58:31 -05001737 Ops << DimID;
SJWf93f5f32020-05-05 07:27:56 -05001738
1739 // TODO: Set up Depth.
SJW01901d92020-05-21 08:58:31 -05001740 Ops << 0;
SJWf93f5f32020-05-05 07:27:56 -05001741
1742 uint32_t arrayed = STy->getName().contains("_array_") ? 1 : 0;
SJW01901d92020-05-21 08:58:31 -05001743 Ops << arrayed;
SJWf93f5f32020-05-05 07:27:56 -05001744
1745 // TODO: Set up MS.
SJW01901d92020-05-21 08:58:31 -05001746 Ops << 0;
SJWf93f5f32020-05-05 07:27:56 -05001747
1748 // Set up Sampled.
1749 //
1750 // From Spec
1751 //
1752 // 0 indicates this is only known at run time, not at compile time
1753 // 1 indicates will be used with sampler
1754 // 2 indicates will be used without a sampler (a storage image)
1755 uint32_t Sampled = 1;
1756 if (!STy->getName().contains(".sampled")) {
1757 Sampled = 2;
1758 }
SJW01901d92020-05-21 08:58:31 -05001759 Ops << Sampled;
SJWf93f5f32020-05-05 07:27:56 -05001760
1761 // TODO: Set up Image Format.
SJW01901d92020-05-21 08:58:31 -05001762 Ops << spv::ImageFormatUnknown;
SJWf93f5f32020-05-05 07:27:56 -05001763 RID = addSPIRVInst<kTypes>(spv::OpTypeImage, Ops);
1764
alan-bakerf6bc8252020-09-23 14:58:55 -04001765 // Only need a sampled version of the type if it is used with a sampler.
1766 if (Sampled == 1) {
1767 Ops.clear();
1768 Ops << RID;
1769 getImageTypeMap()[Ty] =
1770 addSPIRVInst<kTypes>(spv::OpTypeSampledImage, Ops);
1771 }
SJWf93f5f32020-05-05 07:27:56 -05001772 break;
1773 }
1774 }
1775
1776 //
1777 // Generate OpTypeStruct
1778 //
1779 // Ops[0] ... Ops[n] = Member IDs
1780 SPIRVOperandVec Ops;
1781
1782 for (auto *EleTy : STy->elements()) {
SJW01901d92020-05-21 08:58:31 -05001783 Ops << EleTy;
SJWf93f5f32020-05-05 07:27:56 -05001784 }
1785
1786 RID = addSPIRVInst<kTypes>(spv::OpTypeStruct, Ops);
1787
1788 // Generate OpMemberDecorate.
1789 if (TypesNeedingLayout.idFor(STy)) {
1790 for (unsigned MemberIdx = 0; MemberIdx < STy->getNumElements();
1791 MemberIdx++) {
1792 // Ops[0] = Structure Type ID
1793 // Ops[1] = Member Index(Literal Number)
1794 // Ops[2] = Decoration (Offset)
1795 // Ops[3] = Byte Offset (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05001796 const auto ByteOffset =
1797 GetExplicitLayoutStructMemberOffset(STy, MemberIdx, DL);
1798
SJW01901d92020-05-21 08:58:31 -05001799 Ops.clear();
1800 Ops << RID << MemberIdx << spv::DecorationOffset << ByteOffset;
SJWf93f5f32020-05-05 07:27:56 -05001801
1802 addSPIRVInst<kAnnotations>(spv::OpMemberDecorate, Ops);
1803 }
1804 }
1805
1806 // Generate OpDecorate.
1807 if (StructTypesNeedingBlock.idFor(STy)) {
1808 Ops.clear();
1809 // Use Block decorations with StorageBuffer storage class.
SJW01901d92020-05-21 08:58:31 -05001810 Ops << RID << spv::DecorationBlock;
SJWf93f5f32020-05-05 07:27:56 -05001811
1812 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
1813 }
1814 break;
1815 }
1816 case Type::IntegerTyID: {
alan-bakere2a62752020-07-09 22:53:23 -04001817 uint32_t bit_width = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits());
SJWf93f5f32020-05-05 07:27:56 -05001818
alan-bakere2a62752020-07-09 22:53:23 -04001819 if (clspv::Option::Int8Support() && bit_width == 8) {
SJW01901d92020-05-21 08:58:31 -05001820 addCapability(spv::CapabilityInt8);
alan-bakere2a62752020-07-09 22:53:23 -04001821 } else if (bit_width == 16) {
SJW01901d92020-05-21 08:58:31 -05001822 addCapability(spv::CapabilityInt16);
alan-bakere2a62752020-07-09 22:53:23 -04001823 } else if (bit_width == 64) {
SJW01901d92020-05-21 08:58:31 -05001824 addCapability(spv::CapabilityInt64);
1825 }
1826
alan-bakere2a62752020-07-09 22:53:23 -04001827 if (bit_width == 1) {
SJWf93f5f32020-05-05 07:27:56 -05001828 RID = addSPIRVInst<kTypes>(spv::OpTypeBool);
1829 } else {
alan-bakere2a62752020-07-09 22:53:23 -04001830 if (!clspv::Option::Int8Support() && bit_width == 8) {
SJWf93f5f32020-05-05 07:27:56 -05001831 // i8 is added to TypeMap as i32.
1832 RID = getSPIRVType(Type::getIntNTy(Ty->getContext(), 32));
1833 } else {
1834 SPIRVOperandVec Ops;
alan-bakere2a62752020-07-09 22:53:23 -04001835 Ops << bit_width << 0 /* not signed */;
SJWf93f5f32020-05-05 07:27:56 -05001836 RID = addSPIRVInst<kTypes>(spv::OpTypeInt, Ops);
1837 }
1838 }
1839 break;
1840 }
1841 case Type::HalfTyID:
1842 case Type::FloatTyID:
1843 case Type::DoubleTyID: {
alan-bakere2a62752020-07-09 22:53:23 -04001844 uint32_t bit_width = static_cast<uint32_t>(Ty->getPrimitiveSizeInBits());
1845 if (bit_width == 16) {
SJW01901d92020-05-21 08:58:31 -05001846 addCapability(spv::CapabilityFloat16);
alan-bakere2a62752020-07-09 22:53:23 -04001847 } else if (bit_width == 64) {
SJW01901d92020-05-21 08:58:31 -05001848 addCapability(spv::CapabilityFloat64);
1849 }
1850
SJWf93f5f32020-05-05 07:27:56 -05001851 SPIRVOperandVec Ops;
alan-bakere2a62752020-07-09 22:53:23 -04001852 Ops << bit_width;
SJWf93f5f32020-05-05 07:27:56 -05001853
1854 RID = addSPIRVInst<kTypes>(spv::OpTypeFloat, Ops);
1855 break;
1856 }
1857 case Type::ArrayTyID: {
1858 ArrayType *ArrTy = cast<ArrayType>(Ty);
1859 const uint64_t Length = ArrTy->getArrayNumElements();
1860 if (Length == 0) {
1861 // By convention, map it to a RuntimeArray.
1862
1863 Type *EleTy = ArrTy->getArrayElementType();
1864
1865 //
1866 // Generate OpTypeRuntimeArray.
1867 //
1868 // OpTypeRuntimeArray
1869 // Ops[0] = Element Type ID
1870 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05001871 Ops << EleTy;
SJWf93f5f32020-05-05 07:27:56 -05001872
1873 RID = addSPIRVInst<kTypes>(spv::OpTypeRuntimeArray, Ops);
1874
1875 if (Hack_generate_runtime_array_stride_early) {
1876 // Generate OpDecorate.
1877
1878 // Ops[0] = Target ID
1879 // Ops[1] = Decoration (ArrayStride)
1880 // Ops[2] = Stride Number(Literal Number)
1881 Ops.clear();
1882
SJW01901d92020-05-21 08:58:31 -05001883 Ops << RID << spv::DecorationArrayStride
1884 << static_cast<uint32_t>(GetTypeAllocSize(EleTy, DL));
SJWf93f5f32020-05-05 07:27:56 -05001885
1886 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
1887 }
1888
1889 } else {
1890
1891 //
1892 // Generate OpConstant and OpTypeArray.
1893 //
1894
1895 //
1896 // Generate OpConstant for array length.
1897 //
1898 // Add constant for length to constant list.
1899 Constant *CstLength =
1900 ConstantInt::get(Type::getInt32Ty(module->getContext()), Length);
SJWf93f5f32020-05-05 07:27:56 -05001901
1902 // Remember to generate ArrayStride later
1903 getTypesNeedingArrayStride().insert(Ty);
1904
1905 //
1906 // Generate OpTypeArray.
1907 //
1908 // Ops[0] = Element Type ID
1909 // Ops[1] = Array Length Constant ID
1910 SPIRVOperandVec Ops;
1911
SJW01901d92020-05-21 08:58:31 -05001912 Ops << ArrTy->getElementType() << CstLength;
SJWf93f5f32020-05-05 07:27:56 -05001913
1914 RID = addSPIRVInst<kTypes>(spv::OpTypeArray, Ops);
1915 }
1916 break;
1917 }
1918 case Type::FixedVectorTyID: {
1919 auto VecTy = cast<VectorType>(Ty);
1920 // <4 x i8> is changed to i32 if i8 is not generally supported.
1921 if (!clspv::Option::Int8Support() &&
1922 VecTy->getElementType() == Type::getInt8Ty(module->getContext())) {
alan-baker5a8c3be2020-09-09 13:44:26 -04001923 if (VecTy->getElementCount().getKnownMinValue() == 4) {
SJWf93f5f32020-05-05 07:27:56 -05001924 RID = getSPIRVType(VecTy->getElementType());
1925 break;
1926 } else {
1927 Ty->print(errs());
1928 llvm_unreachable("Support above i8 vector type");
1929 }
1930 }
1931
1932 // Ops[0] = Component Type ID
1933 // Ops[1] = Component Count (Literal Number)
1934 SPIRVOperandVec Ops;
alan-baker5a8c3be2020-09-09 13:44:26 -04001935 Ops << VecTy->getElementType()
1936 << VecTy->getElementCount().getKnownMinValue();
SJWf93f5f32020-05-05 07:27:56 -05001937
1938 RID = addSPIRVInst<kTypes>(spv::OpTypeVector, Ops);
1939 break;
1940 }
1941 case Type::VoidTyID: {
1942 RID = addSPIRVInst<kTypes>(spv::OpTypeVoid);
1943 break;
1944 }
1945 case Type::FunctionTyID: {
1946 // Generate SPIRV instruction for function type.
1947 FunctionType *FTy = cast<FunctionType>(Ty);
1948
1949 // Ops[0] = Return Type ID
1950 // Ops[1] ... Ops[n] = Parameter Type IDs
1951 SPIRVOperandVec Ops;
1952
1953 // Find SPIRV instruction for return type
SJW01901d92020-05-21 08:58:31 -05001954 Ops << FTy->getReturnType();
SJWf93f5f32020-05-05 07:27:56 -05001955
1956 // Find SPIRV instructions for parameter types
1957 for (unsigned k = 0; k < FTy->getNumParams(); k++) {
1958 // Find SPIRV instruction for parameter type.
1959 auto ParamTy = FTy->getParamType(k);
1960 if (ParamTy->isPointerTy()) {
1961 auto PointeeTy = ParamTy->getPointerElementType();
1962 if (PointeeTy->isStructTy() &&
1963 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
1964 ParamTy = PointeeTy;
1965 }
1966 }
1967
SJW01901d92020-05-21 08:58:31 -05001968 Ops << ParamTy;
SJWf93f5f32020-05-05 07:27:56 -05001969 }
1970
1971 RID = addSPIRVInst<kTypes>(spv::OpTypeFunction, Ops);
1972 break;
1973 }
1974 }
1975
SJW01901d92020-05-21 08:58:31 -05001976 if (RID.isValid()) {
SJWf93f5f32020-05-05 07:27:56 -05001977 TypeMap[Ty] = RID;
1978 }
1979 return RID;
David Neto22f144c2017-06-12 14:26:21 -04001980}
1981
SJW77b87ad2020-04-21 14:37:52 -05001982void SPIRVProducerPass::GenerateSPIRVTypes() {
David Neto22f144c2017-06-12 14:26:21 -04001983 for (Type *Ty : getTypeList()) {
SJWf93f5f32020-05-05 07:27:56 -05001984 getSPIRVType(Ty);
David Netoc6f3ab22018-04-06 18:02:31 -04001985 }
David Neto22f144c2017-06-12 14:26:21 -04001986}
1987
SJW806a5d82020-07-15 12:51:38 -05001988SPIRVID SPIRVProducerPass::getSPIRVInt32Constant(uint32_t CstVal) {
1989 Type *i32 = Type::getInt32Ty(module->getContext());
1990 Constant *Cst = ConstantInt::get(i32, CstVal);
1991 return getSPIRVValue(Cst);
1992}
1993
SJWf93f5f32020-05-05 07:27:56 -05001994SPIRVID SPIRVProducerPass::getSPIRVConstant(Constant *Cst) {
David Neto22f144c2017-06-12 14:26:21 -04001995 ValueMapType &VMap = getValueMap();
David Neto482550a2018-03-24 05:21:07 -07001996 const bool hack_undef = clspv::Option::HackUndef();
David Neto22f144c2017-06-12 14:26:21 -04001997
SJW01901d92020-05-21 08:58:31 -05001998 SPIRVID RID;
David Neto22f144c2017-06-12 14:26:21 -04001999
SJWf93f5f32020-05-05 07:27:56 -05002000 //
2001 // Generate OpConstant.
2002 //
2003 // Ops[0] = Result Type ID
2004 // Ops[1] .. Ops[n] = Values LiteralNumber
2005 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04002006
SJW01901d92020-05-21 08:58:31 -05002007 Ops << Cst->getType();
David Neto22f144c2017-06-12 14:26:21 -04002008
SJWf93f5f32020-05-05 07:27:56 -05002009 std::vector<uint32_t> LiteralNum;
2010 spv::Op Opcode = spv::OpNop;
David Neto22f144c2017-06-12 14:26:21 -04002011
SJWf93f5f32020-05-05 07:27:56 -05002012 if (isa<UndefValue>(Cst)) {
David Neto22f144c2017-06-12 14:26:21 -04002013 // Ops[0] = Result Type ID
SJWf93f5f32020-05-05 07:27:56 -05002014 Opcode = spv::OpUndef;
2015 if (hack_undef && IsTypeNullable(Cst->getType())) {
2016 Opcode = spv::OpConstantNull;
2017 }
2018 } else if (const ConstantInt *CI = dyn_cast<ConstantInt>(Cst)) {
alan-bakere2a62752020-07-09 22:53:23 -04002019 unsigned bit_width = CI->getBitWidth();
2020 if (bit_width == 1) {
SJWf93f5f32020-05-05 07:27:56 -05002021 // If the bitwidth of constant is 1, generate OpConstantTrue or
2022 // OpConstantFalse.
2023 if (CI->getZExtValue()) {
2024 // Ops[0] = Result Type ID
2025 Opcode = spv::OpConstantTrue;
David Neto22f144c2017-06-12 14:26:21 -04002026 } else {
SJWf93f5f32020-05-05 07:27:56 -05002027 // Ops[0] = Result Type ID
2028 Opcode = spv::OpConstantFalse;
David Neto22f144c2017-06-12 14:26:21 -04002029 }
SJWf93f5f32020-05-05 07:27:56 -05002030 } else {
2031 auto V = CI->getZExtValue();
2032 LiteralNum.push_back(V & 0xFFFFFFFF);
2033
alan-bakere2a62752020-07-09 22:53:23 -04002034 if (bit_width > 32) {
SJWf93f5f32020-05-05 07:27:56 -05002035 LiteralNum.push_back(V >> 32);
David Neto22f144c2017-06-12 14:26:21 -04002036 }
2037
2038 Opcode = spv::OpConstant;
David Neto22f144c2017-06-12 14:26:21 -04002039
SJW01901d92020-05-21 08:58:31 -05002040 Ops << LiteralNum;
SJWf93f5f32020-05-05 07:27:56 -05002041 }
2042 } else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(Cst)) {
2043 uint64_t FPVal = CFP->getValueAPF().bitcastToAPInt().getZExtValue();
2044 Type *CFPTy = CFP->getType();
2045 if (CFPTy->isFloatTy()) {
2046 LiteralNum.push_back(FPVal & 0xFFFFFFFF);
2047 } else if (CFPTy->isDoubleTy()) {
2048 LiteralNum.push_back(FPVal & 0xFFFFFFFF);
2049 LiteralNum.push_back(FPVal >> 32);
2050 } else if (CFPTy->isHalfTy()) {
2051 LiteralNum.push_back(FPVal & 0xFFFF);
2052 } else {
2053 CFPTy->print(errs());
2054 llvm_unreachable("Implement this ConstantFP Type");
2055 }
David Neto22f144c2017-06-12 14:26:21 -04002056
SJWf93f5f32020-05-05 07:27:56 -05002057 Opcode = spv::OpConstant;
David Neto49351ac2017-08-26 17:32:20 -04002058
SJW01901d92020-05-21 08:58:31 -05002059 Ops << LiteralNum;
SJWf93f5f32020-05-05 07:27:56 -05002060 } else if (isa<ConstantDataSequential>(Cst) &&
2061 cast<ConstantDataSequential>(Cst)->isString()) {
2062 Cst->print(errs());
2063 llvm_unreachable("Implement this Constant");
David Neto49351ac2017-08-26 17:32:20 -04002064
SJWf93f5f32020-05-05 07:27:56 -05002065 } else if (const ConstantDataSequential *CDS =
2066 dyn_cast<ConstantDataSequential>(Cst)) {
2067 // Let's convert <4 x i8> constant to int constant specially.
2068 // This case occurs when all the values are specified as constant
2069 // ints.
2070 Type *CstTy = Cst->getType();
2071 if (is4xi8vec(CstTy)) {
SJWf93f5f32020-05-05 07:27:56 -05002072 //
2073 // Generate OpConstant with OpTypeInt 32 0.
2074 //
2075 uint32_t IntValue = 0;
2076 for (unsigned k = 0; k < 4; k++) {
2077 const uint64_t Val = CDS->getElementAsInteger(k);
2078 IntValue = (IntValue << 8) | (Val & 0xffu);
David Neto49351ac2017-08-26 17:32:20 -04002079 }
2080
SJW806a5d82020-07-15 12:51:38 -05002081 RID = getSPIRVInt32Constant(IntValue);
SJWf93f5f32020-05-05 07:27:56 -05002082 } else {
2083
David Neto49351ac2017-08-26 17:32:20 -04002084 // A normal constant-data-sequential case.
David Neto22f144c2017-06-12 14:26:21 -04002085 for (unsigned k = 0; k < CDS->getNumElements(); k++) {
SJW01901d92020-05-21 08:58:31 -05002086 Ops << CDS->getElementAsConstant(k);
David Neto22f144c2017-06-12 14:26:21 -04002087 }
2088
2089 Opcode = spv::OpConstantComposite;
SJWf93f5f32020-05-05 07:27:56 -05002090 }
2091 } else if (const ConstantAggregate *CA = dyn_cast<ConstantAggregate>(Cst)) {
2092 // Let's convert <4 x i8> constant to int constant specially.
2093 // This case occurs when at least one of the values is an undef.
2094 Type *CstTy = Cst->getType();
2095 if (is4xi8vec(CstTy)) {
SJWf93f5f32020-05-05 07:27:56 -05002096 //
2097 // Generate OpConstant with OpTypeInt 32 0.
2098 //
2099 uint32_t IntValue = 0;
2100 for (User::const_op_iterator I = Cst->op_begin(), E = Cst->op_end();
2101 I != E; ++I) {
2102 uint64_t Val = 0;
2103 const Value *CV = *I;
2104 if (auto *CI2 = dyn_cast<ConstantInt>(CV)) {
2105 Val = CI2->getZExtValue();
David Neto22f144c2017-06-12 14:26:21 -04002106 }
SJWf93f5f32020-05-05 07:27:56 -05002107 IntValue = (IntValue << 8) | (Val & 0xffu);
David Neto22f144c2017-06-12 14:26:21 -04002108 }
2109
SJW806a5d82020-07-15 12:51:38 -05002110 RID = getSPIRVInt32Constant(IntValue);
SJWf93f5f32020-05-05 07:27:56 -05002111 } else {
2112
David Neto22f144c2017-06-12 14:26:21 -04002113 // We use a constant composite in SPIR-V for our constant aggregate in
2114 // LLVM.
2115 Opcode = spv::OpConstantComposite;
David Neto22f144c2017-06-12 14:26:21 -04002116
2117 for (unsigned k = 0; k < CA->getNumOperands(); k++) {
David Neto22f144c2017-06-12 14:26:21 -04002118 // And add an operand to the composite we are constructing
SJW01901d92020-05-21 08:58:31 -05002119 Ops << CA->getAggregateElement(k);
David Neto22f144c2017-06-12 14:26:21 -04002120 }
David Neto22f144c2017-06-12 14:26:21 -04002121 }
SJWf93f5f32020-05-05 07:27:56 -05002122 } else if (Cst->isNullValue()) {
2123 Opcode = spv::OpConstantNull;
2124 } else {
2125 Cst->print(errs());
2126 llvm_unreachable("Unsupported Constant???");
2127 }
David Neto22f144c2017-06-12 14:26:21 -04002128
SJWf93f5f32020-05-05 07:27:56 -05002129 if (Opcode == spv::OpConstantNull && Cst->getType()->isPointerTy()) {
2130 // Null pointer requires variable pointers.
2131 setVariablePointersCapabilities(Cst->getType()->getPointerAddressSpace());
2132 }
alan-baker5b86ed72019-02-15 08:26:50 -05002133
SJWf93f5f32020-05-05 07:27:56 -05002134 if (RID == 0) {
2135 RID = addSPIRVInst<kConstants>(Opcode, Ops);
2136 }
2137
2138 VMap[Cst] = RID;
2139
2140 return RID;
2141}
2142
2143SPIRVID SPIRVProducerPass::getSPIRVValue(Value *V) {
2144 auto II = ValueMap.find(V);
2145 if (II != ValueMap.end()) {
SJW01901d92020-05-21 08:58:31 -05002146 assert(II->second.isValid());
SJWf93f5f32020-05-05 07:27:56 -05002147 return II->second;
2148 }
2149 if (Constant *Cst = dyn_cast<Constant>(V)) {
2150 return getSPIRVConstant(Cst);
2151 } else {
2152 llvm_unreachable("Variable not found");
2153 }
2154}
2155
SJW77b87ad2020-04-21 14:37:52 -05002156void SPIRVProducerPass::GenerateSamplers() {
alan-bakerb6b09dc2018-11-08 16:59:28 -05002157 auto &sampler_map = getSamplerMap();
alan-baker09cb9802019-12-10 13:16:27 -05002158 SamplerLiteralToIDMap.clear();
David Neto862b7d82018-06-14 18:48:37 -04002159 DenseMap<unsigned, unsigned> SamplerLiteralToDescriptorSetMap;
2160 DenseMap<unsigned, unsigned> SamplerLiteralToBindingMap;
David Neto22f144c2017-06-12 14:26:21 -04002161
David Neto862b7d82018-06-14 18:48:37 -04002162 // We might have samplers in the sampler map that are not used
2163 // in the translation unit. We need to allocate variables
2164 // for them and bindings too.
2165 DenseSet<unsigned> used_bindings;
David Neto22f144c2017-06-12 14:26:21 -04002166
SJW77b87ad2020-04-21 14:37:52 -05002167 auto *var_fn = module->getFunction(clspv::LiteralSamplerFunction());
alan-baker09cb9802019-12-10 13:16:27 -05002168 // Return if there are no literal samplers.
alan-bakerb6b09dc2018-11-08 16:59:28 -05002169 if (!var_fn)
2170 return;
alan-baker09cb9802019-12-10 13:16:27 -05002171
David Neto862b7d82018-06-14 18:48:37 -04002172 for (auto user : var_fn->users()) {
2173 // Populate SamplerLiteralToDescriptorSetMap and
2174 // SamplerLiteralToBindingMap.
2175 //
2176 // Look for calls like
2177 // call %opencl.sampler_t addrspace(2)*
2178 // @clspv.sampler.var.literal(
2179 // i32 descriptor,
2180 // i32 binding,
alan-baker09cb9802019-12-10 13:16:27 -05002181 // i32 (index-into-sampler-map|sampler_mask))
alan-bakerb6b09dc2018-11-08 16:59:28 -05002182 if (auto *call = dyn_cast<CallInst>(user)) {
alan-baker09cb9802019-12-10 13:16:27 -05002183 const auto third_param = static_cast<unsigned>(
alan-bakerb6b09dc2018-11-08 16:59:28 -05002184 dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue());
alan-baker09cb9802019-12-10 13:16:27 -05002185 auto sampler_value = third_param;
2186 if (clspv::Option::UseSamplerMap()) {
2187 if (third_param >= sampler_map.size()) {
2188 errs() << "Out of bounds index to sampler map: " << third_param;
2189 llvm_unreachable("bad sampler init: out of bounds");
2190 }
2191 sampler_value = sampler_map[third_param].first;
David Neto862b7d82018-06-14 18:48:37 -04002192 }
2193
David Neto862b7d82018-06-14 18:48:37 -04002194 const auto descriptor_set = static_cast<unsigned>(
2195 dyn_cast<ConstantInt>(call->getArgOperand(0))->getZExtValue());
2196 const auto binding = static_cast<unsigned>(
2197 dyn_cast<ConstantInt>(call->getArgOperand(1))->getZExtValue());
2198
2199 SamplerLiteralToDescriptorSetMap[sampler_value] = descriptor_set;
2200 SamplerLiteralToBindingMap[sampler_value] = binding;
2201 used_bindings.insert(binding);
2202 }
2203 }
2204
alan-baker09cb9802019-12-10 13:16:27 -05002205 DenseSet<size_t> seen;
2206 for (auto user : var_fn->users()) {
2207 if (!isa<CallInst>(user))
2208 continue;
2209
2210 auto call = cast<CallInst>(user);
2211 const unsigned third_param = static_cast<unsigned>(
2212 dyn_cast<ConstantInt>(call->getArgOperand(2))->getZExtValue());
2213
2214 // Already allocated a variable for this value.
2215 if (!seen.insert(third_param).second)
2216 continue;
2217
2218 auto sampler_value = third_param;
2219 if (clspv::Option::UseSamplerMap()) {
2220 sampler_value = sampler_map[third_param].first;
2221 }
2222
SJW806a5d82020-07-15 12:51:38 -05002223 auto sampler_var_id = addSPIRVGlobalVariable(
2224 getSPIRVType(SamplerTy), spv::StorageClassUniformConstant);
David Neto22f144c2017-06-12 14:26:21 -04002225
alan-baker09cb9802019-12-10 13:16:27 -05002226 SamplerLiteralToIDMap[sampler_value] = sampler_var_id;
David Neto22f144c2017-06-12 14:26:21 -04002227
David Neto862b7d82018-06-14 18:48:37 -04002228 unsigned descriptor_set;
2229 unsigned binding;
alan-baker09cb9802019-12-10 13:16:27 -05002230 if (SamplerLiteralToBindingMap.find(sampler_value) ==
alan-bakerb6b09dc2018-11-08 16:59:28 -05002231 SamplerLiteralToBindingMap.end()) {
David Neto862b7d82018-06-14 18:48:37 -04002232 // This sampler is not actually used. Find the next one.
alan-baker7506abb2020-09-10 15:02:55 -04002233 for (binding = 0; used_bindings.count(binding); binding++) {
2234 }
David Neto862b7d82018-06-14 18:48:37 -04002235 descriptor_set = 0; // Literal samplers always use descriptor set 0.
2236 used_bindings.insert(binding);
2237 } else {
alan-baker09cb9802019-12-10 13:16:27 -05002238 descriptor_set = SamplerLiteralToDescriptorSetMap[sampler_value];
2239 binding = SamplerLiteralToBindingMap[sampler_value];
alan-bakercff80152019-06-15 00:38:00 -04002240
alan-baker86ce19c2020-08-05 13:09:19 -04002241 auto import_id = getReflectionImport();
2242 SPIRVOperandVec Ops;
2243 Ops << getSPIRVType(Type::getVoidTy(module->getContext())) << import_id
2244 << reflection::ExtInstLiteralSampler
2245 << getSPIRVInt32Constant(descriptor_set)
2246 << getSPIRVInt32Constant(binding)
2247 << getSPIRVInt32Constant(sampler_value);
2248 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
David Neto862b7d82018-06-14 18:48:37 -04002249 }
2250
SJW69939d52020-04-16 07:29:07 -05002251 // Ops[0] = Target ID
2252 // Ops[1] = Decoration (DescriptorSet)
2253 // Ops[2] = LiteralNumber according to Decoration
SJW806a5d82020-07-15 12:51:38 -05002254 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05002255 Ops << sampler_var_id << spv::DecorationDescriptorSet << descriptor_set;
David Neto22f144c2017-06-12 14:26:21 -04002256
SJWf93f5f32020-05-05 07:27:56 -05002257 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002258
2259 // Ops[0] = Target ID
2260 // Ops[1] = Decoration (Binding)
2261 // Ops[2] = LiteralNumber according to Decoration
2262 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002263 Ops << sampler_var_id << spv::DecorationBinding << binding;
David Neto22f144c2017-06-12 14:26:21 -04002264
SJWf93f5f32020-05-05 07:27:56 -05002265 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002266 }
David Neto862b7d82018-06-14 18:48:37 -04002267}
David Neto22f144c2017-06-12 14:26:21 -04002268
SJW77b87ad2020-04-21 14:37:52 -05002269void SPIRVProducerPass::GenerateResourceVars() {
David Neto862b7d82018-06-14 18:48:37 -04002270 ValueMapType &VMap = getValueMap();
David Neto22f144c2017-06-12 14:26:21 -04002271
David Neto862b7d82018-06-14 18:48:37 -04002272 // Generate variables. Make one for each of resource var info object.
2273 for (auto *info : ModuleOrderedResourceVars) {
2274 Type *type = info->var_fn->getReturnType();
2275 // Remap the address space for opaque types.
2276 switch (info->arg_kind) {
2277 case clspv::ArgKind::Sampler:
alan-bakerf6bc8252020-09-23 14:58:55 -04002278 case clspv::ArgKind::SampledImage:
2279 case clspv::ArgKind::StorageImage:
David Neto862b7d82018-06-14 18:48:37 -04002280 type = PointerType::get(type->getPointerElementType(),
2281 clspv::AddressSpace::UniformConstant);
2282 break;
2283 default:
2284 break;
2285 }
David Neto22f144c2017-06-12 14:26:21 -04002286
David Neto862b7d82018-06-14 18:48:37 -04002287 const auto sc = GetStorageClassForArgKind(info->arg_kind);
David Neto22f144c2017-06-12 14:26:21 -04002288
SJW806a5d82020-07-15 12:51:38 -05002289 info->var_id = addSPIRVGlobalVariable(getSPIRVType(type), sc);
David Neto862b7d82018-06-14 18:48:37 -04002290
2291 // Map calls to the variable-builtin-function.
2292 for (auto &U : info->var_fn->uses()) {
2293 if (auto *call = dyn_cast<CallInst>(U.getUser())) {
2294 const auto set = unsigned(
2295 dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue());
2296 const auto binding = unsigned(
2297 dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue());
2298 if (set == info->descriptor_set && binding == info->binding) {
2299 switch (info->arg_kind) {
2300 case clspv::ArgKind::Buffer:
Alan Bakerfcda9482018-10-02 17:09:59 -04002301 case clspv::ArgKind::BufferUBO:
David Neto862b7d82018-06-14 18:48:37 -04002302 case clspv::ArgKind::Pod:
alan-baker9b0ec3c2020-04-06 14:45:34 -04002303 case clspv::ArgKind::PodUBO:
2304 case clspv::ArgKind::PodPushConstant:
David Neto862b7d82018-06-14 18:48:37 -04002305 // The call maps to the variable directly.
2306 VMap[call] = info->var_id;
2307 break;
2308 case clspv::ArgKind::Sampler:
alan-bakerf6bc8252020-09-23 14:58:55 -04002309 case clspv::ArgKind::SampledImage:
2310 case clspv::ArgKind::StorageImage:
David Neto862b7d82018-06-14 18:48:37 -04002311 // The call maps to a load we generate later.
2312 ResourceVarDeferredLoadCalls[call] = info->var_id;
2313 break;
2314 default:
2315 llvm_unreachable("Unhandled arg kind");
2316 }
2317 }
David Neto22f144c2017-06-12 14:26:21 -04002318 }
David Neto862b7d82018-06-14 18:48:37 -04002319 }
2320 }
David Neto22f144c2017-06-12 14:26:21 -04002321
David Neto862b7d82018-06-14 18:48:37 -04002322 // Generate associated decorations.
SJWf93f5f32020-05-05 07:27:56 -05002323 SPIRVOperandVec Ops;
David Neto862b7d82018-06-14 18:48:37 -04002324 for (auto *info : ModuleOrderedResourceVars) {
alan-baker9b0ec3c2020-04-06 14:45:34 -04002325 // Push constants don't need descriptor set or binding decorations.
2326 if (info->arg_kind == clspv::ArgKind::PodPushConstant)
2327 continue;
2328
David Neto862b7d82018-06-14 18:48:37 -04002329 // Decorate with DescriptorSet and Binding.
2330 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002331 Ops << info->var_id << spv::DecorationDescriptorSet << info->descriptor_set;
SJWf93f5f32020-05-05 07:27:56 -05002332 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto862b7d82018-06-14 18:48:37 -04002333
2334 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002335 Ops << info->var_id << spv::DecorationBinding << info->binding;
SJWf93f5f32020-05-05 07:27:56 -05002336 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto862b7d82018-06-14 18:48:37 -04002337
alan-bakere9308012019-03-15 10:25:13 -04002338 if (info->coherent) {
2339 // Decorate with Coherent if required for the variable.
2340 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002341 Ops << info->var_id << spv::DecorationCoherent;
SJWf93f5f32020-05-05 07:27:56 -05002342 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
alan-bakere9308012019-03-15 10:25:13 -04002343 }
2344
David Neto862b7d82018-06-14 18:48:37 -04002345 // Generate NonWritable and NonReadable
2346 switch (info->arg_kind) {
2347 case clspv::ArgKind::Buffer:
Alan Bakerfcda9482018-10-02 17:09:59 -04002348 case clspv::ArgKind::BufferUBO:
David Neto862b7d82018-06-14 18:48:37 -04002349 if (info->var_fn->getReturnType()->getPointerAddressSpace() ==
2350 clspv::AddressSpace::Constant) {
2351 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002352 Ops << info->var_id << spv::DecorationNonWritable;
SJWf93f5f32020-05-05 07:27:56 -05002353 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002354 }
David Neto862b7d82018-06-14 18:48:37 -04002355 break;
alan-bakerf6bc8252020-09-23 14:58:55 -04002356 case clspv::ArgKind::StorageImage: {
2357 auto *type = info->var_fn->getReturnType();
2358 auto *struct_ty = cast<StructType>(type->getPointerElementType());
2359 // TODO(alan-baker): This is conservative. If compiling for OpenCL 2.0 or
2360 // above, the compiler treats all write_only images as read_write images.
2361 if (struct_ty->getName().contains("_wo_t")) {
2362 Ops.clear();
2363 Ops << info->var_id << spv::DecorationNonReadable;
2364 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
2365 }
David Neto862b7d82018-06-14 18:48:37 -04002366 break;
alan-bakerf6bc8252020-09-23 14:58:55 -04002367 }
David Neto862b7d82018-06-14 18:48:37 -04002368 default:
2369 break;
David Neto22f144c2017-06-12 14:26:21 -04002370 }
2371 }
2372}
2373
2374void SPIRVProducerPass::GenerateGlobalVar(GlobalVariable &GV) {
David Neto22f144c2017-06-12 14:26:21 -04002375 ValueMapType &VMap = getValueMap();
SJW01901d92020-05-21 08:58:31 -05002376 std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec();
David Neto85082642018-03-24 06:55:20 -07002377 const DataLayout &DL = GV.getParent()->getDataLayout();
David Neto22f144c2017-06-12 14:26:21 -04002378
2379 const spv::BuiltIn BuiltinType = GetBuiltin(GV.getName());
2380 Type *Ty = GV.getType();
2381 PointerType *PTy = cast<PointerType>(Ty);
2382
SJW01901d92020-05-21 08:58:31 -05002383 SPIRVID InitializerID;
David Neto22f144c2017-06-12 14:26:21 -04002384
2385 // Workgroup size is handled differently (it goes into a constant)
2386 if (spv::BuiltInWorkgroupSize == BuiltinType) {
David Neto22f144c2017-06-12 14:26:21 -04002387 uint32_t PrevXDimCst = 0xFFFFFFFF;
2388 uint32_t PrevYDimCst = 0xFFFFFFFF;
2389 uint32_t PrevZDimCst = 0xFFFFFFFF;
alan-baker3b609772020-09-03 19:10:17 -04002390 bool HasMD = true;
David Neto22f144c2017-06-12 14:26:21 -04002391 for (Function &Func : *GV.getParent()) {
2392 if (Func.isDeclaration()) {
2393 continue;
2394 }
2395
2396 // We only need to check kernels.
2397 if (Func.getCallingConv() != CallingConv::SPIR_KERNEL) {
2398 continue;
2399 }
2400
2401 if (const MDNode *MD =
2402 dyn_cast<Function>(&Func)->getMetadata("reqd_work_group_size")) {
2403 uint32_t CurXDimCst = static_cast<uint32_t>(
2404 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
2405 uint32_t CurYDimCst = static_cast<uint32_t>(
2406 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue());
2407 uint32_t CurZDimCst = static_cast<uint32_t>(
2408 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue());
2409
2410 if (PrevXDimCst == 0xFFFFFFFF && PrevYDimCst == 0xFFFFFFFF &&
2411 PrevZDimCst == 0xFFFFFFFF) {
2412 PrevXDimCst = CurXDimCst;
2413 PrevYDimCst = CurYDimCst;
2414 PrevZDimCst = CurZDimCst;
2415 } else if (CurXDimCst != PrevXDimCst || CurYDimCst != PrevYDimCst ||
2416 CurZDimCst != PrevZDimCst) {
alan-baker3b609772020-09-03 19:10:17 -04002417 HasMD = false;
2418 continue;
David Neto22f144c2017-06-12 14:26:21 -04002419 } else {
2420 continue;
2421 }
2422
2423 //
2424 // Generate OpConstantComposite.
2425 //
2426 // Ops[0] : Result Type ID
2427 // Ops[1] : Constant size for x dimension.
2428 // Ops[2] : Constant size for y dimension.
2429 // Ops[3] : Constant size for z dimension.
SJWf93f5f32020-05-05 07:27:56 -05002430 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04002431
SJW01901d92020-05-21 08:58:31 -05002432 SPIRVID XDimCstID =
SJWf93f5f32020-05-05 07:27:56 -05002433 getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(0)));
SJW01901d92020-05-21 08:58:31 -05002434 SPIRVID YDimCstID =
SJWf93f5f32020-05-05 07:27:56 -05002435 getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(1)));
SJW01901d92020-05-21 08:58:31 -05002436 SPIRVID ZDimCstID =
SJWf93f5f32020-05-05 07:27:56 -05002437 getSPIRVValue(mdconst::extract<ConstantInt>(MD->getOperand(2)));
David Neto22f144c2017-06-12 14:26:21 -04002438
SJW01901d92020-05-21 08:58:31 -05002439 Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID
2440 << ZDimCstID;
David Neto22f144c2017-06-12 14:26:21 -04002441
SJWf93f5f32020-05-05 07:27:56 -05002442 InitializerID =
2443 addSPIRVInst<kGlobalVariables>(spv::OpConstantComposite, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002444 } else {
alan-baker3b609772020-09-03 19:10:17 -04002445 HasMD = false;
David Neto22f144c2017-06-12 14:26:21 -04002446 }
2447 }
2448
2449 // If all kernels do not have metadata for reqd_work_group_size, generate
2450 // OpSpecConstants for x/y/z dimension.
Kévin Petit21c23c62020-04-29 01:38:28 +01002451 if (!HasMD || clspv::Option::NonUniformNDRangeSupported()) {
David Neto22f144c2017-06-12 14:26:21 -04002452 //
2453 // Generate OpSpecConstants for x/y/z dimension.
2454 //
2455 // Ops[0] : Result Type ID
2456 // Ops[1] : Constant size for x/y/z dimension (Literal Number).
David Neto22f144c2017-06-12 14:26:21 -04002457
alan-bakera1be3322020-04-20 12:48:18 -04002458 // Allocate spec constants for workgroup size.
SJW77b87ad2020-04-21 14:37:52 -05002459 clspv::AddWorkgroupSpecConstants(module);
alan-bakera1be3322020-04-20 12:48:18 -04002460
SJWf93f5f32020-05-05 07:27:56 -05002461 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05002462 SPIRVID result_type_id = getSPIRVType(
SJWf93f5f32020-05-05 07:27:56 -05002463 dyn_cast<VectorType>(Ty->getPointerElementType())->getElementType());
David Neto22f144c2017-06-12 14:26:21 -04002464
David Neto257c3892018-04-11 13:19:45 -04002465 // X Dimension
SJW01901d92020-05-21 08:58:31 -05002466 Ops << result_type_id << 1;
2467 SPIRVID XDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002468
2469 // Y Dimension
2470 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002471 Ops << result_type_id << 1;
2472 SPIRVID YDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002473
2474 // Z Dimension
2475 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002476 Ops << result_type_id << 1;
2477 SPIRVID ZDimCstID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002478
David Neto257c3892018-04-11 13:19:45 -04002479 BuiltinDimVec.push_back(XDimCstID);
2480 BuiltinDimVec.push_back(YDimCstID);
David Neto22f144c2017-06-12 14:26:21 -04002481 BuiltinDimVec.push_back(ZDimCstID);
2482
David Neto22f144c2017-06-12 14:26:21 -04002483 //
2484 // Generate OpSpecConstantComposite.
2485 //
2486 // Ops[0] : Result Type ID
2487 // Ops[1] : Constant size for x dimension.
2488 // Ops[2] : Constant size for y dimension.
2489 // Ops[3] : Constant size for z dimension.
David Neto22f144c2017-06-12 14:26:21 -04002490 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002491 Ops << Ty->getPointerElementType() << XDimCstID << YDimCstID << ZDimCstID;
David Neto22f144c2017-06-12 14:26:21 -04002492
SJWf93f5f32020-05-05 07:27:56 -05002493 InitializerID =
2494 addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002495 }
alan-bakerbed3a882020-04-21 14:42:41 -04002496 } else if (BuiltinType == spv::BuiltInWorkDim) {
2497 // 1. Generate a specialization constant with a default of 3.
2498 // 2. Allocate and annotate a SpecId for the constant.
2499 // 3. Use the spec constant as the initializer for the variable.
SJWf93f5f32020-05-05 07:27:56 -05002500 SPIRVOperandVec Ops;
alan-bakerbed3a882020-04-21 14:42:41 -04002501
2502 //
2503 // Generate OpSpecConstant.
2504 //
2505 // Ops[0] : Result Type ID
2506 // Ops[1] : Default literal value
alan-bakerbed3a882020-04-21 14:42:41 -04002507
SJW01901d92020-05-21 08:58:31 -05002508 Ops << IntegerType::get(GV.getContext(), 32) << 3;
alan-bakerbed3a882020-04-21 14:42:41 -04002509
SJWf93f5f32020-05-05 07:27:56 -05002510 InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
alan-bakerbed3a882020-04-21 14:42:41 -04002511
2512 //
2513 // Generate SpecId decoration.
2514 //
2515 // Ops[0] : target
2516 // Ops[1] : decoration
2517 // Ops[2] : SpecId
Alan Baker75ccc252020-04-21 17:11:52 -04002518 auto spec_id = AllocateSpecConstant(module, SpecConstant::kWorkDim);
alan-bakerbed3a882020-04-21 14:42:41 -04002519 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002520 Ops << InitializerID << spv::DecorationSpecId << spec_id;
alan-bakerbed3a882020-04-21 14:42:41 -04002521
SJWf93f5f32020-05-05 07:27:56 -05002522 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002523 } else if (BuiltinType == spv::BuiltInGlobalOffset) {
2524 // 1. Generate a spec constant with a default of {0, 0, 0}.
2525 // 2. Allocate and annotate SpecIds for the constants.
2526 // 3. Use the spec constant as the initializer for the variable.
SJWf93f5f32020-05-05 07:27:56 -05002527 SPIRVOperandVec Ops;
alan-bakere1996972020-05-04 08:38:12 -04002528
2529 //
2530 // Generate OpSpecConstant for each dimension.
2531 //
2532 // Ops[0] : Result Type ID
2533 // Ops[1] : Default literal value
2534 //
SJW01901d92020-05-21 08:58:31 -05002535 Ops << IntegerType::get(GV.getContext(), 32) << 0;
2536 SPIRVID x_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002537
alan-bakere1996972020-05-04 08:38:12 -04002538 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002539 Ops << IntegerType::get(GV.getContext(), 32) << 0;
2540 SPIRVID y_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002541
alan-bakere1996972020-05-04 08:38:12 -04002542 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002543 Ops << IntegerType::get(GV.getContext(), 32) << 0;
2544 SPIRVID z_id = addSPIRVInst<kConstants>(spv::OpSpecConstant, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002545
2546 //
2547 // Generate SpecId decoration for each dimension.
2548 //
2549 // Ops[0] : target
2550 // Ops[1] : decoration
2551 // Ops[2] : SpecId
2552 //
2553 auto spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetX);
2554 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002555 Ops << x_id << spv::DecorationSpecId << spec_id;
SJWf93f5f32020-05-05 07:27:56 -05002556 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002557
2558 spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetY);
2559 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002560 Ops << y_id << spv::DecorationSpecId << spec_id;
SJWf93f5f32020-05-05 07:27:56 -05002561 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002562
2563 spec_id = AllocateSpecConstant(module, SpecConstant::kGlobalOffsetZ);
2564 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002565 Ops << z_id << spv::DecorationSpecId << spec_id;
SJWf93f5f32020-05-05 07:27:56 -05002566 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
alan-bakere1996972020-05-04 08:38:12 -04002567
2568 //
2569 // Generate OpSpecConstantComposite.
2570 //
2571 // Ops[0] : type id
2572 // Ops[1..n-1] : elements
2573 //
alan-bakere1996972020-05-04 08:38:12 -04002574 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002575 Ops << GV.getType()->getPointerElementType() << x_id << y_id << z_id;
SJWf93f5f32020-05-05 07:27:56 -05002576 InitializerID = addSPIRVInst<kConstants>(spv::OpSpecConstantComposite, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002577 }
2578
David Neto85082642018-03-24 06:55:20 -07002579 const auto AS = PTy->getAddressSpace();
SJW806a5d82020-07-15 12:51:38 -05002580 const auto spvSC = GetStorageClass(AS);
David Neto22f144c2017-06-12 14:26:21 -04002581
David Neto85082642018-03-24 06:55:20 -07002582 const bool module_scope_constant_external_init =
David Neto862b7d82018-06-14 18:48:37 -04002583 (AS == AddressSpace::Constant) && GV.hasInitializer() &&
David Neto85082642018-03-24 06:55:20 -07002584 clspv::Option::ModuleConstantsInStorageBuffer();
2585
Kévin Petit23d5f182019-08-13 16:21:29 +01002586 if (GV.hasInitializer()) {
2587 auto GVInit = GV.getInitializer();
2588 if (!isa<UndefValue>(GVInit) && !module_scope_constant_external_init) {
SJWf93f5f32020-05-05 07:27:56 -05002589 InitializerID = getSPIRVValue(GVInit);
David Neto85082642018-03-24 06:55:20 -07002590 }
2591 }
Kévin Petit23d5f182019-08-13 16:21:29 +01002592
SJW806a5d82020-07-15 12:51:38 -05002593 SPIRVID var_id =
2594 addSPIRVGlobalVariable(getSPIRVType(Ty), spvSC, InitializerID);
David Neto85082642018-03-24 06:55:20 -07002595
SJWf93f5f32020-05-05 07:27:56 -05002596 VMap[&GV] = var_id;
David Neto22f144c2017-06-12 14:26:21 -04002597
alan-bakere1996972020-05-04 08:38:12 -04002598 auto IsOpenCLBuiltin = [](spv::BuiltIn builtin) {
2599 return builtin == spv::BuiltInWorkDim ||
2600 builtin == spv::BuiltInGlobalOffset;
2601 };
2602
alan-bakere1996972020-05-04 08:38:12 -04002603 // If we have a builtin (not an OpenCL builtin).
2604 if (spv::BuiltInMax != BuiltinType && !IsOpenCLBuiltin(BuiltinType)) {
David Neto22f144c2017-06-12 14:26:21 -04002605 //
2606 // Generate OpDecorate.
2607 //
2608 // DOps[0] = Target ID
2609 // DOps[1] = Decoration (Builtin)
2610 // DOps[2] = BuiltIn ID
SJW01901d92020-05-21 08:58:31 -05002611 SPIRVID ResultID;
David Neto22f144c2017-06-12 14:26:21 -04002612
2613 // WorkgroupSize is different, we decorate the constant composite that has
2614 // its value, rather than the variable that we use to access the value.
2615 if (spv::BuiltInWorkgroupSize == BuiltinType) {
2616 ResultID = InitializerID;
David Netoa60b00b2017-09-15 16:34:09 -04002617 // Save both the value and variable IDs for later.
2618 WorkgroupSizeValueID = InitializerID;
SJWf93f5f32020-05-05 07:27:56 -05002619 WorkgroupSizeVarID = getSPIRVValue(&GV);
David Neto22f144c2017-06-12 14:26:21 -04002620 } else {
SJWf93f5f32020-05-05 07:27:56 -05002621 ResultID = getSPIRVValue(&GV);
David Neto22f144c2017-06-12 14:26:21 -04002622 }
2623
SJW806a5d82020-07-15 12:51:38 -05002624 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05002625 Ops << ResultID << spv::DecorationBuiltIn << BuiltinType;
David Neto22f144c2017-06-12 14:26:21 -04002626
SJW01901d92020-05-21 08:58:31 -05002627 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto85082642018-03-24 06:55:20 -07002628 } else if (module_scope_constant_external_init) {
2629 // This module scope constant is initialized from a storage buffer with data
2630 // provided by the host at binding 0 of the next descriptor set.
SJW77b87ad2020-04-21 14:37:52 -05002631 const uint32_t descriptor_set = TakeDescriptorIndex(module);
David Neto85082642018-03-24 06:55:20 -07002632
alan-baker86ce19c2020-08-05 13:09:19 -04002633 // Emit the intializer as a reflection instruction.
David Neto85082642018-03-24 06:55:20 -07002634 // Use "kind,buffer" to indicate storage buffer. We might want to expand
2635 // that later to other types, like uniform buffer.
alan-bakerf5e5f692018-11-27 08:33:24 -05002636 std::string hexbytes;
2637 llvm::raw_string_ostream str(hexbytes);
2638 clspv::ConstantEmitter(DL, str).Emit(GV.getInitializer());
alan-baker86ce19c2020-08-05 13:09:19 -04002639
2640 // Reflection instruction for constant data.
2641 SPIRVOperandVec Ops;
2642 auto data_id = addSPIRVInst<kDebug>(spv::OpString, str.str().c_str());
2643 Ops << getSPIRVType(Type::getVoidTy(module->getContext()))
2644 << getReflectionImport() << reflection::ExtInstConstantDataStorageBuffer
2645 << getSPIRVInt32Constant(descriptor_set) << getSPIRVInt32Constant(0)
2646 << data_id;
2647 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
David Neto85082642018-03-24 06:55:20 -07002648
David Neto85082642018-03-24 06:55:20 -07002649 // OpDecorate %var DescriptorSet <descriptor_set>
alan-baker86ce19c2020-08-05 13:09:19 -04002650 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002651 Ops << var_id << spv::DecorationDescriptorSet << descriptor_set;
2652 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
SJW69939d52020-04-16 07:29:07 -05002653
2654 // OpDecorate %var Binding <binding>
SJW01901d92020-05-21 08:58:31 -05002655 Ops.clear();
2656 Ops << var_id << spv::DecorationBinding << 0;
2657 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Netoc6f3ab22018-04-06 18:02:31 -04002658 }
2659}
2660
David Neto22f144c2017-06-12 14:26:21 -04002661void SPIRVProducerPass::GenerateFuncPrologue(Function &F) {
David Neto22f144c2017-06-12 14:26:21 -04002662 ValueMapType &VMap = getValueMap();
2663 EntryPointVecType &EntryPoints = getEntryPointVec();
David Neto22f144c2017-06-12 14:26:21 -04002664 auto &GlobalConstFuncTyMap = getGlobalConstFuncTypeMap();
2665 auto &GlobalConstArgSet = getGlobalConstArgSet();
2666
2667 FunctionType *FTy = F.getFunctionType();
2668
2669 //
David Neto22f144c2017-06-12 14:26:21 -04002670 // Generate OPFunction.
2671 //
2672
2673 // FOps[0] : Result Type ID
2674 // FOps[1] : Function Control
2675 // FOps[2] : Function Type ID
SJWf93f5f32020-05-05 07:27:56 -05002676 SPIRVOperandVec FOps;
David Neto22f144c2017-06-12 14:26:21 -04002677
2678 // Find SPIRV instruction for return type.
SJW01901d92020-05-21 08:58:31 -05002679 FOps << FTy->getReturnType();
David Neto22f144c2017-06-12 14:26:21 -04002680
2681 // Check function attributes for SPIRV Function Control.
2682 uint32_t FuncControl = spv::FunctionControlMaskNone;
2683 if (F.hasFnAttribute(Attribute::AlwaysInline)) {
2684 FuncControl |= spv::FunctionControlInlineMask;
2685 }
2686 if (F.hasFnAttribute(Attribute::NoInline)) {
2687 FuncControl |= spv::FunctionControlDontInlineMask;
2688 }
2689 // TODO: Check llvm attribute for Function Control Pure.
2690 if (F.hasFnAttribute(Attribute::ReadOnly)) {
2691 FuncControl |= spv::FunctionControlPureMask;
2692 }
2693 // TODO: Check llvm attribute for Function Control Const.
2694 if (F.hasFnAttribute(Attribute::ReadNone)) {
2695 FuncControl |= spv::FunctionControlConstMask;
2696 }
2697
SJW01901d92020-05-21 08:58:31 -05002698 FOps << FuncControl;
David Neto22f144c2017-06-12 14:26:21 -04002699
SJW01901d92020-05-21 08:58:31 -05002700 SPIRVID FTyID;
David Neto22f144c2017-06-12 14:26:21 -04002701 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
2702 SmallVector<Type *, 4> NewFuncParamTys;
2703 FunctionType *NewFTy =
2704 FunctionType::get(FTy->getReturnType(), NewFuncParamTys, false);
SJWf93f5f32020-05-05 07:27:56 -05002705 FTyID = getSPIRVType(NewFTy);
David Neto22f144c2017-06-12 14:26:21 -04002706 } else {
David Neto9ed8e2f2018-03-24 06:47:24 -07002707 // Handle regular function with global constant parameters.
David Neto22f144c2017-06-12 14:26:21 -04002708 if (GlobalConstFuncTyMap.count(FTy)) {
SJWf93f5f32020-05-05 07:27:56 -05002709 FTyID = getSPIRVType(GlobalConstFuncTyMap[FTy].first);
David Neto22f144c2017-06-12 14:26:21 -04002710 } else {
SJWf93f5f32020-05-05 07:27:56 -05002711 FTyID = getSPIRVType(FTy);
David Neto22f144c2017-06-12 14:26:21 -04002712 }
2713 }
2714
SJW01901d92020-05-21 08:58:31 -05002715 FOps << FTyID;
David Neto22f144c2017-06-12 14:26:21 -04002716
SJWf93f5f32020-05-05 07:27:56 -05002717 // Generate SPIRV instruction for function.
2718 SPIRVID FID = addSPIRVInst(spv::OpFunction, FOps);
2719 VMap[&F] = FID;
David Neto22f144c2017-06-12 14:26:21 -04002720
SJWf93f5f32020-05-05 07:27:56 -05002721 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
2722 EntryPoints.push_back(std::make_pair(&F, FID));
2723 }
David Neto22f144c2017-06-12 14:26:21 -04002724
David Neto482550a2018-03-24 05:21:07 -07002725 if (clspv::Option::ShowIDs()) {
SJW01901d92020-05-21 08:58:31 -05002726 errs() << "Function " << F.getName() << " is " << FID.get() << "\n";
David Netob05675d2018-02-16 12:37:49 -05002727 }
David Neto22f144c2017-06-12 14:26:21 -04002728
2729 //
2730 // Generate OpFunctionParameter for Normal function.
2731 //
David Neto22f144c2017-06-12 14:26:21 -04002732 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
alan-bakere9308012019-03-15 10:25:13 -04002733
David Neto22f144c2017-06-12 14:26:21 -04002734 // Iterate Argument for name instead of param type from function type.
2735 unsigned ArgIdx = 0;
2736 for (Argument &Arg : F.args()) {
David Neto22f144c2017-06-12 14:26:21 -04002737 // ParamOps[0] : Result Type ID
SJW01901d92020-05-21 08:58:31 -05002738 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04002739
2740 // Find SPIRV instruction for parameter type.
SJW01901d92020-05-21 08:58:31 -05002741 SPIRVID ParamTyID = getSPIRVType(Arg.getType());
David Neto22f144c2017-06-12 14:26:21 -04002742 if (PointerType *PTy = dyn_cast<PointerType>(Arg.getType())) {
2743 if (GlobalConstFuncTyMap.count(FTy)) {
2744 if (ArgIdx == GlobalConstFuncTyMap[FTy].second) {
2745 Type *EleTy = PTy->getPointerElementType();
2746 Type *ArgTy =
2747 PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
SJWf93f5f32020-05-05 07:27:56 -05002748 ParamTyID = getSPIRVType(ArgTy);
David Neto22f144c2017-06-12 14:26:21 -04002749 GlobalConstArgSet.insert(&Arg);
2750 }
2751 }
2752 }
SJW01901d92020-05-21 08:58:31 -05002753 Ops << ParamTyID;
David Neto22f144c2017-06-12 14:26:21 -04002754
2755 // Generate SPIRV instruction for parameter.
SJW01901d92020-05-21 08:58:31 -05002756 SPIRVID param_id = addSPIRVInst(spv::OpFunctionParameter, Ops);
SJWf93f5f32020-05-05 07:27:56 -05002757 VMap[&Arg] = param_id;
2758
2759 if (CalledWithCoherentResource(Arg)) {
2760 // If the arg is passed a coherent resource ever, then decorate this
2761 // parameter with Coherent too.
SJW01901d92020-05-21 08:58:31 -05002762 Ops.clear();
2763 Ops << param_id << spv::DecorationCoherent;
2764 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
SJWf93f5f32020-05-05 07:27:56 -05002765 }
David Neto22f144c2017-06-12 14:26:21 -04002766
2767 ArgIdx++;
2768 }
2769 }
2770}
2771
SJW77b87ad2020-04-21 14:37:52 -05002772void SPIRVProducerPass::GenerateModuleInfo() {
David Neto22f144c2017-06-12 14:26:21 -04002773 EntryPointVecType &EntryPoints = getEntryPointVec();
SJW806a5d82020-07-15 12:51:38 -05002774 auto &EntryPointInterfaces = getEntryPointInterfacesList();
SJW01901d92020-05-21 08:58:31 -05002775 std::vector<SPIRVID> &BuiltinDimVec = getBuiltinDimVec();
David Neto22f144c2017-06-12 14:26:21 -04002776
SJWf93f5f32020-05-05 07:27:56 -05002777 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04002778
SJW01901d92020-05-21 08:58:31 -05002779 for (auto Capability : CapabilitySet) {
David Neto22f144c2017-06-12 14:26:21 -04002780 //
SJW01901d92020-05-21 08:58:31 -05002781 // Generate OpCapability
David Neto22f144c2017-06-12 14:26:21 -04002782 //
2783 // Ops[0] = Capability
SJW01901d92020-05-21 08:58:31 -05002784 addSPIRVInst<kCapabilities>(spv::OpCapability, Capability);
alan-baker5b86ed72019-02-15 08:26:50 -05002785 }
2786
2787 // Always add the storage buffer extension
2788 {
David Neto22f144c2017-06-12 14:26:21 -04002789 //
2790 // Generate OpExtension.
2791 //
2792 // Ops[0] = Name (Literal String)
2793 //
SJWf93f5f32020-05-05 07:27:56 -05002794 addSPIRVInst<kExtensions>(spv::OpExtension,
2795 "SPV_KHR_storage_buffer_storage_class");
alan-baker5b86ed72019-02-15 08:26:50 -05002796 }
David Neto22f144c2017-06-12 14:26:21 -04002797
alan-baker5b86ed72019-02-15 08:26:50 -05002798 if (hasVariablePointers() || hasVariablePointersStorageBuffer()) {
2799 //
2800 // Generate OpExtension.
2801 //
2802 // Ops[0] = Name (Literal String)
2803 //
SJWf93f5f32020-05-05 07:27:56 -05002804 addSPIRVInst<kExtensions>(spv::OpExtension, "SPV_KHR_variable_pointers");
David Neto22f144c2017-06-12 14:26:21 -04002805 }
2806
2807 //
2808 // Generate OpMemoryModel
2809 //
2810 // Memory model for Vulkan will always be GLSL450.
2811
2812 // Ops[0] = Addressing Model
2813 // Ops[1] = Memory Model
2814 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002815 Ops << spv::AddressingModelLogical << spv::MemoryModelGLSL450;
David Neto22f144c2017-06-12 14:26:21 -04002816
SJWf93f5f32020-05-05 07:27:56 -05002817 addSPIRVInst<kMemoryModel>(spv::OpMemoryModel, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002818
2819 //
2820 // Generate OpEntryPoint
2821 //
2822 for (auto EntryPoint : EntryPoints) {
2823 // Ops[0] = Execution Model
2824 // Ops[1] = EntryPoint ID
2825 // Ops[2] = Name (Literal String)
2826 // ...
2827 //
2828 // TODO: Do we need to consider Interface ID for forward references???
2829 Ops.clear();
alan-bakerb6b09dc2018-11-08 16:59:28 -05002830 const StringRef &name = EntryPoint.first->getName();
SJW01901d92020-05-21 08:58:31 -05002831 Ops << spv::ExecutionModelGLCompute << EntryPoint.second << name;
David Neto22f144c2017-06-12 14:26:21 -04002832
SJW806a5d82020-07-15 12:51:38 -05002833 for (auto &Interface : EntryPointInterfaces) {
SJW01901d92020-05-21 08:58:31 -05002834 Ops << Interface;
David Neto22f144c2017-06-12 14:26:21 -04002835 }
2836
SJWf93f5f32020-05-05 07:27:56 -05002837 addSPIRVInst<kEntryPoints>(spv::OpEntryPoint, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002838 }
2839
alan-baker3b609772020-09-03 19:10:17 -04002840 if (BuiltinDimVec.empty()) {
2841 for (auto EntryPoint : EntryPoints) {
2842 const MDNode *MD = dyn_cast<Function>(EntryPoint.first)
2843 ->getMetadata("reqd_work_group_size");
2844 if ((MD != nullptr) && !clspv::Option::NonUniformNDRangeSupported()) {
2845 //
2846 // Generate OpExecutionMode
2847 //
David Neto22f144c2017-06-12 14:26:21 -04002848
alan-baker3b609772020-09-03 19:10:17 -04002849 // Ops[0] = Entry Point ID
2850 // Ops[1] = Execution Mode
2851 // Ops[2] ... Ops[n] = Optional literals according to Execution Mode
2852 Ops.clear();
2853 Ops << EntryPoint.second << spv::ExecutionModeLocalSize;
2854
2855 uint32_t XDim = static_cast<uint32_t>(
2856 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
2857 uint32_t YDim = static_cast<uint32_t>(
2858 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue());
2859 uint32_t ZDim = static_cast<uint32_t>(
2860 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue());
2861
2862 Ops << XDim << YDim << ZDim;
2863
2864 addSPIRVInst<kExecutionModes>(spv::OpExecutionMode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002865 }
David Neto22f144c2017-06-12 14:26:21 -04002866 }
2867 }
2868
2869 //
2870 // Generate OpSource.
2871 //
2872 // Ops[0] = SourceLanguage ID
2873 // Ops[1] = Version (LiteralNum)
2874 //
SJW01901d92020-05-21 08:58:31 -05002875 uint32_t LangID = spv::SourceLanguageUnknown;
2876 uint32_t LangVer = 0;
Kévin Petitf0515712020-01-07 18:29:20 +00002877 switch (clspv::Option::Language()) {
2878 case clspv::Option::SourceLanguage::OpenCL_C_10:
SJW01901d92020-05-21 08:58:31 -05002879 LangID = spv::SourceLanguageOpenCL_C;
2880 LangVer = 100;
Kévin Petitf0515712020-01-07 18:29:20 +00002881 break;
2882 case clspv::Option::SourceLanguage::OpenCL_C_11:
SJW01901d92020-05-21 08:58:31 -05002883 LangID = spv::SourceLanguageOpenCL_C;
2884 LangVer = 110;
Kévin Petitf0515712020-01-07 18:29:20 +00002885 break;
2886 case clspv::Option::SourceLanguage::OpenCL_C_12:
SJW01901d92020-05-21 08:58:31 -05002887 LangID = spv::SourceLanguageOpenCL_C;
2888 LangVer = 120;
Kévin Petitf0515712020-01-07 18:29:20 +00002889 break;
2890 case clspv::Option::SourceLanguage::OpenCL_C_20:
SJW01901d92020-05-21 08:58:31 -05002891 LangID = spv::SourceLanguageOpenCL_C;
2892 LangVer = 200;
Kévin Petitf0515712020-01-07 18:29:20 +00002893 break;
2894 case clspv::Option::SourceLanguage::OpenCL_CPP:
SJW01901d92020-05-21 08:58:31 -05002895 LangID = spv::SourceLanguageOpenCL_CPP;
2896 LangVer = 100;
Kévin Petitf0515712020-01-07 18:29:20 +00002897 break;
2898 default:
Kévin Petitf0515712020-01-07 18:29:20 +00002899 break;
Kévin Petit0fc88042019-04-09 23:25:02 +01002900 }
David Neto22f144c2017-06-12 14:26:21 -04002901
SJW01901d92020-05-21 08:58:31 -05002902 Ops.clear();
2903 Ops << LangID << LangVer;
SJWf93f5f32020-05-05 07:27:56 -05002904 addSPIRVInst<kDebug>(spv::OpSource, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002905
2906 if (!BuiltinDimVec.empty()) {
2907 //
2908 // Generate OpDecorates for x/y/z dimension.
2909 //
2910 // Ops[0] = Target ID
2911 // Ops[1] = Decoration (SpecId)
David Neto257c3892018-04-11 13:19:45 -04002912 // Ops[2] = Specialization Constant ID (Literal Number)
David Neto22f144c2017-06-12 14:26:21 -04002913
2914 // X Dimension
2915 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002916 Ops << BuiltinDimVec[0] << spv::DecorationSpecId << 0;
SJWf93f5f32020-05-05 07:27:56 -05002917 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002918
2919 // Y Dimension
2920 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002921 Ops << BuiltinDimVec[1] << spv::DecorationSpecId << 1;
SJWf93f5f32020-05-05 07:27:56 -05002922 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002923
2924 // Z Dimension
2925 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05002926 Ops << BuiltinDimVec[2] << spv::DecorationSpecId << 2;
SJWf93f5f32020-05-05 07:27:56 -05002927 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Neto22f144c2017-06-12 14:26:21 -04002928 }
2929}
2930
David Netob6e2e062018-04-25 10:32:06 -04002931void SPIRVProducerPass::GenerateEntryPointInitialStores() {
2932 // Work around a driver bug. Initializers on Private variables might not
2933 // work. So the start of the kernel should store the initializer value to the
2934 // variables. Yes, *every* entry point pays this cost if *any* entry point
2935 // uses this builtin. At this point I judge this to be an acceptable tradeoff
2936 // of complexity vs. runtime, for a broken driver.
alan-bakerb6b09dc2018-11-08 16:59:28 -05002937 // TODO(dneto): Remove this at some point once fixed drivers are widely
2938 // available.
SJW01901d92020-05-21 08:58:31 -05002939 if (WorkgroupSizeVarID.isValid()) {
2940 assert(WorkgroupSizeValueID.isValid());
David Netob6e2e062018-04-25 10:32:06 -04002941
SJWf93f5f32020-05-05 07:27:56 -05002942 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05002943 Ops << WorkgroupSizeVarID << WorkgroupSizeValueID;
David Netob6e2e062018-04-25 10:32:06 -04002944
SJWf93f5f32020-05-05 07:27:56 -05002945 addSPIRVInst(spv::OpStore, Ops);
David Netob6e2e062018-04-25 10:32:06 -04002946 }
2947}
2948
David Neto22f144c2017-06-12 14:26:21 -04002949void SPIRVProducerPass::GenerateFuncBody(Function &F) {
David Neto22f144c2017-06-12 14:26:21 -04002950 ValueMapType &VMap = getValueMap();
2951
David Netob6e2e062018-04-25 10:32:06 -04002952 const bool IsKernel = F.getCallingConv() == CallingConv::SPIR_KERNEL;
David Neto22f144c2017-06-12 14:26:21 -04002953
2954 for (BasicBlock &BB : F) {
2955 // Register BasicBlock to ValueMap.
David Neto22f144c2017-06-12 14:26:21 -04002956
2957 //
2958 // Generate OpLabel for Basic Block.
2959 //
SJWf93f5f32020-05-05 07:27:56 -05002960 VMap[&BB] = addSPIRVInst(spv::OpLabel);
David Neto22f144c2017-06-12 14:26:21 -04002961
David Neto6dcd4712017-06-23 11:06:47 -04002962 // OpVariable instructions must come first.
2963 for (Instruction &I : BB) {
alan-baker5b86ed72019-02-15 08:26:50 -05002964 if (auto *alloca = dyn_cast<AllocaInst>(&I)) {
2965 // Allocating a pointer requires variable pointers.
2966 if (alloca->getAllocatedType()->isPointerTy()) {
Diego Novillo3cc8d7a2019-04-10 13:30:34 -04002967 setVariablePointersCapabilities(
2968 alloca->getAllocatedType()->getPointerAddressSpace());
alan-baker5b86ed72019-02-15 08:26:50 -05002969 }
David Neto6dcd4712017-06-23 11:06:47 -04002970 GenerateInstruction(I);
2971 }
2972 }
2973
David Neto22f144c2017-06-12 14:26:21 -04002974 if (&BB == &F.getEntryBlock() && IsKernel) {
David Netob6e2e062018-04-25 10:32:06 -04002975 if (clspv::Option::HackInitializers()) {
2976 GenerateEntryPointInitialStores();
2977 }
David Neto22f144c2017-06-12 14:26:21 -04002978 }
2979
2980 for (Instruction &I : BB) {
David Neto6dcd4712017-06-23 11:06:47 -04002981 if (!isa<AllocaInst>(I)) {
2982 GenerateInstruction(I);
2983 }
David Neto22f144c2017-06-12 14:26:21 -04002984 }
2985 }
2986}
2987
2988spv::Op SPIRVProducerPass::GetSPIRVCmpOpcode(CmpInst *I) {
2989 const std::map<CmpInst::Predicate, spv::Op> Map = {
2990 {CmpInst::ICMP_EQ, spv::OpIEqual},
2991 {CmpInst::ICMP_NE, spv::OpINotEqual},
2992 {CmpInst::ICMP_UGT, spv::OpUGreaterThan},
2993 {CmpInst::ICMP_UGE, spv::OpUGreaterThanEqual},
2994 {CmpInst::ICMP_ULT, spv::OpULessThan},
2995 {CmpInst::ICMP_ULE, spv::OpULessThanEqual},
2996 {CmpInst::ICMP_SGT, spv::OpSGreaterThan},
2997 {CmpInst::ICMP_SGE, spv::OpSGreaterThanEqual},
2998 {CmpInst::ICMP_SLT, spv::OpSLessThan},
2999 {CmpInst::ICMP_SLE, spv::OpSLessThanEqual},
3000 {CmpInst::FCMP_OEQ, spv::OpFOrdEqual},
3001 {CmpInst::FCMP_OGT, spv::OpFOrdGreaterThan},
3002 {CmpInst::FCMP_OGE, spv::OpFOrdGreaterThanEqual},
3003 {CmpInst::FCMP_OLT, spv::OpFOrdLessThan},
3004 {CmpInst::FCMP_OLE, spv::OpFOrdLessThanEqual},
3005 {CmpInst::FCMP_ONE, spv::OpFOrdNotEqual},
3006 {CmpInst::FCMP_UEQ, spv::OpFUnordEqual},
3007 {CmpInst::FCMP_UGT, spv::OpFUnordGreaterThan},
3008 {CmpInst::FCMP_UGE, spv::OpFUnordGreaterThanEqual},
3009 {CmpInst::FCMP_ULT, spv::OpFUnordLessThan},
3010 {CmpInst::FCMP_ULE, spv::OpFUnordLessThanEqual},
3011 {CmpInst::FCMP_UNE, spv::OpFUnordNotEqual}};
3012
3013 assert(0 != Map.count(I->getPredicate()));
3014
3015 return Map.at(I->getPredicate());
3016}
3017
3018spv::Op SPIRVProducerPass::GetSPIRVCastOpcode(Instruction &I) {
3019 const std::map<unsigned, spv::Op> Map{
3020 {Instruction::Trunc, spv::OpUConvert},
3021 {Instruction::ZExt, spv::OpUConvert},
3022 {Instruction::SExt, spv::OpSConvert},
3023 {Instruction::FPToUI, spv::OpConvertFToU},
3024 {Instruction::FPToSI, spv::OpConvertFToS},
3025 {Instruction::UIToFP, spv::OpConvertUToF},
3026 {Instruction::SIToFP, spv::OpConvertSToF},
3027 {Instruction::FPTrunc, spv::OpFConvert},
3028 {Instruction::FPExt, spv::OpFConvert},
3029 {Instruction::BitCast, spv::OpBitcast}};
3030
3031 assert(0 != Map.count(I.getOpcode()));
3032
3033 return Map.at(I.getOpcode());
3034}
3035
3036spv::Op SPIRVProducerPass::GetSPIRVBinaryOpcode(Instruction &I) {
Kévin Petit24272b62018-10-18 19:16:12 +00003037 if (I.getType()->isIntOrIntVectorTy(1)) {
David Neto22f144c2017-06-12 14:26:21 -04003038 switch (I.getOpcode()) {
3039 default:
3040 break;
3041 case Instruction::Or:
3042 return spv::OpLogicalOr;
3043 case Instruction::And:
3044 return spv::OpLogicalAnd;
3045 case Instruction::Xor:
3046 return spv::OpLogicalNotEqual;
3047 }
3048 }
3049
alan-bakerb6b09dc2018-11-08 16:59:28 -05003050 const std::map<unsigned, spv::Op> Map{
David Neto22f144c2017-06-12 14:26:21 -04003051 {Instruction::Add, spv::OpIAdd},
3052 {Instruction::FAdd, spv::OpFAdd},
3053 {Instruction::Sub, spv::OpISub},
3054 {Instruction::FSub, spv::OpFSub},
3055 {Instruction::Mul, spv::OpIMul},
3056 {Instruction::FMul, spv::OpFMul},
3057 {Instruction::UDiv, spv::OpUDiv},
3058 {Instruction::SDiv, spv::OpSDiv},
3059 {Instruction::FDiv, spv::OpFDiv},
3060 {Instruction::URem, spv::OpUMod},
3061 {Instruction::SRem, spv::OpSRem},
3062 {Instruction::FRem, spv::OpFRem},
3063 {Instruction::Or, spv::OpBitwiseOr},
3064 {Instruction::Xor, spv::OpBitwiseXor},
3065 {Instruction::And, spv::OpBitwiseAnd},
3066 {Instruction::Shl, spv::OpShiftLeftLogical},
3067 {Instruction::LShr, spv::OpShiftRightLogical},
3068 {Instruction::AShr, spv::OpShiftRightArithmetic}};
3069
3070 assert(0 != Map.count(I.getOpcode()));
3071
3072 return Map.at(I.getOpcode());
3073}
3074
SJW806a5d82020-07-15 12:51:38 -05003075SPIRVID SPIRVProducerPass::getSPIRVBuiltin(spv::BuiltIn BID,
3076 spv::Capability Cap) {
3077 SPIRVID RID;
3078
3079 auto ii = BuiltinConstantMap.find(BID);
3080
3081 if (ii != BuiltinConstantMap.end()) {
3082 return ii->second;
3083 } else {
SJW806a5d82020-07-15 12:51:38 -05003084 addCapability(Cap);
3085
3086 Type *type = PointerType::get(IntegerType::get(module->getContext(), 32),
3087 AddressSpace::Input);
3088
3089 RID = addSPIRVGlobalVariable(getSPIRVType(type), spv::StorageClassInput);
3090
3091 BuiltinConstantMap[BID] = RID;
3092
3093 //
3094 // Generate OpDecorate.
3095 //
3096 // Ops[0] : target
3097 // Ops[1] : decoration
3098 // Ops[2] : SpecId
3099 SPIRVOperandVec Ops;
3100 Ops << RID << spv::DecorationBuiltIn << static_cast<int>(BID);
3101
3102 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
3103 }
3104
3105 return RID;
3106}
3107
3108SPIRVID
3109SPIRVProducerPass::GenerateClspvInstruction(CallInst *Call,
3110 const FunctionInfo &FuncInfo) {
3111 SPIRVID RID;
3112
3113 switch (FuncInfo.getType()) {
3114 case Builtins::kClspvCompositeConstruct:
3115 RID = addSPIRVPlaceholder(Call);
3116 break;
3117 case Builtins::kClspvResource: {
3118 if (ResourceVarDeferredLoadCalls.count(Call) && Call->hasNUsesOrMore(1)) {
3119 // Generate an OpLoad
3120 SPIRVOperandVec Ops;
3121
3122 Ops << Call->getType()->getPointerElementType()
3123 << ResourceVarDeferredLoadCalls[Call];
3124
3125 RID = addSPIRVInst(spv::OpLoad, Ops);
3126
3127 } else {
3128 // This maps to an OpVariable we've already generated.
3129 // No code is generated for the call.
3130 }
3131 break;
3132 }
3133 case Builtins::kClspvLocal: {
3134 // Don't codegen an instruction here, but instead map this call directly
3135 // to the workgroup variable id.
3136 int spec_id = static_cast<int>(
3137 cast<ConstantInt>(Call->getOperand(0))->getSExtValue());
3138 const auto &info = LocalSpecIdInfoMap[spec_id];
3139 RID = info.variable_id;
3140 break;
3141 }
3142 case Builtins::kClspvSamplerVarLiteral: {
3143 // Sampler initializers become a load of the corresponding sampler.
3144 // Map this to a load from the variable.
3145 const auto third_param = static_cast<unsigned>(
3146 dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue());
3147 auto sampler_value = third_param;
3148 if (clspv::Option::UseSamplerMap()) {
3149 sampler_value = getSamplerMap()[third_param].first;
3150 }
3151
3152 // Generate an OpLoad
3153 SPIRVOperandVec Ops;
3154
3155 Ops << SamplerTy->getPointerElementType()
3156 << SamplerLiteralToIDMap[sampler_value];
3157
3158 RID = addSPIRVInst(spv::OpLoad, Ops);
3159 break;
3160 }
3161 case Builtins::kSpirvAtomicXor: {
3162 // Handle SPIR-V intrinsics
3163 SPIRVOperandVec Ops;
3164
3165 if (!Call->getType()->isVoidTy()) {
3166 Ops << Call->getType();
3167 }
3168
3169 for (unsigned i = 0; i < Call->getNumArgOperands(); i++) {
3170 Ops << Call->getArgOperand(i);
3171 }
3172
3173 RID = addSPIRVInst(spv::OpAtomicXor, Ops);
3174 break;
3175 }
3176 case Builtins::kSpirvOp: {
3177 // Handle SPIR-V intrinsics
3178 auto *arg0 = dyn_cast<ConstantInt>(Call->getArgOperand(0));
3179 spv::Op opcode = static_cast<spv::Op>(arg0->getZExtValue());
3180 if (opcode != spv::OpNop) {
3181 SPIRVOperandVec Ops;
3182
3183 if (!Call->getType()->isVoidTy()) {
3184 Ops << Call->getType();
3185 }
3186
3187 for (unsigned i = 1; i < Call->getNumArgOperands(); i++) {
3188 Ops << Call->getArgOperand(i);
3189 }
3190
3191 RID = addSPIRVInst(opcode, Ops);
3192 }
3193 break;
3194 }
3195 case Builtins::kSpirvCopyMemory: {
3196 //
3197 // Generate OpCopyMemory.
3198 //
3199
3200 // Ops[0] = Dst ID
3201 // Ops[1] = Src ID
3202 // Ops[2] = Memory Access
3203 // Ops[3] = Alignment
3204
3205 auto IsVolatile =
3206 dyn_cast<ConstantInt>(Call->getArgOperand(3))->getZExtValue() != 0;
3207
3208 auto VolatileMemoryAccess = (IsVolatile) ? spv::MemoryAccessVolatileMask
3209 : spv::MemoryAccessMaskNone;
3210
3211 auto MemoryAccess = VolatileMemoryAccess | spv::MemoryAccessAlignedMask;
3212
3213 auto Alignment =
3214 dyn_cast<ConstantInt>(Call->getArgOperand(2))->getZExtValue();
3215
3216 SPIRVOperandVec Ops;
3217 Ops << Call->getArgOperand(0) << Call->getArgOperand(1) << MemoryAccess
3218 << static_cast<uint32_t>(Alignment);
3219
3220 RID = addSPIRVInst(spv::OpCopyMemory, Ops);
3221 break;
3222 }
3223 default:
3224 llvm_unreachable("Unknown CLSPV Instruction");
3225 break;
3226 }
3227 return RID;
3228}
3229
3230SPIRVID
3231SPIRVProducerPass::GenerateImageInstruction(CallInst *Call,
3232 const FunctionInfo &FuncInfo) {
3233 SPIRVID RID;
3234
3235 LLVMContext &Context = module->getContext();
3236 switch (FuncInfo.getType()) {
3237 case Builtins::kReadImagef:
3238 case Builtins::kReadImageh:
3239 case Builtins::kReadImagei:
3240 case Builtins::kReadImageui: {
3241 // read_image is converted to OpSampledImage and OpImageSampleExplicitLod.
3242 // Additionally, OpTypeSampledImage is generated.
alan-bakerf6bc8252020-09-23 14:58:55 -04003243 const auto image_ty = Call->getArgOperand(0)->getType();
SJW806a5d82020-07-15 12:51:38 -05003244 const auto &pi = FuncInfo.getParameter(1);
3245 if (pi.isSampler()) {
3246 //
3247 // Generate OpSampledImage.
3248 //
3249 // Ops[0] = Result Type ID
3250 // Ops[1] = Image ID
3251 // Ops[2] = Sampler ID
3252 //
3253 SPIRVOperandVec Ops;
3254
3255 Value *Image = Call->getArgOperand(0);
3256 Value *Sampler = Call->getArgOperand(1);
3257 Value *Coordinate = Call->getArgOperand(2);
3258
3259 TypeMapType &OpImageTypeMap = getImageTypeMap();
3260 Type *ImageTy = Image->getType()->getPointerElementType();
3261 SPIRVID ImageTyID = OpImageTypeMap[ImageTy];
3262
3263 Ops << ImageTyID << Image << Sampler;
3264
3265 SPIRVID SampledImageID = addSPIRVInst(spv::OpSampledImage, Ops);
3266
3267 //
3268 // Generate OpImageSampleExplicitLod.
3269 //
3270 // Ops[0] = Result Type ID
3271 // Ops[1] = Sampled Image ID
3272 // Ops[2] = Coordinate ID
3273 // Ops[3] = Image Operands Type ID
3274 // Ops[4] ... Ops[n] = Operands ID
3275 //
3276 Ops.clear();
3277
3278 const bool is_int_image = IsIntImageType(Image->getType());
3279 SPIRVID result_type;
3280 if (is_int_image) {
3281 result_type = v4int32ID;
3282 } else {
3283 result_type = getSPIRVType(Call->getType());
3284 }
3285
3286 Constant *CstFP0 = ConstantFP::get(Context, APFloat(0.0f));
3287 Ops << result_type << SampledImageID << Coordinate
3288 << spv::ImageOperandsLodMask << CstFP0;
3289
3290 RID = addSPIRVInst(spv::OpImageSampleExplicitLod, Ops);
3291
3292 if (is_int_image) {
3293 // Generate the bitcast.
3294 Ops.clear();
3295 Ops << Call->getType() << RID;
3296 RID = addSPIRVInst(spv::OpBitcast, Ops);
3297 }
alan-bakerf6bc8252020-09-23 14:58:55 -04003298 } else if (IsStorageImageType(image_ty)) {
3299 // read_image on a storage image is mapped to OpImageRead.
3300 Value *Image = Call->getArgOperand(0);
3301 Value *Coordinate = Call->getArgOperand(1);
3302
3303 //
3304 // Generate OpImageRead
3305 //
3306 // Ops[0] = Result Type ID
3307 // Ops[1] = Image ID
3308 // Ops[2] = Coordinate
3309 // No optional image operands.
3310 //
3311 SPIRVOperandVec Ops;
3312
3313 const bool is_int_image = IsIntImageType(Image->getType());
3314 SPIRVID result_type;
3315 if (is_int_image) {
3316 result_type = v4int32ID;
3317 } else {
3318 result_type = getSPIRVType(Call->getType());
3319 }
3320
3321 Ops << result_type << Image << Coordinate;
3322 RID = addSPIRVInst(spv::OpImageRead, Ops);
3323
3324 if (is_int_image) {
3325 // Generate the bitcast.
3326 Ops.clear();
3327 Ops << Call->getType() << RID;
3328 RID = addSPIRVInst(spv::OpBitcast, Ops);
3329 }
3330
3331 // OpImageRead requires StorageImageReadWithoutFormat.
3332 addCapability(spv::CapabilityStorageImageReadWithoutFormat);
SJW806a5d82020-07-15 12:51:38 -05003333 } else {
alan-bakerf6bc8252020-09-23 14:58:55 -04003334 // read_image on a sampled image (without a sampler) is mapped to
3335 // OpImageFetch.
SJW806a5d82020-07-15 12:51:38 -05003336 Value *Image = Call->getArgOperand(0);
3337 Value *Coordinate = Call->getArgOperand(1);
3338
3339 //
3340 // Generate OpImageFetch
3341 //
3342 // Ops[0] = Result Type ID
3343 // Ops[1] = Image ID
3344 // Ops[2] = Coordinate ID
3345 // Ops[3] = Lod
3346 // Ops[4] = 0
3347 //
3348 SPIRVOperandVec Ops;
3349
3350 const bool is_int_image = IsIntImageType(Image->getType());
3351 SPIRVID result_type;
3352 if (is_int_image) {
3353 result_type = v4int32ID;
3354 } else {
3355 result_type = getSPIRVType(Call->getType());
3356 }
3357
3358 Ops << result_type << Image << Coordinate << spv::ImageOperandsLodMask
3359 << getSPIRVInt32Constant(0);
3360
3361 RID = addSPIRVInst(spv::OpImageFetch, Ops);
3362
3363 if (is_int_image) {
3364 // Generate the bitcast.
3365 Ops.clear();
3366 Ops << Call->getType() << RID;
3367 RID = addSPIRVInst(spv::OpBitcast, Ops);
3368 }
3369 }
3370 break;
3371 }
3372
3373 case Builtins::kWriteImagef:
3374 case Builtins::kWriteImageh:
3375 case Builtins::kWriteImagei:
3376 case Builtins::kWriteImageui: {
3377 // write_image is mapped to OpImageWrite.
3378 //
3379 // Generate OpImageWrite.
3380 //
3381 // Ops[0] = Image ID
3382 // Ops[1] = Coordinate ID
3383 // Ops[2] = Texel ID
3384 // Ops[3] = (Optional) Image Operands Type (Literal Number)
3385 // Ops[4] ... Ops[n] = (Optional) Operands ID
3386 //
3387 SPIRVOperandVec Ops;
3388
3389 Value *Image = Call->getArgOperand(0);
3390 Value *Coordinate = Call->getArgOperand(1);
3391 Value *Texel = Call->getArgOperand(2);
3392
3393 SPIRVID TexelID = getSPIRVValue(Texel);
3394
3395 const bool is_int_image = IsIntImageType(Image->getType());
3396 if (is_int_image) {
3397 // Generate a bitcast to v4int and use it as the texel value.
3398 Ops << v4int32ID << TexelID;
3399 TexelID = addSPIRVInst(spv::OpBitcast, Ops);
3400 Ops.clear();
3401 }
3402 Ops << Image << Coordinate << TexelID;
SJW806a5d82020-07-15 12:51:38 -05003403 RID = addSPIRVInst(spv::OpImageWrite, Ops);
alan-bakerf6bc8252020-09-23 14:58:55 -04003404
3405 // Image writes require StorageImageWriteWithoutFormat.
3406 addCapability(spv::CapabilityStorageImageWriteWithoutFormat);
SJW806a5d82020-07-15 12:51:38 -05003407 break;
3408 }
3409
3410 case Builtins::kGetImageHeight:
3411 case Builtins::kGetImageWidth:
3412 case Builtins::kGetImageDepth:
3413 case Builtins::kGetImageDim: {
3414 // get_image_* is mapped to OpImageQuerySize or OpImageQuerySizeLod
3415 addCapability(spv::CapabilityImageQuery);
3416
3417 //
3418 // Generate OpImageQuerySize[Lod]
3419 //
3420 // Ops[0] = Image ID
3421 //
3422 // Result type has components equal to the dimensionality of the image,
3423 // plus 1 if the image is arrayed.
3424 //
3425 // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0]
3426 SPIRVOperandVec Ops;
3427
3428 // Implement:
3429 // %sizes = OpImageQuerySize[Lod] %uint[2|3|4] %im [%uint_0]
3430 SPIRVID SizesTypeID;
3431
3432 Value *Image = Call->getArgOperand(0);
3433 const uint32_t dim = ImageDimensionality(Image->getType());
3434 const uint32_t components =
3435 dim + (IsArrayImageType(Image->getType()) ? 1 : 0);
3436 if (components == 1) {
3437 SizesTypeID = getSPIRVType(Type::getInt32Ty(Context));
3438 } else {
3439 SizesTypeID = getSPIRVType(
3440 FixedVectorType::get(Type::getInt32Ty(Context), components));
3441 }
3442 Ops << SizesTypeID << Image;
3443 spv::Op query_opcode = spv::OpImageQuerySize;
3444 if (IsSampledImageType(Image->getType())) {
3445 query_opcode = spv::OpImageQuerySizeLod;
3446 // Need explicit 0 for Lod operand.
3447 Ops << getSPIRVInt32Constant(0);
3448 }
3449
3450 RID = addSPIRVInst(query_opcode, Ops);
3451
3452 // May require an extra instruction to create the appropriate result of
3453 // the builtin function.
3454 if (FuncInfo.getType() == Builtins::kGetImageDim) {
3455 if (dim == 3) {
3456 // get_image_dim returns an int4 for 3D images.
3457 //
3458
3459 // Implement:
3460 // %result = OpCompositeConstruct %uint4 %sizes %uint_0
3461 Ops.clear();
3462 Ops << FixedVectorType::get(Type::getInt32Ty(Context), 4) << RID
3463 << getSPIRVInt32Constant(0);
3464
3465 RID = addSPIRVInst(spv::OpCompositeConstruct, Ops);
3466 } else if (dim != components) {
3467 // get_image_dim return an int2 regardless of the arrayedness of the
3468 // image. If the image is arrayed an element must be dropped from the
3469 // query result.
3470 //
3471
3472 // Implement:
3473 // %result = OpVectorShuffle %uint2 %sizes %sizes 0 1
3474 Ops.clear();
3475 Ops << FixedVectorType::get(Type::getInt32Ty(Context), 2) << RID << RID
3476 << 0 << 1;
3477
3478 RID = addSPIRVInst(spv::OpVectorShuffle, Ops);
3479 }
3480 } else if (components > 1) {
3481 // Implement:
3482 // %result = OpCompositeExtract %uint %sizes <component number>
3483 Ops.clear();
3484 Ops << Call->getType() << RID;
3485
3486 uint32_t component = 0;
3487 if (FuncInfo.getType() == Builtins::kGetImageHeight)
3488 component = 1;
3489 else if (FuncInfo.getType() == Builtins::kGetImageDepth)
3490 component = 2;
3491 Ops << component;
3492
3493 RID = addSPIRVInst(spv::OpCompositeExtract, Ops);
3494 }
3495 break;
3496 }
3497 default:
3498 llvm_unreachable("Unsupported Image builtin");
3499 }
3500
3501 return RID;
3502}
3503
3504SPIRVID
3505SPIRVProducerPass::GenerateSubgroupInstruction(CallInst *Call,
3506 const FunctionInfo &FuncInfo) {
3507 SPIRVID RID;
3508
3509 // requires SPIRV version 1.3 or greater
3510 if (SpvVersion() != SPIRVVersion::SPIRV_1_3) {
3511 // llvm_unreachable("SubGroups extension requires SPIRV 1.3 or greater");
3512 // TODO(sjw): error out gracefully
3513 }
3514
3515 auto loadBuiltin = [this, Call](spv::BuiltIn spvBI,
3516 spv::Capability spvCap =
3517 spv::CapabilityGroupNonUniform) {
3518 SPIRVOperandVec Ops;
3519 Ops << Call->getType() << this->getSPIRVBuiltin(spvBI, spvCap);
3520
3521 return addSPIRVInst(spv::OpLoad, Ops);
3522 };
3523
3524 spv::Op op = spv::OpNop;
3525 switch (FuncInfo.getType()) {
3526 case Builtins::kGetSubGroupSize:
3527 return loadBuiltin(spv::BuiltInSubgroupSize);
3528 case Builtins::kGetNumSubGroups:
3529 return loadBuiltin(spv::BuiltInNumSubgroups);
3530 case Builtins::kGetSubGroupId:
3531 return loadBuiltin(spv::BuiltInSubgroupId);
3532 case Builtins::kGetSubGroupLocalId:
3533 return loadBuiltin(spv::BuiltInSubgroupLocalInvocationId);
3534
3535 case Builtins::kSubGroupBroadcast:
3536 if (SpvVersion() < SPIRVVersion::SPIRV_1_5 &&
3537 !dyn_cast<ConstantInt>(Call->getOperand(1))) {
3538 llvm_unreachable("sub_group_broadcast requires constant lane Id for "
3539 "SPIRV version < 1.5");
3540 }
3541 addCapability(spv::CapabilityGroupNonUniformBallot);
3542 op = spv::OpGroupNonUniformBroadcast;
3543 break;
3544
3545 case Builtins::kSubGroupAll:
3546 addCapability(spv::CapabilityGroupNonUniformVote);
3547 op = spv::OpGroupNonUniformAll;
3548 break;
3549 case Builtins::kSubGroupAny:
3550 addCapability(spv::CapabilityGroupNonUniformVote);
3551 op = spv::OpGroupNonUniformAny;
3552 break;
3553 case Builtins::kSubGroupReduceAdd:
3554 case Builtins::kSubGroupScanExclusiveAdd:
3555 case Builtins::kSubGroupScanInclusiveAdd: {
3556 addCapability(spv::CapabilityGroupNonUniformArithmetic);
3557 if (FuncInfo.getParameter(0).type_id == Type::IntegerTyID) {
3558 op = spv::OpGroupNonUniformIAdd;
3559 } else {
3560 op = spv::OpGroupNonUniformFAdd;
3561 }
3562 break;
3563 }
3564 case Builtins::kSubGroupReduceMin:
3565 case Builtins::kSubGroupScanExclusiveMin:
3566 case Builtins::kSubGroupScanInclusiveMin: {
3567 addCapability(spv::CapabilityGroupNonUniformArithmetic);
3568 auto &param = FuncInfo.getParameter(0);
3569 if (param.type_id == Type::IntegerTyID) {
3570 op = param.is_signed ? spv::OpGroupNonUniformSMin
3571 : spv::OpGroupNonUniformUMin;
3572 } else {
3573 op = spv::OpGroupNonUniformFMin;
3574 }
3575 break;
3576 }
3577 case Builtins::kSubGroupReduceMax:
3578 case Builtins::kSubGroupScanExclusiveMax:
3579 case Builtins::kSubGroupScanInclusiveMax: {
3580 addCapability(spv::CapabilityGroupNonUniformArithmetic);
3581 auto &param = FuncInfo.getParameter(0);
3582 if (param.type_id == Type::IntegerTyID) {
3583 op = param.is_signed ? spv::OpGroupNonUniformSMax
3584 : spv::OpGroupNonUniformUMax;
3585 } else {
3586 op = spv::OpGroupNonUniformFMax;
3587 }
3588 break;
3589 }
3590
3591 case Builtins::kGetEnqueuedNumSubGroups:
3592 // TODO(sjw): requires CapabilityKernel (incompatible with Shader)
3593 case Builtins::kGetMaxSubGroupSize:
3594 // TODO(sjw): use SpecConstant, capability Kernel (incompatible with Shader)
3595 case Builtins::kSubGroupBarrier:
3596 case Builtins::kSubGroupReserveReadPipe:
3597 case Builtins::kSubGroupReserveWritePipe:
3598 case Builtins::kSubGroupCommitReadPipe:
3599 case Builtins::kSubGroupCommitWritePipe:
3600 case Builtins::kGetKernelSubGroupCountForNdrange:
3601 case Builtins::kGetKernelMaxSubGroupSizeForNdrange:
3602 default:
3603 Call->print(errs());
3604 llvm_unreachable("Unsupported sub_group operation");
3605 break;
3606 }
3607
3608 assert(op != spv::OpNop);
3609
3610 SPIRVOperandVec Operands;
3611
3612 //
3613 // Generate OpGroupNonUniform*
3614 //
3615 // Ops[0] = Result Type ID
3616 // Ops[1] = ScopeSubgroup
3617 // Ops[2] = Value ID
3618 // Ops[3] = Local ID
3619
3620 // The result type.
3621 Operands << Call->getType();
3622
3623 // Subgroup Scope
3624 Operands << getSPIRVInt32Constant(spv::ScopeSubgroup);
3625
3626 switch (FuncInfo.getType()) {
3627 case Builtins::kSubGroupReduceAdd:
3628 case Builtins::kSubGroupReduceMin:
3629 case Builtins::kSubGroupReduceMax:
3630 Operands << spv::GroupOperationReduce;
3631 break;
3632 case Builtins::kSubGroupScanExclusiveAdd:
3633 case Builtins::kSubGroupScanExclusiveMin:
3634 case Builtins::kSubGroupScanExclusiveMax:
3635 Operands << spv::GroupOperationExclusiveScan;
3636 break;
3637 case Builtins::kSubGroupScanInclusiveAdd:
3638 case Builtins::kSubGroupScanInclusiveMin:
3639 case Builtins::kSubGroupScanInclusiveMax:
3640 Operands << spv::GroupOperationInclusiveScan;
3641 break;
3642 default:
3643 break;
3644 }
3645
3646 for (Use &use : Call->arg_operands()) {
3647 Operands << use.get();
3648 }
3649
3650 return addSPIRVInst(op, Operands);
3651}
3652
3653SPIRVID SPIRVProducerPass::GenerateInstructionFromCall(CallInst *Call) {
3654 LLVMContext &Context = module->getContext();
3655
3656 auto &func_info = Builtins::Lookup(Call->getCalledFunction());
3657 auto func_type = func_info.getType();
3658
3659 if (BUILTIN_IN_GROUP(func_type, Clspv)) {
3660 return GenerateClspvInstruction(Call, func_info);
3661 } else if (BUILTIN_IN_GROUP(func_type, Image)) {
3662 return GenerateImageInstruction(Call, func_info);
3663 } else if (BUILTIN_IN_GROUP(func_type, SubgroupsKHR)) {
3664 return GenerateSubgroupInstruction(Call, func_info);
3665 }
3666
3667 SPIRVID RID;
3668
3669 switch (func_type) {
3670 case Builtins::kPopcount: {
3671 //
3672 // Generate OpBitCount
3673 //
3674 // Ops[0] = Result Type ID
3675 // Ops[1] = Base ID
3676 SPIRVOperandVec Ops;
3677 Ops << Call->getType() << Call->getOperand(0);
3678
3679 RID = addSPIRVInst(spv::OpBitCount, Ops);
3680 break;
3681 }
3682 default: {
3683 glsl::ExtInst EInst = getDirectOrIndirectExtInstEnum(func_info);
3684
3685 if (EInst) {
3686 SPIRVID ExtInstImportID = getOpExtInstImportID();
3687
3688 //
3689 // Generate OpExtInst.
3690 //
3691
3692 // Ops[0] = Result Type ID
3693 // Ops[1] = Set ID (OpExtInstImport ID)
3694 // Ops[2] = Instruction Number (Literal Number)
3695 // Ops[3] ... Ops[n] = Operand 1, ... , Operand n
3696 SPIRVOperandVec Ops;
3697
3698 Ops << Call->getType() << ExtInstImportID << EInst;
3699
3700 for (auto &use : Call->arg_operands()) {
3701 Ops << use.get();
3702 }
3703
3704 RID = addSPIRVInst(spv::OpExtInst, Ops);
3705
3706 const auto IndirectExtInst = getIndirectExtInstEnum(func_info);
3707 if (IndirectExtInst != kGlslExtInstBad) {
SJW806a5d82020-07-15 12:51:38 -05003708 // Generate one more instruction that uses the result of the extended
3709 // instruction. Its result id is one more than the id of the
3710 // extended instruction.
3711 auto generate_extra_inst = [this, &Context, &Call,
3712 &RID](spv::Op opcode, Constant *constant) {
3713 //
3714 // Generate instruction like:
3715 // result = opcode constant <extinst-result>
3716 //
3717 // Ops[0] = Result Type ID
3718 // Ops[1] = Operand 0 ;; the constant, suitably splatted
3719 // Ops[2] = Operand 1 ;; the result of the extended instruction
3720 SPIRVOperandVec Ops;
3721
3722 Type *resultTy = Call->getType();
3723
3724 if (auto *vectorTy = dyn_cast<VectorType>(resultTy)) {
alan-baker931253b2020-08-20 17:15:38 -04003725 constant =
3726 ConstantVector::getSplat(vectorTy->getElementCount(), constant);
SJW806a5d82020-07-15 12:51:38 -05003727 }
3728 Ops << resultTy << constant << RID;
3729
3730 RID = addSPIRVInst(opcode, Ops);
3731 };
3732
3733 auto IntTy = Type::getInt32Ty(Context);
3734 switch (IndirectExtInst) {
3735 case glsl::ExtInstFindUMsb: // Implementing clz
3736 generate_extra_inst(spv::OpISub, ConstantInt::get(IntTy, 31));
3737 break;
3738 case glsl::ExtInstAcos: // Implementing acospi
3739 case glsl::ExtInstAsin: // Implementing asinpi
3740 case glsl::ExtInstAtan: // Implementing atanpi
3741 case glsl::ExtInstAtan2: // Implementing atan2pi
3742 generate_extra_inst(
3743 spv::OpFMul,
3744 ConstantFP::get(Type::getFloatTy(Context), kOneOverPi));
3745 break;
3746
3747 default:
3748 assert(false && "internally inconsistent");
3749 }
3750 }
3751 } else {
SJW806a5d82020-07-15 12:51:38 -05003752 // A real function call (not builtin)
3753 // Call instruction is deferred because it needs function's ID.
3754 RID = addSPIRVPlaceholder(Call);
3755 }
3756
3757 break;
3758 }
3759 }
3760
3761 return RID;
3762}
3763
David Neto22f144c2017-06-12 14:26:21 -04003764void SPIRVProducerPass::GenerateInstruction(Instruction &I) {
David Neto22f144c2017-06-12 14:26:21 -04003765 ValueMapType &VMap = getValueMap();
SJW806a5d82020-07-15 12:51:38 -05003766 LLVMContext &Context = module->getContext();
David Neto22f144c2017-06-12 14:26:21 -04003767
SJW806a5d82020-07-15 12:51:38 -05003768 SPIRVID RID;
David Neto22f144c2017-06-12 14:26:21 -04003769
3770 switch (I.getOpcode()) {
3771 default: {
3772 if (Instruction::isCast(I.getOpcode())) {
3773 //
3774 // Generate SPIRV instructions for cast operators.
3775 //
3776
David Netod2de94a2017-08-28 17:27:47 -04003777 auto Ty = I.getType();
David Neto22f144c2017-06-12 14:26:21 -04003778 auto OpTy = I.getOperand(0)->getType();
David Netod2de94a2017-08-28 17:27:47 -04003779 auto toI8 = Ty == Type::getInt8Ty(Context);
3780 auto fromI32 = OpTy == Type::getInt32Ty(Context);
David Neto22f144c2017-06-12 14:26:21 -04003781 // Handle zext, sext and uitofp with i1 type specially.
3782 if ((I.getOpcode() == Instruction::ZExt ||
3783 I.getOpcode() == Instruction::SExt ||
3784 I.getOpcode() == Instruction::UIToFP) &&
alan-bakerb6b09dc2018-11-08 16:59:28 -05003785 OpTy->isIntOrIntVectorTy(1)) {
David Neto22f144c2017-06-12 14:26:21 -04003786 //
3787 // Generate OpSelect.
3788 //
3789
3790 // Ops[0] = Result Type ID
3791 // Ops[1] = Condition ID
3792 // Ops[2] = True Constant ID
3793 // Ops[3] = False Constant ID
SJWf93f5f32020-05-05 07:27:56 -05003794 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003795
SJW01901d92020-05-21 08:58:31 -05003796 Ops << I.getType() << I.getOperand(0);
David Neto22f144c2017-06-12 14:26:21 -04003797
David Neto22f144c2017-06-12 14:26:21 -04003798 if (I.getOpcode() == Instruction::ZExt) {
SJW01901d92020-05-21 08:58:31 -05003799 Ops << ConstantInt::get(I.getType(), 1);
David Neto22f144c2017-06-12 14:26:21 -04003800 } else if (I.getOpcode() == Instruction::SExt) {
SJW01901d92020-05-21 08:58:31 -05003801 Ops << ConstantInt::getSigned(I.getType(), -1);
David Neto22f144c2017-06-12 14:26:21 -04003802 } else {
SJW01901d92020-05-21 08:58:31 -05003803 Ops << ConstantFP::get(Context, APFloat(1.0f));
David Neto22f144c2017-06-12 14:26:21 -04003804 }
David Neto22f144c2017-06-12 14:26:21 -04003805
David Neto22f144c2017-06-12 14:26:21 -04003806 if (I.getOpcode() == Instruction::ZExt) {
SJW01901d92020-05-21 08:58:31 -05003807 Ops << Constant::getNullValue(I.getType());
David Neto22f144c2017-06-12 14:26:21 -04003808 } else if (I.getOpcode() == Instruction::SExt) {
SJW01901d92020-05-21 08:58:31 -05003809 Ops << Constant::getNullValue(I.getType());
David Neto22f144c2017-06-12 14:26:21 -04003810 } else {
SJW01901d92020-05-21 08:58:31 -05003811 Ops << ConstantFP::get(Context, APFloat(0.0f));
David Neto22f144c2017-06-12 14:26:21 -04003812 }
David Neto22f144c2017-06-12 14:26:21 -04003813
SJWf93f5f32020-05-05 07:27:56 -05003814 RID = addSPIRVInst(spv::OpSelect, Ops);
alan-bakerb39c8262019-03-08 14:03:37 -05003815 } else if (!clspv::Option::Int8Support() &&
3816 I.getOpcode() == Instruction::Trunc && fromI32 && toI8) {
David Netod2de94a2017-08-28 17:27:47 -04003817 // The SPIR-V target type is a 32-bit int. Keep only the bottom
3818 // 8 bits.
3819 // Before:
3820 // %result = trunc i32 %a to i8
3821 // After
3822 // %result = OpBitwiseAnd %uint %a %uint_255
3823
SJWf93f5f32020-05-05 07:27:56 -05003824 SPIRVOperandVec Ops;
David Netod2de94a2017-08-28 17:27:47 -04003825
SJW806a5d82020-07-15 12:51:38 -05003826 Ops << OpTy << I.getOperand(0) << getSPIRVInt32Constant(255);
David Netod2de94a2017-08-28 17:27:47 -04003827
SJWf93f5f32020-05-05 07:27:56 -05003828 RID = addSPIRVInst(spv::OpBitwiseAnd, Ops);
David Neto22f144c2017-06-12 14:26:21 -04003829 } else {
3830 // Ops[0] = Result Type ID
3831 // Ops[1] = Source Value ID
SJWf93f5f32020-05-05 07:27:56 -05003832 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003833
SJW01901d92020-05-21 08:58:31 -05003834 Ops << I.getType() << I.getOperand(0);
David Neto22f144c2017-06-12 14:26:21 -04003835
SJWf93f5f32020-05-05 07:27:56 -05003836 RID = addSPIRVInst(GetSPIRVCastOpcode(I), Ops);
David Neto22f144c2017-06-12 14:26:21 -04003837 }
3838 } else if (isa<BinaryOperator>(I)) {
3839 //
3840 // Generate SPIRV instructions for binary operators.
3841 //
3842
3843 // Handle xor with i1 type specially.
3844 if (I.getOpcode() == Instruction::Xor &&
3845 I.getType() == Type::getInt1Ty(Context) &&
Kévin Petit24272b62018-10-18 19:16:12 +00003846 ((isa<ConstantInt>(I.getOperand(0)) &&
3847 !cast<ConstantInt>(I.getOperand(0))->isZero()) ||
3848 (isa<ConstantInt>(I.getOperand(1)) &&
3849 !cast<ConstantInt>(I.getOperand(1))->isZero()))) {
David Neto22f144c2017-06-12 14:26:21 -04003850 //
3851 // Generate OpLogicalNot.
3852 //
3853 // Ops[0] = Result Type ID
3854 // Ops[1] = Operand
SJWf93f5f32020-05-05 07:27:56 -05003855 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003856
SJW01901d92020-05-21 08:58:31 -05003857 Ops << I.getType();
David Neto22f144c2017-06-12 14:26:21 -04003858
3859 Value *CondV = I.getOperand(0);
3860 if (isa<Constant>(I.getOperand(0))) {
3861 CondV = I.getOperand(1);
3862 }
SJW01901d92020-05-21 08:58:31 -05003863 Ops << CondV;
David Neto22f144c2017-06-12 14:26:21 -04003864
SJWf93f5f32020-05-05 07:27:56 -05003865 RID = addSPIRVInst(spv::OpLogicalNot, Ops);
David Neto22f144c2017-06-12 14:26:21 -04003866 } else {
3867 // Ops[0] = Result Type ID
3868 // Ops[1] = Operand 0
3869 // Ops[2] = Operand 1
SJWf93f5f32020-05-05 07:27:56 -05003870 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003871
SJW01901d92020-05-21 08:58:31 -05003872 Ops << I.getType() << I.getOperand(0) << I.getOperand(1);
David Neto22f144c2017-06-12 14:26:21 -04003873
SJWf93f5f32020-05-05 07:27:56 -05003874 RID = addSPIRVInst(GetSPIRVBinaryOpcode(I), Ops);
David Neto22f144c2017-06-12 14:26:21 -04003875 }
alan-bakerc9c55ae2019-12-02 16:01:27 -05003876 } else if (I.getOpcode() == Instruction::FNeg) {
3877 // The only unary operator.
3878 //
3879 // Ops[0] = Result Type ID
3880 // Ops[1] = Operand 0
SJW01901d92020-05-21 08:58:31 -05003881 SPIRVOperandVec Ops;
alan-bakerc9c55ae2019-12-02 16:01:27 -05003882
SJW01901d92020-05-21 08:58:31 -05003883 Ops << I.getType() << I.getOperand(0);
3884 RID = addSPIRVInst(spv::OpFNegate, Ops);
Marco Antognini68e5c512020-09-09 16:08:57 +01003885 } else if (I.getOpcode() == Instruction::Unreachable) {
3886 RID = addSPIRVInst(spv::OpUnreachable);
David Neto22f144c2017-06-12 14:26:21 -04003887 } else {
3888 I.print(errs());
3889 llvm_unreachable("Unsupported instruction???");
3890 }
3891 break;
3892 }
3893 case Instruction::GetElementPtr: {
3894 auto &GlobalConstArgSet = getGlobalConstArgSet();
3895
3896 //
3897 // Generate OpAccessChain.
3898 //
3899 GetElementPtrInst *GEP = cast<GetElementPtrInst>(&I);
3900
3901 //
3902 // Generate OpAccessChain.
3903 //
3904
3905 // Ops[0] = Result Type ID
3906 // Ops[1] = Base ID
3907 // Ops[2] ... Ops[n] = Indexes ID
SJWf93f5f32020-05-05 07:27:56 -05003908 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003909
alan-bakerb6b09dc2018-11-08 16:59:28 -05003910 PointerType *ResultType = cast<PointerType>(GEP->getType());
David Neto22f144c2017-06-12 14:26:21 -04003911 if (GEP->getPointerAddressSpace() == AddressSpace::ModuleScopePrivate ||
3912 GlobalConstArgSet.count(GEP->getPointerOperand())) {
3913 // Use pointer type with private address space for global constant.
3914 Type *EleTy = I.getType()->getPointerElementType();
David Neto1a1a0582017-07-07 12:01:44 -04003915 ResultType = PointerType::get(EleTy, AddressSpace::ModuleScopePrivate);
David Neto22f144c2017-06-12 14:26:21 -04003916 }
David Neto257c3892018-04-11 13:19:45 -04003917
SJW01901d92020-05-21 08:58:31 -05003918 Ops << ResultType;
David Neto22f144c2017-06-12 14:26:21 -04003919
David Neto862b7d82018-06-14 18:48:37 -04003920 // Generate the base pointer.
SJW01901d92020-05-21 08:58:31 -05003921 Ops << GEP->getPointerOperand();
David Neto22f144c2017-06-12 14:26:21 -04003922
David Neto862b7d82018-06-14 18:48:37 -04003923 // TODO(dneto): Simplify the following?
David Neto22f144c2017-06-12 14:26:21 -04003924
3925 //
3926 // Follows below rules for gep.
3927 //
David Neto862b7d82018-06-14 18:48:37 -04003928 // 1. If gep's first index is 0 generate OpAccessChain and ignore gep's
3929 // first index.
David Neto22f144c2017-06-12 14:26:21 -04003930 // 2. If gep's first index is not 0, generate OpPtrAccessChain and use gep's
3931 // first index.
3932 // 3. If gep's first index is not constant, generate OpPtrAccessChain and
3933 // use gep's first index.
3934 // 4. If it is not above case 1, 2 and 3, generate OpAccessChain and use
3935 // gep's first index.
3936 //
3937 spv::Op Opcode = spv::OpAccessChain;
3938 unsigned offset = 0;
3939 if (ConstantInt *CstInt = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
David Neto862b7d82018-06-14 18:48:37 -04003940 if (CstInt->getZExtValue() == 0) {
David Neto22f144c2017-06-12 14:26:21 -04003941 offset = 1;
David Neto862b7d82018-06-14 18:48:37 -04003942 } else if (CstInt->getZExtValue() != 0) {
David Neto22f144c2017-06-12 14:26:21 -04003943 Opcode = spv::OpPtrAccessChain;
David Neto22f144c2017-06-12 14:26:21 -04003944 }
David Neto862b7d82018-06-14 18:48:37 -04003945 } else {
David Neto22f144c2017-06-12 14:26:21 -04003946 Opcode = spv::OpPtrAccessChain;
David Neto1a1a0582017-07-07 12:01:44 -04003947 }
3948
3949 if (Opcode == spv::OpPtrAccessChain) {
alan-baker7506abb2020-09-10 15:02:55 -04003950 // Shader validation in the SPIR-V spec requires that the base pointer to
3951 // OpPtrAccessChain (in StorageBuffer storage class) be decorated with
3952 // ArrayStride.
alan-baker5b86ed72019-02-15 08:26:50 -05003953 auto address_space = ResultType->getAddressSpace();
3954 setVariablePointersCapabilities(address_space);
3955 switch (GetStorageClass(address_space)) {
Alan Bakerfcda9482018-10-02 17:09:59 -04003956 case spv::StorageClassStorageBuffer:
David Neto1a1a0582017-07-07 12:01:44 -04003957 // Save the need to generate an ArrayStride decoration. But defer
3958 // generation until later, so we only make one decoration.
alan-baker7506abb2020-09-10 15:02:55 -04003959 getTypesNeedingArrayStride().insert(GEP->getPointerOperandType());
3960 break;
3961 case spv::StorageClassWorkgroup:
Alan Bakerfcda9482018-10-02 17:09:59 -04003962 break;
3963 default:
alan-baker7506abb2020-09-10 15:02:55 -04003964 llvm_unreachable(
3965 "OpPtrAccessChain is not supported for this storage class");
Alan Bakerfcda9482018-10-02 17:09:59 -04003966 break;
David Neto1a1a0582017-07-07 12:01:44 -04003967 }
David Neto22f144c2017-06-12 14:26:21 -04003968 }
3969
3970 for (auto II = GEP->idx_begin() + offset; II != GEP->idx_end(); II++) {
SJW01901d92020-05-21 08:58:31 -05003971 Ops << *II;
David Neto22f144c2017-06-12 14:26:21 -04003972 }
3973
SJWf93f5f32020-05-05 07:27:56 -05003974 RID = addSPIRVInst(Opcode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04003975 break;
3976 }
3977 case Instruction::ExtractValue: {
3978 ExtractValueInst *EVI = cast<ExtractValueInst>(&I);
3979 // Ops[0] = Result Type ID
3980 // Ops[1] = Composite ID
3981 // Ops[2] ... Ops[n] = Indexes (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05003982 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04003983
SJW01901d92020-05-21 08:58:31 -05003984 Ops << I.getType();
David Neto22f144c2017-06-12 14:26:21 -04003985
SJW01901d92020-05-21 08:58:31 -05003986 Ops << EVI->getAggregateOperand();
David Neto22f144c2017-06-12 14:26:21 -04003987
3988 for (auto &Index : EVI->indices()) {
SJW01901d92020-05-21 08:58:31 -05003989 Ops << Index;
David Neto22f144c2017-06-12 14:26:21 -04003990 }
3991
SJWf93f5f32020-05-05 07:27:56 -05003992 RID = addSPIRVInst(spv::OpCompositeExtract, Ops);
David Neto22f144c2017-06-12 14:26:21 -04003993 break;
3994 }
3995 case Instruction::InsertValue: {
3996 InsertValueInst *IVI = cast<InsertValueInst>(&I);
3997 // Ops[0] = Result Type ID
3998 // Ops[1] = Object ID
3999 // Ops[2] = Composite ID
4000 // Ops[3] ... Ops[n] = Indexes (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05004001 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004002
SJW01901d92020-05-21 08:58:31 -05004003 Ops << I.getType() << IVI->getInsertedValueOperand()
4004 << IVI->getAggregateOperand();
David Neto22f144c2017-06-12 14:26:21 -04004005
4006 for (auto &Index : IVI->indices()) {
SJW01901d92020-05-21 08:58:31 -05004007 Ops << Index;
David Neto22f144c2017-06-12 14:26:21 -04004008 }
4009
SJWf93f5f32020-05-05 07:27:56 -05004010 RID = addSPIRVInst(spv::OpCompositeInsert, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004011 break;
4012 }
4013 case Instruction::Select: {
4014 //
4015 // Generate OpSelect.
4016 //
4017
4018 // Ops[0] = Result Type ID
4019 // Ops[1] = Condition ID
4020 // Ops[2] = True Constant ID
4021 // Ops[3] = False Constant ID
SJWf93f5f32020-05-05 07:27:56 -05004022 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004023
4024 // Find SPIRV instruction for parameter type.
4025 auto Ty = I.getType();
4026 if (Ty->isPointerTy()) {
4027 auto PointeeTy = Ty->getPointerElementType();
4028 if (PointeeTy->isStructTy() &&
4029 dyn_cast<StructType>(PointeeTy)->isOpaque()) {
4030 Ty = PointeeTy;
alan-baker5b86ed72019-02-15 08:26:50 -05004031 } else {
4032 // Selecting between pointers requires variable pointers.
4033 setVariablePointersCapabilities(Ty->getPointerAddressSpace());
4034 if (!hasVariablePointers() && !selectFromSameObject(&I)) {
SJW01901d92020-05-21 08:58:31 -05004035 setVariablePointers();
alan-baker5b86ed72019-02-15 08:26:50 -05004036 }
David Neto22f144c2017-06-12 14:26:21 -04004037 }
4038 }
4039
SJW01901d92020-05-21 08:58:31 -05004040 Ops << Ty << I.getOperand(0) << I.getOperand(1) << I.getOperand(2);
David Neto22f144c2017-06-12 14:26:21 -04004041
SJWf93f5f32020-05-05 07:27:56 -05004042 RID = addSPIRVInst(spv::OpSelect, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004043 break;
4044 }
4045 case Instruction::ExtractElement: {
4046 // Handle <4 x i8> type manually.
4047 Type *CompositeTy = I.getOperand(0)->getType();
4048 if (is4xi8vec(CompositeTy)) {
4049 //
4050 // Generate OpShiftRightLogical and OpBitwiseAnd for extractelement with
4051 // <4 x i8>.
4052 //
4053
4054 //
4055 // Generate OpShiftRightLogical
4056 //
4057 // Ops[0] = Result Type ID
4058 // Ops[1] = Operand 0
4059 // Ops[2] = Operand 1
4060 //
SJWf93f5f32020-05-05 07:27:56 -05004061 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004062
SJW01901d92020-05-21 08:58:31 -05004063 Ops << CompositeTy << I.getOperand(0);
David Neto22f144c2017-06-12 14:26:21 -04004064
SJW01901d92020-05-21 08:58:31 -05004065 SPIRVID Op1ID = 0;
David Neto22f144c2017-06-12 14:26:21 -04004066 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
4067 // Handle constant index.
SJW806a5d82020-07-15 12:51:38 -05004068 uint32_t Idx = static_cast<uint32_t>(CI->getZExtValue());
4069 Op1ID = getSPIRVInt32Constant(Idx * 8);
David Neto22f144c2017-06-12 14:26:21 -04004070 } else {
4071 // Handle variable index.
SJWf93f5f32020-05-05 07:27:56 -05004072 SPIRVOperandVec TmpOps;
David Neto22f144c2017-06-12 14:26:21 -04004073
SJW806a5d82020-07-15 12:51:38 -05004074 TmpOps << Type::getInt32Ty(Context) << I.getOperand(1)
4075 << getSPIRVInt32Constant(8);
David Neto22f144c2017-06-12 14:26:21 -04004076
SJWf93f5f32020-05-05 07:27:56 -05004077 Op1ID = addSPIRVInst(spv::OpIMul, TmpOps);
David Neto22f144c2017-06-12 14:26:21 -04004078 }
SJW01901d92020-05-21 08:58:31 -05004079 Ops << Op1ID;
David Neto22f144c2017-06-12 14:26:21 -04004080
SJW01901d92020-05-21 08:58:31 -05004081 SPIRVID ShiftID = addSPIRVInst(spv::OpShiftRightLogical, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004082
4083 //
4084 // Generate OpBitwiseAnd
4085 //
4086 // Ops[0] = Result Type ID
4087 // Ops[1] = Operand 0
4088 // Ops[2] = Operand 1
4089 //
4090 Ops.clear();
4091
SJW806a5d82020-07-15 12:51:38 -05004092 Ops << CompositeTy << ShiftID << getSPIRVInt32Constant(0xFF);
David Neto22f144c2017-06-12 14:26:21 -04004093
SJWf93f5f32020-05-05 07:27:56 -05004094 RID = addSPIRVInst(spv::OpBitwiseAnd, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004095 break;
4096 }
4097
4098 // Ops[0] = Result Type ID
4099 // Ops[1] = Composite ID
4100 // Ops[2] ... Ops[n] = Indexes (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05004101 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004102
SJW01901d92020-05-21 08:58:31 -05004103 Ops << I.getType() << I.getOperand(0);
David Neto22f144c2017-06-12 14:26:21 -04004104
4105 spv::Op Opcode = spv::OpCompositeExtract;
4106 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1))) {
SJW01901d92020-05-21 08:58:31 -05004107 Ops << static_cast<uint32_t>(CI->getZExtValue());
David Neto22f144c2017-06-12 14:26:21 -04004108 } else {
SJW01901d92020-05-21 08:58:31 -05004109 Ops << I.getOperand(1);
David Neto22f144c2017-06-12 14:26:21 -04004110 Opcode = spv::OpVectorExtractDynamic;
4111 }
4112
SJWf93f5f32020-05-05 07:27:56 -05004113 RID = addSPIRVInst(Opcode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004114 break;
4115 }
4116 case Instruction::InsertElement: {
4117 // Handle <4 x i8> type manually.
4118 Type *CompositeTy = I.getOperand(0)->getType();
4119 if (is4xi8vec(CompositeTy)) {
SJW806a5d82020-07-15 12:51:38 -05004120 SPIRVID CstFFID = getSPIRVInt32Constant(0xFF);
David Neto22f144c2017-06-12 14:26:21 -04004121
SJW01901d92020-05-21 08:58:31 -05004122 SPIRVID ShiftAmountID = 0;
David Neto22f144c2017-06-12 14:26:21 -04004123 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) {
4124 // Handle constant index.
SJW806a5d82020-07-15 12:51:38 -05004125 uint32_t Idx = static_cast<uint32_t>(CI->getZExtValue());
4126 ShiftAmountID = getSPIRVInt32Constant(Idx * 8);
David Neto22f144c2017-06-12 14:26:21 -04004127 } else {
4128 // Handle variable index.
SJWf93f5f32020-05-05 07:27:56 -05004129 SPIRVOperandVec TmpOps;
David Neto22f144c2017-06-12 14:26:21 -04004130
SJW806a5d82020-07-15 12:51:38 -05004131 TmpOps << Type::getInt32Ty(Context) << I.getOperand(2)
4132 << getSPIRVInt32Constant(8);
David Neto22f144c2017-06-12 14:26:21 -04004133
SJWf93f5f32020-05-05 07:27:56 -05004134 ShiftAmountID = addSPIRVInst(spv::OpIMul, TmpOps);
David Neto22f144c2017-06-12 14:26:21 -04004135 }
4136
4137 //
4138 // Generate mask operations.
4139 //
4140
4141 // ShiftLeft mask according to index of insertelement.
SJWf93f5f32020-05-05 07:27:56 -05004142 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004143
SJW01901d92020-05-21 08:58:31 -05004144 Ops << CompositeTy << CstFFID << ShiftAmountID;
David Neto22f144c2017-06-12 14:26:21 -04004145
SJW01901d92020-05-21 08:58:31 -05004146 SPIRVID MaskID = addSPIRVInst(spv::OpShiftLeftLogical, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004147
4148 // Inverse mask.
4149 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05004150 Ops << CompositeTy << MaskID;
David Neto22f144c2017-06-12 14:26:21 -04004151
SJW01901d92020-05-21 08:58:31 -05004152 SPIRVID InvMaskID = addSPIRVInst(spv::OpNot, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004153
4154 // Apply mask.
4155 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05004156 Ops << CompositeTy << I.getOperand(0) << InvMaskID;
David Neto22f144c2017-06-12 14:26:21 -04004157
SJW01901d92020-05-21 08:58:31 -05004158 SPIRVID OrgValID = addSPIRVInst(spv::OpBitwiseAnd, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004159
4160 // Create correct value according to index of insertelement.
4161 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05004162 Ops << CompositeTy << I.getOperand(1) << ShiftAmountID;
David Neto22f144c2017-06-12 14:26:21 -04004163
SJW01901d92020-05-21 08:58:31 -05004164 SPIRVID InsertValID = addSPIRVInst(spv::OpShiftLeftLogical, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004165
4166 // Insert value to original value.
4167 Ops.clear();
SJW01901d92020-05-21 08:58:31 -05004168 Ops << CompositeTy << OrgValID << InsertValID;
David Neto22f144c2017-06-12 14:26:21 -04004169
SJWf93f5f32020-05-05 07:27:56 -05004170 RID = addSPIRVInst(spv::OpBitwiseOr, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004171 break;
4172 }
4173
SJWf93f5f32020-05-05 07:27:56 -05004174 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004175
James Priced26efea2018-06-09 23:28:32 +01004176 // Ops[0] = Result Type ID
SJW01901d92020-05-21 08:58:31 -05004177 Ops << I.getType();
David Neto22f144c2017-06-12 14:26:21 -04004178
4179 spv::Op Opcode = spv::OpCompositeInsert;
4180 if (const ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2))) {
David Neto257c3892018-04-11 13:19:45 -04004181 const auto value = CI->getZExtValue();
4182 assert(value <= UINT32_MAX);
James Priced26efea2018-06-09 23:28:32 +01004183 // Ops[1] = Object ID
4184 // Ops[2] = Composite ID
4185 // Ops[3] ... Ops[n] = Indexes (Literal Number)
SJW01901d92020-05-21 08:58:31 -05004186 Ops << I.getOperand(1) << I.getOperand(0) << static_cast<uint32_t>(value);
David Neto22f144c2017-06-12 14:26:21 -04004187 } else {
James Priced26efea2018-06-09 23:28:32 +01004188 // Ops[1] = Composite ID
4189 // Ops[2] = Object ID
4190 // Ops[3] ... Ops[n] = Indexes (Literal Number)
SJW01901d92020-05-21 08:58:31 -05004191 Ops << I.getOperand(0) << I.getOperand(1) << I.getOperand(2);
David Neto22f144c2017-06-12 14:26:21 -04004192 Opcode = spv::OpVectorInsertDynamic;
4193 }
4194
SJWf93f5f32020-05-05 07:27:56 -05004195 RID = addSPIRVInst(Opcode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004196 break;
4197 }
4198 case Instruction::ShuffleVector: {
4199 // Ops[0] = Result Type ID
4200 // Ops[1] = Vector 1 ID
4201 // Ops[2] = Vector 2 ID
4202 // Ops[3] ... Ops[n] = Components (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05004203 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004204
SJW01901d92020-05-21 08:58:31 -05004205 Ops << I.getType() << I.getOperand(0) << I.getOperand(1);
David Neto22f144c2017-06-12 14:26:21 -04004206
alan-bakerc9666712020-04-01 16:31:21 -04004207 auto shuffle = cast<ShuffleVectorInst>(&I);
4208 SmallVector<int, 4> mask;
4209 shuffle->getShuffleMask(mask);
4210 for (auto i : mask) {
4211 if (i == UndefMaskElem) {
4212 if (clspv::Option::HackUndef())
4213 // Use 0 instead of undef.
SJW01901d92020-05-21 08:58:31 -05004214 Ops << 0;
alan-bakerc9666712020-04-01 16:31:21 -04004215 else
4216 // Undef for shuffle in SPIR-V.
SJW01901d92020-05-21 08:58:31 -05004217 Ops << 0xffffffff;
David Neto22f144c2017-06-12 14:26:21 -04004218 } else {
SJW01901d92020-05-21 08:58:31 -05004219 Ops << i;
David Neto22f144c2017-06-12 14:26:21 -04004220 }
4221 }
4222
SJWf93f5f32020-05-05 07:27:56 -05004223 RID = addSPIRVInst(spv::OpVectorShuffle, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004224 break;
4225 }
4226 case Instruction::ICmp:
4227 case Instruction::FCmp: {
4228 CmpInst *CmpI = cast<CmpInst>(&I);
4229
David Netod4ca2e62017-07-06 18:47:35 -04004230 // Pointer equality is invalid.
alan-bakerb6b09dc2018-11-08 16:59:28 -05004231 Type *ArgTy = CmpI->getOperand(0)->getType();
David Netod4ca2e62017-07-06 18:47:35 -04004232 if (isa<PointerType>(ArgTy)) {
4233 CmpI->print(errs());
alan-baker21574d32020-01-29 16:00:31 -05004234 std::string name = I.getParent()->getParent()->getName().str();
David Netod4ca2e62017-07-06 18:47:35 -04004235 errs()
4236 << "\nPointer equality test is not supported by SPIR-V for Vulkan, "
4237 << "in function " << name << "\n";
4238 llvm_unreachable("Pointer equality check is invalid");
4239 break;
4240 }
4241
David Neto257c3892018-04-11 13:19:45 -04004242 // Ops[0] = Result Type ID
4243 // Ops[1] = Operand 1 ID
4244 // Ops[2] = Operand 2 ID
SJWf93f5f32020-05-05 07:27:56 -05004245 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004246
SJW01901d92020-05-21 08:58:31 -05004247 Ops << CmpI->getType() << CmpI->getOperand(0) << CmpI->getOperand(1);
David Neto22f144c2017-06-12 14:26:21 -04004248
4249 spv::Op Opcode = GetSPIRVCmpOpcode(CmpI);
SJWf93f5f32020-05-05 07:27:56 -05004250 RID = addSPIRVInst(Opcode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004251 break;
4252 }
4253 case Instruction::Br: {
SJW88ed5fe2020-05-11 12:40:57 -05004254 // Branch instruction is deferred because it needs label's ID.
4255 BasicBlock *BrBB = I.getParent();
4256 if (ContinueBlocks.count(BrBB) || MergeBlocks.count(BrBB)) {
4257 // Placeholder for Merge operation
4258 RID = addSPIRVPlaceholder(&I);
4259 }
4260 RID = addSPIRVPlaceholder(&I);
David Neto22f144c2017-06-12 14:26:21 -04004261 break;
4262 }
4263 case Instruction::Switch: {
4264 I.print(errs());
4265 llvm_unreachable("Unsupported instruction???");
4266 break;
4267 }
4268 case Instruction::IndirectBr: {
4269 I.print(errs());
4270 llvm_unreachable("Unsupported instruction???");
4271 break;
4272 }
4273 case Instruction::PHI: {
SJW88ed5fe2020-05-11 12:40:57 -05004274 // PHI instruction is deferred because it needs label's ID.
4275 RID = addSPIRVPlaceholder(&I);
David Neto22f144c2017-06-12 14:26:21 -04004276 break;
4277 }
4278 case Instruction::Alloca: {
4279 //
4280 // Generate OpVariable.
4281 //
4282 // Ops[0] : Result Type ID
4283 // Ops[1] : Storage Class
SJWf93f5f32020-05-05 07:27:56 -05004284 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004285
SJW01901d92020-05-21 08:58:31 -05004286 Ops << I.getType() << spv::StorageClassFunction;
David Neto22f144c2017-06-12 14:26:21 -04004287
SJWf93f5f32020-05-05 07:27:56 -05004288 RID = addSPIRVInst(spv::OpVariable, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004289 break;
4290 }
4291 case Instruction::Load: {
4292 LoadInst *LD = cast<LoadInst>(&I);
4293 //
4294 // Generate OpLoad.
4295 //
Diego Novillo3cc8d7a2019-04-10 13:30:34 -04004296
alan-baker5b86ed72019-02-15 08:26:50 -05004297 if (LD->getType()->isPointerTy()) {
4298 // Loading a pointer requires variable pointers.
4299 setVariablePointersCapabilities(LD->getType()->getPointerAddressSpace());
4300 }
David Neto22f144c2017-06-12 14:26:21 -04004301
SJW01901d92020-05-21 08:58:31 -05004302 SPIRVID PointerID = getSPIRVValue(LD->getPointerOperand());
David Netoa60b00b2017-09-15 16:34:09 -04004303 // This is a hack to work around what looks like a driver bug.
4304 // When we're loading from the special variable holding the WorkgroupSize
David Neto0a2f98d2017-09-15 19:38:40 -04004305 // builtin value, use an OpBitWiseAnd of the value's ID rather than
4306 // generating a load.
David Neto66cfe642018-03-24 06:13:56 -07004307 // TODO(dneto): Remove this awful hack once drivers are fixed.
David Netoa60b00b2017-09-15 16:34:09 -04004308 if (PointerID == WorkgroupSizeVarID) {
David Neto0a2f98d2017-09-15 19:38:40 -04004309 // Generate a bitwise-and of the original value with itself.
4310 // We should have been able to get away with just an OpCopyObject,
4311 // but we need something more complex to get past certain driver bugs.
4312 // This is ridiculous, but necessary.
4313 // TODO(dneto): Revisit this once drivers fix their bugs.
4314
SJWf93f5f32020-05-05 07:27:56 -05004315 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05004316 Ops << LD->getType() << WorkgroupSizeValueID << WorkgroupSizeValueID;
David Neto0a2f98d2017-09-15 19:38:40 -04004317
SJWf93f5f32020-05-05 07:27:56 -05004318 RID = addSPIRVInst(spv::OpBitwiseAnd, Ops);
David Netoa60b00b2017-09-15 16:34:09 -04004319 break;
4320 }
4321
4322 // This is the normal path. Generate a load.
4323
David Neto22f144c2017-06-12 14:26:21 -04004324 // Ops[0] = Result Type ID
4325 // Ops[1] = Pointer ID
4326 // Ops[2] ... Ops[n] = Optional Memory Access
4327 //
4328 // TODO: Do we need to implement Optional Memory Access???
David Neto0a2f98d2017-09-15 19:38:40 -04004329
SJWf93f5f32020-05-05 07:27:56 -05004330 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05004331 Ops << LD->getType() << LD->getPointerOperand();
David Neto22f144c2017-06-12 14:26:21 -04004332
SJWf93f5f32020-05-05 07:27:56 -05004333 RID = addSPIRVInst(spv::OpLoad, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004334 break;
4335 }
4336 case Instruction::Store: {
4337 StoreInst *ST = cast<StoreInst>(&I);
4338 //
4339 // Generate OpStore.
4340 //
4341
alan-baker5b86ed72019-02-15 08:26:50 -05004342 if (ST->getValueOperand()->getType()->isPointerTy()) {
4343 // Storing a pointer requires variable pointers.
4344 setVariablePointersCapabilities(
4345 ST->getValueOperand()->getType()->getPointerAddressSpace());
4346 }
4347
David Neto22f144c2017-06-12 14:26:21 -04004348 // Ops[0] = Pointer ID
4349 // Ops[1] = Object ID
4350 // Ops[2] ... Ops[n] = Optional Memory Access (later???)
4351 //
4352 // TODO: Do we need to implement Optional Memory Access???
SJWf93f5f32020-05-05 07:27:56 -05004353 SPIRVOperandVec Ops;
SJW01901d92020-05-21 08:58:31 -05004354 Ops << ST->getPointerOperand() << ST->getValueOperand();
David Neto22f144c2017-06-12 14:26:21 -04004355
SJWf93f5f32020-05-05 07:27:56 -05004356 RID = addSPIRVInst(spv::OpStore, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004357 break;
4358 }
4359 case Instruction::AtomicCmpXchg: {
4360 I.print(errs());
4361 llvm_unreachable("Unsupported instruction???");
4362 break;
4363 }
4364 case Instruction::AtomicRMW: {
Neil Henning39672102017-09-29 14:33:13 +01004365 AtomicRMWInst *AtomicRMW = dyn_cast<AtomicRMWInst>(&I);
4366
4367 spv::Op opcode;
4368
4369 switch (AtomicRMW->getOperation()) {
4370 default:
4371 I.print(errs());
4372 llvm_unreachable("Unsupported instruction???");
4373 case llvm::AtomicRMWInst::Add:
4374 opcode = spv::OpAtomicIAdd;
4375 break;
4376 case llvm::AtomicRMWInst::Sub:
4377 opcode = spv::OpAtomicISub;
4378 break;
4379 case llvm::AtomicRMWInst::Xchg:
4380 opcode = spv::OpAtomicExchange;
4381 break;
4382 case llvm::AtomicRMWInst::Min:
4383 opcode = spv::OpAtomicSMin;
4384 break;
4385 case llvm::AtomicRMWInst::Max:
4386 opcode = spv::OpAtomicSMax;
4387 break;
4388 case llvm::AtomicRMWInst::UMin:
4389 opcode = spv::OpAtomicUMin;
4390 break;
4391 case llvm::AtomicRMWInst::UMax:
4392 opcode = spv::OpAtomicUMax;
4393 break;
4394 case llvm::AtomicRMWInst::And:
4395 opcode = spv::OpAtomicAnd;
4396 break;
4397 case llvm::AtomicRMWInst::Or:
4398 opcode = spv::OpAtomicOr;
4399 break;
4400 case llvm::AtomicRMWInst::Xor:
4401 opcode = spv::OpAtomicXor;
4402 break;
4403 }
4404
4405 //
4406 // Generate OpAtomic*.
4407 //
SJWf93f5f32020-05-05 07:27:56 -05004408 SPIRVOperandVec Ops;
Neil Henning39672102017-09-29 14:33:13 +01004409
SJW01901d92020-05-21 08:58:31 -05004410 Ops << I.getType() << AtomicRMW->getPointerOperand();
Neil Henning39672102017-09-29 14:33:13 +01004411
SJW806a5d82020-07-15 12:51:38 -05004412 const auto ConstantScopeDevice = getSPIRVInt32Constant(spv::ScopeDevice);
SJW01901d92020-05-21 08:58:31 -05004413 Ops << ConstantScopeDevice;
Neil Henning39672102017-09-29 14:33:13 +01004414
SJW806a5d82020-07-15 12:51:38 -05004415 const auto ConstantMemorySemantics =
4416 getSPIRVInt32Constant(spv::MemorySemanticsUniformMemoryMask |
4417 spv::MemorySemanticsSequentiallyConsistentMask);
SJW01901d92020-05-21 08:58:31 -05004418 Ops << ConstantMemorySemantics << AtomicRMW->getValOperand();
Neil Henning39672102017-09-29 14:33:13 +01004419
SJWf93f5f32020-05-05 07:27:56 -05004420 RID = addSPIRVInst(opcode, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004421 break;
4422 }
4423 case Instruction::Fence: {
4424 I.print(errs());
4425 llvm_unreachable("Unsupported instruction???");
4426 break;
4427 }
4428 case Instruction::Call: {
4429 CallInst *Call = dyn_cast<CallInst>(&I);
SJW806a5d82020-07-15 12:51:38 -05004430 RID = GenerateInstructionFromCall(Call);
David Neto22f144c2017-06-12 14:26:21 -04004431 break;
4432 }
4433 case Instruction::Ret: {
4434 unsigned NumOps = I.getNumOperands();
4435 if (NumOps == 0) {
4436 //
4437 // Generate OpReturn.
4438 //
SJWf93f5f32020-05-05 07:27:56 -05004439 RID = addSPIRVInst(spv::OpReturn);
David Neto22f144c2017-06-12 14:26:21 -04004440 } else {
4441 //
4442 // Generate OpReturnValue.
4443 //
4444
4445 // Ops[0] = Return Value ID
SJWf93f5f32020-05-05 07:27:56 -05004446 SPIRVOperandVec Ops;
David Neto257c3892018-04-11 13:19:45 -04004447
SJW01901d92020-05-21 08:58:31 -05004448 Ops << I.getOperand(0);
David Neto22f144c2017-06-12 14:26:21 -04004449
SJWf93f5f32020-05-05 07:27:56 -05004450 RID = addSPIRVInst(spv::OpReturnValue, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004451 break;
4452 }
4453 break;
4454 }
4455 }
SJWf93f5f32020-05-05 07:27:56 -05004456
4457 // Register Instruction to ValueMap.
SJW01901d92020-05-21 08:58:31 -05004458 if (RID.isValid()) {
SJWf93f5f32020-05-05 07:27:56 -05004459 VMap[&I] = RID;
4460 }
David Neto22f144c2017-06-12 14:26:21 -04004461}
4462
4463void SPIRVProducerPass::GenerateFuncEpilogue() {
David Neto22f144c2017-06-12 14:26:21 -04004464 //
4465 // Generate OpFunctionEnd
4466 //
SJWf93f5f32020-05-05 07:27:56 -05004467 addSPIRVInst(spv::OpFunctionEnd);
David Neto22f144c2017-06-12 14:26:21 -04004468}
4469
4470bool SPIRVProducerPass::is4xi8vec(Type *Ty) const {
alan-bakerb39c8262019-03-08 14:03:37 -05004471 // Don't specialize <4 x i8> if i8 is generally supported.
4472 if (clspv::Option::Int8Support())
4473 return false;
4474
David Neto22f144c2017-06-12 14:26:21 -04004475 LLVMContext &Context = Ty->getContext();
James Pricecf53df42020-04-20 14:41:24 -04004476 if (auto VecTy = dyn_cast<VectorType>(Ty)) {
4477 if (VecTy->getElementType() == Type::getInt8Ty(Context) &&
alan-baker5a8c3be2020-09-09 13:44:26 -04004478 VecTy->getElementCount().getKnownMinValue() == 4) {
David Neto22f144c2017-06-12 14:26:21 -04004479 return true;
4480 }
4481 }
4482
4483 return false;
4484}
4485
4486void SPIRVProducerPass::HandleDeferredInstruction() {
David Neto22f144c2017-06-12 14:26:21 -04004487 DeferredInstVecType &DeferredInsts = getDeferredInstVec();
4488
SJW88ed5fe2020-05-11 12:40:57 -05004489 for (size_t i = 0; i < DeferredInsts.size(); ++i) {
4490 Value *Inst = DeferredInsts[i].first;
4491 SPIRVInstruction *Placeholder = DeferredInsts[i].second;
4492 SPIRVOperandVec Operands;
4493
4494 auto nextDeferred = [&i, &Inst, &DeferredInsts, &Placeholder]() {
4495 ++i;
4496 assert(DeferredInsts.size() > i);
4497 assert(Inst == DeferredInsts[i].first);
4498 Placeholder = DeferredInsts[i].second;
4499 };
David Neto22f144c2017-06-12 14:26:21 -04004500
4501 if (BranchInst *Br = dyn_cast<BranchInst>(Inst)) {
alan-baker06cad652019-12-03 17:56:47 -05004502 // Check whether this branch needs to be preceeded by merge instruction.
David Neto22f144c2017-06-12 14:26:21 -04004503 BasicBlock *BrBB = Br->getParent();
alan-baker06cad652019-12-03 17:56:47 -05004504 if (ContinueBlocks.count(BrBB)) {
David Neto22f144c2017-06-12 14:26:21 -04004505 //
4506 // Generate OpLoopMerge.
4507 //
4508 // Ops[0] = Merge Block ID
4509 // Ops[1] = Continue Target ID
4510 // Ops[2] = Selection Control
SJWf93f5f32020-05-05 07:27:56 -05004511 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004512
SJW01901d92020-05-21 08:58:31 -05004513 Ops << MergeBlocks[BrBB] << ContinueBlocks[BrBB]
4514 << spv::LoopControlMaskNone;
David Neto22f144c2017-06-12 14:26:21 -04004515
SJW88ed5fe2020-05-11 12:40:57 -05004516 replaceSPIRVInst(Placeholder, spv::OpLoopMerge, Ops);
4517
4518 nextDeferred();
4519
alan-baker06cad652019-12-03 17:56:47 -05004520 } else if (MergeBlocks.count(BrBB)) {
4521 //
4522 // Generate OpSelectionMerge.
4523 //
4524 // Ops[0] = Merge Block ID
4525 // Ops[1] = Selection Control
SJWf93f5f32020-05-05 07:27:56 -05004526 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004527
alan-baker06cad652019-12-03 17:56:47 -05004528 auto MergeBB = MergeBlocks[BrBB];
SJW01901d92020-05-21 08:58:31 -05004529 Ops << MergeBB << spv::SelectionControlMaskNone;
David Neto22f144c2017-06-12 14:26:21 -04004530
SJW88ed5fe2020-05-11 12:40:57 -05004531 replaceSPIRVInst(Placeholder, spv::OpSelectionMerge, Ops);
4532
4533 nextDeferred();
David Neto22f144c2017-06-12 14:26:21 -04004534 }
4535
4536 if (Br->isConditional()) {
4537 //
4538 // Generate OpBranchConditional.
4539 //
4540 // Ops[0] = Condition ID
4541 // Ops[1] = True Label ID
4542 // Ops[2] = False Label ID
4543 // Ops[3] ... Ops[n] = Branch weights (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05004544 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004545
SJW01901d92020-05-21 08:58:31 -05004546 Ops << Br->getCondition() << Br->getSuccessor(0) << Br->getSuccessor(1);
David Neto22f144c2017-06-12 14:26:21 -04004547
SJW88ed5fe2020-05-11 12:40:57 -05004548 replaceSPIRVInst(Placeholder, spv::OpBranchConditional, Ops);
4549
David Neto22f144c2017-06-12 14:26:21 -04004550 } else {
4551 //
4552 // Generate OpBranch.
4553 //
4554 // Ops[0] = Target Label ID
SJWf93f5f32020-05-05 07:27:56 -05004555 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004556
SJW01901d92020-05-21 08:58:31 -05004557 Ops << Br->getSuccessor(0);
David Neto22f144c2017-06-12 14:26:21 -04004558
SJW88ed5fe2020-05-11 12:40:57 -05004559 replaceSPIRVInst(Placeholder, spv::OpBranch, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004560 }
4561 } else if (PHINode *PHI = dyn_cast<PHINode>(Inst)) {
alan-baker5ed87542020-03-23 11:05:22 -04004562 if (PHI->getType()->isPointerTy() && !IsSamplerType(PHI->getType()) &&
4563 !IsImageType(PHI->getType())) {
alan-baker5b86ed72019-02-15 08:26:50 -05004564 // OpPhi on pointers requires variable pointers.
4565 setVariablePointersCapabilities(
4566 PHI->getType()->getPointerAddressSpace());
4567 if (!hasVariablePointers() && !selectFromSameObject(PHI)) {
SJW01901d92020-05-21 08:58:31 -05004568 setVariablePointers();
alan-baker5b86ed72019-02-15 08:26:50 -05004569 }
4570 }
4571
David Neto22f144c2017-06-12 14:26:21 -04004572 //
4573 // Generate OpPhi.
4574 //
4575 // Ops[0] = Result Type ID
4576 // Ops[1] ... Ops[n] = (Variable ID, Parent ID) pairs
SJWf93f5f32020-05-05 07:27:56 -05004577 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004578
SJW01901d92020-05-21 08:58:31 -05004579 Ops << PHI->getType();
David Neto22f144c2017-06-12 14:26:21 -04004580
SJW88ed5fe2020-05-11 12:40:57 -05004581 for (unsigned j = 0; j < PHI->getNumIncomingValues(); j++) {
SJW01901d92020-05-21 08:58:31 -05004582 Ops << PHI->getIncomingValue(j) << PHI->getIncomingBlock(j);
David Neto22f144c2017-06-12 14:26:21 -04004583 }
4584
SJW88ed5fe2020-05-11 12:40:57 -05004585 replaceSPIRVInst(Placeholder, spv::OpPhi, Ops);
4586
David Neto22f144c2017-06-12 14:26:21 -04004587 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) {
4588 Function *Callee = Call->getCalledFunction();
David Neto3fbb4072017-10-16 11:28:14 -04004589 auto callee_name = Callee->getName();
David Neto22f144c2017-06-12 14:26:21 -04004590
SJW61531372020-06-09 07:31:08 -05004591 if (Builtins::Lookup(Callee) == Builtins::kClspvCompositeConstruct) {
David Netoab03f432017-11-03 17:00:44 -04004592 // Generate an OpCompositeConstruct
SJWf93f5f32020-05-05 07:27:56 -05004593 SPIRVOperandVec Ops;
David Netoab03f432017-11-03 17:00:44 -04004594
4595 // The result type.
SJW01901d92020-05-21 08:58:31 -05004596 Ops << Call->getType();
David Netoab03f432017-11-03 17:00:44 -04004597
4598 for (Use &use : Call->arg_operands()) {
SJW01901d92020-05-21 08:58:31 -05004599 Ops << use.get();
David Netoab03f432017-11-03 17:00:44 -04004600 }
4601
SJW88ed5fe2020-05-11 12:40:57 -05004602 replaceSPIRVInst(Placeholder, spv::OpCompositeConstruct, Ops);
David Netoab03f432017-11-03 17:00:44 -04004603
David Neto22f144c2017-06-12 14:26:21 -04004604 } else {
alan-baker5b86ed72019-02-15 08:26:50 -05004605 if (Call->getType()->isPointerTy()) {
4606 // Functions returning pointers require variable pointers.
4607 setVariablePointersCapabilities(
4608 Call->getType()->getPointerAddressSpace());
4609 }
4610
David Neto22f144c2017-06-12 14:26:21 -04004611 //
4612 // Generate OpFunctionCall.
4613 //
4614
4615 // Ops[0] = Result Type ID
4616 // Ops[1] = Callee Function ID
4617 // Ops[2] ... Ops[n] = Argument 0, ... , Argument n
SJWf93f5f32020-05-05 07:27:56 -05004618 SPIRVOperandVec Ops;
David Neto22f144c2017-06-12 14:26:21 -04004619
SJW01901d92020-05-21 08:58:31 -05004620 Ops << Call->getType();
David Neto22f144c2017-06-12 14:26:21 -04004621
SJW01901d92020-05-21 08:58:31 -05004622 SPIRVID CalleeID = getSPIRVValue(Callee);
SJW806a5d82020-07-15 12:51:38 -05004623 if (!CalleeID.isValid()) {
David Neto43568eb2017-10-13 18:25:25 -04004624 errs() << "Can't translate function call. Missing builtin? "
David Neto862b7d82018-06-14 18:48:37 -04004625 << callee_name << " in: " << *Call << "\n";
David Neto43568eb2017-10-13 18:25:25 -04004626 // TODO(dneto): Can we error out? Enabling this llvm_unreachable
4627 // causes an infinite loop. Instead, go ahead and generate
4628 // the bad function call. A validator will catch the 0-Id.
4629 // llvm_unreachable("Can't translate function call");
4630 }
David Neto22f144c2017-06-12 14:26:21 -04004631
SJW01901d92020-05-21 08:58:31 -05004632 Ops << CalleeID;
David Neto22f144c2017-06-12 14:26:21 -04004633
David Neto22f144c2017-06-12 14:26:21 -04004634 FunctionType *CalleeFTy = cast<FunctionType>(Call->getFunctionType());
SJW88ed5fe2020-05-11 12:40:57 -05004635 for (unsigned j = 0; j < CalleeFTy->getNumParams(); j++) {
4636 auto *operand = Call->getOperand(j);
alan-bakerd4d50652019-12-03 17:17:15 -05004637 auto *operand_type = operand->getType();
4638 // Images and samplers can be passed as function parameters without
4639 // variable pointers.
4640 if (operand_type->isPointerTy() && !IsImageType(operand_type) &&
4641 !IsSamplerType(operand_type)) {
alan-baker5b86ed72019-02-15 08:26:50 -05004642 auto sc =
4643 GetStorageClass(operand->getType()->getPointerAddressSpace());
4644 if (sc == spv::StorageClassStorageBuffer) {
4645 // Passing SSBO by reference requires variable pointers storage
4646 // buffer.
SJW01901d92020-05-21 08:58:31 -05004647 setVariablePointersStorageBuffer();
alan-baker5b86ed72019-02-15 08:26:50 -05004648 } else if (sc == spv::StorageClassWorkgroup) {
4649 // Workgroup references require variable pointers if they are not
4650 // memory object declarations.
4651 if (auto *operand_call = dyn_cast<CallInst>(operand)) {
4652 // Workgroup accessor represents a variable reference.
SJW61531372020-06-09 07:31:08 -05004653 if (Builtins::Lookup(operand_call->getCalledFunction()) !=
4654 Builtins::kClspvLocal)
SJW01901d92020-05-21 08:58:31 -05004655 setVariablePointers();
alan-baker5b86ed72019-02-15 08:26:50 -05004656 } else {
4657 // Arguments are function parameters.
4658 if (!isa<Argument>(operand))
SJW01901d92020-05-21 08:58:31 -05004659 setVariablePointers();
alan-baker5b86ed72019-02-15 08:26:50 -05004660 }
4661 }
4662 }
SJW01901d92020-05-21 08:58:31 -05004663 Ops << operand;
David Neto22f144c2017-06-12 14:26:21 -04004664 }
4665
SJW88ed5fe2020-05-11 12:40:57 -05004666 replaceSPIRVInst(Placeholder, spv::OpFunctionCall, Ops);
David Neto22f144c2017-06-12 14:26:21 -04004667 }
4668 }
4669 }
4670}
4671
SJW77b87ad2020-04-21 14:37:52 -05004672void SPIRVProducerPass::HandleDeferredDecorations() {
4673 const auto &DL = module->getDataLayout();
alan-baker5a8c3be2020-09-09 13:44:26 -04004674 if (getTypesNeedingArrayStride().empty()) {
David Neto1a1a0582017-07-07 12:01:44 -04004675 return;
David Netoc6f3ab22018-04-06 18:02:31 -04004676 }
David Neto1a1a0582017-07-07 12:01:44 -04004677
David Netoc6f3ab22018-04-06 18:02:31 -04004678 // Insert ArrayStride decorations on pointer types, due to OpPtrAccessChain
4679 // instructions we generated earlier.
David Neto85082642018-03-24 06:55:20 -07004680 for (auto *type : getTypesNeedingArrayStride()) {
4681 Type *elemTy = nullptr;
4682 if (auto *ptrTy = dyn_cast<PointerType>(type)) {
4683 elemTy = ptrTy->getElementType();
alan-bakerb6b09dc2018-11-08 16:59:28 -05004684 } else if (auto *arrayTy = dyn_cast<ArrayType>(type)) {
alan-baker8eb435a2020-04-08 00:42:06 -04004685 elemTy = arrayTy->getElementType();
4686 } else if (auto *vecTy = dyn_cast<VectorType>(type)) {
4687 elemTy = vecTy->getElementType();
David Neto85082642018-03-24 06:55:20 -07004688 } else {
4689 errs() << "Unhandled strided type " << *type << "\n";
4690 llvm_unreachable("Unhandled strided type");
4691 }
David Neto1a1a0582017-07-07 12:01:44 -04004692
4693 // Ops[0] = Target ID
4694 // Ops[1] = Decoration (ArrayStride)
4695 // Ops[2] = Stride number (Literal Number)
SJWf93f5f32020-05-05 07:27:56 -05004696 SPIRVOperandVec Ops;
David Neto1a1a0582017-07-07 12:01:44 -04004697
David Neto85082642018-03-24 06:55:20 -07004698 // Same as DL.getIndexedOffsetInType( elemTy, { 1 } );
Alan Bakerfcda9482018-10-02 17:09:59 -04004699 const uint32_t stride = static_cast<uint32_t>(GetTypeAllocSize(elemTy, DL));
David Neto257c3892018-04-11 13:19:45 -04004700
SJW01901d92020-05-21 08:58:31 -05004701 Ops << type << spv::DecorationArrayStride << stride;
David Neto1a1a0582017-07-07 12:01:44 -04004702
SJWf93f5f32020-05-05 07:27:56 -05004703 addSPIRVInst<kAnnotations>(spv::OpDecorate, Ops);
David Netoc6f3ab22018-04-06 18:02:31 -04004704 }
David Neto1a1a0582017-07-07 12:01:44 -04004705}
4706
SJW61531372020-06-09 07:31:08 -05004707glsl::ExtInst
4708SPIRVProducerPass::getExtInstEnum(const Builtins::FunctionInfo &func_info) {
SJW61531372020-06-09 07:31:08 -05004709 switch (func_info.getType()) {
SJW2c317da2020-03-23 07:39:13 -05004710 case Builtins::kClamp: {
SJW61531372020-06-09 07:31:08 -05004711 auto param_type = func_info.getParameter(0);
SJW2c317da2020-03-23 07:39:13 -05004712 if (param_type.type_id == Type::FloatTyID) {
4713 return glsl::ExtInst::ExtInstFClamp;
4714 }
4715 return param_type.is_signed ? glsl::ExtInst::ExtInstSClamp
4716 : glsl::ExtInst::ExtInstUClamp;
4717 }
4718 case Builtins::kMax: {
SJW61531372020-06-09 07:31:08 -05004719 auto param_type = func_info.getParameter(0);
SJW2c317da2020-03-23 07:39:13 -05004720 if (param_type.type_id == Type::FloatTyID) {
4721 return glsl::ExtInst::ExtInstFMax;
4722 }
4723 return param_type.is_signed ? glsl::ExtInst::ExtInstSMax
4724 : glsl::ExtInst::ExtInstUMax;
4725 }
4726 case Builtins::kMin: {
SJW61531372020-06-09 07:31:08 -05004727 auto param_type = func_info.getParameter(0);
SJW2c317da2020-03-23 07:39:13 -05004728 if (param_type.type_id == Type::FloatTyID) {
4729 return glsl::ExtInst::ExtInstFMin;
4730 }
4731 return param_type.is_signed ? glsl::ExtInst::ExtInstSMin
4732 : glsl::ExtInst::ExtInstUMin;
4733 }
4734 case Builtins::kAbs:
4735 return glsl::ExtInst::ExtInstSAbs;
4736 case Builtins::kFmax:
Marco Antognini55d51862020-07-21 17:50:07 +01004737 return glsl::ExtInst::ExtInstNMax;
SJW2c317da2020-03-23 07:39:13 -05004738 case Builtins::kFmin:
Marco Antognini55d51862020-07-21 17:50:07 +01004739 return glsl::ExtInst::ExtInstNMin;
SJW2c317da2020-03-23 07:39:13 -05004740 case Builtins::kDegrees:
4741 return glsl::ExtInst::ExtInstDegrees;
4742 case Builtins::kRadians:
4743 return glsl::ExtInst::ExtInstRadians;
4744 case Builtins::kMix:
4745 return glsl::ExtInst::ExtInstFMix;
4746 case Builtins::kAcos:
4747 case Builtins::kAcospi:
4748 return glsl::ExtInst::ExtInstAcos;
4749 case Builtins::kAcosh:
4750 return glsl::ExtInst::ExtInstAcosh;
4751 case Builtins::kAsin:
4752 case Builtins::kAsinpi:
4753 return glsl::ExtInst::ExtInstAsin;
4754 case Builtins::kAsinh:
4755 return glsl::ExtInst::ExtInstAsinh;
4756 case Builtins::kAtan:
4757 case Builtins::kAtanpi:
4758 return glsl::ExtInst::ExtInstAtan;
4759 case Builtins::kAtanh:
4760 return glsl::ExtInst::ExtInstAtanh;
4761 case Builtins::kAtan2:
4762 case Builtins::kAtan2pi:
4763 return glsl::ExtInst::ExtInstAtan2;
4764 case Builtins::kCeil:
4765 return glsl::ExtInst::ExtInstCeil;
4766 case Builtins::kSin:
4767 case Builtins::kHalfSin:
4768 case Builtins::kNativeSin:
4769 return glsl::ExtInst::ExtInstSin;
4770 case Builtins::kSinh:
4771 return glsl::ExtInst::ExtInstSinh;
4772 case Builtins::kCos:
4773 case Builtins::kHalfCos:
4774 case Builtins::kNativeCos:
4775 return glsl::ExtInst::ExtInstCos;
4776 case Builtins::kCosh:
4777 return glsl::ExtInst::ExtInstCosh;
4778 case Builtins::kTan:
4779 case Builtins::kHalfTan:
4780 case Builtins::kNativeTan:
4781 return glsl::ExtInst::ExtInstTan;
4782 case Builtins::kTanh:
4783 return glsl::ExtInst::ExtInstTanh;
4784 case Builtins::kExp:
4785 case Builtins::kHalfExp:
4786 case Builtins::kNativeExp:
4787 return glsl::ExtInst::ExtInstExp;
4788 case Builtins::kExp2:
4789 case Builtins::kHalfExp2:
4790 case Builtins::kNativeExp2:
4791 return glsl::ExtInst::ExtInstExp2;
4792 case Builtins::kLog:
4793 case Builtins::kHalfLog:
4794 case Builtins::kNativeLog:
4795 return glsl::ExtInst::ExtInstLog;
4796 case Builtins::kLog2:
4797 case Builtins::kHalfLog2:
4798 case Builtins::kNativeLog2:
4799 return glsl::ExtInst::ExtInstLog2;
4800 case Builtins::kFabs:
4801 return glsl::ExtInst::ExtInstFAbs;
4802 case Builtins::kFma:
4803 return glsl::ExtInst::ExtInstFma;
4804 case Builtins::kFloor:
4805 return glsl::ExtInst::ExtInstFloor;
4806 case Builtins::kLdexp:
4807 return glsl::ExtInst::ExtInstLdexp;
4808 case Builtins::kPow:
4809 case Builtins::kPowr:
4810 case Builtins::kHalfPowr:
4811 case Builtins::kNativePowr:
4812 return glsl::ExtInst::ExtInstPow;
James Price38553362020-09-03 18:30:40 -04004813 case Builtins::kRint:
4814 return glsl::ExtInst::ExtInstRoundEven;
SJW2c317da2020-03-23 07:39:13 -05004815 case Builtins::kRound:
4816 return glsl::ExtInst::ExtInstRound;
4817 case Builtins::kSqrt:
4818 case Builtins::kHalfSqrt:
4819 case Builtins::kNativeSqrt:
4820 return glsl::ExtInst::ExtInstSqrt;
4821 case Builtins::kRsqrt:
4822 case Builtins::kHalfRsqrt:
4823 case Builtins::kNativeRsqrt:
4824 return glsl::ExtInst::ExtInstInverseSqrt;
4825 case Builtins::kTrunc:
4826 return glsl::ExtInst::ExtInstTrunc;
4827 case Builtins::kFrexp:
4828 return glsl::ExtInst::ExtInstFrexp;
SJW61531372020-06-09 07:31:08 -05004829 case Builtins::kClspvFract:
SJW2c317da2020-03-23 07:39:13 -05004830 case Builtins::kFract:
4831 return glsl::ExtInst::ExtInstFract;
4832 case Builtins::kSign:
4833 return glsl::ExtInst::ExtInstFSign;
4834 case Builtins::kLength:
4835 case Builtins::kFastLength:
4836 return glsl::ExtInst::ExtInstLength;
4837 case Builtins::kDistance:
4838 case Builtins::kFastDistance:
4839 return glsl::ExtInst::ExtInstDistance;
4840 case Builtins::kStep:
4841 return glsl::ExtInst::ExtInstStep;
4842 case Builtins::kSmoothstep:
4843 return glsl::ExtInst::ExtInstSmoothStep;
4844 case Builtins::kCross:
4845 return glsl::ExtInst::ExtInstCross;
4846 case Builtins::kNormalize:
4847 case Builtins::kFastNormalize:
4848 return glsl::ExtInst::ExtInstNormalize;
SJW61531372020-06-09 07:31:08 -05004849 case Builtins::kSpirvPack:
4850 return glsl::ExtInst::ExtInstPackHalf2x16;
4851 case Builtins::kSpirvUnpack:
4852 return glsl::ExtInst::ExtInstUnpackHalf2x16;
SJW2c317da2020-03-23 07:39:13 -05004853 default:
4854 break;
4855 }
4856
SJW61531372020-06-09 07:31:08 -05004857 if (func_info.getName().find("llvm.fmuladd.") == 0) {
4858 return glsl::ExtInst::ExtInstFma;
4859 }
4860 return kGlslExtInstBad;
David Neto3fbb4072017-10-16 11:28:14 -04004861}
4862
SJW61531372020-06-09 07:31:08 -05004863glsl::ExtInst SPIRVProducerPass::getIndirectExtInstEnum(
4864 const Builtins::FunctionInfo &func_info) {
4865 switch (func_info.getType()) {
SJW2c317da2020-03-23 07:39:13 -05004866 case Builtins::kClz:
4867 return glsl::ExtInst::ExtInstFindUMsb;
4868 case Builtins::kAcospi:
4869 return glsl::ExtInst::ExtInstAcos;
4870 case Builtins::kAsinpi:
4871 return glsl::ExtInst::ExtInstAsin;
4872 case Builtins::kAtanpi:
4873 return glsl::ExtInst::ExtInstAtan;
4874 case Builtins::kAtan2pi:
4875 return glsl::ExtInst::ExtInstAtan2;
4876 default:
4877 break;
4878 }
4879 return kGlslExtInstBad;
David Neto3fbb4072017-10-16 11:28:14 -04004880}
4881
SJW61531372020-06-09 07:31:08 -05004882glsl::ExtInst SPIRVProducerPass::getDirectOrIndirectExtInstEnum(
4883 const Builtins::FunctionInfo &func_info) {
4884 auto direct = getExtInstEnum(func_info);
David Neto3fbb4072017-10-16 11:28:14 -04004885 if (direct != kGlslExtInstBad)
4886 return direct;
SJW61531372020-06-09 07:31:08 -05004887 return getIndirectExtInstEnum(func_info);
David Neto22f144c2017-06-12 14:26:21 -04004888}
4889
David Neto22f144c2017-06-12 14:26:21 -04004890void SPIRVProducerPass::WriteOneWord(uint32_t Word) {
David Neto0676e6f2017-07-11 18:47:44 -04004891 binaryOut->write(reinterpret_cast<const char *>(&Word), sizeof(uint32_t));
David Neto22f144c2017-06-12 14:26:21 -04004892}
4893
SJW88ed5fe2020-05-11 12:40:57 -05004894void SPIRVProducerPass::WriteResultID(const SPIRVInstruction &Inst) {
SJW01901d92020-05-21 08:58:31 -05004895 WriteOneWord(Inst.getResultID().get());
David Neto22f144c2017-06-12 14:26:21 -04004896}
4897
SJW88ed5fe2020-05-11 12:40:57 -05004898void SPIRVProducerPass::WriteWordCountAndOpcode(const SPIRVInstruction &Inst) {
David Neto22f144c2017-06-12 14:26:21 -04004899 // High 16 bit : Word Count
4900 // Low 16 bit : Opcode
SJW88ed5fe2020-05-11 12:40:57 -05004901 uint32_t Word = Inst.getOpcode();
4902 const uint32_t count = Inst.getWordCount();
David Netoee2660d2018-06-28 16:31:29 -04004903 if (count > 65535) {
4904 errs() << "Word count limit of 65535 exceeded: " << count << "\n";
4905 llvm_unreachable("Word count too high");
4906 }
SJW88ed5fe2020-05-11 12:40:57 -05004907 Word |= Inst.getWordCount() << 16;
David Neto22f144c2017-06-12 14:26:21 -04004908 WriteOneWord(Word);
4909}
4910
SJW88ed5fe2020-05-11 12:40:57 -05004911void SPIRVProducerPass::WriteOperand(const SPIRVOperand &Op) {
4912 SPIRVOperandType OpTy = Op.getType();
David Neto22f144c2017-06-12 14:26:21 -04004913 switch (OpTy) {
4914 default: {
4915 llvm_unreachable("Unsupported SPIRV Operand Type???");
4916 break;
4917 }
4918 case SPIRVOperandType::NUMBERID: {
SJW88ed5fe2020-05-11 12:40:57 -05004919 WriteOneWord(Op.getNumID());
David Neto22f144c2017-06-12 14:26:21 -04004920 break;
4921 }
4922 case SPIRVOperandType::LITERAL_STRING: {
SJW88ed5fe2020-05-11 12:40:57 -05004923 std::string Str = Op.getLiteralStr();
David Neto22f144c2017-06-12 14:26:21 -04004924 const char *Data = Str.c_str();
4925 size_t WordSize = Str.size() / 4;
4926 for (unsigned Idx = 0; Idx < WordSize; Idx++) {
4927 WriteOneWord(*reinterpret_cast<const uint32_t *>(&Data[4 * Idx]));
4928 }
4929
4930 uint32_t Remainder = Str.size() % 4;
4931 uint32_t LastWord = 0;
4932 if (Remainder) {
4933 for (unsigned Idx = 0; Idx < Remainder; Idx++) {
4934 LastWord |= Data[4 * WordSize + Idx] << 8 * Idx;
4935 }
4936 }
4937
4938 WriteOneWord(LastWord);
4939 break;
4940 }
SJW88ed5fe2020-05-11 12:40:57 -05004941 case SPIRVOperandType::LITERAL_WORD: {
4942 WriteOneWord(Op.getLiteralNum()[0]);
4943 break;
4944 }
4945 case SPIRVOperandType::LITERAL_DWORD: {
4946 WriteOneWord(Op.getLiteralNum()[0]);
4947 WriteOneWord(Op.getLiteralNum()[1]);
David Neto22f144c2017-06-12 14:26:21 -04004948 break;
4949 }
4950 }
4951}
4952
4953void SPIRVProducerPass::WriteSPIRVBinary() {
SJW69939d52020-04-16 07:29:07 -05004954 for (int i = 0; i < kSectionCount; ++i) {
4955 WriteSPIRVBinary(SPIRVSections[i]);
4956 }
4957}
4958
4959void SPIRVProducerPass::WriteSPIRVBinary(SPIRVInstructionList &SPIRVInstList) {
SJW88ed5fe2020-05-11 12:40:57 -05004960 for (const auto &Inst : SPIRVInstList) {
4961 const auto &Ops = Inst.getOperands();
4962 spv::Op Opcode = static_cast<spv::Op>(Inst.getOpcode());
David Neto22f144c2017-06-12 14:26:21 -04004963
4964 switch (Opcode) {
4965 default: {
David Neto5c22a252018-03-15 16:07:41 -04004966 errs() << "Unsupported SPIR-V instruction opcode " << int(Opcode) << "\n";
David Neto22f144c2017-06-12 14:26:21 -04004967 llvm_unreachable("Unsupported SPIRV instruction");
4968 break;
4969 }
Marco Antognini68e5c512020-09-09 16:08:57 +01004970 case spv::OpUnreachable:
David Neto22f144c2017-06-12 14:26:21 -04004971 case spv::OpCapability:
4972 case spv::OpExtension:
4973 case spv::OpMemoryModel:
4974 case spv::OpEntryPoint:
4975 case spv::OpExecutionMode:
4976 case spv::OpSource:
4977 case spv::OpDecorate:
4978 case spv::OpMemberDecorate:
4979 case spv::OpBranch:
4980 case spv::OpBranchConditional:
4981 case spv::OpSelectionMerge:
4982 case spv::OpLoopMerge:
4983 case spv::OpStore:
4984 case spv::OpImageWrite:
4985 case spv::OpReturnValue:
4986 case spv::OpControlBarrier:
4987 case spv::OpMemoryBarrier:
4988 case spv::OpReturn:
4989 case spv::OpFunctionEnd:
4990 case spv::OpCopyMemory: {
4991 WriteWordCountAndOpcode(Inst);
4992 for (uint32_t i = 0; i < Ops.size(); i++) {
4993 WriteOperand(Ops[i]);
4994 }
4995 break;
4996 }
4997 case spv::OpTypeBool:
4998 case spv::OpTypeVoid:
4999 case spv::OpTypeSampler:
5000 case spv::OpLabel:
5001 case spv::OpExtInstImport:
5002 case spv::OpTypePointer:
5003 case spv::OpTypeRuntimeArray:
5004 case spv::OpTypeStruct:
5005 case spv::OpTypeImage:
5006 case spv::OpTypeSampledImage:
5007 case spv::OpTypeInt:
5008 case spv::OpTypeFloat:
5009 case spv::OpTypeArray:
5010 case spv::OpTypeVector:
alan-baker86ce19c2020-08-05 13:09:19 -04005011 case spv::OpTypeFunction:
5012 case spv::OpString: {
David Neto22f144c2017-06-12 14:26:21 -04005013 WriteWordCountAndOpcode(Inst);
5014 WriteResultID(Inst);
5015 for (uint32_t i = 0; i < Ops.size(); i++) {
5016 WriteOperand(Ops[i]);
5017 }
5018 break;
5019 }
5020 case spv::OpFunction:
5021 case spv::OpFunctionParameter:
5022 case spv::OpAccessChain:
5023 case spv::OpPtrAccessChain:
5024 case spv::OpInBoundsAccessChain:
5025 case spv::OpUConvert:
5026 case spv::OpSConvert:
5027 case spv::OpConvertFToU:
5028 case spv::OpConvertFToS:
5029 case spv::OpConvertUToF:
5030 case spv::OpConvertSToF:
5031 case spv::OpFConvert:
5032 case spv::OpConvertPtrToU:
5033 case spv::OpConvertUToPtr:
5034 case spv::OpBitcast:
alan-bakerc9c55ae2019-12-02 16:01:27 -05005035 case spv::OpFNegate:
David Neto22f144c2017-06-12 14:26:21 -04005036 case spv::OpIAdd:
5037 case spv::OpFAdd:
5038 case spv::OpISub:
5039 case spv::OpFSub:
5040 case spv::OpIMul:
5041 case spv::OpFMul:
5042 case spv::OpUDiv:
5043 case spv::OpSDiv:
5044 case spv::OpFDiv:
5045 case spv::OpUMod:
5046 case spv::OpSRem:
5047 case spv::OpFRem:
Kévin Petit8a560882019-03-21 15:24:34 +00005048 case spv::OpUMulExtended:
5049 case spv::OpSMulExtended:
David Neto22f144c2017-06-12 14:26:21 -04005050 case spv::OpBitwiseOr:
5051 case spv::OpBitwiseXor:
5052 case spv::OpBitwiseAnd:
David Netoa394f392017-08-26 20:45:29 -04005053 case spv::OpNot:
David Neto22f144c2017-06-12 14:26:21 -04005054 case spv::OpShiftLeftLogical:
5055 case spv::OpShiftRightLogical:
5056 case spv::OpShiftRightArithmetic:
5057 case spv::OpBitCount:
David Netoab03f432017-11-03 17:00:44 -04005058 case spv::OpCompositeConstruct:
David Neto22f144c2017-06-12 14:26:21 -04005059 case spv::OpCompositeExtract:
5060 case spv::OpVectorExtractDynamic:
5061 case spv::OpCompositeInsert:
David Neto0a2f98d2017-09-15 19:38:40 -04005062 case spv::OpCopyObject:
David Neto22f144c2017-06-12 14:26:21 -04005063 case spv::OpVectorInsertDynamic:
5064 case spv::OpVectorShuffle:
5065 case spv::OpIEqual:
5066 case spv::OpINotEqual:
5067 case spv::OpUGreaterThan:
5068 case spv::OpUGreaterThanEqual:
5069 case spv::OpULessThan:
5070 case spv::OpULessThanEqual:
5071 case spv::OpSGreaterThan:
5072 case spv::OpSGreaterThanEqual:
5073 case spv::OpSLessThan:
5074 case spv::OpSLessThanEqual:
5075 case spv::OpFOrdEqual:
5076 case spv::OpFOrdGreaterThan:
5077 case spv::OpFOrdGreaterThanEqual:
5078 case spv::OpFOrdLessThan:
5079 case spv::OpFOrdLessThanEqual:
5080 case spv::OpFOrdNotEqual:
5081 case spv::OpFUnordEqual:
5082 case spv::OpFUnordGreaterThan:
5083 case spv::OpFUnordGreaterThanEqual:
5084 case spv::OpFUnordLessThan:
5085 case spv::OpFUnordLessThanEqual:
5086 case spv::OpFUnordNotEqual:
5087 case spv::OpExtInst:
5088 case spv::OpIsInf:
5089 case spv::OpIsNan:
5090 case spv::OpAny:
5091 case spv::OpAll:
5092 case spv::OpUndef:
5093 case spv::OpConstantNull:
5094 case spv::OpLogicalOr:
5095 case spv::OpLogicalAnd:
5096 case spv::OpLogicalNot:
5097 case spv::OpLogicalNotEqual:
5098 case spv::OpConstantComposite:
5099 case spv::OpSpecConstantComposite:
5100 case spv::OpConstantTrue:
5101 case spv::OpConstantFalse:
5102 case spv::OpConstant:
5103 case spv::OpSpecConstant:
5104 case spv::OpVariable:
5105 case spv::OpFunctionCall:
5106 case spv::OpSampledImage:
alan-baker75090e42020-02-20 11:21:04 -05005107 case spv::OpImageFetch:
alan-bakerf6bc8252020-09-23 14:58:55 -04005108 case spv::OpImageRead:
David Neto22f144c2017-06-12 14:26:21 -04005109 case spv::OpImageSampleExplicitLod:
David Neto5c22a252018-03-15 16:07:41 -04005110 case spv::OpImageQuerySize:
alan-bakerce179f12019-12-06 19:02:22 -05005111 case spv::OpImageQuerySizeLod:
David Neto22f144c2017-06-12 14:26:21 -04005112 case spv::OpSelect:
5113 case spv::OpPhi:
5114 case spv::OpLoad:
5115 case spv::OpAtomicIAdd:
5116 case spv::OpAtomicISub:
5117 case spv::OpAtomicExchange:
5118 case spv::OpAtomicIIncrement:
5119 case spv::OpAtomicIDecrement:
5120 case spv::OpAtomicCompareExchange:
5121 case spv::OpAtomicUMin:
5122 case spv::OpAtomicSMin:
5123 case spv::OpAtomicUMax:
5124 case spv::OpAtomicSMax:
5125 case spv::OpAtomicAnd:
5126 case spv::OpAtomicOr:
5127 case spv::OpAtomicXor:
SJW806a5d82020-07-15 12:51:38 -05005128 case spv::OpDot:
5129 case spv::OpGroupNonUniformAll:
5130 case spv::OpGroupNonUniformAny:
5131 case spv::OpGroupNonUniformBroadcast:
5132 case spv::OpGroupNonUniformIAdd:
5133 case spv::OpGroupNonUniformFAdd:
5134 case spv::OpGroupNonUniformSMin:
5135 case spv::OpGroupNonUniformUMin:
5136 case spv::OpGroupNonUniformFMin:
5137 case spv::OpGroupNonUniformSMax:
5138 case spv::OpGroupNonUniformUMax:
5139 case spv::OpGroupNonUniformFMax: {
David Neto22f144c2017-06-12 14:26:21 -04005140 WriteWordCountAndOpcode(Inst);
5141 WriteOperand(Ops[0]);
5142 WriteResultID(Inst);
5143 for (uint32_t i = 1; i < Ops.size(); i++) {
5144 WriteOperand(Ops[i]);
5145 }
5146 break;
5147 }
5148 }
5149 }
5150}
Alan Baker9bf93fb2018-08-28 16:59:26 -04005151
alan-bakerb6b09dc2018-11-08 16:59:28 -05005152bool SPIRVProducerPass::IsTypeNullable(const Type *type) const {
Alan Baker9bf93fb2018-08-28 16:59:26 -04005153 switch (type->getTypeID()) {
alan-bakerb6b09dc2018-11-08 16:59:28 -05005154 case Type::HalfTyID:
5155 case Type::FloatTyID:
5156 case Type::DoubleTyID:
5157 case Type::IntegerTyID:
James Price59a1c752020-04-23 23:06:16 -04005158 case Type::FixedVectorTyID:
alan-bakerb6b09dc2018-11-08 16:59:28 -05005159 return true;
5160 case Type::PointerTyID: {
5161 const PointerType *pointer_type = cast<PointerType>(type);
5162 if (pointer_type->getPointerAddressSpace() !=
5163 AddressSpace::UniformConstant) {
5164 auto pointee_type = pointer_type->getPointerElementType();
5165 if (pointee_type->isStructTy() &&
5166 cast<StructType>(pointee_type)->isOpaque()) {
5167 // Images and samplers are not nullable.
5168 return false;
Alan Baker9bf93fb2018-08-28 16:59:26 -04005169 }
Alan Baker9bf93fb2018-08-28 16:59:26 -04005170 }
alan-bakerb6b09dc2018-11-08 16:59:28 -05005171 return true;
5172 }
5173 case Type::ArrayTyID:
alan-baker8eb435a2020-04-08 00:42:06 -04005174 return IsTypeNullable(type->getArrayElementType());
alan-bakerb6b09dc2018-11-08 16:59:28 -05005175 case Type::StructTyID: {
5176 const StructType *struct_type = cast<StructType>(type);
5177 // Images and samplers are not nullable.
5178 if (struct_type->isOpaque())
Alan Baker9bf93fb2018-08-28 16:59:26 -04005179 return false;
alan-bakerb6b09dc2018-11-08 16:59:28 -05005180 for (const auto element : struct_type->elements()) {
5181 if (!IsTypeNullable(element))
5182 return false;
5183 }
5184 return true;
5185 }
5186 default:
5187 return false;
Alan Baker9bf93fb2018-08-28 16:59:26 -04005188 }
5189}
Alan Bakerfcda9482018-10-02 17:09:59 -04005190
SJW77b87ad2020-04-21 14:37:52 -05005191void SPIRVProducerPass::PopulateUBOTypeMaps() {
Alan Bakerfcda9482018-10-02 17:09:59 -04005192 if (auto *offsets_md =
SJW77b87ad2020-04-21 14:37:52 -05005193 module->getNamedMetadata(clspv::RemappedTypeOffsetMetadataName())) {
Alan Bakerfcda9482018-10-02 17:09:59 -04005194 // Metdata is stored as key-value pair operands. The first element of each
5195 // operand is the type and the second is a vector of offsets.
5196 for (const auto *operand : offsets_md->operands()) {
5197 const auto *pair = cast<MDTuple>(operand);
5198 auto *type =
5199 cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType();
5200 const auto *offset_vector = cast<MDTuple>(pair->getOperand(1));
5201 std::vector<uint32_t> offsets;
5202 for (const Metadata *offset_md : offset_vector->operands()) {
5203 const auto *constant_md = cast<ConstantAsMetadata>(offset_md);
alan-bakerb6b09dc2018-11-08 16:59:28 -05005204 offsets.push_back(static_cast<uint32_t>(
5205 cast<ConstantInt>(constant_md->getValue())->getZExtValue()));
Alan Bakerfcda9482018-10-02 17:09:59 -04005206 }
5207 RemappedUBOTypeOffsets.insert(std::make_pair(type, offsets));
5208 }
5209 }
5210
5211 if (auto *sizes_md =
SJW77b87ad2020-04-21 14:37:52 -05005212 module->getNamedMetadata(clspv::RemappedTypeSizesMetadataName())) {
Alan Bakerfcda9482018-10-02 17:09:59 -04005213 // Metadata is stored as key-value pair operands. The first element of each
5214 // operand is the type and the second is a triple of sizes: type size in
5215 // bits, store size and alloc size.
5216 for (const auto *operand : sizes_md->operands()) {
5217 const auto *pair = cast<MDTuple>(operand);
5218 auto *type =
5219 cast<ConstantAsMetadata>(pair->getOperand(0))->getValue()->getType();
5220 const auto *size_triple = cast<MDTuple>(pair->getOperand(1));
5221 uint64_t type_size_in_bits =
5222 cast<ConstantInt>(
5223 cast<ConstantAsMetadata>(size_triple->getOperand(0))->getValue())
5224 ->getZExtValue();
5225 uint64_t type_store_size =
5226 cast<ConstantInt>(
5227 cast<ConstantAsMetadata>(size_triple->getOperand(1))->getValue())
5228 ->getZExtValue();
5229 uint64_t type_alloc_size =
5230 cast<ConstantInt>(
5231 cast<ConstantAsMetadata>(size_triple->getOperand(2))->getValue())
5232 ->getZExtValue();
5233 RemappedUBOTypeSizes.insert(std::make_pair(
5234 type, std::make_tuple(type_size_in_bits, type_store_size,
5235 type_alloc_size)));
5236 }
5237 }
5238}
5239
5240uint64_t SPIRVProducerPass::GetTypeSizeInBits(Type *type,
5241 const DataLayout &DL) {
5242 auto iter = RemappedUBOTypeSizes.find(type);
5243 if (iter != RemappedUBOTypeSizes.end()) {
5244 return std::get<0>(iter->second);
5245 }
5246
5247 return DL.getTypeSizeInBits(type);
5248}
5249
5250uint64_t SPIRVProducerPass::GetTypeStoreSize(Type *type, const DataLayout &DL) {
5251 auto iter = RemappedUBOTypeSizes.find(type);
5252 if (iter != RemappedUBOTypeSizes.end()) {
5253 return std::get<1>(iter->second);
5254 }
5255
5256 return DL.getTypeStoreSize(type);
5257}
5258
5259uint64_t SPIRVProducerPass::GetTypeAllocSize(Type *type, const DataLayout &DL) {
5260 auto iter = RemappedUBOTypeSizes.find(type);
5261 if (iter != RemappedUBOTypeSizes.end()) {
5262 return std::get<2>(iter->second);
5263 }
5264
5265 return DL.getTypeAllocSize(type);
5266}
alan-baker5b86ed72019-02-15 08:26:50 -05005267
Kévin Petitbbbda972020-03-03 19:16:31 +00005268uint32_t SPIRVProducerPass::GetExplicitLayoutStructMemberOffset(
5269 StructType *type, unsigned member, const DataLayout &DL) {
5270 const auto StructLayout = DL.getStructLayout(type);
5271 // Search for the correct offsets if this type was remapped.
5272 std::vector<uint32_t> *offsets = nullptr;
5273 auto iter = RemappedUBOTypeOffsets.find(type);
5274 if (iter != RemappedUBOTypeOffsets.end()) {
5275 offsets = &iter->second;
5276 }
5277 auto ByteOffset =
5278 static_cast<uint32_t>(StructLayout->getElementOffset(member));
5279 if (offsets) {
5280 ByteOffset = (*offsets)[member];
5281 }
5282
5283 return ByteOffset;
5284}
5285
Diego Novillo3cc8d7a2019-04-10 13:30:34 -04005286void SPIRVProducerPass::setVariablePointersCapabilities(
5287 unsigned address_space) {
alan-baker5b86ed72019-02-15 08:26:50 -05005288 if (GetStorageClass(address_space) == spv::StorageClassStorageBuffer) {
SJW01901d92020-05-21 08:58:31 -05005289 setVariablePointersStorageBuffer();
alan-baker5b86ed72019-02-15 08:26:50 -05005290 } else {
SJW01901d92020-05-21 08:58:31 -05005291 setVariablePointers();
alan-baker5b86ed72019-02-15 08:26:50 -05005292 }
5293}
5294
Diego Novillo3cc8d7a2019-04-10 13:30:34 -04005295Value *SPIRVProducerPass::GetBasePointer(Value *v) {
alan-baker5b86ed72019-02-15 08:26:50 -05005296 if (auto *gep = dyn_cast<GetElementPtrInst>(v)) {
5297 return GetBasePointer(gep->getPointerOperand());
5298 }
5299
5300 // Conservatively return |v|.
5301 return v;
5302}
5303
5304bool SPIRVProducerPass::sameResource(Value *lhs, Value *rhs) const {
5305 if (auto *lhs_call = dyn_cast<CallInst>(lhs)) {
5306 if (auto *rhs_call = dyn_cast<CallInst>(rhs)) {
alan-baker7506abb2020-09-10 15:02:55 -04005307 const auto &lhs_func_info =
5308 Builtins::Lookup(lhs_call->getCalledFunction());
5309 const auto &rhs_func_info =
5310 Builtins::Lookup(rhs_call->getCalledFunction());
SJW61531372020-06-09 07:31:08 -05005311 if (lhs_func_info.getType() == Builtins::kClspvResource &&
5312 rhs_func_info.getType() == Builtins::kClspvResource) {
alan-baker5b86ed72019-02-15 08:26:50 -05005313 // For resource accessors, match descriptor set and binding.
5314 if (lhs_call->getOperand(0) == rhs_call->getOperand(0) &&
5315 lhs_call->getOperand(1) == rhs_call->getOperand(1))
5316 return true;
SJW61531372020-06-09 07:31:08 -05005317 } else if (lhs_func_info.getType() == Builtins::kClspvLocal &&
5318 rhs_func_info.getType() == Builtins::kClspvLocal) {
alan-baker5b86ed72019-02-15 08:26:50 -05005319 // For workgroup resources, match spec id.
5320 if (lhs_call->getOperand(0) == rhs_call->getOperand(0))
5321 return true;
5322 }
5323 }
5324 }
5325
5326 return false;
5327}
5328
5329bool SPIRVProducerPass::selectFromSameObject(Instruction *inst) {
5330 assert(inst->getType()->isPointerTy());
5331 assert(GetStorageClass(inst->getType()->getPointerAddressSpace()) ==
5332 spv::StorageClassStorageBuffer);
5333 const bool hack_undef = clspv::Option::HackUndef();
5334 if (auto *select = dyn_cast<SelectInst>(inst)) {
5335 auto *true_base = GetBasePointer(select->getTrueValue());
5336 auto *false_base = GetBasePointer(select->getFalseValue());
5337
5338 if (true_base == false_base)
5339 return true;
5340
5341 // If either the true or false operand is a null, then we satisfy the same
5342 // object constraint.
5343 if (auto *true_cst = dyn_cast<Constant>(true_base)) {
5344 if (true_cst->isNullValue() || (hack_undef && isa<UndefValue>(true_base)))
5345 return true;
5346 }
5347
5348 if (auto *false_cst = dyn_cast<Constant>(false_base)) {
5349 if (false_cst->isNullValue() ||
5350 (hack_undef && isa<UndefValue>(false_base)))
5351 return true;
5352 }
5353
5354 if (sameResource(true_base, false_base))
5355 return true;
5356 } else if (auto *phi = dyn_cast<PHINode>(inst)) {
5357 Value *value = nullptr;
5358 bool ok = true;
5359 for (unsigned i = 0; ok && i != phi->getNumIncomingValues(); ++i) {
5360 auto *base = GetBasePointer(phi->getIncomingValue(i));
5361 // Null values satisfy the constraint of selecting of selecting from the
5362 // same object.
5363 if (!value) {
5364 if (auto *cst = dyn_cast<Constant>(base)) {
5365 if (!cst->isNullValue() && !(hack_undef && isa<UndefValue>(base)))
5366 value = base;
5367 } else {
5368 value = base;
5369 }
5370 } else if (base != value) {
5371 if (auto *base_cst = dyn_cast<Constant>(base)) {
5372 if (base_cst->isNullValue() || (hack_undef && isa<UndefValue>(base)))
5373 continue;
5374 }
5375
5376 if (sameResource(value, base))
5377 continue;
5378
5379 // Values don't represent the same base.
5380 ok = false;
5381 }
5382 }
5383
5384 return ok;
5385 }
5386
5387 // Conservatively return false.
5388 return false;
5389}
alan-bakere9308012019-03-15 10:25:13 -04005390
5391bool SPIRVProducerPass::CalledWithCoherentResource(Argument &Arg) {
5392 if (!Arg.getType()->isPointerTy() ||
5393 Arg.getType()->getPointerAddressSpace() != clspv::AddressSpace::Global) {
5394 // Only SSBOs need to be annotated as coherent.
5395 return false;
5396 }
5397
5398 DenseSet<Value *> visited;
5399 std::vector<Value *> stack;
5400 for (auto *U : Arg.getParent()->users()) {
5401 if (auto *call = dyn_cast<CallInst>(U)) {
5402 stack.push_back(call->getOperand(Arg.getArgNo()));
5403 }
5404 }
5405
5406 while (!stack.empty()) {
5407 Value *v = stack.back();
5408 stack.pop_back();
5409
5410 if (!visited.insert(v).second)
5411 continue;
5412
5413 auto *resource_call = dyn_cast<CallInst>(v);
5414 if (resource_call &&
SJW61531372020-06-09 07:31:08 -05005415 Builtins::Lookup(resource_call->getCalledFunction()).getType() ==
5416 Builtins::kClspvResource) {
alan-bakere9308012019-03-15 10:25:13 -04005417 // If this is a resource accessor function, check if the coherent operand
5418 // is set.
5419 const auto coherent =
5420 unsigned(dyn_cast<ConstantInt>(resource_call->getArgOperand(5))
5421 ->getZExtValue());
5422 if (coherent == 1)
5423 return true;
5424 } else if (auto *arg = dyn_cast<Argument>(v)) {
5425 // If this is a function argument, trace through its callers.
alan-bakere98f3f92019-04-08 15:06:36 -04005426 for (auto U : arg->getParent()->users()) {
alan-bakere9308012019-03-15 10:25:13 -04005427 if (auto *call = dyn_cast<CallInst>(U)) {
5428 stack.push_back(call->getOperand(arg->getArgNo()));
5429 }
5430 }
5431 } else if (auto *user = dyn_cast<User>(v)) {
5432 // If this is a user, traverse all operands that could lead to resource
5433 // variables.
5434 for (unsigned i = 0; i != user->getNumOperands(); ++i) {
5435 Value *operand = user->getOperand(i);
5436 if (operand->getType()->isPointerTy() &&
5437 operand->getType()->getPointerAddressSpace() ==
5438 clspv::AddressSpace::Global) {
5439 stack.push_back(operand);
5440 }
5441 }
5442 }
5443 }
5444
5445 // No coherent resource variables encountered.
5446 return false;
5447}
alan-baker06cad652019-12-03 17:56:47 -05005448
SJW77b87ad2020-04-21 14:37:52 -05005449void SPIRVProducerPass::PopulateStructuredCFGMaps() {
alan-baker06cad652019-12-03 17:56:47 -05005450 // First, track loop merges and continues.
5451 DenseSet<BasicBlock *> LoopMergesAndContinues;
SJW77b87ad2020-04-21 14:37:52 -05005452 for (auto &F : *module) {
alan-baker06cad652019-12-03 17:56:47 -05005453 if (F.isDeclaration())
5454 continue;
5455
5456 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>(F).getDomTree();
5457 const LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>(F).getLoopInfo();
5458 std::deque<BasicBlock *> order;
5459 DenseSet<BasicBlock *> visited;
5460 clspv::ComputeStructuredOrder(&*F.begin(), &DT, LI, &order, &visited);
5461
5462 for (auto BB : order) {
5463 auto terminator = BB->getTerminator();
5464 auto branch = dyn_cast<BranchInst>(terminator);
5465 if (LI.isLoopHeader(BB)) {
5466 auto L = LI.getLoopFor(BB);
5467 BasicBlock *ContinueBB = nullptr;
5468 BasicBlock *MergeBB = nullptr;
5469
5470 MergeBB = L->getExitBlock();
5471 if (!MergeBB) {
5472 // StructurizeCFG pass converts CFG into triangle shape and the cfg
5473 // has regions with single entry/exit. As a result, loop should not
5474 // have multiple exits.
5475 llvm_unreachable("Loop has multiple exits???");
5476 }
5477
5478 if (L->isLoopLatch(BB)) {
5479 ContinueBB = BB;
5480 } else {
5481 // From SPIR-V spec 2.11, Continue Target must dominate that back-edge
5482 // block.
5483 BasicBlock *Header = L->getHeader();
5484 BasicBlock *Latch = L->getLoopLatch();
5485 for (auto *loop_block : L->blocks()) {
5486 if (loop_block == Header) {
5487 continue;
5488 }
5489
5490 // Check whether block dominates block with back-edge.
5491 // The loop latch is the single block with a back-edge. If it was
5492 // possible, StructurizeCFG made the loop conform to this
5493 // requirement, otherwise |Latch| is a nullptr.
5494 if (DT.dominates(loop_block, Latch)) {
5495 ContinueBB = loop_block;
5496 }
5497 }
5498
5499 if (!ContinueBB) {
5500 llvm_unreachable("Wrong continue block from loop");
5501 }
5502 }
5503
5504 // Record the continue and merge blocks.
5505 MergeBlocks[BB] = MergeBB;
5506 ContinueBlocks[BB] = ContinueBB;
5507 LoopMergesAndContinues.insert(MergeBB);
5508 LoopMergesAndContinues.insert(ContinueBB);
5509 } else if (branch && branch->isConditional()) {
5510 auto L = LI.getLoopFor(BB);
5511 bool HasBackedge = false;
5512 while (L && !HasBackedge) {
5513 if (L->isLoopLatch(BB)) {
5514 HasBackedge = true;
5515 }
5516 L = L->getParentLoop();
5517 }
5518
5519 if (!HasBackedge) {
5520 // Only need a merge if the branch doesn't include a loop break or
5521 // continue.
5522 auto true_bb = branch->getSuccessor(0);
5523 auto false_bb = branch->getSuccessor(1);
5524 if (!LoopMergesAndContinues.count(true_bb) &&
5525 !LoopMergesAndContinues.count(false_bb)) {
5526 // StructurizeCFG pass already manipulated CFG. Just use false block
5527 // of branch instruction as merge block.
5528 MergeBlocks[BB] = false_bb;
5529 }
5530 }
5531 }
5532 }
5533 }
5534}
alan-baker86ce19c2020-08-05 13:09:19 -04005535
5536SPIRVID SPIRVProducerPass::getReflectionImport() {
5537 if (!ReflectionID.isValid()) {
5538 addSPIRVInst<kExtensions>(spv::OpExtension, "SPV_KHR_non_semantic_info");
5539 ReflectionID = addSPIRVInst<kImports>(spv::OpExtInstImport,
5540 "NonSemantic.ClspvReflection.1");
5541 }
5542 return ReflectionID;
5543}
5544
5545void SPIRVProducerPass::GenerateReflection() {
5546 GenerateKernelReflection();
5547 GeneratePushConstantReflection();
5548 GenerateSpecConstantReflection();
5549}
5550
5551void SPIRVProducerPass::GeneratePushConstantReflection() {
5552 if (auto GV = module->getGlobalVariable(clspv::PushConstantsVariableName())) {
5553 auto const &DL = module->getDataLayout();
5554 auto MD = GV->getMetadata(clspv::PushConstantsMetadataName());
5555 auto STy = cast<StructType>(GV->getValueType());
5556
5557 for (unsigned i = 0; i < STy->getNumElements(); i++) {
5558 auto pc = static_cast<clspv::PushConstant>(
5559 mdconst::extract<ConstantInt>(MD->getOperand(i))->getZExtValue());
5560 if (pc == PushConstant::KernelArgument)
5561 continue;
5562
5563 auto memberType = STy->getElementType(i);
5564 auto offset = GetExplicitLayoutStructMemberOffset(STy, i, DL);
5565 unsigned previousOffset = 0;
5566 if (i > 0) {
5567 previousOffset = GetExplicitLayoutStructMemberOffset(STy, i - 1, DL);
5568 }
5569 auto size = static_cast<uint32_t>(GetTypeSizeInBits(memberType, DL)) / 8;
5570 assert(isValidExplicitLayout(*module, STy, i,
5571 spv::StorageClassPushConstant, offset,
5572 previousOffset));
5573
5574 reflection::ExtInst pc_inst = reflection::ExtInstMax;
5575 switch (pc) {
5576 case PushConstant::GlobalOffset:
5577 pc_inst = reflection::ExtInstPushConstantGlobalOffset;
5578 break;
5579 case PushConstant::EnqueuedLocalSize:
5580 pc_inst = reflection::ExtInstPushConstantEnqueuedLocalSize;
5581 break;
5582 case PushConstant::GlobalSize:
5583 pc_inst = reflection::ExtInstPushConstantGlobalSize;
5584 break;
5585 case PushConstant::RegionOffset:
5586 pc_inst = reflection::ExtInstPushConstantRegionOffset;
5587 break;
5588 case PushConstant::NumWorkgroups:
5589 pc_inst = reflection::ExtInstPushConstantNumWorkgroups;
5590 break;
5591 case PushConstant::RegionGroupOffset:
5592 pc_inst = reflection::ExtInstPushConstantRegionGroupOffset;
5593 break;
5594 default:
5595 llvm_unreachable("Unhandled push constant");
5596 break;
5597 }
5598
5599 auto import_id = getReflectionImport();
5600 SPIRVOperandVec Ops;
5601 Ops << getSPIRVType(Type::getVoidTy(module->getContext())) << import_id
5602 << pc_inst << getSPIRVInt32Constant(offset)
5603 << getSPIRVInt32Constant(size);
5604 addSPIRVInst(spv::OpExtInst, Ops);
5605 }
5606 }
5607}
5608
5609void SPIRVProducerPass::GenerateSpecConstantReflection() {
5610 const uint32_t kMax = std::numeric_limits<uint32_t>::max();
5611 uint32_t wgsize_id[3] = {kMax, kMax, kMax};
5612 uint32_t global_offset_id[3] = {kMax, kMax, kMax};
5613 uint32_t work_dim_id = kMax;
5614 for (auto pair : clspv::GetSpecConstants(module)) {
5615 auto kind = pair.first;
5616 auto id = pair.second;
5617
5618 // Local memory size is only used for kernel arguments.
5619 if (kind == SpecConstant::kLocalMemorySize)
5620 continue;
5621
5622 switch (kind) {
5623 case SpecConstant::kWorkgroupSizeX:
5624 wgsize_id[0] = id;
5625 break;
5626 case SpecConstant::kWorkgroupSizeY:
5627 wgsize_id[1] = id;
5628 break;
5629 case SpecConstant::kWorkgroupSizeZ:
5630 wgsize_id[2] = id;
5631 break;
5632 case SpecConstant::kGlobalOffsetX:
5633 global_offset_id[0] = id;
5634 break;
5635 case SpecConstant::kGlobalOffsetY:
5636 global_offset_id[1] = id;
5637 break;
5638 case SpecConstant::kGlobalOffsetZ:
5639 global_offset_id[2] = id;
5640 break;
5641 case SpecConstant::kWorkDim:
5642 work_dim_id = id;
5643 break;
5644 default:
5645 llvm_unreachable("Unhandled spec constant");
5646 }
5647 }
5648
5649 auto import_id = getReflectionImport();
5650 auto void_id = getSPIRVType(Type::getVoidTy(module->getContext()));
5651 SPIRVOperandVec Ops;
5652 if (wgsize_id[0] != kMax) {
5653 assert(wgsize_id[1] != kMax);
5654 assert(wgsize_id[2] != kMax);
5655 Ops.clear();
5656 Ops << void_id << import_id << reflection::ExtInstSpecConstantWorkgroupSize
5657 << getSPIRVInt32Constant(wgsize_id[0])
5658 << getSPIRVInt32Constant(wgsize_id[1])
5659 << getSPIRVInt32Constant(wgsize_id[2]);
5660 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5661 }
5662 if (global_offset_id[0] != kMax) {
5663 assert(global_offset_id[1] != kMax);
5664 assert(global_offset_id[2] != kMax);
5665 Ops.clear();
5666 Ops << void_id << import_id << reflection::ExtInstSpecConstantGlobalOffset
5667 << getSPIRVInt32Constant(global_offset_id[0])
5668 << getSPIRVInt32Constant(global_offset_id[1])
5669 << getSPIRVInt32Constant(global_offset_id[2]);
5670 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5671 }
5672 if (work_dim_id != kMax) {
5673 Ops.clear();
5674 Ops << void_id << import_id << reflection::ExtInstSpecConstantWorkDim
5675 << getSPIRVInt32Constant(work_dim_id);
5676 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5677 }
5678}
5679
5680void SPIRVProducerPass::GenerateKernelReflection() {
5681 const auto &DL = module->getDataLayout();
5682 auto import_id = getReflectionImport();
5683 auto void_id = getSPIRVType(Type::getVoidTy(module->getContext()));
5684
5685 for (auto &F : *module) {
5686 if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) {
5687 continue;
5688 }
5689
5690 // OpString for the kernel name.
5691 auto kernel_name =
5692 addSPIRVInst<kDebug>(spv::OpString, F.getName().str().c_str());
5693
5694 // Kernel declaration
5695 // Ops[0] = void type
5696 // Ops[1] = reflection ext import
5697 // Ops[2] = function id
5698 // Ops[3] = kernel name
5699 SPIRVOperandVec Ops;
5700 Ops << void_id << import_id << reflection::ExtInstKernel << ValueMap[&F]
5701 << kernel_name;
5702 auto kernel_decl = addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5703
5704 // Generate the required workgroup size property if it was specified.
5705 if (const MDNode *MD = F.getMetadata("reqd_work_group_size")) {
5706 uint32_t CurXDimCst = static_cast<uint32_t>(
5707 mdconst::extract<ConstantInt>(MD->getOperand(0))->getZExtValue());
5708 uint32_t CurYDimCst = static_cast<uint32_t>(
5709 mdconst::extract<ConstantInt>(MD->getOperand(1))->getZExtValue());
5710 uint32_t CurZDimCst = static_cast<uint32_t>(
5711 mdconst::extract<ConstantInt>(MD->getOperand(2))->getZExtValue());
5712
5713 Ops.clear();
5714 Ops << void_id << import_id
5715 << reflection::ExtInstPropertyRequiredWorkgroupSize << kernel_decl
5716 << getSPIRVInt32Constant(CurXDimCst)
5717 << getSPIRVInt32Constant(CurYDimCst)
5718 << getSPIRVInt32Constant(CurZDimCst);
5719 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5720 }
5721
5722 auto &resource_var_at_index = FunctionToResourceVarsMap[&F];
5723 auto *func_ty = F.getFunctionType();
5724
5725 // If we've clustered POD arguments, then argument details are in metadata.
5726 // If an argument maps to a resource variable, then get descriptor set and
5727 // binding from the resource variable. Other info comes from the metadata.
5728 const auto *arg_map = F.getMetadata(clspv::KernelArgMapMetadataName());
5729 auto local_spec_id_md =
5730 module->getNamedMetadata(clspv::LocalSpecIdMetadataName());
5731 if (arg_map) {
5732 for (const auto &arg : arg_map->operands()) {
5733 const MDNode *arg_node = dyn_cast<MDNode>(arg.get());
5734 assert(arg_node->getNumOperands() == 6);
5735 const auto name =
5736 dyn_cast<MDString>(arg_node->getOperand(0))->getString();
5737 const auto old_index =
5738 dyn_extract<ConstantInt>(arg_node->getOperand(1))->getZExtValue();
5739 // Remapped argument index
5740 const int new_index = static_cast<int>(
5741 dyn_extract<ConstantInt>(arg_node->getOperand(2))->getSExtValue());
5742 const auto offset =
5743 dyn_extract<ConstantInt>(arg_node->getOperand(3))->getZExtValue();
5744 const auto size =
5745 dyn_extract<ConstantInt>(arg_node->getOperand(4))->getZExtValue();
5746 const auto argKind = clspv::GetArgKindFromName(
5747 dyn_cast<MDString>(arg_node->getOperand(5))->getString().str());
5748
5749 // If this is a local memory argument, find the right spec id for this
5750 // argument.
5751 int64_t spec_id = -1;
5752 if (argKind == clspv::ArgKind::Local) {
5753 for (auto spec_id_arg : local_spec_id_md->operands()) {
5754 if ((&F == dyn_cast<Function>(
5755 dyn_cast<ValueAsMetadata>(spec_id_arg->getOperand(0))
5756 ->getValue())) &&
5757 (static_cast<uint64_t>(new_index) ==
5758 mdconst::extract<ConstantInt>(spec_id_arg->getOperand(1))
5759 ->getZExtValue())) {
5760 spec_id =
5761 mdconst::extract<ConstantInt>(spec_id_arg->getOperand(2))
5762 ->getSExtValue();
5763 break;
5764 }
5765 }
5766 }
5767
5768 // Generate the specific argument instruction.
5769 const uint32_t ordinal = static_cast<uint32_t>(old_index);
5770 const uint32_t arg_offset = static_cast<uint32_t>(offset);
5771 const uint32_t arg_size = static_cast<uint32_t>(size);
5772 uint32_t elem_size = 0;
5773 uint32_t descriptor_set = 0;
5774 uint32_t binding = 0;
5775 if (spec_id > 0) {
5776 elem_size = static_cast<uint32_t>(
5777 GetTypeAllocSize(func_ty->getParamType(unsigned(new_index))
5778 ->getPointerElementType(),
5779 DL));
5780 } else if (new_index >= 0) {
5781 auto *info = resource_var_at_index[new_index];
5782 assert(info);
5783 descriptor_set = info->descriptor_set;
5784 binding = info->binding;
5785 }
5786 AddArgumentReflection(kernel_decl, name.str(), argKind, ordinal,
5787 descriptor_set, binding, arg_offset, arg_size,
5788 static_cast<uint32_t>(spec_id), elem_size);
5789 }
5790 } else {
5791 // There is no argument map.
5792 // Take descriptor info from the resource variable calls.
5793 // Take argument name and size from the arguments list.
5794
5795 SmallVector<Argument *, 4> arguments;
5796 for (auto &arg : F.args()) {
5797 arguments.push_back(&arg);
5798 }
5799
5800 unsigned arg_index = 0;
5801 for (auto *info : resource_var_at_index) {
5802 if (info) {
5803 auto arg = arguments[arg_index];
5804 unsigned arg_size = 0;
5805 if (info->arg_kind == clspv::ArgKind::Pod ||
5806 info->arg_kind == clspv::ArgKind::PodUBO ||
5807 info->arg_kind == clspv::ArgKind::PodPushConstant) {
5808 arg_size =
5809 static_cast<uint32_t>(DL.getTypeStoreSize(arg->getType()));
5810 }
5811
5812 // Local pointer arguments are unused in this case.
5813 // offset, spec_id and elem_size always 0.
5814 AddArgumentReflection(kernel_decl, arg->getName().str(),
5815 info->arg_kind, arg_index, info->descriptor_set,
5816 info->binding, 0, arg_size, 0, 0);
5817 }
5818 arg_index++;
5819 }
5820 // Generate mappings for pointer-to-local arguments.
5821 for (arg_index = 0; arg_index < arguments.size(); ++arg_index) {
5822 Argument *arg = arguments[arg_index];
5823 auto where = LocalArgSpecIds.find(arg);
5824 if (where != LocalArgSpecIds.end()) {
5825 auto &local_arg_info = LocalSpecIdInfoMap[where->second];
5826
5827 // descriptor_set, binding, offset and size are always 0.
5828 AddArgumentReflection(kernel_decl, arg->getName().str(),
5829 ArgKind::Local, arg_index, 0, 0, 0, 0,
5830 static_cast<uint32_t>(local_arg_info.spec_id),
5831 static_cast<uint32_t>(GetTypeAllocSize(
5832 local_arg_info.elem_type, DL)));
5833 }
5834 }
5835 }
5836 }
5837}
5838
5839void SPIRVProducerPass::AddArgumentReflection(
5840 SPIRVID kernel_decl, const std::string &name, clspv::ArgKind arg_kind,
5841 uint32_t ordinal, uint32_t descriptor_set, uint32_t binding,
5842 uint32_t offset, uint32_t size, uint32_t spec_id, uint32_t elem_size) {
5843 // Generate ArgumentInfo for this argument.
5844 // TODO: generate remaining optional operands.
5845 auto import_id = getReflectionImport();
5846 auto arg_name = addSPIRVInst<kDebug>(spv::OpString, name.c_str());
5847 auto void_id = getSPIRVType(Type::getVoidTy(module->getContext()));
5848 SPIRVOperandVec Ops;
5849 Ops << void_id << import_id << reflection::ExtInstArgumentInfo << arg_name;
5850 auto arg_info = addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5851
5852 Ops.clear();
5853 Ops << void_id << import_id;
5854 reflection::ExtInst ext_inst = reflection::ExtInstMax;
5855 // Determine the extended instruction.
5856 switch (arg_kind) {
5857 case clspv::ArgKind::Buffer:
5858 ext_inst = reflection::ExtInstArgumentStorageBuffer;
5859 break;
5860 case clspv::ArgKind::BufferUBO:
5861 ext_inst = reflection::ExtInstArgumentUniform;
5862 break;
5863 case clspv::ArgKind::Local:
5864 ext_inst = reflection::ExtInstArgumentWorkgroup;
5865 break;
5866 case clspv::ArgKind::Pod:
5867 ext_inst = reflection::ExtInstArgumentPodStorageBuffer;
5868 break;
5869 case clspv::ArgKind::PodUBO:
5870 ext_inst = reflection::ExtInstArgumentPodUniform;
5871 break;
5872 case clspv::ArgKind::PodPushConstant:
5873 ext_inst = reflection::ExtInstArgumentPodPushConstant;
5874 break;
alan-bakerf6bc8252020-09-23 14:58:55 -04005875 case clspv::ArgKind::SampledImage:
alan-baker86ce19c2020-08-05 13:09:19 -04005876 ext_inst = reflection::ExtInstArgumentSampledImage;
5877 break;
alan-bakerf6bc8252020-09-23 14:58:55 -04005878 case clspv::ArgKind::StorageImage:
alan-baker86ce19c2020-08-05 13:09:19 -04005879 ext_inst = reflection::ExtInstArgumentStorageImage;
5880 break;
5881 case clspv::ArgKind::Sampler:
5882 ext_inst = reflection::ExtInstArgumentSampler;
5883 break;
5884 default:
5885 llvm_unreachable("Unhandled argument reflection");
5886 break;
5887 }
5888 Ops << ext_inst << kernel_decl << getSPIRVInt32Constant(ordinal);
5889
5890 // Add descriptor set and binding for applicable arguments.
5891 switch (arg_kind) {
5892 case clspv::ArgKind::Buffer:
5893 case clspv::ArgKind::BufferUBO:
5894 case clspv::ArgKind::Pod:
5895 case clspv::ArgKind::PodUBO:
alan-bakerf6bc8252020-09-23 14:58:55 -04005896 case clspv::ArgKind::SampledImage:
5897 case clspv::ArgKind::StorageImage:
alan-baker86ce19c2020-08-05 13:09:19 -04005898 case clspv::ArgKind::Sampler:
5899 Ops << getSPIRVInt32Constant(descriptor_set)
5900 << getSPIRVInt32Constant(binding);
5901 break;
5902 default:
5903 break;
5904 }
5905
5906 // Add remaining operands for arguments.
5907 switch (arg_kind) {
5908 case clspv::ArgKind::Local:
5909 Ops << getSPIRVInt32Constant(spec_id) << getSPIRVInt32Constant(elem_size);
5910 break;
5911 case clspv::ArgKind::Pod:
5912 case clspv::ArgKind::PodUBO:
5913 case clspv::ArgKind::PodPushConstant:
5914 Ops << getSPIRVInt32Constant(offset) << getSPIRVInt32Constant(size);
5915 break;
5916 default:
5917 break;
5918 }
5919 Ops << arg_info;
5920 addSPIRVInst<kReflection>(spv::OpExtInst, Ops);
5921}