clang 20.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"
10#include "TargetInfo.h"
11
12using namespace clang;
13using namespace clang::CodeGen;
14
15//===----------------------------------------------------------------------===//
16// Base ABI and target codegen info implementation common between SPIR and
17// SPIR-V.
18//===----------------------------------------------------------------------===//
19
20namespace {
21class CommonSPIRABIInfo : public DefaultABIInfo {
22public:
23 CommonSPIRABIInfo(CodeGenTypes &CGT) : DefaultABIInfo(CGT) { setCCs(); }
24
25private:
26 void setCCs();
27};
28
29class SPIRVABIInfo : public CommonSPIRABIInfo {
30public:
31 SPIRVABIInfo(CodeGenTypes &CGT) : CommonSPIRABIInfo(CGT) {}
32 void computeInfo(CGFunctionInfo &FI) const override;
33
34private:
36 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
38};
39} // end anonymous namespace
40namespace {
41class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
42public:
43 CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
44 : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
45 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
46 : TargetCodeGenInfo(std::move(ABIInfo)) {}
47
48 LangAS getASTAllocaAddressSpace() const override {
50 getABIInfo().getDataLayout().getAllocaAddrSpace());
51 }
52
53 unsigned getOpenCLKernelCallingConv() const override;
54 llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const override;
55 llvm::Type *getHLSLType(CodeGenModule &CGM, const Type *Ty) const override;
56 llvm::Type *getSPIRVImageTypeFromHLSLResource(
58 llvm::Type *ElementType, llvm::LLVMContext &Ctx) const;
59};
60class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
61public:
62 SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
63 : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
64 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
65 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
66 const VarDecl *D) const override;
67 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
69 llvm::AtomicOrdering Ordering,
70 llvm::LLVMContext &Ctx) const override;
71};
72
73inline StringRef mapClangSyncScopeToLLVM(SyncScope Scope) {
74 switch (Scope) {
75 case SyncScope::HIPSingleThread:
76 case SyncScope::SingleScope:
77 return "singlethread";
78 case SyncScope::HIPWavefront:
79 case SyncScope::OpenCLSubGroup:
80 case SyncScope::WavefrontScope:
81 return "subgroup";
82 case SyncScope::HIPWorkgroup:
83 case SyncScope::OpenCLWorkGroup:
84 case SyncScope::WorkgroupScope:
85 return "workgroup";
86 case SyncScope::HIPAgent:
87 case SyncScope::OpenCLDevice:
88 case SyncScope::DeviceScope:
89 return "device";
90 case SyncScope::SystemScope:
91 case SyncScope::HIPSystem:
92 case SyncScope::OpenCLAllSVMDevices:
93 return "";
94 }
95 return "";
96}
97} // End anonymous namespace.
98
99void CommonSPIRABIInfo::setCCs() {
100 assert(getRuntimeCC() == llvm::CallingConv::C);
101 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
102}
103
104ABIArgInfo SPIRVABIInfo::classifyReturnType(QualType RetTy) const {
105 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
107 if (!isAggregateTypeForABI(RetTy) || getRecordArgABI(RetTy, getCXXABI()))
109
110 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
111 const RecordDecl *RD = RT->getDecl();
112 if (RD->hasFlexibleArrayMember())
114 }
115
116 // TODO: The AMDGPU ABI is non-trivial to represent in SPIR-V; in order to
117 // avoid encoding various architecture specific bits here we return everything
118 // as direct to retain type info for things like aggregates, for later perusal
119 // when translating back to LLVM/lowering in the BE. This is also why we
120 // disable flattening as the outcomes can mismatch between SPIR-V and AMDGPU.
121 // This will be revisited / optimised in the future.
122 return ABIArgInfo::getDirect(CGT.ConvertType(RetTy), 0u, nullptr, false);
123}
124
125ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
126 if (getContext().getLangOpts().CUDAIsDevice) {
127 // Coerce pointer arguments with default address space to CrossWorkGroup
128 // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
129 // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
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);
136 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
137 }
138
139 if (isAggregateTypeForABI(Ty)) {
140 if (getTarget().getTriple().getVendor() == llvm::Triple::AMD)
141 // TODO: The AMDGPU kernel ABI passes aggregates byref, which is not
142 // currently expressible in SPIR-V; SPIR-V passes aggregates byval,
143 // which the AMDGPU kernel ABI does not allow. Passing aggregates as
144 // direct works around this impedance mismatch, as it retains type info
145 // and can be correctly handled, post reverse-translation, by the AMDGPU
146 // BE, which has to support this CC for legacy OpenCL purposes. It can
147 // be brittle and does lead to performance degradation in certain
148 // pathological cases. This will be revisited / optimised in the future,
149 // once a way to deal with the byref/byval impedance mismatch is
150 // identified.
151 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
152 // Force copying aggregate type in kernel arguments by value when
153 // compiling CUDA targeting SPIR-V. This is required for the object
154 // copied to be valid on the device.
155 // This behavior follows the CUDA spec
156 // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
157 // and matches the NVPTX implementation.
158 return getNaturalAlignIndirect(Ty, /* byval */ true);
159 }
160 }
161 return classifyArgumentType(Ty);
162}
163
164ABIArgInfo SPIRVABIInfo::classifyArgumentType(QualType Ty) const {
165 if (getTarget().getTriple().getVendor() != llvm::Triple::AMD)
167 if (!isAggregateTypeForABI(Ty))
169
170 // Records with non-trivial destructors/copy-constructors should not be
171 // passed by value.
172 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
173 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
174
175 if (const RecordType *RT = Ty->getAs<RecordType>()) {
176 const RecordDecl *RD = RT->getDecl();
177 if (RD->hasFlexibleArrayMember())
179 }
180
181 return ABIArgInfo::getDirect(CGT.ConvertType(Ty), 0u, nullptr, false);
182}
183
184void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
185 // The logic is same as in DefaultABIInfo with an exception on the kernel
186 // arguments handling.
187 llvm::CallingConv::ID CC = FI.getCallingConvention();
188
189 if (!getCXXABI().classifyReturnType(FI))
191
192 for (auto &I : FI.arguments()) {
193 if (CC == llvm::CallingConv::SPIR_KERNEL) {
194 I.info = classifyKernelArgumentType(I.type);
195 } else {
196 I.info = classifyArgumentType(I.type);
197 }
198 }
199}
200
201namespace clang {
202namespace CodeGen {
204 if (CGM.getTarget().getTriple().isSPIRV())
205 SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
206 else
207 CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
208}
209}
210}
211
212unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
213 return llvm::CallingConv::SPIR_KERNEL;
214}
215
216void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
217 const FunctionType *&FT) const {
218 // Convert HIP kernels to SPIR-V kernels.
219 if (getABIInfo().getContext().getLangOpts().HIP) {
220 FT = getABIInfo().getContext().adjustFunctionType(
222 return;
223 }
224}
225
226LangAS
227SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
228 const VarDecl *D) const {
229 assert(!CGM.getLangOpts().OpenCL &&
230 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
231 "Address space agnostic languages only");
232 // If we're here it means that we're using the SPIRDefIsGen ASMap, hence for
233 // the global AS we can rely on either cuda_device or sycl_global to be
234 // correct; however, since this is not a CUDA Device context, we use
235 // sycl_global to prevent confusion with the assertion.
236 LangAS DefaultGlobalAS = getLangASFromTargetAS(
237 CGM.getContext().getTargetAddressSpace(LangAS::sycl_global));
238 if (!D)
239 return DefaultGlobalAS;
240
241 LangAS AddrSpace = D->getType().getAddressSpace();
242 if (AddrSpace != LangAS::Default)
243 return AddrSpace;
244
245 return DefaultGlobalAS;
246}
247
248llvm::SyncScope::ID
249SPIRVTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &, SyncScope Scope,
250 llvm::AtomicOrdering,
251 llvm::LLVMContext &Ctx) const {
252 return Ctx.getOrInsertSyncScopeID(mapClangSyncScopeToLLVM(Scope));
253}
254
255/// Construct a SPIR-V target extension type for the given OpenCL image type.
256static llvm::Type *getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType,
257 StringRef OpenCLName,
258 unsigned AccessQualifier) {
259 // These parameters compare to the operands of OpTypeImage (see
260 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage
261 // for more details). The first 6 integer parameters all default to 0, and
262 // will be changed to 1 only for the image type(s) that set the parameter to
263 // one. The 7th integer parameter is the access qualifier, which is tacked on
264 // at the end.
265 SmallVector<unsigned, 7> IntParams = {0, 0, 0, 0, 0, 0};
266
267 // Choose the dimension of the image--this corresponds to the Dim enum in
268 // SPIR-V (first integer parameter of OpTypeImage).
269 if (OpenCLName.starts_with("image2d"))
270 IntParams[0] = 1; // 1D
271 else if (OpenCLName.starts_with("image3d"))
272 IntParams[0] = 2; // 2D
273 else if (OpenCLName == "image1d_buffer")
274 IntParams[0] = 5; // Buffer
275 else
276 assert(OpenCLName.starts_with("image1d") && "Unknown image type");
277
278 // Set the other integer parameters of OpTypeImage if necessary. Note that the
279 // OpenCL image types don't provide any information for the Sampled or
280 // Image Format parameters.
281 if (OpenCLName.contains("_depth"))
282 IntParams[1] = 1;
283 if (OpenCLName.contains("_array"))
284 IntParams[2] = 1;
285 if (OpenCLName.contains("_msaa"))
286 IntParams[3] = 1;
287
288 // Access qualifier
289 IntParams.push_back(AccessQualifier);
290
291 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
292 IntParams);
293}
294
295llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
296 const Type *Ty) const {
297 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
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"
322 default:
323 return nullptr;
324 }
325 }
326
327 return nullptr;
328}
329
330llvm::Type *CommonSPIRTargetCodeGenInfo::getHLSLType(CodeGenModule &CGM,
331 const Type *Ty) const {
332 auto *ResType = dyn_cast<HLSLAttributedResourceType>(Ty);
333 if (!ResType)
334 return nullptr;
335
336 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
337 const HLSLAttributedResourceType::Attributes &ResAttrs = ResType->getAttrs();
338 switch (ResAttrs.ResourceClass) {
339 case llvm::dxil::ResourceClass::UAV:
340 case llvm::dxil::ResourceClass::SRV: {
341 // TypedBuffer and RawBuffer both need element type
342 QualType ContainedTy = ResType->getContainedType();
343 if (ContainedTy.isNull())
344 return nullptr;
345
346 assert(!ResAttrs.RawBuffer &&
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");
350
351 // convert element type
352 llvm::Type *ElemType = CGM.getTypes().ConvertType(ContainedTy);
353 return getSPIRVImageTypeFromHLSLResource(ResAttrs, ElemType, Ctx);
354 }
355 case llvm::dxil::ResourceClass::CBuffer:
356 llvm_unreachable("CBuffer handles are not implemented for SPIR-V yet");
357 break;
358 case llvm::dxil::ResourceClass::Sampler:
359 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
360 }
361 return nullptr;
362}
363
364llvm::Type *CommonSPIRTargetCodeGenInfo::getSPIRVImageTypeFromHLSLResource(
366 llvm::Type *ElementType, llvm::LLVMContext &Ctx) const {
367
368 if (ElementType->isVectorTy())
369 ElementType = ElementType->getScalarType();
370
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.");
374
375 // These parameters correspond to the operands to the OpTypeImage SPIR-V
376 // instruction. See
377 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage.
378 SmallVector<unsigned, 6> IntParams(6, 0);
379
380 // Dim
381 // For now we assume everything is a buffer.
382 IntParams[0] = 5;
383
384 // Depth
385 // HLSL does not indicate if it is a depth texture or not, so we use unknown.
386 IntParams[1] = 2;
387
388 // Arrayed
389 IntParams[2] = 0;
390
391 // MS
392 IntParams[3] = 0;
393
394 // Sampled
395 IntParams[4] =
396 attributes.ResourceClass == llvm::dxil::ResourceClass::UAV ? 2 : 1;
397
398 // Image format.
399 // Setting to unknown for now.
400 IntParams[5] = 0;
401
402 return llvm::TargetExtType::get(Ctx, "spirv.Image", {ElementType}, IntParams);
403}
404
405std::unique_ptr<TargetCodeGenInfo>
407 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.getTypes());
408}
409
410std::unique_ptr<TargetCodeGenInfo>
412 return std::make_unique<SPIRVTargetCodeGenInfo>(CGM.getTypes());
413}
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition: CGCall.cpp:293
const Decl * D
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:256
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:47
@ 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.
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:46
TargetCodeGenInfo - This class organizes various target-specific codegeneration issues,...
Definition: TargetInfo.h:47
virtual llvm::Type * getOpenCLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to an OpenCL type.
Definition: TargetInfo.h:437
virtual llvm::Type * getHLSLType(CodeGenModule &CGM, const Type *T) const
Return an LLVM type that corresponds to a HLSL type.
Definition: TargetInfo.h:442
const T & getABIInfo() const
Definition: TargetInfo.h:57
virtual unsigned getOpenCLKernelCallingConv() const
Get LLVM calling convention for OpenCL kernel.
Definition: TargetInfo.cpp:106
virtual LangAS getASTAllocaAddressSpace() const
Get the AST address space for alloca.
Definition: TargetInfo.h:316
ExtInfo withCallingConv(CallingConv cc) const
Definition: Type.h:4547
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4321
ExtInfo getExtInfo() const
Definition: Type.h:4655
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:499
A (possibly-)qualified type.
Definition: Type.h:929
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition: Type.h:996
Represents a struct/union/class.
Definition: Decl.h:4148
bool hasFlexibleArrayMember() const
Definition: Decl.h:4181
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:6072
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:1255
The base class of the type hierarchy.
Definition: Type.h:1828
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:8731
Represents a variable declaration or definition.
Definition: Decl.h:882
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:203
bool isAggregateTypeForABI(QualType T)
Definition: ABIInfoImpl.cpp:94
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:411
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:406
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.
Definition: AddressSpaces.h:25
const FunctionProtoType * T
SyncScope
Defines synch scope values used internally by clang.
Definition: SyncScope.h:42
@ CC_OpenCLKernel
Definition: Specifiers.h:292
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:86
llvm::dxil::ResourceClass ResourceClass
Definition: Type.h:6255