Skip to content

Commit a17dbc7

Browse files
committed
Enable run-time exclusivity checking in release mode.
This change could impact Swift programs that previously appeared well-behaved, but weren't fully tested in debug mode. Now, when running in release mode, they may trap with the message "error: overlapping accesses...". Recent optimizations have brought performance where I think it needs to be for adoption. More optimizations are planned, and some benchmarks should be further improved, but at this point we're ready to begin receiving bug reports. That will help prioritize the remaining work for Swift 5. Of the 656 public microbenchmarks in the Swift repository, there are still several regressions larger than 10%: TEST OLD NEW DELTA RATIO ClassArrayGetter2 139 1307 +840.3% **0.11x** HashTest 631 1233 +95.4% **0.51x** NopDeinit 21269 32389 +52.3% **0.66x** Hanoi 1478 2166 +46.5% **0.68x** Calculator 127 158 +24.4% **0.80x** Dictionary3OfObjects 391 455 +16.4% **0.86x** CSVParsingAltIndices2 526 604 +14.8% **0.87x** Prims 549 626 +14.0% **0.88x** CSVParsingAlt2 1252 1411 +12.7% **0.89x** Dictionary4OfObjects 206 232 +12.6% **0.89x** ArrayInClass 46 51 +10.9% **0.90x** The common pattern in these benchmarks is to define an array of data as a class property and to repeatedly access that array through the class reference. Each of those class property accesses now incurs a runtime call. Naturally, introducing a runtime call in a loop that otherwise does almost no work incurs substantial overhead. This is similar to the issue caused by automatic reference counting. In some cases, more sophistacated optimization will be able to determine the same object is repeatedly accessed. Furthermore, the overhead of the runtime call itself can be improved. But regardless of how well we optimize, there will always a class of microbenchmarks in which the runtime check has a noticeable impact. As a general guideline, avoid performing class property access within the most performance critical loops, particularly on different objects in each loop iteration. If that isn't possible, it may help if the visibility of those class properties is private or internal.
1 parent 48dff2b commit a17dbc7

14 files changed

+17
-19
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ function(_compile_swift_files
245245
list(APPEND swift_flags "-Xfrontend" "-enable-sil-ownership")
246246
endif()
247247

248-
if(SWIFT_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING AND SWIFTFILE_IS_STDLIB)
249-
list(APPEND swift_flags "-Xfrontend" "-enforce-exclusivity=checked")
248+
if(NOT SWIFT_ENABLE_STDLIBCORE_EXCLUSIVITY_CHECKING AND SWIFTFILE_IS_STDLIB)
249+
list(APPEND swift_flags "-Xfrontend" "-enforce-exclusivity=unchecked")
250250
endif()
251251

252252
if(SWIFT_EMIT_SORTED_SIL_OUTPUT)

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,8 +770,6 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
770770
Opts.VerifyExclusivity
771771
= A->getOption().matches(OPT_enable_verify_exclusivity);
772772
}
773-
if (Opts.shouldOptimize() && !Opts.VerifyExclusivity)
774-
Opts.EnforceExclusivityDynamic = false;
775773
if (const Arg *A = Args.getLastArg(options::OPT_enforce_exclusivity_EQ)) {
776774
parseExclusivityEnforcementOptions(A, Opts, Diags);
777775
}

test/IRGen/class_field_other_module.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %empty-directory(%t)
22

33
// RUN: %target-swift-frontend -emit-module -emit-module-path=%t/other_class.swiftmodule %S/Inputs/other_class.swift
4-
// RUN: %target-swift-frontend -I %t -emit-ir -O %s | %FileCheck %s -DINT=i%target-ptrsize
4+
// RUN: %target-swift-frontend -I %t -emit-ir -O -enforce-exclusivity=unchecked %s | %FileCheck %s -DINT=i%target-ptrsize
55

66
import other_class
77

test/SILOptimizer/array_contentof_opt.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil -enforce-exclusivity=unchecked %s | %FileCheck %s
22
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
33

44
// This is an end-to-end test of the array(contentsOf) -> array(Element) optimization

test/SILOptimizer/bridged_casts_folding.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -O -emit-sil -enforce-exclusivity=unchecked %s | %FileCheck %s
33

44
// REQUIRES: objc_interop
55

