Simulink code optimization method and device based on large language model

By using a large language model to identify and verify semantically equivalent code segments in Simulink code, optimized code is generated, solving the efficiency and quality problems of code optimization in embedded scenarios and reducing memory usage.

CN122018887APending Publication Date: 2026-05-12TSINGHUA UNIVERSITY +1
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
TSINGHUA UNIVERSITY
Filing Date
2025-12-24
Publication Date
2026-05-12

Smart Images

  • Figure CN122018887A_ABST
    Figure CN122018887A_ABST
Patent Text Reader

Abstract

The invention provides a Simulink code optimization method and device based on a large language model, and relates to the technical field of code optimizing.The method comprises the steps that code generation is executed on a target Simulink model, semantic information supplementation is conducted on generated codes, and to-be-optimized codes are obtained; the semantic information is used for identifying equivalent code segments; performing semantic equivalence code segment identification on the to-be-optimized code by utilizing the large language model to obtain a plurality of candidate code segments, and generating a shared function corresponding to each candidate code segment in the plurality of candidate code segments; and verifying semantic equivalence between each shared function and the corresponding candidate code segment, and performing code rewriting and compilation optimization by using the verified shared functions to generate a final optimized code. According to the Simulink code optimization method and device based on the large language model, the large language model is used for carrying out equivalent identification on the code segments, and the identified code segments are reduced, so that memory occupation is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of code optimization technology, and in particular to a Simulink code optimization method and apparatus based on a large language model. Background Technology

[0002] Simulink is a key tool for Model-Driven Development (MDD) and has become a cornerstone in safety-critical systems engineering, such as automotive, aerospace, and medical systems. It improves development productivity, reliability, and maintainability by abstracting complex system behavior into high-level models. Automated code generation is a core component of Simulink, significantly reducing human workload and minimizing potential coding errors.

[0003] However, due to strict resource constraints, especially in embedded scenarios, there are high requirements for the quality and size of the generated code. As a result, the code generation solutions in related technologies are difficult to achieve efficient and accurate code optimization. Summary of the Invention

[0004] The purpose of this application is to provide a Simulink code optimization method and apparatus based on a large language model, which uses a large language model to identify code segments as equivalent and reduces the identified code segments to reduce memory usage.

[0005] This application provides a Simulink code optimization method based on a large language model, including: Code generation is performed on the target Simulink model, and semantic information is added to the generated code to obtain the code to be optimized. The semantic information is used for the identification of equivalent code segments. The code to be optimized is used to identify semantically equivalent code segments using a large language model to obtain multiple candidate code segments, and a shared function corresponding to each candidate code segment is generated. The semantic equivalence between each shared function and the corresponding candidate code segment is verified, and the verified shared functions are used for code rewriting and compilation optimization to generate the final optimized code.

[0006] Optionally, the step of supplementing the generated code with semantic information to obtain the code to be optimized includes: replacing hard-coded constants in the generated code with variable forms based on a pre-built parameterized mapping table to obtain the replaced code; determining the input and output ports of each subsystem based on the model structure and connection relationships of the target Simulink model, and adding comments to the replaced code based on the input and output ports of each subsystem to obtain the code to be optimized; wherein, the comments in the code to be optimized are used to identify different code regions.

[0007] Optionally, the large language model includes: an equivalence extraction agent and a constraint verification agent; the step of using the large language model to identify semantically equivalent code segments of the code to be optimized, obtaining multiple candidate code segments, and generating a shared function corresponding to each candidate code segment includes: constructing a first prompt word for the equivalence extraction agent and a second prompt word for the constraint verification agent based on first prompt information; based on the first prompt word, using the equivalence extraction agent to identify candidate code segments of the code to be optimized, obtaining multiple code segments, and generating a shared function corresponding to each code segment; based on the second prompt word, using the constraint verification agent to verify the multiple code segments and the shared function corresponding to each code segment, and repairing the shared function if an anomaly is detected, ultimately obtaining the multiple candidate codes and the shared function corresponding to each candidate code; wherein, the first prompt information includes at least one of the following: equivalence cases, subsystem semantics, formatting specifications, and counterexamples.

[0008] Optionally, the step of using the constraint verification agent to verify the plurality of code segments and the shared functions corresponding to each code segment, and repairing the shared functions in the event of an anomaly, includes: compiling the target shared function using a compiler; if the compilation is successful, the target shared function is determined to have been successfully verified; otherwise, a third prompt word is constructed based on the second prompt information, and the target shared function is repaired using the constraint verification agent based on the third prompt word; wherein, the target shared function is any one of the plurality of shared functions corresponding to the plurality of code segments; the second prompt information includes at least one of the following: compilation error information, the target shared function, and a counterexample.

[0009] Optionally, verifying the semantic equivalence between each shared function and its corresponding candidate code segment includes: generating a test function corresponding to each candidate code segment based on a data flow graph; the data flow graph is used to characterize the source and target ends connected by the target Simulink model; and using a satisfiability modulus theory (SMT) solver to perform semantic equivalence verification on the test function and shared function corresponding to each candidate code segment, thereby generating a verification result for each candidate code segment.

