Commit Graph

2864 Commits

Author SHA1 Message Date
Michael Gottesman
096d32b958 Merge pull request #25412 from gottesmm/pr-e50b660a602a20338a4dc4bb5447649782a69175
[mandatory-inlining] When running with sil-verify-all, verify at the …
2019-06-13 21:13:19 -07:00
ravikandhadai
6d3e3f02eb Merge pull request #24971 from ravikandhadai/oslog-optimization-transparent
[os_log][SIL Optimization] Make new os log APIs @_transparent and change OSLogOptimization pass so that inlining is not performed.
2019-06-13 11:46:12 -07:00
ravikandhadai
ff3d081284 Merge pull request #25403 from ravikandhadai/rdar51198592-DI-crash
[Definite Initialization] Fix a bug that made DI wrongly think that an unused access to "self" in a delegating initializer is a use of the form: `type(of:self)`
2019-06-12 17:40:51 -07:00
Michael Gottesman
9c5061d9ff [mandatory-inlining] When running with sil-verify-all, verify at the root of each function we visit.
This at least ensures that we error immediately after processing the bad
root function. NOTE: we will inline recursively into it still, but at least we
stop before processing the entire module.
2019-06-12 16:42:09 -07:00
Ravi Kandhadai
8d1d863f43 [Definite Initialization] Fix a bug that made the DI wrongly think
that an unused access to "self" in a delegating initializer is a use
of the form: `type(of:self)`

<rdar://51198592>
2019-06-12 14:53:01 -07:00
Michael Gottesman
2e9c904e16 [mandatory-inlining] Teach mandatory inlining how to handle begin_borrow instructions.
This was exposed by test/SILOptimizer/diagnostic_constant_propagation.swift. It
isn't a pattern in the mandatory inlining tests. I added some tests for it.
2019-06-12 13:13:52 -07:00
Ravi Kandhadai
4d4609a7d8 [os_log][SIL Optimization] Modify the OSLogOptimization pass to run
on ownership SIL.
2019-06-11 17:24:16 -07:00
Ravi Kandhadai
27eefd7311 [os_log][SIL Optimization] Make new os log APIs @_transparent and
change OSLogOptimization pass so that inlining is not performed.
2019-06-11 15:47:21 -07:00
Michael Gottesman
cf0405c7f1 [clf] Simplify the double diamond lifetime extension for escaping swift funcs -> noescape blocks.
Specifically, when we optimize conversions such as:

  Optional<@escaping () -> ()>

  ->

  Optional<@noescape () -> ()>

  ->

  Optional<@noescape @convention(block) () -> ()>

previously we were lifetime extending over the @noescape lifetime barrier by
making a copy and then putting a mark_dependence from the copy onto the original
value. This was just a quick way to tell the ownership verifier that the copy
was tied to the other value and thus should not be eliminated. The correctness
of the actual lifetime extension comes from the optimizer being conservative
around rr insts.

This commit instead changes our optimization to borrow the copied optional
value, extract the payload, and use that instead.
2019-06-09 22:04:32 -07:00
Saleem Abdulrasool
731c31f9a5 MSVC: litter the code with llvm_unreachable (NFC)
Add `llvm_unreachable` to mark covered switches which MSVC does not
analyze correctly and believes that there exists a path through the
function without a return value.
2019-06-01 19:02:46 -07:00
Michael Gottesman
ada2f9ce46 [pred-deadalloc-elim] Teach the pass how to eliminate dead allocations that are load [take], store [init] back into memory.
I am doing this to eliminate some differences in codegen before/after
serialization ownership. It just means less of the tests need to be touched when
I flip the switch.

Specifically, today this change allows us to handle certain cases where there is
a dead allocation being used to pass around a value at +1 by performing a load
[take] and then storing a value back into the memory. The general format is an
allocation that only has stores, load [take], and destroy_addr users. Consider
the following SIL:

```
store %x to [init] %mem            (0)
%xhat = load [take] %mem           (1)
%xhat_cast = apply %f(%xhat)       (2)
store %xhat_cast to [init] %mem    (3)
destroy_addr %mem
```

Notice how assuming that we can get rid of the store, we can perform the
following store -> load forwarding:

```
%xhat_cast = apply %f(%x)          (2)
store %xhat_cast to [init] %mem    (3)
destroy_addr %mem
```

In contrast, notice how we get an ownership violation (double consume of %x by
(0) and (2)) if we can not get rid of the store:

```
store %x to [init] %mem
%xhat_cast = apply %f(%x)
store %xhat_cast to [init] %mem    (2)
destroy_addr %mem
```

This is in fact the same condition for promoting a destroy_addr since when a
destroy_addr is a load [take] + destroy_value. So I was able to generalize the
code for destroy_addr to handle this case.
2019-05-31 12:12:54 -07:00
Doug Gregor
c02ecf9859 [SE-0258] Rename to Property Wrappers 2019-05-29 22:17:50 -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
Michael Gottesman
372046cc04 [diagnose-unreachable] Ignore/eliminate end_borrows after noreturn functions.
This seems to happen more often when we strip ownership after diagnose
unreachable.
2019-05-22 20:06:18 -07:00
Andrew Trick
df2afaa5c2 Merge pull request #24936 from atrick/comment-closure-scope
Add a comment in ClosureScopeAnalysis.
2019-05-22 10:36:19 -07:00
Andrew Trick
6e3f56fa92 Fix exclusivity diagnostics to be aware of [dynamically_replaceable].
Previously, any function marked [dynamically_replaceable] that was
partially applied and captured by address would not be diagnosed.

