A compiler performance optimization method
By calculating the complexity of the intermediate representation at the intermediate code level and screening out effective optimization passes, the problem of invalid or negative optimization in the compiler is solved, thus shortening the compilation time and improving the performance of the target program.
Patent Information
- Application Number
- CN202510962698.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-14
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2045-07-14
AI Technical Summary
In the prior art, compilers often perform too many invalid or negative optimizations during the optimization phase, resulting in wasted computing resources, extended compilation time, and reduced target program performance.
By calculating the intermediate representation complexity at the intermediate code level, we can filter out optimization passes with positive optimization effects, generate a customized optimization pass list, and only perform effective optimizations to avoid invalid or negative optimizations.
It shortens the compilation time, improves the target program performance, is applicable to multiple target platforms, and has good versatility.
Smart Images

Figure CN120491975B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer program compilation, and more particularly, to a compiler performance optimization method. Background Art
[0002] In modern software development, programmers often improve program performance by simply specifying optimization levels (such as -O1, -O2, and -O3) during the compilation phase. However, these optimization levels correspond to the combined execution of hundreds of optimization passes within the compiler. For example, when using the Clang compiler, the -O3 option typically triggers the execution of up to 300 optimization passes.
[0003] The optimization phase accounts for the largest portion of the time spent in the entire compilation process. Although the original intention of the optimization pass is to improve program efficiency or reduce resource consumption, the actual effect varies depending on factors such as the specific program, target platform, and optimization sequence. Specifically, the effects of the optimization pass can be roughly divided into three categories:
[0004] Effective optimization: Positively impacts the final code, such as improving runtime performance, reducing the number of instructions, or reducing code size.
[0005] Ineffective optimization: No substantial changes to the program code, only increasing the compilation time burden.
[0006] Negative optimization: Instead, it leads to program performance degradation or code expansion, which has a negative impact.
[0007] Excessive ineffective or negative optimizations not only waste computing resources, extend compilation time, and even degrade target program performance, severely impacting the developer's compiler experience. Therefore, how to quickly determine whether an optimization pass has a positive effect on the current program and, based on this, decide whether to execute it has become a pressing issue. Summary of the Invention
[0008] The present invention aims to solve the technical problems existing in the prior art and provides a compiler performance optimization method, which can overcome the problem that too much invalid or negative optimization not only wastes computing resources, prolongs compilation time, but even reduces the performance of the target program.
[0009] The present invention provides a compiler performance optimization method, comprising:
[0010] Step S1, compile the source code program into initial intermediate code based on the lowest optimization level ;
[0011] Step S2: Get the optimization names of all optimization passes of the compiler used and put them into the complete optimization list set Among them, , To optimize the name, To optimize the number of passes;
[0012] Step S3, using the intermediate code optimization tool provided by the compiler, the initial intermediate code is optimized in sequence Execute the complete optimization list set For each optimization Pass in the process, we get the intermediate code set of the execution result. , intermediate code Intermediate code Run Optimization Pass The intermediate code result obtained after
[0013] Step S4, calculate the intermediate code set Each intermediate code in The complexity of the intermediate representation ;
[0014] Step S5, according to each intermediate code The complexity of the intermediate representation is constructed to optimize the complexity change vector ,in, Represents each intermediate code The complexity of the change;
[0015] Step S6, according to the optimization complexity change vector Each intermediate code The complexity changes of the forward optimization pass are screened out, and a customized optimization pass list set is generated. , the customized optimization Pass list collection What is saved in is the optimization name of the forward optimization pass;
[0016] Step S7, based on the minimum optimization level and the customized optimization Pass list set The name of the forward optimization pass in , which compiles the source code into the target program.
[0017] The present invention provides a compiler performance optimization method, which compiles the source code program into the initial intermediate code based on the lowest optimization level. ; Sequentially process the initial intermediate code Execute each optimization to obtain the intermediate code of the execution result; calculate the intermediate representation complexity of the intermediate code; screen out optimizations with positive optimization effects based on the intermediate representation complexity of each intermediate code; compile the source program from the source code into the target program based on the positive optimization. The present invention proposes a method for judging the effectiveness of optimization based on the intermediate representation complexity at the intermediate code level, so as to avoid invalid or negative optimization during the compilation process; the method does not rely on the specific hardware architecture, has good versatility, and is applicable to a variety of target platforms; by dynamically evaluating the Pass effect at the intermediate language level, the present invention not only greatly shortens the compilation time, but also makes the target program performance better. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 A flow chart of a compiler performance optimization method provided by one embodiment of the present invention;
[0019] Figure 2 This is an overall flow chart of a compiler performance optimization method according to an embodiment of the present invention;
[0020] Figure 3 This is the main workflow diagram of the compiler;
[0021] Figure 4 A flowchart of calculating the intermediate representation complexity of an intermediate code according to an embodiment of the present invention;
[0022] Figure 5 A flowchart for constructing and optimizing complex change vectors according to one embodiment of the present invention;
[0023] Figure 6 This is a flowchart of forward optimization for screening according to one embodiment of the present invention. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present invention. In addition, the technical features in the various embodiments or single embodiments provided by the present invention can be arbitrarily combined with each other to form a feasible technical solution. This combination is not restricted by the sequence of steps and / or structural composition mode, but must be based on the ability of ordinary technicians in this field to implement it. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.
[0025] Based on the problems in the background technology, the present invention provides a compiler performance optimization method that calculates the intermediate representation complexity at the intermediate code level, determines whether each optimization is a forward optimization based on the intermediate representation complexity, and only executes optimizations with positive effects.
[0026] The solution of the present invention is mainly divided into two parts: the first part: compiles the source code program (such as C, Java, etc.) into intermediate code (such as LLVMIR) using the lowest optimization level (such as -00), runs each optimization pass in sequence at the intermediate code layer, calculates the intermediate representation complexity to judge the effectiveness of each optimization pass, and establishes an intermediate complexity change vector; the second part: uses the established intermediate complexity change vector to guide compilation, avoid invalid or negative optimization, shorten compilation time, and improve the performance of the target program.
[0027] Among them, the intermediate code in the present invention can refer to LLVMIR in the Clang / LLVM architecture, or GIMPLE of GCC, etc., or various customized intermediate codes. It is a key intermediate code form in the compilation process, which is used to transfer and optimize program code between the front end (source code parsing) and the back end (code generation).
[0028] See also Figure 1 and Figure 2 The compiler performance optimization method provided by the present invention comprises the following steps:
[0029] Step S1, compile the source code program into initial intermediate code based on the lowest optimization level .
[0030] It is understandable that the source code is usually a high-level program code (such as C, Java, etc.), which is compiled into the initial intermediate code based on the compiler using the lowest optimization level (such as -O0) (such as LLVMIR, GIMPLE, etc.).
[0031] Among them, a compiler is a program that converts the original code written in a high-level programming language (C, C++ code, etc.) into another representation (machine binary language).
[0032] The main purpose of a compiler is to translate high-level computer languages that are easy for humans to write, read, and maintain into machine languages that computers can interpret and run, that is, executable programs. The source code is generally a high-level language (High-level language), such as Pascal, C, C++, C#, Java, etc., while the target language is assembly language or the target machine's object code (Object code), sometimes also called machine code. The main workflow of the compiler can be seen in Figure 3 .
[0033] The minimum optimization level ensures that only necessary semantically preserving transformations are performed during compilation, preventing unnecessary optimizations from affecting subsequent complexity change analysis, thereby achieving a gradual and controllable optimization evaluation.
[0034] Step S2: Get the optimization names of all optimization passes of the compiler used and put them into the complete optimization list set Among them, , To optimize the name, To optimize the number of passes.
[0035] It is understandable that a Pass is a step used in the compiler to optimize or analyze a program. A program usually goes through multiple Passes, and each Pass may be executed multiple times.
[0036] The following describes the optimization pass using the Clang / LLVM architecture as an example.
[0037] Converting LLVM IR to target assembly code involves several steps. The IR is transformed into a backend-friendly representation of instructions, functions, and global variables. This representation evolves as the program passes through the various backend stages, becoming closer and closer to the actual target machine instructions.
[0038] To obtain the optimization list, you can check the corresponding manual of the compiler used, or use the highest optimization level provided by the compiler (such as O3) and a specific command to obtain it. For example, for the Clang / LLVM architecture, you can use the command opt -S input.ll -o output.ll -O3 -debug-pass=Structure.
[0039] Step S3, using the intermediate code optimization tool provided by the compiler, the initial intermediate code is optimized in sequence Execute the complete optimization list set For each optimization Pass in the process, we get the intermediate code set of the execution result. , intermediate code Intermediate code Run Optimization Pass The intermediate code result obtained after .
[0040] It is understandable that each compiler provides a corresponding intermediate code optimization tool. For example, the Clang / LLVM architecture can use opt -S -{pi} input.ll -o output.ll, where opt is the intermediate language optimization work provided by Clang / LLVM, and input.ll is the intermediate code before optimization. , output.ll is executed The optimized intermediate code result is as follows: .
[0041] The intermediate representation (IR) is a data structure or code used by compilers to represent source code. It is the program representation between the source language and the target language during the compilation process. Almost all compilers require some form of intermediate representation to model the code being analyzed, transformed, and optimized. During the compilation process, the intermediate representation must be sufficiently expressive to accurately represent the source code without losing information, and must fully consider the completeness of the compilation from source to target code, the ease of use of compilation optimization, and the performance.
[0042] In step S3, the intermediate code optimization tool provided by the compiler is used to optimize the initial intermediate code in sequence. Execute the complete optimization list set For each optimization Pass in the process, we get the intermediate code set of the execution result. ,include:
[0043] Initial intermediate code Execute Optimization Pass , get the intermediate code of the execution result ;
[0044] Intermediate code Execute Optimization Pass , get the intermediate code of the execution result ;
[0045] And so on, for the intermediate code Execute Optimization Pass , get the intermediate code of the execution result ;
[0046] Code between execution results A collection of intermediate codes that make up the execution results .
[0047] Step S4, calculate the intermediate code set Each intermediate code in The complexity of the intermediate representation .
[0048] See Figure 4 In one embodiment of the present invention, step S4 is to calculate the intermediate code set Each intermediate code in The complexity of the intermediate representation ,include:
[0049] Step S401, give each intermediate code Each intermediate code instruction is assigned a first weight, wherein the first weight of each intermediate code instruction is assigned based on the instruction cost model such as the number of CPU cycles consumed by the instruction. It is understandable that each intermediate code instruction It includes multiple intermediate code instructions, and a weight function is established for each intermediate code instruction , the input parameter of the weight function is the instruction name, and it will return the weight of the intermediate code instruction, which is called the first weight.
[0050] The instruction weight function can return the number of cycles required for the instruction based on the instruction cost model, such as the number of cycles required for the instruction as described in the specific CPU manual. For example, if it is a load / store instruction, which is very time-consuming, it can be set to return 30, while simple calculation instructions such as add / sub can be set to return 8. The first weights assigned to different instructions can be found in Table 1 below.
[0051] See Table 1 below for a table of different instruction names and instruction weights.
[0052] Instruction number Command Name Instruction Weight 1 load 30 2 store 30 3 add 8 4 sub 8 … … …
[0053] Step S402: assign a second weight to each control flow graph structure type and analyze each intermediate code The control flow graph structure types included and the number of each control flow graph structure type.
[0054] A control flow graph is a graphical tool for representing program control flow, commonly used in compiler design, code analysis (such as code coverage analysis and defect detection), and software testing. It abstracts the control structures within a program (such as sequences, conditionals, and loops) and represents the program as a directed graph, facilitating analysis of the program's execution path and logical structure.
[0055] (1) Nodes:
[0056] Represents a basic block (BasicBlock) in a program: a continuous section of code with only one entry and one exit (that is, during execution, it can only be entered from the beginning of the block and exited from the end of the block, with no branches or jumps in between).
[0057] Special nodes:
[0058] Start node: represents the starting point of the program (possibly corresponding to the first basic block).
[0059] End node: indicates the end point of the program (may correspond to the last basic block).
[0060] (2) Edges:
[0061] It represents the control transfer relationship between basic blocks, that is, the possible path from one basic block to another during program execution.
[0062] (3) Common edge types:
[0063] Conditional edge: corresponds to the branch after the conditional judgment (such as if-else).
[0064] Loop edge: corresponds to the back edge of the loop structure (such as for, while).
[0065] Sequential edge: corresponds to the sequential execution without branching.
[0066] Analyze each intermediate code according to existing tools The control flow graph structure type mainly includes 6 types of structures: loop structure, basic block number, branch edge, conditional jump, switch statement, and function call. Based on the existing tools, the control flow graph structure type contained in each intermediate code and the number of each control flow graph type are analyzed and counted. It should be noted that each intermediate code The control flow graph does not necessarily include all 6 types, but may only include some of them. In this case, the embodiment of the present invention assigns a weight (called the second weight) to each structure type and uses existing tools to count each intermediate code. The structure types of the control flow graph included and the number of each structure type.
[0067] For example, the following weights can be assigned to the six control flow graph structures:
[0068] Since the loop structure consumes a lot of CPU cycles, 105 is assigned. The following values can be adjusted according to the specific CPU situation. Please refer to Table 2 below.
[0069] Table 2 Weights assigned to different CFG structures
[0070]
[0071] Step S403, according to each intermediate code The first weight of each intermediate code instruction is calculated, and the first weight sum of all intermediate code instructions of the intermediate code is calculated.
[0072] Specifically, traverse the intermediate code Each intermediate code instruction , calculate the first weight sum of all intermediate code instructions ,in Intermediate code The number of intermediate code instructions, Indicates the The first weight of the intermediate code instruction.
[0073] Step S404: According to each intermediate code The second weight of each control flow graph type is used to calculate the intermediate code The control flow weight and ,in, Indicates type The second weight of the control flow graph, Indicates intermediate code The type is The number of control flow graphs, means The number of types of control flow graphs in .
[0074] Step S405, calculate each intermediate code The complexity of the intermediate representation .
[0075] Specifically, calculate the intermediate code The complexity of the intermediate representation , ,in Intermediate code The first weight sum of all intermediate code instructions, Intermediate code The second weight of the control flow graph and, is the control flow graph weight coefficient.
[0076] Among them, the control flow weight is always It can be set according to the actual architecture characteristics. For example, when the control flow changes consume a lot of CPU cycles, you can set It is set to be larger, otherwise it can be Set it smaller.
[0077] Step S5, according to each intermediate code The complexity of the intermediate representation is constructed to optimize the complexity change vector ,in, Represents each intermediate code The complexity changes.
[0078] Understandably, see Figure 5 , step S5 mainly includes:
[0079] Step S501: for each intermediate code , calculate its complexity change , that is, calculating the intermediate code Complete optimization The intermediate code generated later , the complexity change of the intermediate code.
[0080] in, Indicates that among all the calculated Select the largest subscript as , making The reason for this is that if , indicating that this optimization is an invalid optimization or negative optimization, and will not be executed later. Therefore, when calculating the complexity change difference, we should choose the last positive optimization that has been calculated and will be executed, that is, The corresponding optimized subscript is .
[0081] Step S502: Constructing an optimization complexity change vector ,here Indicates the total number of optimization passes.
[0082] Step S6, according to the optimization complexity change vector Each intermediate code The complexity changes of the forward optimization pass are screened out, and a customized optimization pass list set is generated. , the customized optimization Pass list collection The optimization name of the forward optimization pass is included.
[0083] Understandably, see Figure 6 , step S6 mainly includes the following steps:
[0084] Step S601: Optimize the complexity change vector Every dimension ,Will Compare with 0.
[0085] Step S602, if , no processing is done. Because the intermediate code will not change when executing this optimization, the corresponding optimization Invalid optimization.
[0086] Step S603, if , no processing is done, because executing this optimization will make the target code more complex and the program performance will decrease. Negative optimization.
[0087] Step S604, if , and optimize its corresponding Add to the custom optimization pass list Because executing this optimization will make the target code simpler and improve program performance, the corresponding optimization For effective optimization.
[0088] For example , then it means and For effective optimization, and For negative optimization, Ineffective optimization, ultimately .
[0089] Each compiler provides a corresponding intermediate code optimization tool. For example, the Clang / LLVM architecture can use opt-S -{pi} input.ll -o output.ll, where opt is the intermediate language optimization work provided by Clang / LLVM, and input.ll is the intermediate code before optimization. , output.ll is executed The optimized intermediate code results are as follows .
[0090] Step S7, based on the minimum optimization level and the customized optimization Pass list set The name of the forward optimization pass in , which compiles the source program from source code into the target program.
[0091] It is understandable that using the lowest optimization level (such as -O0) and a custom optimization pass list set The optimization pass name given in compiles the source program from source code into the target program.
[0092] Using the lowest optimization level here will prevent the compiler from running non Other Pass, The Pass name in the command line can be passed to the compiler. If you are using the Clang / LLVM architecture, For {tree-vectorize, unroll-loops}, you can use the command clang -O0 -ftree-vectorize -funroll-loops. This command will still run vector optimization and loop unrolling optimization under O0 level optimization. O0 does not run these two optimizations by default.
[0093] The present invention provides a compiler performance optimization method, which compiles the source code program into the initial intermediate code based on the lowest optimization level. ; Sequentially process the initial intermediate code Execute each optimization to obtain the intermediate code of the execution result; calculate the intermediate representation complexity of the intermediate code; screen out optimizations with positive optimization effects based on the intermediate representation complexity of each intermediate code; compile the source program from the source code into the target program based on the positive optimization. The present invention proposes a method for judging the effectiveness of optimization based on the intermediate representation complexity at the intermediate code level, so as to avoid invalid or negative optimization during the compilation process; the method does not rely on the specific hardware architecture, has good versatility, and is applicable to a variety of target platforms; by dynamically evaluating the Pass effect at the intermediate language level, the present invention not only greatly shortens the compilation time, but also makes the target program performance better.
[0094] It should be noted that, in the above embodiments, the description of each embodiment has its own focus. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant description of other embodiments.
[0095] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present invention.
[0096] Obviously, those skilled in the art may make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if such changes and modifications fall within the scope of the claims and their equivalents, the present invention is intended to include such changes and modifications.
Claims
1. A compiler performance optimization method, characterized in that: include: Step S1, compile the source code program into initial intermediate code based on the lowest optimization level ; Step S2: Get the optimization names of all optimization passes of the compiler used and put them into the complete optimization list set Among them, , For the Optimized name, To optimize the number of passes; Step S3, using the intermediate code optimization tool provided by the compiler, the initial intermediate code is optimized in sequence Execute the complete optimization list set For each optimization Pass in the process, we get the intermediate code set of the execution result. , intermediate code Intermediate code Run Optimization Pass The intermediate code result obtained after Step S4, calculate the intermediate code set Each intermediate code in The complexity of the intermediate representation ; Step S5, according to each intermediate code The complexity of the intermediate representation , construct the optimization complexity change vector ,in, Represents each intermediate code The complexity of the change; Step S6: According to the optimization complexity change vector Each intermediate code The complexity changes of the forward optimization pass are screened out, and a customized optimization pass list set is generated. , the customized optimization Pass list collection The optimization name of the forward optimization pass is saved in Step S7, based on the minimum optimization level and the customized optimization Pass list set The name of the forward optimization pass in , which compiles the source code into the target program.
2. The compiler performance optimization method according to claim 1, wherein: In step S3, the initial intermediate code is optimized in sequence using the intermediate code optimization tool provided by the compiler. Execute the complete optimization list set For each optimization Pass in the process, we get the intermediate code set of the execution result. ,include: Initial intermediate code Execute Optimization Pass , get the intermediate code of the execution result ; Intermediate code Execute Optimization Pass , get the intermediate code of the execution result ; And so on, for the intermediate code Execute Optimization Pass , get the intermediate code of the execution result ; Intermediate code of execution result A collection of intermediate codes that make up the execution results .
3. The compiler performance optimization method according to claim 1, wherein: In step S4, the intermediate code set is calculated. Each intermediate code in The complexity of the intermediate representation ,include: For each intermediate code Assigning a first weight to each intermediate code instruction, wherein the first weight is assigned to each intermediate code instruction based on an instruction cost model; Assign the second weight to each control flow graph structure type and analyze each intermediate code The types of control flow graph structures included and the number of each type of control flow graph structure; According to each intermediate code The first weight of each intermediate code instruction and the second weight of each control flow graph structure type are calculated for each intermediate code. The complexity of the intermediate representation .
4. The compiler performance optimization method according to claim 3, wherein: According to each intermediate code The first weight of each intermediate code instruction and the second weight of each control flow graph structure type are calculated for each intermediate code. The complexity of the intermediate representation ,include: Traversing the intermediate code Each intermediate code instruction , calculate the first weight sum of all intermediate code instructions ,in Intermediate code The number of intermediate code instructions, Indicates the The first weight of the intermediate code instruction; Calculate intermediate code The control flow weight and ,in, Indicates type The second weight of the control flow graph, Indicates intermediate code The type is The number of control flow graphs, means The number of types of control flow graphs in ; Calculate intermediate code The complexity of the intermediate representation , ,in Intermediate code The first weight sum of all intermediate code instructions, Intermediate code The second weight of the control flow graph and, is the second weight constant.
5. The compiler performance optimization method according to claim 1, wherein: In step S5, according to each intermediate code The complexity of the intermediate representation is constructed to optimize the complexity change vector ,include: For each intermediate code , calculate the intermediate code The complexity changes , that is, calculating the intermediate code Complete optimization The intermediate code generated later The complexity changes of Indicates that among all the calculated Select the largest subscript as , making ; Constructing an optimization complexity change vector , Indicates the total number of optimization passes.
6. The compiler performance optimization method according to claim 1, characterized in that: In step S6, according to the optimization complexity change vector Each intermediate code The complexity changes of the forward optimization pass are screened out, and a customized optimization pass list set is generated. ,include: The optimization complexity change vector Every dimension ,Will Compare with 0; if , the corresponding optimization Pass Invalid optimization, no processing is performed; if , the corresponding optimization Pass This is negative optimization and no processing is performed; if , the corresponding optimization Pass For forward optimization, the corresponding optimization Pass Add to the custom optimization Pass list collection middle.
Citation Information
Patent Citations
Domain-specific language compiler optimization method and system based on multi-layer intermediate representation
CN119512557A
Compilation method, system and equipment based on confusion mechanism and medium
CN120046145A