This is an archive of the discontinued LLVM Phabricator instance.

[coroutines] Adding builtins for coroutine intrinsics and backendutil support.
ClosedPublic

Authored by GorNishanov on Sep 8 2016, 9:35 PM.
Tokens
"Mountain of Wealth" token, awarded by GorNishanov.

Diff Detail

Event Timeline

GorNishanov updated this revision to Diff 70780.Sep 8 2016, 9:35 PM
GorNishanov retitled this revision from to [Coroutines] Adding builtins for coroutine intrinsics and backendutil support..
GorNishanov updated this object.
GorNishanov added reviewers: rsmith, majnemer.
GorNishanov added a subscriber: cfe-commits.
rsmith added inline comments.Sep 21 2016, 7:23 PM
include/clang/Basic/Builtins.def
1343

I don't really like having builtins which will result in errors from the middle-end in some cases; there are a bunch of side-conditions on llvm.coro.id that aren't being enforced here. In particular, this call must be present in any function that also calls coro.alloc and friends, and must dominate those other calls.

Modeling the 'token' value as a void* also seems problematic. If the user uses that value for anything other than the argument to a builtin that wants the token, bad things are going to happen.

(From dinner discussion:) one possible way to deal with this would be to generate the call to @llvm.coro.id implicitly in the entry block, in a function that uses the other builtins. The challenge then is communicating the promise object to the intrinsic, which is problematic if we allow an arbitrary pointer value to be passed in.

However, we're only interested in supporting a stack variable as the promise object, so here's one possible approach:

  • add an attribute that can be applied to a local variable to mark it as the promise object for the current function
  • remove the __builtin_coro_id builtin, and instead implicitly generate the llvm.coro.id intrinsic call in the entry block when we need its token or see a promise object
  • when we emit a local variable with the promise-object attribute, update the llvm.coro.id call to pass its alloca as the promise object
  • remove the 'token' parameter from __builtin_coro_alloc etc, and instead implicitly provide it from the result of the implicit llvm.coro.id call
test/Coroutines/coro.c
1 ↗(On Diff #70780)

Please just check the IR generated by the frontend is correct for each of the intrinsics rather than using an end-to-end test that depends on the LLVM IR optimizations. You can use -disable-llvm-optzns to see the IR coming out of clang before the mandatory coroutine passes monkey with it.

GorNishanov updated this object.
GorNishanov removed a reviewer: majnemer.
  1. Added documentation for builtins
  2. Added a couple of tests with -disable-llvm-passes to check that builtins are emitted correctly
GorNishanov marked an inline comment as done.Sep 27 2016, 4:48 PM
GorNishanov added inline comments.
include/clang/Basic/Builtins.def
1343

I added clarification to the documentation that all but four builtins are for internal compiler use and for the use as development tools for the coroutine feature, so, possibly we should not worry too much about people misusing them.

Alternatively, I can get rid of most of the coroutine builtins, apart from the ones that are intended to be used to implement coroutine standard library facilities.

At the moment, I think, we should prioritize getting C++ Coroutines online. We can polish C coroutine story later.

test/Coroutines/coro.c
1 ↗(On Diff #70780)

I added two tests to check that builtins are lowered to coro intrinsics correctly. I would like to keep a small number of "-O2" tests as a sanity end-to-end testing.

GorNishanov added reviewers: rnk, EricWF.
GorNishanov marked an inline comment as done.

Improved diagnostics in CGCoroutine and ping...

GorNishanov retitled this revision from [Coroutines] Adding builtins for coroutine intrinsics and backendutil support. to [coroutines] Adding builtins for coroutine intrinsics and backendutil support..Sep 30 2016, 3:18 PM

rebase on top of the trunk

rsmith added inline comments.Oct 3 2016, 11:40 AM
docs/LanguageExtensions.rst
1886

Return type here is void*, not void.

1916

Remove both "the"s here.

test/CodeGenCoroutines/O2-coro.c
1 ↗(On Diff #73213)

Please do not test the output of LLVM optimization in a clang unit test. It seems fine to just remove this test, you have the relevant coverage elsewhere.

test/CodeGenCoroutines/coro-builtins.c
6

Use a pattern here rather than hardcoding %0; it's not reasonable to assume that this will always be the first instruction in the function.

Please also pass actual arguments here so we can test they're passed to the correct parameter.

GorNishanov updated this revision to Diff 73350.Oct 3 2016, 3:09 PM
  1. O2 test removed
  2. Update LanguageExtensions.rst with suggested edits
  3. coro-builtins.c test now passes useful arguments and pattern matches that proper arguments are passed in emitted intrinsics.
GorNishanov marked 5 inline comments as done.
rsmith accepted this revision.Oct 3 2016, 3:24 PM
rsmith edited edge metadata.
This revision is now accepted and ready to land.Oct 3 2016, 3:24 PM
GorNishanov updated this object.Oct 3 2016, 3:30 PM
GorNishanov edited edge metadata.
GorNishanov closed this revision.Oct 3 2016, 4:14 PM

Closed by commit r283155 - [coroutines] Adding builtins for coroutine intrinsics and backendutil support.

For some reason phabricator did not close it automatically.