Automated C/C++ Program Repair for High-Level Synthesis via Large Language Models

Kangwei Xu, Grace Li Zhang, Xunzhao Yin, Cheng Zhuo, Ulf Schlichtmann, Bing Li

Introduction

High-Level Synthesis (HLS) has revolutionized hardware design by allowing developers to use C/C++ instead of low-level Hardware Description Languages (HDLs) like Verilog. However, a significant gap remains: HLS tools only support a restricted "synthesizable" subset of C++. Standard software constructs such as recursion, dynamic memory allocation, and complex pointer arithmetic are incompatible with fixed hardware architectures.

The traditional HLS development workflow is manual, iterative, and requires significant hardware expertise.

Currently, bridging this gap requires manual code refactoring by experts—a process that is both time-consuming and prone to error. While Large Language Models (LLMs) show promise in general code generation, they often struggle with the niche constraints of hardware synthesis. This research introduces an automated framework that leverages LLMs and Retrieval-Augmented Generation (RAG) to reliably "repair" and optimize C/C++ code for HLS.

Addressing HLS Incompatibilities

The primary challenge in HLS is transforming software-centric code into hardware-compatible structures. Common incompatibilities include the use of pointers-to-pointers, variable-length arrays, and recursive functions. These features do not map directly to hardware gates and must be refactored into loops or static array accesses.

Table I: Examples of common HLS-incompatible types, their symptoms in compiler reports, and corresponding repair strategies.

To automate these repairs, the proposed framework utilizes a five-stage pipeline:

  1. Fault Localization: Identifying errors using HLS compilers and LLM-based scans.
  2. RAG-Driven Repair: Using a library of correction templates derived from HLS manuals to guide the LLM, preventing hallucinations and ensuring adherence to hardware constraints.
  3. Bit Width Optimization: The LLM generates profiling scripts to analyze data ranges, replacing generic 32/64-bit types with bit-accurate types (e.g., ac_int<9>) to save hardware resources.
  4. Equivalence Verification: Ensuring the logic remains identical through RTL synthesis and simulation.
  5. PPA Optimization: Automatically inserting HLS pragmas (like #pragma hls_pipeline) to improve Power, Performance, and Area.

Key Insights and Results

The framework was evaluated on 24 real-world applications, ranging from data structures like hash tables to complex algorithms like AES encryption and Dijkstra’s algorithm.

  • RAG as a Safeguard: Grounding the LLM in official documentation via RAG increased the repair success rate by 23% compared to vanilla LLM approaches. This highlights the necessity of domain-specific knowledge for specialized engineering tasks.
  • Cost-Effective Hybrid Repair: To manage computational costs, the framework first employs traditional regex-based scripts for simple fixes before utilizing the LLM for complex structural repairs, reducing operational costs by approximately 21%.
  • Hardware Efficiency Gains: Automated bit-width optimization led to substantial improvements in hardware metrics, reducing area by 36.57%, power by 33.03%, and the minimum clock period by 29.08% on average.

Conclusion

This work demonstrates that LLMs, when properly constrained by RAG and integrated into a verification pipeline, can significantly automate the transition from software to hardware. By reducing the need for manual intervention and deep hardware expertise, this framework lowers the barrier to entry for HLS and improves the efficiency of the resulting hardware designs. Future work will focus on scaling the system for larger projects with complex dependencies and exploring multi-agent architectures for more robust repair and optimization.

Summary updated

BibTeX
@inproceedings{xu2024automated,
  author = {Kangwei Xu and Grace Li Zhang and Xunzhao Yin and Cheng Zhuo and Ulf Schlichtmann and Bing Li},
  title = {{Automated C/C++ Program Repair for High-Level Synthesis via Large Language Models}},
  booktitle = {MLCAD},
  year = {2024},
}
Figure