This is an archive of the discontinued LLVM Phabricator instance.

[LIBC] Implement `sched_yield()`
ClosedPublic

Authored by goldstein.w.n on Apr 10 2023, 6:07 PM.

Details

Summary

Implements: https://linux.die.net/man/2/sched_yield

Possibly we don't need the return value check / errno as according to
both the manpage (and current linux source) sched_yield cannot fail.

Diff Detail

Event Timeline

goldstein.w.n created this revision.Apr 10 2023, 6:07 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptApr 10 2023, 6:07 PM
goldstein.w.n requested review of this revision.Apr 10 2023, 6:07 PM

The function looks good, but since you're adding a new function there's a bit of extra configuration that needs to be done. I'll walk you through it here, but if you want an example then this patch should also show what needs to be done: https://reviews.llvm.org/D147970.

First, we need the function to have a FunctionSpec in the spec/ folder. For this function it will be in spec/gnu_ext.td, alongside sched_getaffinity.
Next, to get the function to build on the target platform we need to add the entrypoint to the list in config/linux/x86_64/entrypoints.txt. If you want you can also add it to the entrypoints list for other CPU architectures (e.g. RISC-V), but x86_64 is the most important one for testing.
We require all of the functions to have unit tests, which will go in test/src/sched/sched_yield_test.cpp. Don't worry about this making this too complex. This is a simple syscall wrapper so it just needs to test that calling the function succeeds.
Finally, the cmake for the sched folder is a little unusual, so you'll also need to add an alias to src/sched/CMakeLists.txt.

I know this looks like a lot, but each part should be fairly quick. Feel free to reach out if you want more guidance on how to do it.

Do the rest of the work

The function looks good, but since you're adding a new function there's a bit of extra configuration that needs to be done. I'll walk you through it here, but if you want an example then this patch should also show what needs to be done: https://reviews.llvm.org/D147970.

First, we need the function to have a FunctionSpec in the spec/ folder. For this function it will be in spec/gnu_ext.td, alongside sched_getaffinity.
Next, to get the function to build on the target platform we need to add the entrypoint to the list in config/linux/x86_64/entrypoints.txt. If you want you can also add it to the entrypoints list for other CPU architectures (e.g. RISC-V), but x86_64 is the most important one for testing.
We require all of the functions to have unit tests, which will go in test/src/sched/sched_yield_test.cpp. Don't worry about this making this too complex. This is a simple syscall wrapper so it just needs to test that calling the function succeeds.
Finally, the cmake for the sched folder is a little unusual, so you'll also need to add an alias to src/sched/CMakeLists.txt.

I know this looks like a lot, but each part should be fairly quick. Feel free to reach out if you want more guidance on how to do it.

Done, thanks for the guidance.

