Skip to content

Commit 3fa2091

Browse files
Merge pull request #5171 from swiftwasm/katei/merge-release-5.8-2023-01-11
Merge release/5.8 2023-01-11
2 parents cee4133 + 11dd72a commit 3fa2091

File tree

9 files changed

+157
-39
lines changed

9 files changed

+157
-39
lines changed

lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -283,37 +283,33 @@ void DCE::markLive() {
283283
break;
284284
}
285285
case SILInstructionKind::BeginBorrowInst: {
286-
// Currently we only support borrows of owned values.
287-
// Nested borrow handling can be complex in the presence of reborrows.
288-
// So it is not handled currently.
289286
auto *borrowInst = cast<BeginBorrowInst>(&I);
287+
// Populate guaranteedPhiDependencies for this borrowInst
288+
findGuaranteedPhiDependencies(BorrowedValue(borrowInst));
289+
auto disableBorrowDCE = [&](SILValue borrow) {
290+
visitTransitiveEndBorrows(borrow, [&](EndBorrowInst *endBorrow) {
291+
markInstructionLive(endBorrow);
292+
});
293+
};
294+
// If we have a begin_borrow of a @guaranteed operand, disable DCE'ing
295+
// of parent borrow scopes. Dead reborrows needs complex handling, whuch
296+
// is why it is disabled for now.
290297
if (borrowInst->getOperand()->getOwnershipKind() ==
291298
OwnershipKind::Guaranteed) {
292-
// Visit the end_borrows of all the borrow scopes that this
293-
// begin_borrow could be borrowing.
294299
SmallVector<SILValue, 4> roots;
295300
findGuaranteedReferenceRoots(borrowInst->getOperand(),
296301
/*lookThroughNestedBorrows=*/false,
297302
roots);
303+
// Visit the end_borrows of all the borrow scopes that this
304+
// begin_borrow could be borrowing, and mark them live.
298305
for (auto root : roots) {
299-
visitTransitiveEndBorrows(root,
300-
[&](EndBorrowInst *endBorrow) {
301-
markInstructionLive(endBorrow);
302-
});
306+
disableBorrowDCE(root);
303307
}
304-
continue;
305308
}
306-
// Populate guaranteedPhiDependencies for this borrow
307-
findGuaranteedPhiDependencies(BorrowedValue(borrowInst));
308-
// Don't optimize a borrow scope if it is lexical or has a pointer
309-
// escape.
309+
// If we have a lexical borrow scope or a pointer escape, disable DCE.
310310
if (borrowInst->isLexical() ||
311311
hasPointerEscape(BorrowedValue(borrowInst))) {
312-
// Visit all end_borrows and mark them live
313-
visitTransitiveEndBorrows(borrowInst, [&](EndBorrowInst *endBorrow) {
314-
markInstructionLive(endBorrow);
315-
});
316-
continue;
312+
disableBorrowDCE(borrowInst);
317313
}
318314
break;
319315
}

stdlib/public/BackDeployConcurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/BackDeployConcurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

153158
/// Binds the task-local to the specific value for the duration of the

stdlib/public/Concurrency/TaskCancellation.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public func withTaskCancellationHandler<T>(
4646
// unconditionally add the cancellation record to the task.
4747
// if the task was already cancelled, it will be executed right away.
4848
let record = _taskAddCancellationHandler(handler: handler)
49-
defer { _taskRemoveCancellationHandler(record: record) }
50-
51-
return try await operation()
49+
do {
50+
let result = try await operation()
51+
_taskRemoveCancellationHandler(record: record)
52+
return result
53+
} catch {
54+
_taskRemoveCancellationHandler(record: record)
55+
throw error
56+
}
5257
}
5358

5459
@available(SwiftStdlib 5.1, *)

stdlib/public/Concurrency/TaskLocal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ public final class TaskLocal<Value: Sendable>: Sendable, CustomStringConvertible
145145
_checkIllegalTaskLocalBindingWithinWithTaskGroup(file: file, line: line)
146146

147147
_taskLocalValuePush(key: key, value: valueDuringOperation)
148-
defer { _taskLocalValuePop() }
149-
150-
return try await operation()
148+
do {
149+
let result = try await operation()
150+
_taskLocalValuePop()
151+
return result
152+
} catch {
153+
_taskLocalValuePop()
154+
throw error
155+
}
151156
}
152157

153158
/// Binds the task-local to the specific value for the duration of the

stdlib/public/core/StringGraphemeBreaking.swift

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ extension Unicode.Scalar {
407407
}
408408
}
409409

