Skip to content

Commit d5481a6

Browse files
committed
[SYCL] Add template parameter support for intel_reqd_sub_group_size attribute
Signed-off-by: Soumi Manna <[email protected]>
1 parent 0473316 commit d5481a6

File tree

7 files changed

+93
-20
lines changed

7 files changed

+93
-20
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1262,7 +1262,7 @@ def LoopUnrollHint : InheritableAttr {
12621262

12631263
def IntelReqdSubGroupSize: InheritableAttr {
12641264
let Spellings = [GNU<"intel_reqd_sub_group_size">, CXX11<"cl", "intel_reqd_sub_group_size">];
1265-
let Args = [UnsignedArgument<"SubGroupSize">];
1265+
let Args = [ExprArgument<"SubGroupSize">];
12661266
let Subjects = SubjectList<[Function, CXXMethod], ErrorDiag>;
12671267
let Documentation = [IntelReqdSubGroupSizeDocs];
12681268
let LangOpts = [OpenCL, SYCLIsDevice, SYCLIsHost];

clang/include/clang/Sema/Sema.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9917,6 +9917,11 @@ class Sema final {
99179917

99189918
bool checkNSReturnsRetainedReturnType(SourceLocation loc, QualType type);
99199919

9920+
// addIntelReqdSubGroupSizeAttr - Adds an intel_reqd_sub_group_size attribute
9921+
// to a particular declaration.
9922+
void addIntelReqdSubGroupSizeAttr(Decl *D, const AttributeCommonInfo &CI,
9923+
Expr *E);
9924+
99209925
//===--------------------------------------------------------------------===//
99219926
// C++ Coroutines TS
99229927
//

clang/lib/CodeGen/CodeGenFunction.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,14 @@ void CodeGenFunction::EmitOpenCLKernelMetadata(const FunctionDecl *FD,
574574

575575
if (const IntelReqdSubGroupSizeAttr *A =
576576
FD->getAttr<IntelReqdSubGroupSizeAttr>()) {
577+
llvm::APSInt ArgVal(32);
578+
llvm::LLVMContext &Context = getLLVMContext();
579+
bool IsValid = A->getSubGroupSize()->isIntegerConstantExpr(
580+
ArgVal, FD->getASTContext());
581+
assert(IsValid && "Not an integer constant expression");
582+
(void)IsValid;
577583
llvm::Metadata *AttrMDArgs[] = {
578-
llvm::ConstantAsMetadata::get(Builder.getInt32(A->getSubGroupSize()))};
584+
llvm::ConstantAsMetadata::get(Builder.getInt32(ArgVal.getSExtValue()))};
579585
Fn->setMetadata("intel_reqd_sub_group_size",
580586
llvm::MDNode::get(Context, AttrMDArgs));
581587
}

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,27 +2980,46 @@ static void handleWorkGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
29802980
}
29812981

29822982
// Handles intel_reqd_sub_group_size.
2983+
void Sema::addIntelReqdSubGroupSizeAttr(Decl *D,
2984+
const AttributeCommonInfo &Attr,
2985+
Expr *E) {
2986+
if (!E)
2987+
return;
2988+
2989+
if (!E->isInstantiationDependent()) {
2990+
llvm::APSInt ArgVal(32);
2991+
if (!E->isIntegerConstantExpr(ArgVal, getASTContext())) {
2992+
Diag(E->getExprLoc(), diag::err_attribute_argument_type)
2993+
<< Attr.getAttrName() << AANT_ArgumentIntegerConstant
2994+
<< E->getSourceRange();
2995+
return;
2996+
}
2997+
int32_t ArgInt = ArgVal.getSExtValue();
2998+
if (ArgInt < 0) {
2999+
Diag(E->getExprLoc(), diag::err_attribute_requires_positive_integer)
3000+
<< Attr.getAttrName() << /*non-negative*/ 1;
3001+
return;
3002+
}
3003+
if (ArgInt == 0) {
3004+
Diag(E->getExprLoc(), diag::err_attribute_argument_is_zero)
3005+
<< Attr.getAttrName() << 0;
3006+
return;
3007+
}
3008+
}
3009+
3010+
D->addAttr(::new (Context) IntelReqdSubGroupSizeAttr(Context, Attr, E));
3011+
}
3012+
29833013
static void handleSubGroupSize(Sema &S, Decl *D, const ParsedAttr &AL) {
29843014
if (S.LangOpts.SYCLIsHost)
29853015
return;
29863016

2987-
uint32_t SGSize;
2988-
const Expr *E = AL.getArgAsExpr(0);
2989-
if (!checkUInt32Argument(S, AL, E, SGSize))
2990-
return;
2991-
if (SGSize == 0) {
2992-
S.Diag(AL.getLoc(), diag::err_attribute_argument_is_zero)
2993-
<< AL << E->getSourceRange();
2994-
return;
2995-
}
3017+
Expr *E = AL.getArgAsExpr(0);
29963018

2997-
IntelReqdSubGroupSizeAttr *Existing =
2998-
D->getAttr<IntelReqdSubGroupSizeAttr>();
2999-
if (Existing && Existing->getSubGroupSize() != SGSize)
3019+
if (D->getAttr<IntelReqdSubGroupSizeAttr>())
30003020
S.Diag(AL.getLoc(), diag::warn_duplicate_attribute) << AL;
30013021

3002-
D->addAttr(::new (S.Context)
3003-
IntelReqdSubGroupSizeAttr(S.Context, AL, SGSize));
3022+
S.addIntelReqdSubGroupSizeAttr(D, AL, E);
30043023
}
30053024

30063025
// Handles num_simd_work_items.

clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,17 @@ static void instantiateSYCLIntelPipeIOAttr(
579579
S.addSYCLIntelPipeIOAttr(New, *Attr, Result.getAs<Expr>());
580580
}
581581

582+
static void instantiateIntelReqdSubGroupSizeAttr(
583+
Sema &S, const MultiLevelTemplateArgumentList &TemplateArgs,
584+
const IntelReqdSubGroupSizeAttr *Attr, Decl *New) {
585+
// The SubGroupSize expression is a constant expressions.
586+
EnterExpressionEvaluationContext Unevaluated(
587+
S, Sema::ExpressionEvaluationContext::ConstantEvaluated);
588+
ExprResult Result = S.SubstExpr(Attr->getSubGroupSize(), TemplateArgs);
589+
if (!Result.isInvalid())
590+
S.addIntelReqdSubGroupSizeAttr(New, *Attr, Result.getAs<Expr>());
591+
}
592+
582593
void Sema::InstantiateAttrsForDecl(
583594
const MultiLevelTemplateArgumentList &TemplateArgs, const Decl *Tmpl,
584595
Decl *New, LateInstantiatedAttrVec *LateAttrs,
@@ -721,7 +732,12 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
721732
instantiateSYCLIntelPipeIOAttr(*this, TemplateArgs, SYCLIntelPipeIO, New);
722733
continue;
723734
}
724-
735+
if (const auto *IntelReqdSubGroupSize =
736+
dyn_cast<IntelReqdSubGroupSizeAttr>(TmplAttr)) {
737+
instantiateIntelReqdSubGroupSizeAttr(*this, TemplateArgs,
738+
IntelReqdSubGroupSize, New);
739+
continue;
740+
}
725741
// Existing DLL attribute on the instantiation takes precedence.
726742
if (TmplAttr->getKind() == attr::DLLExport ||
727743
TmplAttr->getKind() == attr::DLLImport) {

clang/test/SemaSYCL/reqd-sub-group-size-device.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ void bar() {
4949
}
5050

5151
// CHECK: FunctionDecl {{.*}} {{.*}}kernel_name1
52-
// CHECK: IntelReqdSubGroupSizeAttr {{.*}} 16
52+
// CHECK: IntelReqdSubGroupSizeAttr {{.*}}
53+
// CHECK-NEXT: IntegerLiteral{{.*}}16{{$}}
5354
// CHECK: FunctionDecl {{.*}} {{.*}}kernel_name2
54-
// CHECK: IntelReqdSubGroupSizeAttr {{.*}} 4
55+
// CHECK: IntelReqdSubGroupSizeAttr {{.*}}
56+
// CHECK-NEXT: IntegerLiteral{{.*}}4{{$}}
5557
// CHECK: FunctionDecl {{.*}} {{.*}}kernel_name5
56-
// CHECK: IntelReqdSubGroupSizeAttr {{.*}} 2
58+
// CHECK: IntelReqdSubGroupSizeAttr {{.*}}
59+
// CHECK-NEXT: IntegerLiteral{{.*}}2{{$}}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsyntax-only -verify -pedantic %s
2+
// RUN: %clang_cc1 -fsycl -fsycl-is-device -fsyntax-only -ast-dump -verify -pedantic %s | FileCheck %s
3+
4+
// Test that checkes template parameter support for 'intel_reqd_sub_group_size' attribute on sycl device.
5+
6+
template <int SIZE>
7+
class KernelFunctor {
8+
public:
9+
//expected-error@+1{{'intel_reqd_sub_group_size' attribute requires a non-negative integral compile time constant expression}}
10+
[[cl::intel_reqd_sub_group_size(SIZE)]] void operator()(){};
11+
12+
int main() {
13+
//expected-note@+1{{in instantiation of template class 'KernelFunctor<-1>' requested here}}
14+
KernelFunctor<-1>();
15+
// no error expected
16+
KernelFunctor<10>();
17+
}
18+
19+
// CHECK: ClassTemplateDecl {{.*}} {{.*}} KernelFunctor
20+
// CHECK: ClassTemplateSpecializationDecl {{.*}} {{.*}} class KernelFunctor definition
21+
// CHECK: CXXRecordDecl {{.*}} {{.*}} implicit class KernelFunctor
22+
// CHECK: IntelReqdSubGroupSizeAttr {{.*}}
23+
// CHECK: SubstNonTypeTemplateParmExpr {{.*}}
24+
// CHECK-NEXT: IntegerLiteral{{.*}}10{{$}}

0 commit comments

Comments
 (0)