Commit Graph

224 Commits

Author SHA1 Message Date
Michael Gottesman
26a734e58e [sil] Rename ValueOwnershipKind::{Any,None} 2019-10-25 10:28:25 -07:00
Michael Gottesman
6a54531811 [silgen] Add SILGen support for emitting copy_unmanaged_value instead of unmanaged_to_ref + strong_retain.
This will ensure that the optimizer never eliminates the strong_retain. This
operation is meant to be unmanaged, we should respect the user's choice here
even in optimized builds.
2019-08-26 09:40:27 -07:00
Jordan Rose
844ae38722 Remove some Swift STLExtras that LLVM now provides (#26443)
No functionality change.
2019-07-31 18:34:52 -07:00
Slava Pestov
9587a3a1f6 Merge pull request #23170 from slavapestov/enable-sil-resilience-expansion
Start using the best resilience expansion in SIL
2019-04-27 07:07:24 -04:00
Slava Pestov
16d5716e71 SIL: Use the best resilience expansion when lowering types
This is a large patch; I couldn't split it up further while still
keeping things working. There are four things being changed at
once here:

- Places that call SILType::isAddressOnly()/isLoadable() now call
  the SILFunction overload and not the SILModule one.

- SILFunction's overloads of getTypeLowering() and getLoweredType()
  now pass the function's resilience expansion down, instead of
  hardcoding ResilienceExpansion::Minimal.

- Various other places with '// FIXME: Expansion' now use a better
  resilience expansion.

- A few tests were updated to reflect SILGen's improved code
  generation, and some new tests are added to cover more code paths
  that previously were uncovered and only manifested themselves as
  standard library build failures while I was working on this change.
2019-04-26 22:47:59 -04:00
Michael Gottesman
ade2df1253 [silgen] Fix SILGenBuilder::emitDestructureValueOperation to create all sub-ManagedValues before invoking the user defined function.
This is necessary since our func may want to emit conditional code with an early
exit, emitting unused cleanups from the current scope via the function
emitBranchAndCleanups(). If we have not yet created those cleanups, we will
introduce a leak along that path.

rdar://49990484
2019-04-26 12:27:06 -07:00
Slava Pestov
9a1abf705a SILGen: Remove SILGenSILBuilder
This reverts commit 59cc3c1216fbb1719e5357dcef3f8b249528fc74.
2019-04-25 02:06:14 -04:00
Michael Gottesman
3419662847 [silgen] Split off SILGenSILBuilder from SILGenBuilder and have SILGenBuilder inherit from it.
This prevents ManagedValue APIs on SILGenBuilder from by mistake accesing
non-conformance tracking APIs on SILBuilder.

NOTE: Eventually, we should make SILGenBuilder compose with SILGenSILBuilder and
then rename SILGenBuilder to ManagedValueBuilder. I did not do that in this PR
since it becomes very disruptive since there is still a lot of code in SILGen
that uses SILValue APIs and I have not found a concise way to write such
code. But this patch at least defines away this error which has bitten us
before.
2019-04-06 00:53:47 -07:00
Michael Gottesman
bec13367d2 [silgen] Eliminate some unneeded constructors from SILGenBuilder. 2019-04-05 12:49:19 -07:00
Slava Pestov
8915f96e3e SIL: Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction) 2019-03-12 01:16:04 -04:00
Arnold Schwaighofer
cb0c53abee SIL: Remove isEscapedByUser flag on convert_escape_to_noescape instruction
It was only used for materializeForSet and is now dead code.
2019-01-04 09:21:38 -08:00
Michael Gottesman
0af0d5fddc [ownership] Replace ValueOwnershipKind::Trivial with ValueOwnershipKind::Any.
In a previous commit, I banned in the verifier any SILValue from producing
ValueOwnershipKind::Any in preparation for this.

This change arises out of discussions in between John, Andy, and I around
ValueOwnershipKind::Trivial. The specific realization was that this ownership
kind was an unnecessary conflation of the a type system idea (triviality) with
an ownership idea (@any, an ownership kind that is compatible with any other
ownership kind at value merge points and can only create). This caused the
ownership model to have to contort to handle the non-payloaded or trivial cases
of non-trivial enums. This is unnecessary if we just eliminate the any case and
in the verifier separately verify that trivial => @any (notice that we do not
verify that @any => trivial).

