|
| 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 |
0 commit comments