What does clang do if more than one optimization flag is set?

Viewed 202

E.g., what happens when you type clang -O3 -O0?

Is the behavior the same as in gcc? I.e., all but the last optimization flags are ignored (described here and here)?

I looked up in the manual page for clang but couldn't find an explanation in the optimization flag section.

Is it undefined behaviour?

2 Answers

Though a test case does not provide a definitive proof of uniform behavior throughout, it may hint on one or another direction, from what I was able to observe in test cases for Clang in several versions both with C and C++ code, it appears that, as in GCC, the previous conflicting flags are ignored and the last one stands. I did look into the Clang documentation and it appears no mention is made about this situation.

Sample C program with Clang 11.0.1

The assumption may be that this will not be an issue and that obviously the last flag is the one that stands. One might argue that is not so obvious, for example, an argument can be made that the higher optimization level should stand.

As to why it was not documented, one can only speculate, maybe lack of awareness of the situation lead to it. The fact is that this should at least, as in GCC, be documented.

I have no documentation to support it, but this test indicates it picks the last one:

klutt@desktop:/tmp$ clang f.c -O3; ls -l a.out
-rwxr-xr-x 1 klutt klutt 16752 Mar 19 15:37 a.out
klutt@desktop:/tmp$ clang f.c -O0; ls -l a.out
-rwxr-xr-x 1 klutt klutt 16704 Mar 19 15:37 a.out
klutt@desktop:/tmp$ clang f.c -O3 -O0; ls -l a.out
-rwxr-xr-x 1 klutt klutt 16704 Mar 19 15:37 a.out
klutt@desktop:/tmp$ clang f.c -O0 -O3; ls -l a.out
-rwxr-xr-x 1 klutt klutt 16752 Mar 19 15:37 a.out
Related