NOTE: This is technically an NFC intended change since I am just replacing
Trivial with Any. That is why if you look at the tests you will see that I
actually did not need to update anything except removing some @trivial ownership
since @any ownership is represented without writing @any in the parsed sil.

rdar://46294760
2018-12-04 23:01:43 -08:00
Arnold Schwaighofer
b102c7f6b4 Parser/Sema/SILGen changes for @_dynamicReplacement(for:)
Dynamic replacements are currently written in extensions as

extension ExtendedType {
  @_dynamicReplacement(for: replacedFun())
  func replacement() { }
}

The runtime implementation allows an implementation in the future where
dynamic replacements are gather in a scope and can be dynamically
enabled and disabled.

For example:

dynamic_extension_scope CollectionOfReplacements {
  extension ExtentedType {
    func replacedFun() {}
  }

  extension ExtentedType2 {
    func replacedFun() {}
  }
}

CollectionOfReplacements.enable()
CollectionOfReplacements.disable()
2018-11-06 09:58:36 -08:00
John McCall
7da688d75a Always manage subobject projections with formal-access cleanups.
To make that work, enter appropriate scopes (ArgumentScopes and
FormalEvaluationScopes) at a bunch of places.  But note that l-value
emission generally can't enter such a scope, so in generic routines
like emitOpenExistentialExpr we have to just assert that we're
already in a scope.
2018-11-03 02:14:06 -04:00
Michael Gottesman
6b9986839c [silgen] Fix the ownership of enum element pattern initialization.
The problem here is that we were taking advantage of swift not-trashing the
original memory location after performing a load [take] to use load [take] as a
+0 load. This breaks the ownership verifier since the load [take] is never
actually destroyed. Instead this commit changes this code to use a load_borrow.

I found this when trying to enable ownership verification on
test/SILGen/indirect_enum.swift using some out of tree work that should have
fixed all of the ownership issues with SILGenPattern. Turns out I can just
enable ownership verification with just this change... so I did that as well in
this PR.

rdar://29791263
2018-10-14 15:39:49 -07:00
Michael Gottesman
d57a88af0d [gardening] Rename references to SILPHIArgument => SILPhiArgument. 2018-09-25 22:23:34 -07:00
Michael Gottesman
a57ad403ec [silgen] Now that we have destructure, use it in RValue to reduce copies emitted by SILGen.
rdar://43493020
2018-08-20 08:52:51 -07:00
Andrew Trick
c9033ed938 Add a SIL attribute [without_actually_escaping].
ConvertFunction and reabstraction thunks need this attribute. Otherwise,
there is no way to identify that withoutActuallyEscaping was used
to explicitly perform a conversion.

