Skip to content

Commit 430a3b3

Browse files
[SYCL] Disable dead arg elimination for free function kernels (#19776)
This PR disables dead argument elimination for free function kernels. When using a free function kernel, the user sets the arguments manually using `handler::set_arg` but if certain arguments are optimized away in case they are not used, the implementation needs to be aware of this and remove the relevant `set_arg` calls. Instead, its much more feasible to just disable dead arg elimination in this case. --------- Co-authored-by: Alexey Sachkov <[email protected]>
1 parent 69ac374 commit 430a3b3

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

llvm/lib/Transforms/IPO/DeadArgumentElimination.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,17 @@ void DeadArgumentEliminationPass::surveyFunction(const Function &F) {
575575
return;
576576
}
577577

578+
// Do not modify arguments when the SYCL kernel is a free function kernel.
579+
// In this case, the user sets the arguments of the kernel by themselves
580+
// and dead argument elimination may interfere with their expectations.
581+
const bool FuncIsSyclFreeFunctionKernel =
582+
F.hasFnAttribute("sycl-single-task-kernel") ||
583+
F.hasFnAttribute("sycl-nd-range-kernel");
584+
if (FuncIsSyclFreeFunctionKernel) {
585+
markFrozen(F);
586+
return;
587+
}
588+
578589
LLVM_DEBUG(
579590
dbgs() << "DeadArgumentEliminationPass - Inspecting callers for fn: "
580591
<< F.getName() << "\n");

llvm/test/Transforms/DeadArgElim/sycl-kernels-neg.ll

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,30 @@ define weak_odr void @NotASpirKernel(float %arg1, float %arg2) {
2424

2525
define weak_odr void @ESIMDKernel(float %arg1, float %arg2) !sycl_explicit_simd !0 {
2626
; CHECK-LABEL: define {{[^@]+}}@ESIMDKernel
27-
; CHECK-SAME: (float [[ARG1:%.*]], float [[ARG2:%.*]]) !sycl_explicit_simd !0 {
27+
; CHECK-SAME: (float [[ARG1:%.*]], float [[ARG2:%.*]]) {{.*}}{
28+
; CHECK-NEXT: call void @foo(float [[ARG1]])
29+
; CHECK-NEXT: ret void
30+
;
31+
call void @foo(float %arg1)
32+
ret void
33+
}
34+
35+
; The following two tests ensure that dead arguments are not eliminated
36+
; from a free function kernel.
37+
38+
define weak_odr spir_kernel void @FreeFuncKernelSingleTask(float %arg1, float %arg2) "sycl-single-task-kernel"="0" {
39+
; CHECK-LABEL: define {{[^@]+}}@FreeFuncKernelSingleTask
40+
; CHECK-SAME: (float [[ARG1:%.*]], float [[ARG2:%.*]]) #[[ATTR0:[0-9]+]] {
41+
; CHECK-NEXT: call void @foo(float [[ARG1]])
42+
; CHECK-NEXT: ret void
43+
;
44+
call void @foo(float %arg1)
45+
ret void
46+
}
47+
48+
define weak_odr spir_kernel void @FreeFuncKernelNdRange(float %arg1, float %arg2) "sycl-nd-range-kernel"="0" {
49+
; CHECK-LABEL: define {{[^@]+}}@FreeFuncKernelNdRange
50+
; CHECK-SAME: (float [[ARG1:%.*]], float [[ARG2:%.*]]) #[[ATTR1:[0-9]+]] {
2851
; CHECK-NEXT: call void @foo(float [[ARG1]])
2952
; CHECK-NEXT: ret void
3053
;

sycl/test-e2e/FreeFunctionKernels/template_specialization.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,10 @@ int main() {
173173
test_func_custom_type<A::B::C::TestClass>();
174174
test_func<F<float>, float>();
175175
test_func<F<uint32_t>, uint32_t>();
176-
test_func<variadic_templated<double>, double>();
176+
// Variadic template functions do not work with free function kernels. See
177+
// CMPLRLLVM-69528.
178+
// TODO: Uncomment the following line once the tracker is resolved.
179+
// test_func<variadic_templated<double>, double>();
177180
test_func<sum1<3, float>, float>();
178181
test_accessor();
179182
test_shared();

0 commit comments

Comments
 (0)