Skip to content

Commit 4c7e509

Browse files
authored
PYBIND11_NOINLINE-related cleanup. (#3179)
* Removing pragma for GCC -Wattributes, fixing forward declarations. * Introducing PYBIND11_NOINLINE_FWD to deal with CUDA, GCC7, GCC8. * Updating PYBIND11_NOINLINE_DCL in Doxyfile. * Trying noinline, noinline for {CUDA, GCC7, GCC8} * Trying noinline, inline for {CUDA, GCC7, GCC8} * Adding GCC -Wattributes `pragma` in 3 header files. * Introducing PYBIND11_NOINLINE_GCC_PRAGMA_ATTRIBUTES_NEEDED, used in 9 header files. * Removing ICC pragma 2196, to see if it is still needed. * Trying noinline, noinline for ICC * Trying noinline, inline for ICC * Restoring ICC pragma 2196, introducing PYBIND11_NOINLINE_FORCED, defined for testing. * Removing code accidentally left in (was for experimentation only). * Removing one-time-test define. * Removing PYBIND11_NOINLINE_FWD macro (after learning that it makes no sense). * Testing with PYBIND11_NOINLINE_DISABLED. Minor non-functional enhancements. * Removing #define PYBIND11_NOINLINE_DISABLED (test was successful). * Removing PYBIND11_NOINLINE_FORCED and enhancing comments for PYBIND11_NOINLINE. * WIP stripping back * Making -Wattributes pragma in pybind11 specific to GCC7, GCC8, CUDA.
1 parent ff590c1 commit 4c7e509

File tree

8 files changed

+36
-31
lines changed

8 files changed

+36
-31
lines changed

include/pybind11/attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ enum op_id : int;
124124
enum op_type : int;
125125
struct undefined_t;
126126
template <op_id id, op_type ot, typename L = undefined_t, typename R = undefined_t> struct op_;
127-
inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
127+
void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret);
128128

129129
/// Internal data structure which holds metadata about a keyword argument
130130
struct argument_record {

include/pybind11/detail/common.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,15 @@
9999
# endif
100100
#endif
101101

102-
#if defined(_MSC_VER)
103-
# define PYBIND11_NOINLINE __declspec(noinline)
102+
// The PYBIND11_NOINLINE macro is for function DEFINITIONS.
103+
// In contrast, FORWARD DECLARATIONS should never use this macro:
104+
// https://stackoverflow.com/questions/9317473/forward-declaration-of-inline-functions
105+
#if defined(PYBIND11_NOINLINE_DISABLED) // Option for maximum portability and experimentation.
106+
# define PYBIND11_NOINLINE inline
107+
#elif defined(_MSC_VER)
108+
# define PYBIND11_NOINLINE __declspec(noinline) inline
104109
#else
105-
# define PYBIND11_NOINLINE __attribute__ ((noinline))
110+
# define PYBIND11_NOINLINE __attribute__ ((noinline)) inline
106111
#endif
107112

108113
#if defined(__MINGW32__)
@@ -778,8 +783,8 @@ PYBIND11_RUNTIME_EXCEPTION(import_error, PyExc_ImportError)
778783
PYBIND11_RUNTIME_EXCEPTION(cast_error, PyExc_RuntimeError) /// Thrown when pybind11::cast or handle::call fail due to a type casting error
779784
PYBIND11_RUNTIME_EXCEPTION(reference_cast_error, PyExc_RuntimeError) /// Used internally
780785

781-
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
782-
[[noreturn]] PYBIND11_NOINLINE inline void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }
786+
[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const char *reason) { throw std::runtime_error(reason); }
787+
[[noreturn]] PYBIND11_NOINLINE void pybind11_fail(const std::string &reason) { throw std::runtime_error(reason); }
783788

784789
template <typename T, typename SFINAE = void> struct format_descriptor { };
785790

