Skip to content

Handle scalable store size in MemCpyOptimizer #118957

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 2 commits into from
Dec 6, 2024
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
66 changes: 35 additions & 31 deletions llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -787,43 +787,47 @@ bool MemCpyOptPass::processStore(StoreInst *SI, BasicBlock::iterator &BBI) {
// Ensure that the value being stored is something that can be memset'able a
// byte at a time like "0" or "-1" or any width, as well as things like
// 0xA0A0A0A0 and 0.0.
auto *V = SI->getOperand(0);
if (Value *ByteVal = isBytewiseValue(V, DL)) {
if (Instruction *I =
tryMergingIntoMemset(SI, SI->getPointerOperand(), ByteVal)) {
BBI = I->getIterator(); // Don't invalidate iterator.
return true;
}
Value *V = SI->getOperand(0);
Value *ByteVal = isBytewiseValue(V, DL);
if (!ByteVal)
return false;

// If we have an aggregate, we try to promote it to memset regardless
// of opportunity for merging as it can expose optimization opportunities
// in subsequent passes.
auto *T = V->getType();
if (T->isAggregateType()) {
uint64_t Size = DL.getTypeStoreSize(T);
IRBuilder<> Builder(SI);
auto *M = Builder.CreateMemSet(SI->getPointerOperand(), ByteVal, Size,
SI->getAlign());
M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);
if (Instruction *I =
tryMergingIntoMemset(SI, SI->getPointerOperand(), ByteVal)) {
BBI = I->getIterator(); // Don't invalidate iterator.
return true;
}

// If we have an aggregate, we try to promote it to memset regardless
// of opportunity for merging as it can expose optimization opportunities
// in subsequent passes.
auto *T = V->getType();
if (!T->isAggregateType())
return false;

LLVM_DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");
TypeSize Size = DL.getTypeStoreSize(T);
if (Size.isScalable())
return false;

// The newly inserted memset is immediately overwritten by the original
// store, so we do not need to rename uses.
auto *StoreDef = cast<MemoryDef>(MSSA->getMemoryAccess(SI));
auto *NewAccess = MSSAU->createMemoryAccessBefore(M, nullptr, StoreDef);
MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/false);
IRBuilder<> Builder(SI);
auto *M = Builder.CreateMemSet(SI->getPointerOperand(), ByteVal, Size,
SI->getAlign());
M->copyMetadata(*SI, LLVMContext::MD_DIAssignID);

eraseInstruction(SI);
NumMemSetInfer++;
LLVM_DEBUG(dbgs() << "Promoting " << *SI << " to " << *M << "\n");

// Make sure we do not invalidate the iterator.
BBI = M->getIterator();
return true;
}
}
// The newly inserted memset is immediately overwritten by the original
// store, so we do not need to rename uses.
auto *StoreDef = cast<MemoryDef>(MSSA->getMemoryAccess(SI));
auto *NewAccess = MSSAU->createMemoryAccessBefore(M, nullptr, StoreDef);
MSSAU->insertDef(cast<MemoryDef>(NewAccess), /*RenameUses=*/false);

return false;
eraseInstruction(SI);
NumMemSetInfer++;

// Make sure we do not invalidate the iterator.
BBI = M->getIterator();
return true;
}

bool MemCpyOptPass::processMemSet(MemSetInst *MSI, BasicBlock::iterator &BBI) {
Expand Down
14 changes: 13 additions & 1 deletion llvm/test/Transforms/MemCpyOpt/vscale-memset.ll
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
define void @foo(ptr %p) {
; CHECK-LABEL: @foo(
; CHECK-NEXT: store <vscale x 16 x i8> zeroinitializer, ptr [[P:%.*]], align 16
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr <vscale x 16 x i8>, ptr [[P:%.*]], i64 1
; CHECK-NEXT: [[TMP1:%.*]] = getelementptr <vscale x 16 x i8>, ptr [[P]], i64 1
; CHECK-NEXT: store <vscale x 16 x i8> zeroinitializer, ptr [[TMP1]], align 16
; CHECK-NEXT: ret void
;
Expand All @@ -18,6 +18,18 @@ define void @foo(ptr %p) {
ret void
}

; Test the compiler does not crash on a store of a scalable aggregate type.
define void @test_no_crash_scalable_agg(ptr %p) {
; CHECK-LABEL: @test_no_crash_scalable_agg(
; CHECK-NEXT: entry:
; CHECK-NEXT: store { <vscale x 16 x i1>, <vscale x 16 x i1> } zeroinitializer, ptr [[P:%.*]], align 2
; CHECK-NEXT: ret void
;
entry:
store { <vscale x 16 x i1>, <vscale x 16 x i1> } zeroinitializer, ptr %p, align 2
ret void
}

; Positive test

define void @memset_vscale_index_zero(ptr %p, i8 %z) {
Expand Down
Loading