swift icon indicating copy to clipboard operation
swift copied to clipboard

Add AST representation for coroutines

Open asl opened this issue 4 months ago • 15 comments

This is a proof-of-concept PR adding AST representation for coroutines

Further background at https://forums.swift.org/t/pitch-yield-once-functions-first-class-coroutines/77081

asl avatar Nov 06 '25 22:11 asl

@tkremenek @rjmccall Following discussions on LLVM Dev Meeting, this is a stripped-down version of https://github.com/swiftlang/swift/pull/78508:

  • No autodiff-related parts
  • No silgen (besides the "standard" special path used for coroutine accessors)
  • Some other fixes for SIL optimizer is omitted

Essentially the PR contains AST-related bits for coroutine declarations and coroutine function types as well as required boilerplate for AST mangling, dumping, serialization, etc.

@xedin comments from the original PR were addressed as well.

asl avatar Nov 06 '25 22:11 asl

Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test

asl avatar Nov 06 '25 23:11 asl

Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test

asl avatar Nov 13 '25 07:11 asl

Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test

asl avatar Nov 13 '25 20:11 asl

Please test with following pull request: https://github.com/swiftlang/llvm-project/pull/11396 @swift-ci please test windows platform

asl avatar Nov 14 '25 08:11 asl

https://github.com/swiftlang/swift-syntax/pull/3225 @swift-ci please test

asl avatar Jan 05 '26 22:01 asl

@slavapestov @rjmccall

The updated PR contains the support for yields w/o dedicated yield result type (I added them on top of the previous attempts to show the approach).

Things to note:

  1. There are no silgen changes here (as we discussed), though I have ported them separately and verified that removing special path for coroutine accessors and treating them as "ordinary coroutines" does not lead to any test breakage
  2. Function types support multiple yields (there was some initial boilerplate for this there), but I decided to postpone decision how we'd represent multiple yields for declarations (similar to error types), so declarations only support a single yield for now.
  3. It turned out that swift syntax parser treats function result arrow as a part of result clause, so to minimize amount of changes I decided to use for yields the syntax similar to the throws, so the yielded type uses contextual yields keyword that (if present) should be after after specifiers like throws or async
  4. Function type construction always requires yield types to be passed (even if empty), so they won't forgotten by accident. Though, the default implementation w/o yields could be added (I left it commented out for now)
  5. Everything is hidden behind dedicated experimental flag

Please let me know if there is something that should be changed / done differently in this approach.

asl avatar Jan 05 '26 22:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 @swift-ci please test

asl avatar Jan 05 '26 22:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test

asl avatar Jan 06 '26 08:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test

asl avatar Jan 06 '26 20:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform

asl avatar Jan 07 '26 19:01 asl

Looks like main is broken:

[2026-01-08T05:03:21.576Z] /Users/ec2-user/jenkins/workspace/swift-PR-macos/branch-main/swift/stdlib/public/core/StaticPrint.swift:866:29: error: expression uses unsafe constructs but is not marked with 'unsafe' [#]8;;https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety\StrictMemorySafety]8;;\]
[2026-01-08T05:03:21.576Z] 864 |   let argumentClosures = message.interpolation.arguments.argumentClosures
[2026-01-08T05:03:21.576Z] 865 |   if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
[2026-01-08T05:03:21.576Z] 866 |   let formatStringPointer = _getGlobalStringTablePointer(formatString)
[2026-01-08T05:03:21.576Z]     |                             |- error: expression uses unsafe constructs but is not marked with 'unsafe' [#]8;;https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety\StrictMemorySafety]8;;\]
[2026-01-08T05:03:21.576Z]     |                             `- note: reference to global function '_getGlobalStringTablePointer' involves unsafe type 'UnsafePointer<CChar>' (aka 'UnsafePointer<Int8>')
[2026-01-08T05:03:21.576Z] 867 |   unsafe constant_vprintf_backend(
[2026-01-08T05:03:21.576Z] 868 |     fmt: formatStringPointer,
[2026-01-08T05:03:21.576Z] 
[2026-01-08T05:03:21.576Z] [#StrictMemorySafety]: <https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety>
[2026-01-08T05:03:21.576Z] ninja: build stopped: subcommand failed.
[2026-01-08T05:03:21.576Z] ERROR: command `['env', '/usr/local/bin/cmake', '--build', '/Users/ec2-user/jenkins/workspace/swift-PR-macos/branch-main/build/buildbot_incremental/minimalstdlib-macosx-x86_64', '--config', 

asl avatar Jan 08 '26 06:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform

asl avatar Jan 08 '26 06:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test

asl avatar Jan 15 '26 05:01 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test macos platform

asl avatar Jan 16 '26 07:01 asl

Hi, Anton. I'll try to get back to this in a week; I've been getting over a case of the flu and needing to budget my energy.

rjmccall avatar Jan 20 '26 21:01 rjmccall

@rjmccall This is a new version to review. Changes:

  • No metadata-related changes for now
  • Default implementation for builtins (_function() vs _coroutine() helpers)
  • Removed refactoring remnants

Please take a look

asl avatar Feb 09 '26 20:02 asl

https://github.com/swiftlang/swift-syntax/pull/3225 https://github.com/swiftlang/llvm-project/pull/12064 @swift-ci please test

asl avatar Feb 09 '26 22:02 asl