Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/gpgmm/utils/Assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@
} while (GPGMM_ASSERT_LOOP_CONDITION)
#else
# if defined(GPGMM_COMPILER_MSVC)
# define GPGMM_ASSERT_CALLSITE_HELPER(file, func, line, condition) __assume(condition)
// Avoid calling __assume(condition) directly because we can't assume the |condition| will always
// evaluate to be true at runtime and when false, the condition (or code) could be optimized out by
// MSVC and never executed. To protect the ASSERT's code, the equivelent generated code using
// __assume(0) is used.
# define GPGMM_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
do { \
if (!(condition)) { \
GPGMM_UNREACHABLE(); \
} \
} while (GPGMM_ASSERT_LOOP_CONDITION)
# elif defined(GPGMM_COMPILER_CLANG) && GPGMM_HAS_BUILTIN(__builtin_unreachable)
// Avoid using __builtin_assume since it results into clang assuming _every_ function call has a
// side effect. Alternatively, suppress these warnings with -Wno-assume or wrap _builtin_assume in
Expand All @@ -57,7 +66,7 @@
# define GPGMM_ASSERT_CALLSITE_HELPER(file, func, line, condition) \
do { \
if (!(condition)) { \
GPGMM_BUILTIN_UNREACHABLE(); \
GPGMM_UNREACHABLE(); \
} \
} while (GPGMM_ASSERT_LOOP_CONDITION)
# else
Expand All @@ -70,10 +79,11 @@

#define GPGMM_ASSERT(condition) \
GPGMM_ASSERT_CALLSITE_HELPER(__FILE__, __func__, __LINE__, condition)
#define GPGMM_UNREACHABLE() \

#define GPGMM_ASSERT_UNREACHABLE() \
do { \
GPGMM_ASSERT(GPGMM_ASSERT_LOOP_CONDITION && "Unreachable code hit"); \
GPGMM_BUILTIN_UNREACHABLE(); \
GPGMM_UNREACHABLE(); \
} while (GPGMM_ASSERT_LOOP_CONDITION)

// Disable short-hand defined macros due to possible name clash.
Expand All @@ -83,7 +93,7 @@
#endif

#if !defined(UNREACHABLE)
# define UNREACHABLE GPGMM_UNREACHABLE
# define UNREACHABLE GPGMM_ASSERT_UNREACHABLE
#endif

namespace gpgmm {
Expand Down
8 changes: 4 additions & 4 deletions src/gpgmm/utils/Compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
// Defines macros for compiler-specific functionality
// - GPGMM_COMPILER_[CLANG|GCC|MSVC]: Compiler detection
// - GPGMM_BREAKPOINT(): Raises an exception and breaks in the debugger
// - GPGMM_BUILTIN_UNREACHABLE(): Hints the compiler that a code path is unreachable
// - GPGMM_UNREACHABLE(): Hints the compiler that a code path is unreachable
// - GPGMM_NO_DISCARD: An attribute that is C++17 [[nodiscard]] where available
// - GPGMM_(UN)?LIKELY(EXPR): Where available, hints the compiler that the expression will be true
// (resp. false) to help it generate code that leads to better branch prediction.
Expand All @@ -44,7 +44,7 @@
# define GPGMM_BREAKPOINT()
# endif

# define GPGMM_BUILTIN_UNREACHABLE() __builtin_unreachable()
# define GPGMM_UNREACHABLE() __builtin_unreachable()
# define GPGMM_LIKELY(x) __builtin_expect(!!(x), 1)
# define GPGMM_UNLIKELY(x) __builtin_expect(!!(x), 0)

Expand All @@ -64,10 +64,10 @@
extern void __cdecl __debugbreak(void);
# define GPGMM_BREAKPOINT() __debugbreak()

# define GPGMM_BUILTIN_UNREACHABLE() __assume(false)
# define GPGMM_UNREACHABLE() __assume(false)

// Visual Studio 2017 15.3 adds support for [[nodiscard]]
# if _MSC_VER >= 1911 && DAWN_CPP_VERSION >= 17
# if _MSC_VER >= 1911 && GPGMM_CPP_VERSION >= 17
# define GPGMM_NO_DISCARD [[nodiscard]]
# endif

Expand Down