Commit Graph

1235 Commits

Author SHA1 Message Date
Erik Eckstein
87f2f41d51 Swift Optimizer: add infrastructure for module passes.
To add a module pass in `Passes.def` use the new `SWIFT_MODULE_PASS` macro.
On the swift side, create a `ModulePass`.
It’s run function receives a `ModulePassContext`, which provides access to all functions of a module.
But it doesn't provide any APIs to modify functions.
In order to modify a function, a module pass must use `ModulePassContext.transform(function:)`.
2022-08-24 17:55:02 +02:00
Erik Eckstein
7b9b97fb76 Swift SIL: fix BeginAccessInst.endInstructions 2022-08-24 17:54:46 +02:00
Erik Eckstein
de6e539a0a Swift Optimizer: make the set's insert functions return a Bool.
Returning true if the element was not contained in the set before inserting
2022-08-24 17:54:46 +02:00
Erik Eckstein
fe40707d5e Swift SIL: add Function.isPossiblyUsedExternally and Function.isAvailableExternally 2022-08-24 17:54:46 +02:00
Erik Eckstein
5015555737 Swift SIL: add a utility protocol BridgedRandomAccessCollection
It fixes the default reflection for bridged random access collections, which usually have a `bridged` stored property.
Conforming to this protocol displays the "real" children of a bridged random access collection and not just `bridged`.
2022-08-24 17:54:46 +02:00
Erik Eckstein
c2ef10661a Swift Optimizer: move function passes which are only used for unit testing to their own TestPasses directory. 2022-08-24 17:54:46 +02:00
Erik Eckstein
306e5b0c9c Swift SIL: make Function hashable 2022-08-24 17:54:46 +02:00
Erik Eckstein
beebd5a920 PassManager: add a invalidation kind for non-body function data
E.g. used if function effects are changed. This tells the passmanager that something changed, but no SIL-specific analysis have to be invalidated.
2022-08-24 17:54:46 +02:00
Erik Eckstein
3bba7caacc Swift SIL: add var Function.instructions
To be used to iterate over all instructions in a function without the need of two loops - one for blocks and one for instructions.
2022-08-24 17:54:46 +02:00
Erik Eckstein
20a8f450dd Swift AccessUtils: improvements and bug fixes
While I was using the new AccessUtils for a new optimization pass I discovered some areas for improvements. Also I found some bugs.

Changes:

* AccessBase: remove the unhealthy redundancy between `kind` and `baseAddress` types. Now AccessBase is single enum with the relevant base objects/addresses as payloads.

* AccessBase: for `global`, store the `GlobalValue` and not a `global_address` instruction, which is more accurate (because there can be multiple `global_addr`s for a single global variable)

* AccessBase: drop the support for function argument "pointers". The `pointer` is now always a `pointer_to_address` instruction. This also simplifies `PointerIdentification`: either it finds a matching `address_to_pointer` or it bails.

* AccessBase: improve `func isDistinct(from:)`. There are more possibilities to prove that two access bases do not alias.

* AccessBase: replace `var isUniquelyIdentified` with `var hasKnownStorageKind` which is more useful for aliasing checking.

* AccessPath: fix `func isDistinct(from:)`. `SmallProjectionPath.matches` is the wrong way to check if two expression paths may overlap. Instead use the new `SmallProjectionPath.mayOverlap`.

* AccessStoragePathWalker: rename `getAccessStorage` -> `visitAccessStorageRoots` and let it return false if it's not a class/reference AccessBase.

