Skip to content

Commit 8ae936d

Browse files
authored
Merge pull request #85784 from eeckstein/fix-sil-combine-6.3
[6.3] SILCombine: fix propagation of concrete existentials in enums
2 parents 59cbba1 + 75c5b91 commit 8ae936d

File tree

4 files changed

+33
-71
lines changed

4 files changed

+33
-71
lines changed

include/swift/SILOptimizer/Utils/InstOptUtils.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,6 @@ castValueToABICompatibleType(SILBuilder *builder, SILPassManager *pm,
213213
/// ```
214214
bool layoutIsTypeDependent(NominalTypeDecl *decl);
215215

216-
/// Peek through trivial Enum initialization, typically for pointless
217-
/// Optionals.
218-
///
219-
/// The returned InitEnumDataAddr dominates the given
220-
/// UncheckedTakeEnumDataAddrInst.
221-
InitEnumDataAddrInst *
222-
findInitAddressForTrivialEnum(UncheckedTakeEnumDataAddrInst *utedai);
223-
224216
/// Returns a project_box if it is the next instruction after \p ABI and
225217
/// and has \p ABI as operand. Otherwise it creates a new project_box right
226218
/// after \p ABI and returns it.

lib/SILOptimizer/Utils/Existential.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,6 @@ static SILInstruction *getStackInitInst(SILValue allocStackAddr,
171171
if (auto *ASI = dyn_cast<AllocStackInst>(CAI->getSrc()))
172172
return getStackInitInst(ASI, CAI, isCopied);
173173

174-
// Peek through a stack location holding an Enum.
175-
// %stack_adr = alloc_stack
176-
// %data_adr = init_enum_data_addr %stk_adr
177-
// %enum_adr = inject_enum_addr %stack_adr
178-
// %copy_src = unchecked_take_enum_data_addr %enum_adr
179-
// Replace %copy_src with %data_adr and recurse.
180-
//
181-
// TODO: a general Optional elimination sil-combine could
182-
// supersede this check.
183-
if (auto *UTEDAI = dyn_cast<UncheckedTakeEnumDataAddrInst>(CAI->getSrc())) {
184-
if (InitEnumDataAddrInst *IEDAI = findInitAddressForTrivialEnum(UTEDAI))
185-
return getStackInitInst(IEDAI, CAI, isCopied);
186-
}
187-
188174
// Check if the CAISrc is a global_addr.
189175
if (auto *GAI = dyn_cast<GlobalAddrInst>(CAI->getSrc()))
190176
return findInitExistentialFromGlobalAddr(GAI, CAI);

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -886,55 +886,6 @@ ProjectBoxInst *swift::getOrCreateProjectBox(AllocBoxInst *abi,
886886
return builder.createProjectBox(abi->getLoc(), abi, index);
887887
}
888888

889-
// Peek through trivial Enum initialization, typically for pointless
890-
// Optionals.
891-
//
892-
// Given an UncheckedTakeEnumDataAddrInst, check that there are no
893-
// other uses of the Enum value and return the address used to initialized the
894-
// enum's payload:
895-
//
896-
// %stack_adr = alloc_stack
897-
// %data_adr = init_enum_data_addr %stk_adr
898-
// %enum_adr = inject_enum_addr %stack_adr
899-
// %copy_src = unchecked_take_enum_data_addr %enum_adr
900-
// dealloc_stack %stack_adr
901-
// (No other uses of %stack_adr.)
902-
InitEnumDataAddrInst *
903-
swift::findInitAddressForTrivialEnum(UncheckedTakeEnumDataAddrInst *utedai) {
904-
auto *asi = dyn_cast<AllocStackInst>(utedai->getOperand());
905-
if (!asi)
906-
return nullptr;
907-
908-
InjectEnumAddrInst *singleInject = nullptr;
909-
InitEnumDataAddrInst *singleInit = nullptr;
910-
for (auto use : asi->getUses()) {
911-
auto *user = use->getUser();
912-
if (user == utedai)
913-
continue;
914-
915-
// If there is a single init_enum_data_addr and a single inject_enum_addr,
916-
// those instructions must dominate the unchecked_take_enum_data_addr.
917-
// Otherwise the enum wouldn't be initialized on all control flow paths.
918-
if (auto *inj = dyn_cast<InjectEnumAddrInst>(user)) {
919-
if (singleInject)
920-
return nullptr;
921-
singleInject = inj;
922-
continue;
923-
}
924-
925-
if (auto *init = dyn_cast<InitEnumDataAddrInst>(user)) {
926-
if (singleInit)
927-
return nullptr;
928-
singleInit = init;
929-
continue;
930-
}
931-
932-
if (isa<DeallocStackInst>(user) || isa<DebugValueInst>(user))
933-
continue;
934-
}
935-
return singleInit;
936-
}
937-
938889
//===----------------------------------------------------------------------===//
939890
// Closure Deletion
940891
//===----------------------------------------------------------------------===//

test/SILOptimizer/sil_combine_concrete_existential.sil

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,39 @@ bb0(%0 : $S):
786786
return %20 : $()
787787
}
788788

789+
protocol Q2 {
790+
func f()
791+
}
792+
793+
struct S2 : Q2 {
794+
func f()
795+
}
796+
797+
// CHECK-LABEL: sil [ossa] @overridden_enum_in_alloc_stack :
798+
// CHECK: witness_method $@opened
799+
// CHECK-LABEL: } // end sil function 'overridden_enum_in_alloc_stack'
800+
sil [ossa] @overridden_enum_in_alloc_stack : $@convention(thin) (S2, @in_guaranteed Optional<any Q2>) -> () {
801+
bb0(%0 : $S2, %1 : $*Optional<any Q2>):
802+
%2 = alloc_stack $Optional<any Q2>
803+
%3 = init_enum_data_addr %2, #Optional.some!enumelt
804+
%4 = init_existential_addr %3, $S2
805+
store %0 to [trivial] %4
806+
inject_enum_addr %2, #Optional.some!enumelt
807+
copy_addr %1 to %2
808+
%8 = unchecked_take_enum_data_addr %2, #Optional.some!enumelt
809+
%9 = alloc_stack $any Q2
810+
copy_addr %8 to [init] %9
811+
%11 = open_existential_addr immutable_access %9 to $*@opened("9EED66EA-C9C5-11F0-85FF-0EA13E3AABB2", any Q2) Self
812+
%12 = witness_method $@opened("9EED66EA-C9C5-11F0-85FF-0EA13E3AABB2", any Q2) Self, #Q2.f : <Self where Self : Q2> (Self) -> () -> (), %11 : $*@opened("9EED66EA-C9C5-11F0-85FF-0EA13E3AABB2", any Q2) Self : $@convention(witness_method: Q2) <τ_0_0 where τ_0_0 : Q2> (@in_guaranteed τ_0_0) -> ()
813+
%13 = apply %12<@opened("9EED66EA-C9C5-11F0-85FF-0EA13E3AABB2", any Q2) Self>(%11) : $@convention(witness_method: Q2) <τ_0_0 where τ_0_0 : Q2> (@in_guaranteed τ_0_0) -> ()
814+
destroy_addr %9
815+
dealloc_stack %9
816+
destroy_addr %2
817+
dealloc_stack %2
818+
%18 = tuple ()
819+
return %18
820+
}
821+
789822
sil_vtable SubscriptionViewControllerBuilder {}
790823
sil_vtable SubscriptionViewController {}
791824
sil_vtable ViewController {}

0 commit comments

Comments
 (0)