Stochastic superoptimization

Eric Schkufza, Rahul Sharma, Alex Aiken

TL;DR

STOKE is a "stochastic superoptimizer" that uses Markov Chain Monte Carlo (MCMC) sampling to find the most efficient X86 code sequences possible. By trading away the guarantee of finding every possible optimization for a probabilistic search, it can optimize much longer code sequences than previous tools, often outperforming production compilers like gcc and even expert-written assembly.

What Problem Does This Paper Address?

Superoptimization is the "holy grail" of code generation: given a program, find the absolute shortest or fastest sequence of instructions that performs the same task. Traditional compilers (like gcc or llvm) use a pipeline of heuristics to solve sub-problems like register allocation and instruction selection. However, these sub-problems are mutually dependent, and solving them separately often misses the "global optimum."

Existing superoptimizers attempt to solve this by exhaustively enumerating every possible combination of instructions. Because the search space grows exponentially, these tools are typically limited to sequences of about 10–12 instructions. The authors of this paper tackle this "length barrier" by reframing superoptimization as a stochastic search problem rather than an exhaustive one.

Methods

The authors introduce STOKE, a tool that treats program optimization as a cost-minimization problem.

  1. MCMC Sampling: Instead of checking every program, STOKE uses a Markov Chain Monte Carlo (MCMC) sampler (specifically the Metropolis-Hastings algorithm). It starts with a program and makes random "moves"—changing an opcode, swapping two instructions, or changing an operand.
  2. The Cost Function: To guide the search, STOKE uses a cost function that balances two terms:
  3. Correctness ($eq$): Instead of a binary "correct/incorrect," STOKE measures "how correct" a program is by running it on test cases and calculating the Hamming distance (bit-level difference) between its output and the target's output. This creates a "smooth" landscape that allows the search to hill-climb toward a correct solution.
  4. Performance ($perf$): A heuristic based on the sum of the latencies of the instructions used.
  5. Two-Phase Search:
    • Synthesis Phase: Starts from a random sequence of instructions and ignores performance to find any program that is functionally equivalent to the target.
  6. Optimization Phase: Starts from a known correct program (like the output of a compiler) and searches for faster versions while maintaining correctness.
  7. Formal Verification: Once the stochastic search finds a promising candidate, STOKE uses an SMT solver (STP) to formally prove that the new code is equivalent to the original for all possible inputs.

Datasets

The authors evaluated STOKE on a suite of 25 benchmark kernels:

  • Hacker’s Delight: A famous collection of complex bit-manipulation algorithms (e.g., rounding to the next power of two, computing parity).
  • OpenSSL: Specifically the Montgomery multiplication kernel used in cryptography.
  • BLAS: The SAXPY (Single-precision Alpha X Plus Y) vector operation.
  • Linked List Traversal: A basic data structure benchmark.

Key Insights and Ideas

  • "Almost Correct" is a Signal: The most profound insight is that a program that is 90% correct (in terms of output bits) is often "closer" in the search space to a 100% correct program than a totally random one. This allows the MCMC sampler to find its way through a massive search space that would be impossible to enumerate.
  • Relaxing the "Plumbing": The authors found that the search performs better if it doesn't penalize a program for putting the correct answer in the wrong register. By allowing "improved equality" (checking if the answer exists anywhere in the registers), the algorithm can focus on finding the logic first and fixing the register assignments later.
  • Algorithmic Discovery: STOKE doesn't just rearrange instructions; it can discover entirely different algorithms. For example, it transformed a 41-line unoptimized sequence into a 5-line sequence using X86 conditional move instructions that the production compilers failed to utilize.

Key Results

  • Beating Expert Assembly: In the Montgomery multiplication benchmark, STOKE produced code that was 1.6x faster than gcc -O3 and even slightly faster than the expert-handwritten assembly in the OpenSSL repository.
  • Vectorization: For the SAXPY benchmark, STOKE automatically discovered how to use SSE vector instructions to process multiple data points at once, even though the input was a simple scalar loop.
  • Scaling: While previous superoptimizers hit a wall at 12 instructions, STOKE successfully reasoned about and optimized sequences that were originally over 100 lines long (by focusing on loop-free fragments).

Limitations and Future Work

  • Loop-Free Constraint: The current version of STOKE is primarily limited to loop-free code. While it can optimize the "body" of a loop, it cannot yet propose structural changes to the loops themselves (like unrolling or tiling) stochastically.
  • Deceptive Landscapes: Some functions are "all-or-nothing" (e.g., a single boolean output). In these cases, the Hamming distance doesn't provide a smooth hill-climbing signal, and the search reverts to something closer to a random walk.
  • Memory Aliasing: The tool makes some simplifying assumptions about memory and the stack to keep the formal verification tractable, which might limit its use in very complex systems code.

Summary updated

BibTeX
@inproceedings{schkufza2013stochastic,
  author = {Eric Schkufza and Rahul Sharma and Alex Aiken},
  title = {{Stochastic superoptimization}},
  booktitle = {ASPLOS},
  year = {2013},
  doi = {10.1145/2451116.2451150},
}
Figure