Commit Graph

184 Commits

Author SHA1 Message Date
Slava Pestov
b677a2e6a7 AST: Use the new Type::subst() in a few places
SILType substitutions are still done with the old form, and until
BoundGenericTypes hold conformances, we still have to pass around
a ModuleDecl in a few places we really shouldn't, but one step
at a time.
2016-09-08 21:59:13 -07:00
Slava Pestov
a993e36c06 AST: Add a new SubstitutionMap data structure
This replaces the TypeSubstitutionMap / ConformanceMap pair that
has been appearing more and more lately.
2016-09-08 21:59:11 -07:00
Slava Pestov
772cf3a2fa SIL Optimizer: More principled substitution remapping in devirtualizer
When devirtualizing witness method and class method calls, we
transform apply instructions operating on the result of a SIL
witness_method or class_method instruction to direct calls of
a function_ref.

The generic signature of the dynamic call site might not match
the generic signature of the static thunk, so the substitution
list from the dynamic apply instruction cannot be used directly;
instead, we must transform it to a substitution list suitable
for the static thunk.

- With witness methods, the method is called using the protocol
  requirement's signature, <Self : P, ...>, however the
  witness thunk has a generic signature derived from the
  concrete witness.

  For example, the requirement might have a signature
  <Self : P, T>, where the concrete witness thunk might
  have a signature <X, Y>, where the concrete conforming type
  is G<X, Y>.

  At the call site, we substitute Self := G<X', Y'>; however
  to be able to call the witness thunk directly, we need to
  form substitutions X := X' and Y := Y'.

- A similar situation occurs with class methods when the
  dynamically-dispatched call is performed against a derived
  class, but devirtualization actually finds the method on a
  base class of the derived class.

  The base class may have a different number of generic
  parameters than the derived class, either because the
  derived class makes some generic parameters of the base
  class concrete, or if the derived class introduces new
  generic parameters of its own.

In both cases, we need to consider the generic signature of the
dynamic call site (the protocol requirement or the derived
class method) as well as the generic signature of the static
thunk, and carefully remap the substitutions from one form
into another.

Previously the optimizer would implicitly rely on substitutions
being in AllArchetypes order, in particular that concatenating
outer substitutions with inner substitutions makes sense.

This assumption is about to go away, so this patch refactors
the optimizer to use some new abstractions for remapping
substitution lists.
2016-09-06 11:51:13 -07:00
John McCall
afdda3d107 Implement SE-0117.
One minor revision: this lifts the proposed restriction against
overriding a non-open method with an open one.  On reflection,
that was inconsistent with the existing rule permitting non-public
methods to be overridden with public ones.  The restriction on
subclassing a non-open class with an open class remains, and is
in fact consistent with the existing access rule.
2016-08-02 07:46:38 -07:00
Jordan Rose
508e825ff2 Split 'fileprivate' and 'private', but give them the same behavior.
'fileprivate' is considered a broader level of access than 'private',
but for now both of them are still available to the entire file. This
is intended as a migration aid.

One interesting fallout of the "access scope" model described in
758cf64 is that something declared 'private' at file scope is actually
treated as 'fileprivate' for diagnostic purposes. This is something
we can fix later, once the full model is in place. (It's not really
/wrong/ in that they have identical behavior, but diagnostics still
shouldn't refer to a type explicitly declared 'private' as
'fileprivate'.)

As a note, ValueDecl::getEffectiveAccess will always return 'FilePrivate'
rather than 'Private'; for purposes of optimization and code generation,
we should never try to distinguish these two cases.

This should have essentially no effect on code that's /not/ using
'fileprivate' other than altered diagnostics.

Progress on SE-0025 ('fileprivate' and 'private')
2016-07-25 13:13:35 -07:00
Slava Pestov
98a0e73b02 AST: Merge BoundGenericType::getSubstitutions() with TypeBase::gatherAllSubstitutions(), NFC 2016-06-13 00:57:09 -07:00
Roman Levenstein
73b6a38edc [sil-devirtualizer] Do not perform a speculative devirtualization for no-opt callees. 2016-05-11 16:28:50 -07:00
Roman Levenstein
7a9b05babf [sil-devirtualizer] Fix some bugs in the devirtualizer.
- Don't crash if a class_method instruction could not be devirtualized.
- Improve devirtualization of methods with generic parameters and using dependent types.
- Fix a bug in isBindableToSuperclassOf, uncovered while fixing the original bug reported in SR-1206.
  This bug could lead in certain cases to invocations of a wrong method from the base class, instead
  of using a method from a derived class.

