Skip to content

Commit b07a0cf

Browse files
authored
[DevMSAN] Always make sure poison shuffle mask point to clean shadow (#18191)
For Spirv target, we need to make sure poison mask always point to a clean shadow to avoid pollution of launch info clean shadow. Signed-off-by: Zhao, Maosu <[email protected]>
1 parent 4c1557f commit b07a0cf

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3152,8 +3152,24 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
31523152
IRBuilder<> IRB(&I);
31533153
auto *Shadow0 = getShadow(&I, 0);
31543154
auto *Shadow1 = getShadow(&I, 1);
3155-
setShadow(&I, IRB.CreateShuffleVector(Shadow0, Shadow1, I.getShuffleMask(),
3156-
"_msprop"));
3155+
// For Spirv target, we need to make sure poison mask always point to a
3156+
// clean shadow to avoid pollution of launch info clean shadow.
3157+
if (SpirOrSpirv &&
3158+
any_of(I.getShuffleMask(), [](int Mask) { return Mask == -1; })) {
3159+
auto *V = IRB.CreateShuffleVector(Shadow0, Shadow1, I.getShuffleMask(),
3160+
"_msprop");
3161+
auto *Shadow2 = getCleanShadow(V);
3162+
SmallVector<int, 4> NewMask;
3163+
I.getShuffleMask(NewMask);
3164+
for (auto &Mask : NewMask) {
3165+
if (Mask == -1)
3166+
Mask = NewMask.size() + 1;
3167+
}
3168+
setShadow(&I, IRB.CreateShuffleVector(V, Shadow2, NewMask, "_msprop"));
3169+
} else {
3170+
setShadow(&I, IRB.CreateShuffleVector(Shadow0, Shadow1,
3171+
I.getShuffleMask(), "_msprop"));
3172+
}
31573173
setOriginForNaryOp(I);
31583174
}
31593175

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: opt < %s -passes=msan -msan-instrumentation-with-call-threshold=0 -msan-eager-checks=1 -S | FileCheck %s
2+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1"
3+
target triple = "spir64-unknown-unknown"
4+
5+
define spir_kernel void @MyKernel(ptr addrspace(1) noundef align 4 %_arg_array) sanitize_memory {
6+
entry:
7+
%arrayidx3 = getelementptr inbounds <3 x i16>, ptr addrspace(1) %_arg_array, i64 0
8+
%extractVec4 = shufflevector <3 x i16> <i16 0, i16 0, i16 0>, <3 x i16> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 poison>
9+
; CHECK: [[REG1:%[0-9]+]] = call i64 @__msan_get_shadow
10+
; CHECK: [[REG2:%[0-9]+]] = inttoptr i64 [[REG1]] to ptr addrspace(1)
11+
; CHECK: store <4 x i16> zeroinitializer, ptr addrspace(1) [[REG2]]
12+
store <4 x i16> %extractVec4, ptr addrspace(1) %arrayidx3, align 8
13+
br label %exit
14+
15+
exit:
16+
ret void
17+
}

0 commit comments

Comments
 (0)