test/SILOptimizer/generic_specialization_loops_detection_with_loops.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -O -emit-sil -Xllvm -sil-print-generic-specialization-loops -Xllvm -sil-print-generic-specialization-info %s 2>&1 | %FileCheck --check-prefix=CHECK %s
1+
// RUN: %target-swift-frontend -O -emit-sil -enforce-exclusivity=unchecked -Xllvm -sil-print-generic-specialization-loops -Xllvm -sil-print-generic-specialization-info %s 2>&1 | %FileCheck --check-prefix=CHECK %s
22

33
// Check that the generic specializer does not hang a compiler by
44
// creating and infinite loop of generic specializations.

test/SILOptimizer/globalopt_global_propagation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
2-
// RUN: %target-swift-frontend -O -wmo -emit-sil %s | %FileCheck -check-prefix=CHECK-WMO %s
1+
// RUN: %target-swift-frontend -O -emit-sil -enforce-exclusivity=unchecked %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -O -wmo -emit-sil -enforce-exclusivity=unchecked %s | %FileCheck -check-prefix=CHECK-WMO %s
33

44
// Check that values of internal and private global variables, which are provably assigned only
55
// once, are propagated into their uses and enable further optimizations like constant

test/SILOptimizer/globalopt_let_propagation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -O -emit-sil -primary-file %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -O -emit-sil -enforce-exclusivity=unchecked -primary-file %s | %FileCheck %s
22

33
// Check that values of static let and global let variables are propagated into their uses
44
// and enable further optimizations like constant propagation, simplifications, etc.

test/SILOptimizer/illegal_escaping_address.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil -parse-as-library %s
1+
// RUN: %target-swift-frontend -O -sil-verify-all -emit-sil -enforce-exclusivity=unchecked -parse-as-library %s
22

33
// Check that the compiler does not crash for illegal escaping of an address
44
// of a local variable.

test/SILOptimizer/let_propagation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -primary-file %s -emit-sil -O | %FileCheck %s
1+
// RUN: %target-swift-frontend -primary-file %s -emit-sil -enforce-exclusivity=unchecked -O | %FileCheck %s
22

33
// Check that LoadStoreOpts can handle "let" variables properly.
44
// Such variables should be loaded only once and their loaded values can be reused.

test/SILOptimizer/optionset.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -emit-sil | %FileCheck %s
2-
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -emit-sil | %FileCheck %s
1+
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -O -sil-verify-all -module-name=test -emit-sil -enforce-exclusivity=unchecked | %FileCheck %s
2+
// RUN: %target-swift-frontend -parse-as-library -primary-file %s -Osize -sil-verify-all -module-name=test -emit-sil -enforce-exclusivity=unchecked | %FileCheck %s
33
// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib
44

55
public struct TestOptions: OptionSet {

test/SILOptimizer/outliner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-swift-frontend -Osize -import-objc-header %S/Inputs/Outliner.h %s -emit-sil | %FileCheck %s
2-
// RUN: %target-swift-frontend -Osize -g -import-objc-header %S/Inputs/Outliner.h %s -emit-sil | %FileCheck %s
1+
// RUN: %target-swift-frontend -Osize -import-objc-header %S/Inputs/Outliner.h %s -emit-sil -enforce-exclusivity=unchecked | %FileCheck %s
2+
// RUN: %target-swift-frontend -Osize -g -import-objc-header %S/Inputs/Outliner.h %s -emit-sil -enforce-exclusivity=unchecked | %FileCheck %s
33

44
// REQUIRES: objc_interop
55

test/SILOptimizer/sil_combine_protocol_conf.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend %s -O -wmo -emit-sil -Xllvm -sil-disable-pass=DeadFunctionElimination | %FileCheck %s
1+
// RUN: %target-swift-frontend %s -O -wmo -emit-sil -Xllvm -sil-disable-pass=DeadFunctionElimination -enforce-exclusivity=unchecked | %FileCheck %s
22

33
// case 1: class protocol -- should optimize
44
internal protocol SomeProtocol : class {

test/SILOptimizer/static_arrays.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -primary-file %s -O -sil-verify-all -Xllvm -sil-disable-pass=FunctionSignatureOpts -module-name=test -emit-sil | %FileCheck %s
1+
// RUN: %target-swift-frontend -primary-file %s -O -sil-verify-all -Xllvm -sil-disable-pass=FunctionSignatureOpts -module-name=test -emit-sil -enforce-exclusivity=unchecked | %FileCheck %s
22

33
// Also do an end-to-end test to check all components, including IRGen.
44
// RUN: %empty-directory(%t)

0 commit comments

Comments
 (0)