LLVM 20.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"
26#include "llvm/IR/IntrinsicsSPIRV.h"
27#include "llvm/Support/ModRef.h"
28
29using namespace llvm;
30
33 : CallLowering(&TLI), GR(GR) {}
34
36 const Value *Val, ArrayRef<Register> VRegs,
38 Register SwiftErrorVReg) const {
39 // Ignore if called from the internal service function
40 if (MIRBuilder.getMF()
43 .isValid())
44 return true;
45
46 // Maybe run postponed production of types for function pointers
47 if (IndirectCalls.size() > 0) {
48 produceIndirectPtrTypes(MIRBuilder);
49 IndirectCalls.clear();
50 }
51
52 // Currently all return types should use a single register.
53 // TODO: handle the case of multiple registers.
54 if (VRegs.size() > 1)
55 return false;
56 if (Val) {
57 const auto &STI = MIRBuilder.getMF().getSubtarget();
58 return MIRBuilder.buildInstr(SPIRV::OpReturnValue)
59 .addUse(VRegs[0])
60 .constrainAllUses(MIRBuilder.getTII(), *STI.getRegisterInfo(),
61 *STI.getRegBankInfo());
62 }
63 MIRBuilder.buildInstr(SPIRV::OpReturn);
64 return true;
65}
66
67// Based on the LLVM function attributes, get a SPIR-V FunctionControl.
69 const SPIRVSubtarget *ST) {
70 MemoryEffects MemEffects = F.getMemoryEffects();
71
72 uint32_t FuncControl = static_cast<uint32_t>(SPIRV::FunctionControl::None);
73
74 if (F.hasFnAttribute(Attribute::AttrKind::NoInline))
75 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::DontInline);
76 else if (F.hasFnAttribute(Attribute::AttrKind::AlwaysInline))
77 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Inline);
78
79 if (MemEffects.doesNotAccessMemory())
80 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Pure);
81 else if (MemEffects.onlyReadsMemory())
82 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Const);
83
84 if (ST->canUseExtension(SPIRV::Extension::SPV_INTEL_optnone) ||
85 ST->canUseExtension(SPIRV::Extension::SPV_EXT_optnone))
86 if (F.hasFnAttribute(Attribute::OptimizeNone))
87 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::OptNoneEXT);
88
89 return FuncControl;
90}
91
92static ConstantInt *getConstInt(MDNode *MD, unsigned NumOp) {
93 if (MD->getNumOperands() > NumOp) {
94 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(NumOp));
95 if (CMeta)
96 return dyn_cast<ConstantInt>(CMeta->getValue());
97 }
98 return nullptr;
99}
100
101// If the function has pointer arguments, we are forced to re-create this
102// function type from the very beginning, changing PointerType by
103// TypedPointerType for each pointer argument. Otherwise, the same `Type*`
104// potentially corresponds to different SPIR-V function type, effectively
105// invalidating logic behind global registry and duplicates tracker.
106static FunctionType *
108 FunctionType *FTy, const SPIRVType *SRetTy,
109 const SmallVector<SPIRVType *, 4> &SArgTys) {
110 bool hasArgPtrs = false;
111 for (auto &Arg : F.args()) {
112 // check if it's an instance of a non-typed PointerType
113 if (Arg.getType()->isPointerTy()) {
114 hasArgPtrs = true;
115 break;
116 }
117 }
118 if (!hasArgPtrs) {
119 Type *RetTy = FTy->getReturnType();
120 // check if it's an instance of a non-typed PointerType
121 if (!RetTy->isPointerTy())
122 return FTy;
123 }
124
125 // re-create function type, using TypedPointerType instead of PointerType to
126 // properly trace argument types
127 const Type *RetTy = GR->getTypeForSPIRVType(SRetTy);
129 for (auto SArgTy : SArgTys)
130 ArgTys.push_back(const_cast<Type *>(GR->getTypeForSPIRVType(SArgTy)));
131 return FunctionType::get(const_cast<Type *>(RetTy), ArgTys, false);
132}
133
134// This code restores function args/retvalue types for composite cases
135// because the final types should still be aggregate whereas they're i32
136// during the translation to cope with aggregate flattening etc.
138 auto *NamedMD = F.getParent()->getNamedMetadata("spv.cloned_funcs");
139 if (NamedMD == nullptr)
140 return F.getFunctionType();
141
142 Type *RetTy = F.getFunctionType()->getReturnType();
143 SmallVector<Type *, 4> ArgTypes;
144 for (auto &Arg : F.args())
145 ArgTypes.push_back(Arg.getType());
146
147 auto ThisFuncMDIt =
148 std::find_if(NamedMD->op_begin(), NamedMD->op_end(), [&F](MDNode *N) {
149 return isa<MDString>(N->getOperand(0)) &&
150 cast<MDString>(N->getOperand(0))->getString() == F.getName();
151 });
152 // TODO: probably one function can have numerous type mutations,
153 // so we should support this.
154 if (ThisFuncMDIt != NamedMD->op_end()) {
155 auto *ThisFuncMD = *ThisFuncMDIt;
156 MDNode *MD = dyn_cast<MDNode>(ThisFuncMD->getOperand(1));
157 assert(MD && "MDNode operand is expected");
158 ConstantInt *Const = getConstInt(MD, 0);
159 if (Const) {
160 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
161 assert(CMeta && "ConstantAsMetadata operand is expected");
162 assert(Const->getSExtValue() >= -1);
163 // Currently -1 indicates return value, greater values mean
164 // argument numbers.
165 if (Const->getSExtValue() == -1)
166 RetTy = CMeta->getType();
167 else
168 ArgTypes[Const->getSExtValue()] = CMeta->getType();
169 }
170 }
171
172 return FunctionType::get(RetTy, ArgTypes, F.isVarArg());
173}
174
175static SPIRV::AccessQualifier::AccessQualifier
176getArgAccessQual(const Function &F, unsigned ArgIdx) {
177 if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
178 return SPIRV::AccessQualifier::ReadWrite;
179
180 MDString *ArgAttribute = getOCLKernelArgAccessQual(F, ArgIdx);
181 if (!ArgAttribute)
182 return SPIRV::AccessQualifier::ReadWrite;
183
184 if (ArgAttribute->getString() == "read_only")
185 return SPIRV::AccessQualifier::ReadOnly;
186 if (ArgAttribute->getString() == "write_only")
187 return SPIRV::AccessQualifier::WriteOnly;
188 return SPIRV::AccessQualifier::ReadWrite;
189}
190
191static std::vector<SPIRV::Decoration::Decoration>
192getKernelArgTypeQual(const Function &F, unsigned ArgIdx) {
193 MDString *ArgAttribute = getOCLKernelArgTypeQual(F, ArgIdx);
194 if (ArgAttribute && ArgAttribute->getString() == "volatile")
195 return {SPIRV::Decoration::Volatile};
196 return {};
197}
198
199static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
201 MachineIRBuilder &MIRBuilder,
202 const SPIRVSubtarget &ST) {
203 // Read argument's access qualifier from metadata or default.
204 SPIRV::AccessQualifier::AccessQualifier ArgAccessQual =
205 getArgAccessQual(F, ArgIdx);
206
207 Type *OriginalArgType = getOriginalFunctionType(F)->getParamType(ArgIdx);
208
209 // If OriginalArgType is non-pointer, use the OriginalArgType (the type cannot
210 // be legally reassigned later).
211 if (!isPointerTy(OriginalArgType))
212 return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual);
213
214 Argument *Arg = F.getArg(ArgIdx);
215 Type *ArgType = Arg->getType();
216 if (isTypedPointerTy(ArgType)) {
217 SPIRVType *ElementType = GR->getOrCreateSPIRVType(
218 cast<TypedPointerType>(ArgType)->getElementType(), MIRBuilder);
220 ElementType, MIRBuilder,
222 }
223
224 // In case OriginalArgType is of untyped pointer type, there are three
225 // possibilities:
226 // 1) This is a pointer of an LLVM IR element type, passed byval/byref.
227 // 2) This is an OpenCL/SPIR-V builtin type if there is spv_assign_type
228 // intrinsic assigning a TargetExtType.
229 // 3) This is a pointer, try to retrieve pointer element type from a
230 // spv_assign_ptr_type intrinsic or otherwise use default pointer element
231 // type.
232 if (hasPointeeTypeAttr(Arg)) {
233 SPIRVType *ElementType =
234 GR->getOrCreateSPIRVType(getPointeeTypeByAttr(Arg), MIRBuilder);
236 ElementType, MIRBuilder,
238 }
239
240 for (auto User : Arg->users()) {
241 auto *II = dyn_cast<IntrinsicInst>(User);
242 // Check if this is spv_assign_type assigning OpenCL/SPIR-V builtin type.
243 if (II && II->getIntrinsicID() == Intrinsic::spv_assign_type) {
244 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
245 Type *BuiltinType =
246 cast<ConstantAsMetadata>(VMD->getMetadata())->getType();
247 assert(BuiltinType->isTargetExtTy() && "Expected TargetExtType");
248 return GR->getOrCreateSPIRVType(BuiltinType, MIRBuilder, ArgAccessQual);
249 }
250
251 // Check if this is spv_assign_ptr_type assigning pointer element type.
252 if (!II || II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type)
253 continue;
254
255 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
256 Type *ElementTy =
257 toTypedPointer(cast<ConstantAsMetadata>(VMD->getMetadata())->getType());
258 SPIRVType *ElementType = GR->getOrCreateSPIRVType(ElementTy, MIRBuilder);
260 ElementType, MIRBuilder,
262 cast<ConstantInt>(II->getOperand(2))->getZExtValue(), ST));
263 }
264
265 // Replace PointerType with TypedPointerType to be able to map SPIR-V types to
266 // LLVM types in a consistent manner
267 return GR->getOrCreateSPIRVType(toTypedPointer(OriginalArgType), MIRBuilder,
268 ArgAccessQual);
269}
270
271static SPIRV::ExecutionModel::ExecutionModel
273 if (STI.isOpenCLEnv())
274 return SPIRV::ExecutionModel::Kernel;
275
276 auto attribute = F.getFnAttribute("hlsl.shader");
277 if (!attribute.isValid()) {
279 "This entry point lacks mandatory hlsl.shader attribute.");
280 }
281
282 const auto value = attribute.getValueAsString();
283 if (value == "compute")
284 return SPIRV::ExecutionModel::GLCompute;
285
286 report_fatal_error("This HLSL entry point is not supported by this backend.");
287}
288
290 const Function &F,
292 FunctionLoweringInfo &FLI) const {
293 // Discard the internal service function
294 if (F.getFnAttribute(SPIRV_BACKEND_SERVICE_FUN_NAME).isValid())
295 return true;
296
297 assert(GR && "Must initialize the SPIRV type registry before lowering args.");
298 GR->setCurrentFunc(MIRBuilder.getMF());
299
300 // Get access to information about available extensions
301 const SPIRVSubtarget *ST =
302 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
303
304 // Assign types and names to all args, and store their types for later.
305 SmallVector<SPIRVType *, 4> ArgTypeVRegs;
306 if (VRegs.size() > 0) {
307 unsigned i = 0;
308 for (const auto &Arg : F.args()) {
309 // Currently formal args should use single registers.
310 // TODO: handle the case of multiple registers.
311 if (VRegs[i].size() > 1)
312 return false;
313 auto *SpirvTy = getArgSPIRVType(F, i, GR, MIRBuilder, *ST);
314 GR->assignSPIRVTypeToVReg(SpirvTy, VRegs[i][0], MIRBuilder.getMF());
315 ArgTypeVRegs.push_back(SpirvTy);
316
317 if (Arg.hasName())
318 buildOpName(VRegs[i][0], Arg.getName(), MIRBuilder);
319 if (isPointerTyOrWrapper(Arg.getType())) {
320 auto DerefBytes = static_cast<unsigned>(Arg.getDereferenceableBytes());
321 if (DerefBytes != 0)
322 buildOpDecorate(VRegs[i][0], MIRBuilder,
323 SPIRV::Decoration::MaxByteOffset, {DerefBytes});
324 }
325 if (Arg.hasAttribute(Attribute::Alignment)) {
326 auto Alignment = static_cast<unsigned>(
327 Arg.getAttribute(Attribute::Alignment).getValueAsInt());
328 buildOpDecorate(VRegs[i][0], MIRBuilder, SPIRV::Decoration::Alignment,
329 {Alignment});
330 }
331 if (Arg.hasAttribute(Attribute::ReadOnly)) {
332 auto Attr =
333 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoWrite);
334 buildOpDecorate(VRegs[i][0], MIRBuilder,
335 SPIRV::Decoration::FuncParamAttr, {Attr});
336 }
337 if (Arg.hasAttribute(Attribute::ZExt)) {
338 auto Attr =
339 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Zext);
340 buildOpDecorate(VRegs[i][0], MIRBuilder,
341 SPIRV::Decoration::FuncParamAttr, {Attr});
342 }
343 if (Arg.hasAttribute(Attribute::NoAlias)) {
344 auto Attr =
345 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoAlias);
346 buildOpDecorate(VRegs[i][0], MIRBuilder,
347 SPIRV::Decoration::FuncParamAttr, {Attr});
348 }
349 if (Arg.hasAttribute(Attribute::ByVal)) {
350 auto Attr =
351 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::ByVal);
352 buildOpDecorate(VRegs[i][0], MIRBuilder,
353 SPIRV::Decoration::FuncParamAttr, {Attr});
354 }
355 if (Arg.hasAttribute(Attribute::StructRet)) {
356 auto Attr =
357 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Sret);
358 buildOpDecorate(VRegs[i][0], MIRBuilder,
359 SPIRV::Decoration::FuncParamAttr, {Attr});
360 }
361
362 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
363 std::vector<SPIRV::Decoration::Decoration> ArgTypeQualDecs =
365 for (SPIRV::Decoration::Decoration Decoration : ArgTypeQualDecs)
366 buildOpDecorate(VRegs[i][0], MIRBuilder, Decoration, {});
367 }
368
369 MDNode *Node = F.getMetadata("spirv.ParameterDecorations");
370 if (Node && i < Node->getNumOperands() &&
371 isa<MDNode>(Node->getOperand(i))) {
372 MDNode *MD = cast<MDNode>(Node->getOperand(i));
373 for (const MDOperand &MDOp : MD->operands()) {
374 MDNode *MD2 = dyn_cast<MDNode>(MDOp);
375 assert(MD2 && "Metadata operand is expected");
376 ConstantInt *Const = getConstInt(MD2, 0);
377 assert(Const && "MDOperand should be ConstantInt");
378 auto Dec =
379 static_cast<SPIRV::Decoration::Decoration>(Const->getZExtValue());
380 std::vector<uint32_t> DecVec;
381 for (unsigned j = 1; j < MD2->getNumOperands(); j++) {
382 ConstantInt *Const = getConstInt(MD2, j);
383 assert(Const && "MDOperand should be ConstantInt");
384 DecVec.push_back(static_cast<uint32_t>(Const->getZExtValue()));
385 }
386 buildOpDecorate(VRegs[i][0], MIRBuilder, Dec, DecVec);
387 }
388 }
389 ++i;
390 }
391 }
392
393 auto MRI = MIRBuilder.getMRI();
394 Register FuncVReg = MRI->createGenericVirtualRegister(LLT::scalar(64));
395 MRI->setRegClass(FuncVReg, &SPIRV::iIDRegClass);
396 if (F.isDeclaration())
397 GR->add(&F, &MIRBuilder.getMF(), FuncVReg);
399 Type *FRetTy = FTy->getReturnType();
400 if (isUntypedPointerTy(FRetTy)) {
401 if (Type *FRetElemTy = GR->findDeducedElementType(&F)) {
403 toTypedPointer(FRetElemTy), getPointerAddressSpace(FRetTy));
404 GR->addReturnType(&F, DerivedTy);
405 FRetTy = DerivedTy;
406 }
407 }
408 SPIRVType *RetTy = GR->getOrCreateSPIRVType(FRetTy, MIRBuilder);
409 FTy = fixFunctionTypeIfPtrArgs(GR, F, FTy, RetTy, ArgTypeVRegs);
411 FTy, RetTy, ArgTypeVRegs, MIRBuilder);
412 uint32_t FuncControl = getFunctionControl(F, ST);
413
414 // Add OpFunction instruction
415 MachineInstrBuilder MB = MIRBuilder.buildInstr(SPIRV::OpFunction)
416 .addDef(FuncVReg)
418 .addImm(FuncControl)
419 .addUse(GR->getSPIRVTypeID(FuncTy));
421
422 // Add OpFunctionParameter instructions
423 int i = 0;
424 for (const auto &Arg : F.args()) {
425 assert(VRegs[i].size() == 1 && "Formal arg has multiple vregs");
426 Register ArgReg = VRegs[i][0];
427 MRI->setRegClass(ArgReg, GR->getRegClass(ArgTypeVRegs[i]));
428 MRI->setType(ArgReg, GR->getRegType(ArgTypeVRegs[i]));
429 MIRBuilder.buildInstr(SPIRV::OpFunctionParameter)
430 .addDef(ArgReg)
431 .addUse(GR->getSPIRVTypeID(ArgTypeVRegs[i]));
432 if (F.isDeclaration())
433 GR->add(&Arg, &MIRBuilder.getMF(), ArgReg);
434 i++;
435 }
436 // Name the function.
437 if (F.hasName())
438 buildOpName(FuncVReg, F.getName(), MIRBuilder);
439
440 // Handle entry points and function linkage.
441 if (isEntryPoint(F)) {
442 auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint)
443 .addImm(static_cast<uint32_t>(getExecutionModel(*ST, F)))
444 .addUse(FuncVReg);
445 addStringImm(F.getName(), MIB);
446 } else if (F.getLinkage() != GlobalValue::InternalLinkage &&
447 F.getLinkage() != GlobalValue::PrivateLinkage) {
448 SPIRV::LinkageType::LinkageType LnkTy =
449 F.isDeclaration()
450 ? SPIRV::LinkageType::Import
451 : (F.getLinkage() == GlobalValue::LinkOnceODRLinkage &&
452 ST->canUseExtension(
453 SPIRV::Extension::SPV_KHR_linkonce_odr)
454 ? SPIRV::LinkageType::LinkOnceODR
455 : SPIRV::LinkageType::Export);
456 buildOpDecorate(FuncVReg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
457 {static_cast<uint32_t>(LnkTy)}, F.getGlobalIdentifier());
458 }
459
460 // Handle function pointers decoration
461 bool hasFunctionPointers =
462 ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
463 if (hasFunctionPointers) {
464 if (F.hasFnAttribute("referenced-indirectly")) {
465 assert((F.getCallingConv() != CallingConv::SPIR_KERNEL) &&
466 "Unexpected 'referenced-indirectly' attribute of the kernel "
467 "function");
468 buildOpDecorate(FuncVReg, MIRBuilder,
469 SPIRV::Decoration::ReferencedIndirectlyINTEL, {});
470 }
471 }
472
473 return true;
474}
475
476// Used to postpone producing of indirect function pointer types after all
477// indirect calls info is collected
478// TODO:
479// - add a topological sort of IndirectCalls to ensure the best types knowledge
480// - we may need to fix function formal parameter types if they are opaque
481// pointers used as function pointers in these indirect calls
482void SPIRVCallLowering::produceIndirectPtrTypes(
483 MachineIRBuilder &MIRBuilder) const {
484 // Create indirect call data types if any
485 MachineFunction &MF = MIRBuilder.getMF();
486 for (auto const &IC : IndirectCalls) {
487 SPIRVType *SpirvRetTy = GR->getOrCreateSPIRVType(IC.RetTy, MIRBuilder);
488 SmallVector<SPIRVType *, 4> SpirvArgTypes;
489 for (size_t i = 0; i < IC.ArgTys.size(); ++i) {
490 SPIRVType *SPIRVTy = GR->getOrCreateSPIRVType(IC.ArgTys[i], MIRBuilder);
491 SpirvArgTypes.push_back(SPIRVTy);
492 if (!GR->getSPIRVTypeForVReg(IC.ArgRegs[i]))
493 GR->assignSPIRVTypeToVReg(SPIRVTy, IC.ArgRegs[i], MF);
494 }
495 // SPIR-V function type:
496 FunctionType *FTy =
497 FunctionType::get(const_cast<Type *>(IC.RetTy), IC.ArgTys, false);
499 FTy, SpirvRetTy, SpirvArgTypes, MIRBuilder);
500 // SPIR-V pointer to function type:
501 SPIRVType *IndirectFuncPtrTy = GR->getOrCreateSPIRVPointerType(
502 SpirvFuncTy, MIRBuilder, SPIRV::StorageClass::Function);
503 // Correct the Callee type
504 GR->assignSPIRVTypeToVReg(IndirectFuncPtrTy, IC.Callee, MF);
505 }
506}
507
509 CallLoweringInfo &Info) const {
510 // Currently call returns should have single vregs.
511 // TODO: handle the case of multiple registers.
512 if (Info.OrigRet.Regs.size() > 1)
513 return false;
514 MachineFunction &MF = MIRBuilder.getMF();
515 GR->setCurrentFunc(MF);
516 const Function *CF = nullptr;
517 std::string DemangledName;
518 const Type *OrigRetTy = Info.OrigRet.Ty;
519
520 // Emit a regular OpFunctionCall. If it's an externally declared function,
521 // be sure to emit its type and function declaration here. It will be hoisted
522 // globally later.
523 if (Info.Callee.isGlobal()) {
524 std::string FuncName = Info.Callee.getGlobal()->getName().str();
525 DemangledName = getOclOrSpirvBuiltinDemangledName(FuncName);
526 CF = dyn_cast_or_null<const Function>(Info.Callee.getGlobal());
527 // TODO: support constexpr casts and indirect calls.
528 if (CF == nullptr)
529 return false;
530 if (FunctionType *FTy = getOriginalFunctionType(*CF)) {
531 OrigRetTy = FTy->getReturnType();
532 if (isUntypedPointerTy(OrigRetTy)) {
533 if (auto *DerivedRetTy = GR->findReturnType(CF))
534 OrigRetTy = DerivedRetTy;
535 }
536 }
537 }
538
539 MachineRegisterInfo *MRI = MIRBuilder.getMRI();
540 Register ResVReg =
541 Info.OrigRet.Regs.empty() ? Register(0) : Info.OrigRet.Regs[0];
542 const auto *ST = static_cast<const SPIRVSubtarget *>(&MF.getSubtarget());
543
544 bool isFunctionDecl = CF && CF->isDeclaration();
545 bool canUseOpenCL = ST->canUseExtInstSet(SPIRV::InstructionSet::OpenCL_std);
546 bool canUseGLSL = ST->canUseExtInstSet(SPIRV::InstructionSet::GLSL_std_450);
547 assert(canUseGLSL != canUseOpenCL &&
548 "Scenario where both sets are enabled is not supported.");
549
550 if (isFunctionDecl && !DemangledName.empty() &&
551 (canUseGLSL || canUseOpenCL)) {
552 if (ResVReg.isValid()) {
553 if (!GR->getSPIRVTypeForVReg(ResVReg)) {
554 const Type *RetTy = OrigRetTy;
555 if (auto *PtrRetTy = dyn_cast<PointerType>(OrigRetTy)) {
556 const Value *OrigValue = Info.OrigRet.OrigValue;
557 if (!OrigValue)
558 OrigValue = Info.CB;
559 if (OrigValue)
560 if (Type *ElemTy = GR->findDeducedElementType(OrigValue))
561 RetTy =
562 TypedPointerType::get(ElemTy, PtrRetTy->getAddressSpace());
563 }
564 setRegClassType(ResVReg, RetTy, GR, MIRBuilder);
565 }
566 } else {
567 ResVReg = createVirtualRegister(OrigRetTy, GR, MIRBuilder);
568 }
570 for (auto Arg : Info.OrigArgs) {
571 assert(Arg.Regs.size() == 1 && "Call arg has multiple VRegs");
572 Register ArgReg = Arg.Regs[0];
573 ArgVRegs.push_back(ArgReg);
574 SPIRVType *SpvType = GR->getSPIRVTypeForVReg(ArgReg);
575 if (!SpvType) {
576 Type *ArgTy = nullptr;
577 if (auto *PtrArgTy = dyn_cast<PointerType>(Arg.Ty)) {
578 // If Arg.Ty is an untyped pointer (i.e., ptr [addrspace(...)]) and we
579 // don't have access to original value in LLVM IR or info about
580 // deduced pointee type, then we should wait with setting the type for
581 // the virtual register until pre-legalizer step when we access
582 // @llvm.spv.assign.ptr.type.p...(...)'s info.
583 if (Arg.OrigValue)
584 if (Type *ElemTy = GR->findDeducedElementType(Arg.OrigValue))
585 ArgTy =
586 TypedPointerType::get(ElemTy, PtrArgTy->getAddressSpace());
587 } else {
588 ArgTy = Arg.Ty;
589 }
590 if (ArgTy) {
591 SpvType = GR->getOrCreateSPIRVType(ArgTy, MIRBuilder);
592 GR->assignSPIRVTypeToVReg(SpvType, ArgReg, MF);
593 }
594 }
595 if (!MRI->getRegClassOrNull(ArgReg)) {
596 // Either we have SpvType created, or Arg.Ty is an untyped pointer and
597 // we know its virtual register's class and type even if we don't know
598 // pointee type.
599 MRI->setRegClass(ArgReg, SpvType ? GR->getRegClass(SpvType)
600 : &SPIRV::pIDRegClass);
601 MRI->setType(
602 ArgReg,
603 SpvType ? GR->getRegType(SpvType)
604 : LLT::pointer(cast<PointerType>(Arg.Ty)->getAddressSpace(),
605 GR->getPointerSize()));
606 }
607 }
608 auto instructionSet = canUseOpenCL ? SPIRV::InstructionSet::OpenCL_std
609 : SPIRV::InstructionSet::GLSL_std_450;
610 if (auto Res =
611 SPIRV::lowerBuiltin(DemangledName, instructionSet, MIRBuilder,
612 ResVReg, OrigRetTy, ArgVRegs, GR))
613 return *Res;
614 }
615
616 if (isFunctionDecl && !GR->find(CF, &MF).isValid()) {
617 // Emit the type info and forward function declaration to the first MBB
618 // to ensure VReg definition dependencies are valid across all MBBs.
619 MachineIRBuilder FirstBlockBuilder;
620 FirstBlockBuilder.setMF(MF);
621 FirstBlockBuilder.setMBB(*MF.getBlockNumbered(0));
622
625 for (const Argument &Arg : CF->args()) {
626 if (MIRBuilder.getDataLayout().getTypeStoreSize(Arg.getType()).isZero())
627 continue; // Don't handle zero sized types.
628 Register Reg = MRI->createGenericVirtualRegister(LLT::scalar(64));
629 MRI->setRegClass(Reg, &SPIRV::iIDRegClass);
630 ToInsert.push_back({Reg});
631 VRegArgs.push_back(ToInsert.back());
632 }
633 // TODO: Reuse FunctionLoweringInfo
634 FunctionLoweringInfo FuncInfo;
635 lowerFormalArguments(FirstBlockBuilder, *CF, VRegArgs, FuncInfo);
636 }
637
638 // Ignore the call if it's called from the internal service function
639 if (MIRBuilder.getMF()
640 .getFunction()
642 .isValid()) {
643 // insert a no-op
644 MIRBuilder.buildTrap();
645 return true;
646 }
647
648 unsigned CallOp;
649 if (Info.CB->isIndirectCall()) {
650 if (!ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers))
651 report_fatal_error("An indirect call is encountered but SPIR-V without "
652 "extensions does not support it",
653 false);
654 // Set instruction operation according to SPV_INTEL_function_pointers
655 CallOp = SPIRV::OpFunctionPointerCallINTEL;
656 // Collect information about the indirect call to support possible
657 // specification of opaque ptr types of parent function's parameters
658 Register CalleeReg = Info.Callee.getReg();
659 if (CalleeReg.isValid()) {
660 SPIRVCallLowering::SPIRVIndirectCall IndirectCall;
661 IndirectCall.Callee = CalleeReg;
662 IndirectCall.RetTy = OrigRetTy;
663 for (const auto &Arg : Info.OrigArgs) {
664 assert(Arg.Regs.size() == 1 && "Call arg has multiple VRegs");
665 IndirectCall.ArgTys.push_back(Arg.Ty);
666 IndirectCall.ArgRegs.push_back(Arg.Regs[0]);
667 }
668 IndirectCalls.push_back(IndirectCall);
669 }
670 } else {
671 // Emit a regular OpFunctionCall
672 CallOp = SPIRV::OpFunctionCall;
673 }
674
675 // Make sure there's a valid return reg, even for functions returning void.
676 if (!ResVReg.isValid())
677 ResVReg = MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::iIDRegClass);
678 SPIRVType *RetType = GR->assignTypeToVReg(OrigRetTy, ResVReg, MIRBuilder);
679
680 // Emit the call instruction and its args.
681 auto MIB = MIRBuilder.buildInstr(CallOp)
682 .addDef(ResVReg)
683 .addUse(GR->getSPIRVTypeID(RetType))
684 .add(Info.Callee);
685
686 for (const auto &Arg : Info.OrigArgs) {
687 // Currently call args should have single vregs.
688 if (Arg.Regs.size() > 1)
689 return false;
690 MIB.addUse(Arg.Regs[0]);
691 }
692 return MIB.constrainAllUses(MIRBuilder.getTII(), *ST->getRegisterInfo(),
693 *ST->getRegBankInfo());
694}
unsigned const MachineRegisterInfo * MRI
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
return RetTy
Given that RA is a live value
#define F(x, y, z)
Definition: MD5.cpp:55
uint64_t IntrinsicInst * II
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static ConstantInt * getConstInt(MDNode *MD, unsigned NumOp)
static SPIRV::ExecutionModel::ExecutionModel getExecutionModel(const SPIRVSubtarget &STI, const Function &F)
static FunctionType * getOriginalFunctionType(const Function &F)
static uint32_t getFunctionControl(const Function &F, const SPIRVSubtarget *ST)
static SPIRV::AccessQualifier::AccessQualifier getArgAccessQual(const Function &F, unsigned ArgIdx)
static SPIRVType * getArgSPIRVType(const Function &F, unsigned ArgIdx, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIRBuilder, const SPIRVSubtarget &ST)
static FunctionType * fixFunctionTypeIfPtrArgs(SPIRVGlobalRegistry *GR, const Function &F, FunctionType *FTy, const SPIRVType *SRetTy, const SmallVector< SPIRVType *, 4 > &SArgTys)
static std::vector< SPIRV::Decoration::Decoration > getKernelArgTypeQual(const Function &F, unsigned ArgIdx)
#define SPIRV_BACKEND_SERVICE_FUN_NAME
Definition: SPIRVUtils.h:382
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition: DataLayout.h:421
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Class to represent function types.
Definition: DerivedTypes.h:105
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:137
Type * getReturnType() const
Definition: DerivedTypes.h:126
static 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:892
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.cpp:766
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:296
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:60
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:55
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
Definition: LowLevelType.h:42
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
Definition: LowLevelType.h:57
Metadata node.
Definition: Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1430
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1428
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1436
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:891
A single uniqued string.
Definition: Metadata.h:720
StringRef getString() const
Definition: Metadata.cpp:616
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)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
bool constrainAllUses(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
MachineInstr * getInstr() const
If conversion operators fail, use this method to get the MachineInstr explicitly.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:585
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
bool doesNotAccessMemory() const
Whether this function accesses no memory.
Definition: ModRef.h:192
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition: ModRef.h:195
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:176
Metadata * getMetadata() const
Definition: Metadata.h:193
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
constexpr bool isValid() const
Definition: Register.h:116
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 recordFunctionDefinition(const Function *F, const MachineOperand *MO)
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
const TypedPointerType * findReturnType(const Function *ArgF)
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, const MachineFunction &MF)
void add(const Constant *C, MachineFunction *MF, Register R)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
void addReturnType(const Function *ArgF, TypedPointerType *DerivedTy)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
SPIRVType * assignTypeToVReg(const Type *Type, Register VReg, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
SPIRVType * getOrCreateOpTypeFunctionWithArgs(const Type *Ty, SPIRVType *RetType, const SmallVectorImpl< SPIRVType * > &ArgTypes, MachineIRBuilder &MIRBuilder)
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
MachineFunction * setCurrentFunc(MachineFunction &MF)
Register find(const MachineInstr *MI, MachineFunction *MF)
SPIRVType * getOrCreateSPIRVPointerType(SPIRVType *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SClass=SPIRV::StorageClass::Function)
LLT getRegType(SPIRVType *SpvType) const
Type * findDeducedElementType(const Value *Val)
bool isOpenCLEnv() const
size_t size() const
Definition: SmallVector.h:78
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
A few GPU targets, such as DXIL and SPIR-V, have typed pointers.
static 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:74
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:421
constexpr bool isZero() const
Definition: TypeSize.h:156
@ SPIR_KERNEL
Used for SPIR kernel functions.
Definition: CallingConv.h:144
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)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
Definition: SPIRVUtils.cpp:103
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:1697
unsigned getPointerAddressSpace(const Type *T)
Definition: SPIRVUtils.h:256
MDString * getOCLKernelArgAccessQual(const Function &F, unsigned ArgIdx)
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
Definition: SPIRVUtils.cpp:392
bool isTypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:240
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Definition: SPIRVUtils.cpp:130
Register createVirtualRegister(SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
Definition: SPIRVUtils.cpp:748
Type * toTypedPointer(Type *Ty)
Definition: SPIRVUtils.h:347
void setRegClassType(Register Reg, SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF, bool Force)
Definition: SPIRVUtils.cpp:727
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:250
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:167
bool isEntryPoint(const Function &F)
Definition: SPIRVUtils.cpp:445
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
Definition: SPIRVUtils.cpp:211
MDString * getOCLKernelArgTypeQual(const Function &F, unsigned ArgIdx)
Type * getPointeeTypeByAttr(Argument *Arg)
Definition: SPIRVUtils.h:269
bool hasPointeeTypeAttr(Argument *Arg)
Definition: SPIRVUtils.h:264
bool isPointerTyOrWrapper(const Type *Ty)
Definition: SPIRVUtils.h:299
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:54
bool isUntypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:245
#define N