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

Test '-march=native' support for all architectures and platforms #96

Open
ljishen opened this issue Mar 6, 2022 · 8 comments · Fixed by #98
Open

Test '-march=native' support for all architectures and platforms #96

ljishen opened this issue Mar 6, 2022 · 8 comments · Fixed by #98
Labels
enhancement New feature or request

Comments

@ljishen
Copy link

ljishen commented Mar 6, 2022

This flag fails on some ARM platforms including the Apple M1 (1, 2). We may be able to check whether it is supported by using clang -march=native -cx /dev/null (1, 2) before adding to the compile options.

Upvote & Fund

  • I am using Polar.sh so you can upvote and help fund this issue. The funding is received once the issue is completed & confirmed by you.

  • Thank you in advance for helping prioritize & fund our backlog.

Fund with Polar
@aminya aminya changed the title The clang compiler does not support '-march=native' The clang compiler does not support '-march=native' on ARM Mar 6, 2022
@aminya
Copy link
Owner

aminya commented Mar 6, 2022

Is this supported on other compilers like GCC and MSVC?

@aminya aminya added the bug Something isn't working label Mar 6, 2022
@ljishen
Copy link
Author

ljishen commented Mar 6, 2022

I don't see the same problem with GCC. And I don't have an environment to test with MSVC.

@aminya
Copy link
Owner

aminya commented Mar 6, 2022

I tested inside Godbolt, and it seems that GCC doesn't support it either!

gcc arm
https://godbolt.org/z/G93d8qPed

clang arm
https://godbolt.org/z/zfTcTbhG9

It also seems that /arch:native doesn't actually work on MSVC.
https://godbolt.org/z/G3W1EqeMo

It seems it only accepts the exact instruction set like AVX
https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64

@aminya aminya changed the title The clang compiler does not support '-march=native' on ARM Arm Clang/GCC do not support '-march=native' Mar 6, 2022
@ljishen
Copy link
Author

ljishen commented Mar 6, 2022

I found another issue while testing this.

@ljishen
Copy link
Author

ljishen commented Mar 6, 2022

I tested inside Godbolt, and it seems that GCC doesn't support it either!

gcc arm https://godbolt.org/z/G93d8qPed

clang arm https://godbolt.org/z/zfTcTbhG9

It also seems that /arch:native doesn't actually work on MSVC. https://godbolt.org/z/G3W1EqeMo

It seems it only accepts the exact instruction set like AVX https://docs.microsoft.com/en-us/cpp/build/reference/arch-x64

I'm glad you found that. Maybe my ARM machine is supported by GCC but not clang. The key is that not all ARM machines are supported by these two compilers with that flag.

@ljishen
Copy link
Author

ljishen commented Mar 7, 2022

I suspect that the reason why your GCC test on Godbolt doesn't work is that Godbolt uses cross-compiling on an x86_64 machine and the "native" flag means using the processor's features of the host machine for compiling code, which doesn't work if the toolchain is for Arm and you ask it to optimize for a different type of CPU (e.g., x86_64). Therefore, for cross-compiling, the flag should be explicit for a relevant type of CPU.

This article suggests that the optimization flag for x86 and Arm cannot be the same and we should use -mcpu for Arm and -march for x86:
https://community.arm.com/arm-community-blogs/b/tools-software-ides-blog/posts/compiler-flags-across-architectures-march-mtune-and-mcpu

@aminya aminya closed this as completed in #98 Mar 7, 2022
@aminya aminya reopened this Mar 7, 2022
@aminya aminya changed the title Arm Clang/GCC do not support '-march=native' Test '-march=native' support for all architectures and platforms Mar 7, 2022
@aminya aminya added enhancement New feature or request and removed bug Something isn't working labels Mar 7, 2022
@aminya
Copy link
Owner

aminya commented Mar 16, 2022

If there is a deterministic and cross-platform way to do this, I will be open to doing that.

For GCC and clang, we can use [gcc/clang] -Werror [the test flag] -E -xc /dev/null and check the return code.
For example, [gcc/clang] -Werror -march=native -E -xc /dev/null or [gcc/clang] -Werror -mcpu=native -E -xc /dev/null.

I guess MSVC will have a similar way to test, but I don't have the environment for testing.

Originally posted by @ljishen in #98 (comment)

@aminya
Copy link
Owner

aminya commented May 17, 2022

I found a project that does this! We can use that
https://github.com/scivision/cmake-cpu-detect

@abeimler abeimler mentioned this issue Dec 30, 2022
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants