Commit Graph

1032 Commits

Author SHA1 Message Date
Michael Gottesman
9e70b855e4 [cmake] Use a helper macro to simplify adding sources to the SILOptimizer library.
All this does is automate the creation of the ${DIRNAME}_SOURCES variables that we already create and allows for the author to avoid having to prefix with the directory name, i.e.:

set(FOOBAR_SOURCES
  FooBar/Source.cpp
  PARENT_SCOPE)

=>

silopt_register_sources(
  Source.cpp)

Much easier and cleaner to read. I put the code that implements this in the
CMakeLists.txt file just for the SILOptimizer.
2018-06-27 17:48:59 -07:00
Michael Gottesman
97367d3dd4 [ownership] Do not lower copy_unowned_value to strong_retain_unowned.
The major important thing here is that by using copy_unowned_value we can
guarantee that the non-ownership SIL ARC optimizer will treat the release
associated with the strong_retain_unowned as on a distinc rc-identity from its
argument. As an example of this problem consider the following SILGen like
output:

----
%1 = copy_value %0 : $Builtin.NativeObject
%2 = ref_to_unowned %1
%3 = copy_unowned_value %2
destroy_value %1
...
destroy_value %3
----

In this case, we are converting a strong reference to an unowned value and then
lifetime extending the value past the original value. After eliminating
ownership this lowers to:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
strong_retain_unowned %1
strong_release %0
...
strong_release %0
----

From an RC identity perspective, we have now blurred the lines in between %3 and
%1 in the previous example. This can then result in the following miscompile:

----
%1 = ref_to_unowned %0
strong_retain_unowned %1
...
strong_release %0
----

In this case, it is possible that we created a lifetime gap that will then cause
strong_retain_unowned to assert. By not lowering copy_unowned_value throughout
the SIL pipeline, we instead get this after lowering:

----
strong_retain %0 : $Builtin.NativeObject
%1 = ref_to_unowned %0
%2 = copy_unowned_value %1
strong_release %0
...
strong_release %2
----

And we do not miscompile since we preserved the high level rc identity
pairing.

There shouldn't be any performance impact since we do not really optimize
strong_retain_unowned at the SIL level. I went through all of the places that
strong_retain_unowned was referenced and added appropriate handling for
copy_unowned_value.

rdar://41328987

**NOTE** I am going to remove strong_retain_unowned in a forthcoming commit. I
just want something more minimal for cherry-picking purposes.
2018-06-27 13:02:58 -07:00
swift-ci
ec732b09b1 Merge pull request #17367 from gottesmm/pr-54f11c8bd266d77cf730855fefc3d2a135669272 2018-06-20 12:26:04 -07:00
Michael Gottesman
00d95425be [erel-matcher] Do not throw away the list of releases if we do not have a joint post-dominating release set. Instead, use a flag.
I am tuning a new argument explosion heuristic to reduce code-size. One part of
the heuristic I am playing with is the part of the algorithm that attempts to
figure out if we could eliminate additonal arguments after performing
owned->guaranteed an additional release when we run FSO a second time. Today we
do this unconditionally. I am trying to do it in a more conservative way where
we only do it if we know that we aren't going to increase the number of
arguments too much.

rdar://41146023
2018-06-20 11:10:30 -07:00
Slava Pestov
e53ec62536 Merge pull request #17177 from rajbarik/raj-protoconfanal
ProtocolConformanceAnalysis
2018-06-19 14:37:01 -07:00
Raj Barik
c2aef11cff ProtocolConformanceAnalysis for a non-public protocol returns all the types (classes, structs, and enums) that conform to it. It is performed in the whole-module-compilation mode. 2018-06-18 13:38:24 -07:00
Michael Gottesman
369b1f41c1 [epilogue-release-matcher] Pass DenseSet by reference instead of by value.
This is particularly egrigious since we are only /reading/ from the DenseSet. So
we are basically mallocing/copying a DenseSet just to read from it... I don't
think I need to say more.

rdar://41146023
2018-06-16 23:18:36 -07:00
Michael Gottesman
595a7dbc42 [epilogue-arc-analysis] Do not copy lists by value to read them. Use an ArrayRef instead.
I don't think I need to say more here.

rdar://41146023
2018-06-16 23:18:36 -07:00
Michael Gottesman
5dcb806694 Fix comment typo. 2018-06-13 09:28:05 -07:00
Devin Coughlin
be60d9bc10 [Exclusivity] Teach separate-stored-property relaxation about TSan instrumentation
The compile-time exclusivity diagnostics explicitly allow conflicting accesses
to a struct when it can prove that the two accesses are used to project addresses
for separate stored properties. Unfortunately, the logic that detects this special
case gets confused by Thread Sanitizer's SIL-level instrumentation. This causes
the exclusivity diagnostics to have false positives when TSan is enabled.

To fix this, teach the AccessSummaryAnalysis to ignore TSan builtins when
determining whether an access has a single projected subpath.

