Commit Graph

271 Commits

Author SHA1 Message Date
zoecarver
e38da06f7c Stash debugging changes 2020-01-26 19:34:16 -08:00
zoecarver
e93919b754 Add comments, remove commented code, and other general cleanup 2020-01-24 12:44:11 -08:00
zoecarver
ae64719824 Support store instructions in createApplyWithConcreteType
Supporting stores in more places means that more code can be _completely_ devirtualized which also means other optimizations (e.g. inlining) can also happen.

Also, updates tests.
2020-01-24 12:22:13 -08:00
zoecarver
0992e482d4 Abort if not address type 2020-01-24 11:07:31 -08:00
Ravi Kandhadai
935686460c [SIL Optimization] Create a new utility InstructionDeleter to delete instructions
and eliminate dead code. This is meant to be a replacement for the utility:
recursivelyDeleteTriviallyDeadInstructions. The new utility performs more aggresive
dead-code elimination for ownership SIL.

This patch also migrates most non-force-delete uses of
recursivelyDeleteTriviallyDeadInstructions to the new utility.
and migrates one force-delete use of recursivelyDeleteTriviallyDeadInstructions
(in IRGenPrepare) to use the new utility.
2019-12-18 13:17:17 -08:00
Jonathan Keller
e1ceb4f437 [SILOptimizer] Generalize optimization of static keypaths
We have an optimization in SILCombiner that "inlines" the use of compile-time constant key paths by performing the property access directly instead of calling a runtime function (leading to huge performance gains e.g. for heavy use of @dynamicMemberLookup). However, this optimization previously only supported key paths which solely access stored properties, so computed properties, optional chaining, etc. still had to call a runtime function. This commit generalizes the optimization to support all types of key paths.
2019-12-15 14:10:00 -08:00
Michael Gottesman
113c22a680 [sil] Move partial apply combiner code from SILCombiner into InstOptUtils.h/SILCombinerApplyVisitor.cpp.
This is in preparation for moving this into the mandatory combiner.
2019-12-11 14:48:19 -08:00
zoecarver
12883d07d9 Merge branch 'master' into semantics-def 2019-11-19 13:02:47 -08:00
Erik Eckstein
40c1e8f977 SILCombine: fix a wrong dealloc_stack ordering when propagating concrete types of apply arguments.
Fixes a SILVerifier crash.

rdar://problem/56808923
2019-11-05 09:11:51 -08:00
zoecarver
2ca448b23d Address review comments
* add namespace
* fix block comment style
* SEMA_ATTR -> SEMANTICS_ATTR
* error when SEMANTICS_ATTR isn't defined
2019-11-02 21:58:26 -07:00
zoecarver
9c1a614efb Fix based on review comments 2019-11-02 13:07:20 -07:00
zoecarver
d4129d8659 Add def file for semantics attributes and use constants instead of cstring literals 2019-11-02 11:36:13 -07:00
Andrew Trick
fd6497bbc7 SILCombine cleanup; replace null check with dyn_cast_or_null.
Remove 'not' and add '-emit-sil' to the corresponding lit test's run line.
2019-11-01 21:36:35 -07:00
Andrew Trick
4e106fb2e4 Merge pull request #28012 from zoecarver/fix/optimizer-and-SR-11624
Fix crash when optimizing protocol composition
2019-11-01 19:52:42 -07:00
zoecarver
a8d162fd1f Fix crash by checking if nominal type is null 2019-11-01 10:16:44 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Erik Eckstein
4ad7f3dea9 SILCombine: handle properties in superclasses in the keypath optimization
We ended up with invalid ref_element_addr instructions which let the SILVerifier crash.
Inserting upcasts fixes this.

rdar://problem/56410011
2019-10-21 16:00:56 +02:00
Erik Eckstein
090a7a3311 SILCombine: fix a problem with thin_to_thick_function elimination.
The optimizer can generate a thin_to_thick_function conversion from a throwing thin to a non-throwing thick function (in case it can prove that the function is actually not throwing).
Therefore, when removing such a  conversion from the callee-argument of an apply, we have to check if the new callee (= the argument of the thin_to_thick_function) is a throwing function and set the not-throwing flag in this case.

This fixes a SILVerifier crash.
rdar://problem/56358645
2019-10-21 11:29:22 +02:00
Andrew Trick
bddc69c8a6 Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels
the SIL/XXUtils convention.

New:
- InstOptUtils.h
- CFGOptUtils.h
- BasicBlockOptUtils.h
- ValueLifetime.h

Removed:
- Local.h
- Two conflicting CFG.h files

This reorganization is helpful before I introduce more
utilities for block cloning similar to SinkAddressProjections.

Move the control flow utilies out of Local.h, which was an
unreadable, unprincipled mess. Rename it to InstOptUtils.h, and
confine it to small APIs for working with individual instructions.
These are the optimizer's additions to /SIL/InstUtils.h.

Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now
there is only SIL/CFG.h, resolving the naming conflict within the
swift project (this has always been a problem for source tools). Limit
this header to low-level APIs for working with branches and CFG edges.

Add BasicBlockOptUtils.h for block level transforms (it makes me sad
that I can't use BBOptUtils.h, but SIL already has
BasicBlockUtils.h). These are larger APIs for cloning or removing
whole blocks.
2019-10-02 11:34:54 -07:00
Raj Barik
d40207743c Merge pull request #25038 from atrick/fix-specializer-leak
Fix ExistentialSpecializer to correctly cleanup storage.
2019-06-03 09:31:47 -07:00
Arnold Schwaighofer
c187c8ac13 SIL: Replace uses of getReferencedFunction() by getReferencedFunctionOrNull() and getInitialReferencedFunction()
With the advent of dynamic_function_ref the actual callee of such a ref
my vary. Optimizations should not assume to know the content of a
function referenced by dynamic_function_ref. Introduce
getReferencedFunctionOrNull which will return null for such function
refs. And getInitialReferencedFunction to return the referenced
function.
Use as appropriate.

rdar://50959798
2019-05-26 08:58:14 -07:00
Andrew Trick
daa82bc783 Fix ExistentialSpecializer to correctly cleanup storage.
Handle calling conventions and cleanups in all the places (hopefully).
- when ExistentialSpecializer copies the specialized concrete arg into the
original existential value
- when ExistentialSpecializer generates a think
- when SILCombine substitutes concrete values in place of the opened
existential.

One particularly nasty problem is the existential boxes need to be
destroyed. It is not ok to simply destroy their value. The "leaks"
tool does not catch this problem.

Ownership SIL will make this all much more robust.

Fixes <rdar://problem/50595630> Multiple leaks detected - Swift Perf
2019-05-23 17:20:34 -07:00
Arnold Schwaighofer
4985e0f818 SILCombine: We are not guaranteed to get a function_ref instruction here
Also fix an issue in that cast optimizer and objc bridging thunks that
are marked dynamic.

Same test case tests both issues.

rdar://51068786
2019-05-23 08:39:31 -07:00
Erik Eckstein
da38e3ac81 SILCombine: optimize keypath instructions.
If the keypath argument of a keypath access function is a keypath literal instruction, generate the projection inline and remove the access function.
For example, replaces (simplified SIL):
   %kp = keypath ... stored_property #Foo.bar
   apply %keypath_runtime_function(%root_object, %kp, %addr)
with:
   %addr = struct_element_addr %root_object, #Foo.bar
   load/store %addr

Currently this only handles stored property patterns.

rdar://problem/36244734
2019-05-21 09:44:59 -07:00
Michael Gottesman
179251d167 [sil-combine] Do not perform existential type propagation on @in parameters when we have a copy of the underlying value.
Otherwise, we may get use-after-frees as in the added test.

rdar://50609145
2019-05-09 23:01:43 -07: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
Andrew Trick
b5ad038ca5 Fix SILCombine infinite iteration optimizing existentials.
Optimizing applies that take existential arguments is a complex
process with multiple transformations. If the apply isn't rewritten,
earlier transformations need to be undone.

SILCombine is obviously the wrong place to implement type propagation,
as I point out every time I need to fix it.

Fixes <rdar://problem/49336444> SILCombine infinite loop.
2019-04-01 10:58:01 -07:00
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