Commit Graph

63 Commits

Author SHA1 Message Date
Mark Lacey
1f23ff27bb Remove the transparent bit from apply instructions.
We no longer need or use it since we can always refer to the same bit on
the applied function when deciding whether to inline during mandatory
inlining.

Resolves rdar://problem/19478366.

Swift SVN r26534
2015-03-25 08:36:34 +00:00
Nadav Rotem
d78b376d07 [passes] Replace the old invalidation lattice with a new invalidation scheme.
The old invalidation lattice was incorrect because changes to control flow could cause changes to the
call graph, so we've decided to change the way passes invalidate analysis.  In the new scheme, the lattice
is replaced with a list of traits that passes preserve or invalidate. The current traits are Calls and Branches.
Now, passes report which traits they preserve, which is the opposite of the previous implementation where
passes needed to report what they invalidate.

Node: I tried to limit the changes in this commit to mechanical changes to ease the review. I will cleanup some
of the code in a following commit.

Swift SVN r26449
2015-03-23 21:18:58 +00:00
Mark Lacey
f6337d9688 Make call graph edge sets be SmallPtrSets rather than SmallVectors.
Re-apply r26072 with a fix to ensure that we get stable bottom-up
orderings in the call grpah.

Using SmallPtrSet here makes sense, and paves the way to more reasonable
code for editing the call graph by clients.

One unfortunate casualty of this is the ArrayRevView that was used by
one client.

Swift SVN r26093
2015-03-13 07:33:57 +00:00
Mark Lacey
1936396a5b Revert "Make call graph edge sets be SmallPtrSets rather than SmallVectors."
This reverts commit r26072, which could result in non-deterministic
bottom-up ordering in the call graph.

I'll reapply with a fix soon.

Swift SVN r26074
2015-03-13 00:58:51 +00:00
Mark Lacey
0163c66c1e Make call graph edge sets be SmallPtrSets rather than SmallVectors.
It makes more sense, and paves the way to more reasonable code for
editing the call graph by clients.

One unfortunate casualty of this is the ArrayRevView that was used by
one client.

Swift SVN r26072
2015-03-12 23:15:35 +00:00
Michael Gottesman
c898b2b0ff [+0 self] Remove fix lifetime from FunctionSignature Opts.
With the improvements I have made to the ARC optimizer, these are no longer
necessary. This will give us an idea if guaranteed is being respected properly
without enabling +0 self.

The main effect that this could cause are use-after-frees. I checked with guard
malloc and asan that we are not performing any use-after-frees on the
PerfTestSuite and asan with ninja check.

Swift SVN r25654
2015-03-01 06:49:48 +00:00
Mark Lacey
64af0798df Rename members and interfaces of the call graph for clarity.
Small clean-up to try to improve on the clarity of the call graph code.

I think this is an overall improvement although there is still some
clunky naming that could be improved in the future.

Swift SVN r25492
2015-02-24 00:08:19 +00:00
Dmitri Hrybenko
61286f0260 Fix warnings produced by a newer version of Clang
Swift SVN r25257
2015-02-12 23:50:47 +00:00
Arnold Schwaighofer
d892d5159e FuncSigOpts: Change the calling conv from method to freestanding if we change self
We could explode or dce the argument. Make sure we change the calling convention
of the specialized method if we do so.

rdar://19733030

Swift SVN r25030
2015-02-06 01:23:41 +00:00
Arnold Schwaighofer
bbb81a4c4a FuncSigOpts: Don't transfer array.semantic attributes
Array semantic clients rely on the signature being as in the original
unspecialized function.

Swift SVN r25028
2015-02-06 01:23:40 +00:00
Erik Eckstein
9dfd349faf Add a new Thunk-flag in SILFunction which specifies that a function is a thunk.
This will have an effect on inlining into thunks.
Currently this flag is set for witness thunks and thunks from function signature optimization.
No change in code generation, yet.



Swift SVN r24998
2015-02-05 16:45:05 +00:00
Mark Lacey
9afc924fdf Cast away constness in client, not in call graph code.
Swift SVN r24789
2015-01-28 05:57:08 +00:00
Michael Gottesman
0ac649d87b [proj-tree] Use a passed in BumpPtrAllocator instead of SmallVector for memory.
Previously I was using a large SmallVector to create Nodes for the
ProjectionTree. This created an issue when the SmallVector would convert
from small to large representation in the middle of a method on an
object that is stored in the SmallVector. Thus the 'this' pointer will
be invalidated and all sorts of fun times will occur.

I switched now to using a BumpPtrAllocator which is passed into the tree
and used in FunctionSignatureOptimization for all projection trees.

