From 7938a50cb26b4465b7d03ca5754b1dbdc8a3d973 Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Thu, 18 Sep 2025 11:54:09 +0200 Subject: [PATCH 1/2] [SYCL] Fix error with type aliases used as free function kernel args This PR fixes type name that is being printed as free function kernel argument type in its forward-declaration in the integration header. Before the change, we used the original argument type name, which could be an alias - this patch makes use of the canonical type's name to make sure that all type aliases are "unwrapped". --- clang/lib/Sema/SemaSYCL.cpp | 8 +-- .../free-function-kernel-type-alias-arg.cpp | 53 +++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp diff --git a/clang/lib/Sema/SemaSYCL.cpp b/clang/lib/Sema/SemaSYCL.cpp index c86e49a1be33b..b9a5d202af373 100644 --- a/clang/lib/Sema/SemaSYCL.cpp +++ b/clang/lib/Sema/SemaSYCL.cpp @@ -6822,13 +6822,13 @@ class FreeFunctionPrinter { continue; } - TemplateName TN = TST->getTemplateName(); + TemplateName CTN = CTST->getTemplateName(); + CTN.getAsTemplateDecl()->printQualifiedName(ParmListOstream); + ParmListOstream << "<"; + auto SpecArgs = TST->template_arguments(); auto DeclArgs = CTST->template_arguments(); - TN.getAsTemplateDecl()->printQualifiedName(ParmListOstream); - ParmListOstream << "<"; - for (size_t I = 0, E = std::max(DeclArgs.size(), SpecArgs.size()), SE = SpecArgs.size(); I < E; ++I) { diff --git a/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp b/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp new file mode 100644 index 0000000000000..00dc0abbe0db0 --- /dev/null +++ b/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp @@ -0,0 +1,53 @@ +// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64-unknown-unknown -sycl-std=2020 -fsycl-int-header=%t.h %s +// RUN: FileCheck -input-file=%t.h %s +// +// The purpose of this test is to ensure that forward declarations of free +// function kernels are emitted properly. +// However, this test checks a specific scenario: +// - free function arguments are type aliases (through using or typedef) + +namespace ns { + +using IntUsing = int; +typedef int IntTypedef; + +template +struct Foo {}; + +using FooIntUsing = Foo; +typedef Foo FooIntTypedef; + +template +struct Bar {}; + +template +using BarUsing = Bar; + +} // namespace ns + +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void int_using(ns::IntUsing Arg) {} + +// CHECK: void int_using(int Arg); + +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void int_typedef(ns::IntTypedef Arg) {} + +// CHECK: void int_typedef(int Arg); + +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void foo_using(ns::FooIntUsing Arg) {} + +// CHECK: void foo_using(ns::Foo Arg); + +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void foo_typedef(ns::FooIntTypedef Arg) {} + +// CHECK: void foo_typedef(ns::Foo Arg); + +template +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void bar_using(ns::BarUsing Arg) {} +template void bar_using(ns::BarUsing); + +// CHECK: template void bar_using(ns::Bar); From e32713371f2b925232dddbf8356596fab10a8efa Mon Sep 17 00:00:00 2001 From: Alexey Sachkov Date: Thu, 18 Sep 2025 13:06:31 +0200 Subject: [PATCH 2/2] Add one more test case --- .../free-function-kernel-type-alias-arg.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp b/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp index 00dc0abbe0db0..d605d8be537bf 100644 --- a/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp +++ b/clang/test/CodeGenSYCL/free-function-kernel-type-alias-arg.cpp @@ -23,6 +23,11 @@ struct Bar {}; template using BarUsing = Bar; +class Baz { +public: + using type = BarUsing; +}; + } // namespace ns [[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] @@ -51,3 +56,8 @@ void bar_using(ns::BarUsing Arg) {} template void bar_using(ns::BarUsing); // CHECK: template void bar_using(ns::Bar); + +[[__sycl_detail__::add_ir_attributes_function("sycl-nd-range-kernel", 2)]] +void baz_type(ns::Baz::type Arg) {} + +// CHECK: void baz_type(ns::Bar Arg);