LLVM 23.0.0git
SPIRVCallLowering.cpp
Go to the documentation of this file.
1//===--- SPIRVCallLowering.cpp - Call lowering ------------------*- 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// This file implements the lowering of LLVM calls to machine code calls for
10// GlobalISel.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRVCallLowering.h"
16#include "SPIRV.h"
17#include "SPIRVBuiltins.h"
18#include "SPIRVGlobalRegistry.h"
19#include "SPIRVISelLowering.h"
20#include "SPIRVMetadata.h"
21#include "SPIRVRegisterInfo.h"
22#include "SPIRVSubtarget.h"
23#include "SPIRVUtils.h"
24#include "llvm/ADT/STLExtras.h"
27#include "llvm/IR/IntrinsicsSPIRV.h"
28#include "llvm/Support/ModRef.h"
29
30using namespace llvm;
31
35
37 const Value *Val, ArrayRef<Register> VRegs,
39 Register SwiftErrorVReg) const {
40 // Ignore if called from the internal service function
41 if (MIRBuilder.getMF()
44 .isValid())
45 return true;
46
47 // Currently all return types should use a single register.
48 // TODO: handle the case of multiple registers.
49 if (VRegs.size() > 1)
50 return false;
51
52 if (Val) {
53 const auto &STI = MIRBuilder.getMF().getSubtarget();
54 MIRBuilder.buildInstr(SPIRV::OpReturnValue)
55 .addUse(VRegs[0])
56 .constrainAllUses(MIRBuilder.getTII(), *STI.getRegisterInfo(),
57 *STI.getRegBankInfo());
58 return true;
59 }
60 MIRBuilder.buildInstr(SPIRV::OpReturn);
61 return true;
62}
63
64// Based on the LLVM function attributes, get a SPIR-V FunctionControl.
66 const SPIRVSubtarget *ST) {
67 MemoryEffects MemEffects = F.getMemoryEffects();
68
69 uint32_t FuncControl = static_cast<uint32_t>(SPIRV::FunctionControl::None);
70
71 if (F.hasFnAttribute(Attribute::AttrKind::NoInline))
72 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::DontInline);
73 else if (F.hasFnAttribute(Attribute::AttrKind::AlwaysInline))
74 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Inline);
75
76 if (MemEffects.doesNotAccessMemory())
77 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Pure);
78 else if (MemEffects.onlyReadsMemory())
79 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Const);
80
81 if (ST->canUseExtension(SPIRV::Extension::SPV_INTEL_optnone) ||
82 ST->canUseExtension(SPIRV::Extension::SPV_EXT_optnone))
83 if (F.hasFnAttribute(Attribute::OptimizeNone))
84 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::OptNoneEXT);
85
86 return FuncControl;
87}
88
89static ConstantInt *getConstInt(MDNode *MD, unsigned NumOp) {
90 if (MD->getNumOperands() > NumOp) {
91 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(NumOp));
92 if (CMeta)
93 return dyn_cast<ConstantInt>(CMeta->getValue());
94 }
95 return nullptr;
96}
97
98// If the function has pointer arguments, we are forced to re-create this
99// function type from the very beginning, changing PointerType by
100// TypedPointerType for each pointer argument. Otherwise, the same `Type*`
101// potentially corresponds to different SPIR-V function type, effectively
102// invalidating logic behind global registry and duplicates tracker.
103static FunctionType *
105 FunctionType *FTy, SPIRVTypeInst SRetTy,
106 const SmallVector<SPIRVTypeInst, 4> &SArgTys) {
107 bool hasArgPtrs = any_of(F.args(), [](const Argument &Arg) {
108 // check if it's an instance of a non-typed PointerType
109 return Arg.getType()->isPointerTy();
110 });
111 if (!hasArgPtrs) {
112 Type *RetTy = FTy->getReturnType();
113 // check if it's an instance of a non-typed PointerType
114 if (!RetTy->isPointerTy())
115 return FTy;
116 }
117
118 // re-create function type, using TypedPointerType instead of PointerType to
119 // properly trace argument types
120 const Type *RetTy = GR->getTypeForSPIRVType(SRetTy);
122 for (auto SArgTy : SArgTys)
123 ArgTys.push_back(const_cast<Type *>(GR->getTypeForSPIRVType(SArgTy)));
124 return FunctionType::get(const_cast<Type *>(RetTy), ArgTys, false);
125}
126
127static SPIRV::AccessQualifier::AccessQualifier
128getArgAccessQual(const Function &F, unsigned ArgIdx) {
129 if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
130 return SPIRV::AccessQualifier::ReadWrite;
131
132 MDString *ArgAttribute = getOCLKernelArgAccessQual(F, ArgIdx);
133 if (!ArgAttribute)
134 return SPIRV::AccessQualifier::ReadWrite;
135
136 if (ArgAttribute->getString() == "read_only")
137 return SPIRV::AccessQualifier::ReadOnly;
138 if (ArgAttribute->getString() == "write_only")
139 return SPIRV::AccessQualifier::WriteOnly;
140 return SPIRV::AccessQualifier::ReadWrite;
141}
142
143static std::vector<SPIRV::Decoration::Decoration>
144getKernelArgTypeQual(const Function &F, unsigned ArgIdx) {
145 MDString *ArgAttribute = getOCLKernelArgTypeQual(F, ArgIdx);
146 if (ArgAttribute && ArgAttribute->getString() == "volatile")
147 return {SPIRV::Decoration::Volatile};
148 return {};
149}
150
151static SPIRVTypeInst getArgSPIRVType(const Function &F, unsigned ArgIdx,
153 MachineIRBuilder &MIRBuilder,
154 const SPIRVSubtarget &ST) {
155 // Read argument's access qualifier from metadata or default.
156 SPIRV::AccessQualifier::AccessQualifier ArgAccessQual =
157 getArgAccessQual(F, ArgIdx);
158
159 Type *OriginalArgType =
161
162 // Vector of untyped pointers: build with the deduced pointee instead of
163 // the default i8 (mismatches typed uses downstream).
164 Argument *Arg = F.getArg(ArgIdx);
165 if (auto *VTy = dyn_cast<FixedVectorType>(OriginalArgType);
166 VTy && isUntypedPointerTy(VTy->getElementType()))
167 if (Type *ElemTy = GR->findDeducedElementType(Arg))
170 ElemTy, MIRBuilder,
172 getPointerAddressSpace(OriginalArgType), ST)),
173 VTy->getNumElements(), MIRBuilder, true);
174
175 // If OriginalArgType is non-pointer, use the OriginalArgType (the type cannot
176 // be legally reassigned later).
177 if (!isPointerTy(OriginalArgType))
178 return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual,
179 true);
180
181 Type *ArgType = Arg->getType();
182 if (isTypedPointerTy(ArgType)) {
184 cast<TypedPointerType>(ArgType)->getElementType(), MIRBuilder,
186 }
187
188 // In case OriginalArgType is of untyped pointer type, there are three
189 // possibilities:
190 // 1) This is a pointer of an LLVM IR element type, passed byval/byref.
191 // 2) This is an OpenCL/SPIR-V builtin type if there is spv_assign_type
192 // intrinsic assigning a TargetExtType.
193 // 3) This is a pointer, try to retrieve pointer element type from a
194 // spv_assign_ptr_type intrinsic or otherwise use default pointer element
195 // type.
196 if (hasPointeeTypeAttr(Arg)) {
198 getPointeeTypeByAttr(Arg), MIRBuilder,
200 }
201
202 for (auto User : Arg->users()) {
204 // Check if this is spv_assign_type assigning OpenCL/SPIR-V builtin type.
205 if (II && II->getIntrinsicID() == Intrinsic::spv_assign_type) {
206 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
207 Type *BuiltinType =
208 cast<ConstantAsMetadata>(VMD->getMetadata())->getType();
209 assert(BuiltinType->isTargetExtTy() && "Expected TargetExtType");
210 return GR->getOrCreateSPIRVType(BuiltinType, MIRBuilder, ArgAccessQual,
211 true);
212 }
213
214 // Check if this is spv_assign_ptr_type assigning pointer element type.
215 if (!II || II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type)
216 continue;
217
218 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
219 Type *ElementTy =
222 ElementTy, MIRBuilder,
224 cast<ConstantInt>(II->getOperand(2))->getZExtValue(), ST));
225 }
226
227 // Replace PointerType with TypedPointerType to be able to map SPIR-V types to
228 // LLVM types in a consistent manner
229 return GR->getOrCreateSPIRVType(toTypedPointer(OriginalArgType), MIRBuilder,
230 ArgAccessQual, true);
231}
232
233static SPIRV::ExecutionModel::ExecutionModel
236 "Environment must be resolved before lowering entry points.");
237
238 if (STI.isKernel())
239 return SPIRV::ExecutionModel::Kernel;
240
241 auto attribute = F.getFnAttribute("hlsl.shader");
242 if (!attribute.isValid()) {
244 "This entry point lacks mandatory hlsl.shader attribute.");
245 }
246
247 const auto value = attribute.getValueAsString();
248 if (value == "compute")
249 return SPIRV::ExecutionModel::GLCompute;
250 if (value == "vertex")
251 return SPIRV::ExecutionModel::Vertex;
252 if (value == "pixel")
253 return SPIRV::ExecutionModel::Fragment;
254
255 report_fatal_error("This HLSL entry point is not supported by this backend.");
256}
257
259 const Function &F,
261 FunctionLoweringInfo &FLI) const {
262 // Discard the internal service function
263 if (F.getFnAttribute(SPIRV_BACKEND_SERVICE_FUN_NAME).isValid())
264 return true;
265
266 assert(GR && "Must initialize the SPIRV type registry before lowering args.");
267 GR->setCurrentFunc(MIRBuilder.getMF());
268
269 // Get access to information about available extensions
270 const SPIRVSubtarget *ST =
271 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
272
273 // Assign types and names to all args, and store their types for later.
275 if (VRegs.size() > 0) {
276 unsigned i = 0;
277 for (const auto &Arg : F.args()) {
278 // Currently formal args should use single registers.
279 // TODO: handle the case of multiple registers.
280 if (VRegs[i].size() > 1)
281 return false;
282 SPIRVTypeInst SpirvTy = getArgSPIRVType(F, i, GR, MIRBuilder, *ST);
283 GR->assignSPIRVTypeToVReg(SpirvTy, VRegs[i][0], MIRBuilder.getMF());
284 ArgTypeVRegs.push_back(SpirvTy);
285
286 if (Arg.hasName())
287 buildOpName(VRegs[i][0], Arg.getName(), MIRBuilder);
288 if (isPointerTyOrWrapper(Arg.getType())) {
289 auto DerefBytes = static_cast<unsigned>(Arg.getDereferenceableBytes());
290 if (DerefBytes != 0)
291 buildOpDecorate(VRegs[i][0], MIRBuilder,
292 SPIRV::Decoration::MaxByteOffset, {DerefBytes});
293 }
294 if (Arg.hasAttribute(Attribute::Alignment) && !ST->isShader()) {
295 auto Alignment = static_cast<unsigned>(
296 Arg.getAttribute(Attribute::Alignment).getValueAsInt());
297 buildOpDecorate(VRegs[i][0], MIRBuilder, SPIRV::Decoration::Alignment,
298 {Alignment});
299 }
300 if (!ST->isShader()) {
301 if (Arg.hasAttribute(Attribute::ReadOnly)) {
302 auto Attr =
303 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoWrite);
304 buildOpDecorate(VRegs[i][0], MIRBuilder,
305 SPIRV::Decoration::FuncParamAttr, {Attr});
306 }
307 if (Arg.hasAttribute(Attribute::ZExt)) {
308 auto Attr =
309 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Zext);
310 buildOpDecorate(VRegs[i][0], MIRBuilder,
311 SPIRV::Decoration::FuncParamAttr, {Attr});
312 }
313 if (Arg.hasAttribute(Attribute::SExt)) {
314 auto Attr =
315 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Sext);
316 buildOpDecorate(VRegs[i][0], MIRBuilder,
317 SPIRV::Decoration::FuncParamAttr, {Attr});
318 }
319 if (Arg.hasAttribute(Attribute::NoAlias)) {
320 auto Attr =
321 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoAlias);
322 buildOpDecorate(VRegs[i][0], MIRBuilder,
323 SPIRV::Decoration::FuncParamAttr, {Attr});
324 }
325 // TODO: the AMDGPU BE only supports ByRef argument passing, thus for
326 // AMDGCN flavoured SPIRV we CodeGen for ByRef, but lower it to
327 // ByVal, handling the impedance mismatch during reverse
328 // translation from SPIRV to LLVM IR; the vendor check should be
329 // removed once / if SPIRV adds ByRef support.
330 if (Arg.hasAttribute(Attribute::ByVal) ||
331 (Arg.hasAttribute(Attribute::ByRef) &&
332 F.getParent()->getTargetTriple().getVendor() ==
334 auto Attr =
335 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::ByVal);
336 buildOpDecorate(VRegs[i][0], MIRBuilder,
337 SPIRV::Decoration::FuncParamAttr, {Attr});
338 }
339 if (Arg.hasAttribute(Attribute::StructRet)) {
340 auto Attr =
341 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Sret);
342 buildOpDecorate(VRegs[i][0], MIRBuilder,
343 SPIRV::Decoration::FuncParamAttr, {Attr});
344 }
345 }
346
347 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
348 std::vector<SPIRV::Decoration::Decoration> ArgTypeQualDecs =
350 for (SPIRV::Decoration::Decoration Decoration : ArgTypeQualDecs)
351 buildOpDecorate(VRegs[i][0], MIRBuilder, Decoration, {});
352 }
353
354 MDNode *Node = F.getMetadata("spirv.ParameterDecorations");
355 if (Node && i < Node->getNumOperands() &&
356 isa<MDNode>(Node->getOperand(i))) {
357 MDNode *MD = cast<MDNode>(Node->getOperand(i));
358 for (const MDOperand &MDOp : MD->operands()) {
359 MDNode *MD2 = dyn_cast<MDNode>(MDOp);
360 assert(MD2 && "Metadata operand is expected");
361 ConstantInt *Const = getConstInt(MD2, 0);
362 assert(Const && "MDOperand should be ConstantInt");
363 auto Dec =
364 static_cast<SPIRV::Decoration::Decoration>(Const->getZExtValue());
365 std::vector<uint32_t> DecVec;
366 for (unsigned j = 1; j < MD2->getNumOperands(); j++) {
367 ConstantInt *Const = getConstInt(MD2, j);
368 assert(Const && "MDOperand should be ConstantInt");
369 DecVec.push_back(static_cast<uint32_t>(Const->getZExtValue()));
370 }
371 buildOpDecorate(VRegs[i][0], MIRBuilder, Dec, DecVec);
372 }
373 }
374 ++i;
375 }
376 }
377
378 auto MRI = MIRBuilder.getMRI();
379 Register FuncVReg = MRI->createGenericVirtualRegister(LLT::scalar(64));
380 MRI->setRegClass(FuncVReg, &SPIRV::iIDRegClass);
382 Type *FRetTy = FTy->getReturnType();
383 if (isUntypedPointerTy(FRetTy)) {
384 if (Type *FRetElemTy = GR->findDeducedElementType(&F)) {
386 toTypedPointer(FRetElemTy), getPointerAddressSpace(FRetTy));
387 GR->addReturnType(&F, DerivedTy);
388 FRetTy = DerivedTy;
389 }
390 }
391 SPIRVTypeInst RetTy = GR->getOrCreateSPIRVType(
392 FRetTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true);
393 FTy = fixFunctionTypeIfPtrArgs(GR, F, FTy, RetTy, ArgTypeVRegs);
394 SPIRVTypeInst FuncTy = GR->getOrCreateOpTypeFunctionWithArgs(
395 FTy, RetTy, ArgTypeVRegs, MIRBuilder);
396 uint32_t FuncControl = getFunctionControl(F, ST);
397
398 // Add OpFunction instruction
399 MachineInstrBuilder MB = MIRBuilder.buildInstr(SPIRV::OpFunction)
400 .addDef(FuncVReg)
401 .addUse(GR->getSPIRVTypeID(RetTy))
402 .addImm(FuncControl)
403 .addUse(GR->getSPIRVTypeID(FuncTy));
404 GR->recordFunctionDefinition(&F, &MB.getInstr()->getOperand(0));
405 GR->addGlobalObject(&F, &MIRBuilder.getMF(), FuncVReg);
406 if (F.isDeclaration())
407 GR->add(&F, MB);
408
409 // Add OpFunctionParameter instructions
410 int i = 0;
411 for (const auto &Arg : F.args()) {
412 assert(VRegs[i].size() == 1 && "Formal arg has multiple vregs");
413 Register ArgReg = VRegs[i][0];
414 MRI->setRegClass(ArgReg, GR->getRegClass(ArgTypeVRegs[i]));
415 auto MIB = MIRBuilder.buildInstr(SPIRV::OpFunctionParameter)
416 .addDef(ArgReg)
417 .addUse(GR->getSPIRVTypeID(ArgTypeVRegs[i]));
418 if (F.isDeclaration())
419 GR->add(&Arg, MIB);
420 GR->addGlobalObject(&Arg, &MIRBuilder.getMF(), ArgReg);
421 i++;
422 }
423 // Name the function.
424 if (F.hasName())
425 buildOpName(FuncVReg, F.getName(), MIRBuilder);
426
427 // Handle entry points and function linkage.
428 if (isEntryPoint(F)) {
429 auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint)
430 .addImm(static_cast<uint32_t>(getExecutionModel(*ST, F)))
431 .addUse(FuncVReg);
432 addStringImm(F.getName(), MIB);
433 } else if (const auto LnkTy = getSpirvLinkageTypeFor(*ST, F)) {
434 buildOpDecorate(FuncVReg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
435 {static_cast<uint32_t>(*LnkTy)}, F.getName());
436 }
437
438 // Handle function pointers decoration
439 bool hasFunctionPointers =
440 ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
441 if (hasFunctionPointers) {
442 if (F.hasFnAttribute("referenced-indirectly")) {
443 assert((F.getCallingConv() != CallingConv::SPIR_KERNEL) &&
444 "Unexpected 'referenced-indirectly' attribute of the kernel "
445 "function");
446 buildOpDecorate(FuncVReg, MIRBuilder,
447 SPIRV::Decoration::ReferencedIndirectlyINTEL, {});
448 }
449 }
450
451 return true;
452}
453
454// TODO:
455// - add a topological sort of IndirectCalls to ensure the best types knowledge
456// - we may need to fix function formal parameter types if they are opaque
457// pointers used as function pointers in these indirect calls
458// - defaulting to StorageClass::Function in the absence of the
459// SPV_INTEL_function_pointers extension seems wrong, as that might not be
460// able to hold a full width pointer to function, and it also does not model
461// the semantics of a pointer to function in a generic fashion.
462void SPIRVCallLowering::produceIndirectPtrType(
463 MachineIRBuilder &MIRBuilder,
464 const SPIRVCallLowering::SPIRVIndirectCall &IC) const {
465 // Create indirect call data type if any
466 MachineFunction &MF = MIRBuilder.getMF();
468 SPIRVTypeInst SpirvRetTy = GR->getOrCreateSPIRVType(
469 IC.RetTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true);
470 SmallVector<SPIRVTypeInst, 4> SpirvArgTypes;
471 for (size_t i = 0; i < IC.ArgTys.size(); ++i) {
473 IC.ArgTys[i], MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true);
474 SpirvArgTypes.push_back(SPIRVTy);
475 if (!GR->getSPIRVTypeForVReg(IC.ArgRegs[i]))
476 GR->assignSPIRVTypeToVReg(SPIRVTy, IC.ArgRegs[i], MF);
477 }
478 // SPIR-V function type:
479 FunctionType *FTy =
480 FunctionType::get(const_cast<Type *>(IC.RetTy), IC.ArgTys, false);
482 FTy, SpirvRetTy, SpirvArgTypes, MIRBuilder);
483 // SPIR-V pointer to function type:
484 auto SC = ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers)
485 ? SPIRV::StorageClass::CodeSectionINTEL
486 : SPIRV::StorageClass::Function;
487 SPIRVTypeInst IndirectFuncPtrTy =
488 GR->getOrCreateSPIRVPointerType(SpirvFuncTy, MIRBuilder, SC);
489 // Correct the Callee type
490 GR->assignSPIRVTypeToVReg(IndirectFuncPtrTy, IC.Callee, MF);
491}
492
494 CallLoweringInfo &Info) const {
495 // Currently call returns should have single vregs.
496 // TODO: handle the case of multiple registers.
497 if (Info.OrigRet.Regs.size() > 1)
498 return false;
499 MachineFunction &MF = MIRBuilder.getMF();
500 GR->setCurrentFunc(MF);
501 const Function *CF = nullptr;
502 std::string DemangledName;
503 const Type *OrigRetTy = Info.OrigRet.Ty;
504
505 // Emit a regular OpFunctionCall. If it's an externally declared function,
506 // be sure to emit its type and function declaration here. It will be hoisted
507 // globally later.
508 if (Info.Callee.isGlobal()) {
509 std::string FuncName = Info.Callee.getGlobal()->getName().str();
510 DemangledName = getOclOrSpirvBuiltinDemangledName(FuncName);
511 CF = dyn_cast_or_null<const Function>(Info.Callee.getGlobal());
512 // TODO: support constexpr casts and indirect calls.
513 if (CF == nullptr)
514 return false;
515
517 OrigRetTy = FTy->getReturnType();
518 if (isUntypedPointerTy(OrigRetTy)) {
519 if (auto *DerivedRetTy = GR->findReturnType(CF))
520 OrigRetTy = DerivedRetTy;
521 }
522 }
523
524 MachineRegisterInfo *MRI = MIRBuilder.getMRI();
525 Register ResVReg =
526 Info.OrigRet.Regs.empty() ? Register(0) : Info.OrigRet.Regs[0];
527 const auto *ST = static_cast<const SPIRVSubtarget *>(&MF.getSubtarget());
528
529 bool isFunctionDecl = CF && CF->isDeclaration();
530 if (isFunctionDecl && !DemangledName.empty()) {
531 if (ResVReg.isValid()) {
532 if (!GR->getSPIRVTypeForVReg(ResVReg)) {
533 const Type *RetTy = OrigRetTy;
534 if (auto *PtrRetTy = dyn_cast<PointerType>(OrigRetTy)) {
535 const Value *OrigValue = Info.OrigRet.OrigValue;
536 if (!OrigValue)
537 OrigValue = Info.CB;
538 if (OrigValue)
539 if (Type *ElemTy = GR->findDeducedElementType(OrigValue))
540 RetTy =
541 TypedPointerType::get(ElemTy, PtrRetTy->getAddressSpace());
542 }
543 setRegClassType(ResVReg, RetTy, GR, MIRBuilder,
544 SPIRV::AccessQualifier::ReadWrite, true);
545 }
546 } else {
547 ResVReg = createVirtualRegister(OrigRetTy, GR, MIRBuilder,
548 SPIRV::AccessQualifier::ReadWrite, true);
549 }
551 for (auto Arg : Info.OrigArgs) {
552 assert(Arg.Regs.size() == 1 && "Call arg has multiple VRegs");
553 Register ArgReg = Arg.Regs[0];
554 ArgVRegs.push_back(ArgReg);
555 SPIRVTypeInst SpvType = GR->getSPIRVTypeForVReg(ArgReg);
556 if (!SpvType) {
557 Type *ArgTy = nullptr;
558 if (auto *PtrArgTy = dyn_cast<PointerType>(Arg.Ty)) {
559 // If Arg.Ty is an untyped pointer (i.e., ptr [addrspace(...)]) and we
560 // don't have access to original value in LLVM IR or info about
561 // deduced pointee type, then we should wait with setting the type for
562 // the virtual register until pre-legalizer step when we access
563 // @llvm.spv.assign.ptr.type.p...(...)'s info.
564 if (Arg.OrigValue)
565 if (Type *ElemTy = GR->findDeducedElementType(Arg.OrigValue))
566 ArgTy =
567 TypedPointerType::get(ElemTy, PtrArgTy->getAddressSpace());
568 } else {
569 ArgTy = Arg.Ty;
570 }
571 if (ArgTy) {
572 SpvType = GR->getOrCreateSPIRVType(
573 ArgTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true);
574 GR->assignSPIRVTypeToVReg(SpvType, ArgReg, MF);
575 }
576 }
577 if (!MRI->getRegClassOrNull(ArgReg)) {
578 // Either we have SpvType created, or Arg.Ty is an untyped pointer and
579 // we know its virtual register's class and type even if we don't know
580 // pointee type.
581 MRI->setRegClass(ArgReg, SpvType ? GR->getRegClass(SpvType)
582 : &SPIRV::pIDRegClass);
583 MRI->setType(
584 ArgReg,
585 SpvType ? GR->getRegType(SpvType)
586 : LLT::pointer(cast<PointerType>(Arg.Ty)->getAddressSpace(),
587 GR->getPointerSize()));
588 }
589 }
590 if (auto Res = SPIRV::lowerBuiltin(
591 DemangledName, ST->getPreferredInstructionSet(), MIRBuilder,
592 ResVReg, OrigRetTy, ArgVRegs, GR, *Info.CB))
593 return *Res;
594 }
595
596 if (isFunctionDecl && !GR->find(CF, &MF).isValid()) {
597 // Emit the type info and forward function declaration to the first MBB
598 // to ensure VReg definition dependencies are valid across all MBBs.
599 MachineIRBuilder FirstBlockBuilder;
600 FirstBlockBuilder.setMF(MF);
601 FirstBlockBuilder.setMBB(*MF.getBlockNumbered(0));
602
605 for (const Argument &Arg : CF->args()) {
606 if (MIRBuilder.getDataLayout().getTypeStoreSize(Arg.getType()).isZero())
607 continue; // Don't handle zero sized types.
609 MRI->setRegClass(Reg, &SPIRV::iIDRegClass);
610 ToInsert.push_back({Reg});
611 VRegArgs.push_back(ToInsert.back());
612 }
613 // TODO: Reuse FunctionLoweringInfo
614 FunctionLoweringInfo FuncInfo;
615 lowerFormalArguments(FirstBlockBuilder, *CF, VRegArgs, FuncInfo);
616 }
617
618 // Ignore the call if it's called from the internal service function
619 if (MIRBuilder.getMF()
620 .getFunction()
622 .isValid()) {
623 // insert a no-op
624 MIRBuilder.buildTrap();
625 return true;
626 }
627
628 unsigned CallOp;
629 if (Info.CB->isIndirectCall()) {
630 if (!ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers))
631 report_fatal_error("An indirect call is encountered but SPIR-V without "
632 "extensions does not support it",
633 false);
634 // Set instruction operation according to SPV_INTEL_function_pointers
635 CallOp = SPIRV::OpFunctionPointerCallINTEL;
636 // Collect information about the indirect call to create correct types.
637 Register CalleeReg = Info.Callee.getReg();
638 if (CalleeReg.isValid()) {
639 SPIRVCallLowering::SPIRVIndirectCall IndirectCall;
640 IndirectCall.Callee = CalleeReg;
642 IndirectCall.RetTy = OrigRetTy = FTy->getReturnType();
643 assert(FTy->getNumParams() == Info.OrigArgs.size() &&
644 "Function types mismatch");
645 for (unsigned I = 0; I != Info.OrigArgs.size(); ++I) {
646 assert(Info.OrigArgs[I].Regs.size() == 1 &&
647 "Call arg has multiple VRegs");
648 IndirectCall.ArgTys.push_back(FTy->getParamType(I));
649 IndirectCall.ArgRegs.push_back(Info.OrigArgs[I].Regs[0]);
650 }
651 produceIndirectPtrType(MIRBuilder, IndirectCall);
652 }
653 } else {
654 // Emit a regular OpFunctionCall
655 CallOp = SPIRV::OpFunctionCall;
656 }
657
658 // Make sure there's a valid return reg, even for functions returning void.
659 if (!ResVReg.isValid())
660 ResVReg = MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
661 SPIRVTypeInst RetType = GR->assignTypeToVReg(
662 OrigRetTy, ResVReg, MIRBuilder, SPIRV::AccessQualifier::ReadWrite, true);
663
664 // Emit the call instruction and its args.
665 auto MIB = MIRBuilder.buildInstr(CallOp)
666 .addDef(ResVReg)
667 .addUse(GR->getSPIRVTypeID(RetType))
668 .add(Info.Callee);
669
670 for (const auto &Arg : Info.OrigArgs) {
671 // Currently call args should have single vregs.
672 if (Arg.Regs.size() > 1)
673 return false;
674 MIB.addUse(Arg.Regs[0]);
675 }
676
677 if (ST->canUseExtension(SPIRV::Extension::SPV_INTEL_memory_access_aliasing)) {
678 // Process aliasing metadata.
679 const CallBase *CI = Info.CB;
680 if (CI && CI->hasMetadata()) {
681 if (MDNode *MD = CI->getMetadata(LLVMContext::MD_alias_scope))
682 GR->buildMemAliasingOpDecorate(ResVReg, MIRBuilder,
683 SPIRV::Decoration::AliasScopeINTEL, MD);
684 if (MDNode *MD = CI->getMetadata(LLVMContext::MD_noalias))
685 GR->buildMemAliasingOpDecorate(ResVReg, MIRBuilder,
686 SPIRV::Decoration::NoAliasINTEL, MD);
687 }
688 }
689
690 MIB.constrainAllUses(MIRBuilder.getTII(), *ST->getRegisterInfo(),
691 *ST->getRegBankInfo());
692 return true;
693}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Promote Memory to Register
Definition Mem2Reg.cpp:110
uint64_t IntrinsicInst * II
static ConstantInt * getConstInt(MDNode *MD, unsigned NumOp)
static SPIRVTypeInst getArgSPIRVType(const Function &F, unsigned ArgIdx, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIRBuilder, const SPIRVSubtarget &ST)
static SPIRV::ExecutionModel::ExecutionModel getExecutionModel(const SPIRVSubtarget &STI, const Function &F)
static uint32_t getFunctionControl(const Function &F, const SPIRVSubtarget *ST)
static SPIRV::AccessQualifier::AccessQualifier getArgAccessQual(const Function &F, unsigned ArgIdx)
static FunctionType * fixFunctionTypeIfPtrArgs(SPIRVGlobalRegistry *GR, const Function &F, FunctionType *FTy, SPIRVTypeInst SRetTy, const SmallVector< SPIRVTypeInst, 4 > &SArgTys)
static std::vector< SPIRV::Decoration::Decoration > getKernelArgTypeQual(const Function &F, unsigned ArgIdx)
#define SPIRV_BACKEND_SERVICE_FUN_NAME
Definition SPIRVUtils.h:540
This file contains some templates that are useful if you are working with the STL at all.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:261
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
CallLowering(const TargetLowering *TLI)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition DataLayout.h:579
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Class to represent function types.
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Type * getParamType(unsigned i) const
Parameter type accessors.
Type * getReturnType() const
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
iterator_range< arg_iterator > args()
Definition Function.h:866
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:758
LLVM_ABI bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition Globals.cpp:346
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1426
ArrayRef< MDOperand > operands() const
Definition Metadata.h:1424
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1432
Tracking metadata reference owned by Metadata.
Definition Metadata.h:891
A single uniqued string.
Definition Metadata.h:722
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:632
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
const TargetInstrInfo & getTII()
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
void setMBB(MachineBasicBlock &MBB)
Set the insertion point to the end of MBB.
MachineInstrBuilder buildTrap(bool Debug=false)
Build and insert G_TRAP or G_DEBUGTRAP.
MachineRegisterInfo * getMRI()
Getter for MRI.
const DataLayout & getDataLayout() const
void setMF(MachineFunction &MF)
void constrainAllUses(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
const MachineInstrBuilder & addUse(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
const MachineInstrBuilder & addDef(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a virtual register definition operand.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineOperand & getOperand(unsigned i) const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
const TargetRegisterClass * getRegClassOrNull(Register Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet.
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition ModRef.h:246
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:249
Metadata wrapper in the Value hierarchy.
Definition Metadata.h:184
Metadata * getMetadata() const
Definition Metadata.h:202
Wrapper class representing virtual and physical registers.
Definition Register.h:20
constexpr bool isValid() const
Definition Register.h:112
bool lowerCall(MachineIRBuilder &MIRBuilder, CallLoweringInfo &Info) const override
This hook must be implemented to lower the given call instruction, including argument and return valu...
bool lowerReturn(MachineIRBuilder &MIRBuiler, const Value *Val, ArrayRef< Register > VRegs, FunctionLoweringInfo &FLI, Register SwiftErrorVReg) const override
This hook must be implemented to lower outgoing return values, described by Val, into the specified v...
SPIRVCallLowering(const SPIRVTargetLowering &TLI, SPIRVGlobalRegistry *GR)
bool lowerFormalArguments(MachineIRBuilder &MIRBuilder, const Function &F, ArrayRef< ArrayRef< Register > > VRegs, FunctionLoweringInfo &FLI) const override
This hook must be implemented to lower the incoming (formal) arguments, described by VRegs,...
void assignSPIRVTypeToVReg(SPIRVTypeInst Type, Register VReg, const MachineFunction &MF)
SPIRVTypeInst getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVTypeInst RetType, const SmallVectorImpl< SPIRVTypeInst > &ArgTypes, MachineIRBuilder &MIRBuilder)
SPIRVTypeInst getOrCreateSPIRVVectorType(SPIRVTypeInst BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
const Type * getTypeForSPIRVType(SPIRVTypeInst Ty) const
SPIRVTypeInst getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
SPIRVTypeInst getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
Type * findDeducedElementType(const Value *Val)
SPIRVEnvType getEnv() const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
const TargetRegisterInfo & getRegisterInfo() const
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
iterator_range< user_iterator > users()
Definition Value.h:426
constexpr bool isZero() const
Definition TypeSize.h:153
@ SPIR_KERNEL
Used for SPIR kernel functions.
std::optional< bool > lowerBuiltin(const StringRef DemangledCall, SPIRV::InstructionSet::InstructionSet Set, MachineIRBuilder &MIRBuilder, const Register OrigRet, const Type *OrigRetTy, const SmallVectorImpl< Register > &Args, SPIRVGlobalRegistry *GR, const CallBase &CB)
FunctionType * getOriginalFunctionType(const Function &F)
This is an optimization pass for GlobalISel generic memory operations.
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:389
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
Register createVirtualRegister(SPIRVTypeInst SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, ArrayRef< uint32_t > DecArgs, StringRef StrImm)
MDString * getOCLKernelArgAccessQual(const Function &F, unsigned ArgIdx)
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
bool isTypedPointerTy(const Type *T)
Definition SPIRVUtils.h:367
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1746
Type * toTypedPointer(Type *Ty)
Definition SPIRVUtils.h:473
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:377
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
void setRegClassType(Register Reg, SPIRVTypeInst SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF, bool Force)
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
std::optional< SPIRV::LinkageType::LinkageType > getSpirvLinkageTypeFor(const SPIRVSubtarget &ST, const GlobalValue &GV)
bool isEntryPoint(const Function &F)
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
MDString * getOCLKernelArgTypeQual(const Function &F, unsigned ArgIdx)
Type * getPointeeTypeByAttr(Argument *Arg)
Definition SPIRVUtils.h:402
bool hasPointeeTypeAttr(Argument *Arg)
Definition SPIRVUtils.h:397
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool isPointerTyOrWrapper(const Type *Ty)
Definition SPIRVUtils.h:425
void addStringImm(const StringRef &Str, MCInst &Inst)
bool isUntypedPointerTy(const Type *T)
Definition SPIRVUtils.h:372