410-
internal struct _GraphemeBreakingState {
410+
internal struct _GraphemeBreakingState: Sendable, Equatable {
411411
// When we're looking through an indic sequence, one of the requirements is
412412
// that there is at LEAST 1 Virama present between two linking consonants.
413413
// This value helps ensure that when we ultimately need to decide whether or
@@ -436,6 +436,18 @@ internal struct _GraphemeBreakingState {
436436
var shouldBreakRI = false
437437
}
438438

439+
extension _GraphemeBreakingState: CustomStringConvertible {
440+
var description: String {
441+
var r = "["
442+
if hasSeenVirama { r += "V" }
443+
if isInEmojiSequence { r += "E" }
444+
if isInIndicSequence { r += "I" }
445+
if shouldBreakRI { r += "R" }
446+
r += "]"
447+
return r
448+
}
449+
}
450+
439451
extension Unicode {
440452
/// A state machine for recognizing character (i.e., extended grapheme
441453
/// cluster) boundaries in an arbitrary series of Unicode scalars.
@@ -448,7 +460,7 @@ extension Unicode {
448460
/// `String` splits its contents into `Character` values.
449461
@available(SwiftStdlib 5.8, *)
450462
public // SPI(Foundation) FIXME: We need API for this
451-
struct _CharacterRecognizer {
463+
struct _CharacterRecognizer: Sendable {
452464
internal var _previous: Unicode.Scalar
453465
internal var _state: _GraphemeBreakingState
454466

@@ -547,6 +559,21 @@ extension Unicode {
547559
}
548560
}
549561

562+
@available(SwiftStdlib 5.8, *)
563+
extension Unicode._CharacterRecognizer: Equatable {
564+
public static func ==(left: Self, right: Self) -> Bool {
565+
left._previous == right._previous && left._state == right._state
566+
}
567+
}
568+
569+
@available(SwiftStdlib 5.8, *)
570+
extension Unicode._CharacterRecognizer: CustomStringConvertible {
571+
public var description: String {
572+
return "\(_state)U+\(String(_previous.value, radix: 16, uppercase: true))"
573+
}
574+
}
575+
576+
550577
extension _StringGuts {
551578
// Returns the stride of the grapheme cluster starting at offset `index`,
552579
// assuming it is on a grapheme cluster boundary.

test/SILOptimizer/dead_code_elimination_nontrivial_ossa.sil

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,48 @@ bb1(%newborrowi : @guaranteed $Klass, %newborrowo : @guaranteed $Klass):
529529
return %res : $()
530530
}
531531

532+
533+
sil [ossa] @dce_nestedborrowlifetime4 : $@convention(thin) (@guaranteed Wrapper1, @guaranteed Wrapper1) -> () {
534+
bb0(%0 : @guaranteed $Wrapper1, %1 : @guaranteed $Wrapper1):
535+
cond_br undef, bb1, bb2
536+
537+
bb1:
538+
%outer1 = begin_borrow %0 : $Wrapper1
539+
%ex1 = struct_extract %outer1 : $Wrapper1, #Wrapper1.val1
540+
%ex11 = struct_extract %ex1 : $Wrapper2, #Wrapper2.val2
541+
br bb3(%ex11 : $Klass, %outer1 : $Wrapper1)
542+
543+
bb2:
544+
%outer2 = begin_borrow %1 : $Wrapper1
545+
%ex2 = struct_extract %outer2 : $Wrapper1, #Wrapper1.val1
546+
%ex21 = struct_extract %ex2 : $Wrapper2, #Wrapper2.val2
547+
br bb3(%ex21 : $Klass, %outer2 : $Wrapper1)
548+
549+
bb3(%phi1 : @guaranteed $Klass, %phi2 : @guaranteed $Wrapper1):
550+
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
551+
apply %f(%phi1) : $@convention(thin) (@guaranteed Klass) -> ()
552+
end_borrow %phi2 : $Wrapper1
553+
%9999 = tuple()
554+
return %9999 : $()
555+
}
556+
557+
sil [ossa] @dce_nestedborrowlifetime5 : $@convention(thin) (@guaranteed Wrapper1) -> () {
558+
bb0(%0 : @guaranteed $Wrapper1):
559+
%outer1 = begin_borrow %0 : $Wrapper1
560+
%inner1 = begin_borrow %outer1 : $Wrapper1
561+
%ex1 = struct_extract %inner1 : $Wrapper1, #Wrapper1.val1
562+
%ex11 = struct_extract %ex1 : $Wrapper2, #Wrapper2.val2
563+
br bb2(%ex11 : $Klass, %inner1 : $Wrapper1)
564+
565+
bb2(%phi1 : @guaranteed $Klass, %phi2 : @guaranteed $Wrapper1):
566+
%f = function_ref @use_klass : $@convention(thin) (@guaranteed Klass) -> ()
567+
apply %f(%phi1) : $@convention(thin) (@guaranteed Klass) -> ()
568+
end_borrow %phi2 : $Wrapper1
569+
end_borrow %outer1 : $Wrapper1
570+
%9999 = tuple()
571+
return %9999 : $()
572+
}
573+
532574
// CHECK-LABEL: sil [ossa] @infinite_loop :
533575
// CHECK-NOT: copy_value
534576
// CHECK-LABEL: } // end sil function 'infinite_loop'

test/stdlib/CharacterRecognizer.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,36 @@ if #available(SwiftStdlib 5.8, *) {
114114
""")
115115
}
116116
}
117+
118+
if #available(SwiftStdlib 5.8, *) {
119+
suite.test("Equatable") {
120+
var r1 = Unicode._CharacterRecognizer()
121+
var r2 = Unicode._CharacterRecognizer()
122+
expectEqual(r1, r2)
123+
expectTrue(r1.hasBreak(before: "a"))
124+
expectNotEqual(r1, r2)
125+
expectTrue(r2.hasBreak(before: "a"))
126+
expectEqual(r1, r2)
127+
expectTrue(r2.hasBreak(before: "\u{1f44f}")) // CLAPPING HANDS SIGN
128+
expectNotEqual(r1, r2)
129+
expectTrue(r1.hasBreak(before: "b"))
130+
expectNotEqual(r1, r2)
131+
expectFalse(r2.hasBreak(before: "\u{1f3fc}")) // EMOJI MODIFIER FITZPATRICK TYPE-3
132+
expectNotEqual(r1, r2)
133+
expectTrue(r2.hasBreak(before: "b"))
134+
expectEqual(r1, r2) // breaks should reset state
135+
}
136+
}
137+
138+
if #available(SwiftStdlib 5.8, *) {
139+
suite.test("CustomStringConvertible") {
140+
var r = Unicode._CharacterRecognizer()
141+
expectEqual("\(r)", "[]U+0")
142+
expectTrue(r.hasBreak(before: "\u{1F1FA}")) // REGIONAL INDICATOR SYMBOL LETTER U
143+
expectEqual("\(r)", "[]U+1F1FA")
144+
expectFalse(r.hasBreak(before: "\u{1F1F8}")) // REGIONAL INDICATOR SYMBOL LETTER S
145+
expectEqual("\(r)", "[R]U+1F1F8")
146+
expectTrue(r.hasBreak(before: "$"))
147+
expectEqual("\(r)", "[]U+24")
148+
}
149+
}

utils/update_checkout/update-checkout-config.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898
"swift-argument-parser": "1.0.3",
9999
"swift-atomics": "1.0.2",
100100
"swift-collections": "1.0.1",
101-
"swift-crypto": "1.1.5",
101+
"swift-crypto": "2.2.3",
102102
"swift-driver": "main",
103103
"swift-numerics": "1.0.1",
104104
"swift-syntax": "main",
@@ -141,7 +141,7 @@
141141
"swift-argument-parser": "1.0.3",
142142
"swift-atomics": "1.0.2",
143143
"swift-collections": "1.0.1",
144-
"swift-crypto": "1.1.5",
144+
"swift-crypto": "2.2.3",
145145
"swift-driver": "release/5.8",
146146
"swift-numerics": "1.0.1",
147147
"swift-syntax": "release/5.8",
@@ -183,7 +183,7 @@
183183
"swift-argument-parser": "1.0.3",
184184
"swift-atomics": "1.0.2",
185185
"swift-collections": "1.0.1",
186-
"swift-crypto": "1.1.5",
186+
"swift-crypto": "2.2.3",
187187
"swift-driver": "main",
188188
"swift-numerics": "1.0.1",
189189
"swift-syntax": "main",
@@ -225,7 +225,7 @@
225225
"swift-argument-parser": "1.0.3",
226226
"swift-atomics": "1.0.2",
227227
"swift-collections": "1.0.1",
228-
"swift-crypto": "1.1.5",
228+
"swift-crypto": "2.2.3",
229229
"swift-driver": "main",
230230
"swift-numerics": "1.0.1",
231231
"swift-syntax": "main",
@@ -267,7 +267,7 @@
267267
"swift-argument-parser": "1.0.3",
268268
"swift-atomics": "1.0.2",
269269
"swift-collections": "1.0.1",
270-
"swift-crypto": "1.1.5",
270+
"swift-crypto": "2.2.3",
271271
"swift-driver": "release/5.8",
272272
"swift-numerics": "1.0.1",
273273
"swift-syntax": "release/5.8",
@@ -510,7 +510,7 @@
510510
"swift-argument-parser": "1.0.3",
511511
"swift-atomics": "1.0.2",
512512
"swift-collections": "1.0.1",
513-
"swift-crypto": "1.1.5",
513+
"swift-crypto": "2.2.3",
514514
"swift-driver": "main",
515515
"swift-numerics": "1.0.1",
516516
"swift-syntax": "main",

0 commit comments

Comments
 (0)