rdar://problem/40455335
2018-06-10 16:44:19 -07:00
Erik Little
863f3a19ff Rename @effects to @_effects
@effects is too low a level, and not meant for general usage outside
the standard library. Therefore it deserves to be underscored like
other such attributes.
2018-06-06 12:53:03 -04:00
Erik Eckstein
a471bccd6d ARC code motion: handle unowned retains/releases correctly.
This fixes an assert-crash in the ReleaseHoisting pass.

rdar://problem/40525543
2018-05-24 10:40:39 -07:00
Andrew Trick
e29c2089a4 Rework AccessStorageAnalysis design. 2018-05-23 09:23:39 -07:00
Andrew Trick
e1baf91ae1 Merge pull request #16595 from atrick/accessfold
[Exclusivity] Access Enforcement Folding Optimization
2018-05-15 13:27:52 -07:00
Andrew Trick
7fc2d6267b Rename findAccessedStorageOrigin() to findAccessedStorageNonNested(). 2018-05-15 12:29:19 -07:00
Andrew Trick
495d5aecf6 [exclusivity] Add an access marker folding pass.
Use AccessedStorageAnalysis to find access markers with no nested conflicts.

This optimization analyzes the scope of each access to determine
whether it contains a potentially conflicting access. If not, then it
can be demoted to an instantaneous check, which still catches
conflicts on any enclosing outer scope.

This removes up to half of the runtime calls associated with
exclusivity checking.
2018-05-15 12:29:19 -07:00
Michael Gottesman
d71d69821f [rcid] Add new method getRCUses() and reimplement getRCUsers() on top of it.
The actual algorithm used here has not changed at all so this is basically a NFC
commit. What this PR does is change the underlying algorithm to return the
operands that it computes internally rather than transforming the operand list
into the user list internally. This enables the callers of the optimization to
find the operand number related to the uses. This makes working with
instructions with multiple operands much easier since one does not need to mess
around with rederiving the operand number from the user instruction/SILValue
pair.

getRCUsers() works now by running getRCUses() internally and then maps the
operand list to the user list.

rdar://38196046
2018-05-15 10:22:23 -07:00
Andrew Trick
841fc150ee Add several AccessedStorageAnalysis unit tests, and fix iterator invalidation. 2018-05-14 10:55:21 -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
71e793f74f [SILOptimizer] NFC: Fix implicit fallthrough warning 2018-05-06 07:44:08 -04:00
Andrew Trick
0a2c935e06 Merge pull request #16398 from atrick/access-analysis-callee-origin
Access analysis callee origin
2018-05-04 18:26:08 -07:00
Andrew Trick
c5f002969f Merge pull request #16392 from atrick/sideeffect-noescape
Fix basic callee analysis to see noescape partial_applies.
2018-05-04 16:27:24 -07:00
Andrew Trick
b975156186 Fix AccessedStorageAnalysis to use getCalleeOrigin for closures.
Added test case for closures.
2018-05-04 14:02:10 -07:00
Andrew Trick
4b70d8608a Fix basic callee analysis to see noescape partial_applies.
This in turn allows side-effect analysis to handle directly applied noescape
closures.
2018-05-04 13:16:40 -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
swift-ci
9d3b6be3fc Merge pull request #16003 from atrick/access-analysis 2018-05-02 10:22:26 -07:00
Arnold Schwaighofer
c4ed564a46 Fixes to AccessSummaryAnalysis
The pattern we see for noescape closure passed to objective c is different:
There is the additional without actually escaping closure sentinel.

rdar://39682865
2018-05-01 07:24:19 -07:00
Huon Wilson
978a5c89b1 [SILOptimizer] std::function -> llvm::function_ref for some non-escaping params. 2018-05-01 08:29:07 +10:00
Andrew Trick
b66fa09c29 Add AccessedStorageAnalysis.
An interprocedural analysis pass that summarizes the dynamically
enforced formal accesses within a function. These summaries will be
used by a new AccessEnforcementOpts pass to locally fold access scopes
and remove dynamic checks based on whole module analysis.
2018-04-17 17:35:39 -07:00
Andrew Trick
cdcb7c7a2c [NFC] SideEffectAnalysis refactoring and cleanup.
Make this a generic analysis so that it can be used to analyze any
kind of function effect.

FunctionSideEffect becomes a trivial specialization of the analysis.

The immediate need for this is to introduce an new
AccessedStorageAnalysis, although I foresee it as a generally very
useful utility. This way, new kinds of function effects can be
computed without adding any complexity or compile time to
FunctionSideEffects. We have the flexibility of computing different
kinds of function effects at different points in the pipeline.

In the case of AccessedStorageAnalysis, it will compute both
FunctionSideEffects and FunctionAccessedStorage in the same pass by
implementing a simple wrapper on top of FunctionEffects.

This cleanup reflects my feeling that nested classes make the code
extremely unreadable unless they are very small and either private or
only used directly via its parent class. It's easier to see how these
classes compose with a flat type system.

