Showing posts with label clang_built_in. Show all posts
Showing posts with label clang_built_in. Show all posts

Jul 18, 2022

[C++23] std::unreachable

Reference:
(msvc) __assume
(gcc/clang) __builtin_unreachable()
(c++23) std::unreachable

folly code:
https://github.com/facebook/folly/blob/0713d3b213a2a2560065d479030da5769ea5f406/folly/lang/Hint-inl.h#L37


[[noreturn]] inline void unreachable()
{
    // Uses compiler specific extensions if possible.
    // Even if no extension is used, undefined behavior is still raised by
    // an empty function body and the noreturn attribute.
#ifdef __GNUC__ // GCC, Clang, ICC
    __builtin_unreachable();
#elifdef _MSC_VER // MSVC
    __assume(false);
#endif
}

Jan 14, 2018

[clang] __builtin_unreachable

https://clang.llvm.org/docs/LanguageExtensions.html

__builtin_unreachable

__builtin_unreachable is used to indicate that a specific point in the program cannot be reached, even if the compiler might otherwise think it can. This is useful to improve optimization and eliminates certain warnings. For example, without the __builtin_unreachable in the example below, the compiler assumes that the inline asm can fall through and prints a “function declared ‘noreturn’ should not return” warning.
Syntax:
__builtin_unreachable()
Example of use:
void myabort(void) __attribute__((noreturn));
void myabort(void) {
  asm("int3");
  __builtin_unreachable();
}
Description:
The __builtin_unreachable() builtin has completely undefined behavior. Since it has undefined behavior, it is a statement that it is never reached and the optimizer can take advantage of this to produce better code. This builtin takes no arguments and produces a void result.
Query for this feature with __has_builtin(__builtin_unreachable).