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"
13#include "llvm/IR/DerivedTypes.h"
14
15#include <stdint.h>
16#include <utility>
17
18using namespace clang;
19using namespace clang::CodeGen;
20
21//===----------------------------------------------------------------------===//
22// Base ABI and target codegen info implementation common between SPIR and
23// SPIR-V.
24//===----------------------------------------------------------------------===//
25
26namespace {
27class CommonSPIRABIInfo : public DefaultABIInfo {
28public:
29 CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
30
31private:
32 void setCCs();
33};
34
35class SPIRVABIInfo : public CommonSPIRABIInfo {
36public:
37 SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
38 void computeInfo(CGFunctionInfo &FI) const override;
39
40private:
41 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
42};
43
44class AMDGCNSPIRVABIInfo : public SPIRVABIInfo {
45 // TODO: this should be unified / shared with AMDGPU, ideally we'd like to
46 // re-use AMDGPUABIInfo eventually, rather than duplicate.
47 static constexpr unsigned MaxNumRegsForArgsRet = 16; // 16 32-bit registers
48 mutable unsigned NumRegsLeft = 0;
49
50 unsigned numRegsForType(QualType Ty) const;
51
52 bool isHomogeneousAggregateBaseType(QualType Ty) const override {
53 return true;
54 }
55 bool isHomogeneousAggregateSmallEnough(const Type *Base,
56 uint64_t Members) const override {
57 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
58
59 // Homogeneous Aggregates may occupy at most 16 registers.
60 return Members * NumRegs <= MaxNumRegsForArgsRet;
61 }
62
63 // Coerce HIP scalar pointer arguments from generic pointers to global ones.
64 llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
65 unsigned ToAS) const;
66
67 ABIArgInfo classifyReturnType(QualType RetTy) const;
68 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
69 ABIArgInfo classifyArgumentType(QualType Ty) const;
70
71public:
72 AMDGCNSPIRVABIInfo(CodeGenTypes &CGT) : SPIRVABIInfo(CGT) {}
73 void computeInfo(CGFunctionInfo &FI) const override;
74
75 llvm::FixedVectorType *
76 getOptimalVectorMemoryType(llvm::FixedVectorType *Ty,
77 const LangOptions &LangOpt) const override;
78};
79} // end anonymous namespace
80namespace {
81class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
82public:
83 CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
84 : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
85 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
86 : TargetCodeGenInfo(std::move(ABIInfo)) {}
87
88 LangAS getASTAllocaAddressSpace() const override {
90 getABIInfo().getDataLayout().getAllocaAddrSpace());
91 }
92
93 unsigned getDeviceKernelCallingConv() const override;
94 llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const override;
95 llvm::Type *getHLSLType(CodeGenModule &CGM, const Type *Ty,
96 const CGHLSLOffsetInfo &OffsetInfo) const override;
97
98 llvm::Type *getHLSLPadding(CodeGenModule &CGM,
99 CharUnits NumBytes) const override {
100 unsigned Size = NumBytes.getQuantity();
101 return llvm::TargetExtType::get(CGM.getLLVMContext(), "spirv.Padding", {},
102 {Size});
103 }
104
105 bool isHLSLPadding(llvm::Type *Ty) const override {
106 if (auto *TET = dyn_cast<llvm::TargetExtType>(Ty))
107 return TET->getName() == "spirv.Padding";
108 return false;
109 }
110
111 llvm::Type *getSPIRVImageTypeFromHLSLResource(
112 const HLSLAttributedResourceType::Attributes &attributes,
113 QualType SampledType, CodeGenModule &CGM) const;
114 void
115 setOCLKernelStubCallingConvention(const FunctionType *&FT) const override;
116 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
117 llvm::PointerType *T,
118 QualType QT) const override;
119};
120class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
121public:
122 SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
123 : CommonSPIRTargetCodeGenInfo(
124 (CGT.getTarget().getTriple().getVendor() == llvm::Triple::AMD)
125 ? std::make_unique<AMDGCNSPIRVABIInfo>(CGT)
126 : std::make_unique<SPIRVABIInfo>(CGT)) {}
127 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
128 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
129 const VarDecl *D) const override;
130 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
131 CodeGen::CodeGenModule &M) const override;
132 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
133 SyncScope Scope,
134 llvm::AtomicOrdering Ordering,
135 llvm::LLVMContext &Ctx) const override;
136 bool supportsLibCall() const override {
137 return getABIInfo().getTarget().getTriple().getVendor() !=
138 llvm::Triple::AMD;
139 }
140};
141
142inline StringRef mapClangSyncScopeToLLVM(SyncScope Scope) {
143 switch (Scope) {
146 return "singlethread";
150 return "subgroup";
156 return "workgroup";
160 return "device";
164 return "";
165 }
166 return "";
167}
168} // End anonymous namespace.
169
170void CommonSPIRABIInfo::setCCs() {
171 assert(getRuntimeCC() == llvm::CallingConv::C);
172 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
173}
174
175ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
176 if (getContext().getLangOpts().isTargetDevice()) {
177 // Coerce pointer arguments with default address space to CrossWorkGroup
178 // pointers for target devices as default address space kernel arguments
179 // are not allowed. We use the opencl_global language address space which
180 // always maps to CrossWorkGroup.
181 llvm::Type *LTy = CGT.ConvertType(Ty);
182 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
183 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::opencl_global);
184 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
185 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
186 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
187 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
188 }
189
190 if (isAggregateTypeForABI(Ty)) {
191 // Force copying aggregate type in kernel arguments by value when
192 // compiling CUDA targeting SPIR-V. This is required for the object
193 // copied to be valid on the device.
194 // This behavior follows the CUDA spec
195 // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
196 // and matches the NVPTX implementation. TODO: hardcoding to 0 should be
197 // revisited if HIPSPV / byval starts making use of the AS of an indirect
198 // arg.
199 return getNaturalAlignIndirect(Ty, /*AddrSpace=*/0, /*byval=*/true);
200 }
201 }
202 return classifyArgumentType(Ty);
203}
204
205void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
206 // The logic is same as in DefaultABIInfo with an exception on the kernel
207 // arguments handling.
208 llvm::CallingConv::ID CC = FI.getCallingConvention();
209
210 if (!getCXXABI().classifyReturnType(FI))
212
213 for (auto &I : FI.arguments()) {
214 if (CC == llvm::CallingConv::SPIR_KERNEL) {
215 I.info = classifyKernelArgumentType(I.type);
216 } else {
217 I.info = classifyArgumentType(I.type);
218 }
219 }
220}
221
222unsigned AMDGCNSPIRVABIInfo::numRegsForType(QualType Ty) const {
223 // This duplicates the AMDGPUABI computation.
224 unsigned NumRegs = 0;
225
226 if (const VectorType *VT = Ty->getAs<VectorType>()) {
227 // Compute from the number of elements. The reported size is based on the
228 // in-memory size, which includes the padding 4th element for 3-vectors.
229 QualType EltTy = VT->getElementType();
230 unsigned EltSize = getContext().getTypeSize(EltTy);
231
232 // 16-bit element vectors should be passed as packed.
233 if (EltSize == 16)
234 return (VT->getNumElements() + 1) / 2;
235
236 unsigned EltNumRegs = (EltSize + 31) / 32;
237 return EltNumRegs * VT->getNumElements();
238 }
239
240 if (const auto *RD = Ty->getAsRecordDecl()) {
241 assert(!RD->hasFlexibleArrayMember());
242
243 for (const FieldDecl *Field : RD->fields()) {
244 QualType FieldTy = Field->getType();
245 NumRegs += numRegsForType(FieldTy);
246 }
247
248 return NumRegs;
249 }
250
251 return (getContext().getTypeSize(Ty) + 31) / 32;
252}
253
254llvm::Type *AMDGCNSPIRVABIInfo::coerceKernelArgumentType(llvm::Type *Ty,
255 unsigned FromAS,
256 unsigned ToAS) const {
257 // Single value types.
258 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
259 if (PtrTy && PtrTy->getAddressSpace() == FromAS)
260 return llvm::PointerType::get(Ty->getContext(), ToAS);
261 return Ty;
262}
263
264ABIArgInfo AMDGCNSPIRVABIInfo::classifyReturnType(QualType RetTy) const {
265 if (!isAggregateTypeForABI(RetTy) || getRecordArgABI(RetTy, getCXXABI()))
267
268 // Ignore empty structs/unions.
269 if (isEmptyRecord(getContext(), RetTy, true))
270 return ABIArgInfo::getIgnore();
271
272 // Lower single-element structs to just return a regular value.
273 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
274 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
275
276 if (const auto *RD = RetTy->getAsRecordDecl();
277 RD && RD->hasFlexibleArrayMember())
279
280 // Pack aggregates <= 4 bytes into single VGPR or pair.
281 uint64_t Size = getContext().getTypeSize(RetTy);
282 if (Size <= 16)
283 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
284
285 if (Size <= 32)
286 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
287
288 // TODO: This carried over from AMDGPU oddity, we retain it to
289 // ensure consistency, but it might be reasonable to return Int64.
290 if (Size <= 64) {
291 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
292 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
293 }
294
295 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
296 return ABIArgInfo::getDirect();
298}
299
300/// For kernels all parameters are really passed in a special buffer. It doesn't
301/// make sense to pass anything byval, so everything must be direct.
302ABIArgInfo AMDGCNSPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
304
305 // TODO: Can we omit empty structs?
306
307 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
308 Ty = QualType(SeltTy, 0);
309
310 llvm::Type *OrigLTy = CGT.ConvertType(Ty);
311 llvm::Type *LTy = OrigLTy;
312 if (getContext().getLangOpts().isTargetDevice()) {
313 LTy = coerceKernelArgumentType(
314 OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
315 /*ToAS=*/getContext().getTargetAddressSpace(LangAS::opencl_global));
316 }
317
318 // FIXME: This doesn't apply the optimization of coercing pointers in structs
319 // to global address space when using byref. This would require implementing a
320 // new kind of coercion of the in-memory type when for indirect arguments.
321 if (LTy == OrigLTy && isAggregateTypeForABI(Ty)) {
323 getContext().getTypeAlignInChars(Ty),
324 getContext().getTargetAddressSpace(LangAS::opencl_constant),
325 false /*Realign*/, nullptr /*Padding*/);
326 }
327
328 // TODO: inhibiting flattening is an AMDGPU workaround for Clover, which might
329 // be vestigial and should be revisited.
330 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
331}
332
333ABIArgInfo AMDGCNSPIRVABIInfo::classifyArgumentType(QualType Ty) const {
334 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
335
337
338 // TODO: support for variadics.
339
340 if (!isAggregateTypeForABI(Ty)) {
341 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
342 if (!ArgInfo.isIndirect()) {
343 unsigned NumRegs = numRegsForType(Ty);
344 NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
345 }
346
347 return ArgInfo;
348 }
349
350 // Records with non-trivial destructors/copy-constructors should not be
351 // passed by value.
352 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
353 return getNaturalAlignIndirect(Ty, getDataLayout().getAllocaAddrSpace(),
355
356 // Ignore empty structs/unions.
357 if (isEmptyRecord(getContext(), Ty, true))
358 return ABIArgInfo::getIgnore();
359
360 // Lower single-element structs to just pass a regular value. TODO: We
361 // could do reasonable-size multiple-element structs too, using getExpand(),
362 // though watch out for things like bitfields.
363 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
364 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
365
366 if (const auto *RD = Ty->getAsRecordDecl();
367 RD && RD->hasFlexibleArrayMember())
369
370 uint64_t Size = getContext().getTypeSize(Ty);
371 if (Size <= 64) {
372 // Pack aggregates <= 8 bytes into single VGPR or pair.
373 unsigned NumRegs = (Size + 31) / 32;
374 NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
375
376 if (Size <= 16)
377 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
378
379 if (Size <= 32)
380 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
381
382 // TODO: This is an AMDGPU oddity, and might be vestigial, we retain it to
383 // ensure consistency, but it should be revisited.
384 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
385 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
386 }
387
388 if (NumRegsLeft > 0) {
389 unsigned NumRegs = numRegsForType(Ty);
390 if (NumRegsLeft >= NumRegs) {
391 NumRegsLeft -= NumRegs;
392 return ABIArgInfo::getDirect();
393 }
394 }
395
396 // Use pass-by-reference in stead of pass-by-value for struct arguments in
397 // function ABI.
399 getContext().getTypeAlignInChars(Ty),
400 getContext().getTargetAddressSpace(LangAS::opencl_private));
401}
402
403void AMDGCNSPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
404 llvm::CallingConv::ID CC = FI.getCallingConvention();
405
406 if (!getCXXABI().classifyReturnType(FI))
408
409 NumRegsLeft = MaxNumRegsForArgsRet;
410 for (auto &I : FI.arguments()) {
411 if (CC == llvm::CallingConv::SPIR_KERNEL)
412 I.info = classifyKernelArgumentType(I.type);
413 else
414 I.info = classifyArgumentType(I.type);
415 }
416}
417
418llvm::FixedVectorType *AMDGCNSPIRVABIInfo::getOptimalVectorMemoryType(
419 llvm::FixedVectorType *Ty, const LangOptions &LangOpt) const {
420 // AMDGPU has legal instructions for 96-bit so 3x32 can be supported.
421 if (Ty->getNumElements() == 3 && getDataLayout().getTypeSizeInBits(Ty) == 96)
422 return Ty;
423 return DefaultABIInfo::getOptimalVectorMemoryType(Ty, LangOpt);
424}
425
426namespace clang {
427namespace CodeGen {
429 if (CGM.getTarget().getTriple().isSPIRV()) {
430 if (CGM.getTarget().getTriple().getVendor() == llvm::Triple::AMD)
431 AMDGCNSPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
432 else
433 SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
434 } else {
435 CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
436 }
437}
438}
439}
440
441unsigned CommonSPIRTargetCodeGenInfo::getDeviceKernelCallingConv() const {
442 return llvm::CallingConv::SPIR_KERNEL;
443}
444
445void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
446 const FunctionType *&FT) const {
447 // Convert HIP kernels to SPIR-V kernels.
448 if (getABIInfo().getContext().getLangOpts().HIP) {
449 FT = getABIInfo().getContext().adjustFunctionType(
451 return;
452 }
453}
454
455void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention(
456 const FunctionType *&FT) const {
457 FT = getABIInfo().getContext().adjustFunctionType(
459}
460
461// LLVM currently assumes a null pointer has the bit pattern 0, but some GPU
462// targets use a non-zero encoding for null in certain address spaces.
463// Because SPIR(-V) is a generic target and the bit pattern of null in
464// non-generic AS is unspecified, materialize null in non-generic AS via an
465// addrspacecast from null in generic AS. This allows later lowering to
466// substitute the target's real sentinel value.
467llvm::Constant *
468CommonSPIRTargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
469 llvm::PointerType *PT,
470 QualType QT) const {
472 ? LangAS::Default
474 unsigned ASAsInt = static_cast<unsigned>(AS);
475 unsigned FirstTargetASAsInt =
476 static_cast<unsigned>(LangAS::FirstTargetAddressSpace);
477 unsigned CodeSectionINTELAS = FirstTargetASAsInt + 9;
478 // As per SPV_INTEL_function_pointers, it is illegal to addrspacecast
479 // function pointers to/from the generic AS.
480 bool IsFunctionPtrAS =
481 CGM.getTriple().isSPIRV() && ASAsInt == CodeSectionINTELAS;
482 if (AS == LangAS::Default || AS == LangAS::opencl_generic ||
483 AS == LangAS::opencl_constant || IsFunctionPtrAS)
484 return llvm::ConstantPointerNull::get(PT);
485
486 auto &Ctx = CGM.getContext();
487 auto NPT = llvm::PointerType::get(
488 PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic));
489 return llvm::ConstantExpr::getAddrSpaceCast(
490 llvm::ConstantPointerNull::get(NPT), PT);
491}
492
493LangAS
494SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
495 const VarDecl *D) const {
496 assert(!CGM.getLangOpts().OpenCL &&
497 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
498 "Address space agnostic languages only");
499 // If we're here it means that we're using the SPIRDefIsGen ASMap, hence for
500 // the global AS we can rely on either cuda_device or sycl_global to be
501 // correct; however, since this is not a CUDA Device context, we use
502 // sycl_global to prevent confusion with the assertion.
503 LangAS DefaultGlobalAS = getLangASFromTargetAS(
504 CGM.getContext().getTargetAddressSpace(LangAS::sycl_global));
505 if (!D)
506 return DefaultGlobalAS;
507
508 LangAS AddrSpace = D->getType().getAddressSpace();
509 if (AddrSpace != LangAS::Default)
510 return AddrSpace;
511
512 return DefaultGlobalAS;
513}
514
515void SPIRVTargetCodeGenInfo::setTargetAttributes(
516 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
517 if (GV->isDeclaration())
518 return;
519
520 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
521 if (!FD)
522 return;
523
524 llvm::Function *F = dyn_cast<llvm::Function>(GV);
525 assert(F && "Expected GlobalValue to be a Function");
526
527 if (!M.getLangOpts().HIP ||
528 M.getTarget().getTriple().getVendor() != llvm::Triple::AMD)
529 return;
530
531 if (!FD->hasAttr<CUDAGlobalAttr>())
532 return;
533
534 unsigned N = M.getLangOpts().GPUMaxThreadsPerBlock;
535 if (auto FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>())
536 N = FlatWGS->getMax()->EvaluateKnownConstInt(M.getContext()).getExtValue();
537
538 // We encode the maximum flat WG size in the first component of the 3D
539 // max_work_group_size attribute, which will get reverse translated into the
540 // original AMDGPU attribute when targeting AMDGPU.
541 auto Int32Ty = llvm::IntegerType::getInt32Ty(M.getLLVMContext());
542 llvm::Metadata *AttrMDArgs[] = {
543 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, N)),
544 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
545 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1))};
546
547 F->setMetadata("max_work_group_size",
548 llvm::MDNode::get(M.getLLVMContext(), AttrMDArgs));
549}
550
551llvm::SyncScope::ID
552SPIRVTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &, SyncScope Scope,
553 llvm::AtomicOrdering,
554 llvm::LLVMContext &Ctx) const {
555 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(Scope));
556}
557
558/// Construct a SPIR-V target extension type for the given OpenCL image type.
559static llvm::Type *getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType,
560 StringRef OpenCLName,
561 unsigned AccessQualifier) {
562 // These parameters compare to the operands of OpTypeImage (see
563 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage
564 // for more details). The first 6 integer parameters all default to 0, and
565 // will be changed to 1 only for the image type(s) that set the parameter to
566 // one. The 7th integer parameter is the access qualifier, which is tacked on
567 // at the end.
568 SmallVector<unsigned, 7> IntParams = {0, 0, 0, 0, 0, 0};
569
570 // Choose the dimension of the image--this corresponds to the Dim enum in
571 // SPIR-V (first integer parameter of OpTypeImage).
572 if (OpenCLName.starts_with("image2d"))
573 IntParams[0] = 1;
574 else if (OpenCLName.starts_with("image3d"))
575 IntParams[0] = 2;
576 else if (OpenCLName == "image1d_buffer")
577 IntParams[0] = 5; // Buffer
578 else
579 assert(OpenCLName.starts_with("image1d") && "Unknown image type");
580
581 // Set the other integer parameters of OpTypeImage if necessary. Note that the
582 // OpenCL image types don't provide any information for the Sampled or
583 // Image Format parameters.
584 if (OpenCLName.contains("_depth"))
585 IntParams[1] = 1;
586 if (OpenCLName.contains("_array"))
587 IntParams[2] = 1;
588 if (OpenCLName.contains("_msaa"))
589 IntParams[3] = 1;
590
591 // Access qualifier
592 IntParams.push_back(AccessQualifier);
593
594 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
595 IntParams);
596}
597
598llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
599 const Type *Ty) const {
600 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
601 if (auto *PipeTy = dyn_cast<PipeType>(Ty))
602 return llvm::TargetExtType::get(Ctx, "spirv.Pipe", {},
603 {!PipeTy->isReadOnly()});
604 if (auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
605 enum AccessQualifier : unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
606 switch (BuiltinTy->getKind()) {
607#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
608 case BuiltinType::Id: \
609 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
610#include "clang/Basic/OpenCLImageTypes.def"
611 case BuiltinType::OCLSampler:
612 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
613 case BuiltinType::OCLEvent:
614 return llvm::TargetExtType::get(Ctx, "spirv.Event");
615 case BuiltinType::OCLClkEvent:
616 return llvm::TargetExtType::get(Ctx, "spirv.DeviceEvent");
617 case BuiltinType::OCLQueue:
618 return llvm::TargetExtType::get(Ctx, "spirv.Queue");
619 case BuiltinType::OCLReserveID:
620 return llvm::TargetExtType::get(Ctx, "spirv.ReserveId");
621#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
622 case BuiltinType::OCLIntelSubgroupAVC##Id: \
623 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
624#include "clang/Basic/OpenCLExtensionTypes.def"
625 default:
626 return nullptr;
627 }
628 }
629
630 return nullptr;
631}
632
633// Gets a spirv.IntegralConstant or spirv.Literal. If IntegralType is present,
634// returns an IntegralConstant, otherwise returns a Literal.
635static llvm::Type *getInlineSpirvConstant(CodeGenModule &CGM,
636 llvm::Type *IntegralType,
637 llvm::APInt Value) {
638 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
639
640 // Convert the APInt value to an array of uint32_t words
642
643 while (Value.ugt(0)) {
644 uint32_t Word = Value.trunc(32).getZExtValue();
645 Value.lshrInPlace(32);
646
647 Words.push_back(Word);
648 }
649 if (Words.size() == 0)
650 Words.push_back(0);
651
652 if (IntegralType)
653 return llvm::TargetExtType::get(Ctx, "spirv.IntegralConstant",
654 {IntegralType}, Words);
655 return llvm::TargetExtType::get(Ctx, "spirv.Literal", {}, Words);
656}
657
658static llvm::Type *getInlineSpirvType(CodeGenModule &CGM,
659 const HLSLInlineSpirvType *SpirvType) {
660 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
661
663
664 for (auto &Operand : SpirvType->getOperands()) {
665 using SpirvOperandKind = SpirvOperand::SpirvOperandKind;
666
667 llvm::Type *Result = nullptr;
668 switch (Operand.getKind()) {
669 case SpirvOperandKind::ConstantId: {
670 llvm::Type *IntegralType =
671 CGM.getTypes().ConvertType(Operand.getResultType());
672
673 Result = getInlineSpirvConstant(CGM, IntegralType, Operand.getValue());
674 break;
675 }
676 case SpirvOperandKind::Literal: {
677 Result = getInlineSpirvConstant(CGM, nullptr, Operand.getValue());
678 break;
679 }
680 case SpirvOperandKind::TypeId: {
681 QualType TypeOperand = Operand.getResultType();
682 if (const auto *RD = TypeOperand->getAsRecordDecl()) {
683 assert(RD->isCompleteDefinition() &&
684 "Type completion should have been required in Sema");
685
686 const FieldDecl *HandleField = RD->findFirstNamedDataMember();
687 if (HandleField) {
688 QualType ResourceType = HandleField->getType();
689 if (ResourceType->getAs<HLSLAttributedResourceType>()) {
690 TypeOperand = ResourceType;
691 }
692 }
693 }
694 Result = CGM.getTypes().ConvertType(TypeOperand);
695 break;
696 }
697 default:
698 llvm_unreachable("HLSLInlineSpirvType had invalid operand!");
699 break;
700 }
701
702 assert(Result);
703 Operands.push_back(Result);
704 }
705
706 return llvm::TargetExtType::get(Ctx, "spirv.Type", Operands,
707 {SpirvType->getOpcode(), SpirvType->getSize(),
708 SpirvType->getAlignment()});
709}
710
711llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
712 CodeGenModule &CGM, const Type *Ty,
713 const CGHLSLOffsetInfo &OffsetInfo) const {
714 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
715
716 if (auto *SpirvType = dyn_cast<HLSLInlineSpirvType>(Ty))
717 return getInlineSpirvType(CGM, SpirvType);
718
719 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
720 if (!ResType)
721 return nullptr;
722
723 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
724 switch (ResAttrs.ResourceClass) {
725 case llvm::dxil::ResourceClass::UAV:
726 case llvm::dxil::ResourceClass::SRV: {
727 // TypedBuffer and RawBuffer both need element type
728 QualType ContainedTy = ResType->getContainedType();
729 if (ContainedTy.isNull())
730 return nullptr;
731
732 assert(!ResAttrs.IsROV &&
733 "Rasterizer order views not implemented for SPIR-V yet");
734
735 if (!ResAttrs.RawBuffer) {
736 // convert element type
737 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ContainedTy, CGM);
738 }
739
740 if (ResAttrs.IsCounter) {
741 llvm::Type *ElemType = llvm::Type::getInt32Ty(Ctx);
742 uint32_t StorageClass = /* StorageBuffer storage class */ 12;
743 return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {ElemType},
744 {StorageClass, true});
745 }
746 llvm::Type *ElemType = CGM.getTypes().ConvertTypeForMem(ContainedTy);
747 llvm::ArrayType *RuntimeArrayType = llvm::ArrayType::get(ElemType, 0);
748 uint32_t StorageClass = /* StorageBuffer storage class */ 12;
749 bool IsWritable = ResAttrs.ResourceClass == llvm::dxil::ResourceClass::UAV;
750 return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer",
751 {RuntimeArrayType},
752 {StorageClass, IsWritable});
753 }
754 case llvm::dxil::ResourceClass::CBuffer: {
755 QualType ContainedTy = ResType->getContainedType();
756 if (ContainedTy.isNull() || !ContainedTy->isStructureType())
757 return nullptr;
758
759 llvm::StructType *BufferLayoutTy =
760 HLSLBufferLayoutBuilder(CGM).layOutStruct(
761 ContainedTy->getAsCanonical<RecordType>(), OffsetInfo);
762 uint32_t StorageClass = /* Uniform storage class */ 2;
763 return llvm::TargetExtType::get(Ctx, "spirv.VulkanBuffer", {BufferLayoutTy},
764 {StorageClass, false});
765 break;
766 }
767 case llvm::dxil::ResourceClass::Sampler:
768 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
769 }
770 return nullptr;
771}
772
773static unsigned
775 const HLSLAttributedResourceType::Attributes &attributes,
776 llvm::Type *SampledType, QualType Ty, unsigned NumChannels) {
777 // For images with `Sampled` operand equal to 2, there are restrictions on
778 // using the Unknown image format. To avoid these restrictions in common
779 // cases, we guess an image format for them based on the sampled type and the
780 // number of channels. This is intended to match the behaviour of DXC.
781 if (LangOpts.HLSLSpvUseUnknownImageFormat ||
782 attributes.ResourceClass != llvm::dxil::ResourceClass::UAV) {
783 return 0; // Unknown
784 }
785
786 if (SampledType->isIntegerTy(32)) {
787 if (Ty->isSignedIntegerType()) {
788 if (NumChannels == 1)
789 return 24; // R32i
790 if (NumChannels == 2)
791 return 25; // Rg32i
792 if (NumChannels == 4)
793 return 21; // Rgba32i
794 } else {
795 if (NumChannels == 1)
796 return 33; // R32ui
797 if (NumChannels == 2)
798 return 35; // Rg32ui
799 if (NumChannels == 4)
800 return 30; // Rgba32ui
801 }
802 } else if (SampledType->isIntegerTy(64)) {
803 if (NumChannels == 1) {
804 if (Ty->isSignedIntegerType()) {
805 return 41; // R64i
806 }
807 return 40; // R64ui
808 }
809 } else if (SampledType->isFloatTy()) {
810 if (NumChannels == 1)
811 return 3; // R32f
812 if (NumChannels == 2)
813 return 6; // Rg32f
814 if (NumChannels == 4)
815 return 1; // Rgba32f
816 }
817
818 return 0; // Unknown
819}
820
821llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
822 const HLSLAttributedResourceType::Attributes &attributes, QualType Ty,
823 CodeGenModule &CGM) const {
824 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
825
826 unsigned NumChannels = 1;
828 if (const VectorType *V = dyn_cast<VectorType>(Ty)) {
829 NumChannels = V->getNumElements();
830 Ty = V->getElementType();
831 }
832 assert(!Ty->isVectorType() && "We still have a vector type.");
833
834 llvm::Type *SampledType = CGM.getTypes().ConvertTypeForMem(Ty);
835
836 assert((SampledType->isIntegerTy() || SampledType->isFloatingPointTy()) &&
837 "The element type for a SPIR-V resource must be a scalar integer or "
838 "floating point type.");
839
840 // These parameters correspond to the operands to the OpTypeImage SPIR-V
841 // instruction. See
842 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage.
843 SmallVector<unsigned, 6> IntParams(6, 0);
844
845 const char *Name =
846 Ty->isSignedIntegerType() ? "spirv.SignedImage" : "spirv.Image";
847
848 // Dim
849 // For now we assume everything is a buffer.
850 IntParams[0] = 5;
851
852 // Depth
853 // HLSL does not indicate if it is a depth texture or not, so we use unknown.
854 IntParams[1] = 2;
855
856 // Arrayed
857 IntParams[2] = 0;
858
859 // MS
860 IntParams[3] = 0;
861
862 // Sampled
863 IntParams[4] =
864 attributes.ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
865
866 // Image format.
867 IntParams[5] = getImageFormat(CGM.getLangOpts(), attributes, SampledType, Ty,
868 NumChannels);
869
870 llvm::TargetExtType *ImageType =
871 llvm::TargetExtType::get(Ctx, Name, {SampledType}, IntParams);
872 return ImageType;
873}
874
875std::unique_ptr<TargetCodeGenInfo>
877 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.getTypes());
878}
879
880std::unique_ptr<TargetCodeGenInfo>
882 return std::make_unique<SPIRVTargetCodeGenInfo>(CGM.getTypes());
883}
#define V(N, I)
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition CGCall.cpp:360
static llvm::Type * getInlineSpirvType(CodeGenModule &CGM, const HLSLInlineSpirvType *SpirvType)
Definition SPIR.cpp:658
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:559
static unsigned getImageFormat(const LangOptions &LangOpts, const HLSLAttributedResourceType::Attributes &attributes, llvm::Type *SampledType, QualType Ty, unsigned NumChannels)
Definition SPIR.cpp:774
static llvm::Type * getInlineSpirvConstant(CodeGenModule &CGM, llvm::Type *IntegralType, llvm::APInt Value)
Definition SPIR.cpp:635
Defines the clang::LangOptions interface.
unsigned getTargetAddressSpace(LangAS AS) const
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition CharUnits.h:185
static ABIArgInfo getIgnore()
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
static ABIArgInfo getIndirectAliased(CharUnits Alignment, unsigned AddrSpace, bool Realign=false, llvm::Type *Padding=nullptr)
Pass this in memory using the IR byref attribute.
@ 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
const llvm::Triple & getTriple() const
ASTContext & getContext() const
llvm::LLVMContext & getLLVMContext()
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
ABIArgInfo classifyReturnType(QualType RetTy) const
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition TargetInfo.h:49
T * getAttr() const
Definition DeclBase.h:573
bool hasAttr() const
Definition DeclBase.h:577
Represents a member of a struct/union/class.
Definition Decl.h:3160
ExtInfo withCallingConv(CallingConv cc) const
Definition TypeBase.h:4673
ExtInfo getExtInfo() const
Definition TypeBase.h:4806
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8404
bool hasFlexibleArrayMember() const
Definition Decl.h:4354
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.
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:2205
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
CanQualType getCanonicalTypeUnqualified() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:752
bool isVectorType() const
Definition TypeBase.h:8654
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:2921
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9091
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
Definition Type.cpp:653
bool isNullPtrType() const
Definition TypeBase.h:8908
QualType getType() const
Definition Decl.h:723
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
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:428
bool isAggregateTypeForABI(QualType T)
const Type * isSingleElementStruct(QualType T, ASTContext &Context)
isSingleElementStruct - Determine if a structure is a "singleelement struct", i.e.
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
Definition SPIR.cpp:881
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
Definition SPIR.cpp:876
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
The JSON file list parser is used to communicate input to InstallAPI.
StorageClass
Storage classes.
Definition Specifiers.h:248
const FunctionProtoType * T
@ Type
The name was classified as a type.
Definition Sema.h:562
LangAS
Defines the address space values used by the address space qualifier of QualType.
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)
unsigned long uint64_t
unsigned int uint32_t