Skip to content

TempRValueOpt: Support yield #37668

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
May 31, 2021
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
13 changes: 12 additions & 1 deletion lib/SILOptimizer/Transforms/TempRValueElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ collectLoads(Operand *addressUse, CopyAddrInst *originalCopy,
}
return true;
}
case SILInstructionKind::YieldInst: {
auto *yield = cast<YieldInst>(user);
auto convention = yield->getArgumentConventionForOperand(*addressUse);
if (!convention.isGuaranteedConvention())
return false;

loadInsts.insert(user);
return true;
}
case SILInstructionKind::OpenExistentialAddrInst: {
// We only support open existential addr if the access is immutable.
auto *oeai = cast<OpenExistentialAddrInst>(user);
Expand Down Expand Up @@ -331,8 +340,10 @@ SILInstruction *TempRValueOptPass::getLastUseWhileSourceIsNotModified(
if (numLoadsFound == useInsts.size()) {
// Function calls are an exception: in a called function a potential
// modification of copySrc could occur _before_ the read of the temporary.
if (FullApplySite::isa(inst) && aa->mayWriteToMemory(inst, copySrc))
if ((FullApplySite::isa(inst) || isa<YieldInst>(inst)) &&
aa->mayWriteToMemory(inst, copySrc)) {
return nullptr;
}

return inst;
}
Expand Down
24 changes: 24 additions & 0 deletions test/SILOptimizer/temp_rvalue_opt_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1386,3 +1386,27 @@ bb0(%0 : @owned $Klass):
return %105 : $Klass
}

// CHECK-LABEL: sil [ossa] @test_yield
// CHECK: [[TA:%[0-9]+]] = ref_tail_addr
// CHECK-NOT: copy_addr
// CHECK: yield [[TA]]
// CHECK: } // end sil function 'test_yield'
sil [ossa] @test_yield : $@yield_once @convention(thin) (@guaranteed Klass) -> @yields @in_guaranteed Two {
bb0(%0 : @guaranteed $Klass):
%1 = alloc_stack $Two
%2 = ref_tail_addr [immutable] %0 : $Klass, $Two
copy_addr %2 to [initialization] %1 : $*Two
yield %1 : $*Two, resume bb1, unwind bb2

bb1:
destroy_addr %1 : $*Two
dealloc_stack %1 : $*Two
%90 = tuple ()
return %90 : $()

bb2:
destroy_addr %1 : $*Two
dealloc_stack %1 : $*Two
unwind
}