Code Transpilation for Hardware Accelerators

Yuto Nishida, Sahil Bhatia, Shadaj Laddad, Hasan Genc, Yakun Sophia Shao, Alvin CheungarXiv 2023 · DOI

Introduction

As the industry moves toward specialized computing to meet the demands of artificial intelligence and big data, the gap between legacy software and modern hardware continues to widen. While general-purpose CPUs are being supplemented or replaced by Domain-Specific Languages (DSLs) and hardware accelerators like Gemmini, a massive volume of legacy code—written in languages such as Fortran and C++—remains incompatible with these new architectures. Traditionally, bridging this gap required manual rewriting, a process that is notoriously slow, error-prone, and requires rare expertise in both the application domain and specific hardware Instruction Set Architectures (ISAs).

The Challenge of Legacy Code

The primary obstacle to automating this translation is the brittleness of current compilers. Most existing tools rely on "pattern matching," where the compiler looks for specific code structures to replace with hardware-optimized instructions. If the source code is structured even slightly differently than the expected pattern, the compiler fails to optimize it. This leaves developers with the burden of manual translation, which introduces the risk of "Heisenbugs"—subtle bugs that are difficult to detect and resolve in hardware-level code.

Methodology: Verified Lifting with Metalift and Gemmini

Researchers at UC Berkeley have proposed a novel solution by integrating Metalift, a verified lifting framework, with Gemmini, a generator for Deep Neural Network (DNN) accelerators. This approach moves away from rigid, rule-based compilation toward a search-based synthesis model.

The Pipeline

The translation process follows a rigorous formal pipeline:

  1. Grammar and Semantics Definition: The user defines the "building blocks" of the target hardware (e.g., matrix multiplication or convolution instructions) and provides a mathematical description of their behavior.
  2. Symbolic Analysis: Metalift analyzes the source code (Python or LLVM) to create a symbolic expression representing its functional behavior.
  3. Synthesis and Verification: Instead of following fixed rules, Metalift searches through a grammar of possible target programs. It uses an SMT (Satisfiability Modulo Theories) solver to prove that a candidate program is mathematically equivalent to the source code.
  4. Loop Invariants: To handle complex iterations, the system automatically generates loop invariants—logical proofs ensuring the code remains correct across any number of iterations.

Key Results and Insights

The prototype evaluation demonstrated that this search-based approach is both efficient and remarkably simple to implement.

  • Translation Efficiency: The system successfully translated a C++ sliding window sum—a common kernel in image processing—into Gemmini-optimized instructions in less than one minute.
  • Implementation Simplicity: The compiler for this specific translation was implemented in fewer than 100 lines of code (LOC), highlighting the power of the underlying Metalift framework to handle the complex logic of synthesis and verification.
  • Discovery over Pattern Matching: A significant finding was the system's ability to identify that a standard C++ loop was functionally equivalent to a specialized Gemmini convolution instruction, even when the source code contained no explicit references to convolution. This demonstrates that the system can discover optimizations that pattern-matching compilers would miss.

Discussion and Future Work

By treating code translation as a search problem backed by formal verification, this framework ensures that the resulting code is not only high-performing but also provably correct. This eliminates the need for extensive manual debugging and allows for a more agile transition to new hardware architectures.

However, several areas for improvement remain:

  • Hardware-Software Co-Design: Future iterations could allow the system to search for optimal hardware parameters (e.g., scratchpad memory size) alongside the code synthesis.
  • Performance Modeling: To handle more complex searches, the researchers aim to implement "proxy cost models" to quickly estimate the performance of candidate programs without full execution.
  • Scaling: While successful on small kernels, the next challenge is scaling this approach to the massive, multi-million-line codebases found in production scientific computing environments.

Conclusion

The integration of Metalift and Gemmini represents a significant step toward automating the modernization of legacy software. By leveraging program synthesis and formal methods, researchers are providing a path to utilize the full potential of hardware accelerators without the prohibitive cost of manual software re-engineering.

Summary updated

BibTeX
@misc{nishida2023code,
  author = {Yuto Nishida and Sahil Bhatia and Shadaj Laddad and Hasan Genc and Yakun Sophia Shao and Alvin Cheung},
  title = {{Code Transpilation for Hardware Accelerators}},
  year = {2023},
  eprint = {2308.06410},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2308.06410},
}
Figure