Optimization, asserts and release mode

和自甴很熟 提交于 2019-12-01 02:25:51

问题


Consider a function

void f() {
   assert(condition);

   ...
}

In debug mode, where asserts are enabled, the compiler is free to assume condition holds, since the remaining code will not be executed if it does not.

However, in release mode, I believe the compiler will only see

void f() {
   ...
}

and can no longer assume condition.

Are there any compiler directives or static assert tricks to let compiler know about certain invariants?


回答1:


This can't be done in portable C or C++.

Some compilers provide intrinsic functions such as __assume (for MSVC) and __builtin_unreachable (for GCC, ICC, and Clang), that can be used for this purpose.

For example:

void f() {
    __assume(condition); //For MSVC
    /*...*/
}

void f() {
    if (!condition) __builtin_unreachable(); //for GCC and Clang
    /*...*/
}



回答2:


A part the verb "assume", that makes the question a little obscure, (and makes me worry abot you have a real understandment of what a compiler really does) the assert is a macro that is replaced by nothing when NDEBUG is defined (normally in release builds), and -when used in debug builds- throws an error if condition is not true.

The point, here, is that the program is "correct" only if condition is always true. After you proved it (in testing builds) there is no point in continue to prove it even in release code.

The more subtle problem can be if "condition" itself has a side effect you don't want to remove in release builds.

In this case, just put "condition" out from the assert, store the result in a bool and assert on the bool, like

bool condition_fine = (condition);
assert(condition_fine);

This let condition to be evaluated in any case, but removes the error check from release builds.



来源:https://stackoverflow.com/questions/23709259/optimization-asserts-and-release-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!