New submission from Sergey Fedoseev <fedoseev.ser...@gmail.com>:

GCC (along with Clang and ICC) has __builtin_unreachable() and MSVC has 
__assume() builtins, that can be used to optimize out unreachable conditions.

So we could add macro like this:

#ifdef Py_DEBUG
#   define Py_ASSUME(cond) (assert(cond))
#else
#   if defined(_MSC_VER)
#       define Py_ASSUME(cond) (__assume(cond))
#   elif defined(__GNUC__)
#       define Py_ASSUME(cond) (cond? (void)0: __builtin_unreachable())
#   else
#       define Py_ASSUME(cond) ((void)0);
#   endif
#endif

Here's a pair of really simple examples showing how it can optimize code: 
https://godbolt.org/z/g9LYXF.

Real world example. _PyLong_Copy() [1] calls _PyLong_New() [2]. _PyLong_New() 
checks the size, so that overflow does not occur. This check is redundant when 
_PyLong_New() is called from _PyLong_Copy(). We could add a function that 
bypass that check, but in LTO build PyObject_MALLOC() is inlined into 
_PyLong_New() and it also checks the size. Adding Py_ASSUME((size_t)size <= 
MAX_LONG_DIGITS) allows to bypass both checks. 

[1] 
https://github.com/python/cpython/blob/3a4f66707e824ef3a8384827590ebaa6ca463dc0/Objects/longobject.c#L287-L309
[2] 
https://github.com/python/cpython/blob/3a4f66707e824ef3a8384827590ebaa6ca463dc0/Objects/longobject.c#L264-L283

----------
messages: 352228
nosy: sir-sigurd
priority: normal
severity: normal
status: open
title: add macro for __builtin_unreachable

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38147>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to