Skip to content

Commit bef0981

Browse files
elizabethandrewsFznamznon
authored andcommitted
[SYCL] Apply review comments - Builtins
1 parent eeea84b commit bef0981

File tree

6 files changed

+76
-10
lines changed

6 files changed

+76
-10
lines changed

clang/include/clang/Basic/Builtins.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4797,7 +4797,7 @@ def GetDeviceSideMangledName : LangBuiltin<"CUDA_LANG"> {
47974797
// SYCL
47984798
def SYCLKernelName : LangBuiltin<"SYCL_LANG"> {
47994799
let Spellings = ["__builtin_sycl_kernel_name"];
4800-
let Attributes = [NoThrow, CustomTypeChecking];
4800+
let Attributes = [NoThrow, Const, Constexpr, CustomTypeChecking];
48014801
let Prototype = "char const*(...)";
48024802
}
48034803

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ def warn_unreachable_association : Warning<
815815
InGroup<UnreachableCodeGenericAssoc>;
816816

817817
/// Built-in functions.
818+
def err_builtin_invalid_argument_count : Error<
819+
"builtin %plural{0:takes no arguments|1:takes one argument|"
820+
":requires exactly %0 arguments}0">;
818821
def ext_implicit_lib_function_decl : ExtWarn<
819822
"implicitly declaring library function '%0' with type %1">,
820823
InGroup<ImplicitFunctionDeclare>;
@@ -12789,9 +12792,9 @@ def err_sycl_entry_point_deduced_return_type : Error<
1278912792
def warn_cuda_maxclusterrank_sm_90 : Warning<
1279012793
"maxclusterrank requires sm_90 or higher, CUDA arch provided: %0, ignoring "
1279112794
"%1 attribute">, InGroup<IgnoredAttributes>;
12792-
def err_builtin_invalid_argument_count : Error<"builtin requires exactly 1 argument">;
12793-
def err_builtin_invalid_argument : Error<"invalid argument; argument must be a class or struct type"
12794-
" with a member type alias named 'type'">;
12795+
def err_sycl_kernel_name_invalid_arg : Error<"invalid argument; expected a class "
12796+
"or structure with a member typedef "
12797+
"or type alias alias named 'type'">;
1279512798

1279612799
// VTable pointer authentication errors
1279712800
def err_non_polymorphic_vtable_pointer_auth : Error<

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,6 +2527,22 @@ static RValue EmitHipStdParUnsupportedBuiltin(CodeGenFunction *CGF,
25272527
return RValue::get(CGF->Builder.CreateCall(UBF, Args));
25282528
}
25292529

2530+
static const SYCLKernelInfo *GetSYCLKernelInfo(ASTContext &Ctx,
2531+
const CallExpr *E) {
2532+
// Argument to the builtin is a type trait which is used to retrieve the
2533+
// kernel name type.
2534+
// FIXME: Improve the comment.
2535+
RecordDecl *RD = E->getArg(0)->getType()->castAs<RecordType>()->getDecl();
2536+
IdentifierTable &IdentTable = Ctx.Idents;
2537+
auto Name = DeclarationName(&(IdentTable.get("type")));
2538+
NamedDecl *ND = (RD->lookup(Name)).front();
2539+
TypedefNameDecl *TD = cast<TypedefNameDecl>(ND);
2540+
CanQualType KernelNameType = Ctx.getCanonicalType(TD->getUnderlyingType());
2541+
2542+
// Retrieve KernelInfo using the kernel name.
2543+
return Ctx.findSYCLKernelInfo(KernelNameType);
2544+
}
2545+
25302546
RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
25312547
const CallExpr *E,
25322548
ReturnValueSlot ReturnValue) {

clang/lib/Sema/SemaChecking.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,7 +2232,8 @@ static bool CheckBuiltinSyclKernelName(Sema &S, CallExpr *TheCall) {
22322232
IdentifierTable &IdentTable = S.Context.Idents;
22332233
auto Name = DeclarationName(&(IdentTable.get("type")));
22342234
DeclContext::lookup_result Lookup = RD->lookup(Name);
2235-
if (Lookup.empty() || !Lookup.isSingleResult() || !isa<TypeAliasDecl>(Lookup.front()))
2235+
if (Lookup.empty() || !Lookup.isSingleResult() ||
2236+
!isa<TypedefNameDecl>(Lookup.front()))
22362237
return true;
22372238

22382239
return false;
@@ -3052,12 +3053,14 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
30523053
case Builtin::BI__builtin_sycl_kernel_name: {
30533054
// Builtin takes 1 argument
30543055
if (TheCall->getNumArgs() != 1) {
3055-
Diag(TheCall->getBeginLoc(), diag::err_builtin_invalid_argument_count);
3056+
Diag(TheCall->getBeginLoc(), diag::err_builtin_invalid_argument_count)
3057+
<< 1;
30563058
return ExprError();
30573059
}
30583060

30593061
if (CheckBuiltinSyclKernelName(*this, TheCall)) {
3060-
Diag(TheCall->getArg(0)->getBeginLoc(), diag::err_builtin_invalid_argument);
3062+
Diag(TheCall->getArg(0)->getBeginLoc(),
3063+
diag::err_sycl_kernel_name_invalid_arg);
30613064
return ExprError();
30623065
}
30633066

clang/test/SemaSYCL/builtin-sycl-kernel-name.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ void test() {
3737
const char* test1 = __builtin_sycl_kernel_name(kernel_id_1<kernel_name_1>()); // Valid
3838
const char* test2 = __builtin_sycl_kernel_name(kernel_id_1<kernel_name_TD>()); // Valid
3939
const char* test3 = __builtin_sycl_kernel_name(kernel_id_2()); // Valid
40-
const char* test4 = __builtin_sycl_kernel_name(kernel_id_3()); // expected-error {{invalid argument; argument must be a class or struct type with a member type alias named 'type'}}
41-
const char* test5 = __builtin_sycl_kernel_name("str"); // expected-error {{invalid argument; argument must be a class or struct type with a member type alias named 'type'}}
42-
const char* test6 = __builtin_sycl_kernel_name(kernel_id_2(), kernel_id_2()); // expected-error {{builtin requires exactly 1 argument}}
40+
const char* test4 = __builtin_sycl_kernel_name(kernel_id_3()); // expected-error {{invalid argument; expected a class or structure with a member typedef or type alias alias named 'type'}}
41+
const char* test5 = __builtin_sycl_kernel_name("str"); // expected-error {{invalid argument; expected a class or structure with a member typedef or type alias alias named 'type'}}
42+
const char* test6 = __builtin_sycl_kernel_name(kernel_id_2(), kernel_id_2()); // expected-error {{builtin takes one argument}}
4343
}
4444

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// RUN: %clang_cc1 -fsycl-is-host -fsyntax-only -verify %s
2+
3+
class kernel_name_1;
4+
class kernel_name_2;
5+
class kernel_name_3;
6+
typedef kernel_name_3 kernel_name_TD;
7+
8+
template<typename KN>
9+
struct kernel_id_1 {
10+
using type = KN;
11+
};
12+
13+
struct kernel_id_2 {
14+
using type = kernel_name_2;
15+
};
16+
17+
struct kernel_id_3 {
18+
using invalid_name = kernel_name_2;
19+
};
20+
21+
template <typename name, typename Func>
22+
__attribute__((sycl_kernel_entry_point(name))) void kernel_single_task(const Func kernelFunc) {
23+
kernelFunc();
24+
}
25+
26+
struct SYCLKernel {
27+
int m;
28+
public:
29+
void operator()() const {}
30+
};
31+
32+
void test() {
33+
SYCLKernel Obj;
34+
kernel_single_task<kernel_name_1>(Obj);
35+
kernel_single_task<kernel_name_2>(Obj);
36+
kernel_single_task<kernel_name_TD>(Obj);
37+
int test1 = __builtin_sycl_kernel_param_count(kernel_id_1<kernel_name_1>()); // Valid
38+
int test2 = __builtin_sycl_kernel_param_count(kernel_id_1<kernel_name_TD>()); // Valid
39+
int test3 = __builtin_sycl_kernel_param_count(kernel_id_2()); // Valid
40+
int test4 = __builtin_sycl_kernel_param_count(kernel_id_3()); // expected-error {{invalid argument; expected a class or structure with a member typedef or type alias alias named 'type'}}
41+
int test5 = __builtin_sycl_kernel_param_count("str"); // expected-error {{invalid argument; expected a class or structure with a member typedef or type alias alias named 'type'}}
42+
int test6 = __builtin_sycl_kernel_param_count(kernel_id_2(), kernel_id_2()); // expected-error {{builtin takes one argument}}
43+
}
44+

0 commit comments

Comments
 (0)