Skip to content

[pmo] Teach the use collector how to handle store [assign]. #22036

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
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
41 changes: 28 additions & 13 deletions lib/SILOptimizer/Mandatory/PMOMemoryUseCollector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,21 +239,36 @@ bool ElementUseCollector::collectUses(SILValue Pointer) {
}

// Stores *to* the allocation are writes.
if (isa<StoreInst>(User) && UI->getOperandNumber() == 1) {
if (PointeeType.is<TupleType>()) {
UsesToScalarize.push_back(User);
if (auto *si = dyn_cast<StoreInst>(User)) {
if (UI->getOperandNumber() == StoreInst::Dest) {
if (PointeeType.is<TupleType>()) {
UsesToScalarize.push_back(User);
continue;
}

auto kind = ([&]() -> PMOUseKind {
switch (si->getOwnershipQualifier()) {
// Coming out of SILGen, we assume that raw stores are
// initializations, unless they have trivial type (which we classify
// as InitOrAssign).
case StoreOwnershipQualifier::Unqualified:
if (PointeeType.isTrivial(User->getModule()))
return PMOUseKind::InitOrAssign;
return PMOUseKind::Initialization;

case StoreOwnershipQualifier::Init:
return PMOUseKind::Initialization;

case StoreOwnershipQualifier::Assign:
return PMOUseKind::Assign;

case StoreOwnershipQualifier::Trivial:
return PMOUseKind::InitOrAssign;
}
})();
Uses.emplace_back(si, kind);
continue;
}

// Coming out of SILGen, we assume that raw stores are initializations,
// unless they have trivial type (which we classify as InitOrAssign).
auto Kind = ([&]() -> PMOUseKind {
if (PointeeType.isTrivial(User->getModule()))
return PMOUseKind::InitOrAssign;
return PMOUseKind::Initialization;
})();
Uses.emplace_back(User, Kind);
continue;
}

if (auto *CAI = dyn_cast<CopyAddrInst>(User)) {
Expand Down
21 changes: 21 additions & 0 deletions test/SILOptimizer/predictable_memaccess_opts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,27 @@ bb0(%0 : @owned $KlassAndTuple):
return %4 : $Klass
}

// CHECK-LABEL: sil [ossa] @simple_assignstore : $@convention(thin) (@owned Klass, @owned Klass) -> @owned Klass {
// CHECK: bb0([[ARG0:%.*]] : @owned $Klass, [[ARG1:%.*]] : @owned $Klass):
// CHECK: [[STACK:%.*]] = alloc_stack $Klass
// CHECK: store [[ARG0]] to [init] [[STACK]]
// CHECK: [[ARG1_COPY:%.*]] = copy_value [[ARG1]]
// CHECK: store [[ARG1]] to [assign] [[STACK]]
// CHECK: destroy_addr [[STACK]]
// CHECK: dealloc_stack [[STACK]]
// CHECK: return [[ARG1_COPY]]
// CHECK: } // end sil function 'simple_assignstore'
sil [ossa] @simple_assignstore : $@convention(thin) (@owned Klass, @owned Klass) -> @owned Klass {
bb0(%0 : @owned $Klass, %1 : @owned $Klass):
%2 = alloc_stack $Klass
store %0 to [init] %2 : $*Klass
store %1 to [assign] %2 : $*Klass
%3 = load [copy] %2 : $*Klass
destroy_addr %2 : $*Klass
dealloc_stack %2 : $*Klass
return %3 : $Klass
}

////////////////////
// Negative Tests //
////////////////////
Expand Down