forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Concurrency] Nest stack traffic in withValue.
Because `_taskLocalValuePush` and `_taskLocalValuePop` can result in calls to `swift_task_alloc` and `swift_task_dealloc` respectively, and because the compiler hasn't been taught about that (e.g. `SILInstruction::isAllocatingStack`, `SILInstruction::isDeallocatingStack`, etc), calling them (push and pop) from a function which makes use the stack for dynamically sized allocations can result in violations of stack discipline of the form ``` swift_task_alloc // allocates %ptr_1 copy_value_witness // copies into %ptr_1 swift_task_localValuePush // calls swift_task_alloc and allocates %ptr_2 swift_task_dealloc // deallocates %ptr_1 swift_task_localValuePop // calls swift_task_dealloc and deallocates %ptr_2 ``` Avoid the problem by not allocating dynamically sized stack space in the function which calls `_taskLocalValuePush` and `_taskLocalValuePop`. Split the calls to those functions into `withValueImpl` function which takes its argument `__owned`. Call that function from `withValue`, ensuring that the necessary copy (to account for the fact that withValue takes its argument `__guaranteed` but `_taskLocalValuePush` takes its `__owned`) and associated stack traffic occur in `withValue`. Still, allow `withValueImpl` to be inlined. The stack nesting will be preserved across it. rdar://107275872
- Loading branch information
1 parent
4e14288
commit 4fe988b
Showing
3 changed files
with
92 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// RUN: %empty-directory(%t) | ||
// RUN: %target-build-swift -O -Xfrontend -disable-availability-checking %s -parse-as-library -module-name main -o %t/main | ||
// RUN: %target-codesign %t/main | ||
// RUN: %target-run %t/main | %FileCheck %s | ||
|
||
// REQUIRES: objc_interop | ||
// REQUIRES: concurrency | ||
// REQUIRES: executable_test | ||
// REQUIRES: concurrency_runtime | ||
|
||
import Foundation | ||
|
||
@main struct M { | ||
@TaskLocal static var v: UUID = UUID() | ||
static func test(_ t: UUID) async { | ||
await Self.$v.withValue(t) { | ||
await Task.sleep(1) | ||
print(Self.$v.get()) | ||
} | ||
} | ||
static func main() async { | ||
// CHECK: before | ||
print("before") | ||
await test(UUID()) | ||
// CHECK: after | ||
print("after") | ||
} | ||
} |