Skip to content

[SYCL] Refactor reqd_work_group_size attribute implementation #5782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3985bb2
[SYCL] Refactor reqd_work_group_size attribute implementation
smanna12 Mar 10, 2022
6dd0cd5
Fix format issues
smanna12 Mar 10, 2022
4299ef6
Fix format errors
smanna12 Mar 10, 2022
a7833f1
Fix the clang-tidy build after work_group_size attr changes
smanna12 Mar 11, 2022
4588ed7
Fix Format errors with clang-tidy build fix
smanna12 Mar 11, 2022
9d18d2d
Fix Format errors with clang-tidy build fix
smanna12 Mar 11, 2022
5571595
Merge remote-tracking branch 'my_remote/sycl' into Refactor_Reqd_Work…
smanna12 Mar 11, 2022
c892481
address review comments
smanna12 Mar 13, 2022
7f2135c
Update num_simd_work_items attribute so that it takes ZExtValue inste…
smanna12 Mar 13, 2022
88f0d61
Add comparator as a template argument and get rid of one function
smanna12 Mar 13, 2022
dfcbd02
Fix format errors
smanna12 Mar 13, 2022
4032496
Fix build errors
smanna12 Mar 13, 2022
87314f9
Fix regressions
smanna12 Mar 15, 2022
ea792e3
Fix format errors
smanna12 Mar 15, 2022
9b964fc
Merge remote-tracking branch 'my_remote/sycl' into Refactor_Reqd_Work…
smanna12 Mar 15, 2022
88ce3de
Update comments
smanna12 Mar 15, 2022
6acca53
remov extra space
smanna12 Mar 15, 2022
39ae1a3
Merge remote-tracking branch 'my_remote/sycl' into Refactor_Reqd_Work…
smanna12 Mar 17, 2022
77f4283
update comments and fix regression when reqd_work_group_size attribut…
smanna12 Mar 18, 2022
041c839
Merge remote-tracking branch 'my_remote/sycl' into Refactor_Reqd_Work…
smanna12 Mar 18, 2022
bc1da4e
Fix format errors
smanna12 Mar 18, 2022
a2d1c23
address @elizabeth review comments
smanna12 Mar 18, 2022
16d2aec
Fix format errors
smanna12 Mar 18, 2022
34ba930
Update tests as per review comments
smanna12 Mar 18, 2022
5b924a0
Fix format errors
smanna12 Mar 18, 2022
1137c2d
update function name to make it more readable
smanna12 Mar 18, 2022
f376e43
Update comments
smanna12 Mar 21, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ void SingleWorkItemBarrierCheck::check(const MatchFinder::MatchResult &Result) {
bool IsNDRange = false;
if (MatchedDecl->hasAttr<ReqdWorkGroupSizeAttr>()) {
const auto *Attribute = MatchedDecl->getAttr<ReqdWorkGroupSizeAttr>();
if (*Attribute->getXDimVal(*Result.Context) > 1 ||
*Attribute->getYDimVal(*Result.Context) > 1 ||
*Attribute->getZDimVal(*Result.Context) > 1)
if (*Attribute->getXDimVal() > 1 || *Attribute->getYDimVal() > 1 ||
*Attribute->getZDimVal() > 1)
IsNDRange = true;
}
if (IsNDRange) // No warning if kernel is treated as an NDRange.
Expand Down
21 changes: 12 additions & 9 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -3251,17 +3251,20 @@ def ReqdWorkGroupSize : InheritableAttr {
ExprArgument<"ZDim", /*optional*/1>];
let Subjects = SubjectList<[Function], ErrorDiag>;
let AdditionalMembers = [{
ArrayRef<const Expr *> dimensions() const {
return {getXDim(), getYDim(), getZDim()};
}
Optional<llvm::APSInt> getXDimVal(ASTContext &Ctx) const {
return getXDim()->getIntegerConstantExpr(Ctx);
Optional<llvm::APSInt> getXDimVal() const {
if (const auto *CE = dyn_cast<ConstantExpr>(getXDim()))
return CE->getResultAsAPSInt();
return None;
}
Optional<llvm::APSInt> getYDimVal(ASTContext &Ctx) const {
return getYDim()->getIntegerConstantExpr(Ctx);
Optional<llvm::APSInt> getYDimVal() const {
if (const auto *CE = dyn_cast<ConstantExpr>(getYDim()))
return CE->getResultAsAPSInt();
return None;
}
Optional<llvm::APSInt> getZDimVal(ASTContext &Ctx) const {
return getZDim()->getIntegerConstantExpr(Ctx);
Optional<llvm::APSInt> getZDimVal() const {
if (const auto *CE = dyn_cast<ConstantExpr>(getZDim()))
return CE->getResultAsAPSInt();
return None;
}
}];
let Documentation = [ReqdWorkGroupSizeAttrDocs];
Expand Down
63 changes: 4 additions & 59 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -10541,9 +10541,6 @@ class Sema final {

void AddIntelFPGABankBitsAttr(Decl *D, const AttributeCommonInfo &CI,
Expr **Exprs, unsigned Size);
template <typename AttrType>
void addIntelTripleArgAttr(Decl *D, const AttributeCommonInfo &CI,
Expr *XDimExpr, Expr *YDimExpr, Expr *ZDimExpr);
void AddWorkGroupSizeHintAttr(Decl *D, const AttributeCommonInfo &CI,
Expr *XDim, Expr *YDim, Expr *ZDim);
WorkGroupSizeHintAttr *
Expand Down Expand Up @@ -10640,6 +10637,10 @@ class Sema final {
void AddSYCLAddIRAttributesGlobalVariableAttr(Decl *D,
const AttributeCommonInfo &CI,
MutableArrayRef<Expr *> Args);
void AddReqdWorkGroupSizeAttr(Decl *D, const AttributeCommonInfo &CI,
Expr *XDim, Expr *YDim, Expr *ZDim);
ReqdWorkGroupSizeAttr *
MergeReqdWorkGroupSizeAttr(Decl *D, const ReqdWorkGroupSizeAttr &A);
/// AddAlignedAttr - Adds an aligned attribute to a particular declaration.
void AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
bool IsPackExpansion);
Expand Down Expand Up @@ -13736,62 +13737,6 @@ class Sema final {
}
};

inline Expr *checkMaxWorkSizeAttrExpr(Sema &S, const AttributeCommonInfo &CI,
Expr *E) {
assert(E && "Attribute must have an argument.");

if (!E->isInstantiationDependent()) {
llvm::APSInt ArgVal;
ExprResult ICE = S.VerifyIntegerConstantExpression(E, &ArgVal);

if (ICE.isInvalid())
return nullptr;

E = ICE.get();

if (ArgVal.isNegative()) {
S.Diag(E->getExprLoc(),
diag::warn_attribute_requires_non_negative_integer_argument)
<< E->getType() << S.Context.UnsignedLongLongTy
<< E->getSourceRange();
return E;
}

unsigned Val = ArgVal.getZExtValue();
if (Val == 0) {
S.Diag(E->getExprLoc(), diag::err_attribute_argument_is_zero)
<< CI << E->getSourceRange();
return nullptr;
}
}
return E;
}

template <typename WorkGroupAttrType>
void Sema::addIntelTripleArgAttr(Decl *D, const AttributeCommonInfo &CI,
Expr *XDimExpr, Expr *YDimExpr,
Expr *ZDimExpr) {

assert((XDimExpr && YDimExpr && ZDimExpr) &&
"argument has unexpected null value");

// Accept template arguments for now as they depend on something else.
// We'll get to check them when they eventually get instantiated.
if (!XDimExpr->isValueDependent() && !YDimExpr->isValueDependent() &&
!ZDimExpr->isValueDependent()) {

// Save ConstantExpr in semantic attribute
XDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, XDimExpr);
YDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, YDimExpr);
ZDimExpr = checkMaxWorkSizeAttrExpr(*this, CI, ZDimExpr);

if (!XDimExpr || !YDimExpr || !ZDimExpr)
return;
}
D->addAttr(::new (Context)
WorkGroupAttrType(Context, CI, XDimExpr, YDimExpr, ZDimExpr));
}

/// RAII object that enters a new expression evaluation context.
class EnterExpressionEvaluationContext {
Sema &Actions;
Expand Down
23 changes: 7 additions & 16 deletions clang/lib/CodeGen/CodeGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,22 +636,13 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
}

if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
ASTContext &ClangCtx = FD->getASTContext();
Optional<llvm::APSInt> XDimVal = A->getXDimVal(ClangCtx);
Optional<llvm::APSInt> YDimVal = A->getYDimVal(ClangCtx);
Optional<llvm::APSInt> ZDimVal = A->getZDimVal(ClangCtx);

// For a SYCLDevice ReqdWorkGroupSizeAttr arguments are reversed.
if (getLangOpts().SYCLIsDevice)
std::swap(XDimVal, ZDimVal);

// Attributes arguments (first and third) are reversed on SYCLDevice.
llvm::Metadata *AttrMDArgs[] = {
llvm::ConstantAsMetadata::get(
Builder.getInt32(XDimVal->getZExtValue())),
llvm::ConstantAsMetadata::get(
Builder.getInt32(YDimVal->getZExtValue())),
llvm::ConstantAsMetadata::get(
Builder.getInt32(ZDimVal->getZExtValue()))};
llvm::ConstantAsMetadata::get(Builder.getInt(
getLangOpts().SYCLIsDevice ? *A->getZDimVal() : *A->getXDimVal())),
llvm::ConstantAsMetadata::get(Builder.getInt(*A->getYDimVal())),
llvm::ConstantAsMetadata::get(Builder.getInt(
getLangOpts().SYCLIsDevice ? *A->getXDimVal() : *A->getZDimVal()))};
Fn->setMetadata("reqd_work_group_size",
llvm::MDNode::get(Context, AttrMDArgs));
}
Expand Down Expand Up @@ -715,7 +706,7 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
const auto *CE = cast<ConstantExpr>(A->getValue());
Optional<llvm::APSInt> ArgVal = CE->getResultAsAPSInt();
llvm::Metadata *AttrMDArgs[] = {llvm::ConstantAsMetadata::get(
Builder.getInt32(ArgVal->getSExtValue()))};
Builder.getInt32(ArgVal->getZExtValue()))};
Fn->setMetadata("num_simd_work_items",
llvm::MDNode::get(Context, AttrMDArgs));
}
Expand Down
13 changes: 6 additions & 7 deletions clang/lib/CodeGen/TargetInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8372,10 +8372,9 @@ void TCETargetCodeGenInfo::setTargetAttributes(

SmallVector<llvm::Metadata *, 5> Operands;
Operands.push_back(llvm::ConstantAsMetadata::get(F));
ASTContext &Ctx = M.getContext();
unsigned XDim = Attr->getXDimVal(Ctx)->getZExtValue();
unsigned YDim = Attr->getYDimVal(Ctx)->getZExtValue();
unsigned ZDim = Attr->getZDimVal(Ctx)->getZExtValue();
unsigned XDim = Attr->getXDimVal()->getZExtValue();
unsigned YDim = Attr->getYDimVal()->getZExtValue();
unsigned ZDim = Attr->getZDimVal()->getZExtValue();

Operands.push_back(llvm::ConstantAsMetadata::get(
llvm::Constant::getIntegerValue(M.Int32Ty, llvm::APInt(32, XDim))));
Expand Down Expand Up @@ -9255,9 +9254,9 @@ void AMDGPUTargetCodeGenInfo::setFunctionDeclAttributes(
Max = FlatWGS->getMax()->EvaluateKnownConstInt(Ctx).getExtValue();
}
if (ReqdWGS) {
XDim = ReqdWGS->getXDimVal(Ctx)->getZExtValue();
YDim = ReqdWGS->getYDimVal(Ctx)->getZExtValue();
ZDim = ReqdWGS->getZDimVal(Ctx)->getZExtValue();
XDim = ReqdWGS->getXDimVal()->getZExtValue();
YDim = ReqdWGS->getYDimVal()->getZExtValue();
ZDim = ReqdWGS->getZDimVal()->getZExtValue();
}
if (ReqdWGS && Min == 0 && Max == 0)
Min = Max = XDim * YDim * ZDim;
Expand Down
25 changes: 2 additions & 23 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,8 @@ static bool mergeDeclAttribute(Sema &S, NamedDecl *D,
else if (const auto *A =
dyn_cast<SYCLAddIRAttributesGlobalVariableAttr>(Attr))
NewAttr = S.MergeSYCLAddIRAttributesGlobalVariableAttr(D, *A);
else if (const auto *A = dyn_cast<ReqdWorkGroupSizeAttr>(Attr))
NewAttr = S.MergeReqdWorkGroupSizeAttr(D, *A);
else if (Attr->shouldInheritEvenIfAlreadyPresent() || !DeclHasAttr(D, Attr))
NewAttr = cast<InheritableAttr>(Attr->clone(S.Context));

Expand Down Expand Up @@ -3395,27 +3397,6 @@ static void adjustDeclContextForDeclaratorDecl(DeclaratorDecl *NewD,
FixSemaDC(VD->getDescribedVarTemplate());
}

template <typename AttributeType>
static void checkDimensionsAndSetDiagnostics(Sema &S, FunctionDecl *New,
FunctionDecl *Old) {
const auto *NewDeclAttr = New->getAttr<AttributeType>();
const auto *OldDeclAttr = Old->getAttr<AttributeType>();

if (!NewDeclAttr || !OldDeclAttr)
return;

ASTContext &Ctx = S.getASTContext();
if (NewDeclAttr->getXDimVal(Ctx) != OldDeclAttr->getXDimVal(Ctx) ||
NewDeclAttr->getYDimVal(Ctx) != OldDeclAttr->getYDimVal(Ctx) ||
NewDeclAttr->getZDimVal(Ctx) != OldDeclAttr->getZDimVal(Ctx)) {
S.Diag(New->getLocation(), diag::err_conflicting_sycl_function_attributes)
<< OldDeclAttr << NewDeclAttr;
S.Diag(New->getLocation(), diag::warn_duplicate_attribute) << OldDeclAttr;
S.Diag(OldDeclAttr->getLocation(), diag::note_conflicting_attribute);
S.Diag(NewDeclAttr->getLocation(), diag::note_conflicting_attribute);
}
}

/// MergeFunctionDecl - We just parsed a function 'New' from
/// declarator D which has the same name and scope as a previous
/// declaration 'Old'. Figure out how to resolve this situation,
Expand Down Expand Up @@ -3504,8 +3485,6 @@ bool Sema::MergeFunctionDecl(FunctionDecl *New, NamedDecl *&OldD,
}
}

checkDimensionsAndSetDiagnostics<ReqdWorkGroupSizeAttr>(*this, New, Old);

if (const auto *ILA = New->getAttr<InternalLinkageAttr>())
if (!Old->hasAttr<InternalLinkageAttr>()) {
Diag(New->getLocation(), diag::err_attribute_missing_on_first_decl)
Expand Down
Loading