Skip to content

Commit aff4e79

Browse files
Merge pull request #59726 from nate-chandler/freestanding/task-to-thread-model
[Freestanding] Disable unstructured tasks.
2 parents 99ee194 + 34c08b8 commit aff4e79

40 files changed

+1244
-43
lines changed

include/swift/ABI/MetadataValues.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,6 +2360,8 @@ enum class TaskOptionRecordKind : uint8_t {
23602360
AsyncLet = 2,
23612361
/// Request a child task for an 'async let'.
23622362
AsyncLetWithBuffer = 3,
2363+
/// Request a child task for swift_task_run_inline.
2364+
RunInline = UINT8_MAX,
23632365
};
23642366

23652367
/// Flags for cancellation records.

include/swift/ABI/TaskOptions.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,24 @@ class AsyncLetWithBufferTaskOptionRecord : public TaskOptionRecord {
141141
}
142142
};
143143

144+
class RunInlineTaskOptionRecord : public TaskOptionRecord {
145+
void *allocation;
146+
size_t allocationBytes;
147+
148+
public:
149+
RunInlineTaskOptionRecord(void *allocation, size_t allocationBytes)
150+
: TaskOptionRecord(TaskOptionRecordKind::RunInline),
151+
allocation(allocation), allocationBytes(allocationBytes) {}
152+
153+
void *getAllocation() const { return allocation; }
154+
155+
size_t getAllocationBytes() const { return allocationBytes; }
156+
157+
static bool classof(const TaskOptionRecord *record) {
158+
return record->getKind() == TaskOptionRecordKind::RunInline;
159+
}
160+
};
161+
144162
} // end namespace swift
145163

146164
#endif

include/swift/AST/ASTSynthesis.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,13 @@ enum FunctionRepresentationSynthesizer {
316316
};
317317
template <class S> struct ThrowsSynthesizer { S sub; };
318318
template <class S> struct AsyncSynthesizer { S sub; };
319+
template <class S> struct NoescapeSynthesizer { S sub; };
319320
template <class S>
320321
constexpr ThrowsSynthesizer<S> _throws(S sub) { return {sub}; }
321322
template <class S>
322323
constexpr AsyncSynthesizer<S> _async(S sub) { return {sub}; }
324+
template <class S>
325+
constexpr NoescapeSynthesizer<S> _noescape(S sub) { return {sub}; }
323326

324327
inline ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
325328
FunctionRepresentationSynthesizer kind) {
@@ -340,6 +343,11 @@ ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
340343
const AsyncSynthesizer<S> &s) {
341344
return synthesizeExtInfo(SC, s.sub).withAsync();
342345
}
346+
template <class S>
347+
ASTExtInfo synthesizeExtInfo(SynthesisContext &SC,
348+
const NoescapeSynthesizer<S> &s) {
349+
return synthesizeExtInfo(SC, s.sub).withNoEscape();
350+
}
343351

344352
/// Synthesize a function type.
345353
template <class ExtInfoS, class ResultS, class ParamsS>

include/swift/AST/Builtins.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,16 @@ BUILTIN_MISC_OPERATION(StartAsyncLet, "startAsyncLet", "", Special)
894894
/// destruction.
895895
BUILTIN_MISC_OPERATION(StartAsyncLetWithLocalBuffer, "startAsyncLetWithLocalBuffer", "", Special)
896896

897+
/// taskRunInline()<T>: (
898+
/// () async -> T
899+
/// ) -> T
900+
///
901+
/// Create an async context inline in the current synchronous context and run
902+
/// the specified closure.
903+
///
904+
/// This is only supported under the task-to-thread concurrency model.
905+
BUILTIN_MISC_OPERATION(TaskRunInline, "taskRunInline", "", Special)
906+
897907
/// endAsyncLet(): (Builtin.RawPointer) -> Void
898908
///
899909
/// DEPRECATED. The swift_asyncLet_finish intrinsic and endAsyncLetLifetime

include/swift/AST/DiagnosticsSema.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6380,5 +6380,17 @@ NOTE(candidate_result_requires_explicit_coercion,none,
63806380
"loss of generic requirements",
63816381
(Type))
63826382

6383+
ERROR(concurrency_task_to_thread_model_custom_executor,none,
6384+
"custom executors are not permitted within %0", (StringRef))
6385+
ERROR(concurrency_task_to_thread_model_async_main,none,
6386+
"async main functions are not permitted within %0", (StringRef))
6387+
ERROR(concurrency_task_to_thread_model_main_actor,none,
6388+
"MainActor is not permitted within %0", (StringRef))
6389+
ERROR(concurrency_task_to_thread_model_global_actor,none,
6390+
"globalActor is not permitted within %0", (StringRef))
6391+
ERROR(concurrency_task_to_thread_model_global_actor_annotation,none,
6392+
"annotating a type with a global actor %0 is not permitted within %1",
6393+
(TypeRepr*, StringRef))
6394+
63836395
#define UNDEFINE_DIAGNOSTIC_MACROS
63846396
#include "DefineDiagnosticMacros.h"

