Software package source code optimization method of cross-instruction-set architecture
Through the combination of large language models and genetic algorithms, the inefficiency of instruction-level conflict identification and repair in RISC-V architecture migration is solved, and the automation optimization and performance improvement of cross-platform software packages are achieved. It is suitable for open source hardware ecosystem, high-performance computing, Internet of Things and edge computing and other fields.
Patent Information
- Application Number
- CN202510857916.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2045-06-25
AI Technical Summary
The existing technology lacks the ability to intelligently identify and repair underlying instruction-level conflicts in the RISC-V architecture migration, resulting in low migration efficiency, relying on manual modification and repeated testing, and lacks the integrated application of large language models, which affects the development efficiency of open source ecosystems.
The large language model (LLM) combined with genetic algorithm is used to combine software package source code optimization methods across instruction set architecture, including use case collection, optimization library construction, semantic feature analysis and automated code generation, and pre-trained models such as CodeT5 and CodeBERT for semantic understanding and conflict annotation, combined with genetic operator optimization strategies, the automation and performance improvement of cross-platform migration is achieved.
It effectively reduces the risk of semantic drift, improves the accuracy of instruction-level conflict repair, improves the automation level and performance of cross-platform migration, meets the hard constraints of embedded real-time systems, shortens the migration cycle, and reduces manual verification and debugging costs.
Smart Images

