A Compiler Performance Optimization Method

By recording the hash values ​​before and after function optimization in the compiler and establishing map tables, the redundant optimization problem is solved, faster compilation time is achieved, and the performance of the compiler is improved.

CN118760436BActive Publication Date: 2024-12-06WUHAN LINGJIU MICROELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411244153.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-06
Publication Date
2024-12-06
Estimated Expiration
2044-09-06

AI Technical Summary

Technical Problem

Existing compilers will perform a lot of redundant optimizations when the program is modified, resulting in an increase in compilation time and affecting the user experience.

Method used

Record the hash values ​​before and after each function optimization at the first compilation, and establish a function optimization map table. During the second compilation, determine whether optimization needs to be performed based on the hash value comparison, skip invalid optimization, and reduce redundant optimization.

Benefits of technology

By establishing a function to optimize map table, we guide the skipping invalid optimization during the second compilation, significantly shortening the compilation time and improving compiler performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118760436B_ABST
    Figure CN118760436B_ABST
Patent Text Reader

Abstract

The present invention provides a compiler performance optimization method, comprising: recording the hash value of each function IR before and after each optimization executed by each function in the program code during the first compilation, and recording the optimization effectiveness; recording the hash value of the function IR before and after the optimization and the optimization effectiveness in a function optimization map table; calculating the hash value of the current function before the current optimization during the second compilation; searching in the function optimization map table, comparing the hash value of the function IR before the current optimization executed by the current function found with the hash value of the current function before the current optimization executed by the current function calculated during the second compilation; determining whether the current optimization needs to be executed according to the comparison result and the optimization effectiveness value of the current optimization for the current function found in the function optimization map table. The method of the present invention guides the second compilation by establishing the function optimization effectiveness table of the first compilation, reduces redundant optimization as much as possible, and shortens the time required for the second compilation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of program compilation optimization, and more specifically, to a compiler performance optimization method. Background Art

[0002] 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).

[0003] The main purpose of a compiler is to translate high-level computer languages ​​that are easy for people 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, such as Pascal, C, C++, C#, Java, etc., while the target language is assembly language or the target machine's object code, sometimes also called machine code.

[0004] The main workflow of a modern compiler can be found in Figure 1 :

[0005] Optimization pass introduction:

[0006] A pass is a step in the compiler used to optimize or analyze a program. A program usually goes through multiple passes, and each pass may be executed multiple times.

[0007] The following introduces the optimization pass using the clang / llvm architecture as an example.

[0008] There are several steps involved in converting LLVM IR to target assembly code. The IR is transformed into a backend-friendly representation of instructions, functions, and global variables. This representation changes as the program passes through the various backend stages, getting closer and closer to the actual target machine instructions. Figure 2 Given the required steps to get from LLVM IR to target code or assembly, there are optional optimization passes that can be performed to further improve the quality of the translation.

[0009] The translation pipeline consists of multiple stages of the backend, which are also called super passes internally because they are implemented by several small passes. Compared with other steps, the former passes are critical to the success of the backend, while the latter are more important for improving the efficiency of the generated code.

[0010] Figure 2 is the execution flow chart of the LLVM backend, Figure 2The optimization passes are shown in Figure 1. One pass may perform multiple optimization passes in succession, and the others are processing processes.

[0011] The intermediate representation that can be used as input and output for optimization is collectively referred to as IR, such as IR, MI, MC in clang / llvm.

[0012] All processing except optimization passes is required at compile time. Optimization passes are generally optional, but if they are done the final code will be better. Not every optimization has a positive optimization effect on a specific code.

[0013] During the programming process, programmers will frequently modify the code and recompile it. Although using compilation systems such as CMake, incremental compilation can be achieved to minimize the number of files that need to be compiled. However, this incremental method determines whether recompilation is required based on whether the file has been modified. This method is at the file level. In most cases, programmers only modify a few functions in a file, but this will cause the entire file and its dependencies to be recompiled. Even if a space is added to the file, a large number of files will be recompiled.

