[llvm] [msan] Generalize handleIntrinsicByApplyingToShadow by adding bitcasting (PR #123474)
Thurston Dang via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 18 12:06:38 PST 2025
https://github.com/thurstond updated https://github.com/llvm/llvm-project/pull/123474
>From 9d64d8ba4550ba1a42c2c7398931a00ba5d61d08 Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Sat, 18 Jan 2025 19:43:33 +0000
Subject: [PATCH 1/2] [msan] Generalize handleIntrinsicByApplyingToShadow by
adding bitcasting
handleIntrinsicByApplyingToShadow (introduced in
https://github.com/llvm/llvm-project/pull/114490) requires that the
intrinsic supports integer-ish operands; this is not the case for all
intrinsics. This patch generalizes the function to bitcast the shadow
arguments to be the same type as the original intrinsic, thus
guaranteeing that the intrinsic exists. Additionally, it casts the
computed shadow to be an appropriate shadow type.
This function assumes that the intrinsic will handle arbitrary bit-patterns (for example, if the intrinsic only accepts floats
for var1, we require that it doesn't care if inputs are NaNs).
---
.../Transforms/Instrumentation/MemorySanitizer.cpp | 12 ++++++++++--
1 file changed, 10 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 6daee7a3b6e815..7b5a3fa4066a70 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -4008,6 +4008,10 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
/// shadow[out] =
/// intrinsic(shadow[var1], shadow[var2], opType) | shadow[opType]
///
+ /// CAUTION: this assumes that the intrinsic will handle arbitrary
+ /// bit-patterns (for example, if the intrinsic only accepts floats
+ /// for var1, we require that it doesn't care if inputs are NaNs).
+ ///
/// For example, this can be applied to the Arm NEON vector table intrinsics
/// (tbl{1,2,3,4}).
///
@@ -4022,7 +4026,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
// Don't use getNumOperands() because it includes the callee
for (unsigned int i = 0; i < I.arg_size() - trailingVerbatimArgs; i++) {
Value *Shadow = getShadow(&I, i);
- ShadowArgs.push_back(Shadow);
+
+ // Shadows are integer-ish types but some intrinsics require a
+ // different (e.g., floating-point) type.
+ ShadowArgs.push_back(
+ IRB.CreateBitCast(Shadow, I.getArgOperand(i)->getType()));
}
for (unsigned int i = I.arg_size() - trailingVerbatimArgs; i < I.arg_size();
@@ -4043,7 +4051,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
CombinedShadow = IRB.CreateOr(Shadow, CombinedShadow, "_msprop");
}
- setShadow(&I, CombinedShadow);
+ setShadow(&I, IRB.CreateBitCast(CombinedShadow, getShadowTy(&I)));
setOriginForNaryOp(I);
}
>From f5d688cec26286f4a4f45c5e89f473ed8c520f0b Mon Sep 17 00:00:00 2001
From: Thurston Dang <thurston at google.com>
Date: Sat, 18 Jan 2025 20:06:09 +0000
Subject: [PATCH 2/2] Fix comment wording
---
llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
index 7b5a3fa4066a70..0b63c2059268cd 100644
--- a/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
@@ -4009,8 +4009,8 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
/// intrinsic(shadow[var1], shadow[var2], opType) | shadow[opType]
///
/// CAUTION: this assumes that the intrinsic will handle arbitrary
- /// bit-patterns (for example, if the intrinsic only accepts floats
- /// for var1, we require that it doesn't care if inputs are NaNs).
+ /// bit-patterns (for example, if the intrinsic accepts floats for
+ /// var1, we require that it doesn't care if inputs are NaNs).
///
/// For example, this can be applied to the Arm NEON vector table intrinsics
/// (tbl{1,2,3,4}).
More information about the llvm-commits
mailing list