Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Assert.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2022, The Eigen authors.
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_CORE_UTIL_ASSERT_H
12#define EIGEN_CORE_UTIL_ASSERT_H
13
14// Eigen custom assert function.
15//
16// The combination of Eigen's relative includes and cassert's `assert` function
17// (or any usage of the __FILE__ macro) can lead to ODR issues:
18// a header included using different relative paths in two different TUs will
19// have two different token-for-token definitions, since __FILE__ is expanded
20// as an in-line string with different values. Normally this would be
21// harmless - the linker would just choose one definition. However, it breaks
22// with C++20 modules when functions in different modules have different
23// definitions.
24//
25// To get around this, we need to use __builtin_FILE() when available, which is
26// considered a single token, and thus satisfies the ODR.
27
28// Only define eigen_plain_assert if we are debugging, and either
29// - we are not compiling for GPU, or
30// - gpu debugging is enabled.
31#if !defined(EIGEN_NO_DEBUG) && (!defined(EIGEN_GPU_COMPILE_PHASE) || !defined(EIGEN_NO_DEBUG_GPU))
32
33#include <cassert>
34
35#ifndef EIGEN_USE_CUSTOM_PLAIN_ASSERT
36// Disable new custom asserts by default for now.
37#define EIGEN_USE_CUSTOM_PLAIN_ASSERT 0
38#endif
39
40#if EIGEN_USE_CUSTOM_PLAIN_ASSERT
41
42#ifndef EIGEN_HAS_BUILTIN_FILE
43// Clang can check if __builtin_FILE() is supported.
44// Supported GCC versions and MSVC 2019 14.26 (1926) all have __builtin_FILE().
45//
46// Supported NVCC toolkits provide __builtin_FILE() on both host and device.
47#if (EIGEN_HAS_BUILTIN(__builtin_FILE) && (EIGEN_COMP_CLANG || !defined(EIGEN_CUDA_ARCH))) || \
48 (EIGEN_COMP_GNUC_STRICT && (EIGEN_COMP_NVCC || !defined(EIGEN_CUDA_ARCH))) || (EIGEN_COMP_MSVC >= 1926)
49#define EIGEN_HAS_BUILTIN_FILE 1
50#else
51#define EIGEN_HAS_BUILTIN_FILE 0
52#endif
53#endif // EIGEN_HAS_BUILTIN_FILE
54
55#if EIGEN_HAS_BUILTIN_FILE
56#define EIGEN_BUILTIN_FILE __builtin_FILE()
57#define EIGEN_BUILTIN_LINE __builtin_LINE()
58#else
59// Default (potentially unsafe) values.
60#define EIGEN_BUILTIN_FILE __FILE__
61#define EIGEN_BUILTIN_LINE __LINE__
62#endif
63
64// Use __PRETTY_FUNCTION__ when available, since it is more descriptive, as
65// __builtin_FUNCTION() only returns the undecorated function name.
66// This should still be okay ODR-wise since it is a compiler-specific fixed
67// value. Mixing compilers will likely lead to ODR violations anyways.
68#if EIGEN_COMP_MSVC
69#define EIGEN_BUILTIN_FUNCTION __FUNCSIG__
70#elif EIGEN_COMP_GNUC
71#define EIGEN_BUILTIN_FUNCTION __PRETTY_FUNCTION__
72#else
73#define EIGEN_BUILTIN_FUNCTION __func__
74#endif
75
76namespace Eigen {
77namespace internal {
78
79// Generic default assert handler.
80template <typename EnableIf = void, typename... EmptyArgs>
81struct assert_handler_impl {
82 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE static inline void run(const char* expression, const char* file, unsigned line,
83 const char* function) {
84#ifdef EIGEN_GPU_COMPILE_PHASE
85 // GPU device code doesn't allow stderr or abort, so use printf and raise an
86 // illegal instruction exception to trigger a kernel failure.
87#ifndef EIGEN_NO_IO
88 printf("Assertion failed at %s:%u in %s: %s\n", file == nullptr ? "<file>" : file, line,
89 function == nullptr ? "<function>" : function, expression);
90#endif
91 __trap();
92
93#else // EIGEN_GPU_COMPILE_PHASE
94
95 // Print to stderr and abort, as specified in <cassert>.
96#ifndef EIGEN_NO_IO
97 fprintf(stderr, "Assertion failed at %s:%u in %s: %s\n", file == nullptr ? "<file>" : file, line,
98 function == nullptr ? "<function>" : function, expression);
99#endif
100 std::abort();
101
102#endif // EIGEN_GPU_COMPILE_PHASE
103 }
104};
105
106// Use POSIX __assert_fail handler when available.
107//
108// This allows us to integrate with systems that have custom handlers.
109//
110// NOTE: this handler is not always available on all POSIX systems (otherwise
111// we could simply test for __unix__ or similar). The handler function name
112// seems to depend on the specific toolchain implementation, and differs between
113// compilers, platforms, OSes, etc. Hence, we detect support via SFINAE.
114template <typename... EmptyArgs>
115struct assert_handler_impl<void_t<decltype(__assert_fail((const char*)nullptr, // expression
116 (const char*)nullptr, // file
117 0, // line
118 (const char*)nullptr, // function
119 std::declval<EmptyArgs>()... // Empty substitution required
120 // for SFINAE.
121 ))>,
122 EmptyArgs...> {
123 EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE static inline void run(const char* expression, const char* file, unsigned line,
124 const char* function) {
125 // GCC requires this call to be dependent on the template parameters.
126 __assert_fail(expression, file, line, function, std::declval<EmptyArgs>()...);
127 }
128};
129
130EIGEN_DEVICE_FUNC EIGEN_DONT_INLINE inline void __assert_handler(const char* expression, const char* file,
131 unsigned line, const char* function) {
132 assert_handler_impl<>::run(expression, file, line, function);
133}
134
135} // namespace internal
136} // namespace Eigen
137
138#define eigen_plain_assert(expression) \
139 (EIGEN_PREDICT_FALSE(!(expression)) ? Eigen::internal::__assert_handler(#expression, EIGEN_BUILTIN_FILE, \
140 EIGEN_BUILTIN_LINE, EIGEN_BUILTIN_FUNCTION) \
141 : (void)0)
142
143#else // EIGEN_USE_CUSTOM_PLAIN_ASSERT
144
145// Use regular assert.
146#define eigen_plain_assert(condition) assert(condition)
147
148#endif // EIGEN_USE_CUSTOM_PLAIN_ASSERT
149
150#else // EIGEN_NO_DEBUG
151
152#define eigen_plain_assert(condition) ((void)0)
153
154#endif // EIGEN_NO_DEBUG
155
156#endif // EIGEN_CORE_UTIL_ASSERT_H