11#include "TargetInfo.h"
24 CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
30class SPIRVABIInfo :
public CommonSPIRABIInfo {
32 SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
33 void computeInfo(CGFunctionInfo &FI)
const override;
37 ABIArgInfo classifyKernelArgumentType(QualType Ty)
const;
44 CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
45 : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
46 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
47 : TargetCodeGenInfo(std::move(ABIInfo)) {}
49 LangAS getASTAllocaAddressSpace()
const override {
51 getABIInfo().getDataLayout().getAllocaAddrSpace());
54 unsigned getDeviceKernelCallingConv()
const override;
55 llvm::Type *getOpenCLType(CodeGenModule &CGM,
const Type *
T)
const override;
57 getHLSLType(CodeGenModule &CGM,
const Type *Ty,
58 const SmallVector<int32_t> *Packoffsets =
nullptr)
const override;
59 llvm::Type *getSPIRVImageTypeFromHLSLResource(
60 const HLSLAttributedResourceType::Attributes &attributes,
61 QualType SampledType, CodeGenModule &CGM)
const;
63 setOCLKernelStubCallingConvention(
const FunctionType *&FT)
const override;
64 llvm::Constant *getNullPointer(
const CodeGen::CodeGenModule &CGM,
66 QualType QT)
const override;
68class SPIRVTargetCodeGenInfo :
public CommonSPIRTargetCodeGenInfo {
70 SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
71 : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
73 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
74 const VarDecl *D)
const override;
75 void setTargetAttributes(
const Decl *D, llvm::GlobalValue *GV,
76 CodeGen::CodeGenModule &M)
const override;
77 llvm::SyncScope::ID getLLVMSyncScopeID(
const LangOptions &LangOpts,
79 llvm::AtomicOrdering Ordering,
80 llvm::LLVMContext &Ctx)
const override;
81 bool supportsLibCall()
const override {
82 return getABIInfo().getTarget().getTriple().getVendor() !=
91 return "singlethread";
113void CommonSPIRABIInfo::setCCs() {
114 assert(getRuntimeCC() == llvm::CallingConv::C);
115 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
118ABIArgInfo SPIRVABIInfo::classifyReturnType(QualType RetTy)
const {
119 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
137ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty)
const {
138 if (getContext().getLangOpts().isTargetDevice()) {
143 llvm::Type *LTy = CGT.ConvertType(Ty);
144 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
145 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::opencl_global);
146 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
147 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
148 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
153 if (getTarget().getTriple().getVendor() == llvm::Triple::AMD)
173 return getNaturalAlignIndirect(Ty, 0,
true);
179ABIArgInfo SPIRVABIInfo::classifyArgumentType(QualType Ty)
const {
180 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
188 return getNaturalAlignIndirect(Ty, getDataLayout().getAllocaAddrSpace(),
198void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI)
const {
207 if (CC == llvm::CallingConv::SPIR_KERNEL) {
208 I.info = classifyKernelArgumentType(I.type);
219 SPIRVABIInfo(CGM.
getTypes()).computeInfo(FI);
221 CommonSPIRABIInfo(CGM.
getTypes()).computeInfo(FI);
226unsigned CommonSPIRTargetCodeGenInfo::getDeviceKernelCallingConv()
const {
227 return llvm::CallingConv::SPIR_KERNEL;
230void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
231 const FunctionType *&FT)
const {
233 if (getABIInfo().getContext().getLangOpts().
HIP) {
234 FT = getABIInfo().getContext().adjustFunctionType(
240void CommonSPIRTargetCodeGenInfo::setOCLKernelStubCallingConvention(
241 const FunctionType *&FT)
const {
242 FT = getABIInfo().getContext().adjustFunctionType(
253CommonSPIRTargetCodeGenInfo::getNullPointer(
const CodeGen::CodeGenModule &CGM,
254 llvm::PointerType *PT,
259 if (AS == LangAS::Default || AS == LangAS::opencl_generic)
260 return llvm::ConstantPointerNull::get(PT);
263 auto NPT = llvm::PointerType::get(
264 PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic));
265 return llvm::ConstantExpr::getAddrSpaceCast(
266 llvm::ConstantPointerNull::get(NPT), PT);
270SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
271 const VarDecl *D)
const {
274 "Address space agnostic languages only");
282 return DefaultGlobalAS;
285 if (AddrSpace != LangAS::Default)
288 return DefaultGlobalAS;
291void SPIRVTargetCodeGenInfo::setTargetAttributes(
292 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M)
const {
296 if (GV->isDeclaration())
299 auto F = dyn_cast<llvm::Function>(GV);
303 auto FD = dyn_cast_or_null<FunctionDecl>(D);
306 if (!FD->hasAttr<CUDAGlobalAttr>())
309 unsigned N = M.
getLangOpts().GPUMaxThreadsPerBlock;
310 if (
auto FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>())
311 N = FlatWGS->getMax()->EvaluateKnownConstInt(M.
getContext()).getExtValue();
317 llvm::Metadata *AttrMDArgs[] = {
318 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, N)),
319 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
320 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1))};
322 F->setMetadata(
"max_work_group_size",
327SPIRVTargetCodeGenInfo::getLLVMSyncScopeID(
const LangOptions &,
SyncScope Scope,
328 llvm::AtomicOrdering,
329 llvm::LLVMContext &Ctx)
const {
330 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(Scope));
335 StringRef OpenCLName,
336 unsigned AccessQualifier) {
347 if (OpenCLName.starts_with(
"image2d"))
349 else if (OpenCLName.starts_with(
"image3d"))
351 else if (OpenCLName ==
"image1d_buffer")
354 assert(OpenCLName.starts_with(
"image1d") &&
"Unknown image type");
359 if (OpenCLName.contains(
"_depth"))
361 if (OpenCLName.contains(
"_array"))
363 if (OpenCLName.contains(
"_msaa"))
367 IntParams.push_back(AccessQualifier);
369 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
373llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
374 const Type *Ty)
const {
376 if (
auto *PipeTy = dyn_cast<PipeType>(Ty))
377 return llvm::TargetExtType::get(Ctx,
"spirv.Pipe", {},
378 {!PipeTy->isReadOnly()});
379 if (
auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
380 enum AccessQualifier :
unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
381 switch (BuiltinTy->getKind()) {
382#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
383 case BuiltinType::Id: \
384 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
385#include "clang/Basic/OpenCLImageTypes.def"
386 case BuiltinType::OCLSampler:
387 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
388 case BuiltinType::OCLEvent:
389 return llvm::TargetExtType::get(Ctx,
"spirv.Event");
390 case BuiltinType::OCLClkEvent:
391 return llvm::TargetExtType::get(Ctx,
"spirv.DeviceEvent");
392 case BuiltinType::OCLQueue:
393 return llvm::TargetExtType::get(Ctx,
"spirv.Queue");
394 case BuiltinType::OCLReserveID:
395 return llvm::TargetExtType::get(Ctx,
"spirv.ReserveId");
396#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
397 case BuiltinType::OCLIntelSubgroupAVC##Id: \
398 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
399#include "clang/Basic/OpenCLExtensionTypes.def"
411 llvm::Type *IntegralType,
418 while (
Value.ugt(0)) {
419 uint32_t Word =
Value.trunc(32).getZExtValue();
420 Value.lshrInPlace(32);
422 Words.push_back(Word);
424 if (Words.size() == 0)
428 return llvm::TargetExtType::get(Ctx,
"spirv.IntegralConstant",
429 {IntegralType}, Words);
430 return llvm::TargetExtType::get(Ctx,
"spirv.Literal", {}, Words);
434 const HLSLInlineSpirvType *SpirvType) {
439 for (
auto &Operand : SpirvType->getOperands()) {
440 using SpirvOperandKind = SpirvOperand::SpirvOperandKind;
442 llvm::Type *Result =
nullptr;
443 switch (Operand.getKind()) {
444 case SpirvOperandKind::ConstantId: {
445 llvm::Type *IntegralType =
451 case SpirvOperandKind::Literal: {
455 case SpirvOperandKind::TypeId: {
456 QualType TypeOperand = Operand.getResultType();
458 assert(RD->isCompleteDefinition() &&
459 "Type completion should have been required in Sema");
461 const FieldDecl *HandleField = RD->findFirstNamedDataMember();
464 if (ResourceType->
getAs<HLSLAttributedResourceType>()) {
465 TypeOperand = ResourceType;
473 llvm_unreachable(
"HLSLInlineSpirvType had invalid operand!");
478 Operands.push_back(Result);
481 return llvm::TargetExtType::get(Ctx,
"spirv.Type", Operands,
482 {SpirvType->getOpcode(), SpirvType->getSize(),
483 SpirvType->getAlignment()});
486llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
487 CodeGenModule &CGM,
const Type *Ty,
488 const SmallVector<int32_t> *Packoffsets)
const {
491 if (
auto *SpirvType = dyn_cast<HLSLInlineSpirvType>(Ty))
494 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
498 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
499 switch (ResAttrs.ResourceClass) {
500 case llvm::dxil::ResourceClass::UAV:
501 case llvm::dxil::ResourceClass::SRV: {
503 QualType ContainedTy = ResType->getContainedType();
507 assert(!ResAttrs.IsROV &&
508 "Rasterizer order views not implemented for SPIR-V yet");
510 if (!ResAttrs.RawBuffer) {
512 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ContainedTy, CGM);
515 if (ResAttrs.IsCounter) {
516 llvm::Type *ElemType = llvm::Type::getInt32Ty(Ctx);
518 return llvm::TargetExtType::get(Ctx,
"spirv.VulkanBuffer", {ElemType},
522 llvm::ArrayType *RuntimeArrayType = llvm::ArrayType::get(ElemType, 0);
524 bool IsWritable = ResAttrs.ResourceClass == llvm::dxil::ResourceClass::UAV;
525 return llvm::TargetExtType::get(Ctx,
"spirv.VulkanBuffer",
529 case llvm::dxil::ResourceClass::CBuffer: {
530 QualType ContainedTy = ResType->getContainedType();
534 llvm::Type *BufferLayoutTy =
535 HLSLBufferLayoutBuilder(CGM,
"spirv.Layout")
539 return llvm::TargetExtType::get(Ctx,
"spirv.VulkanBuffer", {BufferLayoutTy},
543 case llvm::dxil::ResourceClass::Sampler:
544 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
551 const HLSLAttributedResourceType::Attributes &attributes,
552 llvm::Type *SampledType,
QualType Ty,
unsigned NumChannels) {
557 if (LangOpts.HLSLSpvUseUnknownImageFormat ||
558 attributes.ResourceClass != llvm::dxil::ResourceClass::UAV) {
562 if (SampledType->isIntegerTy(32)) {
564 if (NumChannels == 1)
566 if (NumChannels == 2)
568 if (NumChannels == 4)
571 if (NumChannels == 1)
573 if (NumChannels == 2)
575 if (NumChannels == 4)
578 }
else if (SampledType->isIntegerTy(64)) {
579 if (NumChannels == 1) {
585 }
else if (SampledType->isFloatTy()) {
586 if (NumChannels == 1)
588 if (NumChannels == 2)
590 if (NumChannels == 4)
597llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
598 const HLSLAttributedResourceType::Attributes &attributes, QualType Ty,
599 CodeGenModule &CGM)
const {
602 unsigned NumChannels = 1;
604 if (
const VectorType *
V = dyn_cast<VectorType>(Ty)) {
605 NumChannels =
V->getNumElements();
606 Ty =
V->getElementType();
608 assert(!Ty->
isVectorType() &&
"We still have a vector type.");
612 assert((SampledType->isIntegerTy() || SampledType->isFloatingPointTy()) &&
613 "The element type for a SPIR-V resource must be a scalar integer or "
614 "floating point type.");
619 SmallVector<unsigned, 6> IntParams(6, 0);
640 attributes.ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
646 llvm::TargetExtType *ImageType =
647 llvm::TargetExtType::get(Ctx, Name, {SampledType}, IntParams);
651std::unique_ptr<TargetCodeGenInfo>
653 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.
getTypes());
656std::unique_ptr<TargetCodeGenInfo>
658 return std::make_unique<SPIRVTargetCodeGenInfo>(CGM.
getTypes());
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
static llvm::Type * getInlineSpirvType(CodeGenModule &CGM, const HLSLInlineSpirvType *SpirvType)
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.
static unsigned getImageFormat(const LangOptions &LangOpts, const HLSLAttributedResourceType::Attributes &attributes, llvm::Type *SampledType, QualType Ty, unsigned NumChannels)
static llvm::Type * getInlineSpirvConstant(CodeGenModule &CGM, llvm::Type *IntegralType, llvm::APInt Value)
unsigned getTargetAddressSpace(LangAS AS) const
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
@ RAA_DirectInMemory
Pass it on the stack using its defined layout.
CGFunctionInfo - Class to encapsulate the information about a function definition.
ABIArgInfo & getReturnInfo()
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
CodeGenTypes & getTypes()
const TargetInfo & getTarget() 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.
ABIArgInfo classifyArgumentType(QualType RetTy) const
ABIArgInfo classifyReturnType(QualType RetTy) const
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Represents a member of a struct/union/class.
ExtInfo withCallingConv(CallingConv cc) const
ExtInfo getExtInfo() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
LangAS getAddressSpace() const
Return the address space of this type.
bool hasFlexibleArrayMember() const
Scope - A scope is a transient data structure that is used while parsing the program.
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
bool isStructureType() const
bool isSignedIntegerType() const
Return true if this is an integer type that is signed, according to C99 6.2.5p4 [char,...
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
CanQualType getCanonicalTypeUnqualified() const
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isVectorType() const
const T * castAsCanonical() const
Return this type's canonical type cast to the specified type.
const T * getAs() const
Member-template getAs<specific type>'.
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
bool isNullPtrType() const
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 ...
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
bool isAggregateTypeForABI(QualType T)
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
The JSON file list parser is used to communicate input to InstallAPI.
StorageClass
Storage classes.
const FunctionProtoType * T
@ Type
The name was classified as a type.
LangAS
Defines the address space values used by the address space qualifier of QualType.
SyncScope
Defines sync scope values used internally by clang.
LangAS getLangASFromTargetAS(unsigned TargetAS)