Skip to content

Commit 2017256

Browse files
authored
[DeviceSanitizer] Fix device global type of KernelMetadata (#16357)
OpenCL CPU requires the type of device global wraps with structure type
1 parent c673258 commit 2017256

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

llvm/lib/SYCLLowerIR/SanitizerKernelMetadata.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,29 @@ PreservedAnalyses SanitizerKernelMetadataPass::run(Module &M,
3838
auto &DL = M.getDataLayout();
3939
auto &Ctx = M.getContext();
4040

41+
// Fix device global type, by wrapping a structure type
42+
{
43+
assert(KernelMetadata->getValueType()->isArrayTy());
44+
45+
auto *KernelMetadataOld = KernelMetadata;
46+
47+
StructType *StructTypeWithArray = StructType::create(Ctx);
48+
StructTypeWithArray->setBody(KernelMetadataOld->getValueType());
49+
50+
KernelMetadata = new GlobalVariable(
51+
M, StructTypeWithArray, false, GlobalValue::ExternalLinkage,
52+
ConstantStruct::get(StructTypeWithArray,
53+
KernelMetadataOld->getInitializer()),
54+
"", nullptr, GlobalValue::NotThreadLocal, 1); // Global AddressSpace
55+
KernelMetadata->takeName(KernelMetadataOld);
56+
KernelMetadata->setUnnamedAddr(GlobalValue::UnnamedAddr::Local);
57+
KernelMetadata->setDSOLocal(true);
58+
KernelMetadata->copyAttributesFrom(KernelMetadataOld);
59+
KernelMetadata->copyMetadata(KernelMetadataOld, 0);
60+
61+
KernelMetadataOld->eraseFromParent();
62+
}
63+
4164
// Fix attributes
4265
KernelMetadata->addAttribute(
4366
"sycl-device-global-size",

llvm/test/tools/sycl-post-link/device-sanitizer/asan.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E8MyKernel = comdat any
1818

1919
@__asan_kernel = internal addrspace(1) constant [55 x i8] c"_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E8MyKernel\00"
2020
@__AsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global [1 x { i64, i64 }] [{ i64, i64 } { i64 ptrtoint (ptr addrspace(1) @__asan_kernel to i64), i64 54 }] #2
21-
; CHECK-IR: @__AsanKernelMetadata {{.*}} !spirv.Decorations
21+
; CHECK-IR: @__AsanKernelMetadata = dso_local local_unnamed_addr addrspace(1) global %0 { {{.*}} }, !spirv.Decorations
2222
@__spirv_BuiltInGlobalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32
2323
@__asan_func = internal addrspace(2) constant [106 x i8] c"typeinfo name for main::'lambda'(sycl::_V1::handler&)::operator()(sycl::_V1::handler&) const::MyKernelR_4\00"
2424

llvm/test/tools/sycl-post-link/device-sanitizer/msan.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ $_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E8MyKernel = comdat any
1818

1919
@__msan_kernel = internal addrspace(1) constant [55 x i8] c"_ZTSZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_E8MyKernel\00"
2020
@__MsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global [1 x { i64, i64 }] [{ i64, i64 } { i64 ptrtoint (ptr addrspace(1) @__msan_kernel to i64), i64 54 }] #0
21-
; CHECK-IR: @__MsanKernelMetadata {{.*}} !spirv.Decorations
21+
; CHECK-IR: @__MsanKernelMetadata = dso_local local_unnamed_addr addrspace(1) global %0 { {{.*}} }, !spirv.Decorations
2222
@__spirv_BuiltInGlobalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32
2323
@__asan_func = internal addrspace(2) constant [106 x i8] c"typeinfo name for main::'lambda'(sycl::_V1::handler&)::operator()(sycl::_V1::handler&) const::MyKernelR_4\00"
2424

0 commit comments

Comments
 (0)