Commit Graph

243 Commits

Author SHA1 Message Date
Slava Pestov
8915f96e3e SIL: Replace SILType::isTrivial(SILModule) with isTrivial(SILFunction) 2019-03-12 01:16:04 -04:00
Raj Barik
2e6b0cbf72 Merge pull request #22247 from rajbarik/raj-recursion-fix
Fix ExistentialSpecializer: Recursive function specialization
2019-03-05 11:02:55 -08:00
Slava Pestov
1944254253 SIL: Use SILFunction type lowering APIs in various places 2019-03-01 02:07:16 -05:00
Raj Barik
6d9f5327bf Fix ExistentialSpecializer: Recursive function specialization 2019-02-23 07:47:50 -08:00
Michael Gottesman
4d90cddd2e [sil-combine] When simplifying convert functions, base whether or not an apply has nothrow on the actual function_ref.
Previously, we based it off of whether or not the original apply had a nothrow
bit. This is incorrect in the case where we added an error result to a function
without an error result. In such a case, we need to /not/ put on the nothrow
bit. This commit generalizes this idea slightly by assuming that if we are asked
to perform this transformation we should just match what the underlying
function_ref (i.e. setting nothrow if the underlying function type has an error
result and not setting nothrow if the underlying function type does not have an
error result).

rdar://47828439
2019-02-18 11:48:30 -08:00
Arnold Schwaighofer
d830aa9a1c Fix a use-after-free in SILCombiner 2019-01-18 13:49:18 -08:00
Arnold Schwaighofer
0d9aced1ac Fix SILCombiner for partial_apply [stack] 2019-01-16 13:55:03 -08:00
Andrew Trick
bc884d4ba8 Merge pull request #20977 from rajbarik/raj-extend-existential2generic
Perform ExistentialSpecializer when SoleConformingType is known
2019-01-14 18:23:19 -08:00
Erik Eckstein
1072bb6bc6 SILCombine: fix a miscompile caused by dead-apply elimination
SILCombine ended up moving a strong_release past a dealloc_ref.
fixes https://bugs.swift.org/browse/SR-9627
rdar://problem/47153896
2019-01-14 09:00:00 -08:00
Raj Barik
0070730e5a Perform ExistentialSpecializer when SoleConformingType is known 2019-01-07 14:47:39 -08:00
Andrew Trick
207d6a949d Use AbstractionPattern::getOpaque() per Slava's review. 2019-01-02 12:57:33 -08:00
Andrew Trick
c7dcd211ed Fix a latent bug in propagateSoleConformingType: check reabstraction.
This code was force casting the address of the opened existential to
the lowered SIL type of the known conformance. This is superficially
incorrect because values are stored in existentials with maximal
reabstraction.

Bail on any concrete types that require reabstraction. I don't think
we will hit this currently because there is an earlier check that
prevents optimizing a conformance on a generic types. In theory
someone could add that functionality later for internal generic types
with a single instantiation.

More importantly, we don't want anyone copying this logic and assuming
it's generally correct.
2019-01-02 08:44:37 -08:00
Andrew Trick
118a725d96 Generalize and cleanup code for existential specialization.
Generalizes the ConcreteExistentialInfo abstraction so it can be used
both by the ExistentialSpecializer and SILCombine, allowing redundant
code in ExistentialSpecializer.cpp to be deleted.

Splits OpenedArchetypeInfo from ConcreteExistentialInfo. Adds a
ConcreteOpenedArchetypeInfo convenience wrapper around them both, for
use wherever we were originally using ConcreteExistentialInfo.

Splits getAddressOfStackInit into getStackInitInst, This is cleaner and
allows both the ExistentialSpecializer and SILCombine to handle more
interesting cases in the future, like unconditional_checked_cast.

Creates utilities, initializeSubstitutionMap, and
initializeConcreteTypeDef to simplify an generalize
ConcreteExistentialInfo.

While rewriting ExistentialSpecializer to use the new
abstraction, I fixed a latent bug in which is was using a SIL
argument index as a function type parameter index (this would
have broken up if/when we decide to enable calls with indirect
results).
2019-01-02 08:44:37 -08:00
Raj Barik
e90b6cadf1 Merge pull request #19460 from rajbarik/raj-cp-pca-allargs
Refactor SILCombiner to build ConcreteExistentialInfo uniformly
2018-12-17 13:38:15 -08:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Andrew Trick
984bd99b40 Fix SILCombine to devirtualize existentials wrapped in enum.
This allows Swift code to implement a fast path via a protocol type
check as follows:

  if let existentialVal = genericVal as? SomeProtocol {
    // do something fast.
  }

Fixes <rdar://problem/46322928> Failure to devirtualize a protocol
method applied to an opened existential blocks implemention of
DataProtocol.

