TL;DR
AgRefactor is an automated, LLM-powered multi-agent workflow designed to bridge the gap between standard software code and hardware-synthesizable High-Level Synthesis (HLS) designs. By combining a self-evolving long-term memory system with existing rule-based refactoring tools, it achieves state-of-the-art refactoring success rates on complex codebases while delivering up to a 6.51× performance speedup via automated structural code optimization.
What Problem Does This Paper Address?
High-Level Synthesis (HLS) allows developers to accelerate applications by writing hardware descriptions in C/C++ rather than low-level Register-Transfer Level (RTL) languages like Verilog. However, standard software code frequently relies on constructs that HLS compilers cannot synthesize, such as dynamic memory allocation (malloc), pointer-to-pointer operations, and Standard Template Library (STL) containers.
Manual refactoring to eliminate these constructs can take domain experts days. Existing rule-based automated tools lack generalizability and fail on complex data structures, while pure Large Language Model (LLM) approaches suffer from randomness, high token costs, and an inability to handle long, real-world programs.
Methods
AgRefactor introduces a hybrid, multi-agent architecture that coordinates code analysis, refactoring, and performance optimization.
Key innovations include:
- Self-Evolving Memory System: The workflow accumulates experience from successful and failed compilation trials. It stores these as tuples containing the program, identified incompatibilities, strategies, and generalized critiques. When facing a new program, it uses a specialized distance metric—blending raw code embeddings with error-type embeddings—to retrieve the most relevant past strategies.
- Tool Integration & The "Tool Specialist": To minimize LLM computational overhead, AgRefactor first attempts to use HeteroRefactor (a fast, deterministic rule-based tool). If that fails, a specialized LLM agent rewrites only the problematic code constructs to make them compatible with the rule-based tool, combining AI flexibility with algorithmic efficiency.
- Performance Optimization Agent: Once the code is synthesizable, a downstream agent uses a tree-based working memory to explore optimizations. It systematically applies structural rewrites, analyzes scheduling reports to resolve critical-path bottlenecks, and tunes compiler pragmas.
Datasets
The authors evaluated AgRefactor on a diverse suite of 45 benchmarks, split into 24 for training (memory accumulation) and 21 for testing. The suite includes LeetCode problems alongside complex, real-world software repositories such as libsodium, minimap2, libjpeg-turbo, and av1 codecs. These benchmarks feature up to 1,266 lines of code—5× to 10× longer than codebases evaluated in prior HLS refactoring research.
For the optimization agent, they utilized an extended suite of 51 open-source HLS designs spanning StreamHLS, Prometheus, SoDA, HLSFactory, and official Vitis Libraries.
Key Insights and Ideas
- The Hybrid Sweet Spot: Pure LLM refactoring is flexible but expensive and erratic; rule-based tools are rigid but fast and deterministic. Using an LLM agent as a "preprocessor" to make code compatible with a rule-based tool yields a faster, cheaper, and more reliable pipeline than either method alone.
- Structure Trumps Pragmas: Traditional HLS optimization focuses heavily on tuning compiler directives (pragmas like
pipelineorunroll). AgRefactor demonstrates that structural algorithmic changes—such as replacing an $O(n^2)$ sort with a radix sort—yield far greater hardware performance leaps. - Error-Aware RAG: Standard Retrieval-Augmented Generation (RAG) often fails in coding tasks because syntactic code similarity does not correlate with compilation error similarity. AgRefactor’s dual-embedding distance metric ensures the agent learns from logically relevant past failures.
Key Results
- Refactoring Success: AgRefactor matched or outperformed existing state-of-the-art tools on 9 out of 11 complex benchmarks.
- Memory Gains: The self-evolving memory mechanism boosted the refactoring success rate by 82% compared to a no-memory baseline when using GPT-5-mini.
- Hardware Speedups: The optimization agent achieved a 6.51× geometric mean speedup over AutoDSE (a state-of-the-art pragma tuning tool) by successfully executing structural code modifications. It also achieved up to a 1.41× speedup over already-optimized open-source designs.
- Efficiency: The tool-integrated workflow reduced the average refactoring runtime from 10 minutes (pure LLM flow) to just 4 minutes, significantly lowering API token costs.
Limitations and Future Work
- Distribution Shifts at Scale: On extremely large files (>1,000 lines of code), the effectiveness of the memory system degrades because the code introduces significant distribution shifts relative to the stored training tasks.
- Testbench Dependability: The framework relies on LLM-generated testbenches for verification. While the authors introduced an engineer-rater loop to filter out weak testbenches, ensuring 100% functional correctness remains an orthogonal challenge.
- Future Directions: The authors suggest that instead of just modifying code to fit existing rule-based tools, future workflows should empower LLM agents to directly modify or extend the capabilities of the underlying compilation tools themselves.
Summary updated