From c47c7435574d384bda3902a61921fbe680caeb81 Mon Sep 17 00:00:00 2001 From: Thurston Dang Date: Wed, 9 Jul 2025 16:40:15 +0000 Subject: [PATCH 1/2] [msan] Check mask and rounding mode in handleAVX512VectorConvertFPToInt The checks were missing in "Add handler for llvm.x86.avx512.mask.cvtps2dq.512 (https://github.com/llvm/llvm-project/pull/147377) --- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp | 4 +++- .../MemorySanitizer/X86/avx512-intrinsics.ll | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp index bb2eb99c00317..1a23705bad100 100644 --- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -4391,7 +4391,7 @@ struct MemorySanitizerVisitor : public InstVisitor { Value *A = I.getOperand(0); Value *WriteThrough = I.getOperand(1); Value *Mask = I.getOperand(2); - [[maybe_unused]] Value *RoundingMode = I.getOperand(3); + Value *RoundingMode = I.getOperand(3); assert(isa(A->getType())); assert(A->getType()->isFPOrFPVectorTy()); @@ -4406,8 +4406,10 @@ struct MemorySanitizerVisitor : public InstVisitor { assert(Mask->getType()->isIntegerTy()); assert(Mask->getType()->getScalarSizeInBits() == ANumElements); + insertCheckShadowOf(Mask, &I); assert(RoundingMode->getType()->isIntegerTy()); + insertCheckShadowOf(RoundingMode, &I); assert(I.getType() == WriteThrough->getType()); diff --git a/llvm/test/Instrumentation/MemorySanitizer/X86/avx512-intrinsics.ll b/llvm/test/Instrumentation/MemorySanitizer/X86/avx512-intrinsics.ll index 1b42396ff31d5..a5d387df59ff8 100644 --- a/llvm/test/Instrumentation/MemorySanitizer/X86/avx512-intrinsics.ll +++ b/llvm/test/Instrumentation/MemorySanitizer/X86/avx512-intrinsics.ll @@ -7941,6 +7941,7 @@ declare <16 x i32> @llvm.x86.avx512.mask.cvtps2dq.512(<16 x float>, <16 x i32>, define <16 x i32>@test_int_x86_avx512_mask_cvt_ps2dq_512(<16 x float> %x0, <16 x i32> %x1, i16 %x2) #0 { ; CHECK-LABEL: @test_int_x86_avx512_mask_cvt_ps2dq_512( +; CHECK-NEXT: [[TMP10:%.*]] = load i16, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_param_tls to i64), i64 128) to ptr), align 8 ; CHECK-NEXT: [[TMP1:%.*]] = load <16 x i32>, ptr @__msan_param_tls, align 8 ; CHECK-NEXT: [[TMP2:%.*]] = load <16 x i32>, ptr inttoptr (i64 add (i64 ptrtoint (ptr @__msan_param_tls to i64), i64 64) to ptr), align 8 ; CHECK-NEXT: call void @llvm.donothing() @@ -7948,6 +7949,12 @@ define <16 x i32>@test_int_x86_avx512_mask_cvt_ps2dq_512(<16 x float> %x0, <16 x ; CHECK-NEXT: [[TMP4:%.*]] = icmp ne <16 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: [[TMP5:%.*]] = sext <16 x i1> [[TMP4]] to <16 x i32> ; CHECK-NEXT: [[TMP6:%.*]] = select <16 x i1> [[TMP3]], <16 x i32> [[TMP5]], <16 x i32> [[TMP2]] +; CHECK-NEXT: [[_MSCMP:%.*]] = icmp ne i16 [[TMP10]], 0 +; CHECK-NEXT: br i1 [[_MSCMP]], label [[TMP11:%.*]], label [[TMP12:%.*]], !prof [[PROF1]] +; CHECK: 8: +; CHECK-NEXT: call void @__msan_warning_noreturn() #[[ATTR10]] +; CHECK-NEXT: unreachable +; CHECK: 9: ; CHECK-NEXT: [[RES:%.*]] = call <16 x i32> @llvm.x86.avx512.mask.cvtps2dq.512(<16 x float> [[X0:%.*]], <16 x i32> [[X1:%.*]], i16 [[X2]], i32 10) ; CHECK-NEXT: [[TMP7:%.*]] = icmp ne <16 x i32> [[TMP1]], zeroinitializer ; CHECK-NEXT: [[TMP8:%.*]] = sext <16 x i1> [[TMP7]] to <16 x i32> From b4b203b7c9f3679771ffa7d19d8a5fe3a302c1a0 Mon Sep 17 00:00:00 2001 From: Thurston Dang Date: Wed, 9 Jul 2025 19:06:28 +0000 Subject: [PATCH 2/2] Add comment about rounding mode --- llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp index 1a23705bad100..230d4735398c3 100644 --- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp @@ -4409,6 +4409,9 @@ struct MemorySanitizerVisitor : public InstVisitor { insertCheckShadowOf(Mask, &I); assert(RoundingMode->getType()->isIntegerTy()); + // Only four bits of the rounding mode are used, though it's very + // unusual to have uninitialized bits there (more commonly, it's a + // constant). insertCheckShadowOf(RoundingMode, &I); assert(I.getType() == WriteThrough->getType());