Note: the approach of devirtualization via backward pattern matching
is fundamentally wrong and will never be fully general. It should be a
forward type propagation.
2018-12-02 00:39:55 -08:00
Raj Barik
ff5a8009fa Refactor SILCombiner to build ConcreteExistentialInfo using an Uniform interface that first uses Data-Flow analysis and then uses ProtocolConformanceAnalysis 2018-11-27 11:54:35 -08:00
Erik Eckstein
c7593fb97e SILCombine: fix an assert in the optimization which promotes a concrete type of an existential
rdar://problem/45915705
2018-11-08 16:57:24 -08:00
Andrew Trick
f19d91d15a Fix SILCombiner::propagateConcreteTypeOfInitExistential.
A SIL argument index was being passed as an AST function type
parameter index. Don't do this.

I also cleaned up the peephole to avoid processing indirect results as
parameter indices, but that part is NFC.

Fixes rdar://45415719 Assertion failed: (Index < Length && "Invalid index!")
2018-10-23 18:44:16 -07:00
Raj Barik
3c5b13dd9e Propagate concrete types for all arguments of an apply instruction 2018-09-12 17:57:08 -07:00
Raj Barik
9a507641fe Propagate concrete types for all arguments of an apply instruction 2018-09-12 17:45:19 -07:00
Erik Eckstein
99a9ed5535 SIL: remove the pinning instructions: strong_pin, strong_unpin, is_unique_or_pinned
They are not used anymore after removing the pinning addressors.
2018-08-23 12:47:56 -07:00
Raj Barik
d9a051ecdc Concrete type propagation using ProtocolConformanceAnalysis 2018-08-22 11:08:29 -07:00
Raj Barik
484925c26f Extend SILCombiner code to handle existential self concrete type propagation using ProtocolConformanceAnalysis 2018-08-01 14:25:52 -07: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
Slava Pestov
13e1c83410 SILCombiner: Remove bogus dump() call (#17553) 2018-06-27 01:14:10 -07:00
Andrew Trick
9d4b4c755c Rewrite SILCombiner::propagateConcreteTypeOfInitExistential. (#17315)
Fixes <rdar://40555427> [SR-7773]:
SILCombiner::propagateConcreteTypeOfInitExistential fails to full propagate type
substitutions.

Fixes <rdar://problem/40923849>
SILCombiner::propagateConcreteTypeOfInitExistential crashes on protocol
compositions.

This rewrite fixes several fundamental bugs in the SILCombiner optimization that
propagates concrete types. In particular, the pass needs to handle:

- Arguments of callee Self type in non-self position.
- Indirect and direct return values of Self type.
- Types that indirectly depend on Self within callee function signature.
- Protocol composition existentials.
- All of the above need to work for protocol extensions as well as witness methods.
- For protocol extensions, conformance lookup should be based on the existential's conformance list.

Additionally, the optimization should not depend on a SILFunction's DeclContext,
which is not serialized. (In fact, we should prevent SIL passes from using
DeclContext). Furthermore, the code needs to be expressed in a way that one can
reason about correctness and invariants.

The root cause of these bugs is that SIL passes are written based on untested
assumptions of Swift type system. A SIL pass needs to handle all verifiable SIL
input because passes need to be composable. Bail-out logic can be added to
simplify the design; however, _the bail-out logic itself cannot make any
assumptions about the language or type system_ that aren't clearly and
explicitly enforced in the SIL verifier. This is a common mistake and major
source of bugs.

I created as many unit tests as I reasonably could to prevent this code from
regressing. Creating enough unit tests to cover all corner cases that were
broken in the original code would be intractable. But the code has been
simplified such that many corner cases disappear.

This opens up some oportunity for generalizing the optimization and eliminating
special cases. However, I want this PR to be limited to fixing correctness
issues only. In the long term, it would be preferable to replace this
optimization entirely with a much more powerful general type propagation pass.
2018-06-26 19:33:31 -07:00
Raj Barik
c277c90be6 Reorganizing the code to find init_existential; Move them to Existential.cpp/h in order for other passes such as ExistentialSpecializer to use it apart from SILCombiner 2018-06-04 11:07:12 -07:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07: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
911ed60a98 Eliminate dead code making use of SubstitutionList. 2018-05-11 17:37:27 -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
Raj Barik
612d771b9e Handle global_addr for concrete type propagation 2018-04-30 14:38:13 -07:00
Davide Italiano
b4d563802b [SILInstruction] Introduce isDebugInstruction().
This is a property of an instruction and should be a member
function of `SILInstruction` and not a free function in
`DebugUtils`. Discussed with Adrian.
2018-04-11 10:14:21 -07:00
Andrew Trick
e7b63e53b8 Fix closure inlining after witness devirtualization.
Certain patterns of directly applied partial_apply's were not being
inlined. This can happen when a closure is defined in the
implementation of a generic witness method. I noticed this issue while
debugging SR-6254: "Fix (or explain) strange ways to make code >3
times faster/slower".

After the witness method is devirtualization and specialized, the
closure application looks like this:

  %pa = partial_apply [callee_guaranteed] %fn() : $@convention(thin) (@in Int) -> @out Int
  %cvt = convert_escape_to_noescape %pa
  apply %cvt(...)

SILCombine already removes the partial_apply and convert_escape_to_noescape generating:

  %thick = thin_to_thick %fn
  apply %thick(...)

However, surprisingly, neither the inliner nor SILCombine can handle this.

I cleaned up the code in SILCombine's apply visitor that handles
thin_to_thick and generalized it to handle this and other patterns.

I also added a restriction to this code so that it doesn't break SIL
ownership in the future, as I discussed with Arnold.
2018-03-08 13:36:59 -08:00
Erik Eckstein
1c20806d44 Revert "Fix closure inlining after witness devirtualization."
This reverts commit 8990ed4f78.
2018-03-07 16:48:05 -08:00
Andrew Trick
8990ed4f78 Fix closure inlining after witness devirtualization.
Certain patterns of directly applied partial_apply's were not being
inlined. This can happen when a closure is defined in the
implementation of a generic witness method. I noticed this issue while
debugging SR-6254: "Fix (or explain) strange ways to make code >3
times faster/slower".

After the witness method is devirtualization and specialized, the
closure application looks like this:

  %pa = partial_apply [callee_guaranteed] %fn() : $@convention(thin) (@in Int) -> @out Int
  %cvt = convert_escape_to_noescape %pa
  apply %cvt(...)

SILCombine already removes the partial_apply and convert_escape_to_noescape generating:

  %thick = thin_to_thick %fn
  apply %thick(...)

However, surprisingly, neither the inliner nor SILCombine can handle this.

I cleaned up the code in SILCombine's apply visitor that handles
thin_to_thick and generalized it to handle this and other patterns.

I also added a restriction to this code so that it doesn't break SIL
ownership in the future, as I discussed with Arnold.
2018-03-07 11:25:19 -08:00
Arnold Schwaighofer
390ba419fc Add an effects(releasenone) function effects attribute
A ``@effects(releasenone)`` function might read/write global state but does not
perform a release.
2018-03-05 07:03:54 -08:00
Adrian Prantl
9b6a9946ec Be explicit about whether a DebugInfo-carying SILInstruction has debug info.
This patch both makes debug variable information it optional on
alloc_stack and alloc_box instructions, and forced variable
information on debug_value and debug_value_addr instructions. The
change of the interface uncovered a plethora of bugs in SILGen,
SILTransform, and IRGen's LoadableByAddress pass.

Most importantly this fixes the previously commented part of the
DebugInfo/local-vars.swift.gyb testcase.

rdar://problem/37720555
2018-02-21 10:50:19 -08:00
Arnold Schwaighofer
a05f0398a1 SILCombiner: Fixes for convert_escape_to_noescape
- also combine thin_to_thick_function and convert_escape_to_noescape
2018-02-13 04:19:59 -08:00
Andrew Trick
113bebb035 Centralize logic for access marker and exclusivity verification.
Create helpers in InstructionUtils.h wherever we need a guarantee that the diagnostics cover the same patterns as the verifier. Eventually this will be called from both SILVerifier and the diagnostic pass:
- findAccessedAddressBase
- isPossibleFormalAccessBase
- isPartialApplyOfReabstractionThunk
- findClosureForAppliedArg
- visitAccessedAddress

Add partial_apply verification assert.

This applies the normal "find a closure" logic inside the "find all partial_apply uses" verification. Making the verifier round-trip ensures that we don't have holes in exclusivity enforcement related to this logic.
2018-02-05 18:43:30 -08:00
John McCall
b13f30ff30 Move a convenience API for changing a SILFunctionType into the AST. NFC. 2017-12-15 18:19:07 -05:00
Andrew Trick
32a8dbcfa8 Fix a dangling stack pointer in SILCombine.
The bug was introduced recently:
commit ac8a48b2
Date:   Fri Oct 6 12:34:14 2017 -0700

Fixes <rdar://35800315> (crash using freed OpenedArchetypesTracker).
2017-12-04 10:04:48 -08:00
Andrew Trick
708aadfada Cleanup SILCombine code in preparation for a bug fix.
This is not NFC because the isUseAfterFree helper and surrounding code
is rewritten. The previous code's intention is unclear, but it was at
best imprecise and unsafe w.r.t. future SIL changes.

Pattern matching code that becomes highly complicated should
be commented with motivating SIL examples.
2017-12-04 10:04:48 -08:00
Slava Pestov
1f79af7504 SIL: Use objc_method instruction for Objective-C protocol method calls
Fixes <rdar://problem/15933365>.
2017-11-29 16:26:43 -08:00
Arnold Schwaighofer
ea9907ae15 Revert "SIL: Use objc_method instruction for Objective-C protocol method calls" 2017-11-29 11:19:46 -08:00
Slava Pestov
1ee0970934 SIL: Use objc_method instruction for Objective-C protocol method calls
Fixes <rdar://problem/15933365>.
2017-11-29 01:22:05 -08:00
Slava Pestov
fb2a6d2025 SILOptimizer: Fix convert_function -> apply peephole for metatype conversions
Fixes <rdar://problem/35492775>.
2017-11-13 22:10:14 -08:00
Arnold Schwaighofer
3d349ae2f8 SILCombine: Fix @callee_guaranteed combine of try_apply
I missed another place where we were releasing the context.

SR-5441
rdar://33255593
2017-11-10 15:18:01 -08:00