For a long time i used gcc
to compile C code. Sometimes i had to use the optimize("O0")
attribute to disable optimizations for a specific 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 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 google (and also) stackoverflow to find out what attribute is required for clang
, because many of them are not in the standard (as soon as i know).
I found this thread: In clang, how do you use per-function optimization attributes?
If i try the 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 attribute optnone
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 to move the attribute after the function name, but it doesn't 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;
}
It 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.
Probably i do something wrong, but i cannot see what.
-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
The -fno-inline-functions will disable this optimization.
The syntax is #pragma GCC optimize (option, ...) From the official source on GCC pragmas, this pragma allows you to set global optimization flags (specified by option ) for functions that come after it.
No. GCC passes your assembly source through the preprocessor and then to the assembler. At no time are any optimisations performed.
LLVM divides the entire compilation process into three steps: Frontend: Convert the high-level language to IR. Middle-End: Perform optimization in the IR layer. Backend: Convert the IR into the assembly language of the corresponding hardware platform.
Try the following, clang-style attribute specification:
[[clang::optnone]]
void blabla(void);
EDIT: Clang 3.3 is pretty outdated. Use a more recent version, and your original ((optnone))
code will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With