[0014] When compiling a program, compilation optimization takes up most of the time. A modern compiler generally has more than 100 optimization passes, but these passes are not always effective, that is, even if they are run, they will not change the code. The running of these invalid optimization passes wastes a lot of compilation time and affects the use experience of the compiler. Summary of the invention

[0015] The present invention aims at the technical problems existing in the prior art and provides a compiler performance optimization method, comprising:

[0016] During the first compilation, record the hash value of the function IR before and after each optimization of each function in the program code. IR refers to the intermediate representation in the compilation process.

[0017] According to whether the hash value of the function IR changes before and after each optimization is executed for each function, set the optimization effectiveness of each optimization for the function;

[0018] Record the triplet <hash value of the function IR before optimization, hash value of the function IR after optimization, and optimization effectiveness> in the function optimization map table;

[0019] During the second compilation, the hash value of the current function before the current optimization is calculated;

[0020] According to the current function and the current optimization, the function optimization map table is searched, and the hash value of the function IR of the current function before the current optimization is executed is compared with the hash value of the current function before the current optimization is executed calculated during the second compilation;

[0021] According to the comparison result and the value of the optimization effectiveness of the current optimization for the current function found in the function optimization map table, it is determined whether the current optimization needs to be performed.

[0022] On the basis of the above technical solution, the present invention can also be improved as follows.

[0023] Optionally, setting the optimization effectiveness of each optimization for the function according to whether the hash value of the function IR changes before and after each optimization is executed by each function includes:

[0024] If the hash value of the function IR changes before and after any function executes any optimization, then any optimization is valid for the optimization of any function, and the optimization validity is set to 1; otherwise, the optimization is invalid, and the optimization validity is set to 0.

[0025] Optionally, the function optimization map table is stored as a hidden file in the source code directory for use during the next compilation.

[0026] Optionally, it also includes optimizing and storing the function optimization map table according to whether the two optimizations are adjacent.

[0027] If two optimizations are adjacent, for the same function, the hash value of the function IR after the previous optimization is used as the hash value of the function IR before the current optimization, and the hash value of the function IR before the current optimization is represented by LAST, indicating that it is the same as the hash value of the function IR after the previous optimization;

[0028] If the two optimizations are not adjacent, you need to calculate the hash values ​​of the function IR before and after the two optimizations respectively;

[0029] When the hash value of the same function IR before and after optimization remains unchanged, the hash value of the optimized function IR is represented by SAME, indicating that the hash values ​​of the function IR before and after optimization are the same.

[0030] Optionally, determining whether the current optimization needs to be performed according to the comparison result and the value of the optimization effectiveness of the current optimization for the current function found in the function optimization map table includes:

[0031] If the hash value of the function IR of the current function before the current optimization is different from the hash value of the current function before the current optimization calculated during the second compilation, or if the hash value of the function IR of the current function before the current optimization is the same as the hash value of the current function before the current optimization calculated during the second compilation, and the optimization validity is 1, then the current optimization is valid and needs to be executed; otherwise, the current optimization is invalid and does not need to be executed.

[0032] Optionally, the step of determining whether to perform the current optimization is necessary based on the comparison result and the optimization effectiveness value of the current optimization for the current function found in the function optimization map table, and then further comprising:

[0033] When the current optimization needs to be executed, after the current optimization is completed, the hash value of the current function IR after the optimization is completed is calculated, and the optimization validity is set according to whether the hash value of the function IR before and after the current optimization changes;

[0034] Update the function optimization map table based on the hash value of the function IR before and after the current optimization and the optimization effectiveness.

[0035] The present invention provides a compiler performance optimization method. During the first compilation, the hash value of each function IR before and after each optimization is executed by each function in the program code, and the optimization validity is recorded; the hash value of the function IR before and after the optimization and the optimization validity are recorded in the function optimization map table; during the second compilation, the hash value of the current function before the current optimization is calculated; the hash value of the function IR before the current optimization is found in the function optimization map table is compared with the hash value of the current function before the current optimization is calculated during the second compilation; according to the comparison result and the value of the optimization validity of the current optimization for the current function found in the function optimization map table, it is determined whether the current optimization needs to be executed. The method of the present invention guides the second compilation by establishing the function optimization validity table of the first compilation, reduces redundant optimization as much as possible, and shortens the time required for the second compilation. BRIEF DESCRIPTION OF THE DRAWINGS

