Skip to content

Handle more instructions in SROA #83353

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 10 additions & 2 deletions lib/SILOptimizer/Transforms/COWOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,20 @@ bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
if (SILPhiArgument *arg = dyn_cast<SILPhiArgument>(v)) {
if (handled.insert(arg)) {
SmallVector<SILValue, 4> incomingVals;
if (!arg->getIncomingPhiValues(incomingVals))
if (!arg->getIncomingPhiValues(incomingVals)) {
LLVM_DEBUG(llvm::dbgs() << "Bailing out, found non-phi: " << arg);
return false;
}
for (SILValue incomingVal : incomingVals) {
LLVM_DEBUG(llvm::dbgs() << "Incoming phi value: " << incomingVal);
workList.push_back(incomingVal);
}
}
} else if (auto *ECM = dyn_cast<EndCOWMutationInst>(v)) {
if (endCOWMutationsFound.insert(ECM))
endCOWMutationInsts.push_back(ECM);
} else {
LLVM_DEBUG(llvm::dbgs() << "Found unhandled SILValue: " << v);
return false;
}
}
Expand Down Expand Up @@ -230,8 +234,10 @@ bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
// buffer is stored to.
if (numStoresFound != 0) {
// Avoid quadratic behavior. Usually this limit is not exceeded.
if (numStoresFound * numLoadsFound > 128)
if (numStoresFound * numLoadsFound > 128) {
LLVM_DEBUG(llvm::dbgs() << "Bailing out, threshold reached\n");
return false;
}
for (SILInstruction *load : potentialLoadInsts) {
for (SILValue storeAddr : storeAddrs) {
if (!AA || AA->mayReadFromMemory(load, storeAddr)) {
Expand All @@ -245,6 +251,8 @@ bool COWOptsPass::optimizeBeginCOW(BeginCOWMutationInst *BCM) {
}
}

LLVM_DEBUG(llvm::dbgs() << "Success: marking keep_unique\n");

// Replace the uniqueness result of the begin_cow_mutation with an integer
// literal of "true".
SILBuilderWithScope B(BCM);
Expand Down
31 changes: 28 additions & 3 deletions lib/SILOptimizer/Transforms/SILSROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,16 @@ bool SROAMemoryUseAnalyzer::analyze() {
continue;
}

if (isa<DeallocStackInst>(User)) {
if (isa<DeallocStackInst>(User) || isa<DestroyAddrInst>(User) ) {
// We can ignore the dealloc_stack.
continue;
}

if (auto *mdi = dyn_cast<MarkDependenceInst>(User)) {
if (mdi->getBase() == Operand->get()) {
continue;
}
}

// Otherwise we do not understand this instruction, so bail.
LLVM_DEBUG(llvm::dbgs() <<" Found unknown user, pointer escapes!\n");
Expand Down Expand Up @@ -288,11 +294,30 @@ void SROAMemoryUseAnalyzer::chopUpAlloca(std::vector<AllocStackInst *> &Worklist
// up allocas, we also chop up the relevant dealloc stack insts.
if (auto *DSI = dyn_cast<DeallocStackInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found DeallocStackInst!\n");
// Create the allocations in reverse order.
// Create the deallocations in reverse order.
for (auto *NewAI : llvm::reverse(NewAllocations))
B.createDeallocStack(DSI->getLoc(), SILValue(NewAI));
ToRemove.push_back(DSI);
}
if (auto *DAI = dyn_cast<DestroyAddrInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found DestroyAddrInst!\n");
for (auto *NewAI : NewAllocations) {
auto &srcTL = B.getTypeLowering(NewAI->getType());
srcTL.emitDestroyAddress(B, DAI->getLoc(), SILValue(NewAI));
}
ToRemove.push_back(DAI);
}
if (auto *origMDI = dyn_cast<MarkDependenceInst>(User)) {
LLVM_DEBUG(llvm::dbgs() << " Found MarkDependenceInst!\n");
auto value = origMDI->getValue();
for (auto *NewAI : NewAllocations) {
auto *newMdi = B.createMarkDependence(origMDI->getLoc(), value,
NewAI, origMDI->dependenceKind());
value = newMdi;
}
origMDI->replaceAllUsesWith(value);
ToRemove.push_back(origMDI);
}
}

for (auto *Operand : getDebugUses(SILValue(AI))) {
Expand Down Expand Up @@ -324,7 +349,7 @@ void SROAMemoryUseAnalyzer::chopUpAlloca(std::vector<AllocStackInst *> &Worklist
ToRemove.push_back(DVI);
}

// Remove the old DeallocStackInst/DebugValueInst instructions.
// Remove the old instructions.
for (auto *DSI : ToRemove) {
DSI->eraseFromParent();
}
Expand Down
100 changes: 100 additions & 0 deletions test/SILOptimizer/sroa_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,24 @@ bb0(%0 : @owned $NotTrivial):
return %6 : $()
}

// CHECK-LABEL: sil [ossa] @struct_with_obj_fields_destroy_addr :
// CHECK: [[VAR_1:%[0-9]+]] = alloc_stack $Obj
// CHECK: [[VAR_2:%[0-9]+]] = alloc_stack $Obj
// CHECK-LABEL: } // end sil function 'struct_with_obj_fields_destroy_addr'
sil [ossa] @struct_with_obj_fields_destroy_addr : $@convention(thin) (@owned NotTrivial) -> () {
bb0(%0 : @owned $NotTrivial):
%1 = alloc_stack $NotTrivial
store %0 to [init] %1 : $*NotTrivial
%2 = function_ref @use_obj : $@convention(thin) (@owned Obj) -> ()
%3 = struct_element_addr %1 : $*NotTrivial, #NotTrivial.y
%4 = load [copy] %3 : $*Obj
apply %2(%4) : $@convention(thin) (@owned Obj) -> ()
destroy_addr %1
dealloc_stack %1
%6 = tuple ()
return %6 : $()
}

// CHECK-LABEL: sil [ossa] @struct_with_obj_fields_multiple_stores :
// CHECK: bb0([[ARG_0:%[0-9]+]] : @owned $NotTrivial, [[ARG_1:%[0-9]+]] : @owned $NotTrivial):
// CHECK: [[VAR_1:%[0-9]+]] = alloc_stack $Obj
Expand Down Expand Up @@ -583,3 +601,85 @@ bb6:
%res = tuple ()
return %res : $()
}


struct Wrapper {
var array: Array<Int>
}

struct DoubleWrapper {
var array1: Array<Int>
var array2: Array<Int>
}

sil @get_span_from_array : $@convention(thin) (@guaranteed Array<Int>) -> Span<Int>
sil @get_span_from_wrapper : $@convention(thin) (@guaranteed Wrapper) -> Span<Int>
sil @get_span_from_doublewrapper : $@convention(thin) (@guaranteed DoubleWrapper) -> Span<Int>
sil @use_span : $@convention(thin) (Span<Int>) -> ()

// CHECK-LABEL: sil [ossa] @struct_with_dependence1 :
// CHECK: alloc_stack $Array
// CHECK-LABEL: } // end sil function 'struct_with_dependence1'
sil [ossa] @struct_with_dependence1 : $@convention(thin) (@owned Array<Int>) -> () {
bb0(%0 : @owned $Array<Int>):
%stk = alloc_stack $Wrapper
%3 = struct_element_addr %stk : $*Wrapper, #Wrapper.array
store %0 to [init] %3
%4 = load_borrow %3
%2 = function_ref @get_span_from_array : $@convention(thin) (@guaranteed Array<Int>) -> Span<Int>
%5 = apply %2(%4) : $@convention(thin) (@guaranteed Array<Int>) -> Span<Int>
%6 = mark_dependence [nonescaping] %5 on %stk
%7 = function_ref @use_span : $@convention(thin) (Span<Int>) -> ()
%8 = apply %7(%6) : $@convention(thin) (Span<Int>) -> ()
end_borrow %4
%9 = load [take] %stk
destroy_value %9
dealloc_stack %stk
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @struct_with_dependence2 :
// CHECK: alloc_stack $Array
// CHECK-LABEL: } // end sil function 'struct_with_dependence2'
sil [ossa] @struct_with_dependence2 : $@convention(thin) (@owned Array<Int>) -> () {
bb0(%0 : @owned $Array<Int>):
%stk = alloc_stack $Wrapper
%3 = struct_element_addr %stk : $*Wrapper, #Wrapper.array
store %0 to [init] %3
%4 = load [copy] %stk
%2 = function_ref @get_span_from_wrapper : $@convention(thin) (@guaranteed Wrapper) -> Span<Int>
%5 = apply %2(%4) : $@convention(thin) (@guaranteed Wrapper) -> Span<Int>
%6 = mark_dependence [nonescaping] %5 on %stk
%7 = function_ref @use_span : $@convention(thin) (Span<Int>) -> ()
%8 = apply %7(%6) : $@convention(thin) (Span<Int>) -> ()
destroy_value %4
destroy_addr %stk
dealloc_stack %stk
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @struct_with_dependence3 :
// CHECK: alloc_stack $Array
// CHECK: alloc_stack $Array
// CHECK-LABEL: } // end sil function 'struct_with_dependence3'
sil [ossa] @struct_with_dependence3 : $@convention(thin) (@owned Array<Int>, @owned Array<Int>) -> () {
bb0(%0 : @owned $Array<Int>, %1 : @owned $Array<Int>):
%stk = alloc_stack $DoubleWrapper
%3 = struct_element_addr %stk : $*DoubleWrapper, #DoubleWrapper.array1
store %0 to [init] %3
%3b = struct_element_addr %stk : $*DoubleWrapper, #DoubleWrapper.array2
store %1 to [init] %3b
%4 = load [copy] %stk
%2 = function_ref @get_span_from_doublewrapper : $@convention(thin) (@guaranteed DoubleWrapper) -> Span<Int>
%5 = apply %2(%4) : $@convention(thin) (@guaranteed DoubleWrapper) -> Span<Int>
%6 = mark_dependence [nonescaping] %5 on %stk
%7 = function_ref @use_span : $@convention(thin) (Span<Int>) -> ()
%8 = apply %7(%6) : $@convention(thin) (Span<Int>) -> ()
destroy_value %4
destroy_addr %stk
dealloc_stack %stk
%r = tuple ()
return %r : $()
}