Compiler Auto-Vectorization with Imitation Learning

Charith Mendis, Cambridge Yang, Yewen Pu, Saman P. Amarasinghe, Michael CarbinNeurIPS 2019

TL;DR

This paper introduces "Vemal," a novel approach to compiler auto-vectorization that uses imitation learning to mimic the optimal decisions of an Integer Linear Programming (ILP) solver. Vemal, powered by a Gated Graph Neural Network, significantly outperforms traditional compiler heuristics in terms of static cost reduction and achieves competitive runtime speedups, demonstrating the potential of learning-based methods for complex compiler optimizations.

What Problem Does This Paper Address?

Modern processors leverage Single Instruction, Multiple Data (SIMD) or vector instructions to speed up computations by processing multiple data items in parallel. Compilers use auto-vectorization to automatically transform scalar code into vector code, exploiting this parallelism. A key technique is Superword Level Parallelism (SLP) vectorization, which groups independent and isomorphic scalar instructions into vector instructions. However, current compiler heuristics for SLP vectorization are often suboptimal. While an ILP-based approach (goSLP) has shown superior results, it's computationally expensive. This paper addresses the challenge of achieving the performance benefits of the ILP-based approach without its computational cost, by learning to imitate its optimal decisions.

Methods

The core of this work is the development of "Vemal," a system that learns to perform SLP vectorization. The problem is framed as a Markov Decision Process (MDP), where the agent sequentially makes decisions to form vector packs. A Gated Graph Neural Network (GGNN) is used to model the policy network, taking the current state of the vectorization process (represented as a graph) as input.

The training process involves two stages:

  1. Supervised Pre-training: The ILP solver (goSLP) generates optimal packing decisions for functions. These optimal trajectories are used to train the GGNN policy using cross-entropy loss.
  2. Imitation Learning with DAGGER: The trained GGNN policy is further refined using the DAGGER algorithm. This involves sampling rollouts from the current policy, using the ILP solver to provide optimal actions for states encountered during these rollouts, and augmenting the training data. This iterative process allows the policy to learn from its mistakes and improve its imitation of the ILP solver.

The state representation for the GGNN is a graph that encodes instructions, potential packs, dependencies, and the current "focus" instruction being considered for packing.

Datasets

The paper uses benchmark programs from established suites:

  • SPEC2006fp C/C++
  • SPEC2017fp C/C++
  • NAS benchmark suites

These benchmarks are compiled to LLVM's intermediate representation (IR) just before the existing SLP vectorizer runs. The dataset is partitioned into training and testing sets, with a focus on functions that are vectorizable by goSLP to mitigate dataset imbalance. The paper also explores function partitioning at 100 and 200 instruction counts to handle large functions and scaling issues with graph neural networks.

Key Insights and Ideas

  • Learning Optimal Compiler Decisions: The paper demonstrates that imitation learning can effectively capture complex, optimal decision-making processes for compiler optimizations, matching the performance of computationally expensive ILP solvers.
  • GGNNs for Code Representation: Gated Graph Neural Networks are shown to be effective in encoding the structural information of code for vectorization tasks, leveraging graph representations of program states.
  • Trade-off between Optimality and Learnability: Function partitioning, while necessary to handle large codebases, introduces sub-optimality compared to solving for the entire function. This highlights a trade-off between the complexity of the problem and the ability of the neural network to learn an accurate policy.
  • Inaccuracies in Cost Models: The results suggest that LLVM's static cost model might not perfectly align with actual runtime performance, as Vemal sometimes achieves better runtimes with strategies that are suboptimal according to the cost model.

Key Results

  • Static Cost Reduction: Vemal achieves an average static cost reduction that is 22.6% higher than the LLVM compiler's SLP vectorizer, as measured by LLVM's own cost model.
  • Runtime Performance: Vemal achieves a geometric mean runtime speedup of 1.015x on the NAS benchmark suite compared to LLVM SLP.
  • Comparison to goSLP: Vemal matches the runtime performance of the goSLP (ILP-based) solution in 5 out of 7 applications in the NAS benchmark suite.
  • Forward vs. Backward Traversal: Agents using a forward instruction traversal policy generally learn better vectorization policies than those using backward traversal.
  • Impact of Rollouts: Using multiple rollouts (10 vs. 1) during inference generally improves the performance of the learned agents.

Limitations and Future Work

  • Function Partitioning Sub-optimality: The paper acknowledges that partitioning functions leads to sub-optimal solutions compared to optimizing the entire function at once. This is attributed to the GGNN's difficulty in approximating goSLP's packing policy for larger graphs.
  • LLVM Cost Model Limitations: The authors note that LLVM's cost model may not perfectly reflect runtime performance, as Vemal sometimes finds better runtime strategies that are suboptimal according to the cost model.
  • Future Work: The paper suggests that a reinforcement learning approach with a better cost model could potentially lead to even more effective end-to-end vectorization policies. Further research into improving the GGNN's ability to handle larger graphs and more complex optimization problems is also implied.

Summary updated

BibTeX
@inproceedings{mendis2019compiler,
  author = {Charith Mendis and Cambridge Yang and Yewen Pu and Saman P. Amarasinghe and Michael Carbin},
  title = {{Compiler Auto-Vectorization with Imitation Learning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2019},
  url = {https://openreview.net/forum?id=XFyVgY8gAT},
}
Figure