Skip to content

[6.0] Fix SILCombine of inject_enum_addr to correctly check for unreferenceable storage #72489

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
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions include/swift/SILOptimizer/Utils/InstOptUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ bool specializeAppliesInFunction(SILFunction &F,
/// types aggregated together at each level.
SILValue createEmptyAndUndefValue(SILType ty, SILInstruction *insertionPoint,
SILBuilderContext &ctx, bool noUndef = false);

/// Check if a struct or its fields can have unreferenceable storage.
bool findUnreferenceableStorage(StructDecl *decl, SILType structType,
SILFunction *func);
} // end namespace swift

#endif // SWIFT_SILOPTIMIZER_UTILS_INSTOPTUTILS_H
2 changes: 1 addition & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,7 +1246,7 @@ SILCombiner::visitInjectEnumAddrInst(InjectEnumAddrInst *IEAI) {

// We cannot create a struct when it has unreferenceable storage.
if (elemType.isEmpty(*IEAI->getFunction()) && structDecl &&
structDecl->hasUnreferenceableStorage()) {
findUnreferenceableStorage(structDecl, elemType, IEAI->getFunction())) {
return nullptr;
}

Expand Down
18 changes: 18 additions & 0 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2009,3 +2009,21 @@ SILValue swift::createEmptyAndUndefValue(SILType ty,
assert(!noUndef);
return SILUndef::get(insertionPoint->getFunction(), ty);
}

bool swift::findUnreferenceableStorage(StructDecl *decl, SILType structType,
SILFunction *func) {
if (decl->hasUnreferenceableStorage()) {
return true;
}
// Check if any fields have unreferenceable stoage
for (auto *field : decl->getStoredProperties()) {
TypeExpansionContext tec = *func;
auto fieldTy = structType.getFieldType(field, func->getModule(), tec);
if (auto *fieldStructDecl = fieldTy.getStructOrBoundGenericStruct()) {
if (findUnreferenceableStorage(fieldStructDecl, fieldTy, func)) {
return true;
}
}
}
return false;
}
30 changes: 30 additions & 0 deletions test/SILOptimizer/sil_combine_enum_addr.sil
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,33 @@ bb3:
%8 = tuple ()
return %8 : $()
}

struct NestedUnreferenceableStorage {
let e: EmptyCUnion
}

sil @no_init_nested_c_union : $@convention(thin) () -> (@out NestedUnreferenceableStorage)

// CHECK-LABEL: sil @test_empty_nested_c_union : $@convention(thin) () -> () {
// CHECK: inject_enum_addr
// CHECK-LABEL: } // end sil function 'test_empty_nested_c_union'
sil @test_empty_nested_c_union : $@convention(thin) () -> () {
bb0:
%0 = alloc_stack $Optional<NestedUnreferenceableStorage>
%1 = init_enum_data_addr %0 : $*Optional<NestedUnreferenceableStorage>, #Optional.some!enumelt
%f = function_ref @no_init_nested_c_union : $@convention(thin) () -> (@out NestedUnreferenceableStorage)
apply %f(%1) : $@convention(thin) () -> (@out NestedUnreferenceableStorage)
inject_enum_addr %0 : $*Optional<NestedUnreferenceableStorage>, #Optional.some!enumelt
switch_enum_addr %0 : $*Optional<NestedUnreferenceableStorage>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1:
br bb3

bb2:
br bb3

bb3:
dealloc_stack %0 : $*Optional<NestedUnreferenceableStorage>
%8 = tuple ()
return %8 : $()
}