Skip to content

[clang][SYCL] Add sycl_external attribute and restrict emitting device code #140282

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

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
abdbf89
Add sycl_external attribute
schittir May 16, 2025
f631d7a
Fix test and remove space
schittir May 16, 2025
128ab1b
Address review comments #1
schittir May 23, 2025
118656c
Fix conditional and failing tests
schittir May 28, 2025
7c592a4
Fix the remaining six failing tests
schittir Jun 4, 2025
90ead01
Fix formatting
schittir Jun 4, 2025
195a3cc
Merge branch 'main' into sycl_external
schittir Jun 5, 2025
a0071d1
Remove sycl_external attribute support to variables.
schittir Jun 9, 2025
d20382c
Rename test file
schittir Jun 9, 2025
65262ba
Add tests for sycl_external attribute
schittir Jun 9, 2025
770c65e
Add code examples to sycl_external documentation
schittir Jun 10, 2025
328d242
Merge branch 'main' into sycl_external
schittir Jun 10, 2025
aab6f7d
Update clang/lib/Sema/SemaDeclAttr.cpp
schittir Jun 10, 2025
385ea37
Address review comments -2
schittir Jun 10, 2025
be80436
Address review comments -3
schittir Jun 17, 2025
060b24f
Rename test file
schittir Jun 17, 2025
625cff2
Address review comments -4
schittir Jun 17, 2025
a9fe3fb
Merge branch 'main' into sycl_external
schittir Jun 17, 2025
4eb05b8
Fix failing tests and address review comments
schittir Jun 18, 2025
ab845a2
Address review comments -3
schittir Jun 24, 2025
3ff689e
Merge branch 'main' into sycl_external
schittir Jun 24, 2025
a177b9b
Merge branch 'main' into sycl_external
schittir Jun 24, 2025
58ffb64
Merge branch 'main' into sycl_external
schittir Jul 1, 2025
7893e90
Merge branch 'main' into sycl_external
schittir Jul 1, 2025
7e76afd
Change the second RUN line to use -sycl-is-host
schittir Jul 3, 2025
e8d26a2
Switch to using sycl_external attr to pass the failing test
schittir Jul 3, 2025
82fa98a
Change diagnostic messages
schittir Jun 25, 2025
e4d15eb
Revert RUN line to -fsycl-is-device
schittir Jul 3, 2025
b38e578
Revert test change
schittir Jul 3, 2025
1d82fc1
Merge branch 'main' into sycl_external
schittir Jul 8, 2025
d751b43
Fix conflict resolution errors.
schittir Jul 8, 2025
2b22ed2
Remove changes introduced from downstream.
schittir Jul 8, 2025
1b3a198
Update diagnostic messages in tests
schittir Jul 8, 2025
568b569
Undo more downstream changes
schittir Jul 8, 2025
0ab9ac5
Ungroup diagnostics and add test cases
schittir Jul 9, 2025
19d1660
Merge branch 'main' into sycl_external
schittir Jul 10, 2025
a70e2df
Fix newly failing tests by adding sycl_external attribute
schittir Jul 10, 2025
45f7b09
Add constexpr and consteval test cases
schittir Jul 10, 2025
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
7 changes: 7 additions & 0 deletions clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,13 @@ def DeviceKernel : DeclOrTypeAttr {
}];
}

def SYCLExternal : InheritableAttr {
let Spellings = [Clang<"sycl_external">];
let Subjects = SubjectList<[Function], ErrorDiag>;
let LangOpts = [SYCLDevice];
let Documentation = [SYCLExternalDocs];
}

def SYCLKernelEntryPoint : InheritableAttr {
let Spellings = [Clang<"sycl_kernel_entry_point">];
let Args = [
Expand Down
42 changes: 42 additions & 0 deletions clang/include/clang/Basic/AttrDocs.td
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,48 @@ The SYCL kernel in the previous code sample meets these expectations.
}];
}

def SYCLExternalDocs : Documentation {
let Category = DocCatFunction;
let Heading = "sycl_external";
let Content = [{
The ``sycl_external`` attribute indicates that a function defined in another
translation unit may be called by a device function defined in the current
translation unit or, if defined in the current translation unit, the function
may be called by device functions defined in other translation units.
The attribute is intended for use in the implementation of the ``SYCL_EXTERNAL``
macro as specified in section 5.10.1, "SYCL functions and member functions
linkage", of the SYCL 2020 specification.

The attribute only appertains to functions and only those that meet the
following requirements.

* Has external linkage.
* Is not explicitly defined as deleted (the function may be an explicitly
defaulted function that is defined as deleted).

The attribute shall be present on the first declaration of a function and
may optionally be present on subsequent declarations.

When compiling for a SYCL device target that does not support the generic
address space, the function shall not specify a raw pointer or reference type
as the return type or as a parameter type.
See section 5.9, "Address-space deduction", of the SYCL 2020 specification.

The following examples demonstrate the use of this attribute:

.. code-block:: c++

[[clang::sycl_external]] void Foo(); // Ok.

[[clang::sycl_external]] void Bar() { /* ... */ } // Ok.

[[clang::sycl_external]] extern void Baz(); // Ok.

[[clang::sycl_external]] static void Quux() { /* ... */ } // error: Quux() has internal linkage.

}];
}