Figure CN120371387A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of embedded real-time system development, and particularly relates to a method for optimizing the source code of software packages across instruction set architectures. Background Art
[0002] RISC-V is an emerging open-source processor architecture with customizability, flexibility, and open source nature, and has received great attention in the software development environment. The construction of the RISC-V software ecosystem is closely related to the migration of existing software packages. Due to different architectures, some source codes cannot be directly compatible. At this time, it is necessary to modify the architecture-related parts of the source code so that the software can be installed and run on the specified architecture and the functions are basically the same. When migrating software from other architectures to the RISC-V architecture, this process involves rebuilding the existing software.
[0003] In the migration of the Linux operating system, due to the large number of software packages, the existing source code packages are often directly used through the build system to build software packages in the RISC-V environment. If the software package build fails, the migration developer will view the build failure log, analyze the problem cause, and then fix the problem to complete the migration of the software package.
[0004] Currently, the solutions to these build problems largely rely on the experience of the migration team, resulting in repetitive work and fragmented solutions, which has a non-negligible impact on the efficiency of the development of the RISC-V open source ecosystem.
[0005] The existing technical solutions generally focus on the refinement of the migration process. The migration process requires manual analysis of the technology stack, reverse engineering modeling, and manual marking of code modifications. The efficiency is limited when dealing with complex code or large-scale migrations. Moreover, only manual modification of the source code is used to handle configuration parameter changes, lacking the ability to intelligently identify and repair underlying instruction-level conflicts, and the migration efficiency is limited by repeated testing and manual adaptation.
[0006] Existing patents and academic research in the field of software migration lack the application of large language models, and have not yet formed an end-to-end solution that integrates semantic understanding of large language models, domain knowledge guidance, and dynamic optimization. Summary of the Invention
[0007] Object of the Invention: The technical problem to be solved by the present invention is to provide a method for optimizing the source code of software packages across instruction set architectures in view of the deficiencies of the prior art, including the following steps: Step 1, retrieve and collect various architecture migration optimization use cases from open source websites; Step 2, accumulate the verified optimization use cases through an optimization library; Step 3: Collect the source code of software packages with poor migration effects encountered in the project. The poor migration effect means that the code cannot be compiled on the new architecture or the running efficiency on the new architecture decreases significantly. A significant decrease in running efficiency on the new architecture refers to code with an execution time increase greater than the threshold. Collect the original architecture, target architecture, and the programming language used by the software package to be optimized, and provide them to the large language model (LLM) to generate optimization suggestions for migration between the original architecture and the target architecture. Step 4: Input the source code of the software package to be optimized and the optimization suggestions obtained in Step 3 into the large language model (LLM), and use chain-of-thought prompting to prompt the large language model (LLM) to generate more than two candidate optimized codes that are initially adapted to the target architecture. Step 5: Further evaluate and screen the candidate optimized codes generated in Step 4, and use genetic operators to eliminate the adaptation solutions with poor optimization by the large language model (LLM). Step 6: Retrieve excellent instances in the optimization library based on the semantic features of the current code. Step 7: Use the large language model (LLM) for further optimization: Provide the candidate optimized codes, excellent instances, and instructions to the large language model (LLM). The large language model (LLM) will take the excellent instances as examples, combine chain-of-thought prompting, and further optimize the candidate optimized codes using the genetic operator idea on the basis of the initial optimization to generate the final optimized code. Only one optimization scheme will be generated this time, which is the best optimized code that can be obtained in this iteration. Step 8: Evaluate the build speed improvement rate and functional consistency. When the improvement rate is greater than or equal to the threshold, it indicates that there is still room for improvement in the code. Upload the existing optimized code to Step 3 and iterate the optimization process until the performance gain of the generated code meets the conditions.
[0008] Step 1 includes: Construct a distributed data collection module based on the Scrapy framework, retrieve relevant repositories and code snippets using keywords, synchronously collect code files, commit histories, Issue discussions, and build logs, match architecture-related features in the code through regular expressions, filter out the results where the number of lines with feature hits divided by the total number of code lines is less than the threshold, and generate the original use case set D. raw 。
[0009] Step 1 also includes: Quantitatively evaluate the migration value of all use cases in the original use case set D. raw Define the migration value score S. migration Quantitatively evaluate the value of use cases from three dimensions: code quality, migration integrity, and community verification. , where W1, W2, and W3 are weight coefficients, S community is the community verification score, S completeness is the migration integrity score, S codeFor code quality scoring, the calculation formula is: , where X1 represents the cyclomatic complexity and X2 represents the Halstead complexity; Migration integrity score S completeness The calculation formula is: , where X3 represents the number of lines modified across architectures, X4 represents the total number of code lines, and X5 represents the number of successful builds; The calculation formula for community verification scoring is: , where X6 represents the number of positive comments, X7 represents the number of negative comments, X8 represents the total number of comments, X9 represents the number of recent Stars, and X 10 represents the total number of Stars; Filter S migration Use cases with S greater than or equal to the threshold are entered into the optimization library candidate set D candidate , and then for the optimization library candidate set D candidate Extract the instruction set conflict patterns for the use cases in it: Use the pre-trained model CodeT5 to analyze the differences Diff in the repository commit history, extract the architecture adaptation modification fragments, and generate conflict labels to annotate the conflict types; and based on D candidate In the conflict type distribution, use oversampling technology to enhance the differences Diff that appear less frequently than the threshold and have a historical repair failure rate greater than the threshold in the optimization library candidate set, so that this method can specifically strengthen the training of rare migration problems; Establish an incremental analysis pipeline: Use the Github Webhook to capture relevant repository Push / Issue / Pull Request patch submission events in real time, analyze the new code differences Diff and Issue discussions in real time, monitor the update events of the target repository, and when the current migration value S migration new and the previous migration value S migration old The change in the score satisfies |S migration new -S migration old | is greater than or equal to the threshold, dynamically update the optimization library candidate set D candidate .
[0010] Step 2 includes: In the optimization library construction stage, convert the migration cases that have passed functional and performance verification in the optimization library candidate set D candidate collected in Step 1 into reusable architecture adaptation templates, establish the mapping relationship from the software package source code A to the optimized software code B, and optimize the migration process of the software package from the original architecture C to the new architecture D; First, use the tree-sitter tool to parse the source code A of the software package and the optimized software code B to generate a standardized Abstract Syntax Tree (AST). Through the breadth-first traversal algorithm, the AST A and the AST B are respectively transformed into standardized node sequences Seq A and Seq B , ensuring that the nodes are arranged in hierarchical order: , , where AST A is the Abstract Syntax Tree of the source code A, AST B is the Abstract Syntax Tree of the optimized code B, and BFS is the breadth-first traversal algorithm; Next, use the pre-trained CodeBERT model to encode the serialized results to generate 768-dimensional semantic vectors. The semantic vector V A generated from the source code A and the semantic vector V B generated from the optimized code B are defined as: , , And add metadata tags to each code. The metadata Metadata is defined as: , where c is the number of cores, b is the memory bandwidth, and the instruction set architecture ISA extension is marked as a binary vector; Next, perform the construction of the FAISS index for the vector database and the calculation of similarity. Store all the semantic vectors {V A (i) ,V B (i)} N i=1 into the FAISS index, where N is the total number of optimization cases, and i represents the serial number of the optimization case. , V A (i) is the CodeBERT encoding result of the original code A of the i-th optimization case, and V B (i) is the CodeBERT encoding result of the optimized code B; Adopt a structure optimization based on inverted files and product quantization to improve storage and retrieval efficiency. The parameter configuration is: the number of clusters is 1024, the quantization bit width is 8, and the similarity metric uses cosine similarity; when there is new content (A, B new ) in the optimization library, dynamically update the FAISS index Index: , where indicates that whenever a new optimization use case appears, use to update the Index, where represents the encoding result of the newly added content.
[0011] Step 2 also includes: adopting a streaming clustering strategy, and triggering an index reconstruction every time the optimization library collects Y1 new optimization contents; if the optimization plan B new is the same as the B corresponding to A in the historical record old there is a performance difference, that is, satisfying |T new -T old | is greater than the threshold, then start the re-evaluation process, re-measure the compilation time T of the newly added optimization content new and the compilation time T of the historical optimization in the optimization library old , and retain the version with better performance; where B old is the original optimization plan for the software package source code A in the optimization library, and B new is the newly added optimization plan for the software package source code A.
[0012] Step 5 includes: for each candidate optimization code C i , calculate the functional correctness score R func ∈[0,1]: , where is the total number of test cases, is the code before optimization, is the input data, is the indicator function. If the function of the optimized software package does not change when migrated to the new architecture, then is 1, otherwise is 0; Calculate the compilation time optimization rate S using the following formula compile : , where T orig is the compilation time of the original code, and T Ci is the compilation time of the candidate optimization code; S compile ∈R, and R represents the set of real numbers; Calculate the runtime performance gain S using the following formula runtime : , where, T orig_run is the runtime of the original code, and T Ci_run is the runtime of the candidate optimization code, and S runtime ∈R.
[0013] Step 5 further includes: measuring the execution time of the original code and the candidate optimized code on the target architecture, and setting the following filtering conditions: R func > 0.95, passing at least 95% of the test cases, T Ci ≤ 1.5·T orig , and directly eliminating the candidate optimized code that does not meet any of the filtering conditions, and saving the candidate code that meets the conditions, denoted as Q.
[0014] Step 6 includes: for the candidate code Q, calculating the similarity score between the candidate code Q and the cases in the optimization library: , where is the metadata matching weight, V Q is the semantic vector of the candidate code Q, A i is the i-th use case in the optimization library, is the use case A i 's semantic vector, and the MetadataMatch function is defined as: , where k is the total number of ISA extension dimensions, is the support flag of the candidate code Q in the k-th ISA extension dimension, is the support flag of the optimization library use case A i in the k-th ISA extension dimension; For the candidate code Q, retrieve the Top-K most similar cases {A1, A2,..., A k} from the FAISS index, satisfying: , where is the set threshold, ; Through similarity retrieval, it is retrieved that there is a mapping of the source code and the optimized code similar to the code to be optimized in the optimization library: before performing the difference set operation, it is judged whether the excluded character class is empty or has no intersection with the main character class. If the condition is met, the difference set calculation process is directly skipped, and the result of the main character class is reused for encoding generation; In the mutation stage, non-local search ability is introduced through differential optimization instances to simulate the random mutation process in biological evolution, and the difference degree index between Q and A i is , and comprehensively considering the code semantic difference and the optimization strategy difference, it is defined as: , where and are weights, SemDiff is the difference between Q and A iThe semantic differences are calculated based on the CodeBERT vector distance: , StrategyDiff is the strategy difference between Q and A i which is calculated by the Jaccard distance according to the optimized action labels: , where T Q is the set of optimized strategy labels of the candidate code Q; is the set of optimized strategy labels of the optimization case A i ; Then, cases that meet the following conditions are screened from the optimization library: the metadata compatibility matching degree between the candidate code Q and the optimization library case A i is relatively high, that is, it meets and the functional correctness score R func (A i ) = 1, and sorting retrieval is used. The Top-1 is taken in descending order according to the difference degree D(Q, A i ) as the differential optimization template, which is the mutation stage of the genetic operator. By simulating the random mutation process in biological evolution through the differential optimization template, the limitation of the local optimal solution is broken through.
[0015] The present invention also provides an electronic device, including a processor and a memory. The memory stores program codes. When the program codes are executed by the processor, the processor is caused to execute the steps of the method.
[0016] The present invention also provides a storage medium storing a computer program or instruction. When the computer program or instruction runs on a computer, the steps of the method are executed.
[0017] The method of the present invention is mainly applied to fields such as open-source hardware ecosystem support, high-performance computing, Internet of Things and edge computing, and embedded real-time system development. This technology realizes the migration method of software packages from the original architecture to the new architecture, such as efficient code migration and performance tuning from the x86 architecture to the RISC-V architecture, by analyzing the semantic features and instruction set differences of the source code, combining large language models and automated optimization algorithms. Its core value lies in solving the compatibility conflicts, performance losses, and development efficiency problems in cross-platform migration, providing technical support for the coordinated development of software and hardware in the open-source ecosystem.
[0018] This method can be applied to the following fields: Open-source Hardware Ecosystem: In the RISC-V open-source instruction set ecosystem, cross-architecture optimization technologies can accelerate the adaptation of software ecosystems. For example, when migrating a Linux distribution from x86 to the RISC-V architecture, compatibility issues with instruction set extensions can be resolved through automated code conversion tools, improving the compatibility and performance of software packages in fragmented hardware ecosystems.
[0019] High-performance Computing: For heterogeneous computing clusters in supercomputing centers (such as x86 CPUs + ARM acceleration cards), multi-target compilation optimization technologies are used to generate parallel code adapted to different instruction sets, solving performance bottlenecks in cross-architecture task scheduling.
[0020] Internet of Things and Edge Computing: Internet of Things devices are usually designed based on low-power RISC-V chips, but after migrating existing x86 / ARM software, energy efficiency may decrease due to instruction redundancy. This technology enables migrated software packages to balance functional integrity and low-power requirements in resource-constrained devices.
[0021] Embedded Real-time System Development: In real-time embedded scenarios such as automotive electronics and industrial control, the deterministic execution characteristics of the RISC-V architecture are crucial. Optimization technologies can ensure that migrated code meets hard real-time constraints, for example, eliminating unpredictable branch jumps through static analysis, optimizing the instruction pipeline efficiency of interrupt handling functions, and adapting to the physical memory protection mechanism of RISC-V to enhance system security and reliability.
[0022] Beneficial Effects: The present invention improves the automation level and performance of cross-instruction set software migration by integrating the semantic understanding of large language models and the dynamic optimization of genetic algorithms. Compared with traditional manual migration methods, this solution adopts a two-stage task decomposition strategy, effectively reducing the risk of semantic drift and improving the accuracy of instruction-level conflict repair. Based on the semantic feature retrieval mechanism of tree-sitter, combined with similar and different instances in the optimization knowledge base, it realizes the coordination of global exploration and local development of code optimization, breaking through the local optimum limit. The optimized code exhibits high energy efficiency and low latency characteristics on target architectures such as RISC-V, meeting the hard constraints of embedded real-time systems. At the same time, this method improves the cross-platform adaptation success rate of open-source software packages, accelerates the construction of emerging instruction set ecosystems, and provides compatibility guarantee and performance tuning support for heterogeneous computing scenarios. Description of the Drawings
[0023] Figure 1 It is a flowchart of the method of the present invention.
[0024] Figure 2 It is a flowchart of genetic operators. Detailed Implementation Manner
[0025] Such as Figure 1As shown in the figure, the present invention provides a method for optimizing the source code of software packages across instruction set architectures, including the following steps: Step 1, retrieve and collect various architecture migration optimization use cases from the Github / Gitee open source websites. Build a distributed data collection module based on the Scrapy framework, and retrieve relevant repositories and code snippets with keyword combinations such as "x86 to RISC-V", "RISC-V PORT", "ISAmigration", etc. Synchronously collect code files, commit history, Issue discussions, and build logs. Match the architecture-related features in the code through regular expressions (such as the feature -march=rv64gc in the RISC-V architecture migration task), filter out the results where the number of lines with feature hits divided by the total number of code lines is less than the threshold of 0.3, and generate the original use case set D raw . After obtaining the use case set, quantitatively evaluate the migration value of all use cases. Define the migration value scoring model S migration , and quantitatively evaluate the value of use cases from three dimensions: code quality, migration integrity, and community verification: , where S code is the code quality score, which is based on the Halstrad complexity, cyclomatic complexity, and the output results of static analysis tools to calculate the code maintainability index: , where X1 represents the cyclomatic complexity and X2 represents the Halstead complexity; The migration integrity score S completeness The calculation formula is: , where X3 represents the number of lines modified across architectures, X4 represents the total number of code lines, and X5 represents the number of successful builds; The calculation formula for the community verification score is: , where X6 represents the number of positive comments, X7 represents the number of negative comments, X8 represents the total number of comments, X9 represents the recent number of Stars (Star is the total number of Stars accumulated by a project on the open source website that users click to indicate value, reflecting the long-term influence and community recognition of the project), and X 10 represents the total number of Stars; And set the weights W1 = 0.5, W2 = 0.3, W3 = 0.2. This method screens S migration≥ 0.7 of the use cases into the optimization library candidate set D candidate . Then for the optimization library candidate set D candidateUse cases to extract the instruction set conflict mode: Use the pre-trained model CodeT5 to analyze the differences (Diff) in the repository commit history, extract the architecture adaptation modification fragments, and generate conflict labels to annotate the conflict types. For example, for the input Diff fragment -__m256i a;+vint32m8_t b, the output label is SIMD_INTRINSIC_MISMATCH; and based on the candidate conflict type distribution in the middle, adopt oversampling technology to enhance the differences (Diff) that appear less than the threshold of 5% in the optimization library candidate set and the historical repair failure rate is greater than the threshold of 30%, so that this method can specifically strengthen the training of rare migration problems; Establish an incremental analysis pipeline: Use the Github Webhook to capture Push / Issue / Pull Request patch submission events of relevant repositories in real time, analyze the newly added code differences (Diff) and Issue discussions in real time, and monitor the update events of the target repository. When the current migration value S migration new is the same as the previous migration value S migration old and the change amount of the score satisfies |S migration new -S migration old | is greater than or equal to the threshold of 0.05, dynamically update the optimization library candidate set D candidate .
[0026] Step 2: Accumulate verified optimization examples through the optimization library, reduce repetitive manual labor, provide excellent templates for subsequent optimization processes, and improve the accuracy of optimization. In the optimization library construction stage, this method converts the optimization use cases collected in Step 1 into reusable architecture adaptation templates. The core lies in establishing the mapping relationship from the software package source code (A) to the optimized software code (B), and optimizing the migration process of the software package from the original architecture (C) to the new architecture (D). First, use the tree-sitter tool to parse the source code (A) and the optimized code (B) to generate a standardized abstract syntax tree (AST), and use the pre-trained CodeBERT model to encode the AST to generate 768-dimensional semantic vectors: , , Among them, the AST sequence is generated through breadth - first traversal, preserving key semantic information such as variable scopes and control flows. In addition, the present invention adds metadata tags to each piece of code, where c is the number of cores, b is the memory bandwidth (GB / s), and the instruction set architecture ISA (which defines the set of all instructions that a computer can execute, each instruction specifying what operation the computer performs, the address space where the operands are stored, and the operand types) extension is marked as a binary vector (e.g., RISCV V extension = 1, otherwise = 0): , Next, the FAISS index construction and similarity calculation are performed. The semantic vectors {V A (i) ,V B (i)} N i=1 of all code pairs are stored in the FAISS index. The structure based on inverted files and product quantization (IVFPQ) is adopted to optimize the storage and retrieval efficiency, and the parameter configuration is as follows: the number of clusters is 1024, the quantization bit width is 8, and the cosine similarity is used as the similarity metric. When there are new optimization cases (A, B new ) in the library, the FAISS index is updated dynamically: , A streaming clustering strategy is adopted, and the index reconstruction is triggered every time 100 new cases are accumulated to balance efficiency and consistency. If there is a performance difference (|T new - T old |>10%) between the new case B new and the B old corresponding to the same A in the historical records, where B old is the original optimization solution for the source code A in the optimization library, and B new is the newly added optimization solution for A, then the re - evaluation process is started, Tnew and Told are measured again, and the version with better performance is retained.
[0027] Step 3, in order to precisely optimize the source code of sub - optimal software packages, this method collects the source code of software packages with poor migration effects encountered in the project. The poor migration effect means that it cannot be compiled on the new architecture or the running efficiency on the new architecture drops significantly. The significant drop in running efficiency on the new architecture refers to the code with an execution time increase greater than the threshold of 15%. Specific information such as the original architecture and the target architecture, and the programming language used by the software package to be optimized is collected and provided to the LLM (Large Language Models) to generate optimization suggestions for the migration between these two architectures. The prompt content is as follows: [Target] Provide RISC - V migration optimization solutions for the following x86 architecture software package source code, requirements: 1. The compilation time is reduced by more than 20% (based on the same build environment); 2. The functional consistency reaches 100%; 3. Make full use of the RISC-V features; 4. Do not generate optimized code, but only output multiple optimization suggestions; [Input context]; Source code: {Code}; Code language: {C++}; [Output]; {Optimization suggestions}; There is an original x86 architecture function isl_set_subtract, which belongs to the isl software package and its function is to calculate the difference set of two sets. The isl_set_subtract function is called 2 times on the x86 architecture platform, with a cumulative running time of about 248 microseconds and an average call time of about 124 microseconds. In contrast, under the same input conditions and logic, this function is called 1634 times on the RISC-V architecture platform, with a cumulative running time reaching about 988.2 milliseconds and an average call time of about 605 microseconds. According to the above data calculation, the total execution time of this function on the RISC-V platform shows a performance degradation of about 3983 times compared to the x86 platform, that is, the running time increases by about 398349.6%, and the migration effect is not good. So this function is input to the large language model, replacing the Code in the prompt with this function, and outputting modification opinions for the code to be optimized: 1. Precondition check: Add a judgment of set intersection before calling type conversion to avoid meaningless operations.
[0028] 2. Optimization of the underlying difference set algorithm: Develop a native set-level difference operation isl_set_subtract_direct to bypass the set_to_map conversion.
[0029] 3. Reduce type conversion: Refactor the code logic and prefer to use the native API of the set to replace the mapping conversion.
[0030] 4. Pre-computation of set relationships: Cache the intersection and union states of sets to reduce repeated calculations (such as reusing the result of isl_set_is_disjoint).
[0031] 5. Memory reuse mechanism: Enable object pool caching for the frequently called set_to_map results.
[0032] When the present invention uses a large language model to generate optimized code for a software package, it preferentially generates optimization suggestions and then generates optimized code, rather than directly generating optimized code in a single stage. In contrast, the method of the present invention is more effective. The advantage of this method is that it reduces the complexity of the generation process by means of task decomposition, improves the accuracy of the code generated by the large language model, and avoids the semantic drift problem caused by multi-task processing.
[0033] Step 4, input the source code of the software package to be optimized and the optimization suggestions obtained in step 3 into the LLM, and use chain of thought to prompt the large language model to generate multiple optimized codes that are initially adapted to the target architecture, corresponding to Figure 2 The initial population creation stage of the genetic operator, the specific prompt is as follows: You are a system-level software engineer proficient in RISC-V architecture migration and performance optimization. Your task is to migrate the following software package source code under the x86 architecture to the RISC-V architecture and optimize its performance on the premise of maintaining exactly the same function. Please think about the following questions in stages and output multiple high-quality optimized code versions.
[0034] [Task1: Understand functional semantics]; 1. Read the given C / C++ / Rust / Asm language source code to clarify its core function.
[0035] 2. Ensure that the migrated RISC-V version is 100% functionally consistent with the original version.
[0036] [Task2: Analyze optimization suggestions]; Obtain the optimization path according to the optimization suggestions and analyze the necessary modifications to the source code.
[0037] [Task3: Optimize code output]; Output multiple optimized versions of the code, and each solution should reflect different optimization strategies.
[0038] [Input source code]; {Code}; [Input language] C / C++ / Rust / Asm; [Target]; Compilation time reduced by >20%; Guarantee 100% functional consistency; Optimize using RISC-V architecture features; Output multiple optimized code versions; This step can complete the replacement of instruction sets in the software package source code without manual analysis and modification, greatly reducing the workload of researchers. According to the modification suggestions, three solutions were generated: Solution 1 introduced a pre-judgment logic to reduce unnecessary overhead of set difference operations. Specifically, before performing the set difference calculation, the intersection detection of the input sets is first performed. If there is no intersection between the two sets, the original set is directly returned to avoid entering the subsequent complex set difference process, thereby improving the efficiency of the code. In addition, the isl_set_subtract_direct interface was introduced to bypass the intermediate set-to-map type conversion operation to reduce unnecessary data structure mapping and memory allocation; Solution 2 constructed a global memory pool mechanism to reduce the dynamic memory allocation overhead during the set type conversion. The memory pool is initialized during the first call, and the map type object is reused through the pre-allocated resources in the pool subsequently, avoiding frequent heap memory application and release. And the set difference operation is still completed by converting the set into a mapping form and then performing the set difference calculation; while Solution 3 maintains the intersection status of the set pairs in the global cache to avoid repeated intersection judgments on the set pairs with known relationships. When determining whether a set pair intersects for the first time, the result is cached in the least recently used (LRU) structure; if the same set pair is encountered again subsequently, the cache is directly hit, and a quick decision is made on whether to perform the set difference operation. This method is suitable for scenarios where the relationship between sets is stable and the reuse rate is high, and can effectively reduce the redundancy of judgment and operation, improving the overall throughput efficiency of the system.
[0039] Step 5, after generating multiple preliminarily optimized adaptation codes through the large language model, the present invention further evaluates and screens the candidate optimization codes, corresponding to the fitness and selection stage of each individual in the computational population of genetic operators, as Figure 2 shown, eliminating the adaptation solutions with poor optimization by the LLM. Specifically, the evaluation metrics for this step include functional correctness verification and compilation time, and the generated codes are sorted according to the constructed performance metrics, and the software package codes with consistent functions on the migrated homologous platform and high speed improvement are preferentially retained as candidate optimization codes.
[0040] For each candidate optimization code C i , calculate the functional correctness score R func ∈[0,1]: , where is the total number of test cases, is the code before optimization, is the input data, is the indicator function. If the function of the software package does not change after migration to the new architecture, then is 1, otherwise is 0; Compilation time optimization rate S compile ∈R: , where T orig is the compilation time of the original code, and T Ci is the compilation time of the candidate optimized code; Runtime performance gain S runtime ∈R: , Measure the execution times of the original code and the candidate optimized code on the target architecture. The hard filtering condition is that R func > 0.95, passing at least 95% of the test cases, that is, the compilation time does not exceed 1.5 times that of the original code. Candidate optimized codes that do not meet any of the conditions are directly eliminated.
[0041] Step 6, retrieve in the optimization code optimization library based on the semantic features of the current code. The cases in the optimization library retrieved by this method that highly match the semantics of the source code of the software package to be optimized include similar optimization instances and differential optimization instances, corresponding to Figure 2 the crossover and mutation stages in the genetic operator process respectively. Similar optimization instances are excellent examples that match the current optimized code scheme, while differential optimization instances are cases with a large difference from the optimization scheme of the candidate optimized code. For candidate code Q, calculate the similarity score between candidate code Q and the cases in the optimization library (the reusable architecture adaptation template in the optimization library after step 2): , where is the metadata matching weight, and the MetadataMatch function is defined as: , where k is the total number of ISA expansion dimensions.
[0042] For target code Q, retrieve the Top-K most similar cases {A1, A2,..., A K} from the FAISS index, satisfying: , In this embodiment takes the value of 0.85.
[0043] Through similarity retrieval, source code and optimized code mappings similar to the code to be optimized are retrieved in the knowledge base: The character class difference set processing logic in the PCRE2 library is structurally similar to the isl_set_subtract function. As a widely used regular expression engine, the original processing logic of the character class difference set in PCRE2 includes steps such as parsing the main character class and the exclusion class, converting to a bitmap structure, and then performing the difference set operation. This processing chain is highly consistent with the operation mode of the code to be optimized, isl_set → map → subtract → set. The core idea of the improvement strategy for this use case for the difference set operation is to judge whether the exclusion character class is empty or has no intersection with the main character class before performing the difference set operation. If the condition is met, the difference set calculation process is directly skipped, and the result of the main character class is reused for encoding generation.
[0044] In the mutation stage, non-local search ability is introduced through differential optimization examples to simulate the random mutation process in biological evolution. Specifically, the difference degree index is , which combines the code semantic difference and the optimization strategy difference and is defined as: , where , , SemDiff is the semantic difference, calculated based on the CodeBERT vector distance: , The strategy difference StrategyDiff calculates the Jaccard distance based on the optimization action labels: , where T Q and are the sets of optimization strategy labels.
[0045] Then, cases that meet and R func (A i ) = 1 are screened from the optimization library, and sorting retrieval is used to take the Top-1 in descending order of the difference degree D(Q, A i ) as the differential optimization template.
[0046] For example, a differential optimization solution is retrieved from the optimization library. This solution introduces a global caching mechanism, uniquely identifies character classes through a hash function, and uses an array to cache historical conversion results. When the cache is hit, the pre-computed bitmap can be directly returned, avoiding repeated execution of class_to_bitmap conversion. This optimization idea reflects the combination of cache reuse and lazy evaluation strategies, improves the diversity ability of the model in the structural search space, and promotes the discovery of the global optimal solution.
[0047] Step 7, use the large language model to optimize again. In step 6, the excellent examples retrieved from the optimization library are obtained, and the candidate optimization code, excellent optimization cases and instructions are provided to the large language model. The large language model will use the excellent cases as examples and combine the chain thinking prompts to further optimize the candidate optimization code based on the initial optimization and generate the final optimized code. This time, only one optimization solution is generated, which is the best optimization code that can be obtained in this iteration.
[0048] Step 8: Evaluate the build speed improvement rate and functional consistency. When the improvement rate is greater than or equal to the threshold (generally refers to the build speed improvement rate speedup ≥ 10% in a single round of optimization), it indicates that there is room for code improvement. Upload the existing optimized code to step 3 and iterate the optimization process until the performance gain of the generated code stabilizes.
[0049] The present invention sets that when the speed improvement rate of three consecutive rounds is less than 10%, the iteration ends, indicating that the most optimized code has been obtained, and the optimization knowledge base is fed back to increase the diversity of optimization schemes in the optimization knowledge base. After evaluating the optimized code, the total time consumption is about 681.1 milliseconds. After calculation, the construction time improvement rate is 31.07%. If it is greater than 10%, the iteration process will continue. Finally, after 2 iterations, the iteration ends, and the final optimized code that can be provided by this method is obtained.
[0050] Compared with the existing method of calling a large number of team members to carry out software package migration optimization work, a lot of manpower and material resources are saved, because manual migration is inefficient and requires manual search, matching and modification work. Under high-intensity delivery pressure, errors are prone to occur. The present invention shows significant advantages in terms of automation and resource conservation. Traditional manual migration relies on manual review of source code line by line and manual adjustment of instruction set related logic. This method uses the semantic understanding ability of LLM to automatically parse the platform-dependent features in the source code, and accurately locates the migration point based on AST-driven pattern matching technology, without the need for engineers to check line by line, greatly reducing the workload of manual code review. At the same time, the cross-architecture adaptation templates accumulated in the optimization knowledge base can automatically match the current task, generate verified efficient code, and avoid repeated development and trial and error costs. In addition, the dynamic screening mechanism of the genetic algorithm automatically eliminates inefficient candidate optimization codes, retaining only functionally correct and performance-optimized solutions, further reducing the investment in manual verification and debugging. Users do not need to deeply understand the hardware characteristics of the target architecture or manually write adaptation logic. They only need to provide source code and migration targets, and the system can output optimization results through an end-to-end automated process, significantly shortening the migration cycle and reducing dependence on professional engineering teams. It is especially suitable for large-scale software ecosystem migration scenarios, providing enterprises with efficient and intensive solutions in terms of human resource allocation and R&D costs.
[0051] The present invention provides a method for optimizing the source code of software packages across instruction set architectures. There are many methods and approaches to specifically implement this technical solution. The above description is only a preferred embodiment of the present invention. It should be noted that for those of ordinary skill in the art of this technology, without departing from the principle of the present invention, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of the present invention. Each component not clearly defined in this embodiment can be implemented by existing technologies.
Claims
1. A method for optimizing the source code of software packages across instruction set architectures, characterized in that, It includes the following steps: Step 1, retrieve and collect various architecture migration optimization use cases from open-source websites; Step 2, accumulate the verified optimization use cases through an optimization library; Step 3, collect the source code of software packages with poor migration effects encountered in the project. The poor migration effect means that it cannot be compiled on the new architecture or the running efficiency on the new architecture drops significantly. The running efficiency dropping significantly on the new architecture refers to the code whose execution time increase rate is greater than the threshold; Collect the original architecture, target architecture, and the programming language used by the software package to be optimized, and provide them to the large language model LLM to generate optimization suggestions for the migration between the original architecture and the target architecture; Step 4, input the source code of the software package to be optimized and the optimization suggestions obtained in Step 3 into the large language model LLM, and use chain-of-thought to prompt the large language model LLM to generate more than two candidate optimized codes that are initially adapted to the target architecture; Step 5, further evaluate and screen the candidate optimized codes generated in Step 4, and use genetic operators to eliminate the adaptation solutions poorly optimized by the large language model LLM; Step 6, retrieve in the optimization library based on the semantic features of the current code to obtain excellent instances; Step 7, use the large language model LLM to optimize again: provide the candidate optimized codes, excellent instances, and instructions to the large language model LLM. The large language model LLM will take the excellent instances as examples, combine chain-of-thought prompts, and further optimize the candidate optimized codes using the genetic operator idea on the basis of the initial optimization to generate the final optimized code. Only one optimization solution will be generated this time, which is the best optimized code that can be obtained in this iteration; Step 8, evaluate the build speed improvement rate and function consistency. When the improvement rate is greater than or equal to the threshold, it indicates that there is still room for improvement in the code. Upload the existing optimized code to Step 3 and iterate the optimization process until the performance gain of the generated code meets the conditions.
2. The method according to claim 1, wherein Step 1 includes: building a distributed data collection module based on the Scrapy framework, retrieving relevant repositories and code snippets with keywords, synchronously collecting code files, commit history, Issue discussions, and build logs, matching architecture-related features in the code through regular expressions, filtering out the results where the number of lines with feature hits divided by the total number of code lines is less than the threshold, and generating the original test case set D raw .
3. The method according to claim 2, characterized in that, Step 1 further includes: quantifying and evaluating the migration value of all use cases in the original use case set D raw Define a migration value score S migration to quantitatively evaluate the value of use cases from three dimensions: code quality, migration integrity, and community verification , where W1, W2, and W3 are weight coefficients, and S community is the community verification score, and S completeness is the migration integrity score, and S code is the code quality score, and the calculation formula is: , Where X1 represents the cyclomatic complexity, and X2 represents the Halstead complexity; Migration integrity score S completeness The calculation formula is as follows: , Where X3 represents the number of cross-architecture modified lines, X4 represents the total number of code lines, and X5 represents the number of successful builds; The community verification score calculation formula is: , Among them, X6 represents the number of positive comments, X7 represents the number of negative comments, X8 represents the total number of comments, X9 represents the recent Star number, and X 10 represents the total number of Stars; Screening S migration Use cases greater than or equal to the threshold enter the optimization library candidate set D candidate , and then for the optimization library candidate set D candidate Extract the instruction set conflict patterns for the use cases in it: Use the pre-trained model CodeT5 to analyze the differences Diff in the repository submission history, extract the architecture adaptation modification fragments, and generate conflict labels to annotate the conflict types; and based on D candidate In the conflict type distribution, adopt oversampling technology to enhance the differences Diff that appear less frequently than the threshold and have a historical repair failure rate greater than the threshold in the optimization library candidate set; Build an incremental analysis pipeline: Real-time capture of Push / Issue / Pull Request patch submission events in relevant repositories through the Github Webhook, real-time analysis of the differences in newly added code Diff and Issue discussions, monitoring of update events in the target repository, and when the current migration value S migration new compared with the previous migration value S migration old The change in the score satisfies |S migration new -S migration old | is greater than or equal to the threshold, dynamically update and optimize the candidate set D of the library candidate .
4. The method according to claim 3, wherein Step 2 includes: in the optimization library construction phase, converting the migration cases that have passed functional and performance verification in the optimization library candidate set D collected in Step 1 into reusable architecture adaptation templates, establishing a mapping relationship from the software package source code A to the optimized software code B, and optimizing the migration process of the software package from the original architecture C to the new architecture D; candidate Among them, the migration cases that have passed functional and performance verification are converted into reusable architecture adaptation templates, a mapping relationship from the software package source code A to the optimized software code B is established, and the migration process of the software package from the original architecture C to the new architecture D is optimized; First, use the tree-sitter tool to parse the software package source code A and the optimized software code B to generate a standardized Abstract Syntax Tree (AST). Through the breadth-first traversal algorithm, transform the AST A and the AST B into standardized node sequences Seq A and Seq B respectively, ensuring that the nodes are arranged in hierarchical order: , , Among them, AST A is the abstract syntax tree of source code A, and AST B is the abstract syntax tree of optimized code B. BFS is the breadth-first search algorithm; Next, the pre-trained CodeBERT model is used to encode the serialized results to generate 768-dimensional semantic vectors. The semantic vector V generated by the source code A A and the semantic vector V generated by the optimized code B B are defined as: , , And add metadata tags to each code. The metadata Metadata is defined as: , Where c is the number of cores, b is the memory bandwidth, and the instruction set architecture ISA extension is marked as a binary vector; Next, the FAISS index of the vector database is constructed and the similarity is calculated. The semantic vectors of all code pairs {V A (i) ,V B (i)} N i=1 are stored in the FAISS index, where N is the total number of optimization use cases, and i represents the serial number of the optimization use case. , V A (i) is the CodeBERT encoding result of the original code A of the i-th optimization use case, and V B (i) is the CodeBERT encoding result of the optimized code B; Adopt a structure based on inverted files and product quantization to optimize storage and retrieval efficiency. The parameter configuration is as follows: the number of clusters is 1024, the quantization bit width is 8, and the cosine similarity is used as the similarity metric; when there is new content (A, B new ) in the optimized library, dynamically update the FAISS index Index: , Among them indicates that whenever a new optimization use case appears, use to update the Index, where represents the encoding result of the newly added content.
5. The method according to claim 4, wherein Step 2 further includes: adopting a streaming clustering strategy, and triggering an index reconstruction every time the library collects Y1 new optimization contents; if optimization scheme B new is the same as B corresponding to A in the historical record old has a performance difference, that is, satisfies |T new -T old | is greater than the threshold, then start the re-evaluation process, re-measure the compilation time T of the newly added optimization content new and the compilation time T of the historical optimization in the optimization library old , and retain the version with better performance; where B old is the original optimization scheme for software package source code A in the optimization library, and B new is the newly added optimization scheme for software package source code A.
6. The method according to claim 5, wherein Step 5 includes: for each candidate optimized code C i , calculate the functional correctness score R func ∈[0,1]: , Among them is the total number of test cases, is the code before optimization, is the input data, is an indicator function. If the function of the optimized software package does not change after migrating to a new architecture, then is 1, otherwise is 0; The compilation time optimization rate S is calculated using the following formula compile : , Among which T orig is the compilation time of the original code, T Ci is the compilation time of the candidate optimized code; S compile ∈R, where R represents the set of real numbers; The runtime performance gain S is calculated using the following formula runtime :[[]]END]] , Among them, T orig_run is the running time of the original code, and T Ci_run is the running time of the candidate optimized code, and S runtime ∈R.
7. The method according to claim 6, characterized in that Step 5 further includes: measuring the execution time of the original code and the candidate optimized code on the target architecture, and setting the following filtering conditions: R func > 0.95, , and directly eliminating the candidate optimized code that does not meet any of the filtering conditions, and saving the candidate code that meets the conditions, denoted as Q.
8. The method according to claim 7, characterized in that, Step 6 includes: for the candidate code Q, calculate the similarity score between the candidate code Q and the cases in the optimization library: , Among them, is the metadata matching weight, V Q is the semantic vector of candidate code Q, A i is the i-th use case in the optimization library, is the use case A i of the semantic vector, and the MetadataMatch function is defined as: , where k is the total number of ISA extension dimensions, is the support flag of the candidate code Q in the k-th ISA extension dimension, is the optimized library use case A i in the k-th ISA extension dimension; For the candidate code Q, retrieve the Top-K most similar cases {A1, A2,..., A k} from the FAISS index, satisfying: , wherein is a set threshold value; Through similarity retrieval, it is retrieved that there is a mapping between the source code similar to the code to be optimized and the optimized code in the optimization library: before performing the difference set operation, judge whether the excluded character class is empty or has no intersection with the main character class. If the condition is met, directly skip the difference set calculation process and reuse the result of the main character class for encoding generation; In the mutation stage, non-local search ability is introduced through differential optimization examples to simulate the random mutation process in biological evolution, and the difference index between Q and A i is the difference degree index , which comprehensively considers the code semantic difference and the optimization strategy difference, is defined as: , Among them and are weights, and SemDiff is the semantic difference between Q and A i calculated based on the CodeBERT vector distance: , StrategyDiff is the strategy difference between Q and A i obtained by calculating the Jaccard distance based on the optimized action tags: , Among which T Q is the set of optimization strategy tags for candidate code Q; is the optimization use case A i 's set of optimization strategy tags; Next, select cases from the optimization library that meet the following conditions: The candidate code Q and the use case A in the optimization library i have a high degree of metadata compatibility matching, that is, they meet and the functional correctness score R func (A i ) = 1, and use sorted retrieval. Take the Top-1 in descending order according to the difference degree D(Q, A i ) as the differential optimization template. For the mutation stage of the genetic operator, simulate the random mutation process in biological evolution through the differential optimization template to break through the limitation of the local optimal solution.
9. An electronic device, characterized in that, It includes a processor and a memory. The memory stores program code. When the program code is executed by the processor, the processor is caused to execute the steps of the method according to any one of claims 1 to 8.
10. A storage medium, characterized in that, Store a computer program or instruction. When the computer program or instruction runs on a computer, it executes the steps of the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Hybrid front-end framework migration method based on AST and LLM
CN117608656A
Multi-program language cross-instruction-set architecture transplantation method
CN118963831A
Code review and optimization method driven by large language model
CN119512556A
Automatic code security optimization method based on large language model
CN120046149A
Cited By
Structured data calculation result optimization method and system
CN120724010A
Optimization design method and system for mechanical part driven by large model
CN120822304A
Library migration recommendation method based on retrieval enhancement generation
CN120950480A
Optimization program determination method and device based on large model and intelligent agent
CN121349463A