The destination of a [without_actually_escaping] conversion always has
an escaping function type. The source may have either an escaping or
@noescape function type. The conversion itself may be a nop, and there
is nothing distinctive about it. The thing that is special about these
conversions is that the source function type may have unboxed
captures. i.e. they have @inout_aliasable parameters. Exclusivity
requires that the compiler enforce a SIL data flow invariant that
nonescaping closures with unboxed captures can never be stored or
passed as an @escaping function argument. Adding this attribute allows
the compiler to enforce the invariant in general with an escape hatch
for withoutActuallyEscaping.
2018-08-14 17:14:25 -07:00
John McCall
7a4aeed570 Implement generalized accessors using yield-once coroutines.
For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
2018-07-23 18:59:58 -04:00
Slava Pestov
5030dbb6ae SILGen: Avoid calling getContextSubstitutionMap() on a protocol type
There's no point.
2018-07-16 16:44:27 -07:00
John McCall
34b0cbc11d Merge pull request #16237 from davezarzycki/metaprogram_ref_storage_types
[AST] NFC: Enable reference storage type meta-programming
2018-07-05 14:45:38 -04:00
Ben Cohen
2b04e9f105 Suppress a number of warnings in no-assert builds (#17721)
* Supress a number of warnings about things used only in asserts

* Re-use a couple of variables instead of supressing the warning
2018-07-04 07:15:14 -07:00
David Zarzycki
057bbb366a [IRGen] Adopt reference storage type meta-programming macros
This commit also fixes reference storage extra inhabitant bugs.
2018-06-30 11:48:47 -04:00
David Zarzycki
54e85ba49a [SILGen] NFC: Adopt reference storage type meta-programming macros 2018-06-30 06:44:33 -04:00
Doug Gregor
ef020c74aa Eliminate all vestiges of Substitution and SubstitutionList.
Introduced during the bring-up of the generics system in July, 2012,
Substitution (and SubstitutionList) has been completely superseded by
SubstitutionMap. R.I.P.
2018-05-11 21:43:40 -07:00
Doug Gregor
17572ced70 [SILGen] Eliminate useConformancesFromSubstitutions() on SubstitutionList.
Update one caller first.
2018-05-11 17:37:26 -07:00
Doug Gregor
4b5abbddbc [SIL] Teach *ApplyInst to traffic in SubstitutionMap.
Push SubstitutionMaps through most of SILGen and the SIL optimizers
that involve the various *ApplyInsts.
2018-05-11 13:18:06 -07:00
David Zarzycki
8c0c55539f [SIL] NFC: Rename misleading getSwiftRValueType() to getASTType()
Reference storage types are not RValues. Also, use more SILType helper
methods to avoid line wrap.
2018-05-04 08:14:38 -04:00
Doug Gregor
408aaa5332 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 08:48:55 -07:00
Doug Gregor
d2cf60c465 Revert "[SIL] Replace more SubstitutionLists with SubstitutionMap" 2018-05-03 08:35:20 -07:00
Doug Gregor
ed1983d9d0 [SIL] Use SubstitutionMap in BuiltinInst. 2018-05-03 00:05:21 -07:00
Huon Wilson
0343a788f7 [SILGen] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:07 +10:00
Arnold Schwaighofer
e36655fddc SILGen: Remove PostponedCleanup in favor or the SIL pass that fixes
closure lifetimes.

SILGen will now unconditionally emit

  %cvt = convert_escape_to_noescape [guaranteed] %op

instructions. The mandatory ClosureLifetimeFixup pass ensures that %op's
lifetime spans %cvt's uses.

The code in DefiniteInitialization that handled a subset of cases is
removed.
2018-04-13 13:44:09 -07:00
Arnold Schwaighofer
36d5408125 SIL: Add an [escaped] attribute to convert_escape_to_noescape instruction
To mark when a user of it is known to escape the value. This happens
with materializeForSet arguments which are captured and used in the
write-back. This means we need to keep the context alive until after
the write-back.

Follow-up patches to fully replace the PostponedCleanup hack in SILGen
by a mandatory SIL transformation pass to guarantee the proper lifetime
will use this flag to be more conservative when extending the lifetime.

The problem:

%pa = partial_apply %f(%some_context)
%cvt = convert_escape_to_noescape [not_guaranteed] [escaped] %pa
%ptr = %materialize_for_set(..., %cvt)
...  write_back
... // <-- %pa needs to be alive until after write_back
2018-04-13 12:40:10 -07:00
Slava Pestov
28d24f8f38 SIL: Remove redundant utility method and rename another one 2018-04-03 18:14:40 -07:00
Arnold Schwaighofer
fce790920f Use the convert_escape_to_noescape [not_guaranteed] instruction 2018-03-30 08:55:24 -07:00
Arnold Schwaighofer
b510227224 SILGen: Remove variable with negated name: 2018-03-30 08:51:06 -07:00
Arnold Schwaighofer
3ab1580cf9 SILGen: Don't postpone the cleanup of the convertEscapeToNoEscape inside an bind optional
This would violate dominance since the convertEscapeToNoEscape
instruction does not dominate the postpone point.

rdar://38124009
2018-03-30 06:20:13 -07:00
Doug Gregor
213ad4fc97 [SILGen] Ensure that we keep the convert_escape_to_noescape argument alive.
Thanks, Arnold!
2018-03-20 13:13:46 -07:00
Doug Gregor
b8efde988f [SILGen] Fix ownership handling for createConvertEscapeToNoEscape().
Introduce SILGenBuilder::createConvertEscapeToNoEscape() to correctly handle
ownership, and switch all relevant callers to it.
2018-03-20 13:02:49 -07:00
Michael Gottesman
e567bc9028 [+0-all-args] Enable +0 normal arguments.
rdar://34222540
2018-03-19 20:25:31 -07:00
Michael Gottesman
8b1d38aba8 [+0-normal-args] When transforming, be sure to use SILGenBuilder APIs to ensure that cleanups are properly forwarded.
Caught by the ownership verifier when updating objc_bridging_peephole.swift for +0 args.
2018-03-11 23:49:51 -07:00
Michael Gottesman
3823ff0958 [+0-all-args] Make SILGenBuilder::createUncheckedBitCast more correct.
The main change here is that we no longer forward ownership through
unchecked_bitwise_cast. The reason why is that unchecked_bitwise_cast creates a
new "reference count" identity to the optimizer. This is reflected in the SIL
ownership model by unchecked_bitwise_cast returning a value with unowned
ownership. We really do not want to traffic in such values, so we copy the value
after the cast to ensure that result is valid.

Found while updating SILGen tests for +0.

rdar://34222540
2018-03-11 23:49:51 -07:00
Michael Gottesman
8af8e4f7c7 [+0-all-args] Fix SILGenBuilder::createFunctionInputArgument(...) for in_guaranteed parameters
This method was not distinguishing in between in_guaranteed and in
parameters. This would cause the curry thunk where this is used to not copy
in_guaranteed parameters before passing in the parameter to the
partial_apply. This can not affect +1 code since the curry thunk will always
have self at +1.

I also refactored code in:

1. SILGenPoly.
2. SILGenProlog.
3. SILGenConstructor.

to use this function instead of their own reimplementations of the same thing.

This should be NFC for +1 code and is tested by test updates when +0 is enabled.

rdar://34222540
2018-03-08 16:51:38 -08:00
Michael Gottesman
76af5c5b16 [silgen] Centralize SwitchEnumBuilder and SwitchCaseFullExpr into the same file: SwitchEnumBuilder.{h,cpp}.
Previously SwitchEnumBuilder was in SILGenBuilder.{h,cpp} and
SwitchEnumCaseFullExpr was in its own file. This really made no sense since:

1. The two classes are inherently related to each other so really should be
together in the source base.
2. SwitchEnumBuilder uses a SILGenBuilder, but is really a separate independent
concept/entity, so there really is no reason to keep it in SILGenBuilder.cpp.

Just a quick fix to eliminate something that was bugging me.

NFC.
2018-02-17 13:09:23 -08:00
Michael Gottesman
0a5f054753 [+0-normal-args] Fix a typo in createTuple where we were looking for the first trivial element instead of the first non-trivial element.
This would cause us to not properly create cleanups on tuples, causing potential
memory leaks. Luckily, I verified that we previously did not call this anywhere
in the compiler by removing the method in question from SILGenBuilder and
performing a successful build of swift. So we do not need to worry about this
bug on 4.1/5.0. I think I added it just for completeness since we had other such
ManagedValue methods.

Caught by the sil ownership verifier while running the argument shuffle test
with +0 rvalues and the commit merged with this commit.

rdar://34222540
2018-02-15 16:43:59 -08:00
Michael Gottesman
398f20c506 [+0-all-args] Add SILGenBuilder APIs for createRefToBridgeObject and use it in emitBuiltinCastToBridgeObject to ensure cleanups are on the actual bridge object.
This eliminates a case where we were not moving a cleanup onto a forwarding case
and instead just jammed the original cleanup and the new value into 1 managed
value.

NFC, but helps the ownership verifier.

rdar://34222540
2018-02-14 15:20:42 -08:00
Michael Gottesman
d6b4fabc0e [silgen] All ownership endowed APIs on SILGenBuilder should use SILGenBuilder SILValue APIs to ensure we get proper conformances.
I was thinking about this last night. I think the reason that this difference
came up is that people saw the SILGenBuilder SILValue based conformance APIs and
copied their calling of SILBuilder SILValue APIs. In the general case this is
not safe, since in some cases we need to add conformances.

Noticed by inspection.
2018-02-12 13:14:45 -08:00
Michael Gottesman
83349e7acf [silgen] Reorganize SILGenBuilder.cpp into two sections, one for SILValue APIs + conformance support and the other ManagedValue APIs.
NFC.
2018-02-12 13:14:45 -08:00
Michael Gottesman
3888b5b054 [+0-normal-args] Use SILGenBuilder in SILGenConvert when injecting an optional so we propagate through +0 vs +1.
This commit does two different things:

1. Changes some code paths in SILGenConvert to use SILGenBuilder APIs.
2. It eliminates a few bugs in this path exposed by running the SILGen tests
with +0 enabled.

The bug specifically is that we are assuming in this code that the input value
is always at +1, so we unconditionally forward the input value and then create
destroys on the output. This is not correct in general (since we have guaranteed
values) and occurs all the time with +0 arguments enabled.

This is tested by running the SILGen tests with +0 arguments enabled.

rdar://34222540
2018-02-12 11:17:55 -08:00