Skip to content

[SILGen] Remove subtle identity function call. #67846

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
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
30 changes: 13 additions & 17 deletions lib/SILGen/SILGenFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,18 +795,14 @@ void SILGenFunction::emitCaptures(SILLocation loc,
case CaptureKind::Box: {
assert(!isPack);

auto entryValue = getAddressValue(val, /*forceCopy=*/false);
// LValues are captured as both the box owning the value and the
// address of the value.
assert(entryValue->getType().isAddress() && "no address for captured var!");
assert(val->getType().isAddress() &&
"no address for captured var!");
// Boxes of opaque return values stay opaque.
auto minimalLoweredType = SGM.Types.getLoweredRValueType(
TypeExpansionContext::minimal(), type->getCanonicalType());
// If this is a boxed variable, we can use it directly.
if (Entry.box &&
entryValue->getType().getASTType() == minimalLoweredType) {
// If our captured value is a box with a moveonlywrapped type inside,
// unwrap it.
val->getType().getASTType() == minimalLoweredType) {
auto box = ManagedValue::forBorrowedObjectRValue(Entry.box);
// We can guarantee our own box to the callee.
if (canGuarantee) {
Expand All @@ -815,6 +811,8 @@ void SILGenFunction::emitCaptures(SILLocation loc,
box = box.copy(*this, loc);
}

// If our captured value is a box with a moveonlywrapped type inside,
// unwrap it.
if (box.getType().isBoxedMoveOnlyWrappedType(&F)) {
CleanupCloner cloner(*this, box);
box = cloner.clone(
Expand All @@ -824,7 +822,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,
capturedArgs.push_back(box);

if (captureCanEscape)
escapesToMark.push_back(entryValue);
escapesToMark.push_back(val);
} else {
// Address only 'let' values are passed by box. This isn't great, in
// that a variable captured by multiple closures will be boxed for each
Expand All @@ -838,12 +836,13 @@ void SILGenFunction::emitCaptures(SILLocation loc,
// in-place.
// TODO: Use immutable box for immutable captures.
auto boxTy = SGM.Types.getContextBoxTypeForCapture(
vd, minimalLoweredType, FunctionDC->getGenericEnvironmentOfContext(),
vd, minimalLoweredType,
FunctionDC->getGenericEnvironmentOfContext(),
/*mutable*/ true);

AllocBoxInst *allocBox = B.createAllocBox(loc, boxTy);
ProjectBoxInst *boxAddress = B.createProjectBox(loc, allocBox, 0);
B.createCopyAddr(loc, entryValue, boxAddress, IsNotTake,
B.createCopyAddr(loc, val, boxAddress, IsNotTake,
IsInitialization);
if (canGuarantee)
capturedArgs.push_back(
Expand All @@ -857,17 +856,14 @@ void SILGenFunction::emitCaptures(SILLocation loc,
case CaptureKind::ImmutableBox: {
assert(!isPack);

auto entryValue = getAddressValue(val, /*forceCopy=*/false);
// LValues are captured as both the box owning the value and the
// address of the value.
assert(entryValue->getType().isAddress() &&
assert(val->getType().isAddress() &&
"no address for captured var!");
// Boxes of opaque return values stay opaque.
auto minimalLoweredType = SGM.Types.getLoweredRValueType(
TypeExpansionContext::minimal(), type->getCanonicalType());
// If this is a boxed variable, we can use it directly.
if (Entry.box &&
entryValue->getType().getASTType() == minimalLoweredType) {
val->getType().getASTType() == minimalLoweredType) {
// We can guarantee our own box to the callee.
if (canGuarantee) {
capturedArgs.push_back(
Expand All @@ -876,7 +872,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,
capturedArgs.push_back(emitManagedRetain(loc, Entry.box));
}
if (captureCanEscape)
escapesToMark.push_back(entryValue);
escapesToMark.push_back(val);
} else {
// Address only 'let' values are passed by box. This isn't great, in
// that a variable captured by multiple closures will be boxed for each
Expand All @@ -896,7 +892,7 @@ void SILGenFunction::emitCaptures(SILLocation loc,

AllocBoxInst *allocBox = B.createAllocBox(loc, boxTy);
ProjectBoxInst *boxAddress = B.createProjectBox(loc, allocBox, 0);
B.createCopyAddr(loc, entryValue, boxAddress, IsNotTake,
B.createCopyAddr(loc, val, boxAddress, IsNotTake,
IsInitialization);
if (canGuarantee)
capturedArgs.push_back(
Expand Down