As an aside, are you planning to implement anything along the lines of gnu ifunc? If not is the method
for arch-specific just with higher isa builds and compiler flags (i.e #ifdef __AVX2__ and stuff like that).

Also are you guys planning on implementing dynamic loader?

dxf added a subscriber: dxf.Apr 11 2023, 2:13 PM

As an aside, are you planning to implement anything along the lines of gnu ifunc? If not is the method
for arch-specific just with higher isa builds and compiler flags (i.e #ifdef __AVX2__ and stuff like that).

Yes, the method is to build with architecture-specific flags. You can see examples of this in files like https://github.com/llvm/llvm-project/blob/main/libc/src/string/memory_utils/op_x86.h and https://github.com/llvm/llvm-project/blob/main/libc/src/math/generic/sinf.cpp

The model is to consider the libc functions as just another part of your application code that gets statically linked in. So you build the libc with the same optimization level, sanitizers, architecture defines, etc. you build your application with, and then deploy appropriately.

Also are you guys planning on implementing dynamic loader?

At some point, though it's not a current area of investment for anyone. If this is an area that interests you, patches are welcome!

You're just missing the test file then I'll approve.

libc/test/src/sched/CMakeLists.txt
24

did you forget to include the test file?

goldstein.w.n marked an inline comment as done.Apr 11 2023, 3:29 PM
goldstein.w.n added inline comments.
libc/test/src/sched/CMakeLists.txt
24

rip untracked files. fixed.

goldstein.w.n marked an inline comment as done.

Add tests

michaelrj accepted this revision.Apr 11 2023, 3:59 PM

LGTM with nit

libc/test/src/sched/yield_test.cpp
10

Nit: I'm not sure you need to include syscall here.

This revision is now accepted and ready to land.Apr 11 2023, 3:59 PM

Remove unneeded dep

goldstein.w.n marked an inline comment as done.Apr 11 2023, 5:01 PM
goldstein.w.n added inline comments.
libc/test/src/sched/yield_test.cpp
10

Fixed. 1 more unrelated question if you have the time.

I'm working on adding support for proper attr handling in pthread_create, but am finding normal libc tests / build doesn't include pthread (on linux).

Any idea for the extra steps to build/test pthread?

goldstein.w.n marked an inline comment as done.Apr 11 2023, 5:47 PM
goldstein.w.n added inline comments.
libc/spec/gnu_ext.td
52

@michaelrj are you sure this is the right place for sched_yield. Seems to be part of POSIX specification:
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/sched.h.html

sivachandra added inline comments.Apr 11 2023, 10:30 PM
libc/spec/gnu_ext.td
52

You are correct - this should be spec/posix.td.

libc/test/src/sched/yield_test.cpp
10

I'm working on adding support for proper attr handling in pthread_create, but am finding normal libc tests / build doesn't include pthread (on linux).

Any idea for the extra steps to build/test pthread?

Depends on what you mean by "normal libc tests". If you are running check-libc on linux, it should already be including the pthread tests. But, you can run pthread tests by running libc-integration-tests or use a more focused target libc-pthread-integration-tests. The tests live here: https://github.com/llvm/llvm-project/tree/main/libc/test/integration/src/pthread

Move to posix.td

goldstein.w.n marked an inline comment as done.Apr 11 2023, 10:34 PM
goldstein.w.n added inline comments.Apr 11 2023, 10:36 PM
libc/test/src/sched/yield_test.cpp
10

I'm working on adding support for proper attr handling in pthread_create, but am finding normal libc tests / build doesn't include pthread (on linux).

Any idea for the extra steps to build/test pthread?

Depends on what you mean by "normal libc tests". If you are running check-libc on linux, it should already be including the pthread tests.

Hmm, I'm noticing in a variety of cases some tests are skipped (including the entire pthread suite).
Building:

cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS='libc;llvm;clang' -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_CXX_FLAGS="-O1 -Wno-address" -DCMAKE_C_FLAGS="-O1 -Wno-address" -DLLVM_ENABLE_RTTI=ON -DLLVM_ENABLE_EH=ON -DBUILD_SHARED_LIBS=ON -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DCMAKE_INSTALL_PREFIX=/home/noah/programs/opensource/llvm-dev/build -DLLVM_CCACHE_BUILD=ON -DLLVM_BINUTILS_INCDIR=/home/noah/programs/opensource/binutils-dev/src/binutils-gdb/include/plugin-api.h

and testing with ninja check-libc

Anything inherently wrong with that?

But, you can run pthread tests by running libc-integration-tests or use a more focused target libc-pthread-integration-tests. The tests live here: https://github.com/llvm/llvm-project/tree/main/libc/test/integration/src/pthread

sivachandra accepted this revision.Apr 11 2023, 11:30 PM

OK from my side but please wait for @michaelrj who is leading this review.

libc/test/src/sched/yield_test.cpp
10

Ah! Parts like pthread which depend on type definitions are only available in the full build mode: -DLLVM_LIBC_FULL_BUILD=ON.

good catch that this should be in posix, it should be good to go once you make this change, if you want to make sure that everything is working then run ninja libc-integration-tests as well as check-libc

libc/spec/posix.td
1319

you need to add Sched to the list of headers here.

Add Sched to posix.td header

goldstein.w.n marked an inline comment as done.Apr 12 2023, 11:44 AM

good catch that this should be in posix, it should be good to go once you make this change, if you want to make sure that everything is working then run ninja libc-integration-tests as well as check-libc

Done, all pass. Likewise for D148069

michaelrj accepted this revision.Apr 12 2023, 11:44 AM

Looks good to me, feel free to land it.

This revision was automatically updated to reflect the committed changes.