<rdar://problem/19534462>

Swift SVN r24706
2015-01-24 09:12:50 +00:00
Michael Gottesman
897325b096 Codebase Gardening. NFC.
1. Eliminate unused variable warnings.
2. Change field names to match capitalization of the rest of the field names in the file.
3. Change method names to match rest of the file.
4. Change get,set method for a field to match the field type.

Swift SVN r24501
2015-01-19 00:34:07 +00:00
Michael Gottesman
39817d5fd8 Remove arc namespace.
Swift SVN r24500
2015-01-19 00:10:48 +00:00
Michael Gottesman
f111d29fae [func-sig-opts] The projection tree knows how to explode address arguments... but the rest of func sig opts doesn't. So do not try to optimize in such cases.
I do not think that function signature opts is the right place to handle
promotion of @in, @out arguments. The right place to do such things is when we
perform specialization. That is the natural place to do it since that is where
we transition from address only to loadable types.

Swift SVN r23940
2014-12-15 18:42:20 +00:00
Michael Gottesman
7e39f33f98 [mangle] Include a pass id in the mangling, just to be careful.
I am starting to reuse manglings for different passes. I want to make sure that
when we reuse functions we actually get a function created by the same pass.

Swift SVN r23924
2014-12-14 10:29:11 +00:00
Erik Eckstein
14af3a57e8 Enable elimination of dead methods which are in classes of higher visibility.
The underlying problem is that e.g. even if a method is private but its class is public, the method can be referenced from another module - from the vtable of a derived class.
So far we handled this by setting the SILLinkage of such methods according to the visibility of the class. But this prevented dead method elimination.
Now I set the SILLinkage according to the visibility of the method. This enables dead method elimination, but it requires the following:
1) Still set the linkage in llvm so that it can be referenced from outside.
2) If the method is dead and eliminated, create a stub for it (which calls swift_reportMissingMethod).



Swift SVN r23889
2014-12-12 17:35:40 +00:00
Michael Gottesman
1e63e0e22d [mangle] Add support for mangling dead, owned2guaranteed, and sroa args. Wire up function sig opts.
Now all SIL function specialization passes use the new mangling infrastructure.
Lets keep it that way for future passes as well. = ).

Implements:
<rdar://problem/18831609>

Fixes:
<rdar://problem/19065735>
<rdar://problem/18906781>
<rdar://problem/18956916>

Swift SVN r23859
2014-12-11 05:51:26 +00:00
Michael Gottesman
cb8760148d Reapply "Revert "[func-sig-opts] If we know all callsites of a function, delete the old unoptimized function instead of creating a thunk.""
This reverts commit r23725. This time with fixes so we don't hit that
assertion.

Swift SVN r23762
2014-12-06 23:08:17 +00:00
Mark Lacey
688d1d2eaa Revert "[func-sig-opts] If we know all callsites of a function, delete the old unoptimized function instead of creating a thunk."
This reverts commit r23715 because it breaks the build of the
StrSplitter.swift benchmark, hitting an assert about deleting a function
with references.

Swift SVN r23725
2014-12-05 05:19:12 +00:00
Michael Gottesman
eafef9b733 [func-sig-opts] If we know all callsites of a function, delete the old unoptimized function instead of creating a thunk.
<rdar://problem/19137435>

Swift SVN r23715
2014-12-05 02:08:37 +00:00
Michael Gottesman
3e513eac40 [func-sig-opts] SROA and delete partially dead arguments.
The reason why I am interested in doing this is that:

1. Performing partial dead argument elimination is just a good thing to
do since it reduces dead opaque uses of explosion values, helping other
optimizations.

2. By performing SROA on function arguments, I am able to avoid having
to teach the ARC optimizer about aggregates in arguments. It is
important to try to keep the ARC optimizer as slim as possible.

Additionally the projection tree infrastructure that was built for this
optimization will also allow me to easily "trim" and explode indirect
arguments as well.

On my machine I am seeing the following speedups:

Dictionary3||||||||||-33.53%
Dictionary2||||||||||-19.20%
StringBuilder||||||||-11.04%
StdlibSort|||||||||||-9.82%
StringInterpolation||-9.82%

rdar://17005816

Swift SVN r23587
2014-11-28 20:38:56 +00:00
Michael Gottesman
1afc987739 Refactor the SILArgument API on SILBasicBlock so we can insert bb arguments anywhere in the argument list. Also clean up the API names so that they all match.
Swift SVN r23543
2014-11-22 00:24:40 +00:00
Michael Gottesman
4bee99df2e [func-sig-opts] Change deletion of argument functions into a general updateOptimizedBBArgs methods since I will use the same code path to update the arguments when SROAing arguments.
Swift SVN r23540
2014-11-21 22:29:35 +00:00
Michael Gottesman
ddfe953554 [func-sig-opts] Remove DeadArgIndices now that each ArgumentDescriptor has its original index cached and refactor adding thunk args onto ArgumentDescriptor::addThunkArgs.
I also renamed ArgumentDescriptor::addNewArguments() =>
ArgumentDescriptor::addCallerArgs() to match addThunkArgs. This name fits better
and gives both concepts distinct names.

Swift SVN r23507
2014-11-21 01:50:42 +00:00
Michael Gottesman
3cda8c1bd3 [func-sig-opts] Make ArgToEpilogueReleaseMap a local variable instead of a field on FunctionAnalyzer since it is only needed in one location of the computation now.
Swift SVN r23501
2014-11-21 01:00:57 +00:00
Michael Gottesman
f6655c0f30 [func-sig-opts] Move computation of optimized arg types and optimized arguments from functions into methods on ArgumentDescriptor.
This allows me to SROA arguments without messing with any of the rest of
the machinery in FunctionSignatureOpts since now the main machinery of
FunctionSigOpts manipulates ArgumentDescriptors instead of SIL directly.

Swift SVN r23500
2014-11-21 01:00:56 +00:00
Michael Gottesman
09953020b6 [func-sig-opts] Track the original index of the argument in ArgumentDescriptor and use it to create fix lifetimes and releases.
This means that:

1. I can make the ConsumedArgToEpilogueReleaseMatcher a local variable
instead of being on the FunctionAnalyzer class. This will be done in a
latter commit.

2. When I SROA arguments, I always put the fix_lifetime, release_value
on the correct argument by reusing old values. In the case of creating
the thunk, I use the thunks arguments which are the same as the old args
and I use the argument list of the old ApplyInst in the caller functions
before I delete the old ApplyInst. This means that when I SROA the
arguments of a function, the @owned => @guaranteed argument will work
without any changes.

Swift SVN r23499
2014-11-21 01:00:56 +00:00
Michael Gottesman
df5a2a73cc [func-sig-opts] Add comments to ArgumentDescriptor fields.
Swift SVN r23498
2014-11-21 01:00:54 +00:00
Michael Gottesman
048d1a17c9 [func-sig-opts] Update comment. NFC.
Swift SVN r23316
2014-11-14 01:51:42 +00:00
Michael Gottesman
3d396f8b12 [arc-analysis] Refactor OwnedArgToEpilogueReleaseMatcher from FunctionSigOpts.
Given a function F, this utility class attempts to match up owned arguments with
releases in the epilogue of the function.

I am refactoring this from FunctionSigOpts so I can use it in the ARC optimizer.

<rdar://problem/18923030>

Swift SVN r23219
2014-11-11 00:42:30 +00:00
Michael Gottesman
d677cc7750 [func-sig-opts] Use the debug scope of the old function instead of the new function when turning the old function into a thunk.
This is tested via an assertion that Adrian put in.

Swift SVN r23022
2014-10-30 23:22:50 +00:00
Adrian Prantl
c41b30299f Audit all SILPasses to ensure that new instructions are never created
without a valid SILDebugScope. An assertion in IRGenSIL prevents future
optimizations from regressing in this regard.
Introducing SILBuilderWithScope and SILBuilderwithPostprocess to ease the
transition.

This patch is large, but mostly mechanical.
<rdar://problem/18494573> Swift: Debugger is not stopping at the set breakpoint

Swift SVN r22978
2014-10-28 01:49:11 +00:00
Michael Gottesman
91e621df64 Reapply "[func-sig-opts] Teach func sig opts how to handle multiple @owned arguments."
This reverts commit r22965. I forgot to commit some necessary LLVM changes to
MapVector before committing this. These have hit trunk now so I am recommitting.

Swift SVN r22973
2014-10-27 18:20:06 +00:00
Erik Eckstein
f2a0e331ef Revert "[func-sig-opts] Teach func sig opts how to handle multiple @owned arguments."
It breaks the build:

/Users/erik/proj/main-swift/swift/lib/SILPasses/FunctionSignatureOpts.cpp:125:9: error: no template named 'reverse_iterator' in 'llvm::MapVector<unsigned int, swift::SILInstruction *, llvm::SmallDenseMap<unsigned int, unsigned int, 8, llvm::DenseMapInfo<unsigned int> >, llvm::SmallVector<std::__1::pair<unsigned int, swift::SILInstruction *>, 8> >'; did you mean 'std::reverse_iterator'?
  Range<ArgIndexInstMapTy::reverse_iterator>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        std::reverse_iterator
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:529:29: note: 'std::reverse_iterator' declared here
class _LIBCPP_TYPE_VIS_ONLY reverse_iterator
                            ^
