LLVM 19.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 // Maybe run postponed production of types for function pointers
40 if (IndirectCalls.size() > 0) {
41 produceIndirectPtrTypes(MIRBuilder);
42 IndirectCalls.clear();
43 }
44
45 // Currently all return types should use a single register.
46 // TODO: handle the case of multiple registers.
47 if (VRegs.size() > 1)
48 return false;
49 if (Val) {
50 const auto &STI = MIRBuilder.getMF().getSubtarget();
51 return MIRBuilder.buildInstr(SPIRV::OpReturnValue)
52 .addUse(VRegs[0])
53 .constrainAllUses(MIRBuilder.getTII(), *STI.getRegisterInfo(),
54 *STI.getRegBankInfo());
55 }
56 MIRBuilder.buildInstr(SPIRV::OpReturn);
57 return true;
58}
59
60// Based on the LLVM function attributes, get a SPIR-V FunctionControl.
62 MemoryEffects MemEffects = F.getMemoryEffects();
63
64 uint32_t FuncControl = static_cast<uint32_t>(SPIRV::FunctionControl::None);
65
66 if (F.hasFnAttribute(Attribute::AttrKind::NoInline))
67 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::DontInline);
68 else if (F.hasFnAttribute(Attribute::AttrKind::AlwaysInline))
69 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Inline);
70
71 if (MemEffects.doesNotAccessMemory())
72 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Pure);
73 else if (MemEffects.onlyReadsMemory())
74 FuncControl |= static_cast<uint32_t>(SPIRV::FunctionControl::Const);
75
76 return FuncControl;
77}
78
79static ConstantInt *getConstInt(MDNode *MD, unsigned NumOp) {
80 if (MD->getNumOperands() > NumOp) {
81 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(NumOp));
82 if (CMeta)
83 return dyn_cast<ConstantInt>(CMeta->getValue());
84 }
85 return nullptr;
86}
87
88// If the function has pointer arguments, we are forced to re-create this
89// function type from the very beginning, changing PointerType by
90// TypedPointerType for each pointer argument. Otherwise, the same `Type*`
91// potentially corresponds to different SPIR-V function type, effectively
92// invalidating logic behind global registry and duplicates tracker.
93static FunctionType *
95 FunctionType *FTy, const SPIRVType *SRetTy,
96 const SmallVector<SPIRVType *, 4> &SArgTys) {
97 if (F.getParent()->getNamedMetadata("spv.cloned_funcs"))
98 return FTy;
99
100 bool hasArgPtrs = false;
101 for (auto &Arg : F.args()) {
102 // check if it's an instance of a non-typed PointerType
103 if (Arg.getType()->isPointerTy()) {
104 hasArgPtrs = true;
105 break;
106 }
107 }
108 if (!hasArgPtrs) {
109 Type *RetTy = FTy->getReturnType();
110 // check if it's an instance of a non-typed PointerType
111 if (!RetTy->isPointerTy())
112 return FTy;
113 }
114
115 // re-create function type, using TypedPointerType instead of PointerType to
116 // properly trace argument types
117 const Type *RetTy = GR->getTypeForSPIRVType(SRetTy);
119 for (auto SArgTy : SArgTys)
120 ArgTys.push_back(const_cast<Type *>(GR->getTypeForSPIRVType(SArgTy)));
121 return FunctionType::get(const_cast<Type *>(RetTy), ArgTys, false);
122}
123
124// This code restores function args/retvalue types for composite cases
125// because the final types should still be aggregate whereas they're i32
126// during the translation to cope with aggregate flattening etc.
128 auto *NamedMD = F.getParent()->getNamedMetadata("spv.cloned_funcs");
129 if (NamedMD == nullptr)
130 return F.getFunctionType();
131
132 Type *RetTy = F.getFunctionType()->getReturnType();
133 SmallVector<Type *, 4> ArgTypes;
134 for (auto &Arg : F.args())
135 ArgTypes.push_back(Arg.getType());
136
137 auto ThisFuncMDIt =
138 std::find_if(NamedMD->op_begin(), NamedMD->op_end(), [&F](MDNode *N) {
139 return isa<MDString>(N->getOperand(0)) &&
140 cast<MDString>(N->getOperand(0))->getString() == F.getName();
141 });
142 // TODO: probably one function can have numerous type mutations,
143 // so we should support this.
144 if (ThisFuncMDIt != NamedMD->op_end()) {
145 auto *ThisFuncMD = *ThisFuncMDIt;
146 MDNode *MD = dyn_cast<MDNode>(ThisFuncMD->getOperand(1));
147 assert(MD && "MDNode operand is expected");
148 ConstantInt *Const = getConstInt(MD, 0);
149 if (Const) {
150 auto *CMeta = dyn_cast<ConstantAsMetadata>(MD->getOperand(1));
151 assert(CMeta && "ConstantAsMetadata operand is expected");
152 assert(Const->getSExtValue() >= -1);
153 // Currently -1 indicates return value, greater values mean
154 // argument numbers.
155 if (Const->getSExtValue() == -1)
156 RetTy = CMeta->getType();
157 else
158 ArgTypes[Const->getSExtValue()] = CMeta->getType();
159 }
160 }
161
162 return FunctionType::get(RetTy, ArgTypes, F.isVarArg());
163}
164
165static SPIRV::AccessQualifier::AccessQualifier
166getArgAccessQual(const Function &F, unsigned ArgIdx) {
167 if (F.getCallingConv() != CallingConv::SPIR_KERNEL)
168 return SPIRV::AccessQualifier::ReadWrite;
169
170 MDString *ArgAttribute = getOCLKernelArgAccessQual(F, ArgIdx);
171 if (!ArgAttribute)
172 return SPIRV::AccessQualifier::ReadWrite;
173
174 if (ArgAttribute->getString().compare("read_only") == 0)
175 return SPIRV::AccessQualifier::ReadOnly;
176 if (ArgAttribute->getString().compare("write_only") == 0)
177 return SPIRV::AccessQualifier::WriteOnly;
178 return SPIRV::AccessQualifier::ReadWrite;
179}
180
181static std::vector<SPIRV::Decoration::Decoration>
182getKernelArgTypeQual(const Function &F, unsigned ArgIdx) {
183 MDString *ArgAttribute = getOCLKernelArgTypeQual(F, ArgIdx);
184 if (ArgAttribute && ArgAttribute->getString().compare("volatile") == 0)
185 return {SPIRV::Decoration::Volatile};
186 return {};
187}
188
189static SPIRVType *getArgSPIRVType(const Function &F, unsigned ArgIdx,
191 MachineIRBuilder &MIRBuilder,
192 const SPIRVSubtarget &ST) {
193 // Read argument's access qualifier from metadata or default.
194 SPIRV::AccessQualifier::AccessQualifier ArgAccessQual =
195 getArgAccessQual(F, ArgIdx);
196
197 Type *OriginalArgType = getOriginalFunctionType(F)->getParamType(ArgIdx);
198
199 // If OriginalArgType is non-pointer, use the OriginalArgType (the type cannot
200 // be legally reassigned later).
201 if (!isPointerTy(OriginalArgType))
202 return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual);
203
204 Argument *Arg = F.getArg(ArgIdx);
205 Type *ArgType = Arg->getType();
206 if (isTypedPointerTy(ArgType)) {
207 SPIRVType *ElementType = GR->getOrCreateSPIRVType(
208 cast<TypedPointerType>(ArgType)->getElementType(), MIRBuilder);
210 ElementType, MIRBuilder,
212 }
213
214 // In case OriginalArgType is of untyped pointer type, there are three
215 // possibilities:
216 // 1) This is a pointer of an LLVM IR element type, passed byval/byref.
217 // 2) This is an OpenCL/SPIR-V builtin type if there is spv_assign_type
218 // intrinsic assigning a TargetExtType.
219 // 3) This is a pointer, try to retrieve pointer element type from a
220 // spv_assign_ptr_type intrinsic or otherwise use default pointer element
221 // type.
222 if (hasPointeeTypeAttr(Arg)) {
223 SPIRVType *ElementType =
224 GR->getOrCreateSPIRVType(getPointeeTypeByAttr(Arg), MIRBuilder);
226 ElementType, MIRBuilder,
228 }
229
230 for (auto User : Arg->users()) {
231 auto *II = dyn_cast<IntrinsicInst>(User);
232 // Check if this is spv_assign_type assigning OpenCL/SPIR-V builtin type.
233 if (II && II->getIntrinsicID() == Intrinsic::spv_assign_type) {
234 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
235 Type *BuiltinType =
236 cast<ConstantAsMetadata>(VMD->getMetadata())->getType();
237 assert(BuiltinType->isTargetExtTy() && "Expected TargetExtType");
238 return GR->getOrCreateSPIRVType(BuiltinType, MIRBuilder, ArgAccessQual);
239 }
240
241 // Check if this is spv_assign_ptr_type assigning pointer element type.
242 if (!II || II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type)
243 continue;
244
245 MetadataAsValue *VMD = cast<MetadataAsValue>(II->getOperand(1));
246 Type *ElementTy = cast<ConstantAsMetadata>(VMD->getMetadata())->getType();
247 if (isUntypedPointerTy(ElementTy))
248 ElementTy =
250 getPointerAddressSpace(ElementTy));
251 SPIRVType *ElementType = GR->getOrCreateSPIRVType(ElementTy, MIRBuilder);
253 ElementType, MIRBuilder,
255 cast<ConstantInt>(II->getOperand(2))->getZExtValue(), ST));
256 }
257
258 // Replace PointerType with TypedPointerType to be able to map SPIR-V types to
259 // LLVM types in a consistent manner
260 if (isUntypedPointerTy(OriginalArgType)) {
261 OriginalArgType =
263 getPointerAddressSpace(OriginalArgType));
264 }
265 return GR->getOrCreateSPIRVType(OriginalArgType, MIRBuilder, ArgAccessQual);
266}
267
268static SPIRV::ExecutionModel::ExecutionModel
270 if (STI.isOpenCLEnv())
271 return SPIRV::ExecutionModel::Kernel;
272
273 auto attribute = F.getFnAttribute("hlsl.shader");
274 if (!attribute.isValid()) {
276 "This entry point lacks mandatory hlsl.shader attribute.");
277 }
278
279 const auto value = attribute.getValueAsString();
280 if (value == "compute")
281 return SPIRV::ExecutionModel::GLCompute;
282
283 report_fatal_error("This HLSL entry point is not supported by this backend.");
284}
285
287 const Function &F,
289 FunctionLoweringInfo &FLI) const {
290 assert(GR && "Must initialize the SPIRV type registry before lowering args.");
291 GR->setCurrentFunc(MIRBuilder.getMF());
292
293 // Get access to information about available extensions
294 const SPIRVSubtarget *ST =
295 static_cast<const SPIRVSubtarget *>(&MIRBuilder.getMF().getSubtarget());
296
297 // Assign types and names to all args, and store their types for later.
298 SmallVector<SPIRVType *, 4> ArgTypeVRegs;
299 if (VRegs.size() > 0) {
300 unsigned i = 0;
301 for (const auto &Arg : F.args()) {
302 // Currently formal args should use single registers.
303 // TODO: handle the case of multiple registers.
304 if (VRegs[i].size() > 1)
305 return false;
306 auto *SpirvTy = getArgSPIRVType(F, i, GR, MIRBuilder, *ST);
307 GR->assignSPIRVTypeToVReg(SpirvTy, VRegs[i][0], MIRBuilder.getMF());
308 ArgTypeVRegs.push_back(SpirvTy);
309
310 if (Arg.hasName())
311 buildOpName(VRegs[i][0], Arg.getName(), MIRBuilder);
312 if (isPointerTy(Arg.getType())) {
313 auto DerefBytes = static_cast<unsigned>(Arg.getDereferenceableBytes());
314 if (DerefBytes != 0)
315 buildOpDecorate(VRegs[i][0], MIRBuilder,
316 SPIRV::Decoration::MaxByteOffset, {DerefBytes});
317 }
318 if (Arg.hasAttribute(Attribute::Alignment)) {
319 auto Alignment = static_cast<unsigned>(
320 Arg.getAttribute(Attribute::Alignment).getValueAsInt());
321 buildOpDecorate(VRegs[i][0], MIRBuilder, SPIRV::Decoration::Alignment,
322 {Alignment});
323 }
324 if (Arg.hasAttribute(Attribute::ReadOnly)) {
325 auto Attr =
326 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoWrite);
327 buildOpDecorate(VRegs[i][0], MIRBuilder,
328 SPIRV::Decoration::FuncParamAttr, {Attr});
329 }
330 if (Arg.hasAttribute(Attribute::ZExt)) {
331 auto Attr =
332 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::Zext);
333 buildOpDecorate(VRegs[i][0], MIRBuilder,
334 SPIRV::Decoration::FuncParamAttr, {Attr});
335 }
336 if (Arg.hasAttribute(Attribute::NoAlias)) {
337 auto Attr =
338 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::NoAlias);
339 buildOpDecorate(VRegs[i][0], MIRBuilder,
340 SPIRV::Decoration::FuncParamAttr, {Attr});
341 }
342 if (Arg.hasAttribute(Attribute::ByVal)) {
343 auto Attr =
344 static_cast<unsigned>(SPIRV::FunctionParameterAttribute::ByVal);
345 buildOpDecorate(VRegs[i][0], MIRBuilder,
346 SPIRV::Decoration::FuncParamAttr, {Attr});
347 }
348
349 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
350 std::vector<SPIRV::Decoration::Decoration> ArgTypeQualDecs =
352 for (SPIRV::Decoration::Decoration Decoration : ArgTypeQualDecs)
353 buildOpDecorate(VRegs[i][0], MIRBuilder, Decoration, {});
354 }
355
356 MDNode *Node = F.getMetadata("spirv.ParameterDecorations");
357 if (Node && i < Node->getNumOperands() &&
358 isa<MDNode>(Node->getOperand(i))) {
359 MDNode *MD = cast<MDNode>(Node->getOperand(i));
360 for (const MDOperand &MDOp : MD->operands()) {
361 MDNode *MD2 = dyn_cast<MDNode>(MDOp);
362 assert(MD2 && "Metadata operand is expected");
363 ConstantInt *Const = getConstInt(MD2, 0);
364 assert(Const && "MDOperand should be ConstantInt");
365 auto Dec =
366 static_cast<SPIRV::Decoration::Decoration>(Const->getZExtValue());
367 std::vector<uint32_t> DecVec;
368 for (unsigned j = 1; j < MD2->getNumOperands(); j++) {
369 ConstantInt *Const = getConstInt(MD2, j);
370 assert(Const && "MDOperand should be ConstantInt");
371 DecVec.push_back(static_cast<uint32_t>(Const->getZExtValue()));
372 }
373 buildOpDecorate(VRegs[i][0], MIRBuilder, Dec, DecVec);
374 }
375 }
376 ++i;
377 }
378 }
379
380 auto MRI = MIRBuilder.getMRI();
381 Register FuncVReg = MRI->createGenericVirtualRegister(LLT::scalar(32));
382 MRI->setRegClass(FuncVReg, &SPIRV::IDRegClass);
383 if (F.isDeclaration())
384 GR->add(&F, &MIRBuilder.getMF(), FuncVReg);
386 SPIRVType *RetTy = GR->getOrCreateSPIRVType(FTy->getReturnType(), MIRBuilder);
387 FTy = fixFunctionTypeIfPtrArgs(GR, F, FTy, RetTy, ArgTypeVRegs);
389 FTy, RetTy, ArgTypeVRegs, MIRBuilder);
390 uint32_t FuncControl = getFunctionControl(F);
391
392 // Add OpFunction instruction
393 MachineInstrBuilder MB = MIRBuilder.buildInstr(SPIRV::OpFunction)
394 .addDef(FuncVReg)
396 .addImm(FuncControl)
397 .addUse(GR->getSPIRVTypeID(FuncTy));
399
400 // Add OpFunctionParameter instructions
401 int i = 0;
402 for (const auto &Arg : F.args()) {
403 assert(VRegs[i].size() == 1 && "Formal arg has multiple vregs");
404 MRI->setRegClass(VRegs[i][0], &SPIRV::IDRegClass);
405 MIRBuilder.buildInstr(SPIRV::OpFunctionParameter)
406 .addDef(VRegs[i][0])
407 .addUse(GR->getSPIRVTypeID(ArgTypeVRegs[i]));
408 if (F.isDeclaration())
409 GR->add(&Arg, &MIRBuilder.getMF(), VRegs[i][0]);
410 i++;
411 }
412 // Name the function.
413 if (F.hasName())
414 buildOpName(FuncVReg, F.getName(), MIRBuilder);
415
416 // Handle entry points and function linkage.
417 if (isEntryPoint(F)) {
418 const auto &STI = MIRBuilder.getMF().getSubtarget<SPIRVSubtarget>();
419 auto executionModel = getExecutionModel(STI, F);
420 auto MIB = MIRBuilder.buildInstr(SPIRV::OpEntryPoint)
421 .addImm(static_cast<uint32_t>(executionModel))
422 .addUse(FuncVReg);
423 addStringImm(F.getName(), MIB);
424 } else if (F.getLinkage() == GlobalValue::LinkageTypes::ExternalLinkage ||
425 F.getLinkage() == GlobalValue::LinkOnceODRLinkage) {
426 SPIRV::LinkageType::LinkageType LnkTy =
427 F.isDeclaration()
428 ? SPIRV::LinkageType::Import
429 : (F.getLinkage() == GlobalValue::LinkOnceODRLinkage &&
430 ST->canUseExtension(
431 SPIRV::Extension::SPV_KHR_linkonce_odr)
432 ? SPIRV::LinkageType::LinkOnceODR
433 : SPIRV::LinkageType::Export);
434 buildOpDecorate(FuncVReg, MIRBuilder, SPIRV::Decoration::LinkageAttributes,
435 {static_cast<uint32_t>(LnkTy)}, F.getGlobalIdentifier());
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// Used to postpone producing of indirect function pointer types after all
455// indirect calls info is collected
456// TODO:
457// - add a topological sort of IndirectCalls to ensure the best types knowledge
458// - we may need to fix function formal parameter types if they are opaque
459// pointers used as function pointers in these indirect calls
460void SPIRVCallLowering::produceIndirectPtrTypes(
461 MachineIRBuilder &MIRBuilder) const {
462 // Create indirect call data types if any
463 MachineFunction &MF = MIRBuilder.getMF();
464 for (auto const &IC : IndirectCalls) {
465 SPIRVType *SpirvRetTy = GR->getOrCreateSPIRVType(IC.RetTy, MIRBuilder);
466 SmallVector<SPIRVType *, 4> SpirvArgTypes;
467 for (size_t i = 0; i < IC.ArgTys.size(); ++i) {
468 SPIRVType *SPIRVTy = GR->getOrCreateSPIRVType(IC.ArgTys[i], MIRBuilder);
469 SpirvArgTypes.push_back(SPIRVTy);
470 if (!GR->getSPIRVTypeForVReg(IC.ArgRegs[i]))
471 GR->assignSPIRVTypeToVReg(SPIRVTy, IC.ArgRegs[i], MF);
472 }
473 // SPIR-V function type:
474 FunctionType *FTy =
475 FunctionType::get(const_cast<Type *>(IC.RetTy), IC.ArgTys, false);
477 FTy, SpirvRetTy, SpirvArgTypes, MIRBuilder);
478 // SPIR-V pointer to function type:
479 SPIRVType *IndirectFuncPtrTy = GR->getOrCreateSPIRVPointerType(
480 SpirvFuncTy, MIRBuilder, SPIRV::StorageClass::Function);
481 // Correct the Callee type
482 GR->assignSPIRVTypeToVReg(IndirectFuncPtrTy, IC.Callee, MF);
483 }
484}
485
487 CallLoweringInfo &Info) const {
488 // Currently call returns should have single vregs.
489 // TODO: handle the case of multiple registers.
490 if (Info.OrigRet.Regs.size() > 1)
491 return false;
492 MachineFunction &MF = MIRBuilder.getMF();
493 GR->setCurrentFunc(MF);
494 const Function *CF = nullptr;
495 std::string DemangledName;
496 const Type *OrigRetTy = Info.OrigRet.Ty;
497
498 // Emit a regular OpFunctionCall. If it's an externally declared function,
499 // be sure to emit its type and function declaration here. It will be hoisted
500 // globally later.
501 if (Info.Callee.isGlobal()) {
502 std::string FuncName = Info.Callee.getGlobal()->getName().str();
503 DemangledName = getOclOrSpirvBuiltinDemangledName(FuncName);
504 CF = dyn_cast_or_null<const Function>(Info.Callee.getGlobal());
505 // TODO: support constexpr casts and indirect calls.
506 if (CF == nullptr)
507 return false;
509 OrigRetTy = FTy->getReturnType();
510 }
511
512 MachineRegisterInfo *MRI = MIRBuilder.getMRI();
513 Register ResVReg =
514 Info.OrigRet.Regs.empty() ? Register(0) : Info.OrigRet.Regs[0];
515 const auto *ST = static_cast<const SPIRVSubtarget *>(&MF.getSubtarget());
516
517 bool isFunctionDecl = CF && CF->isDeclaration();
518 bool canUseOpenCL = ST->canUseExtInstSet(SPIRV::InstructionSet::OpenCL_std);
519 bool canUseGLSL = ST->canUseExtInstSet(SPIRV::InstructionSet::GLSL_std_450);
520 assert(canUseGLSL != canUseOpenCL &&
521 "Scenario where both sets are enabled is not supported.");
522
523 if (isFunctionDecl && !DemangledName.empty() &&
524 (canUseGLSL || canUseOpenCL)) {
526 for (auto Arg : Info.OrigArgs) {
527 assert(Arg.Regs.size() == 1 && "Call arg has multiple VRegs");
528 ArgVRegs.push_back(Arg.Regs[0]);
529 SPIRVType *SPIRVTy = GR->getOrCreateSPIRVType(Arg.Ty, MIRBuilder);
530 if (!GR->getSPIRVTypeForVReg(Arg.Regs[0]))
531 GR->assignSPIRVTypeToVReg(SPIRVTy, Arg.Regs[0], MF);
532 }
533 auto instructionSet = canUseOpenCL ? SPIRV::InstructionSet::OpenCL_std
534 : SPIRV::InstructionSet::GLSL_std_450;
535 if (auto Res =
536 SPIRV::lowerBuiltin(DemangledName, instructionSet, MIRBuilder,
537 ResVReg, OrigRetTy, ArgVRegs, GR))
538 return *Res;
539 }
540
541 if (isFunctionDecl && !GR->find(CF, &MF).isValid()) {
542 // Emit the type info and forward function declaration to the first MBB
543 // to ensure VReg definition dependencies are valid across all MBBs.
544 MachineIRBuilder FirstBlockBuilder;
545 FirstBlockBuilder.setMF(MF);
546 FirstBlockBuilder.setMBB(*MF.getBlockNumbered(0));
547
550 for (const Argument &Arg : CF->args()) {
551 if (MIRBuilder.getDataLayout().getTypeStoreSize(Arg.getType()).isZero())
552 continue; // Don't handle zero sized types.
553 Register Reg = MRI->createGenericVirtualRegister(LLT::scalar(32));
554 MRI->setRegClass(Reg, &SPIRV::IDRegClass);
555 ToInsert.push_back({Reg});
556 VRegArgs.push_back(ToInsert.back());
557 }
558 // TODO: Reuse FunctionLoweringInfo
559 FunctionLoweringInfo FuncInfo;
560 lowerFormalArguments(FirstBlockBuilder, *CF, VRegArgs, FuncInfo);
561 }
562
563 unsigned CallOp;
564 if (Info.CB->isIndirectCall()) {
565 if (!ST->canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers))
566 report_fatal_error("An indirect call is encountered but SPIR-V without "
567 "extensions does not support it",
568 false);
569 // Set instruction operation according to SPV_INTEL_function_pointers
570 CallOp = SPIRV::OpFunctionPointerCallINTEL;
571 // Collect information about the indirect call to support possible
572 // specification of opaque ptr types of parent function's parameters
573 Register CalleeReg = Info.Callee.getReg();
574 if (CalleeReg.isValid()) {
575 SPIRVCallLowering::SPIRVIndirectCall IndirectCall;
576 IndirectCall.Callee = CalleeReg;
577 IndirectCall.RetTy = OrigRetTy;
578 for (const auto &Arg : Info.OrigArgs) {
579 assert(Arg.Regs.size() == 1 && "Call arg has multiple VRegs");
580 IndirectCall.ArgTys.push_back(Arg.Ty);
581 IndirectCall.ArgRegs.push_back(Arg.Regs[0]);
582 }
583 IndirectCalls.push_back(IndirectCall);
584 }
585 } else {
586 // Emit a regular OpFunctionCall
587 CallOp = SPIRV::OpFunctionCall;
588 }
589
590 // Make sure there's a valid return reg, even for functions returning void.
591 if (!ResVReg.isValid())
592 ResVReg = MIRBuilder.getMRI()->createVirtualRegister(&SPIRV::IDRegClass);
593 SPIRVType *RetType = GR->assignTypeToVReg(OrigRetTy, ResVReg, MIRBuilder);
594
595 // Emit the call instruction and its args.
596 auto MIB = MIRBuilder.buildInstr(CallOp)
597 .addDef(ResVReg)
598 .addUse(GR->getSPIRVTypeID(RetType))
599 .add(Info.Callee);
600
601 for (const auto &Arg : Info.OrigArgs) {
602 // Currently call args should have single vregs.
603 if (Arg.Regs.size() > 1)
604 return false;
605 MIB.addUse(Arg.Regs[0]);
606 }
607 return MIB.constrainAllUses(MIRBuilder.getTII(), *ST->getRegisterInfo(),
608 *ST->getRegBankInfo());
609}
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static ConstantInt * getConstInt(MDNode *MD, unsigned NumOp)
static uint32_t getFunctionControl(const Function &F)
static SPIRV::ExecutionModel::ExecutionModel getExecutionModel(const SPIRVSubtarget &STI, const Function &F)
static FunctionType * getOriginalFunctionType(const Function &F)
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)
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:165
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
TypeSize getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type.
Definition: DataLayout.h:472
FunctionLoweringInfo - This contains information that is global to a function that is used when lower...
Class to represent function types.
Definition: DerivedTypes.h:103
Type * getParamType(unsigned i) const
Parameter type accessors.
Definition: DerivedTypes.h:135
Type * getReturnType() const
Definition: DerivedTypes.h:124
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:838
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:281
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
@ 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
Metadata node.
Definition: Metadata.h:1067
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1428
ArrayRef< MDOperand > operands() const
Definition: Metadata.h:1426
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1434
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:889
A single uniqued string.
Definition: Metadata.h:720
StringRef getString() const
Definition: Metadata.cpp:610
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...
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.
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:556
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)
void add(const Constant *C, MachineFunction *MF, Register R)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
SPIRVType * getSPIRVTypeForVReg(Register VReg) const
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, MachineFunction &MF)
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)
MachineFunction * setCurrentFunc(MachineFunction &MF)
Register find(const MachineInstr *MI, MachineFunction *MF)
SPIRVType * getOrCreateSPIRVPointerType(SPIRVType *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SClass=SPIRV::StorageClass::Function)
bool isOpenCLEnv() const
size_t size() const
Definition: SmallVector.h:91
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
int compare(StringRef RHS) const
compare - Compare two strings; the result is negative, zero, or positive if this string is lexicograp...
Definition: StringRef.h:177
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
static IntegerType * getInt8Ty(LLVMContext &C)
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:100
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:1680
unsigned getPointerAddressSpace(const Type *T)
Definition: SPIRVUtils.h:122
MDString * getOCLKernelArgAccessQual(const Function &F, unsigned ArgIdx)
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
Definition: SPIRVUtils.cpp:306
bool isTypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:106
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Definition: SPIRVUtils.cpp:117
bool isPointerTy(const Type *T)
Definition: SPIRVUtils.h:116
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:156
bool isEntryPoint(const Function &F)
Definition: SPIRVUtils.cpp:357
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
Definition: SPIRVUtils.cpp:162
MDString * getOCLKernelArgTypeQual(const Function &F, unsigned ArgIdx)
Type * getPointeeTypeByAttr(Argument *Arg)
Definition: SPIRVUtils.h:135
bool hasPointeeTypeAttr(Argument *Arg)
Definition: SPIRVUtils.h:130
void addStringImm(const StringRef &Str, MCInst &Inst)
Definition: SPIRVUtils.cpp:51
bool isUntypedPointerTy(const Type *T)
Definition: SPIRVUtils.h:111
#define N