* add tests for `AccessPath.isDistinct(from:)`
2022-08-22 13:22:14 +02:00
Erik Eckstein
05d62293e6 Swift SIL: add NominalFieldsArray.getNameOfField(withIndex:) 2022-08-22 13:22:06 +02:00
Erik Eckstein
3367fdd04d SmallProjectionPath: add mayOverlap(with:)
"Overlapping" means that both paths may project the same field.
2022-08-22 13:22:06 +02:00
Anxhelo Xhebraj
7a20bc3ea6 Swift Optimizer: add AccessUtils
This set of utilities introduce concepts such as `AccessBase`,
`AccessPath` and `AccessStoragePath` useful to analyze memory accesses.
2022-08-12 09:42:13 -07:00
Anxhelo Xhebraj
d6192f4f43 Swift SIL: add ScopedInstruction protocol for instructions like BeginAccess 2022-08-11 10:16:40 -07:00
Anxhelo Xhebraj
462d208d15 Swift SIL: add GlobalVariable equality 2022-08-11 10:16:40 -07:00
Anxhelo Xhebraj
e748f01d90 Swift SIL: add constant checking for ref_element_addr field and global 2022-08-11 10:16:40 -07:00
Anxhelo Xhebraj
303ed781ff Swift SIL: add AccessKind to BeginAccess 2022-08-11 10:16:40 -07:00
Anxhelo Xhebraj
b8b8f1ac88 Swift Optimizer: add SmallProjectionWalkingPaths in WalkUtils
- This protocol with its default implementations simplifies
  writing walkers, requiring only `merge` for conformance