[0010] Optionally, the step of rewriting and optimizing the code using verified shared functions to generate the final optimized code includes: when the verification result of the target candidate code segment indicates that the corresponding test function and shared function have passed verification, rewriting the target candidate code segment using the shared function corresponding to the target candidate code segment to obtain the rewritten code segment; compiling the target candidate code segment and the rewritten code segment using a compiler respectively; if the compilation result indicates that the shared function corresponding to the target candidate code segment can reduce the amount of code, then generating the final optimized code based on the shared function corresponding to the target candidate code segment; wherein, the target candidate code segment is any one of the plurality of candidate code segments.

[0011] Optionally, after generating the verification result for each candidate code segment, the method further includes: if the verification result of the target candidate code segment indicates that the corresponding test function and shared function have failed verification, sending failure feedback information to the equivalent extraction agent to regenerate the shared function until the shared function generated by the equivalent extraction agent passes verification; wherein, the failure feedback information includes at least one of the following: the target candidate code segment, the shared function corresponding to the target candidate code segment, and the counterexample information output by the SMT solver.

[0012] This application also provides a Simulink code optimization device based on a large language model, including: The code generation module is used to generate code for the target Simulink model and supplement the generated code with semantic information to obtain the code to be optimized; the semantic information is used for the identification of equivalent code segments; the equivalence identification module is used to identify semantically equivalent code segments in the code to be optimized using a large language model to obtain multiple candidate code segments and generate a shared function corresponding to each candidate code segment; the code optimization module is used to verify the semantic equivalence between each shared function and the corresponding candidate code segment, and use the verified shared functions to rewrite and optimize the code to generate the final optimized code.

[0013] Optionally, the code generation module is specifically used to replace hard-coded constants in the generated code with variable forms based on a pre-built parameterized mapping table to obtain the replaced code; the code generation module is also specifically used to determine the input ports and output ports of each subsystem based on the model structure and connection relationship of the target Simulink model, and add comments to the replaced code based on the input ports and output ports of each subsystem to obtain the code to be optimized; wherein, the comments in the code to be optimized are used to identify different code regions.

[0014] Optionally, the large language model includes: an equivalence extraction agent and a constraint verification agent; the equivalence recognition module is specifically used to construct a first prompt word for the equivalence extraction agent and a second prompt word for the constraint verification agent based on the first prompt information; the equivalence recognition module is further used to identify candidate code segments of the code to be optimized using the equivalence extraction agent based on the first prompt word, obtain multiple code segments, and generate a shared function corresponding to each code segment; the equivalence recognition module is further used to verify the multiple code segments and the shared function corresponding to each code segment using the constraint verification agent based on the second prompt word, and repair the shared function if an anomaly is found during verification, finally obtaining the multiple candidate codes and the shared function corresponding to each candidate code; wherein, the first prompt information includes at least one of the following: equivalence cases, subsystem semantics, formatting specifications, and counterexamples.

[0015] Optionally, the equivalence recognition module is specifically used to compile the target shared function using a compiler. If the compilation is successful, the target shared function is determined to have been successfully verified. Otherwise, a third prompt word is constructed based on the second prompt information, and the constraint verification agent is used to repair the target shared function based on the third prompt word. The target shared function is any one of the multiple shared functions corresponding to the multiple code segments. The second prompt information includes at least one of the following: compilation error information, the target shared function, and counterexamples.

[0016] Optionally, the code optimization module is specifically used to generate test functions corresponding to each candidate code segment based on a data flow graph; the data flow graph is used to characterize the source and target ends connected to the target Simulink model; and the Satisfiability Modulus Theory (SMT) solver is used to perform semantic equivalence verification on the test functions and shared functions corresponding to each candidate code segment, generating verification results for each candidate code segment.

[0017] Optionally, the code optimization module is specifically used to rewrite the target candidate code segment using the shared function corresponding to the target candidate code segment when the verification result of the target candidate code segment indicates that the corresponding test function and shared function have passed the verification, thereby obtaining a rewritten code segment; the code optimization module is further used to compile the target candidate code segment and the rewritten code segment separately using a compiler, and if the compilation result indicates that the shared function corresponding to the target candidate code segment can reduce the amount of code, then the final optimized code is generated based on the shared function corresponding to the target candidate code segment; wherein, the target candidate code segment is any one of the plurality of candidate code segments.

[0018] Optionally, the code optimization module is specifically used to send failure feedback information to the equivalent extraction agent to regenerate the shared function when the verification result of the target candidate code segment indicates that the corresponding test function and shared function have failed verification, until the shared function generated by the equivalent extraction agent passes verification; wherein, the failure feedback information includes at least one of the following: the target candidate code segment, the shared function corresponding to the target candidate code segment, and the counterexample information output by the SMT solver.

[0019] This application also provides a computer program product, including a computer program / instructions that, when executed by a processor, implement the steps of the Simulink code optimization method based on a large language model as described above.

[0020] This application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the steps of any of the above-described Simulink code optimization methods based on a large language model.

[0021] This application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the Simulink code optimization method based on any of the above-described methods.

