Skip to content

Commit 766ca16

Browse files
committed
[SYCL] Update aspect propagation for virtual functions
1 parent 48a75f4 commit 766ca16

File tree

4 files changed

+144
-1
lines changed

4 files changed

+144
-1
lines changed

llvm/lib/SYCLLowerIR/SYCLPropagateAspectsUsage.cpp

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,37 @@ void setSyclFixedTargetsMD(const std::vector<Function *> &EntryPoints,
647647
F->setMetadata("sycl_fixed_targets", MDN);
648648
}
649649

650+
void collectVirtualFunctionSetInfo(
651+
Function &F, StringMap<std::vector<Function *>> &VirtualFunctionSets) {
652+
if (!F.hasFnAttribute("indirectly-callable"))
653+
return;
654+
Attribute IndirectlyCallableAttr = F.getFnAttribute("indirectly-callable");
655+
StringRef SetName = IndirectlyCallableAttr.getValueAsString();
656+
VirtualFunctionSets[SetName].push_back(&F);
657+
}
658+
659+
// For each set S of virtual functions that F declares,
660+
// propagate S through the CG and then
661+
void processDeclaredVirtualFunctionSets(
662+
Function *F, CallGraphTy &CG, FunctionToAspectsMapTy &AspectsMap,
663+
SmallPtrSet<const Function *, 16> &Visited,
664+
StringMap<std::vector<Function *>> &VirtualFunctionSets) {
665+
if (!F->hasFnAttribute("calls-indirectly"))
666+
return;
667+
Attribute CallsIndirectlyAttr = F->getFnAttribute("calls-indirectly");
668+
SmallVector<StringRef, 4> DeclaredVirtualFunctionSetNames;
669+
CallsIndirectlyAttr.getValueAsString().split(DeclaredVirtualFunctionSetNames,
670+
",");
671+
auto &AspectsF = AspectsMap[F];
672+
for (auto Name : DeclaredVirtualFunctionSetNames) {
673+
for (auto VFn : VirtualFunctionSets[Name]) {
674+
propagateAspectsThroughCG(VFn, CG, AspectsMap, Visited);
675+
for (auto Aspect : AspectsMap[VFn])
676+
AspectsF.insert(Aspect);
677+
}
678+
}
679+
}
680+
650681
/// Returns a map of functions with corresponding used aspects.
651682
std::pair<FunctionToAspectsMapTy, FunctionToAspectsMapTy>
652683
buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects,
@@ -655,16 +686,21 @@ buildFunctionsToAspectsMap(Module &M, TypeToAspectsMapTy &TypesWithAspects,
655686
bool ValidateAspects, bool FP64ConvEmu) {
656687
FunctionToAspectsMapTy FunctionToUsedAspects;
657688
FunctionToAspectsMapTy FunctionToDeclaredAspects;
689+
StringMap<std::vector<Function *>> VirtualFunctionSets;
658690
CallGraphTy CG;
659691

660692
for (Function &F : M.functions()) {
661693
processFunction(F, FunctionToUsedAspects, FunctionToDeclaredAspects,
662694
TypesWithAspects, CG, AspectValues, FP64ConvEmu);
695+
collectVirtualFunctionSetInfo(F, VirtualFunctionSets);
663696
}
664697

665698
SmallPtrSet<const Function *, 16> Visited;
666-
for (Function *F : EntryPoints)
699+
for (Function *F : EntryPoints) {
667700
propagateAspectsThroughCG(F, CG, FunctionToUsedAspects, Visited);
701+
processDeclaredVirtualFunctionSets(F, CG, FunctionToUsedAspects, Visited,
702+
VirtualFunctionSets);
703+
}
668704

669705
if (ValidateAspects)
670706
validateUsedAspectsForFunctions(FunctionToUsedAspects, AspectValues,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s
2+
3+
; CHECK: @vfn() #0 !sycl_used_aspects ![[#aspects:]]
4+
define spir_func void @vfn() #0 {
5+
%tmp = alloca double
6+
ret void
7+
}
8+
9+
; CHECK: @foo() #1 !sycl_used_aspects ![[#aspects]]
10+
define spir_kernel void @foo() #1 {
11+
ret void
12+
}
13+
14+
; CHECK: ![[#aspects]] = !{i32 6}
15+
16+
attributes #0 = { "indirectly-callable"="_ZTSv" }
17+
attributes #1 = { "calls-indirectly"="_ZTSv" }
18+
19+
!sycl_aspects = !{!0}
20+
!0 = !{!"fp64", i32 6}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s
2+
3+
%Foo = type { i32 }
4+
%Bar = type { i32 }
5+
6+
; CHECK: @vfnFoo() #0 !sycl_used_aspects ![[#aspectsFoo:]]
7+
define spir_func void @vfnFoo() #0 {
8+
%tmp = alloca %Foo
9+
ret void
10+
}
11+
12+
; CHECK: @vfnBar() #1 !sycl_used_aspects ![[#aspectsBar:]]
13+
define spir_func void @vfnBar() #1 {
14+
%tmp = alloca %Bar
15+
ret void
16+
}
17+
18+
; CHECK: @kernel() #2 !sycl_used_aspects ![[#aspectsKernel:]]
19+
define spir_kernel void @kernel() #2 {
20+
ret void
21+
}
22+
23+
; CHECK: ![[#aspectsFoo]] = !{i32 1}
24+
; CHECK: ![[#aspectsBar]] = !{i32 2}
25+
; CHECK: ![[#aspectsKernel]] = !{i32 1, i32 2}
26+
27+
attributes #0 = { "indirectly-callable"="setFoo" }
28+
attributes #1 = { "indirectly-callable"="setBar" }
29+
attributes #2 = { "calls-indirectly"="setFoo,setBar" }
30+
31+
!sycl_aspects = !{!0}
32+
!0 = !{!"fp64", i32 6}
33+
34+
!sycl_types_that_use_aspects = !{!1, !2}
35+
!1 = !{!"Foo", i32 1}
36+
!2 = !{!"Bar", i32 2}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
; RUN: opt -passes=sycl-propagate-aspects-usage < %s -S | FileCheck %s
2+
3+
%Foo = type { i32 }
4+
%Bar = type { i32 }
5+
6+
; CHECK: @vfnFoo() #0 !sycl_used_aspects ![[#aspectsFoo:]]
7+
define spir_func void @vfnFoo() #0 {
8+
call void @subFoo()
9+
ret void
10+
}
11+
12+
define spir_func void @subFoo() {
13+
%tmp = alloca %Foo
14+
ret void
15+
}
16+
17+
; CHECK: @vfnBar() #1 !sycl_used_aspects ![[#aspectsBar:]]
18+
define spir_func void @vfnBar() #1 {
19+
call void @subBar()
20+
ret void
21+
}
22+
23+
define spir_func void @subBar() {
24+
%tmp = alloca %Bar
25+
ret void
26+
}
27+
28+
; CHECK: @kernelA() #2 !sycl_used_aspects ![[#aspectsFoo]]
29+
define spir_kernel void @kernelA() #2 {
30+
ret void
31+
}
32+
33+
; CHECK: @kernelB() #3 !sycl_used_aspects ![[#aspectsBar]]
34+
define spir_kernel void @kernelB() #3 {
35+
ret void
36+
}
37+
38+
; CHECK: ![[#aspectsFoo]] = !{i32 1}
39+
; CHECK: ![[#aspectsBar]] = !{i32 2}
40+
41+
attributes #0 = { "indirectly-callable"="setFoo" }
42+
attributes #1 = { "indirectly-callable"="setBar" }
43+
attributes #2 = { "calls-indirectly"="setFoo" }
44+
attributes #3 = { "calls-indirectly"="setBar" }
45+
46+
!sycl_aspects = !{!0}
47+
!0 = !{!"fp64", i32 6}
48+
49+
!sycl_types_that_use_aspects = !{!1, !2}
50+
!1 = !{!"Foo", i32 1}
51+
!2 = !{!"Bar", i32 2}

0 commit comments

Comments
 (0)