[SILGen] Remove subtle identity function call.

Back in 33f4f57cc4 of
https://github.com/apple/swift/pull/28044 fame,
non-`CaptureKind::Constant:` uses of `Entry.val` in
`SILGenFunction::emitCaptures` were replaced with a use of the newly
added lambda `getAddressValue` applied at `Entry.val`.

The replacement of `Entry.value` with `getAddressValue(Entry.value)` in
the case of `CaptureKind::Box` had no effect.

Back then, the reason was that the condition

    SGM.Types
        .getTypeLowering(
            valueType,
            TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
                expansion.getResilienceExpansion()))
            .isAddressOnly() &&
        !entryValue->getType().isAddress()

under which something other than the input was returned would never
hold: CaptureKind::Box is used for LValues and those never satisfy
`!entryValue->getType().isAddress()`.

Since that PR, the getAddressValue lambda has grown.  There are two
additional aspects to consider: (1) forceCopy, (2) isPack.  (1) is not
relevant because `false` was being passed for the `CaptureKind::Box`
case.  (2) can not currently happen because pack lvalues haven't been
implemented.  But even if they were implemented, the argument passed to
the lambda would still need to be an address.

The same all holds for the `CaptureKind::ImmutableBox` case which only
differs from the `CaptureKind::Box` by a couple of lines.
This commit is contained in:
Nate Chandler
2023-08-08 16:43:10 -07:00
parent e894a34ef8
commit 368536ce92

View File

@@ -795,15 +795,14 @@ void SILGenFunction::emitCaptures(SILLocation loc,
case CaptureKind::Box: {
assert(!isPack);
auto entryValue = getAddressValue(val, /*forceCopy=*/false);
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) {
auto box = ManagedValue::forBorrowedObjectRValue(Entry.box);
// We can guarantee our own box to the callee.
if (canGuarantee) {
@@ -823,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
@@ -843,7 +842,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(
@@ -857,15 +856,14 @@ void SILGenFunction::emitCaptures(SILLocation loc,
case CaptureKind::ImmutableBox: {
assert(!isPack);
auto entryValue = getAddressValue(val, /*forceCopy=*/false);
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(
@@ -874,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
@@ -894,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(