clang 22.0.0git
SPIR.cpp
Go to the documentation of this file.
1//===- SPIR.cpp -----------------------------------------------------------===//
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#include "ABIInfoImpl.h"
11#include "TargetInfo.h"
12
13using namespace clang;
14using namespace clang::CodeGen;
15
16//===----------------------------------------------------------------------===//
17// Base ABI and target codegen info implementation common between SPIR and
18// SPIR-V.
19//===----------------------------------------------------------------------===//
20
21namespace {
22class CommonSPIRABIInfo : public DefaultABIInfo {
23public:
24 CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
25
26private:
27 void setCCs();
28};
29
30class SPIRVABIInfo : public CommonSPIRABIInfo {
31public:
32 SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
33 void computeInfo(CGFunctionInfo &FI) const override;
34
35private:
37 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
39};
40} // end anonymous namespace
41namespace {
42class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
43public:
44 CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
45 : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
46 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
47 : TargetCodeGenInfo(std::move(ABIInfo)) {}
48
49 LangAS getASTAllocaAddressSpace() const override {
51 getABIInfo().getDataLayout().getAllocaAddrSpace());
52 }
53
54 unsigned getDeviceKernelCallingConv() const override;
55 llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const override;
56 llvm::Type *
57 getHLSLType(CodeGenModule &CGM, const Type *Ty,
58 const SmallVector<int32_t> *Packoffsets = nullptr) const override;
59 llvm::Type *getSPIRVImageTypeFromHLSLResource(
61 QualType SampledType, CodeGenModule &CGM) const;
62 void
63 setOCLKernelStubCallingConvention(const FunctionType *&FT) const override;
64};
65class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
66public:
67 SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
68 : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
69 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
70 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
71 const VarDecl *D) const override;
72 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
73 CodeGen::CodeGenModule &M) const override;
74 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
76 llvm::AtomicOrdering Ordering,
77 llvm::LLVMContext &Ctx) const override;
78 bool supportsLibCall() const override {
79 return getABIInfo().getTarget().getTriple().getVendor() !=
80 llvm::Triple::AMD;
81 }
82};
83
84inline StringRef mapClangSyncScopeToLLVM(SyncScope Scope) {
85 switch (Scope) {
86 case SyncScope::HIPSingleThread:
87 case SyncScope::SingleScope:
88 return "singlethread";
89 case SyncScope::HIPWavefront:
90 case SyncScope::OpenCLSubGroup:
91 case SyncScope::WavefrontScope:
92 return "subgroup";
93 case SyncScope::HIPWorkgroup:
94 case SyncScope::OpenCLWorkGroup:
95 case SyncScope::WorkgroupScope:
96 return "workgroup";
97 case SyncScope::HIPAgent:
98 case SyncScope::OpenCLDevice:
99 case SyncScope::DeviceScope:
100 return "device";
101 case SyncScope::SystemScope:
102 case SyncScope::HIPSystem:
103 case SyncScope::OpenCLAllSVMDevices:
104 return "";
105 }
106 return "";
107}
108} // End anonymous namespace.
109
110void CommonSPIRABIInfo::setCCs() {
111 assert(getRuntimeCC() == llvm::CallingConv::C);
112 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
113}
114
115ABIArgInfo SPIRVABIInfo::classifyReturnType(QualType RetTy) const {
116 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
118 if (!isAggregateTypeForABI(RetTy) || getRecordArgABI(RetTy, getCXXABI()))
120
121 if (const auto *RD = RetTy->getAsRecordDecl();
122 RD && RD->hasFlexibleArrayMember())
124
125 // TODO: The AMDGPU ABI is non-trivial to represent in SPIR-V; in order to
126 // avoid encoding various architecture specific bits here we return everything
127 // as direct to retain type info for things like aggregates, for later perusal
128 // when translating back to LLVM/lowering in the BE. This is also why we
129 // disable flattening as the outcomes can mismatch between SPIR-V and AMDGPU.
130 // This will be revisited / optimised in the future.
131 return ABIArgInfo::getDirect(CGT.ConvertType(RetTy), 0u, nullptr, false);
132}
133
134ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
135 if (getContext().getLangOpts().CUDAIsDevice) {
136 // Coerce pointer arguments with default address space to CrossWorkGroup
137 // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
138 // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
139 llvm::Type *LTy = CGT.ConvertType(Ty);
140 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
141 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
142 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
143 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
144 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
145 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
146 }
147
148 if (isAggregateTypeForABI(Ty)) {
149 if (getTarget().getTriple().getVendor() == llvm::Triple::AMD)
150 // TODO: The AMDGPU kernel ABI passes aggregates byref, which is not
151 // currently expressible in SPIR-V; SPIR-V passes aggregates byval,
152 // which the AMDGPU kernel ABI does not allow. Passing aggregates as
153 // direct works around this impedance mismatch, as it retains type info
154 // and can be correctly handled, post reverse-translation, by the AMDGPU
155 // BE, which has to support this CC for legacy OpenCL purposes. It can
156 // be brittle and does lead to performance degradation in certain
157 // pathological cases. This will be revisited / optimised in the future,
158 // once a way to deal with the byref/byval impedance mismatch is
159 // identified.
160 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
161 // Force copying aggregate type in kernel arguments by value when
162 // compiling CUDA targeting SPIR-V. This is required for the object
163 // copied to be valid on the device.
164 // This behavior follows the CUDA spec
165 // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
166 // and matches the NVPTX implementation. TODO: hardcoding to 0 should be
167 // revisited if HIPSPV / byval starts making use of the AS of an indirect
168 // arg.
169 return getNaturalAlignIndirect(Ty, /*AddrSpace=*/0, /*byval=*/true);
170 }
171 }
172 return classifyArgumentType(Ty);
173}
174
175ABIArgInfo SPIRVABIInfo::classifyArgumentType(QualType Ty) const {
176 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
178 if (!isAggregateTypeForABI(Ty))
180
181 // Records with non-trivial destructors/copy-constructors should not be
182 // passed by value.
183 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
184 return getNaturalAlignIndirect(Ty, getDataLayout().getAllocaAddrSpace(),
186
187 if (const auto *RD = Ty->getAsRecordDecl();
188 RD && RD->hasFlexibleArrayMember())
190
191 return ABIArgInfo::getDirect(CGT.ConvertType(Ty), 0u, nullptr, false);
192}
193
194void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
195 // The logic is same as in DefaultABIInfo with an exception on the kernel
196 // arguments handling.
197 llvm::CallingConv::ID CC = FI.getCallingConvention();
198
199 if (!getCXXABI().classifyReturnType(FI))
201
202 for (auto &I : FI.arguments()) {
203 if (CC == llvm::CallingConv::SPIR_KERNEL) {
204 I.info = classifyKernelArgumentType(I.type);
205 } else {
206 I.info = classifyArgumentType(I.type);
207 }
208 }
209}
210
211namespace clang {
212namespace CodeGen {
214 if (CGM.getTarget().getTriple().isSPIRV())
215 SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
216 else
217 CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
218}
219}
220}
221
222unsigned CommonSPIRTargetCodeGenInfo::getDeviceKernelCallingConv() const {
223 return llvm::CallingConv::SPIR_KERNEL;
224}
225
226void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
227 const FunctionType *&FT) const {
228 // Convert HIP kernels to SPIR-V kernels.
229 if (getABIInfo().getContext().getLangOpts().HIP) {
230 FT = getABIInfo().getContext().adjustFunctionType(
232 return;
233 }
234}
235
236void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention(
237 const FunctionType *&FT) const {
238 FT = getABIInfo().getContext().adjustFunctionType(
240}
241
242LangAS
243SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
244 const VarDecl *D) const {
245 assert(!CGM.getLangOpts().OpenCL &&
246 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
247 "Address space agnostic languages only");
248 // If we're here it means that we're using the SPIRDefIsGen ASMap, hence for
249 // the global AS we can rely on either cuda_device or sycl_global to be
250 // correct; however, since this is not a CUDA Device context, we use
251 // sycl_global to prevent confusion with the assertion.
252 LangAS DefaultGlobalAS = getLangASFromTargetAS(
253 CGM.getContext().getTargetAddressSpace(LangAS::sycl_global));
254 if (!D)
255 return DefaultGlobalAS;
256
257 LangAS AddrSpace = D->getType().getAddressSpace();
258 if (AddrSpace != LangAS::Default)
259 return AddrSpace;
260
261 return DefaultGlobalAS;
262}
263
264void SPIRVTargetCodeGenInfo::setTargetAttributes(
265 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
266 if (!M.getLangOpts().HIP ||
267 M.getTarget().getTriple().getVendor() != llvm::Triple::AMD)
268 return;
269 if (GV->isDeclaration())
270 return;
271
272 auto F = dyn_cast<llvm::Function>(GV);
273 if (!F)
274 return;
275
276 auto FD = dyn_cast_or_null<FunctionDecl>(D);
277 if (!FD)
278 return;
279 if (!FD->hasAttr<CUDAGlobalAttr>())
280 return;
281
282 unsigned N = M.getLangOpts().GPUMaxThreadsPerBlock;
283 if (auto FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>())
284 N = FlatWGS->getMax()->EvaluateKnownConstInt(M.getContext()).getExtValue();
285
286 // We encode the maximum flat WG size in the first component of the 3D
287 // max_work_group_size attribute, which will get reverse translated into the
288 // original AMDGPU attribute when targeting AMDGPU.
289 auto Int32Ty = llvm::IntegerType::getInt32Ty(M.getLLVMContext());
290 llvm::Metadata *AttrMDArgs[] = {
291 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, N)),
292 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
293 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1))};
294
295 F->setMetadata("max_work_group_size",
296 llvm::MDNode::get(M.getLLVMContext(), AttrMDArgs));
297}
298
299llvm::SyncScope::ID
300SPIRVTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &, SyncScope Scope,
301 llvm::AtomicOrdering,
302 llvm::LLVMContext &Ctx) const {
303 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(Scope));
304}
305
306/// Construct a SPIR-V target extension type for the given OpenCL image type.
307static llvm::Type *getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType,
308 StringRef OpenCLName,
309 unsigned AccessQualifier) {
310 // These parameters compare to the operands of OpTypeImage (see
311 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage
312 // for more details). The first 6 integer parameters all default to 0, and
313 // will be changed to 1 only for the image type(s) that set the parameter to
314 // one. The 7th integer parameter is the access qualifier, which is tacked on
315 // at the end.
316 SmallVector<unsigned, 7> IntParams = {0, 0, 0, 0, 0, 0};
317
318 // Choose the dimension of the image--this corresponds to the Dim enum in
319 // SPIR-V (first integer parameter of OpTypeImage).
320 if (OpenCLName.starts_with("image2d"))
321 IntParams[0] = 1;
322 else if (OpenCLName.starts_with("image3d"))
323 IntParams[0] = 2;
324 else if (OpenCLName == "image1d_buffer")
325 IntParams[0] = 5; // Buffer
326 else
327 assert(OpenCLName.starts_with("image1d") && "Unknown image type");
328
329 // Set the other integer parameters of OpTypeImage if necessary. Note that the
330 // OpenCL image types don't provide any information for the Sampled or
331 // Image Format parameters.
332 if (OpenCLName.contains("_depth"))
333 IntParams[1] = 1;
334 if (OpenCLName.contains("_array"))
335 IntParams[2] = 1;
336 if (OpenCLName.contains("_msaa"))
337 IntParams[3] = 1;
338
339 // Access qualifier
340 IntParams.push_back(AccessQualifier);
341
342 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
343 IntParams);
344}
345
346llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
347 const Type *Ty) const {
348 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
349 if (auto *PipeTy = dyn_cast<PipeType>(Ty))
350 return llvm::TargetExtType::get(Ctx, "spirv.Pipe", {},
351 {!PipeTy->isReadOnly()});
352 if (auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
353 enum AccessQualifier : unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
354 switch (BuiltinTy->getKind()) {
355#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
356 case BuiltinType::Id: \
357 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
358#include "clang/Basic/OpenCLImageTypes.def"
359 case BuiltinType::OCLSampler:
360 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
361 case BuiltinType::OCLEvent:
362 return llvm::TargetExtType::get(Ctx, "spirv.Event");
363 case BuiltinType::OCLClkEvent:
364 return llvm::TargetExtType::get(Ctx, "spirv.DeviceEvent");
365 case BuiltinType::OCLQueue:
366 return llvm::TargetExtType::get(Ctx, "spirv.Queue");
367 case BuiltinType::OCLReserveID:
368 return llvm::TargetExtType::get(Ctx, "spirv.ReserveId");
369#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
370 case BuiltinType::OCLIntelSubgroupAVC##Id: \
371 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
372#include "clang/Basic/OpenCLExtensionTypes.def"
373 default:
374 return nullptr;
375 }
376 }
377
378 return nullptr;
379}
380
381// Gets a spirv.IntegralConstant or spirv.Literal. If IntegralType is present,
382// returns an IntegralConstant, otherwise returns a Literal.
383static llvm::Type *getInlineSpirvConstant(CodeGenModule &CGM,
384 llvm::Type *IntegralType,
385 llvm::APInt Value) {
386 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
387
388 // Convert the APInt value to an array of uint32_t words
390
391 while (Value.ugt(0)) {
392 uint32_t Word = Value.trunc(32).getZExtValue();
393 Value.lshrInPlace(32);
394
395 Words.push_back(Word);
396 }
397 if (Words.size() == 0)
398 Words.push_back(0);
399
400 if (IntegralType)
401 return llvm::TargetExtType::get(Ctx, "spirv.IntegralConstant",
402 {IntegralType}, Words);
403 return llvm::TargetExtType::get(Ctx, "spirv.Literal", {}, Words);
404}
405
406static llvm::Type *getInlineSpirvType(CodeGenModule &CGM,
407 const HLSLInlineSpirvType *SpirvType) {
408 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
409
411
412 for (auto &Operand : SpirvType->getOperands()) {
413 using SpirvOperandKind = SpirvOperand::SpirvOperandKind;
414
415 llvm::Type *Result = nullptr;
416 switch (Operand.getKind()) {
417 case SpirvOperandKind::ConstantId: {
418 llvm::Type *IntegralType =
419 CGM.getTypes().ConvertType(Operand.getResultType());
420
421 Result = getInlineSpirvConstant(CGM, IntegralType, Operand.getValue());
422 break;
423 }
424 case SpirvOperandKind::Literal: {
425 Result = getInlineSpirvConstant(CGM, nullptr, Operand.getValue());
426 break;
427 }
428 case SpirvOperandKind::TypeId: {
429 QualType TypeOperand = Operand.getResultType();
430 if (const auto *RD = TypeOperand->getAsRecordDecl()) {
431 assert(RD->isCompleteDefinition() &&
432 "Type completion should have been required in Sema");
433
434 const FieldDecl *HandleField = RD->findFirstNamedDataMember();
435 if (HandleField) {
436 QualType ResourceType = HandleField->getType();
437 if (ResourceType->getAs<HLSLAttributedResourceType>()) {
438 TypeOperand = ResourceType;
439 }
440 }
441 }
442 Result = CGM.getTypes().ConvertType(TypeOperand);
443 break;
444 }
445 default:
446 llvm_unreachable("HLSLInlineSpirvType had invalid operand!");
447 break;
448 }
449
450 assert(Result);
451 Operands.push_back(Result);
452 }
453
454 return llvm::TargetExtType::get(Ctx, "spirv.Type", Operands,
455 {SpirvType->getOpcode(), SpirvType->getSize(),
456 SpirvType->getAlignment()});
457}
458
459llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
460 CodeGenModule &CGM, const Type *Ty,
461 const SmallVector<int32_t> *Packoffsets) const {
462 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
463
464 if (auto *SpirvType = dyn_cast<HLSLInlineSpirvType>(Ty))
465 return getInlineSpirvType(CGM, SpirvType);
466
467 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
468 if (!ResType)
469 return nullptr;
470
471 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
472 switch (ResAttrs.ResourceClass) {
473 case llvm::dxil::ResourceClass::UAV:
474 case llvm::dxil::ResourceClass::SRV: {
475 // TypedBuffer and RawBuffer both need element type
476 QualType ContainedTy = ResType->getContainedType();
477 if (ContainedTy.isNull())
478 return nullptr;
479
480 assert(!ResAttrs.IsROV &&
481 "Rasterizer order views not implemented for SPIR-V yet");
482
483 if (!ResAttrs.RawBuffer) {
484 // convert element type
485 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ContainedTy, CGM);
486 }
487
488 llvm::Type *ElemType = CGM.getTypes().ConvertTypeForMem(ContainedTy);
489 llvm::ArrayType *RuntimeArrayType = llvm::ArrayType::get(ElemType, 0);
490 uint32_t StorageClass = /* StorageBuffer storage class */ 12;
491 bool IsWritable = ResAttrs.ResourceClass == llvm::dxil::ResourceClass::UAV;
492 return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer",
493 {RuntimeArrayType},
494 {StorageClass, IsWritable});
495 }
496 case llvm::dxil::ResourceClass::CBuffer: {
497 QualType ContainedTy = ResType->getContainedType();
498 if (ContainedTy.isNull() || !ContainedTy->isStructureType())
499 return nullptr;
500
501 llvm::Type *BufferLayoutTy =
502 HLSLBufferLayoutBuilder(CGM, "spirv.Layout")
504 Packoffsets);
505 uint32_t StorageClass = /* Uniform storage class */ 2;
506 return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {BufferLayoutTy},
507 {StorageClass, false});
508 break;
509 }
510 case llvm::dxil::ResourceClass::Sampler:
511 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
512 }
513 return nullptr;
514}
515
516static unsigned
519 llvm::Type *SampledType, QualType Ty, unsigned NumChannels) {
520 // For images with `Sampled` operand equal to 2, there are restrictions on
521 // using the Unknown image format. To avoid these restrictions in common
522 // cases, we guess an image format for them based on the sampled type and the
523 // number of channels. This is intended to match the behaviour of DXC.
524 if (LangOpts.HLSLSpvUseUnknownImageFormat ||
525 attributes.ResourceClass != llvm::dxil::ResourceClass::UAV) {
526 return 0; // Unknown
527 }
528
529 if (SampledType->isIntegerTy(32)) {
530 if (Ty->isSignedIntegerType()) {
531 if (NumChannels == 1)
532 return 24; // R32i
533 if (NumChannels == 2)
534 return 25; // Rg32i
535 if (NumChannels == 4)
536 return 21; // Rgba32i
537 } else {
538 if (NumChannels == 1)
539 return 33; // R32ui
540 if (NumChannels == 2)
541 return 35; // Rg32ui
542 if (NumChannels == 4)
543 return 30; // Rgba32ui
544 }
545 } else if (SampledType->isIntegerTy(64)) {
546 if (NumChannels == 1) {
547 if (Ty->isSignedIntegerType()) {
548 return 41; // R64i
549 }
550 return 40; // R64ui
551 }
552 } else if (SampledType->isFloatTy()) {
553 if (NumChannels == 1)
554 return 3; // R32f
555 if (NumChannels == 2)
556 return 6; // Rg32f
557 if (NumChannels == 4)
558 return 1; // Rgba32f
559 }
560
561 return 0; // Unknown
562}
563
564llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
566 CodeGenModule &CGM) const {
567 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
568
569 unsigned NumChannels = 1;
571 if (const VectorType *V = dyn_cast<VectorType>(Ty)) {
572 NumChannels = V->getNumElements();
573 Ty = V->getElementType();
574 }
575 assert(!Ty->isVectorType() && "We still have a vector type.");
576
577 llvm::Type *SampledType = CGM.getTypes().ConvertTypeForMem(Ty);
578
579 assert((SampledType->isIntegerTy() || SampledType->isFloatingPointTy()) &&
580 "The element type for a SPIR-V resource must be a scalar integer or "
581 "floating point type.");
582
583 // These parameters correspond to the operands to the OpTypeImage SPIR-V
584 // instruction. See
585 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage.
586 SmallVector<unsigned, 6> IntParams(6, 0);
587
588 const char *Name =
589 Ty->isSignedIntegerType() ? "spirv.SignedImage" : "spirv.Image";
590
591 // Dim
592 // For now we assume everything is a buffer.
593 IntParams[0] = 5;
594
595 // Depth
596 // HLSL does not indicate if it is a depth texture or not, so we use unknown.
597 IntParams[1] = 2;
598
599 // Arrayed
600 IntParams[2] = 0;
601
602 // MS
603 IntParams[3] = 0;
604
605 // Sampled
606 IntParams[4] =
607 attributes.ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
608
609 // Image format.
610 IntParams[5] = getImageFormat(CGM.getLangOpts(), attributes, SampledType, Ty,
611 NumChannels);
612
613 llvm::TargetExtType *ImageType =
614 llvm::TargetExtType::get(Ctx, Name, {SampledType}, IntParams);
615 return ImageType;
616}
617
618std::unique_ptr<TargetCodeGenInfo>
620 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.getTypes());
621}
622
623std::unique_ptr<TargetCodeGenInfo>
625 return std::make_unique<SPIRVTargetCodeGenInfo>(CGM.getTypes());
626}
#define V(N, I)
Definition: ASTContext.h:3597
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition: CGCall.cpp:359
const Decl * D
static llvm::Type * getInlineSpirvType(CodeGenModule &CGM, const HLSLInlineSpirvType *SpirvType)
Definition: SPIR.cpp:406
static llvm::Type * getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType, StringRef OpenCLName, unsigned AccessQualifier)
Construct a SPIR-V target extension type for the given OpenCL image type.
Definition: SPIR.cpp:307
static unsigned getImageFormat(const LangOptions &LangOpts, const HLSLAttributedResourceType::Attributes &attributes, llvm::Type *SampledType, QualType Ty, unsigned NumChannels)
Definition: SPIR.cpp:517
static llvm::Type * getInlineSpirvConstant(CodeGenModule &CGM, llvm::Type *IntegralType, llvm::APInt Value)
Definition: SPIR.cpp:383
unsigned getTargetAddressSpace(LangAS AS) const
ABIArgInfo - Helper class to encapsulate information about how a specific C type should be passed to ...
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
ABIInfo - Target specific hooks for defining how a type should be passed or returned from functions.
Definition: ABIInfo.h:48
@ RAA_DirectInMemory
Pass it on the stack using its defined layout.
Definition: CGCXXABI.h:158
CGFunctionInfo - Class to encapsulate the information about a function definition.
unsigned getCallingConvention() const
getCallingConvention - Return the user specified calling convention, which has been translated into a...
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
This class organizes the cross-function state that is used while generating LLVM code.
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
ASTContext & getContext() const
llvm::LLVMContext & getLLVMContext()
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
Definition: CodeGenTypes.h:54
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
llvm::Type * ConvertTypeForMem(QualType T)
ConvertTypeForMem - Convert type T into a llvm::Type.
DefaultABIInfo - The default implementation for ABI specific details.
Definition: ABIInfoImpl.h:21
ABIArgInfo classifyArgumentType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:17
ABIArgInfo classifyReturnType(QualType RetTy) const
Definition: ABIInfoImpl.cpp:47
llvm::TargetExtType * createLayoutType(const RecordType *StructType, const llvm::SmallVector< int32_t > *Packoffsets=nullptr)
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition: TargetInfo.h:47
virtual unsigned getDeviceKernelCallingConv() const
Get LLVM calling convention for device kernels.
Definition: TargetInfo.cpp:113
virtual llvm::Type * getOpenCLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to an OpenCL type.
Definition: TargetInfo.h:440
virtual void setOCLKernelStubCallingConvention(const FunctionType *&FT) const
Definition: TargetInfo.cpp:130
virtual llvm::Type * getHLSLType(CodeGenModule &CGM, const Type *T, const SmallVector< int32_t > *Packoffsets=nullptr) const
Return an LLVM type that corresponds to a HLSL type.
Definition: TargetInfo.h:446
const T & getABIInfo() const
Definition: TargetInfo.h:57
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Definition: TargetInfo.h:320
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
Represents a member of a struct/union/class.
Definition: Decl.h:3157
ExtInfo withCallingConv(CallingConv cc) const
Definition: TypeBase.h:4701
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: TypeBase.h:4478
ExtInfo getExtInfo() const
Definition: TypeBase.h:4834
Represents an arbitrary, user-specified SPIR-V type instruction.
Definition: TypeBase.h:6860
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:434
A (possibly-)qualified type.
Definition: TypeBase.h:937
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: TypeBase.h:1004
bool hasFlexibleArrayMember() const
Definition: Decl.h:4342
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: TypeBase.h:6502
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:41
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1288
The base class of the type hierarchy.
Definition: TypeBase.h:1833
bool isStructureType() const
Definition: Type.cpp:678
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
Definition: Type.cpp:2209
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition: Type.h:41
CanQualType getCanonicalTypeUnqualified() const
bool isVectorType() const
Definition: TypeBase.h:8719
const T * castAsCanonical() const
Return this type's canonical type cast to the specified type.
Definition: TypeBase.h:2946
const T * getAs() const
Member-template getAs<specific type>'.
Definition: TypeBase.h:9159
QualType getType() const
Definition: Decl.h:722
Represents a variable declaration or definition.
Definition: Decl.h:925
Represents a GCC generic vector type.
Definition: TypeBase.h:4191
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Definition: SPIR.cpp:213
bool isAggregateTypeForABI(QualType T)
Definition: ABIInfoImpl.cpp:96
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:624
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:619
The JSON file list parser is used to communicate input to InstallAPI.
StorageClass
Storage classes.
Definition: Specifiers.h:248
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
const FunctionProtoType * T
SyncScope
Defines sync scope values used internally by clang.
Definition: SyncScope.h:42
@ CC_DeviceKernel
Definition: Specifiers.h:292
@ CC_SpirFunction
Definition: Specifiers.h:291
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:90
unsigned int uint32_t
llvm::dxil::ResourceClass ResourceClass
Definition: TypeBase.h:6713