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

Atomics not working with Newlib toolchain #1181

Open
Plociennik opened this issue Jan 25, 2023 · 9 comments
Open

Atomics not working with Newlib toolchain #1181

Plociennik opened this issue Jan 25, 2023 · 9 comments

Comments

@Plociennik
Copy link

Dear all,

I am trying to use the Newlib toolchain for the ima/ilp32 architecture and ABI to compile source code making use of atomics, e.g., the following one (from the LLVM code base):

#include <atomic>

std::atomic<int> x;
std::atomic<short> y;
std::atomic<char> z;

int main() {
  ++z;
  ++y;
  return ++x;
}

When trying to build this using riscv32-unknown-elf-g++ I get the following error:

test.cpp:(.text._ZNSt13__atomic_baseIcEppEv[_ZNSt13__atomic_baseIcEppEv]+0x24): undefined reference to `__atomic_fetch_add_1'
.../riscv32-unknown-elf/bin/ld: test.cpp:(.text._ZNSt13__atomic_baseIsEppEv[_ZNSt13__atomic_baseIsEppEv]+0x24): undefined reference to `__atomic_fetch_add_2'

I tried to supply -latomic but it seems this library is not included in the Newlib build.

I am having trouble to understand what to do here. I can find the C++ atomic header in my toolchain build, and the compiler also seems to generate atomic builtins, but I cannot find any definition for the required functions in any of the libraries.

As mentioned, I am using the ima ISA, so my target machine has atomics support, but I cannot see how I could use it.

Am I doing something wrong here and is there a way to make this work with the Newlib toolchain? I noticed that libatomic is built for the Linux toolchain, but I cannot use that one since I want to run my code on a bare metal target.

Any help would be greatly appreciated.

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Jan 25, 2023

Is this of any relevance?

Does the code compile and link if you omit the short and char atomics?

@Plociennik
Copy link
Author

Thanks a lot!

Yes, this clarifies the situation and I think the idea of using the necessary code from the Linux libatomic is something I will try.

As a note, I tried to compile the code without the short and char atomics, as you suggested, and this compiles. However, I have different kinds of code I need to compile and cannot yet see which exact types of atomics are included, or whether I can change it, so having a general approach such as providing the necessary atomics code using things from the Linux implementation is good.

@TommyMurphyTM1234
Copy link
Collaborator

Do you have to use the bare metal/newlib toolchain or could you use the Linux one? Maybe with -ffreestanding? Or would the resulting executables still depend on Linux OS features that you don't have?

@Plociennik
Copy link
Author

Hmm, that is a good question. The problem is that I have to integrate with lots of existing code, some of which provides low level calls to make Newlib run on our specific bare metal hardware. Furthermore, we need to have code that is really optimized for size, which makes the Linux/glibc toolchain maybe not so appropriate.

So I think in principle one could use the Linux toolchain but due to the above requirements I think I will go for the above-described solution.

Thanks again for your help!

@TommyMurphyTM1234
Copy link
Collaborator

TommyMurphyTM1234 commented Jan 25, 2023

One other thing - presumably you are targeting a multi hart/CPU environment that actually does need atomics? Just that, if not, then they are presumably redundant and you could just as well target your rv32ima/ilp32 as if it was actually rv32im/ilp32?

@Plociennik
Copy link
Author

Thanks for the hint! However, I will be running the code on a cluster with several RISCV32 cores that need to be synchronized at certain points, so I actually do need the a instructions.

@TommyMurphyTM1234
Copy link
Collaborator

Fair enough. Just asking in case... :⁠-⁠)

@hyperion009
Copy link

@Plociennik Did you have this done successfully? I want to know how many extra efforts I need put in. thanks in advance.

@Plociennik
Copy link
Author

No, I was not able to build libatomic in the Newlib build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants