-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-126599: Refactor the _PyOptimizer
APIs
#126853
Conversation
Please do not open unfinished PR. (Alternatively, you can convert it to a draft PR.) |
I disagree. Unfinished PRs are fine as long as they're for discussion. Also they help us lead contributors in the right direction before they put too much effort in. |
I don't object to this kind of behavior, but it should be opened as a draft before it is completed, which will waste review resources. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for picking this up. I think there might be some confusion of what is the API and what isn't so let me try my best to explain:
- The API in CPython refers to what people use externally.
pycore_*
files in general cannot be accessed when including the C API, unless they are included directly.PyAPI_FUNC
exports the symbol on Windows and other platforms.
So the thing we need to do is:
- Make sure the API now lives in
pycore_optimizer.h
- Remove
PyAPI_FUNC
, except for when the JIT compiler needs it (The JIT compiler needs symbols exported for Windows).
So the code in JUMP_BACKWARD
and ENTER_EXECUTOR
shouldn't change either. We need those code for optimizing the JIT!
Include/internal/pycore_optimizer.h
Outdated
PyAPI_FUNC(PyObject *) _PyOptimizer_NewCounter(void); | ||
PyAPI_FUNC(PyObject *) _PyOptimizer_NewUOpOptimizer(void); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory, the only change you need is to remove the PyAPI_FUNC
. That will stop the optimizer from being exported as public API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the header file isn't used elsewhere, we can remove it and keep the signature only in the source file.
(Sorry for the term mobile. I don't look it.)
Edit
I think. A small suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pycore_optimizer.h
is used. Please don't remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These 2 APIs are used for _testinternalcapi
for testing. It seems like _testinternalcapi
fails to be imported due to missing symbols after removing the PyAPI_FUNC
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should likely mark them as extern
if they are needed to be exported then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC, the APIs used by _testinternalcapi
need to be exported? It seems extern
does not help here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, right, it seems that declaring them as extern is not sufficient. So we should probably keep PyAPI_FUNC
. However, you should add a comment "// Export for '_testinternalcapi' shared extension.".
The reason why we advised you to use draft PRs is that you are committing a lot before the code is fine to be reviewed. If a PR is in a state where it should be reviewed (for instance if the CI is green or if there is no code planned to be committed just after), then it's perfectly fine to keep it open. |
There seems to be a little misunderstanding. I think this comment implies that there is still unfinished work :( |
Sorry, I'm still a little confused about the goal of this PR.
Does it mean we should define all JIT related APIs in this file?
It seems that the currently exported APIs in Reverted to bring the tests back. |
@xuantengh Thank you for your efforts on this PR. @Fidget-Spinner who has experience with this area of the code should be able to provide guidance as you move this PR forward. @rruuaanng, Let's try to keep the comments positive and constructive. Please try to encourage other contributors instead of telling them what to do or not do. The goal for PRs are to build understanding during review and iterate to an optimal solution. The solution, as we have discussed before, must consider many factors including backward compatibility, maintainability, security, testing, complexity and code churn. Sometimes a question lends much better results than telling someone what to do. For example, "Would you appreciate feedback on the PR so far?" instead of "Please do not open unfinished PR.". You can trust that the triagers and core developers are working toward creating a productive and healthy contributor experience for all. Please respect the importance of this goal. |
Sorry this got derailed, @xuantengh. It's also a little trickier than I originally assumed. Let's start over. For this PR, can you just remove the "counter" optimizer, and any tests that use it? I think that's a fairly self-contained task, and gives us a better idea of what difficulty remains in extracting the other two. I also don't think it will involve deleting any files. @rruuaanng, I can handle the review from here. I'm not worried about my time spent working with the author to get things in a good state here. |
Sure, I'll try it. Update: the |
b51de05
to
4c0e6c0
Compare
f6e0abb
to
3f89146
Compare
I'm sorry for forgetting to remove the "draft" label for this PR.
Regarding this goal, I think it's ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for reworking this!
There's a bit more to do, though. Please also remove these:
_PyCounterOptimizerObject
counter_dealloc
_PyCounterExecutor_Type
counter_optimize
counter_get_counter
counter_optimizer_methods
- Remove the check for "counting_executor" from
add_executor_dependency
(it isn't needed).
You'll also need to remove the _INTERNAL_INCREMENT_OPT_COUNTER
bytecode instruction. It's defined in Python/bytecodes.c
. Just remove it there, then run make regen-cases
to update all of the generated files.
Let me know if you get stuck.
Misc/NEWS.d/next/C_API/2025-01-01-03-25-38.gh-issue-126599.MRCYlH.rst
Outdated
Show resolved
Hide resolved
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
@xuantengh, is this ready for review again? Happy to help if you're stuck, but I'd like to get this change in soon. :) (Also, looks like you might need to merge in |
…YlH.rst Co-authored-by: Brandt Bucher <brandtbucher@gmail.com>
03555d2
to
f0ffafd
Compare
I have made the requested changes; please review again. Hi @brandtbucher, I've merged in |
Thanks for making the requested changes! @brandtbucher: please review the changes made to this pull request. |
It's not a problem to push new commits periodically (just don't force-push, please... merge instead of rebasing). I just wasn't sure if it was were ready yet. :) |
I'll try to finish reviewing this today, but I might not finish until tomorrow. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks! I'm just going to push a couple of changes to address some warnings and clean up the diff.
|
Hi this is my draft to remove the
_PyOptimizer_NewCounter
and_PyOptimizer_NewUOpOptimizer
APIs, where the related tests are removed or skipped.For the most challenging
_PyOptimizer_Optimize
, I'm still trying to figure out how to achieve its functionality in generated interpreter. To remove the "artificial boundaries", should we just remove this API call and replace its code in theJUMP_BACKWARD
,_EXIT_TRACE
and_DYNAMIC_EXIT
instruction cases?_PyOptimizer
APIs #126599