Skip to content

Commit 18eb60d

Browse files
committed
wip
1 parent 20f08fe commit 18eb60d

File tree

6 files changed

+94
-12
lines changed

6 files changed

+94
-12
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===-- AsanKernelMetadata.h - instrument device global for sanitizer ---===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// This pass adds red zone to each image scope device global and record the
9+
// information like size, red zone size and beginning address. The information
10+
// will be used by address sanitizer.
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "llvm/IR/PassManager.h"
14+
15+
namespace llvm {
16+
17+
class AsanKernelMetadataPass : public PassInfoMixin<AsanKernelMetadataPass> {
18+
public:
19+
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
20+
};
21+
22+
} // namespace llvm
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- SanitizeDeviceGlobal.cpp - instrument device global for sanitizer -===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
// This pass adds red zone to each image scope device global and record the
9+
// information like size, red zone size and beginning address. The information
10+
// will be used by address sanitizer.
11+
// TODO: Do this in AddressSanitizer pass when urProgramGetGlobalVariablePointer
12+
// is implemented.
13+
//===----------------------------------------------------------------------===//
14+
15+
#include "llvm/SYCLLowerIR/AsanKernelMetadata.h"
16+
17+
#include "llvm/IR/IRBuilder.h"
18+
19+
#define DEBUG_TYPE "AsanKernelMetadata"
20+
21+
using namespace llvm;
22+
23+
namespace llvm {
24+
25+
constexpr StringRef SPIRV_DECOR_MD_KIND = "spirv.Decorations";
26+
constexpr uint32_t SPIRV_HOST_ACCESS_DECOR = 6147;
27+
28+
PreservedAnalyses AsanKernelMetadataPass::run(Module &M,
29+
ModuleAnalysisManager &MAM) {
30+
auto *KernelMetadata = M.getNamedGlobal("__AsanKernelMetadata");
31+
if (!KernelMetadata) {
32+
return PreservedAnalyses::all();
33+
}
34+
35+
auto &DL = M.getDataLayout();
36+
auto &Ctx = M.getContext();
37+
38+
// Fix attributes
39+
KernelMetadata->addAttribute(
40+
"sycl-device-global-size",
41+
std::to_string(DL.getTypeAllocSize(KernelMetadata->getType())));
42+
43+
// Fix metadata
44+
unsigned MDKindID = Ctx.getMDKindID(SPIRV_DECOR_MD_KIND);
45+
46+
SmallVector<Metadata *, 1> MDOps;
47+
48+
SmallVector<Metadata *, 3> MD;
49+
auto *Ty = Type::getInt32Ty(Ctx);
50+
MD.push_back(ConstantAsMetadata::get(
51+
Constant::getIntegerValue(Ty, APInt(32, SPIRV_HOST_ACCESS_DECOR))));
52+
MD.push_back(
53+
ConstantAsMetadata::get(Constant::getIntegerValue(Ty, APInt(32, 2))));
54+
MD.push_back(MDString::get(Ctx, "_Z20__AsanKernelMetadata"));
55+
56+
MDOps.push_back(MDNode::get(Ctx, MD));
57+
58+
KernelMetadata->addMetadata(MDKindID, *MDNode::get(Ctx, MDOps));
59+
60+
return PreservedAnalyses::none();
61+
}
62+
63+
} // namespace llvm

llvm/lib/SYCLLowerIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ add_llvm_component_library(LLVMSYCLLowerIR
7070
SYCLVirtualFunctionsAnalysis.cpp
7171
SYCLUtils.cpp
7272
SanitizeDeviceGlobal.cpp
73+
AsanKernelMetadata.cpp
7374

7475
LocalAccessorToSharedMemory.cpp
7576
GlobalOffset.cpp

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,17 +1328,6 @@ static GlobalVariable *GetOrCreateGlobalString(Module &M, StringRef Name,
13281328
return StringGV;
13291329
}
13301330

1331-
static std::string SYCLUniqueStableId(const GlobalValue *GV,
1332-
const DataLayout &DL) {
1333-
std::string Buffer;
1334-
llvm::raw_string_ostream Out(Buffer);
1335-
1336-
Mangler Mgl;
1337-
Mgl.getNameWithPrefix(Out, GV, true);
1338-
1339-
return Out.str();
1340-
}
1341-
13421331
// Append a new argument "launch_data" to user's spir_kernels
13431332
static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM) {
13441333
SmallVector<Function *> SpirFixupKernels;
@@ -1399,6 +1388,7 @@ static void ExtendSpirKernelArgs(Module &M, FunctionAnalysisManager &FAM) {
13991388
AsanSpirKernelMetadata->addAttribute("sycl-host-access", "2");
14001389
AsanSpirKernelMetadata->addAttribute("sycl-unique-id",
14011390
"_Z20__AsanKernelMetadata");
1391+
AsanSpirKernelMetadata->setDSOLocal(true);
14021392

14031393
// Handle SpirFixupKernels
14041394
SmallVector<std::pair<Function *, Function *>> SpirFuncs;

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "llvm/IRReader/IRReader.h"
3030
#include "llvm/Linker/Linker.h"
3131
#include "llvm/Passes/PassBuilder.h"
32+
#include "llvm/SYCLLowerIR/AsanKernelMetadata.h"
3233
#include "llvm/SYCLLowerIR/CompileTimePropertiesPass.h"
3334
#include "llvm/SYCLLowerIR/ComputeModuleRuntimeInfo.h"
3435
#include "llvm/SYCLLowerIR/DeviceConfigFile.hpp"
@@ -793,8 +794,10 @@ processInputModule(std::unique_ptr<Module> M) {
793794

794795
// Instrument each image scope device globals if the module has been
795796
// instrumented by sanitizer pass.
796-
if (isModuleUsingAsan(*M))
797+
if (isModuleUsingAsan(*M)) {
797798
Modified |= runModulePass<SanitizeDeviceGlobalPass>(*M);
799+
Modified |= runModulePass<AsanKernelMetadataPass>(*M);
800+
}
798801

799802
// Transform Joint Matrix builtin calls to align them with SPIR-V friendly
800803
// LLVM IR specification.

sycl/test-e2e/AddressSanitizer/common/kernel-filter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// REQUIRES: linux
22
// RUN: %{build} %device_asan_flags -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t
33
// RUN: %{run} not %t &> %t.txt ; FileCheck --input-file %t.txt %s
4+
// RUN: %{build} %device_asan_flags %if cpu %{ -fsycl-targets=spir64_x86_64 %} %if gpu %{ -fsycl-targets=spir64_gen -Xsycl-target-backend=spir64_gen %gpu_aot_target_opts %} -O2 -fsanitize-ignorelist=%p/ignorelist.txt -o %t2
5+
// RUN: %{run} not %t2 &> %t2.txt ; FileCheck --input-file %t2.txt %s
6+
47
#include <sycl/detail/core.hpp>
58
#include <sycl/usm.hpp>
69

0 commit comments

Comments
 (0)