Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mono] Second try at using C11 atomics #91808

Merged
merged 6 commits into from
Oct 10, 2023
Merged

Conversation

lambdageek
Copy link
Member

@lambdageek lambdageek commented Sep 8, 2023

  • For each platform decide if we will use C11 standard atomics, Win32 API atomics, GCC atomic intrinsics or emulated atomics.

    To use C11 atomics we generally want them to be lock-free for the primitive types we care about (that is, ATOMIC_LONG_LONG_LOCK_FREE == 2 not 1 and similarly for other macros) because otherwise we cannot be sure if the atomic might use a global lock in which case we could have thread suspend problems if both the GC and the rest of the runtime use an atomic at the same time.

  • On win32, use the win32 atomics until MSVC atomics support is not experimental, or we update our build to pass /experimental:c11atomics; and also until we build our C++ code with C++23 or later (otherwise MSVC will complain about including stdatomic.h)

  • If the header gets included while we're generating offsets using offsets-tool.py, pretend we're using emulated atomics. On some Linux configurations, the libclang that we use ends up picking up the platform atomic.h header, not the clang one, and then errors out on their underlying implementation.

  • Replace the use of bool in some macros in Mono - it will expand to _Bool in C11 and mess things up.

    Use boolean instead

    Fixes [Android][arm64] Use of undeclared identifier 'ICALL_SIG_TYPE__Bool' #91779

@lambdageek

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@lambdageek
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lambdageek
Copy link
Member Author

lambdageek commented Sep 8, 2023

"Build linux-arm64 Release AllSubsets_Mono_LLVMFullAot_RuntimeTests llvmfullaot failed" is failing with

https://dev.azure.com/dnceng-public/public/_build/results?buildId=400016&view=logs&jobId=e45de9b4-b3b3-54f9-2ea5-8e56201c788d&j=e45de9b4-b3b3-54f9-2ea5-8e56201c788d&t=5472c3eb-a71b-57d3-d7b0-31d38a8b059e

Running 'python3 /__w/1/s/src/mono/mono/tools/offsets-tool/offsets-tool.py --abi=aarch64-linux-gnu --netcore --targetdir="/__w/1/s/artifacts/obj/mono/linux.arm64.Release" --monodir="/__w/1/s/src/mono" --nativedir="/__w/1/s/src/native" --outfile="/__w/1/s/artifacts/obj/mono/linux.arm64.Release/cross/offsets-aarch-linux-gnu.h" --libclang="/usr/local/lib/libclang.so.16" --prefix="/crossrootfs/arm64/usr/lib/gcc/aarch64-linux-gnu/5" --sysroot="/crossrootfs/arm64"'
  Running clang: --target=aarch64-linux-gnu --sysroot /crossrootfs/arm64 -I /crossrootfs/arm64/include -I /crossrootfs/arm64/usr/lib/gcc/aarch64-linux-gnu/5/include -I /crossrootfs/arm64/usr/lib/gcc/aarch64-linux-gnu/5/include-fixed -std=gnu11 -DMONO_GENERATING_OFFSETS -I /__w/1/s/src/mono -I /__w/1/s/src/mono/mono -I /__w/1/s/src/mono/mono/eglib -I /__w/1/s/src/native -I /__w/1/s/src/native/public -I /__w/1/s/artifacts/obj/mono/linux.arm64.Release -I /__w/1/s/artifacts/obj/mono/linux.arm64.Release/mono/eglib -DTARGET_ARM64 -DHOST_LINUX -DMONO_CROSS_COMPILE -DUSE_MONO_CTX -DHAVE_SGEN_GC -DHAVE_MOVING_COLLECTOR /__w/1/s/src/mono/mono/metadata/metadata-cross-helpers.c
  
  /__w/1/s/src/mono/mono/utils/atomic.h:79:8: error: address argument to atomic operation must be a pointer to a trivially-copyable type ('volatile atomic_int *' (aka 'volatile _Atomic(int) *') invalid)

