Skip to content

Commit 2135f24

Browse files
authored
Merge pull request swiftlang#39520 from eeckstein/fix-cow-checks
stdlib: add a build-script option --enable-array-cow-checks to enable compilation of COW checks.
2 parents b9586b9 + eb9621f commit 2135f24

File tree

11 files changed

+59
-55
lines changed

11 files changed

+59
-55
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@ option(SWIFT_CHECK_INCREMENTAL_COMPILATION
397397
"Check if incremental compilation works when compiling the Swift libraries"
398398
FALSE)
399399

400+
option(SWIFT_ENABLE_ARRAY_COW_CHECKS
401+
"Compile the stdlib with Array COW checks enabled (only relevant for assert builds)"
402+
FALSE)
403+
400404
option(SWIFT_REPORT_STATISTICS
401405
"Create json files which contain internal compilation statistics"
402406
FALSE)

stdlib/public/core/ArrayBuffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ extension _ArrayBuffer {
128128
} else {
129129
isUnique = _isNative
130130
}
131-
#if INTERNAL_CHECKS_ENABLED
131+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
132132
if isUnique {
133133
_native.isImmutable = false
134134
}
@@ -145,7 +145,7 @@ extension _ArrayBuffer {
145145
@_alwaysEmitIntoClient
146146
@inline(__always)
147147
internal mutating func endCOWMutation() {
148-
#if INTERNAL_CHECKS_ENABLED
148+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
149149
_native.isImmutable = true
150150
#endif
151151
_storage.endCOWMutation()

stdlib/public/core/Builtin.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,11 @@ internal func _class_getInstancePositiveExtentSize(_ theClass: AnyClass) -> Int
345345
#endif
346346
}
347347

348-
#if INTERNAL_CHECKS_ENABLED
349-
// "9999" means: enable if linked with a built library, but not when linked with
350-
// the OS libraries.
351-
// Note: this must not be changed to a "real" OS version.
352-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
348+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
353349
@usableFromInline
354350
@_silgen_name("_swift_isImmutableCOWBuffer")
355351
internal func _swift_isImmutableCOWBuffer(_ object: AnyObject) -> Bool
356352

357-
// "9999" means: enable if linked with a built library, but not when linked with
358-
// the OS libraries.
359-
// Note: this must not be changed to a "real" OS version.
360-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
361353
@usableFromInline
362354
@_silgen_name("_swift_setImmutableCOWBuffer")
363355
internal func _swift_setImmutableCOWBuffer(_ object: AnyObject, _ immutable: Bool) -> Bool

stdlib/public/core/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@ endif()
283283
if(SWIFT_STDLIB_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING)
284284
list(APPEND swift_stdlib_compile_flags "-enforce-exclusivity=checked")
285285
endif()
286+
if(SWIFT_ENABLE_ARRAY_COW_CHECKS)
287+
list(APPEND swift_stdlib_compile_flags "-DCOW_CHECKS_ENABLED")
288+
endif()
286289

287290
# STAGING: Temporarily avoids having to write #fileID in Swift.swiftinterface.
288291
list(APPEND swift_stdlib_compile_flags "-Xfrontend" "-enable-experimental-concise-pound-file")

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@
1212

1313
import SwiftShims
1414

15-
#if INTERNAL_CHECKS_ENABLED
16-
// "9999" means: enable if linked with a built library, but not when linked with
17-
// the OS libraries.
18-
// Note: this must not be changed to a "real" OS version.
19-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, *)
15+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
2016
@_silgen_name("swift_COWChecksEnabled")
2117
public func _COWChecksEnabled() -> Bool
2218
#endif
@@ -428,7 +424,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
428424
@_alwaysEmitIntoClient
429425
@inline(__always)
430426
internal var immutableStorage : __ContiguousArrayStorageBase {
431-
#if INTERNAL_CHECKS_ENABLED
427+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
432428
_internalInvariant(isImmutable, "Array storage is not immutable")
433429
#endif
434430
return Builtin.COWBufferForReading(_storage)
@@ -440,7 +436,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
440436
@_alwaysEmitIntoClient
441437
@inline(__always)
442438
internal var mutableStorage : __ContiguousArrayStorageBase {
443-
#if INTERNAL_CHECKS_ENABLED
439+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
444440
_internalInvariant(isMutable, "Array storage is immutable")
445441
#endif
446442
return _storage
@@ -452,44 +448,34 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
452448
@_alwaysEmitIntoClient
453449
@inline(__always)
454450
internal var mutableOrEmptyStorage : __ContiguousArrayStorageBase {
455-
#if INTERNAL_CHECKS_ENABLED
451+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
456452
_internalInvariant(isMutable || _storage.countAndCapacity.capacity == 0,
457453
"Array storage is immutable and not empty")
458454
#endif
459455
return _storage
460456
}
461457

462-
#if INTERNAL_CHECKS_ENABLED
458+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
463459
@_alwaysEmitIntoClient
464460
internal var isImmutable: Bool {
465461
get {
466-
// "9999" means: enable if linked with a built library, but not when
467-
// linked with the OS libraries.
468-
// Note: this must not be changed to a "real" OS version.
469-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
470-
if (_COWChecksEnabled()) {
471-
return capacity == 0 || _swift_isImmutableCOWBuffer(_storage)
472-
}
462+
if (_COWChecksEnabled()) {
463+
return capacity == 0 || _swift_isImmutableCOWBuffer(_storage)
473464
}
474465
return true
475466
}
476467
nonmutating set {
477-
// "9999" means: enable if linked with a built library, but not when
478-
// linked with the OS libraries.
479-
// Note: this must not be changed to a "real" OS version.
480-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
481-
if (_COWChecksEnabled()) {
482-
// Make sure to not modify the empty array singleton (which has a
483-
// capacity of 0).
484-
if capacity > 0 {
485-
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, newValue)
486-
if newValue {
487-
_internalInvariant(!wasImmutable,
488-
"re-setting immutable array buffer to immutable")
489-
} else {
490-
_internalInvariant(wasImmutable,
491-
"re-setting mutable array buffer to mutable")
492-
}
468+
if (_COWChecksEnabled()) {
469+
// Make sure to not modify the empty array singleton (which has a
470+
// capacity of 0).
471+
if capacity > 0 {
472+
let wasImmutable = _swift_setImmutableCOWBuffer(_storage, newValue)
473+
if newValue {
474+
_internalInvariant(!wasImmutable,
475+
"re-setting immutable array buffer to immutable")
476+
} else {
477+
_internalInvariant(wasImmutable,
478+
"re-setting mutable array buffer to mutable")
493479
}
494480
}
495481
}
@@ -498,13 +484,8 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
498484

499485
@_alwaysEmitIntoClient
500486
internal var isMutable: Bool {
501-
// "9999" means: enable if linked with a built library, but not when
502-
// linked with the OS libraries.
503-
// Note: this must not be changed to a "real" OS version.
504-
if #available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) {
505-
if (_COWChecksEnabled()) {
506-
return !_swift_isImmutableCOWBuffer(_storage)
507-
}
487+
if (_COWChecksEnabled()) {
488+
return !_swift_isImmutableCOWBuffer(_storage)
508489
}
509490
return true
510491
}
@@ -704,7 +685,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
704685
@_alwaysEmitIntoClient
705686
internal mutating func beginCOWMutation() -> Bool {
706687
if Bool(Builtin.beginCOWMutation(&_storage)) {
707-
#if INTERNAL_CHECKS_ENABLED
688+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
708689
isImmutable = false
709690
#endif
710691
return true
@@ -721,7 +702,7 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
721702
@_alwaysEmitIntoClient
722703
@inline(__always)
723704
internal mutating func endCOWMutation() {
724-
#if INTERNAL_CHECKS_ENABLED
705+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
725706
isImmutable = true
726707
#endif
727708
Builtin.endCOWMutation(&_storage)

stdlib/public/core/SliceBuffer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ internal struct _SliceBuffer<Element>
325325
return false
326326
}
327327
if Bool(Builtin.beginCOWMutation(&owner)) {
328-
#if INTERNAL_CHECKS_ENABLED
328+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
329329
nativeBuffer.isImmutable = false
330330
#endif
331331
return true
@@ -342,7 +342,7 @@ internal struct _SliceBuffer<Element>
342342
@_alwaysEmitIntoClient
343343
@inline(__always)
344344
internal mutating func endCOWMutation() {
345-
#if INTERNAL_CHECKS_ENABLED
345+
#if INTERNAL_CHECKS_ENABLED && COW_CHECKS_ENABLED
346346
nativeBuffer.isImmutable = true
347347
#endif
348348
Builtin.endCOWMutation(&owner)

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ if (SWIFT_TEST_USE_LEAKS)
159159
list(APPEND SWIFT_LIT_ARGS "--param" "leaks-all")
160160
endif()
161161

162+
if (SWIFT_ENABLE_ARRAY_COW_CHECKS)
163+
list(APPEND SWIFT_LIT_ARGS
164+
"--param" "array_cow_checks")
165+
endif()
166+
162167
if(NOT CMAKE_CFG_INTDIR STREQUAL ".")
163168
list(APPEND SWIFT_LIT_ARGS
164169
"--param" "build_mode=${CMAKE_CFG_INTDIR}")

test/api-digester/stability-stdlib-abi-with-asserts.test

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
// Thank you for your help ensuring the stdlib remains compatible with its past!
3939
// -- Your friendly stdlib engineers
4040

41-
// REQUIRES: swift_stdlib_asserts
41+
// REQUIRES: swift_stdlib_asserts, array_cow_checks
4242

4343
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)
4444

@@ -60,5 +60,8 @@ Protocol _RuntimeFunctionCountersStats is a new API without @available attribute
6060
Struct _GlobalRuntimeFunctionCountersState is a new API without @available attribute
6161
Struct _ObjectRuntimeFunctionCountersState is a new API without @available attribute
6262
Struct _RuntimeFunctionCounters is a new API without @available attribute
63+
Func _COWChecksEnabled() is a new API without @available attribute
64+
Func _swift_isImmutableCOWBuffer(_:) is a new API without @available attribute
65+
Func _swift_setImmutableCOWBuffer(_:_:) is a new API without @available attribute
6366

6467
// *** DO NOT DISABLE OR XFAIL THIS TEST. *** (See comment above.)

test/lit.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ native_swift_tools_path = lit_config.params.get('native_swift_tools_path')
264264
if native_swift_tools_path is not None:
265265
append_to_env_path(native_swift_tools_path)
266266

267+
array_cow_checks = lit_config.params.get('array_cow_checks')
268+
if array_cow_checks is not None:
269+
config.available_features.add('array_cow_checks')
270+
267271
if lit_config.params.get('libswift', None) is not None:
268272
config.available_features.add('libswift')
269273

utils/build-presets.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ test-optimize-for-size
9494
swift-stdlib-build-type=RelWithDebInfo
9595
swift-stdlib-enable-assertions=true
9696

97+
enable-array-cow-checks
98+
9799
# This is a release non-incremental build. Run sil-verify-all.
98100
sil-verify-all
99101

@@ -111,6 +113,8 @@ test-optimized
111113
swift-stdlib-build-type=RelWithDebInfo
112114
swift-stdlib-enable-assertions=true
113115

116+
enable-array-cow-checks
117+
114118
# This is a release non-incremental build. Run sil-verify-all.
115119
sil-verify-all
116120

@@ -153,6 +157,8 @@ assertions
153157
swift-stdlib-build-type=Debug
154158
swift-stdlib-enable-assertions=true
155159

160+
enable-array-cow-checks
161+
156162

157163
[preset: buildbot,tools=RA,stdlib=DA]
158164
mixin-preset=
@@ -328,6 +334,8 @@ build-subdir=buildbot_incremental
328334
release
329335
assertions
330336

337+
enable-array-cow-checks
338+
331339
build-swift-stdlib-unittest-extra
332340

333341
libcxx
@@ -1056,6 +1064,8 @@ installable-package=%(installable_package)s
10561064
assertions
10571065
release
10581066

1067+
enable-array-cow-checks
1068+
10591069
verbose-build
10601070

10611071
test

0 commit comments

Comments
 (0)