A cross-instruction set architecture software package source code optimization method
Through the combination of large language models and genetic algorithms, cross-architecture optimization code is automatically generated, which solves the problem of instruction-level conflicts in RISC-V architecture migration, and realizes efficient code migration and performance tuning, which is suitable for open source hardware ecosystems, embedded real-time systems and other fields.
Patent Information
- Application Number
- CN202510857916.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-08-22
- Estimated Expiration
- 2045-06-25
AI Technical Summary
The prior art lacks the ability to intelligently identify and repair instruction-level conflicts in RISC-V architecture migration, resulting in inefficient migration, relying on manual modifications and lack of end-to-end automation solutions.
The large language model (LLM) combined with genetic algorithm is used to analyze the semantic features and instruction set differences of source code, and cross-architecture optimization code is automatically generated, and optimization libraries and pre-trained models are used for code migration and performance tuning, including use case collection, evaluation, optimization suggestions generation and code generation.
It improves the automation level and performance of cross-instruction set software migration, reduces the risk of semantic drift, improves the accuracy of instruction-level conflict repair, meets the hard constraints of embedded real-time systems, and accelerates the construction of emerging instruction set ecosystems.
Smart Images

Figure CN120371387B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of embedded real-time system development, and in particular relates to a method for optimizing software package source code across instruction set architectures. Background Art
[0002] RISC-V is an emerging open-source processor architecture. Its customizability, flexibility, and open-source nature have garnered significant attention in software development. Building the RISC-V software ecosystem is closely tied to migrating existing software packages. Due to architecture differences, some source code is not directly compatible. In these cases, architecture-specific modifications are required to enable software to install and run on a specific architecture while maintaining consistent functionality. Migrating software from other architectures to the RISC-V architecture involves rebuilding existing software.
[0003] Due to the large number of software packages involved in Linux operating system migrations, the build system often uses existing source code packages to build them in the RISC-V environment. If a package build fails, the migration developer will review the build failure log, analyze the cause, and then fix the problem to complete the package migration.
[0004] Currently, solutions to these build problems rely heavily on the experience of the migration team, resulting in duplicated work and fragmented solutions, which has a significant impact on the efficiency of the RISC-V open source ecosystem development.
[0005] Existing technical solutions generally focus on refining the migration process. The migration process requires manual analysis of the technology stack, reverse engineering modeling, and manual marking of code modifications. This has limited efficiency when faced with complex code or large-scale migrations. Furthermore, configuration parameter changes are only addressed by manually modifying the source code, lacking the ability to intelligently identify and repair underlying instruction-level conflicts. Furthermore, 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 an end-to-end solution that integrates semantic understanding, domain knowledge guidance, and dynamic optimization of large language models has not yet been formed. Summary of the Invention
[0007] Purpose of the invention: The technical problem to be solved by the present invention is to address the deficiencies of the existing technology and provide a method for optimizing the source code of a software package across instruction set architectures, comprising the following steps:
[0008] Step 1: Search and collect various architecture migration optimization use cases from open source websites;
[0009] Step 2: Accumulate verified optimization use cases through the optimization library;
[0010] Step 3: Collect the source code of software packages encountered in the project that have poor migration effects. Poor migration effects refer to: failure to compile on the new architecture or significant reduction in running efficiency on the new architecture. Significant reduction in running efficiency on the new architecture refers to code whose execution time increases by more than a threshold. Collect the source code of the original architecture, target architecture, and programming languages used in the software packages 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.
[0011] 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). Use chain thinking to prompt the large language model (LLM) to generate two or more candidate optimization codes that are initially adapted to the target architecture.
[0012] Step 5: Further evaluate and screen the candidate optimized codes generated in step 4, using genetic operators to eliminate poorly optimized adaptation solutions for the large language model (LLM).
[0013] Step 6: Search the optimization library based on the semantic features of the current code to obtain excellent examples;
[0014] Step 7: Use the Large Language Model (LLM) for further optimization: Provide the candidate optimization code, excellent examples, and instructions to the Large Language Model (LLM). The Large Language Model (LLM) will use the excellent examples as examples and combine chain thinking prompts to further optimize the candidate optimization code based on the initial optimization using genetic operators to generate the final optimized code. This time, only one optimization solution is generated, which is the best optimized code obtained in this iteration.
[0015] In 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 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 meets the requirements.
[0016] Step 1 includes: building a distributed data collection module based on the Scrapy framework, searching related repositories and code snippets with keywords, synchronously collecting code files, submission history, issue discussions, and build logs, matching architecture-related features in the code with regular expressions, filtering out the results where the number of feature hit lines divided by the total number of code lines is less than a threshold, and generating the original use case set D. raw .
[0017] Step 1 also includes: the original use case set D raw Quantitatively evaluate the migration value of all use cases in the migration value evaluation: define the migration value score S migration , quantitatively evaluate the value of use cases from three dimensions: code quality, migration completeness, and community verification:
[0018] ,
[0019] Where W1, W2, W3 are weight coefficients, S community Score community verification, S completeness Score for migration completeness, S code Score the code quality using the formula:
[0020] ,
[0021] Where X1 represents cyclomatic complexity and X2 represents Halstead complexity;
[0022] Migration integrity score S completeness The calculation formula is:
[0023] ,
[0024] Where X3 represents the number of modified lines across architectures, X4 represents the total number of code lines, and X5 represents the number of successful builds;
[0025] The formula for calculating the community verification score is:
[0026] ,
[0027] 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 number of recent stars, and X 10 Indicates the total number of Stars;
[0028] Screening S migration Use cases greater than or equal to the threshold enter the optimization library candidate set D candidate , then optimize the candidate set D candidate Extract instruction set conflict patterns in the use case: Use the pre-trained model CodeT5 to analyze the differences in the repository submission history, extract the architecture adaptation modification fragments, generate conflict labels to mark the conflict types; and based on D candidate In the conflict type distribution, oversampling technology is used to enhance the difference Diff between the optimization library candidate set with a frequency less than a threshold and a historical repair failure rate greater than a threshold, so that the method can strengthen the training of rare migration problems in a targeted manner;
[0029] Establish an incremental analysis pipeline: Use Github Webhook to capture Push / Issue / Pull Request patch submission events of related repositories in real time, analyze new code differences and Issue discussions in real time, monitor update events of the target repository, and migrate the value of S in real time. migration new With the previous migration value S migration old The score change satisfies |Smigration new -S migration old When the value is greater than or equal to the threshold, the optimization library candidate set D is dynamically updated. candidate .
[0030] Step 2 includes: in the optimization library construction phase, the optimization library candidate set D collected in step 1 is candidate Transform migration cases that have passed functional and performance verification into reusable architecture adaptation templates, establish a mapping relationship from software package source code A to optimized software code B, and optimize the migration process of the software package from the original architecture C to the new architecture D;
[0031] First, use the tree-sitter tool to parse the software package source code A and the optimized software code B, generate a standardized abstract syntax tree AST, and use the breadth-first traversal algorithm to convert the AST A and AST B Converted into standardized node sequence Seq A 、Seq B , ensuring that the nodes are arranged in hierarchical order:
[0032] ,
[0033] ,
[0034] AST A is the abstract syntax tree of source code A, AST B This is the abstract syntax tree of the optimized code B. BFS is the breadth-first search algorithm.
[0035] Then, the pre-trained CodeBERT model is used to encode the serialized result to generate a 768-dimensional semantic vector. The semantic vector V generated by source code A is A and the semantic vector V generated by the optimized code B B Defined as:
[0036] ,
[0037] ,
[0038] And add metadata tags for each code. The metadata is defined as:
[0039] ,
[0040] 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;
[0041] Next, we construct the vector database FAISS index and calculate the similarity, and then we can get the semantic vectors {V A (i) ,V B (i)} N i=1 Store in FAISS index, where N is the total number of optimization cases, i is the sequence number of the optimization case, , V A (i) CodeBERT encoding result of the original code A for the i-th optimization case, V B (i) This is the CodeBERT encoding result of the optimized code B;
[0042] The storage and retrieval efficiency is optimized by using a structure based on inverted files and product quantization. The parameter configuration is: the number of clusters is 1024, the quantization bit width is 8, and the similarity metric is cosine similarity. When there are new contents in the optimization library (A, B new ), dynamically update the FAISS index:
[0043] ,
[0044] in Indicates that whenever a new optimization use case appears, Update Index, where Indicates the encoding result of the newly added content.
[0045] Step 2 also includes: using a streaming clustering strategy, the optimization library triggers an index rebuild every time it collects Y1 new optimization content; if optimization plan B new B corresponding to the same A in the history old There is a performance difference, that is, satisfying |T new -T old | If it is greater than the threshold, the re-evaluation process is started and the compilation time T of the newly added optimized content is re-measured new Compile time T with historical optimization in the optimization library old , and retain the version with better performance; among them, B old To optimize the original optimization solution for the software package source code A in the library, B new This is a new optimization solution for software package source code A.
[0046] Step 5 includes: for each candidate optimized code C i , calculate the functional correctness score R func ∈[0,1]:
[0047] ,
[0048] in is the total number of test cases, To optimize the code, For input data, To indicate the function, if the optimized software package is migrated to the new architecture without changing its function, then is 1, otherwise is 0;
[0049] The compile time optimization rate S is calculated using the following formula: compile :
[0050] ,
[0051] Where T orig is the original code compilation time, T Ci Optimize code compilation time for candidates; S compile ∈R, R represents the set of real numbers;
[0052] The runtime performance gain S is calculated using the following formula: runtime :
[0053] ,
[0054] Among them, T orig_run is the running time of the original code, T Ci_run Optimize the code running time for the candidate, S runtime ∈R.
[0055] Step 5 also includes: measuring the execution time of the original code and the candidate optimized code on the target architecture, setting the following filter conditions: R func >0.95, passing at least 95% of the test cases, T Ci ≤1.5·T orig , the candidate optimization codes that do not meet any filtering conditions are directly eliminated, and the candidate codes that meet the conditions are saved and recorded as Q.
[0056] Step 6 includes: for the candidate code Q, calculating the similarity score between the candidate code Q and the cases in the optimization library:
[0057] ,
[0058] in, is the metadata matching weight, V Q is the semantic vector of the candidate code Q, A i To optimize the i-th use case in the library, For use case A i The semantic vector of MetadataMatch function is defined as:
[0059] ,
[0060] Among them, k is the total number of ISA expansion dimensions, is the support mark of candidate code Q in the kth ISA extension dimension, To optimize library use case A i Support flags in the k-th ISA extension dimension;
[0061] For candidate code Q, retrieve the Top-K most similar cases {A1,A2,...,A k},satisfy:
[0062] ,
[0063] in is the set threshold, ;
[0064] Through similarity search, the optimization library is retrieved to find source code and optimized code mapping that are similar to the code to be optimized. Before performing the difference operation, it is determined whether the excluded character class is empty or has no intersection with the main character class. If the conditions are met, the difference calculation process is directly skipped and the result of the main character class is reused for code generation.
[0065] In the mutation phase, non-local search capability is introduced through differential optimization examples to simulate the random mutation process in biological evolution. i The difference index is ,Comprehensive code semantic differences and optimization strategy differences are defined as:
[0066] ,
[0067] in and is the weight, SemDiff is Q and A i The semantic difference is calculated based on the CodeBERT vector distance:
[0068] ,
[0069] StrategyDiff is Q and A i The strategy difference is calculated based on the optimized action label Jaccard distance:
[0070] ,
[0071] Where T Q is the optimization strategy label set of the candidate code Q; To optimize use case A i The optimization strategy label set;
[0072] Then filter cases from the optimization library that meet the following conditions: candidate code Q and optimization library use case A i The metadata compatibility matching degree is high, that is, it satisfies And the functional correctness score R func (A i ) = 1, and use sorting search, according to the difference D (Q, A i ) Take the top-1 in descending order as the difference optimization template, which is the mutation stage of the genetic operator. The difference optimization template is used to simulate the random mutation process in biological evolution and break through the limitations of the local optimal solution.
[0073] The present invention also provides an electronic device, comprising a processor and a memory, wherein the memory stores program code, and when the program code is executed by the processor, the processor executes the steps of the method.
[0074] The present invention also provides a storage medium storing a computer program or instruction, which executes the steps of the method when the computer program or instruction is run on a computer.
[0075] The method of the present invention is mainly used in the fields of open source hardware ecosystem support, high-performance computing, Internet of Things and edge computing, embedded real-time system development, etc. This technology analyzes the semantic characteristics and instruction set differences of the source code, combines large language models with automated optimization algorithms, and implements a method for porting 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. Its core value lies in resolving compatibility conflicts, performance losses, and development efficiency issues in cross-platform migration, providing technical support for the coordinated development of software and hardware in the open source ecosystem.
[0076] This method can be applied in the following areas:
[0077] Open source hardware ecosystem: In the RISC-V open source instruction set ecosystem, cross-architecture optimization technology can accelerate software ecosystem adaptation. For example, when migrating a Linux distribution from x86 to the RISC-V architecture, automated code conversion tools can be used to resolve compatibility issues with instruction set expansion, thereby improving the compatibility and performance of software packages in a fragmented hardware ecosystem.
[0078] High-performance computing: For heterogeneous computing clusters in supercomputing centers (such as x86 CPU + ARM accelerator cards), multi-target compilation and optimization technology is used to generate parallel code adapted to different instruction sets, solving performance bottlenecks in cross-architecture task scheduling.
[0079] IoT and edge computing: IoT devices are often based on low-power RISC-V chips, but migrating existing x86 / ARM software can lead to reduced energy efficiency due to redundant instructions. This technology enables migrated software packages to maintain both functional integrity and low power consumption in resource-constrained devices.
[0080] Embedded real-time system development: The deterministic execution characteristics of the RISC-V architecture are crucial in real-time embedded scenarios such as automotive electronics and industrial control. Optimization technologies ensure that migrated code meets hard real-time constraints. For example, static analysis eliminates unpredictable branches, optimizes the instruction pipeline efficiency of interrupt handlers, and adapts to RISC-V's physical memory protection mechanisms to enhance system security and reliability.
[0081] Beneficial effects: The present invention improves the automation level and performance of cross-instruction set software migration by integrating the semantic understanding of a large language model with the dynamic optimization of a genetic algorithm. Compared with traditional manual migration methods, this solution adopts a two-stage task decomposition strategy to effectively reduce the risk of semantic drift and improve the accuracy of instruction-level conflict repair. Based on the semantic feature retrieval mechanism of tree-sitter, combined with the similarity and difference instances in the optimization knowledge base, global exploration and local development collaboration of code optimization are achieved, breaking through the local optimal limitation. The optimized code shows high performance 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 guarantees and performance tuning support for heterogeneous computing scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0082] Figure 1 Flow chart of the method of the present invention.
[0083] Figure 2 This is the flow chart of the genetic operator. DETAILED DESCRIPTION
[0084] like Figure 1 As shown, the present invention provides a method for optimizing software package source code across instruction set architectures, comprising the following steps:
[0085] Step 1: Retrieve and collect various architecture migration optimization use cases from Github / Gitee open source websites. Build a distributed data collection module based on the Scrapy framework, and use keyword combinations such as "x86 to RISC-V," "RISC-V PORT," and "ISAmigration" to retrieve relevant repositories and code snippets. Simultaneously collect code files, commit history, issue discussions, and build logs. Use regular expressions to match architecture-related features in the code (such as the feature -march=rv64gc in the RISC-V architecture migration task), filter out the results where the number of feature hit lines divided by the total number of code lines is less than a threshold of 0.3, and generate the original use case set D. raw After obtaining the use case set, perform a quantitative assessment of the migration value of all use cases. Define the migration value scoring model S migration , quantitatively evaluate the value of use cases from three dimensions: code quality, migration completeness, and community verification:
[0086] ,
[0087] Among them S code The code quality score is based on Halstrad complexity, cyclomatic complexity and the output of static analysis tools to calculate the code maintainability index:
[0088] ,
[0089] Where X1 represents cyclomatic complexity and X2 represents Halstead complexity;
[0090] Migration integrity score S completeness The calculation formula is:
[0091] ,
[0092] Where X3 represents the number of modified lines across architectures, X4 represents the total number of code lines, and X5 represents the number of successful builds;
[0093] The formula for calculating the community verification score is:
[0094] ,
[0095] Among them, X6 represents the number of positive comments, X7 represents the number of negative comments, X8 represents the total number of comments, and X9 represents the number of recent stars (Star is the total number of stars that users on the open source website have clicked on as valuable, reflecting the long-term influence and community recognition of the project). 10 Indicates the total number of Stars;
[0096] And set the weights W1=0.5, W2=0.3, W3=0.2. This method selects S migration≥The use case of 0.7 enters the optimization library candidate set D candidate Then optimize the candidate set D candidate Extract instruction set conflict patterns in the use case: Use the pre-trained model CodeT5 to analyze the differences in the repository submission history, extract the architecture adaptation modification fragments, generate conflict labels to mark the conflict types, such as input Diff fragment -__m256i a;+vint32m8_t b, output label SIMD_INTRINSIC_MISMATCH; and based on D candidate In the conflict type distribution, oversampling technology is used to enhance the difference Diff between the optimization library candidate set with a frequency less than a threshold of 5% and a historical repair failure rate greater than a threshold of 30%, so that the method can strengthen the training of rare migration problems in a targeted manner;
[0097] Establish an incremental analysis pipeline: Use Github Webhook to capture Push / Issue / Pull Request patch submission events of related repositories in real time, analyze new code differences and Issue discussions in real time, monitor update events of the target repository, and migrate the value of S in real time. migration new With the previous migration value S migration old The score change satisfies |S migration new -S migration old When the threshold is greater than or equal to 0.05, the optimization library candidate set D is dynamically updated. candidate .
[0098] Step 2: Accumulate verified optimization examples through the optimization library to reduce repetitive manual labor, provide excellent templates for subsequent optimization processes, and improve optimization accuracy. During the optimization library construction phase, this method converts the optimization use cases collected in step 1 into reusable architecture adaptation templates. Its core lies in 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). 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). The pre-trained CodeBERT model is used to encode the AST to generate a 768-dimensional semantic vector:
[0099] ,
[0100] ,
[0101] The AST sequence is generated through a breadth-first traversal, preserving key semantic information such as variable scope and control flow. Furthermore, the present invention adds metadata tags to each code, where c represents the number of cores, b represents the memory bandwidth (GB / s), and the instruction set architecture (ISA) (which defines the set of instructions a computer can execute, with each instruction specifying the operation the computer performs, the address space where the operands are stored, and the operand types) is extended as a binary vector (e.g., RISC V extension = 1, otherwise = 0):
[0102] ,
[0103] Next, we construct the FAISS index and calculate the similarity, and combine the semantic vectors {V A (i) ,V B (i)} N i=1 The data is stored in the FAISS index and the storage and retrieval efficiency is optimized by using the structure based on inverted file and product quantization (IVFPQ). The parameter configuration is as follows: the number of clusters is 1024, the quantization bit width is 8, and the similarity metric is cosine similarity. new ), dynamically update the FAISS index:
[0104] ,
[0105] A streaming clustering strategy is used, triggering an index rebuild every time 100 new cases are accumulated, balancing efficiency and consistency. new B corresponding to the same A in the history old There is a performance difference (|T new -T old |>10%), of which B old To optimize the original optimization solution for source code A in the library, B new For the newly added optimization solution for A, the re-evaluation process is started, Tnew and Told are remeasured, and the version with better performance is retained.
[0106] Step 3: To precisely optimize suboptimal software package source code, this method collects software package source code encountered in the project that has poor migration performance. Poor migration performance refers to code that cannot be compiled on the new architecture or has significantly reduced efficiency on the new architecture. A significant reduction in efficiency on the new architecture refers to code with an execution time increase greater than a threshold of 15%. Specific information such as the original and target architectures, and the programming language used by the software package to be optimized, is collected and provided to the Large Language Model (LLM) to generate optimization suggestions for migrating between the two architectures. The prompt content is as follows:
[0107] [Objective] Provide RISC-V migration optimization solutions for the following x86 architecture software packages source code, requirements:
[0108] 1. Compilation time reduced by >20% (based on the same build environment);
[0109] 2. Functional consistency reaches 100%;
[0110] 3. Make full use of RISC-V features;
[0111] 4. No optimization code is generated, only multiple optimization suggestions are output;
[0112] [input context];
[0113] Source code: {Code};
[0114] Code language: {C++};
[0115] [output];
[0116] {Optimization suggestions};
[0117] The original x86 architecture function isl_set_subtract, part of the isl software package, computes the set difference of two sets. On the x86 platform, this function was called twice, with a cumulative runtime of approximately 248 microseconds and an average runtime of approximately 124 microseconds per call. In contrast, under the same input conditions and logic, this function was called 1634 times after migration to the RISC-V architecture, with a cumulative runtime of approximately 988.2 milliseconds and an average runtime of approximately 605 microseconds per call. Based on this data, the RISC-V platform exhibited a performance degradation of approximately 3983 times in the total execution time of this function compared to the x86 platform, resulting in an increase in runtime of approximately 398,349.6%, indicating poor migration results. This function was then fed into the large language model, replacing the prompt code with this function, and outputting modification suggestions for the code to be optimized:
[0118] 1. Precondition check: Add set intersection check before calling type conversion to avoid meaningless operations.
[0119] 2. Optimize the underlying set difference algorithm: Develop the native set difference operation isl_set_subtract_direct to bypass the set_to_map conversion.
[0120] 3. Reduce type conversion: Restructure code logic and prioritize using collection native APIs instead of mapping conversions.
[0121] 4. Set relationship pre-computation: cache the set intersection and union states to reduce repeated calculations (such as reusing the isl_set_is_disjoint result).
[0122] 5. Memory reuse mechanism: Enable object pool caching for frequently called set_to_map results.
[0123] When using a large language model to generate software package optimization code, the present invention prioritizes generating optimization suggestions before generating optimized code, rather than directly generating optimized code in a single phase. This method is more effective. The advantage of this approach is that it reduces the complexity of the generation process through task decomposition, improves the accuracy of code generated by the large language model, and avoids semantic drift caused by multitasking.
[0124] Step 4: Input the source code of the software package to be optimized and the optimization suggestions obtained in step 3 into LLM, and use chain thinking 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:
[0125] You are a system-level software engineer proficient in RISC-V architecture migration and performance optimization. Your task is to migrate the source code of the following x86-based software package to the RISC-V architecture and optimize its performance while maintaining full functional consistency. Please consider the following issues in stages and produce multiple high-quality optimized code versions.
[0126] [Task1: Understand functional semantics];
[0127] 1. Read the given C / C++ / Rust / Asm language source code and identify its core functions.
[0128] 2. Ensure that the migrated RISC-V version is 100% functionally consistent with the original version.
[0129] [Task2: Analyze optimization suggestions];
[0130] Obtain the optimization path based on the optimization suggestions and analyze the necessary modifications to the source code.
[0131] [Task3: Optimize code output];
[0132] Output multiple optimized versions of the code, each of which should reflect a different optimization strategy.
[0133] [Input source code];
[0134] {Code};
[0135] [Input language] C / C++ / Rust / Asm;
[0136] [Target];
[0137] Compile time reduced by >20%;
[0138] Guarantee 100% functional consistency;
[0139] Leverage RISC-V architecture feature optimization;
[0140] Output multiple optimized code versions;
[0141] This step completes the replacement of the software package's source code, including the instruction set, without requiring manual analysis or modification, significantly reducing the researcher's workload. Three solutions were generated based on the suggested modifications: Solution 1 introduces pre-conditioning logic to reduce unnecessary set difference computation overhead. Specifically, before performing set difference computations, the input sets are first checked for intersection. If the two sets do not intersect, the original sets are directly returned, avoiding the subsequent complex set difference computation process and thus improving code efficiency. Furthermore, the isl_set_subtract_direct interface was introduced to bypass the intermediate set-to-map type conversion operation, reducing unnecessary data structure mapping and memory allocation. Solution 2 establishes a global memory pool mechanism to reduce dynamic memory allocation overhead during set type conversion. The memory pool is initialized upon the first call, and map type objects are subsequently reused using pre-allocated resources from the pool, avoiding frequent heap memory allocation and deallocation. Furthermore, set difference computations are still performed by converting the sets to maps before performing the difference computation. Solution 3 maintains the intersection state of set pairs in a global cache, avoiding repeated intersection checks for sets with known relationships. When determining whether a set pair intersects for the first time, the result is cached in a least recently used (LRU) structure. If the same set pair is encountered again, the cache is directly hit, allowing a quick decision on whether to perform a set difference operation. This approach is suitable for scenarios with stable relationships between sets and high reuse rates, effectively reducing redundant determination and calculations and improving overall system throughput.
[0142] Step 5, after generating multiple preliminary optimized adaptation codes through the large language model, the present invention further evaluates and screens the candidate optimization codes, corresponding to the fitness of each individual in the calculation population of the genetic operator, the selection stage, such as Figure 2 As shown, adaptation solutions with poor LLM optimization are eliminated. Specifically, the evaluation metrics for this step include functional correctness verification and compilation time. The generated code is sorted according to the build performance metrics, and the software package code with consistent functionality on the same source platform and high speed improvement after migration is prioritized as candidate optimization code.
[0143] For each candidate optimization code C i, calculate the functional correctness score R func ∈[0,1]:
[0144] ,
[0145] in is the total number of test cases, To optimize the code, For input data, To indicate the function, if the optimized software package is migrated to the new architecture without changing its function, then is 1, otherwise is 0;
[0146] Compile time optimization rate S compile ∈R:
[0147] ,
[0148] Where T orig is the original code compilation time, T Ci Optimize code compilation time for candidates;
[0149] Runtime performance gain S runtime ∈R:
[0150] ,
[0151] Measure the execution time of the original code and the candidate optimized code on the target architecture. The hard filter condition is R func >0.95, passing at least 95% of the test cases, That is, the compilation time does not exceed 1.5 times of the original code, and candidate optimization codes that do not meet any conditions are directly eliminated.
[0152] Step 6: Search the optimized code library based on the semantic features of the current code. The optimized library retrieved by this method contains use cases that are highly matched with the semantics of the source code of the software package to be optimized, including similar optimization instances and difference optimization instances, corresponding to Figure 2 The crossover and mutation phases of the genetic operator process. Similar optimization cases are excellent examples that match the current optimization code solution, while different optimization cases are cases with significantly different optimization solutions from the candidate optimization code. For the candidate code Q, the similarity score between the candidate code Q and the cases in the optimization library (the reusable architecture adaptation template in the optimization library after step 2) is calculated:
[0153] ,
[0154] in, For metadata matching weight, the MetadataMatch function is defined as:
[0155] ,
[0156] Where k is the total number of ISA expansion dimensions.
[0157] For the target code Q, retrieve the Top-K most similar cases {A1,A2,...,A K},satisfy:
[0158] ,
[0159] In this embodiment The value is 0.85.
[0160] Through similarity search, we retrieved source code from the knowledge base that is similar to the code to be optimized, and mapped it to the optimized code after optimization: The character class difference processing logic in the PCRE2 library is structurally similar to the isl_set_subtract function. As a widely used regular expression engine, PCRE2's original character class difference processing logic includes steps such as parsing the main character class and the exclusion class, converting them into a bitmap structure, and then performing a difference 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 this use case's improved strategy for difference operations is to determine whether the exclusion class is empty or has no intersection with the main character class before performing the difference operation. If the conditions are met, the difference calculation process is skipped directly, and the result of the main character class is reused for encoding generation.
[0161] The mutation phase introduces non-local search capabilities through differential optimization examples to simulate the random mutation process in biological evolution. Specifically, the difference index is ,Comprehensive code semantic differences and optimization strategy differences are defined as:
[0162] ,
[0163] in , , SemDiff is the semantic difference, calculated based on the CodeBERT vector distance:
[0164] ,
[0165] StrategyDiff calculates the Jaccard distance based on the optimized action labels:
[0166] ,
[0167] Where T Q and A collection of optimization strategy tags.
[0168] Then filter the optimization library to meet the requirements And R func (A i )=1 condition, and use sorting search, according to the difference D(Q,A i ) Take the top-1 in descending order as the difference optimization template.
[0169] For example, a differentiated optimization solution was retrieved from the optimization library. This solution introduces a global cache mechanism, uniquely identifying character classes through a hash function and caching historical conversion results in an array. When a cache hit occurs, the calculated bitmap is directly returned, avoiding repeated class_to_bitmap conversions. This optimization strategy combines cache reuse with lazy evaluation strategies, enhancing the model's diversity in the structural search space and promoting the discovery of the global optimal solution.
[0170] Step 7: Use the large language model for further optimization. Step 6 retrieves the excellent examples from the optimization library. The large language model then provides the candidate optimization code, excellent optimization cases, and instructions. Using these excellent cases as examples and combining them with chain-thinking prompts, the large language model further optimizes the candidate optimization code based on the initial optimization, generating the final optimized code. This time, only one optimization solution is generated, which is the best optimized code achieved in this iteration.
[0171] In step 8, evaluate the build speed improvement and functional consistency. When the improvement is greater than or equal to the threshold (generally referring to a speedup ≥ 10% in a single round of optimization), 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.
[0172] The present invention terminates iterations when the speed improvement rate for three consecutive rounds is less than 10%, indicating that the optimal code has been obtained. This code is then fed back to the optimization knowledge base, increasing the diversity of optimization solutions in the knowledge base. After evaluating the optimized code, a total time consumption of approximately 681.1 milliseconds was obtained. The calculated build time improvement rate was 31.07%. If the improvement rate exceeds 10%, the iteration process continues. Finally, after two iterations, the iteration ends, and the final optimized code provided by this method is obtained.
[0173] Compared with the existing method of calling a large number of team members to carry out software package migration and optimization work, a lot of manpower and material resources are saved, because manual migration is not efficient 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 dependency features in the source code and accurately locate the migration points based on AST-driven pattern matching technology. There is no need for engineers to check line by line, which greatly reduces 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 and generate verified efficient code, avoiding 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 the source code and migration target, 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.
[0174] The present invention provides a method for optimizing software package source code across instruction set architectures. There are numerous methods and approaches for implementing this technical solution. The foregoing description is merely a preferred embodiment of the present invention. It should be noted that those skilled in the art may make improvements and modifications without departing from the principles of the present invention, and such improvements and modifications are also within the scope of protection of the present invention. Any components not specified in this embodiment may be implemented using existing technologies.
Claims
1. A method for optimizing software package source code across instruction set architectures, characterized in that: The following steps are involved: Step 1: Search and collect various architecture migration optimization use cases from open source websites; Step 2: Accumulate verified optimization use cases through the optimization library; Step 3: Collect the source code of software packages that have poor migration performance in the project. Poor migration performance refers to the inability to compile on the new architecture or a significant decrease in running efficiency on the new architecture. A significant decrease in running efficiency on the new architecture refers to code whose execution time increases by more than a threshold. Collect the programming languages used by the original and target architectures, as well as the software packages to be optimized, and provide them to the Large Language Model (LLM) to generate optimization suggestions for migrating between the original and target architectures. 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). Use chain thinking to prompt the large language model (LLM) to generate two or more candidate optimization codes that are initially adapted to the target architecture. Step 5: Further evaluate and screen the candidate optimized codes generated in step 4, using genetic operators to eliminate poorly optimized adaptation solutions for the large language model (LLM). Step 6: Search the optimization library based on the semantic features of the current code to obtain excellent examples; Step 7: Use the Large Language Model (LLM) for further optimization: Provide the candidate optimization code, excellent examples, and instructions to the Large Language Model (LLM). The Large Language Model (LLM) will use the excellent examples as examples and combine chain thinking prompts to further optimize the candidate optimization code based on the initial optimization using genetic operators to generate the final optimized code. This time, only one optimization solution is generated, which is the best optimized code obtained in this iteration. In 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 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 meets the requirements.
2. The method according to claim 1, characterized in that Step 1 includes: building a distributed data collection module based on the Scrapy framework, searching related repositories and code snippets with keywords, synchronously collecting code files, submission history, issue discussions, and build logs, matching architecture-related features in the code with regular expressions, filtering out the results where the number of feature hit lines divided by the total number of code lines is less than a threshold, and generating the original use case set D. raw .
3. The method according to claim 2, characterized in that Step 1 also includes: the original use case set D raw Quantitatively evaluate the migration value of all use cases in the migration value evaluation: define the migration value score S migration , quantitatively evaluate the value of use cases from three dimensions: code quality, migration completeness, and community verification: , Where W1, W2, W3 are weight coefficients, S community Score community verification, S completeness Score for migration completeness, S code Score the code quality using the formula: , Where X1 represents cyclomatic complexity and X2 represents Halstead complexity; Migration integrity score S completeness The calculation formula is: , Where X3 represents the number of modified lines across architectures, X4 represents the total number of code lines, and X5 represents the number of successful builds; The formula for calculating the community verification score 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 number of recent stars, and X 10 Indicates 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 , then optimize the candidate set D candidate Extract instruction set conflict patterns in the use case: Use the pre-trained model CodeT5 to analyze the differences in the repository submission history, extract the architecture adaptation modification fragments, generate conflict labels to mark the conflict types; and based on D candidate In the conflict type distribution, oversampling technology is used to enhance the difference Diff between the frequency of occurrence in the optimization library candidate set that is less than the threshold and the historical repair failure rate that is greater than the threshold; Establish an incremental analysis pipeline: Use Github Webhook to capture Push / Issue / Pull Request patch submission events of related repositories in real time, analyze new code differences and Issue discussions in real time, monitor update events of the target repository, and migrate the value of S in real time. migration new With the previous migration value S migration old The score change satisfies |S migration new -S migration old When the value is greater than or equal to the threshold, the optimization library candidate set D is dynamically updated. candidate .
4. The method according to claim 3, characterized in that Step 2 includes: in the optimization library construction phase, the optimization library candidate set D collected in step 1 is candidate Transform migration cases that have passed functional and performance verification into reusable architecture adaptation templates, establish a mapping relationship from software package source code A to 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 software package source code A and the optimized software code B, generate a standardized abstract syntax tree AST, and use the breadth-first traversal algorithm to convert the AST A and AST B Converted into standardized node sequence Seq A 、Seq B , ensuring that the nodes are arranged in hierarchical order: , , AST A is the abstract syntax tree of source code A, AST B This is the abstract syntax tree of the optimized code B. BFS is the breadth-first search algorithm. Then, the pre-trained CodeBERT model is used to encode the serialized result to generate a 768-dimensional semantic vector. The semantic vector V generated by source code A is A and the semantic vector V generated by the optimized code B B Defined as: , , And add metadata tags for each code. The 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, we construct the vector database FAISS index and calculate the similarity, and then we can get the semantic vectors {V A (i) ,V B (i) } N i=1 Store in FAISS index, where N is the total number of optimization cases, i is the sequence number of the optimization case, , V A (i) CodeBERT encoding result of the original code A for the i-th optimization case, V B (i) This is the CodeBERT encoding result of the optimized code B; The storage and retrieval efficiency is optimized by using a structure based on inverted files and product quantization. The parameter configuration is: the number of clusters is 1024, the quantization bit width is 8, and the similarity metric is cosine similarity. When there are new contents in the optimization library (A, B new ), dynamically update the FAISS index: , in Indicates that whenever a new optimization use case appears, Update Index, where Indicates the encoding result of the newly added content.
5. The method according to claim 4, characterized in that Step 2 also includes: using a streaming clustering strategy, the optimization library triggers an index rebuild every time it collects Y1 new optimization content; if optimization plan B new B corresponding to the same A in the history old There is a performance difference, that is, satisfying |T new -T old | If it is greater than the threshold, the re-evaluation process is started and the compilation time T of the newly added optimized content is re-measured new Compile time T with historical optimization in the optimization library old , and retain the version with better performance; among them, B old To optimize the original optimization solution for the software package source code A in the library, B new This is a new optimization solution for software package source code A.
6. The method according to claim 5, characterized in that Step 5 includes: for each candidate optimized code C i , calculate the functional correctness score R func ∈[0,1]: , in is the total number of test cases, To optimize the code, For input data, To indicate the function, if the optimized software package is migrated to the new architecture without changing its function, then is 1, otherwise is 0; The compile time optimization rate S is calculated using the following formula: compile : , Where T orig is the original code compilation time, T Ci Optimize code compilation time for candidates; S compile ∈R, R represents the set of real numbers; The runtime performance gain S is calculated using the following formula: runtime : , Among them, T orig_run is the running time of the original code, T Ci_run Optimize the code running time for the candidate, S runtime ∈R.
7. The method according to claim 6, characterized in that Step 5 also includes: measuring the execution time of the original code and the candidate optimized code on the target architecture, setting the following filter conditions: R func >0.95, , the candidate optimization codes that do not meet any filtering conditions are directly eliminated, and the candidate codes that meet the conditions are saved and recorded as Q.
8. The method according to claim 7, characterized in that Step 6 includes: for the candidate code Q, calculating the similarity score between the candidate code Q and the cases in the optimization library: , in, is the metadata matching weight, V Q is the semantic vector of the candidate code Q, A i To optimize the i-th use case in the library, For use case A i The semantic vector of MetadataMatch function is defined as: , Among them, k is the total number of ISA expansion dimensions, is the support mark of candidate code Q in the kth ISA extension dimension, To optimize library use case A i Support flags in the k-th ISA extension dimension; For candidate code Q, retrieve the Top-K most similar cases {A1,A2,...,A k },satisfy: , in is the set threshold; Through similarity search, the optimization library is retrieved to find source code and optimized code mapping that are similar to the code to be optimized. Before performing the difference operation, it is determined whether the excluded character class is empty or has no intersection with the main character class. If the conditions are met, the difference calculation process is directly skipped and the result of the main character class is reused for code generation. In the mutation phase, non-local search capability is introduced through differential optimization examples to simulate the random mutation process in biological evolution. i The difference index is ,Comprehensive code semantic differences and optimization strategy differences are defined as: , in and is the weight, SemDiff is Q and A i The semantic difference is calculated based on the CodeBERT vector distance: , StrategyDiff is Q and A i The strategy difference is calculated based on the optimized action label Jaccard distance: , Where T Q is the optimization strategy label set of the candidate code Q; To optimize use case A i The optimization strategy label set; Then filter cases from the optimization library that meet the following conditions: candidate code Q and optimization library use case A i The metadata compatibility matching degree is high, that is, it satisfies And the functional correctness score R func (A i ) = 1, and use sorting search, according to the difference D (Q, A i ) Take the top-1 in descending order as the difference optimization template, which is the mutation stage of the genetic operator. The difference optimization template is used to simulate the random mutation process in biological evolution and break through the limitations of the local optimal solution.
9. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores program codes, and when the program codes are executed by the processor, the processor is caused to perform the steps of the method according to any one of claims 1 to 8.
10. A storage medium, characterized in that: A computer program or instruction is stored, and when the computer program or instruction is run on a computer, the steps of the method according to any one of claims 1 to 8 are executed.
Citation Information
Patent Citations
Hybrid front-end framework migration method based on AST and LLM
CN117608656A
Automatic code security optimization method based on large language model
CN120046149A