Commit Graph

225 Commits

Author SHA1 Message Date
Kuba Mracek
ae2e903574 [embedded] Build an initial embedded Swift standard library
This isn't a "complete" port of the standard library for embedded Swift, but
something that should serve as a starting point for further iterations on the
stdlib.

- General CMake logic for building a library as ".swiftmodule only" (ONLY_SWIFTMODULE).
- CMake logic in stdlib/public/core/CMakeLists.txt to start building the embedded stdlib for a handful of hardcoded target triples.
- Lots of annotations throughout the standard library to make types, functions, protocols unavailable in embedded Swift (@_unavailableInEmbedded).
- Mainly this is about stdlib functionality that relies on existentials, type erasure, metatypes, reflection, string interpolations.
- We rely on function body removal of unavailable functions to eliminate the actual problematic SIL code (existentials).
- Many .swift files are not included in the compilation of embedded stdlib at all, to simplify the scope of the annotations.
- EmbeddedStubs.swift is used to stub out (as unavailable and fatalError'd) the missing functionality.
2023-09-16 12:38:46 -07:00
Mishal Shah
475ed80c3e Update the SwiftStdlib 5.7 availability 2022-06-21 15:23:10 -07:00
LucianoAlmeida
09bf4cb6d1 [NFC] Fixing some stdlib unused warnings 2022-02-20 18:18:35 -03:00
Arnold Schwaighofer
9f2b6a4ebb Reuse _ContiguousArrayStorage<AnyObject> metadata for any class or objc generic type
Reduces the number of _ContiguousArrayStorage metadata.

In order to support constant time bridging we do need to set the correct
metadata when we bridge to Objective-C. This is so that the type check
succeeds when bridging back from Objective-C to reuse the storage
instance rather than bridging the elements.

To support dynamically setting the `_ContiguousArrayStorage` element
type i needed to add support for optimizing `alloc_ref_dynamic`
throughout the optimizer.

Possible future improvements:
* Use different metadata such that we can disambiguate native Swift
  classes during destruction -- allowing native release rather then unknown
  release usage.
* Optimize the newly added semantic function
  getContiguousArrayStorageType

rdar://86171143
2022-02-16 07:55:34 -08:00
Alex Martini
1186fc7f13 Remove "iff" from doc comments.
This abbreviation for "if and only if" is confusing to those not coming
from a background in formal mathematics, and is frequently reported as a
type by developers reading the documentation.

This commit also changes doc comments in internal and private members,
which don't become part of the public documentation, because omitting
"iff" everywhere makes it much easier to check for any later changes
that reintroduce it.
2021-10-29 10:16:27 -07:00
Karoy Lorentey
dd40ff2929 [stdlib][NFC] Put stored properties & primary initializers before other members in struct declarations 2021-10-05 22:01:35 -07:00
Erik Eckstein
eb9621fe86 stdlib: add a build-script option --enable-array-cow-checks to enable compilation of COW checks.
And set this option in various presets for buildbots.

Don't enable the checks by default because when linking against the OS library (which does not support COW checking) it will result in unresolved symbol errors.
So far it was handled by an availability checks against 9999 (which was a hack), but this does not work anymore.

Note, all this is only relevant for assert builds of the stdlib.

rdar://83673798
2021-09-30 18:06:52 +02:00
Erik Eckstein
2f2c132e27 stdlib: fix availability for internal COW checks.
Those checks should only be done if linked with a built library, but not when linked with the OS libraries.
2021-06-17 11:30:59 +02:00
Karoy Lorentey
466e26a872 [stdlib] Implement _copyContents on internal Array types
`_copyContents(initializing:)` is a core method of Sequence, and it is used surprisingly often to copy stuff out of sequences. Array’s internal types currently have explicit implementations of it that trap (to prevent a performance bug due to the default iterator-based implementation. This has proved a bad idea, as not all code paths that end up calling `_copyContents` have actually been expunged — so we replaced a performance bug with a catastrophic correctness bug. 😥

Rather than trying to play whack-a-mole with code paths that end up in `_copyContents`, replace the traps with (relatively) efficient implementations, based on the ancient `_copyContents(subRange:initializing)` methods that have already been there all this time.

This resolves https://bugs.swift.org/browse/SR-14663.

I expect specialization will make this fix deploy back to earlier OSes in most (but unfortunately not all) cases.
2021-06-16 13:47:00 -07:00
Mishal Shah
23c3b15f5f Support Xcode 13 beta
* Updating availability versions
* Remove all remaining overlays in stdlib/public/Darwin/*:
   - ObjectiveC
   - Dispatch
   - CoreFoundation
   - CoreGraphics
   - Foundation
2021-06-07 12:04:31 -07:00
Erik Eckstein
6d0bbba156 stdlib: update comments for the array buffer endCowMutation functions. 2020-11-09 15:30:23 +01:00
Erik Eckstein
51c2bd0a1c stdlib: remove a wrong internal check for COW array mutation
Don't complain if the empty array buffer singleton is set to mutable.
This can happen if reserveCapacity is called with a 0-capacity for an empty array.
In this case the resulting array is still the empty array singleton.
For the compiler, it's safe to treat the empty array singleton as "mutable", because there is never anything written to an array with zero capacity:
all mutating array operations do any form of capacity/size > 0 check in addition to the uniqueness check.

Unfortunately the empty array singleton sometimes needs such special handling with is not obvious (not to say hacky).

This wrong check only fired in very specific optimization scenarios, where the inliner and the COW optimizations must play together in a certain way.
I could not find an isolated test case for this problem.

rdar://problem/71107908
2020-11-09 15:29:41 +01:00
Erik Eckstein
66d62ff144 stdlib: fix a back-deployment issue for array COW checks with a stdlib assert-build
And rename COWSanityChecks -> COWChecks.

rdar://problem/68181747
2020-09-21 12:21:11 +02:00
Erik Eckstein
666c5e087c stdlib: enable COW sanity checks with an environment variable
Instead of doing the sanity checks by default (with an assert-build of the stdlib), only do the checks if the environment variable SWIFT_DEBUG_ENABLE_COW_SANITY_CHECKS is set to true.

The checks can give false alarms in case a binary is built against a no-assert stdlib but run with an assert-stdlib.
Therefore only do the checks if it's explicitly enabled at runtime.

rdar://problem/65475776
2020-07-20 11:23:42 +02:00
Erik Eckstein
bc27356d73 stdlib: enable runtime checking for COW support by default in assert builds.
This was blocked by an LLDB problem, which is now fixed (https://github.com/apple/llvm-project/pull/1333)
2020-07-03 15:56:50 +02:00
Erik Eckstein
f0711367b5 stdlib: temporarily disable COW support runtime checks.
Some lldb tests are failing with that.
2020-06-08 20:27:37 +02:00
Erik Eckstein
71a642e51b stdlib, SIL optimizer: use the SIL copy-on-write representation in the Array types.
Use the new builtins for COW representation in Array, ContiguousArray and ArraySlice.
The basic idea is to strictly separate code which mutates an array buffer from code which reads from an array.
The concept is explained in more detail in docs/SIL.rst, section "Copy-on-Write Representation".

The main change is to use beginCOWMutation() instead of isUniquelyReferenced() and insert endCOWMutation() at the end of all mutating functions. Also, reading from the array buffer must be done differently, depending on if the buffer is in a mutable or immutable state.

All the required invariants are enforced by runtime checks - but only in an assert-build of the library: a bit in the buffer object side-table indicates if the buffer is mutable or not.

Along with the library changes, also two optimizations needed to be updated: COWArrayOpt and ObjectOutliner.
2020-06-08 15:02:22 +02:00
Erik Eckstein
68728dcb7d stdlib: move the new-buffer creation function from Array to ArrayBuffer
This has two advantages:
1. It does not force the Array in memory (to pass it as inout self to the non-inlinable _createNewBuffer).
2. The new _consumeAndCreateNew is annotated to consume self. This helps to reduce unnecessary retains/releases.

The change applies for Array and ContiguousArray.
2020-05-29 08:46:54 +02:00
Ben Rimmington
ff264da966 [stdlib] Remove unneeded numericCasts 2020-05-08 06:03:49 +01:00
3405691582
7830028f55 [stdlib] Avoid malloc_size on OpenBSD.
malloc introspection is a platform feature that is unavailable on
OpenBSD. There is no workaround for the feature, so we have to assume
that allocations succeed in allocating exactly the amount of memory
requested, and nothing more.

Here a new mallocSize shim is introduced so the feature check for malloc
introspection is pushed to the shims, rather than using os checks
directly from Swift. Not every use of malloc_size has been converted
yet; ManagedBuffer.swift still remains. However, this module requires
special care to fix, which will be done separately.
2020-04-27 15:52:37 -04:00
David Smith
637af2f9fe Merge pull request #27709 from Catfish-Man/bridge-to-the-future
Optimize accessing Swift arrays via ObjC
2019-10-23 00:19:06 -07:00
David Smith
d9cef0afef Optimize accessing Swift Arrays from ObjC 2019-10-22 20:57:36 -07:00
Julian Lettner
381dcc24fc [TSan] Do not report benign race on _swiftEmptyArrayStorage
A previous commit [1] identified and fixed a benign race on
`_swiftEmptyArrayStorage`.  Unfortunately, we have some code duplication
and the fix was not applied to all the necessary places.  Essentially,
we need to ensure that the "empty array storage" object that backs many
"array like types" is never written to.

I tried to improve the test to capture this, however, it is very
finicky.  Currently, it does not go red on my system even when I remove
the check to avoid the benign race in Release mode.

Relevant classes: Array, ArraySlice, ContiguousArray, ArrayBuffer,
ContiguousArrayBuffer, SliceBuffer.

[1] b9b4c789f3

rdar://55161564
2019-10-21 11:22:25 -07:00
David Smith
1efa946caf Remove stdlib and runtime dependencies on Foundation and CF 2019-09-12 18:03:14 -07:00
Arnold Schwaighofer
fe69a86929 Revert "Remove stdlib and runtime dependencies on Foundation and CF"
It is causing bots to fail.

* Revert "The __has_include(<os/system_version.h>) branch here wasn't quite right, we'll just use the dlsym one for now"

This reverts commit f824922456.

* Revert "Remove stdlib and runtime dependencies on Foundation and CF"

This reverts commit 3fe46e3f16.

rdar://54709269
2019-08-26 13:00:08 -07:00
David Smith
3fe46e3f16 Remove stdlib and runtime dependencies on Foundation and CF 2019-08-23 14:10:23 -07:00
Paul Hudson
06f82a53b5 Replaced the majority of ' : ' with ': '. 2019-07-18 20:46:07 +01:00
Karoy Lorentey
d186e9ca91 [stdlib] Simplify __SwiftNativeNSArrayWithContiguousStorage.count 2019-07-12 16:43:03 -07:00
Ben Cohen
e9d4687e31 De-underscore @frozen, apply it to structs (#24185)
* De-underscore @frozen for enums

* Add @frozen for structs, deprecate @_fixed_layout for them

* Switch usage from _fixed_layout to frozen
2019-05-30 17:55:37 -07:00
Andrew Trick
6d3397337d Fix performance of array initialization from a generic sequence.
This fixes a major perform bug involving array initialization from any
contiguously stored collection.	 This is not a recent regression. This fix
results in a 10,000X speedup (that's 4 zeros) for this code path:

func initializeFromSlice(_ a: [Int]) -> [Int] {
  return Array<Int>(a[...])
}

A benchmark is included.
2019-02-11 19:41:45 -08:00
Ben Cohen
1673c12d78 [stdlib] Replace "sanityCheck" with "internalInvariant" (#20616)
* Replace "sanityCheck" with "internalInvariant"
2018-11-15 20:50:22 -08:00
Karoy Lorentey
ee17e975ca [stdlib] Make protocol _NSArrayCore internal
These shadow protocols don’t add any real type safety, and exposing them in the ABI seems unnecessary.
2018-11-15 09:54:25 +00:00
Jordan Rose
e7dd1c17b2 Merge pull request #20384 from jrose-apple/witless
Require @usableFromInline on associated type witnesses
2018-11-08 16:36:48 -08:00
Jordan Rose
65fe556c1a [stdlib] Mark associated type witnesses as @usableFromInline
Inlinable code is permitted to rely on these associated types, so we
need to make sure their declarations are printed in the parseable
interface.

Part of rdar://problem/43824052
2018-11-06 17:00:46 -08:00
Lance Parker
90fd4ba4d7 All the @inlinables in ContigArray are good 2018-11-06 14:38:09 -08:00
Karoy Lorentey
ad8276f14c [stdlib] Array: Remove some deferred bridging details from ABI
- __SwiftNativeNSArrayWithContiguousStorage.withUnsafeBufferOfObjects
- __ContiguousArrayStorageBase._getNonVerbatimBridgingBuffer()
- __ContiguousArrayStorageBase._getNonVerbatimBridgedCount()
- Most __SwiftDeferredNSArray members
2018-10-26 23:53:34 +01:00
Karoy Lorentey
a8f29a3ce5 [stdlib] Simplify _HeapBuffer; limit its scope and rename to _BridgingBuffer
- HeapBuffer was at the wrong abstraction level, for no good reason. We have ManagedBuffer for the general case; we don’t need a slightly less general variant of it.
- Current usages of HeapBuffer are hyper specific: they all are flat buffers of AnyObjects, facilitating bridging. It makes sense to dedicate _HeapBuffer for this single usecase.
- Introduce a dedicate ManagedBuffer subclass for bridging buffers. This will make it slightly easier to recognize these in heap dumps.
- Inlinability audit.
2018-10-26 23:53:06 +01:00
Mike Ash
f4db1dd7a4 Merge pull request #19614 from mikeash/no-internal-export
[Stdlib] Change SWIFT_RUNTIME_STDLIB_INTERNAL to not export the symbol.
2018-10-05 09:26:03 -04:00
Ben Cohen
9ce2143335 Additional ownership annotations 2018-10-03 19:10:22 -07:00
Mike Ash
e18e03171f [Stdlib] Change SWIFT_RUNTIME_STDLIB_INTERNAL to not export the symbol.
The functions in LibcShims are used externally, some directly and some through @inlineable functions. These are changed to SWIFT_RUNTIME_STDLIB_SPI to better match their actual usage. Their names are also changed to add "_swift" to the front to match our naming conventions.

Three functions from SwiftObject.mm are changed to SPI and get a _swift prefix.

A few other support functions are also changed to SPI. They already had a prefix and look like they were meant to be SPI anyway. It was just hard to notice any mixup when they were #defined to the same thing.

rdar://problem/35863717
2018-10-03 09:55:33 -04:00
Mike Ash
317451c8ee [Runtime][Stdlib][Overlays] Add comments to all the renamed classes explaining the rename and noting that the old name cannot be used due to conflicts.
rdar://problem/35768222
2018-09-20 12:07:30 -04:00
Mike Ash
798edb9d0e [Runtime][Stdlib][Overlays] Rename various Objective-C classes and methods that would conflict when loading old Swift libraries into a process alongside ABI-stable libraries.
rdar://problem/35768222
2018-09-13 16:55:10 -04:00
Erik Eckstein
6ba45473df Remove the pinning addressors
It was used for Array + related types.
With exclusivity checking the pinned addressors are not useful anymore.

rdar://problem/35401528
2018-08-23 12:47:56 -07:00
Slava Pestov
89e48ff2e8 stdlib: change a var to a let to squash a warning 2018-05-12 01:48:29 -07:00
Erik Eckstein
b519d1d873 stdlib: Speed up UTF8View -> Array conversion by using _copyContents 2018-05-08 13:52:05 -07:00
Slava Pestov
2e5aef9c8d stdlib: Remove redundant @usableFromInline attributes 2018-04-06 00:02:30 -07:00
Slava Pestov
e1f50b2d36 SE-0193: Rename @_inlineable to @inlinable, @_versioned to @usableFromInline 2018-03-30 21:55:30 -07:00
Lance Parker
fef19d44ee Fix a data race with _swiftEmptyArrayStorage's count 2018-02-21 15:43:27 -08:00
Ben Cohen
9ee856f386 [stdlib][WIP] Eliminate (Closed)CountableRange using conditional conformance (#13342)
* Make Range conditionally a Collection

* Convert ClosedRange to conditionally a collection

* De-gyb Range/ClosedRange, refactoring some methods.

* Remove use of Countable{Closed}Range from stdlib

* Remove Countable use from Foundation

* Fix test errors and warnings resulting from Range/CountableRange collapse

* fix prespecialize test for new mangling

* Update CoreAudio use of CountableRange

* Update SwiftSyntax use of CountableRange

* Restore ClosedRange.Index: Hashable conformance

* Move fixed typechecker slowness test for array-of-ranges from slow to fast, yay

* Apply Doug's patch to loosen test to just check for error
2018-02-01 20:59:28 -08:00
Slava Pestov
834a121b10 AST: @objc_non_lazy_realization should be underscored and UserInaccessible 2018-01-21 01:31:44 -08:00