@@ -183,6 +183,27 @@ CONSTANT_OWNERSHIP_INST(None, TypeValue)
183183
184184#undef CONSTANT_OWNERSHIP_INST
185185
186+ ValueOwnershipKind ValueOwnershipKindClassifier::visitStructExtractInst (StructExtractInst *sei) {
187+ if (sei->getType ().isTrivial (*sei->getFunction ()) ||
188+ // A struct value can have "none" ownership even if its type is not trivial.
189+ // This happens when the struct/tuple contains a non-trivial enum, but it's initialized with
190+ // a trivial enum case (e.g. with `Optional.none`).
191+ sei->getOperand ()->getOwnershipKind () == OwnershipKind::None) {
192+ return OwnershipKind::None;
193+ }
194+ return OwnershipKind::Guaranteed;
195+ }
196+
197+ ValueOwnershipKind ValueOwnershipKindClassifier::visitTupleExtractInst (TupleExtractInst *tei) {
198+ if (tei->getType ().isTrivial (*tei->getFunction ()) ||
199+ // A tuple value can have "none" ownership even if its type is not trivial.
200+ // This happens when the struct/tuple contains a non-trivial enum, but it's initialized with
201+ // a trivial enum case (e.g. with `Optional.none`).
202+ tei->getOperand ()->getOwnershipKind () == OwnershipKind::None)
203+ return OwnershipKind::None;
204+ return OwnershipKind::Guaranteed;
205+ }
206+
186207#define CONSTANT_OR_NONE_OWNERSHIP_INST (OWNERSHIP, INST ) \
187208 ValueOwnershipKind ValueOwnershipKindClassifier::visit##INST##Inst( \
188209 INST##Inst *I) { \
@@ -192,8 +213,6 @@ CONSTANT_OWNERSHIP_INST(None, TypeValue)
192213 } \
193214 return OwnershipKind::OWNERSHIP; \
194215 }
195- CONSTANT_OR_NONE_OWNERSHIP_INST (Guaranteed, StructExtract)
196- CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, TupleExtract)
197216CONSTANT_OR_NONE_OWNERSHIP_INST (Guaranteed, TuplePackExtract)
198217CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, DifferentiableFunctionExtract)
199218CONSTANT_OR_NONE_OWNERSHIP_INST(Guaranteed, LinearFunctionExtract)
0 commit comments