-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Change function calls to use vectorcall #5948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+345
−222
Merged
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
ef662f6
Make argument_vector re-usable for other types.
b-pass 418a034
Attempt to collect args into array for vectorcall
b-pass fc22c97
Revert "Attempt to collect args into array for vectorcall"
b-pass 9894a5a
Implement vectorcall args collector
b-pass 73ee046
pre-commit fixes
b-pass 4615f8a
Checkpoint in moving to METH_FASTCALL
b-pass 1dc7b3f
pre-commit fixes
b-pass 893df5d
Use the names tuple directly, cleaner code and less reference counting
b-pass 655db8a
Fix unit test, the code now holds more references
b-pass c43b156
Make clangtidy happy
b-pass d881a7b
Oops, _v is C++14
b-pass 76831ec
style: pre-commit fixes
pre-commit-ci[bot] 0285818
Minor code cleanup
b-pass cc2b5a9
Fix signed conversions
b-pass 25485b3
Fix args expansion
b-pass 6c6bd71
style: pre-commit fixes
pre-commit-ci[bot] afd6299
Code cleanup
b-pass b8ba02d
Merge branch 'master' into b-pass→vectorcall
rwgk 6ed6d5a
fix(tests): Install multiple-interpreter test modules into wheel
rwgk ca72d6c
tests: Pin numpy 2.4.0 for Python 3.14 CI tests
rwgk 7148a4a
tests: Add verbose flag to CIBW pytest command
rwgk bc9c877
tests: Skip subinterpreter tests on iOS, add pytest timeout
rwgk 849df0b
More cleanups in argument vector, per comments.
b-pass b517942
Per Cursor, move all versions to Vectorcall since it has been support…
b-pass b4ca366
Switch to a bool vec for the used_kwargs flag...
b-pass c2e65f1
Fix signedness for clang
b-pass 1526a85
Another signedness issue
b-pass a926160
tests: Disable pytest-timeout for Pyodide (no signal.setitimer)
rwgk c163bf2
Combine temp storage and args into one vector
b-pass 34fd03b
Phrasing
b-pass 1d539c2
Delete incorrect comment
b-pass eea6651
Fix push_back
b-pass 5059cea
Update push_back in argument_vector.h
b-pass 8ad086d
style: pre-commit fixes
pre-commit-ci[bot] a78a398
Use real types for these instead of object
b-pass d828e7d
refactor: Replace small_vector<object> with ref_small_vector for expl…
rwgk e09c222
Merge branch 'master' into b-pass→vectorcall
rwgk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2078,8 +2078,7 @@ using is_pos_only = std::is_same<intrinsic_t<T>, pos_only>; | |||||
| // forward declaration (definition in attr.h) | ||||||
| struct function_record; | ||||||
|
|
||||||
| /// (Inline size chosen mostly arbitrarily; 6 should pad function_call out to two cache lines | ||||||
| /// (16 pointers) in size.) | ||||||
| /// Inline size chosen mostly arbitrarily. | ||||||
| constexpr std::size_t arg_vector_small_size = 6; | ||||||
|
|
||||||
| /// Internal data associated with a single function call | ||||||
|
|
@@ -2191,94 +2190,131 @@ class argument_loader { | |||||
| std::tuple<make_caster<Args>...> argcasters; | ||||||
| }; | ||||||
|
|
||||||
| /// Helper class which collects only positional arguments for a Python function call. | ||||||
| /// A fancier version below can collect any argument, but this one is optimal for simple calls. | ||||||
| template <return_value_policy policy> | ||||||
| class simple_collector { | ||||||
| public: | ||||||
| template <typename... Ts> | ||||||
| explicit simple_collector(Ts &&...values) | ||||||
| : m_args(pybind11::make_tuple<policy>(std::forward<Ts>(values)...)) {} | ||||||
|
|
||||||
| const tuple &args() const & { return m_args; } | ||||||
| dict kwargs() const { return {}; } | ||||||
|
|
||||||
| tuple args() && { return std::move(m_args); } | ||||||
|
|
||||||
| /// Call a Python function and pass the collected arguments | ||||||
| object call(PyObject *ptr) const { | ||||||
| PyObject *result = PyObject_CallObject(ptr, m_args.ptr()); | ||||||
| if (!result) { | ||||||
| throw error_already_set(); | ||||||
| } | ||||||
| return reinterpret_steal<object>(result); | ||||||
| } | ||||||
|
|
||||||
| private: | ||||||
| tuple m_args; | ||||||
| }; | ||||||
| // [workaround(intel)] Separate function required here | ||||||
| // We need to put this into a separate function because the Intel compiler | ||||||
| // fails to compile enable_if_t<!all_of<is_positional<Args>...>::value> | ||||||
| // (tested with ICC 2021.1 Beta 20200827). | ||||||
| template <typename... Args> | ||||||
| constexpr bool args_has_keyword_or_ds() { | ||||||
| return any_of<is_keyword_or_ds<Args>...>::value; | ||||||
| } | ||||||
|
|
||||||
| /// Helper class which collects positional, keyword, * and ** arguments for a Python function call | ||||||
| template <return_value_policy policy> | ||||||
| class unpacking_collector { | ||||||
| public: | ||||||
| template <typename... Ts> | ||||||
| explicit unpacking_collector(Ts &&...values) { | ||||||
| // Tuples aren't (easily) resizable so a list is needed for collection, | ||||||
| // but the actual function call strictly requires a tuple. | ||||||
| auto args_list = list(); | ||||||
| using expander = int[]; | ||||||
| (void) expander{0, (process(args_list, std::forward<Ts>(values)), 0)...}; | ||||||
| /* | ||||||
| Python can sometimes utilize an extra space before the arguments to prepend `self`. | ||||||
| This is important enough that there is a special flag for it: | ||||||
| PY_VECTORCALL_ARGUMENTS_OFFSET. | ||||||
| All we have to do is allocate an extra space at the beginning of this array, and set the | ||||||
| flag. Note that the extra space is not passed directly in to vectorcall. | ||||||
| */ | ||||||
| m_args.reserve(sizeof...(values) + 1); | ||||||
| m_args.emplace_back(); | ||||||
|
|
||||||
| if (args_has_keyword_or_ds<Ts...>()) { | ||||||
| object names_list = list(); | ||||||
|
|
||||||
| // collect_arguments guarantees this can't be constructed with kwargs before the last | ||||||
| // positional so we don't need to worry about Ts... being in anything but normal python | ||||||
| // order. | ||||||
| using expander = int[]; | ||||||
| (void) expander{0, (process(names_list, std::forward<Ts>(values)), 0)...}; | ||||||
|
|
||||||
| m_names = reinterpret_steal<tuple>(PyList_AsTuple(names_list.ptr())); | ||||||
| } else { | ||||||
| object not_used; | ||||||
|
|
||||||
| m_args = std::move(args_list); | ||||||
| using expander = int[]; | ||||||
| (void) expander{0, (process(not_used, std::forward<Ts>(values)), 0)...}; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| const tuple &args() const & { return m_args; } | ||||||
| const dict &kwargs() const & { return m_kwargs; } | ||||||
|
|
||||||
| tuple args() && { return std::move(m_args); } | ||||||
| dict kwargs() && { return std::move(m_kwargs); } | ||||||
|
|
||||||
| /// Call a Python function and pass the collected arguments | ||||||
| object call(PyObject *ptr) const { | ||||||
| PyObject *result = PyObject_Call(ptr, m_args.ptr(), m_kwargs.ptr()); | ||||||
| size_t nargs = m_args.size() - 1; // -1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) | ||||||
| if (m_names) { | ||||||
| nargs -= static_cast<size_t>(PyTuple_GET_SIZE(m_names.ptr())); | ||||||
| } | ||||||
| static_assert(sizeof(object) == sizeof(PyObject *), | ||||||
| "this cast requires objects to be wrapped pointers"); | ||||||
| PyObject *result | ||||||
| = _PyObject_Vectorcall(ptr, | ||||||
| reinterpret_cast<PyObject *const *>(m_args.data() + 1), | ||||||
| nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, | ||||||
| m_names.ptr()); | ||||||
| if (!result) { | ||||||
| throw error_already_set(); | ||||||
| } | ||||||
| return reinterpret_steal<object>(result); | ||||||
| } | ||||||
|
|
||||||
| tuple args() const { | ||||||
| size_t nargs = m_args.size() - 1; // -1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) | ||||||
| if (m_names) { | ||||||
| nargs -= static_cast<size_t>(PyTuple_GET_SIZE(m_names.ptr())); | ||||||
| } | ||||||
| tuple val(nargs); | ||||||
| for (size_t i = 0; i < nargs; ++i) { | ||||||
| val[i] = m_args[i + 1]; // +1 for PY_VECTORCALL_ARGUMENTS_OFFSET (see ctor) | ||||||
| } | ||||||
| return val; | ||||||
| } | ||||||
|
|
||||||
| dict kwargs() const { | ||||||
| dict val; | ||||||
| if (m_names) { | ||||||
| auto namestup = reinterpret_borrow<tuple>(m_names); | ||||||
b-pass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| size_t offset = m_args.size() - namestup.size(); | ||||||
| for (size_t i = 0; i < namestup.size(); ++i, ++offset) { | ||||||
| val[namestup[i]] = m_args[offset]; | ||||||
| } | ||||||
| } | ||||||
| return val; | ||||||
| } | ||||||
|
|
||||||
| private: | ||||||
| // normal argument, possibly needing conversion | ||||||
| template <typename T> | ||||||
| void process(list &args_list, T &&x) { | ||||||
| void process(object & /*names_list*/, T &&x) { | ||||||
| auto o = reinterpret_steal<object>( | ||||||
| detail::make_caster<T>::cast(std::forward<T>(x), policy, {})); | ||||||
| if (!o) { | ||||||
| #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) | ||||||
| throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size())); | ||||||
| throw cast_error_unable_to_convert_call_arg(std::to_string(m_args.size() - 1)); | ||||||
| #else | ||||||
| throw cast_error_unable_to_convert_call_arg(std::to_string(args_list.size()), | ||||||
| throw cast_error_unable_to_convert_call_arg(std::to_string(m_args.size() - 1), | ||||||
| type_id<T>()); | ||||||
| #endif | ||||||
| } | ||||||
| args_list.append(std::move(o)); | ||||||
| m_args.emplace_back(std::move(o)); | ||||||
| } | ||||||
|
|
||||||
| void process(list &args_list, detail::args_proxy ap) { | ||||||
| // * unpacking | ||||||
| void process(object & /*names_list*/, detail::args_proxy ap) { | ||||||
| if (!ap) { | ||||||
| return; | ||||||
| } | ||||||
| for (auto a : ap) { | ||||||
| args_list.append(a); | ||||||
| m_args.emplace_back(reinterpret_borrow<object>(a)); // keep alive | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| void process(list & /*args_list*/, arg_v a) { | ||||||
| // named argument | ||||||
| void process(object &names_list, arg_v a) { | ||||||
b-pass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| assert(names_list); | ||||||
| if (!a.name) { | ||||||
| #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) | ||||||
| nameless_argument_error(); | ||||||
| #else | ||||||
| nameless_argument_error(a.type); | ||||||
| #endif | ||||||
| } | ||||||
| if (m_kwargs.contains(a.name)) { | ||||||
| auto name = str(a.name); | ||||||
| if (names_list.contains(name)) { | ||||||
| #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) | ||||||
| multiple_values_error(); | ||||||
| #else | ||||||
|
|
@@ -2292,22 +2328,31 @@ class unpacking_collector { | |||||
| throw cast_error_unable_to_convert_call_arg(a.name, a.type); | ||||||
| #endif | ||||||
| } | ||||||
| m_kwargs[a.name] = std::move(a.value); | ||||||
| if (PyList_Append(names_list.ptr(), name.release().ptr()) < 0) { | ||||||
| throw error_already_set(); | ||||||
| } | ||||||
| m_args.emplace_back(a.value); | ||||||
|
||||||
| m_args.emplace_back(a.value); | |
| m_args.emplace_back(std::move(a.value)); |
b-pass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
b-pass marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.