[0022] The Simulink code optimization method and apparatus based on a large language model provided in this application firstly generates code for the target Simulink model and supplements the generated code with semantic information to obtain code to be optimized; the semantic information is used for the identification of equivalent code segments; then, the large language model is used to identify semantically equivalent code segments in the code to be optimized, resulting in multiple candidate code segments, and a shared function corresponding to each of the multiple candidate code segments is generated; finally, the semantic equivalence between each shared function and the corresponding candidate code segment is verified, and the verified shared functions are used for code rewriting and compilation optimization to generate the final optimized code. Thus, by using a large language model to identify code segments for equivalence and reducing the identified code segments, memory consumption is reduced. Attached Figure Description

[0023] To more clearly illustrate the technical solutions in this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0024] Figure 1This is a schematic diagram of the system architecture provided in this application for executing a Simulink code optimization method based on a large language model; Figure 2 This is one of the flowcharts illustrating the Simulink code optimization method based on a large language model provided in this application; Figure 3 This is the second flowchart of the Simulink code optimization method based on a large language model provided in this application; Figure 4 This is a schematic diagram of the Simulink code optimization device based on a large language model provided in this application; Figure 5 This is a schematic diagram of the structure of the electronic device provided in this application. Detailed Implementation

[0025] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0026] The terms "first," "second," etc., used in the specification and claims of this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.

[0027] Due to stringent resource constraints, especially in embedded systems, higher demands are placed on the quality of generated code. For example, automotive electronic control units (ECUs) must simultaneously support thousands of different applications within limited memory resources. In such cases, even minor inefficiencies in program size can have serious limitations. Since assembly code accounts for a large portion of the compiled output and directly impacts memory usage, reducing its size is a crucial step towards efficient resource deployment.

[0028] In related technologies, code generators and compilers employ various optimization techniques to minimize code size. Simulink Embedded Coder uses a matching-based approach to eliminate code redundancy. Specifically, it treats subsystems as basic units, generating independent functions for identical subsystems with the same components, parameters, and configurations. Mainstream compilers, such as GCC and Clang, also include various memory-related optimization strategies, such as common expression elimination, function inlining, and register allocation strategies. Despite these significant improvements, difficulties remain when handling semantically equivalent code segments. Simulink Embedded Coder struggles to generate shared functions for equivalent code segments because it requires all modules and parameters to be identical. GCC and Clang can optimize arithmetic equivalence through numerous rules, but lack effective mechanisms for identifying and optimizing parameterization and control flow equivalence. Such equivalence often involves compensatory transformations or divergent control paths, requiring deep semantic reasoning across different program contexts, which is computationally expensive and potentially unsafe, making it difficult to achieve in traditional compiler optimizations.

[0029] To address the aforementioned technical problems in related technologies, this application provides a Simulink code optimization method based on a large language model. The core idea of ​​this method is to utilize Simulink subsystem semantics as a partitioning context to guide the LLM in identifying candidate code segments, and then rigorously verify their semantic equivalence using an SMT solver. First, this application generates code for a given Simulink model, parameterizes and extends the generated code with subsystem embedding, and constructs a data flow graph as a preparation stage. Subsequently, equivalent code segment examples are constructed, providing model semantic descriptions and format specifications, and generating counterexamples. This content serves as input prompts to the LLM for identifying candidate code segments. This application annotates the identified code segments and generates corresponding shared functions for extraction. To avoid errors, a constraint checking agent is introduced to verify the validity of the extracted functions, such as function definitions and parameter lists. After successful verification, this application generates test functions for the identified code segments and calls a Satisfiability Modulo Theories (SMT) solver to verify the extractability between the test functions and the shared functions. If verification fails, the system will send the result back to the LLM for correction. Finally, in this embodiment, the original code and the rewritten code are compiled separately, and only the rewritten versions of functions that can effectively reduce the code size are retained.

[0030] like Figure 1 The diagram shown is a system architecture diagram for executing a Simulink code optimization method based on a large language model, provided in an embodiment of this application. Figure 1As shown, the system includes: The optimized auxiliary content generation unit aims to supplement the generated code with subsystem semantic information based on the Simulink model semantics for code partitioning, while eliminating the disadvantages of LLM in identifying equivalent code segments. Given a Simulink model, this unit first calls the code generator to generate embedded code, and then supplements the generated code with additional semantic information through constant parameterization and subsystem metadata embedding. Subsequently, a data flow graph is constructed to lay the foundation for subsequent code optimization and verification.

[0031] The LLM-based equivalent code segment recognition unit aims to identify potential equivalent code segments as candidates based on supplementary semantic information. Simultaneously, it invokes a constraint checking agent to determine the syntactic correctness of the extracted lines and whether they meet the constraints. The unit first constructs semantically equivalent code segment examples, providing detailed descriptions of the model's semantics, format specifications, and counterexamples. These are synthesized into prompts for the equivalent code segment recognition agent to identify potentially extractable candidate code segments. Subsequently, the identified code segments are labeled, and corresponding shared functions are generated to extract the candidate code segments. Furthermore, the unit incorporates a constraint checking agent to verify the standardization and syntactic correctness of the generated functions (e.g., function signatures and parameter lists) and correct any identified errors.

[0032] Semantic Equivalence Verification Unit: The core objective of this unit is to verify the semantic consistency between shared functions and identified candidate code segments, and to save shared functions that effectively reduce the length of compiled code segments. This unit generates a test function for each candidate code segment and uses the SMT solver to verify the extractability between the test function and the shared function. If the verification fails, the failed candidate code segment, the corresponding shared function, and the counterexample information provided by the SMT solver are recorded as failure feedback and returned to Unit 2 for optimization and correction. After successful verification, the corresponding extractable code segment is rewritten using the shared function, and the original code and the rewritten code are pre-compiled. Finally, only those rewritten versions that effectively reduce code size are retained to synthesize and generate the final optimized code.

