[llvm] [msan] handle modf, sincos and sincospi intrinsics (PR #220416)
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 1 22:11:22 PDT 2026
https://github.com/vitalybuka updated https://github.com/llvm/llvm-project/pull/220416
>From 23758d1d640097f463a1920e8218a7a805154dbf Mon Sep 17 00:00:00 2001
From: "ecg at google.com" <ecg at google.com>
Date: Tue, 1 Sep 2026 14:55:41 +0000
Subject: [PATCH] [msan] handle modf, sincos and sincospi intrinsics
These intrinsics compute two values from a single floating-point (or
vector of floating-point) operand, returning a 2-element struct {T,T}.
Because of the struct return type, they bypass the generic nomem
intrinsic heuristic and fall back to strict operand shadow checks in
visitInstruction. This causes false positives on vector operations with
inactive/masked lanes, and zeroes out the result shadow.
Fix this by propagating the operand shadow and origin to both elements
of the result struct.
---
.../Instrumentation/MemorySanitizer.cpp | 15 +++
.../MemorySanitizer/modf-sincos.ll | 119 ++++++++++++++++++
2 files changed, 134 insertions(+)
create mode 100644 llvm/test/Instrumentation/MemorySanitizer/modf-sincos.ll
diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 00c4cd89a73b2..0d520cb292218 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -5088,6 +5088,16 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
setOriginForNaryOp(I);
}
+ void handleModfOrSincos(IntrinsicInst &I) {
+ IRBuilder<> IRB(&I);
+ Value *ArgShadow = getShadow(&I, 0);
+ Value *Shadow = PoisonValue::get(getShadowTy(&I));
+ Shadow = IRB.CreateInsertValue(Shadow, ArgShadow, 0);
+ Shadow = IRB.CreateInsertValue(Shadow, ArgShadow, 1);
+ setShadow(&I, Shadow);
+ setOrigin(&I, getOrigin(&I, 0));
+ }
+
Value *extractLowerShadow(IRBuilder<> &IRB, Value *V) {
assert(isa<FixedVectorType>(V->getType()));
assert(cast<FixedVectorType>(V->getType())->getNumElements() > 0);
@@ -5872,6 +5882,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
case Intrinsic::smul_with_overflow:
handleArithmeticWithOverflow(I);
break;
+ case Intrinsic::modf:
+ case Intrinsic::sincos:
+ case Intrinsic::sincospi:
+ handleModfOrSincos(I);
+ break;
case Intrinsic::abs:
handleAbsIntrinsic(I);
break;
diff --git a/llvm/test/Instrumentation/MemorySanitizer/modf-sincos.ll b/llvm/test/Instrumentation/MemorySanitizer/modf-sincos.ll
new file mode 100644
index 0000000000000..bc546db711627
--- /dev/null
+++ b/llvm/test/Instrumentation/MemorySanitizer/modf-sincos.ll
@@ -0,0 +1,119 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt %s -S -passes=msan 2>&1 | FileCheck %s
+;
+; Test MemorySanitizer instrumentation of the llvm.modf / llvm.sincos /
+; llvm.sincospi intrinsics. The handler should propagate the argument's shadow,
+; unchanged, to both elements of the returned struct.
+
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define { float, float } @test_modf_f32(float %a) #0 {
+; CHECK-LABEL: define { float, float } @test_modf_f32(
+; CHECK-SAME: float [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i32, i32 } poison, i32 [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i32, i32 } [[TMP2]], i32 [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { float, float } @llvm.modf.f32(float [[A]])
+; CHECK-NEXT: store { i32, i32 } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { float, float } [[RES]]
+;
+ %res = call { float, float } @llvm.modf.f32(float %a)
+ ret { float, float } %res
+}
+
+define { <4 x float>, <4 x float> } @test_modf_v4f32(<4 x float> %a) #0 {
+; CHECK-LABEL: define { <4 x float>, <4 x float> } @test_modf_v4f32(
+; CHECK-SAME: <4 x float> [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { <4 x i32>, <4 x i32> } poison, <4 x i32> [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { <4 x i32>, <4 x i32> } [[TMP2]], <4 x i32> [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { <4 x float>, <4 x float> } @llvm.modf.v4f32(<4 x float> [[A]])
+; CHECK-NEXT: store { <4 x i32>, <4 x i32> } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { <4 x float>, <4 x float> } [[RES]]
+;
+ %res = call { <4 x float>, <4 x float> } @llvm.modf.v4f32(<4 x float> %a)
+ ret { <4 x float>, <4 x float> } %res
+}
+
+define { float, float } @test_sincos_f32(float %a) #0 {
+; CHECK-LABEL: define { float, float } @test_sincos_f32(
+; CHECK-SAME: float [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i32, i32 } poison, i32 [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i32, i32 } [[TMP2]], i32 [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { float, float } @llvm.sincos.f32(float [[A]])
+; CHECK-NEXT: store { i32, i32 } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { float, float } [[RES]]
+;
+ %res = call { float, float } @llvm.sincos.f32(float %a)
+ ret { float, float } %res
+}
+
+define { <8 x float>, <8 x float> } @test_sincos_v8f32(<8 x float> %a) #0 {
+; CHECK-LABEL: define { <8 x float>, <8 x float> } @test_sincos_v8f32(
+; CHECK-SAME: <8 x float> [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load <8 x i32>, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { <8 x i32>, <8 x i32> } poison, <8 x i32> [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { <8 x i32>, <8 x i32> } [[TMP2]], <8 x i32> [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { <8 x float>, <8 x float> } @llvm.sincos.v8f32(<8 x float> [[A]])
+; CHECK-NEXT: store { <8 x i32>, <8 x i32> } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { <8 x float>, <8 x float> } [[RES]]
+;
+ %res = call { <8 x float>, <8 x float> } @llvm.sincos.v8f32(<8 x float> %a)
+ ret { <8 x float>, <8 x float> } %res
+}
+
+define { double, double } @test_sincospi_f64(double %a) #0 {
+; CHECK-LABEL: define { double, double } @test_sincospi_f64(
+; CHECK-SAME: double [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load i64, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i64, i64 } poison, i64 [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i64, i64 } [[TMP2]], i64 [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { double, double } @llvm.sincospi.f64(double [[A]])
+; CHECK-NEXT: store { i64, i64 } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { double, double } [[RES]]
+;
+ %res = call { double, double } @llvm.sincospi.f64(double %a)
+ ret { double, double } %res
+}
+
+define { <4 x float>, <4 x float> } @test_sincospi_v4f32(<4 x float> %a) #0 {
+; CHECK-LABEL: define { <4 x float>, <4 x float> } @test_sincospi_v4f32(
+; CHECK-SAME: <4 x float> [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load <4 x i32>, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { <4 x i32>, <4 x i32> } poison, <4 x i32> [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { <4 x i32>, <4 x i32> } [[TMP2]], <4 x i32> [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { <4 x float>, <4 x float> } @llvm.sincospi.v4f32(<4 x float> [[A]])
+; CHECK-NEXT: store { <4 x i32>, <4 x i32> } [[TMP3]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret { <4 x float>, <4 x float> } [[RES]]
+;
+ %res = call { <4 x float>, <4 x float> } @llvm.sincospi.v4f32(<4 x float> %a)
+ ret { <4 x float>, <4 x float> } %res
+}
+
+define float @test_sincos_extract_sin(float %a) #0 {
+; CHECK-LABEL: define float @test_sincos_extract_sin(
+; CHECK-SAME: float [[A:%.*]]) #[[ATTR0]] {
+; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr @__msan_param_tls, align 8
+; CHECK-NEXT: call void @llvm.donothing()
+; CHECK-NEXT: [[TMP2:%.*]] = insertvalue { i32, i32 } poison, i32 [[TMP1]], 0
+; CHECK-NEXT: [[TMP3:%.*]] = insertvalue { i32, i32 } [[TMP2]], i32 [[TMP1]], 1
+; CHECK-NEXT: [[RES:%.*]] = call { float, float } @llvm.sincos.f32(float [[A]])
+; CHECK-NEXT: [[TMP4:%.*]] = extractvalue { i32, i32 } [[TMP3]], 0
+; CHECK-NEXT: [[SIN:%.*]] = extractvalue { float, float } [[RES]], 0
+; CHECK-NEXT: store i32 [[TMP4]], ptr @__msan_retval_tls, align 8
+; CHECK-NEXT: ret float [[SIN]]
+;
+ %res = call { float, float } @llvm.sincos.f32(float %a)
+ %sin = extractvalue { float, float } %res, 0
+ ret float %sin
+}
+
+attributes #0 = { sanitize_memory }
More information about the llvm-commits
mailing list