[llvm-branch-commits] [llvm] [LLVM] Add DeviceAddressSanitizer instrumentation pass (PR #242443)

Joseph Huber via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Nov 14 11:34:02 PST 2026


https://github.com/jhuber6 updated https://github.com/llvm/llvm-project/pull/242443

>From 37bc7508717c01fd2f745495824dd594f4d35774 Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Sun, 23 Aug 2026 20:15:04 -0500
Subject: [PATCH 1/2] [LLVM] Add DeviceAddressSanitizer instrumentation pass

Instrument loads, stores, and memory intrinsics against dasan's
size-class metadata. The handler locates the access by its return
address, so each check calls a report of its own that is never merged,
inlined, or tail called. On AMDGPU, keep the dispatch pointer that
shared memory checks read, and preserve the __dasan_ entry points.

AMDGPU keeps one decode per inbounds root and selects the zero word for
an outside pointer, so the load can be hoisted and the wave does not
branch. Elsewhere an outside pointer is the common case, so membership
is a branch and the decode is its cold side. Global relocation defaults
to AMDGPU and follows -dasan-instrument-globals when set. The metadata
pointer uses the data layout's default globals address space.

RFC: https://discourse.llvm.org/t/rfc-add-a-device-address-sanitizer-for-gpus-dasan/92710
---
 .../Instrumentation/DeviceAddressSanitizer.h  |  35 +
 llvm/lib/Passes/PassBuilder.cpp               |   1 +
 llvm/lib/Passes/PassRegistry.def              |   1 +
 .../lib/Target/AMDGPU/AMDGPUTargetMachine.cpp |   1 +
 .../Transforms/Instrumentation/CMakeLists.txt |   1 +
 .../DeviceAddressSanitizer.cpp                | 879 ++++++++++++++++++
 .../CodeGen/AMDGPU/amdgpu-attributor-dasan.ll |  34 +
 .../DeviceAddressSanitizer/basic.ll           | 403 ++++++++
 .../DeviceAddressSanitizer/debug.ll           |  90 ++
 .../do-not-internalize.ll                     |  12 +
 .../DeviceAddressSanitizer/globals.ll         | 123 +++
 .../DeviceAddressSanitizer/layout.ll          |  30 +
 .../DeviceAddressSanitizer/marker.ll          |  17 +
 .../DeviceAddressSanitizer/module.ll          |  17 +
 .../DeviceAddressSanitizer/rooting.ll         |  68 ++
 .../DeviceAddressSanitizer/shared.ll          | 282 ++++++
 .../DeviceAddressSanitizer/wave.ll            |  19 +
 .../lib/Transforms/Instrumentation/BUILD.gn   |   1 +
 18 files changed, 2014 insertions(+)
 create mode 100644 llvm/include/llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h
 create mode 100644 llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
 create mode 100644 llvm/test/CodeGen/AMDGPU/amdgpu-attributor-dasan.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/basic.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/debug.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/globals.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/layout.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/marker.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/module.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/rooting.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/shared.ll
 create mode 100644 llvm/test/Instrumentation/DeviceAddressSanitizer/wave.ll

diff --git a/llvm/include/llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h b/llvm/include/llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h
new file mode 100644
index 00000000000000..f19593f512d1f7
--- /dev/null
+++ b/llvm/include/llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h
@@ -0,0 +1,35 @@
+//===- DeviceAddressSanitizer.h - device address sanitizer ------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TRANSFORMS_INSTRUMENTATION_DEVICEADDRESSSANITIZER_H
+#define LLVM_TRANSFORMS_INSTRUMENTATION_DEVICEADDRESSSANITIZER_H
+
+#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Compiler.h"
+
+namespace llvm {
+
+struct DeviceAddressSanitizerOptions {
+  bool Recover = false;
+};
+
+struct DeviceAddressSanitizerPass
+    : public RequiredPassInfoMixin<DeviceAddressSanitizerPass> {
+  DeviceAddressSanitizerPass() = default;
+  explicit DeviceAddressSanitizerPass(DeviceAddressSanitizerOptions Options)
+      : Options(Options) {}
+
+  LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
+
+private:
+  DeviceAddressSanitizerOptions Options;
+};
+
+} // namespace llvm
+
+#endif // LLVM_TRANSFORMS_INSTRUMENTATION_DEVICEADDRESSSANITIZER_H
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 263c7738721c46..0ee2cd1ad03a97 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -281,6 +281,7 @@
 #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
 #include "llvm/Transforms/Instrumentation/CopyProf.h"
 #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
+#include "llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h"
 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def
index 3e16a2f6a9d238..87a456ef4b307b 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -75,6 +75,7 @@ MODULE_PASS("ctx-prof-flatten",
 MODULE_PASS("ctx-prof-flatten-prethinlink",
             PGOCtxProfFlatteningPass(/*IsPreThinlink=*/true))
 MODULE_PASS("noinline-nonprevailing", NoinlineNonPrevailing())
+MODULE_PASS("dasan", DeviceAddressSanitizerPass())
 MODULE_PASS("deadargelim", DeadArgumentEliminationPass())
 MODULE_PASS("debugify", NewPMDebugifyPass())
 MODULE_PASS("declare-runtime-libcalls", DeclareRuntimeLibcallsPass())
diff --git a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
index 2a9d3435998667..793026172d7e76 100644
--- a/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
+++ b/llvm/lib/Target/AMDGPU/AMDGPUTargetMachine.cpp
@@ -770,6 +770,7 @@ AMDGPUTargetMachine::createMachineScheduler(MachineSchedContext *C) const {
 static bool mustPreserveGV(const GlobalValue &GV) {
   if (const Function *F = dyn_cast<Function>(&GV))
     return F->isDeclaration() || F->getName().starts_with("__asan_") ||
+           F->getName().starts_with("__dasan_") ||
            F->getName().starts_with("__sanitizer_") ||
            AMDGPU::isEntryFunctionCC(F->getCallingConv());
 
diff --git a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
index c262f0625206c4..4d117c31a9b7b0 100644
--- a/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
+++ b/llvm/lib/Transforms/Instrumentation/CMakeLists.txt
@@ -11,6 +11,7 @@ add_llvm_component_library(LLVMInstrumentation
   ControlHeightReduction.cpp
   CopyProf.cpp
   DataFlowSanitizer.cpp
+  DeviceAddressSanitizer.cpp
   GCOVProfiling.cpp
   BlockCoverageInference.cpp
   MemProfInstrumentation.cpp
diff --git a/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
new file mode 100644
index 00000000000000..b3035177fb4ce7
--- /dev/null
+++ b/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
@@ -0,0 +1,879 @@
+//===- DeviceAddressSanitizer.cpp - device address sanitizer --------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Bounds checking for device code. Allocations live in [2^45, 2^46). That
+// window is 32 regions of 2^40 bytes, one per size class, in virtual memory:
+//
+//   bits [45]     = 1                      in the window
+//   bits [44:40]  = C                      size class
+//   bits [39:0]   = offset in region C
+//
+// Class C has chunks of 2^(6 + C) bytes. Chunk index is offset >> (6 + C).
+// Metadata is an array of i64 at the high end of the region, one word per
+// reachable chunk, indexed backwards from the last byte:
+//
+//   [ chunk 0 | chunk 1 | ... | unused | ... | md N-1 | ... | md 1 | md 0 ]
+//   |<------ 2^40 bytes ----------------------------------------------- >|
+//
+// We assume that the metadata is unchanged inside of the module by asserting it
+// does not alias with anything else. This allows the load to be hoisted, but
+// degrades use-after-free identification. An operation that changes metadata
+// state, like memory allocation, should not be instrumented by this pass.
+//
+//   %v = load i32, ptr addrspace(1) %p
+//
+// becomes:
+//
+//   ; bound: decode base(%p)
+//   %addr     = ptrtoint ptr addrspace(1) %p to i64
+//   %valid    = icmp eq i64 (lshr %addr, 45), 1      ; bit 45: in the window?
+//   %class    = and i64 (lshr %addr, 40), 31         ; bits [44:40]: size class
+//   %shift    = add i64 %class, 6                    ; chunk size 2^(6+C)
+//   %inregion = and i64 %addr, (1<<40)-1             ; offset in this class
+//   %idx      = lshr i64 %inregion, %shift           ; chunk index ("block")
+//   %slot     = or i64 (shl %idx, 3), 7              ; last byte of md word idx
+//   %meta     = sub i64 (%addr or (1<<40)-1), %slot  ; region top minus slot
+//   %ep       = inttoptr (select %valid, %meta, @__dasan_no_entry)
+//   %entry    = load i64, ptr addrspace(1) %ep, align 8, !alias.scope !0
+//   %size     = and i64 %entry, (1<<37)-1            ; live allocation size
+//   %off      = ashr i64 (and %entry, ~((1<<38)-1)), 32 ; chunk-base offset
+//   %chunkoff = sub i64 %inregion, (shl %idx, %shift)
+//   %start    = sub i64 %addr, (sub %chunkoff, %off) ; object base
+//
+//   ; check: this access against that bound
+//   %oob = icmp uge i64 (%addr - %start + 3), %size   ; last byte of i32
+//   %bad = and i1 %valid, %oob                        ; ignore non-window ptrs
+//   br i1 %bad, label %fail, label %go, !prof !unlikely
+// fail:
+//   call void @__dasan_report_load(ptr %p, i64 4, i64 %start, i64 %size)
+//   unreachable
+// go:
+//   %v = load i32, ptr addrspace(1) %p, !noalias !0
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Transforms/Instrumentation/DeviceAddressSanitizer.h"
+#include "llvm/ADT/DenseMap.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/InstSimplifyFolder.h"
+#include "llvm/Analysis/MemoryBuiltins.h"
+#include "llvm/Analysis/ValueTracking.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/InstIterator.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/IntrinsicsAMDGPU.h"
+#include "llvm/IR/MDBuilder.h"
+#include "llvm/IR/Module.h"
+#include "llvm/Support/AMDGPUAddrSpace.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/MathExtras.h"
+#include "llvm/Transforms/Instrumentation/AddressSanitizerCommon.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Instrumentation.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+
+using namespace llvm;
+
+#define DEBUG_TYPE "dasan"
+
+STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
+STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
+STATISTIC(NumDecodes, "Number of base pointer decodes emitted");
+
+// Constants describing the layout of the tracked memory region.
+static constexpr uint64_t kSpaceSizeLog = 45;
+static constexpr uint64_t kNumClassesLog = 5;
+static constexpr uint64_t kClassMask = (1ULL << kNumClassesLog) - 1;
+static constexpr uint64_t kRegionSizeLog = kSpaceSizeLog - kNumClassesLog;
+static constexpr uint64_t kRegionMask = (1ULL << kRegionSizeLog) - 1;
+static constexpr uint64_t kMinSizeLog = 6;
+static constexpr uint64_t kMinAlignment = 1ULL << kMinSizeLog;
+static constexpr uint64_t kMetadataSizeLog = 3;
+static constexpr uint64_t kMetadataSize = 1ULL << kMetadataSizeLog;
+static constexpr uint64_t kSizeBits = 37;
+static constexpr uint64_t kSizeMask = (1ULL << kSizeBits) - 1;
+static constexpr uint64_t kOffsetShift = kSizeBits + 1;
+static constexpr uint64_t kOffsetFieldMask = ~((1ULL << kOffsetShift) - 1);
+static constexpr uint64_t kOffsetScaleLog = kMinSizeLog;
+static constexpr unsigned kWordBits = 64;
+static_assert(kOffsetShift < kWordBits, "an entry is one word");
+
+static constexpr uint64_t kMetadataBytes =
+    (1ULL << (kRegionSizeLog - kMinSizeLog)) << kMetadataSizeLog;
+
+static constexpr uint64_t countSizeClasses() {
+  uint64_t N = 0;
+  while (N <= kClassMask &&
+         (1ULL << (kMinSizeLog + N)) <= (kRegionMask + 1) - kMetadataBytes)
+    ++N;
+  return N;
+}
+static_assert(countSizeClasses() << kRegionSizeLog == 1ULL << kSpaceSizeLog,
+              "the layout must keep every size class");
+static_assert(kMinSizeLog + countSizeClasses() - 1 <= kSizeBits,
+              "the largest chunk outgrows its entry");
+
+static constexpr char kReportLoadName[] = "__dasan_report_load";
+static constexpr char kReportStoreName[] = "__dasan_report_store";
+static constexpr char kReportSharedLoadName[] = "__dasan_report_shared_load";
+static constexpr char kReportSharedStoreName[] = "__dasan_report_shared_store";
+static constexpr char kRecoverSuffix[] = "_noabort";
+static constexpr char kModuleMarkerName[] = "__dasan_instrumented";
+static constexpr char kGlobalGuardName[] = "__dasan_guard";
+static constexpr char kNoEntryName[] = "__dasan_no_entry";
+static constexpr char kGlobalsSectionName[] = ".rodata.dasan.globals";
+static constexpr char kGlobalsDataSectionName[] = ".data.dasan.globals";
+static constexpr char kGlobalsRelRoSectionName[] = ".data.rel.ro.dasan.globals";
+static constexpr char kGlobalsBssSectionName[] = ".sbss.dasan.globals";
+
+static cl::opt<bool> ClInstrumentReads("dasan-instrument-reads", cl::Hidden,
+                                       cl::init(true));
+static cl::opt<bool> ClInstrumentWrites("dasan-instrument-writes", cl::Hidden,
+                                        cl::init(true));
+static cl::opt<bool> ClInstrumentAtomics("dasan-instrument-atomics", cl::Hidden,
+                                         cl::init(true));
+static cl::opt<bool> ClRecover("dasan-recover", cl::Hidden, cl::init(false));
+static cl::opt<bool> ClTrapOnError("dasan-trap-on-error", cl::Hidden,
+                                   cl::init(false));
+static cl::opt<cl::boolOrDefault>
+    ClInstrumentGlobals("dasan-instrument-globals", cl::Hidden,
+                        cl::desc("Wrap globals into the dasan window"));
+static cl::opt<bool> ClInstrumentShared("dasan-instrument-shared", cl::Hidden,
+                                        cl::init(true));
+
+namespace {
+
+// A single memory access.
+struct Access {
+  Instruction *I;
+  BasicBlock *Block;
+  Value *Ptr;
+  Value *Len;
+  uint64_t FixedLen;
+  MaybeAlign Alignment;
+  bool IsWrite;
+};
+
+// Access information from its associated metadata.
+struct Bound {
+  Value *Start;
+  Value *Size;
+  Value *Valid;
+};
+
+class NoSanitizeInserter final : public IRBuilderDefaultInserter {
+public:
+  void InsertHelper(Instruction *I, const Twine &Name,
+                    BasicBlock::iterator IP) const {
+    IRBuilderDefaultInserter::InsertHelper(I, Name, IP);
+    I->setMetadata(LLVMContext::MD_nosanitize,
+                   MDNode::get(I->getContext(), {}));
+  }
+};
+
+using BuilderTy = IRBuilder<InstSimplifyFolder, NoSanitizeInserter>;
+
+class DeviceAddressSanitizer {
+public:
+  DeviceAddressSanitizer(Module &M, const DeviceAddressSanitizerOptions &Opts)
+      : M(M), C(M.getContext()), DL(M.getDataLayout()),
+        IntptrTy(DL.getIntPtrType(C)), Int32Ty(Type::getInt32Ty(C)),
+        Recover(ClRecover.getNumOccurrences() ? ClRecover : Opts.Recover),
+        CheckShared(ClInstrumentShared && M.getTargetTriple().isAMDGPU()),
+        GlobalsAS(DL.getDefaultGlobalsAddressSpace()) {}
+
+  bool run();
+
+private:
+  void initializeCallbacks();
+  bool placeGlobals();
+  void emitModuleMarker();
+  bool instrumentFunction(Function &F);
+  void collect(Instruction &I, SmallVectorImpl<Access> &Accesses) const;
+  bool instrumentPointer(Value *Ptr) const;
+  bool shouldPlaceGlobal(const GlobalVariable &G) const;
+  bool isGlobalAS(unsigned AS) const;
+  bool isLocalAS(unsigned AS) const;
+  bool isPrivateAS(unsigned AS) const;
+  bool relocateGlobals() const;
+
+  void initEntryScope();
+  void markNoAliasEntry(Instruction *I) const;
+  Bound heapBound(BuilderTy &IRB, Value *Base, bool KnownMine = false);
+  Bound sharedBound(BuilderTy &IRB, Value *Ptr, Value *Limit);
+  void check(BuilderTy &IRB, const Access &A, const Bound &B, Instruction *At,
+             bool Shared, BasicBlock *Trap);
+  Value *sharedIntPtr(BuilderTy &IRB, Value *P);
+  Value *groupSegmentLimit(BuilderTy &IRB, Function &F);
+  void splitTo(BuilderTy &IRB, Instruction *At, Value *Bad, BasicBlock *Fail);
+  BasicBlock *createTrap(Function &F);
+  void emitReport(BuilderTy &IRB, Value *Addr, Value *Len, Value *Start,
+                  Value *Size, bool Shared, bool IsWrite);
+  GlobalVariable *sizedSharedGV(const Value *P) const;
+  GlobalVariable *anySharedGV(const Value *P) const;
+  uint64_t sharedAllocSize(const GlobalVariable &G) const;
+  bool aborting() const { return !Recover || ClTrapOnError; }
+
+  Constant *intptr(uint64_t V) const { return ConstantInt::get(IntptrTy, V); }
+  Constant *int32(uint32_t V) const { return ConstantInt::get(Int32Ty, V); }
+  void setPoint(BuilderTy &IRB, Instruction *At) const {
+    IRB.SetInsertPoint(At);
+    IRB.SetCurrentDebugLocation(Loc);
+  }
+
+  Module &M;
+  LLVMContext &C;
+  const DataLayout &DL;
+  IntegerType *IntptrTy;
+  Type *Int32Ty;
+  bool Recover;
+  bool CheckShared;
+  unsigned GlobalsAS;
+  FunctionCallee Report[2];
+  FunctionCallee ReportShared[2];
+  Constant *NoEntry = nullptr;
+  MDNode *EntryScope = nullptr;
+  DebugLoc Loc;
+};
+
+} // namespace
+
+bool DeviceAddressSanitizer::isGlobalAS(unsigned AS) const {
+  if (!M.getTargetTriple().isAMDGPU())
+    return AS == 0 || AS == GlobalsAS;
+  return AS == AMDGPUAS::FLAT_ADDRESS || AS == AMDGPUAS::GLOBAL_ADDRESS ||
+         AS == AMDGPUAS::CONSTANT_ADDRESS;
+}
+
+bool DeviceAddressSanitizer::isLocalAS(unsigned AS) const {
+  return M.getTargetTriple().isAMDGPU() && AS == AMDGPUAS::LOCAL_ADDRESS;
+}
+
+bool DeviceAddressSanitizer::isPrivateAS(unsigned AS) const {
+  return M.getTargetTriple().isAMDGPU() && AS == AMDGPUAS::PRIVATE_ADDRESS;
+}
+
+bool DeviceAddressSanitizer::relocateGlobals() const {
+  switch (ClInstrumentGlobals) {
+  case cl::boolOrDefault::BOU_TRUE:
+    return true;
+  case cl::boolOrDefault::BOU_FALSE:
+    return false;
+  case cl::boolOrDefault::BOU_UNSET:
+    return M.getTargetTriple().isAMDGPU();
+  }
+  llvm_unreachable("invalid -dasan-instrument-globals");
+}
+
+// Special accesses that should not be instrumented like the dispatch packet.
+static bool isSpecialMemory(const Value *Obj) {
+  const auto *II = dyn_cast<IntrinsicInst>(Obj);
+  if (!II)
+    return false;
+  switch (II->getIntrinsicID()) {
+  case Intrinsic::amdgcn_dispatch_ptr:
+  case Intrinsic::amdgcn_implicitarg_ptr:
+  case Intrinsic::amdgcn_kernarg_segment_ptr:
+  case Intrinsic::amdgcn_queue_ptr:
+  case Intrinsic::amdgcn_implicit_buffer_ptr:
+    return true;
+  default:
+    return false;
+  }
+}
+
+bool DeviceAddressSanitizer::instrumentPointer(Value *Ptr) const {
+  unsigned AS = Ptr->getType()->getPointerAddressSpace();
+  if (isLocalAS(AS))
+    return CheckShared;
+  if (isPrivateAS(AS))
+    return false;
+  if (!isGlobalAS(AS))
+    return false;
+  const Value *Obj = getUnderlyingObject(Ptr);
+  return !isa<AllocaInst>(Obj) && !isSpecialMemory(Obj);
+}
+
+void DeviceAddressSanitizer::collect(Instruction &I,
+                                     SmallVectorImpl<Access> &Accesses) const {
+  auto Add = [&](Value *Ptr, Value *Len, uint64_t Fixed, MaybeAlign A,
+                 bool IsWrite) {
+    if (!(IsWrite ? ClInstrumentWrites : ClInstrumentReads))
+      return;
+    if (!instrumentPointer(Ptr))
+      return;
+    Accesses.push_back({&I, I.getParent(), Ptr, Len,
+                        Fixed <= UINT32_MAX ? Fixed : 0, A, IsWrite});
+  };
+  auto AddTyped = [&](Value *Ptr, Type *Ty, MaybeAlign A, bool IsWrite) {
+    TypeSize Size = DL.getTypeStoreSize(Ty);
+    if (Size.isScalable() || Size.isZero())
+      return;
+    Add(Ptr, intptr(Size.getFixedValue()), Size.getFixedValue(), A, IsWrite);
+  };
+
+  // Collect every interesting instruction that touches memory.
+  if (auto *LI = dyn_cast<LoadInst>(&I))
+    AddTyped(LI->getPointerOperand(), LI->getType(), LI->getAlign(), false);
+  else if (auto *SI = dyn_cast<StoreInst>(&I))
+    AddTyped(SI->getPointerOperand(), SI->getValueOperand()->getType(),
+             SI->getAlign(), true);
+  else if (auto *RMW = dyn_cast<AtomicRMWInst>(&I)) {
+    if (ClInstrumentAtomics)
+      AddTyped(RMW->getPointerOperand(), RMW->getValOperand()->getType(),
+               std::nullopt, true);
+  } else if (auto *XCHG = dyn_cast<AtomicCmpXchgInst>(&I)) {
+    if (ClInstrumentAtomics)
+      AddTyped(XCHG->getPointerOperand(), XCHG->getCompareOperand()->getType(),
+               std::nullopt, true);
+  } else if (auto *MI = dyn_cast<AnyMemIntrinsic>(&I)) {
+    Value *Len = MI->getLength();
+    if (Len->getType()->getIntegerBitWidth() > IntptrTy->getBitWidth())
+      return;
+    auto *CI = dyn_cast<ConstantInt>(Len);
+    if (CI && CI->isZero())
+      return;
+    uint64_t Fixed = CI ? CI->getZExtValue() : 0;
+    Add(MI->getRawDest(), Len, Fixed, MI->getDestAlign(), true);
+    if (auto *MT = dyn_cast<AnyMemTransferInst>(MI))
+      Add(MT->getRawSource(), Len, Fixed, MT->getSourceAlign(), false);
+  }
+}
+
+static bool isSafeAccess(ObjectSizeOffsetVisitor &Vis, Value *Ptr,
+                         uint64_t Bytes) {
+  // Do not check accesses that are provably inbounds their respecitve object.
+  SizeOffsetAPInt SO = Vis.compute(Ptr);
+  if (!SO.bothKnown())
+    return false;
+  uint64_t Size = SO.Size.getZExtValue();
+  int64_t Off = SO.Offset.getSExtValue();
+
+  // Three checks are required to ensure safety:
+  // . Offset >= 0  (since the offset is given from the base ptr)
+  // . Size >= Offset  (unsigned)
+  // . Size - Offset >= NeededSize  (unsigned)
+  bool Safe =
+      Off >= 0 && Size >= uint64_t(Off) && Size - uint64_t(Off) >= Bytes;
+  return Safe;
+}
+
+static bool lastByteSuffices(const Access &A, uint64_t Bound) {
+  return A.FixedLen && isPowerOf2_64(A.FixedLen) && A.FixedLen <= Bound &&
+         A.Alignment && A.Alignment->value() >= A.FixedLen;
+}
+
+uint64_t
+DeviceAddressSanitizer::sharedAllocSize(const GlobalVariable &G) const {
+  if (!isLocalAS(G.getAddressSpace()) || G.isDeclaration())
+    return 0;
+  Type *Ty = G.getValueType();
+  if (!Ty->isSized())
+    return 0;
+  uint64_t Size = DL.getTypeAllocSize(Ty).getFixedValue();
+  return Size && Size <= UINT32_MAX ? Size : 0;
+}
+
+GlobalVariable *DeviceAddressSanitizer::sizedSharedGV(const Value *P) const {
+  auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(P));
+  return GV && sharedAllocSize(*GV) ? const_cast<GlobalVariable *>(GV)
+                                    : nullptr;
+}
+
+GlobalVariable *DeviceAddressSanitizer::anySharedGV(const Value *P) const {
+  auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(P));
+  return GV && isLocalAS(GV->getAddressSpace())
+             ? const_cast<GlobalVariable *>(GV)
+             : nullptr;
+}
+
+Bound DeviceAddressSanitizer::heapBound(BuilderTy &IRB, Value *Base,
+                                        bool KnownMine) {
+  // Compute the address in the metadata region and return its pointer bounds.
+  // KnownMine means the caller already branched on membership, so the slot is
+  // the only legal address and a select back to the zero word is dead.
+  Value *Addr = IRB.CreatePtrToInt(Base, IntptrTy);
+  Value *Valid = nullptr;
+  if (!KnownMine)
+    Valid = IRB.CreateICmpEQ(IRB.CreateLShr(Addr, kSpaceSizeLog), intptr(1));
+  Value *Class =
+      IRB.CreateAnd(IRB.CreateLShr(Addr, kRegionSizeLog), intptr(kClassMask));
+  Value *Shift = IRB.CreateAdd(Class, intptr(kMinSizeLog));
+  Value *InRegion = IRB.CreateAnd(Addr, intptr(kRegionMask));
+  Value *Idx = IRB.CreateLShr(InRegion, Shift);
+  Value *Slot = IRB.CreateOr(IRB.CreateShl(Idx, kMetadataSizeLog),
+                             intptr(kMetadataSize - 1));
+  Value *Meta = IRB.CreateSub(IRB.CreateOr(Addr, intptr(kRegionMask)), Slot);
+  if (!KnownMine)
+    Meta = IRB.CreateSelect(Valid, Meta, NoEntry);
+  LoadInst *Entry = IRB.CreateAlignedLoad(
+      IntptrTy, IRB.CreateIntToPtr(Meta, IRB.getPtrTy(GlobalsAS)),
+      Align(kMetadataSize));
+
+  Entry->setMetadata(LLVMContext::MD_alias_scope, EntryScope);
+  Value *Size = IRB.CreateAnd(Entry, intptr(kSizeMask));
+  Value *Off = IRB.CreateAShr(IRB.CreateAnd(Entry, intptr(kOffsetFieldMask)),
+                              kOffsetShift - kOffsetScaleLog);
+  Value *ChunkOff = IRB.CreateSub(InRegion, IRB.CreateShl(Idx, Shift));
+  Value *Start = IRB.CreateSub(Addr, IRB.CreateSub(ChunkOff, Off));
+
+  ++NumDecodes;
+  return {Start, Size, Valid};
+}
+
+Value *DeviceAddressSanitizer::sharedIntPtr(BuilderTy &IRB, Value *P) {
+  return IRB.CreateZExtOrTrunc(
+      IRB.CreatePtrToInt(P, DL.getIntPtrType(P->getType())), Int32Ty);
+}
+
+Bound DeviceAddressSanitizer::sharedBound(BuilderTy &IRB, Value *Ptr,
+                                          Value *Limit) {
+  if (GlobalVariable *GV = sizedSharedGV(Ptr))
+    return {ConstantExpr::getPtrToInt(GV, Int32Ty), int32(sharedAllocSize(*GV)),
+            nullptr};
+  return {int32(0), Limit, nullptr};
+}
+
+Value *DeviceAddressSanitizer::groupSegmentLimit(BuilderTy &IRB, Function &F) {
+  IRB.SetInsertPoint(&*F.getEntryBlock().getFirstInsertionPt());
+  CallInst *Packet =
+      IRB.CreateIntrinsicWithoutFolding(Intrinsic::amdgcn_dispatch_ptr, {});
+  Packet->addRetAttr(Attribute::NoAlias);
+  Packet->addRetAttr(Attribute::NonNull);
+  Packet->addDereferenceableRetAttr(64);
+
+  // Offset of the group_segment_size field in the HSA dispatch pointer.
+  static constexpr uint64_t kGroupSegmentSizeOffset = 28;
+  auto *Load =
+      IRB.CreateAlignedLoad(Int32Ty,
+                            IRB.CreateConstInBoundsGEP1_64(
+                                Int32Ty, Packet, kGroupSegmentSizeOffset / 4),
+                            Align(4));
+  Load->setMetadata(LLVMContext::MD_invariant_load, MDNode::get(C, {}));
+  return Load;
+}
+
+void DeviceAddressSanitizer::splitTo(BuilderTy &IRB, Instruction *At,
+                                     Value *Bad, BasicBlock *Fail) {
+  BasicBlock *Orig = At->getParent();
+  Orig->splitBasicBlock(At, "dasan.cont");
+  Instruction *Uncond = Orig->getTerminator();
+  IRB.SetInsertPoint(Uncond);
+  IRB.SetCurrentDebugLocation(Loc);
+  IRB.CreateCondBr(Bad, Fail, At->getParent(),
+                   MDBuilder(C).createUnlikelyBranchWeights());
+  Uncond->eraseFromParent();
+  setPoint(IRB, At);
+}
+
+BasicBlock *DeviceAddressSanitizer::createTrap(Function &F) {
+  BasicBlock *BB = BasicBlock::Create(C, "dasan.trap", &F);
+  BuilderTy IRB(M, InstSimplifyFolder(DL));
+  IRB.SetInsertPoint(BB);
+  if (DISubprogram *SP = F.getSubprogram())
+    IRB.SetCurrentDebugLocation(DILocation::get(C, SP->getScopeLine(), 0, SP));
+  IRB.CreateIntrinsic(Intrinsic::trap, {});
+  IRB.CreateUnreachable();
+  return BB;
+}
+
+void DeviceAddressSanitizer::emitReport(BuilderTy &IRB, Value *Addr, Value *Len,
+                                        Value *Start, Value *Size, bool Shared,
+                                        bool IsWrite) {
+  // The access may sit in a chunk that describes another object, or has no
+  // metadata mapped, so the handler reports against the bound the check used.
+  Value *P = IRB.CreateIntToPtr(Addr, IRB.getPtrTy());
+  CallInst *CI = IRB.CreateCall(
+      Shared ? ReportShared[IsWrite] : Report[IsWrite], {P, Len, Start, Size});
+
+  // The handler locates the access by its own return address, so every check
+  // needs a call of its own that is never merged, inlined, or tail called.
+  CI->setCannotMerge();
+  CI->setIsNoInline();
+  CI->setTailCallKind(CallInst::TCK_NoTail);
+}
+
+static Value *outOfBounds(BuilderTy &IRB, Value *Addr, Value *Len, Value *Start,
+                          Value *Size, bool LastByteOnly) {
+  Value *One = ConstantInt::get(Len->getType(), 1);
+  Value *First = IRB.CreateSub(Addr, Start);
+  Value *Last = IRB.CreateAdd(First, IRB.CreateSub(Len, One));
+  return IRB.CreateICmpUGE(
+      LastByteOnly ? Last
+                   : IRB.CreateBinaryIntrinsic(Intrinsic::umax, First, Last),
+      Size);
+}
+
+void DeviceAddressSanitizer::check(BuilderTy &IRB, const Access &A,
+                                   const Bound &B, Instruction *At, bool Shared,
+                                   BasicBlock *Trap) {
+  // Check if the given access fits within the known offset and size.
+  setPoint(IRB, At);
+  Value *Addr =
+      Shared ? sharedIntPtr(IRB, A.Ptr) : IRB.CreatePtrToInt(A.Ptr, IntptrTy);
+  Value *Len = IRB.CreateZExtOrTrunc(A.Len, IntptrTy);
+  Value *LenCmp = Shared ? IRB.CreateTrunc(Len, Int32Ty) : Len;
+  Value *Bad = outOfBounds(IRB, Addr, LenCmp, B.Start, B.Size,
+                           !Shared && lastByteSuffices(A, kMinAlignment));
+  if (!A.FixedLen) {
+    Bad = IRB.CreateAnd(Bad, IRB.CreateIsNotNull(Len));
+    Value *Sz = Shared ? IRB.CreateZExt(B.Size, IntptrTy) : B.Size;
+    Bad = IRB.CreateOr(Bad, IRB.CreateICmpUGT(Len, Sz));
+  }
+  if (B.Valid)
+    Bad = IRB.CreateAnd(B.Valid, Bad);
+
+  if (Trap) {
+    splitTo(IRB, At, Bad, Trap);
+    return;
+  }
+
+  Instruction *Then =
+      SplitBlockAndInsertIfThen(Bad, At, /*Unreachable=*/aborting(),
+                                MDBuilder(C).createUnlikelyBranchWeights());
+  Then->setDebugLoc(Loc);
+  setPoint(IRB, Then);
+  Value *AddrI = IRB.CreateZExtOrTrunc(Addr, IntptrTy);
+  Value *StartI = IRB.CreateZExtOrTrunc(B.Start, IntptrTy);
+  Value *SizeI = IRB.CreateZExtOrTrunc(B.Size, IntptrTy);
+  emitReport(IRB, AddrI, Len, StartI, SizeI, Shared, A.IsWrite);
+}
+
+void DeviceAddressSanitizer::initEntryScope() {
+  MDBuilder MDB(C);
+  MDNode *Domain = MDB.createAnonymousAliasScopeDomain("dasan.entry");
+  MDNode *Scope = MDB.createAnonymousAliasScope(Domain, "dasan.entry");
+  EntryScope = MDNode::get(C, {Scope});
+}
+
+void DeviceAddressSanitizer::markNoAliasEntry(Instruction *I) const {
+  I->setMetadata(
+      LLVMContext::MD_noalias,
+      MDNode::concatenate(I->getMetadata(LLVMContext::MD_noalias), EntryScope));
+}
+
+static bool isMemoryAccess(const Instruction &I) {
+  return isa<LoadInst, StoreInst, AtomicRMWInst, AtomicCmpXchgInst,
+             AnyMemIntrinsic>(I);
+}
+
+bool DeviceAddressSanitizer::instrumentFunction(Function &F) {
+  EntryScope = nullptr;
+  if (F.isDeclaration() || F.getName().starts_with("__dasan_"))
+    return false;
+  if (!F.hasFnAttribute(Attribute::SanitizeDeviceAddress) ||
+      F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation) ||
+      F.hasFnAttribute(Attribute::Naked))
+    return false;
+
+  SmallVector<Access, 16> Accesses;
+  for (Instruction &I : instructions(F))
+    if (!I.hasMetadata(LLVMContext::MD_nosanitize))
+      collect(I, Accesses);
+
+  ObjectSizeOffsetVisitor Vis(DL, /*TLI=*/nullptr, C);
+  auto IsSafe = [&](const Access &A) {
+    return A.FixedLen && isSafeAccess(Vis, A.Ptr, A.FixedLen);
+  };
+  // No scope when this function never decodes an entry.
+  const bool NeedsEntry = any_of(Accesses, [&](const Access &A) {
+    return isGlobalAS(A.Ptr->getType()->getPointerAddressSpace()) && !IsSafe(A);
+  });
+  if (NeedsEntry) {
+    initEntryScope();
+    for (Instruction &I : instructions(F)) {
+      if (I.hasMetadata(LLVMContext::MD_nosanitize) || !isMemoryAccess(I))
+        continue;
+      markNoAliasEntry(&I);
+    }
+  }
+
+  erase_if(Accesses, IsSafe);
+  if (Accesses.empty())
+    return false;
+
+  if (!NoEntry)
+    initializeCallbacks();
+  removeASanIncompatibleFnAttributes(F, /*ReadsArgMem=*/false);
+
+  BasicBlock *Trap = ClTrapOnError ? createTrap(F) : nullptr;
+
+  BuilderTy IRB(M, InstSimplifyFolder(DL));
+  Value *Limit = nullptr;
+  auto NeedsLimit = [this](const Access &A) {
+    if (isLocalAS(A.Ptr->getType()->getPointerAddressSpace()))
+      return !sizedSharedGV(A.Ptr);
+    return anySharedGV(A.Ptr) && !sizedSharedGV(A.Ptr);
+  };
+  if (CheckShared && any_of(Accesses, NeedsLimit)) {
+    F.removeFnAttr("amdgpu-no-dispatch-ptr");
+    Limit = groupSegmentLimit(IRB, F);
+  }
+
+  auto TakeLoc = [&](const Access &A) {
+    Loc = A.I->getDebugLoc();
+    if (!Loc)
+      if (DISubprogram *SP = A.I->getFunction()->getSubprogram())
+        Loc = DILocation::get(C, SP->getScopeLine(), 0, SP);
+  };
+
+  // One decode per inbounds root in the block. The bounds are available
+  // anywhere inbounds from the access.
+  //
+  // Off the GPU the common pointer is outside the window. A select would still
+  // decode metadata for it, so membership is a branch and the decode is the
+  // cold side. One icmp per root; each access then jumps past its own check.
+  DenseMap<Value *, Bound> HeapB;
+  DenseMap<Value *, Value *> MineB;
+  DenseMap<Value *, Bound> SharedB;
+  BasicBlock *Block = nullptr;
+  const bool BranchUnlessMine = !M.getTargetTriple().isAMDGPU();
+  for (const Access &A : Accesses) {
+    if (A.Block != Block) {
+      Block = A.Block;
+      HeapB.clear();
+      MineB.clear();
+      SharedB.clear();
+    }
+    Instruction *At = A.I;
+    TakeLoc(A);
+    unsigned AS = A.Ptr->getType()->getPointerAddressSpace();
+    if (CheckShared && (isLocalAS(AS) || anySharedGV(A.Ptr))) {
+      // TODO: LDS instrumentation relies entirely on finding the underlying
+      //       object. This is sufficient to catch most uses, but a real
+      //       solution will require some form of pointer tagging.
+      GlobalVariable *GV = sizedSharedGV(A.Ptr);
+      if (!GV && !Limit)
+        continue;
+      Value *Root = A.Ptr;
+      if (GV)
+        Root = GV;
+      auto [It, New] = SharedB.try_emplace(Root);
+      if (New) {
+        setPoint(IRB, At);
+        It->second = sharedBound(IRB, A.Ptr, Limit);
+      }
+      check(IRB, A, It->second, At, true, Trap);
+    } else if (isGlobalAS(AS)) {
+      Value *Root = A.Ptr->stripInBoundsOffsets();
+      if (BranchUnlessMine) {
+        auto [It, New] = MineB.try_emplace(Root);
+        if (New) {
+          setPoint(IRB, At);
+          Value *Addr = IRB.CreatePtrToInt(Root, IntptrTy);
+          It->second =
+              IRB.CreateICmpEQ(IRB.CreateLShr(Addr, kSpaceSizeLog), intptr(1));
+        }
+        Instruction *IP = SplitBlockAndInsertIfThen(
+            It->second, At, /*Unreachable=*/false,
+            MDBuilder(C).createUnlikelyBranchWeights());
+        IP->getParent()->setName("dasan.mine");
+        setPoint(IRB, IP);
+        check(IRB, A, heapBound(IRB, Root, /*KnownMine=*/true), IP, false,
+              Trap);
+      } else {
+        auto [It, New] = HeapB.try_emplace(Root);
+        if (New) {
+          setPoint(IRB, At);
+          It->second = heapBound(IRB, Root);
+        }
+        check(IRB, A, It->second, At, false, Trap);
+      }
+    }
+    ++(A.IsWrite ? NumInstrumentedWrites : NumInstrumentedReads);
+  }
+  return true;
+}
+
+void DeviceAddressSanitizer::initializeCallbacks() {
+  IRBuilder<> IRB(M);
+  AttributeList AL = AttributeList()
+                         .addFnAttribute(C, Attribute::NoUnwind)
+                         .addFnAttribute(C, Attribute::Cold);
+  if (!Recover)
+    AL = AL.addFnAttribute(C, Attribute::NoReturn);
+  StringRef Suffix = Recover ? kRecoverSuffix : "";
+  if (!ClTrapOnError) {
+    FunctionType *Ty = FunctionType::get(
+        IRB.getVoidTy(),
+        {IRB.getPtrTy(), IRB.getInt64Ty(), IRB.getInt64Ty(), IRB.getInt64Ty()},
+        false);
+    Report[0] =
+        M.getOrInsertFunction((Twine(kReportLoadName) + Suffix).str(), Ty, AL);
+    Report[1] =
+        M.getOrInsertFunction((Twine(kReportStoreName) + Suffix).str(), Ty, AL);
+    ReportShared[0] = M.getOrInsertFunction(
+        (Twine(kReportSharedLoadName) + Suffix).str(), Ty, AL);
+    ReportShared[1] = M.getOrInsertFunction(
+        (Twine(kReportSharedStoreName) + Suffix).str(), Ty, AL);
+  }
+  auto *Word = IRB.getInt64Ty();
+  auto *GV = new GlobalVariable(
+      M, Word, /*isConstant=*/true, GlobalValue::PrivateLinkage,
+      ConstantInt::get(Word, 0), kNoEntryName, nullptr,
+      GlobalValue::NotThreadLocal, GlobalsAS);
+  GV->setAlignment(Align(kMetadataSize));
+  NoEntry = ConstantExpr::getPtrToInt(GV, IntptrTy);
+}
+
+void DeviceAddressSanitizer::emitModuleMarker() {
+  Type *Ty = Type::getInt8Ty(C);
+  auto *GV = new GlobalVariable(
+      M, Ty, /*isConstant=*/true, GlobalValue::LinkOnceODRLinkage,
+      ConstantInt::get(Ty, 1), kModuleMarkerName, nullptr,
+      GlobalValue::NotThreadLocal, GlobalsAS);
+  GV->setVisibility(GlobalValue::ProtectedVisibility);
+  GV->setAlignment(Align(1ULL << kMinSizeLog));
+  appendToUsed(M, {GV});
+}
+
+static bool isReservedName(StringRef N) {
+  return N.starts_with("__dasan_") || N.starts_with("llvm.");
+}
+
+bool DeviceAddressSanitizer::shouldPlaceGlobal(const GlobalVariable &G) const {
+  if (!isGlobalAS(G.getAddressSpace()))
+    return false;
+  if (G.isDeclarationForLinker() || G.isThreadLocal() || G.hasCommonLinkage() ||
+      G.hasAppendingLinkage() || G.hasSection() ||
+      G.hasMetadata(LLVMContext::MD_type))
+    return false;
+  if (isReservedName(G.getName()))
+    return false;
+  return G.getParent()->getDataLayout().getTypeAllocSize(G.getValueType()) != 0;
+}
+
+static StringRef sectionForGlobal(const GlobalVariable &G) {
+  const Constant *Init = G.getInitializer();
+  if (Init->isNullValue())
+    return kGlobalsBssSectionName;
+  if (!G.isConstant())
+    return kGlobalsDataSectionName;
+  if (Init->needsRelocation())
+    return kGlobalsRelRoSectionName;
+  return kGlobalsSectionName;
+}
+
+static uint64_t globalRedzoneSize(uint64_t Size, uint64_t ChunkSize) {
+  constexpr uint64_t kMaxGlobalRedzone = 1ULL << 18;
+  if (Size <= ChunkSize / 2)
+    return ChunkSize - Size;
+  uint64_t RZ = std::clamp((Size / ChunkSize / 4) * ChunkSize, ChunkSize,
+                           kMaxGlobalRedzone);
+  if (Size % ChunkSize)
+    RZ += ChunkSize - (Size % ChunkSize);
+  return RZ;
+}
+
+bool DeviceAddressSanitizer::placeGlobals() {
+  if (!relocateGlobals())
+    return false;
+
+  // Global placement works by loading the executable itself inside of the
+  // tracked region. This requires that globals to be instrumented are aligned
+  // at least to the minimum chunk size. A single global can span many chunks,
+  // but the signed offset points back to the original offset.
+  constexpr uint64_t ChunkSize = 1ULL << kMinSizeLog;
+  const Align ChunkAlign(ChunkSize);
+  SmallVector<GlobalVariable *, 8> GlobalsToPlace;
+  SmallVector<GlobalVariable *, 8> GlobalsToPin;
+  for (GlobalVariable &G : M.globals()) {
+    if (!shouldPlaceGlobal(G))
+      continue;
+    if (G.getName().starts_with("__") || G.getName().starts_with("."))
+      GlobalsToPin.push_back(&G);
+    else
+      GlobalsToPlace.push_back(&G);
+  }
+
+  // We size globals based off of their symbol `st_size`. Private symbols do not
+  // get an entry in the symbol table so we convert them to internal symbols.
+  // These are globals we do not own the layout for so we only align them.
+  for (GlobalVariable *G : GlobalsToPin) {
+    if (G->hasPrivateLinkage())
+      G->setLinkage(GlobalValue::InternalLinkage);
+    G->setAlignment(std::max(G->getAlign().valueOrOne(), ChunkAlign));
+    G->setSection(sectionForGlobal(*G));
+  }
+
+  // Add additional redzones to globals to detect buffer overruns without
+  // stepping into the next valid object.
+  Type *Int8Ty = Type::getInt8Ty(C);
+  SmallVector<GlobalValue *, 16> Kept;
+  for (GlobalVariable *G : GlobalsToPlace) {
+    Type *Ty = G->getValueType();
+    const unsigned AS = G->getAddressSpace();
+    const uint64_t Size = DL.getTypeAllocSize(Ty).getFixedValue();
+    const Align Alignment = std::max(G->getAlign().valueOrOne(), ChunkAlign);
+    const uint64_t Total =
+        alignTo(Size + globalRedzoneSize(Size, ChunkSize), Alignment);
+    const uint64_t GuardAt = alignTo(Size, ChunkSize);
+    auto *PadTy = ArrayType::get(Int8Ty, Total - Size);
+    auto *WholeTy = StructType::get(C, {Ty, PadTy});
+
+    // Generate a new global variable with sufficient redzone padding.
+    auto *Whole = new GlobalVariable(
+        M, WholeTy, G->isConstant(), GlobalValue::PrivateLinkage,
+        ConstantStruct::get(
+            WholeTy, {G->getInitializer(), Constant::getNullValue(PadTy)}),
+        G->getName() + ".dasan", G, GlobalValue::NotThreadLocal, AS);
+    Whole->setAlignment(Alignment);
+    Whole->setSection(sectionForGlobal(*G));
+    Whole->setExternallyInitialized(G->isExternallyInitialized());
+    Whole->copyMetadata(G, 0);
+    Whole->setComdat(G->getComdat());
+
+    // Replace the original global with an alias into the created fat global.
+    auto *Obj = GlobalAlias::create(
+        Ty, AS,
+        G->hasPrivateLinkage() ? GlobalValue::InternalLinkage : G->getLinkage(),
+        "", Whole, &M);
+    Obj->setVisibility(G->getVisibility());
+    Obj->setDSOLocal(G->isDSOLocal());
+    G->replaceAllUsesWith(Obj);
+    Obj->takeName(G);
+    G->eraseFromParent();
+    Kept.push_back(Obj);
+    Kept.push_back(Whole);
+    if (Total > GuardAt)
+      Kept.push_back(GlobalAlias::create(
+          ArrayType::get(Int8Ty, Total - GuardAt), AS,
+          GlobalValue::InternalLinkage, kGlobalGuardName,
+          ConstantExpr::getPtrAdd(Whole, intptr(GuardAt)), &M));
+  }
+  appendToCompilerUsed(M, Kept);
+  return !GlobalsToPlace.empty() || !GlobalsToPin.empty();
+}
+
+bool DeviceAddressSanitizer::run() {
+  bool Changed = false;
+  for (Function &F : M)
+    Changed |= instrumentFunction(F);
+  Changed |= placeGlobals();
+  if (Changed && !M.getNamedValue(kModuleMarkerName))
+    emitModuleMarker();
+  return Changed;
+}
+
+PreservedAnalyses DeviceAddressSanitizerPass::run(Module &M,
+                                                  ModuleAnalysisManager &MAM) {
+  if (checkIfAlreadyInstrumented(M, "nosanitize_device_address"))
+    return PreservedAnalyses::all();
+  if (M.getDataLayout().getPointerSizeInBits() != kWordBits)
+    return PreservedAnalyses::all();
+
+  if (!DeviceAddressSanitizer(M, Options).run())
+    return PreservedAnalyses::all();
+  return PreservedAnalyses::none();
+}
diff --git a/llvm/test/CodeGen/AMDGPU/amdgpu-attributor-dasan.ll b/llvm/test/CodeGen/AMDGPU/amdgpu-attributor-dasan.ll
new file mode 100644
index 00000000000000..b1d4b1c9049b9a
--- /dev/null
+++ b/llvm/test/CodeGen/AMDGPU/amdgpu-attributor-dasan.ll
@@ -0,0 +1,34 @@
+; RUN: opt -S -mtriple=amdgcn-amd-amdhsa -passes=amdgpu-attributor %s | FileCheck %s
+
+; CHECK-LABEL: define amdgpu_kernel void @k0(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+define amdgpu_kernel void @k0() #0 {
+  ret void
+}
+
+; CHECK-LABEL: define void @f0(
+; CHECK-SAME: ) #[[ATTR0]] {
+define void @f0(ptr addrspace(3) %p) #0 {
+  store i32 0, ptr addrspace(3) %p
+  ret void
+}
+
+; CHECK-LABEL: define amdgpu_kernel void @caller(
+; CHECK-SAME: ) #[[CALLER:[0-9]+]] {
+define amdgpu_kernel void @caller() {
+  call void @f0(ptr addrspace(3) null)
+  ret void
+}
+
+; CHECK-LABEL: define amdgpu_kernel void @k_preannotated(
+; CHECK-SAME: ) #[[ATTR0]] {
+define amdgpu_kernel void @k_preannotated() #1 {
+  ret void
+}
+
+attributes #0 = { sanitize_device_address }
+attributes #1 = { sanitize_device_address "amdgpu-no-dispatch-ptr" }
+
+; CHECK: attributes #[[ATTR0]] = { sanitize_device_address "amdgpu-no-cluster-id-x" "amdgpu-no-cluster-id-y" "amdgpu-no-cluster-id-z" "amdgpu-no-completion-action" "amdgpu-no-default-queue" "amdgpu-no-dispatch-id" "amdgpu-no-dispatch-ptr" "amdgpu-no-flat-scratch-init" "amdgpu-no-heap-ptr" "amdgpu-no-hostcall-ptr" "amdgpu-no-implicitarg-ptr" "amdgpu-no-lds-dma" "amdgpu-no-lds-kernel-id" "amdgpu-no-multigrid-sync-arg" "amdgpu-no-queue-ptr" "amdgpu-no-workgroup-id-x" "amdgpu-no-workgroup-id-y" "amdgpu-no-workgroup-id-z" "amdgpu-no-workitem-id-x" "amdgpu-no-workitem-id-y" "amdgpu-no-workitem-id-z" "amdgpu-no-wwm" }
+; CHECK: attributes #[[CALLER]] = {
+; CHECK-SAME: "amdgpu-no-dispatch-ptr"
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/basic.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/basic.ll
new file mode 100644
index 00000000000000..59887514988aeb
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/basic.ll
@@ -0,0 +1,403 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+; CHECK: @__dasan_no_entry = private addrspace(1) constant i64 0
+; CHECK: @__dasan_instrumented = linkonce_odr protected addrspace(1) constant i8 1
+
+define i32 @load4(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define i32 @load4(
+; CHECK-NEXT:    [[ADDR:%.*]] = ptrtoint ptr addrspace(1) %a to i64
+; CHECK-NEXT:    [[TOP:%.*]] = lshr i64 [[ADDR]], 45
+; CHECK-NEXT:    [[MINE:%.*]] = icmp eq i64 [[TOP]], 1
+; CHECK-NEXT:    [[CLS:%.*]] = lshr i64 [[ADDR]], 40
+; CHECK-NEXT:    [[CLSM:%.*]] = and i64 [[CLS]], 31
+; CHECK-NEXT:    [[SHIFT:%.*]] = add i64 [[CLSM]], 6
+; CHECK-NEXT:    [[INREG:%.*]] = and i64 [[ADDR]], 1099511627775
+; CHECK-NEXT:    [[IDX:%.*]] = lshr i64 [[INREG]], [[SHIFT]]
+; CHECK-NEXT:    [[BYTE:%.*]] = shl i64 [[IDX]], 3
+; CHECK-NEXT:    [[SLOT:%.*]] = or i64 [[BYTE]], 7
+; CHECK-NEXT:    [[END:%.*]] = or i64 [[ADDR]], 1099511627775
+; CHECK-NEXT:    [[EA:%.*]] = sub i64 [[END]], [[SLOT]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[MINE]], i64 [[EA]], i64 ptrtoint (ptr addrspace(1) @__dasan_no_entry to i64)
+; CHECK-NEXT:    [[EP:%.*]] = inttoptr i64 [[SEL]] to ptr addrspace(1)
+; CHECK-NEXT:    [[ENTRY:%.*]] = load i64, ptr addrspace(1) [[EP]], align 8, !alias.scope ![[SCOPE:[0-9]+]], !nosanitize
+; CHECK-NEXT:    [[SIZE:%.*]] = and i64 [[ENTRY]], 137438953471
+; CHECK-NEXT:    [[OFFW:%.*]] = and i64 [[ENTRY]], -274877906944
+; CHECK-NEXT:    [[OFF:%.*]] = ashr i64 [[OFFW]], 32
+; CHECK-NEXT:    [[CBASE:%.*]] = shl i64 [[IDX]], [[SHIFT]]
+; CHECK-NEXT:    [[COFF:%.*]] = sub i64 [[INREG]], [[CBASE]]
+; CHECK-NEXT:    [[INOBJ:%.*]] = sub i64 [[COFF]], [[OFF]]
+; CHECK:         [[START:%.*]] = sub i64 [[ADDR]], [[INOBJ]]
+; CHECK:         [[LAST:%.*]] = add i64 [[INOBJ]], 3
+; CHECK-NEXT:    [[OOB:%.*]] = icmp uge i64 [[LAST]], [[SIZE]]
+; CHECK-NEXT:    [[BAD:%.*]] = and i1 [[MINE]], [[OOB]]
+; CHECK-NEXT:    br i1 [[BAD]], label %[[FAIL:.*]], label %[[GO:.*]], !prof
+; CHECK:       [[FAIL]]:
+; CHECK:         notail call void @__dasan_report_load(ptr {{.*}}, i64 4, i64 [[START]], i64 [[SIZE]]) #[[SITE:[0-9]+]]
+; CHECK-NEXT:    unreachable
+; CHECK:       [[GO]]:
+; CHECK-NEXT:    %v = load i32, ptr addrspace(1) %a, align 4, !noalias ![[SCOPE]]
+  %v = load i32, ptr addrspace(1) %a, align 4
+  ret i32 %v
+}
+
+define void @store4_unaligned(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @store4_unaligned(
+; CHECK:         [[L:%.*]] = add i64 [[F:%.*]], 3
+; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.umax.i64(i64 [[F]], i64 [[L]])
+; CHECK-NEXT:    icmp uge i64 [[M]], %{{.*}}
+; CHECK:         call void @__dasan_report_store(
+  store i32 0, ptr addrspace(1) %a, align 1
+  ret void
+}
+
+define void @store64_aligned(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @store64_aligned(
+; CHECK:         [[L:%.*]] = add i64 %{{.*}}, 63
+; CHECK-NEXT:    icmp uge i64 [[L]], %{{.*}}
+; CHECK:         store <16 x i32>
+  store <16 x i32> zeroinitializer, ptr addrspace(1) %a, align 64
+  ret void
+}
+
+define void @store128_aligned(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @store128_aligned(
+; CHECK:         [[L:%.*]] = add i64 [[F:%.*]], 127
+; CHECK-NEXT:    [[M:%.*]] = call i64 @llvm.umax.i64(i64 [[F]], i64 [[L]])
+; CHECK-NEXT:    icmp uge i64 [[M]], %{{.*}}
+  store <32 x i32> zeroinitializer, ptr addrspace(1) %a, align 128
+  ret void
+}
+
+define void @other_addrspace(ptr addrspace(3) %a) sanitize_device_address {
+; CHECK-LABEL: define void @other_addrspace(
+; CHECK-NEXT:    [[P:%.*]] = call{{.*}} ptr addrspace(4) @llvm.amdgcn.dispatch.ptr()
+; CHECK-NEXT:    [[G:%.*]] = getelementptr inbounds i8, ptr addrspace(4) [[P]], i64 28
+; CHECK-NEXT:    [[LIM:%.*]] = load i32, ptr addrspace(4) [[G]], align 4, !invariant.load
+; CHECK:         icmp uge i32 %{{.*}}, [[LIM]]
+; CHECK:         call void @__dasan_report_shared_store(
+; CHECK:         store i32 0, ptr addrspace(3) %{{.*}}, align 4
+  store i32 0, ptr addrspace(3) %a, align 4
+  ret void
+}
+
+define void @loop(ptr addrspace(1) %a, i32 %n) sanitize_device_address {
+; CHECK-LABEL: define void @loop(
+; CHECK:       body:
+; CHECK-NEXT:    %i = phi i32
+; CHECK-NEXT:    %p = getelementptr
+; CHECK-NEXT:    [[BASE:%.*]] = ptrtoint ptr addrspace(1) %a to i64
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         call void @__dasan_report_store(
+entry:
+  br label %body
+body:
+  %i = phi i32 [ 0, %entry ], [ %next, %latch ]
+  %p = getelementptr inbounds i32, ptr addrspace(1) %a, i32 %i
+  store i32 1, ptr addrspace(1) %p, align 4
+  br label %latch
+latch:
+  %next = add i32 %i, 1
+  %c = icmp slt i32 %next, %n
+  br i1 %c, label %body, label %out
+out:
+  ret void
+}
+
+; RUN: opt < %s -passes='dasan,function(loop-mssa(licm))' -S \
+; RUN:   | FileCheck %s --check-prefix=LICM
+; LICM-LABEL: define void @loop(
+; LICM:       entry:
+; LICM:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; LICM:       body:
+; LICM-NOT:     load i64
+; LICM:         call void @__dasan_report_store(
+
+; RUN: opt < %s -passes='dasan,function(loop-mssa(licm))' -dasan-recover -S \
+; RUN:   | FileCheck %s --check-prefix=REC-LICM
+; The recover handler returns, so it is a call in the loop and the load stays.
+; REC-LICM-LABEL: define void @loop(
+; REC-LICM:       body:
+; REC-LICM:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; REC-LICM:         call void @__dasan_report_store_noabort(
+
+declare void @mutate()
+declare void @peek() readonly nounwind
+
+; The decode is in the header, ahead of the call, so it is free to leave the
+; loop. mutate may store the entry word, so the load stays. peek only reads.
+define void @loop_call(ptr addrspace(1) %a, i32 %n) sanitize_device_address {
+; CHECK-LABEL: define void @loop_call(
+; CHECK:       body:
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         call void @mutate()
+; LICM-LABEL: define void @loop_call(
+; LICM:       body:
+; LICM:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; LICM:         call void @mutate()
+entry:
+  br label %body
+body:
+  %i = phi i32 [ 0, %entry ], [ %next, %body ]
+  store i32 %i, ptr addrspace(1) %a, align 4
+  call void @mutate()
+  %next = add i32 %i, 1
+  %c = icmp slt i32 %next, %n
+  br i1 %c, label %body, label %out
+out:
+  ret void
+}
+
+define void @loop_peek(ptr addrspace(1) %a, i32 %n) sanitize_device_address {
+; CHECK-LABEL: define void @loop_peek(
+; CHECK:       body:
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         call void @peek()
+; LICM-LABEL: define void @loop_peek(
+; LICM:       entry:
+; LICM:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; LICM:       body:
+; LICM-NOT:     load i64
+; LICM:         call void @peek()
+entry:
+  br label %body
+body:
+  %i = phi i32 [ 0, %entry ], [ %next, %body ]
+  store i32 %i, ptr addrspace(1) %a, align 4
+  call void @peek()
+  %next = add i32 %i, 1
+  %c = icmp slt i32 %next, %n
+  br i1 %c, label %body, label %out
+out:
+  ret void
+}
+
+declare ptr addrspace(4) @llvm.amdgcn.implicitarg.ptr()
+
+define i32 @dims() sanitize_device_address {
+; CHECK-LABEL: define i32 @dims(
+; CHECK-NEXT:    %p = call
+; CHECK-NEXT:    %g = getelementptr
+; CHECK-NEXT:    %v = load i32, ptr addrspace(4) %g
+; CHECK-NEXT:    ret i32 %v
+  %p = call ptr addrspace(4) @llvm.amdgcn.implicitarg.ptr()
+  %g = getelementptr i32, ptr addrspace(4) %p, i64 3
+  %v = load i32, ptr addrspace(4) %g, align 4
+  ret i32 %v
+}
+
+define void @no_attribute(ptr addrspace(1) %a) {
+; CHECK-LABEL: define void @no_attribute(
+; CHECK-NEXT:    store i32 0, ptr addrspace(1) %a, align 4
+; CHECK-NEXT:    ret void
+  store i32 0, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+; RUN: opt < %s -passes=dasan,gvn -S | FileCheck %s --check-prefix=GVN
+define void @bump(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @bump(
+; CHECK-COUNT-1: load i64, ptr addrspace(1)
+; CHECK-NOT:     load i64, ptr addrspace(1)
+; CHECK:         ret void
+; GVN-LABEL: define void @bump(
+; GVN:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; GVN-NOT:     load i64
+; GVN:         ret void
+  %v = load i32, ptr addrspace(1) %a, align 4
+  %w = add i32 %v, 1
+  store i32 %w, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+define void @notoptimized(ptr addrspace(1) %a) sanitize_device_address optnone noinline {
+; CHECK-LABEL: define void @notoptimized(
+; CHECK:         load i64, ptr addrspace(1)
+; CHECK:         call void @__dasan_report_store
+  store i32 1, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+ at g = addrspace(1) global [4 x i32] zeroinitializer
+
+define void @named(i64 %i) sanitize_device_address {
+; CHECK-LABEL: define void @named(
+; CHECK:         lshr i64 %{{.*}}, 45
+; CHECK:         [[A:%.*]] = ptrtoint ptr addrspace(1) %p to i64
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 1, ptr addrspace(1) %p
+; CHECK-NEXT:    %q = alloca
+; CHECK-NEXT:    %r = addrspacecast
+; CHECK-NEXT:    store i32 2, ptr %r
+; CHECK-NEXT:    ret void
+  %p = getelementptr [4 x i32], ptr addrspace(1) @g, i64 0, i64 %i
+  store i32 1, ptr addrspace(1) %p, align 4
+  %q = alloca i32, align 4, addrspace(5)
+  %r = addrspacecast ptr addrspace(5) %q to ptr
+  store i32 2, ptr %r, align 4
+  ret void
+}
+
+define void @constant_indices(i64 %i) sanitize_device_address {
+; CHECK-LABEL: define void @constant_indices(
+; CHECK:         %inside = getelementptr
+; CHECK-NEXT:    store i32 1, ptr addrspace(1) %inside, align 4, !noalias
+; CHECK:         %past = getelementptr
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 2, ptr addrspace(1) %past
+; CHECK:         %var = getelementptr
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 3, ptr addrspace(1) %var
+  %inside = getelementptr [4 x i32], ptr addrspace(1) @g, i64 0, i64 3
+  store i32 1, ptr addrspace(1) %inside, align 4
+  %past = getelementptr [4 x i32], ptr addrspace(1) @g, i64 0, i64 4
+  store i32 2, ptr addrspace(1) %past, align 4
+  %var = getelementptr [4 x i32], ptr addrspace(1) @g, i64 0, i64 %i
+  store i32 3, ptr addrspace(1) %var, align 4
+  ret void
+}
+
+define void @widen(ptr addrspace(1) %p, i64 %i) sanitize_device_address {
+; CHECK-LABEL: define void @widen(
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 0, ptr addrspace(1) %a
+; CHECK:         store i32 1, ptr addrspace(1) %b
+; CHECK:         store i32 2, ptr addrspace(1) %c
+; CHECK:         store i32 3, ptr addrspace(1) %d
+; CHECK-NEXT:    ret void
+  %a = getelementptr i32, ptr addrspace(1) %p, i64 %i
+  store i32 0, ptr addrspace(1) %a, align 4
+  %j = add i64 %i, 1
+  %b = getelementptr i32, ptr addrspace(1) %p, i64 %j
+  store i32 1, ptr addrspace(1) %b, align 4
+  %k = add i64 %i, 2
+  %c = getelementptr i32, ptr addrspace(1) %p, i64 %k
+  store i32 2, ptr addrspace(1) %c, align 4
+  %l = add i64 %i, 3
+  %d = getelementptr i32, ptr addrspace(1) %p, i64 %l
+  store i32 3, ptr addrspace(1) %d, align 4
+  ret void
+}
+
+define void @chased(ptr addrspace(1) %p) sanitize_device_address {
+; CHECK-LABEL: define void @chased(
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 0, ptr addrspace(1) %p
+; CHECK:         call void @__dasan_report_load(
+; CHECK:         %q = load ptr addrspace(1), ptr addrspace(1) %p
+; CHECK:         call void @__dasan_report_store(
+; CHECK:         store i32 1, ptr addrspace(1) %q
+  store i32 0, ptr addrspace(1) %p, align 8
+  %q = load ptr addrspace(1), ptr addrspace(1) %p, align 8
+  store i32 1, ptr addrspace(1) %q, align 4
+  ret void
+}
+
+define void @across_call(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @across_call(
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK-NOT:     !alias.scope
+; CHECK:         call void @mutate()
+; CHECK:         ret void
+  store i32 1, ptr addrspace(1) %a, align 4
+  call void @mutate()
+  store i32 2, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+define void @across_peek(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define void @across_peek(
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         call void @peek()
+; CHECK-NOT:     load i64, ptr addrspace(1)
+; CHECK:         ret void
+  store i32 1, ptr addrspace(1) %a, align 4
+  call void @peek()
+  store i32 2, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+define void @across_raw(ptr addrspace(1) %a, ptr addrspace(1) %w) sanitize_device_address {
+; CHECK-LABEL: define void @across_raw(
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         store i64 0, ptr addrspace(1) %w, align 8, !nosanitize !{{[0-9]+}}{{$}}
+; CHECK:         ret void
+  store i32 1, ptr addrspace(1) %a, align 4
+  store i64 0, ptr addrspace(1) %w, align 8, !nosanitize !{}
+  store i32 2, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+define void @only_safe() sanitize_device_address {
+; CHECK-LABEL: define void @only_safe(
+; CHECK-NEXT:    store i32 1, ptr addrspace(1) @g, align 4
+; CHECK-NEXT:    ret void
+  store i32 1, ptr addrspace(1) getelementptr inbounds ([4 x i32], ptr addrspace(1) @g, i64 0, i64 0), align 4
+  ret void
+}
+
+; A provably-inbounds store is not checked, but it carries the scope so the
+; decode still hoists out of the loop.
+define void @loop_safe(ptr addrspace(1) %a, i32 %n) sanitize_device_address {
+; CHECK-LABEL: define void @loop_safe(
+; CHECK:         store i32 0, ptr addrspace(1) @g, align 4, !noalias ![[SAFE:[0-9]+]]
+; CHECK:         load i64, ptr addrspace(1) {{.*}}, !alias.scope ![[SAFE]]
+; LICM-LABEL: define void @loop_safe(
+; LICM:       entry:
+; LICM:         load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; LICM:       body:
+; LICM-NOT:     load i64
+; LICM:         ret void
+entry:
+  br label %body
+body:
+  %i = phi i32 [ 0, %entry ], [ %next, %body ]
+  store i32 0, ptr addrspace(1) getelementptr inbounds ([4 x i32], ptr addrspace(1) @g, i64 0, i64 0), align 4
+  store i32 %i, ptr addrspace(1) %a, align 4
+  %next = add i32 %i, 1
+  %c = icmp slt i32 %next, %n
+  br i1 %c, label %body, label %out
+out:
+  ret void
+}
+
+define void @two_blocks(ptr addrspace(1) %a, i1 %c) sanitize_device_address {
+; CHECK-LABEL: define void @two_blocks(
+; CHECK-COUNT-2: load i64, ptr addrspace(1) {{.*}}, !alias.scope
+; CHECK:         ret void
+entry:
+  br i1 %c, label %left, label %right
+left:
+  store i32 1, ptr addrspace(1) %a, align 4
+  ret void
+right:
+  store i32 2, ptr addrspace(1) %a, align 4
+  ret void
+}
+
+; RUN: opt < %s -passes=dasan -dasan-recover -S | FileCheck %s --check-prefix=REC
+; REC-LABEL: define i32 @load4(
+; REC:         notail call void @__dasan_report_load_noabort(
+; REC-NEXT:    br label %[[GO:.*]]
+; REC:       [[GO]]:
+; REC-NEXT:    %v = load i32, ptr addrspace(1) %a, align 4
+; REC-LABEL: define void @store4_unaligned(
+; REC:         call void @__dasan_report_store_noabort(
+; REC-NOT:     noreturn
+; REC:       attributes #{{[0-9]+}} = { cold nounwind }
+
+; RUN: opt < %s -passes=dasan -dasan-trap-on-error -S | FileCheck %s --check-prefix=TRAP
+; TRAP-LABEL: define i32 @load4(
+; TRAP:         %v = load i32, ptr addrspace(1) %a, align 4
+; TRAP:         call void @llvm.trap()
+; TRAP-NEXT:    unreachable
+; TRAP-NOT:   @__dasan_report
+
+; RUN: opt < %s -passes=dasan -dasan-recover -dasan-trap-on-error -S \
+; RUN:   | FileCheck %s --check-prefix=TRAP
+
+; CHECK: attributes #{{[0-9]+}} = { cold noreturn nounwind }
+; CHECK: attributes #[[SITE]] = { noinline nomerge }
+; CHECK: [[PROF:![0-9]+]] = !{!"branch_weights", i32 1, i32 1048575}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/debug.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/debug.ll
new file mode 100644
index 00000000000000..13cd7494939a68
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/debug.ll
@@ -0,0 +1,90 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+; RUN: opt < %s -passes=dasan -dasan-recover -S | FileCheck %s --check-prefix=REC
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+; The handler takes its return address as the location, so every check reports
+; through a call of its own carrying the location of its access.
+
+; CHECK-LABEL: define i32 @located
+; CHECK: notail call void @__dasan_report_load({{.*}}) #[[SITE:[0-9]+]], !dbg [[LOC:![0-9]+]]
+define i32 @located(ptr addrspace(1) %p) sanitize_device_address !dbg !4 {
+  %v = load i32, ptr addrspace(1) %p, align 4, !dbg !8
+  ret i32 %v, !dbg !8
+}
+
+; CHECK-LABEL: define i32 @unlocated
+; CHECK: notail call void @__dasan_report_load({{.*}}) #[[SITE]], !dbg [[SCOPE:![0-9]+]]
+define i32 @unlocated(ptr addrspace(1) %p) sanitize_device_address !dbg !9 {
+  %v = load i32, ptr addrspace(1) %p, align 4
+  ret i32 %v
+}
+
+; CHECK-LABEL: define i32 @nodebug
+; CHECK: notail call void @__dasan_report_load({{.*}}) #[[SITE]], {{.*}}!nosanitize
+define i32 @nodebug(ptr addrspace(1) %p) sanitize_device_address {
+  %v = load i32, ptr addrspace(1) %p, align 4
+  ret i32 %v
+}
+
+; Checks sharing a bound, or inlined from elsewhere, still get their own call.
+; CHECK-LABEL: define void @sites
+; CHECK: notail call void @__dasan_report_store({{.*}}) #[[SITE]], !dbg [[S1:![0-9]+]]
+; CHECK-NEXT: unreachable
+; CHECK: store i32 0, ptr addrspace(1) %a, align 4, !dbg [[S1]]
+; CHECK: notail call void @__dasan_report_store({{.*}}) #[[SITE]], !dbg [[S2:![0-9]+]]
+; CHECK-NEXT: unreachable
+; CHECK: store i32 1, ptr addrspace(1) %b, align 4, !dbg [[S2]]
+; CHECK: notail call void @__dasan_report_load({{.*}}) #[[SITE]], !dbg [[S3:![0-9]+]]
+; CHECK-NEXT: unreachable
+; CHECK: load i32, ptr addrspace(1) %c, align 4, !dbg [[S3]]
+; REC-LABEL: define void @sites
+; REC: notail call void @__dasan_report_store_noabort({{.*}}) #[[SITE:[0-9]+]], !dbg [[S1:![0-9]+]]
+; REC-NEXT: br label
+; REC: store i32 0, ptr addrspace(1) %a, align 4, !dbg [[S1]]
+; REC: notail call void @__dasan_report_store_noabort({{.*}}) #[[SITE]], !dbg [[S2:![0-9]+]]
+; REC-NEXT: br label
+; REC: store i32 1, ptr addrspace(1) %b, align 4, !dbg [[S2]]
+; REC: notail call void @__dasan_report_load_noabort({{.*}}) #[[SITE]], !dbg [[S3:![0-9]+]]
+; REC-NEXT: br label
+; REC: load i32, ptr addrspace(1) %c, align 4, !dbg [[S3]]
+define void @sites(ptr addrspace(1) %p, ptr addrspace(1) %q, i64 %i) sanitize_device_address !dbg !10 {
+  %a = getelementptr i32, ptr addrspace(1) %p, i64 %i
+  store i32 0, ptr addrspace(1) %a, align 4, !dbg !11
+  %b = getelementptr i32, ptr addrspace(1) %q, i64 %i
+  store i32 1, ptr addrspace(1) %b, align 4, !dbg !12
+  %j = add i64 %i, 8
+  %c = getelementptr i32, ptr addrspace(1) %p, i64 %j
+  %v = load i32, ptr addrspace(1) %c, align 4, !dbg !14
+  ret void
+}
+
+; CHECK: attributes #[[SITE]] = { noinline nomerge }
+; REC: attributes #[[SITE]] = { noinline nomerge }
+
+; CHECK-DAG: [[LOC]] = !DILocation(line: 7, column: 3, scope: !{{[0-9]+}})
+; CHECK-DAG: [[SCOPE]] = !DILocation(line: 11, scope: !{{[0-9]+}})
+; CHECK-DAG: [[S1]] = !DILocation(line: 21, column: 3, scope: !{{[0-9]+}})
+; CHECK-DAG: [[S2]] = !DILocation(line: 22, column: 3, scope: !{{[0-9]+}})
+; CHECK-DAG: [[S3]] = !DILocation(line: 31, column: 5, scope: !{{[0-9]+}}, inlinedAt: !{{[0-9]+}})
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
+!1 = !DIFile(filename: "t.c", directory: "/")
+!2 = !{i32 2, !"Debug Info Version", i32 3}
+!3 = !{i32 7, !"Dwarf Version", i32 5}
+!4 = distinct !DISubprogram(name: "located", scope: !1, file: !1, line: 6, type: !5, scopeLine: 6, spFlags: DISPFlagDefinition, unit: !0)
+!5 = !DISubroutineType(types: !6)
+!6 = !{!7}
+!7 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!8 = !DILocation(line: 7, column: 3, scope: !4)
+!9 = distinct !DISubprogram(name: "unlocated", scope: !1, file: !1, line: 10, type: !5, scopeLine: 11, spFlags: DISPFlagDefinition, unit: !0)
+!10 = distinct !DISubprogram(name: "sites", scope: !1, file: !1, line: 20, type: !5, scopeLine: 20, spFlags: DISPFlagDefinition, unit: !0)
+!11 = !DILocation(line: 21, column: 3, scope: !10)
+!12 = !DILocation(line: 22, column: 3, scope: !10)
+!13 = distinct !DISubprogram(name: "helper", scope: !1, file: !1, line: 30, type: !5, scopeLine: 30, spFlags: DISPFlagDefinition, unit: !0)
+!14 = !DILocation(line: 31, column: 5, scope: !13, inlinedAt: !15)
+!15 = distinct !DILocation(line: 23, column: 3, scope: !10)
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
new file mode 100644
index 00000000000000..683bb73a24f9ff
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
@@ -0,0 +1,12 @@
+; RUN: opt -passes='default<O0>' -S -mtriple=amdgcn-amd-amdhsa -amdgpu-internalize-symbols < %s | FileCheck %s
+; RUN: opt -passes='default<O1>' -S -mtriple=amdgcn-amd-amdhsa -amdgpu-internalize-symbols < %s | FileCheck %s
+
+; CHECK: define void @__dasan_report_load(
+define void @__dasan_report_load(ptr %p, i64 %size, i64 %base, i64 %limit) {
+  ret void
+}
+
+; CHECK: define weak void @__dasan_report_store_noabort(
+define weak void @__dasan_report_store_noabort(ptr %p, i64 %size, i64 %base, i64 %limit) {
+  ret void
+}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/globals.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/globals.ll
new file mode 100644
index 00000000000000..cc396b8ab8e58d
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/globals.ll
@@ -0,0 +1,123 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+; RUN: opt < %s -passes=dasan -S | FileCheck %s --check-prefix=ALIAS
+; RUN: opt < %s -passes=dasan -dasan-instrument-globals=0 -S | FileCheck %s --check-prefix=OFF
+
+; REQUIRES: amdgpu-registered-target
+; RUN: opt < %s -passes=dasan -S | llc -mtriple=amdgcn-amd-amdhsa -mcpu=gfx900 -o - | FileCheck %s --check-prefix=ASM
+; ASM: .section .sbss.dasan.globals,"awG", at nobits,odr,comdat
+; ASM: .size init, 16
+; ASM: .size __dasan_guard, 4032
+
+; RUN: opt < %s -passes=dasan,globalopt -S | FileCheck %s --check-prefix=FOLD
+; FOLD: @init = alias [4 x i32], ptr addrspace(1) @init.dasan
+; FOLD: @literal = internal alias [4 x i8], ptr addrspace(4) @literal.dasan
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+ at init = addrspace(1) global [4 x i32] [i32 1, i32 2, i32 3, i32 4], align 4
+; CHECK: @init.dasan = private addrspace(1) global { [4 x i32], [48 x i8] } { [4 x i32] [i32 1, i32 2, i32 3, i32 4], [48 x i8] zeroinitializer }, section ".data.dasan.globals", align 64
+; ALIAS: @init = alias [4 x i32], ptr addrspace(1) @init.dasan
+; ALIAS-NOT: @__dasan_guard = internal alias {{.*}}, getelementptr (i8, ptr addrspace(1) @init.dasan
+; OFF: @init = addrspace(1) global [4 x i32] [i32 1, i32 2, i32 3, i32 4], align 4
+; OFF-NOT: @__dasan_guard
+
+ at zero = addrspace(1) global [4 x i32] zeroinitializer, align 4
+; CHECK: @zero.dasan = private addrspace(1) global { [4 x i32], [48 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @zero = alias [4 x i32], ptr addrspace(1) @zero.dasan
+
+ at konst = addrspace(4) constant [2 x i32] [i32 7, i32 8], align 4
+; CHECK: @konst.dasan = private addrspace(4) constant { [2 x i32], [56 x i8] } { [2 x i32] [i32 7, i32 8], [56 x i8] zeroinitializer }, section ".rodata.dasan.globals", align 64
+; ALIAS: @konst = alias [2 x i32], ptr addrspace(4) @konst.dasan
+
+ at relro = addrspace(1) constant ptr addrspace(1) @init, align 8
+; CHECK: @relro.dasan = private addrspace(1) constant { ptr addrspace(1), [56 x i8] } { ptr addrspace(1) @init, [56 x i8] zeroinitializer }, section ".data.rel.ro.dasan.globals", align 64
+
+ at literal = private unnamed_addr addrspace(4) constant [4 x i8] c"abc\00", align 1
+; ALIAS: @literal = internal alias [4 x i8], ptr addrspace(4) @literal.dasan
+; OFF: @literal = private unnamed_addr addrspace(4) constant [4 x i8] c"abc\00", align 1
+
+ at overaligned = addrspace(1) global [4 x i32] zeroinitializer, align 4096
+; CHECK: @overaligned.dasan = private addrspace(1) global { [4 x i32], [4080 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 4096
+; ALIAS: @overaligned = alias [4 x i32], ptr addrspace(1) @overaligned.dasan
+; ALIAS-NEXT: @__dasan_guard = internal alias [4032 x i8], getelementptr (i8, ptr addrspace(1) @overaligned.dasan, i64 64)
+
+ at filled = addrspace(1) global [64 x i32] zeroinitializer, align 4
+; CHECK: @filled.dasan = private addrspace(1) global { [64 x i32], [64 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @filled = alias [64 x i32], ptr addrspace(1) @filled.dasan
+; ALIAS-NEXT: @__dasan_guard.1 = internal alias [64 x i8], getelementptr (i8, ptr addrspace(1) @filled.dasan, i64 256)
+
+ at wide = addrspace(1) global [256 x i32] zeroinitializer, align 4
+; CHECK: @wide.dasan = private addrspace(1) global { [256 x i32], [256 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @wide = alias [256 x i32], ptr addrspace(1) @wide.dasan
+; ALIAS-NEXT: @__dasan_guard.2 = internal alias [256 x i8], getelementptr (i8, ptr addrspace(1) @wide.dasan, i64 1024)
+
+ at slack = addrspace(1) global [63 x i32] zeroinitializer, align 4
+; CHECK: @slack.dasan = private addrspace(1) global { [63 x i32], [68 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @slack = alias [63 x i32], ptr addrspace(1) @slack.dasan
+; ALIAS-NEXT: @__dasan_guard.3 = internal alias [64 x i8], getelementptr (i8, ptr addrspace(1) @slack.dasan, i64 256)
+
+ at __omp_rtl_thing = addrspace(1) constant i32 1, align 4
+; CHECK: @__omp_rtl_thing = addrspace(1) constant i32 1, section ".rodata.dasan.globals", align 64
+; ALIAS-NOT: @__omp_rtl_thing =
+
+ at .str = private unnamed_addr addrspace(4) constant [4 x i8] c"abc\00", align 1
+; CHECK: @.str = internal unnamed_addr addrspace(4) constant [4 x i8] c"abc\00", section ".rodata.dasan.globals", align 64
+; ALIAS-NOT: @.str = {{.*}}alias
+
+$odr = comdat any
+ at odr = linkonce_odr addrspace(1) global [64 x i32] zeroinitializer, comdat, align 4
+; CHECK: @odr.dasan = private addrspace(1) global { [64 x i32], [64 x i8] } zeroinitializer, section ".sbss.dasan.globals", comdat($odr), align 64
+; ALIAS: @odr = linkonce_odr alias [64 x i32], ptr addrspace(1) @odr.dasan
+; ALIAS-NEXT: @__dasan_guard.4 = internal alias [64 x i8], getelementptr (i8, ptr addrspace(1) @odr.dasan, i64 256)
+; ASM: .size odr, 256
+
+ at weak = weak addrspace(1) global [64 x i32] zeroinitializer, align 4
+; CHECK: @weak.dasan = private addrspace(1) global { [64 x i32], [64 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @weak = weak alias [64 x i32], ptr addrspace(1) @weak.dasan
+; ALIAS-NEXT: @__dasan_guard.5 = internal alias [64 x i8], getelementptr (i8, ptr addrspace(1) @weak.dasan, i64 256)
+
+ at big = addrspace(1) global [4096 x i32] zeroinitializer, align 4
+; CHECK: @big.dasan = private addrspace(1) global { [4096 x i32], [4096 x i8] } zeroinitializer, section ".sbss.dasan.globals", align 64
+; ALIAS: @big = alias [4096 x i32], ptr addrspace(1) @big.dasan
+; ALIAS-NEXT: @__dasan_guard.6 = internal alias [4096 x i8], getelementptr (i8, ptr addrspace(1) @big.dasan, i64 16384)
+
+ at lds = addrspace(3) global [4 x i32] poison, align 4
+; CHECK: @lds = addrspace(3) global [4 x i32] poison, align 4
+
+ at ext = external addrspace(1) global [4 x i32]
+; CHECK: @ext = external addrspace(1) global [4 x i32]
+
+ at pinned = addrspace(1) global i32 0, section "mine", align 4
+; CHECK: @pinned = addrspace(1) global i32 0, section "mine", align 4
+
+ at vtable = addrspace(1) constant [2 x ptr] zeroinitializer, align 8, !type !0
+; CHECK: @vtable = addrspace(1) constant [2 x ptr] zeroinitializer, align 8
+
+; CHECK: @__dasan_no_entry = private addrspace(1) constant i64 0, align 8
+
+; CHECK: @llvm.compiler.used = appending addrspace(1) global {{.*}} @init to ptr), {{.*}} @init.dasan {{.*}} @overaligned.dasan {{.*}} @__dasan_guard {{.*}} @slack.dasan {{.*}} @__dasan_guard.3 {{.*}} @only.dasan
+; CHECK: @__dasan_instrumented = linkonce_odr protected addrspace(1) constant i8 1, align 64
+
+define void @use(ptr addrspace(1) %p) sanitize_device_address {
+  store i32 1, ptr addrspace(1) %p, align 4
+  ret void
+}
+
+define i32 @readconst(i64 %i) sanitize_device_address {
+; CHECK-LABEL: define i32 @readconst(
+; CHECK: [[P:%.*]] = getelementptr inbounds [2 x i32], ptr addrspace(4) @konst, i64 0, i64 %i
+; CHECK-NEXT: lshr i64 ptrtoint (ptr addrspace(4) @konst to i64), 45
+; CHECK: ptrtoint ptr addrspace(4) [[P]] to i64
+; CHECK: call void @__dasan_report_load(
+  %p = getelementptr inbounds [2 x i32], ptr addrspace(4) @konst, i64 0, i64 %i
+  %v = load i32, ptr addrspace(4) %p, align 4
+  ret i32 %v
+}
+
+; RUN: opt < %s -passes=dasan -dasan-instrument-writes=0 -S | FileCheck %s --check-prefix=MARK
+; MARK: @only.dasan = private addrspace(1) global { i32, [60 x i8] } { i32 1, [60 x i8] zeroinitializer }, section ".data.dasan.globals", align 64
+; MARK: @__dasan_instrumented
+ at only = addrspace(1) global i32 1, align 4
+
+!0 = !{i64 0, !"typeid"}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/layout.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/layout.ll
new file mode 100644
index 00000000000000..e3f2b8bf370c34
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/layout.ll
@@ -0,0 +1,30 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+; RUN: opt < %s -passes=dasan -dasan-instrument-globals -S | FileCheck %s --check-prefix=GLOBALS
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+ at g = global i32 1
+
+; CHECK: @g = global i32 1
+; CHECK-NOT: @g.dasan
+; CHECK: @__dasan_no_entry = private constant i64 0
+; CHECK-NOT: addrspace(
+
+; GLOBALS: @g.dasan = private global { i32, [60 x i8] } { i32 1, [60 x i8] zeroinitializer }, section ".data.dasan.globals", align 64
+; GLOBALS: @g = {{.*}}alias i32, ptr @g.dasan
+
+define i32 @load4(ptr %a) sanitize_device_address {
+; CHECK-LABEL: define i32 @load4(
+; CHECK: [[TOP:%.*]] = lshr i64 {{.*}}, 45
+; CHECK-NEXT: [[MINE:%.*]] = icmp eq i64 [[TOP]], 1
+; CHECK-NEXT: br i1 [[MINE]], label %[[SLOW:.*]], label %[[GO:.*]], !prof
+; CHECK: [[SLOW]]:
+; CHECK-NOT: select
+; CHECK: load i64, ptr {{.*}}, align 8, !alias.scope
+; CHECK: call void @__dasan_report_load(ptr %a, i64 4, i64 {{.*}}, i64 {{.*}})
+; CHECK: [[GO]]:
+; CHECK: ret i32
+  %v = load i32, ptr %a, align 4
+  ret i32 %v
+}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/marker.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/marker.ll
new file mode 100644
index 00000000000000..620713f52eafb7
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/marker.ll
@@ -0,0 +1,17 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+; CHECK: @__dasan_instrumented = linkonce_odr protected addrspace(1) constant i8 1
+
+define internal i32 @checked(ptr addrspace(1) %p) sanitize_device_address {
+  %v = load i32, ptr addrspace(1) %p, align 4
+  ret i32 %v
+}
+
+; RUN: opt < %s -passes=dasan -o %t.a.bc
+; RUN: opt < %s -passes=dasan -o %t.b.bc
+; RUN: llvm-link %t.a.bc %t.b.bc -S | FileCheck %s --check-prefix=LINKED
+; LINKED: @__dasan_instrumented = linkonce_odr protected addrspace(1) constant i8 1
+; LINKED-NOT: @__dasan_instrumented
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/module.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/module.ll
new file mode 100644
index 00000000000000..6cecf01362966f
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/module.ll
@@ -0,0 +1,17 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+; RUN: opt < %s -passes=dasan,dasan -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+; CHECK-NOT: @__dasan_no_entry
+; CHECK-NOT: @__dasan_instrumented
+; CHECK-NOT: @__dasan_report
+
+define void @nothing_to_check(i32 %x) sanitize_device_address {
+; CHECK-LABEL: define void @nothing_to_check(
+; CHECK-NEXT:    ret void
+  ret void
+}
+
+; CHECK: !{i32 4, !"nosanitize_device_address", i32 1}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/rooting.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/rooting.ll
new file mode 100644
index 00000000000000..2b7ae933ba13d4
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/rooting.ll
@@ -0,0 +1,68 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+; CHECK-LABEL: define void @elements(
+; CHECK:         ptrtoint ptr addrspace(1) %p to i64
+; CHECK:         load i64, ptr addrspace(1) {{%.*}}, align 8, !alias.scope
+; CHECK-NOT:     !alias.scope
+; CHECK:         ret void
+define void @elements(ptr addrspace(1) %p, i64 %i, i64 %j) sanitize_device_address {
+  %a = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %i
+  %b = getelementptr inbounds i32, ptr addrspace(1) %p, i64 %j
+  store i32 0, ptr addrspace(1) %a, align 4
+  store i32 1, ptr addrspace(1) %b, align 4
+  ret void
+}
+
+; CHECK-LABEL: define void @rows(
+; CHECK:         %row = getelementptr [4000 x i8], ptr addrspace(1) %p, i64 %r
+; CHECK:         [[B:%.*]] = ptrtoint ptr addrspace(1) %row to i64
+; CHECK-NOT:     ptrtoint ptr addrspace(1) %p to i64
+; CHECK:         ret void
+define void @rows(ptr addrspace(1) %p, i64 %r, i64 %i) sanitize_device_address {
+  %row = getelementptr [4000 x i8], ptr addrspace(1) %p, i64 %r
+  %e = getelementptr inbounds i32, ptr addrspace(1) %row, i64 %i
+  store i32 0, ptr addrspace(1) %e, align 4
+  ret void
+}
+
+; CHECK-LABEL: define void @field(
+; CHECK:         ptrtoint ptr addrspace(1) %s to i64
+; CHECK:         ret void
+define void @field(ptr addrspace(1) %s) sanitize_device_address {
+  %f = getelementptr inbounds { i32, [4000 x i8] }, ptr addrspace(1) %s, i64 0, i32 1
+  store i8 0, ptr addrspace(1) %f, align 1
+  ret void
+}
+
+; CHECK-LABEL: define void @wide(
+; CHECK:         ptrtoint ptr addrspace(1) %p to i64
+; CHECK:         ret void
+define void @wide(ptr addrspace(1) %p, i64 %i) sanitize_device_address {
+  %a = getelementptr inbounds <4 x float>, ptr addrspace(1) %p, i64 %i
+  store <4 x float> zeroinitializer, ptr addrspace(1) %a, align 16
+  ret void
+}
+
+; CHECK-LABEL: define void @rows2d(
+; CHECK:         %e = getelementptr [8 x [4000 x i8]], ptr addrspace(1) %p, i64 0, i64 %r, i64 %i
+; CHECK:         ptrtoint ptr addrspace(1) %e to i64
+; CHECK-NOT:     ptrtoint ptr addrspace(1) %p to i64
+define void @rows2d(ptr addrspace(1) %p, i64 %r, i64 %i) sanitize_device_address {
+  %e = getelementptr [8 x [4000 x i8]], ptr addrspace(1) %p, i64 0, i64 %r, i64 %i
+  store i8 0, ptr addrspace(1) %e, align 1
+  ret void
+}
+
+; CHECK-LABEL: define void @rows2d_split(
+; CHECK:         %row = getelementptr [8 x [4000 x i8]], ptr addrspace(1) %p, i64 0, i64 %r
+; CHECK:         ptrtoint ptr addrspace(1) %row to i64
+; CHECK-NOT:     ptrtoint ptr addrspace(1) %p to i64
+define void @rows2d_split(ptr addrspace(1) %p, i64 %r, i64 %i) sanitize_device_address {
+  %row = getelementptr [8 x [4000 x i8]], ptr addrspace(1) %p, i64 0, i64 %r
+  %e = getelementptr inbounds [4000 x i8], ptr addrspace(1) %row, i64 0, i64 %i
+  store i32 0, ptr addrspace(1) %e, align 4
+  ret void
+}
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/shared.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/shared.ll
new file mode 100644
index 00000000000000..f8c47de17f71bf
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/shared.ll
@@ -0,0 +1,282 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+; RUN: opt < %s -passes=dasan -dasan-instrument-shared=0 -S | FileCheck %s --check-prefix=OFF
+
+target datalayout = "e-p:64:64:64-p3:32:32-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "amdgcn-amd-amdhsa"
+
+ at vis = addrspace(3) global [4 x i32] poison
+; CHECK: @vis = addrspace(3) global [4 x i32] poison
+
+ at ext = weak addrspace(3) global [32 x i32] poison
+; CHECK: @ext = weak addrspace(3) global [32 x i32] poison
+
+ at dyn = internal addrspace(3) global [0 x i32] poison
+; CHECK: @dyn = internal addrspace(3) global [0 x i32] poison
+
+ at kept = internal addrspace(3) global [4 x i32] poison
+; CHECK: @kept = internal addrspace(3) global [4 x i32] poison
+
+ at llvm.used = appending addrspace(1) global [1 x ptr] [ptr addrspacecast (ptr addrspace(3) @kept to ptr)], section "llvm.metadata"
+
+ at a = internal addrspace(3) global [64 x i32] poison
+ at b = internal addrspace(3) global [16 x i32] poison
+ at slot = internal addrspace(3) global ptr addrspace(3) poison
+; CHECK: @a = internal addrspace(3) global [64 x i32] poison
+; CHECK: @b = internal addrspace(3) global [16 x i32] poison
+; CHECK-NOT: @__dasan_shared =
+; CHECK-NOT: @__dasan_lds_bound
+; OFF: @a = internal addrspace(3) global [64 x i32] poison
+; OFF: @b = internal addrspace(3) global [16 x i32] poison
+; OFF-NOT: @__dasan_lds_bound
+
+define void @in_bounds() sanitize_device_address {
+; CHECK-LABEL: define void @in_bounds()
+; CHECK-NEXT: store i32 1, ptr addrspace(3) @a
+; CHECK-NEXT: ret void
+  store i32 1, ptr addrspace(3) @a
+  ret void
+}
+
+define void @dynamic(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @dynamic(
+; CHECK: [[PKT:%.*]] = call {{.*}}@llvm.amdgcn.dispatch.ptr()
+; CHECK: [[GEP:%.*]] = getelementptr inbounds i8, ptr addrspace(4) [[PKT]], i64 28
+; CHECK: [[LIMIT:%.*]] = load i32, ptr addrspace(4) [[GEP]], align 4, !invariant.load
+; CHECK: icmp uge i32 {{.*}}, [[LIMIT]]
+; CHECK-NOT: ballot
+; CHECK: call void @__dasan_report_shared_store(
+; CHECK: store i32 1, ptr addrspace(3) %{{.*}}
+  %p = getelementptr [0 x i32], ptr addrspace(3) @dyn, i32 0, i32 %i
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+; The pass needs the dispatch packet for unsized LDS. Drop the attribute rather
+; than skipping the check.
+define void @dynamic_no_dispatch(i32 %i) sanitize_device_address "amdgpu-no-dispatch-ptr" {
+; CHECK-LABEL: define void @dynamic_no_dispatch(
+; CHECK-NOT: amdgpu-no-dispatch-ptr
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: call void @__dasan_report_shared_store(
+; CHECK: store i32 1, ptr addrspace(3) %{{.*}}
+  %p = getelementptr [0 x i32], ptr addrspace(3) @dyn, i32 0, i32 %i
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+define void @past_end_no_dispatch() sanitize_device_address "amdgpu-no-dispatch-ptr" {
+; CHECK-LABEL: define void @past_end_no_dispatch(
+; CHECK-NOT: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: call void @__dasan_report_shared_store(ptr {{.*}}, i64 4, i64 {{.*}}, i64 256)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 64
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+define void @flat_dyn(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @flat_dyn(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK-NOT: lshr i64 {{.*}}, 45
+; CHECK: call void @__dasan_report_shared_store(
+  %p = addrspacecast ptr addrspace(3) @dyn to ptr
+  %q = getelementptr i32, ptr %p, i32 %i
+  store i32 1, ptr %q
+  ret void
+}
+
+define void @past_end() sanitize_device_address {
+; CHECK-LABEL: define void @past_end()
+; CHECK: call void @__dasan_report_shared_store(ptr {{.*}}, i64 4, i64 {{.*}}, i64 256)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 64
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+; PHI: getUnderlyingObject is the PHI, so the check is the group-segment limit.
+define void @phi(i1 %c, i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @phi(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %p
+  br i1 %c, label %l, label %r
+l:
+  %pa = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 %i
+  br label %j
+r:
+  %pb = getelementptr [16 x i32], ptr addrspace(3) @b, i32 0, i32 %i
+  br label %j
+j:
+  %p = phi ptr addrspace(3) [ %pa, %l ], [ %pb, %r ]
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+; Load from private: no underlying global.
+define void @spill(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @spill(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %q
+  %s = alloca ptr addrspace(3), addrspace(5)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 %i
+  store ptr addrspace(3) %p, ptr addrspace(5) %s
+  %q = load ptr addrspace(3), ptr addrspace(5) %s
+  store i32 1, ptr addrspace(3) %q
+  ret void
+}
+
+define void @callee(ptr addrspace(3) %p) sanitize_device_address {
+; CHECK-LABEL: define void @callee(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %p
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+define void @caller() sanitize_device_address {
+; CHECK-LABEL: define void @caller()
+; CHECK: call void @callee(ptr addrspace(3) @a)
+  call void @callee(ptr addrspace(3) @a)
+  ret void
+}
+
+define void @weak_past() sanitize_device_address {
+; CHECK-LABEL: define void @weak_past()
+; CHECK: call void @__dasan_report_shared_store(ptr {{.*}}, i64 4, i64 {{.*}}, i64 128)
+  %p = getelementptr [32 x i32], ptr addrspace(3) @ext, i32 0, i32 32
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+; Decay does not hide the global from getUnderlyingObject.
+define void @flat_of_local(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @flat_of_local(
+; CHECK: icmp uge i32 {{.*}}, 256
+; CHECK-NOT: call i1 @llvm.amdgcn.is.shared
+; CHECK-NOT: lshr i64 {{.*}}, 45
+; CHECK: call void @__dasan_report_shared_store(
+  %p = addrspacecast ptr addrspace(3) @a to ptr
+  %q = getelementptr i32, ptr %p, i32 %i
+  store i32 1, ptr %q
+  ret void
+}
+
+define void @past_end_optnone() sanitize_device_address optnone noinline {
+; CHECK-LABEL: define void @past_end_optnone()
+; CHECK: call void @__dasan_report_shared_store(ptr {{.*}}, i64 4, i64 {{.*}}, i64 256)
+; CHECK: store i32 1, ptr addrspace(3) %p
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 64
+  store i32 1, ptr addrspace(3) %p
+  ret void
+}
+
+define void @spill_optnone(i32 %i) sanitize_device_address optnone noinline {
+; CHECK-LABEL: define void @spill_optnone(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %q
+  %s = alloca ptr addrspace(3), addrspace(5)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 %i
+  store ptr addrspace(3) %p, ptr addrspace(5) %s
+  %q = load ptr addrspace(3), ptr addrspace(5) %s
+  store i32 1, ptr addrspace(3) %q
+  ret void
+}
+
+define ptr addrspace(3) @gives() sanitize_device_address {
+; CHECK-LABEL: define ptr addrspace(3) @gives()
+; CHECK: ret ptr addrspace(3) @a
+  ret ptr addrspace(3) @a
+}
+
+declare void @sink(ptr addrspace(3))
+
+define void @escape_unsanitized() sanitize_device_address {
+; CHECK-LABEL: define void @escape_unsanitized()
+; CHECK: call void @sink(ptr addrspace(3) @a)
+  call void @sink(ptr addrspace(3) @a)
+  ret void
+}
+
+define void @callee_flat(ptr %p, i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @callee_flat(
+; CHECK-NOT: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK-NOT: call i1 @llvm.amdgcn.is.shared
+; CHECK: lshr i64 {{.*}}, 45
+  %q = getelementptr i32, ptr %p, i32 %i
+  store i32 1, ptr %q
+  ret void
+}
+
+define void @caller_flat(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @caller_flat(
+; CHECK: call void @callee_flat(ptr %p, i32 %i)
+  %p = addrspacecast ptr addrspace(3) @a to ptr
+  call void @callee_flat(ptr %p, i32 %i)
+  ret void
+}
+
+define void @callee_off(ptr %p) sanitize_device_address {
+; CHECK-LABEL: define void @callee_off(
+; CHECK-NOT: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: lshr i64 {{.*}}, 45
+  store i32 1, ptr %p
+  ret void
+}
+
+define void @caller_decayed_gep(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @caller_decayed_gep(
+; CHECK: call void @callee_off(ptr %q)
+  %p = addrspacecast ptr addrspace(3) @a to ptr
+  %q = getelementptr i32, ptr %p, i32 %i
+  call void @callee_off(ptr %q)
+  ret void
+}
+
+define amdgpu_kernel void @kernel_heap(ptr %out, i32 %i) sanitize_device_address {
+; CHECK-LABEL: define amdgpu_kernel void @kernel_heap(
+; CHECK-NOT: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK-NOT: call i1 @llvm.amdgcn.is.shared
+; CHECK: %p = getelementptr i32, ptr %out, i32 %i
+; CHECK: lshr i64 {{.*}}, 45
+  %p = getelementptr i32, ptr %out, i32 %i
+  store i32 1, ptr %p
+  ret void
+}
+
+define i1 @cmp_spill(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define i1 @cmp_spill(
+; CHECK: icmp eq ptr addrspace(3) %q, @a
+  %s = alloca ptr addrspace(3), addrspace(5)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 %i
+  store ptr addrspace(3) %p, ptr addrspace(5) %s
+  %q = load ptr addrspace(3), ptr addrspace(5) %s
+  %c = icmp eq ptr addrspace(3) %q, @a
+  ret i1 %c
+}
+
+define void @spill_to_lds(i32 %i) sanitize_device_address {
+; CHECK-LABEL: define void @spill_to_lds(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %q
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 %i
+  store ptr addrspace(3) %p, ptr addrspace(3) @slot
+  %q = load ptr addrspace(3), ptr addrspace(3) @slot
+  store i32 1, ptr addrspace(3) %q
+  ret void
+}
+
+; Escaped GEP of @a, then a load: the store is not a GEP of @a.
+define void @escaped_oob() sanitize_device_address {
+; CHECK-LABEL: define void @escaped_oob(
+; CHECK: call {{.*}}@llvm.amdgcn.dispatch.ptr
+; CHECK: store i32 1, ptr addrspace(3) %q
+  %s = alloca ptr addrspace(3), addrspace(5)
+  %p = getelementptr [64 x i32], ptr addrspace(3) @a, i32 0, i32 64
+  store ptr addrspace(3) %p, ptr addrspace(5) %s
+  %q = load ptr addrspace(3), ptr addrspace(5) %s
+  store i32 1, ptr addrspace(3) %q
+  ret void
+}
+
+; OFF-LABEL: define void @dynamic(
+; OFF-NEXT: %p = getelementptr
+; OFF-NEXT: store i32 1, ptr addrspace(3) %p
+; OFF-NEXT: ret void
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/wave.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/wave.ll
new file mode 100644
index 00000000000000..c9686d0d4aed0c
--- /dev/null
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/wave.ll
@@ -0,0 +1,19 @@
+; RUN: opt < %s -passes=dasan -S | FileCheck %s
+
+target datalayout = "e-p:64:64:64-i8:8:8-i16:16:16-i32:32:32-i64:64:64-n8:16:32:64-S128-A5-G1"
+target triple = "nvptx64-nvidia-cuda"
+
+; CHECK: @__dasan_no_entry = private addrspace(1) constant i64 0
+
+define i32 @load4(ptr addrspace(1) %a) sanitize_device_address {
+; CHECK-LABEL: define i32 @load4(
+; CHECK: lshr i64 {{.*}}, 45
+; CHECK: icmp eq i64 {{.*}}, 1
+; CHECK: inttoptr {{.*}} to ptr addrspace(1)
+; CHECK: call void @__dasan_report_load(
+; CHECK: ret i32
+  %v = load i32, ptr addrspace(1) %a, align 4
+  ret i32 %v
+}
+
+; CHECK-NOT: @{{.*}}.dasan
diff --git a/llvm/utils/gn/secondary/llvm/lib/Transforms/Instrumentation/BUILD.gn b/llvm/utils/gn/secondary/llvm/lib/Transforms/Instrumentation/BUILD.gn
index a735bf8301703b..d629590bb46f13 100644
--- a/llvm/utils/gn/secondary/llvm/lib/Transforms/Instrumentation/BUILD.gn
+++ b/llvm/utils/gn/secondary/llvm/lib/Transforms/Instrumentation/BUILD.gn
@@ -28,6 +28,7 @@ static_library("Instrumentation") {
     "ControlHeightReduction.cpp",
     "CopyProf.cpp",
     "DataFlowSanitizer.cpp",
+    "DeviceAddressSanitizer.cpp",
     "GCOVProfiling.cpp",
     "HWAddressSanitizer.cpp",
     "IndirectCallPromotion.cpp",

>From 5ce00eef4c19966ac90fd4aedc018ac0a2e6545c Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Sat, 14 Nov 2026 13:19:40 -0600
Subject: [PATCH 2/2] comments

---
 llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp  | 1 +
 .../DeviceAddressSanitizer/do-not-internalize.ll                | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
index b3035177fb4ce7..bf7e9f21689682 100644
--- a/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/DeviceAddressSanitizer.cpp
@@ -324,6 +324,7 @@ void DeviceAddressSanitizer::collect(Instruction &I,
   };
 
   // Collect every interesting instruction that touches memory.
+  // TODO: Handle other intrinsics with memory effects.
   if (auto *LI = dyn_cast<LoadInst>(&I))
     AddTyped(LI->getPointerOperand(), LI->getType(), LI->getAlign(), false);
   else if (auto *SI = dyn_cast<StoreInst>(&I))
diff --git a/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll b/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
index 683bb73a24f9ff..72742afde47112 100644
--- a/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
+++ b/llvm/test/Instrumentation/DeviceAddressSanitizer/do-not-internalize.ll
@@ -1,6 +1,8 @@
 ; RUN: opt -passes='default<O0>' -S -mtriple=amdgcn-amd-amdhsa -amdgpu-internalize-symbols < %s | FileCheck %s
 ; RUN: opt -passes='default<O1>' -S -mtriple=amdgcn-amd-amdhsa -amdgpu-internalize-symbols < %s | FileCheck %s
 
+; REQUIRES: amdgpu-registered-target
+
 ; CHECK: define void @__dasan_report_load(
 define void @__dasan_report_load(ptr %p, i64 %size, i64 %base, i64 %limit) {
   ret void



More information about the llvm-branch-commits mailing list