10#include "TargetInfo.h"
29class SPIRVABIInfo :
public CommonSPIRABIInfo {
31 SPIRVABIInfo(
CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
45 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo>
ABIInfo)
50 getABIInfo().getDataLayout().getAllocaAddrSpace());
56 llvm::Type *getSPIRVImageTypeFromHLSLResource(
58 llvm::Type *ElementType, llvm::LLVMContext &Ctx)
const;
60class SPIRVTargetCodeGenInfo :
public CommonSPIRTargetCodeGenInfo {
63 : CommonSPIRTargetCodeGenInfo(
std::make_unique<SPIRVABIInfo>(CGT)) {}
67 void setTargetAttributes(
const Decl *
D, llvm::GlobalValue *GV,
69 llvm::SyncScope::ID getLLVMSyncScopeID(
const LangOptions &LangOpts,
71 llvm::AtomicOrdering Ordering,
72 llvm::LLVMContext &Ctx)
const override;
77 case SyncScope::HIPSingleThread:
78 case SyncScope::SingleScope:
79 return "singlethread";
80 case SyncScope::HIPWavefront:
81 case SyncScope::OpenCLSubGroup:
82 case SyncScope::WavefrontScope:
84 case SyncScope::HIPWorkgroup:
85 case SyncScope::OpenCLWorkGroup:
86 case SyncScope::WorkgroupScope:
88 case SyncScope::HIPAgent:
89 case SyncScope::OpenCLDevice:
90 case SyncScope::DeviceScope:
92 case SyncScope::SystemScope:
93 case SyncScope::HIPSystem:
94 case SyncScope::OpenCLAllSVMDevices:
101void CommonSPIRABIInfo::setCCs() {
102 assert(getRuntimeCC() == llvm::CallingConv::C);
103 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
107 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
128 if (getContext().getLangOpts().CUDAIsDevice) {
132 llvm::Type *LTy = CGT.ConvertType(Ty);
133 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
134 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
135 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
136 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
137 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
142 if (getTarget().getTriple().getVendor() == llvm::Triple::AMD)
160 return getNaturalAlignIndirect(Ty,
true);
167 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
195 if (CC == llvm::CallingConv::SPIR_KERNEL) {
196 I.info = classifyKernelArgumentType(I.type);
207 SPIRVABIInfo(CGM.
getTypes()).computeInfo(FI);
209 CommonSPIRABIInfo(CGM.
getTypes()).computeInfo(FI);
214unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv()
const {
215 return llvm::CallingConv::SPIR_KERNEL;
218void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
221 if (getABIInfo().getContext().getLangOpts().
HIP) {
222 FT = getABIInfo().getContext().adjustFunctionType(
229SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(
CodeGenModule &CGM,
233 "Address space agnostic languages only");
241 return DefaultGlobalAS;
243 LangAS AddrSpace =
D->getType().getAddressSpace();
244 if (AddrSpace != LangAS::Default)
247 return DefaultGlobalAS;
250void SPIRVTargetCodeGenInfo::setTargetAttributes(
255 if (GV->isDeclaration())
258 auto F = dyn_cast<llvm::Function>(GV);
262 auto FD = dyn_cast_or_null<FunctionDecl>(
D);
265 if (!FD->hasAttr<CUDAGlobalAttr>())
268 unsigned N = M.
getLangOpts().GPUMaxThreadsPerBlock;
269 if (
auto FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>())
270 N = FlatWGS->getMax()->EvaluateKnownConstInt(M.
getContext()).getExtValue();
276 llvm::Metadata *AttrMDArgs[] = {
277 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, N)),
278 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1)),
279 llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(Int32Ty, 1))};
281 F->setMetadata(
"max_work_group_size",
287 llvm::AtomicOrdering,
288 llvm::LLVMContext &Ctx)
const {
289 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(
Scope));
294 StringRef OpenCLName,
295 unsigned AccessQualifier) {
306 if (OpenCLName.starts_with(
"image2d"))
308 else if (OpenCLName.starts_with(
"image3d"))
310 else if (OpenCLName ==
"image1d_buffer")
313 assert(OpenCLName.starts_with(
"image1d") &&
"Unknown image type");
318 if (OpenCLName.contains(
"_depth"))
320 if (OpenCLName.contains(
"_array"))
322 if (OpenCLName.contains(
"_msaa"))
326 IntParams.push_back(AccessQualifier);
328 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
332llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(
CodeGenModule &CGM,
333 const Type *Ty)
const {
335 if (
auto *PipeTy = dyn_cast<PipeType>(Ty))
336 return llvm::TargetExtType::get(Ctx,
"spirv.Pipe", {},
337 {!PipeTy->isReadOnly()});
338 if (
auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
339 enum AccessQualifier :
unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
340 switch (BuiltinTy->getKind()) {
341#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
342 case BuiltinType::Id: \
343 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
344#include "clang/Basic/OpenCLImageTypes.def"
345 case BuiltinType::OCLSampler:
346 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
347 case BuiltinType::OCLEvent:
348 return llvm::TargetExtType::get(Ctx,
"spirv.Event");
349 case BuiltinType::OCLClkEvent:
350 return llvm::TargetExtType::get(Ctx,
"spirv.DeviceEvent");
351 case BuiltinType::OCLQueue:
352 return llvm::TargetExtType::get(Ctx,
"spirv.Queue");
353 case BuiltinType::OCLReserveID:
354 return llvm::TargetExtType::get(Ctx,
"spirv.ReserveId");
355#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
356 case BuiltinType::OCLIntelSubgroupAVC##Id: \
357 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
358#include "clang/Basic/OpenCLExtensionTypes.def"
367llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
CodeGenModule &CGM,
368 const Type *Ty)
const {
369 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
376 case llvm::dxil::ResourceClass::UAV:
377 case llvm::dxil::ResourceClass::SRV: {
379 QualType ContainedTy = ResType->getContainedType();
384 "Raw buffers handles are not implemented for SPIR-V yet");
385 assert(!ResAttrs.
IsROV &&
386 "Rasterizer order views not implemented for SPIR-V yet");
390 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ElemType, Ctx);
392 case llvm::dxil::ResourceClass::CBuffer:
393 llvm_unreachable(
"CBuffer handles are not implemented for SPIR-V yet");
395 case llvm::dxil::ResourceClass::Sampler:
396 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
401llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
403 llvm::Type *ElementType, llvm::LLVMContext &Ctx)
const {
405 if (ElementType->isVectorTy())
406 ElementType = ElementType->getScalarType();
408 assert((ElementType->isIntegerTy() || ElementType->isFloatingPointTy()) &&
409 "The element type for a SPIR-V resource must be a scalar integer or "
410 "floating point type.");
433 attributes.
ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
439 return llvm::TargetExtType::get(Ctx,
"spirv.Image", {ElementType}, IntParams);
442std::unique_ptr<TargetCodeGenInfo>
444 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.
getTypes());
447std::unique_ptr<TargetCodeGenInfo>
449 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 * getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType, StringRef OpenCLName, unsigned AccessQualifier)
Construct a SPIR-V target extension type for the given OpenCL image type.
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.
@ 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()
This class organizes the cross-module state that is used while lowering AST types to LLVM types.
llvm::Type * ConvertType(QualType T)
ConvertType - 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,...
virtual llvm::Type * getOpenCLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to an OpenCL type.
virtual llvm::Type * getHLSLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to a HLSL type.
const T & getABIInfo() const
virtual unsigned getOpenCLKernelCallingConv() const
Get LLVM calling convention for OpenCL kernel.
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Decl - This represents one declaration (or definition), e.g.
ExtInfo withCallingConv(CallingConv cc) const
FunctionType - C99 6.7.5.3 - Function Declarators.
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.
Represents a struct/union/class.
bool hasFlexibleArrayMember() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
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.
The base class of the type hierarchy.
const T * getAs() const
Member-template getAs<specific type>'.
Represents a variable declaration or definition.
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)
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.
LangAS
Defines the address space values used by the address space qualifier of QualType.
const FunctionProtoType * T
SyncScope
Defines synch scope values used internally by clang.
LangAS getLangASFromTargetAS(unsigned TargetAS)
llvm::dxil::ResourceClass ResourceClass