C2HLSC: Leveraging Large Language Models to Bridge the Software-to-Hardware Design Gap

Luca Collini,Siddharth Garg,Ramesh Karri
2024-11-30
Abstract:High-Level Synthesis (HLS) tools offer rapid hardware design from C code, but their compatibility is limited by code constructs. This paper investigates Large Language Models (LLMs) for automatically refactoring C code into HLS-compatible formats. We present a case study using an LLM to rewrite C code for NIST 800-22 randomness tests, a QuickSort algorithm, and AES-128 into HLS-synthesizable C. The LLM iteratively transforms the C code guided by the, implementing functions like streaming data and hardware-specific signals. With the hindsight obtained from the case study, we implement a fully automated framework to refactor C code into HLS-compatible formats using LLMs. To tackle complex designs, we implement a preprocessing step that breaks down the hierarchy in order to approach the problem in a divide-and-conquer bottom-up way. We validated our framework on three ciphers, one hash function, five NIST 800-22 randomness tests, and a QuickSort algorithm. Our results show a high success rate on benchmarks that are orders of magnitude more complex than what has been achieved generating Verilog with LLMs.
Hardware Architecture,Software Engineering
What problem does this paper attempt to address?
### Problems the paper attempts to solve This paper aims to solve the compatibility problems in the conversion process from C code to Hardware Description Language (HDL). Specifically, the paper explores how to use large - language models (LLMs) to automatically refactor C code so that it can be compatible with High - Level Synthesis (HLS) tools. HLS tools can quickly generate hardware designs from C code, but their compatibility with C code is limited, especially for certain specific code structures. This results in the need for manual adjustment and refactoring of C code to meet the requirements of HLS tools in practical applications, which is both time - consuming and error - prone. ### Main contributions 1. **Automation process**: - **Support hierarchical design**: Automatically build unit tests from top - level tests. - **Function rewriting**: Support the rewriting of streaming functions. - **Generate optimization instructions**: Support the generation of optimization instructions (such as pragmas) for area or throughput targets. - **Cost control**: Use in - context learning and intelligent model selection to reduce costs. 2. **Experimental evaluation**: - Use real - world application benchmarks for evaluation, including the implementation of encryption algorithms and NIST randomness tests. ### Method overview 1. **Background introduction**: - **High - Level Synthesis (HLS)**: Introduces the capabilities and limitations of HLS tools, including unsupported features such as dynamic memory allocation, recursion, and pointers. - **Large - language models (LLMs)**: Discusses the advantages of LLMs in code generation and translation, especially in widely used programming languages such as C, C++, and Python. 2. **Related work**: - Outlines previous work on using LLMs to design hardware, including Verigen, ChipChat, AutoChip, etc. - Emphasizes the uniqueness of the method in this paper, that is, using HLS tools by generating synthesis - compatible C code instead of directly generating HDL code. 3. **Case study**: - **Interactive method**: Use the Gemini LLM to refactor the C code of NIST 800 - 22 randomness tests, QuickSort algorithm, and AES - 128 encryption to make it suitable for HLS tools. - **Methodology**: Details each step, including removing print statements, rewriting streaming interfaces, optimizing data types, etc. - **Results**: Shows the performance of LLM in different tasks, including successful and failed cases. 4. **Fully automated C2HLSC framework**: - **Pre - processing step**: Handle hierarchical design and decompose multi - function tasks into single - function tasks. - **Dual - feedback loop**: Ensure that the generated code can be both compiled successfully and synthesized successfully by HLS tools. - **Optimization instruction generation**: After the code is synthesizable, apply optimization instructions (such as pragmas). ### Conclusion This paper significantly reduces the time and effort required for manual code adjustment to adapt to HLS tools by using LLMs to automatically refactor C code. The experimental results show that this method has a high success rate in handling complex designs, demonstrating the great potential of LLMs in hardware design.