LLVM 22.0.0git
SPIRVModuleAnalysis.cpp
Go to the documentation of this file.
1//===- SPIRVModuleAnalysis.cpp - analysis of global instrs & regs - C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The analysis collects instructions that should be output at the module level
10// and performs the global register numbering.
11//
12// The results of this analysis are used in AsmPrinter to rename registers
13// globally and to output required instructions at the module level.
14//
15//===----------------------------------------------------------------------===//
16
17// TODO: uses or report_fatal_error (which is also deprecated) /
18// ReportFatalUsageError in this file should be refactored, as per LLVM
19// best practices, to rely on the Diagnostic infrastructure.
20
21#include "SPIRVModuleAnalysis.h"
24#include "SPIRV.h"
25#include "SPIRVSubtarget.h"
26#include "SPIRVTargetMachine.h"
27#include "SPIRVUtils.h"
28#include "llvm/ADT/STLExtras.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "spirv-module-analysis"
35
36static cl::opt<bool>
37 SPVDumpDeps("spv-dump-deps",
38 cl::desc("Dump MIR with SPIR-V dependencies info"),
39 cl::Optional, cl::init(false));
40
42 AvoidCapabilities("avoid-spirv-capabilities",
43 cl::desc("SPIR-V capabilities to avoid if there are "
44 "other options enabling a feature"),
46 cl::values(clEnumValN(SPIRV::Capability::Shader, "Shader",
47 "SPIR-V Shader capability")));
48// Use sets instead of cl::list to check "if contains" condition
53
55
56INITIALIZE_PASS(SPIRVModuleAnalysis, DEBUG_TYPE, "SPIRV module analysis", true,
57 true)
58
59// Retrieve an unsigned from an MDNode with a list of them as operands.
60static unsigned getMetadataUInt(MDNode *MdNode, unsigned OpIndex,
61 unsigned DefaultVal = 0) {
62 if (MdNode && OpIndex < MdNode->getNumOperands()) {
63 const auto &Op = MdNode->getOperand(OpIndex);
64 return mdconst::extract<ConstantInt>(Op)->getZExtValue();
65 }
66 return DefaultVal;
67}
68
70getSymbolicOperandRequirements(SPIRV::OperandCategory::OperandCategory Category,
71 unsigned i, const SPIRVSubtarget &ST,
73 // A set of capabilities to avoid if there is another option.
74 AvoidCapabilitiesSet AvoidCaps;
75 if (!ST.isShader())
76 AvoidCaps.S.insert(SPIRV::Capability::Shader);
77 else
78 AvoidCaps.S.insert(SPIRV::Capability::Kernel);
79
80 VersionTuple ReqMinVer = getSymbolicOperandMinVersion(Category, i);
81 VersionTuple ReqMaxVer = getSymbolicOperandMaxVersion(Category, i);
82 VersionTuple SPIRVVersion = ST.getSPIRVVersion();
83 bool MinVerOK = SPIRVVersion.empty() || SPIRVVersion >= ReqMinVer;
84 bool MaxVerOK =
85 ReqMaxVer.empty() || SPIRVVersion.empty() || SPIRVVersion <= ReqMaxVer;
87 ExtensionList ReqExts = getSymbolicOperandExtensions(Category, i);
88 if (ReqCaps.empty()) {
89 if (ReqExts.empty()) {
90 if (MinVerOK && MaxVerOK)
91 return {true, {}, {}, ReqMinVer, ReqMaxVer};
92 return {false, {}, {}, VersionTuple(), VersionTuple()};
93 }
94 } else if (MinVerOK && MaxVerOK) {
95 if (ReqCaps.size() == 1) {
96 auto Cap = ReqCaps[0];
97 if (Reqs.isCapabilityAvailable(Cap)) {
99 SPIRV::OperandCategory::CapabilityOperand, Cap));
100 return {true, {Cap}, std::move(ReqExts), ReqMinVer, ReqMaxVer};
101 }
102 } else {
103 // By SPIR-V specification: "If an instruction, enumerant, or other
104 // feature specifies multiple enabling capabilities, only one such
105 // capability needs to be declared to use the feature." However, one
106 // capability may be preferred over another. We use command line
107 // argument(s) and AvoidCapabilities to avoid selection of certain
108 // capabilities if there are other options.
109 CapabilityList UseCaps;
110 for (auto Cap : ReqCaps)
111 if (Reqs.isCapabilityAvailable(Cap))
112 UseCaps.push_back(Cap);
113 for (size_t i = 0, Sz = UseCaps.size(); i < Sz; ++i) {
114 auto Cap = UseCaps[i];
115 if (i == Sz - 1 || !AvoidCaps.S.contains(Cap)) {
117 SPIRV::OperandCategory::CapabilityOperand, Cap));
118 return {true, {Cap}, std::move(ReqExts), ReqMinVer, ReqMaxVer};
119 }
120 }
121 }
122 }
123 // If there are no capabilities, or we can't satisfy the version or
124 // capability requirements, use the list of extensions (if the subtarget
125 // can handle them all).
126 if (llvm::all_of(ReqExts, [&ST](const SPIRV::Extension::Extension &Ext) {
127 return ST.canUseExtension(Ext);
128 })) {
129 return {true,
130 {},
131 std::move(ReqExts),
132 VersionTuple(),
133 VersionTuple()}; // TODO: add versions to extensions.
134 }
135 return {false, {}, {}, VersionTuple(), VersionTuple()};
136}
137
138void SPIRVModuleAnalysis::setBaseInfo(const Module &M) {
139 MAI.MaxID = 0;
140 for (int i = 0; i < SPIRV::NUM_MODULE_SECTIONS; i++)
141 MAI.MS[i].clear();
142 MAI.RegisterAliasTable.clear();
143 MAI.InstrsToDelete.clear();
144 MAI.FuncMap.clear();
145 MAI.GlobalVarList.clear();
146 MAI.ExtInstSetMap.clear();
147 MAI.Reqs.clear();
148 MAI.Reqs.initAvailableCapabilities(*ST);
149
150 // TODO: determine memory model and source language from the configuratoin.
151 if (auto MemModel = M.getNamedMetadata("spirv.MemoryModel")) {
152 auto MemMD = MemModel->getOperand(0);
153 MAI.Addr = static_cast<SPIRV::AddressingModel::AddressingModel>(
154 getMetadataUInt(MemMD, 0));
155 MAI.Mem =
156 static_cast<SPIRV::MemoryModel::MemoryModel>(getMetadataUInt(MemMD, 1));
157 } else {
158 // TODO: Add support for VulkanMemoryModel.
159 MAI.Mem = ST->isShader() ? SPIRV::MemoryModel::GLSL450
160 : SPIRV::MemoryModel::OpenCL;
161 if (MAI.Mem == SPIRV::MemoryModel::OpenCL) {
162 unsigned PtrSize = ST->getPointerSize();
163 MAI.Addr = PtrSize == 32 ? SPIRV::AddressingModel::Physical32
164 : PtrSize == 64 ? SPIRV::AddressingModel::Physical64
165 : SPIRV::AddressingModel::Logical;
166 } else {
167 // TODO: Add support for PhysicalStorageBufferAddress.
168 MAI.Addr = SPIRV::AddressingModel::Logical;
169 }
170 }
171 // Get the OpenCL version number from metadata.
172 // TODO: support other source languages.
173 if (auto VerNode = M.getNamedMetadata("opencl.ocl.version")) {
174 MAI.SrcLang = SPIRV::SourceLanguage::OpenCL_C;
175 // Construct version literal in accordance with SPIRV-LLVM-Translator.
176 // TODO: support multiple OCL version metadata.
177 assert(VerNode->getNumOperands() > 0 && "Invalid SPIR");
178 auto VersionMD = VerNode->getOperand(0);
179 unsigned MajorNum = getMetadataUInt(VersionMD, 0, 2);
180 unsigned MinorNum = getMetadataUInt(VersionMD, 1);
181 unsigned RevNum = getMetadataUInt(VersionMD, 2);
182 // Prevent Major part of OpenCL version to be 0
183 MAI.SrcLangVersion =
184 (std::max(1U, MajorNum) * 100 + MinorNum) * 1000 + RevNum;
185 } else {
186 // If there is no information about OpenCL version we are forced to generate
187 // OpenCL 1.0 by default for the OpenCL environment to avoid puzzling
188 // run-times with Unknown/0.0 version output. For a reference, LLVM-SPIRV
189 // Translator avoids potential issues with run-times in a similar manner.
190 if (!ST->isShader()) {
191 MAI.SrcLang = SPIRV::SourceLanguage::OpenCL_CPP;
192 MAI.SrcLangVersion = 100000;
193 } else {
194 MAI.SrcLang = SPIRV::SourceLanguage::Unknown;
195 MAI.SrcLangVersion = 0;
196 }
197 }
198
199 if (auto ExtNode = M.getNamedMetadata("opencl.used.extensions")) {
200 for (unsigned I = 0, E = ExtNode->getNumOperands(); I != E; ++I) {
201 MDNode *MD = ExtNode->getOperand(I);
202 if (!MD || MD->getNumOperands() == 0)
203 continue;
204 for (unsigned J = 0, N = MD->getNumOperands(); J != N; ++J)
205 MAI.SrcExt.insert(cast<MDString>(MD->getOperand(J))->getString());
206 }
207 }
208
209 // Update required capabilities for this memory model, addressing model and
210 // source language.
211 MAI.Reqs.getAndAddRequirements(SPIRV::OperandCategory::MemoryModelOperand,
212 MAI.Mem, *ST);
213 MAI.Reqs.getAndAddRequirements(SPIRV::OperandCategory::SourceLanguageOperand,
214 MAI.SrcLang, *ST);
215 MAI.Reqs.getAndAddRequirements(SPIRV::OperandCategory::AddressingModelOperand,
216 MAI.Addr, *ST);
217
218 if (!ST->isShader()) {
219 // TODO: check if it's required by default.
220 MAI.ExtInstSetMap[static_cast<unsigned>(
221 SPIRV::InstructionSet::OpenCL_std)] = MAI.getNextIDRegister();
222 }
223}
224
225// Appends the signature of the decoration instructions that decorate R to
226// Signature.
227static void appendDecorationsForReg(const MachineRegisterInfo &MRI, Register R,
228 InstrSignature &Signature) {
229 for (MachineInstr &UseMI : MRI.use_instructions(R)) {
230 // We don't handle OpDecorateId because getting the register alias for the
231 // ID can cause problems, and we do not need it for now.
232 if (UseMI.getOpcode() != SPIRV::OpDecorate &&
233 UseMI.getOpcode() != SPIRV::OpMemberDecorate)
234 continue;
235
236 for (unsigned I = 0; I < UseMI.getNumOperands(); ++I) {
237 const MachineOperand &MO = UseMI.getOperand(I);
238 if (MO.isReg())
239 continue;
240 Signature.push_back(hash_value(MO));
241 }
242 }
243}
244
245// Returns a representation of an instruction as a vector of MachineOperand
246// hash values, see llvm::hash_value(const MachineOperand &MO) for details.
247// This creates a signature of the instruction with the same content
248// that MachineOperand::isIdenticalTo uses for comparison.
249static InstrSignature instrToSignature(const MachineInstr &MI,
251 bool UseDefReg) {
252 Register DefReg;
253 InstrSignature Signature{MI.getOpcode()};
254 for (unsigned i = 0; i < MI.getNumOperands(); ++i) {
255 // The only decorations that can be applied more than once to a given <id>
256 // or structure member are FuncParamAttr (38), UserSemantic (5635),
257 // CacheControlLoadINTEL (6442), and CacheControlStoreINTEL (6443). For all
258 // the rest of decorations, we will only add to the signature the Opcode,
259 // the id to which it applies, and the decoration id, disregarding any
260 // decoration flags. This will ensure that any subsequent decoration with
261 // the same id will be deemed as a duplicate. Then, at the call site, we
262 // will be able to handle duplicates in the best way.
263 unsigned Opcode = MI.getOpcode();
264 if ((Opcode == SPIRV::OpDecorate) && i >= 2) {
265 unsigned DecorationID = MI.getOperand(1).getImm();
266 if (DecorationID != SPIRV::Decoration::FuncParamAttr &&
267 DecorationID != SPIRV::Decoration::UserSemantic &&
268 DecorationID != SPIRV::Decoration::CacheControlLoadINTEL &&
269 DecorationID != SPIRV::Decoration::CacheControlStoreINTEL)
270 continue;
271 }
272 const MachineOperand &MO = MI.getOperand(i);
273 size_t h;
274 if (MO.isReg()) {
275 if (!UseDefReg && MO.isDef()) {
276 assert(!DefReg.isValid() && "Multiple def registers.");
277 DefReg = MO.getReg();
278 continue;
279 }
280 Register RegAlias = MAI.getRegisterAlias(MI.getMF(), MO.getReg());
281 if (!RegAlias.isValid()) {
282 LLVM_DEBUG({
283 dbgs() << "Unexpectedly, no global id found for the operand ";
284 MO.print(dbgs());
285 dbgs() << "\nInstruction: ";
286 MI.print(dbgs());
287 dbgs() << "\n";
288 });
289 report_fatal_error("All v-regs must have been mapped to global id's");
290 }
291 // mimic llvm::hash_value(const MachineOperand &MO)
292 h = hash_combine(MO.getType(), (unsigned)RegAlias, MO.getSubReg(),
293 MO.isDef());
294 } else {
295 h = hash_value(MO);
296 }
297 Signature.push_back(h);
298 }
299
300 if (DefReg.isValid()) {
301 // Decorations change the semantics of the current instruction. So two
302 // identical instruction with different decorations cannot be merged. That
303 // is why we add the decorations to the signature.
304 appendDecorationsForReg(MI.getMF()->getRegInfo(), DefReg, Signature);
305 }
306 return Signature;
307}
308
309bool SPIRVModuleAnalysis::isDeclSection(const MachineRegisterInfo &MRI,
310 const MachineInstr &MI) {
311 unsigned Opcode = MI.getOpcode();
312 switch (Opcode) {
313 case SPIRV::OpTypeForwardPointer:
314 // omit now, collect later
315 return false;
316 case SPIRV::OpVariable:
317 return static_cast<SPIRV::StorageClass::StorageClass>(
318 MI.getOperand(2).getImm()) != SPIRV::StorageClass::Function;
319 case SPIRV::OpFunction:
320 case SPIRV::OpFunctionParameter:
321 return true;
322 }
323 if (GR->hasConstFunPtr() && Opcode == SPIRV::OpUndef) {
324 Register DefReg = MI.getOperand(0).getReg();
325 for (MachineInstr &UseMI : MRI.use_instructions(DefReg)) {
326 if (UseMI.getOpcode() != SPIRV::OpConstantFunctionPointerINTEL)
327 continue;
328 // it's a dummy definition, FP constant refers to a function,
329 // and this is resolved in another way; let's skip this definition
330 assert(UseMI.getOperand(2).isReg() &&
331 UseMI.getOperand(2).getReg() == DefReg);
332 MAI.setSkipEmission(&MI);
333 return false;
334 }
335 }
336 return TII->isTypeDeclInstr(MI) || TII->isConstantInstr(MI) ||
337 TII->isInlineAsmDefInstr(MI);
338}
339
340// This is a special case of a function pointer refering to a possibly
341// forward function declaration. The operand is a dummy OpUndef that
342// requires a special treatment.
343void SPIRVModuleAnalysis::visitFunPtrUse(
344 Register OpReg, InstrGRegsMap &SignatureToGReg,
345 std::map<const Value *, unsigned> &GlobalToGReg, const MachineFunction *MF,
346 const MachineInstr &MI) {
347 const MachineOperand *OpFunDef =
348 GR->getFunctionDefinitionByUse(&MI.getOperand(2));
349 assert(OpFunDef && OpFunDef->isReg());
350 // find the actual function definition and number it globally in advance
351 const MachineInstr *OpDefMI = OpFunDef->getParent();
352 assert(OpDefMI && OpDefMI->getOpcode() == SPIRV::OpFunction);
353 const MachineFunction *FunDefMF = OpDefMI->getParent()->getParent();
354 const MachineRegisterInfo &FunDefMRI = FunDefMF->getRegInfo();
355 do {
356 visitDecl(FunDefMRI, SignatureToGReg, GlobalToGReg, FunDefMF, *OpDefMI);
357 OpDefMI = OpDefMI->getNextNode();
358 } while (OpDefMI && (OpDefMI->getOpcode() == SPIRV::OpFunction ||
359 OpDefMI->getOpcode() == SPIRV::OpFunctionParameter));
360 // associate the function pointer with the newly assigned global number
361 MCRegister GlobalFunDefReg =
362 MAI.getRegisterAlias(FunDefMF, OpFunDef->getReg());
363 assert(GlobalFunDefReg.isValid() &&
364 "Function definition must refer to a global register");
365 MAI.setRegisterAlias(MF, OpReg, GlobalFunDefReg);
366}
367
368// Depth first recursive traversal of dependencies. Repeated visits are guarded
369// by MAI.hasRegisterAlias().
370void SPIRVModuleAnalysis::visitDecl(
371 const MachineRegisterInfo &MRI, InstrGRegsMap &SignatureToGReg,
372 std::map<const Value *, unsigned> &GlobalToGReg, const MachineFunction *MF,
373 const MachineInstr &MI) {
374 unsigned Opcode = MI.getOpcode();
375
376 // Process each operand of the instruction to resolve dependencies
377 for (const MachineOperand &MO : MI.operands()) {
378 if (!MO.isReg() || MO.isDef())
379 continue;
380 Register OpReg = MO.getReg();
381 // Handle function pointers special case
382 if (Opcode == SPIRV::OpConstantFunctionPointerINTEL &&
383 MRI.getRegClass(OpReg) == &SPIRV::pIDRegClass) {
384 visitFunPtrUse(OpReg, SignatureToGReg, GlobalToGReg, MF, MI);
385 continue;
386 }
387 // Skip already processed instructions
388 if (MAI.hasRegisterAlias(MF, MO.getReg()))
389 continue;
390 // Recursively visit dependencies
391 if (const MachineInstr *OpDefMI = MRI.getUniqueVRegDef(OpReg)) {
392 if (isDeclSection(MRI, *OpDefMI))
393 visitDecl(MRI, SignatureToGReg, GlobalToGReg, MF, *OpDefMI);
394 continue;
395 }
396 // Handle the unexpected case of no unique definition for the SPIR-V
397 // instruction
398 LLVM_DEBUG({
399 dbgs() << "Unexpectedly, no unique definition for the operand ";
400 MO.print(dbgs());
401 dbgs() << "\nInstruction: ";
402 MI.print(dbgs());
403 dbgs() << "\n";
404 });
406 "No unique definition is found for the virtual register");
407 }
408
409 MCRegister GReg;
410 bool IsFunDef = false;
411 if (TII->isSpecConstantInstr(MI)) {
412 GReg = MAI.getNextIDRegister();
413 MAI.MS[SPIRV::MB_TypeConstVars].push_back(&MI);
414 } else if (Opcode == SPIRV::OpFunction ||
415 Opcode == SPIRV::OpFunctionParameter) {
416 GReg = handleFunctionOrParameter(MF, MI, GlobalToGReg, IsFunDef);
417 } else if (Opcode == SPIRV::OpTypeStruct ||
418 Opcode == SPIRV::OpConstantComposite) {
419 GReg = handleTypeDeclOrConstant(MI, SignatureToGReg);
420 const MachineInstr *NextInstr = MI.getNextNode();
421 while (NextInstr &&
422 ((Opcode == SPIRV::OpTypeStruct &&
423 NextInstr->getOpcode() == SPIRV::OpTypeStructContinuedINTEL) ||
424 (Opcode == SPIRV::OpConstantComposite &&
425 NextInstr->getOpcode() ==
426 SPIRV::OpConstantCompositeContinuedINTEL))) {
427 MCRegister Tmp = handleTypeDeclOrConstant(*NextInstr, SignatureToGReg);
428 MAI.setRegisterAlias(MF, NextInstr->getOperand(0).getReg(), Tmp);
429 MAI.setSkipEmission(NextInstr);
430 NextInstr = NextInstr->getNextNode();
431 }
432 } else if (TII->isTypeDeclInstr(MI) || TII->isConstantInstr(MI) ||
433 TII->isInlineAsmDefInstr(MI)) {
434 GReg = handleTypeDeclOrConstant(MI, SignatureToGReg);
435 } else if (Opcode == SPIRV::OpVariable) {
436 GReg = handleVariable(MF, MI, GlobalToGReg);
437 } else {
438 LLVM_DEBUG({
439 dbgs() << "\nInstruction: ";
440 MI.print(dbgs());
441 dbgs() << "\n";
442 });
443 llvm_unreachable("Unexpected instruction is visited");
444 }
445 MAI.setRegisterAlias(MF, MI.getOperand(0).getReg(), GReg);
446 if (!IsFunDef)
447 MAI.setSkipEmission(&MI);
448}
449
450MCRegister SPIRVModuleAnalysis::handleFunctionOrParameter(
451 const MachineFunction *MF, const MachineInstr &MI,
452 std::map<const Value *, unsigned> &GlobalToGReg, bool &IsFunDef) {
453 const Value *GObj = GR->getGlobalObject(MF, MI.getOperand(0).getReg());
454 assert(GObj && "Unregistered global definition");
455 const Function *F = dyn_cast<Function>(GObj);
456 if (!F)
457 F = dyn_cast<Argument>(GObj)->getParent();
458 assert(F && "Expected a reference to a function or an argument");
459 IsFunDef = !F->isDeclaration();
460 auto [It, Inserted] = GlobalToGReg.try_emplace(GObj);
461 if (!Inserted)
462 return It->second;
463 MCRegister GReg = MAI.getNextIDRegister();
464 It->second = GReg;
465 if (!IsFunDef)
466 MAI.MS[SPIRV::MB_ExtFuncDecls].push_back(&MI);
467 return GReg;
468}
469
471SPIRVModuleAnalysis::handleTypeDeclOrConstant(const MachineInstr &MI,
472 InstrGRegsMap &SignatureToGReg) {
473 InstrSignature MISign = instrToSignature(MI, MAI, false);
474 auto [It, Inserted] = SignatureToGReg.try_emplace(MISign);
475 if (!Inserted)
476 return It->second;
477 MCRegister GReg = MAI.getNextIDRegister();
478 It->second = GReg;
479 MAI.MS[SPIRV::MB_TypeConstVars].push_back(&MI);
480 return GReg;
481}
482
483MCRegister SPIRVModuleAnalysis::handleVariable(
484 const MachineFunction *MF, const MachineInstr &MI,
485 std::map<const Value *, unsigned> &GlobalToGReg) {
486 MAI.GlobalVarList.push_back(&MI);
487 const Value *GObj = GR->getGlobalObject(MF, MI.getOperand(0).getReg());
488 assert(GObj && "Unregistered global definition");
489 auto [It, Inserted] = GlobalToGReg.try_emplace(GObj);
490 if (!Inserted)
491 return It->second;
492 MCRegister GReg = MAI.getNextIDRegister();
493 It->second = GReg;
494 MAI.MS[SPIRV::MB_TypeConstVars].push_back(&MI);
495 return GReg;
496}
497
498void SPIRVModuleAnalysis::collectDeclarations(const Module &M) {
499 InstrGRegsMap SignatureToGReg;
500 std::map<const Value *, unsigned> GlobalToGReg;
501 for (const Function &F : M) {
502 MachineFunction *MF = MMI->getMachineFunction(F);
503 if (!MF)
504 continue;
505 const MachineRegisterInfo &MRI = MF->getRegInfo();
506 unsigned PastHeader = 0;
507 for (MachineBasicBlock &MBB : *MF) {
508 for (MachineInstr &MI : MBB) {
509 if (MI.getNumOperands() == 0)
510 continue;
511 unsigned Opcode = MI.getOpcode();
512 if (Opcode == SPIRV::OpFunction) {
513 if (PastHeader == 0) {
514 PastHeader = 1;
515 continue;
516 }
517 } else if (Opcode == SPIRV::OpFunctionParameter) {
518 if (PastHeader < 2)
519 continue;
520 } else if (PastHeader > 0) {
521 PastHeader = 2;
522 }
523
524 const MachineOperand &DefMO = MI.getOperand(0);
525 switch (Opcode) {
526 case SPIRV::OpExtension:
527 MAI.Reqs.addExtension(SPIRV::Extension::Extension(DefMO.getImm()));
528 MAI.setSkipEmission(&MI);
529 break;
530 case SPIRV::OpCapability:
531 MAI.Reqs.addCapability(SPIRV::Capability::Capability(DefMO.getImm()));
532 MAI.setSkipEmission(&MI);
533 if (PastHeader > 0)
534 PastHeader = 2;
535 break;
536 default:
537 if (DefMO.isReg() && isDeclSection(MRI, MI) &&
538 !MAI.hasRegisterAlias(MF, DefMO.getReg()))
539 visitDecl(MRI, SignatureToGReg, GlobalToGReg, MF, MI);
540 }
541 }
542 }
543 }
544}
545
546// Look for IDs declared with Import linkage, and map the corresponding function
547// to the register defining that variable (which will usually be the result of
548// an OpFunction). This lets us call externally imported functions using
549// the correct ID registers.
550void SPIRVModuleAnalysis::collectFuncNames(MachineInstr &MI,
551 const Function *F) {
552 if (MI.getOpcode() == SPIRV::OpDecorate) {
553 // If it's got Import linkage.
554 auto Dec = MI.getOperand(1).getImm();
555 if (Dec == SPIRV::Decoration::LinkageAttributes) {
556 auto Lnk = MI.getOperand(MI.getNumOperands() - 1).getImm();
557 if (Lnk == SPIRV::LinkageType::Import) {
558 // Map imported function name to function ID register.
559 const Function *ImportedFunc =
560 F->getParent()->getFunction(getStringImm(MI, 2));
561 Register Target = MI.getOperand(0).getReg();
562 MAI.FuncMap[ImportedFunc] = MAI.getRegisterAlias(MI.getMF(), Target);
563 }
564 }
565 } else if (MI.getOpcode() == SPIRV::OpFunction) {
566 // Record all internal OpFunction declarations.
567 Register Reg = MI.defs().begin()->getReg();
568 MCRegister GlobalReg = MAI.getRegisterAlias(MI.getMF(), Reg);
569 assert(GlobalReg.isValid());
570 MAI.FuncMap[F] = GlobalReg;
571 }
572}
573
574// Collect the given instruction in the specified MS. We assume global register
575// numbering has already occurred by this point. We can directly compare reg
576// arguments when detecting duplicates.
577static void collectOtherInstr(MachineInstr &MI, SPIRV::ModuleAnalysisInfo &MAI,
579 bool Append = true) {
580 MAI.setSkipEmission(&MI);
581 InstrSignature MISign = instrToSignature(MI, MAI, true);
582 auto FoundMI = IS.insert(std::move(MISign));
583 if (!FoundMI.second) {
584 if (MI.getOpcode() == SPIRV::OpDecorate) {
585 assert(MI.getNumOperands() >= 2 &&
586 "Decoration instructions must have at least 2 operands");
587 assert(MSType == SPIRV::MB_Annotations &&
588 "Only OpDecorate instructions can be duplicates");
589 // For FPFastMathMode decoration, we need to merge the flags of the
590 // duplicate decoration with the original one, so we need to find the
591 // original instruction that has the same signature. For the rest of
592 // instructions, we will simply skip the duplicate.
593 if (MI.getOperand(1).getImm() != SPIRV::Decoration::FPFastMathMode)
594 return; // Skip duplicates of other decorations.
595
596 const SPIRV::InstrList &Decorations = MAI.MS[MSType];
597 for (const MachineInstr *OrigMI : Decorations) {
598 if (instrToSignature(*OrigMI, MAI, true) == MISign) {
599 assert(OrigMI->getNumOperands() == MI.getNumOperands() &&
600 "Original instruction must have the same number of operands");
601 assert(
602 OrigMI->getNumOperands() == 3 &&
603 "FPFastMathMode decoration must have 3 operands for OpDecorate");
604 unsigned OrigFlags = OrigMI->getOperand(2).getImm();
605 unsigned NewFlags = MI.getOperand(2).getImm();
606 if (OrigFlags == NewFlags)
607 return; // No need to merge, the flags are the same.
608
609 // Emit warning about possible conflict between flags.
610 unsigned FinalFlags = OrigFlags | NewFlags;
611 llvm::errs()
612 << "Warning: Conflicting FPFastMathMode decoration flags "
613 "in instruction: "
614 << *OrigMI << "Original flags: " << OrigFlags
615 << ", new flags: " << NewFlags
616 << ". They will be merged on a best effort basis, but not "
617 "validated. Final flags: "
618 << FinalFlags << "\n";
619 MachineInstr *OrigMINonConst = const_cast<MachineInstr *>(OrigMI);
620 MachineOperand &OrigFlagsOp = OrigMINonConst->getOperand(2);
621 OrigFlagsOp = MachineOperand::CreateImm(FinalFlags);
622 return; // Merge done, so we found a duplicate; don't add it to MAI.MS
623 }
624 }
625 assert(false && "No original instruction found for the duplicate "
626 "OpDecorate, but we found one in IS.");
627 }
628 return; // insert failed, so we found a duplicate; don't add it to MAI.MS
629 }
630 // No duplicates, so add it.
631 if (Append)
632 MAI.MS[MSType].push_back(&MI);
633 else
634 MAI.MS[MSType].insert(MAI.MS[MSType].begin(), &MI);
635}
636
637// Some global instructions make reference to function-local ID regs, so cannot
638// be correctly collected until these registers are globally numbered.
639void SPIRVModuleAnalysis::processOtherInstrs(const Module &M) {
640 InstrTraces IS;
641 for (const Function &F : M) {
642 if (F.isDeclaration())
643 continue;
644 MachineFunction *MF = MMI->getMachineFunction(F);
645 assert(MF);
646
647 for (MachineBasicBlock &MBB : *MF)
648 for (MachineInstr &MI : MBB) {
649 if (MAI.getSkipEmission(&MI))
650 continue;
651 const unsigned OpCode = MI.getOpcode();
652 if (OpCode == SPIRV::OpString) {
653 collectOtherInstr(MI, MAI, SPIRV::MB_DebugStrings, IS);
654 } else if (OpCode == SPIRV::OpExtInst && MI.getOperand(2).isImm() &&
655 MI.getOperand(2).getImm() ==
656 SPIRV::InstructionSet::
657 NonSemantic_Shader_DebugInfo_100) {
658 MachineOperand Ins = MI.getOperand(3);
659 namespace NS = SPIRV::NonSemanticExtInst;
660 static constexpr int64_t GlobalNonSemanticDITy[] = {
661 NS::DebugSource, NS::DebugCompilationUnit, NS::DebugInfoNone,
662 NS::DebugTypeBasic, NS::DebugTypePointer};
663 bool IsGlobalDI = false;
664 for (unsigned Idx = 0; Idx < std::size(GlobalNonSemanticDITy); ++Idx)
665 IsGlobalDI |= Ins.getImm() == GlobalNonSemanticDITy[Idx];
666 if (IsGlobalDI)
667 collectOtherInstr(MI, MAI, SPIRV::MB_NonSemanticGlobalDI, IS);
668 } else if (OpCode == SPIRV::OpName || OpCode == SPIRV::OpMemberName) {
669 collectOtherInstr(MI, MAI, SPIRV::MB_DebugNames, IS);
670 } else if (OpCode == SPIRV::OpEntryPoint) {
671 collectOtherInstr(MI, MAI, SPIRV::MB_EntryPoints, IS);
672 } else if (TII->isAliasingInstr(MI)) {
673 collectOtherInstr(MI, MAI, SPIRV::MB_AliasingInsts, IS);
674 } else if (TII->isDecorationInstr(MI)) {
675 collectOtherInstr(MI, MAI, SPIRV::MB_Annotations, IS);
676 collectFuncNames(MI, &F);
677 } else if (TII->isConstantInstr(MI)) {
678 // Now OpSpecConstant*s are not in DT,
679 // but they need to be collected anyway.
680 collectOtherInstr(MI, MAI, SPIRV::MB_TypeConstVars, IS);
681 } else if (OpCode == SPIRV::OpFunction) {
682 collectFuncNames(MI, &F);
683 } else if (OpCode == SPIRV::OpTypeForwardPointer) {
684 collectOtherInstr(MI, MAI, SPIRV::MB_TypeConstVars, IS, false);
685 }
686 }
687 }
688}
689
690// Number registers in all functions globally from 0 onwards and store
691// the result in global register alias table. Some registers are already
692// numbered.
693void SPIRVModuleAnalysis::numberRegistersGlobally(const Module &M) {
694 for (const Function &F : M) {
695 if (F.isDeclaration())
696 continue;
697 MachineFunction *MF = MMI->getMachineFunction(F);
698 assert(MF);
699 for (MachineBasicBlock &MBB : *MF) {
700 for (MachineInstr &MI : MBB) {
701 for (MachineOperand &Op : MI.operands()) {
702 if (!Op.isReg())
703 continue;
704 Register Reg = Op.getReg();
705 if (MAI.hasRegisterAlias(MF, Reg))
706 continue;
707 MCRegister NewReg = MAI.getNextIDRegister();
708 MAI.setRegisterAlias(MF, Reg, NewReg);
709 }
710 if (MI.getOpcode() != SPIRV::OpExtInst)
711 continue;
712 auto Set = MI.getOperand(2).getImm();
713 auto [It, Inserted] = MAI.ExtInstSetMap.try_emplace(Set);
714 if (Inserted)
715 It->second = MAI.getNextIDRegister();
716 }
717 }
718 }
719}
720
721// RequirementHandler implementations.
723 SPIRV::OperandCategory::OperandCategory Category, uint32_t i,
724 const SPIRVSubtarget &ST) {
725 addRequirements(getSymbolicOperandRequirements(Category, i, ST, *this));
726}
727
728void SPIRV::RequirementHandler::recursiveAddCapabilities(
729 const CapabilityList &ToPrune) {
730 for (const auto &Cap : ToPrune) {
731 AllCaps.insert(Cap);
732 CapabilityList ImplicitDecls =
733 getSymbolicOperandCapabilities(OperandCategory::CapabilityOperand, Cap);
734 recursiveAddCapabilities(ImplicitDecls);
735 }
736}
737
739 for (const auto &Cap : ToAdd) {
740 bool IsNewlyInserted = AllCaps.insert(Cap).second;
741 if (!IsNewlyInserted) // Don't re-add if it's already been declared.
742 continue;
743 CapabilityList ImplicitDecls =
744 getSymbolicOperandCapabilities(OperandCategory::CapabilityOperand, Cap);
745 recursiveAddCapabilities(ImplicitDecls);
746 MinimalCaps.push_back(Cap);
747 }
748}
749
751 const SPIRV::Requirements &Req) {
752 if (!Req.IsSatisfiable)
753 report_fatal_error("Adding SPIR-V requirements this target can't satisfy.");
754
755 if (Req.Cap.has_value())
756 addCapabilities({Req.Cap.value()});
757
758 addExtensions(Req.Exts);
759
760 if (!Req.MinVer.empty()) {
761 if (!MaxVersion.empty() && Req.MinVer > MaxVersion) {
762 LLVM_DEBUG(dbgs() << "Conflicting version requirements: >= " << Req.MinVer
763 << " and <= " << MaxVersion << "\n");
764 report_fatal_error("Adding SPIR-V requirements that can't be satisfied.");
765 }
766
767 if (MinVersion.empty() || Req.MinVer > MinVersion)
768 MinVersion = Req.MinVer;
769 }
770
771 if (!Req.MaxVer.empty()) {
772 if (!MinVersion.empty() && Req.MaxVer < MinVersion) {
773 LLVM_DEBUG(dbgs() << "Conflicting version requirements: <= " << Req.MaxVer
774 << " and >= " << MinVersion << "\n");
775 report_fatal_error("Adding SPIR-V requirements that can't be satisfied.");
776 }
777
778 if (MaxVersion.empty() || Req.MaxVer < MaxVersion)
779 MaxVersion = Req.MaxVer;
780 }
781}
782
784 const SPIRVSubtarget &ST) const {
785 // Report as many errors as possible before aborting the compilation.
786 bool IsSatisfiable = true;
787 auto TargetVer = ST.getSPIRVVersion();
788
789 if (!MaxVersion.empty() && !TargetVer.empty() && MaxVersion < TargetVer) {
791 dbgs() << "Target SPIR-V version too high for required features\n"
792 << "Required max version: " << MaxVersion << " target version "
793 << TargetVer << "\n");
794 IsSatisfiable = false;
795 }
796
797 if (!MinVersion.empty() && !TargetVer.empty() && MinVersion > TargetVer) {
798 LLVM_DEBUG(dbgs() << "Target SPIR-V version too low for required features\n"
799 << "Required min version: " << MinVersion
800 << " target version " << TargetVer << "\n");
801 IsSatisfiable = false;
802 }
803
804 if (!MinVersion.empty() && !MaxVersion.empty() && MinVersion > MaxVersion) {
806 dbgs()
807 << "Version is too low for some features and too high for others.\n"
808 << "Required SPIR-V min version: " << MinVersion
809 << " required SPIR-V max version " << MaxVersion << "\n");
810 IsSatisfiable = false;
811 }
812
813 AvoidCapabilitiesSet AvoidCaps;
814 if (!ST.isShader())
815 AvoidCaps.S.insert(SPIRV::Capability::Shader);
816 else
817 AvoidCaps.S.insert(SPIRV::Capability::Kernel);
818
819 for (auto Cap : MinimalCaps) {
820 if (AvailableCaps.contains(Cap) && !AvoidCaps.S.contains(Cap))
821 continue;
822 LLVM_DEBUG(dbgs() << "Capability not supported: "
824 OperandCategory::CapabilityOperand, Cap)
825 << "\n");
826 IsSatisfiable = false;
827 }
828
829 for (auto Ext : AllExtensions) {
830 if (ST.canUseExtension(Ext))
831 continue;
832 LLVM_DEBUG(dbgs() << "Extension not supported: "
834 OperandCategory::ExtensionOperand, Ext)
835 << "\n");
836 IsSatisfiable = false;
837 }
838
839 if (!IsSatisfiable)
840 report_fatal_error("Unable to meet SPIR-V requirements for this target.");
841}
842
843// Add the given capabilities and all their implicitly defined capabilities too.
845 for (const auto Cap : ToAdd)
846 if (AvailableCaps.insert(Cap).second)
847 addAvailableCaps(getSymbolicOperandCapabilities(
848 SPIRV::OperandCategory::CapabilityOperand, Cap));
849}
850
852 const Capability::Capability ToRemove,
853 const Capability::Capability IfPresent) {
854 if (AllCaps.contains(IfPresent))
855 AllCaps.erase(ToRemove);
856}
857
858namespace llvm {
859namespace SPIRV {
860void RequirementHandler::initAvailableCapabilities(const SPIRVSubtarget &ST) {
861 // Provided by both all supported Vulkan versions and OpenCl.
862 addAvailableCaps({Capability::Shader, Capability::Linkage, Capability::Int8,
863 Capability::Int16});
864
865 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 3)))
866 addAvailableCaps({Capability::GroupNonUniform,
867 Capability::GroupNonUniformVote,
868 Capability::GroupNonUniformArithmetic,
869 Capability::GroupNonUniformBallot,
870 Capability::GroupNonUniformClustered,
871 Capability::GroupNonUniformShuffle,
872 Capability::GroupNonUniformShuffleRelative});
873
874 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 6)))
875 addAvailableCaps({Capability::DotProduct, Capability::DotProductInputAll,
876 Capability::DotProductInput4x8Bit,
877 Capability::DotProductInput4x8BitPacked,
878 Capability::DemoteToHelperInvocation});
879
880 // Add capabilities enabled by extensions.
881 for (auto Extension : ST.getAllAvailableExtensions()) {
882 CapabilityList EnabledCapabilities =
884 addAvailableCaps(EnabledCapabilities);
885 }
886
887 if (!ST.isShader()) {
888 initAvailableCapabilitiesForOpenCL(ST);
889 return;
890 }
891
892 if (ST.isShader()) {
893 initAvailableCapabilitiesForVulkan(ST);
894 return;
895 }
896
897 report_fatal_error("Unimplemented environment for SPIR-V generation.");
898}
899
900void RequirementHandler::initAvailableCapabilitiesForOpenCL(
901 const SPIRVSubtarget &ST) {
902 // Add the min requirements for different OpenCL and SPIR-V versions.
903 addAvailableCaps({Capability::Addresses, Capability::Float16Buffer,
904 Capability::Kernel, Capability::Vector16,
905 Capability::Groups, Capability::GenericPointer,
906 Capability::StorageImageWriteWithoutFormat,
907 Capability::StorageImageReadWithoutFormat});
908 if (ST.hasOpenCLFullProfile())
909 addAvailableCaps({Capability::Int64, Capability::Int64Atomics});
910 if (ST.hasOpenCLImageSupport()) {
911 addAvailableCaps({Capability::ImageBasic, Capability::LiteralSampler,
912 Capability::Image1D, Capability::SampledBuffer,
913 Capability::ImageBuffer});
914 if (ST.isAtLeastOpenCLVer(VersionTuple(2, 0)))
915 addAvailableCaps({Capability::ImageReadWrite});
916 }
917 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 1)) &&
918 ST.isAtLeastOpenCLVer(VersionTuple(2, 2)))
919 addAvailableCaps({Capability::SubgroupDispatch, Capability::PipeStorage});
920 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 4)))
921 addAvailableCaps({Capability::DenormPreserve, Capability::DenormFlushToZero,
922 Capability::SignedZeroInfNanPreserve,
923 Capability::RoundingModeRTE,
924 Capability::RoundingModeRTZ});
925 // TODO: verify if this needs some checks.
926 addAvailableCaps({Capability::Float16, Capability::Float64});
927
928 // TODO: add OpenCL extensions.
929}
930
931void RequirementHandler::initAvailableCapabilitiesForVulkan(
932 const SPIRVSubtarget &ST) {
933
934 // Core in Vulkan 1.1 and earlier.
935 addAvailableCaps({Capability::Int64, Capability::Float16, Capability::Float64,
936 Capability::GroupNonUniform, Capability::Image1D,
937 Capability::SampledBuffer, Capability::ImageBuffer,
938 Capability::UniformBufferArrayDynamicIndexing,
939 Capability::SampledImageArrayDynamicIndexing,
940 Capability::StorageBufferArrayDynamicIndexing,
941 Capability::StorageImageArrayDynamicIndexing,
942 Capability::DerivativeControl});
943
944 // Became core in Vulkan 1.2
945 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 5))) {
947 {Capability::ShaderNonUniformEXT, Capability::RuntimeDescriptorArrayEXT,
948 Capability::InputAttachmentArrayDynamicIndexingEXT,
949 Capability::UniformTexelBufferArrayDynamicIndexingEXT,
950 Capability::StorageTexelBufferArrayDynamicIndexingEXT,
951 Capability::UniformBufferArrayNonUniformIndexingEXT,
952 Capability::SampledImageArrayNonUniformIndexingEXT,
953 Capability::StorageBufferArrayNonUniformIndexingEXT,
954 Capability::StorageImageArrayNonUniformIndexingEXT,
955 Capability::InputAttachmentArrayNonUniformIndexingEXT,
956 Capability::UniformTexelBufferArrayNonUniformIndexingEXT,
957 Capability::StorageTexelBufferArrayNonUniformIndexingEXT});
958 }
959
960 // Became core in Vulkan 1.3
961 if (ST.isAtLeastSPIRVVer(VersionTuple(1, 6)))
962 addAvailableCaps({Capability::StorageImageWriteWithoutFormat,
963 Capability::StorageImageReadWithoutFormat});
964}
965
966} // namespace SPIRV
967} // namespace llvm
968
969// Add the required capabilities from a decoration instruction (including
970// BuiltIns).
971static void addOpDecorateReqs(const MachineInstr &MI, unsigned DecIndex,
973 const SPIRVSubtarget &ST) {
974 int64_t DecOp = MI.getOperand(DecIndex).getImm();
975 auto Dec = static_cast<SPIRV::Decoration::Decoration>(DecOp);
976 Reqs.addRequirements(getSymbolicOperandRequirements(
977 SPIRV::OperandCategory::DecorationOperand, Dec, ST, Reqs));
978
979 if (Dec == SPIRV::Decoration::BuiltIn) {
980 int64_t BuiltInOp = MI.getOperand(DecIndex + 1).getImm();
981 auto BuiltIn = static_cast<SPIRV::BuiltIn::BuiltIn>(BuiltInOp);
982 Reqs.addRequirements(getSymbolicOperandRequirements(
983 SPIRV::OperandCategory::BuiltInOperand, BuiltIn, ST, Reqs));
984 } else if (Dec == SPIRV::Decoration::LinkageAttributes) {
985 int64_t LinkageOp = MI.getOperand(MI.getNumOperands() - 1).getImm();
986 SPIRV::LinkageType::LinkageType LnkType =
987 static_cast<SPIRV::LinkageType::LinkageType>(LinkageOp);
988 if (LnkType == SPIRV::LinkageType::LinkOnceODR)
989 Reqs.addExtension(SPIRV::Extension::SPV_KHR_linkonce_odr);
990 } else if (Dec == SPIRV::Decoration::CacheControlLoadINTEL ||
991 Dec == SPIRV::Decoration::CacheControlStoreINTEL) {
992 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_cache_controls);
993 } else if (Dec == SPIRV::Decoration::HostAccessINTEL) {
994 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_global_variable_host_access);
995 } else if (Dec == SPIRV::Decoration::InitModeINTEL ||
996 Dec == SPIRV::Decoration::ImplementInRegisterMapINTEL) {
997 Reqs.addExtension(
998 SPIRV::Extension::SPV_INTEL_global_variable_fpga_decorations);
999 } else if (Dec == SPIRV::Decoration::NonUniformEXT) {
1000 Reqs.addRequirements(SPIRV::Capability::ShaderNonUniformEXT);
1001 } else if (Dec == SPIRV::Decoration::FPMaxErrorDecorationINTEL) {
1002 Reqs.addRequirements(SPIRV::Capability::FPMaxErrorINTEL);
1003 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_fp_max_error);
1004 } else if (Dec == SPIRV::Decoration::FPFastMathMode) {
1005 if (ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2)) {
1006 Reqs.addRequirements(SPIRV::Capability::FloatControls2);
1007 Reqs.addExtension(SPIRV::Extension::SPV_KHR_float_controls2);
1008 }
1009 }
1010}
1011
1012// Add requirements for image handling.
1013static void addOpTypeImageReqs(const MachineInstr &MI,
1015 const SPIRVSubtarget &ST) {
1016 assert(MI.getNumOperands() >= 8 && "Insufficient operands for OpTypeImage");
1017 // The operand indices used here are based on the OpTypeImage layout, which
1018 // the MachineInstr follows as well.
1019 int64_t ImgFormatOp = MI.getOperand(7).getImm();
1020 auto ImgFormat = static_cast<SPIRV::ImageFormat::ImageFormat>(ImgFormatOp);
1021 Reqs.getAndAddRequirements(SPIRV::OperandCategory::ImageFormatOperand,
1022 ImgFormat, ST);
1023
1024 bool IsArrayed = MI.getOperand(4).getImm() == 1;
1025 bool IsMultisampled = MI.getOperand(5).getImm() == 1;
1026 bool NoSampler = MI.getOperand(6).getImm() == 2;
1027 // Add dimension requirements.
1028 assert(MI.getOperand(2).isImm());
1029 switch (MI.getOperand(2).getImm()) {
1030 case SPIRV::Dim::DIM_1D:
1031 Reqs.addRequirements(NoSampler ? SPIRV::Capability::Image1D
1032 : SPIRV::Capability::Sampled1D);
1033 break;
1034 case SPIRV::Dim::DIM_2D:
1035 if (IsMultisampled && NoSampler)
1036 Reqs.addRequirements(SPIRV::Capability::ImageMSArray);
1037 break;
1038 case SPIRV::Dim::DIM_Cube:
1039 Reqs.addRequirements(SPIRV::Capability::Shader);
1040 if (IsArrayed)
1041 Reqs.addRequirements(NoSampler ? SPIRV::Capability::ImageCubeArray
1042 : SPIRV::Capability::SampledCubeArray);
1043 break;
1044 case SPIRV::Dim::DIM_Rect:
1045 Reqs.addRequirements(NoSampler ? SPIRV::Capability::ImageRect
1046 : SPIRV::Capability::SampledRect);
1047 break;
1048 case SPIRV::Dim::DIM_Buffer:
1049 Reqs.addRequirements(NoSampler ? SPIRV::Capability::ImageBuffer
1050 : SPIRV::Capability::SampledBuffer);
1051 break;
1052 case SPIRV::Dim::DIM_SubpassData:
1053 Reqs.addRequirements(SPIRV::Capability::InputAttachment);
1054 break;
1055 }
1056
1057 // Has optional access qualifier.
1058 if (!ST.isShader()) {
1059 if (MI.getNumOperands() > 8 &&
1060 MI.getOperand(8).getImm() == SPIRV::AccessQualifier::ReadWrite)
1061 Reqs.addRequirements(SPIRV::Capability::ImageReadWrite);
1062 else
1063 Reqs.addRequirements(SPIRV::Capability::ImageBasic);
1064 }
1065}
1066
1067static bool isBFloat16Type(const SPIRVType *TypeDef) {
1068 return TypeDef && TypeDef->getNumOperands() == 3 &&
1069 TypeDef->getOpcode() == SPIRV::OpTypeFloat &&
1070 TypeDef->getOperand(1).getImm() == 16 &&
1071 TypeDef->getOperand(2).getImm() == SPIRV::FPEncoding::BFloat16KHR;
1072}
1073
1074// Add requirements for handling atomic float instructions
1075#define ATOM_FLT_REQ_EXT_MSG(ExtName) \
1076 "The atomic float instruction requires the following SPIR-V " \
1077 "extension: SPV_EXT_shader_atomic_float" ExtName
1078static void AddAtomicVectorFloatRequirements(const MachineInstr &MI,
1080 const SPIRVSubtarget &ST) {
1081 SPIRVType *VecTypeDef =
1082 MI.getMF()->getRegInfo().getVRegDef(MI.getOperand(1).getReg());
1083
1084 const unsigned Rank = VecTypeDef->getOperand(2).getImm();
1085 if (Rank != 2 && Rank != 4)
1086 reportFatalUsageError("Result type of an atomic vector float instruction "
1087 "must be a 2-component or 4 component vector");
1088
1089 SPIRVType *EltTypeDef =
1090 MI.getMF()->getRegInfo().getVRegDef(VecTypeDef->getOperand(1).getReg());
1091
1092 if (EltTypeDef->getOpcode() != SPIRV::OpTypeFloat ||
1093 EltTypeDef->getOperand(1).getImm() != 16)
1095 "The element type for the result type of an atomic vector float "
1096 "instruction must be a 16-bit floating-point scalar");
1097
1098 if (isBFloat16Type(EltTypeDef))
1100 "The element type for the result type of an atomic vector float "
1101 "instruction cannot be a bfloat16 scalar");
1102 if (!ST.canUseExtension(SPIRV::Extension::SPV_NV_shader_atomic_fp16_vector))
1104 "The atomic float16 vector instruction requires the following SPIR-V "
1105 "extension: SPV_NV_shader_atomic_fp16_vector");
1106
1107 Reqs.addExtension(SPIRV::Extension::SPV_NV_shader_atomic_fp16_vector);
1108 Reqs.addCapability(SPIRV::Capability::AtomicFloat16VectorNV);
1109}
1110
1111static void AddAtomicFloatRequirements(const MachineInstr &MI,
1113 const SPIRVSubtarget &ST) {
1114 assert(MI.getOperand(1).isReg() &&
1115 "Expect register operand in atomic float instruction");
1116 Register TypeReg = MI.getOperand(1).getReg();
1117 SPIRVType *TypeDef = MI.getMF()->getRegInfo().getVRegDef(TypeReg);
1118
1119 if (TypeDef->getOpcode() == SPIRV::OpTypeVector)
1120 return AddAtomicVectorFloatRequirements(MI, Reqs, ST);
1121
1122 if (TypeDef->getOpcode() != SPIRV::OpTypeFloat)
1123 report_fatal_error("Result type of an atomic float instruction must be a "
1124 "floating-point type scalar");
1125
1126 unsigned BitWidth = TypeDef->getOperand(1).getImm();
1127 unsigned Op = MI.getOpcode();
1128 if (Op == SPIRV::OpAtomicFAddEXT) {
1129 if (!ST.canUseExtension(SPIRV::Extension::SPV_EXT_shader_atomic_float_add))
1131 Reqs.addExtension(SPIRV::Extension::SPV_EXT_shader_atomic_float_add);
1132 switch (BitWidth) {
1133 case 16:
1134 if (isBFloat16Type(TypeDef)) {
1135 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_16bit_atomics))
1137 "The atomic bfloat16 instruction requires the following SPIR-V "
1138 "extension: SPV_INTEL_16bit_atomics",
1139 false);
1140 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_16bit_atomics);
1141 Reqs.addCapability(SPIRV::Capability::AtomicBFloat16AddINTEL);
1142 } else {
1143 if (!ST.canUseExtension(
1144 SPIRV::Extension::SPV_EXT_shader_atomic_float16_add))
1145 report_fatal_error(ATOM_FLT_REQ_EXT_MSG("16_add"), false);
1146 Reqs.addExtension(SPIRV::Extension::SPV_EXT_shader_atomic_float16_add);
1147 Reqs.addCapability(SPIRV::Capability::AtomicFloat16AddEXT);
1148 }
1149 break;
1150 case 32:
1151 Reqs.addCapability(SPIRV::Capability::AtomicFloat32AddEXT);
1152 break;
1153 case 64:
1154 Reqs.addCapability(SPIRV::Capability::AtomicFloat64AddEXT);
1155 break;
1156 default:
1158 "Unexpected floating-point type width in atomic float instruction");
1159 }
1160 } else {
1161 if (!ST.canUseExtension(
1162 SPIRV::Extension::SPV_EXT_shader_atomic_float_min_max))
1163 report_fatal_error(ATOM_FLT_REQ_EXT_MSG("_min_max"), false);
1164 Reqs.addExtension(SPIRV::Extension::SPV_EXT_shader_atomic_float_min_max);
1165 switch (BitWidth) {
1166 case 16:
1167 if (isBFloat16Type(TypeDef)) {
1168 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_16bit_atomics))
1170 "The atomic bfloat16 instruction requires the following SPIR-V "
1171 "extension: SPV_INTEL_16bit_atomics",
1172 false);
1173 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_16bit_atomics);
1174 Reqs.addCapability(SPIRV::Capability::AtomicBFloat16MinMaxINTEL);
1175 } else {
1176 Reqs.addCapability(SPIRV::Capability::AtomicFloat16MinMaxEXT);
1177 }
1178 break;
1179 case 32:
1180 Reqs.addCapability(SPIRV::Capability::AtomicFloat32MinMaxEXT);
1181 break;
1182 case 64:
1183 Reqs.addCapability(SPIRV::Capability::AtomicFloat64MinMaxEXT);
1184 break;
1185 default:
1187 "Unexpected floating-point type width in atomic float instruction");
1188 }
1189 }
1190}
1191
1192bool isUniformTexelBuffer(MachineInstr *ImageInst) {
1193 if (ImageInst->getOpcode() != SPIRV::OpTypeImage)
1194 return false;
1195 uint32_t Dim = ImageInst->getOperand(2).getImm();
1196 uint32_t Sampled = ImageInst->getOperand(6).getImm();
1197 return Dim == SPIRV::Dim::DIM_Buffer && Sampled == 1;
1198}
1199
1200bool isStorageTexelBuffer(MachineInstr *ImageInst) {
1201 if (ImageInst->getOpcode() != SPIRV::OpTypeImage)
1202 return false;
1203 uint32_t Dim = ImageInst->getOperand(2).getImm();
1204 uint32_t Sampled = ImageInst->getOperand(6).getImm();
1205 return Dim == SPIRV::Dim::DIM_Buffer && Sampled == 2;
1206}
1207
1208bool isSampledImage(MachineInstr *ImageInst) {
1209 if (ImageInst->getOpcode() != SPIRV::OpTypeImage)
1210 return false;
1211 uint32_t Dim = ImageInst->getOperand(2).getImm();
1212 uint32_t Sampled = ImageInst->getOperand(6).getImm();
1213 return Dim != SPIRV::Dim::DIM_Buffer && Sampled == 1;
1214}
1215
1216bool isInputAttachment(MachineInstr *ImageInst) {
1217 if (ImageInst->getOpcode() != SPIRV::OpTypeImage)
1218 return false;
1219 uint32_t Dim = ImageInst->getOperand(2).getImm();
1220 uint32_t Sampled = ImageInst->getOperand(6).getImm();
1221 return Dim == SPIRV::Dim::DIM_SubpassData && Sampled == 2;
1222}
1223
1224bool isStorageImage(MachineInstr *ImageInst) {
1225 if (ImageInst->getOpcode() != SPIRV::OpTypeImage)
1226 return false;
1227 uint32_t Dim = ImageInst->getOperand(2).getImm();
1228 uint32_t Sampled = ImageInst->getOperand(6).getImm();
1229 return Dim != SPIRV::Dim::DIM_Buffer && Sampled == 2;
1230}
1231
1232bool isCombinedImageSampler(MachineInstr *SampledImageInst) {
1233 if (SampledImageInst->getOpcode() != SPIRV::OpTypeSampledImage)
1234 return false;
1235
1236 const MachineRegisterInfo &MRI = SampledImageInst->getMF()->getRegInfo();
1237 Register ImageReg = SampledImageInst->getOperand(1).getReg();
1238 auto *ImageInst = MRI.getUniqueVRegDef(ImageReg);
1239 return isSampledImage(ImageInst);
1240}
1241
1242bool hasNonUniformDecoration(Register Reg, const MachineRegisterInfo &MRI) {
1243 for (const auto &MI : MRI.reg_instructions(Reg)) {
1244 if (MI.getOpcode() != SPIRV::OpDecorate)
1245 continue;
1246
1247 uint32_t Dec = MI.getOperand(1).getImm();
1248 if (Dec == SPIRV::Decoration::NonUniformEXT)
1249 return true;
1250 }
1251 return false;
1252}
1253
1254void addOpAccessChainReqs(const MachineInstr &Instr,
1256 const SPIRVSubtarget &Subtarget) {
1257 const MachineRegisterInfo &MRI = Instr.getMF()->getRegInfo();
1258 // Get the result type. If it is an image type, then the shader uses
1259 // descriptor indexing. The appropriate capabilities will be added based
1260 // on the specifics of the image.
1261 Register ResTypeReg = Instr.getOperand(1).getReg();
1262 MachineInstr *ResTypeInst = MRI.getUniqueVRegDef(ResTypeReg);
1263
1264 assert(ResTypeInst->getOpcode() == SPIRV::OpTypePointer);
1265 uint32_t StorageClass = ResTypeInst->getOperand(1).getImm();
1266 if (StorageClass != SPIRV::StorageClass::StorageClass::UniformConstant &&
1267 StorageClass != SPIRV::StorageClass::StorageClass::Uniform &&
1268 StorageClass != SPIRV::StorageClass::StorageClass::StorageBuffer) {
1269 return;
1270 }
1271
1272 bool IsNonUniform =
1273 hasNonUniformDecoration(Instr.getOperand(0).getReg(), MRI);
1274
1275 auto FirstIndexReg = Instr.getOperand(3).getReg();
1276 bool FirstIndexIsConstant =
1277 Subtarget.getInstrInfo()->isConstantInstr(*MRI.getVRegDef(FirstIndexReg));
1278
1279 if (StorageClass == SPIRV::StorageClass::StorageClass::StorageBuffer) {
1280 if (IsNonUniform)
1281 Handler.addRequirements(
1282 SPIRV::Capability::StorageBufferArrayNonUniformIndexingEXT);
1283 else if (!FirstIndexIsConstant)
1284 Handler.addRequirements(
1285 SPIRV::Capability::StorageBufferArrayDynamicIndexing);
1286 return;
1287 }
1288
1289 Register PointeeTypeReg = ResTypeInst->getOperand(2).getReg();
1290 MachineInstr *PointeeType = MRI.getUniqueVRegDef(PointeeTypeReg);
1291 if (PointeeType->getOpcode() != SPIRV::OpTypeImage &&
1292 PointeeType->getOpcode() != SPIRV::OpTypeSampledImage &&
1293 PointeeType->getOpcode() != SPIRV::OpTypeSampler) {
1294 return;
1295 }
1296
1297 if (isUniformTexelBuffer(PointeeType)) {
1298 if (IsNonUniform)
1299 Handler.addRequirements(
1300 SPIRV::Capability::UniformTexelBufferArrayNonUniformIndexingEXT);
1301 else if (!FirstIndexIsConstant)
1302 Handler.addRequirements(
1303 SPIRV::Capability::UniformTexelBufferArrayDynamicIndexingEXT);
1304 } else if (isInputAttachment(PointeeType)) {
1305 if (IsNonUniform)
1306 Handler.addRequirements(
1307 SPIRV::Capability::InputAttachmentArrayNonUniformIndexingEXT);
1308 else if (!FirstIndexIsConstant)
1309 Handler.addRequirements(
1310 SPIRV::Capability::InputAttachmentArrayDynamicIndexingEXT);
1311 } else if (isStorageTexelBuffer(PointeeType)) {
1312 if (IsNonUniform)
1313 Handler.addRequirements(
1314 SPIRV::Capability::StorageTexelBufferArrayNonUniformIndexingEXT);
1315 else if (!FirstIndexIsConstant)
1316 Handler.addRequirements(
1317 SPIRV::Capability::StorageTexelBufferArrayDynamicIndexingEXT);
1318 } else if (isSampledImage(PointeeType) ||
1319 isCombinedImageSampler(PointeeType) ||
1320 PointeeType->getOpcode() == SPIRV::OpTypeSampler) {
1321 if (IsNonUniform)
1322 Handler.addRequirements(
1323 SPIRV::Capability::SampledImageArrayNonUniformIndexingEXT);
1324 else if (!FirstIndexIsConstant)
1325 Handler.addRequirements(
1326 SPIRV::Capability::SampledImageArrayDynamicIndexing);
1327 } else if (isStorageImage(PointeeType)) {
1328 if (IsNonUniform)
1329 Handler.addRequirements(
1330 SPIRV::Capability::StorageImageArrayNonUniformIndexingEXT);
1331 else if (!FirstIndexIsConstant)
1332 Handler.addRequirements(
1333 SPIRV::Capability::StorageImageArrayDynamicIndexing);
1334 }
1335}
1336
1337static bool isImageTypeWithUnknownFormat(SPIRVType *TypeInst) {
1338 if (TypeInst->getOpcode() != SPIRV::OpTypeImage)
1339 return false;
1340 assert(TypeInst->getOperand(7).isImm() && "The image format must be an imm.");
1341 return TypeInst->getOperand(7).getImm() == 0;
1342}
1343
1344static void AddDotProductRequirements(const MachineInstr &MI,
1346 const SPIRVSubtarget &ST) {
1347 if (ST.canUseExtension(SPIRV::Extension::SPV_KHR_integer_dot_product))
1348 Reqs.addExtension(SPIRV::Extension::SPV_KHR_integer_dot_product);
1349 Reqs.addCapability(SPIRV::Capability::DotProduct);
1350
1351 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1352 assert(MI.getOperand(2).isReg() && "Unexpected operand in dot");
1353 // We do not consider what the previous instruction is. This is just used
1354 // to get the input register and to check the type.
1355 const MachineInstr *Input = MRI.getVRegDef(MI.getOperand(2).getReg());
1356 assert(Input->getOperand(1).isReg() && "Unexpected operand in dot input");
1357 Register InputReg = Input->getOperand(1).getReg();
1358
1359 SPIRVType *TypeDef = MRI.getVRegDef(InputReg);
1360 if (TypeDef->getOpcode() == SPIRV::OpTypeInt) {
1361 assert(TypeDef->getOperand(1).getImm() == 32);
1362 Reqs.addCapability(SPIRV::Capability::DotProductInput4x8BitPacked);
1363 } else if (TypeDef->getOpcode() == SPIRV::OpTypeVector) {
1364 SPIRVType *ScalarTypeDef = MRI.getVRegDef(TypeDef->getOperand(1).getReg());
1365 assert(ScalarTypeDef->getOpcode() == SPIRV::OpTypeInt);
1366 if (ScalarTypeDef->getOperand(1).getImm() == 8) {
1367 assert(TypeDef->getOperand(2).getImm() == 4 &&
1368 "Dot operand of 8-bit integer type requires 4 components");
1369 Reqs.addCapability(SPIRV::Capability::DotProductInput4x8Bit);
1370 } else {
1371 Reqs.addCapability(SPIRV::Capability::DotProductInputAll);
1372 }
1373 }
1374}
1375
1376void addPrintfRequirements(const MachineInstr &MI,
1378 const SPIRVSubtarget &ST) {
1379 SPIRVGlobalRegistry *GR = ST.getSPIRVGlobalRegistry();
1380 const SPIRVType *PtrType = GR->getSPIRVTypeForVReg(MI.getOperand(4).getReg());
1381 if (PtrType) {
1382 MachineOperand ASOp = PtrType->getOperand(1);
1383 if (ASOp.isImm()) {
1384 unsigned AddrSpace = ASOp.getImm();
1385 if (AddrSpace != SPIRV::StorageClass::UniformConstant) {
1386 if (!ST.canUseExtension(
1388 SPV_EXT_relaxed_printf_string_address_space)) {
1389 report_fatal_error("SPV_EXT_relaxed_printf_string_address_space is "
1390 "required because printf uses a format string not "
1391 "in constant address space.",
1392 false);
1393 }
1394 Reqs.addExtension(
1395 SPIRV::Extension::SPV_EXT_relaxed_printf_string_address_space);
1396 }
1397 }
1398 }
1399}
1400
1401void addInstrRequirements(const MachineInstr &MI,
1403 const SPIRVSubtarget &ST) {
1404 SPIRV::RequirementHandler &Reqs = MAI.Reqs;
1405 switch (MI.getOpcode()) {
1406 case SPIRV::OpMemoryModel: {
1407 int64_t Addr = MI.getOperand(0).getImm();
1408 Reqs.getAndAddRequirements(SPIRV::OperandCategory::AddressingModelOperand,
1409 Addr, ST);
1410 int64_t Mem = MI.getOperand(1).getImm();
1411 Reqs.getAndAddRequirements(SPIRV::OperandCategory::MemoryModelOperand, Mem,
1412 ST);
1413 break;
1414 }
1415 case SPIRV::OpEntryPoint: {
1416 int64_t Exe = MI.getOperand(0).getImm();
1417 Reqs.getAndAddRequirements(SPIRV::OperandCategory::ExecutionModelOperand,
1418 Exe, ST);
1419 break;
1420 }
1421 case SPIRV::OpExecutionMode:
1422 case SPIRV::OpExecutionModeId: {
1423 int64_t Exe = MI.getOperand(1).getImm();
1424 Reqs.getAndAddRequirements(SPIRV::OperandCategory::ExecutionModeOperand,
1425 Exe, ST);
1426 break;
1427 }
1428 case SPIRV::OpTypeMatrix:
1429 Reqs.addCapability(SPIRV::Capability::Matrix);
1430 break;
1431 case SPIRV::OpTypeInt: {
1432 unsigned BitWidth = MI.getOperand(1).getImm();
1433 if (BitWidth == 64)
1434 Reqs.addCapability(SPIRV::Capability::Int64);
1435 else if (BitWidth == 16)
1436 Reqs.addCapability(SPIRV::Capability::Int16);
1437 else if (BitWidth == 8)
1438 Reqs.addCapability(SPIRV::Capability::Int8);
1439 else if (BitWidth == 4 &&
1440 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_int4)) {
1441 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_int4);
1442 Reqs.addCapability(SPIRV::Capability::Int4TypeINTEL);
1443 } else if (BitWidth != 32) {
1444 if (!ST.canUseExtension(
1445 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers))
1447 "OpTypeInt type with a width other than 8, 16, 32 or 64 bits "
1448 "requires the following SPIR-V extension: "
1449 "SPV_ALTERA_arbitrary_precision_integers");
1450 Reqs.addExtension(
1451 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_integers);
1452 Reqs.addCapability(SPIRV::Capability::ArbitraryPrecisionIntegersALTERA);
1453 }
1454 break;
1455 }
1456 case SPIRV::OpDot: {
1457 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1458 SPIRVType *TypeDef = MRI.getVRegDef(MI.getOperand(1).getReg());
1459 if (isBFloat16Type(TypeDef))
1460 Reqs.addCapability(SPIRV::Capability::BFloat16DotProductKHR);
1461 break;
1462 }
1463 case SPIRV::OpTypeFloat: {
1464 unsigned BitWidth = MI.getOperand(1).getImm();
1465 if (BitWidth == 64)
1466 Reqs.addCapability(SPIRV::Capability::Float64);
1467 else if (BitWidth == 16) {
1468 if (isBFloat16Type(&MI)) {
1469 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_bfloat16))
1470 report_fatal_error("OpTypeFloat type with bfloat requires the "
1471 "following SPIR-V extension: SPV_KHR_bfloat16",
1472 false);
1473 Reqs.addExtension(SPIRV::Extension::SPV_KHR_bfloat16);
1474 Reqs.addCapability(SPIRV::Capability::BFloat16TypeKHR);
1475 } else {
1476 Reqs.addCapability(SPIRV::Capability::Float16);
1477 }
1478 }
1479 break;
1480 }
1481 case SPIRV::OpTypeVector: {
1482 unsigned NumComponents = MI.getOperand(2).getImm();
1483 if (NumComponents == 8 || NumComponents == 16)
1484 Reqs.addCapability(SPIRV::Capability::Vector16);
1485 break;
1486 }
1487 case SPIRV::OpTypePointer: {
1488 auto SC = MI.getOperand(1).getImm();
1489 Reqs.getAndAddRequirements(SPIRV::OperandCategory::StorageClassOperand, SC,
1490 ST);
1491 // If it's a type of pointer to float16 targeting OpenCL, add Float16Buffer
1492 // capability.
1493 if (ST.isShader())
1494 break;
1495 assert(MI.getOperand(2).isReg());
1496 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1497 SPIRVType *TypeDef = MRI.getVRegDef(MI.getOperand(2).getReg());
1498 if ((TypeDef->getNumOperands() == 2) &&
1499 (TypeDef->getOpcode() == SPIRV::OpTypeFloat) &&
1500 (TypeDef->getOperand(1).getImm() == 16))
1501 Reqs.addCapability(SPIRV::Capability::Float16Buffer);
1502 break;
1503 }
1504 case SPIRV::OpExtInst: {
1505 if (MI.getOperand(2).getImm() ==
1506 static_cast<int64_t>(
1507 SPIRV::InstructionSet::NonSemantic_Shader_DebugInfo_100)) {
1508 Reqs.addExtension(SPIRV::Extension::SPV_KHR_non_semantic_info);
1509 break;
1510 }
1511 if (MI.getOperand(3).getImm() ==
1512 static_cast<int64_t>(SPIRV::OpenCLExtInst::printf)) {
1513 addPrintfRequirements(MI, Reqs, ST);
1514 break;
1515 }
1516 // TODO: handle bfloat16 extended instructions when
1517 // SPV_INTEL_bfloat16_arithmetic is enabled.
1518 break;
1519 }
1520 case SPIRV::OpAliasDomainDeclINTEL:
1521 case SPIRV::OpAliasScopeDeclINTEL:
1522 case SPIRV::OpAliasScopeListDeclINTEL: {
1523 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_memory_access_aliasing);
1524 Reqs.addCapability(SPIRV::Capability::MemoryAccessAliasingINTEL);
1525 break;
1526 }
1527 case SPIRV::OpBitReverse:
1528 case SPIRV::OpBitFieldInsert:
1529 case SPIRV::OpBitFieldSExtract:
1530 case SPIRV::OpBitFieldUExtract:
1531 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions)) {
1532 Reqs.addCapability(SPIRV::Capability::Shader);
1533 break;
1534 }
1535 Reqs.addExtension(SPIRV::Extension::SPV_KHR_bit_instructions);
1536 Reqs.addCapability(SPIRV::Capability::BitInstructions);
1537 break;
1538 case SPIRV::OpTypeRuntimeArray:
1539 Reqs.addCapability(SPIRV::Capability::Shader);
1540 break;
1541 case SPIRV::OpTypeOpaque:
1542 case SPIRV::OpTypeEvent:
1543 Reqs.addCapability(SPIRV::Capability::Kernel);
1544 break;
1545 case SPIRV::OpTypePipe:
1546 case SPIRV::OpTypeReserveId:
1547 Reqs.addCapability(SPIRV::Capability::Pipes);
1548 break;
1549 case SPIRV::OpTypeDeviceEvent:
1550 case SPIRV::OpTypeQueue:
1551 case SPIRV::OpBuildNDRange:
1552 Reqs.addCapability(SPIRV::Capability::DeviceEnqueue);
1553 break;
1554 case SPIRV::OpDecorate:
1555 case SPIRV::OpDecorateId:
1556 case SPIRV::OpDecorateString:
1557 addOpDecorateReqs(MI, 1, Reqs, ST);
1558 break;
1559 case SPIRV::OpMemberDecorate:
1560 case SPIRV::OpMemberDecorateString:
1561 addOpDecorateReqs(MI, 2, Reqs, ST);
1562 break;
1563 case SPIRV::OpInBoundsPtrAccessChain:
1564 Reqs.addCapability(SPIRV::Capability::Addresses);
1565 break;
1566 case SPIRV::OpConstantSampler:
1567 Reqs.addCapability(SPIRV::Capability::LiteralSampler);
1568 break;
1569 case SPIRV::OpInBoundsAccessChain:
1570 case SPIRV::OpAccessChain:
1571 addOpAccessChainReqs(MI, Reqs, ST);
1572 break;
1573 case SPIRV::OpTypeImage:
1574 addOpTypeImageReqs(MI, Reqs, ST);
1575 break;
1576 case SPIRV::OpTypeSampler:
1577 if (!ST.isShader()) {
1578 Reqs.addCapability(SPIRV::Capability::ImageBasic);
1579 }
1580 break;
1581 case SPIRV::OpTypeForwardPointer:
1582 // TODO: check if it's OpenCL's kernel.
1583 Reqs.addCapability(SPIRV::Capability::Addresses);
1584 break;
1585 case SPIRV::OpAtomicFlagTestAndSet:
1586 case SPIRV::OpAtomicLoad:
1587 case SPIRV::OpAtomicStore:
1588 case SPIRV::OpAtomicExchange:
1589 case SPIRV::OpAtomicCompareExchange:
1590 case SPIRV::OpAtomicIIncrement:
1591 case SPIRV::OpAtomicIDecrement:
1592 case SPIRV::OpAtomicIAdd:
1593 case SPIRV::OpAtomicISub:
1594 case SPIRV::OpAtomicUMin:
1595 case SPIRV::OpAtomicUMax:
1596 case SPIRV::OpAtomicSMin:
1597 case SPIRV::OpAtomicSMax:
1598 case SPIRV::OpAtomicAnd:
1599 case SPIRV::OpAtomicOr:
1600 case SPIRV::OpAtomicXor: {
1601 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1602 const MachineInstr *InstrPtr = &MI;
1603 if (MI.getOpcode() == SPIRV::OpAtomicStore) {
1604 assert(MI.getOperand(3).isReg());
1605 InstrPtr = MRI.getVRegDef(MI.getOperand(3).getReg());
1606 assert(InstrPtr && "Unexpected type instruction for OpAtomicStore");
1607 }
1608 assert(InstrPtr->getOperand(1).isReg() && "Unexpected operand in atomic");
1609 Register TypeReg = InstrPtr->getOperand(1).getReg();
1610 SPIRVType *TypeDef = MRI.getVRegDef(TypeReg);
1611 if (TypeDef->getOpcode() == SPIRV::OpTypeInt) {
1612 unsigned BitWidth = TypeDef->getOperand(1).getImm();
1613 if (BitWidth == 64)
1614 Reqs.addCapability(SPIRV::Capability::Int64Atomics);
1615 }
1616 break;
1617 }
1618 case SPIRV::OpGroupNonUniformIAdd:
1619 case SPIRV::OpGroupNonUniformFAdd:
1620 case SPIRV::OpGroupNonUniformIMul:
1621 case SPIRV::OpGroupNonUniformFMul:
1622 case SPIRV::OpGroupNonUniformSMin:
1623 case SPIRV::OpGroupNonUniformUMin:
1624 case SPIRV::OpGroupNonUniformFMin:
1625 case SPIRV::OpGroupNonUniformSMax:
1626 case SPIRV::OpGroupNonUniformUMax:
1627 case SPIRV::OpGroupNonUniformFMax:
1628 case SPIRV::OpGroupNonUniformBitwiseAnd:
1629 case SPIRV::OpGroupNonUniformBitwiseOr:
1630 case SPIRV::OpGroupNonUniformBitwiseXor:
1631 case SPIRV::OpGroupNonUniformLogicalAnd:
1632 case SPIRV::OpGroupNonUniformLogicalOr:
1633 case SPIRV::OpGroupNonUniformLogicalXor: {
1634 assert(MI.getOperand(3).isImm());
1635 int64_t GroupOp = MI.getOperand(3).getImm();
1636 switch (GroupOp) {
1637 case SPIRV::GroupOperation::Reduce:
1638 case SPIRV::GroupOperation::InclusiveScan:
1639 case SPIRV::GroupOperation::ExclusiveScan:
1640 Reqs.addCapability(SPIRV::Capability::GroupNonUniformArithmetic);
1641 break;
1642 case SPIRV::GroupOperation::ClusteredReduce:
1643 Reqs.addCapability(SPIRV::Capability::GroupNonUniformClustered);
1644 break;
1645 case SPIRV::GroupOperation::PartitionedReduceNV:
1646 case SPIRV::GroupOperation::PartitionedInclusiveScanNV:
1647 case SPIRV::GroupOperation::PartitionedExclusiveScanNV:
1648 Reqs.addCapability(SPIRV::Capability::GroupNonUniformPartitionedNV);
1649 break;
1650 }
1651 break;
1652 }
1653 case SPIRV::OpGroupNonUniformShuffle:
1654 case SPIRV::OpGroupNonUniformShuffleXor:
1655 Reqs.addCapability(SPIRV::Capability::GroupNonUniformShuffle);
1656 break;
1657 case SPIRV::OpGroupNonUniformShuffleUp:
1658 case SPIRV::OpGroupNonUniformShuffleDown:
1659 Reqs.addCapability(SPIRV::Capability::GroupNonUniformShuffleRelative);
1660 break;
1661 case SPIRV::OpGroupAll:
1662 case SPIRV::OpGroupAny:
1663 case SPIRV::OpGroupBroadcast:
1664 case SPIRV::OpGroupIAdd:
1665 case SPIRV::OpGroupFAdd:
1666 case SPIRV::OpGroupFMin:
1667 case SPIRV::OpGroupUMin:
1668 case SPIRV::OpGroupSMin:
1669 case SPIRV::OpGroupFMax:
1670 case SPIRV::OpGroupUMax:
1671 case SPIRV::OpGroupSMax:
1672 Reqs.addCapability(SPIRV::Capability::Groups);
1673 break;
1674 case SPIRV::OpGroupNonUniformElect:
1675 Reqs.addCapability(SPIRV::Capability::GroupNonUniform);
1676 break;
1677 case SPIRV::OpGroupNonUniformAll:
1678 case SPIRV::OpGroupNonUniformAny:
1679 case SPIRV::OpGroupNonUniformAllEqual:
1680 Reqs.addCapability(SPIRV::Capability::GroupNonUniformVote);
1681 break;
1682 case SPIRV::OpGroupNonUniformBroadcast:
1683 case SPIRV::OpGroupNonUniformBroadcastFirst:
1684 case SPIRV::OpGroupNonUniformBallot:
1685 case SPIRV::OpGroupNonUniformInverseBallot:
1686 case SPIRV::OpGroupNonUniformBallotBitExtract:
1687 case SPIRV::OpGroupNonUniformBallotBitCount:
1688 case SPIRV::OpGroupNonUniformBallotFindLSB:
1689 case SPIRV::OpGroupNonUniformBallotFindMSB:
1690 Reqs.addCapability(SPIRV::Capability::GroupNonUniformBallot);
1691 break;
1692 case SPIRV::OpSubgroupShuffleINTEL:
1693 case SPIRV::OpSubgroupShuffleDownINTEL:
1694 case SPIRV::OpSubgroupShuffleUpINTEL:
1695 case SPIRV::OpSubgroupShuffleXorINTEL:
1696 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_subgroups)) {
1697 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_subgroups);
1698 Reqs.addCapability(SPIRV::Capability::SubgroupShuffleINTEL);
1699 }
1700 break;
1701 case SPIRV::OpSubgroupBlockReadINTEL:
1702 case SPIRV::OpSubgroupBlockWriteINTEL:
1703 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_subgroups)) {
1704 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_subgroups);
1705 Reqs.addCapability(SPIRV::Capability::SubgroupBufferBlockIOINTEL);
1706 }
1707 break;
1708 case SPIRV::OpSubgroupImageBlockReadINTEL:
1709 case SPIRV::OpSubgroupImageBlockWriteINTEL:
1710 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_subgroups)) {
1711 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_subgroups);
1712 Reqs.addCapability(SPIRV::Capability::SubgroupImageBlockIOINTEL);
1713 }
1714 break;
1715 case SPIRV::OpSubgroupImageMediaBlockReadINTEL:
1716 case SPIRV::OpSubgroupImageMediaBlockWriteINTEL:
1717 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_media_block_io)) {
1718 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_media_block_io);
1719 Reqs.addCapability(SPIRV::Capability::SubgroupImageMediaBlockIOINTEL);
1720 }
1721 break;
1722 case SPIRV::OpAssumeTrueKHR:
1723 case SPIRV::OpExpectKHR:
1724 if (ST.canUseExtension(SPIRV::Extension::SPV_KHR_expect_assume)) {
1725 Reqs.addExtension(SPIRV::Extension::SPV_KHR_expect_assume);
1726 Reqs.addCapability(SPIRV::Capability::ExpectAssumeKHR);
1727 }
1728 break;
1729 case SPIRV::OpPtrCastToCrossWorkgroupINTEL:
1730 case SPIRV::OpCrossWorkgroupCastToPtrINTEL:
1731 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes)) {
1732 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_usm_storage_classes);
1733 Reqs.addCapability(SPIRV::Capability::USMStorageClassesINTEL);
1734 }
1735 break;
1736 case SPIRV::OpConstantFunctionPointerINTEL:
1737 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers)) {
1738 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
1739 Reqs.addCapability(SPIRV::Capability::FunctionPointersINTEL);
1740 }
1741 break;
1742 case SPIRV::OpGroupNonUniformRotateKHR:
1743 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_subgroup_rotate))
1744 report_fatal_error("OpGroupNonUniformRotateKHR instruction requires the "
1745 "following SPIR-V extension: SPV_KHR_subgroup_rotate",
1746 false);
1747 Reqs.addExtension(SPIRV::Extension::SPV_KHR_subgroup_rotate);
1748 Reqs.addCapability(SPIRV::Capability::GroupNonUniformRotateKHR);
1749 Reqs.addCapability(SPIRV::Capability::GroupNonUniform);
1750 break;
1751 case SPIRV::OpFixedCosALTERA:
1752 case SPIRV::OpFixedSinALTERA:
1753 case SPIRV::OpFixedCosPiALTERA:
1754 case SPIRV::OpFixedSinPiALTERA:
1755 case SPIRV::OpFixedExpALTERA:
1756 case SPIRV::OpFixedLogALTERA:
1757 case SPIRV::OpFixedRecipALTERA:
1758 case SPIRV::OpFixedSqrtALTERA:
1759 case SPIRV::OpFixedSinCosALTERA:
1760 case SPIRV::OpFixedSinCosPiALTERA:
1761 case SPIRV::OpFixedRsqrtALTERA:
1762 if (!ST.canUseExtension(
1763 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_fixed_point))
1764 report_fatal_error("This instruction requires the "
1765 "following SPIR-V extension: "
1766 "SPV_ALTERA_arbitrary_precision_fixed_point",
1767 false);
1768 Reqs.addExtension(
1769 SPIRV::Extension::SPV_ALTERA_arbitrary_precision_fixed_point);
1770 Reqs.addCapability(SPIRV::Capability::ArbitraryPrecisionFixedPointALTERA);
1771 break;
1772 case SPIRV::OpGroupIMulKHR:
1773 case SPIRV::OpGroupFMulKHR:
1774 case SPIRV::OpGroupBitwiseAndKHR:
1775 case SPIRV::OpGroupBitwiseOrKHR:
1776 case SPIRV::OpGroupBitwiseXorKHR:
1777 case SPIRV::OpGroupLogicalAndKHR:
1778 case SPIRV::OpGroupLogicalOrKHR:
1779 case SPIRV::OpGroupLogicalXorKHR:
1780 if (ST.canUseExtension(
1781 SPIRV::Extension::SPV_KHR_uniform_group_instructions)) {
1782 Reqs.addExtension(SPIRV::Extension::SPV_KHR_uniform_group_instructions);
1783 Reqs.addCapability(SPIRV::Capability::GroupUniformArithmeticKHR);
1784 }
1785 break;
1786 case SPIRV::OpReadClockKHR:
1787 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_shader_clock))
1788 report_fatal_error("OpReadClockKHR instruction requires the "
1789 "following SPIR-V extension: SPV_KHR_shader_clock",
1790 false);
1791 Reqs.addExtension(SPIRV::Extension::SPV_KHR_shader_clock);
1792 Reqs.addCapability(SPIRV::Capability::ShaderClockKHR);
1793 break;
1794 case SPIRV::OpFunctionPointerCallINTEL:
1795 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers)) {
1796 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
1797 Reqs.addCapability(SPIRV::Capability::FunctionPointersINTEL);
1798 }
1799 break;
1800 case SPIRV::OpAtomicFAddEXT:
1801 case SPIRV::OpAtomicFMinEXT:
1802 case SPIRV::OpAtomicFMaxEXT:
1803 AddAtomicFloatRequirements(MI, Reqs, ST);
1804 break;
1805 case SPIRV::OpConvertBF16ToFINTEL:
1806 case SPIRV::OpConvertFToBF16INTEL:
1807 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_bfloat16_conversion)) {
1808 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_bfloat16_conversion);
1809 Reqs.addCapability(SPIRV::Capability::BFloat16ConversionINTEL);
1810 }
1811 break;
1812 case SPIRV::OpRoundFToTF32INTEL:
1813 if (ST.canUseExtension(
1814 SPIRV::Extension::SPV_INTEL_tensor_float32_conversion)) {
1815 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_tensor_float32_conversion);
1816 Reqs.addCapability(SPIRV::Capability::TensorFloat32RoundingINTEL);
1817 }
1818 break;
1819 case SPIRV::OpVariableLengthArrayINTEL:
1820 case SPIRV::OpSaveMemoryINTEL:
1821 case SPIRV::OpRestoreMemoryINTEL:
1822 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_variable_length_array)) {
1823 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_variable_length_array);
1824 Reqs.addCapability(SPIRV::Capability::VariableLengthArrayINTEL);
1825 }
1826 break;
1827 case SPIRV::OpAsmTargetINTEL:
1828 case SPIRV::OpAsmINTEL:
1829 case SPIRV::OpAsmCallINTEL:
1830 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_inline_assembly)) {
1831 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_inline_assembly);
1832 Reqs.addCapability(SPIRV::Capability::AsmINTEL);
1833 }
1834 break;
1835 case SPIRV::OpTypeCooperativeMatrixKHR: {
1836 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix))
1838 "OpTypeCooperativeMatrixKHR type requires the "
1839 "following SPIR-V extension: SPV_KHR_cooperative_matrix",
1840 false);
1841 Reqs.addExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix);
1842 Reqs.addCapability(SPIRV::Capability::CooperativeMatrixKHR);
1843 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1844 SPIRVType *TypeDef = MRI.getVRegDef(MI.getOperand(1).getReg());
1845 if (isBFloat16Type(TypeDef))
1846 Reqs.addCapability(SPIRV::Capability::BFloat16CooperativeMatrixKHR);
1847 break;
1848 }
1849 case SPIRV::OpArithmeticFenceEXT:
1850 if (!ST.canUseExtension(SPIRV::Extension::SPV_EXT_arithmetic_fence))
1851 report_fatal_error("OpArithmeticFenceEXT requires the "
1852 "following SPIR-V extension: SPV_EXT_arithmetic_fence",
1853 false);
1854 Reqs.addExtension(SPIRV::Extension::SPV_EXT_arithmetic_fence);
1855 Reqs.addCapability(SPIRV::Capability::ArithmeticFenceEXT);
1856 break;
1857 case SPIRV::OpControlBarrierArriveINTEL:
1858 case SPIRV::OpControlBarrierWaitINTEL:
1859 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_split_barrier)) {
1860 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_split_barrier);
1861 Reqs.addCapability(SPIRV::Capability::SplitBarrierINTEL);
1862 }
1863 break;
1864 case SPIRV::OpCooperativeMatrixMulAddKHR: {
1865 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix))
1866 report_fatal_error("Cooperative matrix instructions require the "
1867 "following SPIR-V extension: "
1868 "SPV_KHR_cooperative_matrix",
1869 false);
1870 Reqs.addExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix);
1871 Reqs.addCapability(SPIRV::Capability::CooperativeMatrixKHR);
1872 constexpr unsigned MulAddMaxSize = 6;
1873 if (MI.getNumOperands() != MulAddMaxSize)
1874 break;
1875 const int64_t CoopOperands = MI.getOperand(MulAddMaxSize - 1).getImm();
1876 if (CoopOperands &
1877 SPIRV::CooperativeMatrixOperands::MatrixAAndBTF32ComponentsINTEL) {
1878 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix))
1879 report_fatal_error("MatrixAAndBTF32ComponentsINTEL type interpretation "
1880 "require the following SPIR-V extension: "
1881 "SPV_INTEL_joint_matrix",
1882 false);
1883 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
1884 Reqs.addCapability(
1885 SPIRV::Capability::CooperativeMatrixTF32ComponentTypeINTEL);
1886 }
1887 if (CoopOperands & SPIRV::CooperativeMatrixOperands::
1888 MatrixAAndBBFloat16ComponentsINTEL ||
1889 CoopOperands &
1890 SPIRV::CooperativeMatrixOperands::MatrixCBFloat16ComponentsINTEL ||
1891 CoopOperands & SPIRV::CooperativeMatrixOperands::
1892 MatrixResultBFloat16ComponentsINTEL) {
1893 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix))
1894 report_fatal_error("***BF16ComponentsINTEL type interpretations "
1895 "require the following SPIR-V extension: "
1896 "SPV_INTEL_joint_matrix",
1897 false);
1898 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
1899 Reqs.addCapability(
1900 SPIRV::Capability::CooperativeMatrixBFloat16ComponentTypeINTEL);
1901 }
1902 break;
1903 }
1904 case SPIRV::OpCooperativeMatrixLoadKHR:
1905 case SPIRV::OpCooperativeMatrixStoreKHR:
1906 case SPIRV::OpCooperativeMatrixLoadCheckedINTEL:
1907 case SPIRV::OpCooperativeMatrixStoreCheckedINTEL:
1908 case SPIRV::OpCooperativeMatrixPrefetchINTEL: {
1909 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix))
1910 report_fatal_error("Cooperative matrix instructions require the "
1911 "following SPIR-V extension: "
1912 "SPV_KHR_cooperative_matrix",
1913 false);
1914 Reqs.addExtension(SPIRV::Extension::SPV_KHR_cooperative_matrix);
1915 Reqs.addCapability(SPIRV::Capability::CooperativeMatrixKHR);
1916
1917 // Check Layout operand in case if it's not a standard one and add the
1918 // appropriate capability.
1919 std::unordered_map<unsigned, unsigned> LayoutToInstMap = {
1920 {SPIRV::OpCooperativeMatrixLoadKHR, 3},
1921 {SPIRV::OpCooperativeMatrixStoreKHR, 2},
1922 {SPIRV::OpCooperativeMatrixLoadCheckedINTEL, 5},
1923 {SPIRV::OpCooperativeMatrixStoreCheckedINTEL, 4},
1924 {SPIRV::OpCooperativeMatrixPrefetchINTEL, 4}};
1925
1926 const auto OpCode = MI.getOpcode();
1927 const unsigned LayoutNum = LayoutToInstMap[OpCode];
1928 Register RegLayout = MI.getOperand(LayoutNum).getReg();
1929 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
1930 MachineInstr *MILayout = MRI.getUniqueVRegDef(RegLayout);
1931 if (MILayout->getOpcode() == SPIRV::OpConstantI) {
1932 const unsigned LayoutVal = MILayout->getOperand(2).getImm();
1933 if (LayoutVal ==
1934 static_cast<unsigned>(SPIRV::CooperativeMatrixLayout::PackedINTEL)) {
1935 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix))
1936 report_fatal_error("PackedINTEL layout require the following SPIR-V "
1937 "extension: SPV_INTEL_joint_matrix",
1938 false);
1939 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
1940 Reqs.addCapability(SPIRV::Capability::PackedCooperativeMatrixINTEL);
1941 }
1942 }
1943
1944 // Nothing to do.
1945 if (OpCode == SPIRV::OpCooperativeMatrixLoadKHR ||
1946 OpCode == SPIRV::OpCooperativeMatrixStoreKHR)
1947 break;
1948
1949 std::string InstName;
1950 switch (OpCode) {
1951 case SPIRV::OpCooperativeMatrixPrefetchINTEL:
1952 InstName = "OpCooperativeMatrixPrefetchINTEL";
1953 break;
1954 case SPIRV::OpCooperativeMatrixLoadCheckedINTEL:
1955 InstName = "OpCooperativeMatrixLoadCheckedINTEL";
1956 break;
1957 case SPIRV::OpCooperativeMatrixStoreCheckedINTEL:
1958 InstName = "OpCooperativeMatrixStoreCheckedINTEL";
1959 break;
1960 }
1961
1962 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix)) {
1963 const std::string ErrorMsg =
1964 InstName + " instruction requires the "
1965 "following SPIR-V extension: SPV_INTEL_joint_matrix";
1966 report_fatal_error(ErrorMsg.c_str(), false);
1967 }
1968 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
1969 if (OpCode == SPIRV::OpCooperativeMatrixPrefetchINTEL) {
1970 Reqs.addCapability(SPIRV::Capability::CooperativeMatrixPrefetchINTEL);
1971 break;
1972 }
1973 Reqs.addCapability(
1974 SPIRV::Capability::CooperativeMatrixCheckedInstructionsINTEL);
1975 break;
1976 }
1977 case SPIRV::OpCooperativeMatrixConstructCheckedINTEL:
1978 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix))
1979 report_fatal_error("OpCooperativeMatrixConstructCheckedINTEL "
1980 "instructions require the following SPIR-V extension: "
1981 "SPV_INTEL_joint_matrix",
1982 false);
1983 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
1984 Reqs.addCapability(
1985 SPIRV::Capability::CooperativeMatrixCheckedInstructionsINTEL);
1986 break;
1987 case SPIRV::OpReadPipeBlockingALTERA:
1988 case SPIRV::OpWritePipeBlockingALTERA:
1989 if (ST.canUseExtension(SPIRV::Extension::SPV_ALTERA_blocking_pipes)) {
1990 Reqs.addExtension(SPIRV::Extension::SPV_ALTERA_blocking_pipes);
1991 Reqs.addCapability(SPIRV::Capability::BlockingPipesALTERA);
1992 }
1993 break;
1994 case SPIRV::OpCooperativeMatrixGetElementCoordINTEL:
1995 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_joint_matrix))
1996 report_fatal_error("OpCooperativeMatrixGetElementCoordINTEL requires the "
1997 "following SPIR-V extension: SPV_INTEL_joint_matrix",
1998 false);
1999 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_joint_matrix);
2000 Reqs.addCapability(
2001 SPIRV::Capability::CooperativeMatrixInvocationInstructionsINTEL);
2002 break;
2003 case SPIRV::OpConvertHandleToImageINTEL:
2004 case SPIRV::OpConvertHandleToSamplerINTEL:
2005 case SPIRV::OpConvertHandleToSampledImageINTEL: {
2006 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_bindless_images))
2007 report_fatal_error("OpConvertHandleTo[Image/Sampler/SampledImage]INTEL "
2008 "instructions require the following SPIR-V extension: "
2009 "SPV_INTEL_bindless_images",
2010 false);
2011 SPIRVGlobalRegistry *GR = ST.getSPIRVGlobalRegistry();
2012 SPIRV::AddressingModel::AddressingModel AddrModel = MAI.Addr;
2013 SPIRVType *TyDef = GR->getSPIRVTypeForVReg(MI.getOperand(1).getReg());
2014 if (MI.getOpcode() == SPIRV::OpConvertHandleToImageINTEL &&
2015 TyDef->getOpcode() != SPIRV::OpTypeImage) {
2016 report_fatal_error("Incorrect return type for the instruction "
2017 "OpConvertHandleToImageINTEL",
2018 false);
2019 } else if (MI.getOpcode() == SPIRV::OpConvertHandleToSamplerINTEL &&
2020 TyDef->getOpcode() != SPIRV::OpTypeSampler) {
2021 report_fatal_error("Incorrect return type for the instruction "
2022 "OpConvertHandleToSamplerINTEL",
2023 false);
2024 } else if (MI.getOpcode() == SPIRV::OpConvertHandleToSampledImageINTEL &&
2025 TyDef->getOpcode() != SPIRV::OpTypeSampledImage) {
2026 report_fatal_error("Incorrect return type for the instruction "
2027 "OpConvertHandleToSampledImageINTEL",
2028 false);
2029 }
2030 SPIRVType *SpvTy = GR->getSPIRVTypeForVReg(MI.getOperand(2).getReg());
2031 unsigned Bitwidth = GR->getScalarOrVectorBitWidth(SpvTy);
2032 if (!(Bitwidth == 32 && AddrModel == SPIRV::AddressingModel::Physical32) &&
2033 !(Bitwidth == 64 && AddrModel == SPIRV::AddressingModel::Physical64)) {
2035 "Parameter value must be a 32-bit scalar in case of "
2036 "Physical32 addressing model or a 64-bit scalar in case of "
2037 "Physical64 addressing model",
2038 false);
2039 }
2040 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_bindless_images);
2041 Reqs.addCapability(SPIRV::Capability::BindlessImagesINTEL);
2042 break;
2043 }
2044 case SPIRV::OpSubgroup2DBlockLoadINTEL:
2045 case SPIRV::OpSubgroup2DBlockLoadTransposeINTEL:
2046 case SPIRV::OpSubgroup2DBlockLoadTransformINTEL:
2047 case SPIRV::OpSubgroup2DBlockPrefetchINTEL:
2048 case SPIRV::OpSubgroup2DBlockStoreINTEL: {
2049 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_2d_block_io))
2050 report_fatal_error("OpSubgroup2DBlock[Load/LoadTranspose/LoadTransform/"
2051 "Prefetch/Store]INTEL instructions require the "
2052 "following SPIR-V extension: SPV_INTEL_2d_block_io",
2053 false);
2054 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_2d_block_io);
2055 Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockIOINTEL);
2056
2057 const auto OpCode = MI.getOpcode();
2058 if (OpCode == SPIRV::OpSubgroup2DBlockLoadTransposeINTEL) {
2059 Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockTransposeINTEL);
2060 break;
2061 }
2062 if (OpCode == SPIRV::OpSubgroup2DBlockLoadTransformINTEL) {
2063 Reqs.addCapability(SPIRV::Capability::Subgroup2DBlockTransformINTEL);
2064 break;
2065 }
2066 break;
2067 }
2068 case SPIRV::OpKill: {
2069 Reqs.addCapability(SPIRV::Capability::Shader);
2070 } break;
2071 case SPIRV::OpDemoteToHelperInvocation:
2072 Reqs.addCapability(SPIRV::Capability::DemoteToHelperInvocation);
2073
2074 if (ST.canUseExtension(
2075 SPIRV::Extension::SPV_EXT_demote_to_helper_invocation)) {
2076 if (!ST.isAtLeastSPIRVVer(llvm::VersionTuple(1, 6)))
2077 Reqs.addExtension(
2078 SPIRV::Extension::SPV_EXT_demote_to_helper_invocation);
2079 }
2080 break;
2081 case SPIRV::OpSDot:
2082 case SPIRV::OpUDot:
2083 case SPIRV::OpSUDot:
2084 case SPIRV::OpSDotAccSat:
2085 case SPIRV::OpUDotAccSat:
2086 case SPIRV::OpSUDotAccSat:
2087 AddDotProductRequirements(MI, Reqs, ST);
2088 break;
2089 case SPIRV::OpImageRead: {
2090 Register ImageReg = MI.getOperand(2).getReg();
2091 SPIRVType *TypeDef = ST.getSPIRVGlobalRegistry()->getResultType(
2092 ImageReg, const_cast<MachineFunction *>(MI.getMF()));
2093 // OpImageRead and OpImageWrite can use Unknown Image Formats
2094 // when the Kernel capability is declared. In the OpenCL environment we are
2095 // not allowed to produce
2096 // StorageImageReadWithoutFormat/StorageImageWriteWithoutFormat, see
2097 // https://github.com/KhronosGroup/SPIRV-Headers/issues/487
2098
2099 if (isImageTypeWithUnknownFormat(TypeDef) && ST.isShader())
2100 Reqs.addCapability(SPIRV::Capability::StorageImageReadWithoutFormat);
2101 break;
2102 }
2103 case SPIRV::OpImageWrite: {
2104 Register ImageReg = MI.getOperand(0).getReg();
2105 SPIRVType *TypeDef = ST.getSPIRVGlobalRegistry()->getResultType(
2106 ImageReg, const_cast<MachineFunction *>(MI.getMF()));
2107 // OpImageRead and OpImageWrite can use Unknown Image Formats
2108 // when the Kernel capability is declared. In the OpenCL environment we are
2109 // not allowed to produce
2110 // StorageImageReadWithoutFormat/StorageImageWriteWithoutFormat, see
2111 // https://github.com/KhronosGroup/SPIRV-Headers/issues/487
2112
2113 if (isImageTypeWithUnknownFormat(TypeDef) && ST.isShader())
2114 Reqs.addCapability(SPIRV::Capability::StorageImageWriteWithoutFormat);
2115 break;
2116 }
2117 case SPIRV::OpTypeStructContinuedINTEL:
2118 case SPIRV::OpConstantCompositeContinuedINTEL:
2119 case SPIRV::OpSpecConstantCompositeContinuedINTEL:
2120 case SPIRV::OpCompositeConstructContinuedINTEL: {
2121 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_long_composites))
2123 "Continued instructions require the "
2124 "following SPIR-V extension: SPV_INTEL_long_composites",
2125 false);
2126 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_long_composites);
2127 Reqs.addCapability(SPIRV::Capability::LongCompositesINTEL);
2128 break;
2129 }
2130 case SPIRV::OpSubgroupMatrixMultiplyAccumulateINTEL: {
2131 if (!ST.canUseExtension(
2132 SPIRV::Extension::SPV_INTEL_subgroup_matrix_multiply_accumulate))
2134 "OpSubgroupMatrixMultiplyAccumulateINTEL instruction requires the "
2135 "following SPIR-V "
2136 "extension: SPV_INTEL_subgroup_matrix_multiply_accumulate",
2137 false);
2138 Reqs.addExtension(
2139 SPIRV::Extension::SPV_INTEL_subgroup_matrix_multiply_accumulate);
2140 Reqs.addCapability(
2141 SPIRV::Capability::SubgroupMatrixMultiplyAccumulateINTEL);
2142 break;
2143 }
2144 case SPIRV::OpBitwiseFunctionINTEL: {
2145 if (!ST.canUseExtension(
2146 SPIRV::Extension::SPV_INTEL_ternary_bitwise_function))
2148 "OpBitwiseFunctionINTEL instruction requires the following SPIR-V "
2149 "extension: SPV_INTEL_ternary_bitwise_function",
2150 false);
2151 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_ternary_bitwise_function);
2152 Reqs.addCapability(SPIRV::Capability::TernaryBitwiseFunctionINTEL);
2153 break;
2154 }
2155 case SPIRV::OpCopyMemorySized: {
2156 Reqs.addCapability(SPIRV::Capability::Addresses);
2157 // TODO: Add UntypedPointersKHR when implemented.
2158 break;
2159 }
2160 case SPIRV::OpPredicatedLoadINTEL:
2161 case SPIRV::OpPredicatedStoreINTEL: {
2162 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_predicated_io))
2164 "OpPredicated[Load/Store]INTEL instructions require "
2165 "the following SPIR-V extension: SPV_INTEL_predicated_io",
2166 false);
2167 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_predicated_io);
2168 Reqs.addCapability(SPIRV::Capability::PredicatedIOINTEL);
2169 break;
2170 }
2171 case SPIRV::OpFAddS:
2172 case SPIRV::OpFSubS:
2173 case SPIRV::OpFMulS:
2174 case SPIRV::OpFDivS:
2175 case SPIRV::OpFRemS:
2176 case SPIRV::OpFMod:
2177 case SPIRV::OpFNegate:
2178 case SPIRV::OpFAddV:
2179 case SPIRV::OpFSubV:
2180 case SPIRV::OpFMulV:
2181 case SPIRV::OpFDivV:
2182 case SPIRV::OpFRemV:
2183 case SPIRV::OpFNegateV: {
2184 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
2185 SPIRVType *TypeDef = MRI.getVRegDef(MI.getOperand(1).getReg());
2186 if (TypeDef->getOpcode() == SPIRV::OpTypeVector)
2187 TypeDef = MRI.getVRegDef(TypeDef->getOperand(1).getReg());
2188 if (isBFloat16Type(TypeDef)) {
2189 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_bfloat16_arithmetic))
2191 "Arithmetic instructions with bfloat16 arguments require the "
2192 "following SPIR-V extension: SPV_INTEL_bfloat16_arithmetic",
2193 false);
2194 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_bfloat16_arithmetic);
2195 Reqs.addCapability(SPIRV::Capability::BFloat16ArithmeticINTEL);
2196 }
2197 break;
2198 }
2199 case SPIRV::OpOrdered:
2200 case SPIRV::OpUnordered:
2201 case SPIRV::OpFOrdEqual:
2202 case SPIRV::OpFOrdNotEqual:
2203 case SPIRV::OpFOrdLessThan:
2204 case SPIRV::OpFOrdLessThanEqual:
2205 case SPIRV::OpFOrdGreaterThan:
2206 case SPIRV::OpFOrdGreaterThanEqual:
2207 case SPIRV::OpFUnordEqual:
2208 case SPIRV::OpFUnordNotEqual:
2209 case SPIRV::OpFUnordLessThan:
2210 case SPIRV::OpFUnordLessThanEqual:
2211 case SPIRV::OpFUnordGreaterThan:
2212 case SPIRV::OpFUnordGreaterThanEqual: {
2213 const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
2214 MachineInstr *OperandDef = MRI.getVRegDef(MI.getOperand(2).getReg());
2215 SPIRVType *TypeDef = MRI.getVRegDef(OperandDef->getOperand(1).getReg());
2216 if (TypeDef->getOpcode() == SPIRV::OpTypeVector)
2217 TypeDef = MRI.getVRegDef(TypeDef->getOperand(1).getReg());
2218 if (isBFloat16Type(TypeDef)) {
2219 if (!ST.canUseExtension(SPIRV::Extension::SPV_INTEL_bfloat16_arithmetic))
2221 "Relational instructions with bfloat16 arguments require the "
2222 "following SPIR-V extension: SPV_INTEL_bfloat16_arithmetic",
2223 false);
2224 Reqs.addExtension(SPIRV::Extension::SPV_INTEL_bfloat16_arithmetic);
2225 Reqs.addCapability(SPIRV::Capability::BFloat16ArithmeticINTEL);
2226 }
2227 break;
2228 }
2229 case SPIRV::OpDPdxCoarse:
2230 case SPIRV::OpDPdyCoarse: {
2231 Reqs.addCapability(SPIRV::Capability::DerivativeControl);
2232 break;
2233 }
2234
2235 default:
2236 break;
2237 }
2238
2239 // If we require capability Shader, then we can remove the requirement for
2240 // the BitInstructions capability, since Shader is a superset capability
2241 // of BitInstructions.
2242 Reqs.removeCapabilityIf(SPIRV::Capability::BitInstructions,
2243 SPIRV::Capability::Shader);
2244}
2245
2246static void collectReqs(const Module &M, SPIRV::ModuleAnalysisInfo &MAI,
2247 MachineModuleInfo *MMI, const SPIRVSubtarget &ST) {
2248 // Collect requirements for existing instructions.
2249 for (const Function &F : M) {
2251 if (!MF)
2252 continue;
2253 for (const MachineBasicBlock &MBB : *MF)
2254 for (const MachineInstr &MI : MBB)
2255 addInstrRequirements(MI, MAI, ST);
2256 }
2257 // Collect requirements for OpExecutionMode instructions.
2258 auto Node = M.getNamedMetadata("spirv.ExecutionMode");
2259 if (Node) {
2260 bool RequireFloatControls = false, RequireIntelFloatControls2 = false,
2261 RequireKHRFloatControls2 = false,
2262 VerLower14 = !ST.isAtLeastSPIRVVer(VersionTuple(1, 4));
2263 bool HasIntelFloatControls2 =
2264 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_float_controls2);
2265 bool HasKHRFloatControls2 =
2266 ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2);
2267 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
2268 MDNode *MDN = cast<MDNode>(Node->getOperand(i));
2269 const MDOperand &MDOp = MDN->getOperand(1);
2270 if (auto *CMeta = dyn_cast<ConstantAsMetadata>(MDOp)) {
2271 Constant *C = CMeta->getValue();
2272 if (ConstantInt *Const = dyn_cast<ConstantInt>(C)) {
2273 auto EM = Const->getZExtValue();
2274 // SPV_KHR_float_controls is not available until v1.4:
2275 // add SPV_KHR_float_controls if the version is too low
2276 switch (EM) {
2277 case SPIRV::ExecutionMode::DenormPreserve:
2278 case SPIRV::ExecutionMode::DenormFlushToZero:
2279 case SPIRV::ExecutionMode::RoundingModeRTE:
2280 case SPIRV::ExecutionMode::RoundingModeRTZ:
2281 RequireFloatControls = VerLower14;
2283 SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
2284 break;
2285 case SPIRV::ExecutionMode::RoundingModeRTPINTEL:
2286 case SPIRV::ExecutionMode::RoundingModeRTNINTEL:
2287 case SPIRV::ExecutionMode::FloatingPointModeALTINTEL:
2288 case SPIRV::ExecutionMode::FloatingPointModeIEEEINTEL:
2289 if (HasIntelFloatControls2) {
2290 RequireIntelFloatControls2 = true;
2292 SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
2293 }
2294 break;
2295 case SPIRV::ExecutionMode::FPFastMathDefault: {
2296 if (HasKHRFloatControls2) {
2297 RequireKHRFloatControls2 = true;
2299 SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
2300 }
2301 break;
2302 }
2303 case SPIRV::ExecutionMode::ContractionOff:
2304 case SPIRV::ExecutionMode::SignedZeroInfNanPreserve:
2305 if (HasKHRFloatControls2) {
2306 RequireKHRFloatControls2 = true;
2308 SPIRV::OperandCategory::ExecutionModeOperand,
2309 SPIRV::ExecutionMode::FPFastMathDefault, ST);
2310 } else {
2312 SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
2313 }
2314 break;
2315 default:
2317 SPIRV::OperandCategory::ExecutionModeOperand, EM, ST);
2318 }
2319 }
2320 }
2321 }
2322 if (RequireFloatControls &&
2323 ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls))
2324 MAI.Reqs.addExtension(SPIRV::Extension::SPV_KHR_float_controls);
2325 if (RequireIntelFloatControls2)
2326 MAI.Reqs.addExtension(SPIRV::Extension::SPV_INTEL_float_controls2);
2327 if (RequireKHRFloatControls2)
2328 MAI.Reqs.addExtension(SPIRV::Extension::SPV_KHR_float_controls2);
2329 }
2330 for (const Function &F : M) {
2331 if (F.isDeclaration())
2332 continue;
2333 if (F.getMetadata("reqd_work_group_size"))
2335 SPIRV::OperandCategory::ExecutionModeOperand,
2336 SPIRV::ExecutionMode::LocalSize, ST);
2337 if (F.getFnAttribute("hlsl.numthreads").isValid()) {
2339 SPIRV::OperandCategory::ExecutionModeOperand,
2340 SPIRV::ExecutionMode::LocalSize, ST);
2341 }
2342 if (F.getFnAttribute("enable-maximal-reconvergence").getValueAsBool()) {
2343 MAI.Reqs.addExtension(SPIRV::Extension::SPV_KHR_maximal_reconvergence);
2344 }
2345 if (F.getMetadata("work_group_size_hint"))
2347 SPIRV::OperandCategory::ExecutionModeOperand,
2348 SPIRV::ExecutionMode::LocalSizeHint, ST);
2349 if (F.getMetadata("intel_reqd_sub_group_size"))
2351 SPIRV::OperandCategory::ExecutionModeOperand,
2352 SPIRV::ExecutionMode::SubgroupSize, ST);
2353 if (F.getMetadata("max_work_group_size"))
2355 SPIRV::OperandCategory::ExecutionModeOperand,
2356 SPIRV::ExecutionMode::MaxWorkgroupSizeINTEL, ST);
2357 if (F.getMetadata("vec_type_hint"))
2359 SPIRV::OperandCategory::ExecutionModeOperand,
2360 SPIRV::ExecutionMode::VecTypeHint, ST);
2361
2362 if (F.hasOptNone()) {
2363 if (ST.canUseExtension(SPIRV::Extension::SPV_INTEL_optnone)) {
2364 MAI.Reqs.addExtension(SPIRV::Extension::SPV_INTEL_optnone);
2365 MAI.Reqs.addCapability(SPIRV::Capability::OptNoneINTEL);
2366 } else if (ST.canUseExtension(SPIRV::Extension::SPV_EXT_optnone)) {
2367 MAI.Reqs.addExtension(SPIRV::Extension::SPV_EXT_optnone);
2368 MAI.Reqs.addCapability(SPIRV::Capability::OptNoneEXT);
2369 }
2370 }
2371 }
2372}
2373
2374static unsigned getFastMathFlags(const MachineInstr &I,
2375 const SPIRVSubtarget &ST) {
2376 unsigned Flags = SPIRV::FPFastMathMode::None;
2377 bool CanUseKHRFloatControls2 =
2378 ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2);
2379 if (I.getFlag(MachineInstr::MIFlag::FmNoNans))
2380 Flags |= SPIRV::FPFastMathMode::NotNaN;
2381 if (I.getFlag(MachineInstr::MIFlag::FmNoInfs))
2382 Flags |= SPIRV::FPFastMathMode::NotInf;
2383 if (I.getFlag(MachineInstr::MIFlag::FmNsz))
2384 Flags |= SPIRV::FPFastMathMode::NSZ;
2385 if (I.getFlag(MachineInstr::MIFlag::FmArcp))
2386 Flags |= SPIRV::FPFastMathMode::AllowRecip;
2387 if (I.getFlag(MachineInstr::MIFlag::FmContract) && CanUseKHRFloatControls2)
2388 Flags |= SPIRV::FPFastMathMode::AllowContract;
2389 if (I.getFlag(MachineInstr::MIFlag::FmReassoc)) {
2390 if (CanUseKHRFloatControls2)
2391 // LLVM reassoc maps to SPIRV transform, see
2392 // https://github.com/KhronosGroup/SPIRV-Registry/issues/326 for details.
2393 // Because we are enabling AllowTransform, we must enable AllowReassoc and
2394 // AllowContract too, as required by SPIRV spec. Also, we used to map
2395 // MIFlag::FmReassoc to FPFastMathMode::Fast, which now should instead by
2396 // replaced by turning all the other bits instead. Therefore, we're
2397 // enabling every bit here except None and Fast.
2398 Flags |= SPIRV::FPFastMathMode::NotNaN | SPIRV::FPFastMathMode::NotInf |
2399 SPIRV::FPFastMathMode::NSZ | SPIRV::FPFastMathMode::AllowRecip |
2400 SPIRV::FPFastMathMode::AllowTransform |
2401 SPIRV::FPFastMathMode::AllowReassoc |
2402 SPIRV::FPFastMathMode::AllowContract;
2403 else
2404 Flags |= SPIRV::FPFastMathMode::Fast;
2405 }
2406
2407 if (CanUseKHRFloatControls2) {
2408 // Error out if SPIRV::FPFastMathMode::Fast is enabled.
2409 assert(!(Flags & SPIRV::FPFastMathMode::Fast) &&
2410 "SPIRV::FPFastMathMode::Fast is deprecated and should not be used "
2411 "anymore.");
2412
2413 // Error out if AllowTransform is enabled without AllowReassoc and
2414 // AllowContract.
2415 assert((!(Flags & SPIRV::FPFastMathMode::AllowTransform) ||
2416 ((Flags & SPIRV::FPFastMathMode::AllowReassoc &&
2417 Flags & SPIRV::FPFastMathMode::AllowContract))) &&
2418 "SPIRV::FPFastMathMode::AllowTransform requires AllowReassoc and "
2419 "AllowContract flags to be enabled as well.");
2420 }
2421
2422 return Flags;
2423}
2424
2425static bool isFastMathModeAvailable(const SPIRVSubtarget &ST) {
2426 if (ST.isKernel())
2427 return true;
2428 if (ST.getSPIRVVersion() < VersionTuple(1, 2))
2429 return false;
2430 return ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2);
2431}
2432
2433static void handleMIFlagDecoration(
2434 MachineInstr &I, const SPIRVSubtarget &ST, const SPIRVInstrInfo &TII,
2436 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec) {
2437 if (I.getFlag(MachineInstr::MIFlag::NoSWrap) && TII.canUseNSW(I) &&
2438 getSymbolicOperandRequirements(SPIRV::OperandCategory::DecorationOperand,
2439 SPIRV::Decoration::NoSignedWrap, ST, Reqs)
2440 .IsSatisfiable) {
2441 buildOpDecorate(I.getOperand(0).getReg(), I, TII,
2442 SPIRV::Decoration::NoSignedWrap, {});
2443 }
2444 if (I.getFlag(MachineInstr::MIFlag::NoUWrap) && TII.canUseNUW(I) &&
2445 getSymbolicOperandRequirements(SPIRV::OperandCategory::DecorationOperand,
2446 SPIRV::Decoration::NoUnsignedWrap, ST,
2447 Reqs)
2448 .IsSatisfiable) {
2449 buildOpDecorate(I.getOperand(0).getReg(), I, TII,
2450 SPIRV::Decoration::NoUnsignedWrap, {});
2451 }
2452 if (!TII.canUseFastMathFlags(
2453 I, ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2)))
2454 return;
2455
2456 unsigned FMFlags = getFastMathFlags(I, ST);
2457 if (FMFlags == SPIRV::FPFastMathMode::None) {
2458 // We also need to check if any FPFastMathDefault info was set for the
2459 // types used in this instruction.
2460 if (FPFastMathDefaultInfoVec.empty())
2461 return;
2462
2463 // There are three types of instructions that can use fast math flags:
2464 // 1. Arithmetic instructions (FAdd, FMul, FSub, FDiv, FRem, etc.)
2465 // 2. Relational instructions (FCmp, FOrd, FUnord, etc.)
2466 // 3. Extended instructions (ExtInst)
2467 // For arithmetic instructions, the floating point type can be in the
2468 // result type or in the operands, but they all must be the same.
2469 // For the relational and logical instructions, the floating point type
2470 // can only be in the operands 1 and 2, not the result type. Also, the
2471 // operands must have the same type. For the extended instructions, the
2472 // floating point type can be in the result type or in the operands. It's
2473 // unclear if the operands and the result type must be the same. Let's
2474 // assume they must be. Therefore, for 1. and 2., we can check the first
2475 // operand type, and for 3. we can check the result type.
2476 assert(I.getNumOperands() >= 3 && "Expected at least 3 operands");
2477 Register ResReg = I.getOpcode() == SPIRV::OpExtInst
2478 ? I.getOperand(1).getReg()
2479 : I.getOperand(2).getReg();
2480 SPIRVType *ResType = GR->getSPIRVTypeForVReg(ResReg, I.getMF());
2481 const Type *Ty = GR->getTypeForSPIRVType(ResType);
2482 Ty = Ty->isVectorTy() ? cast<VectorType>(Ty)->getElementType() : Ty;
2483
2484 // Match instruction type with the FPFastMathDefaultInfoVec.
2485 bool Emit = false;
2486 for (SPIRV::FPFastMathDefaultInfo &Elem : FPFastMathDefaultInfoVec) {
2487 if (Ty == Elem.Ty) {
2488 FMFlags = Elem.FastMathFlags;
2489 Emit = Elem.ContractionOff || Elem.SignedZeroInfNanPreserve ||
2490 Elem.FPFastMathDefault;
2491 break;
2492 }
2493 }
2494
2495 if (FMFlags == SPIRV::FPFastMathMode::None && !Emit)
2496 return;
2497 }
2498 if (isFastMathModeAvailable(ST)) {
2499 Register DstReg = I.getOperand(0).getReg();
2500 buildOpDecorate(DstReg, I, TII, SPIRV::Decoration::FPFastMathMode,
2501 {FMFlags});
2502 }
2503}
2504
2505// Walk all functions and add decorations related to MI flags.
2506static void addDecorations(const Module &M, const SPIRVInstrInfo &TII,
2507 MachineModuleInfo *MMI, const SPIRVSubtarget &ST,
2509 const SPIRVGlobalRegistry *GR) {
2510 for (const Function &F : M) {
2512 if (!MF)
2513 continue;
2514
2515 for (auto &MBB : *MF)
2516 for (auto &MI : MBB)
2517 handleMIFlagDecoration(MI, ST, TII, MAI.Reqs, GR,
2519 }
2520}
2521
2522static void addMBBNames(const Module &M, const SPIRVInstrInfo &TII,
2523 MachineModuleInfo *MMI, const SPIRVSubtarget &ST,
2525 for (const Function &F : M) {
2527 if (!MF)
2528 continue;
2530 for (auto &MBB : *MF) {
2531 if (!MBB.hasName() || MBB.empty())
2532 continue;
2533 // Emit basic block names.
2534 Register Reg = MRI.createGenericVirtualRegister(LLT::scalar(64));
2535 MRI.setRegClass(Reg, &SPIRV::IDRegClass);
2536 buildOpName(Reg, MBB.getName(), *std::prev(MBB.end()), TII);
2537 MCRegister GlobalReg = MAI.getOrCreateMBBRegister(MBB);
2538 MAI.setRegisterAlias(MF, Reg, GlobalReg);
2539 }
2540 }
2541}
2542
2543// patching Instruction::PHI to SPIRV::OpPhi
2544static void patchPhis(const Module &M, SPIRVGlobalRegistry *GR,
2545 const SPIRVInstrInfo &TII, MachineModuleInfo *MMI) {
2546 for (const Function &F : M) {
2548 if (!MF)
2549 continue;
2550 for (auto &MBB : *MF) {
2551 for (MachineInstr &MI : MBB.phis()) {
2552 MI.setDesc(TII.get(SPIRV::OpPhi));
2553 Register ResTypeReg = GR->getSPIRVTypeID(
2554 GR->getSPIRVTypeForVReg(MI.getOperand(0).getReg(), MF));
2555 MI.insert(MI.operands_begin() + 1,
2556 {MachineOperand::CreateReg(ResTypeReg, false)});
2557 }
2558 }
2559
2560 MF->getProperties().setNoPHIs();
2561 }
2562}
2563
2565 const Module &M, SPIRV::ModuleAnalysisInfo &MAI, const Function *F) {
2566 auto it = MAI.FPFastMathDefaultInfoMap.find(F);
2567 if (it != MAI.FPFastMathDefaultInfoMap.end())
2568 return it->second;
2569
2570 // If the map does not contain the entry, create a new one. Initialize it to
2571 // contain all 3 elements sorted by bit width of target type: {half, float,
2572 // double}.
2573 SPIRV::FPFastMathDefaultInfoVector FPFastMathDefaultInfoVec;
2574 FPFastMathDefaultInfoVec.emplace_back(Type::getHalfTy(M.getContext()),
2575 SPIRV::FPFastMathMode::None);
2576 FPFastMathDefaultInfoVec.emplace_back(Type::getFloatTy(M.getContext()),
2577 SPIRV::FPFastMathMode::None);
2578 FPFastMathDefaultInfoVec.emplace_back(Type::getDoubleTy(M.getContext()),
2579 SPIRV::FPFastMathMode::None);
2580 return MAI.FPFastMathDefaultInfoMap[F] = std::move(FPFastMathDefaultInfoVec);
2581}
2582
2584 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec,
2585 const Type *Ty) {
2586 size_t BitWidth = Ty->getScalarSizeInBits();
2587 int Index =
2589 BitWidth);
2590 assert(Index >= 0 && Index < 3 &&
2591 "Expected FPFastMathDefaultInfo for half, float, or double");
2592 assert(FPFastMathDefaultInfoVec.size() == 3 &&
2593 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
2594 return FPFastMathDefaultInfoVec[Index];
2595}
2596
2597static void collectFPFastMathDefaults(const Module &M,
2599 const SPIRVSubtarget &ST) {
2600 if (!ST.canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2))
2601 return;
2602
2603 // Store the FPFastMathDefaultInfo in the FPFastMathDefaultInfoMap.
2604 // We need the entry point (function) as the key, and the target
2605 // type and flags as the value.
2606 // We also need to check ContractionOff and SignedZeroInfNanPreserve
2607 // execution modes, as they are now deprecated and must be replaced
2608 // with FPFastMathDefaultInfo.
2609 auto Node = M.getNamedMetadata("spirv.ExecutionMode");
2610 if (!Node)
2611 return;
2612
2613 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
2614 MDNode *MDN = cast<MDNode>(Node->getOperand(i));
2615 assert(MDN->getNumOperands() >= 2 && "Expected at least 2 operands");
2616 const Function *F = cast<Function>(
2617 cast<ConstantAsMetadata>(MDN->getOperand(0))->getValue());
2618 const auto EM =
2620 cast<ConstantAsMetadata>(MDN->getOperand(1))->getValue())
2621 ->getZExtValue();
2622 if (EM == SPIRV::ExecutionMode::FPFastMathDefault) {
2623 assert(MDN->getNumOperands() == 4 &&
2624 "Expected 4 operands for FPFastMathDefault");
2625
2626 const Type *T = cast<ValueAsMetadata>(MDN->getOperand(2))->getType();
2627 unsigned Flags =
2629 cast<ConstantAsMetadata>(MDN->getOperand(3))->getValue())
2630 ->getZExtValue();
2631 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2634 getFPFastMathDefaultInfo(FPFastMathDefaultInfoVec, T);
2635 Info.FastMathFlags = Flags;
2636 Info.FPFastMathDefault = true;
2637 } else if (EM == SPIRV::ExecutionMode::ContractionOff) {
2638 assert(MDN->getNumOperands() == 2 &&
2639 "Expected no operands for ContractionOff");
2640
2641 // We need to save this info for every possible FP type, i.e. {half,
2642 // float, double, fp128}.
2643 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2645 for (SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
2646 Info.ContractionOff = true;
2647 }
2648 } else if (EM == SPIRV::ExecutionMode::SignedZeroInfNanPreserve) {
2649 assert(MDN->getNumOperands() == 3 &&
2650 "Expected 1 operand for SignedZeroInfNanPreserve");
2651 unsigned TargetWidth =
2653 cast<ConstantAsMetadata>(MDN->getOperand(2))->getValue())
2654 ->getZExtValue();
2655 // We need to save this info only for the FP type with TargetWidth.
2656 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2660 assert(Index >= 0 && Index < 3 &&
2661 "Expected FPFastMathDefaultInfo for half, float, or double");
2662 assert(FPFastMathDefaultInfoVec.size() == 3 &&
2663 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
2664 FPFastMathDefaultInfoVec[Index].SignedZeroInfNanPreserve = true;
2665 }
2666 }
2667}
2668
2670
2672 AU.addRequired<TargetPassConfig>();
2673 AU.addRequired<MachineModuleInfoWrapperPass>();
2674}
2675
2677 SPIRVTargetMachine &TM =
2679 ST = TM.getSubtargetImpl();
2680 GR = ST->getSPIRVGlobalRegistry();
2681 TII = ST->getInstrInfo();
2682
2684
2685 setBaseInfo(M);
2686
2687 patchPhis(M, GR, *TII, MMI);
2688
2689 addMBBNames(M, *TII, MMI, *ST, MAI);
2690 collectFPFastMathDefaults(M, MAI, *ST);
2691 addDecorations(M, *TII, MMI, *ST, MAI, GR);
2692
2693 collectReqs(M, MAI, MMI, *ST);
2694
2695 // Process type/const/global var/func decl instructions, number their
2696 // destination registers from 0 to N, collect Extensions and Capabilities.
2697 collectReqs(M, MAI, MMI, *ST);
2698 collectDeclarations(M);
2699
2700 // Number rest of registers from N+1 onwards.
2701 numberRegistersGlobally(M);
2702
2703 // Collect OpName, OpEntryPoint, OpDecorate etc, process other instructions.
2704 processOtherInstrs(M);
2705
2706 // If there are no entry points, we need the Linkage capability.
2707 if (MAI.MS[SPIRV::MB_EntryPoints].empty())
2708 MAI.Reqs.addCapability(SPIRV::Capability::Linkage);
2709
2710 // Set maximum ID used.
2711 GR->setBound(MAI.MaxID);
2712
2713 return false;
2714}
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
ReachingDefInfo InstSet & ToRemove
MachineBasicBlock & MBB
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static SPIRV::FPFastMathDefaultInfoVector & getOrCreateFPFastMathDefaultInfoVec(const Module &M, DenseMap< Function *, SPIRV::FPFastMathDefaultInfoVector > &FPFastMathDefaultInfoMap, Function *F)
static SPIRV::FPFastMathDefaultInfo & getFPFastMathDefaultInfo(SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec, const Type *Ty)
#define ATOM_FLT_REQ_EXT_MSG(ExtName)
static cl::opt< bool > SPVDumpDeps("spv-dump-deps", cl::desc("Dump MIR with SPIR-V dependencies info"), cl::Optional, cl::init(false))
unsigned unsigned DefaultVal
unsigned OpIndex
static cl::list< SPIRV::Capability::Capability > AvoidCapabilities("avoid-spirv-capabilities", cl::desc("SPIR-V capabilities to avoid if there are " "other options enabling a feature"), cl::ZeroOrMore, cl::Hidden, cl::values(clEnumValN(SPIRV::Capability::Shader, "Shader", "SPIR-V Shader capability")))
This file contains some templates that are useful if you are working with the STL at all.
#define LLVM_DEBUG(...)
Definition Debug.h:114
Target-Independent Code Generator Pass Configuration Options pass.
The Input class is used to parse a yaml document into in-memory structs and vectors.
This is the shared class of boolean and integer constants.
Definition Constants.h:87
This is an important base class in LLVM.
Definition Constant.h:43
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Wrapper class representing physical registers. Should be passed by value.
Definition MCRegister.h:41
constexpr bool isValid() const
Definition MCRegister.h:84
Metadata node.
Definition Metadata.h:1078
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1442
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1448
Tracking metadata reference owned by Metadata.
Definition Metadata.h:900
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
const MachineFunctionProperties & getProperties() const
Get the function properties.
Register getReg(unsigned Idx) const
Get the register for the operand index.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
LLVM_ABI const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
const MachineOperand & getOperand(unsigned i) const
This class contains meta information specific to a module.
LLVM_ABI MachineFunction * getMachineFunction(const Function &F) const
Returns the MachineFunction associated to IR function F if there is one, otherwise nullptr.
MachineOperand class - Representation of each machine instruction operand.
unsigned getSubReg() const
int64_t getImm() const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
LLVM_ABI void print(raw_ostream &os, const TargetRegisterInfo *TRI=nullptr) const
Print the MachineOperand to os.
MachineInstr * getParent()
getParent - Return the instruction that this operand belongs to.
static MachineOperand CreateImm(int64_t Val)
MachineOperandType getType() const
getType - Returns the MachineOperandType for this operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
virtual void print(raw_ostream &OS, const Module *M) const
print - Print out the internal state of the pass.
Definition Pass.cpp:140
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const
bool isConstantInstr(const MachineInstr &MI) const
const SPIRVInstrInfo * getInstrInfo() const override
SPIRVGlobalRegistry * getSPIRVGlobalRegistry() const
const SPIRVSubtarget * getSubtargetImpl() const
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:133
bool contains(const T &V) const
Check if the SmallSet contains the given element.
Definition SmallSet.h:228
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:183
reference emplace_back(ArgTypes &&... Args)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
Target-Independent Code Generator Pass Configuration Options.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:285
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:284
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:282
Represents a version number in the form major[.minor[.subminor[.build]]].
bool empty() const
Determine whether this version information is empty (e.g., all version components are zero).
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
Definition ilist_node.h:348
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SmallVector< const MachineInstr * > InstrList
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
NodeAddr< InstrNode * > Instr
Definition RDFGraph.h:389
This is an optimization pass for GlobalISel generic memory operations.
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
std::string getStringImm(const MachineInstr &MI, unsigned StartIndex)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
hash_code hash_value(const FixedPointSemantics &Val)
ExtensionList getSymbolicOperandExtensions(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
CapabilityList getSymbolicOperandCapabilities(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
SmallVector< SPIRV::Extension::Extension, 8 > ExtensionList
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
SmallVector< size_t > InstrSignature
VersionTuple getSymbolicOperandMaxVersion(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
MachineInstr * getImm(const MachineOperand &MO, const MachineRegisterInfo *MRI)
CapabilityList getCapabilitiesEnabledByExtension(SPIRV::Extension::Extension Extension)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
const MachineInstr SPIRVType
std::string getSymbolicOperandMnemonic(SPIRV::OperandCategory::OperandCategory Category, int32_t Value)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
VersionTuple getSymbolicOperandMinVersion(SPIRV::OperandCategory::OperandCategory Category, uint32_t Value)
constexpr unsigned BitWidth
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
SmallVector< SPIRV::Capability::Capability, 8 > CapabilityList
std::set< InstrSignature > InstrTraces
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
std::map< SmallVector< size_t >, unsigned > InstrGRegsMap
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:180
#define N
SmallSet< SPIRV::Capability::Capability, 4 > S
static struct SPIRV::ModuleAnalysisInfo MAI
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
static size_t computeFPFastMathDefaultInfoVecIndex(size_t BitWidth)
Definition SPIRVUtils.h:146
void setSkipEmission(const MachineInstr *MI)
MCRegister getRegisterAlias(const MachineFunction *MF, Register Reg)
MCRegister getOrCreateMBBRegister(const MachineBasicBlock &MBB)
InstrList MS[NUM_MODULE_SECTIONS]
AddressingModel::AddressingModel Addr
void setRegisterAlias(const MachineFunction *MF, Register Reg, MCRegister AliasReg)
DenseMap< const Function *, SPIRV::FPFastMathDefaultInfoVector > FPFastMathDefaultInfoMap
void addCapabilities(const CapabilityList &ToAdd)
bool isCapabilityAvailable(Capability::Capability Cap) const
void checkSatisfiable(const SPIRVSubtarget &ST) const
void getAndAddRequirements(SPIRV::OperandCategory::OperandCategory Category, uint32_t i, const SPIRVSubtarget &ST)
void addExtension(Extension::Extension ToAdd)
void initAvailableCapabilities(const SPIRVSubtarget &ST)
void removeCapabilityIf(const Capability::Capability ToRemove, const Capability::Capability IfPresent)
void addCapability(Capability::Capability ToAdd)
void addAvailableCaps(const CapabilityList &ToAdd)
void addRequirements(const Requirements &Req)
const std::optional< Capability::Capability > Cap