clang 24.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"
11#include "clang/AST/DeclCXX.h"
12#include "llvm/ADT/StringExtras.h"
13#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
14#include "llvm/Support/AMDGPUAddrSpace.h"
15
16using namespace clang;
17using namespace clang::CodeGen;
18
19//===----------------------------------------------------------------------===//
20// AMDGPU ABI Implementation
21//===----------------------------------------------------------------------===//
22
23namespace {
24
25class AMDGPUABIInfo final : public DefaultABIInfo {
26private:
27 static const unsigned MaxNumRegsForArgsRet = 16;
28
29 uint64_t numRegsForType(QualType Ty) const;
30
31 bool isHomogeneousAggregateBaseType(QualType Ty) const override;
32 bool isHomogeneousAggregateSmallEnough(const Type *Base,
33 uint64_t Members) const override;
34
35 // Coerce HIP scalar pointer arguments from generic pointers to global ones.
36 llvm::Type *coerceKernelArgumentType(llvm::Type *Ty, unsigned FromAS,
37 unsigned ToAS) const {
38 // Single value types.
39 auto *PtrTy = llvm::dyn_cast<llvm::PointerType>(Ty);
40 if (PtrTy && PtrTy->getAddressSpace() == FromAS)
41 return llvm::PointerType::get(Ty->getContext(), ToAS);
42 return Ty;
43 }
44
45public:
46 explicit AMDGPUABIInfo(CodeGen::CodeGenTypes &CGT) :
47 DefaultABIInfo(CGT) {}
48
49 ABIArgInfo classifyReturnType(QualType RetTy) const;
50 ABIArgInfo classifyKernelArgumentType(QualType Ty) const;
51 ABIArgInfo classifyArgumentType(QualType Ty, bool Variadic,
52 unsigned &NumRegsLeft) const;
53
54 void computeInfo(CGFunctionInfo &FI) const override;
55 RValue EmitVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType Ty,
56 AggValueSlot Slot) const override;
57
58 llvm::FixedVectorType *
59 getOptimalVectorMemoryType(llvm::FixedVectorType *T,
60 const LangOptions &Opt) const override {
61 // We have legal instructions for 96-bit so 3x32 can be supported.
62 // FIXME: This check should be a subtarget feature as technically SI doesn't
63 // support it.
64 if (T->getNumElements() == 3 && getDataLayout().getTypeSizeInBits(T) == 96)
65 return T;
66 return DefaultABIInfo::getOptimalVectorMemoryType(T, Opt);
67 }
68};
69
70bool AMDGPUABIInfo::isHomogeneousAggregateBaseType(QualType Ty) const {
71 return true;
72}
73
74bool AMDGPUABIInfo::isHomogeneousAggregateSmallEnough(
75 const Type *Base, uint64_t Members) const {
76 uint32_t NumRegs = (getContext().getTypeSize(Base) + 31) / 32;
77
78 // Homogeneous Aggregates may occupy at most 16 registers.
79 return Members * NumRegs <= MaxNumRegsForArgsRet;
80}
81
82/// Estimate number of registers the type will use when passed in registers.
83uint64_t AMDGPUABIInfo::numRegsForType(QualType Ty) const {
84 uint64_t NumRegs = 0;
85
86 if (const VectorType *VT = Ty->getAs<VectorType>()) {
87 // Compute from the number of elements. The reported size is based on the
88 // in-memory size, which includes the padding 4th element for 3-vectors.
89 QualType EltTy = VT->getElementType();
90 uint64_t EltSize = getContext().getTypeSize(EltTy);
91
92 // 16-bit element vectors should be passed as packed.
93 if (EltSize == 16)
94 return (VT->getNumElements() + 1) / 2;
95
96 uint64_t EltNumRegs = (EltSize + 31) / 32;
97 return EltNumRegs * VT->getNumElements();
98 }
99
100 if (const auto *RD = Ty->getAsRecordDecl()) {
101 assert(!RD->hasFlexibleArrayMember());
102
103 for (const FieldDecl *Field : RD->fields()) {
104 QualType FieldTy = Field->getType();
105 NumRegs += numRegsForType(FieldTy);
106 }
107
108 return NumRegs;
109 }
110
111 return (getContext().getTypeSize(Ty) + 31) / 32;
112}
113
114void AMDGPUABIInfo::computeInfo(CGFunctionInfo &FI) const {
115 llvm::CallingConv::ID CC = FI.getCallingConvention();
116
117 if (!getCXXABI().classifyReturnType(FI))
119
120 unsigned ArgumentIndex = 0;
121 const unsigned numFixedArguments = FI.getNumRequiredArgs();
122
123 unsigned NumRegsLeft = MaxNumRegsForArgsRet;
124 for (auto &Arg : FI.arguments()) {
125 if (CC == llvm::CallingConv::AMDGPU_KERNEL) {
126 Arg.info = classifyKernelArgumentType(Arg.type);
127 } else {
128 bool FixedArgument = ArgumentIndex++ < numFixedArguments;
129 Arg.info = classifyArgumentType(Arg.type, !FixedArgument, NumRegsLeft);
130 }
131 }
132}
133
134RValue AMDGPUABIInfo::EmitVAArg(CodeGenFunction &CGF, Address VAListAddr,
135 QualType Ty, AggValueSlot Slot) const {
136 const bool IsIndirect = false;
137 const bool AllowHigherAlign = false;
138 return emitVoidPtrVAArg(CGF, VAListAddr, Ty, IsIndirect,
139 getContext().getTypeInfoInChars(Ty),
140 CharUnits::fromQuantity(4), AllowHigherAlign, Slot);
141}
142
143ABIArgInfo AMDGPUABIInfo::classifyReturnType(QualType RetTy) const {
144 if (isAggregateTypeForABI(RetTy)) {
145 // Records with non-trivial destructors/copy-constructors should not be
146 // returned by value.
147 if (!getRecordArgABI(RetTy, getCXXABI())) {
148 // Ignore empty structs/unions.
149 if (isEmptyRecord(getContext(), RetTy, true))
150 return ABIArgInfo::getIgnore();
151
152 // Lower single-element structs to just return a regular value.
153 if (const Type *SeltTy = isSingleElementStruct(RetTy, getContext()))
154 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
155
156 if (const auto *RD = RetTy->getAsRecordDecl();
157 RD && RD->hasFlexibleArrayMember())
159
160 // Pack aggregates <= 4 bytes into single VGPR or pair.
161 uint64_t Size = getContext().getTypeSize(RetTy);
162 if (Size <= 16)
163 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
164
165 if (Size <= 32)
166 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
167
168 if (Size <= 64) {
169 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
170 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
171 }
172
173 if (numRegsForType(RetTy) <= MaxNumRegsForArgsRet)
174 return ABIArgInfo::getDirect();
175 }
176 }
177
178 // Otherwise just do the default thing.
180}
181
182/// For kernels all parameters are really passed in a special buffer. It doesn't
183/// make sense to pass anything byval, so everything must be direct.
184ABIArgInfo AMDGPUABIInfo::classifyKernelArgumentType(QualType Ty) const {
186
187 // TODO: Can we omit empty structs?
188
189 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
190 Ty = QualType(SeltTy, 0);
191
192 llvm::Type *OrigLTy = CGT.ConvertType(Ty);
193 llvm::Type *LTy = OrigLTy;
194 if (getContext().getLangOpts().HIP) {
195 LTy = coerceKernelArgumentType(
196 OrigLTy, /*FromAS=*/getContext().getTargetAddressSpace(LangAS::Default),
197 /*ToAS=*/getContext().getTargetAddressSpace(LangAS::cuda_device));
198 }
199
200 // FIXME: This doesn't apply the optimization of coercing pointers in structs
201 // to global address space when using byref. This would require implementing a
202 // new kind of coercion of the in-memory type when for indirect arguments.
203 if (LTy == OrigLTy && isAggregateTypeForABI(Ty)) {
205 getContext().getTypeAlignInChars(Ty),
206 getContext().getTargetAddressSpace(LangAS::opencl_constant),
207 false /*Realign*/, nullptr /*Padding*/);
208 }
209
210 // If we set CanBeFlattened to true, CodeGen will expand the struct to its
211 // individual elements, which confuses the Clover OpenCL backend; therefore we
212 // have to set it to false here. Other args of getDirect() are just defaults.
213 return ABIArgInfo::getDirect(LTy, 0, nullptr, false);
214}
215
216ABIArgInfo AMDGPUABIInfo::classifyArgumentType(QualType Ty, bool Variadic,
217 unsigned &NumRegsLeft) const {
218 assert(NumRegsLeft <= MaxNumRegsForArgsRet && "register estimate underflow");
219
221
222 if (Variadic) {
223 return ABIArgInfo::getDirect(/*T=*/nullptr,
224 /*Offset=*/0,
225 /*Padding=*/nullptr,
226 /*CanBeFlattened=*/false,
227 /*Align=*/0);
228 }
229
230 if (isAggregateTypeForABI(Ty)) {
231 // Records with non-trivial destructors/copy-constructors should not be
232 // passed by value.
233 if (auto RAA = getRecordArgABI(Ty, getCXXABI()))
234 return getNaturalAlignIndirect(Ty, getDataLayout().getAllocaAddrSpace(),
236
237 // Ignore empty structs/unions.
238 if (isEmptyRecord(getContext(), Ty, true))
239 return ABIArgInfo::getIgnore();
240
241 // Lower single-element structs to just pass a regular value. TODO: We
242 // could do reasonable-size multiple-element structs too, using getExpand(),
243 // though watch out for things like bitfields.
244 if (const Type *SeltTy = isSingleElementStruct(Ty, getContext()))
245 return ABIArgInfo::getDirect(CGT.ConvertType(QualType(SeltTy, 0)));
246
247 if (const auto *RD = Ty->getAsRecordDecl();
248 RD && RD->hasFlexibleArrayMember())
250
251 // Pack aggregates <= 8 bytes into single VGPR or pair.
252 uint64_t Size = getContext().getTypeSize(Ty);
253 if (Size <= 64) {
254 unsigned NumRegs = (Size + 31) / 32;
255 NumRegsLeft -= std::min(NumRegsLeft, NumRegs);
256
257 if (Size <= 16)
258 return ABIArgInfo::getDirect(llvm::Type::getInt16Ty(getVMContext()));
259
260 if (Size <= 32)
261 return ABIArgInfo::getDirect(llvm::Type::getInt32Ty(getVMContext()));
262
263 // XXX: Should this be i64 instead, and should the limit increase?
264 llvm::Type *I32Ty = llvm::Type::getInt32Ty(getVMContext());
265 return ABIArgInfo::getDirect(llvm::ArrayType::get(I32Ty, 2));
266 }
267
268 if (NumRegsLeft > 0) {
269 uint64_t NumRegs = numRegsForType(Ty);
270 if (NumRegsLeft >= NumRegs) {
271 NumRegsLeft -= NumRegs;
272 return ABIArgInfo::getDirect();
273 }
274 }
275
276 // Use pass-by-reference in stead of pass-by-value for struct arguments in
277 // function ABI.
279 getContext().getTypeAlignInChars(Ty),
280 getContext().getTargetAddressSpace(LangAS::opencl_private));
281 }
282
283 // Otherwise just do the default thing.
284 ABIArgInfo ArgInfo = DefaultABIInfo::classifyArgumentType(Ty);
285 if (!ArgInfo.isIndirect()) {
286 uint64_t NumRegs = numRegsForType(Ty);
287 NumRegsLeft -= std::min(NumRegs, uint64_t{NumRegsLeft});
288 }
289
290 return ArgInfo;
291}
292
293class AMDGPUTargetCodeGenInfo : public TargetCodeGenInfo {
294public:
295 AMDGPUTargetCodeGenInfo(CodeGenTypes &CGT)
296 : TargetCodeGenInfo(std::make_unique<AMDGPUABIInfo>(CGT)) {}
297
298 bool supportsLibCall() const override { return false; }
299 void setFunctionDeclAttributes(const FunctionDecl *FD, llvm::Function *F,
300 CodeGenModule &CGM) const;
301
302 void setTargetAttributes(const Decl *D, llvm::GlobalValue *GV,
303 CodeGen::CodeGenModule &M) const override;
304 unsigned getDeviceKernelCallingConv() const override;
305
306 llvm::Constant *getNullPointer(const CodeGen::CodeGenModule &CGM,
307 llvm::PointerType *T, QualType QT) const override;
308
309 LangAS getSRetAddrSpace(const CXXRecordDecl *RD) const override;
310
311 LangAS getGlobalVarAddressSpace(CodeGenModule &CGM,
312 const VarDecl *D) const override;
313 StringRef getLLVMSyncScopeStr(const LangOptions &LangOpts, SyncScope Scope,
314 llvm::AtomicOrdering Ordering) const override;
315 void setTargetAtomicMetadata(CodeGenFunction &CGF,
316 llvm::Instruction &AtomicInst,
317 const AtomicExpr *Expr = nullptr) const override;
318 llvm::Value *createEnqueuedBlockKernel(CodeGenFunction &CGF,
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<DeviceKernelAttr>() ||
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<DeviceKernelAttr>();
347 const bool IsHIPKernel = M.getLangOpts().HIP && FD->hasAttr<CUDAGlobalAttr>();
348
349 const auto *FlatWGS = FD->getAttr<AMDGPUFlatWorkGroupSizeAttr>();
350
351 // __launch_bounds__ only takes effect on kernels and is silently ignored on
352 // other functions The arguments are honored only if the equivalent native
353 // amdgpu_flat_work_group_size / amdgpu_waves_per_eu attribute was not also
354 // used out; those take precedence.
355 const auto *LaunchBounds =
356 IsHIPKernel ? FD->getAttr<CUDALaunchBoundsAttr>() : nullptr;
357 unsigned LBMaxThreads = 0;
358 unsigned LBMinWaves = 0;
359 if (LaunchBounds) {
360 LBMaxThreads = LaunchBounds->getMaxThreads()
361 ->EvaluateKnownConstInt(M.getContext())
362 .getExtValue();
363 if (const Expr *MinBlocks = LaunchBounds->getMinBlocks()) {
364 LBMinWaves =
365 MinBlocks->EvaluateKnownConstInt(M.getContext()).getExtValue();
366 }
367 }
368
369 if (ReqdWGS || FlatWGS) {
370 M.handleAMDGPUFlatWorkGroupSizeAttr(F, FlatWGS, ReqdWGS);
371 } else if (LBMaxThreads > 0) {
372 F->addFnAttr("amdgpu-flat-work-group-size",
373 "1," + llvm::utostr(LBMaxThreads));
374 } else if (IsOpenCLKernel || IsHIPKernel) {
375 // By default, restrict the maximum size to a value specified by
376 // --gpu-max-threads-per-block=n or its default value for HIP.
377 const unsigned OpenCLDefaultMaxWorkGroupSize = 256;
378 const unsigned DefaultMaxWorkGroupSize =
379 IsOpenCLKernel ? OpenCLDefaultMaxWorkGroupSize
380 : M.getLangOpts().GPUMaxThreadsPerBlock;
381 std::string AttrVal =
382 std::string("1,") + llvm::utostr(DefaultMaxWorkGroupSize);
383 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
384 }
385
386 if (const auto *Attr = FD->getAttr<AMDGPUWavesPerEUAttr>()) {
388 } else if (LBMinWaves > 0) {
389 // HIP reinterprets the second argument as the minimum waves per EU.
390 //
391 // TODO: The third argument (maxclusterrank) could be used if the AMDGPU
392 // "clusters" feature is supported for the current subtarget.
393 F->addFnAttr("amdgpu-waves-per-eu", llvm::utostr(LBMinWaves));
394 }
395
396 if (const auto *Attr = FD->getAttr<AMDGPUNumSGPRAttr>()) {
397 unsigned NumSGPR = Attr->getNumSGPR();
398
399 if (NumSGPR != 0)
400 F->addFnAttr("amdgpu-num-sgpr", llvm::utostr(NumSGPR));
401 }
402
403 if (const auto *Attr = FD->getAttr<AMDGPUNumVGPRAttr>()) {
404 uint32_t NumVGPR = Attr->getNumVGPR();
405
406 if (NumVGPR != 0)
407 F->addFnAttr("amdgpu-num-vgpr", llvm::utostr(NumVGPR));
408 }
409
410 if (const auto *Attr = FD->getAttr<AMDGPUMaxNumWorkGroupsAttr>()) {
411 uint32_t X = Attr->getMaxNumWorkGroupsX()
412 ->EvaluateKnownConstInt(M.getContext())
413 .getExtValue();
414 // Y and Z dimensions default to 1 if not specified
415 uint32_t Y = Attr->getMaxNumWorkGroupsY()
416 ? Attr->getMaxNumWorkGroupsY()
417 ->EvaluateKnownConstInt(M.getContext())
418 .getExtValue()
419 : 1;
420 uint32_t Z = Attr->getMaxNumWorkGroupsZ()
421 ? Attr->getMaxNumWorkGroupsZ()
422 ->EvaluateKnownConstInt(M.getContext())
423 .getExtValue()
424 : 1;
425
426 llvm::SmallString<32> AttrVal;
427 llvm::raw_svector_ostream OS(AttrVal);
428 OS << X << ',' << Y << ',' << Z;
429
430 F->addFnAttr("amdgpu-max-num-workgroups", AttrVal.str());
431 }
432
433 if (auto *Attr = FD->getAttr<CUDAClusterDimsAttr>()) {
434 auto GetExprVal = [&](const auto &E) {
435 return E ? E->EvaluateKnownConstInt(M.getContext()).getExtValue() : 1;
436 };
437 unsigned X = GetExprVal(Attr->getX());
438 unsigned Y = GetExprVal(Attr->getY());
439 unsigned Z = GetExprVal(Attr->getZ());
440 llvm::SmallString<32> AttrVal;
441 llvm::raw_svector_ostream OS(AttrVal);
442 OS << X << ',' << Y << ',' << Z;
443 F->addFnAttr("amdgpu-cluster-dims", AttrVal.str());
444 }
445
446 // OpenCL doesn't support cluster feature.
447 const TargetInfo &TTI = M.getContext().getTargetInfo();
448 if ((IsOpenCLKernel &&
449 TTI.hasFeatureEnabled(TTI.getTargetOpts().FeatureMap, "clusters")) ||
450 FD->hasAttr<CUDANoClusterAttr>())
451 F->addFnAttr("amdgpu-cluster-dims", "0,0,0");
452}
453
454void AMDGPUTargetCodeGenInfo::setTargetAttributes(
455 const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
457 GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
458 GV->setDSOLocal(true);
459 }
460
461 if (GV->isDeclaration())
462 return;
463
464 llvm::Function *F = dyn_cast<llvm::Function>(GV);
465 if (!F)
466 return;
467
468 const FunctionDecl *FD = dyn_cast_or_null<FunctionDecl>(D);
469 if (FD)
470 setFunctionDeclAttributes(FD, F, M);
471 if (!getABIInfo().getCodeGenOpts().EmitIEEENaNCompliantInsts)
472 F->addFnAttr("amdgpu-ieee", "false");
473 if (getABIInfo().getCodeGenOpts().AMDGPUExpandWaitcntProfiling)
474 F->addFnAttr("amdgpu-expand-waitcnt-profiling");
475}
476
477unsigned AMDGPUTargetCodeGenInfo::getDeviceKernelCallingConv() const {
478 return llvm::CallingConv::AMDGPU_KERNEL;
479}
480
481// Currently LLVM assumes null pointers always have value 0,
482// which results in incorrectly transformed IR. Therefore, instead of
483// emitting null pointers in private and local address spaces, a null
484// pointer in generic address space is emitted which is casted to a
485// pointer in local or private address space.
486llvm::Constant *AMDGPUTargetCodeGenInfo::getNullPointer(
487 const CodeGen::CodeGenModule &CGM, llvm::PointerType *PT,
488 QualType QT) const {
489 if (CGM.getContext().getTargetNullPointerValue(QT) == 0)
490 return llvm::ConstantPointerNull::get(PT);
491
492 auto &Ctx = CGM.getContext();
493 auto NPT = llvm::PointerType::get(
494 PT->getContext(), Ctx.getTargetAddressSpace(LangAS::opencl_generic));
495 return llvm::ConstantExpr::getAddrSpaceCast(
496 llvm::ConstantPointerNull::get(NPT), PT);
497}
498
499LangAS
500AMDGPUTargetCodeGenInfo::getSRetAddrSpace(const CXXRecordDecl *RD) const {
501 // Types with no viable copy/move must be constructed in-place , use the
502 // default AS so the sret pointer matches the "this" convention.
503 if (RD && !RD->canPassInRegisters())
504 return LangAS::Default;
506 getABIInfo().getDataLayout().getAllocaAddrSpace());
507}
508
509LangAS
510AMDGPUTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
511 const VarDecl *D) const {
512 assert(!CGM.getLangOpts().OpenCL &&
513 !(CGM.getLangOpts().CUDA && CGM.getLangOpts().CUDAIsDevice) &&
514 "Address space agnostic languages only");
515 LangAS DefaultGlobalAS = getLangASFromTargetAS(
516 CGM.getContext().getTargetAddressSpace(LangAS::opencl_global));
517 if (!D)
518 return DefaultGlobalAS;
519
520 LangAS AddrSpace = D->getType().getAddressSpace();
521 if (AddrSpace != LangAS::Default)
522 return AddrSpace;
523
524 // Only promote to address space 4 if VarDecl has constant initialization.
525 if (D->getType().isConstantStorage(CGM.getContext(), false, false) &&
527 if (auto ConstAS = CGM.getTarget().getConstantAddressSpace())
528 return *ConstAS;
529 }
530 return DefaultGlobalAS;
531}
532
533StringRef AMDGPUTargetCodeGenInfo::getLLVMSyncScopeStr(
534 const LangOptions &LangOpts, SyncScope Scope,
535 llvm::AtomicOrdering Ordering) const {
536
537 // OpenCL assumes by default that atomic scopes are per-address space for
538 // non-sequentially consistent operations.
539 bool IsOneAs = (Scope >= SyncScope::OpenCLWorkGroup &&
540 Scope <= SyncScope::OpenCLSubGroup &&
541 Ordering != llvm::AtomicOrdering::SequentiallyConsistent);
542
543 llvm::AtomicScope AS = getAtomicScope(Scope);
544 assert((AS != llvm::AtomicScope::Cluster || !IsOneAs) &&
545 "OpenCL does not have cluster scope");
546 return *llvm::getAtomicScopeIRString(getABIInfo().getTarget().getTriple(), AS,
547 IsOneAs);
548}
549
550void AMDGPUTargetCodeGenInfo::setTargetAtomicMetadata(
551 CodeGenFunction &CGF, llvm::Instruction &AtomicInst,
552 const AtomicExpr *AE) const {
553 auto *RMW = dyn_cast<llvm::AtomicRMWInst>(&AtomicInst);
554 auto *CmpX = dyn_cast<llvm::AtomicCmpXchgInst>(&AtomicInst);
555
556 // OpenCL and old style HIP atomics consider atomics targeting thread private
557 // memory to be undefined.
558 //
559 // TODO: This is probably undefined for atomic load/store, but there's not
560 // much direct codegen benefit to knowing this.
561 if (((RMW && RMW->getPointerAddressSpace() == llvm::AMDGPUAS::FLAT_ADDRESS) ||
562 (CmpX &&
563 CmpX->getPointerAddressSpace() == llvm::AMDGPUAS::FLAT_ADDRESS)) &&
565 llvm::MDBuilder MDHelper(CGF.getLLVMContext());
566 llvm::MDNode *ASRange = MDHelper.createRange(
567 llvm::APInt(32, llvm::AMDGPUAS::PRIVATE_ADDRESS),
568 llvm::APInt(32, llvm::AMDGPUAS::PRIVATE_ADDRESS + 1));
569 AtomicInst.setMetadata(llvm::LLVMContext::MD_noalias_addrspace, ASRange);
570 }
571
572 CGF.AddAMDGPUAvailableVisibleMMRA(&AtomicInst);
573
574 if (!RMW)
575 return;
576
577 AtomicOptions AO = CGF.CGM.getAtomicOpts();
578 llvm::MDNode *Empty = llvm::MDNode::get(CGF.getLLVMContext(), {});
580 RMW->setMetadata("amdgpu.no.fine.grained.memory", Empty);
582 RMW->setMetadata("amdgpu.no.remote.memory", Empty);
584 RMW->getOperation() == llvm::AtomicRMWInst::FAdd &&
585 RMW->getType()->isFloatTy())
586 RMW->setMetadata("amdgpu.ignore.denormal.mode", Empty);
587}
588
589bool AMDGPUTargetCodeGenInfo::shouldEmitStaticExternCAliases() const {
590 return false;
591}
592
593bool AMDGPUTargetCodeGenInfo::shouldEmitDWARFBitFieldSeparators() const {
594 return true;
595}
596
597void AMDGPUTargetCodeGenInfo::setCUDAKernelCallingConvention(
598 const FunctionType *&FT) const {
599 FT = getABIInfo().getContext().adjustFunctionType(
601}
602
603/// Return IR struct type for rtinfo struct in rocm-device-libs used for device
604/// enqueue.
605///
606/// ptr addrspace(1) kernel_object, i32 private_segment_size,
607/// i32 group_segment_size
608
609static llvm::StructType *
610getAMDGPURuntimeHandleType(llvm::LLVMContext &C,
611 llvm::Type *KernelDescriptorPtrTy) {
612 llvm::Type *Int32 = llvm::Type::getInt32Ty(C);
613 return llvm::StructType::create(C, {KernelDescriptorPtrTy, Int32, Int32},
614 "block.runtime.handle.t");
615}
616
617/// Create an OpenCL kernel for an enqueued block.
618///
619/// The type of the first argument (the block literal) is the struct type
620/// of the block literal instead of a pointer type. The first argument
621/// (block literal) is passed directly by value to the kernel. The kernel
622/// allocates the same type of struct on stack and stores the block literal
623/// to it and passes its pointer to the block invoke function. The kernel
624/// has "enqueued-block" function attribute and kernel argument metadata.
625llvm::Value *AMDGPUTargetCodeGenInfo::createEnqueuedBlockKernel(
626 CodeGenFunction &CGF, llvm::Function *Invoke, llvm::Type *BlockTy) const {
627 auto &Builder = CGF.Builder;
628 auto &C = CGF.getLLVMContext();
629
630 auto *InvokeFT = Invoke->getFunctionType();
631 llvm::SmallVector<llvm::Type *, 2> ArgTys;
632 llvm::SmallVector<llvm::Metadata *, 8> AddressQuals;
633 llvm::SmallVector<llvm::Metadata *, 8> AccessQuals;
634 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeNames;
635 llvm::SmallVector<llvm::Metadata *, 8> ArgBaseTypeNames;
636 llvm::SmallVector<llvm::Metadata *, 8> ArgTypeQuals;
637 llvm::SmallVector<llvm::Metadata *, 8> ArgNames;
638
639 ArgTys.push_back(BlockTy);
640 ArgTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
641 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(0)));
642 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "__block_literal"));
643 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
644 AccessQuals.push_back(llvm::MDString::get(C, "none"));
645 ArgNames.push_back(llvm::MDString::get(C, "block_literal"));
646 for (unsigned I = 1, E = InvokeFT->getNumParams(); I < E; ++I) {
647 ArgTys.push_back(InvokeFT->getParamType(I));
648 ArgTypeNames.push_back(llvm::MDString::get(C, "void*"));
649 AddressQuals.push_back(llvm::ConstantAsMetadata::get(Builder.getInt32(3)));
650 AccessQuals.push_back(llvm::MDString::get(C, "none"));
651 ArgBaseTypeNames.push_back(llvm::MDString::get(C, "void*"));
652 ArgTypeQuals.push_back(llvm::MDString::get(C, ""));
653 ArgNames.push_back(
654 llvm::MDString::get(C, (Twine("local_arg") + Twine(I)).str()));
655 }
656
657 llvm::Module &Mod = CGF.CGM.getModule();
658 const llvm::DataLayout &DL = Mod.getDataLayout();
659
660 llvm::Twine Name = Invoke->getName() + "_kernel";
661 auto *FT = llvm::FunctionType::get(llvm::Type::getVoidTy(C), ArgTys, false);
662
663 // The kernel itself can be internal, the runtime does not directly access the
664 // kernel address (only the kernel descriptor).
665 auto *F = llvm::Function::Create(FT, llvm::GlobalValue::InternalLinkage, Name,
666 &Mod);
667 F->setCallingConv(getDeviceKernelCallingConv());
668
669 llvm::AttrBuilder KernelAttrs(C);
670 // FIXME: The invoke isn't applying the right attributes either
671 // FIXME: This is missing setTargetAttributes
673 F->addFnAttrs(KernelAttrs);
674
675 auto IP = CGF.Builder.saveIP();
676 auto *BB = llvm::BasicBlock::Create(C, "entry", F);
677 Builder.SetInsertPoint(BB);
678 const auto BlockAlign = DL.getPrefTypeAlign(BlockTy);
679 auto *BlockPtr = Builder.CreateAlloca(BlockTy, nullptr);
680 BlockPtr->setAlignment(BlockAlign);
681 Builder.CreateAlignedStore(F->arg_begin(), BlockPtr, BlockAlign);
682 auto *Cast = Builder.CreatePointerCast(BlockPtr, InvokeFT->getParamType(0));
683 llvm::SmallVector<llvm::Value *, 2> Args;
684 Args.push_back(Cast);
685 for (llvm::Argument &A : llvm::drop_begin(F->args()))
686 Args.push_back(&A);
687 llvm::CallInst *call = Builder.CreateCall(Invoke, Args);
688 call->setCallingConv(Invoke->getCallingConv());
689 Builder.CreateRetVoid();
690 Builder.restoreIP(IP);
691
692 F->setMetadata("kernel_arg_addr_space", llvm::MDNode::get(C, AddressQuals));
693 F->setMetadata("kernel_arg_access_qual", llvm::MDNode::get(C, AccessQuals));
694 F->setMetadata("kernel_arg_type", llvm::MDNode::get(C, ArgTypeNames));
695 F->setMetadata("kernel_arg_base_type",
696 llvm::MDNode::get(C, ArgBaseTypeNames));
697 F->setMetadata("kernel_arg_type_qual", llvm::MDNode::get(C, ArgTypeQuals));
698 if (CGF.CGM.getCodeGenOpts().EmitOpenCLArgMetadata)
699 F->setMetadata("kernel_arg_name", llvm::MDNode::get(C, ArgNames));
700
701 llvm::StructType *HandleTy = getAMDGPURuntimeHandleType(
702 C, llvm::PointerType::get(C, DL.getDefaultGlobalsAddressSpace()));
703 llvm::Constant *RuntimeHandleInitializer =
704 llvm::ConstantAggregateZero::get(HandleTy);
705
706 llvm::Twine RuntimeHandleName = F->getName() + ".runtime.handle";
707
708 // The runtime needs access to the runtime handle as an external symbol. The
709 // runtime handle will need to be made external later, in
710 // AMDGPUExportOpenCLEnqueuedBlocks. The kernel itself has a hidden reference
711 // inside the runtime handle, and is not directly referenced.
712
713 // TODO: We would initialize the first field by declaring F->getName() + ".kd"
714 // to reference the kernel descriptor. The runtime wouldn't need to bother
715 // setting it. We would need to have a final symbol name though.
716 // TODO: Can we directly use an external symbol with getGlobalIdentifier?
717 auto *RuntimeHandle = new llvm::GlobalVariable(
718 Mod, HandleTy,
719 /*isConstant=*/true, llvm::GlobalValue::InternalLinkage,
720 /*Initializer=*/RuntimeHandleInitializer, RuntimeHandleName,
721 /*InsertBefore=*/nullptr, llvm::GlobalValue::NotThreadLocal,
722 DL.getDefaultGlobalsAddressSpace(),
723 /*isExternallyInitialized=*/true);
724
725 llvm::MDNode *HandleAsMD =
726 llvm::MDNode::get(C, llvm::ValueAsMetadata::get(RuntimeHandle));
727 F->setMetadata(llvm::LLVMContext::MD_associated, HandleAsMD);
728
729 RuntimeHandle->setSection(".amdgpu.kernel.runtime.handle");
730
731 CGF.CGM.addUsedGlobal(F);
732 CGF.CGM.addUsedGlobal(RuntimeHandle);
733 return RuntimeHandle;
734}
735
737 llvm::Function *F, const AMDGPUFlatWorkGroupSizeAttr *FlatWGS,
738 const ReqdWorkGroupSizeAttr *ReqdWGS, int32_t *MinThreadsVal,
739 int32_t *MaxThreadsVal) {
740 unsigned Min = 0;
741 unsigned Max = 0;
742 auto Eval = [&](Expr *E) {
743 return E->EvaluateKnownConstInt(getContext()).getExtValue();
744 };
745 if (ReqdWGS) {
746 Min = Max = Eval(ReqdWGS->getXDim()) * Eval(ReqdWGS->getYDim()) *
747 Eval(ReqdWGS->getZDim());
748 } else if (FlatWGS) {
749 Min = Eval(FlatWGS->getMin());
750 Max = Eval(FlatWGS->getMax());
751 }
752
753 if (Min != 0 || ReqdWGS) {
754 assert(Min <= Max && "Min must be less than or equal Max");
755
756 if (MinThreadsVal)
757 *MinThreadsVal = Min;
758 if (MaxThreadsVal)
759 *MaxThreadsVal = Max;
760 std::string AttrVal = llvm::utostr(Min) + "," + llvm::utostr(Max);
761 if (F)
762 F->addFnAttr("amdgpu-flat-work-group-size", AttrVal);
763 } else
764 assert(Max == 0 && "Max must be zero");
765}
766
768 llvm::Function *F, const AMDGPUWavesPerEUAttr *Attr) {
769 unsigned Min =
770 Attr->getMin()->EvaluateKnownConstInt(getContext()).getExtValue();
771 unsigned Max =
772 Attr->getMax()
773 ? Attr->getMax()->EvaluateKnownConstInt(getContext()).getExtValue()
774 : 0;
775
776 if (Min != 0) {
777 assert((Max == 0 || Min <= Max) && "Min must be less than or equal Max");
778
779 std::string AttrVal = llvm::utostr(Min);
780 if (Max != 0)
781 AttrVal = AttrVal + "," + llvm::utostr(Max);
782 F->addFnAttr("amdgpu-waves-per-eu", AttrVal);
783 } else
784 assert(Max == 0 && "Max must be zero");
785}
786
787std::unique_ptr<TargetCodeGenInfo>
789 return std::make_unique<AMDGPUTargetCodeGenInfo>(CGM.getTypes());
790}
static void setCUDAKernelCallingConvention(CanQualType &FTy, CodeGenModule &CGM, const FunctionDecl *FD)
Set calling convention for CUDA/HIP kernel.
Definition CGCall.cpp:379
static bool requiresAMDGPUProtectedVisibility(const Decl *D, llvm::GlobalValue *GV)
Definition AMDGPU.cpp:327
static llvm::StructType * getAMDGPURuntimeHandleType(llvm::LLVMContext &C, llvm::Type *KernelDescriptorPtrTy)
Return IR struct type for rtinfo struct in rocm-device-libs used for device enqueue.
Definition AMDGPU.cpp:610
Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....
#define X(type, name)
Definition Value.h:97
static StringRef getTriple(const Command &Job)
uint64_t getTargetNullPointerValue(QualType QT) const
Get target-dependent integer value for null pointer which is used for constant folding.
const TargetInfo & getTargetInfo() const
Definition ASTContext.h:942
unsigned getTargetAddressSpace(LangAS AS) const
bool threadPrivateMemoryAtomicsAreUndefined() const
Return true if atomics operations targeting allocations in private memory are undefined.
Definition Expr.h:7069
Attr - This represents one attribute.
Definition Attr.h:46
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition CharUnits.h:63
static ABIArgInfo getIgnore()
static ABIArgInfo getDirect(llvm::Type *T=nullptr, unsigned Offset=0, llvm::Type *Padding=nullptr, bool CanBeFlattened=true, unsigned Align=0)
static ABIArgInfo getIndirectAliased(CharUnits Alignment, unsigned AddrSpace, bool Realign=false, llvm::Type *Padding=nullptr)
Pass this in memory using the IR byref attribute.
@ RAA_DirectInMemory
Pass it on the stack using its defined layout.
Definition CGCXXABI.h:158
unsigned getCallingConvention() const
getCallingConvention - Return the user specified calling convention, which has been translated into a...
CanQualType getReturnType() const
MutableArrayRef< ArgInfo > arguments()
void AddAMDGPUAvailableVisibleMMRA(llvm::Instruction *Inst)
Attach the AMDGPU availability/visibility MMRA to Inst when the amdgpu_av attribute is active on the ...
Definition AMDGPU.cpp:491
llvm::LLVMContext & getLLVMContext()
This class organizes the cross-function state that is used while generating LLVM code.
llvm::Module & getModule() const
void handleAMDGPUWavesPerEUAttr(llvm::Function *F, const AMDGPUWavesPerEUAttr *A)
Emit the IR encoding to attach the AMD GPU waves-per-eu attribute to F.
Definition AMDGPU.cpp:767
const LangOptions & getLangOpts() const
const TargetInfo & getTarget() const
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
void handleAMDGPUFlatWorkGroupSizeAttr(llvm::Function *F, const AMDGPUFlatWorkGroupSizeAttr *A, const ReqdWorkGroupSizeAttr *ReqdWGS=nullptr, int32_t *MinThreadsVal=nullptr, int32_t *MaxThreadsVal=nullptr)
Emit the IR encoding to attach the AMD GPU flat-work-group-size attribute to F.
Definition AMDGPU.cpp:736
AtomicOptions getAtomicOpts()
Get the current Atomic options.
ASTContext & getContext() const
const CodeGenOptions & getCodeGenOpts() const
void addDefaultFunctionDefinitionAttributes(llvm::AttrBuilder &attrs)
Like the overload taking a Function &, but intended specifically for frontends that want to build on ...
Definition CGCall.cpp:2557
DefaultABIInfo - The default implementation for ABI specific details.
Definition ABIInfoImpl.h:21
ABIArgInfo classifyArgumentType(QualType RetTy) const
ABIArgInfo classifyReturnType(QualType RetTy) const
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
T * getAttr() const
Definition DeclBase.h:581
bool hasAttr() const
Definition DeclBase.h:585
This represents one expression.
Definition Expr.h:112
ExtInfo withCallingConv(CallingConv cc) const
Definition TypeBase.h:4840
ExtInfo getExtInfo() const
Definition TypeBase.h:4973
A (possibly-)qualified type.
Definition TypeBase.h:938
LangAS getAddressSpace() const
Return the address space of this type.
Definition TypeBase.h:8630
bool isConstantStorage(const ASTContext &Ctx, bool ExcludeCtor, bool ExcludeDtor)
Definition TypeBase.h:1037
bool canPassInRegisters() const
Determine whether this class can be passed in registers.
Definition Decl.h:4596
bool hasFlexibleArrayMember() const
Definition Decl.h:4492
TargetOptions & getTargetOpts() const
Retrieve the target options.
Definition TargetInfo.h:333
virtual std::optional< LangAS > getConstantAddressSpace() const
Return an AST address space which can be used opportunistically for constant global memory.
virtual bool hasFeatureEnabled(const llvm::StringMap< bool > &Features, StringRef Name) const
Check if target has a given feature enabled.
llvm::StringMap< bool > FeatureMap
The map of which features have been enabled disabled based on the command line.
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9340
QualType getType() const
Definition Decl.h:723
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2640
ABIArgInfo classifyArgumentType(CodeGenModule &CGM, CanQualType type)
Classify the rules for how to pass a particular type.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
Definition CGValue.h:146
CGCXXABI::RecordArgABI getRecordArgABI(const RecordType *RT, CGCXXABI &CXXABI)
bool classifyReturnType(const CGCXXABI &CXXABI, CGFunctionInfo &FI, const ABIInfo &Info)
std::unique_ptr< TargetCodeGenInfo > createAMDGPUTargetCodeGenInfo(CodeGenModule &CGM)
Definition AMDGPU.cpp:788
RValue emitVoidPtrVAArg(CodeGenFunction &CGF, Address VAListAddr, QualType ValueTy, bool IsIndirect, TypeInfoChars ValueInfo, CharUnits SlotSizeAndAlign, bool AllowHigherAlign, AggValueSlot Slot, bool ForceRightAdjust=false)
Emit va_arg for a platform using the common void* representation, where arguments are simply emitted ...
bool isAggregateTypeForABI(QualType T)
const Type * isSingleElementStruct(QualType T, ASTContext &Context)
isSingleElementStruct - Determine if a structure is a "singleelement struct", i.e.
QualType useFirstFieldIfTransparentUnion(QualType Ty)
Pass transparent unions as if they were the type of the first element.
bool isEmptyRecord(ASTContext &Context, QualType T, bool AllowArrays, bool AsIfNoUniqueAddr=false)
isEmptyRecord - Return true iff a structure contains only empty fields.
@ OS
Indicates that the tracking object is a descendant of a referenced-counted OSObject,...
bool Cast(InterpState &S, CodePtr OpPC)
Definition Interp.h:2832
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
const FunctionProtoType * T
@ Type
The name was classified as a type.
Definition Sema.h:559
LangAS
Defines the address space values used by the address space qualifier of QualType.
llvm::AtomicScope getAtomicScope(SyncScope S)
Collapses a clang sync scope onto the target-neutral llvm::AtomicScope.
Definition TargetInfo.h:40
SyncScope
Defines sync scope values used internally by clang.
Definition SyncScope.h:43
@ CC_DeviceKernel
Definition Specifiers.h:293
U cast(CodeGen::Address addr)
Definition Address.h:327
LangAS getLangASFromTargetAS(unsigned TargetAS)
unsigned long uint64_t
__packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 uint32_t
bool getOption(AtomicOptionKind Kind) const