Skip to content

[SIL Opaque Value] Fix some SILGen issues for closures when opaque values are enabled #42246

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 3 commits into from
Apr 8, 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
43 changes: 2 additions & 41 deletions docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6777,9 +6777,9 @@ Checked Conversions

Some user-level cast operations can fail and thus require runtime checking.

The `unconditional_checked_cast_addr`_, `unconditional_checked_cast_value`_ and `unconditional_checked_cast`_
The `unconditional_checked_cast_addr` and `unconditional_checked_cast`_
instructions performs an unconditional checked cast; it is a runtime failure
if the cast fails. The `checked_cast_addr_br`_, `checked_cast_value_br`_ and `checked_cast_br`_
if the cast fails. The `checked_cast_addr_br`_ and `checked_cast_br`_
terminator instruction performs a conditional checked cast; it branches to one
of two destinations based on whether the cast succeeds or not.

Expand Down Expand Up @@ -6814,25 +6814,6 @@ unconditional_checked_cast_addr
Performs a checked indirect conversion, causing a runtime failure if the
conversion fails.

unconditional_checked_cast_value
````````````````````````````````
::

sil-instruction ::= 'unconditional_checked_cast_value'
sil-operand 'to' sil-type

%1 = unconditional_checked_cast_value %0 : $A to $B
// $A must not be an address
// $B must not be an address
// %1 will be of type $B
// $A is destroyed during the conversion. There is no implicit copy.

Performs a checked conversion, causing a runtime failure if the conversion
fails. Unlike `unconditional_checked_cast`, this destroys its operand and
creates a new value. Consequently, this supports bridging objects to values, as
well as casting to a different ownership classification such as `$AnyObject` to
`$T.Type`.

Runtime Failures
~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -7213,26 +7194,6 @@ An exact cast checks whether the dynamic type is exactly the target
type, not any possible subtype of it. The source and target types
must be class types.

checked_cast_value_br
`````````````````````
::

sil-terminator ::= 'checked_cast_value_br'
sil-operand 'to' sil-type ','
sil-identifier ',' sil-identifier
sil-checked-cast-exact ::= '[' 'exact' ']'

checked_cast_value_br %0 : $A to $B, bb1, bb2
// $A must be not be an address
// $B must be an opaque value
// bb1 must take a single argument of type $B
// bb2 must take no arguments

Performs a checked opaque conversion from ``$A`` to ``$B``. If the conversion
succeeds, control is transferred to ``bb1``, and the result of the cast is
passed into ``bb1`` as an argument. If the conversion fails, control is
transferred to ``bb2``.

checked_cast_addr_br
````````````````````
::
Expand Down
10 changes: 6 additions & 4 deletions lib/SILGen/SILGenEpilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ void SILGenFunction::prepareEpilog(Optional<Type> directResultType,
// Set NeedsReturn for indirect or direct results. This ensures that SILGen
// emits unreachable if there is no source level return.
NeedsReturn = !(*directResultType)->isEqual(TupleType::getEmpty(getASTContext()));
for (auto directResult : fnConv.getDirectSILResults()) {
SILType resultType = F.getLoweredType(F.mapTypeIntoContext(
fnConv.getSILType(directResult, getTypeExpansionContext())));
epilogBB->createPhiArgument(resultType, OwnershipKind::Owned);
if (NeedsReturn) {
for (auto directResult : fnConv.getDirectSILResults()) {
SILType resultType = F.getLoweredType(F.mapTypeIntoContext(
fnConv.getSILType(directResult, getTypeExpansionContext())));
epilogBB->createPhiArgument(resultType, OwnershipKind::Owned);
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion lib/SILGen/SILGenProlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,11 @@ static void emitCaptureArguments(SILGenFunction &SGF,
// Non-escaping stored decls are captured as the address of the value.
auto type = getVarTypeInCaptureContext();
SILType ty = SGF.getLoweredType(type);
if (SGF.SGM.M.useLoweredAddresses()) {
auto argConv = SGF.F.getConventions().getSILArgumentConvention(
SGF.F.begin()->getNumArguments());
bool isInOut = (argConv == SILArgumentConvention::Indirect_Inout ||
argConv == SILArgumentConvention::Indirect_InoutAliasable);
if (isInOut || SGF.SGM.M.useLoweredAddresses()) {
ty = ty.getAddressType();
}
SILValue arg = SGF.F.begin()->createFunctionArgument(ty, VD);
Expand Down
17 changes: 17 additions & 0 deletions test/SILGen/opaque_values_silgen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,20 @@ extension Array where Element == Int {
}
}
}

// CHECK-LABEL: sil private [ossa] @$s20opaque_values_silgen22anon_read_only_captureyS2iFSiyXEfU_ : $@convention(thin) (@inout_aliasable Int) -> Int {
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen22anon_read_only_captureyS2iFSiyXEfU_'
func anon_read_only_capture(_ x: Int) -> Int {
var x = x
return ({ x })()
}


// CHECK-LABEL: sil private [ossa] @$s20opaque_values_silgen22testEmptyReturnClosureyyFyycyKXEfu_yycfU_ : $@convention(thin) @substituted <τ_0_0> () -> @out τ_0_0 for <()> {
// CHECK-NOT: bb1
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen22testEmptyReturnClosureyyFyycyKXEfu_yycfU_'
func testEmptyReturnClosure() {
func bar() {}
let b = nil ?? { bar() }
}