[0033] The Simulink code optimization method based on a large language model provided in this application will be described in detail below with reference to the accompanying drawings, through specific embodiments and application scenarios.

[0034] like Figure 2 As shown in the embodiment of this application, a Simulink code optimization method based on a large language model is provided. This method may include the following steps 201 to 203: Step 201: Generate code for the target Simulink model and supplement the generated code with semantic information to obtain the code to be optimized.

[0035] The semantic information is used for the identification of equivalent code segments.

[0036] For example, before optimizing the code, code is first generated for the target Simulink model using the Simulink Embedded Coder, and this code is considered a semantic baseline. That is, if the optimized code is semantically equivalent to the code generated by the Simulink Embedded Coder, the optimized version is considered correct. Then, semantic information is collected from the target model. The Simulink model is internally stored as a ZIP archive containing multiple XML files, which record the model's configuration information, components, connection relationships, etc. This embodiment implements a custom parser to extract and integrate this information, providing support for subsequent optimization.

[0037] Specifically, step 201 above may also include the following steps 201a1 and 201a2: Step 201a1: Based on the pre-built parameterized mapping table, replace the hard-coded constants in the generated code with variable forms to obtain the replaced code.

[0038] Step 201a2: Based on the model structure and connection relationship of the target Simulink model, determine the input ports and output ports of each subsystem, and add comments to the replaced code based on the input ports and output ports of each subsystem to obtain the code to be optimized.

[0039] The comments in the code to be optimized are used to identify different code regions.

[0040] For example, based on the collected model semantics, additional content can be added to the generated code and a data flow graph can be constructed, such as... Figure 3 As shown, the specific content includes: 1. Parameterization: Inequal constants can hinder the identification of extractable code snippets. Therefore, this application defines a parameterization map Mp, where each entry establishes a mapping relationship between the Const component in the Simulink model and its corresponding constant value. Subsequently, hard-coded constants in the generated code are replaced with variable forms according to Mp.

[0041] 2. Subsystem Embedding: Based on the model structure and connection relationships, the input and output ports of each subsystem are located, and annotations are embedded in the generated code to identify the corresponding code regions. This metadata provides contextual information for the subsequent identification of extractable code fragments.

[0042] 3. Data Flow Graph Construction: By analyzing the source and target ends of the connection, construct the data flow graph of the target Simulink model, and attach component attributes (such as name, function, and data type) to the corresponding nodes in the data flow graph.

[0043] Step 202: Use a large language model to identify semantically equivalent code segments in the code to be optimized, obtain multiple candidate code segments, and generate a shared function corresponding to each candidate code segment.

[0044] For example, after obtaining the information-filled code to be optimized, it is necessary to identify candidate code segments and generate shared functions to achieve extraction, and verify the constraints of the shared functions. In this embodiment, the large language model includes: an equivalence extraction agent and a constraint verification agent.

[0045] Specifically, step 202 above may also include the following steps: 202a, 202b, and 202c: Step 202a: Construct the first prompt word of the equivalent extraction agent and the second prompt word of the constraint verification agent based on the first prompt information.

[0046] Step 202b: Based on the first prompt word, the equivalent extraction agent is used to identify candidate code segments of the code to be optimized, resulting in multiple code segments, and a shared function corresponding to each code segment is generated.

[0047] Step 202c: Based on the second prompt word, the constraint verification agent is used to verify the multiple code segments and the shared functions corresponding to each code segment, and the shared functions are repaired if an anomaly is found during verification, so as to finally obtain the multiple candidate codes and the shared functions corresponding to each candidate code.

[0048] The first prompt information includes at least one of the following: equivalent cases, subsystem semantics, formatting specifications, and counterexamples.

[0049] For example, such as Figure 3 As shown, corresponding prompt words are constructed for the equivalence extraction agent and the constraint checking agent. In addition to the generated code itself, the prompt words also include the following three parts, used to guide the LLM in reasoning and recognition: 1. Equivalence Examples: This application provides representative semantically equivalent code snippets as few-shot cues to enhance the LLM's ability to identify candidate snippets. These examples cover various types of equivalence relations, such as parametric equivalence, arithmetic equivalence, and control flow equivalence. Each example is accompanied by an explanation of the equivalence principle and a corresponding shared function instance.

[0050] 2. Semantics and Specifications: This application provides subsystem semantics and annotation information to guide LLM in performing semantic reasoning beyond local context during recognition. Furthermore, this application defines formatting specifications, including function signatures and input / output parameter requirements, to ensure that the identified code snippets and extracted functions meet unified structural constraints.

[0051] 3. Counterexamples: This application provides typical counterexamples to verify identified code snippets and shared functions, such as syntax errors. Each counterexample is accompanied by a brief description of the violation and possible remediation strategies to help the model self-correct in subsequent iterations.

