Skip to content

Commit 9d4b5ef

Browse files
author
LU-JOHN
authored
[sycl-post-link] Internalize non SYCL_EXTERNAL functions (#14318)
With -support-dynamic-linking, non SYCL_EXTERNAL functions are internalized. --------- Signed-off-by: Lu, John <[email protected]>
1 parent a458595 commit 9d4b5ef

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed

llvm/lib/SYCLLowerIR/ModuleSplitter.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/Support/FileSystem.h"
3030
#include "llvm/Transforms/IPO.h"
3131
#include "llvm/Transforms/IPO/GlobalDCE.h"
32+
#include "llvm/Transforms/IPO/Internalize.h"
3233
#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
3334
#include "llvm/Transforms/IPO/StripSymbols.h"
3435
#include "llvm/Transforms/Utils/Cloning.h"
@@ -643,13 +644,23 @@ void ModuleDesc::restoreLinkageOfDirectInvokeSimdTargets() {
643644
}
644645
}
645646

647+
// Predicate for Internalize pass.
648+
bool mustPreserveGV(const GlobalValue &GV) {
649+
if (const Function *F = dyn_cast<Function>(&GV))
650+
if (!canBeImportedFunction(*F))
651+
return false;
652+
return true;
653+
}
654+
646655
// TODO: try to move all passes (cleanup, spec consts, compile time properties)
647656
// in one place and execute MPM.run() only once.
648657
void ModuleDesc::cleanup() {
649658
ModuleAnalysisManager MAM;
650659
MAM.registerPass([&] { return PassInstrumentationAnalysis(); });
651660
ModulePassManager MPM;
652661
// Do cleanup.
662+
if (SupportDynamicLinking)
663+
MPM.addPass(InternalizePass(mustPreserveGV));
653664
MPM.addPass(GlobalDCEPass()); // Delete unreachable globals.
654665
MPM.addPass(StripDeadDebugInfoPass()); // Remove dead debug info.
655666
MPM.addPass(StripDeadPrototypesPass()); // Remove dead func decls.

llvm/test/tools/sycl-post-link/exclude_external_functions.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
; and is included in the device image.
3030
; Function __private starts with "__" and thus is included in the device image.
3131
; CHECK-SYM-2: childB
32-
; CHECK-LL-2: define weak_odr spir_func void @internal() {
33-
; CHECK-LL-2: define weak_odr spir_func void @__private() #0 {
32+
; CHECK-LL-2: define internal spir_func void @internal() {
33+
; CHECK-LL-2: define internal spir_func void @__private() #0 {
3434
; CHECK-LL-2: define weak_odr spir_func void @childB() #0 {
3535
; CHECK-LL-2: attributes #0 = { "sycl-module-id"="a.cpp" }
3636

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
; Test that when -support-dynamic-linking is used
2+
; non SYCL-EXTERNAL functions are internalized.
3+
; Variables must not be internalized.
4+
5+
; RUN: sycl-post-link -symbols -support-dynamic-linking -split=kernel -S < %s -o %t.table
6+
; RUN: FileCheck %s -input-file=%t_0.sym --check-prefixes CHECK-SYM-0
7+
; RUN: FileCheck %s -input-file=%t_0.ll --check-prefixes CHECK-LL-0
8+
9+
; CHECK-SYM-0: foo0
10+
11+
; Non SYCL-EXTERNAL Functions are internalized
12+
; foo0 is a SYCL-EXTERNAL function
13+
; CHECK-LL-0-DAG: define weak_odr spir_kernel void @foo0() #0 {
14+
; Internalize does not change available_externally
15+
; CHECK-LL-0-DAG: define available_externally spir_func void @internalA() {
16+
; CHECK-LL-0-DAG: define internal spir_func void @internalB() {
17+
; CHECK-LL-0-DAG: define internal spir_func void @internalC() {
18+
; CHECK-LL-0-DAG: define internal spir_func void @internalD() {
19+
; CHECK-LL-0-DAG: define internal spir_func void @internalE() {
20+
; CHECK-LL-0-DAG: define internal spir_func void @internalF() {
21+
; private is already internalized
22+
; CHECK-LL-0-DAG: define private spir_func void @internalG() {
23+
; CHECK-LL-0-DAG: define internal spir_func void @internalH() {
24+
; CHECK-LL-0-DAG: define internal spir_func void @internalI() {
25+
; CHECK-LL-0-DAG: attributes #0 = { "sycl-module-id"="a.cpp" }
26+
27+
; Ensure variables are unchanged
28+
; CHECK-LL-0-DAG: @ae = available_externally addrspace(1) global i32 79, align 4
29+
; CHECK-LL-0-DAG: @i1 = addrspace(1) global i32 1, align 4
30+
; CHECK-LL-0-DAG: @i2 = internal addrspace(1) global i32 2, align 4
31+
; CHECK-LL-0-DAG: @i3 = addrspace(1) global i32 3, align 4
32+
; CHECK-LL-0-DAG: @i4 = common addrspace(1) global i32 0, align 4
33+
; CHECK-LL-0-DAG: @i5 = internal addrspace(1) global i32 0, align 4
34+
; CHECK-LL-0-DAG: @color_table = addrspace(2) constant [2 x i32] [i32 0, i32 1], align 4
35+
; CHECK-LL-0-DAG: @noise_table = external addrspace(2) constant [256 x i32]
36+
; CHECK-LL-0-DAG: @w = addrspace(1) constant i32 0, align 4
37+
; CHECK-LL-0-DAG: @f.color_table = internal addrspace(2) constant [2 x i32] [i32 2, i32 3], align 4
38+
; CHECK-LL-0-DAG: @e = external addrspace(1) global i32
39+
; CHECK-LL-0-DAG: @f.t = internal addrspace(1) global i32 5, align 4
40+
; CHECK-LL-0-DAG: @f.stint = internal addrspace(1) global i32 0, align 4
41+
; CHECK-LL-0-DAG: @f.inside = internal addrspace(1) global i32 0, align 4
42+
; CHECK-LL-0-DAG: @f.b = internal addrspace(2) constant float 1.000000e+00, align 4
43+
44+
target triple = "spir64-unknown-unknown"
45+
46+
@ae = available_externally addrspace(1) global i32 79, align 4
47+
@i1 = addrspace(1) global i32 1, align 4
48+
@i2 = internal addrspace(1) global i32 2, align 4
49+
@i3 = addrspace(1) global i32 3, align 4
50+
@i4 = common addrspace(1) global i32 0, align 4
51+
@i5 = internal addrspace(1) global i32 0, align 4
52+
@color_table = addrspace(2) constant [2 x i32] [i32 0, i32 1], align 4
53+
@noise_table = external addrspace(2) constant [256 x i32]
54+
@w = addrspace(1) constant i32 0, align 4
55+
@f.color_table = internal addrspace(2) constant [2 x i32] [i32 2, i32 3], align 4
56+
@e = external addrspace(1) global i32
57+
@f.t = internal addrspace(1) global i32 5, align 4
58+
@f.stint = internal addrspace(1) global i32 0, align 4
59+
@f.inside = internal addrspace(1) global i32 0, align 4
60+
@f.b = internal addrspace(2) constant float 1.000000e+00, align 4
61+
62+
define available_externally spir_func void @internalA() {
63+
ret void
64+
}
65+
66+
define dso_local spir_func void @internalB() {
67+
ret void
68+
}
69+
70+
define external spir_func void @internalC() {
71+
ret void
72+
}
73+
74+
define internal spir_func void @internalD() {
75+
ret void
76+
}
77+
78+
define linkonce spir_func void @internalE() {
79+
ret void
80+
}
81+
82+
define linkonce_odr spir_func void @internalF() {
83+
ret void
84+
}
85+
86+
define private spir_func void @internalG() {
87+
ret void
88+
}
89+
90+
define weak spir_func void @internalH() {
91+
ret void
92+
}
93+
94+
define weak_odr spir_func void @internalI() {
95+
ret void
96+
}
97+
98+
define weak_odr spir_kernel void @foo0() #0 {
99+
call void @internalA()
100+
call void @internalB()
101+
call void @internalC()
102+
call void @internalD()
103+
call void @internalE()
104+
call void @internalF()
105+
call void @internalG()
106+
call void @internalH()
107+
call void @internalI()
108+
109+
%1 = load i32, ptr addrspace(1) @ae
110+
%2 = load i32, ptr addrspace(1) @i1
111+
%3 = load i32, ptr addrspace(1) @i2
112+
%4 = load i32, ptr addrspace(1) @i3
113+
%5 = load i32, ptr addrspace(1) @i4
114+
%6 = load i32, ptr addrspace(1) @i5
115+
%7 = load i32, ptr addrspace(2) @color_table
116+
%8 = load i32, ptr addrspace(2) @noise_table
117+
%9 = load i32, ptr addrspace(1) @w
118+
%10 = load i32, ptr addrspace(2) @f.color_table
119+
%11 = load i32, ptr addrspace(1) @e
120+
%12 = load i32, ptr addrspace(1) @f.t
121+
%13 = load i32, ptr addrspace(1) @f.stint
122+
%14 = load i32, ptr addrspace(1) @f.inside
123+
%15 = load i32, ptr addrspace(2) @f.b
124+
125+
126+
ret void
127+
}
128+
129+
attributes #0 = { "sycl-module-id"="a.cpp" }

0 commit comments

Comments
 (0)