Skip to content

Commit 61c283d

Browse files
authored
[ScalarizeMaskedMemIntrin] Use pointer alignment from pointer of masked.compressstore/expandload. (#83519)
Previously we used Align(1) for all scalarized load/stores from masked.compressstore/expandload. For targets not supporting unaligned accesses, it make backend need to split aligned large width loads/stores to byte loads/stores. To solve this performance issue, this patch preserves the alignment of base pointer after scalarizing.
1 parent 8d1046a commit 61c283d

File tree

5 files changed

+4186
-4
lines changed

5 files changed

+4186
-4
lines changed

llvm/lib/Transforms/Scalar/ScalarizeMaskedMemIntrin.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ static void scalarizeMaskedExpandLoad(const DataLayout &DL, CallInst *CI,
627627
Value *Ptr = CI->getArgOperand(0);
628628
Value *Mask = CI->getArgOperand(1);
629629
Value *PassThru = CI->getArgOperand(2);
630+
Align Alignment = CI->getParamAlign(0).valueOrOne();
630631

631632
auto *VecType = cast<FixedVectorType>(CI->getType());
632633

@@ -644,6 +645,10 @@ static void scalarizeMaskedExpandLoad(const DataLayout &DL, CallInst *CI,
644645
// The result vector
645646
Value *VResult = PassThru;
646647

648+
// Adjust alignment for the scalar instruction.
649+
const Align AdjustedAlignment =
650+
commonAlignment(Alignment, EltTy->getPrimitiveSizeInBits() / 8);
651+
647652
// Shorten the way if the mask is a vector of constants.
648653
// Create a build_vector pattern, with loads/poisons as necessary and then
649654
// shuffle blend with the pass through value.
@@ -659,7 +664,7 @@ static void scalarizeMaskedExpandLoad(const DataLayout &DL, CallInst *CI,
659664
} else {
660665
Value *NewPtr =
661666
Builder.CreateConstInBoundsGEP1_32(EltTy, Ptr, MemIndex);
662-
InsertElt = Builder.CreateAlignedLoad(EltTy, NewPtr, Align(1),
667+
InsertElt = Builder.CreateAlignedLoad(EltTy, NewPtr, AdjustedAlignment,
663668
"Load" + Twine(Idx));
664669
ShuffleMask[Idx] = Idx;
665670
++MemIndex;
@@ -713,7 +718,7 @@ static void scalarizeMaskedExpandLoad(const DataLayout &DL, CallInst *CI,
713718
CondBlock->setName("cond.load");
714719

715720
Builder.SetInsertPoint(CondBlock->getTerminator());
716-
LoadInst *Load = Builder.CreateAlignedLoad(EltTy, Ptr, Align(1));
721+
LoadInst *Load = Builder.CreateAlignedLoad(EltTy, Ptr, AdjustedAlignment);
717722
Value *NewVResult = Builder.CreateInsertElement(VResult, Load, Idx);
718723

719724
// Move the pointer if there are more blocks to come.
@@ -755,6 +760,7 @@ static void scalarizeMaskedCompressStore(const DataLayout &DL, CallInst *CI,
755760
Value *Src = CI->getArgOperand(0);
756761
Value *Ptr = CI->getArgOperand(1);
757762
Value *Mask = CI->getArgOperand(2);
763+
Align Alignment = CI->getParamAlign(1).valueOrOne();
758764

759765
auto *VecType = cast<FixedVectorType>(Src->getType());
760766

@@ -767,6 +773,10 @@ static void scalarizeMaskedCompressStore(const DataLayout &DL, CallInst *CI,
767773

768774
Type *EltTy = VecType->getElementType();
769775

776+
// Adjust alignment for the scalar instruction.
777+
const Align AdjustedAlignment =
778+
commonAlignment(Alignment, EltTy->getPrimitiveSizeInBits() / 8);
779+
770780
unsigned VectorWidth = VecType->getNumElements();
771781

772782
// Shorten the way if the mask is a vector of constants.
@@ -778,7 +788,7 @@ static void scalarizeMaskedCompressStore(const DataLayout &DL, CallInst *CI,
778788
Value *OneElt =
779789
Builder.CreateExtractElement(Src, Idx, "Elt" + Twine(Idx));
780790
Value *NewPtr = Builder.CreateConstInBoundsGEP1_32(EltTy, Ptr, MemIndex);
781-
Builder.CreateAlignedStore(OneElt, NewPtr, Align(1));
791+
Builder.CreateAlignedStore(OneElt, NewPtr, AdjustedAlignment);
782792
++MemIndex;
783793
}
784794
CI->eraseFromParent();
@@ -824,7 +834,7 @@ static void scalarizeMaskedCompressStore(const DataLayout &DL, CallInst *CI,
824834

825835
Builder.SetInsertPoint(CondBlock->getTerminator());
826836
Value *OneElt = Builder.CreateExtractElement(Src, Idx);
827-
Builder.CreateAlignedStore(OneElt, Ptr, Align(1));
837+
Builder.CreateAlignedStore(OneElt, Ptr, AdjustedAlignment);
828838

829839
// Move the pointer if there are more blocks to come.
830840
Value *NewPtr;

0 commit comments

Comments
 (0)