Skip to content

[SYCL][CUDA] Update code when llvm.used is removed #4409

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 5 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llvm/test/tools/sycl-post-link/erase_used.ll
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
; This test checks that the post-link tool does not add "llvm.used" global to
; the output modules when splitting kernels.
; the output modules when splitting modules, creating a single row table,
; and outputing IR only
;
; RUN: sycl-post-link -split=kernel -S %s -o %t.files.table
; RUN: FileCheck %s -input-file=%t.files_0.ll
; RUN: FileCheck %s -input-file=%t.files_1.ll
;
; RUN: sycl-post-link -S -split=auto -symbols -split-esimd -lower-esimd -O2 -spec-const=default %s -o %t.out.table
; RUN: FileCheck %s --input-file=%t.out_0.ll
;
; RUN: sycl-post-link -S -split=auto -ir-output-only %s -o %t.out_ir_only.ll
; RUN: FileCheck %s --input-file %t.out_ir_only.ll

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

Expand Down
29 changes: 16 additions & 13 deletions llvm/tools/sycl-post-link/sycl-post-link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,20 @@ static TableFiles processOneModule(std::unique_ptr<Module> M, bool IsEsimd,
if (!M)
return TblFiles;

// After linking device bitcode "llvm.used" holds references to the kernels
// that are defined in the device image. But after splitting device image into
// separate kernels we may end up with having references to kernel declaration
// originating from "llvm.used" in the IR that is passed to llvm-spirv tool,
// and these declarations cause an assertion in llvm-spirv. To workaround this
// issue remove "llvm.used" from the input module before performing any other
// actions.
bool IsLLVMUsedRemoved = false;
if (GlobalVariable *GV = M->getGlobalVariable("llvm.used")) {
assert(GV->user_empty() && "unexpected llvm.used users");
GV->eraseFromParent();
IsLLVMUsedRemoved = true;
}

if (IsEsimd && LowerEsimd)
LowerEsimdConstructs(*M);

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

// After linking device bitcode "llvm.used" holds references to the kernels
// that are defined in the device image. But after splitting device image into
// separate kernels we may end up with having references to kernel declaration
// originating from "llvm.used" in the IR that is passed to llvm-spirv tool,
// and these declarations cause an assertion in llvm-spirv. To workaround this
// issue remove "llvm.used" from the input module before performing any other
// actions.
if (GlobalVariable *GV = MPtr->getGlobalVariable("llvm.used")) {
assert(GV->user_empty() && "unexpected llvm.used users");
GV->eraseFromParent();
}

if (OutputFilename.getNumOccurrences() == 0)
OutputFilename = (Twine(sys::path::stem(InputFilename)) + ".files").str();

Expand Down