Skip to content

[DevSAN] Make device sanitizer kernel metadata has a unique id #18819

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 9, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H
#define LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H

#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Type.h"
Expand All @@ -36,6 +37,11 @@ bool isJointMatrixAccess(Value *V);
// it has been used.
void getFunctionsOfUser(User *User, SmallVectorImpl<Function *> &Functions);

// Compute MD5 hash for kernel metadata global as unique id.
SmallString<128>
computeKernelMetadataUniqueId(StringRef Prefix,
SmallVectorImpl<uint8_t> &KernelNamesBytes);

} // namespace llvm

#endif // LLVM_TRANSFORMS_INSTRUMENTATION_SPIRVSANITIZERCOMMONUTILS_H
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,7 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
bool HasESIMD) {
SmallVector<Function *> SpirFixupKernels;
SmallVector<Constant *, 8> SpirKernelsMetadata;
SmallVector<uint8_t, 256> KernelNamesBytes;

const auto &DL = M.getDataLayout();
Type *IntptrTy = DL.getIntPtrType(M.getContext());
Expand Down Expand Up @@ -1438,6 +1439,7 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
SpirFixupKernels.emplace_back(&F);

auto KernelName = F.getName();
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
auto *KernelNameGV = GetOrCreateGlobalString(
M, "__asan_kernel", KernelName, kSpirOffloadConstantAS);
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
Expand All @@ -1459,8 +1461,9 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM,
"sycl-device-global-size", std::to_string(DL.getTypeAllocSize(ArrayTy)));
AsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
AsanSpirKernelMetadata->addAttribute("sycl-host-access", "0"); // read only
AsanSpirKernelMetadata->addAttribute("sycl-unique-id",
"_Z20__AsanKernelMetadata");
AsanSpirKernelMetadata->addAttribute(
"sycl-unique-id",
computeKernelMetadataUniqueId("__AsanKernelMetadata", KernelNamesBytes));
AsanSpirKernelMetadata->setDSOLocal(true);

// Handle SpirFixupKernels
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,7 @@ void MemorySanitizerOnSpirv::instrumentPrivateArguments(
// kernel
void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
SmallVector<Constant *, 8> SpirKernelsMetadata;
SmallVector<uint8_t, 256> KernelNamesBytes;

// SpirKernelsMetadata only saves fixed kernels, and is described by
// following structure:
Expand All @@ -1189,6 +1190,7 @@ void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
continue;

auto KernelName = F.getName();
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
auto *KernelNameGV = getOrCreateGlobalString("__msan_kernel", KernelName,
kSpirOffloadConstantAS);
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
Expand All @@ -1213,8 +1215,9 @@ void MemorySanitizerOnSpirv::instrumentKernelsMetadata() {
MsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
MsanSpirKernelMetadata->addAttribute("sycl-host-access",
"0"); // read only
MsanSpirKernelMetadata->addAttribute("sycl-unique-id",
"_Z20__MsanKernelMetadata");
MsanSpirKernelMetadata->addAttribute(
"sycl-unique-id",
computeKernelMetadataUniqueId("__MsanKernelMetadata", KernelNamesBytes));
MsanSpirKernelMetadata->setDSOLocal(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "llvm/Transforms/Instrumentation/SPIRVSanitizerCommonUtils.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/MD5.h"

using namespace llvm;

Expand Down Expand Up @@ -68,4 +69,16 @@ void getFunctionsOfUser(User *User, SmallVectorImpl<Function *> &Functions) {
}
}

SmallString<128>
computeKernelMetadataUniqueId(StringRef Prefix,
SmallVectorImpl<uint8_t> &KernelNamesBytes) {
MD5 Hash;
SmallString<32> UniqueIdSuffix;
SmallString<128> UniqueId(Prefix);
auto R = Hash.hash(KernelNamesBytes);
Hash.stringifyResult(R, UniqueIdSuffix);
UniqueId.append(UniqueIdSuffix);
return UniqueId;
}

} // namespace llvm
8 changes: 6 additions & 2 deletions llvm/lib/Transforms/Instrumentation/ThreadSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/IR/Type.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/CommandLine.h"
Expand Down Expand Up @@ -674,6 +675,7 @@ void ThreadSanitizerOnSpirv::initializeKernelCallerMap(Function *F) {

void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {
SmallVector<Constant *, 8> SpirKernelsMetadata;
SmallVector<uint8_t, 256> KernelNamesBytes;

// SpirKernelsMetadata only saves fixed kernels, and is described by
// following structure:
Expand All @@ -687,6 +689,7 @@ void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {

if (isSupportedSPIRKernel(F)) {
auto KernelName = F.getName();
KernelNamesBytes.append(KernelName.begin(), KernelName.end());
auto *KernelNameGV = GetOrCreateGlobalString("__tsan_kernel", KernelName,
kSpirOffloadConstantAS);
SpirKernelsMetadata.emplace_back(ConstantStruct::get(
Expand All @@ -709,8 +712,9 @@ void ThreadSanitizerOnSpirv::instrumentKernelsMetadata() {
"sycl-device-global-size", std::to_string(DL.getTypeAllocSize(ArrayTy)));
TsanSpirKernelMetadata->addAttribute("sycl-device-image-scope");
TsanSpirKernelMetadata->addAttribute("sycl-host-access", "0"); // read only
TsanSpirKernelMetadata->addAttribute("sycl-unique-id",
"_Z20__TsanKernelMetadata");
TsanSpirKernelMetadata->addAttribute(
"sycl-unique-id",
computeKernelMetadataUniqueId("__TsanKernelMetadata", KernelNamesBytes));
TsanSpirKernelMetadata->setDSOLocal(true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:
target triple = "spir64-unknown-unknown"

; CHECK: @__AsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global
; CHECK-SAME: [[ATTR0:#[0-9]+]]
; CHECK: @__AsanLaunchInfo = external addrspace(3) global ptr addrspace(1)

define spir_kernel void @sycl_kernel1() #0 {
Expand All @@ -25,4 +26,4 @@ entry:
attributes #0 = { sanitize_address }
;; sycl-device-global-size = 16 * 2
;; sycl-host-access = 0 read-only
; CHECK: attributes #{{.*}} = { "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="_Z20__AsanKernelMetadata" }
; CHECK: attributes [[ATTR0]] = { "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="__AsanKernelMetadata833c47834a0b74946e370c23c39607cc" }
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ entry:
}

; CHECK: attributes [[ATTR0]]
; CHECK-SAME: "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="_Z20__MsanKernelMetadata"
; CHECK-SAME: "sycl-device-global-size"="32" "sycl-device-image-scope" "sycl-host-access"="0" "sycl-unique-id"="__MsanKernelMetadata3ff767e9a7a43f1f3968062dbb4ee3b4"
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ target triple = "spir64-unknown-unknown"

; CHECK-LABEL: @__TsanKernelMetadata = appending dso_local local_unnamed_addr addrspace(1) global
; CHECK-SAME: i64 ptrtoint (ptr addrspace(2) @__tsan_kernel to i64
; CHECK-SAME: [[ATTR0:#[0-9]+]]

; Function Attrs: sanitize_thread
define spir_kernel void @test() #0 {
entry:
ret void
}

; CHECK: attributes [[ATTR0]] = {{.*}} "sycl-unique-id"="__TsanKernelMetadata098f6bcd4621d373cade4e832627b4f6"
attributes #0 = { sanitize_thread }