[0036] Figure 1 This is a diagram of the main workflow of a modern compiler;

[0037] Figure 2 Flowchart for converting LLVM IR to target assembly code;

[0038] Figure 3 A flow chart of a compiler performance optimization method provided by the present invention;

[0039] Figure 4 A schematic diagram of the overall process of a compiler performance optimization method provided by the present invention;

[0040] Figure 5 Schematic diagram of optimized storage for function optimization map table. DETAILED DESCRIPTION

[0041] 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 in the present invention, all other embodiments obtained by ordinary technicians in this field without 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 subject to the constraints of the sequence of steps and / or the structural composition mode, but must be based on the ability of ordinary technicians in this field to achieve. When the combination of technical solutions is contradictory or cannot be achieved, it should be considered that such a combination of technical solutions does not exist and is not within the scope of protection required by the present invention.

[0042] After the first compilation, if the program compilation file is deleted or the code is modified, the second compilation will still do a lot of redundant optimizations like the first time. Even if these optimizations are done, they will not affect the final generated code. These redundant optimizations will affect the speed of code compilation.

[0043] Based on this, the present invention provides a compiler performance optimization method, which guides the second compilation by establishing a function optimization validity table of the previous compilation, skips meaningless redundant optimization as much as possible, and shortens the second compilation time. The method is mainly divided into two parts. The first part: during the first compilation, a function optimization validity map table is established; the second part: during the second compilation, the established map table is used to guide the compilation, reduce redundant optimization, and shorten the compilation time.

[0044] The IR mentioned in the present invention refers to various intermediate representations, not just one, such as IR, MI, MC, etc. in the Clang / LLVM architecture. It is various types of intermediate results in the compilation process, and any representation that can be used as optimized input and output can be regarded as IR.

[0045] Figure 3 A flow chart of a compiler performance optimization method of the present invention is provided, such as Figure 3 and Figure 4 As shown, the method includes:

[0046] Step S1, during the first compilation, the hash value of each function IR before and after each optimization is executed for each function in the program code is recorded, where IR refers to the intermediate representation in the compilation process.

[0047] Step S2, setting the optimization effectiveness of each optimization for the function according to whether the hash value of the function IR changes before and after each optimization of each function.

[0048] Among them, if the hash value of the function IR changes before and after any function executes any optimization, then any optimization is effective for the optimization of any function, and the optimization effectiveness is set to 1; otherwise, the optimization is invalid, and the optimization effectiveness is set to 0.

[0049] Step S3, record the triple <hash value of the function IR before optimization, hash value of the function IR after optimization, optimization effectiveness> in the function optimization map table.

[0050] Wherein, step S3 specifically includes:

[0051] Step S301: Establish map(funcName,passName)= <hash(pfuncName_passName_IR), hash(funcName_passName_IR),isEffective> Table. Here hash(pfuncName_passName_IR) Indicates the hash of a function IR before optimizing passName. The p before the name stands for previous. hash(funcName_ passName_IR) Represents the hash of a function IR after optimizing passName.

[0052] Step S302, setting the effectiveness of the optimization. The setting is performed based on whether the hash value of the function IR before and after the optimization changes. If the hash value of the function IR before and after the optimization changes, the optimization effectiveness is set to 1, otherwise, the optimization effectiveness is set to 0.

[0053] Step S303: Optimize and store the function optimization map table according to whether the two optimizations are adjacent. The specific optimization storage method is as follows:

[0054] See also Figure 5 , in this figure, the optimization process is assumed to be:

[0055] That is, P1 is adjacent to P2, P2 is not adjacent to P3, P3 is adjacent to P4, and P1, P2, P3, and P4 are all optimization passes.

[0056] For non-adjacent optimizations, the hash values ​​before and after the optimization must be calculated hash(pfuncName_passName_IR) and hash(funcName_passName_IR) . Such as (F1, P1), (F1, P3) etc. in Table 1.