which looks similar to https://bugs.launchpad.net/ubuntu/+source/llvm-toolchain-6.0/+bug/1780747 which says that clang 6.0.1 had some kind of a regression. Are we running some really old clang?

@lewing
Copy link
Member

lewing commented Sep 8, 2023

should we revert the atomics pr while we sort this out?

@lewing
Copy link
Member

lewing commented Sep 8, 2023

reverting in #91812 while we figure out the failure

@lambdageek
Copy link
Member Author

lambdageek commented Sep 8, 2023

I kind of suspect the LLVMFullAot lane is failing because it's using the GCC stdatomic.h header, not the clang stdatomic.h header.

In GCC atomic_compare_exchange_strong is a macro defined in terms of __atomic_compare_exchange. And clang doesn't like atomic_int * as an argument type to that intrinsic:

Gnu header:
https://github.com/gcc-mirror/gcc/blob/b96b554592c5cbb6a2c1797ffcb5706fd295f4fd/gcc/ginclude/stdatomic.h#L168-L177

Clang output: https://godbolt.org/z/8WP7hbvG1

Clang header:

https://clang.llvm.org/doxygen/stdatomic_8h_source.html

Note it defines the standard function as __c11_atomic_compare_exchange_strong

Updated godbolt: https://godbolt.org/z/Kq6qrWTjW

@lambdageek
Copy link
Member Author

I merged main and reverted Larry's revert. And also added self.target_args += ["-isystem", clang_path + "/../lib/clang/16/include"] to offsets-tool on linux-arm64 to try and convince it to get the clang header

@lambdageek

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@lambdageek
Copy link
Member Author

@directhex do you think it's safe to do clang_path = os.path.dirname(args.libclang) self.target_args += ["-isystem", clang_path + "/../lib/clang/16/include"] on all platforms? I alaready do it for wasm and now for linux-arm64.

@directhex
Copy link
Member

@lambdageek it seems fair to try for our infra. I can't guarantee it working on anyone else's.

@lambdageek
Copy link
Member Author

The include path didn't help on linux-arm64 llvmfullaot. Its clear what the problem is, but I have no idea how to fix the include paths without a repro environment.

@lambdageek
Copy link
Member Author

New approach. The offsets tool doesn't actually need to look at anything in atomic.h, so just ifdef it out if we're generating offsets.

@lambdageek

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@lambdageek
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

the offset tool doesn't need to see the inlined atomic operations at all

Also, refactor the ifdef logic up front and define one of
MONO_USE_C11_ATOMIC, MONO_USE_WIN32_ATOMIC, MONO_USE_GCC_ATOMIC or
MONO_USE_EMULATED_ATOMIC depending on what we will use
@lambdageek

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@lambdageek lambdageek changed the title [mono] Dont' use bareword bool in icall sig macros [mono] Second try at using C11 atomics Oct 6, 2023
@lambdageek
Copy link
Member Author

I'm back to shaving this yak. New approach: don't try to make the offsets tool smarter, just pretend we're going to use emulated atomics. The offsets tool doesn't actually need to see the implementations of mono's atomic operations, so just avoid the whole problem.

@lambdageek

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@lambdageek
Copy link
Member Author

/azp run runtime-extra-platforms

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@lambdageek lambdageek marked this pull request as ready for review October 6, 2023 19:08
@lambdageek
Copy link
Member Author

@vargaz @jkoritzinsky @akoeplinger I assume your reviews are still 👍 ? Nothing substantially different - just some refactoring to not show offsets-tools the atomic implementations (it doesn't need to see function bodies)

@lambdageek
Copy link
Member Author

I believe the remaining failures are unrelated

@lambdageek lambdageek merged commit c48aa81 into dotnet:main Oct 10, 2023
146 of 159 checks passed
@ghost ghost locked as resolved and limited conversation to collaborators Nov 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android][arm64] Use of undeclared identifier 'ICALL_SIG_TYPE__Bool'
7 participants