2022-08-11 10:16:12 -07:00
Egor Zhdan
0e2d438c5b [cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
2022-07-21 16:32:16 +01:00
Erik Eckstein
872ded9341 ReleaseDevirtualizer: use the ValueUseDefWalker instead of the manual use-def walking implementation 2022-07-20 13:50:18 +02:00
Erik Eckstein
b52297bb79 Swift SIL: some improvements for WalkUtils and EscapeInfo
* "merge" the `Path` and `State` in WalkUtils into a single `WalkingPath`. This makes it simpler for clients to configure a path and additional state variables. EscapeInfo now defines `EscapePath` which includes the projection path and EscapeInfo's specific state variables.
* Make the `WalkerCache` part of the WalkUtils, so that not all clients have to re-implement it.
* Rename `walkDownResults` -> `walkDownAllResults` and `walkUpOperands` -> `walkUpAllOperands` and make these functions client configurable.
2022-07-20 13:50:18 +02:00
zoecarver
12933bf3b4 Mark a few API uses in Swift Compiler Sources as unsafe. 2022-07-18 20:06:43 -04:00
Egor Zhdan
5a33fc951a Merge pull request #60045 from apple/egorzhdan/scs-diagnostic-engine
[cxx-interop][SwiftCompilerSources] Use `swift::DiagnosticEngine` instead of `BridgedDiagnosticEngine`
2022-07-15 14:55:54 +01:00
Egor Zhdan
e9dedf3c27 [cxx-interop][SwiftCompilerSources] Use swift::DiagnosticEngine instead of BridgedDiagnosticEngine
This also removes `BridgedOptionalDiagnosticEngine`.

rdar://83361087
2022-07-14 11:06:31 +01:00
eeckstein
d128d81ea5 Merge pull request #60035 from eeckstein/silnode-set
SIL: add efficient Set data structures for SILValue and SILInstruction, similar to BasicBlockSet.
2022-07-14 10:56:21 +02:00
Egor Zhdan
53986b6b40 [cxx-interop][SwiftCompilerSources] Use swift::DiagnosticArgument instead of BridgedDiagnosticArgument
This also removes `BridgedDiagnosticArgumentKind` in favor of `swift::DiagnosticArgumentKind`, bringing us one step closer to bridging the entire diagnostic engine via C++ interop.

rdar://83361087
2022-07-13 17:29:21 +01:00
Erik Eckstein
ce5e2fb637 Swift SIL: use InstructionSet (instead of Set<Instruction>) in InstructionRange 2022-07-13 14:27:50 +02:00
Erik Eckstein
49a5b3ebdc Swift SIL: add ValueSet and InstructionSet utilities.
These sets are _much_ more efficient than `Set<Value>` and `Set<Instruction>` because they bridge to the efficient `NodeSet`.
Insertions/deletions are just bit operations.
2022-07-13 14:27:50 +02:00
Erik Eckstein
6760dc420c SIL: add a utility which can manage per-value and per-instruction bitfields and flags efficiently.
It's used to implement `InstructionSet` and `ValueSet`: sets of SILValues and SILInstructions.
Just like `BasicBlockSet` for basic blocks, the set is implemented by setting bits directly in SILNode.
This is super efficient because insertion and deletion to/from the set are basic bit operations.

The cost is an additional word in SILNode. But this is basically negligible: it just adds ~0.7% of memory used for SILInstructions.
In my experiments, I didn't see any relevant changes in memory consumption or compile time.
2022-07-13 14:27:50 +02:00
Egor Zhdan
f63b6d8284 Merge pull request #60018 from apple/egorzhdan/scs-diagnostic-fixit
[cxx-interop][SwiftCompilerSources] Use `DiagnosticInfo::FixIt` instead of `BridgedDiagnosticFixIt`
2022-07-13 10:42:38 +01:00
Egor Zhdan
1ae44e77a5 [cxx-interop][SwiftCompilerSources] Use DiagnosticInfo::FixIt instead of BridgedDiagnosticFixIt
This removes some of the bridging code and replaces it with C++ calls.

rdar://83361087
2022-07-12 16:54:16 +01:00
Erik Eckstein
c871197e1a Swift SIL: fix ApplySite.callerArgIndex
It needs to return nil in case the callee argument is not really applied by the ApplySite.
This fixes a crash in EscapeInfo when handling "escaping-to" effects:
If the callee operand of an apply instruction is a `partial_apply` of a `function_ref`, the callee-analysis looks through the `partial_apply` and we get the referenced function as a callee.
In this case there are less apply arguments than callee arguments, because the `partial_apply` already "applies" some of the callee arguments.

rdar://96830171
2022-07-12 17:53:27 +02:00
Egor Zhdan
7f9e39c22c Merge pull request #59907 from apple/egorzhdan/scs-charsourcerange
[cxx-interop][SwiftCompilerSources] Use `swift::CharSourceRange` instead of `BridgedCharSourceRange`
2022-07-08 10:47:32 +01:00
Anxhelo Xhebraj
03e4e5cdb2 Merge pull request #59729 from Angelogeb/apple/walkutils
Swift Optimizer: add def-use/use-def walker utilities
2022-07-07 08:44:31 -07:00
Egor Zhdan
220d95e3d9 [cxx-interop][SwiftCompilerSources] Use swift::CharSourceRange instead of BridgedCharSourceRange
This removes some of the bridging code and replaces it with C++ calls.

rdar://83361087
2022-07-07 12:00:12 +01:00
Egor Zhdan
38edc7f377 [cxx-interop][SwiftCompilerSources] Remove getCopiedBridgedStringRef and freeBridgedStringRef
`std::string`s can now be passed directly between Swift and C++.

rdar://83361087
2022-07-06 12:57:50 +01:00
Anxhelo Xhebraj
c3ccbde52b Swift Optimizer: update EscapeInfo to use the generic walker utilities
`EscapeInfo` now conforms to the generic protocols defined in `WalkUtils`.
This simplifies the implementation a bit, since trivial instructions are handled
by `WalkUtils` and `EscapeInfo` only has to handle a subset of instructions
inherent to escape information.
Passes using `EscapeInfo` are updated accordingly to become visitors that
customize the `EscapeInfo` walk.
2022-07-05 11:28:49 -07:00
Anxhelo Xhebraj
50a7e25e31 Swift Optimizer: add projection path DefUse/UseDef walkers
Introduces a set of protocols useful to perform def-use and use-def
traversals to find uses and definitions of values.

This logic was originally baked into `EscapeInfo` directly.
Here we extract it into general utilities, namely:
- `ValueDefUseWalker`: visit uses of a value walking down value-value projections/constructions.
- `AddressDefUseWalker`: visit uses of an address walking down addr-addr projections/constructions.
- `ValueUseDefWalker`: visit definitions of a value walking up value-value projections/constructions.
- `AddressUseDefWalker`: visit definitions of an address walking up addr-addr projections/constructions.

These utilities can then be used in other passes or to create
new utilities by composing them. For example to find a definition
passing through both address projections and value extractions,
it's enough to implement a visitor conforming to both
`AddressUseDefWalker` and `ValueUseDefWalker`.
2022-07-05 11:26:13 -07:00
Alexis Laferrière
e7b1434ed2 [SwiftCompilerSources] Add missing import 2022-06-30 12:56:46 -07:00
Egor Zhdan
b8e2f2eacc Merge pull request #58699 from apple/egorzhdan/libswift-sourceloc
[cxx-interop][SwiftCompilerSources] Use `swift::SourceLoc` instead of `BridgedSourceLoc`
2022-06-22 10:54:59 +01:00
Egor Zhdan
f476f6245c [cxx-interop][SwiftCompilerSources] Workaround for std::string crash on arm64 Linux
`std::string` constructor crashes when called from `SILBridging.cpp`, likely due to a miscompile. This is a workaround that fixes the crash.
2022-06-15 15:54:45 +01:00
Egor Zhdan
038a4f0b80 [cxx-interop][SwiftCompilerSources] Use swift::SourceLoc instead of BridgedSourceLoc
C++ interop is now enabled in SwiftCompilerSources, so we can remove some of the C bridging layer and use C++ classes directly from Swift.

rdar://83361087
2022-06-11 00:13:39 +01:00
Egor Zhdan
ffb9ee9422 Revert "Revert "[cxx-interop][SwiftCompilerSources] Fix conversion between std::string and Swift.String""
This reverts commit 9542837a18.
2022-06-09 23:40:51 +01:00
eeckstein
b3d9b873a9 Merge pull request #58888 from eeckstein/objc-string-opt
Optimizer: add the ObjCBridgingOptimization to optimize ObjectiveC bridging operations.
2022-06-09 06:48:36 +02:00
Erik Eckstein
ec3d9dd9c7 Optimizer: add the ObjCBridgingOptimization to optimize ObjectiveC bridging operations.
Removes redundant ObjectiveC <-> Swift bridging calls.
Basically, if a value is bridged from ObjectiveC to Swift an then back to ObjectiveC again, then just re-use the original ObjectiveC value.

Also in this commit: add an additional DCE pass before ownership elimination. It can cleanup dead code which is left behind by the ObjCBridgingOptimization.

rdar://89987440
2022-06-08 22:51:57 +02:00
Erik Eckstein
a17ee435b6 Swift SIL: fix var ApplySite.arguments
In case of type-dependent operands, not all operands are part of the arguments
2022-06-08 22:51:57 +02:00
Egor Zhdan
9542837a18 Revert "[cxx-interop][SwiftCompilerSources] Fix conversion between std::string and Swift.String"
This reverts commit be711e387f.
2022-06-08 14:10:04 +01:00
Rintaro Ishizaki
9f4ce612a2 [SwiftCompilerModules] Link lib_InternalSwiftSyntaxParser to libswift
To use _RegexParser from SwiftSyntax.

* Create 'libswiftCompilerModules_SwiftSyntax.a' which is a subset of
  'libswiftCompilerModules.a'
* Link 'lib_InternalSwiftSyntaxParser' to
  'libswiftCompilerModules_SwiftSyntax.a'
* Factor out swift runtime linking logic in CMake so that dynamic
  libraries can link to Swift runtime, in addition to executables
* Link 'lib_InternalSwiftSyntaxParser' to swift runtime
2022-06-02 12:23:03 -07:00
Egor Zhdan
be711e387f [cxx-interop][SwiftCompilerSources] Fix conversion between std::string and Swift.String
This fixes a dangling pointer issue when creating a `Swift.String` from `std::string`.

Also fixes a warning:
```
warning: variable 's' was never mutated; consider changing to 'let' constant
    var s = SILBasicBlock_debugDescription(bridged)
    ~~~ ^
    let
```

rdar://92963081
rdar://93053488
2022-05-30 20:06:36 +01:00
Erik Eckstein
4011e086f0 Swift optimizer: add Value.makeAvailable and `Value.copy(at:andMakeAvailableIn:)
Utilities to make a value available to be used in another basic block.
Inserts required `copy_value` and `destroy_value` operations in case the destination block is in a different control region than the value.
For example, if the destination block is in a loop while the value is not in that loop, the value has to be copied for each loop iteration.
2022-05-12 21:48:37 +02:00