Commit Graph

490 Commits

Author SHA1 Message Date
Erik Eckstein
984ae9e4f1 Define our own assert implementation for the swift compiler sources
For two reasons:
* We also like to check for assert failures in release builds. Although this could be achieved with `precondition`, it's easy to forget about it and use `assert` instead.
* We need to see the error message in crashlogs of release builds. This is even not the case for `precondition`.

Also, re-export the "Basic" module in "SIL" so that the new assert implementation is also available in the Optimizer module (where all files import SIL).
2022-09-14 14:16:26 +02:00
Erik Eckstein
8e2e7a73c5 SIL: make argument effects more readable in textual SIL
So far, argument effects were printed in square brackets before the function name, e.g.
```
sil [escapes !%0.**, !%1, %1.c*.v** => %0.v**] @foo : $@convention(thin) (@guaranteed T) -> @out S {
bb0(%0 : $*S, %1 : @guaranteed $T):
...
```

As we are adding more argument effects, this becomes unreadable.
To make it more readable, print the effects after the opening curly brace, and print a separate line for each argument. E.g.
```
sil [ossa] @foo : $@convention(thin) (@guaranteed T) -> @out S {
[%0: noescape **]
[%1: noescape, escape c*.v** => %0.v**]
bb0(%0 : $*S, %1 : @guaranteed $T):
...
```
2022-09-12 09:14:54 +02:00
Erik Eckstein
e1ff0aa235 SmallProjectionPath: allow parsing an empty path
If the string to parse doesn't start with a token which belongs to a projection path, just return an empty path instead of throwing an error.
2022-09-12 09:14:54 +02:00
Erik Eckstein
024ec094d1 Swift SIL: simplify the representation of argument effects
Just refactoring, it's a NFC.
2022-09-09 21:50:10 +02:00
Michael Gottesman
925a211ed8 Merge pull request #60989 from gottesmm/pr-d5933fd70a08a0acd36e29c39312cf34cc50f904
[move-only] Fix a few small issues around mark must check.
2022-09-08 13:19:53 -07:00
Erik Eckstein
0eddce0dca Swift SIL: add BuiltinInst.ID.stackAlloc
And make all the case identifiers lowercase.
2022-09-08 08:42:25 +02:00
Erik Eckstein
97b2354be6 SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins:
* `Builtin.unprotectedAddressOf`
* `Builtin.unprotectedAddressOfBorrow`
2022-09-08 08:42:22 +02:00
Erik Eckstein
fdca208335 SIL: add the SILFunction.needsStackProtection flag
Indicates that stack protectors are inserted into this function to detect stack related buffer overflows.
2022-09-08 08:37:21 +02:00
Michael Gottesman
03986db44f [mem-access] Teach mem-access about mark_must_check. 2022-09-07 18:20:33 -07:00
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
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
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
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
7b9b97fb76 Swift SIL: fix BeginAccessInst.endInstructions 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
306e5b0c9c Swift SIL: make Function hashable 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
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
Egor Zhdan
0e2d438c5b [cxx-interop][SwiftCompilerSources] Use llvm::StringRef instead of BridgedStringRef
rdar://83361000
2022-07-21 16:32:16 +01: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
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
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
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
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
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
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
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
a91d541a26 Swift SIL: add ApplySite.substitutionMap and ApplySite.getArgumentConvention() 2022-05-12 21:48:37 +02:00
Erik Eckstein
1a933fc76e Swift SIL: add Type.getIndexOfEnumCase(withName:)
... to get the enum case index with a given name.
2022-05-12 21:48:37 +02:00
Erik Eckstein
b0003d51da Swift SIL: add an initializer to create an empty SubstitutionMap 2022-05-12 21:48:37 +02:00
Erik Eckstein
7c71f0091e Swift SIL: add UseList.isSingleUse 2022-05-12 21:48:37 +02:00
Erik Eckstein
dd5d5f97c4 Swift SIL: add Function.hasSemanticsAttribute 2022-05-12 21:48:37 +02:00
Erik Eckstein
ad9dafc9bf Swift SIL: add Value.definingBlock
and re-factor `Value.definingInstruction`
2022-05-12 21:48:37 +02:00
Erik Eckstein
3b43da9637 Swift Optimizer: improve ergonomics of Builder and PassContext
* split the PassUtils.swift file into PassContext.swift and Passes.swift
* rework `Builder` bridging allowing more insertion point variations, e.g. inserting at the end of a block.
* add Builder.create functions for more instructions
* add `PassContext.splitBlock`
* move SIL modification functions from PassContext to extensions of the relevant types (e.g. instructions).
* rename `Location.bridgedLocation` -> `Location.bridged`
2022-05-12 21:48:37 +02:00
Erik Eckstein
14b985d87e Swift SIL: support ownership
* add the `Ownership` enum and the `Value.ownership` getter
* add `Function.hasOwnership`
2022-05-12 21:48:37 +02:00