20#include "llvm/ADT/StringExtras.h"
21#include "llvm/Support/raw_ostream.h"
30handleAMDGPUFlatWorkGroupSizeAttr(
const FunctionDecl *fd, cir::FuncOp func,
32 bool isOpenCLKernel,
bool isHIPKernel,
33 unsigned lbMaxThreads) {
34 const auto *flatWGS = fd->
getAttr<AMDGPUFlatWorkGroupSizeAttr>();
38 if (flatWGS || reqdWGS) {
41 min = flatWGS->getMin()
44 max = flatWGS->getMax()
48 if (reqdWGS &&
min == 0 &&
max == 0) {
49 min =
max = reqdWGS->getXDim()
60 assert(
min <=
max &&
"Min must be less than or equal Max");
61 std::string attrVal = llvm::utostr(
min) +
"," + llvm::utostr(
max);
62 func->setAttr(cir::CIRDialect::getAMDGPUFlatWorkGroupSizeAttrName(),
63 builder.getStringAttr(attrVal));
65 assert(
max == 0 &&
"Max must be zero");
67 }
else if (lbMaxThreads > 0) {
68 func->setAttr(cir::CIRDialect::getAMDGPUFlatWorkGroupSizeAttrName(),
69 builder.getStringAttr(
"1," + llvm::utostr(lbMaxThreads)));
70 }
else if (isOpenCLKernel || isHIPKernel) {
73 const unsigned openCLDefaultMaxWorkGroupSize = 256;
74 const unsigned defaultMaxWorkGroupSize =
75 isOpenCLKernel ? openCLDefaultMaxWorkGroupSize
78 std::string(
"1,") + llvm::utostr(defaultMaxWorkGroupSize);
79 func->setAttr(cir::CIRDialect::getAMDGPUFlatWorkGroupSizeAttrName(),
80 builder.getStringAttr(attrVal));
85static void handleAMDGPUWavesPerEUAttr(
const FunctionDecl *fd, cir::FuncOp func,
88 unsigned lbMinWaves) {
89 const auto *
attr = fd->
getAttr<AMDGPUWavesPerEUAttr>();
93 func->setAttr(cir::CIRDialect::getAMDGPUWavesPerEUAttrName(),
94 builder.getStringAttr(llvm::utostr(lbMinWaves)));
106 assert((
max == 0 ||
min <=
max) &&
"Min must be less than or equal Max");
107 std::string attrVal = llvm::utostr(
min);
109 attrVal = attrVal +
"," + llvm::utostr(
max);
110 func->setAttr(cir::CIRDialect::getAMDGPUWavesPerEUAttrName(),
111 builder.getStringAttr(attrVal));
113 assert(
max == 0 &&
"Max must be zero");
118static void handleAMDGPUNumSGPRAttr(
const FunctionDecl *fd, cir::FuncOp func,
121 const auto *
attr = fd->
getAttr<AMDGPUNumSGPRAttr>();
127 func->setAttr(
"cir.amdgpu-num-sgpr",
128 builder.getStringAttr(llvm::utostr(numSGPR)));
133static void handleAMDGPUNumVGPRAttr(
const FunctionDecl *fd, cir::FuncOp func,
136 const auto *
attr = fd->
getAttr<AMDGPUNumVGPRAttr>();
142 func->setAttr(
"cir.amdgpu-num-vgpr",
143 builder.getStringAttr(llvm::utostr(numVGPR)));
148static void handleAMDGPUMaxNumWorkGroupsAttr(
const FunctionDecl *fd,
152 const auto *
attr = fd->
getAttr<AMDGPUMaxNumWorkGroupsAttr>();
159 ?
attr->getMaxNumWorkGroupsY()
164 ?
attr->getMaxNumWorkGroupsZ()
170 llvm::raw_svector_ostream os(attrVal);
171 os << x <<
',' << y <<
',' << z;
172 func->setAttr(
"cir.amdgpu-max-num-workgroups",
173 builder.getStringAttr(attrVal.str()));
177static void handleAMDGPUClusterDimsAttr(
const FunctionDecl *fd,
180 bool isOpenCLKernel) {
182 if (
const auto *
attr = fd->
getAttr<CUDAClusterDimsAttr>()) {
183 auto getExprVal = [&](
const Expr *e) {
184 return e ? e->EvaluateKnownConstInt(cgm.
getASTContext()).getExtValue()
187 unsigned x = getExprVal(
attr->getX());
188 unsigned y = getExprVal(
attr->getY());
189 unsigned z = getExprVal(
attr->getZ());
192 llvm::raw_svector_ostream os(attrVal);
193 os << x <<
',' << y <<
',' << z;
194 func->setAttr(
"cir.amdgpu-cluster-dims",
195 builder.getStringAttr(attrVal.str()));
199 if ((isOpenCLKernel &&
202 fd->
hasAttr<CUDANoClusterAttr>()) {
203 func->setAttr(
"cir.amdgpu-cluster-dims", builder.getStringAttr(
"0,0,0"));
208static void handleAMDGPUIEEEAttr(cir::FuncOp func,
CIRGenModule &cgm,
211 func->setAttr(
"cir.amdgpu-ieee", builder.getStringAttr(
"false"));
215static void handleAMDGPUExpandWaitcntProfilingAttr(cir::FuncOp func,
219 func->setAttr(
"cir.amdgpu-expand-waitcnt-profiling",
220 builder.getStringAttr(
""));
228 if (func.isDeclaration())
233 const auto *fd = dyn_cast_or_null<FunctionDecl>(
decl);
235 const bool isOpenCLKernel =
237 const bool isHIPKernel =
241 unsigned lbMaxThreads = 0;
242 unsigned lbMinWaves = 0;
244 isHIPKernel ? fd->
getAttr<CUDALaunchBoundsAttr>() :
nullptr) {
245 lbMaxThreads = lb->getMaxThreads()
248 if (
const Expr *minBlocks = lb->getMinBlocks())
250 minBlocks->EvaluateKnownConstInt(cgm.
getASTContext()).getExtValue();
253 handleAMDGPUFlatWorkGroupSizeAttr(fd, func, cgm, builder, isOpenCLKernel,
254 isHIPKernel, lbMaxThreads);
255 handleAMDGPUWavesPerEUAttr(fd, func, cgm, builder, lbMinWaves);
256 handleAMDGPUNumSGPRAttr(fd, func, cgm, builder);
257 handleAMDGPUNumVGPRAttr(fd, func, cgm, builder);
258 handleAMDGPUMaxNumWorkGroupsAttr(fd, func, cgm, builder);
259 handleAMDGPUClusterDimsAttr(fd, func, cgm, builder, isOpenCLKernel);
261 handleAMDGPUIEEEAttr(func, cgm, builder);
262 handleAMDGPUExpandWaitcntProfilingAttr(func, cgm, builder);
__DEVICE__ int min(int __a, int __b)
__DEVICE__ int max(int __a, int __b)
const TargetInfo & getTargetInfo() const
This class organizes the cross-function state that is used while generating CIR code.
clang::ASTContext & getASTContext() const
CIRGenBuilderTy & getBuilder()
const clang::CodeGenOptions & getCodeGenOpts() const
const clang::LangOptions & getLangOpts() const
This represents one expression.
Represents a function declaration or definition.
Exposes information about the current target.
TargetOptions & getTargetOpts() const
Retrieve the target options.
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.
Defines the clang::TargetInfo interface.
void setAMDGPUTargetFunctionAttributes(const clang::Decl *decl, cir::FuncOp func, CIRGenModule &cgm)
Set AMDGPU-specific function attributes for HIP kernels.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Top level wrappers for InstallAPI frontend operations.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...