Skip to content

Commit 04b002b

Browse files
authored
[IRBuilder] Add Align argument for CreateMaskedExpandLoad and CreateMaskedCompressStore (#122878)
This patch adds possibility to specify alignment for llvm.masked.expandload/llvm.masked.compressstore intrinsics in IRBuilder (this is mostly NFC for now since it's only used in MemorySanitizer, but there is an intention to generate these intrinsics in the compiler passes, e.g. in LoopVectorizer)
1 parent 4cec0ba commit 04b002b

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

llvm/include/llvm/IR/IRBuilder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,12 +854,13 @@ class IRBuilderBase {
854854
Value *Mask = nullptr);
855855

856856
/// Create a call to Masked Expand Load intrinsic
857-
CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, Value *Mask = nullptr,
857+
CallInst *CreateMaskedExpandLoad(Type *Ty, Value *Ptr, MaybeAlign Align,
858+
Value *Mask = nullptr,
858859
Value *PassThru = nullptr,
859860
const Twine &Name = "");
860861

861862
/// Create a call to Masked Compress Store intrinsic
862-
CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr,
863+
CallInst *CreateMaskedCompressStore(Value *Val, Value *Ptr, MaybeAlign Align,
863864
Value *Mask = nullptr);
864865

865866
/// Return an all true boolean vector (mask) with \p NumElts lanes.

llvm/lib/IR/IRBuilder.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,38 +644,48 @@ CallInst *IRBuilderBase::CreateMaskedScatter(Value *Data, Value *Ptrs,
644644
/// Create a call to Masked Expand Load intrinsic
645645
/// \p Ty - vector type to load
646646
/// \p Ptr - base pointer for the load
647+
/// \p Align - alignment of \p Ptr
647648
/// \p Mask - vector of booleans which indicates what vector lanes should
648649
/// be accessed in memory
649650
/// \p PassThru - pass-through value that is used to fill the masked-off lanes
650651
/// of the result
651652
/// \p Name - name of the result variable
652653
CallInst *IRBuilderBase::CreateMaskedExpandLoad(Type *Ty, Value *Ptr,
653-
Value *Mask, Value *PassThru,
654+
MaybeAlign Align, Value *Mask,
655+
Value *PassThru,
654656
const Twine &Name) {
655657
assert(Ty->isVectorTy() && "Type should be vector");
656658
assert(Mask && "Mask should not be all-ones (null)");
657659
if (!PassThru)
658660
PassThru = PoisonValue::get(Ty);
659661
Type *OverloadedTypes[] = {Ty};
660662
Value *Ops[] = {Ptr, Mask, PassThru};
661-
return CreateMaskedIntrinsic(Intrinsic::masked_expandload, Ops,
662-
OverloadedTypes, Name);
663+
CallInst *CI = CreateMaskedIntrinsic(Intrinsic::masked_expandload, Ops,
664+
OverloadedTypes, Name);
665+
if (Align)
666+
CI->addParamAttr(0, Attribute::getWithAlignment(CI->getContext(), *Align));
667+
return CI;
663668
}
664669

665670
/// Create a call to Masked Compress Store intrinsic
666671
/// \p Val - data to be stored,
667672
/// \p Ptr - base pointer for the store
673+
/// \p Align - alignment of \p Ptr
668674
/// \p Mask - vector of booleans which indicates what vector lanes should
669675
/// be accessed in memory
670676
CallInst *IRBuilderBase::CreateMaskedCompressStore(Value *Val, Value *Ptr,
677+
MaybeAlign Align,
671678
Value *Mask) {
672679
Type *DataTy = Val->getType();
673680
assert(DataTy->isVectorTy() && "Val should be a vector");
674681
assert(Mask && "Mask should not be all-ones (null)");
675682
Type *OverloadedTypes[] = {DataTy};
676683
Value *Ops[] = {Val, Ptr, Mask};
677-
return CreateMaskedIntrinsic(Intrinsic::masked_compressstore, Ops,
678-
OverloadedTypes);
684+
CallInst *CI = CreateMaskedIntrinsic(Intrinsic::masked_compressstore, Ops,
685+
OverloadedTypes);
686+
if (Align)
687+
CI->addParamAttr(1, Attribute::getWithAlignment(CI->getContext(), *Align));
688+
return CI;
679689
}
680690

681691
template <typename T0>

llvm/lib/Transforms/Instrumentation/MemorySanitizer.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35423542
void handleMaskedExpandLoad(IntrinsicInst &I) {
35433543
IRBuilder<> IRB(&I);
35443544
Value *Ptr = I.getArgOperand(0);
3545+
MaybeAlign Align = I.getParamAlign(0);
35453546
Value *Mask = I.getArgOperand(1);
35463547
Value *PassThru = I.getArgOperand(2);
35473548

@@ -3559,10 +3560,11 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35593560
Type *ShadowTy = getShadowTy(&I);
35603561
Type *ElementShadowTy = cast<VectorType>(ShadowTy)->getElementType();
35613562
auto [ShadowPtr, OriginPtr] =
3562-
getShadowOriginPtr(Ptr, IRB, ElementShadowTy, {}, /*isStore*/ false);
3563+
getShadowOriginPtr(Ptr, IRB, ElementShadowTy, Align, /*isStore*/ false);
35633564

3564-
Value *Shadow = IRB.CreateMaskedExpandLoad(
3565-
ShadowTy, ShadowPtr, Mask, getShadow(PassThru), "_msmaskedexpload");
3565+
Value *Shadow =
3566+
IRB.CreateMaskedExpandLoad(ShadowTy, ShadowPtr, Align, Mask,
3567+
getShadow(PassThru), "_msmaskedexpload");
35663568

35673569
setShadow(&I, Shadow);
35683570

@@ -3574,6 +3576,7 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35743576
IRBuilder<> IRB(&I);
35753577
Value *Values = I.getArgOperand(0);
35763578
Value *Ptr = I.getArgOperand(1);
3579+
MaybeAlign Align = I.getParamAlign(1);
35773580
Value *Mask = I.getArgOperand(2);
35783581

35793582
if (ClCheckAccessAddress) {
@@ -3585,9 +3588,9 @@ struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
35853588
Type *ElementShadowTy =
35863589
getShadowTy(cast<VectorType>(Values->getType())->getElementType());
35873590
auto [ShadowPtr, OriginPtrs] =
3588-
getShadowOriginPtr(Ptr, IRB, ElementShadowTy, {}, /*isStore*/ true);
3591+
getShadowOriginPtr(Ptr, IRB, ElementShadowTy, Align, /*isStore*/ true);
35893592

3590-
IRB.CreateMaskedCompressStore(Shadow, ShadowPtr, Mask);
3593+
IRB.CreateMaskedCompressStore(Shadow, ShadowPtr, Align, Mask);
35913594

35923595
// TODO: Store origins.
35933596
}

0 commit comments

Comments
 (0)