rdar://25891588   and SR-1206
2016-04-28 22:40:47 -07:00
Slava Pestov
d8e1e2e289 SILOptimizer: Fixes for non-fragile references in fragile functions
Two fixes to optimization passes to maintain restrictions about what
[fragile] functions can reference:

- When devirtualizing witness methods, don't devirtualize if the caller
  is fragile and the callee is not. This matches existing logic in
  class devirtualization.

- When performing generic or function signature specialization, don't
  specialize non-fragile functions referenced from fragile functions.

Since @_transparent functions are allowed to call 'static inline'
imported functions, also be sure to mark the foreign-to-native thunk
for such a function as [fragile].

With this patch, the standard library and performance test suite
now build with -enable-resilience.

No new tests for this stuff here -- the existing tests together
with an -enable-resilience build provide coverage.

Closes out <https://bugs.swift.org/browse/SR-267> and
<https://bugs.swift.org/browse/SR-268>.
2016-04-08 02:14:33 -07:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Slava Pestov
a9ad760b78 SIL: Clean up duplicated "can be referenced from a fragile function" checks 2016-03-25 22:46:50 -07:00
Joe Groff
77dd9b2992 Split exact-subclass and bindable-to-subclass queries.
In many places, we're interested in whether a type with archetypes *might be* a superclass of another type with the right bindings, particularly in the optimizer. Provide a separate Type::isBindableToSuperclassOf method that performs this check. Use it in the devirtualizer to fix rdar://problem/24993618. Using it might unblock other places where the optimizer is conservative, but we can fix those separately.
2016-03-09 11:14:45 -08:00
Slava Pestov
c6a0f51611 SIL: Add devirtualizer support for default witness methods
We ignore substitutions from the conformance, using the Self type
substitution from the call site instead.

The new SILFunctionType::getDefaultWitnessMethodProtocol() method
is used to figure out what "shape" the Self substitutions need
to take.

This is cleaner than it was before the method was added, but is
still a bit of a hack; more and more it appears that we need to
stop thinking of witness_method as a separate calling convention,
and design what @rjmccall described as "abstraction patterns for
generic signatures" instead.
2016-02-24 00:29:07 -08:00
John McCall
e249fd680e Destructure result types in SIL function types.
Similarly to how we've always handled parameter types, we
now recursively expand tuples in result types and separately
determine a result convention for each result.

The most important code-generation change here is that
indirect results are now returned separately from each
other and from any direct results.  It is generally far
better, when receiving an indirect result, to receive it
as an independent result; the caller is much more likely
to be able to directly receive the result in the address
they want to initialize, rather than having to receive it
in temporary memory and then copy parts of it into the
target.

The most important conceptual change here that clients and
producers of SIL must be aware of is the new distinction
between a SILFunctionType's *parameters* and its *argument
list*.  The former is just the formal parameters, derived
purely from the parameter types of the original function;
indirect results are no longer in this list.  The latter
includes the indirect result arguments; as always, all
the indirect results strictly precede the parameters.
Apply instructions and entry block arguments follow the
argument list, not the parameter list.

A relatively minor change is that there can now be multiple
direct results, each with its own result convention.
This is a minor change because I've chosen to leave
return instructions as taking a single operand and
apply instructions as producing a single result; when
the type describes multiple results, they are implicitly
bound up in a tuple.  It might make sense to split these
up and allow e.g. return instructions to take a list
of operands; however, it's not clear what to do on the
caller side, and this would be a major change that can
be separated out from this already over-large patch.

