RTLRewriter: Methodologies for Large Models aided RTL Code Optimization

Xufeng Yao, Yiwen Wang, Xing Li, Yingzhao Lian, Ran Chen, Lei Chen, Mingxuan Yuan, Hong Xu, Bei Yu

TL;DR

RTLRewriter is a novel framework that uses Large Language Models (LLMs) to automate the optimization of hardware design code (RTL). By combining circuit partitioning, multi-modal analysis of circuit diagrams, and a cost-aware search algorithm, it significantly outperforms traditional compilers and raw LLMs in reducing circuit area and delay.

What Problem Does This Paper Address?

In chip design, Register Transfer Level (RTL) optimization is the "early stage" where engineers refine Verilog or VHDL code to improve Power, Performance, and Area (PPA). Currently, this is a grueling manual process: expert engineers look at synthesis reports, identify bottlenecks, and rewrite code snippets iteratively.

While automated compilers (like Yosys) exist, they rely on rigid, rule-based transformations that often struggle with complex logic or high-level design patterns. On the other hand, while LLMs are great at coding, they face three major hurdles in hardware design:

  1. Context Limits: Real-world circuits are too massive for an LLM's memory.
  2. Lack of Visual Intuition: Compilers only "read" text, whereas human engineers often use circuit diagrams to spot optimization opportunities.
  3. Verification Costs: LLMs can "hallucinate" incorrect logic, and formally proving that the new code matches the old one (Equivalence Checking) is computationally expensive.

Methods

RTLRewriter introduces a modular pipeline to bridge the gap between human expertise and AI automation:

  • Circuit Partitioning: To handle large designs, the framework uses an XGBoost-based predictor to estimate synthesis time and breaks the circuit into smaller, manageable sub-circuits.
  • Multi-modal Program Analysis: This is a standout feature. It uses a Large Multi-modal Model (LMM) to analyze both the RTL code and its corresponding visual circuit diagram. This allows the AI to "see" critical paths and structural bottlenecks just like a human engineer would.
  • Retrieval-Augmented Generation (RAG): A specialized search engine retrieves relevant optimization "recipes," algorithms (like FSM state reduction), and similar code snippets from a curated database to guide the LLM.
  • Cost-aware Monte Carlo Tree Search (C-MCTS): Since LLM API calls are expensive and slow, C-MCTS intelligently explores different rewriting strategies, prioritizing paths that offer the best PPA improvement for the lowest "inference cost."
  • Fast Verification Pipeline: To avoid the "NP-complete" nightmare of formal SAT solvers, the system first uses fuzz testing (random inputs) to quickly filter out buggy code. Only the most promising, functionally correct candidates proceed to formal equivalence checking.

Datasets

The authors introduced two new benchmarks to evaluate the system:

  1. Small Rewriter Benchmark: 55 cases covering common patterns like MUX trees, adders, and Finite State Machines (FSMs).
  2. Large Rewriter Benchmark: 5 complex, real-world scenarios including CPU designs, neural network accelerators, and image processing units.

They compared RTLRewriter against industry-standard tools like Yosys and Egg (E-graphs), as well as state-of-the-art models like GPT-4 and Claude 3-Opus.

Key Insights and Ideas

  • Visuals are Cues: The paper demonstrates that LMMs provide more accurate optimization analysis when they can "see" the diagram. Textual code alone often hides the structural complexity of a circuit.
  • Partitioning is Non-Negotiable: Without partitioning, LLMs often fail to optimize entire designs correctly. Breaking the problem down not only fits the context window but also allows for parallel synthesis, speeding up the feedback loop by 10x.
  • Fuzzing as a Filter: One of the most practical insights is using fuzz testing as a "cheap" gatekeeper. By catching 90% of LLM errors with random simulation, they drastically reduced the time spent on heavy formal verification.

Key Results

  • Superior PPA: On short benchmarks, RTLRewriter achieved a geometric mean reduction in wires (0.69x) and cells (0.77x) compared to Yosys.
  • Complex Design Success: In the "Large" benchmark (e.g., CPU and CNN designs), it consistently outperformed all baselines, achieving an area ratio of 0.81x compared to the original code.
  • Efficiency: Thanks to the C-MCTS and fast verification, the framework is significantly faster than "brute-force" LLM prompting, which often gets stuck in repetitive or invalid code generation.
  • Beating the Experts: In some cases (like "case 14"), the model actually generated a more efficient implementation than the human-optimized version provided in the benchmark.

Limitations and Future Work

  • Database Dependency: The RAG component relies on a high-quality database of optimization patterns. If a design pattern isn't in the database, the model's effectiveness may drop.
  • Stochastic Nature: Despite the MCTS, LLMs are still probabilistic. There is no 100% guarantee that the optimal rewrite will be found in every run.
  • Future Directions: The authors suggest automating the creation of the optimization database and expanding the framework to handle even more diverse industrial hardware description languages beyond Verilog.

Summary updated

BibTeX
@inproceedings{yao2024rtlrewriter,
  author = {Xufeng Yao and Yiwen Wang and Xing Li and Yingzhao Lian and Ran Chen and Lei Chen and Mingxuan Yuan and Hong Xu and Bei Yu},
  title = {{RTLRewriter: Methodologies for Large Models aided RTL Code Optimization}},
  booktitle = {ICCAD},
  year = {2024},
  doi = {10.1145/3676536.3676775},
}

Loading related content...

Figure