From 766ca160284af763d004f4236421cba74c25b00a Mon Sep 17 00:00:00 2001 From: "Cai, Justin" Date: Tue, 15 Oct 2024 14:07:22 +0000 Subject: [PATCH 1/5] [SYCL] Update aspect propagation for virtual functions --- .../SYCLLowerIR/SYCLPropagateAspectsUsage.cpp | 38 +++++++++++++- .../virtual-functions-1.ll | 20 ++++++++ .../virtual-functions-2.ll | 36 +++++++++++++ .../virtual-functions-3.ll | 51 +++++++++++++++++++ 4 files changed, 144 insertions(+), 1 deletion(-) create mode 100644 llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll create mode 100644 llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll create mode 100644 llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll diff --git a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp index 6d8c248a81607..a606ac09c9976 100644 --- a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp +++ b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp @@ -647,6 +647,37 @@ void setSyclFixedTargetsMD(const std::vector &EntryPoints, F->setMetadata("sycl_fixed_targets", MDN); } +void collectVirtualFunctionSetInfo( + Function &F, StringMap> &VirtualFunctionSets) { + if (!F.hasFnAttribute("indirectly-callable")) + return; + Attribute IndirectlyCallableAttr = F.getFnAttribute("indirectly-callable"); + StringRef SetName = IndirectlyCallableAttr.getValueAsString(); + VirtualFunctionSets[SetName].push_back(&F); +} + +// For each set S of virtual functions that F declares, +// propagate S through the CG and then +void processDeclaredVirtualFunctionSets( + Function *F, CallGraphTy &CG, FunctionToAspectsMapTy &AspectsMap, + SmallPtrSet &Visited, + StringMap> &VirtualFunctionSets) { + if (!F->hasFnAttribute("calls-indirectly")) + return; + Attribute CallsIndirectlyAttr = F->getFnAttribute("calls-indirectly"); + SmallVector DeclaredVirtualFunctionSetNames; + CallsIndirectlyAttr.getValueAsString().split(DeclaredVirtualFunctionSetNames, + ","); + auto &AspectsF = AspectsMap[F]; + for (auto Name : DeclaredVirtualFunctionSetNames) { + for (auto VFn : VirtualFunctionSets[Name]) { + propagateAspectsThroughCG(VFn, CG, AspectsMap, Visited); + for (auto Aspect : AspectsMap[VFn]) + AspectsF.insert(Aspect); + } + } +} + /// Returns a map of functions with corresponding used aspects. std::pair buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects, @@ -655,16 +686,21 @@ buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects, bool ValidateAspects, bool FP64ConvEmu) { FunctionToAspectsMapTy FunctionToUsedAspects; FunctionToAspectsMapTy FunctionToDeclaredAspects; + StringMap> VirtualFunctionSets; CallGraphTy CG; for (Function &F : M.functions()) { processFunction(F, FunctionToUsedAspects, FunctionToDeclaredAspects, TypesWithAspects, CG, AspectValues, FP64ConvEmu); + collectVirtualFunctionSetInfo(F, VirtualFunctionSets); } SmallPtrSet Visited; - for (Function *F : EntryPoints) + for (Function *F : EntryPoints) { propagateAspectsThroughCG(F, CG, FunctionToUsedAspects, Visited); + processDeclaredVirtualFunctionSets(F, CG, FunctionToUsedAspects, Visited, + VirtualFunctionSets); + } if (ValidateAspects) validateUsedAspectsForFunctions(FunctionToUsedAspects, AspectValues, diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll new file mode 100644 index 0000000000000..f500a274d82fe --- /dev/null +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll @@ -0,0 +1,20 @@ +; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s + +; CHECK: @vfn() #0 !sycl_used_aspects ![[#aspects:]] +define spir_func void @vfn() #0 { + %tmp = alloca double + ret void +} + +; CHECK: @foo() #1 !sycl_used_aspects ![[#aspects]] +define spir_kernel void @foo() #1 { + ret void +} + +; CHECK: ![[#aspects]] = !{i32 6} + +attributes #0 = { "indirectly-callable"="_ZTSv" } +attributes #1 = { "calls-indirectly"="_ZTSv" } + +!sycl_aspects = !{!0} +!0 = !{!"fp64", i32 6} \ No newline at end of file diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll new file mode 100644 index 0000000000000..f8fd9306c2403 --- /dev/null +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll @@ -0,0 +1,36 @@ +; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s + +%Foo = type { i32 } +%Bar = type { i32 } + +; CHECK: @vfnFoo() #0 !sycl_used_aspects ![[#aspectsFoo:]] +define spir_func void @vfnFoo() #0 { + %tmp = alloca %Foo + ret void +} + +; CHECK: @vfnBar() #1 !sycl_used_aspects ![[#aspectsBar:]] +define spir_func void @vfnBar() #1 { + %tmp = alloca %Bar + ret void +} + +; CHECK: @kernel() #2 !sycl_used_aspects ![[#aspectsKernel:]] +define spir_kernel void @kernel() #2 { + ret void +} + +; CHECK: ![[#aspectsFoo]] = !{i32 1} +; CHECK: ![[#aspectsBar]] = !{i32 2} +; CHECK: ![[#aspectsKernel]] = !{i32 1, i32 2} + +attributes #0 = { "indirectly-callable"="setFoo" } +attributes #1 = { "indirectly-callable"="setBar" } +attributes #2 = { "calls-indirectly"="setFoo,setBar" } + +!sycl_aspects = !{!0} +!0 = !{!"fp64", i32 6} + +!sycl_types_that_use_aspects = !{!1, !2} +!1 = !{!"Foo", i32 1} +!2 = !{!"Bar", i32 2} \ No newline at end of file diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll new file mode 100644 index 0000000000000..6f229c754d4ce --- /dev/null +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll @@ -0,0 +1,51 @@ +; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s + +%Foo = type { i32 } +%Bar = type { i32 } + +; CHECK: @vfnFoo() #0 !sycl_used_aspects ![[#aspectsFoo:]] +define spir_func void @vfnFoo() #0 { + call void @subFoo() + ret void +} + +define spir_func void @subFoo() { + %tmp = alloca %Foo + ret void +} + +; CHECK: @vfnBar() #1 !sycl_used_aspects ![[#aspectsBar:]] +define spir_func void @vfnBar() #1 { + call void @subBar() + ret void +} + +define spir_func void @subBar() { + %tmp = alloca %Bar + ret void +} + +; CHECK: @kernelA() #2 !sycl_used_aspects ![[#aspectsFoo]] +define spir_kernel void @kernelA() #2 { + ret void +} + +; CHECK: @kernelB() #3 !sycl_used_aspects ![[#aspectsBar]] +define spir_kernel void @kernelB() #3 { + ret void +} + +; CHECK: ![[#aspectsFoo]] = !{i32 1} +; CHECK: ![[#aspectsBar]] = !{i32 2} + +attributes #0 = { "indirectly-callable"="setFoo" } +attributes #1 = { "indirectly-callable"="setBar" } +attributes #2 = { "calls-indirectly"="setFoo" } +attributes #3 = { "calls-indirectly"="setBar" } + +!sycl_aspects = !{!0} +!0 = !{!"fp64", i32 6} + +!sycl_types_that_use_aspects = !{!1, !2} +!1 = !{!"Foo", i32 1} +!2 = !{!"Bar", i32 2} \ No newline at end of file From db5e083dd09f53ee45bc249aaa6dedf341ec7a69 Mon Sep 17 00:00:00 2001 From: "Cai, Justin" Date: Tue, 15 Oct 2024 14:14:47 +0000 Subject: [PATCH 2/5] Update comment and format --- llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp | 3 ++- .../SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll | 2 +- .../SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll | 2 +- .../SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll | 2 +- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp index a606ac09c9976..4a7c6fbf1960a 100644 --- a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp +++ b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp @@ -657,7 +657,8 @@ void collectVirtualFunctionSetInfo( } // For each set S of virtual functions that F declares, -// propagate S through the CG and then +// propagate S through the CG and then add the aspects +// used by S to F. void processDeclaredVirtualFunctionSets( Function *F, CallGraphTy &CG, FunctionToAspectsMapTy &AspectsMap, SmallPtrSet &Visited, diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll index f500a274d82fe..709ca33eae3b0 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll @@ -17,4 +17,4 @@ attributes #0 = { "indirectly-callable"="_ZTSv" } attributes #1 = { "calls-indirectly"="_ZTSv" } !sycl_aspects = !{!0} -!0 = !{!"fp64", i32 6} \ No newline at end of file +!0 = !{!"fp64", i32 6} diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll index f8fd9306c2403..ae600413378f1 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll @@ -33,4 +33,4 @@ attributes #2 = { "calls-indirectly"="setFoo,setBar" } !sycl_types_that_use_aspects = !{!1, !2} !1 = !{!"Foo", i32 1} -!2 = !{!"Bar", i32 2} \ No newline at end of file +!2 = !{!"Bar", i32 2} diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll index 6f229c754d4ce..ada0f533ced56 100644 --- a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll @@ -48,4 +48,4 @@ attributes #3 = { "calls-indirectly"="setBar" } !sycl_types_that_use_aspects = !{!1, !2} !1 = !{!"Foo", i32 1} -!2 = !{!"Bar", i32 2} \ No newline at end of file +!2 = !{!"Bar", i32 2} From 6a45f70119ea4c663a3cd64d513857530bb80db6 Mon Sep 17 00:00:00 2001 From: "Cai, Justin" Date: Thu, 17 Oct 2024 07:27:52 -0700 Subject: [PATCH 3/5] Use small vector --- llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp index 4a7c6fbf1960a..126a03bdf03bf 100644 --- a/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp +++ b/llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp @@ -648,7 +648,7 @@ void setSyclFixedTargetsMD(const std::vector &EntryPoints, } void collectVirtualFunctionSetInfo( - Function &F, StringMap> &VirtualFunctionSets) { + Function &F, StringMap> &VirtualFunctionSets) { if (!F.hasFnAttribute("indirectly-callable")) return; Attribute IndirectlyCallableAttr = F.getFnAttribute("indirectly-callable"); @@ -662,7 +662,7 @@ void collectVirtualFunctionSetInfo( void processDeclaredVirtualFunctionSets( Function *F, CallGraphTy &CG, FunctionToAspectsMapTy &AspectsMap, SmallPtrSet &Visited, - StringMap> &VirtualFunctionSets) { + StringMap> &VirtualFunctionSets) { if (!F->hasFnAttribute("calls-indirectly")) return; Attribute CallsIndirectlyAttr = F->getFnAttribute("calls-indirectly"); @@ -687,7 +687,7 @@ buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects, bool ValidateAspects, bool FP64ConvEmu) { FunctionToAspectsMapTy FunctionToUsedAspects; FunctionToAspectsMapTy FunctionToDeclaredAspects; - StringMap> VirtualFunctionSets; + StringMap> VirtualFunctionSets; CallGraphTy CG; for (Function &F : M.functions()) { From 94552851dc71944659dda9ebade8897b92e990b1 Mon Sep 17 00:00:00 2001 From: "Cai, Justin" Date: Thu, 17 Oct 2024 07:29:02 -0700 Subject: [PATCH 4/5] Add negative test for construct kernel --- .../virtual-functions-4.ll | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll new file mode 100644 index 0000000000000..700a2dc7a8b79 --- /dev/null +++ b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll @@ -0,0 +1,26 @@ +; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s +target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1" +target triple = "spir64-unknown-unknown" + +@vtable = linkonce_odr dso_local unnamed_addr addrspace(1) constant { [3 x ptr addrspace(4)] } { [3 x ptr addrspace(4)] [ptr addrspace(4) null, ptr addrspace(4) null, ptr addrspace(4) addrspacecast (ptr @foo to ptr addrspace(4))] }, align 8 + +; CHECK: @foo() #0 !sycl_used_aspects ![[#aspects:]] +define linkonce_odr spir_func void @foo() #0 { +entry: + %tmp = alloca double + ret void +} + +; CHECK-NOT: @construct({{.*}}){{.*}}!sycl_used_aspects +define weak_odr dso_local spir_kernel void @construct(ptr addrspace(1) noundef align 8 %_arg_StorageAcc) { +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" } + +!sycl_aspects = !{!0} +!0 = !{!"fp64", i32 6} \ No newline at end of file From 8735bf5f1f2d709811bb9a243f0eda0f6ef20106 Mon Sep 17 00:00:00 2001 From: "Cai, Justin" Date: Thu, 17 Oct 2024 07:29:37 -0700 Subject: [PATCH 5/5] Move to own directory --- .../{ => VirtualFunctions}/virtual-functions-1.ll | 0 .../{ => VirtualFunctions}/virtual-functions-2.ll | 0 .../{ => VirtualFunctions}/virtual-functions-3.ll | 0 .../{ => VirtualFunctions}/virtual-functions-4.ll | 0 4 files changed, 0 insertions(+), 0 deletions(-) rename llvm/test/SYCLLowerIR/PropagateAspectsUsage/{ => VirtualFunctions}/virtual-functions-1.ll (100%) rename llvm/test/SYCLLowerIR/PropagateAspectsUsage/{ => VirtualFunctions}/virtual-functions-2.ll (100%) rename llvm/test/SYCLLowerIR/PropagateAspectsUsage/{ => VirtualFunctions}/virtual-functions-3.ll (100%) rename llvm/test/SYCLLowerIR/PropagateAspectsUsage/{ => VirtualFunctions}/virtual-functions-4.ll (100%) diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll similarity index 100% rename from llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-1.ll rename to llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-1.ll diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll similarity index 100% rename from llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-2.ll rename to llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-2.ll diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll similarity index 100% rename from llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-3.ll rename to llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-3.ll diff --git a/llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll b/llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll similarity index 100% rename from llvm/test/SYCLLowerIR/PropagateAspectsUsage/virtual-functions-4.ll rename to llvm/test/SYCLLowerIR/PropagateAspectsUsage/VirtualFunctions/virtual-functions-4.ll