include/pybind11/detail/internals.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ inline void translate_local_exception(std::exception_ptr p) {
257257
#endif
258258

259259
/// Return a reference to the current `internals` data
260-
PYBIND11_NOINLINE inline internals &get_internals() {
260+
PYBIND11_NOINLINE internals &get_internals() {
261261
auto **&internals_pp = get_internals_pp();
262262
if (internals_pp && *internals_pp)
263263
return **internals_pp;
@@ -352,14 +352,14 @@ PYBIND11_NAMESPACE_END(detail)
352352
/// Returns a named pointer that is shared among all extension modules (using the same
353353
/// pybind11 version) running in the current interpreter. Names starting with underscores
354354
/// are reserved for internal usage. Returns `nullptr` if no matching entry was found.
355-
inline PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
355+
PYBIND11_NOINLINE void *get_shared_data(const std::string &name) {
356356
auto &internals = detail::get_internals();
357357
auto it = internals.shared_data.find(name);
358358
return it != internals.shared_data.end() ? it->second : nullptr;
359359
}
360360

361361
/// Set the shared data that can be later recovered by `get_shared_data()`.
362-
inline PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
362+
PYBIND11_NOINLINE void *set_shared_data(const std::string &name, void *data) {
363363
detail::get_internals().shared_data[name] = data;
364364
return data;
365365
}

include/pybind11/detail/type_caster_base.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class loader_life_support {
8181
inline std::pair<decltype(internals::registered_types_py)::iterator, bool> all_type_info_get_cache(PyTypeObject *type);
8282

8383
// Populates a just-created cache entry.
84-
PYBIND11_NOINLINE inline void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
84+
PYBIND11_NOINLINE void all_type_info_populate(PyTypeObject *t, std::vector<type_info *> &bases) {
8585
std::vector<PyTypeObject *> check;
8686
for (handle parent : reinterpret_borrow<tuple>(t->tp_bases))
8787
check.push_back((PyTypeObject *) parent.ptr());
@@ -150,7 +150,7 @@ inline const std::vector<detail::type_info *> &all_type_info(PyTypeObject *type)
150150
* ancestors are pybind11-registered. Throws an exception if there are multiple bases--use
151151
* `all_type_info` instead if you want to support multiple bases.
152152
*/
153-
PYBIND11_NOINLINE inline detail::type_info* get_type_info(PyTypeObject *type) {
153+
PYBIND11_NOINLINE detail::type_info* get_type_info(PyTypeObject *type) {
154154
auto &bases = all_type_info(type);
155155
if (bases.empty())
156156
return nullptr;
@@ -176,7 +176,7 @@ inline detail::type_info *get_global_type_info(const std::type_index &tp) {
176176
}
177177

178178
/// Return the type info for a given C++ type; on lookup failure can either throw or return nullptr.
179-
PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_index &tp,
179+
PYBIND11_NOINLINE detail::type_info *get_type_info(const std::type_index &tp,
180180
bool throw_if_missing = false) {
181181
if (auto ltype = get_local_type_info(tp))
182182
return ltype;
@@ -191,13 +191,13 @@ PYBIND11_NOINLINE inline detail::type_info *get_type_info(const std::type_index
191191
return nullptr;
192192
}
193193

194-
PYBIND11_NOINLINE inline handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
194+
PYBIND11_NOINLINE handle get_type_handle(const std::type_info &tp, bool throw_if_missing) {
195195
detail::type_info *type_info = get_type_info(tp, throw_if_missing);
196196
return handle(type_info ? ((PyObject *) type_info->type) : nullptr);
197197
}
198198

199199
// Searches the inheritance graph for a registered Python instance, using all_type_info().
200-
PYBIND11_NOINLINE inline handle find_registered_python_instance(void *src,
200+
PYBIND11_NOINLINE handle find_registered_python_instance(void *src,
201201
const detail::type_info *tinfo) {
202202
auto it_instances = get_internals().registered_instances.equal_range(src);
203203
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
@@ -325,7 +325,7 @@ struct values_and_holders {
325325
* The returned object should be short-lived: in particular, it must not outlive the called-upon
326326
* instance.
327327
*/
328-
PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/, bool throw_if_missing /*= true in common.h*/) {
328+
PYBIND11_NOINLINE value_and_holder instance::get_value_and_holder(const type_info *find_type /*= nullptr default in common.h*/, bool throw_if_missing /*= true in common.h*/) {
329329
// Optimize common case:
330330
if (!find_type || Py_TYPE(this) == find_type->type)
331331
return value_and_holder(this, find_type, 0, 0);
@@ -349,7 +349,7 @@ PYBIND11_NOINLINE inline value_and_holder instance::get_value_and_holder(const t
349349
#endif
350350
}
351351

352-
PYBIND11_NOINLINE inline void instance::allocate_layout() {
352+
PYBIND11_NOINLINE void instance::allocate_layout() {
353353
auto &tinfo = all_type_info(Py_TYPE(this));
354354

355355
const size_t n_types = tinfo.size();
@@ -397,19 +397,19 @@ PYBIND11_NOINLINE inline void instance::allocate_layout() {
397397
owned = true;
398398
}
399399

400-
PYBIND11_NOINLINE inline void instance::deallocate_layout() const {
400+
PYBIND11_NOINLINE void instance::deallocate_layout() const {
401401
if (!simple_layout)
402402
PyMem_Free(nonsimple.values_and_holders);
403403
}
404404

405-
PYBIND11_NOINLINE inline bool isinstance_generic(handle obj, const std::type_info &tp) {
405+
PYBIND11_NOINLINE bool isinstance_generic(handle obj, const std::type_info &tp) {
406406
handle type = detail::get_type_handle(tp, false);
407407
if (!type)
408408
return false;
409409
return isinstance(obj, type);
410410
}
411411

412-
PYBIND11_NOINLINE inline std::string error_string() {
412+
PYBIND11_NOINLINE std::string error_string() {
413413
if (!PyErr_Occurred()) {
414414
PyErr_SetString(PyExc_RuntimeError, "Unknown internal error occurred");
415415
return "Unknown internal error occurred";
@@ -456,7 +456,7 @@ PYBIND11_NOINLINE inline std::string error_string() {
456456
return errorString;
457457
}
458458

459-
PYBIND11_NOINLINE inline handle get_object_handle(const void *ptr, const detail::type_info *type ) {
459+
PYBIND11_NOINLINE handle get_object_handle(const void *ptr, const detail::type_info *type ) {
460460
auto &instances = get_internals().registered_instances;
461461
auto range = instances.equal_range(ptr);
462462
for (auto it = range.first; it != range.second; ++it) {
@@ -483,7 +483,7 @@ inline PyThreadState *get_thread_state_unchecked() {
483483
}
484484

485485
// Forward declarations
486-
inline void keep_alive_impl(handle nurse, handle patient);
486+
void keep_alive_impl(handle nurse, handle patient);
487487
inline PyObject *make_new_instance(PyTypeObject *type);
488488

489489
class type_caster_generic {

include/pybind11/detail/typeid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ inline void erase_all(std::string &string, const std::string &search) {
2929
}
3030
}
3131

32-
PYBIND11_NOINLINE inline void clean_type_id(std::string &name) {
32+
PYBIND11_NOINLINE void clean_type_id(std::string &name) {
3333
#if defined(__GNUG__)
3434
int status = 0;
3535
std::unique_ptr<char, void (*)(void *)> res {

include/pybind11/numpy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct numpy_internals {
104104
}
105105
};
106106

107-
inline PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) {
107+
PYBIND11_NOINLINE void load_numpy_internals(numpy_internals* &ptr) {
108108
ptr = &get_or_create_shared_data<numpy_internals>("_numpy_internals");
109109
}
110110

@@ -1110,7 +1110,7 @@ struct field_descriptor {
11101110
dtype descr;
11111111
};
11121112

1113-
inline PYBIND11_NOINLINE void register_structured_dtype(
1113+
PYBIND11_NOINLINE void register_structured_dtype(
11141114
any_container<field_descriptor> fields,
11151115
const std::type_info& tinfo, ssize_t itemsize,
11161116
bool (*direct_converter)(PyObject *, void *&)) {

include/pybind11/pybind11.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#pragma once
1212

13-
#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
13+
#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8))
1414
# pragma GCC diagnostic push
1515
# pragma GCC diagnostic ignored "-Wattributes"
1616
#endif
@@ -1875,7 +1875,7 @@ template <typename Type> class enum_ : public class_<Type> {
18751875
PYBIND11_NAMESPACE_BEGIN(detail)
18761876

18771877

1878-
inline void keep_alive_impl(handle nurse, handle patient) {
1878+
PYBIND11_NOINLINE void keep_alive_impl(handle nurse, handle patient) {
18791879
if (!nurse || !patient)
18801880
pybind11_fail("Could not activate keep_alive!");
18811881

@@ -1902,7 +1902,7 @@ inline void keep_alive_impl(handle nurse, handle patient) {
19021902
}
19031903
}
19041904

1905-
PYBIND11_NOINLINE inline void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
1905+
PYBIND11_NOINLINE void keep_alive_impl(size_t Nurse, size_t Patient, function_call &call, handle ret) {
19061906
auto get_arg = [&](size_t n) {
19071907
if (n == 0)
19081908
return ret;
@@ -2152,7 +2152,7 @@ exception<CppException> &register_local_exception(handle scope,
21522152
}
21532153

21542154
PYBIND11_NAMESPACE_BEGIN(detail)
2155-
PYBIND11_NOINLINE inline void print(const tuple &args, const dict &kwargs) {
2155+
PYBIND11_NOINLINE void print(const tuple &args, const dict &kwargs) {
21562156
auto strings = tuple(args.size());
21572157
for (size_t i = 0; i < args.size(); ++i) {
21582158
strings[i] = str(args[i]);
@@ -2384,6 +2384,6 @@ PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
23842384
# pragma GCC diagnostic pop // -Wnoexcept-type
23852385
#endif
23862386

2387-
#if defined(__GNUG__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
2387+
#if defined(__CUDACC__) || (defined(__GNUC__) && (__GNUC__ == 7 || __GNUC__ == 8))
23882388
# pragma GCC diagnostic pop
23892389
#endif

include/pybind11/pytypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct arg; struct arg_v;
2424

2525
PYBIND11_NAMESPACE_BEGIN(detail)
2626
class args_proxy;
27-
inline bool isinstance_generic(handle obj, const std::type_info &tp);
27+
bool isinstance_generic(handle obj, const std::type_info &tp);
2828

2929
// Accessor forward declarations
3030
template <typename Policy> class accessor;
@@ -316,7 +316,7 @@ template <typename T> T reinterpret_borrow(handle h) { return {h, object::borrow
316316
template <typename T> T reinterpret_steal(handle h) { return {h, object::stolen_t{}}; }
317317

318318
PYBIND11_NAMESPACE_BEGIN(detail)
319-
inline std::string error_string();
319+
std::string error_string();
320320
PYBIND11_NAMESPACE_END(detail)
321321

322322
#if defined(_MSC_VER)

0 commit comments

Comments
 (0)