In the past few weeks I’ve been looking at the JIT implementation a little bit more closely. As part of this investigation I wanted to “see” what assembly code was generated that matched the bytecode instruction.
Using capstone, I patched the _PyJIT_Compile function to print the mapping to stdout at runtime. It is something like:
This is a quirky way to do it, but I was wondering if such functionality would be useful at all.
If we were to implement it, one possible way to do it is similarly to the dis module (or even extend the dis module).
Very cool! It would definitely be useful for debugging the JIT itself, I think. Your implementation seems very similar to our use of the PYTHON_LLTRACE environment variable for debugging the tier one interpreter and tier two optimization pipeline.
This also reminds me of a PR (that I forgot about!) by @tonybaloney allowing one to obtain byte strings of the compiled code from pure-Python (at least, for those who know where to look, using secret APIs designed for tests). It looks like capstone has a Python API that users could simply pass these strings to to obtain objects representing the assembly code… super cool! So perhaps that could be a way forward here, without introducing a hard dependency on capstone in CPython itself.
Separately: feel free to reach out if you have any questions or comments about the JIT.
Thanks for the pointers, very useful indeed! If there is interest then I’ll try to put together a draft PR at some point and I’ll ping you here for review/feedback.
Hm, this might be a dealbreaker for me personally. I’m developing for all of the JIT’s platforms, so I’m not really eager to use different libraries on each one.
I think at this point probably the only action item is for @tonybaloney to open his PR against main so JIT maintainers can use Python libraries like capstone or dissy for debugging at runtime.
I’m hesitant to start adding a bunch of other infrastructure and utilities for this, since it’s an experimental feature (and I can easily count on one hand the number of people who would actually use it).