def SYCLKernelEntryPointDocs : Documentation {
let Category = DocCatFunction;
let Content = [{
Expand Down
8 changes: 8 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -12856,6 +12856,14 @@ def err_sycl_special_type_num_init_method : Error<
"types with 'sycl_special_class' attribute must have one and only one '__init' "
"method defined">;

// SYCL external attribute diagnostics
def err_sycl_attribute_invalid_linkage : Error<
"'sycl_external' can only be applied to functions with external linkage">;
def err_sycl_attribute_avoid_main : Error<
"'sycl_external' cannot be applied to main function">;
def err_sycl_attribute_avoid_deleted_function : Error<
"'sycl_external' cannot be applied to explicitly deleted functions">;

// SYCL kernel entry point diagnostics
def err_sycl_entry_point_invalid : Error<
"'sycl_kernel_entry_point' attribute cannot be applied to a"
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Sema/SemaSYCL.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SemaSYCL : public SemaBase {
ParsedType ParsedTy);

void handleKernelAttr(Decl *D, const ParsedAttr &AL);
void handleExternalAttr(Decl *D, const ParsedAttr &AL);
void handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL);

void CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD);
Expand Down
11 changes: 9 additions & 2 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13037,6 +13037,10 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
if (D->hasAttr<WeakRefAttr>())
return false;

if (LangOpts.SYCLIsDevice && !D->hasAttr<SYCLKernelEntryPointAttr>() &&
!D->hasAttr<SYCLExternalAttr>())
return false;

// Aliases and used decls are required.
if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
return true;
Expand All @@ -13052,8 +13056,11 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLKernelEntryPointAttr>())
return true;

// FIXME: Functions declared with SYCL_EXTERNAL are required during
// device compilation.
// Function definitions with the sycl_external attribute are required
// during device compilation regardless of whether they are reachable from
// a SYCL kernel.
if (LangOpts.SYCLIsDevice && FD->hasAttr<SYCLExternalAttr>())
return true;

// Constructors and destructors are required.
if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Sema/SemaDeclAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7206,6 +7206,9 @@ ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D, const ParsedAttr &AL,
case ParsedAttr::AT_EnumExtensibility:
handleEnumExtensibilityAttr(S, D, AL);
break;
case ParsedAttr::AT_SYCLExternal:
S.SYCL().handleExternalAttr(D, AL);
break;
case ParsedAttr::AT_SYCLKernelEntryPoint:
S.SYCL().handleKernelEntryPointAttr(D, AL);
break;
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,25 @@ void SemaSYCL::handleKernelAttr(Decl *D, const ParsedAttr &AL) {
handleSimpleAttribute<DeviceKernelAttr>(*this, D, AL);
}

void SemaSYCL::handleExternalAttr(Decl *D, const ParsedAttr &AL) {
auto *FD = cast<FunctionDecl>(D);
if (!FD->isExternallyVisible()) {
Diag(AL.getLoc(), diag::err_sycl_attribute_invalid_linkage);
return;
}
std::string FunctionName = StringRef(FD->getNameInfo().getAsString()).lower();
if (FunctionName.find("main") != std::string::npos) {
Diag(AL.getLoc(), diag::err_sycl_attribute_avoid_main);
return;
}
if (FD->isDeleted()) {
Diag(AL.getLoc(), diag::err_sycl_attribute_avoid_deleted_function);
return;
}

handleSimpleAttribute<SYCLExternalAttr>(*this, D, AL);
}

void SemaSYCL::handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL) {
ParsedType PT = AL.getTypeArg();
TypeSourceInfo *TSI = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// CHECK-NEXT: [[SPV_CAST:%.*]] = tail call noundef ptr @llvm.spv.generic.cast.to.ptr.explicit.p0(ptr addrspace(4) %p)
// CHECK-NEXT: ret ptr [[SPV_CAST]]
//
__attribute__((opencl_private)) int* test_cast_to_private(int* p) {
[[clang::sycl_external]] __attribute__((opencl_private)) int* test_cast_to_private(int* p) {
return __builtin_spirv_generic_cast_to_ptr_explicit(p, 7);
}

Expand All @@ -18,7 +18,7 @@ __attribute__((opencl_private)) int* test_cast_to_private(int* p) {
// CHECK-NEXT: [[SPV_CAST:%.*]] = tail call noundef ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit.p1(ptr addrspace(4) %p)
// CHECK-NEXT: ret ptr addrspace(1) [[SPV_CAST]]
//
__attribute__((opencl_global)) int* test_cast_to_global(int* p) {
[[clang::sycl_external]] __attribute__((opencl_global)) int* test_cast_to_global(int* p) {
return __builtin_spirv_generic_cast_to_ptr_explicit(p, 5);
}

Expand All @@ -28,6 +28,6 @@ __attribute__((opencl_global)) int* test_cast_to_global(int* p) {
// CHECK-NEXT: [[SPV_CAST:%.*]] = tail call noundef ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit.p3(ptr addrspace(4) %p)
// CHECK-NEXT: ret ptr addrspace(3) [[SPV_CAST]]
//
__attribute__((opencl_local)) int* test_cast_to_local(int* p) {
[[clang::sycl_external]] __attribute__((opencl_local)) int* test_cast_to_local(int* p) {
return __builtin_spirv_generic_cast_to_ptr_explicit(p, 4);
}
Loading
Loading