A code automatic optimization method based on RISC-V and multi-agent
Patent Information
- Application Number
- CN202611464810.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-20
- Publication Date
- 2026-10-23
AI Technical Summary
[0007]然而,现有方法的研究对象多为通用编程任务或高性能计算场景,对于RISC-V及其向量扩展场景而言,代码优化不仅需要理解仓库级上下文,还必须显式纳入指令语义、访存模式及编译运行反馈等领域知识;而针对真实数据库内机器学习工程代码,现有方法在复杂调用链、跨模块上下文提取以及面向性能结果的稳定迭代优化方面仍缺乏针对性设计
[0060]本发明提供的基于RISC-V与多智能体的代码自动优化方法,通过多智能体协同机制与领域知识约束相结合的技术方案,在RISC-V平台数据库内机器学习代码的自动向量化场景中取得了显著的技术效果。实验结果表明,本发明在函数级向量化基准评估中,针对TSVC测试集中GCC编译器无法稳定自动向量化的代表性函数(包括s114、s2233、s261、s293等涵盖复杂访存、循环依赖、规约与控制流等典型代码模式的函数),均实现了稳定且显著的性能提升。
Smart Images

Figure CN122963522A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the interdisciplinary field of computer software optimization and processor architecture, specifically to an automatic code optimization method based on RISC-V and multi-agent architecture. Background Technology
[0002] As data volumes continue to grow and the demand for intelligent analysis continues to increase, intelligent computing processes such as machine learning and large-scale language model inference are gradually migrating to the database to eliminate the overhead of frequent data movement and format conversion in the traditional external database processing mode.
[0003] With its open-source, modular, and on-demand scalability features, the RISC-V instruction set architecture provides a scalable implementation platform for in-database machine learning systems. In particular, the RISC-V vector extension adopts a vector length-independent programming model and supports mechanisms such as straddle memory access, index memory access, masked execution, and register grouping, providing a flexible hardware foundation for data-level parallel computing in in-database machine learning.
[0004] However, the development of the RISC-V software ecosystem still faces problems such as insufficient software adaptation and the need for improved support of compilation toolchains. Different processor implementations differ in vector resource configuration, pipeline organization, and caching systems. This means that the optimization effect is closely related to the specific platform implementation and the capabilities of the compilation toolchain. It is necessary to conduct targeted research on the execution characteristics and optimization methods of the target platform.
[0005] In the performance optimization practice of machine learning systems within databases, while manual vectorization optimization can achieve good performance gains, the target code in real-world systems typically spans multiple files and modules, and is deeply bound to the database execution engine, data structure definitions, and upper-level call logic. The cost and difficulty of manual analysis increase dramatically with the size of the codebase. Automatic vectorization by the compiler, as an important means of lowering the development threshold, can handle relatively simple operations with well-structured code. However, when faced with complex control flows, cross-module dependencies, mixed data types, and non-contiguous memory access in real-world engineering code scenarios, its overly conservative dependency analysis often fails to consistently trigger effective vectorization transformations, resulting in significant limitations in optimization performance under complex engineering scenarios. Existing research indicates that automatic vectorization on the RISC-V platform is still susceptible to the effects of complex control flows, mixed data types, and non-contiguous memory access, making it difficult to consistently achieve results close to manual optimization in real-world complex source code.
[0006] In recent years, automatic code generation technology based on large language models has provided new ideas for solving the above problems. Related research has gradually expanded from general code generation to specialized scenarios oriented towards performance optimization. Some works explore the use of large language models to perform semantically equivalent rewriting of scalar code to improve the automatic vectorization effect of the underlying compiler. Other studies combine verification and test feedback to explore automatic generation and correctness guarantees for loop vectorization. In addition, multi-agent collaborative methods, through mechanisms such as role division, multi-round interaction, test feedback, and code search, decompose complex optimization tasks into controllable sub-problems, showing good potential in the fields of software development and code optimization.
[0007] However, existing methods are mostly applied to general programming tasks or high-performance computing scenarios. For RISC-V and its vector extension scenarios, code optimization not only requires understanding the repository-level context, but also requires explicitly incorporating knowledge in areas such as instruction semantics, memory access patterns, and compilation and runtime feedback. For machine learning engineering code in real databases, existing methods still lack targeted design in terms of complex call chains, cross-module context extraction, and stable iterative optimization for performance results.
[0008] Therefore, how to construct an automatic optimization method for machine learning code within the RISC-V platform database, enabling it to accurately extract context constraint information from complex engineering code without extensive manual intervention, generate correct and efficient vectorized implementations by combining the hardware characteristics of RISC-V vector extensions, and achieve continuous iterative correction through compilation and runtime feedback, has become a pressing technical problem to be solved. Summary of the Invention
[0009] This invention aims to provide an automatic code optimization method based on RISC-V and multiple agents. It achieves vectorized code generation with engineering code context awareness, domain knowledge retrieval, and constraint enhancement through the collaborative mechanism of multiple agents. Through compilation and execution verification and performance feedback-driven multi-round iterative correction, it steadily improves the execution efficiency of optimized code on the RISC-V platform while ensuring the correctness of the generated code and reducing the cost of manual intervention.
[0010] Based on a first key aspect of the present invention, an automatic code optimization method based on RISC-V and multi-agent systems is provided, the method comprising:
[0011] Multiple intelligent agents collaboratively perform context awareness, knowledge retrieval, and code generation. The context awareness is used to obtain target code and its context constraint information. The knowledge retrieval extracts optimization specifications and implementation patterns adapted to RISC-V vector extension from a pre-built RVV domain knowledge base based on the context constraint information. The code generation converts the target code into vectorized optimized code based on the optimization specifications and implementation patterns.
[0012] The vectorized optimized code is compiled, run, and verified. If an error occurs during compilation or execution, the vectorized optimized code is corrected based on the error diagnosis information and then re-verified.
[0013] If the operation is successful but the performance data does not reach the preset threshold, feedback is generated based on the performance data and the collaborative execution of the multiple intelligent agents is retried, and iterative optimization is performed until the preset target is met.
[0014] Optionally, the plurality of intelligent agents include a Code-Finder intelligent agent, a Knowledge-Retriever intelligent agent, a Code-Optimizer intelligent agent, a Code-Updater intelligent agent, a Code-Runner intelligent agent, an Error-Executor intelligent agent, and a Code-Evaluator intelligent agent.
[0015] Furthermore, the context awareness is performed by the Code-Finder agent, including: receiving the target project path, scanning the source files, combining natural language description and abstract syntax tree feature analysis to locate the function to be optimized, extracting function signature, loop structure, call relationship and context dependency information to form context constraint information;
[0016] The knowledge retrieval is performed by the Knowledge-Retriever agent, including: retrieving matching API specifications, optimization patterns and generation constraints from the RVV domain knowledge base based on the nesting depth, memory access patterns and data type information contained in the context constraint information, to form a structured domain knowledge package;
[0017] The code generation is performed by the Code-Optimizer agent, including: generating RVV vectorized optimized code by integrating the original code, the context constraint information, and the structured domain knowledge package.
[0018] Furthermore, the compilation, execution, verification, and iterative optimization are performed collaboratively by the Code-Runner agent, the Error-Executor agent, and the Code-Evaluator agent.
[0019] The Code-Runner agent invokes the compilation and testing toolchain to compile, run, and collect performance data on the updated project code, outputting program execution results, correctness verification information, and performance data. When an exception occurs during compilation or running, the Error-Executor agent parses the error log and generates correction suggestions, which are then fed back to the Code-Optimizer agent to trigger correction.
[0020] For successfully running candidate code, the Code-Evaluator agent combines execution time, speedup ratio, correctness results, and hardware performance indicators to evaluate the quality of the current implementation and generate performance feedback. If the expected optimization goal is not achieved, the performance feedback is sent back to the Code-Optimizer agent to drive the next round of strategy adjustment and code generation.
[0021] Optionally, the RVV domain knowledge base includes an API format reference module, an optimization pattern module, an optimization principle module, and a troubleshooting module;
[0022] The API format reference module covers the naming rules, function signatures, parameter constraints, and usage examples of RVV 1.0 built-in functions;
[0023] The optimization mode module includes RVV vectorization mode and general architecture optimization mode;
[0024] The optimization principle module specifies the code generation boundary in the form of explicit constraints;
[0025] The troubleshooting module establishes a mapping from compiler toolchain error logs to repair suggestions;
[0026] The knowledge base covers optimization patterns and RVV intrinsic function specifications.
[0027] Furthermore, the specific methods by which the Knowledge-Retriever agent retrieves matching API specifications, optimization patterns, and generation constraints from the RVV domain knowledge base include:
[0028] The query is based on the code structure information output by the Code-Finder agent, which includes abstract syntax tree features, loop nesting depth, memory access patterns, and data types.
[0029] We employ a knowledge matching approach assisted by semantic understanding of a large language model to handle the implicit semantic correspondence between code structure features and knowledge descriptions. We extract the API specifications, optimization patterns, and constraint principles most relevant to the target operator to form a structured domain knowledge package.
[0030] Furthermore, the structured domain knowledge package is injected into the Code-Optimizer agent only in the first iteration, and is not injected again in subsequent iterations. The guidance information in the iteration phase mainly comes from the performance feedback returned by the Code-Evaluator agent based on the execution of real hardware.
[0031] Furthermore, the performance data includes instructions per cycle, L1 cache miss rate, and L2 cache miss rate; the instructions per cycle is defined as the ratio of the total number of instructions executed by the CPU to the total number of CPU clock cycles, used to reflect the processor's instruction throughput capability; the L1 cache miss rate is defined as the ratio of the number of L1 cache load misses to the total number of L1 cache loads; and the L2 cache miss rate is defined as the ratio of the number of L2 cache misses to the total number of L2 cache accesses.
[0032] Specifically, the hardware performance data includes instructions per cycle (IPC), L1 cache miss rate, and L2 cache miss rate, which are defined as follows:
[0033]
[0034]
[0035]
[0036] Wherein, instructions represent the total number of instructions executed by the CPU, cycles represent the total number of CPU clock cycles, IPC reflects the processor's instruction throughput capability, and a higher IPC value indicates higher processor execution efficiency; L1 cache load misses represent the number of L1 cache load misses, L1 cache loads represent the total number of L1 cache loads, and R... L1 R represents the L1 cache miss rate. L1 A lower value indicates better data access locality and cache utilization; L2 cache misses represent the number of L2 cache misses, L2 cache references represent the total number of L2 cache accesses, and R... L2 R represents the L2 cache miss rate. L2 The lower the value, the higher the L2 cache utilization efficiency.
[0037] Furthermore, the speedup ratio used by the Code-Evaluator agent to evaluate the quality of the current implementation is defined as the inverse relationship between the average execution time of the optimized implementation and the average execution time of the baseline implementation. Specifically, the speedup ratio equals the average execution time of the baseline implementation divided by the average execution time of the optimized implementation.
[0038] The baseline implementation is the compilation result of the GCC compiler with the -O3 optimization level and the -ftree-vectorize automatic vectorization option enabled;
[0039] Speedup ratio is used to quantify the performance improvement of the current optimized implementation relative to the baseline implementation. A speedup ratio greater than 1 indicates that the optimized implementation is better than the baseline.
[0040] When the speedup ratio is lower than the preset threshold, the Code-Evaluator agent determines that the expected optimization goal has not been achieved and generates targeted performance feedback, including execution time, speedup ratio, number of instructions per cycle, and cache miss rate, and sends it back to the Code-Optimizer agent to drive the next round of strategy adjustment.
[0041] Specifically, the speedup ratio used by the Code-Evaluator module to perform quality evaluation of the current implementation is defined as follows:
[0042]
[0043] Among them, T baseline This represents the average execution time of the baseline implementation, which is the compilation result of the GCC compiler with the -O3 optimization level and the -ftree-vectorize automatic vectorization option enabled. T optimized The average execution time of the current optimized implementation is indicated; the speedup ratio is used to quantify the performance improvement of the current optimized implementation relative to the baseline implementation. A speedup ratio greater than 1 indicates that the optimized implementation is better than the baseline; when the speedup ratio is lower than a preset threshold, the Code-Evaluator module determines that the expected optimization goal has not been achieved and generates targeted performance feedback, which includes execution time, speedup ratio and hardware counter metrics, and sends them back to the Code-Optimizer module to drive the next round of strategy adjustment.
[0044] During the compilation, execution, and verification process, compilation error rate, runtime error rate, and agent collaboration error rate are statistically analyzed. The compilation error rate is defined as the ratio of the number of times the generated code fails to compile to the total number of optimizations. The runtime error rate is defined as the ratio of the number of times the code compiles successfully but the execution result is incorrect to the total number of optimizations. The agent collaboration error rate is defined as the ratio of the number of times the agent generates correct content but the output file name or storage location is incorrect to the total number of optimizations. The compilation error rate and runtime error rate are used to evaluate the effectiveness of the knowledge base in reducing semantically related errors in the underlying hardware, while the agent collaboration error rate is used to evaluate the stability of multi-agent cooperative scheduling and interaction behavior. The changes in each error rate before and after the introduction of the knowledge base are used to quantify the contribution of domain knowledge injection to the correctness of code generation.
[0045] The Code-Runner module calls the compilation and testing toolchain to compile, run, and collect performance data on the updated project code. During this process, it statistically analyzes the compilation error rate, runtime error rate, and agent collaboration error rate, defined as follows:
[0046]
[0047] Where, N total N represents the total number of optimization attempts. compile_fail N represents the number of times the generated code failed to compile. runtime_fail N represents the number of times the code compiles successfully but produces an incorrect result. agent_fail R represents the number of times the agent generates correct content but the output filename or storage location is incorrect. compile R represents the compilation error rate. runtime R represents the runtime error rate. agent The error rate represents the agent collaboration error rate; the compilation error rate and runtime error rate are used to evaluate the effectiveness of the knowledge base in reducing semantically related errors in the underlying hardware; the agent collaboration error rate is used to evaluate the stability of multi-agent collaborative scheduling and interaction behavior; the changes in each error rate before and after the introduction of the knowledge base are used to quantify the contribution of domain knowledge injection to the correctness of code generation.
[0048] Based on a third key aspect of the present invention, a RISC-V-based multi-agent code automatic optimization system for implementing the aforementioned method is provided, the system comprising:
[0049] The Code-Finder agent is configured to receive the target project path, scan the source files, combine natural language description and abstract syntax tree feature analysis to locate the function to be optimized, and extract the function signature, loop structure, call relationship and context dependency information to form context constraint information;
[0050] The Knowledge-Retriever agent is configured to retrieve matching API specifications, optimization patterns, and generation constraints from the RVV domain knowledge base based on the nesting depth, memory access patterns, and data type information contained in the context constraint information, thereby forming a structured domain knowledge package.
[0051] The Code-Optimizer agent is configured to generate RVV vectorized optimized code by integrating the original code, the context constraint information, and the structured domain knowledge package.
[0052] The Code-Updater agent is configured to write the generated optimized code back to the corresponding location in the original project, forming candidate implementations.
[0053] The Code-Runner agent is configured to call the compilation and testing toolchain to compile, run and collect performance data on the updated project code, and output program execution results, correctness verification information and performance data.
[0054] The Error-Executor agent is configured to parse error logs and generate correction suggestions when compilation or runtime exceptions occur, and then feed these suggestions back to the Code-Optimizer agent to trigger corrections.
[0055] The Code-Evaluator agent is configured to evaluate the quality of the current implementation and generate performance feedback for successfully running candidate code, combining execution time, speedup ratio, correctness results and hardware performance indicators. If the expected optimization goal is not achieved, the performance feedback is sent back to the Code-Optimizer agent to drive the next round of strategy adjustment and code generation.
[0056] Based on a third key aspect of the present invention, an electronic device is provided, comprising one or more processors;
[0057] Storage device for storing one or more programs;
[0058] When one or more programs are executed by one or more processors, the processors implement the aforementioned RISC-V-based multi-agent code optimization method. The electronic devices include various hardware entities such as servers, computers, and mobile terminals.
[0059] Based on a fourth key aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored, which, when executed, implements the aforementioned automatic code optimization method based on RISC-V and multiple agents. The readable storage medium includes optical discs, hard disks, USB flash drives, ROMs, PROMs, etc.
[0060] The RISC-V-based multi-agent code automatic optimization method provided by this invention achieves significant technical results in the automatic vectorization of machine learning code within the RISC-V platform database through a technical solution combining multi-agent collaborative mechanisms and domain knowledge constraints. Experimental results show that, in the function-level vectorization benchmark evaluation, this invention achieves stable and significant performance improvements for representative functions in the TSVC test set that the GCC compiler cannot stably and automatically vectorize (including functions such as s114, s2233, s261, and s293, which cover typical code patterns such as complex memory access, circular dependencies, reduction, and control flow).
[0061] In the end-to-end evaluation of real-world machine learning engineering code within the database, 16 representative implementations covering 5 core modules—clustering (kmeans), regression (LinearReg, ElasticNet, Cox), classification (SVM, Bayes, LogReg), and dimensionality reduction (PCA, SVD)—were evaluated. The code optimized by this invention achieved stable speedups on most tasks compared to the baseline implementation with the GCC compiler enabled at the -O3 optimization level and the -ftree-vectorize automatic vectorization option. Specifically, the speedups on computationally intensive operators such as kmenes.G and PCA.P reached 10.86 times and 8.07 times, respectively, indicating that this invention can fully unleash the hardware execution potential of RISC-V vector extensions and is significantly superior to existing compiler automatic vectorization techniques.
[0062] This invention effectively solves the technical problem of error mapping easily generated in vectorized code generation by general-purpose large language models due to the lack of RISC-V hardware prior knowledge by pre-constructing an RVV domain knowledge base covering four modules: API format reference, optimization mode, optimization principle, and troubleshooting. Regarding error rate improvement, experimental data shows that after introducing the RVV domain knowledge base constructed in this invention, the compilation error rate driven by each model decreased from a maximum of 20.0% to 7.0%, and the runtime error rate decreased from a maximum of 10.0% to 3.0%. The agent collaboration error rate only showed slight fluctuations before and after the introduction of the knowledge base, proving that the knowledge base, embedded in the generation process through external prior injection, effectively suppresses low-level semantic errors such as undefined built-in functions, incorrect parameter configuration, type mismatch, and out-of-bounds memory access, without interfering with the collaborative scheduling and interaction behavior of multiple agents.
[0063] The multi-round iterative optimization mechanism of this invention further relies on performance feedback driven by real compilation and runtime environment for continuous correction. Experiments show that the multi-round iteration and domain knowledge injection components form a complementary and synergistic relationship in the MARVO method. The multi-round closed-loop optimization plays a continuous search and convergence role on operators with large optimization space (such as prepare.G), while the knowledge base brings an additional performance improvement of about 50% on the basic generated version by accurately guiding the selection of FMA instructions and LMUL configuration on high computational density operators (such as kmes.G). The two types of components jointly support the system to obtain correct and efficient vectorization implementation in real engineering code scenarios.
[0064] This invention employs a multi-agent collaborative architecture. The Code-Finder agent enables code context awareness and localization; the Knowledge-Retriever agent facilitates domain knowledge retrieval and constraint enhancement; the Code-Optimizer agent generates vectorized code under knowledge constraints; and the Code-Runner, Error-Executor, and Code-Evaluator agents collaborate to achieve iterative correction driven by compilation, execution, verification, error diagnosis, and performance feedback. This forms a complete closed loop from code localization, context extraction, knowledge injection, and code generation to compilation verification, error correction, performance evaluation, and iterative feedback. Experiments show that the method of this invention maintains a consistent acceleration trend under different large language model backends (Claude 4.5, DeepSeek-V3.2, GLM-4.6), achieving stable gains on most in-database machine learning tasks, demonstrating good cross-model consistency and generalization ability.
[0065] In real-world, complex engineering code scenarios within database-based machine learning systems, this invention can automatically identify, vectorize, rewrite, and iteratively optimize target code without extensive manual intervention. The code covered in the experiments comes from three open-source systems: MADlib, PostgresML, and GaussML. These systems exhibit significant differences in interface encapsulation, call hierarchy, and engineering coupling, and also include cross-file dependencies. This invention can reliably complete automatic optimization and achieve effective performance improvements in all three systems, fully verifying its adaptability to real-world, complex engineering scenarios. It significantly reduces the analysis cost and implementation difficulty of manual vectorization optimization, providing a practical and feasible technical solution for automatic performance optimization of machine learning code within RISC-V platform databases. Attached Figure Description
[0066] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, obtaining other drawings based on these drawings without creative effort still falls within the scope of the present invention.
[0067] Figure 1 This illustration shows an execution flow diagram of an automatic code optimization method based on RISC-V and multi-agent technology in one embodiment of the present invention.
[0068] Figure 2 The overall processing flow and intelligent agent architecture for implementing the method of the present invention are shown in one embodiment of the present invention. Detailed Implementation
[0069] The preferred embodiments of the present invention will be described in detail below to provide a clearer understanding of the purpose, features, and advantages of the invention. It should be understood that the following embodiments are not intended to limit the scope of the invention, but are merely illustrative of the essential spirit of the technical solution of the invention.
[0070] In the following description, certain specific details are set forth for the purpose of illustrating various disclosed embodiments in order to provide a thorough understanding of the various disclosed embodiments. However, those skilled in the art will recognize that embodiments may be practiced without one or more of these specific details. In other instances, well-known techniques associated with the invention may not have been shown or described in detail to avoid unnecessarily obscuring the description of the embodiments.
[0071] Throughout this specification, references to "an embodiment" or "an embodiment" indicate that a particular feature, structure, or characteristic described in connection with the embodiment is included in at least one embodiment. Therefore, the appearance of "in an embodiment" or "an embodiment" in various places throughout the specification does not necessarily refer to the same embodiment. Furthermore, a particular feature, structure, or characteristic may be combined in any manner in one or more embodiments.
[0072] The specific meanings of the technical terms that may be used in this invention are explained as follows:
[0073] RISC-V (Reduced Instruction Set Computing - V): An open-source instruction set architecture that follows the principles of reduced instruction set design. Unlike traditional closed architectures, RISC-V employs a modular organization that combines a basic instruction set with optional standard extensions. Processor designers can flexibly choose extension capabilities such as multiplication, atomic operations, floating-point operations, vectors, and matrices according to application requirements. This on-demand expansion structure makes it highly adjustable in terms of area, power consumption, and performance.
[0074] RVV (RISC-V Vector Extension): A significant extension in the RISC-V official standard for data-level parallel computing. Unlike the fixed-width Single Instruction Multiple Data (SIMD) mode, this extension employs a vector length-independent design, decoupling the software programming model from the hardware physical vector length. The processor can freely choose the width of the physical vector registers according to the specific implementation, while the program adapts to different platforms by dynamically setting the current vector length and element width.
[0075] LMUL (Vector Register Grouping Multiplier): A register grouping parameter in RVV used to control the multiplier by which multiple physical vector registers are combined into a single logical register.
[0076] Vsetvli: An instruction in RVV used to dynamically set the length of a vector. It introduces a dynamic strip-mining mechanism based on vsetvli at the loop level, dynamically setting the length of the active vector according to the amount of data remaining.
[0077] Multi-Agent Collaboration: The multi-agent collaboration mechanism employed in this invention refers to a method in which multiple agents with different responsibilities cooperate and interact to jointly complete complex code optimization tasks automatically. Each agent includes Code-Finder (code location), Knowledge-Retriever (knowledge retrieval), Code-Optimizer (code generation), Code-Updater (code update), Code-Runner (compile and run), Error-Executor (error diagnosis), and Code-Evaluator (performance evaluation).
[0078] RVV Domain Knowledge Base: This invention pre-builds a structured knowledge base to provide explicit hardware specification references and optimization pattern guidance for the code generation process.
[0079] Automatic vectorization: An important optimization technique in modern compilers, its basic goal is to automatically identify data-level parallelism in code and convert scalar instruction sequences into vector instruction sequences while keeping the semantics of high-level language programs unchanged.
[0080] Contextual constraint information: The Code-Finder agent combines call relationships and static program analysis results to compress the original project source code into a structured information set. Contextual constraint information includes function interface definitions, data type declarations, cross-file dependencies, and memory access pattern descriptions, providing a unified input for subsequent knowledge retrieval and code generation.
[0081] TSVC (Test Suite for Vectorizing Compilers): A standard test suite for evaluating a compiler's vectorization capabilities. It contains multiple test functions with different memory access modes, circular dependency types, and control flow structures to test the compiler's performance when handling complex vectorization scenarios.
[0082] Abstract Syntax Tree (AST): A tree-like abstract representation of source code, where each node represents a syntactic structure in the source code (such as function definition, loop statement, conditional branch, variable declaration, etc.). In this invention, the Code-Finder agent locates the function to be optimized by analyzing AST features, identifying information such as loop nesting depth, memory access patterns, and data types.
[0083] In one feasible embodiment, combined with Figure 1 As shown, this invention provides an automatic code optimization method based on RISC-V and multi-agent systems, including the following steps S100-S300 executed by a computer system:
[0084] The RISC-V-based multi-agent code automatic optimization method provided by this invention is generally divided into two interconnected stages: an automatic code update stage and a multi-round iterative optimization stage. The automatic code update stage is responsible for code awareness, knowledge retrieval, initial generation, and source code replacement, such as... Figure 1 Step S100 in the process; the multi-round iterative optimization phase is responsible for verifying, diagnosing, and continuously optimizing the generated results in a real compilation and execution environment. The two phases form a unified processing flow through a multi-agent collaborative mechanism, such as... Figure 1 Steps S200-S300 in the process.
[0085] like Figure 2 The overall processing flow and intelligent agent architecture of the present invention shown in the figure are as follows: In the automatic code update stage, the system first uses the Code-Finder intelligent agent to locate the code and extract the context of the target project, and obtain the complete project context information of the function to be optimized.
[0086] Subsequently, the Knowledge-Retriever agent, based on the extracted code features, recalls API specifications, optimization patterns, and generation constraints matching the current operator from a pre-built RVV domain knowledge base, forming a structured domain knowledge package for the current task. The Code-Optimizer agent then integrates the original code, contextual constraints, and the structured domain knowledge package to generate a vectorized optimization implementation for RVV. The Code-Updater agent then writes the results back to the corresponding location in the original project, forming a candidate implementation.
[0087] During multiple rounds of iterative optimization, the system uses the Code-Runner agent to call the underlying compilation and testing toolchain to compile, run, and collect performance data on the updated project code, outputting program execution results, correctness verification information, and performance data. When an exception occurs during compilation or execution, the Error-Executor agent parses the error log and generates correction suggestions and retry requests, which are then fed back to the Code-Optimizer agent to trigger subsequent corrections. For candidate code that runs successfully, the Code-Evaluator agent further evaluates the quality of the current implementation based on execution time, speedup ratio, correctness results, and performance metrics, generating performance feedback. If the results do not meet the expected optimization goals, the Code-Evaluator agent generates targeted performance feedback and sends it back to the Code-Optimizer agent, driving the next round of strategy adjustment and code generation.
[0088] The multiple intelligent agents in this invention specifically include seven intelligent agents: Code-Finder, Knowledge-Retriever, Code-Optimizer, Code-Updater, Code-Runner, Error-Executor, and Code-Evaluator. The division of responsibilities and collaborative relationships among these intelligent agents are as follows.
[0089] The Code-Finder agent, acting as the system entry point, receives the target project path, scans the source files in the specified directory, and, combining natural language description and abstract syntax tree feature analysis, first locates functions with high optimization potential in the source code library. Then, it identifies the scalar operators to be optimized and extracts function signatures, loop structures, call relationships, and necessary contextual dependency information. Subsequently, the Code-Finder agent, combining call relationships and static program analysis results, compresses the original project source code into contextual constraint information. This contextual constraint information includes function interface definitions, data type declarations, cross-file dependencies, and memory access pattern descriptions, providing a unified input for subsequent knowledge retrieval and code generation. The Code-Finder agent marks functions with optimization potential higher than a preset threshold, and skips optimization for functions with optimization potential lower than the preset threshold to avoid unnecessary computational overhead.
[0090] The Knowledge-Retriever agent uses the code structure information output by the Code-Finder agent as the basis for querying, performing a knowledge screening before iterative optimization begins. Code structure information includes abstract syntax tree features, loop nesting depth, memory access patterns, and data types. The Knowledge-Retriever agent employs a knowledge matching method assisted by large language model semantic understanding to handle the implicit semantic correspondence between code structure features and knowledge descriptions, rather than direct matching based on explicit keywords or vector similarity. It extracts the most relevant API specifications, optimization patterns, and constraint principles from the RVV domain knowledge base to form a structured domain knowledge package. This structured domain knowledge package is only injected into the Code-Optimizer agent in the first iteration; subsequent iterations do not repeat the injection. The guiding information during the iteration phase mainly comes from the performance feedback returned by the Code-Evaluator agent based on real hardware execution.
[0091] The Code-Optimizer agent integrates the original code, contextual constraints, and structured domain knowledge retrieved by the Knowledge-Retriever agent to generate vectorized optimized code for RVV. During code generation, the Code-Optimizer agent selects the correct RVV intrinsic function name and signature based on the API format reference module in the structured domain knowledge package, chooses a vectorized implementation strategy matching the current operator's memory access characteristics (continuous memory access, cross-step memory access, or indexed memory access) based on the optimization mode module, and sets the LMUL configuration, mask usage, and boundary handling method based on the optimization principle module.
[0092] The Code-Updater agent writes the optimized code generated by the Code-Optimizer agent back to the corresponding location in the original project and retains a backup of the original code so that it can be restored if a rollback is needed during iteration. The Code-Updater agent identifies the optimized versions generated in different iterations using a suffix naming convention, which facilitates the Code-Evaluator agent in comparing the performance of the optimization results from each iteration and making the optimal selection.
[0093] The Code-Runner agent invokes the underlying compilation and testing toolchain to compile, run, and collect performance data on the updated project code, outputting program execution results, correctness verification information, and performance data. In one implementation, the Code-Runner agent invokes the GCC 14.2 compiler to compile the updated project code and calls the perfstat tool to collect hardware performance counter data.
[0094] When an exception occurs during compilation or runtime, the Error-Executor agent parses the error log, identifies the error type (including but not limited to missing header files, undefined intrinsics, data type mismatches, linking errors, array out of bounds, memory misalignment, register overflows, and result errors), and retrieves corresponding repair suggestions from the troubleshooting module of the RVV domain knowledge base based on the error type. It then generates correction suggestions and retry requests, and feeds them back to the Code-Optimizer agent to trigger subsequent corrections.
[0095] The Code-Evaluator agent evaluates the quality of successfully running candidate code by considering execution time, speedup, correctness, and performance metrics, and generates performance feedback. If the expected optimization goal is not achieved, the Code-Evaluator agent sends the performance feedback back to the Code-Optimizer agent, driving the next round of strategy adjustments and code generation. The Code-Evaluator agent also maintains a historical record of optimization results from each round and outputs the optimized version with the highest speedup as the final result at the end of the iteration.
[0096] The pre-built RVV domain knowledge base of this invention is organized around four core requirements in the code generation process, covering interface specification constraints, high-performance implementation patterns, code generation principles, and error correction priors in sequence.
[0097] The API format reference module covers the naming rules, function signatures, parameter constraints, and usage examples of RVV 1.0 built-in functions, and annotates typical application scenarios for various intrinsics. It provides an interface specification basis for initial code generation and is a primary means of reducing compilation error rates. Specifically, the API format reference module records the complete signature information of more than 40 RVV intrinsic functions, such as vsetvl, vle32.v, vse32.v, vfmul.vv, vfadd.vv, vfredusum.vs, vmfne.vv, vmv.vf, vmmerge.vvm, and vrgather.vv.
[0098] The optimization mode module includes verified high-performance implementation priors, divided into two categories: RVV-specific vectorization modes and general architecture optimization modes. It focuses on typical vectorization challenges in database row-level storage scenarios such as straddle access and index access, providing referable implementation paths for performance optimization. The optimization mode module includes, but is not limited to, 39 optimization modes such as: continuous memory access vectorization mode, straddle-length memory access vectorization mode, index memory access vectorization mode, masked conditional execution mode, vector reduction mode, FMA fusion multiply-accumulate mode, loop unrolling mode, and block processing mode.
[0099] The optimization principles module defines code generation boundaries through explicit constraints, guiding the model to prioritize implementation strategies that match the characteristics of the target microarchitecture, thereby compressing ineffective generation space from the source. Core principles included in the optimization principles module include, but are not limited to: prioritizing the use of FMA instructions to replace separate multiplication and addition, maintaining consistency of LMUL configurations within functions, using vsetvli to dynamically set vector lengths, avoiding unnecessary scalar vector domain switching, and prioritizing the use of register-level broadcast multiplexing of scalar parameters.
[0100] The troubleshooting module establishes a mapping from compiler toolchain error logs to repair suggestions, covering common error types such as missing header files, undefined intrinsics, data type mismatches, linking errors, array out-of-bounds errors, memory misalignment, register overflows, and incorrect results. This provides a verifiable reference for error diagnosis and code correction during the iteration process. Overall, the knowledge base covers 39 optimization patterns and over 40 RVV intrinsic function specifications, encompassing the main domain knowledge required for the three key stages of code generation, performance optimization, and error correction.
[0101] When locating code, the Code-Finder agent first receives the target project path specified by the user, traverses all source files under that path, and identifies optimizable source files based on their file extensions (e.g., .c, .cpp, .cc, .h, .hpp). For each source file, the Code-Finder agent calls a static analysis tool based on an abstract syntax tree (such as ClangLibTooling or tree-sitter) to parse the file content and generate a complete abstract syntax tree.
[0102] On the abstract syntax tree, the Code-Finder agent performs the following analysis steps: First, it identifies all function definition nodes and extracts function signatures, including function name, return type, parameter list, and their data types. Second, it traverses the abstract syntax tree of each function, identifies loop structure nodes (for loop, while loop, and do-while loop), and extracts loop nesting depth, loop boundary conditions, and operation types within the loop body (arithmetic operations, memory access operations, conditional branches, and function calls). Third, it analyzes the memory access pattern within the loop body to determine whether data access is continuous, cross-step, or indexed. Fourth, it calculates the optimization potential score for each function based on the loop nesting depth and operation type, with the scoring rule being: the deeper the loop nesting depth and the denser the arithmetic or memory access operations within the loop body, the higher the optimization potential score. Fifth, it marks functions with optimization potential scores higher than a preset threshold as functions to be optimized and extracts complete context constraint information.
[0103] The construction process of context constraint information is as follows: Starting from the function to be optimized, the Code-Finder agent traces upwards along the call graph to all parent functions that call the function, and downwards to all child functions called by the function, forming a complete call graph. It collects all data structure definitions, type declarations, global variables, and macro definitions involved in the function to be optimized and its call graph. This information, along with the source code of the function to be optimized, is then organized into context constraint information. The context constraint information is stored in a structured format (such as JSON or XML) for easy parsing and use by the agent later.
[0104] The Knowledge-Retriever agent uses the code structure information output by the Code-Finder agent as the basis for its query, performing a knowledge screening before iterative optimization begins. The code structure information includes abstract syntax tree features, loop nesting depth, memory access patterns, and data types.
[0105] The knowledge retrieval process of the Knowledge-Retriever agent includes the following steps: First, extract key feature vectors from the code structure information. These key feature vectors include operator type (reduction operation, element-wise operation, conditional update operation, matrix operation, etc.), memory access mode (continuous memory access, straddle memory access, indexed memory access), data type (integer, floating-point, fixed-point), and data width (8-bit, 16-bit, 32-bit, 64-bit). Second, construct a natural language query statement from the key feature vectors, such as "Generate RVV vectorized code for a 32-bit floating-point reduction operation containing straddle memory access." Third, submit the natural language query statement to the large language model, which understands the query semantics and matches it with the semantic descriptions of each knowledge entry in the RVV domain knowledge base. Fourth, the large language model sorts the knowledge entries based on semantic similarity and selects the entries with the highest similarity to form a structured domain knowledge package. Fifth, the Knowledge-Retriever agent outputs the structured domain knowledge package in a structured format (such as JSON) for use by the Code-Optimizer agent.
[0106] During the retrieval process, the Knowledge-Retriever agent prioritizes recalling the optimization pattern that best matches the memory access pattern of the target operator. For example, when the target operator's memory access pattern is straddle memory access, it prioritizes recalling the straddle memory access vectorization pattern (such as using the vlse32.v instruction); when the target operator's memory access pattern is index memory access, it prioritizes recalling the index memory access vectorization pattern (such as using the vluxei32.v instruction); and when the target operator contains conditional branches, it prioritizes recalling the masked conditional execution pattern (such as using the vmfne.vv and vmmerge.vvm instructions).
[0107] The Code-Optimizer agent integrates the original code, contextual constraint information, and the structured domain knowledge package retrieved by the Knowledge-Retriever agent to generate vectorized optimized code for RVV. The code generation process of the Code-Optimizer agent specifically includes the following steps.
[0108] The first step involves the Code-Optimizer agent parsing the original code, identifying the loop structure or basic block to be vectorized, and extracting the loop control variables, loop boundaries, operation sequences within the loop body, and data dependencies. The second step involves the Code-Optimizer agent determining the vectorization strategy based on optimization patterns retrieved from the structured domain knowledge package. Specifically, if the target operator's memory access pattern is continuous memory access without complex control flow, the basic vectorization pattern is selected; if the memory access pattern is multi-step memory access, the multi-step memory access vectorization pattern is selected; if it includes conditional branches, the mask vectorization pattern is selected; if it is a reduction operation, the vector reduction pattern is selected; and if it is a computationally intensive operation, the FMA fusion pattern is selected. The third step involves the Code-Optimizer agent selecting the correct RVV intrinsic function name and function signature based on the API format reference in the structured domain knowledge package, generating the specific vectorized instruction sequence. The fourth step involves the Code-Optimizer agent setting the LMUL configuration, mask usage, boundary handling method, and vsetvli call location based on the optimization principles in the structured domain knowledge package. In the fifth step, the Code-Optimizer agent merges the generated vectorized code with the non-vectorized parts of the original code (such as unvectorized control flow, external function calls, etc.) to form complete optimized code.
[0109] In one specific implementation, the Code-Optimizer agent vectorizes the following scalar loop:
[0110] The Code-Optimizer agent generates the following RVV vectorized code:
[0111] Specifically, a register grouping configuration of LMUL=4 is used to increase the data size that a single instruction can process. vfmacc.vv replaces the separate vfmul.vv and vfadd.vv to reduce the number of instructions. A single vfredusum.vs is executed after the loop to complete the final reduction, reducing the number of reductions from proportional to the data size to a fixed one. The specific choice of LMUL configuration is dynamically determined based on the data size and vector register resources: when the data size is small (less than 1024 elements), m1 or m2 configuration is chosen to reduce register pressure; when the data size is large, m4 or m8 configuration is chosen to improve parallelism.
[0112] The Code-Runner agent invokes the underlying compilation and testing toolchain to compile, run, and collect performance data on the updated project code. The execution flow of the Code-Runner agent specifically includes the following steps.
[0113] The first step involves the Code-Runner agent invoking the GCC compiler to compile the updated project code. The compilation commands include: -O3 optimization level, -ftree-vectorize automatic vectorization option, -march=rv64gcv (enabling RVV extensions), and necessary header file paths and link library options. If compilation succeeds, an executable file is generated; if compilation fails, the Code-Runner agent captures the compilation error log and passes it to the Error-Executor agent.
[0114] The second step involves the Code-Runner agent executing runtime tests on the successfully compiled executable file. These tests include functional correctness testing and performance testing. The functional correctness test compares the optimized code's calculations with the expected results using a pre-set test dataset to verify consistency with the original code. If the functional correctness test passes, the Code-Runner agent proceeds to the performance testing phase; if the functional correctness test fails, the Code-Runner agent captures runtime error logs and passes them to the Error-Executor agent.
[0115] The third step involves the Code-Runner agent performing performance tests. Each candidate implementation is run 10 times, with the execution time recorded for each run. Simultaneously, the Code-Runner agent uses the `perf stat` tool to collect hardware performance counter data, including six raw counters: total number of CPU instructions executed, total number of CPU clock cycles, L1 cache load misses, total L1 cache loads, L2 cache misses, and total L2 cache references. The Code-Runner agent then summarizes the program execution results, correctness verification information, and performance data and outputs them to the Code-Evaluator agent.
[0116] When an exception occurs during compilation or runtime, the Error-Executor agent parses the error log, generates correction suggestions and retry requests, and feeds them back to the Code-Optimizer agent to trigger subsequent corrections. The error handling process of the Error-Executor agent specifically includes the following steps.
[0117] The first step is for the Error-Executor agent to receive error logs from the Code-Runner agent and use regular expressions and keyword matching techniques to identify the error type. Common error types and their identification keywords include: missing header files. intrinsic is not defined The errors include: data type mismatch ("incompatible type"), linking error ("undefined reference"), array out of bounds ("index out of bounds"), misaligned address ("misaligned address"), register overflow ("registerallocation failed"), and incorrect result ("incorrect result").
[0118] The second step involves the Error-Executor agent retrieving corresponding repair suggestions from the troubleshooting module of the RVV domain knowledge base based on the identified error type. For example, for the "intrinsic not defined" error, the troubleshooting module provides repair suggestions such as "check if the header file includes riscv_vector.h" and "check if the -march parameter includes the v extension"; for the "data type mismatch" error, it provides repair suggestions such as "check if the vector element width SEW setting is correct" and "check if the LMUL configuration matches the data type".
[0119] Third, the Error-Executor agent combines the error type and repair suggestions into a correction suggestion, which is then fed back to the Code-Optimizer agent along with a retry request. The Code-Optimizer agent adjusts its code generation strategy based on the correction suggestion (for example, if the error type is "intrinsic undefined", then #include <intrinsic.h> is added at the beginning of the generated code).<riscv_vector.h> If the error is "data type mismatch," adjust the SEW parameters or LMUL configuration, regenerate the optimized code, and proceed to the next round of verification. The Error-Executor agent records the error type distribution in each iteration for the Code-Evaluator agent to reference during quality evaluation.
[0120] The Code-Evaluator agent evaluates the quality of successfully running candidate code by combining execution time, speedup, correctness results, and performance metrics, and generates performance feedback. The Code-Evaluator agent's quality evaluation specifically includes the following steps.
[0121] The first step is for the Code-Evaluator agent to receive the program execution results, correctness verification information, and performance data output by the Code-Runner agent. It first verifies whether the correctness verification information passes. If it fails, it directly determines that the current implementation is unqualified.
[0122] The second step involves the Code-Evaluator agent extracting the execution time of 10 repeated executions from the performance data for the candidate code that passes the correctness check, calculating its average value T_optimized, and then calculating various hardware performance metrics in the following manner.
[0123] Instructions per cycle (IPC) is defined as the ratio of the total number of instructions executed by the CPU to the total number of CPU clock cycles, i.e.:
[0124]
[0125] Here, instructions represents the total number of instructions executed by the CPU, cycles represents the total number of CPU clock cycles, and IPC reflects the processor's instruction throughput capability. Generally, a higher IPC value indicates higher processor execution efficiency.
[0126] The L1 cache miss rate is defined as the ratio of the number of L1 cache load misses to the total number of L1 cache loads, i.e.:
[0127]
[0128] Where L1 dcache load misses represent the number of L1 data cache load misses, L1 dcache loads represent the total number of L1 data cache loads, and R L1 This represents the L1 cache miss rate. R L1 A lower value indicates better data access locality and cache utilization.
[0129] The L2 cache miss rate is defined as the ratio of the number of L2 cache misses to the total number of L2 cache accesses, i.e.:
[0130]
[0131] Where L2 cache misses represent the number of L2 cache misses, L2 cache references represent the total number of L2 cache accesses, and R L2 This indicates the L2 cache miss rate. R L2 The lower the value, the higher the L2 cache utilization efficiency.
[0132] The third step involves the Code-Evaluator agent further calculating the speedup to quantify the performance improvement. The speedup is defined as the inverse relationship between the average execution time of the optimized implementation and the average execution time of the baseline implementation. The specific formula is as follows:
[0133]
[0134] Among them, T baseline This represents the average execution time of the baseline implementation, which is the compilation result of the GCC compiler with the -O3 optimization level and the -ftree-vectorize automatic vectorization option enabled; T optimized This represents the average execution time of the current optimized implementation. The speedup ratio quantifies the performance improvement of the current optimized implementation relative to the baseline implementation; a speedup ratio greater than 1 indicates that the optimized implementation outperforms the baseline.
[0135] The fourth step involves the Code-Evaluator determining whether the current optimization has achieved its intended goal. If the speedup is greater than a preset threshold, the Code-Evaluator determines that the expected optimization goal has been achieved, and the iterative optimization terminates. If the speedup is less than or equal to the preset threshold, the Code-Evaluator determines that the expected optimization goal has not been achieved and generates targeted performance feedback. This performance feedback includes execution time, speedup, and hardware performance metrics such as instructions per cycle and cache miss rate, and is sent back to the Code-Optimizer to drive the next round of strategy adjustments.
[0136] During the iterative optimization process, the Code-Evaluator agent synchronously calculates the compilation error rate, runtime error rate, and agent collaboration error rate to evaluate the effectiveness of the knowledge base in reducing semantic-related errors in the underlying hardware and the stability of multi-agent collaborative scheduling and interaction behavior.
[0137] The compilation error rate is defined as the ratio of the number of times the generated code fails to compile to the total number of optimizations, i.e.:
[0138]
[0139] Where, N total N represents the total number of optimization attempts. compile_fail This indicates the number of times the generated code failed to compile, R. compileThis indicates the compilation error rate.
[0140] The runtime error rate is defined as the ratio of the number of times the code compiles successfully but produces incorrect results to the total number of optimizations performed.
[0141]
[0142] Where, N total N represents the total number of optimization attempts. runtime_fail R represents the number of times the code compiles successfully but produces incorrect results. runtime This indicates the runtime error rate.
[0143] The agent collaboration error rate is defined as the ratio of the number of times an agent generates correct content but has an incorrect output filename or storage location to the total number of optimizations, i.e.:
[0144]
[0145] Where, N total N represents the total number of optimization attempts. agent_fail R represents the number of times the agent generates correct content but the output filename or storage location is incorrect. agent This represents the error rate in agent collaboration.
[0146] The Code-Evaluator agent calculates the compilation error rate R before and after introducing the RVV domain knowledge base. compile and runtime error rate R runtime The contribution of domain knowledge injection to code generation correctness is quantified by comparing the changes between the two. After introducing a knowledge base, if R... compile and R runtime A significant decrease indicates that the API format reference module and troubleshooting module in the knowledge base effectively suppress low-level semantic errors such as undefined built-in functions, incorrect parameter configurations, type mismatches, and out-of-bounds memory accesses. agent The changes before and after the introduction of the knowledge base are used to evaluate whether the knowledge base injection interferes with the collaborative scheduling and interaction behavior among multiple agents.
[0147] In this invention, the structured domain knowledge package retrieved by the Knowledge-Retriever agent is injected into the Code-Optimizer agent only in the first iteration, and is not injected again in subsequent iterations. The guiding information in the iteration phase mainly comes from the performance feedback returned by the Code-Evaluator agent based on real hardware execution. This performance feedback directly points to the specific performance bottlenecks on the current implementation target platform, and is complementary to rather than a substitute for the static prior knowledge.
[0148] In each iteration, the Code-Optimizer agent receives the following three types of input:
[0149] (1) The original code serves as the starting point for optimization and a semantic reference;
[0150] (2) The structured domain knowledge package injected by the Knowledge-Retriever agent during the first iteration provides RVV vectorization hardware specifications and optimization mode constraints;
[0151] (3) The performance feedback returned by the Code-Evaluator provides specific performance bottleneck information on the target platform (such as high cache miss rate, low IPC, etc.).
[0152] The Code-Optimizer agent integrates the above three types of inputs, adjusts the vectorization strategy (such as changing the LMUL configuration, adjusting the data layout method, switching the vector memory access mode, adding loop unrolling, etc.), and regenerates the optimized code. The Code-Updater agent then writes the optimized code back to the corresponding location in the project for the next round of verification and evaluation.
[0153] The iteration termination condition is determined by the Code-Evaluator agent according to the following rules: When the speedup ratio is greater than a preset threshold, the Code-Evaluator agent determines that the expected optimization goal has been achieved, the iteration terminates, and the current implementation is output as the final optimization result. When the speedup ratio does not reach the preset threshold but the number of iterations reaches the preset maximum number of iterations, the Code-Evaluator agent determines that the optimization process has converged, the iteration terminates, and the implementation with the highest speedup ratio in each iteration is output as the final optimization result, along with performance change curves for each iteration for user reference. When the speedup ratio no longer increases for a consecutive preset number of iterations, the Code-Evaluator agent determines that the optimization process has fallen into a local optimum, the iteration terminates, and the current optimal implementation is output as the final result. The preset maximum number of iterations can be flexibly set according to the actual application scenario and hardware resource constraints. In one implementation, the preset maximum number of iterations is 10 iterations.
[0154] This invention was verified and implemented on a complete RISC-V hardware platform. The hardware platform uses the Spacemit X60 processor, conforms to the RISC-V 64-bit architecture, and fully supports the RVV 1.0 standard. The platform contains 8 physical cores with a base clock speed of 1.6 GHz; the total L1 data cache capacity is 256 KiB, distributed as 8 × 32 KiB per core; the total L2 cache capacity is 1 MiB, divided into two instances, each 512 KiB. In the vector subsystem, the hardware fixed vector register length VLEN is 256 bits, and the logical vector length multiplier LMUL can be configured as m1, m2, m4, and m8.
[0155] The software environment uses PostgresML 2.9.3 as the baseline for in-database machine learning and data preprocessing, deployed on a PostgreSQL 15.10 instance. The system kernel is Linux 6.6.63. The compilation toolchain uses GCC 14.2 and Rust 1.79, with -O3 optimization and RVV vectorization options enabled for the C / C++ parts, and Rust built in release mode. To reduce external interference, unnecessary background services were disabled during the experiment.
[0156] During testing, the Code-Runner agent repeatedly executed each candidate implementation 10 times, and the Code-Evaluator agent took the average of the 10 execution results as the measurement value of T_optimized to reduce system noise interference. The comparison results with the baseline implementation (GCC 14.2 -O3 -ftree-vectorize) were used to quantify the performance gains of the proposed method. Experimental results show that the method provided by this invention exhibits stable performance improvements on representative TSVC test functions and achieves consistent speedup in end-to-end evaluation of in-database machine learning operators. Compared with the baseline implementation, the method of this invention achieves a speedup of more than 10 times on in-database machine learning operators, reducing the compilation error rate from 20.0% to 7.0% and the runtime error rate from 8.0% to 3.0%.
[0157] The above detailed embodiments further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above are merely specific embodiments of the present invention and are not intended to limit the invention. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
[0158] Those skilled in the art should understand that the system module division and workflow step division in the above embodiments are only logical functional divisions. In actual implementation, the functions of multiple modules can be merged into one module as needed, or the functions of one module can be split into multiple modules. Similarly, the numbering of the above process steps is only for ease of description and reference, and does not limit the execution order of each step. The actual execution order should be dynamically determined according to the data dependencies in the workflow.
[0159] This invention can be implemented on various computing devices, including but not limited to: general-purpose servers, dedicated inference servers, virtual machines or container instances on cloud computing platforms, edge computing devices, etc. The controller's large language model can be deployed locally or can call remote services via API. Inference services for candidate models can come from various model service platforms such as Hugging FaceInference API, Replicate, and AWS SageMaker.
[0160] It should be noted that the various technical features described in the above specific embodiments can be freely combined and used in different embodiments, as long as they do not contradict each other. For those skilled in the art, several improvements and modifications can be made without departing from the principle of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.
[0161] It should be understood that the program code used to implement the methods of the present invention can be written in any combination of one or more programming languages. This program code can be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing device, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code can be executed entirely on the machine, partially on the machine, as a standalone software package partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0162] The acquisition, storage, and application of user personal information involved in the technical solution of this invention all comply with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0163] The technical terms, principles, or means related to the technical solutions of the present invention mentioned in the above embodiments, which are not described in detail above, are all well-known technologies or common practices that are known to those skilled in the art.
[0164] The foregoing has shown and described the basic principles, main features, and advantages of the present invention. Those skilled in the art should understand that the present invention is not limited to the above embodiments. The embodiments and descriptions in the specification are merely illustrative of the principles of the invention. Various changes and modifications can be made to the invention without departing from its spirit and scope, and all such changes and modifications fall within the scope of the present invention as claimed. The scope of protection of this invention is defined by the appended claims and their equivalents.
Claims
1. A method for automatic code optimization based on RISC-V and multi-agent systems, characterized in that, The method includes: Multiple intelligent agents collaboratively perform context awareness, knowledge retrieval, and code generation. The context awareness is used to obtain target code and its context constraint information. The knowledge retrieval extracts optimization specifications and implementation patterns adapted to RISC-V vector extension from a pre-built RVV domain knowledge base based on the context constraint information. The code generation converts the target code into vectorized optimized code based on the optimization specifications and implementation patterns. The vectorized optimized code is compiled, run, and verified. If an error occurs during compilation or execution, the vectorized optimized code is corrected based on the error diagnosis information and then re-verified. If the operation is successful but the performance data does not reach the preset threshold, feedback is generated based on the performance data and the collaborative execution of the multiple intelligent agents is retried, and iterative optimization is performed until the preset target is met.
2. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 1, characterized in that, The multiple intelligent agents include the Code-Finder intelligent agent, the Knowledge-Retriever intelligent agent, the Code-Optimizer intelligent agent, the Code-Updater intelligent agent, the Code-Runner intelligent agent, the Error-Executor intelligent agent, and the Code-Evaluator intelligent agent.
3. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 1, characterized in that, The RVV domain knowledge base includes an API format reference module, an optimization pattern module, an optimization principle module, and a troubleshooting module. The API format reference module covers the naming rules, function signatures, parameter constraints, and usage examples of RVV 1.0 built-in functions; The optimization mode module includes RVV vectorization mode and general architecture optimization mode; The optimization principle module specifies the code generation boundary in the form of explicit constraints; The troubleshooting module establishes a mapping from compiler toolchain error logs to repair suggestions; The knowledge base covers optimization patterns and RVV intrinsic function specifications.
4. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 2, characterized in that, The specific methods by which the Knowledge-Retriever agent retrieves matching API specifications, optimization patterns, and generation constraints from the RVV domain knowledge base include: The query is based on the code structure information output by the Code-Finder agent, which includes abstract syntax tree features, loop nesting depth, memory access patterns, and data types. We employ a knowledge matching approach assisted by semantic understanding of a large language model to handle the implicit semantic correspondence between code structure features and knowledge descriptions. We extract the API specifications, optimization patterns, and constraint principles most relevant to the target operator to form a structured domain knowledge package.
5. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 4, characterized in that, The structured domain knowledge package is injected into the Code-Optimizer agent only in the first iteration, and is not injected again in subsequent iterations. The guidance information in the iteration phase mainly comes from the performance feedback returned by the Code-Evaluator agent based on the execution of real hardware.
6. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 3, characterized in that, The performance data includes instructions per cycle, L1 cache miss rate, and L2 cache miss rate. The instructions per cycle are defined as the ratio of the total number of instructions executed by the CPU to the total number of CPU clock cycles, reflecting the processor's instruction throughput capability. The L1 cache miss rate is defined as the ratio of the number of L1 cache load misses to the total number of L1 cache loads. The L2 cache miss rate is defined as the ratio of the number of L2 cache misses to the total number of L2 cache accesses.
7. The automatic code optimization method based on RISC-V and multi-agent systems according to claim 5, characterized in that, The speedup ratio used by the Code-Evaluator agent to evaluate the quality of the current implementation is defined as the inverse relationship between the average execution time of the optimized implementation and the average execution time of the baseline implementation. Specifically, the speedup ratio is equal to the average execution time of the baseline implementation divided by the average execution time of the optimized implementation. The baseline implementation refers to the compilation result of the GCC compiler with the -O3 optimization level and the -ftree-vectorize automatic vectorization option enabled; The speedup ratio is used to quantify the performance improvement of the current optimized implementation relative to the baseline implementation. A speedup ratio greater than 1 indicates that the optimized implementation is better than the baseline. When the speedup ratio is lower than a preset threshold, the Code-Evaluator agent determines that the expected optimization goal has not been achieved and generates targeted performance feedback. The performance feedback includes execution time, speedup ratio, number of instructions per cycle, and cache miss rate, and is sent back to the Code-Optimizer agent to drive the next round of strategy adjustment.
8. A RISC-V-based multi-agent code automatic optimization system for implementing the method of any one of claims 1-7, characterized in that, The system includes: The Code-Finder agent is configured to receive the target project path, scan the source files, combine natural language description and abstract syntax tree feature analysis to locate the function to be optimized, and extract the function signature, loop structure, call relationship and context dependency information to form context constraint information; The Knowledge-Retriever agent is configured to retrieve matching API specifications, optimization patterns, and generation constraints from the RVV domain knowledge base based on the nesting depth, memory access patterns, and data type information contained in the context constraint information, thereby forming a structured domain knowledge package. The Code-Optimizer agent is configured to generate RVV vectorized optimized code by integrating the original code, the context constraint information, and the structured domain knowledge package. The Code-Updater agent is configured to write the generated optimized code back to the corresponding location in the original project, forming candidate implementations. The Code-Runner agent is configured to call the compilation and testing toolchain to compile, run and collect performance data on the updated project code, and output program execution results, correctness verification information and performance data. The Error-Executor agent is configured to parse error logs and generate correction suggestions when compilation or runtime exceptions occur, and then feed these suggestions back to the Code-Optimizer agent to trigger corrections. The Code-Evaluator agent is configured to evaluate the quality of the current implementation and generate performance feedback for successfully running candidate code, combining execution time, speedup ratio, correctness results and hardware performance indicators. If the expected optimization goal is not achieved, the performance feedback is sent back to the Code-Optimizer agent to drive the next round of strategy adjustment and code generation.
9. An electronic device, characterized in that, Includes one or more processors; Storage device for storing one or more programs; When one or more programs are executed by one or more processors, the one or more processors implement the RISC-V-based and multi-agent-based automatic code optimization method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed, it implements the automatic code optimization method based on RISC-V and multi-agent as described in any one of claims 1-7.