Clang: do not optimize a specific function

For a long time, I used gcc to compile C code. Sometimes I had to use the optimize("O0") attribute optimize("O0") to disable optimization for a particular function. Now I like to do this with clang .

Assume the following code:

 #include <stdio.h> void __attribute__((optimize("O0"))) blabla(void) { } int main(void) { blabla(); return 0; } 

If I compile it with clang , I will get this error:

 test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes] void __attribute__((optimize("O0"))) blabla(void) { ^ 1 warning generated. 

Then I used goover (and also) stackoverflow to find out which attribute is required for clang , because many of them are not standard (as soon as I know).

I found this topic: In clang, how do you use the optimization attributes of each function?

If I try the optimize("0") attribute optimize("0") , I get this error:

 test2.c:3:21: warning: unknown attribute 'optimize' ignored [-Wattributes] void __attribute__((optimize("0"))) blabla(void) { ^ 1 warning generated. 

And if I try the optnone attribute, I get this error:

 test2.c:3:21: warning: unknown attribute 'optnone' ignored [-Wattributes] void __attribute__((optnone)) blabla(void) { ^ 1 warning generated. 

I also tried moving the attribute after the function name, but it does not work (for some reason there is a warning about GCC ?!):

 test2.c:3:34: warning: GCC does not allow optnone attribute in this position on a function definition [-Wgcc-compat] void blabla(void) __attribute__((optnone)) { ^ test2.c:3:34: warning: unknown attribute 'optnone' ignored [-Wattributes] 2 warnings generated. 

Another test with the following code:

 #include <stdio.h> [[clang::optnone]] void blabla(void) { } int main(void) { blabla(); return 0; } 

He produces:

 user@ubuntu :/tmp/optxx$ clang test2.c test2.c:3:1: error: expected identifier or '(' [[clang::optnone]] ^ test2.c:3:2: error: expected expression [[clang::optnone]] ^ test2.c:8:5: warning: implicit declaration of function 'blabla' is invalid in C99 [-Wimplicit-function-declaration] blabla(); ^ 1 warning and 2 errors generated. 

Perhaps I am doing something wrong, but I do not see that.

-edit -

clang version:

 user@ubuntu :/tmp/optxx$ clang -v Ubuntu clang version 3.3-16ubuntu1 (branches/release_33) (based on LLVM 3.3) Target: x86_64-pc-linux-gnu Thread model: posix 
+5
source share
1 answer

Try the following clang-style attribute specification:

 [[clang::optnone]] void blabla(void); 

EDIT : Clang 3.3 is pretty dated. Use a newer version and your source code ((optnone)) will work.

+3
source

Source: https://habr.com/ru/post/1241033/


All Articles