Skip to content

Commit 67f7a9d

Browse files
committed
[NFC] Flatten conditionals in AddressLowering.
De-nested the expressions in convertBeginApplyWithOpaqueYield, noting which cases are handled.
1 parent c40a98a commit 67f7a9d

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,31 +2171,34 @@ void ApplyRewriter::convertBeginApplyWithOpaqueYield() {
21712171
for (auto i : indices(oldResults)) {
21722172
auto &oldResult = oldResults[i];
21732173
auto &newResult = newResults[i];
2174+
if (oldResult.getType().isObject() && newResult.getType().isObject()) {
2175+
// Handle direct conventions.
2176+
oldResult.replaceAllUsesWith(&newResult);
2177+
continue;
2178+
}
2179+
if (oldResult.getType().isAddress()) {
2180+
// Handle inout convention.
2181+
assert(newResult.getType().isAddress());
2182+
oldResult.replaceAllUsesWith(&newResult);
2183+
continue;
2184+
}
21742185
if (oldResult.getType().isAddressOnly(*pass.function)) {
2175-
if (!oldResult.getType().isAddress()) {
2176-
pass.valueStorageMap.setStorageAddress(&oldResult, &newResult);
2177-
pass.valueStorageMap.getStorage(&oldResult).markRewritten();
2178-
} else {
2179-
oldResult.replaceAllUsesWith(&newResult);
2180-
}
2186+
// Remap storage when an address-only type is yielded as an opaque value.
2187+
pass.valueStorageMap.setStorageAddress(&oldResult, &newResult);
2188+
pass.valueStorageMap.getStorage(&oldResult).markRewritten();
2189+
continue;
2190+
}
2191+
assert(oldResult.getType().isObject());
2192+
assert(newResult.getType().isAddress());
2193+
if (oldResult.getOwnershipKind() == OwnershipKind::Guaranteed) {
2194+
SILValue load =
2195+
resultBuilder.emitLoadBorrowOperation(callLoc, &newResult);
2196+
oldResult.replaceAllUsesWith(load);
2197+
emitEndBorrows(load, pass);
21812198
} else {
2182-
if (oldResult.getType().isObject() && newResult.getType().isAddress()) {
2183-
if (oldResult.getOwnershipKind() == OwnershipKind::Guaranteed) {
2184-
SILValue load =
2185-
resultBuilder.emitLoadBorrowOperation(callLoc, &newResult);
2186-
oldResult.replaceAllUsesWith(load);
2187-
emitEndBorrows(load, pass);
2188-
} else {
2189-
auto *load = resultBuilder.createLoad(
2190-
callLoc, &newResult,
2191-
newResult.getType().isTrivial(*pass.function)
2192-
? LoadOwnershipQualifier::Trivial
2193-
: LoadOwnershipQualifier::Take);
2194-
oldResult.replaceAllUsesWith(load);
2195-
}
2196-
} else {
2197-
oldResult.replaceAllUsesWith(&newResult);
2198-
}
2199+
auto *load = resultBuilder.createTrivialLoadOr(
2200+
callLoc, &newResult, LoadOwnershipQualifier::Take);
2201+
oldResult.replaceAllUsesWith(load);
21992202
}
22002203
}
22012204
}

test/SILOptimizer/address_lowering.sil

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,22 @@ bb2:
16721672
unwind
16731673
}
16741674

1675+
// CHECK-LABEL: sil [ossa] @testBeginApplyFYieldDirectOwnedAndAddressOnlyGuaranteed : {{.*}} {
1676+
// CHECK: {{bb[0-9]+}}([[OUT_ADDR:%[^,]+]] :
1677+
// CHECK: ([[OWNED_INSTANCE:%[^,]+]], [[ADDR:%[^,]+]], [[TOKEN:%[^,]+]]) = begin_apply
1678+
// CHECK: destroy_value [[OWNED_INSTANCE]]
1679+
// CHECK: copy_addr [[ADDR]] to [init] [[OUT_ADDR]]
1680+
// CHECK: end_apply [[TOKEN]]
1681+
// CHECK-LABEL: } // end sil function 'testBeginApplyFYieldDirectOwnedAndAddressOnlyGuaranteed'
1682+
sil [ossa] @testBeginApplyFYieldDirectOwnedAndAddressOnlyGuaranteed : $@convention(thin) <T> () -> @out T {
1683+
entry:
1684+
(%klass, %instance, %token) = begin_apply undef<T>() : $@yield_once @convention(thin) <T> () -> (@yields @owned Klass, @yields @in_guaranteed T)
1685+
destroy_value %klass : $Klass
1686+
%retval = copy_value %instance : $T
1687+
end_apply %token
1688+
return %retval : $T
1689+
}
1690+
16751691
// CHECK-LABEL: sil hidden [ossa] @testOpaqueYield :
16761692
// CHECK: bb0(%0 : @guaranteed $TestGeneric<T>):
16771693
// CHECK: [[REF:%.*]] = ref_element_addr %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric

0 commit comments

Comments
 (0)