[0057] For two adjacent optimizations, that is, there is no other processing in between, the hash value of the IR after the previous optimization can be used as the hash value before the current optimization, that is, (F1, P2), (F1, P3), etc. in Table 1. This can save one calculation and save table space. The hash value before the current optimization only needs to store the LAST flag, indicating that it is the same as the hash value after the previous optimization. There is no need to store the hash before the current optimization again, such as (F1, P2) <LAST, hash(F1_P2_IR), 1> .

[0058] Table 1 Function optimization map table

[0059]

[0060] In the above map table <hash(pF1_P1_IR), hash(F1_P1_IR), 1> of hash(pF1_P1_IR) , which indicates the hash value of the IR of the F1 function before P1 optimization. hash(F1_P1_IR) It indicates the hash value of the IR after the F1 function completes the P1 optimization. 1 indicates that the P1 optimization done by the F1 function is effective. LAST in Table 1 indicates that the hash value after the previous optimization is used for two adjacent optimizations.

[0061] When the hash values ​​before and after optimization are the same, the optimized hash value can be replaced by SAME. When isEffective is 0, that is, the optimization is invalid, that is, the hash value has not changed. hash(F1_P1_IR) Will be marked as SAME, that is, hash(pF1_ P1_IR) Same as (F1, P4).

[0062] For function-optimized tables map(funcName, passName)= <hash(pfuncName_passName_IR), hash(funcName_passName_IR),isEffective> To simplify the description, the triples stored in the table are

[0063] The first element hash(pfuncName_passName_IR) writing map(funcName, passName)[1] ,

[0064] The second element hash(funcName_passName_IR) writing map(funcName, passName)[2] ,

[0065] The third element isEffective writing map(funcName, passName)[3].

[0066] The function optimization map table can be placed as a hidden file in the source code directory for use during the next compilation.

[0067] Step S4, during the second compilation, the hash value of the current function before the current optimization is performed is calculated.

[0068] Step S5, according to the current function and the current optimization, search in the function optimization map table, and compare the hash value of the function IR of the current function before the current optimization is executed with the hash value of the current function before the current optimization is executed calculated during the second compilation.

[0069] Step S6, determining whether the current optimization needs to be performed based on the comparison result and the value of the optimization effectiveness of the current optimization for the current function found in the function optimization map table.

[0070] Among them, the hash value of the currentFunc function before optimization is calculated (hash(pcurrentFunc_currentPass_IR). In the function optimization map table established above, map(currentFunc, currentPass) is searched.

[0071] Determine whether the function has been modified based on the hash value of the function IR before optimization recorded in the lookup table and the calculated hash (pcurrentFuc_currPass_IR). Specifically, look up the value of map (currentFunc, currentPass) in the function optimization map table.<hash(pcurrentFunc_currentPass_IR),hash(currentFunc_currentPass_IR), isEffective> If the calculated hash(pcurrentFunc_currentPass_IR) is equal to map(currentFunc, currentPass)[1] in the table, it means that the function has not been modified; if they are not equal, it means that the function has been modified.

[0072] If the function is modified or the optimization valid bit in the corresponding map table entry is 1, the currentPass optimization is performed. Otherwise, the optimization is considered invalid and skipped directly.

[0073] There are two situations where optimization needs to be performed:

[0074] ① If the currentFunc function has not been modified, and map(currentFunc,currentPass)[3] in the map table, that is, isEffective is 1, it means that the optimization is effective for the function and needs to be performed.

[0075] ② If the currentFunc function is modified, the optimization passName needs to be executed. Because the function has changed, according to the existing map table, it is impossible to predict whether currentPass is valid for the modified function.

[0076] After step S6, step S7 is also included: when optimization needs to be performed, after executing the currentPass, the hash value of the IR of the function after execution is calculated, and the function optimization map table is updated.

[0077] Specifically, S7 includes the following sub-steps:

[0078] Step S701, after the optimization is executed, the hash value hash (currentFunc_currentPass_IR) of the IR of the currentFunc function after optimization is calculated.

[0079] Step S702, according to whether the hash value of IR before and after optimization changes, that is, whether the calculated hash(funcName_passName_IR) is equal to hash(pfuncName_passName_IR), isEffective is set to 1 when they are not equal, and isEffective is 0 when they are equal.

[0080] Step S703: Update the function optimization map table map (currentFunc, currentPass) to<hash(pfuncName_passName_IR),hash(funcName_passName_IR), isEffective> , for subsequent compilation.

[0081] The compiler performance optimization method provided by the present invention has the following advantages:

[0082] (1) Use a simple and fast method to record the information from the last compilation to guide this compilation, reduce redundant optimization, and speed up compilation.

[0083] (2) You can store each optimized IR to determine whether the function has changed, and to guide the compiler to skip the optimization of a certain function. However, this will require a lot of storage space, and determining whether the function has changed requires a lot of calculations, which will run very slowly.

[0084] (3) The established function optimization map table is fast, simple and efficient. It can be directly retrieved during the next compilation, and the function optimization map table is optimized for storage.

[0085] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0086] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0087] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded computer, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0088] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0089] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0090] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other 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.