Unsurprisingly, the most invasive changes here are in
SILGen; this requires substantial reworking of both call
emission and reabstraction.  It also proved important
to switch several SILGen operations over to work with
RValue instead of ManagedValue, since otherwise they
would be forced to spuriously "implode" buffers.
2016-02-18 01:26:28 -08:00
saisi
7f1da6adcc Fixed more niggling typos 2016-01-29 23:52:24 -05:00
Erik Eckstein
74d44b74e7 SIL: remove SILValue::getDef and add a cast operator to ValueBase * as a repelacement. NFC. 2016-01-25 15:00:49 -08:00
Erik Eckstein
506ab9809f SIL: remove getTyp() from SILValue 2016-01-25 15:00:49 -08:00
Erik Eckstein
b745691a38 SIL refactoring: Move some functions out of SILValue and Operand
SILValue.h/.cpp just defines the SIL base classes. Referring to specific instructions is a (small) kind of layering violation.
Also I want to keep SILValue small so that it is really just a type alias of ValueBase*.
NFC.
2016-01-25 10:37:03 -08:00
practicalswift
71e00fefa1 [gardening] Fix typos: "word word" (two spaces) → "word word" (one space) 2016-01-24 21:27:16 +01:00
practicalswift
8efa5f587e [gardening] Remove "-*- C++ -*-" tag from .cpp files
Emacs assumes .h files are C files by default which is why the
tag "-*- C++ -*-" is needed.

.cpp files do not have this problem.
2016-01-23 12:09:32 +01:00
Erik Eckstein
2db6f3d213 SIL: remove multiple result values from SILValue
As there are no instructions left which produce multiple result values, this is a NFC regarding the generated SIL and generated code.
Although this commit is large, most changes are straightforward adoptions to the changes in the ValueBase and SILValue classes.
2016-01-21 10:30:31 -08:00
practicalswift
6d0eee9b8c Remove unused variables. 2016-01-21 10:33:17 +01:00
John McCall
5112864dad Remove the archetype from Substitution.
This eliminates some minor overheads, but mostly it eliminates
a lot of conceptual complexity due to the overhead basically
appearing outside of its context.
2016-01-08 15:27:13 -08:00
practicalswift
1339b5403b Consistent use of header comment format.
Correct format:
//===--- Name of file - Description ----------------------------*- Lang -*-===//
2016-01-04 13:26:31 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
practicalswift
fd70b26033 Fix typos in comments. 2015-12-28 02:15:34 +01:00
practicalswift
8ab8847684 Fix typos. 2015-12-16 22:09:32 +01:00
Roman Levenstein
e6442dc9b2 [sil-devirtualizer] Improve devirtualization of class_method instructions
Use ClassHierarchyAnalysis to check if a given method is effectively final, i.e. cannot be overridden.
This replaces the old approach based on using  isKnownFinal, which was weaker because it didn't use the ClassHierarchyAnalysis.
2015-12-14 10:11:47 -08:00
Roman Levenstein
ed38ce409c [sil-devirtualizer] Add a helper function to compute all subclasses of a given class.
The new function uses the ClassHierarchyAnalysis to do its job.
2015-12-14 10:11:47 -08:00
Roman Levenstein
fd0b383570 [sil-devirtualizer] Handle devirtualization of generic classes 2015-12-14 10:11:47 -08:00
Roman Levenstein
c909ff4cf2 [sil-devirtualizer] Improve getInstanceWithExactDynamicType
It is now able to cover some new cases where the instance is a parameter of a function. If it can be proven that the static type of instance cannot be derived, then it's static type is also a dynamic type of the instance.

The new functionality is not directly triggered in the current setup, because isKnownFinalMethod is currently invoked before it and subsumes this cases. But getInstanceWithExactDynamicType got this functionality anyways to be self-contained, so that it can be used in other contexts, where it is not invoked after isKnownFinalMethod.
2015-12-14 10:11:47 -08:00
Roman Levenstein
e05a72a1cb [sil-devirtualizer] Add a ClassHierarchyAnalysis parameter to tryDevirtualizeApply.
Not functionality changes yet. Subsequent patches will make use of this parameter.
2015-12-14 10:11:47 -08:00
Roman Levenstein
cbcd008ac4 [sil-devirtualizer] Rename isClassWithUnboundGenericParameters and extend it to work with any nominal types 2015-12-14 10:11:46 -08:00
Andrew Trick
739b0e9c56 Reorganize SILOptimizer directories for better discoverability.
(libraries now)

It has been generally agreed that we need to do this reorg, and now
seems like the perfect time. Some major pass reorganization is in the
works.

This does not have to be the final word on the matter. The consensus
among those working on the code is that it's much better than what we
had and a better starting point for future bike shedding.

Note that the previous organization was designed to allow separate
analysis and optimization libraries. It turns out this is an
artificial distinction and not an important goal.
2015-12-11 15:14:23 -08:00