include/swift/Basic/Features.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ LANGUAGE_FEATURE(BuiltinCreateAsyncTaskInGroup, 0, "MainActor executor building
8282
LANGUAGE_FEATURE(BuiltinMove, 0, "Builtin.move()", true)
8383
LANGUAGE_FEATURE(BuiltinCopy, 0, "Builtin.copy()", true)
8484
LANGUAGE_FEATURE(BuiltinStackAlloc, 0, "Builtin.stackAlloc", true)
85+
LANGUAGE_FEATURE(BuiltinTaskRunInline, 0, "Builtin.taskRunInline", true)
8586
SUPPRESSIBLE_LANGUAGE_FEATURE(SpecializeAttributeWithAvailability, 0, "@_specialize attribute with availability", true)
8687
LANGUAGE_FEATURE(BuiltinAssumeAlignment, 0, "Builtin.assumeAlignment", true)
8788
SUPPRESSIBLE_LANGUAGE_FEATURE(UnsafeInheritExecutor, 0, "@_unsafeInheritExecutor", true)

include/swift/Basic/LangOptions.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ namespace swift {
9595
ErrorOnFailureRemarkOnSuccess
9696
};
9797

98+
enum class ConcurrencyModel : uint8_t {
99+
Standard,
100+
TaskToThread,
101+
};
102+
98103
/// A collection of options that affect the language dialect and
99104
/// provide compiler debugging facilities.
100105
class LangOptions final {
@@ -510,6 +515,13 @@ namespace swift {
510515
/// Enables dumping type witness systems from associated type inference.
511516
bool DumpTypeWitnessSystems = false;
512517

518+
/// The model of concurrency to be used.
519+
ConcurrencyModel ActiveConcurrencyModel = ConcurrencyModel::Standard;
520+
521+
bool isConcurrencyModelTaskToThread() const {
522+
return ActiveConcurrencyModel == ConcurrencyModel::TaskToThread;
523+
}
524+
513525
/// Sets the target we are building for and updates platform conditions
514526
/// to match.
515527
///

include/swift/Option/FrontendOptions.td

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,11 @@ def enable_ad_hoc_availability :
10651065
Flag<["-"], "enable-ad-hoc-availability">,
10661066
HelpText<"Enable experimental support for ad hoc availability">;
10671067

1068+
def concurrency_model :
1069+
Separate<["-"], "concurrency-model">,
1070+
HelpText<"Which concurrency model is used. Defaults to standard.">,
1071+
MetaVarName<"standard|task-to-thread">;
1072+
def concurrency_model_EQ :
1073+
Joined<["-"], "concurrency-model=">,
1074+
Alias<concurrency_model>;
10681075
} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]

include/swift/Runtime/Concurrency.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
#define SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR 0
3333
#endif
3434

35+
// Does the runtime use a task-thread model?
36+
#if defined(SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY)
37+
#define SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL 1
38+
#else
39+
#define SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL 0
40+
#endif
41+
3542
// Does the runtime integrate with libdispatch?
3643
#ifndef SWIFT_CONCURRENCY_ENABLE_DISPATCH
3744
#if SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR
@@ -85,6 +92,20 @@ AsyncTaskAndContext swift_task_create_common(
8592
TaskContinuationFunction *function, void *closureContext,
8693
size_t initialContextSize);
8794

95+
#if SWIFT_CONCURRENCY_TASK_TO_THREAD_MODEL
96+
#define SWIFT_TASK_RUN_INLINE_INITIAL_CONTEXT_BYTES 4096
97+
/// Begin an async context in the current sync context and run the indicated
98+
/// closure in it.
99+
///
100+
/// This is only supported under the task-to-thread concurrency model and
101+
/// relies on a synchronous implementation of task blocking in order to work.
102+
SWIFT_EXPORT_FROM(swift_Concurrency)
103+
SWIFT_CC(swift)
104+
void swift_task_run_inline(OpaqueValue *result, void *closureAFP,
105+
OpaqueValue *closureContext,
106+
const Metadata *futureResultType);
107+
#endif
108+
88109
/// Allocate memory in a task.
89110
///
90111
/// This must be called synchronously with the task.

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2033,6 +2033,24 @@ FUNCTION(EndAsyncLet,
20332033
ATTRS(NoUnwind),
20342034
EFFECT(Concurrency))
20352035

2036+
/// void swift_task_run_inline(
2037+
/// OpaqueValue *result,
2038+
/// void *closureAFP,
2039+
/// OpaqueValue *closureContext,
2040+
/// const Metadata *futureResultType
2041+
/// )
2042+
FUNCTION(TaskRunInline,
2043+
swift_task_run_inline, SwiftCC,
2044+
TaskRunInlineAvailability,
2045+
RETURNS(VoidTy),
2046+
ARGS(OpaquePtrTy, // OpaqueValue *result
2047+
Int8PtrTy, // void *closure
2048+
OpaquePtrTy, // OpaqueValue *closureContext
2049+
TypeMetadataPtrTy, // const Metadata *futureResultType
2050+
),
2051+
ATTRS(NoUnwind),
2052+
EFFECT(NoEffect))
2053+
20362054
// void swift_taskGroup_initialize(TaskGroup *group);
20372055
FUNCTION(TaskGroupInitialize,
20382056
swift_taskGroup_initialize, SwiftCC,

0 commit comments

Comments
 (0)