[0091] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.

Claims

1. A compiler performance optimization method, characterized in that: include: During the first compilation, the hash value of the IR of each function in the program code before and after each optimization is recorded. IR refers to the intermediate representation in the compilation process. According to whether the hash value of the function IR changes before and after each optimization is executed for each function, set the optimization effectiveness of each optimization for the function; Record the triplet <hash value of the function IR before optimization, hash value of the function IR after optimization, and optimization effectiveness> in the function optimization map table; During the second compilation, the hash value of the current function before the current optimization is calculated; According to the current function and the current optimization, the function optimization map table is searched, and the hash value of the function IR of the current function before the current optimization is executed is compared with the hash value of the current function before the current optimization is executed calculated during the second compilation; Determine whether the current optimization needs to be performed based on the comparison result and the optimization effectiveness value of the current optimization for the current function found in the function optimization map table; The optimization effectiveness of each optimization for the function is set according to whether the hash value of the function IR changes before and after each optimization is executed by each function, including: If the hash value of the function IR changes before and after any function executes any optimization, then the optimization is effective for the function, and the optimization effectiveness is set to 1; otherwise, the optimization is invalid, and the optimization effectiveness is set to 0; The step of determining whether to perform the current optimization according to the comparison result and the optimization effectiveness value of the current optimization for the current function found in the function optimization map table includes: If the hash value of the function IR of the current function before the current optimization is different from the hash value of the current function before the current optimization calculated during the second compilation, or if the hash value of the function IR of the current function before the current optimization is the same as the hash value of the current function before the current optimization calculated during the second compilation, and the optimization validity is 1, then the current optimization is valid and needs to be executed; otherwise, the current optimization is invalid and does not need to be executed.

2. The compiler performance optimization method according to claim 1, characterized in that: The function optimization map table is stored as a hidden file in the source code directory for use during the next compilation.

3. The compiler performance optimization method according to claim 1, characterized in that: It also includes optimizing and storing the function optimization map table according to whether two optimizations are adjacent, where: If two optimizations are adjacent, for the same function, the hash value of the function IR after the previous optimization is used as the hash value of the function IR before the current optimization, and the hash value of the function IR before the current optimization is represented by LAST, indicating that it is the same as the hash value of the function IR after the previous optimization; If the two optimizations are not adjacent, you need to calculate the hash values ​​of the function IR before and after the two optimizations respectively; When the hash value of the same function IR before and after optimization remains unchanged, the hash value of the optimized function IR is represented by SAME, indicating that the hash values ​​of the function IR before and after optimization are the same.

4. The compiler performance optimization method according to claim 1, characterized in that: The step of determining whether to perform the current optimization is performed based on the comparison result and the optimization effectiveness value of the current optimization for the current function found in the function optimization map table, and then further comprising: When the current optimization needs to be executed, after the current optimization is completed, the hash value of the current function IR after the optimization is completed is calculated, and the optimization validity is set according to whether the hash value of the function IR before and after the current optimization changes; Update the function optimization map table based on the hash value of the function IR before and after the current optimization and the optimization effectiveness.