Skip to content

[SYCL] Always generate specialization constants buffer #4591

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

Merged
merged 2 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 2 additions & 15 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2220,14 +2220,9 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {
return true;
}

// Generate kernel argument to intialize specialization constants. This
// argument is only generated when the target has no native support for
// specialization constants
// Generate kernel argument to initialize specialization constants.
void handleSyclKernelHandlerType() {
ASTContext &Context = SemaRef.getASTContext();
if (isDefaultSPIRArch(Context))
return;

StringRef Name = "_arg__specialization_constants_buffer";
addParam(Name, Context.getPointerType(Context.getAddrSpaceQualType(
Context.CharTy, LangAS::sycl_global)));
Expand Down Expand Up @@ -2470,9 +2465,6 @@ class SyclOptReportCreator : public SyclKernelFieldHandler {
}

void handleSyclKernelHandlerType() {
ASTContext &Context = SemaRef.getASTContext();
if (isDefaultSPIRArch(Context))
return;
addParam(DC.getParamVarDeclsForCurrentField()[0]->getType(),
"SYCL2020 specialization constant");
}
Expand Down Expand Up @@ -3321,13 +3313,8 @@ class SyclKernelIntHeaderCreator : public SyclKernelFieldHandler {
void handleSyclKernelHandlerType(QualType Ty) {
// The compiler generated kernel argument used to initialize SYCL 2020
// specialization constants, `specialization_constants_buffer`, should
// have corresponding entry in integration header. This argument is
// only generated when target has no native support for specialization
// constants.
// have corresponding entry in integration header.
ASTContext &Context = SemaRef.getASTContext();
if (isDefaultSPIRArch(Context))
return;

// Offset is zero since kernel_handler argument is not part of
// kernel object (i.e. it is not captured)
addParam(Context.getPointerType(Context.CharTy),
Expand Down
22 changes: 16 additions & 6 deletions clang/test/CodeGenSYCL/int_header_sycl2020_spec_const.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple nvptx64-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s --check-prefix=NONATIVESUPPORT --check-prefix=ALL
// Generic SPIR-V target
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s --check-prefix=NATIVESUPPORT --check-prefix=ALL
// RUN: FileCheck -input-file=%t.h %s
//
// SPIR-V AOT targets
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64_gen-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64_x86_64-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64_fpga-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s
//
// Non-SPIR target
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple nvptx64-unknown-unknown -fsycl-int-header=%t.h %s -o %t.out %s -o %t.out
// RUN: FileCheck -input-file=%t.h %s

// This test checks that the compiler generates required information
// in integration header for kernel_handler type (SYCL 2020 specialization
Expand All @@ -24,6 +35,5 @@ int main() {
kh);
});
}
// ALL: const kernel_param_desc_t kernel_signatures[] = {
// NONATIVESUPPORT: { kernel_param_kind_t::kind_specialization_constants_buffer, 8, 0 }
// NATIVESUPPORT-NOT: { kernel_param_kind_t::kind_specialization_constants_buffer, 8, 0 }
// CHECK: const kernel_param_desc_t kernel_signatures[] = {
// CHECK: { kernel_param_kind_t::kind_specialization_constants_buffer, 8, 0 }
27 changes: 13 additions & 14 deletions clang/test/CodeGenSYCL/kernel-handler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple nvptx64-unknown-unknown -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=NONATIVESUPPORT
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64-unknown-unknown -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=NATIVESUPPORT
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple nvptx64-unknown-unknown -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=ALL,NONATIVESUPPORT
// RUN: %clang_cc1 -fsycl-is-device -internal-isystem %S/Inputs -triple spir64-unknown-unknown -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefixes=ALL,NATIVESUPPORT

// This test checks IR generated when kernel_handler argument
// (used to handle SYCL 2020 specialization constants) is passed
Expand All @@ -22,18 +22,17 @@ void test(int val) {
});
}

// NONATIVESUPPORT: define dso_local void @{{.*}}test_kernel_handler{{[^(]*}}
// NONATIVESUPPORT-SAME: (i32 %_arg_, i8 addrspace(1)* %_arg__specialization_constants_buffer)
// NONATIVESUPPORT: %kh = alloca %"class.cl::sycl::kernel_handler", align 1
// ALL: define dso_local{{ spir_kernel | }}void @{{.*}}test_kernel_handler{{[^(]*}}
// ALL-SAME: (i32 %_arg_, i8 addrspace(1)* %_arg__specialization_constants_buffer)
// ALL: %kh = alloca %"class.cl::sycl::kernel_handler", align 1

// NONATIVESUPPORT: %[[KH:[0-9]+]] = load i8 addrspace(1)*, i8 addrspace(1)** %_arg__specialization_constants_buffer.addr, align 8
// NONATIVESUPPORT: %[[ADDRSPACECAST:[0-9]+]] = addrspacecast i8 addrspace(1)* %[[KH]] to i8*
// NONATIVESUPPORT: call void @{{.*}}__init_specialization_constants_buffer{{.*}}(%"class.cl::sycl::kernel_handler"* nonnull align 1 dereferenceable(1) %kh, i8* %[[ADDRSPACECAST]])
// NONATIVESUPPORT: void @[[MANGLEDKERNELCALL:[a-zA-Z0-9_$]+]]
// NONATIVESUPPORT-SAME: byval(%"class.cl::sycl::kernel_handler")

// NATIVESUPPORT: define dso_local spir_kernel void @{{.*}}test_kernel_handler{{[^(]*}}
// NATIVESUPPORT-SAME: (i32 %_arg_)
// NATIVESUPPORT: %kh = alloca %"class.cl::sycl::kernel_handler"
// NATIVESUPPORT-NOT: __init_specialization_constants_buffer
// NATIVE-SUPPORT: call spir_func void @"[[MANGLEDKERNELCALL:[a-zA-Z0-9_$]+]]"
// NATIVE-SUPPORT-SAME: byval(%"class.cl::sycl::kernel_handler")

// NATIVESUPPORT-NOT: load i8 addrspace(1)*, i8 addrspace(1)** %_arg__specialization_constants_buffer.addr, align 8
// NATIVESUPPORT-NOT: addrspacecast i8 addrspace(1)* %{{[0-9]+}} to i8*
// NATIVESUPPORT-NOT: call void @{{.*}}__init_specialization_constants_buffer{{.*}}(%"class.cl::sycl::kernel_handler"* nonnull align 1 dereferenceable(1) %kh, i8* %{{[0-9]+}})

// ALL: call{{ spir_func | }}void @{{[a-zA-Z0-9_$]+}}kernel_handler{{[a-zA-Z0-9_$]+}}
// ALL-SAME: byval(%"class.cl::sycl::kernel_handler")
3 changes: 2 additions & 1 deletion clang/test/SemaSYCL/kernel-handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ int main() {
// Test AST for default SPIR architecture

// Check test_kernel_handler parameters
// NATIVESUPPORT: FunctionDecl {{.*}}test_kernel_handler{{.*}} 'void (int)'
// NATIVESUPPORT: FunctionDecl {{.*}}test_kernel_handler{{.*}} 'void (int, __global char *)'
// NATIVESUPPORT-NEXT: ParmVarDecl {{.*}} used _arg_ 'int'
// NATIVESUPPORT-NEXT: ParmVarDecl {{.*}} used _arg__specialization_constants_buffer '__global char *'

// Check declaration and initialization of kernel object local clone
// NATIVESUPPORT-NEXT: CompoundStmt
Expand Down
2 changes: 1 addition & 1 deletion sycl/source/detail/device_image_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class device_image_impl {

RT::PiMem &get_spec_const_buffer_ref() noexcept {
std::lock_guard<std::mutex> Lock{MSpecConstAccessMtx};
if (nullptr == MSpecConstsBuffer) {
if (nullptr == MSpecConstsBuffer && !MSpecConstsBlob.empty()) {
const detail::plugin &Plugin = getSyclObjImpl(MContext)->getPlugin();
Plugin.call<PiApiKind::piMemBufferCreate>(
detail::getSyclObjImpl(MContext)->getHandleRef(),
Expand Down
5 changes: 4 additions & 1 deletion sycl/source/detail/scheduler/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1797,8 +1797,11 @@ static pi_result SetKernelParamsAndLaunch(
}
assert(DeviceImageImpl != nullptr);
RT::PiMem SpecConstsBuffer = DeviceImageImpl->get_spec_const_buffer_ref();
// Avoid taking an address of nullptr
RT::PiMem *SpecConstsBufferArg =
SpecConstsBuffer ? &SpecConstsBuffer : nullptr;
Plugin.call<PiApiKind::piextKernelSetArgMemObj>(Kernel, NextTrueIndex,
&SpecConstsBuffer);
SpecConstsBufferArg);
break;
}
case kernel_param_kind_t::kind_invalid:
Expand Down