Commit Graph

2864 Commits

Author SHA1 Message Date
Arnold Schwaighofer
8aaa7b4dc1 SILOptimizer: Pipe through TypeExpansionContext 2019-11-11 14:21:52 -08:00
Michael Gottesman
64eeed835a [pmo] Add an assert that when using SILSSAUpdater in ossa, we only have singular values if the underlying type is either trivial or we have a take.
The reason why this is true is that we will be inserting new copy_values for
each available value implying that we can never have such a singular value.

I also added two test cases that show that we have a singular value with the
trivial type and that works.
2019-11-11 13:18:03 -08:00
Michael Gottesman
55b6ff1f5e [pmo] Expand an assert that validates that we never handlePrimitiveValues when promoting takes.
The key thing to note here is that when we are processing a take, we validate
that at all destroy points we have a fully available value. So we should never
hit this code path which is correct. This assert just makes the assumption
clearer in the small when reading code locally in handlePrimitiveValue.
2019-11-11 13:18:03 -08:00
Michael Gottesman
76e243ce55 [pmo] Allow for the AvailableValueAggregator to know if it is being used to Borrow, Copy, Take.
Previously, we only specified via a boolean value whether or not we were a take
or not. Now we have an enum which is more specific and descriptive. It will also
let me fix an issue that occurs with Borrow/Copy incrementally as separate
patches.
2019-11-11 11:31:38 -08:00
Michael Gottesman
476f401b4a [pmo] NFC. Split addMissingDestroysForCopiedValues into a version for load_borrow and one for load.
This is a pure refactor so I can make some subsequent transformations easier to
do.

I also did a little bit of debridement when there were opportunities to
flatten control flow by eliminating direct checks for one or the other classes.
2019-11-10 15:51:40 -08:00
Ravi Kandhadai
882f4d7ba3 [OSLogOptimization] Minor refactoring of the logic for determining
whether a SILValue can be folded and also of the logic for emitting
SIL code for symbolic values.
2019-11-07 16:52:10 -08:00
Ravi Kandhadai
48bc63bb8a [Constant Evaluator] Add support for tracking types with aggregate
symbolic values, which are used to represent constant struct and tuple
instances. Associating those symbolic values with the types of the
aggregate they are representing will allow writing some sanity checks,
and will also make constant folding of the symbolic values easier and
more robust.
2019-11-06 12:47:42 -08:00
Michael Gottesman
8d9455e757 [pmo] Rather than bailing from addMissingDestroysForCopiedValues if we are in non-ossa, just always assume we are in ossa in the code and do not call it if we are in non-ossa. 2019-11-04 11:24:33 -08:00
Michael Gottesman
5ecacb9820 [pmo] Cleanup some old code to use the same style. 2019-11-04 11:24:33 -08:00
Michael Gottesman
fad176e423 [pred-memopt] Extract out the computation of available values into a helper.
NFC.
2019-11-03 17:16:43 -08:00
Michael Gottesman
933af097cb [pred-memopt] Split promoteLoadCopy into 3 methods, one for copy_addr, load, load_borrow.
NFC.

