Page MenuHomePhabricator

Please use GitHub pull requests for new patches. Phabricator shutdown timeline

Add --print-supported-cpus flag for clang.

Authored by ziangwan on Jun 10 2019, 4:10 PM.



This patch allows clang users to print out a list of supported CPU models using

clang [--target=<target triple>] --print-supported-cpus

Then, users can select the CPU model to compile to using

clang --target=<triple> -mcpu=<model> a.c

It is a handy feature to help cross compilation.

While the flag seems to be a front-end option, the supported CPU models are stored in each target machine in the back-end. This patch attempts to create a short cut to directly retrieve information from the back-end target machines at the front-end.

Diff Detail

Event Timeline

ziangwan created this revision.Jun 10 2019, 4:10 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 10 2019, 4:10 PM
ziangwan edited the summary of this revision. (Show Details)Jun 10 2019, 4:11 PM
ziangwan updated this revision to Diff 204191.Jun 11 2019, 4:34 PM

Add full context. Change the name of the static variable from "Printed" to "PrintOnce". Add a newline for the test file.

kongyi added inline comments.Jun 12 2019, 1:35 PM

L615 should be after L609.


Add test for a different architecture to verify that --target works.


RM seems to be unused. Can you use None?


Use unique_ptr instead.

lebedev.ri added inline comments.

this line seems out-of-place, it's already documented in Target-dependent compilation options section


Is this correct? Is this due to the --target?
I've never ever seen that flag used, on x86 -march/-mtune is pretty much always used.

ziangwan updated this revision to Diff 204359.Jun 12 2019, 2:40 PM
ziangwan marked 6 inline comments as done.

Adjust the docs: remove the redundant -mcpu flag. Add an explanation for --print-supported-cpus.
Replace one manual delete with std::unique_ptr.
Use None for an llvm::Optional value.
Add one more test case.
Adjust the message printout to include the usage of -mtune.

ziangwan added inline comments.Jun 12 2019, 2:44 PM

Both -mtune and -mcpu accept the processor names listed by --print-supported-cpus. The difference is that -mcpu also specify the instruction set given the processor while -mtune will use a more generic instruction set.

kongyi accepted this revision.Jun 13 2019, 9:18 PM


This revision is now accepted and ready to land.Jun 13 2019, 9:18 PM
ziangwan closed this revision.Jun 14 2019, 2:40 PM

Committed. SVN Revision: 363464.

dyung added a subscriber: dyung.Jun 14 2019, 3:33 PM

Hi Ziang,

Your test fails if the the compiler does not include the x86_64 and ARM backends. For example, the PS4 target compiler only includes the x86_64 backend, so it fails when trying to use ARM like you are attempting to do.

FAIL: Clang :: Driver/print-supported-cpus.c (8911 of 50224)
******************** TEST 'Clang :: Driver/print-supported-cpus.c' FAILED ********************
: 'RUN: at line 3';   c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\clang.exe --target=x86_64-unknown-linux-gnu    --print-supported-cpus 2>&1    | c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\filecheck.exe C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\tools\clang\test\Driver\print-supported-cpus.c --check-prefix=CHECK-X86
: 'RUN: at line 10';   c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\clang.exe --target=arm-unknown-linux-android    --print-supported-cpus 2>&1    | c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\filecheck.exe C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\tools\clang\test\Driver\print-supported-cpus.c --check-prefix=CHECK-ARM
Exit Code: 1

Command Output (stdout):
$ ":" "RUN: at line 3"
$ "c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\clang.exe" "--target=x86_64-unknown-linux-gnu" "--print-supported-cpus"
$ "c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\filecheck.exe" "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\tools\clang\test\Driver\print-supported-cpus.c" "--check-prefix=CHECK-X86"
$ ":" "RUN: at line 10"
$ "c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\clang.exe" "--target=arm-unknown-linux-android" "--print-supported-cpus"
note: command had no output on stdout or stderr
error: command failed with exit status: 1
$ "c:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.obj\bin\filecheck.exe" "C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\tools\clang\test\Driver\print-supported-cpus.c" "--check-prefix=CHECK-ARM"
# command stderr:
C:\ps4-buildslave2\llvm-clang-lld-x86_64-scei-ps4-windows10pro-fast\llvm.src\tools\clang\test\Driver\print-supported-cpus.c:14:15: error: CHECK-ARM: expected string not found in input

// CHECK-ARM: cortex-a73


<stdin>:3:1: note: scanning from here

Thread model: posix


<stdin>:5:64: note: possible intended match here

No available targets are compatible with triple "armv4t-unknown-linux-android"


error: command failed with exit status: 1

Can you fix the test so that it does not depend on the backend being present if possible, or find another way to test the feature?

There are lots of tests under clang/test/ that target arm-- as a -triple. How is it that they don't fail in the same way? %clang_cc1 -triple arm-- is somehow different? mentions .cfg files, but tests I've looked at don't seem to have a .cfg in their directory.

dyung added a comment.Jun 14 2019, 4:02 PM

There are lots of tests under clang/test/ that target arm-- as a -triple. How is it that they don't fail in the same way? %clang_cc1 -triple arm-- is somehow different?

I can't answer in general, but a quick scan of the test/Driver directory shows most tests use "-###", so it doesn't matter if a particular target is present or not in the backend.

@pirama and @kongyi pointed out to me that -emit-llvm tests don't require backend support, and that the undocumented ( REQUIRES: tag would fix this.

I am working on fixing the test file right now. I will add REQUIRES: x86-registered-target and REQUIRES: arm-registered-target before each test correspondingly.

@dyung I pushed another commit to fix the test. Let me know if the problem persists.

This option is useful but may be hard to discover. Will something like -march=? and -mtune=? make the feature more discoverable?

This option is useful but may be hard to discover. Will something like -march=? and -mtune=? make the feature more discoverable?

That's a good idea. Currently, my change has nothing to do with -march or -mtune. I might work on that later.