Skip to content

Commit ca9d816

Browse files
authored
[SYCL] [FPGA] Add template parameter support for work_group_size attributes (#2906)
This patch adds support for template parameter on triple arguments SYCL function attributes such as: 1. [[intel::reqd_work_group_size()]] 2. [[cl::reqd_work_group_size()]] 3. [[intel::max_work_group_size()]] Default arguments in ReqWorkGroupSize can be used only with intel::reqd_work_group_size spelling. Checks correctness of mutual usage of different work_group_size attributes: reqd_work_group_size, max_work_group_size and max_global_work_dim. Values of reqd_work_group_size arguments shall be equal or less than values coming from max_work_group_size. In case the value of 'max_global_work_dim' attribute equals to 0 we shall ensure that if max_work_group_size and reqd_work_group_size attributes exist, they hold equal values (1, 1, 1). This patch does not add template parameter support on WorkGroupSizeHintAttr since the attribute does not have a [[]] spelling because it is an OpenCL-related attribute. updates sema/codegen tests with mock headers on device. splits tests of 'intel-max-work-group-size' and 'intel-reqd-work-group-size' attributes into host and device. Signed-off-by: Soumi Manna <[email protected]>
1 parent aa84cde commit ca9d816

26 files changed

+1078
-400
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,13 +1287,18 @@ def SYCLIntelSchedulerTargetFmaxMhz : InheritableAttr {
12871287
def SYCLIntelMaxWorkGroupSize : InheritableAttr {
12881288
let Spellings = [CXX11<"intelfpga","max_work_group_size">,
12891289
CXX11<"intel","max_work_group_size">];
1290-
let Args = [UnsignedArgument<"XDim">,
1291-
UnsignedArgument<"YDim">,
1292-
UnsignedArgument<"ZDim">];
1290+
let Args = [ExprArgument<"XDim">,
1291+
ExprArgument<"YDim">,
1292+
ExprArgument<"ZDim">];
12931293
let LangOpts = [SYCLIsDevice, SYCLIsHost];
12941294
let Subjects = SubjectList<[Function], ErrorDiag>;
1295-
let Documentation = [SYCLIntelMaxWorkGroupSizeAttrDocs];
12961295
let PragmaAttributeSupport = 0;
1296+
let AdditionalMembers = [{
1297+
ArrayRef<const Expr *> dimensions() const {
1298+
return {getXDim(), getYDim(), getZDim()};
1299+
}
1300+
}];
1301+
let Documentation = [SYCLIntelMaxWorkGroupSizeAttrDocs];
12971302
}
12981303

12991304
def SYCLIntelMaxGlobalWorkDim : InheritableAttr {
@@ -2825,9 +2830,15 @@ def ReqdWorkGroupSize : InheritableAttr {
28252830
let Spellings = [GNU<"reqd_work_group_size">,
28262831
CXX11<"intel","reqd_work_group_size">,
28272832
CXX11<"cl","reqd_work_group_size">];
2828-
let Args = [UnsignedArgument<"XDim">, DefaultUnsignedArgument<"YDim", 1>,
2829-
DefaultUnsignedArgument<"ZDim", 1>];
2833+
let Args = [ExprArgument<"XDim">,
2834+
ExprArgument<"YDim", /*optional*/1>,
2835+
ExprArgument<"ZDim", /*optional*/1>];
28302836
let Subjects = SubjectList<[Function], ErrorDiag>;
2837+
let AdditionalMembers = [{
2838+
ArrayRef<const Expr *> dimensions() const {
2839+
return {getXDim(), getYDim(), getZDim()};
2840+
}
2841+
}];
28312842
let Documentation = [ReqdWorkGroupSizeAttrDocs];
28322843
}
28332844

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,8 +667,10 @@ def NSReturnsMismatch : DiagGroup<"nsreturns-mismatch">;
667667
def IndependentClassAttribute : DiagGroup<"IndependentClass-attribute">;
668668
def UnknownAttributes : DiagGroup<"unknown-attributes">;
669669
def IgnoredAttributes : DiagGroup<"ignored-attributes">;
670+
def AcceptedAttributes : DiagGroup<"accepted-attributes">;
670671
def Attributes : DiagGroup<"attributes", [UnknownAttributes,
671-
IgnoredAttributes]>;
672+
IgnoredAttributes,
673+
AcceptedAttributes]>;
672674
def UnknownSanitizers : DiagGroup<"unknown-sanitizers">;
673675
def UnnamedTypeTemplateArgs : DiagGroup<"unnamed-type-template-args",
674676
[CXX98CompatUnnamedTypeTemplateArgs]>;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11205,6 +11205,10 @@ def warn_attribute_spelling_deprecated : Warning<
1120511205
InGroup<DeprecatedAttributes>;
1120611206
def note_spelling_suggestion : Note<
1120711207
"did you mean to use %0 instead?">;
11208+
def warn_attribute_requires_non_negative_integer_argument : Warning<
11209+
"the resulting value of the %0 attribute %select{first|second|third}1"
11210+
" parameter is always non-negative after implicit conversion">,
11211+
InGroup<AcceptedAttributes>;
1120811212

1120911213
// errors of expect.with.probability
1121011214
def err_probability_not_constant_float : Error<

clang/include/clang/Sema/Sema.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10083,6 +10083,10 @@ class Sema final {
1008310083
template <typename AttrType>
1008410084
void addIntelSYCLSingleArgFunctionAttr(Decl *D, const AttributeCommonInfo &CI,
1008510085
Expr *E);
10086+
template <typename AttrType>
10087+
void addIntelSYCLTripleArgFunctionAttr(Decl *D, const AttributeCommonInfo &CI,
10088+
Expr *XDimExpr, Expr *YDimExpr,
10089+
Expr *ZDimExpr);
1008610090
/// AddAlignedAttr - Adds an aligned attribute to a particular declaration.
1008710091
void AddAlignedAttr(Decl *D, const AttributeCommonInfo &CI, Expr *E,
1008810092
bool IsPackExpansion);

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,22 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
620620
}
621621

622622
if (const ReqdWorkGroupSizeAttr *A = FD->getAttr<ReqdWorkGroupSizeAttr>()) {
623+
llvm::LLVMContext &Context = getLLVMContext();
624+
Optional<llvm::APSInt> XDimVal =
625+
A->getXDim()->getIntegerConstantExpr(FD->getASTContext());
626+
Optional<llvm::APSInt> YDimVal =
627+
A->getYDim()->getIntegerConstantExpr(FD->getASTContext());
628+
Optional<llvm::APSInt> ZDimVal =
629+
A->getZDim()->getIntegerConstantExpr(FD->getASTContext());
623630
llvm::Metadata *AttrMDArgs[] = {
624-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
625-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
626-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
627-
Fn->setMetadata("reqd_work_group_size", llvm::MDNode::get(Context, AttrMDArgs));
631+
llvm::ConstantAsMetadata::get(
632+
Builder.getInt32(XDimVal->getZExtValue())),
633+
llvm::ConstantAsMetadata::get(
634+
Builder.getInt32(YDimVal->getZExtValue())),
635+
llvm::ConstantAsMetadata::get(
636+
Builder.getInt32(ZDimVal->getZExtValue()))};
637+
Fn->setMetadata("reqd_work_group_size",
638+
llvm::MDNode::get(Context, AttrMDArgs));
628639
}
629640

630641
if (const IntelReqdSubGroupSizeAttr *A =
@@ -670,16 +681,6 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
670681
llvm::MDNode::get(Context, AttrMDArgs));
671682
}
672683

673-
if (const SYCLIntelMaxWorkGroupSizeAttr *A =
674-
FD->getAttr<SYCLIntelMaxWorkGroupSizeAttr>()) {
675-
llvm::Metadata *AttrMDArgs[] = {
676-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getXDim())),
677-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getYDim())),
678-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getZDim()))};
679-
Fn->setMetadata("max_work_group_size",
680-
llvm::MDNode::get(Context, AttrMDArgs));
681-
}
682-
683684
if (const SYCLIntelMaxGlobalWorkDimAttr *A =
684685
FD->getAttr<SYCLIntelMaxGlobalWorkDimAttr>()) {
685686
llvm::LLVMContext &Context = getLLVMContext();
@@ -692,6 +693,26 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
692693
llvm::MDNode::get(Context, AttrMDArgs));
693694
}
694695

696+
if (const SYCLIntelMaxWorkGroupSizeAttr *A =
697+
FD->getAttr<SYCLIntelMaxWorkGroupSizeAttr>()) {
698+
llvm::LLVMContext &Context = getLLVMContext();
699+
Optional<llvm::APSInt> XDimVal =
700+
A->getXDim()->getIntegerConstantExpr(FD->getASTContext());
701+
Optional<llvm::APSInt> YDimVal =
702+
A->getYDim()->getIntegerConstantExpr(FD->getASTContext());
703+
Optional<llvm::APSInt> ZDimVal =
704+
A->getZDim()->getIntegerConstantExpr(FD->getASTContext());
705+
llvm::Metadata *AttrMDArgs[] = {
706+
llvm::ConstantAsMetadata::get(
707+
Builder.getInt32(XDimVal->getZExtValue())),
708+
llvm::ConstantAsMetadata::get(
709+
Builder.getInt32(YDimVal->getZExtValue())),
710+
llvm::ConstantAsMetadata::get(
711+
Builder.getInt32(ZDimVal->getZExtValue()))};
712+
Fn->setMetadata("max_work_group_size",
713+
llvm::MDNode::get(Context, AttrMDArgs));
714+
}
715+
695716
if (const SYCLIntelNoGlobalWorkOffsetAttr *A =
696717
FD->getAttr<SYCLIntelNoGlobalWorkOffsetAttr>()) {
697718
const Expr *Arg = A->getValue();

clang/lib/CodeGen/TargetInfo.cpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8097,16 +8097,22 @@ void TCETargetCodeGenInfo::setTargetAttributes(
80978097

80988098
SmallVector<llvm::Metadata *, 5> Operands;
80998099
Operands.push_back(llvm::ConstantAsMetadata::get(F));
8100-
8101-
Operands.push_back(
8102-
llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8103-
M.Int32Ty, llvm::APInt(32, Attr->getXDim()))));
8104-
Operands.push_back(
8105-
llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8106-
M.Int32Ty, llvm::APInt(32, Attr->getYDim()))));
8107-
Operands.push_back(
8108-
llvm::ConstantAsMetadata::get(llvm::Constant::getIntegerValue(
8109-
M.Int32Ty, llvm::APInt(32, Attr->getZDim()))));
8100+
unsigned XDim = Attr->getXDim()
8101+
->EvaluateKnownConstInt(M.getContext())
8102+
.getZExtValue();
8103+
unsigned YDim = Attr->getYDim()
8104+
->EvaluateKnownConstInt(M.getContext())
8105+
.getZExtValue();
8106+
unsigned ZDim = Attr->getZDim()
8107+
->EvaluateKnownConstInt(M.getContext())
8108+
.getZExtValue();
8109+
8110+
Operands.push_back(llvm::ConstantAsMetadata::get(
8111+
llvm::Constant::getIntegerValue(M.Int32Ty, llvm::APInt(32, XDim))));
8112+
Operands.push_back(llvm::ConstantAsMetadata::get(
8113+
llvm::Constant::getIntegerValue(M.Int32Ty, llvm::APInt(32, YDim))));
8114+
Operands.push_back(llvm::ConstantAsMetadata::get(
8115+
llvm::Constant::getIntegerValue(M.Int32Ty, llvm::APInt(32, ZDim))));
81108116

81118117
// Add a boolean constant operand for "required" (true) or "hint"
81128118
// (false) for implementing the work_group_size_hint attr later.
@@ -8986,6 +8992,9 @@ void AMDGPUTargetCodeGenInfo::setTargetAttributes(
89868992
if (ReqdWGS || FlatWGS) {
89878993
unsigned Min = 0;
89888994
unsigned Max = 0;
8995+
unsigned XDim = 0;
8996+
unsigned YDim = 0;
8997+
unsigned ZDim = 0;
89898998
if (FlatWGS) {
89908999
Min = FlatWGS->getMin()
89919000
->EvaluateKnownConstInt(M.getContext())
@@ -8994,8 +9003,19 @@ void AMDGPUTargetCodeGenInfo::setTargetAttributes(
89949003
->EvaluateKnownConstInt(M.getContext())
89959004
.getExtValue();
89969005
}
9006+
if (ReqdWGS) {
9007+
XDim = ReqdWGS->getXDim()
9008+
->EvaluateKnownConstInt(M.getContext())
9009+
.getZExtValue();
9010+
YDim = ReqdWGS->getYDim()
9011+
->EvaluateKnownConstInt(M.getContext())
9012+
.getZExtValue();
9013+
ZDim = ReqdWGS->getZDim()
9014+
->EvaluateKnownConstInt(M.getContext())
9015+
.getZExtValue();
9016+
}
89979017
if (ReqdWGS && Min == 0 && Max == 0)
8998-
Min = Max = ReqdWGS->getXDim() * ReqdWGS->getYDim() * ReqdWGS->getZDim();
9018+
Min = Max = XDim * YDim * ZDim;
89999019

90009020
if (Min != 0) {
90019021
assert(Min <= Max && "Min must be less than or equal Max");

clang/lib/Sema/SemaDecl.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3209,9 +3209,18 @@ static void checkDimensionsAndSetDiagnostics(Sema &S, FunctionDecl *New,
32093209
if (!NewDeclAttr || !OldDeclAttr)
32103210
return;
32113211

3212-
if ((NewDeclAttr->getXDim() != OldDeclAttr->getXDim()) ||
3213-
(NewDeclAttr->getYDim() != OldDeclAttr->getYDim()) ||
3214-
(NewDeclAttr->getZDim() != OldDeclAttr->getZDim())) {
3212+
/// Returns the usigned constant integer value represented by
3213+
/// given expression.
3214+
auto getExprValue = [](const Expr *E, ASTContext &Ctx) {
3215+
return E->getIntegerConstantExpr(Ctx)->getZExtValue();
3216+
};
3217+
3218+
if ((getExprValue(NewDeclAttr->getXDim(), S.getASTContext()) !=
3219+
getExprValue(OldDeclAttr->getXDim(), S.getASTContext())) ||
3220+
(getExprValue(NewDeclAttr->getYDim(), S.getASTContext()) !=
3221+
getExprValue(OldDeclAttr->getYDim(), S.getASTContext())) ||
3222+
(getExprValue(NewDeclAttr->getZDim(), S.getASTContext()) !=
3223+
getExprValue(OldDeclAttr->getZDim(), S.getASTContext()))) {
32153224
S.Diag(New->getLocation(), diag::err_conflicting_sycl_function_attributes)
32163225
<< OldDeclAttr << NewDeclAttr;
32173226
S.Diag(New->getLocation(), diag::warn_duplicate_attribute) << OldDeclAttr;

0 commit comments

Comments
 (0)