Skip to content

Commit 9b4ed34

Browse files
[SYCL] Keep used function declarations during llvm.used removal (#6308)
It is possible for a function declaration to be used by both @llvm.used and, e.g., a spir_func definition. Ensure that we don't attempt to erase such declarations when removing @llvm.used. Add a regression test. Most builds will hit an assertion in the first RUN line when the declaration is incorrectly erased.
1 parent 0c7a1e1 commit 9b4ed34

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
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 i8*] [i8* bitcast (void ()* @notused to i8*), i8* bitcast (void ()* @stillused to i8*)], 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" }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ static bool removeSYCLKernelsConstRefArray(Module &M) {
651651
}
652652

653653
// Remove unused kernel declarations to avoid LLVM IR check fails.
654-
if (F && F->isDeclaration())
654+
if (F && F->isDeclaration() && F->use_empty())
655655
F->eraseFromParent();
656656
}
657657
return true;

0 commit comments

Comments
 (0)