Skip to content

Commit 079494e

Browse files
[SYCL] Fix llvm.used removal when used with opaque pointers. (#6773)
The code priorily assumed that all functions when used in @llvm.used would be wrapped within a bitcast <fnptr type> to i8*; with opaque pointers, the values would be functions directly, causing a crash since functions don't have any operands.
1 parent 4b6bd14 commit 079494e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; This test checks that the post-link tool doesn't incorrectly remove function
2+
; declarations which are still in use while erasing the "llvm.used" global.
3+
;
4+
; RUN: sycl-post-link -split=auto -symbols -S %s -o %t.files.table
5+
; RUN: FileCheck %s -input-file=%t.files_0.ll
6+
;
7+
target triple = "spir64-unknown-unknown"
8+
9+
; CHECK-NOT: llvm.used
10+
@llvm.used = appending global [2 x ptr] [ptr @notused, ptr @stillused], section "llvm.metadata"
11+
12+
; CHECK: declare spir_func void @stillused
13+
declare spir_func void @stillused() #0
14+
declare spir_func void @notused() #0
15+
16+
define spir_kernel void @entry() #0 {
17+
call spir_func void @stillused()
18+
ret void
19+
}
20+
21+
define spir_kernel void @bar() #0 {
22+
ret void
23+
}
24+
25+
attributes #0 = { "sycl-module-id"="erase_used_decl.cpp" }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; This test checks that the post-link tool does not add "llvm.used" global to
2+
; the output modules when splitting modules, creating a single row table,
3+
; and outputing IR only
4+
;
5+
; RUN: sycl-post-link -split=kernel -S %s -o %t.files.table
6+
; RUN: FileCheck %s -input-file=%t.files_0.ll
7+
; RUN: FileCheck %s -input-file=%t.files_1.ll
8+
;
9+
; RUN: sycl-post-link -S -split=auto -symbols -split-esimd -lower-esimd -O2 -spec-const=default %s -o %t.out.table
10+
; RUN: FileCheck %s --input-file=%t.out_0.ll
11+
;
12+
; RUN: sycl-post-link -S -split=auto -ir-output-only %s -o %t.out_ir_only.ll
13+
; RUN: FileCheck %s --input-file %t.out_ir_only.ll
14+
15+
target triple = "spir64-unknown-unknown"
16+
17+
; CHECK-NOT: llvm.used
18+
@llvm.used = appending global [2 x ptr] [ptr @foo, ptr @bar], section "llvm.metadata"
19+
20+
define weak_odr spir_kernel void @foo() #0 {
21+
ret void
22+
}
23+
24+
define weak_odr spir_kernel void @bar() #0 {
25+
ret void
26+
}
27+
28+
attributes #0 = { "sycl-module-id"="a.cpp" }

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,11 +627,12 @@ static bool removeSYCLKernelsConstRefArray(Module &M) {
627627
"Cannot remove initializer of llvm.used global");
628628
Initializer->destroyConstant();
629629
for (auto It = IOperands.begin(); It != IOperands.end(); It++) {
630-
auto Op = (*It)->getOperand(0);
630+
auto Op = (*It)->stripPointerCasts();
631631
auto *F = dyn_cast<Function>(Op);
632632
if (llvm::isSafeToDestroyConstant(*It)) {
633633
(*It)->destroyConstant();
634-
} else if (F) {
634+
} else if (F && F->getCallingConv() == CallingConv::SPIR_KERNEL &&
635+
!F->use_empty()) {
635636
// The element in "llvm.used" array has other users. That is Ok for
636637
// specialization constants, but is wrong for kernels.
637638
llvm::report_fatal_error("Unexpected usage of SYCL kernel");

0 commit comments

Comments
 (0)