Skip to content

Commit 14d41bb

Browse files
committed
[sycl-post-link] Add device image property for assert feature
A property with the name of the kernel is added whenever the kernel uses assert. Details: https://github.com/intel/llvm/blob/sycl/sycl/doc/Assert.md#online-linking-fallback-__devicelib_assert_fail
1 parent 69fc6dc commit 14d41bb

File tree

4 files changed

+186
-1
lines changed

4 files changed

+186
-1
lines changed

llvm/include/llvm/Support/PropertySetIO.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class PropertySetRegistry {
189189
static constexpr char SYCL_DEVICELIB_REQ_MASK[] = "SYCL/devicelib req mask";
190190
static constexpr char SYCL_KERNEL_PARAM_OPT_INFO[] = "SYCL/kernel param opt";
191191
static constexpr char SYCL_MISC_PROP[] = "SYCL/misc properties";
192+
static constexpr char SYCL_ASSERT_USED[] = "SYCL/assert used";
192193

193194
// Function for bulk addition of an entire property set under given category
194195
// (property set name).

llvm/lib/Support/PropertySetIO.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ constexpr char PropertySetRegistry::SYCL_DEVICELIB_REQ_MASK[];
198198
constexpr char PropertySetRegistry::SYCL_SPEC_CONSTANTS_DEFAULT_VALUES[];
199199
constexpr char PropertySetRegistry::SYCL_KERNEL_PARAM_OPT_INFO[];
200200
constexpr char PropertySetRegistry::SYCL_MISC_PROP[];
201+
constexpr char PropertySetRegistry::SYCL_ASSERT_USED[];
201202

202203
} // namespace util
203204
} // namespace llvm
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
; RUN: sycl-post-link -split=auto -symbols -S %s -o %t.table
2+
; RUN: FileCheck %s -input-file=%t_0.prop
3+
4+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
5+
target triple = "spir64-unknown-linux-sycldevice"
6+
7+
@_ZL2GV = internal addrspace(1) constant [1 x i32] [i32 42], align 4
8+
@.str = private unnamed_addr addrspace(1) constant [2 x i8] c"0\00", align 1
9+
@.str.1 = private unnamed_addr addrspace(1) constant [11 x i8] c"assert.cpp\00", align 1
10+
@__PRETTY_FUNCTION__._Z3foov = private unnamed_addr addrspace(1) constant [11 x i8] c"void foo()\00", align 1
11+
@__spirv_BuiltInGlobalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32
12+
@__spirv_BuiltInLocalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32
13+
@_ZL10assert_fmt = internal addrspace(2) constant [85 x i8] c"%s:%d: %s: global id: [%lu,%lu,%lu], local id: [%lu,%lu,%lu] Assertion `%s` failed.\0A\00", align 1
14+
15+
; CHECK: [SYCL/assert used]
16+
; CHECK-DAG: _ZTSZ4mainE11TU0_kernel0
17+
; CHECK-DAG: _ZTSZ4mainE10TU1_kernel
18+
19+
define dso_local spir_kernel void @_ZTSZ4mainE11TU0_kernel0() #0 {
20+
entry:
21+
call spir_func void @_Z3foov()
22+
ret void
23+
}
24+
25+
define dso_local spir_func void @_Z3foov() {
26+
entry:
27+
%a = alloca i32, align 4
28+
%ptr = bitcast i32* %a to i32 (i32)*
29+
%call = call spir_func i32 %ptr(i32 1)
30+
%add = add nsw i32 2, %call
31+
store i32 %add, i32* %a, align 4
32+
tail call spir_func void @__assert_fail(i8 addrspace(4)* getelementptr inbounds ([2 x i8], [2 x i8] addrspace(4)* addrspacecast ([2 x i8] addrspace(1)* @.str to [2 x i8] addrspace(4)*), i64 0, i64 0), i8 addrspace(4)* getelementptr inbounds ([11 x i8], [11 x i8] addrspace(4)* addrspacecast ([11 x i8] addrspace(1)* @.str.1 to [11 x i8] addrspace(4)*), i64 0, i64 0), i32 8, i8 addrspace(4)* getelementptr inbounds ([11 x i8], [11 x i8] addrspace(4)* addrspacecast ([11 x i8] addrspace(1)* @__PRETTY_FUNCTION__._Z3foov to [11 x i8] addrspace(4)*), i64 0, i64 0))
33+
ret void
34+
}
35+
36+
define dso_local spir_kernel void @_ZTSZ4mainE11TU0_kernel1() #0 {
37+
entry:
38+
call spir_func void @_Z4foo1v()
39+
ret void
40+
}
41+
42+
; Function Attrs: nounwind
43+
define dso_local spir_func void @_Z4foo1v() {
44+
entry:
45+
%a = alloca i32, align 4
46+
store i32 2, i32* %a, align 4
47+
ret void
48+
}
49+
50+
define dso_local spir_kernel void @_ZTSZ4mainE10TU1_kernel() #1 {
51+
entry:
52+
call spir_func void @_Z4foo2v()
53+
ret void
54+
}
55+
56+
; Function Attrs: nounwind
57+
define dso_local spir_func void @_Z4foo2v() {
58+
entry:
59+
%a = alloca i32, align 4
60+
%0 = load i32, i32 addrspace(4)* getelementptr inbounds ([1 x i32], [1 x i32] addrspace(4)* addrspacecast ([1 x i32] addrspace(1)* @_ZL2GV to [1 x i32] addrspace(4)*), i64 0, i64 0), align 4
61+
%add = add nsw i32 4, %0
62+
store i32 %add, i32* %a, align 4
63+
tail call spir_func void @__assert_fail(i8 addrspace(4)* getelementptr inbounds ([2 x i8], [2 x i8] addrspace(4)* addrspacecast ([2 x i8] addrspace(1)* @.str to [2 x i8] addrspace(4)*), i64 0, i64 0), i8 addrspace(4)* getelementptr inbounds ([11 x i8], [11 x i8] addrspace(4)* addrspacecast ([11 x i8] addrspace(1)* @.str.1 to [11 x i8] addrspace(4)*), i64 0, i64 0), i32 8, i8 addrspace(4)* getelementptr inbounds ([11 x i8], [11 x i8] addrspace(4)* addrspacecast ([11 x i8] addrspace(1)* @__PRETTY_FUNCTION__._Z3foov to [11 x i8] addrspace(4)*), i64 0, i64 0))
64+
ret void
65+
}
66+
67+
; Function Attrs: convergent norecurse mustprogress
68+
define weak dso_local spir_func void @__assert_fail(i8 addrspace(4)* %expr, i8 addrspace(4)* %file, i32 %line, i8 addrspace(4)* %func) local_unnamed_addr {
69+
entry:
70+
%call = tail call spir_func i64 @_Z28__spirv_GlobalInvocationId_xv()
71+
%call1 = tail call spir_func i64 @_Z28__spirv_GlobalInvocationId_yv()
72+
%call2 = tail call spir_func i64 @_Z28__spirv_GlobalInvocationId_zv()
73+
%call3 = tail call spir_func i64 @_Z27__spirv_LocalInvocationId_xv()
74+
%call4 = tail call spir_func i64 @_Z27__spirv_LocalInvocationId_yv()
75+
%call5 = tail call spir_func i64 @_Z27__spirv_LocalInvocationId_zv()
76+
tail call spir_func void @__devicelib_assert_fail(i8 addrspace(4)* %expr, i8 addrspace(4)* %file, i32 %line, i8 addrspace(4)* %func, i64 %call, i64 %call1, i64 %call2, i64 %call3, i64 %call4, i64 %call5)
77+
ret void
78+
}
79+
80+
; Function Attrs: inlinehint norecurse mustprogress
81+
define weak dso_local spir_func i64 @_Z28__spirv_GlobalInvocationId_xv() local_unnamed_addr {
82+
entry:
83+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInGlobalInvocationId to <3 x i64> addrspace(4)*), align 32
84+
%1 = extractelement <3 x i64> %0, i64 0
85+
ret i64 %1
86+
}
87+
88+
; Function Attrs: inlinehint norecurse mustprogress
89+
define weak dso_local spir_func i64 @_Z28__spirv_GlobalInvocationId_yv() local_unnamed_addr {
90+
entry:
91+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInGlobalInvocationId to <3 x i64> addrspace(4)*), align 32
92+
%1 = extractelement <3 x i64> %0, i64 1
93+
ret i64 %1
94+
}
95+
96+
; Function Attrs: inlinehint norecurse mustprogress
97+
define weak dso_local spir_func i64 @_Z28__spirv_GlobalInvocationId_zv() local_unnamed_addr {
98+
entry:
99+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInGlobalInvocationId to <3 x i64> addrspace(4)*), align 32
100+
%1 = extractelement <3 x i64> %0, i64 2
101+
ret i64 %1
102+
}
103+
104+
; Function Attrs: inlinehint norecurse mustprogress
105+
define weak dso_local spir_func i64 @_Z27__spirv_LocalInvocationId_xv() local_unnamed_addr {
106+
entry:
107+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInLocalInvocationId to <3 x i64> addrspace(4)*), align 32
108+
%1 = extractelement <3 x i64> %0, i64 0
109+
ret i64 %1
110+
}
111+
112+
; Function Attrs: inlinehint norecurse mustprogress
113+
define weak dso_local spir_func i64 @_Z27__spirv_LocalInvocationId_yv() local_unnamed_addr {
114+
entry:
115+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInLocalInvocationId to <3 x i64> addrspace(4)*), align 32
116+
%1 = extractelement <3 x i64> %0, i64 1
117+
ret i64 %1
118+
}
119+
120+
; Function Attrs: inlinehint norecurse mustprogress
121+
define weak dso_local spir_func i64 @_Z27__spirv_LocalInvocationId_zv() local_unnamed_addr {
122+
entry:
123+
%0 = load <3 x i64>, <3 x i64> addrspace(4)* addrspacecast (<3 x i64> addrspace(1)* @__spirv_BuiltInLocalInvocationId to <3 x i64> addrspace(4)*), align 32
124+
%1 = extractelement <3 x i64> %0, i64 2
125+
ret i64 %1
126+
}
127+
128+
; Function Attrs: convergent norecurse mustprogress
129+
define weak dso_local spir_func void @__devicelib_assert_fail(i8 addrspace(4)* %expr, i8 addrspace(4)* %file, i32 %line, i8 addrspace(4)* %func, i64 %gid0, i64 %gid1, i64 %gid2, i64 %lid0, i64 %lid1, i64 %lid2) local_unnamed_addr {
130+
entry:
131+
%call = tail call spir_func i32 (i8 addrspace(2)*, ...) @_Z18__spirv_ocl_printfPU3AS2Kcz(i8 addrspace(2)* getelementptr inbounds ([85 x i8], [85 x i8] addrspace(2)* @_ZL10assert_fmt, i64 0, i64 0), i8 addrspace(4)* %file, i32 %line, i8 addrspace(4)* %func, i64 %gid0, i64 %gid1, i64 %gid2, i64 %lid0, i64 %lid1, i64 %lid2, i8 addrspace(4)* %expr)
132+
ret void
133+
}
134+
135+
; Function Attrs: convergent
136+
declare dso_local spir_func i32 @_Z18__spirv_ocl_printfPU3AS2Kcz(i8 addrspace(2)*, ...) local_unnamed_addr
137+
138+
attributes #0 = { "sycl-module-id"="TU1.cpp" }
139+
attributes #1 = { "sycl-module-id"="TU2.cpp" }
140+
141+
!opencl.spir.version = !{!0, !0}
142+
!spirv.Source = !{!1, !1}
143+
144+
!0 = !{i32 1, i32 2}
145+
!1 = !{i32 4, i32 100000}