[0052] For example, such as Figure 3 As shown, this embodiment uses an equivalence extraction agent to identify extractable candidate code snippets and generate corresponding shared functions for optimization. First, a few-shot hint is constructed based on embedded code, equivalent examples, semantic and canonical information, and feedback from the semantic equivalence verification stage. Then, LLM is used for inference to identify candidate code snippets and their corresponding shared functions. To mitigate the illusion problem, a constraint checking agent is further invoked to verify each generated shared function. If verification fails, the function is discarded.

[0053] For example, such as Figure 3 As shown, when generating shared functions, LLM may produce seemingly reasonable but actually erroneous results, violating established specifications or containing syntax errors. These inaccuracies can render shared functions unusable or unreliable. Therefore, this application introduces a constraint-checking agent to verify the format specifications and syntactic correctness of shared functions. First, using the shared function, corresponding candidate code snippets, specification requirements, and counterexamples as input, a few-sample hint is constructed and queried by the LLM to verify whether the shared function conforms to the specification, while simultaneously fixing any violations found. The verification process mainly focuses on the following formatting constraints: 1. Function signature: The return type of a shared function must be void. The parameter list should appear in the same order as in the function code, and input parameters should precede output parameters.

[0054] 2. Input parameters: The type of each input parameter must be consistent with the definition in the original code snippet.

[0055] 3. Output parameters: Output parameters should be declared as pointer types, and their data types must be consistent with the corresponding code snippets.

[0056] 4. Local variables: Local variables should be limited to the function scope to avoid duplicate definitions or naming conflicts.

[0057] Specifically, step 202c above may also include the following step 202c1: Step 202c1: Compile the target shared function using a compiler. If the compilation is successful, the target shared function is verified successfully. Otherwise, construct a third prompt word based on the second prompt information, and use the constraint verification agent to repair the target shared function based on the third prompt word.

[0058] Wherein, the target shared function is any one of the multiple shared functions corresponding to the multiple code segments; the second prompt information includes at least one of the following: compilation error information, the target shared function, and counterexamples.

[0059] For example, in this embodiment, the steps for verifying the syntax correctness of a shared function include: First, calling a compiler (using GCC by default) to compile the shared function and collecting compilation diagnostic information. If compilation is successful, meaning the function has no syntax errors, this embodiment will return the verified function. If compilation fails, a prompt word will be constructed using compilation error information, the shared function, and counterexamples, and the LLM will be queried to fix the syntax errors. This process will be executed iteratively until all syntax errors are fixed or a preset attempt limit is reached. If the verification still fails after exceeding the limit, this embodiment will return an empty set, indicating that the verification has failed.

[0060] Step 203: Verify the semantic equivalence between each shared function and its corresponding candidate code segment, and use the verified shared functions to rewrite and optimize the code to generate the final optimized code.

[0061] For example, for candidate code segments and their shared functions, this application embodiment needs to verify the extractability between the two, that is, whether the shared function can safely replace the corresponding candidate code segment without affecting the program behavior.

[0062] Specifically, step 203 above, which verifies the semantic equivalence between each shared function and its corresponding candidate code segment, may further include steps 203a1 and 203a2: Step 203a1: Generate the test function corresponding to each candidate code segment based on the data flow graph.

[0063] The data flow graph is used to characterize the source and target ends connected to the target Simulink model.

[0064] Step 203a2: Use the Satisfiability Modulus (SMT) solver to perform semantic equivalence verification on the test function and shared function corresponding to each candidate code segment, and generate the verification result for each candidate code segment.

[0065] For example, in this embodiment of the application, a test function is first generated for the candidate code snippet, and then it is combined with the corresponding shared function to generate a test file for verification. This step includes mapping the operations in the candidate code snippet to the data flow graph obtained in the optimization auxiliary content generation stage. Next, this embodiment of the application derives the input parameters, output parameters, and local variables of the test function by analyzing the connection relationships in the mapping subgraph.

[0066] The specific steps are as follows: 1. If a variable belongs to a component that has no input port, or its input data comes from outside the subgraph, then the variable is an input parameter; 2. If a variable belongs to a component that has no output port, or its output data is used outside the subgraph, then the variable is an output parameter; 3. All other variables are classified as local variables.

[0067] Subsequently, in this embodiment, the input and output parameters are arranged sequentially according to the order in which the variables first appear in the candidate code snippets, and the candidate code snippets are rewritten using the corresponding parameter or local variable names. After completion, the generated test function and its corresponding shared function are integrated into the same test file.

[0068] In one possible implementation, within the main function of the test file, this embodiment declares the same input parameter variables for the test function and the shared function, but declares different variables for the output parameters. Finally, assertions are used to compare whether the output variables generated by the shared function and each test function are equal. It should be noted that since LLM may generate multiple shared functions for different candidate code snippets, this embodiment synthesizes and generates a separate test file for each shared function.

[0069] For example, the semantic equivalence verification in this application embodiment includes the following: For each synthesized test file, this application embodiment uses a C Bounded Model Checker (CBMC) to verify the semantic equivalence between the shared function and its corresponding test-driving function. The CBMC encodes program variables symbolically and solves the inserted assertions using an SMT solver such as Z3. Essentially, this application embodiment checks whether the output variables of the shared function and the test function are always equal across all valid input spaces.

