Skip to content

[msan] Add handlePairwiseShadowOrIntrinsic and use it to handle Arm NEON pairwise add #126008

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 3 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2602,6 +2602,59 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
SC.Done(&I);
}

/// Propagate shadow for 1- or 2-vector intrinsics that combine adjacent
/// fields.
///
/// 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) {
assert(I.arg_size() == 1 || I.arg_size() == 2);

assert(I.getType()->isVectorTy());
assert(I.getArgOperand(0)->getType()->isVectorTy());

FixedVectorType *ParamType =
cast<FixedVectorType>(I.getArgOperand(0)->getType());
if (I.arg_size() == 2)
assert(ParamType == cast<FixedVectorType>(I.getArgOperand(1)->getType()));
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();

// Horizontal OR of shadow
SmallVector<int, 8> EvenMask;
SmallVector<int, 8> OddMask;
for (unsigned X = 0; X < Width; X += 2) {
EvenMask.push_back(X);
OddMask.push_back(X + 1);
}

Value *FirstArgShadow = getShadow(&I, 0);
Value *EvenShadow;
Value *OddShadow;
if (I.arg_size() == 2) {
Value *SecondArgShadow = getShadow(&I, 1);
EvenShadow =
IRB.CreateShuffleVector(FirstArgShadow, SecondArgShadow, EvenMask);
OddShadow =
IRB.CreateShuffleVector(FirstArgShadow, SecondArgShadow, OddMask);
} else {
EvenShadow = IRB.CreateShuffleVector(FirstArgShadow, EvenMask);
OddShadow = IRB.CreateShuffleVector(FirstArgShadow, OddMask);
}

Value *OrShadow = IRB.CreateOr(EvenShadow, OddShadow);
OrShadow = CreateShadowCast(IRB, OrShadow, getShadowTy(&I));

setShadow(&I, OrShadow);
setOriginForNaryOp(I);
}

void visitFNeg(UnaryOperator &I) { handleShadowOr(I); }

// Handle multiplication by constant.
Expand Down Expand Up @@ -4781,6 +4834,17 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
setOrigin(&I, getCleanOrigin());
break;

// Add Pairwise
case Intrinsic::aarch64_neon_addp:
// Floating-point Add Pairwise
case Intrinsic::aarch64_neon_faddp:
// Add Long Pairwise
case Intrinsic::aarch64_neon_saddlp:
case Intrinsic::aarch64_neon_uaddlp: {
handlePairwiseShadowOrIntrinsic(I);
break;
}

case Intrinsic::aarch64_neon_st1x2:
case Intrinsic::aarch64_neon_st1x3:
case Intrinsic::aarch64_neon_st1x4:
Expand Down
Loading