GH-135904: Add tests for the JIT build process by brandtbucher · Pull Request #136766 · python/cpython
This is overdue, but it's especially valuable now that we're doing more subtle transformations on the machine code at JIT build time. This has a number of benefits:
- When we add a new optimization at this level, we can directly see the impact on example code in the PR diff. And if it doesn't show up, we can add a test for it.
- It gives us visibility into the quality of the generated code (without having the full stencils checked in yet). For example, making this PR helped me realize that there are three obvious quality issues on platforms I don't look at much:
- On some platforms, system headers were putting unused writable data in our read-only data stencils. This is both wrong and wasteful. I fixed this issue as part of this PR (we just optimistically remove this data, and raise if it's actually used).
- The JIT still inserts frame pointer pushes/pops on Intel-based Macs. I'll fix this in a follow-up PR.
- LLVM has a bug that inserts unnecessary spills and reloads of C local variables on 32-bit Windows at the beginning and end of every stencil. I've filed an issue upstream:
musttaildoesn't optimize sibling calls llvm/llvm-project#147813
The new test is in Lib/test/test_jit_stencils.py. This test uses Tools/jit/test/test_executor_cases.c.h to generate a few small test stencils, and compares them to the expected output in the corresponding Tools/jit/test/test_jit_stencils-*.h file.
Most of the changes in Tools/jit/_targets.py are just cleanup to make the generated stencils simpler and more consistent, or to fix the writable-data bug I mentioned above.