Commit Graph

161 Commits

Author SHA1 Message Date
Meghana Gupta
9c5c73370b [ownership] Enable GlobalOpt on ossa and add ossa tests (#32621)
GlobalOpt works mostly on trivial values (there are special cases for ObjectInst and ValueToBridgeObjectInst).
optimizeGlobalAccess is explicitly turned off for non-trivial values. optimizeInitializer calls SILGlobalVariable::isValidStaticInitializerInst which limits it to mostly trivial values except for special cases for ObjectInst and ValueToBridgeObjectInst.

This changes adds GlobalOpt tests for ossa and enables GlobalOpt on ossa
2020-07-06 22:59:43 -07:00
Karoy Lorentey
68351d2110 Revert "Merge remote-tracking branch 'origin/master-next'"
This reverts commit 1c9b0908e6, reversing
changes made to 3eb82c1836.
2020-06-08 16:48:38 -07:00
Karoy Lorentey
1c9b0908e6 Merge remote-tracking branch 'origin/master-next'
# Conflicts:
#	include/swift/Frontend/FrontendInputsAndOutputs.h
2020-06-08 16:43:40 -07:00
Anthony Latsis
9fd1aa5d59 [NFC] Pre- increment and decrement where possible 2020-06-01 15:39:29 +03:00
swift_jenkins
63f5052019 Merge remote-tracking branch 'origin/master' into master-next 2020-03-24 07:20:09 -07:00
Erik Eckstein
84e2a568ca GlobalOpt: don't speculatively execute global initializers
For example: hoist out of loops where the loop count could be 0.
We did this on purpose. But, if not wrong, it's at least very confusing if the initializer has observable side effects.
Instead let CSE and LICM do the job and handle initializer side-effects correctly.

rdar://problem/60292679
2020-03-23 15:53:22 +01:00
Erik Eckstein
526a0c8f33 GlobalOpt: fix global constant propagation of globals with multiple writes
Detect stores to globals which get the address from an addressor call (and not from global_addr).
This kind of SIL is currently not generated, so most likely this bug does not show up. But as soon as we CSE addressor calls, it can be a real problem.
2020-03-23 14:41:31 +01:00
Alex Langford
ea4204244c Adjust for LLVM commit 21390eab4c05
SCCIterator::hasLoop was renamed SCCIterator::hasCycle
2020-03-03 12:33:59 -08:00
Erik Eckstein
370082babe GlobalOpt: handle access markers correctly.
rdar://problem/59345288
2020-03-02 18:18:30 +01:00
Erik Eckstein
43e8b07e3f GlobalOpt: improvements for constant folding global variables
* Simplified the logic for creating static initializers and constant folding for global variables: instead of creating a getter function, directly inline the constant value into the use-sites.
* Wired up the constant folder in GlobalOpt, so that a chains for global variables can be propagated, e.g.

  let a = 1
  let b = a + 10
  let c = b + 5

* Fixed a problem where we didn't create a static initializer if a global is not used in the same module. E.g. a public let variable.
* Simplified the code in general.

rdar://problem/31515927
2020-02-26 17:35:05 +01:00
zoecarver
5d086e7862 Check that there is only one use of a given global addr function 2020-02-05 13:16:49 -08:00
zoecarver
690c1f5ceb Merge branch 'master' into optimize/global-var-init 2020-02-03 21:24:47 -08:00
Slava Pestov
3b9014dc9a Merge pull request #28324 from zoecarver/optimize/dead-global-vars
Optimize dead private global variables
2020-01-13 16:41:21 -05:00
zoecarver
3335ba5fca Update isAssignedOnlyOnceInInitializer to be more correct 2020-01-10 09:31:34 -08:00
zoecarver
be6239aa01 Fix should optimize logic 2019-12-24 15:39:56 -08:00
Michael Gottesman
df6d25fa8a [gardening] Be more defensive around a potentially uninitialized Store by initializing it to nullptr and asserting that it is not nullptr (i.e. was assigned) before using it. 2019-12-18 13:54:11 -08:00
zoecarver
ea0698fcda Add comments and fix checking dead instructions 2019-12-14 10:31:56 -08:00
zoecarver
7304d5a257 Fix logic around checking dead instructions 2019-12-14 10:11:54 -08:00
zoecarver
af84de80b5 Fix typo 2019-12-13 18:39:18 -08:00
zoecarver
2434235216 Fix issues with worklist 2019-12-13 18:36:40 -08:00
zoecarver
b81e47b00b Remove double-collect 2019-12-13 17:56:26 -08:00
zoecarver
6a3d32b6d7 Fix use of worklist 2019-12-13 09:33:25 -08:00
zoecarver
808c9faa50 * use worklist instead of vector
* replace dead global collection with global vector
* fix test
2019-12-13 09:28:24 -08:00
zoecarver
578b15e94d Add assertion in collectUsesOfInstructionForDeletion 2019-12-13 08:55:31 -08:00
zoecarver
b65b3174c2 Check global can be used before removing alloc 2019-12-13 08:53:12 -08:00
zoecarver
eb39989f10 Remove debug dump call 2019-12-07 17:22:08 -08:00
zoecarver
97f1b4e28e Check that the only uses of a global addrs are stores 2019-12-07 17:19:46 -08:00
zoecarver
42a7b653ab Add extra safety check before removing globals and add accessors even in the global accessor function 2019-12-07 17:19:07 -08:00
zoecarver
b366e9b5ad Fix invalid global addr being collected 2019-12-07 12:55:00 -08:00
zoecarver
179652ccae stash 2019-12-06 22:02:31 -08:00
zoecarver
c11a9715d4 Remove duplicate test and fix formatting 2019-12-06 11:18:34 -08:00
zoecarver
95afdd0207 Check size of element, not if the element exists 2019-12-06 11:15:53 -08:00
zoecarver
798eea831c add helper function: canBeUsedOrChangedExternally 2019-12-06 10:45:04 -08:00
zoecarver
a8ad156142 Resolve build failure 2019-12-06 10:39:20 -08:00
zoecarver
e713ead777 Rename GlobalAccess to GlobalAccesses 2019-12-05 12:53:29 -08:00
zoecarver
73af502d2d Create list of instructions to delete 2019-12-05 12:51:35 -08:00
zoecarver
e73f027bf0 Merge branch 'master' into optimize/dead-global-vars 2019-12-03 18:25:18 -08:00
zoecarver
e470ed00f1 Merge branch 'master' into optimize/dead-global-vars 2019-12-03 11:58:46 -08:00
Erik Eckstein
db815521d3 GlobalOpt: Prevent calling a non-resilient (or not usable from inline) function from inlinable code.
This problem can show up with cross module optimization.
2019-12-03 14:37:01 +01:00
zoecarver
2070821df7 Add tests 2019-11-20 10:36:13 -08:00
zoecarver
4dcbc42409 Move collection into collect method 2019-11-19 20:56:06 -08:00
zoecarver
0e389cee87 Cleanup 2019-11-19 18:05:23 -08:00
zoecarver
f52a6f3317 Refactor and add tests 2019-11-18 08:59:50 -08:00
zoecarver
9cd11a6b75 Add optimization to remove unused global private variables 2019-11-17 20:17:19 -08:00
Arnold Schwaighofer
f3a5d69672 Adjust to recent change 2019-11-11 14:21:52 -08:00
Arnold Schwaighofer
8aaa7b4dc1 SILOptimizer: Pipe through TypeExpansionContext 2019-11-11 14:21:52 -08:00
Slava Pestov
a86f174445 SILOptimizer: Don't optimize initializers for dynamically-sized globals
If the size of a global variable is not known at compile time, we emit
a fixed size buffer together with initialization code when the global
variable is initialized.

Make sure the SIL optimizer does not convert this into a static
initialization, even if the size of the type is known inside the
module where the global is declared, because we don't have a way to
statically initialize something that looks like a fixed-size buffer
to client code.

Fixes <https://bugs.swift.org/browse/SR-11709>.
2019-11-07 17:16:40 -05: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
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
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