Commit Graph

1501 Commits

Author SHA1 Message Date
Erik Eckstein
32af2dd5a4 Swift SIL: an autogenerated Location needs to keep the debug scope.
Therefore, instead of creating an autogenerated location with a null scope, make an existing location `autoGenerated`.
2022-09-02 08:56:38 +02:00
Erik Eckstein
275861f832 FunctionUses: don't eagerly reserve array capacities in the initializer
Instead do it in `collect`. This allows creating a `FunctionUses` with zero cost - in case `collect` is never called.
2022-09-02 07:11:49 +02:00
Erik Eckstein
9795f4b944 Swift Optimizer: add bridging for command line options 2022-09-02 07:11:49 +02:00
Erik Eckstein
2a2c009532 Swift AccessUtils: add convenient properties to get the AccessBase and AccessScope of a Value 2022-09-02 07:11:49 +02:00
Erik Eckstein
607268371b Swift AccessUtils: add an AccessBase.unidentified case
Now that `AccessBase` is an enum, it makes sense to add an `unidentified` case. This avoids dealing with optional AccessBases in several place.
Clients don't need to make both, an optional check and a switch, but can check for unidentified access bases just in a single switch statement.
2022-09-02 07:11:49 +02:00
Erik Eckstein
082aec0990 Swift SIL: add var FunctionArgument.convention
Also:
* move the `ArgumentConvention` enum from Function.swift to Argument.swift.
* `FunctionArgument.isExclusiveIndirectParameter` -> `ArgumentConvention.isExclusiveIndirect`
* add `ArgumentConvention.isInout`
2022-09-02 07:11:49 +02:00
Erik Eckstein
f7aaa35092 Swift SIL: fix KeyPathInst.visitReferencedFunctions
It crashed for keypath instructions with zero components.
2022-08-26 18:00:11 +02:00
Erik Eckstein
f6e6b2ecf3 Swift SIL: add the possibility to pass a custom location to Builder initializers.
And add the static property `Location.autoGeneratedLocation`
2022-08-26 18:00:11 +02:00
Erik Eckstein
2745565a83 Swift SIL: correctly bridge "invalid" witness table entries.
Originally, I thought that couldn't appear in valid SIL, but there are "no_default" entries in default witness tables, which map to "invalid" entries.
2022-08-26 18:00:11 +02:00
Erik Eckstein
a12e33e9e9 Swift Optimizer: add the FunctionUses utility
Provides a list of instructions, which reference a function.

A function "use" is an instruction in another (or the same) function which references the function.
In most cases those are `function_ref` instructions, but can also be e.g. `keypath` instructions.

'FunctionUses' performs an analysis of all functions in the module and collects instructions which reference other functions.
This utility can be used to do inter-procedural caller-analysis.
2022-08-24 17:55:02 +02:00
Erik Eckstein
2fe1ee5533 Swift SIL: add a few Builder functions
* add `createAllocStack`
* add `createDeallocStack`
* add `createCopyAddr`
* add `@discardableResult` to the existing `createDeallocStackRef`
2022-08-24 17:55:02 +02:00
Erik Eckstein
0e59dde0fa Swift SIL: add the TermInst.isFunctionExiting property 2022-08-24 17:55:02 +02:00
Erik Eckstein
bfb284f827 Swift SIL: add a few instructions and Instruction.visitReferencedFunctions
* add `DynamicFunctionRefInst` and `PreviousDynamicFunctionRefInst`
* add a common base class to all function-referencing instructions: `FunctionRefBaseInst`
* add `KeyPathInst`
* add `IndexAddrInst.base` and `IndexAddrInst.index` getters
* add `Instruction.visitReferencedFunctions` to visit all functions which are referenced by an instruction
2022-08-24 17:55:02 +02:00
Erik Eckstein
90c83be7a6 Swift Optimizer: make the Stack data structure also work with a ModulePassContext 2022-08-24 17:55:02 +02:00
Erik Eckstein
fbb694bcde Swift SIL: add bridging for witness and default witness tables 2022-08-24 17:55:02 +02:00
Erik Eckstein
7816513f0d Swift SIL: add bridging for vTables 2022-08-24 17:55:02 +02:00
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