The Challenge of Predicting CPU Performance
To optimize code effectively, compilers and performance engineers need to know exactly how many clock cycles a processor takes to execute a specific sequence of instructions (a "basic block"). However, modern x86-64 processors are incredibly complex. They don't just execute instructions one by one; they translate them into "micro-ops," fuse them together, execute them out-of-order, and rename registers on the fly.
Traditionally, the industry has relied on analytical models—hand-written programs that attempt to simulate these hardware internals. Tools like Intel's IACA or LLVM's llvm-mca are standard, but they are a nightmare to maintain. They are tedious to write, prone to human error, and often based on incomplete or incorrect vendor documentation. Furthermore, every time a new processor generation (like Skylake or Haswell) is released, the model must be painstakingly rebuilt from scratch.
Enter Ithemal: A Data-Driven Approach
Ithemal is the first deep-learning-based tool for predicting the throughput of assembly code blocks. Instead of trying to manually model the hardware, Ithemal treats processor performance as a regression problem, "learning" the hardware's behavior by observing millions of examples of code and their actual execution times.
The core architecture relies on a Hierarchical Multiscale RNN:
- Canonicalization: Assembly code is cleaned and tokenized (e.g.,
mul ecxbecomes a structured list of opcode and operands). - Token Layer: Each token is mapped to a high-dimensional vector (embedding).
- Instruction Layer: An LSTM processes the tokens within a single instruction to create a single "instruction embedding."
- Prediction Layer: A second LSTM processes the sequence of these instruction embeddings for the entire block.
- Output: A final linear layer predicts the throughput value.
This hierarchical structure is key: it mirrors the natural syntax of code and keeps the neural network's backpropagation path short and efficient.
Training and Key Insights
The researchers built a massive dataset by extracting over 1.4 million unique basic blocks from benchmark suites (SPEC2006, SPEC2017), Linux shared libraries, and applications like Firefox. Ground truth throughput was measured by executing each block in a loop on real hardware to reach a "steady state."
Training Ithemal on this data revealed several fascinating insights:
- Learning "Hidden" Rules: Ithemal successfully learned hardware optimizations that analytical models missed. For example, it correctly predicted the speedup of "zeroing idioms" (where
xor eax, eaxis executed faster than a normal XOR) simply by observing the pattern in the data. - Sequence Over Graph: The authors tested a DAG-RNN (which processes instructions based on their data dependency graph). Surprisingly, the sequential LSTM performed better. This suggests that the literal order of instructions in memory matters for performance—likely due to the CPU's front-end fetch and decode logic—even if the dependencies are identical.
- Portability is "Free": To support a new CPU architecture, Ithemal doesn't require a human to read a 1,000-page manual. You simply run the timing script on the new hardware and retrain the model.
Key Results
Ithemal achieves more than double the accuracy of industry-standard analytical models while remaining fast enough for use in production compilers.
- Superior Accuracy: Ithemal reduced the Mean Absolute Percentage Error (MAPE) by over 50% compared to state-of-the-art tools like Intel’s IACA and LLVM’s
llvm-mca. As seen in the heatmaps above, Ithemal's predictions tightly hug the diagonal (perfect accuracy), whereas analytical models show significant spread and systematic errors. - High Correlation: It achieved a Spearman rank correlation of 0.96, meaning it is exceptionally good at telling a compiler which of two code sequences is faster.
- Production Speed: Ithemal can process roughly 560 instructions per second, making it comparable in speed to analytical tools and roughly 40x faster than actually measuring the code on a real CPU.
Limitations and Future Work
While Ithemal is a major step forward, it currently focuses on straight-line code (basic blocks) and does not account for branches, jumps, or complex control flow. Additionally, the model assumes all data is in the L1 cache, ignoring the performance impact of L2/L3 cache misses or main memory latency. Finally, Ithemal predicts the performance of a single thread in isolation, without accounting for resource contention in multi-core environments. Future work aims to extend the model to handle larger code structures and more complex memory access patterns.
Summary updated