In addition to enabling new kinds of function effects analyses, I
think this makes the implementation of side effect analysis easier to
understand by separating concerns.
2018-04-16 17:05:04 -07:00
Erik Eckstein
8b5198cc20 EscapeAnalysis: Convert a if to an assert.
because it should always be true
It's a NFC.
2018-04-12 12:28:47 -07:00
eeckstein
4a500008c6 Merge pull request #15874 from eeckstein/fix-membehavior
Fix wrong usage of escape analysis in MemBehavior
2018-04-11 13:38:24 -07:00
Erik Eckstein
3adf59561f Fix wrong usage of escape analysis in MemBehavior
The EscapeAnalysis:canEscapeTo function was actually broken, because it did not detect all escapes of a reference/pointer.
I completely replaced the implementation with the correct one (canObjectOrContentEscapeTo) and removed the now obsolete canObjectOrContentEscapeTo.
Fixes a miscompile.

rdar://problem/39161309
2018-04-11 10:20:36 -07:00
Andrew Trick
e13e1fbe9d Fix a latent bug in RCIdentityAnalysis.getUsers.
This bug was introduced by the migration to multi-value SILInstructions.

This fix will be tested by copy forwarding.

WARNING: To handle the potential existence of multi-value
instructions, don't simply iterate over the results. You may be
inadvertently skipping instructions with no results.

Details:

RCIdentityFunctionInfo::getRCUsers searches the def-use chain to find
all users. When visiting a use, it checks the result to determine if
it's casting or extracting the original reference operand. If, so it
continues along the def-use chain. So, it simply wasn't visiting
instructions with no results.

I inverted the logic so that the special case now requires identifying
a SingleValueInstruction, while the default case is conservative.
2018-04-10 22:08:35 -07:00
Slava Pestov
1a66c89d29 SIL: Remove CHA's unused 'protocol implementations' analysis
This was performing a walk over all parsed Decls in all
sources files, to build a data structure that was never used.
2018-04-01 02:27:26 -07:00
Erik Eckstein
5ce29cbdb5 BasicCalleeAnalysis: improve the fix for witness tables.
Although I think it is a NFC, it makes it more safe. This change is to align the fix with what I did on the 4.1 branch.
Also remove the not needed $ chars in the function names in the test case.
2018-03-21 13:24:53 -07:00
Erik Eckstein
f225c00f15 BasicCalleeAnalysis: fix a problem with witness method visibility.
rdar://problem/38510299
2018-03-19 15:18:57 -07:00
Andrew Trick
9703d56e03 [exclusivity] Remove dead access markers after optimization.
Generalized to handle scope markers which will become common with future
ownership and lifetime dependence markers.
2018-03-11 23:13:30 -07:00
Arnold Schwaighofer
5940796cc1 SIL: Add an is_escaping_closure instruction
Will be used to verify that withoutActuallyEscaping's block does not
escape the closure.

``%escaping = is_escaping_closure %closure`` tests the reference count. If the
closure is not uniquely referenced it prints out and error message and
returns true. Otherwise, it returns false. The returned result can be
used with a ``cond_fail %escaping`` instruction to abort the program.

rdar://35525730
2018-03-07 08:56:00 -08:00
Sho Ikeda
25cdc981c7 Merge pull request #14977 from ikesyo/gardening-use-empty
[gardening] Use `empty()` over `size() == 0`
2018-03-07 09:34:26 +09: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
Sho Ikeda
26d650292f [gardening] Use empty() over size() == 0 2018-03-05 14:43:13 +09:00
Michael Gottesman
4941fba4c8 [arc] An apply of a callee_guaranteed thick function is a "guaranteed use" of the function.
rdar://37820485
2018-02-25 15:10:12 -08:00
Arnold Schwaighofer
a9fa8c9c2b Merge pull request #14514 from aschwaighofer/wip_closure_capture_abi_part2
WIP: Closure ABI - Make @noescape Swift closures trivial
2018-02-14 05:19:25 -08:00
Arnold Schwaighofer
08872244b7 ClosureSpecializer: Handle closure arguments in throwing functions
We can treat a throw instruction like we would a return. Insert the
release for the propagated partial_apply before the throw instruction.
2018-02-13 11:13:14 -08:00
eeckstein
fb3ed8a9e1 Merge pull request #14568 from eeckstein/allow-shared-external
Allow shared_external functions to be considered by interprocedural analysis
2018-02-13 11:08:22 -08:00
Joe Shajrawi
102b6f0b61 Merge pull request #14587 from shajrawi/newinstr
Optimizer peephole for tagged BridgeObjects
2018-02-13 07:38:53 -08:00
Arnold Schwaighofer
4745a7e280 AccessSummaryAnalysis: ConvertEscapeToNoEscapeInst is an expected use of partial_apply 2018-02-13 04:19:59 -08:00
Erik Eckstein
1b7b3898fc Allow shared_external functions to be considered by interprocedural analysis.
This is a follow-up to https://github.com/apple/swift/pull/14516
2018-02-12 14:34:56 -08:00
Joe Shajrawi
f732ea66ef Builtins: add ValueToBridgeObject instruction 2018-02-12 13:27:59 +02:00