Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In clang, how do you use per-function optimization attributes?

I'm trying to compile a specific function with no optimization using clang, in order to prevent certain security-related calls to memset() from being optimized away.

According to the documentation that can be found here, there exists an optnone attribute which allows this. Also, an example can be found here.

Unfortunately, (at least on the below version of clang, on OS X 10.9.5), this is causing compiler warnings, as can be seen in this example:

$ clang --version
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix

$ cat optnone.c
#include <string.h>

__attribute__((optnone)) void*
always_memset(void *b, int c, size_t len)
{
    return memset(b, c, len);
}

$ clang -Wall -O3 -c -o optnone.o optnone.c
optnone.c:3:16: warning: unknown attribute 'optnone' ignored [-Wattributes]
__attribute__((optnone)) void*
               ^
1 warning generated.

I also tried using #pragma clang optimize off, but this caused an unknown pragma ignored warning.

Does anyone know why this isn't working? Did I miss a prerequisite for using this feature? (I also tried using various different -std= parameters, including c11, gnu11, c99, and gnu99, but nothing changed the behavior.)

like image 866
mpontillo Avatar asked Oct 08 '14 21:10

mpontillo


People also ask

What is __ attribute __ in C?

The __attribute__ directive is used to decorate a code declaration in C, C++ and Objective-C programming languages. This gives the declared code additional attributes that would help the compiler incorporate optimizations or elicit useful warnings to the consumer of that code.

How do I use optimization in GCC?

The -O level option to gcc turns on compiler optimization, when the specified value of level has the following effects: 0. The default reduces compilation time and has the effect that debugging always yields the expected result. This level is equivalent to not specifying the -O option at all.

How do I disable Clang optimization?

The -fno-unroll-loops option will disable this optimization.

How does Clang work?

Like many other compilers design, Clang compiler has three phase: The front end that parses source code, checking it for errors, and builds a language-specific Abstract Syntax Tree (AST) to represent the input code. The optimizer: its goal is to do some optimization on the AST generated by the front end.


2 Answers

As clang documentation says,

Clang supports GCC’s gnu attribute namespace. All GCC attributes which are accepted with the __attribute__((foo)) syntax are also accepted as [[gnu::foo]]. This only extends to attributes which are specified by GCC (see the list of GCC function attributes, GCC variable attributes, and GCC type attributes). As with the GCC implementation, these attributes must appertain to the declarator-id in a declaration, which means they must go either at the start of the declaration or immediately after the name being declared.

Try

void* always_memset(void *b, int c, size_t len) [[gnu::optimize(0)]]

or

void* always_memset(void *b, int c, size_t len) __attribute__ ((optimize("0")));
like image 72
EvgeniyZh Avatar answered Oct 17 '22 04:10

EvgeniyZh


As advocated by @dulacc in his comment, __attribute__ ((optnone)) works on clang 9.0.0 on Mac's High Sierra.

like image 43
Paul Price Avatar answered Oct 17 '22 03:10

Paul Price