Skip to content

Commit 3ec96fa

Browse files
committed
[SIL-opaque] Add an assert for open_existential_value.
Add comments. Add a basic dominance sanity check.
1 parent a133549 commit 3ec96fa

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,16 @@ static Operand *getProjectedDefOperand(SILValue value) {
792792

793793
/// If \p value is a an existential or enum, then return the existential or enum
794794
/// operand. These operations are always rewritten by the UseRewriter and always
795-
/// destructively reuse the same storage as their operand. Note that if the
796-
/// operation's result is address-only, then the operand must be address-only
797-
/// and therefore must mapped to ValueStorage.
795+
/// reuse the same storage as their operand. Note that if the operation's result
796+
/// is address-only, then the operand must be address-only and therefore must
797+
/// mapped to ValueStorage.
798+
///
799+
/// open_existential_value must reuse storage because the boxed value is shared
800+
/// with other instances of the existential. An explicit copy is needed to
801+
/// obtain an owned value.
802+
///
803+
/// unchecked_enum_data and switch_enum must reuse storage because extracting
804+
/// the payload destroys the enum value.
798805
static Operand *getReusedStorageOperand(SILValue value) {
799806
switch (value->getKind()) {
800807
default:
@@ -1180,15 +1187,18 @@ createStackAllocation(SILValue value) {
11801187

11811188
auto *openingInst = openingVal->getDefiningInstruction();
11821189
assert(openingVal && "all opened archetypes should be resolved");
1183-
if (latestOpeningInst
1184-
&& pass.domInfo->dominates(openingInst, latestOpeningInst)) {
1185-
return;
1190+
if (latestOpeningInst) {
1191+
if (pass.domInfo->dominates(openingInst, latestOpeningInst))
1192+
return;
1193+
1194+
assert(pass.domInfo->dominates(latestOpeningInst, openingInst) &&
1195+
"opened archetypes must dominate their uses");
11861196
}
11871197
latestOpeningInst = openingInst;
11881198
}
11891199
});
11901200
auto allocPt = latestOpeningInst ? std::next(latestOpeningInst->getIterator())
1191-
: pass.function->begin()->begin();
1201+
: pass.function->begin()->begin();
11921202
auto allocBuilder = pass.getBuilder(allocPt);
11931203
AllocStackInst *alloc = allocBuilder.createAllocStack(pass.genLoc(), allocTy);
11941204

test/SILOptimizer/address_lowering.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ bb0(%0 : @owned $P):
933933
%9 = witness_method $@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P, #P.foo, %8 : $@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
934934
%cpy = copy_value %8 : $@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P
935935
end_borrow %b : $P
936-
// This optional is an aggregate that contains an opened exsitential. May sure it's allocated after open_existential_addr.
936+
// This optional is an aggregate that contains an opened existential. Make sure it is allocated after open_existential_addr.
937937
%opt = enum $Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P>, #Optional.some!enumelt, %cpy : $@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P
938938
%some = unchecked_enum_data %opt : $Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P>, #Optional.some!enumelt
939939
%11 = apply %9<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541") P>(%some) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()

0 commit comments

Comments
 (0)