[0070] Specifically, step 203 above, which involves rewriting and optimizing the code using the verified shared functions to generate the final optimized code, may further include the following steps 203b1 and 203b2: Step 203b1: If the verification result of the target candidate code segment indicates that the corresponding test function and shared function have passed the verification, the target candidate code segment is rewritten using the shared function corresponding to the target candidate code segment to obtain the rewritten code segment.

[0071] Step 203b2: Use a compiler to compile the target candidate code segment and the rewritten code segment respectively. If the compilation result indicates that the shared function corresponding to the target candidate code segment can reduce the amount of code, then generate the final optimized code based on the shared function corresponding to the target candidate code segment.

[0072] The target candidate code segment is any one of the plurality of candidate code segments.

[0073] For example, after semantic verification is completed, this invention rewrites the corresponding extractable code snippets using each shared function. Subsequently, this invention compiles the rewritten code and the original code using the target compiler, depending on the target deployment platform. If the introduction of a shared function can reduce the code size, then the function is considered an optimizable function, and this invention retains it for synthesis to generate the final optimized code.

[0074] Specifically, step 203 above may also include the following step 203b3: Step 203b3: If the verification result of the target candidate code segment indicates that the corresponding test function and shared function have failed verification, the failure feedback information is sent to the equivalent extraction agent to regenerate the shared function until the shared function generated by the equivalent extraction agent passes verification.

[0075] The failure feedback information includes at least one of the following: the target candidate code segment, the shared function corresponding to the target candidate code segment, and the counterexample information output by the SMT solver.

[0076] For example, such as Figure 3 As shown, if the assertion is true, it indicates that the corresponding test-driven function and the shared function are semantically equivalent. Therefore, the candidate code segments associated with these test functions are extractable and can be safely replaced by the shared function without changing the observable behavior of the program. For candidate code segments that fail verification, this embodiment returns these segments, the corresponding shared functions, and the counterexample information provided by the SMT solver as failure feedback to the equivalence extraction agent for improvement. This verification-optimization feedback loop continues until all candidate code segments pass the extractability verification or a preset verification limit is reached. If the verification still fails after exceeding this limit, this embodiment conservatively discards candidate segments that fail the semantic equivalence verification to ensure the correctness of the final generated code.

[0077] The Simulink code optimization method based on a large language model provided in this application first generates code for the target Simulink model and supplements the generated code with semantic information to obtain the code to be optimized; the semantic information is used for the identification of equivalent code segments; then, the large language model is used to identify semantically equivalent code segments in the code to be optimized, resulting in multiple candidate code segments, and a shared function corresponding to each of the multiple candidate code segments is generated; finally, the semantic equivalence between each shared function and the corresponding candidate code segment is verified, and the verified shared functions are used for code rewriting and compilation optimization to generate the final optimized code. Thus, by using a large language model to identify code segments for equivalence and reducing the identified code segments, memory usage is reduced.

[0078] It should be noted that the Simulink code optimization method based on a large language model provided in this application embodiment can be executed by a Simulink code optimization device based on a large language model, or by a control module within that device for executing the Simulink code optimization method based on a large language model. This application embodiment uses the execution of the Simulink code optimization method based on a large language model by a Simulink code optimization device as an example to illustrate the Simulink code optimization device based on a large language model provided in this application embodiment.

[0079] It should be noted that, in the embodiments of this application, the Simulink code optimization methods based on large language models shown in the accompanying drawings are all illustrated using one accompanying drawing from one of the embodiments of this application as an example. In specific implementation, the Simulink code optimization methods based on large language models shown in the accompanying drawings of the above methods can also be implemented in conjunction with any other accompanying drawings illustrated in the above embodiments, which will not be elaborated here.

[0080] The Simulink code optimization device based on a large language model provided in this application is described below. The Simulink code optimization method based on a large language model described above can be referred to in the following text.

[0081] Figure 4 A schematic diagram of the Simulink code optimization device based on a large language model provided in the embodiments of this application is shown below. Figure 4 As shown, it specifically includes: The code generation module 401 is used to generate code for the target Simulink model and supplement the generated code with semantic information to obtain the code to be optimized; the semantic information is used for the identification of equivalent code segments; the equivalence identification module 402 is used to use a large language model to identify semantically equivalent code segments in the code to be optimized, obtain multiple candidate code segments, and generate a shared function corresponding to each candidate code segment; the code optimization module 403 is used to verify the semantic equivalence between each shared function and the corresponding candidate code segment, and use the verified shared function to rewrite and optimize the code to generate the final optimized code.

[0082] Optionally, the code generation module 401 is specifically used to replace hard-coded constants in the generated code with variable forms based on a pre-built parameterized mapping table to obtain the replaced code; the code generation module 401 is also specifically used to determine the input ports and output ports of each subsystem based on the model structure and connection relationship of the target Simulink model, and add comments to the replaced code based on the input ports and output ports of each subsystem to obtain the code to be optimized; wherein, the comments in the code to be optimized are used to identify different code regions.

