direct unit tests for compiler optimisations · Issue #405 · faster-cpython/ideas (original) (raw)

The compiler's optimisation steps are unit tested only indirectly, because we don't have a way to call them from python. So things like test_peepholer input some python code, and check for evidence of the optimisation in the bytecode that it is compiled to. As the compiler changes, the intention of the test may no longer be fulfilled (the input to the optimiser is no longer what we intended it to be), but we wouldn't know. And when we want to write a test to cover an input that we don't know how to make the compiler generate, we can't do it.

The idea here is to create a python API that allows us to input a sequence of bytecode instructions from a unit test, and get the instructions after the optimisations. The test harness would need to construct basicblocks from the instruction sequence (because that's what the optimiser wants) and to convert the blocks in the output back to an instruction sequence.

We can construct similar test for the AST optimiser (currently I believe it just does const folding, but it could do more).