Skip to content

Fix PMO to not scalarize empty tuple #59382

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 1 commit into from
Jun 13, 2022
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
16 changes: 10 additions & 6 deletions lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,11 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
// Stores *to* the allocation are writes.
if (auto *si = dyn_cast<StoreInst>(User)) {
if (UI->getOperandNumber() == StoreInst::Dest) {
if (PointeeType.is<TupleType>()) {
UsesToScalarize.push_back(User);
continue;
if (auto tupleType = PointeeType.getAs<TupleType>()) {
if (!tupleType->isEqual(Module.getASTContext().TheEmptyTupleType)) {
UsesToScalarize.push_back(User);
continue;
}
}

auto kind = ([&]() -> PMOUseKind {
Expand Down Expand Up @@ -322,9 +324,11 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
if (auto *CAI = dyn_cast<CopyAddrInst>(User)) {
// If this is a copy of a tuple, we should scalarize it so that we don't
// have an access that crosses elements.
if (PointeeType.is<TupleType>()) {
UsesToScalarize.push_back(CAI);
continue;
if (auto tupleType = PointeeType.getAs<TupleType>()) {
if (!tupleType->isEqual(Module.getASTContext().TheEmptyTupleType)) {
UsesToScalarize.push_back(CAI);
continue;
}
}

// If this is the source of the copy_addr, then this is a load. If it is
Expand Down
34 changes: 34 additions & 0 deletions test/SILOptimizer/predictable_memopt.sil
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,40 @@ bb0(%0 : $Int):
return %d : $Int
}

sil @read_emptytuple : $@convention(thin) (@in_guaranteed ()) -> ()

// CHECK-LABEL: sil @emptytuple_promotion1 :
// CHECK: store
// CHECK-LABEL: } // end sil function 'emptytuple_promotion1'
sil @emptytuple_promotion1 : $@convention(thin) () -> () {
bb0:
%1 = alloc_stack $()
%2 = tuple ()
store %2 to %1 : $*()
%f = function_ref @read_emptytuple : $@convention(thin) (@in_guaranteed ()) -> ()
apply %f(%1) : $@convention(thin) (@in_guaranteed ()) -> ()
dealloc_stack %1 : $*()
return %2 : $()
}

// CHECK-LABEL: sil @emptytuple_promotion2 :
// CHECK: store
// CHECK: store
// CHECK-LABEL: } // end sil function 'emptytuple_promotion2'
sil @emptytuple_promotion2 : $@convention(thin) () -> () {
bb0:
%1 = alloc_stack $()
%2 = alloc_stack $()
%3 = tuple ()
store %3 to %1 : $*()
copy_addr %2 to %1 : $*()
%f = function_ref @read_emptytuple : $@convention(thin) (@in_guaranteed ()) -> ()
apply %f(%1) : $@convention(thin) (@in_guaranteed ()) -> ()
dealloc_stack %2 : $*()
dealloc_stack %1 : $*()
return %3 : $()
}

// In this example we create two boxes. The first box is initialized and then
// taken from to initialize the second box. This means that the first box must
// be dealloc_boxed (since its underlying memory is considered invalid). In
Expand Down