/Users/erik/proj/main-swift/swift/lib/SILPasses/FunctionSignatureOpts.cpp:125:9: error: use of class template 'ArgIndexInstMapTy::reverse_iterator' requires template arguments
  Range<ArgIndexInstMapTy::reverse_iterator>
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:529:29: note: template is declared here
class _LIBCPP_TYPE_VIS_ONLY reverse_iterator
                            ^
/Users/erik/proj/main-swift/swift/lib/SILPasses/FunctionSignatureOpts.cpp:127:12: error: no matching function for call to 'reversed'
    return reversed(ConsumedArgumentIndexToReleaseMap);
           ^~~~~~~~
/Users/erik/proj/main-swift/swift/include/swift/Basic/Range.h:62:15: note: candidate template ignored: substitution failure [with T = llvm::MapVector<unsigned int, swift::SILInstruction *, llvm::SmallDenseMap<unsigned int, unsigned int, 8, llvm::DenseMapInfo<unsigned int> >, llvm::SmallVector<std::__1::pair<unsigned int, swift::SILInstruction *>, 8> > &]: no member named 'rbegin' in 'llvm::MapVector<unsigned int, swift::SILInstruction *, llvm::SmallDenseMap<unsigned int, unsigned int, 8, llvm::DenseMapInfo<unsigned int> >, llvm::SmallVector<std::__1::pair<unsigned int, swift::SILInstruction *>, 8> >'
  inline auto reversed(T &&container)
              ^
3 errors generated.



Swift SVN r22965
2014-10-27 14:40:07 +00:00
Michael Gottesman
82c0e90eb6 [func-sig-opts] Teach func sig opts how to handle multiple @owned arguments.
Swift SVN r22964
2014-10-27 13:50:50 +00:00
Michael Gottesman
e79bdc899e [func-sig-opts] Refactor func-sig-opts to make it easier to handle multiple @owned arguments.
The main change was to create a FunctionAnalyzer structure which stores
all of the data that we carry around. This reduces the number of
arguments for a bunch of functions and also enables the beginning of a
split in the pass in between the analysis part and the execution
part. There are still some things I need to clean up which I will do in
a little bit.

Swift SVN r22962
2014-10-27 09:15:58 +00:00
Michael Gottesman
1bbd69dbb5 [func-sig-opts] Enable function sig opts on fragile functions.
Previously, we used shared linkage and did not specialize fragile functions
since this exposed serialization issues. These serialization issues have been
dealt with but I realized the model did not make sense: We do not specialize
functions with "external" linkage. This means that we only specialize functions
in the module in which it is defined. Thus shared provides no benefit in terms
of the linker being able to squash the function. Instead we just copy whatever
linkage the original function used.

rdar://16917049

Swift SVN r22940
2014-10-25 02:47:02 +00:00
Michael Gottesman
a0a66e3883 Reapply "[func-sig-opts] Add SILFunction::findReturnBB to easily find the unique basic block containing a ReturnInst in a SILFunction."
This reverts commit r22933. Now with fixes so this actually compiles!

Swift SVN r22935
2014-10-25 00:14:39 +00:00
Jordan Rose
ef30948e4e Revert "[func-sig-opts] Add SILFunction::findReturnBB to easily find the unique basic block containing a ReturnInst in a SILFunction."
This reverts r22923, which doesn't actually compile. The fix is probably
obvious (s/F/this/) but I didn't want to mess with it in case it's wrong.

Swift SVN r22933
2014-10-24 23:33:19 +00:00
Michael Gottesman
c42bdcf418 [func-sig-opts] Add SILFunction::findReturnBB to easily find the unique basic block containing a ReturnInst in a SILFunction.
Swift SVN r22923
2014-10-24 22:21:48 +00:00
Michael Gottesman
dc4dde51bf Remove dead code. NFC.
Swift SVN r22618
2014-10-09 05:29:10 +00:00
Michael Gottesman
993d9c54eb [func-sig-opts] If we already made a specialized version of a function, do not respecialize, just bail.
This patch ensures that if we already have a version of a function with
an optimized function signature, we do not turn the original function
into a thunk or change the original function to call the already created
specialized version of it. The reason for doing this is:

1. It enables us to avoid the question of whether or not the callee and
caller properly mesh. To do this safely, we would really need to analyze
the specialized version of the function to make sure that it
meshes. This could be fragile.

