-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[msan] Generalize handlePairwiseShadowOrIntrinsic, and handle x86 pairwise add/sub #127567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0930461
[msan] Generalize handlePairwiseShadowOrIntrinsic, and handle x86 pai…
thurstond 3aaf7de
Simplify assertions
thurstond 9585fd7
Address Florian's feedback
thurstond 32b6431
Address Vitaly's feedback - split into base change
thurstond fd03942
Revert "Address Vitaly's feedback - split into base change"
thurstond 648e84c
Fork handlePairwiseShadowOrIntrinsic
thurstond d13c617
clang-format
thurstond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2608,8 +2608,15 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| /// e.g., <2 x i32> @llvm.aarch64.neon.saddlp.v2i32.v4i16(<4 x i16>) | ||
| /// <16 x i8> @llvm.aarch64.neon.addp.v16i8(<16 x i8>, <16 x i8>) | ||
| /// | ||
| /// TODO: adapt this function to handle horizontal add/sub? | ||
| void handlePairwiseShadowOrIntrinsic(IntrinsicInst &I) { | ||
| /// Optionally, reinterpret the parameters to have elements of a specified | ||
| /// width. For example: | ||
| /// @llvm.x86.ssse3.phadd.w(<1 x i64> [[VAR1]], <1 x i64> [[VAR2]]) | ||
| /// conceptually operates on | ||
| /// (<4 x i16> [[VAR1]], <4 x i16> [[VAR2]]) | ||
| /// and can be handled with ReinterpretElemWidth == 16. | ||
| void | ||
| handlePairwiseShadowOrIntrinsic(IntrinsicInst &I, | ||
| std::optional<int> ReinterpretElemWidth) { | ||
| assert(I.arg_size() == 1 || I.arg_size() == 2); | ||
|
|
||
| assert(I.getType()->isVectorTy()); | ||
|
|
@@ -2618,28 +2625,54 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| FixedVectorType *ParamType = | ||
| cast<FixedVectorType>(I.getArgOperand(0)->getType()); | ||
| if (I.arg_size() == 2) | ||
|
||
| assert(ParamType == cast<FixedVectorType>(I.getArgOperand(1)->getType())); | ||
| assert(I.getArgOperand(0)->getType() == I.getArgOperand(1)->getType()); | ||
|
|
||
| [[maybe_unused]] FixedVectorType *ReturnType = | ||
| cast<FixedVectorType>(I.getType()); | ||
| assert(ParamType->getNumElements() * I.arg_size() == | ||
| 2 * ReturnType->getNumElements()); | ||
|
|
||
| IRBuilder<> IRB(&I); | ||
| unsigned Width = ParamType->getNumElements() * I.arg_size(); | ||
|
|
||
| unsigned TotalNumElems = ParamType->getNumElements() * I.arg_size(); | ||
| FixedVectorType *ReinterpretShadowTy = nullptr; | ||
| if (ReinterpretElemWidth.has_value()) { | ||
| assert(isAligned(Align(*ReinterpretElemWidth), | ||
| ParamType->getPrimitiveSizeInBits())); | ||
| ReinterpretShadowTy = FixedVectorType::get( | ||
| IRB.getIntNTy(*ReinterpretElemWidth), | ||
| ParamType->getPrimitiveSizeInBits() / *ReinterpretElemWidth); | ||
| TotalNumElems = ReinterpretShadowTy->getNumElements() * I.arg_size(); | ||
| } | ||
|
|
||
| // Horizontal OR of shadow | ||
| SmallVector<int, 8> EvenMask; | ||
| SmallVector<int, 8> OddMask; | ||
| for (unsigned X = 0; X < Width; X += 2) { | ||
| for (unsigned X = 0; X < TotalNumElems - 1; X += 2) { | ||
| EvenMask.push_back(X); | ||
| OddMask.push_back(X + 1); | ||
| } | ||
|
|
||
| Value *FirstArgShadow = getShadow(&I, 0); | ||
| if (ReinterpretShadowTy) | ||
| FirstArgShadow = IRB.CreateBitCast(FirstArgShadow, ReinterpretShadowTy); | ||
|
|
||
| // If we had two parameters each with an odd number of elements, the total | ||
| // number of elements is even, but we have never seen this in extant | ||
| // instruction sets, so we enforce that each parameter must have an even | ||
| // number of elements. | ||
| assert(isAligned( | ||
| Align(2), | ||
| cast<FixedVectorType>(FirstArgShadow->getType())->getNumElements())); | ||
|
|
||
| Value *EvenShadow; | ||
| Value *OddShadow; | ||
| if (I.arg_size() == 2) { | ||
| Value *SecondArgShadow = getShadow(&I, 1); | ||
| if (ReinterpretShadowTy) | ||
| SecondArgShadow = | ||
| IRB.CreateBitCast(SecondArgShadow, ReinterpretShadowTy); | ||
|
|
||
| EvenShadow = | ||
| IRB.CreateShuffleVector(FirstArgShadow, SecondArgShadow, EvenMask); | ||
| OddShadow = | ||
|
|
@@ -4156,87 +4189,6 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| setOriginForNaryOp(I); | ||
| } | ||
|
|
||
| void handleAVXHorizontalAddSubIntrinsic(IntrinsicInst &I) { | ||
| // Approximation only: | ||
| // output = horizontal_add/sub(A, B) | ||
| // => shadow[output] = horizontal_add(shadow[A], shadow[B]) | ||
| // | ||
| // We always use horizontal add instead of subtract, because subtracting | ||
| // a fully uninitialized shadow would result in a fully initialized shadow. | ||
| // | ||
| // - If we add two adjacent zero (initialized) shadow values, the | ||
| // result always be zero i.e., no false positives. | ||
| // - If we add two shadows, one of which is uninitialized, the | ||
| // result will always be non-zero i.e., no false negatives. | ||
| // - However, we can have false negatives if we do an addition that wraps | ||
| // to zero; we consider this an acceptable tradeoff for performance. | ||
| // | ||
| // To make shadow propagation precise, we want the equivalent of | ||
| // "horizontal OR", but this is not available for SSE3/SSSE3/AVX/AVX2. | ||
|
|
||
| Intrinsic::ID shadowIntrinsicID = I.getIntrinsicID(); | ||
|
|
||
| switch (I.getIntrinsicID()) { | ||
| case Intrinsic::x86_sse3_hsub_ps: | ||
| shadowIntrinsicID = Intrinsic::x86_sse3_hadd_ps; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_sse3_hsub_pd: | ||
| shadowIntrinsicID = Intrinsic::x86_sse3_hadd_pd; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_d: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_d; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_d_128: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_d_128; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_w: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_w; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_w_128: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_w_128; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_sw: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_sw; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_ssse3_phsub_sw_128: | ||
| shadowIntrinsicID = Intrinsic::x86_ssse3_phadd_sw_128; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_avx_hsub_pd_256: | ||
| shadowIntrinsicID = Intrinsic::x86_avx_hadd_pd_256; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_avx_hsub_ps_256: | ||
| shadowIntrinsicID = Intrinsic::x86_avx_hadd_ps_256; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_avx2_phsub_d: | ||
| shadowIntrinsicID = Intrinsic::x86_avx2_phadd_d; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_avx2_phsub_w: | ||
| shadowIntrinsicID = Intrinsic::x86_avx2_phadd_w; | ||
| break; | ||
|
|
||
| case Intrinsic::x86_avx2_phsub_sw: | ||
| shadowIntrinsicID = Intrinsic::x86_avx2_phadd_sw; | ||
| break; | ||
|
|
||
| default: | ||
| break; | ||
| } | ||
|
|
||
| return handleIntrinsicByApplyingToShadow(I, shadowIntrinsicID, | ||
| /*trailingVerbatimArgs*/ 0); | ||
| } | ||
|
|
||
| /// Handle Arm NEON vector store intrinsics (vst{2,3,4}, vst1x_{2,3,4}, | ||
| /// and vst{2,3,4}lane). | ||
| /// | ||
|
|
@@ -4783,33 +4735,49 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| handleVtestIntrinsic(I); | ||
| break; | ||
|
|
||
| case Intrinsic::x86_sse3_hadd_ps: | ||
| case Intrinsic::x86_sse3_hadd_pd: | ||
| case Intrinsic::x86_ssse3_phadd_d: | ||
| case Intrinsic::x86_ssse3_phadd_d_128: | ||
| // Packed Horizontal Add/Subtract | ||
| case Intrinsic::x86_ssse3_phadd_w: | ||
| case Intrinsic::x86_ssse3_phadd_w_128: | ||
| case Intrinsic::x86_avx2_phadd_w: | ||
| case Intrinsic::x86_ssse3_phsub_w: | ||
| case Intrinsic::x86_ssse3_phsub_w_128: | ||
| case Intrinsic::x86_avx2_phsub_w: { | ||
| handlePairwiseShadowOrIntrinsic(I, /*ReinterpretElemWidth=*/16); | ||
| break; | ||
| } | ||
|
|
||
| // Packed Horizontal Add/Subtract | ||
| case Intrinsic::x86_ssse3_phadd_d: | ||
| case Intrinsic::x86_ssse3_phadd_d_128: | ||
| case Intrinsic::x86_avx2_phadd_d: | ||
| case Intrinsic::x86_ssse3_phsub_d: | ||
| case Intrinsic::x86_ssse3_phsub_d_128: | ||
| case Intrinsic::x86_avx2_phsub_d: { | ||
| handlePairwiseShadowOrIntrinsic(I, /*ReinterpretElemWidth=*/32); | ||
| break; | ||
| } | ||
|
|
||
| // Packed Horizontal Add/Subtract and Saturate | ||
| case Intrinsic::x86_ssse3_phadd_sw: | ||
| case Intrinsic::x86_ssse3_phadd_sw_128: | ||
| case Intrinsic::x86_avx2_phadd_sw: | ||
| case Intrinsic::x86_ssse3_phsub_sw: | ||
| case Intrinsic::x86_ssse3_phsub_sw_128: | ||
| case Intrinsic::x86_avx2_phsub_sw: { | ||
| handlePairwiseShadowOrIntrinsic(I, /*ReinterpretElemWidth=*/16); | ||
| break; | ||
| } | ||
|
|
||
| // Packed Single/Double Precision Floating-Point Horizontal Add | ||
| case Intrinsic::x86_sse3_hadd_ps: | ||
| case Intrinsic::x86_sse3_hadd_pd: | ||
| case Intrinsic::x86_avx_hadd_pd_256: | ||
| case Intrinsic::x86_avx_hadd_ps_256: | ||
| case Intrinsic::x86_avx2_phadd_d: | ||
| case Intrinsic::x86_avx2_phadd_w: | ||
| case Intrinsic::x86_avx2_phadd_sw: | ||
| case Intrinsic::x86_sse3_hsub_ps: | ||
| case Intrinsic::x86_sse3_hsub_pd: | ||
| case Intrinsic::x86_ssse3_phsub_d: | ||
| case Intrinsic::x86_ssse3_phsub_d_128: | ||
| case Intrinsic::x86_ssse3_phsub_w: | ||
| case Intrinsic::x86_ssse3_phsub_w_128: | ||
| case Intrinsic::x86_ssse3_phsub_sw: | ||
| case Intrinsic::x86_ssse3_phsub_sw_128: | ||
| case Intrinsic::x86_avx_hsub_pd_256: | ||
| case Intrinsic::x86_avx_hsub_ps_256: | ||
| case Intrinsic::x86_avx2_phsub_d: | ||
| case Intrinsic::x86_avx2_phsub_w: | ||
| case Intrinsic::x86_avx2_phsub_sw: { | ||
| handleAVXHorizontalAddSubIntrinsic(I); | ||
| case Intrinsic::x86_avx_hsub_ps_256: { | ||
| handlePairwiseShadowOrIntrinsic(I, /*ReinterpretElemWidth=*/std::nullopt); | ||
| break; | ||
| } | ||
|
|
||
|
|
@@ -4869,7 +4837,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> { | |
| // Add Long Pairwise | ||
| case Intrinsic::aarch64_neon_saddlp: | ||
| case Intrinsic::aarch64_neon_uaddlp: { | ||
| handlePairwiseShadowOrIntrinsic(I); | ||
| handlePairwiseShadowOrIntrinsic(I, std::nullopt); | ||
| break; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To many changes, could be better to fork this function for ReinterpretElemWidth case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done