Skip to content

Commit d985b19

Browse files
committed
[NFCI][msan] Extract 'shrinkVectorShadow' and 'extendVectorShadowWithZeros' into helper functions
These functions will be useful in other intrinsic handlers.
1 parent 6b2cd08 commit d985b19

File tree

1 file changed

+54
-20
lines changed

1 file changed

+54
-20
lines changed

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3389,6 +3389,58 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
33893389
setOriginForNaryOp(I);
33903390
}
33913391

3392+
/// Some instructions have additional zero-elements in the return type
3393+
/// e.g., <16 x i8> @llvm.x86.avx512.mask.pmov.qb.512(<8 x i64>, ...)
3394+
///
3395+
/// This function will return a vector type with the same number of elements
3396+
/// as the input, but same width as the return value e.g., <8 x i8>.
3397+
FixedVectorType *shrinkVectorShadow(Value *Src, IntrinsicInst &I) {
3398+
assert(isa<FixedVectorType>(getShadowTy(&I)));
3399+
// The return type might have more elements than the input.
3400+
// Temporarily shrink the return type's number of elements.
3401+
FixedVectorType *ShadowType = cast<FixedVectorType>(getShadowTy(&I));
3402+
3403+
// TODO: generalize beyond 2x?
3404+
if (ShadowType->getElementCount() ==
3405+
cast<VectorType>(Src->getType())->getElementCount() * 2)
3406+
ShadowType = FixedVectorType::getHalfElementsVectorType(ShadowType);
3407+
3408+
assert(ShadowType->getElementCount() ==
3409+
cast<VectorType>(Src->getType())->getElementCount());
3410+
3411+
return ShadowType;
3412+
}
3413+
3414+
/// Doubles the length of a vector shadow (filled with zeros) if necessary to
3415+
/// match the length of the shadow for the instruction.
3416+
/// This is more type-safe than CreateShadowCast().
3417+
Value *extendVectorShadowWithZeros(Value *Shadow, IntrinsicInst &I) {
3418+
IRBuilder<> IRB(&I);
3419+
assert(isa<FixedVectorType>(Shadow->getType()));
3420+
assert(isa<FixedVectorType>(I.getType()));
3421+
3422+
Value *FullShadow = getCleanShadow(&I);
3423+
assert(cast<FixedVectorType>(Shadow->getType())->getNumElements() <=
3424+
cast<FixedVectorType>(FullShadow->getType())->getNumElements());
3425+
assert(cast<FixedVectorType>(Shadow->getType())->getScalarType() ==
3426+
cast<FixedVectorType>(FullShadow->getType())->getScalarType());
3427+
3428+
if (Shadow->getType() == FullShadow->getType()) {
3429+
FullShadow = Shadow;
3430+
} else {
3431+
// TODO: generalize beyond 2x?
3432+
SmallVector<int, 32> ShadowMask(
3433+
cast<FixedVectorType>(FullShadow->getType())->getNumElements());
3434+
std::iota(ShadowMask.begin(), ShadowMask.end(), 0);
3435+
3436+
// Append zeros
3437+
FullShadow =
3438+
IRB.CreateShuffleVector(Shadow, getCleanShadow(Shadow), ShadowMask);
3439+
}
3440+
3441+
return FullShadow;
3442+
}
3443+
33923444
/// Handle x86 SSE vector conversion.
33933445
///
33943446
/// e.g., single-precision to half-precision conversion:
@@ -3419,13 +3471,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
34193471

34203472
// The return type might have more elements than the input.
34213473
// Temporarily shrink the return type's number of elements.
3422-
VectorType *ShadowType = cast<VectorType>(getShadowTy(&I));
3423-
if (ShadowType->getElementCount() ==
3424-
cast<VectorType>(Src->getType())->getElementCount() * 2)
3425-
ShadowType = VectorType::getHalfElementsVectorType(ShadowType);
3426-
3427-
assert(ShadowType->getElementCount() ==
3428-
cast<VectorType>(Src->getType())->getElementCount());
3474+
VectorType *ShadowType = shrinkVectorShadow(Src, I);
34293475

34303476
IRBuilder<> IRB(&I);
34313477
Value *S0 = getShadow(&I, 0);
@@ -3440,19 +3486,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
34403486

34413487
// The return type might have more elements than the input.
34423488
// Extend the return type back to its original width if necessary.
3443-
Value *FullShadow = getCleanShadow(&I);
3444-
3445-
if (Shadow->getType() == FullShadow->getType()) {
3446-
FullShadow = Shadow;
3447-
} else {
3448-
SmallVector<int, 8> ShadowMask(
3449-
cast<FixedVectorType>(FullShadow->getType())->getNumElements());
3450-
std::iota(ShadowMask.begin(), ShadowMask.end(), 0);
3451-
3452-
// Append zeros
3453-
FullShadow =
3454-
IRB.CreateShuffleVector(Shadow, getCleanShadow(Shadow), ShadowMask);
3455-
}
3489+
Value *FullShadow = extendVectorShadowWithZeros(Shadow, I);
34563490

34573491
setShadow(&I, FullShadow);
34583492
setOriginForNaryOp(I);

0 commit comments

Comments
 (0)