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 llvm::SyncScope::ID getLLVMSyncScopeID(
const LangOptions &LangOpts,
69 llvm::AtomicOrdering Ordering,
70 llvm::LLVMContext &Ctx)
const override;
75 case SyncScope::HIPSingleThread:
76 case SyncScope::SingleScope:
77 return "singlethread";
78 case SyncScope::HIPWavefront:
79 case SyncScope::OpenCLSubGroup:
80 case SyncScope::WavefrontScope:
82 case SyncScope::HIPWorkgroup:
83 case SyncScope::OpenCLWorkGroup:
84 case SyncScope::WorkgroupScope:
86 case SyncScope::HIPAgent:
87 case SyncScope::OpenCLDevice:
88 case SyncScope::DeviceScope:
90 case SyncScope::SystemScope:
91 case SyncScope::HIPSystem:
92 case SyncScope::OpenCLAllSVMDevices:
99void CommonSPIRABIInfo::setCCs() {
100 assert(getRuntimeCC() == llvm::CallingConv::C);
101 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
105 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
126 if (getContext().getLangOpts().CUDAIsDevice) {
130 llvm::Type *LTy = CGT.ConvertType(Ty);
131 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
132 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
133 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
134 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
135 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
140 if (getTarget().getTriple().getVendor() == llvm::Triple::AMD)
158 return getNaturalAlignIndirect(Ty,
true);
165 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
193 if (CC == llvm::CallingConv::SPIR_KERNEL) {
194 I.info = classifyKernelArgumentType(I.type);
205 SPIRVABIInfo(CGM.
getTypes()).computeInfo(FI);
207 CommonSPIRABIInfo(CGM.
getTypes()).computeInfo(FI);
212unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv()
const {
213 return llvm::CallingConv::SPIR_KERNEL;
216void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
219 if (getABIInfo().getContext().getLangOpts().
HIP) {
220 FT = getABIInfo().getContext().adjustFunctionType(
227SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(
CodeGenModule &CGM,
231 "Address space agnostic languages only");
239 return DefaultGlobalAS;
241 LangAS AddrSpace =
D->getType().getAddressSpace();
242 if (AddrSpace != LangAS::Default)
245 return DefaultGlobalAS;
250 llvm::AtomicOrdering,
251 llvm::LLVMContext &Ctx)
const {
252 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(
Scope));
257 StringRef OpenCLName,
258 unsigned AccessQualifier) {
269 if (OpenCLName.starts_with(
"image2d"))
271 else if (OpenCLName.starts_with(
"image3d"))
273 else if (OpenCLName ==
"image1d_buffer")
276 assert(OpenCLName.starts_with(
"image1d") &&
"Unknown image type");
281 if (OpenCLName.contains(
"_depth"))
283 if (OpenCLName.contains(
"_array"))
285 if (OpenCLName.contains(
"_msaa"))
289 IntParams.push_back(AccessQualifier);
291 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
295llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(
CodeGenModule &CGM,
296 const Type *Ty)
const {
298 if (
auto *PipeTy = dyn_cast<PipeType>(Ty))
299 return llvm::TargetExtType::get(Ctx,
"spirv.Pipe", {},
300 {!PipeTy->isReadOnly()});
301 if (
auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
302 enum AccessQualifier :
unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
303 switch (BuiltinTy->getKind()) {
304#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
305 case BuiltinType::Id: \
306 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
307#include "clang/Basic/OpenCLImageTypes.def"
308 case BuiltinType::OCLSampler:
309 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
310 case BuiltinType::OCLEvent:
311 return llvm::TargetExtType::get(Ctx,
"spirv.Event");
312 case BuiltinType::OCLClkEvent:
313 return llvm::TargetExtType::get(Ctx,
"spirv.DeviceEvent");
314 case BuiltinType::OCLQueue:
315 return llvm::TargetExtType::get(Ctx,
"spirv.Queue");
316 case BuiltinType::OCLReserveID:
317 return llvm::TargetExtType::get(Ctx,
"spirv.ReserveId");
318#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
319 case BuiltinType::OCLIntelSubgroupAVC##Id: \
320 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
321#include "clang/Basic/OpenCLExtensionTypes.def"
330llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(
CodeGenModule &CGM,
331 const Type *Ty)
const {
332 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
339 case llvm::dxil::ResourceClass::UAV:
340 case llvm::dxil::ResourceClass::SRV: {
342 QualType ContainedTy = ResType->getContainedType();
347 "Raw buffers handles are not implemented for SPIR-V yet");
348 assert(!ResAttrs.
IsROV &&
349 "Rasterizer order views not implemented for SPIR-V yet");
353 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ElemType, Ctx);
355 case llvm::dxil::ResourceClass::CBuffer:
356 llvm_unreachable(
"CBuffer handles are not implemented for SPIR-V yet");
358 case llvm::dxil::ResourceClass::Sampler:
359 return llvm::TargetExtType::get(Ctx,
"spirv.Sampler");
364llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
366 llvm::Type *ElementType, llvm::LLVMContext &Ctx)
const {
368 if (ElementType->isVectorTy())
369 ElementType = ElementType->getScalarType();
371 assert((ElementType->isIntegerTy() || ElementType->isFloatingPointTy()) &&
372 "The element type for a SPIR-V resource must be a scalar integer or "
373 "floating point type.");
396 attributes.
ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
402 return llvm::TargetExtType::get(Ctx,
"spirv.Image", {ElementType}, IntParams);
405std::unique_ptr<TargetCodeGenInfo>
407 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.
getTypes());
410std::unique_ptr<TargetCodeGenInfo>
412 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.
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