Skip to content

Commit a6076d0

Browse files
authored
[SYCL][CUDA] Update code when llvm.used is removed (#4409)
When using `sycl-post-link` llvm.used should be removed. Otherwise it can cause a crash further on. This patch checks to see if llvm.used is removed and if it is prevents the old code from being reused in the table. This fixes #4294. See issue for more details on problem.
1 parent 6c7b83b commit a6076d0

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
; This test checks that the post-link tool does not add "llvm.used" global to
2-
; the output modules when splitting kernels.
2+
; the output modules when splitting modules, creating a single row table,
3+
; and outputing IR only
34
;
45
; RUN: sycl-post-link -split=kernel -S %s -o %t.files.table
56
; RUN: FileCheck %s -input-file=%t.files_0.ll
67
; 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
714

815
target triple = "spir64-unknown-unknown-sycldevice"
916

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,20 @@ static TableFiles processOneModule(std::unique_ptr<Module> M, bool IsEsimd,
714714
if (!M)
715715
return TblFiles;
716716

717+
// After linking device bitcode "llvm.used" holds references to the kernels
718+
// that are defined in the device image. But after splitting device image into
719+
// separate kernels we may end up with having references to kernel declaration
720+
// originating from "llvm.used" in the IR that is passed to llvm-spirv tool,
721+
// and these declarations cause an assertion in llvm-spirv. To workaround this
722+
// issue remove "llvm.used" from the input module before performing any other
723+
// actions.
724+
bool IsLLVMUsedRemoved = false;
725+
if (GlobalVariable *GV = M->getGlobalVariable("llvm.used")) {
726+
assert(GV->user_empty() && "unexpected llvm.used users");
727+
GV->eraseFromParent();
728+
IsLLVMUsedRemoved = true;
729+
}
730+
717731
if (IsEsimd && LowerEsimd)
718732
LowerEsimdConstructs(*M);
719733

@@ -773,7 +787,8 @@ static TableFiles processOneModule(std::unique_ptr<Module> M, bool IsEsimd,
773787
// no spec constants and no splitting.
774788
// We cannot reuse input module for ESIMD code since it was transformed.
775789
bool CanReuseInputModule = !SpecConstsMet && (ResultModules.size() == 1) &&
776-
!SyclAndEsimdCode && !IsEsimd;
790+
!SyclAndEsimdCode && !IsEsimd &&
791+
!IsLLVMUsedRemoved;
777792
string_vector Files =
778793
CanReuseInputModule
779794
? string_vector{InputFilename}
@@ -981,18 +996,6 @@ int main(int argc, char **argv) {
981996
return 1;
982997
}
983998

984-
// After linking device bitcode "llvm.used" holds references to the kernels
985-
// that are defined in the device image. But after splitting device image into
986-
// separate kernels we may end up with having references to kernel declaration
987-
// originating from "llvm.used" in the IR that is passed to llvm-spirv tool,
988-
// and these declarations cause an assertion in llvm-spirv. To workaround this
989-
// issue remove "llvm.used" from the input module before performing any other
990-
// actions.
991-
if (GlobalVariable *GV = MPtr->getGlobalVariable("llvm.used")) {
992-
assert(GV->user_empty() && "unexpected llvm.used users");
993-
GV->eraseFromParent();
994-
}
995-
996999
if (OutputFilename.getNumOccurrences() == 0)
9971000
OutputFilename = (Twine(sys::path::stem(InputFilename)) + ".files").str();
9981001

0 commit comments

Comments
 (0)