llvm/tools/sycl-post-link/sycl-post-link.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ struct ImagePropSaveInfo {
169169
bool SpecConstsMet;
170170
bool EmitKernelParamInfo;
171171
bool IsEsimdKernel;
172+
bool IsAssertEnabled;
172173
};
173174

174175
static void error(const Twine &Msg) {
@@ -271,6 +272,28 @@ static void collectKernelModuleMap(
271272
}
272273
}
273274

275+
// Collect all the dependencies for the function.
276+
static bool collectFunctionCallGraphNodes(llvm::Function *Func) {
277+
std::vector<llvm::Function *> Workqueue;
278+
Workqueue.push_back(Func);
279+
280+
while (!Workqueue.empty()) {
281+
Function *F = &*Workqueue.back(); // To remove &*
282+
Workqueue.pop_back();
283+
for (auto &I : instructions(F)) {
284+
if (CallBase *CB = dyn_cast<CallBase>(&I))
285+
if (Function *CF = CB->getCalledFunction())
286+
if (!CF->isDeclaration()) {
287+
if (CF->getName().startswith("__devicelib_assert_fail")) {
288+
return true;
289+
}
290+
Workqueue.push_back(CF);
291+
}
292+
}
293+
}
294+
return false;
295+
}
296+
274297
// Input parameter KernelModuleMap is a map containing groups of kernels with
275298
// same values of the sycl-module-id attribute. ResSymbolsLists is a vector of
276299
// kernel name lists. Each vector element is a string with kernel names from the
@@ -463,6 +486,20 @@ static string_vector saveDeviceImageProperty(
463486
{"isEsimdImage", true});
464487
}
465488

