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:
35 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
36};
37} // end anonymous namespace
38namespace {
39class CommonSPIRTargetCodeGenInfo : public TargetCodeGenInfo {
40public:
41 CommonSPIRTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
42 : TargetCodeGenInfo(std::make_unique<CommonSPIRABIInfo>(CGT)) {}
43 CommonSPIRTargetCodeGenInfo(std::unique_ptr<ABIInfo> ABIInfo)
44 : TargetCodeGenInfo(std::move(ABIInfo)) {}
45
46 LangAS getASTAllocaAddressSpace() const override {
48 getABIInfo().getDataLayout().getAllocaAddrSpace());
49 }
50
51 unsigned getOpenCLKernelCallingConv() const override;
52 llvm::Type *getOpenCLType(CodeGenModule &CGM, const Type *T) const override;
53};
54class SPIRVTargetCodeGenInfo : public CommonSPIRTargetCodeGenInfo {
55public:
56 SPIRVTargetCodeGenInfo(CodeGen::CodeGenTypes &CGT)
57 : CommonSPIRTargetCodeGenInfo(std::make_unique<SPIRVABIInfo>(CGT)) {}
58 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
59};
60} // End anonymous namespace.
61
62void CommonSPIRABIInfo::setCCs() {
63 assert(getRuntimeCC() == llvm::CallingConv::C);
64 RuntimeCC = llvm::CallingConv::SPIR_FUNC;
65}
66
67ABIArgInfo SPIRVABIInfo::classifyKernelArgumentType(QualType Ty) const {
68 if (getContext().getLangOpts().CUDAIsDevice) {
69 // Coerce pointer arguments with default address space to CrossWorkGroup
70 // pointers for HIPSPV/CUDASPV. When the language mode is HIP/CUDA, the
71 // SPIRTargetInfo maps cuda_device to SPIR-V's CrossWorkGroup address space.
72 llvm::Type *LTy = CGT.ConvertType(Ty);
73 auto DefaultAS = getContext().getTargetAddressSpace(LangAS::Default);
74 auto GlobalAS = getContext().getTargetAddressSpace(LangAS::cuda_device);
75 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(LTy);
76 if (PtrTy && PtrTy->getAddressSpace() == DefaultAS) {
77 LTy = llvm::PointerType::get(PtrTy->getContext(), GlobalAS);
78 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
79 }
80
81 // Force copying aggregate type in kernel arguments by value when
82 // compiling CUDA targeting SPIR-V. This is required for the object
83 // copied to be valid on the device.
84 // This behavior follows the CUDA spec
85 // https://docs.nvidia.com/cuda/cuda-c-programming-guide/index.html#global-function-argument-processing,
86 // and matches the NVPTX implementation.
88 return getNaturalAlignIndirect(Ty, /* byval */ true);
89 }
90 return classifyArgumentType(Ty);
91}
92
93void SPIRVABIInfo::computeInfo(CGFunctionInfo &FI) const {
94 // The logic is same as in DefaultABIInfo with an exception on the kernel
95 // arguments handling.
96 llvm::CallingConv::ID CC = FI.getCallingConvention();
97
98 if (!getCXXABI().classifyReturnType(FI))
100
101 for (auto &I : FI.arguments()) {
102 if (CC == llvm::CallingConv::SPIR_KERNEL) {
103 I.info = classifyKernelArgumentType(I.type);
104 } else {
105 I.info = classifyArgumentType(I.type);
106 }
107 }
108}
109
110namespace clang {
111namespace CodeGen {
113 if (CGM.getTarget().getTriple().isSPIRV())
114 SPIRVABIInfo(CGM.getTypes()).computeInfo(FI);
115 else
116 CommonSPIRABIInfo(CGM.getTypes()).computeInfo(FI);
117}
118}
119}
120
121unsigned CommonSPIRTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
122 return llvm::CallingConv::SPIR_KERNEL;
123}
124
125void SPIRVTargetCodeGenInfo::setCUDAKernelCallingConvention(
126 const FunctionType *&FT) const {
127 // Convert HIP kernels to SPIR-V kernels.
128 if (getABIInfo().getContext().getLangOpts().HIP) {
129 FT = getABIInfo().getContext().adjustFunctionType(
131 return;
132 }
133}
134
135/// Construct a SPIR-V target extension type for the given OpenCL image type.
136static llvm::Type *getSPIRVImageType(llvm::LLVMContext &Ctx, StringRef BaseType,
137 StringRef OpenCLName,
138 unsigned AccessQualifier) {
139 // These parameters compare to the operands of OpTypeImage (see
140 // https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeImage
141 // for more details). The first 6 integer parameters all default to 0, and
142 // will be changed to 1 only for the image type(s) that set the parameter to
143 // one. The 7th integer parameter is the access qualifier, which is tacked on
144 // at the end.
145 SmallVector<unsigned, 7> IntParams = {0, 0, 0, 0, 0, 0};
146
147 // Choose the dimension of the image--this corresponds to the Dim enum in
148 // SPIR-V (first integer parameter of OpTypeImage).
149 if (OpenCLName.starts_with("image2d"))
150 IntParams[0] = 1; // 1D
151 else if (OpenCLName.starts_with("image3d"))
152 IntParams[0] = 2; // 2D
153 else if (OpenCLName == "image1d_buffer")
154 IntParams[0] = 5; // Buffer
155 else
156 assert(OpenCLName.starts_with("image1d") && "Unknown image type");
157
158 // Set the other integer parameters of OpTypeImage if necessary. Note that the
159 // OpenCL image types don't provide any information for the Sampled or
160 // Image Format parameters.
161 if (OpenCLName.contains("_depth"))
162 IntParams[1] = 1;
163 if (OpenCLName.contains("_array"))
164 IntParams[2] = 1;
165 if (OpenCLName.contains("_msaa"))
166 IntParams[3] = 1;
167
168 // Access qualifier
169 IntParams.push_back(AccessQualifier);
170
171 return llvm::TargetExtType::get(Ctx, BaseType, {llvm::Type::getVoidTy(Ctx)},
172 IntParams);
173}
174
175llvm::Type *CommonSPIRTargetCodeGenInfo::getOpenCLType(CodeGenModule &CGM,
176 const Type *Ty) const {
177 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
178 if (auto *PipeTy = dyn_cast<PipeType>(Ty))
179 return llvm::TargetExtType::get(Ctx, "spirv.Pipe", {},
180 {!PipeTy->isReadOnly()});
181 if (auto *BuiltinTy = dyn_cast<BuiltinType>(Ty)) {
182 enum AccessQualifier : unsigned { AQ_ro = 0, AQ_wo = 1, AQ_rw = 2 };
183 switch (BuiltinTy->getKind()) {
184#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
185 case BuiltinType::Id: \
186 return getSPIRVImageType(Ctx, "spirv.Image", #ImgType, AQ_##Suffix);
187#include "clang/Basic/OpenCLImageTypes.def"
188 case BuiltinType::OCLSampler:
189 return llvm::TargetExtType::get(Ctx, "spirv.Sampler");
190 case BuiltinType::OCLEvent:
191 return llvm::TargetExtType::get(Ctx, "spirv.Event");
192 case BuiltinType::OCLClkEvent:
193 return llvm::TargetExtType::get(Ctx, "spirv.DeviceEvent");
194 case BuiltinType::OCLQueue:
195 return llvm::TargetExtType::get(Ctx, "spirv.Queue");
196 case BuiltinType::OCLReserveID:
197 return llvm::TargetExtType::get(Ctx, "spirv.ReserveId");
198#define INTEL_SUBGROUP_AVC_TYPE(Name, Id) \
199 case BuiltinType::OCLIntelSubgroupAVC##Id: \
200 return llvm::TargetExtType::get(Ctx, "spirv.Avc" #Id "INTEL");
201#include "clang/Basic/OpenCLExtensionTypes.def"
202 default:
203 return nullptr;
204 }
205 }
206
207 return nullptr;
208}
209
210std::unique_ptr<TargetCodeGenInfo>
212 return std::make_unique<CommonSPIRTargetCodeGenInfo>(CGM.getTypes());
213}
214
215std::unique_ptr<TargetCodeGenInfo>
217 return std::make_unique<SPIRVTargetCodeGenInfo>(CGM.getTypes());
218}
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition: CGCall.cpp:293
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:136
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
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 TargetInfo & getTarget() 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
DefaultABIInfo - The default implementation for ABI specific details.
Definition: ABIInfoImpl.h:21
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:417
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:298
ExtInfo withCallingConv(CallingConv cc) const
Definition: Type.h:4534
FunctionType - C99 6.7.5.3 - Function Declarators.
Definition: Type.h:4308
ExtInfo getExtInfo() const
Definition: Type.h:4642
A (possibly-)qualified type.
Definition: Type.h:941
const llvm::Triple & getTriple() const
Returns the target triple of the primary target.
Definition: TargetInfo.h:1256
The base class of the type hierarchy.
Definition: Type.h:1829
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
void computeSPIRKernelABIInfo(CodeGenModule &CGM, CGFunctionInfo &FI)
Definition: SPIR.cpp:112
bool isAggregateTypeForABI(QualType T)
std::unique_ptr< TargetCodeGenInfo > createSPIRVTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:216
std::unique_ptr< TargetCodeGenInfo > createCommonSPIRTargetCodeGenInfo(CodeGenModule &CGM)
Definition: SPIR.cpp:211
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
@ CC_OpenCLKernel
Definition: Specifiers.h:292
LangAS getLangASFromTargetAS(unsigned TargetAS)
Definition: AddressSpaces.h:86