[0083] Optionally, the large language model includes: an equivalence extraction agent and a constraint verification agent; the equivalence recognition module 402 is specifically used to construct a first prompt word for the equivalence extraction agent and a second prompt word for the constraint verification agent based on the first prompt information; the equivalence recognition module 402 is further used to identify candidate code segments of the code to be optimized using the equivalence extraction agent based on the first prompt word, obtain multiple code segments, and generate a shared function corresponding to each code segment; the equivalence recognition module 402 is further used to verify the multiple code segments and the shared function corresponding to each code segment using the constraint verification agent based on the second prompt word, and repair the shared function if an anomaly is found, finally obtaining the multiple candidate codes and the shared function corresponding to each candidate code; wherein, the first prompt information includes at least one of the following: equivalence cases, subsystem semantics, formatting specifications, and counterexamples.

[0084] Optionally, the equivalence recognition module 402 is specifically used to compile the target shared function using a compiler. If the compilation is successful, the target shared function is determined to have been successfully verified. Otherwise, a third prompt word is constructed based on the second prompt information, and the constraint verification agent is used to repair the target shared function based on the third prompt word. The target shared function is any one of the multiple shared functions corresponding to the multiple code segments. The second prompt information includes at least one of the following: compilation error information, the target shared function, and counterexamples.

[0085] Optionally, the code optimization module 403 is specifically used to generate test functions corresponding to each candidate code segment based on a data flow graph; the data flow graph is used to characterize the source end and target end connected to the target Simulink model; and the Satisfiability Modulus Theory (SMT) solver is used to perform semantic equivalence verification on the test functions and shared functions corresponding to each candidate code segment, generating verification results for each candidate code segment.

[0086] Optionally, the code optimization module 403 is specifically used to rewrite the target candidate code segment using the shared function corresponding to the target candidate code segment when the verification result of the target candidate code segment indicates that the corresponding test function and shared function have passed the verification, thereby obtaining a rewritten code segment; the code optimization module 403 is also specifically used to compile the target candidate code segment and the rewritten code segment separately using a compiler, and if the compilation result indicates that the shared function corresponding to the target candidate code segment can reduce the amount of code, then the final optimized code is generated based on the shared function corresponding to the target candidate code segment; wherein, the target candidate code segment is any one of the plurality of candidate code segments.

[0087] Optionally, the code optimization module 403 is specifically used to send failure feedback information to the equivalent extraction agent to regenerate the shared function when the verification result of the target candidate code segment indicates that the corresponding test function and shared function have failed verification, until the shared function generated by the equivalent extraction agent passes verification; wherein, the failure feedback information includes at least one of the following: the target candidate code segment, the shared function corresponding to the target candidate code segment, and the counterexample information output by the SMT solver.

[0088] The Simulink code optimization apparatus based on a large language model provided in this application first generates code for the target Simulink model and supplements the generated code with semantic information to obtain code to be optimized; the semantic information is used for the identification of equivalent code segments; then, the large language model is used to identify semantically equivalent code segments in the code to be optimized, resulting in multiple candidate code segments, and a shared function corresponding to each of the multiple candidate code segments is generated; finally, the semantic equivalence between each shared function and the corresponding candidate code segment is verified, and the verified shared functions are used for code rewriting and compilation optimization to generate the final optimized code. Thus, by using a large language model to identify code segments for equivalence and reducing the identified code segments, memory consumption is reduced.

[0089] Figure 5 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 5 As shown, the electronic device may include a processor 510, a communications interface 520, a memory 530, and a communication bus 540, wherein the processor 510, communications interface 520, and memory 530 communicate with each other via the communication bus 540. The processor 510 can call logical instructions in the memory 530 to execute a Simulink code optimization method based on a large language model. This method includes: first, generating code for the target Simulink model and supplementing the generated code with semantic information to obtain code to be optimized; the semantic information is used for identifying equivalent code segments; then, using the large language model to identify semantically equivalent code segments in the code to be optimized, obtaining multiple candidate code segments, and generating a shared function corresponding to each candidate code segment; finally, verifying the semantic equivalence between each shared function and the corresponding candidate code segment, and using the verified shared functions for code rewriting and compilation optimization to generate the final optimized code. In this way, the large language model is used to identify the equivalence of code segments and the identified code segments are reduced to reduce memory usage.

[0090] Furthermore, the logical instructions in the aforementioned memory 530 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0091] On the other hand, this application also provides a computer program product, which includes a computer program stored on a computer-readable storage medium. The computer program includes program instructions, and when the program instructions are executed by a computer, the computer can execute the Simulink code optimization method based on a large language model provided by the above methods. This method includes: first, performing code generation on a target Simulink model and supplementing the generated code with semantic information to obtain code to be optimized; the semantic information is used for the identification of equivalent code segments; then, using a large language model to identify semantically equivalent code segments in the code to be optimized, obtaining multiple candidate code segments, and generating a shared function corresponding to each of the multiple candidate code segments; finally, verifying the semantic equivalence between each shared function and the corresponding candidate code segment, and using the verified shared function to perform code rewriting and compilation optimization to generate the final optimized code. Thus, by using a large language model to identify code segments for equivalence and reducing the identified code segments, memory usage is reduced.

[0092] Furthermore, this application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, performs the aforementioned Simulink code optimization methods based on large language models. This method includes: first, generating code for a target Simulink model and supplementing the generated code with semantic information to obtain code to be optimized; the semantic information is used for identifying equivalent code segments; then, using a large language model to identify semantically equivalent code segments in the code to be optimized, obtaining multiple candidate code segments, and generating a shared function corresponding to each candidate code segment; finally, verifying the semantic equivalence between each shared function and its corresponding candidate code segment, and using the verified shared functions for code rewriting and compilation optimization to generate the final optimized code. Thus, by using a large language model to identify code segments for equivalence and reducing the identified code segments, memory usage is reduced.