This just simplifies the code and makes it easier to reason about. I debried a
little bit as well, but nothing too much.
2019-11-03 17:16:42 -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
Robert Widmann
8a69f886ad Merge pull request #27955 from AnthonyLatsis/bracestmt_cleanup
NFC: Solidify and tidy up the BraceStmt interface
2019-10-31 13:48:40 -07:00
Anthony Latsis
6325915b4b NFC: Solidify and tidy up the BraceStmt interface 2019-10-30 16:43:59 +03:00
swift-ci
2534af7045 Merge pull request #27942 from ravikandhadai/oslog-append-interpolation-consteval-improved 2019-10-29 21:48:50 -07:00
Ravi Kandhadai
861c0aaca9 [OSLogOptimization][stdlib/private] Make string interpolation methods
of OSLogMessage constant evaluable and remove @_transparent annotation
from the methods. Also, improve diagnostics in the OSLogOptimization
pass as now it rely on seeing the appendInterpolation/Literal calls.
2019-10-29 19:35:37 -07:00
Varun Gandhi
033be473a8 [NFC] Change ValueOwnershipKind::Any to ValueOwnershipKind::None in comments. (#27925)
Follow-up to PR #27879.
2019-10-29 16:50:45 -07:00
swift-ci
c71957c009 Merge pull request #27744 from ravikandhadai/oslog-optimization-bug-fixes 2019-10-28 18:20:52 -07:00
Michael Gottesman
386568c7e9 [gardening] Move ome from Transform -> Mandatory as per @atrick's request. 2019-10-28 16:26:13 -07:00
Ravi Kandhadai
ffd3fef199 [OSLogOptimization] Create a SIL test for OSLogOptimization pass to test folding logic,
add -enable-ownership-stripping-after-serialization flag to OSLog optimization tests,
and update the folding logic and end-of-use discovery logic to handle ownership
and non-ownership SIL.
2019-10-28 15:33:01 -07:00
Michael Gottesman
7ee5ad7318 [sil] Rename {,Strong}Copy{Unowned,Unmanaged}. 2019-10-26 17:03:47 -07:00
Joe Groff
1a1d9e63f7 Merge pull request #27887 from jckarter/subst-sil-function-type-interface
SIL: Add fields to SILFunctionType for substituted function types.
2019-10-26 10:44:48 -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
Michael Gottesman
26a734e58e [sil] Rename ValueOwnershipKind::{Any,None} 2019-10-25 10:28:25 -07:00
Slava Pestov
05898d8deb SIL: More descriptive diagnostic about escaping closure vs local function
Fixes <rdar://problem/50554625>.
2019-10-21 17:47:18 -04:00
Michael Gottesman
95afa847cd [mandatory-inlining] Make cleanupLoadedCalleeValue more conservative.
Specifically, we may have a loaded callee value from a stack value. This change
just makes it so that we do not optimize if we do not actually have the box.

rdar://56386236
2019-10-18 13:33:18 -07:00
Ravi Kandhadai
05e24c0aaf [OSLogOptimization] Improve a comment in the OSLogOptimization pass
and rename a function.
2019-10-11 14:23:44 -07:00
Jordan Rose
171ff440fc Remove swift::reversed in favor of llvm::reverse (#27610)
The former predates the latter, but we don't need it anymore! The
latter has more features anyway.

No functionality change.
2019-10-10 17:16:09 -07:00
Slava Pestov
d8b61ff24b Sema: Peel off typeCheckParameterList()'s specifier computation into a request
Since getSpecifier() now kicks off a request instead of always
returning what was previously set, we can't pass a ParamSpecifier
to the ParamDecl constructor anymore. Instead, callers either
call setSpecifier() if the ParamDecl is synthesized, or they
rely on the request, which can compute the specifier in three
specific cases:

- Ordinary parsed parameters get their specifier from the TypeRepr.

- The 'self' parameter's specifier is based on the self access kind.

- Accessor parameters are either the 'newValue' parameter of a
  setter, or a cloned subscript parameter.

For closure parameters with inferred types, we still end up
calling setSpecifier() twice, once to set the initial defalut
value and a second time when applying the solution in the
case that we inferred an 'inout' specifier. In practice this
should not be a big problem because expression type checking
walks the AST in a pre-determined order anyway.
2019-10-10 15:00:07 -04:00
Michael Gottesman
9f3bb93ab0 [mandatory-combine] Simplify block traversal used to initialize worklist.
Previously, we were using a pointer set to ensure that was checked when a value
was popped off the worklist. In this commit, I change the code so that we
instead use the set to guard insertion into the worklist.

I changed the name of the set to blockAlreadyAddedToWorklist. Hopefully that
will make things a bit clearer.
2019-10-09 23:13:28 -07:00
Michael Gottesman
8512387dce [mandatory-combine] Make sure to bail early if we have an external declaration.
Beyond creating a nice invariant, I am going to be using this invariant in some
small code improvements to MandatoryCombine that assume we have an initial basic
block.
2019-10-09 23:13:28 -07:00
Saleem Abdulrasool
085439781b SILOptimizer: rename cdecl to ctor (NFC)
`cdecl` is a keyword in the non-conforming C++ dialect used on Windows
which expands to `__cdecl`.  Simply rename the variable to avoid the
replacement.  Although it is possible to `#undef cdecl`, renaming makes
it less confusing for non-Windows developers.
2019-10-03 08:44:09 -07:00
Ravi Kandhadai
af5eda5d24 Merge pull request #27419 from ravikandhadai/oslog-backward-dependency-analysis
[SIL Optimization][OSLogOptimization] Improve the logic for discovering the beginning of the string interpolation passed to os log APIs.
2019-10-02 16:45:13 -07: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
Ravi Kandhadai
b2c1540ca2 [SIL Optimization][OSLogOptimization] Improve the logic for
discovering the beginning of the string interpolation passed
to os log APIs.

The implementation follows the chain of dependencies starting
from an initializer call to OSLogMessage until the first
instruction of interpolation is discovered. This is more
robust towards changes to the SIL generation of string
interpolation literals.
2019-09-28 17:59:06 -07:00
Michael Gottesman
2940e7d561 [apply-site] Refactor out a helper function called insertAfterApply -> ApplySite.insertAfter().
Often times when one is working with apply sites, one wants to insert
instructions after both terminator apply sites and normal apply sites. This can
get ackward and result in unnecessary if-else code that is all really doing the
same thing, once for terminator instructions and once for normal instructions.

insertAfterApply is a helper method that MandatoryInlining uses for this purpose
and it is so useful that I want to use it somewhere else in closure lifetime
fixup as well. I am moving it onto apply site since that is the true abstraction
that insertAfterApply works with.
2019-09-27 09:54:11 -07:00
Michael Gottesman
12aa95ac1e [ownership] Only allow BranchPropagatedUser to be constructed from Operands.
This commit only changes how BranchPropagatedUser is constructed and does not
change the internal representation. This is a result of my noticing that
BranchPropagatedUser could also use an operand internally to represent its
state. To simplify how I am making the change, I am splitting the change into
two PRs that should be easy to validate:

1. A commit that maps how the various users of BranchPropagatedUser have been
constructing BPUs to a single routine that takes an Operand. This leaves
BranchPropagatedUser's internal state alone as well as its user the Linear
Lifetime Checker.

2. A second commit that changes the internal bits of the BranchPropagatedUser to
store an Operand instead of a PointerUnion.

This will allow me to use the first commit to validate the second.
2019-09-23 17:24:55 -07:00
Michael Gottesman
e90a68fa17 [polymorphic-builtins] Teach dataflow diagnostics how to emit an error if it sees an unspecialized polymorphic builtin.
This will ensure that if an expert user is using this feature and makes a
mistake as a result of tweaking their code, they get an error. This will ensure
they do not ship and look into why this is happening.

This is not intended to be used by anyone except for expert stdlib users.
2019-09-20 17:25:56 -07:00
Ravi Kandhadai
22861ec0fd [Constant Evaluator] Pass assert configuartion option to the
evaluator to precisely evaluate Builtin.assert_configuration.

Unify UnknownReason::Trap and UnknownReason::AssertionFailure error
values in the constant evaluator, now that we have 'condfail_message'
SIL instruction, which provides an error message for the traps.
2019-09-18 17:35:46 -07:00
swift-ci
00ac8d8f53 Merge pull request #27228 from ravikandhadai/constexpr-oslog-constant-evaluable-check 2019-09-18 16:00:17 -07:00
Ravi Kandhadai
61fd4b1431 [OSLog][Test] Update the new oslog overlay implementation to use
@_semantics("constant_evaluable") annotation to denote constant
evaluable functions.

Add a test suite that uses the sil-opt pass ConstantEvaluableSubsetChecker.cpp
to check the constant evaluability of function in the OSLog
overlay.
2019-09-18 10:44:48 -07:00
David Zarzycki
e40759b335 [SILOpt] NFC: fix -Wgnu-anonymous-struct warning 2019-09-18 10:59:37 +03:00
Michael Gottesman
7e3608e007 [ownership] Add a new checkValue overload to LinearLifetimeChecker that validates the linear lifetime of the passed in value and returns bool to indicate success/failure.
This is the first in a series of patches that begin to hide the underlying
linear lifetime implementation underneath the facade of the linear lifetime
checker. I only updated the users that this applies to.
2019-09-17 14:41:30 -07:00
Michael Gottesman
dceca2bc3b [ownership] Create a context structure for the linear lifetime checker.
This is just a simple refactoring commit in preparation for hiding more of the
details of the linear lifetime checker. This is NFC, just moving around code.
2019-09-16 20:13:56 -07:00
Michael Gottesman
bb4df032e7 [ownership] Add a new class BorrowScopeIntroducingValue that enables code to work abstractly with values that introduce a new borrow scope.
The idea is that this can be used to work with things like load_borrow,
begin_borrow, SILFunctionArgument, results of begin_apply(in the future) and the like in a
generic way using exhaustive switches to make sure this code stays up to date.

I refactored code in SemanticARCOpts and some utilities in OwnershipUtils.cpp to
use these new APIs. The code looks a lot nicer and should be quite easy to
expand to handle new borrow introducers (e.x.: end_apply).
2019-09-16 09:33:49 -07:00
Andrew Trick
d80e2818fe Add a comment to SemanticARC.
To explain something subtle: copying a loaded value is only required
because assignment may cause the in-memory value to be released.
2019-09-09 20:56:07 -07:00
Michael Gottesman
9ffb35c5d3 Merge pull request #27094 from gottesmm/pr-399ba0e079dfa48cb9aba436c0a62bd4087c2304
[semantic-arc-opts] Eliminate dead trivial instructions /after/ the m…
2019-09-09 19:21:09 -07:00
Michael Gottesman
8e110108d3 [semantic-arc-opts] Eliminate dead trivial instructions /after/ the main loop to avoid issues around adding/removing things from the worklist.
rdar://55127027
2019-09-09 14:52:36 -07:00