489+
if (ImgPSInfo.IsAssertEnabled) {
490+
Module *M = ResultModules[I].get();
491+
std::vector<Function *> SyclKernels;
492+
for (auto &F : M->functions()) {
493+
if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
494+
if (collectFunctionCallGraphNodes(&F)) {
495+
SyclKernels.push_back(&F);
496+
PropSet[llvm::util::PropertySetRegistry::SYCL_ASSERT_USED].insert(
497+
{F.getName(), true});
498+
}
499+
}
500+
}
501+
}
502+
466503
std::error_code EC;
467504
std::string SCFile =
468505
makeResultFileName(".prop", I, ImgPSInfo.IsEsimdKernel ? "esimd_" : "");
@@ -609,7 +646,8 @@ static TableFiles processOneModule(std::unique_ptr<Module> M, bool IsEsimd,
609646
{
610647
ImagePropSaveInfo ImgPSInfo = {
611648
true, DoSpecConst, SetSpecConstAtRT,
612-
SpecConstsMet, EmitKernelParamInfo, IsEsimd};
649+
SpecConstsMet, EmitKernelParamInfo, IsEsimd,
650+
true};
613651
string_vector Files = saveDeviceImageProperty(ResultModules, ImgPSInfo);
614652
std::copy(Files.begin(), Files.end(),
615653
std::back_inserter(TblFiles[COL_PROPS]));

0 commit comments

Comments
 (0)