This is a rare thing. For example:

struct S {
  var x = 0
  public mutating func testCallDynamic() {
    dynamic func bar(_ i: inout Int) {
      i = 1
      x = 2
    }
    bar(&x)
  }
}

Fixes <rdar://problem/50972786> Fix exclusivity diagnostics to be
aware of [dynamically_replaceable].
2019-05-20 21:46:30 -07:00
Andrew Trick
4e00cad71e Add a comment in ClosureScopeAnalysis.
As a follow-up to reviewing the dynamically replaceable
implementation, document the place where this analysis will likely
crash in the future once we start optimizing non-escaping closure
captures.
2019-05-20 17:12:13 -07:00
Michael Gottesman
1db75d6430 Merge pull request #24920 from gottesmm/pr-1ce3bba234b68150bc0675d364f95465bb26344d
[mandatory-inlining] When using the linear lifetime checker to insert…
2019-05-20 14:53:41 -07:00
Michael Gottesman
3b029010b9 [mandatory-inlining] When using the linear lifetime checker to insert compensating releases, if we find a double use due to a loop, do not insert an apply at that call site.
Otherwise, one will get use after frees. I added an interpreter test as wlel as
an end to end test.

rdar://50884462
2019-05-20 12:12:13 -07:00
Richard Wei
c718126be1 Update OSLogOptimizationc.cpp to fix warning.
Fixes the following warning:
```console
third_party/unsupported_toolchains/swift/src/swift/lib/SILOptimizer/Mandatory/OSLogOptimization.cpp:396:1: warning: control may reach end of non-void function [-Wreturn-type]
}
^
```
2019-05-19 20:42:52 -07:00
Slava Pestov
d832fb9823 SILOptimizer: Fix crash on invalid in invalid escaping captures pass
An inout capture of 'self' is not lowered as a SIL argument inside
an initializer, so add a new check to handle this case.

Fixes <rdar://problem/50412872>.
2019-05-18 00:24:13 -04:00
Ravi Kandhadai
9be4fef53a [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-14 18:08:59 -07:00
ravikandhadai
f563212f03 Revert "[SIL Optimization] Add a mandatory pass for optimizing the new os log APIs based on string interpolation." 2019-05-14 15:11:05 -07:00
Andrew Trick
c1bda8f090 Replace AccessedStorage projection with an index.
Further simplify AccessedStorage. Shrink it to two words. Remove the
Projection abstraction and streamline the projection logic.
2019-05-14 12:44:46 -07:00
Ravi Kandhadai
b7b46622aa [SIL Optimization] Add a mandatory optimization pass for optimizing
the new os log APIs based on string interpolation.
2019-05-13 19:40:39 -07:00
Andrew Trick
6b1af3de0e Merge pull request #24458 from atrick/clean-accessed-storage
Remove macros and visitors from AccessedStorage.
2019-05-08 16:23:08 -07:00
nate-chandler
5e7f06d3f1 Merge pull request #24546 from nate-chandler/nate/omit-return-pr-fixes
Address PR comments on return omission.
2019-05-07 16:48:41 -07:00
Nate Chandler
1267f659c6 Address PR comments on return omission. 2019-05-07 14:20:21 -07:00
Andrew Trick
f395f86039 Canonicalize loads in SILGenCleanup.
Reimplement load instruction canonicalization as part of the
CanonicalizeInstruction utility.
2019-05-06 13:31:35 -07:00
Andrew Trick
301ff8c9f0 Add a SILGenCleanup pass.
The SILGenCleanup pass runs before diagnostics to perform any
canonicalization required by diagnostics.
2019-05-06 09:36:08 -07:00
Andrew Trick
e400b66897 Cleanup replaceAllUsesAndErase, return an iterator, allow erase handlers.
This will make the forthcoming CanonicalizeInstruction interface more
clear.

This is generally the better approach to utilities that mutate the
instruction stream. It avoids the temptation to assume that only a
single instruction will be deleted or that only instructions before
the current iterator will be deleted. This often happens to work but
eventually fails in the presense of debug and end-of-scope
instructions.

A function returning an iterator has a more clear contract than one
accepting some iterator reference of unknown
providence. Unfortunately, it doesn't work at the lowest level of
utilities, such as recursivelyDeleteTriviallyDeadInstructions, where
we want to handle instruction batches.
2019-05-06 08:36:56 -07:00
Andrew Trick
5cd187d834 Remove macros and visitors from AccessedStorage.
Cleaning up in preparation for making changes that improve
compile-time issues in AccessEnforcementOpts.

This is a simple but important enum with a handful of cases. The cases
need to be easily referenced from the header. Don't define them in a
separate .def. Remove the visitor biolerplate because it doesn't serve
any purpose.

This enum is meant to be used with covered switches. The enum cases do
not have their own types, so there's no performance reason to use a
Visitor pattern.

It should not be possible to add a case to this enum without carefully
considering the impact on the encoding of this class and the impact on
each and every one of the uses. We always want covered switches at the
use sites.

This is a major improvement in readability and usability both in the
definition of the class and in the one place where a visitor was used.
2019-05-02 18:08:27 -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
Slava Pestov
472787bab7 SIL: isNonThrowing parameter of SILBuilder::create{Begin,}Apply() defaults to false
Also remove the overload of createApply() that does not take a SubstitutionMap.
It accomplishes nothing except creating ambiguity.
2019-04-25 22:27:38 -04:00
nate-chandler
155a155000 Merge pull request #23251 from nate-chandler/nate/omit-return
Allow return to be omitted from single expression functions.
2019-04-25 08:36:34 -07:00
Nate Chandler
4f269fc6c0 Added diagnostic for dangling expression at end.
If the final expression in a function or closure which is missing a
return has the appropriate type, rather than producing the usual
diagnostic about a missing return, produce a diagnostic with a fixit to
insert a return before thatn final expression.

h/t Nate Cook
2019-04-24 09:59:54 -07:00
Doug Gregor
03f674018b Eliminate a use-after-free in assign_by_delegate lowering.
ASan is magic. Thank you, ASan.
2019-04-23 11:32:28 -07:00
Erik Eckstein
e1674232c7 RawSILInstLowering: lower assign_by_delegate 2019-04-23 11:32:28 -07:00
Erik Eckstein
24e28caf3d Fix an assert in AccessEnformentSelections to support assign_by_delegate 2019-04-23 11:32:28 -07:00
Erik Eckstein
86fb74a34e DI: support assign_by_delegate instruction 2019-04-23 11:32:28 -07:00
ravikandhadai
28efe03fab Merge pull request #23456 from ravikandhadai/constexpr-local-allocation
[const evaluator] Parameterize allocation of symbolic values in the constant interpreter.
2019-04-11 15:14:09 -07:00
Andrew Trick
63624d92d0 Comment the new code that diagnoses noescape recursion.
This used to be done in Sema. It's a special rule required for
exclusivity that is totally non-obvious. Explain what the code is
doing.
2019-04-09 22:10:59 -07:00
Ravi Kandhadai
bb7363da2e [const evaluator] Parameterize allocation of symbolic values in the
constant interpreter. Based on this, change to a short-lived bump
allocator for storing symbolic values in the pass that checks #assert.
2019-04-09 13:56:21 -07:00
Slava Pestov
e2cb0572fe SILOptimizer: Re-implement NPCR diagnostics in SIL pass
This fixes a test involving transitive captures of local functions,
as well as an infinite recursion possible with the old code.

Fixes <rdar://problem/34496304>.
2019-04-09 16:44:50 -04:00
Slava Pestov
9ac0dc3d6c SILOptimizer: Re-implement escaping capture diagnostics
The new pass is based on existing asserts in DiagnoseStaticExclusivity.
They were compiled out in release builds and only checked for captures of
inout parameters. This patch converts the assertions into diagnostics and
adds checks for captures of non-escaping function values.

Unlike the Sema-based checks that this replaces, the new code handles
transitive captures from recursive local functions, which means certain
invalid code that used to compile will now be rejected with an error.

The new analysis also looks at the ultimate usages of a local function
instead of just assuming all local functions are escaping, which fixes
issues where the compiler would reject valid code.

Fixes a bunch of related issues, including:

- <rdar://problem/29403178>
- <https://bugs.swift.org/browse/SR-8546> / <rdar://problem/43355341>
- <https://bugs.swift.org/browse/SR-9043> / <rdar://problem/45511834>
2019-04-09 15:02:14 -04:00
Michael Gottesman
674721bc38 [semantic-arc-opts] When looking for consuming uses, do not add trivial uses to the worklist.
rdar://49396970
2019-04-01 10:34:00 -07:00
Michael Gottesman
e11794bbfa [closure-lifetime-fixup] Make sure that all copy_value we insert have a data dependence on the cvt_escape_to_no_escape.
Otherwise, the ownership model doesn't understand the need for the copies and
just removes them.
2019-03-26 16:56:29 -07:00
Michael Gottesman
c27b1cbfad [closure-lifetime-fixup] Standardize style. 2019-03-26 15:14:24 -07:00
Michael Gottesman
f8d15143a5 [closure-lifetime-fixup] Expose a flag instead of allocating a new instruction.
Small inefficiency I noticed.
2019-03-25 15:00:35 -07:00
Michael Gottesman
72a093da52 [semantic-arc-opts] Teach the guaranteed copy_value peephole how to handle instructions that can forward either owned or guaranteed ownership.
This fixes issues exposed by my turning off the Nominal Type RValue peephole. It
should give us some nice ARC wins as well potentially.
2019-03-21 13:50:41 -07:00