Skip to content

[AddressLowering] Storage root inherits lexical. #61747

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 5 commits into from
Oct 27, 2022
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
3 changes: 3 additions & 0 deletions lib/SIL/IR/SILInstructions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2884,6 +2884,9 @@ ReturnInst::ReturnInst(SILFunction &func, SILDebugLocation debugLoc,

bool OwnershipForwardingMixin::hasSameRepresentation(SILInstruction *inst) {
switch (inst->getKind()) {
// Explicitly list instructions which definitely involve a representation
// change.
case SILInstructionKind::SwitchEnumInst:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice practice. Too bad we don't do it elsewhere.

default:
// Conservatively assume that a conversion changes representation.
// Operations can be added as needed to participate in SIL opaque values.
Expand Down
5 changes: 5 additions & 0 deletions lib/SILGen/SILGenPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,11 @@ void PatternMatchEmission::emitEnumElementDispatch(
// be passed take_on_success if src is an address only type.
assert(src.getFinalConsumption() != CastConsumptionKind::TakeOnSuccess &&
"Can only have take_on_success with address only values");
if (src.getType().isAddressOnly(SGF.F) &&
src.getOwnershipKind() == OwnershipKind::Guaranteed) {
// If it's an opaque value with guaranteed ownership, we need to copy.
src = src.copy(SGF, PatternMatchStmt);
}

// Finally perform the enum element dispatch.
return emitEnumElementObjectDispatch(rows, src, handleCase, outerFailure,
Expand Down
18 changes: 10 additions & 8 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ void ValueStorageMap::recordComposingUseProjection(Operand *oper,

storage.isUseProjection = true;

if (EnumDecl *enumDecl = userValue->getType().getEnumOrBoundGenericEnum()) {
if (userValue->getType().getEnumOrBoundGenericEnum()) {
storage.initializesEnum = true;
}
assert(!storage.isPhiProjection());
Expand Down Expand Up @@ -1140,7 +1140,7 @@ bool OpaqueStorageAllocation::checkStorageDominates(
}
// Handle both phis and terminator results.
auto *bbArg = cast<SILPhiArgument>(incomingValue);
// The storage block must strictly dominate the phi.
// The storage block must strictly dominate the argument block.
if (!pass.domInfo->properlyDominates(allocInst->getParent(),
bbArg->getParent())) {
return false;
Expand Down Expand Up @@ -2772,13 +2772,15 @@ void UseRewriter::visitBeginBorrowInst(BeginBorrowInst *borrow) {

// Borrows are irrelevant unless they are marked lexical.
if (borrow->isLexical()) {
if (auto *allocStack = dyn_cast<AllocStackInst>(address)) {
allocStack->setIsLexical();
return;
if (auto base = getAccessBase(address)) {
if (auto *allocStack = dyn_cast<AllocStackInst>(base)) {
allocStack->setIsLexical();
return;
}
// Function arguments are inherently lexical.
if (isa<SILFunctionArgument>(base))
return;
}
// Function arguments are inherently lexical.
if (isa<SILFunctionArgument>(address))
return;

SWIFT_ASSERT_ONLY(address->dump());
llvm_unreachable("^^^ unknown lexical address producer");
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/Mandatory/AddressLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace swift {
///
/// 4. Rewritten. The definition of this opaque value is fully translated
/// into lowered SIL. Instructions are typically materialized and rewritten at
/// the same time. A indirect result, however, is materialized as soon as its
/// the same time. An indirect result, however, is materialized as soon as its
/// alloc_stack is emitted, but only rewritten once the call itself is
/// rewritten.
///
Expand Down
49 changes: 48 additions & 1 deletion test/SILOptimizer/address_lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public protocol Comparable {
static func < (lhs: Self, rhs: Self) -> Bool
}

sil [ossa] @unknown : $@convention(thin) () -> ()
sil [ossa] @getT : $@convention(thin) <T> () -> @out T
sil [ossa] @getPair : $@convention(thin) <T> () -> @out Pair<T>
sil [ossa] @takeGuaranteedObject : $@convention(thin) (@guaranteed AnyObject) -> ()
sil [ossa] @takeIndirectClass : $@convention(thin) (@in_guaranteed C) -> ()
sil [ossa] @takeTuple : $@convention(thin) <τ_0_0> (@in_guaranteed (τ_0_0, C)) -> ()
Expand Down Expand Up @@ -1225,6 +1228,51 @@ bb2(%9 : $Error):
throw %9 : $Error
}

// CHECK-LABEL: sil [ossa] @lexical_borrow_struct_extract {{.*}} {
// CHECK: [[PAIR_ADDR:%[^,]+]] = alloc_stack [lexical]
// CHECK: [[X_ADDR:%[^,]+]] = struct_element_addr [[PAIR_ADDR]]
// CHECK: apply {{%[^,]+}}<T>([[X_ADDR]])
// CHECK: destroy_addr [[PAIR_ADDR]]
// CHECK: dealloc_stack [[PAIR_ADDR]]
// CHECK-LABEL: } // end sil function 'lexical_borrow_struct_extract'
sil [ossa] @lexical_borrow_struct_extract : $@convention(thin) <T> () -> () {
%getPair = function_ref @getPair : $@convention(thin) <Tee> () -> @out Pair<Tee>
%instance = apply %getPair<T>() : $@convention(thin) <Tee> () -> @out Pair<Tee>
%scope = begin_borrow %instance : $Pair<T>
%x = struct_extract %scope : $Pair<T>, #Pair.x
%lifetime = begin_borrow [lexical] %x : $T
%takeInGuaranteed = function_ref @takeInGuaranteed : $@convention(thin) <Tee> (@in_guaranteed Tee) -> ()
apply %takeInGuaranteed<T>(%lifetime) : $@convention(thin) <Tee> (@in_guaranteed Tee) -> ()
%unknown = function_ref @unknown : $@convention(thin) () -> ()
apply %unknown() : $@convention(thin) () -> ()
end_borrow %lifetime : $T
end_borrow %scope : $Pair<T>
destroy_value %instance : $Pair<T>
%retval = tuple ()
return %retval : $()
}

// CHECK-LABEL: sil [ossa] @lexical_borrow_struct_extract_arg {{.*}} {
// CHECK: [[X_ADDR:%[^,]+]] = struct_element_addr [[PAIR_ADDR]]
// CHECK: apply {{%[^,]+}}<T>([[X_ADDR]])
// CHECK: destroy_addr [[PAIR_ADDR]]
// CHECK-LABEL: } // end sil function 'lexical_borrow_struct_extract_arg'
sil [ossa] @lexical_borrow_struct_extract_arg : $@convention(thin) <T> (@in Pair<T>) -> () {
entry(%instance : @owned $Pair<T>):
%scope = begin_borrow %instance : $Pair<T>
%x = struct_extract %scope : $Pair<T>, #Pair.x
%lifetime = begin_borrow [lexical] %x : $T
%takeInGuaranteed = function_ref @takeInGuaranteed : $@convention(thin) <Tee> (@in_guaranteed Tee) -> ()
apply %takeInGuaranteed<T>(%lifetime) : $@convention(thin) <Tee> (@in_guaranteed Tee) -> ()
%unknown = function_ref @unknown : $@convention(thin) () -> ()
apply %unknown() : $@convention(thin) () -> ()
end_borrow %lifetime : $T
end_borrow %scope : $Pair<T>
destroy_value %instance : $Pair<T>
%retval = tuple ()
return %retval : $()
}

sil hidden [ossa] @testBeginApplyDeadYield : $@convention(thin) <T> (@guaranteed TestGeneric<T>) -> () {
bb0(%0 : @guaranteed $TestGeneric<T>):
%2 = class_method %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric!read : <T> (TestGeneric<T>) -> () -> (), $@yield_once @convention(method) <τ_0_0> (@guaranteed TestGeneric<τ_0_0>) -> @yields @in_guaranteed τ_0_0
Expand Down Expand Up @@ -1493,4 +1541,3 @@ bb0(%0 : @guaranteed $T):
destroy_value %3 : $U
return %6 : $U
}

54 changes: 54 additions & 0 deletions test/SILOptimizer/opaque_values_Onone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,57 @@
func generic_identity<T>(t: T) -> T {
return t
}


enum Maybe1<T : Equatable> {
case nope
case yep(T)
// CHECK-LABEL: sil hidden @maybe1_compare {{.*}} {
// CHECK: [[LHS_ADDR:%[^,]+]] = alloc_stack [lexical] $Maybe1
// CHECK: [[RHS_ADDR:%[^,]+]] = alloc_stack [lexical] $Maybe1
// CHECK: switch_enum_addr [[LHS_ADDR]] : $*Maybe1<T>, case #Maybe1.yep!enumelt: [[L_YEP:bb[0-9]+]], case #Maybe1.nope!enumelt: {{bb[0-9]+}}
// CHECK: [[L_YEP]]:
// CHECK: unchecked_take_enum_data_addr [[LHS_ADDR]] : $*Maybe1<T>, #Maybe1.yep!enumelt
// CHECK: switch_enum_addr [[RHS_ADDR]] : $*Maybe1<T>, case #Maybe1.yep!enumelt: [[L_AND_R_YEP:bb[0-9]+]], default {{bb[0-9]+}}
// CHECK: [[L_AND_R_YEP]]:
// CHECK: unchecked_take_enum_data_addr [[RHS_ADDR]] : $*Maybe1<T>, #Maybe1.yep!enumelt
// CHECK-LABEL: } // end sil function 'maybe1_compare'
@_silgen_name("maybe1_compare")
static func compare(_ lhs: Maybe1, _ rhs: Maybe1) -> Bool {
switch (lhs, rhs) {
case (.yep(let l), .yep(let r)):
return l == r
case (.nope, .nope):
return true
default:
return false
}
}
}

enum Maybe2<T : Equatable> {
case nope
case yep(T, T)

// CHECK-LABEL: sil hidden @maybe2_compare {{.*}} {
// CHECK: [[LHS_ADDR:%[^,]+]] = alloc_stack [lexical] $Maybe2<T>
// CHECK: [[RHS_ADDR:%[^,]+]] = alloc_stack [lexical] $Maybe2<T>
// CHECK: switch_enum_addr [[LHS_ADDR]] : $*Maybe2<T>, case #Maybe2.yep!enumelt: [[L_YEP:bb[0-9]+]], case #Maybe2.nope!enumelt: {{bb[0-9]+}}
// CHECK: [[L_YEP]]:
// CHECK: unchecked_take_enum_data_addr [[LHS_ADDR]] : $*Maybe2<T>, #Maybe2.yep!enumelt
// CHECK: switch_enum_addr [[RHS_ADDR]] : $*Maybe2<T>, case #Maybe2.yep!enumelt: [[R_YEP:bb[0-9]+]], default {{bb[0-9]+}}
// CHECK: [[L_AND_R_YEP]]:
// CHECK: unchecked_take_enum_data_addr [[RHS_ADDR]] : $*Maybe2<T>, #Maybe2.yep!enumelt
// CHECK-LABEL: } // end sil function 'maybe2_compare'
@_silgen_name("maybe2_compare")
static func compare(_ lhs: Maybe2, _ rhs: Maybe2) -> Bool {
switch (lhs, rhs) {
case (.yep(let l, _), .yep(let r, _)):
return l == r
case (.nope, .nope):
return true
default:
return false
}
}
}