Skip to content

[SILGen] Load trivial in consuming switch. #72494

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
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
5 changes: 4 additions & 1 deletion lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2705,7 +2705,10 @@ void PatternMatchEmission::emitDestructiveCaseBlocks() {
mv.forward(SGF),
enumCase);
if (payload->getType().isLoadable(SGF.F)) {
payload = SGF.B.createLoad(loc, payload, LoadOwnershipQualifier::Take);
payload = SGF.B.createLoad(loc, payload,
payload->getType().isTrivial(SGF.F)
? LoadOwnershipQualifier::Trivial
: LoadOwnershipQualifier::Take);
}
visit(subPattern,
SGF.emitManagedRValueWithCleanup(payload));
Expand Down
37 changes: 37 additions & 0 deletions test/SILGen/moveonly_consuming_switch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// RUN: %target-swift-frontend \
// RUN: -emit-silgen \
// RUN: %s \
// RUN: -enable-experimental-feature BorrowingSwitch \
// RUN: -enable-experimental-feature MoveOnlyPartialConsumption \
// RUN: -enable-experimental-feature NoncopyableGenerics \
// RUN: | %FileCheck %s

enum MaybeMaybeVoid<Wrapped: ~Copyable>: ~Copyable {
case none(())
case some(Wrapped)
}

// CHECK-LABEL: sil {{.*}}[ossa] @maybeMaybeVoid2Optional {{.*}} {
// CHECK: [[STACK:%[^,]+]] = alloc_stack
// CHECK: [[ADDR:%[^,]+]] = mark_unresolved_non_copyable_value [consumable_and_assignable] [[STACK]]
// CHECK: [[ACCESS:%[^,]+]] = begin_access [read] [static] [no_nested_conflict] [[ADDR]]
// CHECK: switch_enum_addr [[ACCESS]]
// CHECK-SAME: case #MaybeMaybeVoid.none!enumelt: [[NONE_BLOCK:bb[0-9]+]]
// CHECK: [[NONE_BLOCK]]:
// CHECK: [[REGISTER_14:%[^,]+]] = tuple ()
// CHECK: end_access [[ACCESS]]
// CHECK: [[ACCESS_AGAIN:%[^,]+]] = begin_access [deinit] [static] [no_nested_conflict] [[ADDR]]
// CHECK: [[NONE_ADDR:%[^,]+]] = unchecked_take_enum_data_addr [[ACCESS_AGAIN]]
// CHECK-SAME: #MaybeMaybeVoid.none!enumelt
// Verify that the load is trivial.
// CHECK: load [trivial] [[NONE_ADDR]]
// CHECK-LABEL: } // end sil function 'maybeMaybeVoid2Optional'
@_silgen_name("maybeMaybeVoid2Optional")
func maybeMaybeVoid2Optional<Wrapped: ~Copyable>(_ o2: consuming MaybeMaybeVoid<Wrapped>) -> Optional<Wrapped> {
switch consume o2 {
case .none(let void):
return .none
case .some(let wrapped):
return .some(wrapped)
}
}