Skip to content

Commit 3459000

Browse files
authored
Merge pull request #42246 from meg-gupta/closureopaque
[SIL Opaque Value] Fix some SILGen issues for closures when opaque values are enabled
2 parents 4399670 + 3b8656c commit 3459000

File tree

4 files changed

+30
-46
lines changed

4 files changed

+30
-46
lines changed

docs/SIL.rst

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6777,9 +6777,9 @@ Checked Conversions
67776777

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

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

@@ -6814,25 +6814,6 @@ unconditional_checked_cast_addr
68146814
Performs a checked indirect conversion, causing a runtime failure if the
68156815
conversion fails.
68166816

6817-
unconditional_checked_cast_value
6818-
````````````````````````````````
6819-
::
6820-
6821-
sil-instruction ::= 'unconditional_checked_cast_value'
6822-
sil-operand 'to' sil-type
6823-
6824-
%1 = unconditional_checked_cast_value %0 : $A to $B
6825-
// $A must not be an address
6826-
// $B must not be an address
6827-
// %1 will be of type $B
6828-
// $A is destroyed during the conversion. There is no implicit copy.
6829-
6830-
Performs a checked conversion, causing a runtime failure if the conversion
6831-
fails. Unlike `unconditional_checked_cast`, this destroys its operand and
6832-
creates a new value. Consequently, this supports bridging objects to values, as
6833-
well as casting to a different ownership classification such as `$AnyObject` to
6834-
`$T.Type`.
6835-
68366817
Runtime Failures
68376818
~~~~~~~~~~~~~~~~
68386819

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

7216-
checked_cast_value_br
7217-
`````````````````````
7218-
::
7219-
7220-
sil-terminator ::= 'checked_cast_value_br'
7221-
sil-operand 'to' sil-type ','
7222-
sil-identifier ',' sil-identifier
7223-
sil-checked-cast-exact ::= '[' 'exact' ']'
7224-
7225-
checked_cast_value_br %0 : $A to $B, bb1, bb2
7226-
// $A must be not be an address
7227-
// $B must be an opaque value
7228-
// bb1 must take a single argument of type $B
7229-
// bb2 must take no arguments
7230-
7231-
Performs a checked opaque conversion from ``$A`` to ``$B``. If the conversion
7232-
succeeds, control is transferred to ``bb1``, and the result of the cast is
7233-
passed into ``bb1`` as an argument. If the conversion fails, control is
7234-
transferred to ``bb2``.
7235-
72367197
checked_cast_addr_br
72377198
````````````````````
72387199
::

lib/SILGen/SILGenEpilog.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ void SILGenFunction::prepareEpilog(Optional<Type> directResultType,
2929
// Set NeedsReturn for indirect or direct results. This ensures that SILGen
3030
// emits unreachable if there is no source level return.
3131
NeedsReturn = !(*directResultType)->isEqual(TupleType::getEmpty(getASTContext()));
32-
for (auto directResult : fnConv.getDirectSILResults()) {
33-
SILType resultType = F.getLoweredType(F.mapTypeIntoContext(
34-
fnConv.getSILType(directResult, getTypeExpansionContext())));
35-
epilogBB->createPhiArgument(resultType, OwnershipKind::Owned);
32+
if (NeedsReturn) {
33+
for (auto directResult : fnConv.getDirectSILResults()) {
34+
SILType resultType = F.getLoweredType(F.mapTypeIntoContext(
35+
fnConv.getSILType(directResult, getTypeExpansionContext())));
36+
epilogBB->createPhiArgument(resultType, OwnershipKind::Owned);
37+
}
3638
}
3739
}
3840

lib/SILGen/SILGenProlog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,11 @@ static void emitCaptureArguments(SILGenFunction &SGF,
458458
// Non-escaping stored decls are captured as the address of the value.
459459
auto type = getVarTypeInCaptureContext();
460460
SILType ty = SGF.getLoweredType(type);
461-
if (SGF.SGM.M.useLoweredAddresses()) {
461+
auto argConv = SGF.F.getConventions().getSILArgumentConvention(
462+
SGF.F.begin()->getNumArguments());
463+
bool isInOut = (argConv == SILArgumentConvention::Indirect_Inout ||
464+
argConv == SILArgumentConvention::Indirect_InoutAliasable);
465+
if (isInOut || SGF.SGM.M.useLoweredAddresses()) {
462466
ty = ty.getAddressType();
463467
}
464468
SILValue arg = SGF.F.begin()->createFunctionArgument(ty, VD);

test/SILGen/opaque_values_silgen.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,3 +367,20 @@ extension Array where Element == Int {
367367
}
368368
}
369369
}
370+
371+
// CHECK-LABEL: sil private [ossa] @$s20opaque_values_silgen22anon_read_only_captureyS2iFSiyXEfU_ : $@convention(thin) (@inout_aliasable Int) -> Int {
372+
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen22anon_read_only_captureyS2iFSiyXEfU_'
373+
func anon_read_only_capture(_ x: Int) -> Int {
374+
var x = x
375+
return ({ x })()
376+
}
377+
378+
379+
// CHECK-LABEL: sil private [ossa] @$s20opaque_values_silgen22testEmptyReturnClosureyyFyycyKXEfu_yycfU_ : $@convention(thin) @substituted <τ_0_0> () -> @out τ_0_0 for <()> {
380+
// CHECK-NOT: bb1
381+
// CHECK-LABEL: } // end sil function '$s20opaque_values_silgen22testEmptyReturnClosureyyFyycyKXEfu_yycfU_'
382+
func testEmptyReturnClosure() {
383+
func bar() {}
384+
let b = nil ?? { bar() }
385+
}
386+

0 commit comments

Comments
 (0)