[0093] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0094] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0095] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.

Claims

1. A Simulink code optimization method based on a large language model, characterized in that, include: Code generation is performed on the target Simulink model, and semantic information is added to the generated code to obtain the code to be optimized; The semantic information is used for the identification of equivalent code segments; Using a large language model, semantically equivalent code segments are identified in the code to be optimized, resulting in multiple candidate code segments, and a shared function corresponding to each candidate code segment is generated. Verify the semantic equivalence between each shared function and its corresponding candidate code segment, and use the verified shared functions to rewrite and optimize the code, generating the final optimized code.

2. The method according to claim 1, characterized in that, The step of supplementing the generated code with semantic information to obtain the code to be optimized includes: Based on a pre-built parameterized mapping table, hard-coded constants in the generated code are replaced with variable forms to obtain the replaced code; Based on the model structure and connection relationship of the target Simulink model, the input ports and output ports of each subsystem are determined, and comments are added to the replaced code based on the input ports and output ports of each subsystem to obtain the code to be optimized. The comments in the code to be optimized are used to identify different code regions.

3. The method according to claim 1, characterized in that, The large language model includes: an equivalence extraction agent and a constraint verification agent; The step of using a large language model to identify semantically equivalent code segments in the code to be optimized, obtaining multiple candidate code segments, and generating a shared function corresponding to each of the multiple candidate code segments includes: The first prompt word of the equivalent extraction agent and the second prompt word of the constraint verification agent are constructed based on the first prompt information; Based on the first prompt word, the equivalent extraction agent is used to identify candidate code segments of the code to be optimized, resulting in multiple code segments, and a shared function corresponding to each code segment is generated; Based on the second prompt word, the constraint verification agent is used to verify the multiple code segments and the shared functions corresponding to each code segment, and to repair the shared functions if an anomaly is found during verification, so as to finally obtain the multiple candidate codes and the shared functions corresponding to each candidate code. The first prompt information includes at least one of the following: equivalent cases, subsystem semantics, formatting specifications, and counterexamples.

4. The method according to claim 3, characterized in that, The process of using the constraint-checking agent to verify the multiple code segments and the shared functions corresponding to each code segment, and repairing the shared functions in case of anomalies, includes: The target shared function is compiled using a compiler. If the compilation is successful, the target shared function is verified successfully. Otherwise, a third prompt word is constructed based on the second prompt information, and the constraint verification agent is used to repair the target shared function based on the third prompt word. Wherein, the target shared function is any one of the multiple shared functions corresponding to the multiple code segments; the second prompt information includes at least one of the following: compilation error information, the target shared function, and counterexamples.

5. The method according to claim 1, characterized in that, The verification of the semantic equivalence between each shared function and its corresponding candidate code segment includes: The test function corresponding to each candidate code segment is generated based on the data flow graph; the data flow graph is used to represent the source and target ends connected to the target Simulink model. The Satisfiability Modulus (SMT) solver is used to perform semantic equivalence verification on the test function and shared function corresponding to each candidate code segment, generating the verification result for each candidate code segment.

6. The method according to claim 5, characterized in that, The process of rewriting and optimizing code using verified shared functions to generate final optimized code includes: If the verification result of the target candidate code segment indicates that the corresponding test function and shared function have passed the verification, the target candidate code segment is rewritten using the shared function corresponding to the target candidate code segment to obtain the rewritten code segment; The target candidate code segment and the rewritten code segment are compiled using a compiler. If the compilation result indicates that the shared function corresponding to the target candidate code segment can reduce the amount of code, the final optimized code is generated based on the shared function corresponding to the target candidate code segment. The target candidate code segment is any one of the plurality of candidate code segments.

7. The method according to claim 5, characterized in that, After generating the verification result for each candidate code segment, the method further includes: If the verification result of the target candidate code segment indicates that the corresponding test function and shared function have failed the verification, the failure feedback information is sent to the equivalent extraction agent to regenerate the shared function until the shared function generated by the equivalent extraction agent passes the verification. The failure feedback information includes at least one of the following: the target candidate code segment, the shared function corresponding to the target candidate code segment, and the counterexample information output by the SMT solver.

8. A Simulink code optimization device based on a large language model, characterized in that, The device includes: The code generation module is used to generate code for the target Simulink model and supplement the generated code with semantic information to obtain the code to be optimized; the semantic information is used for the identification of equivalent code segments; The equivalence identification module is used to identify semantically equivalent code segments in the code to be optimized using a large language model, obtain multiple candidate code segments, and generate a shared function corresponding to each candidate code segment among the multiple candidate code segments; The code optimization module is used to verify the semantic equivalence between each shared function and its corresponding candidate code segment, and to rewrite and optimize the code using the verified shared functions to generate the final optimized code.

9. An electronic device, characterized in that, It includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements the steps of the Simulink code optimization method based on any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, implements the steps of the Simulink code optimization method based on a large language model as described in any one of claims 1 to 7.