Skip to content

Fixes in SILGen for opaque value support in coroutines #58696

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
May 11, 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
8 changes: 5 additions & 3 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3119,7 +3119,7 @@ class ArgEmitter {
auto emissionKind = SGFAccessKind::BorrowedObjectRead;
for (auto param : claimedParams) {
assert(!param.isConsumed());
if (param.isIndirectInGuaranteed()) {
if (param.isIndirectInGuaranteed() && SGF.silConv.useLoweredAddresses()) {
emissionKind = SGFAccessKind::BorrowedAddressRead;
break;
}
Expand Down Expand Up @@ -3497,8 +3497,10 @@ static void emitBorrowedLValueRecursive(SILGenFunction &SGF,

// Load if necessary.
assert(!param.isConsumed() && "emitting borrow into consumed parameter?");
if (!param.isIndirectInGuaranteed() && value.getType().isAddress()) {
value = SGF.B.createFormalAccessLoadBorrow(loc, value);
if (value.getType().isAddress()) {
if (!param.isIndirectInGuaranteed() || !SGF.silConv.useLoweredAddresses()) {
value = SGF.B.createFormalAccessLoadBorrow(loc, value);
}
}

assert(param.getInterfaceType() == value.getType().getASTType());
Expand Down
8 changes: 6 additions & 2 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,8 +1746,12 @@ static ManagedValue manageYield(SILGenFunction &SGF, SILValue value,
if (value.getOwnershipKind() == OwnershipKind::None)
return ManagedValue::forUnmanaged(value);
return ManagedValue::forBorrowedObjectRValue(value);
case ParameterConvention::Indirect_In_Guaranteed:
return ManagedValue::forBorrowedAddressRValue(value);
case ParameterConvention::Indirect_In_Guaranteed: {
bool isOpaque = SGF.getTypeLowering(value->getType()).isAddressOnly() &&
!SGF.silConv.useLoweredAddresses();
return isOpaque ? ManagedValue::forBorrowedObjectRValue(value)
: ManagedValue::forBorrowedAddressRValue(value);
}
}
llvm_unreachable("bad kind");
}
Expand Down
26 changes: 26 additions & 0 deletions test/SILGen/opaque_values_silgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,29 @@ func testCastClassArchetypeToClass<T : AnyObject>(_ o: T) -> C {
break
}
}

class TestGeneric<T> {
init() {}

var generic: T
@_borrowed
var borrowedGeneric: T
}

// CHECK-LABEL: sil hidden [transparent] [ossa] @$s20opaque_values_silgen11TestGenericC08borrowedE0xvr :
// CHECK: bb0(%0 : @guaranteed $TestGeneric<T>):
// CHECK: [[REF:%.*]] = ref_element_addr %0 : $TestGeneric<T>, #TestGeneric.borrowedGeneric
// CHECK: [[ACCESS:%.*]] = begin_access [read] [dynamic] [[REF]] : $*T
// CHECK: [[LD:%.*]] = load_borrow [[ACCESS]] : $*T
// CHECK: yield [[LD]] : $T, resume bb1, unwind bb2
// CHECK: bb1:
// CHECK: end_borrow [[LD]] : $T
// CHECK: end_access [[ACCESS]] : $*T
// CHECK: [[RES:%.*]] = tuple ()
// CHECK: return [[RES]] : $()
// CHECK: bb2:
// CHECK: end_borrow [[LD]] : $T
// CHECK: end_access [[ACCESS]] : $*T
// CHECK: unwind
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen11TestGenericC08borrowedE0xvr'