Skip to content

Commit 2cd9db1

Browse files
committed
[OpenCL] Produce an error when the work group and vec type hint attributes
are used on non-kernel functions. Reviewed by Aaron over IRC! llvm-svn: 197243
1 parent 984c9d8 commit 2cd9db1

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

clang/include/clang/Basic/Attr.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -772,13 +772,15 @@ def ReqdWorkGroupSize : InheritableAttr {
772772
let Spellings = [GNU<"reqd_work_group_size">];
773773
let Args = [UnsignedArgument<"XDim">, UnsignedArgument<"YDim">,
774774
UnsignedArgument<"ZDim">];
775+
let Subjects = SubjectList<[Function], ErrorDiag>;
775776
}
776777

777778
def WorkGroupSizeHint : InheritableAttr {
778779
let Spellings = [GNU<"work_group_size_hint">];
779780
let Args = [UnsignedArgument<"XDim">,
780781
UnsignedArgument<"YDim">,
781782
UnsignedArgument<"ZDim">];
783+
let Subjects = SubjectList<[Function], ErrorDiag>;
782784
}
783785

784786
def InitPriority : InheritableAttr {
@@ -877,6 +879,7 @@ def VectorSize : TypeAttr {
877879
def VecTypeHint : InheritableAttr {
878880
let Spellings = [GNU<"vec_type_hint">];
879881
let Args = [TypeArgument<"TypeHint">];
882+
let Subjects = SubjectList<[Function], ErrorDiag>;
880883
}
881884

882885
def Visibility : InheritableAttr {

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6728,6 +6728,8 @@ def err_wrong_sampler_addressspace: Error<
67286728
def err_opencl_global_invalid_addr_space : Error<
67296729
"global variables must have a constant address space qualifier">;
67306730
def err_opencl_no_main : Error<"%select{function|kernel}0 cannot be called 'main'">;
6731+
def err_opencl_kernel_attr :
6732+
Error<"attribute '%0' can only be applied to a kernel function">;
67316733
} // end of sema category
67326734

67336735
let CategoryName = "OpenMP Issue" in {

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4217,6 +4217,7 @@ void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
42174217
for (const AttributeList* l = AttrList; l; l = l->getNext())
42184218
ProcessDeclAttribute(*this, S, D, *l, IncludeCXX11Attributes);
42194219

4220+
// FIXME: We should be able to handle these cases in TableGen.
42204221
// GCC accepts
42214222
// static int a9 __attribute__((weakref));
42224223
// but that looks really pointless. We reject it.
@@ -4226,6 +4227,24 @@ void Sema::ProcessDeclAttributeList(Scope *S, Decl *D,
42264227
D->dropAttr<WeakRefAttr>();
42274228
return;
42284229
}
4230+
4231+
if (!D->hasAttr<OpenCLKernelAttr>()) {
4232+
// These attributes cannot be applied to a non-kernel function.
4233+
if (D->hasAttr<ReqdWorkGroupSizeAttr>()) {
4234+
Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4235+
<< "reqd_work_group_size";
4236+
D->setInvalidDecl();
4237+
}
4238+
if (D->hasAttr<WorkGroupSizeHintAttr>()) {
4239+
Diag(D->getLocation(), diag::err_opencl_kernel_attr)
4240+
<< "work_group_size_hint";
4241+
D->setInvalidDecl();
4242+
}
4243+
if (D->hasAttr<VecTypeHintAttr>()) {
4244+
Diag(D->getLocation(), diag::err_opencl_kernel_attr) << "vec_type_hint";
4245+
D->setInvalidDecl();
4246+
}
4247+
}
42294248
}
42304249

42314250
// Annotation attributes are the only attributes allowed after an access

clang/test/SemaOpenCL/invalid-kernel-attrs.cl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ kernel __attribute__((work_group_size_hint(8,16,32,4))) void kernel6() {} //expe
1414

1515
kernel __attribute__((work_group_size_hint(1,2,3))) __attribute__((work_group_size_hint(3,2,1))) void kernel7() {} //expected-warning{{attribute 'work_group_size_hint' is already applied with different parameters}}
1616

17+
__attribute__((reqd_work_group_size(8,16,32))) void kernel8(){} // expected-error {{attribute 'reqd_work_group_size' can only be applied to a kernel}}
18+
19+
__attribute__((work_group_size_hint(8,16,32))) void kernel9(){} // expected-error {{attribute 'work_group_size_hint' can only be applied to a kernel}}
20+
21+
__attribute__((vec_type_hint(char))) void kernel10(){} // expected-error {{attribute 'vec_type_hint' can only be applied to a kernel}}
22+
23+
constant int foo1 __attribute__((reqd_work_group_size(8,16,32))); // expected-error {{'reqd_work_group_size' attribute only applies to functions}}
24+
25+
constant int foo2 __attribute__((work_group_size_hint(8,16,32))); // expected-error {{'work_group_size_hint' attribute only applies to functions}}
26+
27+
constant int foo3 __attribute__((vec_type_hint(char))); // expected-error {{'vec_type_hint' attribute only applies to functions}}

0 commit comments

Comments
 (0)