Skip to content

SIL: fix the ownership computation of struct_extract and tuple_extract #80486

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 1 commit into from
Apr 3, 2025
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
23 changes: 21 additions & 2 deletions lib/SIL/IR/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,27 @@ CONSTANT_OWNERSHIP_INST(None, TypeValue)

#undef CONSTANT_OWNERSHIP_INST

ValueOwnershipKind ValueOwnershipKindClassifier::visitStructExtractInst(StructExtractInst *sei) {
if (sei->getType().isTrivial(*sei->getFunction()) ||
// A struct value can have "none" ownership even if its type is not trivial.
// This happens when the struct/tuple contains a non-trivial enum, but it's initialized with
// a trivial enum case (e.g. with `Optional.none`).
sei->getOperand()->getOwnershipKind() == OwnershipKind::None) {
return OwnershipKind::None;
}
return OwnershipKind::Guaranteed;
}

ValueOwnershipKind ValueOwnershipKindClassifier::visitTupleExtractInst(TupleExtractInst *tei) {
if (tei->getType().isTrivial(*tei->getFunction()) ||
// A tuple value can have "none" ownership even if its type is not trivial.
// This happens when the struct/tuple contains a non-trivial enum, but it's initialized with
// a trivial enum case (e.g. with `Optional.none`).
tei->getOperand()->getOwnershipKind() == OwnershipKind::None)
return OwnershipKind::None;
return OwnershipKind::Guaranteed;
}

#define CONSTANT_OR_NONE_OWNERSHIP_INST(OWNERSHIP, INST) \
ValueOwnershipKind ValueOwnershipKindClassifier::visit##INST##Inst( \
INST##Inst *I) { \
Expand All @@ -192,8 +213,6 @@ CONSTANT_OWNERSHIP_INST(None, TypeValue)
} \
return OwnershipKind::OWNERSHIP; \
}
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, StructExtract)
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, TupleExtract)
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, TuplePackExtract)
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, DifferentiableFunctionExtract)
CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, LinearFunctionExtract)
Expand Down
40 changes: 40 additions & 0 deletions test/SILOptimizer/redundant_load_elim_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ struct ExistentialIntPair {
var x: Int
}

public struct S {
var e: Optional<String>
}


sil_global @total : $Int32

sil @use : $@convention(thin) (Builtin.Int32) -> ()
Expand Down Expand Up @@ -1703,3 +1708,38 @@ bb0(%0 : $*ExistentialIntPair, %1 : $*ExistentialIntPair):
return %4
}

// CHECK-LABEL: sil [ossa] @struct_of_optional_none :
// CHECK: [[E:%.*]] = enum
// CHECK: [[S:%.*]] = struct $S ([[E]] : $Optional<String>)
// CHECK: [[F:%.*]] = struct_extract [[S]] : $S, #S.e
// CHECK: return [[F]]
// CHECK-LABEL: } // end sil function 'struct_of_optional_none'
sil [ossa] @struct_of_optional_none : $@convention(thin) () -> @owned Optional<String> {
bb0:
%0 = enum $Optional<String>, #Optional.none!enumelt
%1 = struct $S (%0)
%2 = alloc_stack $S
store %1 to [init] %2
%4 = struct_element_addr %2, #S.e
%5 = load [take] %4
dealloc_stack %2
return %5
}

// CHECK-LABEL: sil [ossa] @tuple_of_optional_none :
// CHECK: [[E:%.*]] = enum
// CHECK: [[T:%.*]] = tuple ([[E]] : $Optional<String>, %0 : $Int)
// CHECK: [[F:%.*]] = tuple_extract [[T]] : $(Optional<String>, Int), 0
// CHECK: return [[F]]
// CHECK-LABEL: } // end sil function 'tuple_of_optional_none'
sil [ossa] @tuple_of_optional_none : $@convention(thin) (Int) -> @owned Optional<String> {
bb0(%0 : $Int):
%1 = enum $Optional<String>, #Optional.none!enumelt
%2 = tuple (%1, %0)
%3 = alloc_stack $(Optional<String>, Int)
store %2 to [init] %3
%5 = tuple_element_addr %3, 0
%6 = load [take] %5
dealloc_stack %3
return %6
}