Skip to content

Commit d5cfafb

Browse files
authored
Merge branch 'main' into guaranteedphis
2 parents ebb4deb + 513b9cb commit d5cfafb

File tree

122 files changed

+2327
-413
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+2327
-413
lines changed

SwiftCompilerSources/Sources/Basic/Utils.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import std
2424
public func assert(_ condition: Bool, _ message: @autoclosure () -> String,
2525
file: StaticString = #fileID, line: UInt = #line) {
2626
if !condition {
27-
print("### basic")
2827
fatalError(message(), file: file, line: line)
2928
}
3029
}

SwiftCompilerSources/Sources/Optimizer/Analysis/CalleeAnalysis.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ import SIL
1616
public struct CalleeAnalysis {
1717
let bridged: BridgedCalleeAnalysis
1818

19+
static func register() {
20+
CalleeAnalysis_register(
21+
// isDeinitBarrierFn:
22+
{ (inst : BridgedInstruction, bca: BridgedCalleeAnalysis) -> Bool in
23+
return inst.instruction.isDeinitBarrier(bca.analysis)
24+
}
25+
)
26+
}
27+
1928
public func getCallees(callee: Value) -> FunctionArray? {
2029
let bridgedFuncs = CalleeAnalysis_getCallees(bridged, callee.bridged)
2130
if bridgedFuncs.incomplete != 0 {
@@ -45,6 +54,33 @@ public struct CalleeAnalysis {
4554
}
4655
}
4756

57+
extension FullApplySite {
58+
fileprivate func isBarrier(_ analysis: CalleeAnalysis) -> Bool {
59+
guard let callees = analysis.getCallees(callee: callee) else {
60+
return true
61+
}
62+
return callees.contains { $0.isDeinitBarrier }
63+
}
64+
}
65+
66+
extension Instruction {
67+
public final func maySynchronize(_ analysis: CalleeAnalysis) -> Bool {
68+
if let site = self as? FullApplySite {
69+
return site.isBarrier(analysis)
70+
}
71+
return maySynchronizeNotConsideringSideEffects
72+
}
73+
74+
/// Whether lifetime ends of lexical values may safely be hoisted over this
75+
/// instruction.
76+
///
77+
/// Deinitialization barriers constrain variable lifetimes. Lexical
78+
/// end_borrow, destroy_value, and destroy_addr cannot be hoisted above them.
79+
public final func isDeinitBarrier(_ analysis: CalleeAnalysis) -> Bool {
80+
return mayAccessPointer || mayLoadWeakOrUnowned || maySynchronize(analysis)
81+
}
82+
}
83+
4884
public struct FunctionArray : RandomAccessCollection, FormattedLikeArray {
4985
fileprivate let bridged: BridgedCalleeList
5086

@@ -55,3 +91,9 @@ public struct FunctionArray : RandomAccessCollection, FormattedLikeArray {
5591
return BridgedFunctionArray_get(bridged, index).function
5692
}
5793
}
94+
// Bridging utilities
95+
96+
extension BridgedCalleeAnalysis {
97+
public var analysis: CalleeAnalysis { .init(bridged: self) }
98+
}
99+

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ComputeSideEffects.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ private struct CollectedEffects {
8888
}
8989

9090
mutating func addInstructionEffects(_ inst: Instruction) {
91+
var checkedIfDeinitBarrier = false
9192
switch inst {
9293
case is CopyValueInst, is RetainValueInst, is StrongRetainInst:
9394
addEffects(.copy, to: inst.operands[0].value, fromInitialPath: SmallProjectionPath(.anyValueFields))
@@ -131,12 +132,14 @@ private struct CollectedEffects {
131132
addDestroyEffects(of: calleeValue)
132133
}
133134
handleApply(apply)
135+
checkedIfDeinitBarrier = true
134136

135137
case let pa as PartialApplyInst:
136138
if pa.canBeAppliedInFunction(context) {
137139
// Only if the created closure can actually be called in the function
138140
// we have to consider side-effects within the closure.
139141
handleApply(pa)
142+
checkedIfDeinitBarrier = true
140143
}
141144

142145
case let fl as FixLifetimeInst:
@@ -178,6 +181,13 @@ private struct CollectedEffects {
178181
globalEffects.allocates = true
179182
}
180183
}
184+
// If we didn't already, check whether the instruction could be a deinit
185+
// barrier. If it's an apply of some sort, that was already done in
186+
// handleApply.
187+
if !checkedIfDeinitBarrier,
188+
inst.mayBeDeinitBarrierNotConsideringSideEffects {
189+
globalEffects.isDeinitBarrier = true
190+
}
181191
}
182192

183193
mutating func addEffectsForEcapingArgument(argument: FunctionArgument) {

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Parse
1717
@_cdecl("initializeSwiftModules")
1818
public func initializeSwiftModules() {
1919
registerSILClasses()
20+
registerSwiftAnalyses()
2021
registerSwiftPasses()
2122
initializeSwiftParseModules()
2223
}
@@ -75,3 +76,7 @@ private func registerSwiftPasses() {
7576
registerPass(rangeDumper, { rangeDumper.run($0) })
7677
registerPass(runUnitTests, { runUnitTests.run($0) })
7778
}
79+
80+
private func registerSwiftAnalyses() {
81+
CalleeAnalysis.register()
82+
}

SwiftCompilerSources/Sources/Optimizer/TestPasses/RunUnitTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import SIL
1414

1515
/// This pass should only be used by sil-opt to run all the unit tests.
1616
///
17-
let runUnitTests = FunctionPass(name: "run-unit-tests", {
18-
(function: Function, context: PassContext) in
17+
let runUnitTests = ModulePass(name: "run-unit-tests", {
18+
(context: ModulePassContext) in
1919

2020
print("--- Run unit tests ---")
2121

SwiftCompilerSources/Sources/Parse/Regex.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private func _RegexLiteralLexingFn(
6666
let diagEngine = DiagnosticEngine(bridged: bridged)
6767
let startLoc = SourceLoc(
6868
locationInFile: error.location.assumingMemoryBound(to: UInt8.self))!
69-
diagEngine.diagnose(startLoc, .regex_literal_parsing_error, error.message)
69+
diagEngine.diagnose(startLoc, .foreign_diagnostic, error.message)
7070
}
7171
return error.completelyErroneous
7272
}
@@ -113,7 +113,7 @@ public func _RegexLiteralParsingFn(
113113
let offset = str.utf8.distance(from: str.startIndex, to: errorLoc)
114114
diagLoc = _diagLoc.advanced(by: offset)
115115
}
116-
diagEngine.diagnose(diagLoc, .regex_literal_parsing_error, error.message)
116+
diagEngine.diagnose(diagLoc, .foreign_diagnostic, error.message)
117117
return true
118118
} catch {
119119
fatalError("Expected CompilerParseError")

SwiftCompilerSources/Sources/SIL/Effects.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,19 +395,29 @@ public struct SideEffects : CustomStringConvertible, NoReflectionChildren {
395395
/// are not observable form the outside and are therefore not considered.
396396
public var allocates: Bool
397397

398+
/// If true, destroys of lexical values may not be hoisted over applies of
399+
/// the function.
400+
///
401+
/// This is true when the function (or a callee, transitively) contains a
402+
/// deinit barrier instruction.
403+
public var isDeinitBarrier: Bool
404+
398405
/// When called with default arguments, it creates an "effect-free" GlobalEffects.
399406
public init(memory: Memory = Memory(read: false, write: false),
400407
ownership: Ownership = Ownership(copy: false, destroy: false),
401-
allocates: Bool = false) {
408+
allocates: Bool = false,
409+
isDeinitBarrier: Bool = false) {
402410
self.memory = memory
403411
self.ownership = ownership
404412
self.allocates = allocates
413+
self.isDeinitBarrier = isDeinitBarrier
405414
}
406415

407416
public mutating func merge(with other: GlobalEffects) {
408417
memory.merge(with: other.memory)
409418
ownership.merge(with: other.ownership)
410419
allocates = allocates || other.allocates
420+
isDeinitBarrier = isDeinitBarrier || other.isDeinitBarrier
411421
}
412422

413423
/// Removes effects, which cannot occur for an `argument` value with a given `convention`.
@@ -444,12 +454,13 @@ public struct SideEffects : CustomStringConvertible, NoReflectionChildren {
444454
}
445455

446456
public static var worstEffects: GlobalEffects {
447-
GlobalEffects(memory: .worstEffects, ownership: .worstEffects, allocates: true)
457+
GlobalEffects(memory: .worstEffects, ownership: .worstEffects, allocates: true, isDeinitBarrier: true)
448458
}
449459

450460
public var description: String {
451461
var res: [String] = [memory.description, ownership.description].filter { !$0.isEmpty }
452462
if allocates { res += ["allocate"] }
463+
if isDeinitBarrier { res += ["deinit_barrier"] }
453464
return res.joined(separator: ",")
454465
}
455466
}
@@ -652,6 +663,7 @@ extension StringParser {
652663
else if consume("copy") { globalEffects.ownership.copy = true }
653664
else if consume("destroy") { globalEffects.ownership.destroy = true }
654665
else if consume("allocate") { globalEffects.allocates = true }
666+
else if consume("deinit_barrier") { globalEffects.isDeinitBarrier = true }
655667
else {
656668
break
657669
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
186186
SILFunction_needsStackProtection(bridged) != 0
187187
}
188188

189+
public var isDeinitBarrier: Bool {
190+
effects.sideEffects?.global.isDeinitBarrier ?? true
191+
}
192+
189193
// Only to be called by PassContext
190194
public func _modifyEffects(_ body: (inout FunctionEffects) -> ()) {
191195
body(&effects)

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,41 @@ public class Instruction : ListNode, CustomStringConvertible, Hashable {
108108
return SILInstruction_hasUnspecifiedSideEffects(bridged)
109109
}
110110

111+
public final var mayAccessPointer: Bool {
112+
return swift_mayAccessPointer(bridged)
113+
}
114+
115+
/// Whether this instruction loads or copies a value whose storage does not
116+
/// increment the stored value's reference count.
117+
public final var mayLoadWeakOrUnowned: Bool {
118+
switch self {
119+
case is LoadWeakInst, is LoadUnownedInst, is StrongCopyUnownedValueInst, is StrongCopyUnmanagedValueInst:
120+
return true
121+
default:
122+
return false
123+
}
124+
}
125+
126+
/// Conservatively, whether this instruction could involve a synchronization
127+
/// point like a memory barrier, lock or syscall.
128+
public final var maySynchronizeNotConsideringSideEffects: Bool {
129+
switch self {
130+
case is FullApplySite, is EndApplyInst, is AbortApplyInst:
131+
return true
132+
default:
133+
return false
134+
}
135+
}
136+
137+
/// Conservatively, whether this instruction could be a barrier to hoisting
138+
/// destroys.
139+
///
140+
/// Does not consider function so effects, so every apply is treated as a
141+
/// barrier.
142+
public final var mayBeDeinitBarrierNotConsideringSideEffects: Bool {
143+
return mayAccessPointer || mayLoadWeakOrUnowned || maySynchronizeNotConsideringSideEffects
144+
}
145+
111146
public func visitReferencedFunctions(_ cl: (Function) -> ()) {
112147
}
113148

@@ -618,6 +653,10 @@ final public class ProjectBoxInst : SingleValueInstruction, UnaryInstruction {
618653

619654
final public class CopyValueInst : SingleValueInstruction, UnaryInstruction {}
620655

656+
final public class StrongCopyUnownedValueInst : SingleValueInstruction, UnaryInstruction {}
657+
658+
final public class StrongCopyUnmanagedValueInst : SingleValueInstruction, UnaryInstruction {}
659+
621660
final public class EndCOWMutationInst : SingleValueInstruction, UnaryInstruction {}
622661

623662
final public

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public func registerSILClasses() {
6060
register(ReleaseValueInst.self)
6161
register(DestroyValueInst.self)
6262
register(DestroyAddrInst.self)
63+
register(StrongCopyUnownedValueInst.self)
64+
register(StrongCopyUnmanagedValueInst.self)
6365
register(InjectEnumAddrInst.self)
6466
register(LoadInst.self)
6567
register(LoadWeakInst.self)

benchmark/scripts/build_script_helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def perform_build(args, swiftbuild_path, config, binary_name, opt_flag):
1616
swiftbuild_path,
1717
"--package-path",
1818
args.package_path,
19-
"--build-path",
19+
"--scratch-path",
2020
inner_build_dir,
2121
"--configuration",
2222
config,

docs/HowToGuides/GettingStarted.md

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ toolchain as a one-off, there are a couple of differences:
1515
- [Cloning the project](#cloning-the-project)
1616
- [Troubleshooting cloning issues](#troubleshooting-cloning-issues)
1717
- [Installing dependencies](#installing-dependencies)
18-
- [macOS](#macOS)
18+
- [macOS](#macos)
1919
- [Linux](#linux)
2020
- [Building the project for the first time](#building-the-project-for-the-first-time)
2121
- [Spot check dependencies](#spot-check-dependencies)
@@ -245,7 +245,7 @@ Phew, that's a lot to digest! Now let's proceed to the actual build itself!
245245
```sh
246246
utils/build-script --skip-build-benchmarks \
247247
--skip-ios --skip-watchos --skip-tvos --swift-darwin-supported-archs "$(uname -m)" \
248-
--sccache --release-debuginfo --swift-disable-dead-stripping --test
248+
--sccache --release-debuginfo --swift-disable-dead-stripping
249249
```
250250
- Via Xcode:
251251
```sh
@@ -254,25 +254,16 @@ Phew, that's a lot to digest! Now let's proceed to the actual build itself!
254254
--sccache --release-debuginfo --swift-disable-dead-stripping \
255255
--xcode
256256
```
257-
**Note:** Building `--xcode` together with `--test` is a common source of issues. So to run
258-
tests is recommended to use `ninja` because is normally more stable.
259257
- Linux (uses Ninja):
260258
```sh
261-
utils/build-script --release-debuginfo --test --skip-early-swift-driver \
259+
utils/build-script --release-debuginfo --skip-early-swift-driver \
262260
--skip-early-swiftsyntax
263261
```
264262
This will create a directory
265263
`swift-project/build/Ninja-RelWithDebInfoAssert`
266264
(with `Xcode` instead of `Ninja` if you used `--xcode`)
267265
containing the Swift compiler and standard library and clang/LLVM build artifacts.
268-
- If the build succeeds: Once the build is complete, the tests will run.
269-
- If the tests are passing: Great! We can go to the next step.
270-
- If some tests are failing:
271-
- Consider [filing a bug report](https://swift.org/contributing/#reporting-bugs).
272-
- Note down which tests are failing as a baseline. This baseline will be
273-
handy later when you run the tests after making a change.
274-
- If the build fails:
275-
See [Troubleshooting build issues](#troubleshooting-build-issues).
266+
If the build fails, see [Troubleshooting build issues](#troubleshooting-build-issues).
276267

277268
If you would like to additionally build the Swift corelibs,
278269
ie swift-corelibs-libdispatch, swift-corelibs-foundation, and swift-corelibs-xctest,
@@ -514,15 +505,14 @@ compiler. Then your changes will not be reflected when the test runs because the
514505
option, but it will lead to a longer feedback loop due to more things getting
515506
rebuilt.
516507

508+
In the rare event that a local test failure happens to be unrelated to your
509+
changes (is not due to stale binaries and reproduces without your changes),
510+
there is a good chance that it has already been caught by our continuous
511+
integration infrastructure, and it may be ignored.
512+
517513
If you want to rerun all the tests, you can either rebuild the whole project
518514
and use `lit.py` without `--filter` or use `run-test` to handle both aspects.
519515

520-
Recall the baseline failures mentioned in
521-
[the build section](#the-actual-build). If your baseline had failing tests, make
522-
sure you compare the failures seen after your changes to the baseline. If some
523-
test failures look totally unrelated to your changes, there is a good chance
524-
that they were already failing as part of the baseline.
525-
526516
For more details on running tests and understanding the various Swift-specific
527517
lit customizations, see [Testing.md](/docs/Testing.md). Also check out the
528518
[lit documentation](https://llvm.org/docs/CommandGuide/lit.html) to understand

docs/SIL.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,10 +2444,10 @@ required.
24442444
Deinit Barriers
24452445
```````````````
24462446

2447-
Deinit barriers (see swift::isDeinitBarrier) are instructions which would be
2448-
affected by the side effects of deinitializers. To maintain the order of
2449-
effects that is visible to the programmer, destroys of lexical values cannot be
2450-
reordered with respect to them. There are three kinds:
2447+
Deinit barriers (see Instruction.isDeinitBarrier(_:)) are instructions which
2448+
would be affected by the side effects of deinitializers. To maintain the order
2449+
of effects that is visible to the programmer, destroys of lexical values cannot
2450+
be reordered with respect to them. There are three kinds:
24512451

24522452
1. synchronization points (locks, memory barriers, syscalls, etc.)
24532453
2. loads of weak or unowned values

include/swift/AST/DiagnosticsParse.def

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ ERROR(forbidden_interpolated_string,none,
9191
ERROR(forbidden_extended_escaping_string,none,
9292
"%0 cannot be an extended escaping string literal", (StringRef))
9393

94-
ERROR(regex_literal_parsing_error,none,
95-
"%0", (StringRef))
96-
9794
//------------------------------------------------------------------------------
9895
// MARK: Lexer diagnostics
9996
//------------------------------------------------------------------------------
@@ -1994,5 +1991,12 @@ ERROR(expected_closure_literal,none,
19941991
ERROR(expected_multiple_closures_block_rbrace,none,
19951992
"expected '}' at the end of a trailing closures block", ())
19961993

1994+
//------------------------------------------------------------------------------
1995+
// MARK: diagnostics emitted by Swift libraries
1996+
//------------------------------------------------------------------------------
1997+
1998+
ERROR(foreign_diagnostic,none,
1999+
"%0", (StringRef))
2000+
19972001
#define UNDEFINE_DIAGNOSTIC_MACROS
19982002
#include "DefineDiagnosticMacros.h"

0 commit comments

Comments
 (0)