Skip to content

Commit bb6b271

Browse files
authored
Merge pull request #37668 from eeckstein/temprvalueopt-yield
TempRValueOpt: Support yield
2 parents 4878af5 + f494f37 commit bb6b271

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ collectLoads(Operand *addressUse, CopyAddrInst *originalCopy,
215215
}
216216
return true;
217217
}
218+
case SILInstructionKind::YieldInst: {
219+
auto *yield = cast<YieldInst>(user);
220+
auto convention = yield->getArgumentConventionForOperand(*addressUse);
221+
if (!convention.isGuaranteedConvention())
222+
return false;
223+
224+
loadInsts.insert(user);
225+
return true;
226+
}
218227
case SILInstructionKind::OpenExistentialAddrInst: {
219228
// We only support open existential addr if the access is immutable.
220229
auto *oeai = cast<OpenExistentialAddrInst>(user);
@@ -331,8 +340,10 @@ SILInstruction *TempRValueOptPass::getLastUseWhileSourceIsNotModified(
331340
if (numLoadsFound == useInsts.size()) {
332341
// Function calls are an exception: in a called function a potential
333342
// modification of copySrc could occur _before_ the read of the temporary.
334-
if (FullApplySite::isa(inst) && aa->mayWriteToMemory(inst, copySrc))
343+
if ((FullApplySite::isa(inst) || isa<YieldInst>(inst)) &&
344+
aa->mayWriteToMemory(inst, copySrc)) {
335345
return nullptr;
346+
}
336347

337348
return inst;
338349
}

test/SILOptimizer/temp_rvalue_opt_ossa.sil

+24
Original file line numberDiff line numberDiff line change
@@ -1386,3 +1386,27 @@ bb0(%0 : @owned $Klass):
13861386
return %105 : $Klass
13871387
}
13881388

1389+
// CHECK-LABEL: sil [ossa] @test_yield
1390+
// CHECK: [[TA:%[0-9]+]] = ref_tail_addr
1391+
// CHECK-NOT: copy_addr
1392+
// CHECK: yield [[TA]]
1393+
// CHECK: } // end sil function 'test_yield'
1394+
sil [ossa] @test_yield : $@yield_once @convention(thin) (@guaranteed Klass) -> @yields @in_guaranteed Two {
1395+
bb0(%0 : @guaranteed $Klass):
1396+
%1 = alloc_stack $Two
1397+
%2 = ref_tail_addr [immutable] %0 : $Klass, $Two
1398+
copy_addr %2 to [initialization] %1 : $*Two
1399+
yield %1 : $*Two, resume bb1, unwind bb2
1400+
1401+
bb1:
1402+
destroy_addr %1 : $*Two
1403+
dealloc_stack %1 : $*Two
1404+
%90 = tuple ()
1405+
return %90 : $()
1406+
1407+
bb2:
1408+
destroy_addr %1 : $*Two
1409+
dealloc_stack %1 : $*Two
1410+
unwind
1411+
}
1412+

0 commit comments

Comments
 (0)