Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

clang

clang++

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?

like image 735
Lime91 Avatar asked Oct 20 '25 13:10

Lime91


1 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.

like image 99
anastaciu Avatar answered Oct 23 '25 03:10

anastaciu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!