diff --git a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp index 126a03bdf03bf..b8cbf4debec85 100644 --- a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp +++ b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp @@ -665,6 +665,24 @@ void processDeclaredVirtualFunctionSets( StringMap> &VirtualFunctionSets) { if (!F->hasFnAttribute("calls-indirectly")) return; + + // "Construction" kernels which reference vtables but do not actually + // perform any virtual calls have the calls-indirectly attribute + // attached by SYCLVirtualFunctionAnalysis pass. We do not want to + // attach sycl_used_aspects metadata to such kernels. + bool hasVirtualCall = false; + for (const Instruction &I : instructions(F)) { + const auto *CI = dyn_cast(&I); + if (!CI) + continue; + if (CI->isIndirectCall() && CI->hasFnAttr("virtual-call")) { + hasVirtualCall = true; + break; + } + } + if (!hasVirtualCall) + return; + Attribute CallsIndirectlyAttr = F->getFnAttribute("calls-indirectly"); SmallVector DeclaredVirtualFunctionSetNames; CallsIndirectlyAttr.getValueAsString().split(DeclaredVirtualFunctionSetNames, diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll index 709ca33eae3b0..5d7df6548282a 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll @@ -6,8 +6,9 @@ define spir_func void @vfn() #0 { ret void } -; CHECK: @foo() #1 !sycl_used_aspects ![[#aspects]] -define spir_kernel void @foo() #1 { +; CHECK: @foo({{.*}}) #1 !sycl_used_aspects ![[#aspects]] +define spir_kernel void @foo(ptr %f) #1 { + call void %f() #2 ret void } @@ -15,6 +16,7 @@ define spir_kernel void @foo() #1 { attributes #0 = { "indirectly-callable"="_ZTSv" } attributes #1 = { "calls-indirectly"="_ZTSv" } +attributes #2 = { "virtual-call" } !sycl_aspects = !{!0} !0 = !{!"fp64", i32 6} diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll index ae600413378f1..0b2f21ee5017b 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll @@ -15,8 +15,9 @@ define spir_func void @vfnBar() #1 { ret void } -; CHECK: @kernel() #2 !sycl_used_aspects ![[#aspectsKernel:]] -define spir_kernel void @kernel() #2 { +; CHECK: @kernel({{.*}}) #2 !sycl_used_aspects ![[#aspectsKernel:]] +define spir_kernel void @kernel(ptr %f) #2 { + call void %f() #3 ret void } @@ -27,6 +28,7 @@ define spir_kernel void @kernel() #2 { attributes #0 = { "indirectly-callable"="setFoo" } attributes #1 = { "indirectly-callable"="setBar" } attributes #2 = { "calls-indirectly"="setFoo,setBar" } +attributes #3 = { "virtual-call" } !sycl_aspects = !{!0} !0 = !{!"fp64", i32 6} diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll index ada0f533ced56..eb3e4e4b9c566 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll @@ -25,13 +25,15 @@ define spir_func void @subBar() { ret void } -; CHECK: @kernelA() #2 !sycl_used_aspects ![[#aspectsFoo]] -define spir_kernel void @kernelA() #2 { +; CHECK: @kernelA({{.*}}) #2 !sycl_used_aspects ![[#aspectsFoo]] +define spir_kernel void @kernelA(ptr %f) #2 { + call void %f() #4 ret void } -; CHECK: @kernelB() #3 !sycl_used_aspects ![[#aspectsBar]] -define spir_kernel void @kernelB() #3 { +; CHECK: @kernelB({{.*}}) #3 !sycl_used_aspects ![[#aspectsBar]] +define spir_kernel void @kernelB(ptr %f) #3 { + call void %f() #4 ret void } @@ -42,6 +44,7 @@ attributes #0 = { "indirectly-callable"="setFoo" } attributes #1 = { "indirectly-callable"="setBar" } attributes #2 = { "calls-indirectly"="setFoo" } attributes #3 = { "calls-indirectly"="setBar" } +attributes #4 = { "virtual-call" } !sycl_aspects = !{!0} !0 = !{!"fp64", i32 6} diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll index 700a2dc7a8b79..c51c8ac49843f 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll @@ -11,6 +11,8 @@ entry: ret void } +; "Construction" kernels which reference vtables but do not actually +; perform any virtual calls should not have aspects propagated to them. ; CHECK-NOT: @construct({{.*}}){{.*}}!sycl_used_aspects define weak_odr dso_local spir_kernel void @construct(ptr addrspace(1) noundef align 8 %_arg_StorageAcc) { entry: @@ -18,9 +20,20 @@ entry: ret void } +; Note: after SYCLVirtualFunctionAnalysis pass, the construction kernels will have +; "calls-indirectly" attribute, but even so they should not have aspects propagated +; to them (as the construction kernels have no virtual calls). +; CHECK-NOT: @construct2({{.*}}){{.*}}!sycl_used_aspects +define weak_odr dso_local spir_kernel void @construct2(ptr addrspace(1) noundef align 8 %_arg_StorageAcc) #1 { +entry: + store ptr addrspace(1) getelementptr inbounds inrange(-16, 8) (i8, ptr addrspace(1) @vtable, i64 16), ptr addrspace(1) %_arg_StorageAcc, align 8 + ret void +} + ; CHECK: ![[#aspects]] = !{i32 6} attributes #0 = { "indirectly-callable"="set-foo" } +attributes #1 = { "calls-indirectly"="set-foo" } !sycl_aspects = !{!0} !0 = !{!"fp64", i32 6} \ No newline at end of file