Skip to content

Commit d163d2c

Browse files
[ESIMD] Skip rewriting functions used through function pointers (#3527)
This fixes a crash when running one of the ESIMD tests that use function pointers with `-O0`. The problem here is that the `ESIMDLowerVecArg` pass expects users of the functions to be only call instructions, but it's not the case with function pointers since the address of a function can be stored in some variable.
1 parent 5f115a4 commit d163d2c

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

llvm/lib/SYCLLowerIR/LowerESIMDVecArg.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
// % 1 = bitcast<16 x i32> * % 0 to %
3232
// "class._ZTSN2cm3gen4simdIiLi16EEE.cm::gen::simd" *
3333
//
34+
// It is OK not to rewrite a function (for example, when its address is taken)
35+
// since it does not affect correctness. But that may lead to vector backend
36+
// not being able to hold the value in GRF and generate memory references.
3437
//
3538
// Change in global variables:
3639
//
@@ -255,7 +258,9 @@ PreservedAnalyses ESIMDLowerVecArgPass::run(Module &M,
255258

256259
SmallVector<Function *, 10> functions;
257260
for (auto &F : M) {
258-
functions.push_back(&F);
261+
// Skip functions that are used through function pointers.
262+
if (!F.hasAddressTaken())
263+
functions.push_back(&F);
259264
}
260265

261266
for (auto F : functions) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -ESIMDLowerVecArg -S | FileCheck %s
3+
4+
; This test checks that there is no crash in ESIMDLowerVecArg pass when
5+
; rewriting funcitons that are used through a function pointer.
6+
7+
%"cl::sycl::INTEL::gpu::simd" = type { <64 x i32> }
8+
9+
define dso_local spir_func void @func(%"cl::sycl::INTEL::gpu::simd"* %arg) {
10+
; CHECK-LABEL: @func(
11+
; CHECK-NEXT: entry:
12+
; CHECK-NEXT: ret void
13+
;
14+
entry:
15+
ret void
16+
}
17+
18+
define dso_local spir_func void @init_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)** %foo) !sycl_explicit_simd !1 {
19+
; CHECK-LABEL: @init_ptr(
20+
; CHECK-NEXT: entry:
21+
; CHECK-NEXT: store void (%"cl::sycl::INTEL::gpu::simd"*)* @func, void (%"cl::sycl::INTEL::gpu::simd"*)** [[FOO:%.*]], align 8
22+
; CHECK-NEXT: ret void
23+
;
24+
entry:
25+
store void (%"cl::sycl::INTEL::gpu::simd"*)* @func, void (%"cl::sycl::INTEL::gpu::simd"*)** %foo
26+
ret void
27+
}
28+
29+
define dso_local spir_func void @use_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)* %foo) !sycl_explicit_simd !1 {
30+
; CHECK-LABEL: @use_ptr(
31+
; CHECK-NEXT: entry:
32+
; CHECK-NEXT: [[AGG_TMP:%.*]] = alloca %"cl::sycl::INTEL::gpu::simd", align 256
33+
; CHECK-NEXT: call spir_func void [[FOO:%.*]](%"cl::sycl::INTEL::gpu::simd"* [[AGG_TMP]])
34+
; CHECK-NEXT: ret void
35+
;
36+
entry:
37+
%agg.tmp = alloca %"cl::sycl::INTEL::gpu::simd"
38+
call spir_func void %foo(%"cl::sycl::INTEL::gpu::simd"* %agg.tmp)
39+
ret void
40+
}
41+
42+
define dso_local spir_func void @esimd_kernel() !sycl_explicit_simd !1 {
43+
; CHECK-LABEL: @esimd_kernel(
44+
; CHECK-NEXT: entry:
45+
; CHECK-NEXT: [[FP:%.*]] = alloca void (%"cl::sycl::INTEL::gpu::simd"*)*, align 8
46+
; CHECK-NEXT: call spir_func void @init_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)** [[FP]])
47+
; CHECK-NEXT: [[TMP0:%.*]] = load void (%"cl::sycl::INTEL::gpu::simd"*)*, void (%"cl::sycl::INTEL::gpu::simd"*)** [[FP]], align 8
48+
; CHECK-NEXT: call spir_func void @use_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)* [[TMP0]])
49+
; CHECK-NEXT: ret void
50+
;
51+
entry:
52+
%fp = alloca void (%"cl::sycl::INTEL::gpu::simd"*)*
53+
call spir_func void @init_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)** %fp)
54+
%0 = load void (%"cl::sycl::INTEL::gpu::simd"*)*, void (%"cl::sycl::INTEL::gpu::simd"*)** %fp
55+
call spir_func void @use_ptr(void (%"cl::sycl::INTEL::gpu::simd"*)* %0)
56+
ret void
57+
}
58+
59+
!1 = !{}

0 commit comments

Comments
 (0)