2. This situation should not occur often since we only run function
signature specialization once in the pipeline and we do not specialize
external functions (* see below) implying linking will not affect this
optimization.

* We do not specialize functions with external linkage since:

1. Linking in a function to the current module does not provide any more
information about the internals or the signature of the function that
was available in the original module.

2. For function signature optimization to work, we need to be able to
optimize both the caller and the callee. External functions are not
emitted into the final object file implying that any changes we make to
the caller or callee will not be apparent in the final object file. So
we will not get any optimization benefit and could potentially introduce
miscompiles.

Swift SVN r22609
2014-10-09 00:50:07 +00:00
Michael Gottesman
7de8c10f20 [func-sig-opts] If we already have a specialization of a function, reuse it and
convert the current Function to a thunk. This ensures we can ignore whether or
not the function was originally a thunk which could potentially be fragile. This
should only occur rarely so we should not be doing more work in the common case.

Swift SVN r22600
2014-10-08 09:21:42 +00:00
Michael Gottesman
f444a3837a [func-sig-opts] Restrict the set of functions that we will optimize.
The sets not being considered are:

1. Any functions that will always be inlined. We ignore the inlined case since we can just
optimize the signature of the caller at each of the given function call sites.

2. Functions that have already been specialized once.

3. External functions. This is because for function signature
optimization to work, we need to be able to modify the caller and the
callee. Thus if the caller or callee is external since we can not modify
the external definition, we can not perform any specialization.

4. Generic functions. I am leaving this for a later iteration.

5. Non-Fragile functions. I am leaving this for a later iteration.

6. ObjCMethods and Witness Methods. We can never perform this
optimization on ObjCMethods and I am going to enable witness methods in
a later iteration.

7. Functions without call sites. Since we are only specializing fragile
functions, if a function without call sites is used in a different module, that
module will be able to deserialize it. Once I handle non-fragile functions, this
will change.

Swift SVN r22564
2014-10-07 08:50:14 +00:00
Michael Gottesman
1bd44a82a6 [func-sig-opts] When creating a new argument, pass in the decl from the old argument.
Swift SVN r22554
2014-10-06 23:16:53 +00:00
Michael Gottesman
c97ab1f11f [func-sig-opts] When splicing BB from one function to another, change
the debug scope of each instruction to be the debug scope of the new
function.

This will be tested by the functionality in function signature opts.

Swift SVN r22553
2014-10-06 23:16:52 +00:00
Jordan Rose
3fcdfd40e9 Remove the "swift/Basic/Optional.h" header.
llvm::Optional lives in "llvm/ADT/Optional.h". Like Clang, we can get
Optional in the 'swift' namespace by including "swift/Basic/LLVM.h".

We're now fully switched over to llvm::Optional!

Swift SVN r22477
2014-10-02 18:51:45 +00:00
Erik Eckstein
c16c510167 Set SILLinkage according to visibility.
Now the SILLinkage for functions and global variables is according to the swift visibility (private, internal or public).

In addition, the fact whether a function or global variable is considered as fragile, is kept in a separate flag at SIL level.
Previously the linkage was used for this (e.g. no inlining of less visible functions to more visible functions). But it had no effect,
because everything was public anyway.

For now this isFragile-flag is set for public transparent functions and for everything if a module is compiled with -sil-serialize-all,
i.e. for the stdlib.

For details see <rdar://problem/18201785> Set SILLinkage correctly and better handling of fragile functions.

The benefits of this change are:
*) Enable to eliminate unused private and internal functions
*) It should be possible now to use private in the stdlib
*) The symbol linkage is as one would expect (previously almost all symbols were public).

More details:

Specializations from fragile functions (e.g. from the stdlib) now get linkonce_odr,default
linkage instead of linkonce_odr,hidden, i.e. they have public visibility.
The reason is: if such a function is called from another fragile function (in the same module),
then it has to be visible from a third module, in case the fragile caller is inlined but not
the specialized function.

I had to update lots of test files, because many CHECK-LABEL lines include the linkage, which has changed.

The -sil-serialize-all option is now handled at SILGen and not at the Serializer.
This means that test files in sil format which are compiled with -sil-serialize-all
must have the [fragile] attribute set for all functions and globals.

The -disable-access-control option doesn't help anymore if the accessed module is not compiled
with -sil-serialize-all, because the linker will complain about unresolved symbols.

A final note: I tried to consider all the implications of this change, but it's not a low-risk change.
If you have any comments, please let me know.



Swift SVN r22215
2014-09-23 12:33:18 +00:00