Skip to content

Commit dd2759e

Browse files
phoebewangnikic
authored andcommitted
[msan] Fix bugs when instrument x86.avx512*_cvt* intrinsics.
Scalar intrinsics x86.avx512*_cvt* have an extra rounding mode operand. We can directly ignore it to reuse the SSE/AVX math. This fix the bug https://bugs.llvm.org/show_bug.cgi?id=48298. Reviewed By: craig.topper Differential Revision: https://reviews.llvm.org/D92206
1 parent 7ade8dc commit dd2759e

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -2635,14 +2635,16 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
26352635
// We copy the shadow of \p CopyOp[NumUsedElements:] to \p
26362636
// Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always
26372637
// return a fully initialized value.
2638-
void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements) {
2638+
void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements,
2639+
bool HasRoundingMode = false) {
26392640
IRBuilder<> IRB(&I);
26402641
Value *CopyOp, *ConvertOp;
26412642

2642-
switch (I.getNumArgOperands()) {
2643-
case 3:
2644-
assert(isa<ConstantInt>(I.getArgOperand(2)) && "Invalid rounding mode");
2645-
LLVM_FALLTHROUGH;
2643+
assert((!HasRoundingMode ||
2644+
isa<ConstantInt>(I.getArgOperand(I.getNumArgOperands() - 1))) &&
2645+
"Invalid rounding mode");
2646+
2647+
switch (I.getNumArgOperands() - HasRoundingMode) {
26462648
case 2:
26472649
CopyOp = I.getArgOperand(0);
26482650
ConvertOp = I.getArgOperand(1);
@@ -3179,6 +3181,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
31793181
case Intrinsic::x86_avx512_cvtusi2ss:
31803182
case Intrinsic::x86_avx512_cvtusi642sd:
31813183
case Intrinsic::x86_avx512_cvtusi642ss:
3184+
handleVectorConvertIntrinsic(I, 1, true);
3185+
break;
31823186
case Intrinsic::x86_sse2_cvtsd2si64:
31833187
case Intrinsic::x86_sse2_cvtsd2si:
31843188
case Intrinsic::x86_sse2_cvtsd2ss:

llvm/test/Instrumentation/MemorySanitizer/vector_cvt.ll

+18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ target triple = "x86_64-unknown-linux-gnu"
99
declare i32 @llvm.x86.sse2.cvtsd2si(<2 x double>) nounwind readnone
1010
declare <2 x double> @llvm.x86.sse2.cvtsi2sd(<2 x double>, i32) nounwind readnone
1111
declare x86_mmx @llvm.x86.sse.cvtps2pi(<4 x float>) nounwind readnone
12+
declare i32 @llvm.x86.avx512.vcvtss2usi32(<4 x float>, i32) nounwind readnone
1213

1314
; Single argument vector conversion.
1415

@@ -45,3 +46,20 @@ entry:
4546
; CHECK: call x86_mmx @llvm.x86.sse.cvtps2pi
4647
; CHECK: store i64 0, {{.*}} @__msan_retval_tls
4748
; CHECK: ret x86_mmx
49+
50+
; avx512 rounding conversion.
51+
52+
define i32 @pr48298(<4 x float> %value) sanitize_memory {
53+
entry:
54+
%0 = tail call i32 @llvm.x86.avx512.vcvtss2usi32(<4 x float> %value, i32 11)
55+
ret i32 %0
56+
}
57+
58+
; CHECK-LABEL: @pr48298
59+
; CHECK: extractelement <4 x i32> {{.*}}, i32 0
60+
; CHECK: icmp ne i32 {{.*}}, 0
61+
; CHECK: br
62+
; CHECK: call void @__msan_warning_with_origin_noreturn
63+
; CHECK: call i32 @llvm.x86.avx512.vcvtss2usi32
64+
; CHECK: store i32 0, {{.*}} @__msan_retval_tls
65+
; CHECK: ret i32

0 commit comments

Comments
 (0)