clang 20.0.0git
AMDGPU.cpp
Go to the documentation of this file.
1//===- AMDGPU.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"
12#include "llvm/Support/AMDGPUAddrSpace.h"
13
14using namespace clang;
15using namespace clang::CodeGen;
16
17//===----------------------------------------------------------------------===//
18// AMDGPU ABI Implementation
19//===----------------------------------------------------------------------===//
20
21namespace {
22
23class AMDGPUABIInfo final : public DefaultABIInfo {
24private:
25 static const unsigned MaxNumRegsForArgsRet = 16;
26
27 unsigned numRegsForType(QualType Ty) const;
28
29 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
31 uint64_t Members) const override;
32
33 // Coerce HIP scalar pointer arguments from generic pointers to global ones.
34 llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
35 unsigned ToAS) const {
36 // Single value types.
37 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
38 if (PtrTy && PtrTy->getAddressSpace() == FromAS)
39 return llvm::PointerType::get(Ty->getContext(), ToAS);
40 return Ty;
41 }
42
43public:
44 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
45 DefaultABIInfo(CGT) {}
46
48 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
50 unsigned &NumRegsLeft) const;
51
52 void computeInfo(CGFunctionInfo &FI) const override;
53 RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
54 AggValueSlot Slot) const override;
55};
56
57bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
58 return true;
59}
60
61bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
62 const Type *Base, uint64_t Members) const {
63 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
64
65 // Homogeneous Aggregates may occupy at most 16 registers.
66 return Members * NumRegs <= MaxNumRegsForArgsRet;
67}
68
69/// Estimate number of registers the type will use when passed in registers.
70unsigned AMDGPUABIInfo::numRegsForType(QualType Ty) const {
71 unsigned NumRegs = 0;
72
73 if (const VectorType *VT = Ty->getAs<VectorType>()) {
74 // Compute from the number of elements. The reported size is based on the
75 // in-memory size, which includes the padding 4th element for 3-vectors.
76 QualType EltTy = VT->getElementType();
77 unsigned EltSize = getContext().getTypeSize(EltTy);
78
79 // 16-bit element vectors should be passed as packed.
80 if (EltSize == 16)
81 return (VT->getNumElements() + 1) / 2;
82
83 unsigned EltNumRegs = (EltSize + 31) / 32;
84 return EltNumRegs * VT->getNumElements();
85 }
86
87 if (const RecordType *RT = Ty->getAs<RecordType>()) {
88 const RecordDecl *RD = RT->getDecl();
89 assert(!RD->hasFlexibleArrayMember());
90
91 for (const FieldDecl *Field : RD->fields()) {
92 QualType FieldTy = Field->getType();
93 NumRegs += numRegsForType(FieldTy);
94 }
95
96 return NumRegs;
97 }
98
99 return (getContext().getTypeSize(Ty) + 31) / 32;
100}
101
102void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
103 llvm::CallingConv::ID CC = FI.getCallingConvention();
104
105 if (!getCXXABI().classifyReturnType(FI))
107
108 unsigned ArgumentIndex = 0;
109 const unsigned numFixedArguments = FI.getNumRequiredArgs();
110
111 unsigned NumRegsLeft = MaxNumRegsForArgsRet;
112 for (auto &Arg : FI.arguments()) {
113 if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
114 Arg.info = classifyKernelArgumentType(Arg.type);
115 } else {
116 bool FixedArgument = ArgumentIndex++ < numFixedArguments;
117 Arg.info = classifyArgumentType(Arg.type, !FixedArgument, NumRegsLeft);
118 }
119 }
120}
121
122RValue AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
123 QualType Ty, AggValueSlot Slot) const {
124 const bool IsIndirect = false;
125 const bool AllowHigherAlign = false;
126 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
127 getContext().getTypeInfoInChars(Ty),
128 CharUnits::fromQuantity(4), AllowHigherAlign, Slot);
129}
130
131ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
132 if (isAggregateTypeForABI(RetTy)) {
133 // Records with non-trivial destructors/copy-constructors should not be
134 // returned by value.
135 if (!getRecordArgABI(RetTy, getCXXABI())) {
136 // Ignore empty structs/unions.
137 if (isEmptyRecord(getContext(), RetTy, true))
138 return ABIArgInfo::getIgnore();
139
140 // Lower single-element structs to just return a regular value.
141 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
142 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
143
144 if (const RecordType *RT = RetTy->getAs<RecordType>()) {
145 const RecordDecl *RD = RT->getDecl();
146 if (RD->hasFlexibleArrayMember())
148 }
149
150 // Pack aggregates <= 4 bytes into single VGPR or pair.
151 uint64_t Size = getContext().getTypeSize(RetTy);
152 if (Size <= 16)
153 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
154
155 if (Size <= 32)
156 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
157
158 if (Size <= 64) {
159 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
160 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
161 }
162
163 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
164 return ABIArgInfo::getDirect();
165 }
166 }
167
168 // Otherwise just do the default thing.
170}
171
172/// For kernels all parameters are really passed in a special buffer. It doesn't
173/// make sense to pass anything byval, so everything must be direct.
174ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
176
177 // TODO: Can we omit empty structs?
178
179 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
180 Ty = QualType(SeltTy, 0);
181
182 llvm::Type *OrigLTy = CGT.ConvertType(Ty);
183 llvm::Type *LTy = OrigLTy;
184 if (getContext().getLangOpts().HIP) {
185 LTy = coerceKernelArgumentType(
186 OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
187 /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
188 }
189
190 // FIXME: Should also use this for OpenCL, but it requires addressing the
191 // problem of kernels being called.
192 //
193 // FIXME: This doesn't apply the optimization of coercing pointers in structs
194 // to global address space when using byref. This would require implementing a
195 // new kind of coercion of the in-memory type when for indirect arguments.
196 if (!getContext().getLangOpts().OpenCL && LTy == OrigLTy &&
199 getContext().getTypeAlignInChars(Ty),
200 getContext().getTargetAddressSpace(LangAS::opencl_constant),
201 false /*Realign*/, nullptr /*Padding*/);
202 }
203
204 // If we set CanBeFlattened to true, CodeGen will expand the struct to its
205 // individual elements, which confuses the Clover OpenCL backend; therefore we
206 // have to set it to false here. Other args of getDirect() are just defaults.
207 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
208}
209
210ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, bool Variadic,
211 unsigned &NumRegsLeft) const {
212 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
213
215
216 if (Variadic) {
217 return ABIArgInfo::getDirect(/*T=*/nullptr,
218 /*Offset=*/0,
219 /*Padding=*/nullptr,
220 /*CanBeFlattened=*/false,
221 /*Align=*/0);
222 }
223
224 if (isAggregateTypeForABI(Ty)) {
225 // Records with non-trivial destructors/copy-constructors should not be
226 // passed by value.
227 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
228 return getNaturalAlignIndirect(Ty, RAA == CGCXXABI::RAA_DirectInMemory);
229
230 // Ignore empty structs/unions.
231 if (isEmptyRecord(getContext(), Ty, true))
232 return ABIArgInfo::getIgnore();
233
234 // Lower single-element structs to just pass a regular value. TODO: We
235 // could do reasonable-size multiple-element structs too, using getExpand(),
236 // though watch out for things like bitfields.
237 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
238 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
239
240 if (const RecordType *RT = Ty->getAs<RecordType>()) {
241 const RecordDecl *RD = RT->getDecl();
242 if (RD->hasFlexibleArrayMember())
244 }
245
246 // Pack aggregates <= 8 bytes into single VGPR or pair.
247 uint64_t Size = getContext().getTypeSize(Ty);
248 if (Size <= 64) {
249 unsigned NumRegs = (Size + 31) / 32;
250 NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
251
252 if (Size <= 16)
253 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
254
255 if (Size <= 32)
256 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
257
258 // XXX: Should this be i64 instead, and should the limit increase?
259 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
260 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
261 }
262
263 if (NumRegsLeft > 0) {
264 unsigned NumRegs = numRegsForType(Ty);
265 if (NumRegsLeft >= NumRegs) {
266 NumRegsLeft -= NumRegs;
267 return ABIArgInfo::getDirect();
268 }
269 }
270
271 // Use pass-by-reference in stead of pass-by-value for struct arguments in
272 // function ABI.
274 getContext().getTypeAlignInChars(Ty),
275 getContext().getTargetAddressSpace(LangAS::opencl_private));
276 }
277
278 // Otherwise just do the default thing.
280 if (!ArgInfo.isIndirect()) {
281 unsigned NumRegs = numRegsForType(Ty);
282 NumRegsLeft -= std::min(NumRegs, NumRegsLeft);
283 }
284
285 return ArgInfo;
286}
287
288class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
289public:
290 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
291 : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
292
293 void setFunctionDeclAttributes(const FunctionDecl *FD, llvm::Function *F,
294 CodeGenModule &CGM) const;
295
296 void emitTargetGlobals(CodeGen::CodeGenModule &CGM) const override;
297
298 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
299 CodeGen::CodeGenModule &M) const override;
300 unsigned getOpenCLKernelCallingConv() const override;
301
302 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
303 llvm::PointerType *T, QualType QT) const override;
304
305 LangAS getASTAllocaAddressSpace() const override {
307 getABIInfo().getDataLayout().getAllocaAddrSpace());
308 }
310 const VarDecl *D) const override;
311 llvm::SyncScope::ID getLLVMSyncScopeID(const LangOptions &LangOpts,
313 llvm::AtomicOrdering Ordering,
314 llvm::LLVMContext &Ctx) const override;
316 llvm::Instruction &AtomicInst,
317 const AtomicExpr *Expr = nullptr) const override;
319 llvm::Function *BlockInvokeFunc,
320 llvm::Type *BlockTy) const override;
321 bool shouldEmitStaticExternCAliases() const override;
322 bool shouldEmitDWARFBitFieldSeparators() const override;
323 void setCUDAKernelCallingConvention(const FunctionType *&FT) const override;
324};
325}
326
328 llvm::GlobalValue *GV) {
329 if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
330 return false;
331
332 return !D->hasAttr<OMPDeclareTargetDeclAttr>() &&
333 (D->hasAttr<OpenCLKernelAttr>() ||
334 (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
335 (isa<VarDecl>(D) &&
336 (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
337 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
338 cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())));
339}
340
341void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
342 const FunctionDecl *FD, llvm::Function *F, CodeGenModule &M) const {
343 const auto *ReqdWGS =
344 M.getLangOpts().OpenCL ? FD->getAttr<ReqdWorkGroupSizeAttr>() : nullptr;
345 const bool IsOpenCLKernel =
346 M.getLangOpts().OpenCL && FD->hasAttr<OpenCLKernelAttr>();
347 const bool IsHIPKernel = M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>();
348
349 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
350 if (ReqdWGS || FlatWGS) {
351 M.handleAMDGPUFlatWorkGroupSizeAttr(F, FlatWGS, ReqdWGS);
352 } else if (IsOpenCLKernel || IsHIPKernel) {
353 // By default, restrict the maximum size to a value specified by
354 // --gpu-max-threads-per-block=n or its default value for HIP.
355 const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
356 const unsigned DefaultMaxWorkGroupSize =
357 IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
358 : M.getLangOpts().GPUMaxThreadsPerBlock;
359 std::string AttrVal =
360 std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
361 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
362 }
363
364 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>())
366
367 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
368 unsigned NumSGPR = Attr->getNumSGPR();
369
370 if (NumSGPR != 0)
371 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
372 }
373
374 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
375 uint32_t NumVGPR = Attr->getNumVGPR();
376
377 if (NumVGPR != 0)
378 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
379 }
380
381 if (const auto *Attr = FD->getAttr<AMDGPUMaxNumWorkGroupsAttr>()) {
382 uint32_t X = Attr->getMaxNumWorkGroupsX()
383 ->EvaluateKnownConstInt(M.getContext())
384 .getExtValue();
385 // Y and Z dimensions default to 1 if not specified
386 uint32_t Y = Attr->getMaxNumWorkGroupsY()
387 ? Attr->getMaxNumWorkGroupsY()
388 ->EvaluateKnownConstInt(M.getContext())
389 .getExtValue()
390 : 1;
391 uint32_t Z = Attr->getMaxNumWorkGroupsZ()
392 ? Attr->getMaxNumWorkGroupsZ()
393 ->EvaluateKnownConstInt(M.getContext())
394 .getExtValue()
395 : 1;
396
397 llvm::SmallString<32> AttrVal;
398 llvm::raw_svector_ostream OS(AttrVal);
399 OS << X << ',' << Y << ',' << Z;
400
401 F->addFnAttr("amdgpu-max-num-workgroups", AttrVal.str());
402 }
403}
404
405/// Emits control constants used to change per-architecture behaviour in the
406/// AMDGPU ROCm device libraries.
407void AMDGPUTargetCodeGenInfo::emitTargetGlobals(
408 CodeGen::CodeGenModule &CGM) const {
409 StringRef Name = "__oclc_ABI_version";
410 llvm::GlobalVariable *OriginalGV = CGM.getModule().getNamedGlobal(Name);
411 if (OriginalGV && !llvm::GlobalVariable::isExternalLinkage(OriginalGV->getLinkage()))
412 return;
413
415 llvm::CodeObjectVersionKind::COV_None)
416 return;
417
418 auto *Type = llvm::IntegerType::getIntNTy(CGM.getModule().getContext(), 32);
419 llvm::Constant *COV = llvm::ConstantInt::get(
421
422 // It needs to be constant weak_odr without externally_initialized so that
423 // the load instuction can be eliminated by the IPSCCP.
424 auto *GV = new llvm::GlobalVariable(
425 CGM.getModule(), Type, true, llvm::GlobalValue::WeakODRLinkage, COV, Name,
426 nullptr, llvm::GlobalValue::ThreadLocalMode::NotThreadLocal,
427 CGM.getContext().getTargetAddressSpace(LangAS::opencl_constant));
428 GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Local);
429 GV->setVisibility(llvm::GlobalValue::VisibilityTypes::HiddenVisibility);
430
431 // Replace any external references to this variable with the new global.
432 if (OriginalGV) {
433 OriginalGV->replaceAllUsesWith(GV);
434 GV->takeName(OriginalGV);
435 OriginalGV->eraseFromParent();
436 }
437}
438
439void AMDGPUTargetCodeGenInfo::setTargetAttributes(
440 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
442 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
443 GV->setDSOLocal(true);
444 }
445
446 if (GV->isDeclaration())
447 return;
448
449 llvm::Function *F = dyn_cast<llvm::Function>(GV);
450 if (!F)
451 return;
452
453 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
454 if (FD)
455 setFunctionDeclAttributes(FD, F, M);
456
457 if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
458 F->addFnAttr("amdgpu-ieee", "false");
459}
460
461unsigned AMDGPUTargetCodeGenInfo::getOpenCLKernelCallingConv() const {
462 return llvm::CallingConv::AMDGPU_KERNEL;
463}
464
465// Currently LLVM assumes null pointers always have value 0,
466// which results in incorrectly transformed IR. Therefore, instead of
467// emitting null pointers in private and local address spaces, a null
468// pointer in generic address space is emitted which is casted to a
469// pointer in local or private address space.
470llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
471 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
472 QualType QT) const {
473 if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
474 return llvm::ConstantPointerNull::get(PT);
475
476 auto &Ctx = CGM.getContext();
477 auto NPT = llvm::PointerType::get(
478 PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic));
479 return llvm::ConstantExpr::getAddrSpaceCast(
480 llvm::ConstantPointerNull::get(NPT), PT);
481}
482
483LangAS
484AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
485 const VarDecl *D) const {
486 assert(!CGM.getLangOpts().OpenCL &&
487 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
488 "Address space agnostic languages only");
489 LangAS DefaultGlobalAS = getLangASFromTargetAS(
490 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
491 if (!D)
492 return DefaultGlobalAS;
493
494 LangAS AddrSpace = D->getType().getAddressSpace();
495 if (AddrSpace != LangAS::Default)
496 return AddrSpace;
497
498 // Only promote to address space 4 if VarDecl has constant initialization.
499 if (D->getType().isConstantStorage(CGM.getContext(), false, false) &&
500 D->hasConstantInitialization()) {
501 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
502 return *ConstAS;
503 }
504 return DefaultGlobalAS;
505}
506
507llvm::SyncScope::ID
508AMDGPUTargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts,
510 llvm::AtomicOrdering Ordering,
511 llvm::LLVMContext &Ctx) const {
512 std::string Name;
513 switch (Scope) {
514 case SyncScope::HIPSingleThread:
515 case SyncScope::SingleScope:
516 Name = "singlethread";
517 break;
518 case SyncScope::HIPWavefront:
519 case SyncScope::OpenCLSubGroup:
520 case SyncScope::WavefrontScope:
521 Name = "wavefront";
522 break;
523 case SyncScope::HIPWorkgroup: