Compilation optimization method and device, equipment and storage medium
By generating compiled programs containing target loop invariants in the compiler, the problem of narrow application scope of loop structure optimization in the existing technology is solved, and broader loop structure optimization and higher program execution efficiency are achieved.
Patent Information
- Application Number
- CN202411666990.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-20
- Publication Date
- 2025-03-14
AI Technical Summary
When existing compilers optimize the loop structure, their application scope is narrow and they cannot effectively extract the loop invariant, resulting in limited optimization effect.
By obtaining the loop structure in the program to be compiled, performing compilation processing, a compiled program containing the second loop structure and the target loop invariant is generated. The target loop invariant includes code to indicate exceptions and code that is not built-in functions to ensure that the optimized semantics are consistent with the original program.
While ensuring the consistency of semantics before and after compilation optimization, the scope of optimization of loop structures is widened, the ability to optimize loop structures is enhanced, and program execution efficiency is improved.
Smart Images

Figure CN119621066A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to computer technology, and relate to but are not limited to a compilation optimization method and apparatus, device, and storage medium. Background Art
[0002] Compiler optimization is a series of technical processes that the compiler automatically performs when converting source code into executable code in order to improve program running efficiency. These optimizations can be performed at different levels, including instruction-level optimization, loop optimization, data path optimization, memory access optimization, etc.
[0003] The basic compilation unit of a program source code file is a function. Usually, the structure that consumes the most computing time in a function is the loop structure. Therefore, optimizing the loop structure is the focus of compiler optimization. Summary of the invention
[0004] In view of this, the compilation optimization method, device, equipment, and storage medium provided in the embodiments of the present application can broaden the optimization application scope of the loop structure and enhance the optimization capability of the loop structure. The compilation optimization method, device, equipment, and storage medium provided in the embodiments of the present application are implemented as follows:
[0005] In a first aspect, an embodiment of the present application provides a compilation optimization method, comprising:
[0006] Acquire a first loop structure in the program to be compiled, where the first loop structure is a loop logic described by a code language;
[0007] The first loop structure is compiled to obtain a compiled program, wherein the compiled program includes a second loop structure and a target loop invariant, wherein the second loop structure has the same semantics as the first loop structure in combination with the semantics of the target loop invariant, wherein the target loop invariant is located outside the second loop structure, wherein the target loop invariant includes the first loop invariant and / or the second loop invariant, wherein the first loop invariant is a code for indicating that an abnormal situation occurs when the program is running, and wherein the second loop invariant is a code of a non-built-in function.
[0008] In a second aspect, an embodiment of the present application provides a compilation optimization device, including:
[0009] An acquisition module, used for acquiring a first loop structure in a program to be compiled, wherein the first loop structure is a loop logic described by a code language;
[0010] A compiling module is used to compile the first loop structure to obtain a compiled program, wherein the compiled program includes a second loop structure and a target loop invariant, wherein the second loop structure has the same semantics as the first loop structure in combination with the semantics of the target loop invariant, wherein the target loop invariant is located outside the second loop structure, wherein the target loop invariant includes the first loop invariant and / or the second loop invariant, wherein the first loop invariant is a code for indicating that an abnormal situation occurs when the program is running, and the second loop invariant is a code of a non-built-in function.
[0011] In a third aspect, an embodiment of the present application provides a computer device, including a memory and a processor, wherein the memory stores a computer program that can be executed on the processor, and when the processor executes the program, the method described in the embodiment of the present application is implemented.
[0012] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method provided in the embodiment of the present application.
[0013] The compilation optimization method, apparatus, computer device, and computer-readable storage medium provided in the embodiments of the present application obtain a first loop structure in a program to be compiled and compile the first loop structure so that the compiled program may include a second loop structure and a target loop invariant, wherein the target loop invariant includes a first loop invariant of code for indicating that an abnormal situation occurs when the program is running, and / or a second loop invariant of code for a non-built-in function whose internal behavior cannot be determined, and the semantics of the second loop structure combined with the target loop invariant are the same as those of the first loop structure.
[0014] In this way, while ensuring that the semantics before and after compilation optimization remain consistent, the optimization application scope of the loop structure can be expanded, the optimization capability for the loop structure can be enhanced, and the technical problems raised in the background technology can be solved. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present application and are used together with the specification to illustrate the technical solution of the present application.
[0016] Figure 1 An application scenario diagram of the compilation optimization method provided in an embodiment of the present application;
[0017] Figure 2 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application;
[0018] Figure 3AA schematic diagram of a code provided in an embodiment of the present application;
[0019] Figure 3B A schematic diagram of another code provided in an embodiment of the present application;
[0020] Figure 3C A schematic diagram of comparing codes before and after optimization provided in an embodiment of the present application;
[0021] Figure 4 A schematic diagram of an implementation flow of a compilation optimization method provided in an embodiment of the present application;
[0022] Figure 5 A schematic diagram of a loop structure in a control flow graph provided in an embodiment of the present application;
[0023] Figure 6 A schematic diagram of code comparison before and after the loop invariant external advance provided in an embodiment of the present application;
[0024] Figure 7 A schematic diagram of the structure after the first loop invariant is externalized provided in an embodiment of the present application;
[0025] Figure 8 A schematic diagram of the structure before and after the compilation optimization of the loop structure provided in the embodiment of the present application;
[0026] Fig. 9 Schematic diagrams before and after optimization of other types of loop invariants provided in embodiments of the present application;
[0027] Fig.10 A schematic diagram of the implementation flow of another compilation optimization method provided in an embodiment of the present application;
[0028] Fig.11 A schematic diagram of the structure of a compilation optimization device provided in an embodiment of the present application;
[0029] Fig.12 A schematic diagram of the structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0030] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the specific technical solution of the present application will be further described in detail below in conjunction with the drawings in the embodiments of the present application. The following embodiments are used to illustrate the present application, but are not used to limit the scope of the present application.
[0031] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein are only for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0032] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0033] It should be pointed out that the terms "first\second\third" involved in the embodiments of the present application are used to distinguish similar or different objects, and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged in a specific order or sequence where permitted, so that the embodiments of the present application described here can be implemented in an order other than that illustrated or described here.
[0034] Before introducing the relevant technical solutions of the present application, the terms involved in the embodiments of the present application are first explained.
[0035] Intermediate Code (IR): Before data such as C specification code written in C, C++, or System C is converted into machine code, it is converted into intermediate code by the compiler.
[0036] Basic Block (BB): refers to a sequence of statements that are executed in order by a program. There is only one entry and one exit. The entry is the first statement in the program, and the exit is the last statement in the program. For a basic block, execution only enters from its entry and exits from its exit.
[0037] Loop Invariant Code Hoisting (LICM): is a compiler optimization technique that involves moving code that does not change during loop iterations (i.e., loop invariants) from the loop body to the outside of the loop. This optimization can reduce unnecessary repeated calculations, thereby improving program execution efficiency.
[0038] Control flow graph: A control flow graph is also called a control flow chart. It is an abstract representation of a process or program. It is an abstract data structure used in compilers and maintained internally by the compiler. It represents all the paths that a program will traverse during execution. It uses a graph to represent the possible flow of execution of all basic blocks in a process, and can also reflect the real-time execution of a process.
[0039] Loop structure: A loop structure is a program structure that is set up to repeatedly execute a function in a program. It determines whether to continue executing a function or exit the loop based on the condition in the loop body.
[0040] Loop invariant protection structure: A loop invariant protection structure is a program structure that is added to ensure that the original semantics and behavior of the program are not changed after the loop invariant statements or expressions are moved outside the loop.
[0041] preheader basic block: The basic block before entering the loop, which has only one successor block, which is the header.
[0042] Header basic block: loop header, the basic block entering the loop, which dominates all basic blocks in the loop.
[0043] body basic block: loop body, the basic block executed in the loop.
[0044] exit basic block: loop exit block, the first basic block after leaving the loop.
[0045] func exit basic block: function leaves the block, leaves the basic block of the function.
[0046] Critical edge: An edge in the control flow graph has a predecessor basic block with multiple successor blocks and the successor block has multiple predecessor blocks. Such an edge is called a critical edge.
[0047] Intrinsic: Built-in functions are special built-in functions implemented by the compiler. The compiler can determine the internal behavior of the function.
[0048] An embodiment of the present application provides a compilation optimization method, which is applied to an electronic device. Figure 1 FIG. 1 is an application scenario diagram of a compilation optimization method provided in an embodiment. Figure 1 As shown, the electronic device can be at least one of a mobile phone, a smart phone, a portable terminal, a terminal, a personal digital assistant (PDA), a portable multimedia player (Personal Media Player, PMP) device, a laptop computer, a notebook (Note Pad), a wireless broadband (Wireless Broadband, Wibro) terminal, a tablet computer (personal computer, PC), a smart PC, a sales terminal (Point of Sales, POS) and a vehicle-mounted computer.
[0049] The functions implemented by the method can be implemented by calling program codes by a processor in the electronic device. Of course, the program codes can be stored in a computer storage medium. It can be seen that the electronic device at least includes a processor and a storage medium.
[0050] Figure 2A schematic diagram of the structure of an electronic device provided in an embodiment of the present application.
[0051] For example, Figure 2 As shown, the electronic device 20 may include a processor 201 , an external memory interface 202 , an internal memory 203 and a universal serial bus (USB) interface 204 .
[0052] It is understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 20. In other embodiments of the present application, the electronic device 20 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0053] The processor 201 may include one or more processing units, for example: the processor 201 may include an application processor (application processor, AP), a modem processor, a graphics processor (graphics processing unit, GPU), an image signal processor (image signal processor, ISP), a controller, a video codec, a digital signal processor (digital signal processor, DSP), a baseband processor, and / or a neural network processor (neural network processing unit, NPU), etc. Among them, different processing units can be independent devices or integrated in one or more processors. Exemplarily, the processor 201 can be a smart terminal CPU, such as a Snapdragon series processor, etc. In some embodiments, the processor 201 may include one or more interfaces. The interface may include an inter integrated circuit (I2C) interface, an inter integrated circuit sound (I2S) interface, a pulse code modulation (PCM) interface, a universal asynchronous receiver / transmitter (UART) interface, a mobile industry processor interface (MIPI), a general-purpose input / output (GPIO) interface, a subscriber identity module (SIM) interface, and / or a universal serial bus (USB) interface, etc.
[0054] The external memory interface 202 can be used to connect an external memory card, such as a Micro SD card, to expand the storage capacity of the electronic device 20. The external memory card communicates with the processor 201 through the external memory interface 202 to implement a data storage function. For example, files such as music and videos are stored in the external memory card.
[0055] The internal memory 203 can be used to store computer executable program codes, which include instructions. The internal memory 203 can include a program storage area and a data storage area. The program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc. The data storage area can store data created during the use of the electronic device 20 (such as audio data, a phone book, etc.), etc.
[0056] In addition, the electronic device involved in the embodiments of the present application may also be installed with an operating system, and application programs may be installed and run on the operating system, which is not limited in the embodiments of the present application.
[0057] The basic compilation unit of a program source code file is a function. Usually, the structure that consumes the most computing time in a function is the loop structure. Therefore, optimization of the loop structure is the focus of compiler optimization.
[0058] Among them, the optimization scheme of loop structure is mostly implemented by loop invariant lifting (LICM). However, in the loop invariant lifting optimization scheme in the related art, in order to meet the restriction that the original semantics of the program will not be changed after optimization, there are many restrictions when analyzing whether the loop invariant meets the lifting condition, resulting in a narrow scope of optimization application.
[0059] For example, in some embodiments, the input definition of a statement or expression (loop invariant) within the loop cannot be extracted, otherwise the input of the statement or expression will be undefined.
[0060] For example, Figure 3A Give a schematic diagram of a code. Figure 3A As shown in the figure, the input y of the y*y expression is defined inside the loop, and y*y cannot be brought out of the loop.
[0061] Alternatively, in some other embodiments, if a statement or expression (loop invariant) has a read-write dependency, then the pull-out cannot be implemented. Figure 3B A schematic diagram of a code is given. Figure 3B As shown, the value v is initially defined outside the loop. Statement A in the loop reads the value v, and then statement B modifies the value v through the address v_addr of v. At this time, the expression v*v cannot be extracted, otherwise the semantics will be changed.
[0062] Alternatively, in other embodiments, memory garbage collection (GC) type languages will save reference variable information that survives the current statement position during the compilation process. If these dependency information are defined in a loop, then these statements or expressions cannot be externalized, otherwise it will cause an undefined reference variable error.
[0063] Alternatively, in other embodiments, a statement or expression (loop invariant) may throw an exception, and such a statement or expression cannot be thrown out, otherwise it will cause an exception that would not be thrown if the loop was not entered to be erroneously thrown outside the loop, resulting in semantic inconsistency between the loop structure before and after optimization.
[0064] For example, Figure 3C As shown in the figure, a comparison diagram of the code before and after optimization is given. When m is null, a null pointer exception will be thrown when entering the loop, but after the code is externalized for loop invariants, a null pointer exception will be thrown even if the loop is not entered.
[0065] Alternatively, in some other embodiments, for statements of non-intrinsic function call type in a loop, since the compiler does not know the internal behavior of the function, they cannot be taken out of the loop, otherwise the original semantics may be destroyed. For example, a global value is incremented by 1 in the called function a[i]=getAndAddVal(), and taking the function call statement out of the loop will cause inconsistent semantics before and after optimization.
[0066] Due to the above limitations, the compiler has a narrow application scope when performing loop invariant extraction optimization on loop structures in functions.
[0067] Based on this, an embodiment of the present application provides a compilation optimization method, which can expand the optimization application scope of the loop structure and enhance the optimization capability for the loop structure while ensuring that the semantics before and after the compilation optimization remain consistent.
[0068] Figure 4 The following is a schematic diagram of the implementation process of the compilation optimization method provided in the embodiment of the present application. Figure 4 As shown, the method may include the following steps 401 to 402:
[0069] Step 401 : obtaining a first loop structure in the program to be compiled, where the first loop structure is a loop logic described by a code language.
[0070] In the embodiment of the present application, there is no limitation on the method of obtaining the first loop structure in the program to be compiled.
[0071] For example, in some embodiments, the intermediate code corresponding to the program to be compiled may be first obtained through a programming language. The intermediate code is an abstract representation method for the program to be compiled, and its semantics are completely consistent with the program to be compiled.
[0072] Then, the compiler can obtain multiple basic blocks corresponding to the intermediate code by analyzing the intermediate code, and the multiple basic blocks include the first loop structure, and the first loop structure includes the loop invariant.
[0073] In some embodiments, when analyzing the intermediate code, a control flow graph constructed by multiple basic blocks may also be obtained, and the control flow relationship between these basic blocks may be represented by the control flow graph.
[0074] Figure 5 A schematic diagram of a loop structure in a control flow graph is given. Figure 5 As shown, this control flow graph may include basic blocks related to the loop structure, such as the basic block B1 before entering the loop, the loop header basic block B2, the loop body basic block B3 (the first loop structure) and the loop exit block B4.
[0075] Among them, in some embodiments, basic block B1 can be a preheader basic block, which can be used to store some statements or expressions before entering the loop; basic block B2 can be a header basic block, which can be used to store statements or expressions related to the judgment condition of whether to enter the loop, and the code stored in this basic block only affects whether the loop starts to execute, and does not affect the execution logic inside the loop. That is, if the condition is true, the loop starts to execute; if the condition is false, the loop is not executed, and the program continues to execute the code after the loop; basic block B3 can be a body basic block, which can be used to store statements or expressions related to the loop structure; basic block B4 can be used to store some statements or expressions after leaving the loop.
[0076] Step 402, compile the first loop structure to obtain a compiled program, the compiled program includes a second loop structure and a target loop invariant, the second loop structure combined with the semantics of the target loop invariant is the same as the semantics of the first loop structure, the target loop invariant is located outside the second loop structure, the target loop invariant includes the first loop invariant and / or the second loop invariant, the first loop invariant is a code used to indicate that an abnormal situation occurs when the program is running, and the second loop invariant is a code of a non-built-in function.
[0077] In the embodiment of the present application, there is no limitation on the compilation method.
[0078] For example, in some embodiments, the compilation process may be a loop optimization process. The first loop structure includes the target loop invariant, so that the first loop structure is compiled to extract the target loop invariant outside the first loop structure, thereby obtaining a compiled program. Here, the compiled program obtained may include the second loop structure and the target loop invariant. That is, the second loop structure does not include the target loop invariant.
[0079] In the embodiment of the present application, after the first loop structure is compiled, the second loop structure obtained, after combining with the target loop invariant, has the same semantics as the first loop structure. That is, after the first loop structure is compiled, the running logic and results of the program remain unchanged.
[0080] Understandably, if the loop nesting level in the loop structure is too complex, as the nesting level increases, each nesting level needs to be iterated, resulting in a significant increase in the execution time of the loop. This performance overhead is particularly evident when processing large-scale data sets. In addition, each layer of the loop corresponds to a stack frame for storing local variables and loop status. The more nesting levels there are, the more stack space is required, which increases memory consumption and the risk of stack overflow. And for complex nested loops, the difficulty of optimizing the algorithm increases significantly. More complex algorithm design or data structure selection may be required to reduce the number of loops or change the loop structure.
[0081] It can be seen that if the loop nesting level in the loop structure is too complex, when optimizing the loop structure, the processing performance overhead will increase, the code complexity and maintainability will decrease, and the optimization strategy will also be limited.
[0082] Based on this, in an embodiment of the present application, before compiling and optimizing the first loop structure, it is possible to first determine whether the loop nesting level in the first loop structure is less than the level threshold; when the loop nesting level of the first loop structure is less than the level threshold, the target loop invariant in the first loop structure is extracted to the outside of the first loop structure.
[0083] In the embodiment of the present application, there is no limitation on the value of the hierarchical threshold, which can be determined comprehensively based on factors such as algorithm performance and optimization strategy.
[0084] In the embodiment of the present application, based on the different types of the target loop invariant included in the first loop structure, the manner of extracting the target loop invariant to the outside of the first loop structure is also different.
[0085] For example, in some embodiments, the target loop invariant includes the first loop invariant. In order to extract the first loop invariant (code used to indicate the existence of an exception when the program is running) to the outside of the first loop structure, the first code in the first loop structure and the first loop invariant can be extracted to the outside of the first loop structure to obtain a compiled program.
[0086] Understandably, in programming, a compiler is a tool that converts executable code from a high-level programming language into executable machine code. During the conversion process, the compiler checks the syntax, semantics, and other rules of the code to ensure its correctness and reasonableness. When the compiler checks the code and finds that certain statements or expressions may cause runtime errors or do not conform to the language specifications, it will throw an exception (or in some cases, a warning or error).
[0087] These statements or expressions are the statements or expressions that can throw exceptions. Statements or expressions that can throw exceptions generally refer to those parts of the code that may fail for certain reasons (such as division by zero, array out-of-bounds, null pointer reference, etc.) during execution, and these failures can be caught or handled. The above types of code are the first loop invariants referred to in the embodiments of the present application.
[0088] In some embodiments, before extracting the first loop invariant to the outside of the first loop structure, it is also necessary to first determine whether the conditional code meets the cloning condition. The cloning condition includes whether the semantics of the loop structure obtained after extracting the first code obtained by copying the conditional code to the outside of the first loop structure are the same as the semantics of the first loop structure. The conditional code is the code that indicates the setting of the initialization parameters in the first loop structure before the first loop structure is compiled.
[0089] That is, when it is determined that the semantics of the loop structure obtained after extracting the first code obtained by copying the conditional code to the outside of the first loop structure are the same as the semantics of the first loop structure, it can be determined that the conditional code meets the cloning condition.
[0090] For example, Figure 6 A schematic diagram of code comparison before and after the loop invariant is hoisted out is given. As Figure 6 shown, in the loop structure of for(int i = 0; i < len; i++), the part "int i = 0; i < len;" is the conditional code. It sets the initial value of the variable i to 0 and needs to determine whether i < len holds. If it holds, the loop body part in the first loop structure can be executed. If the conditional code for entering the loop, after being copied and extracted to the outside of the first loop structure, ensures that the semantics of the loop structure before and after extraction are consistent, it can be determined that the initial condition for entering the loop meets the cloning condition.
[0091] When it is determined that the semantics of the loop structure obtained after extracting the first code to the outside of the first loop structure are not the same as the semantics of the first loop structure, it can be determined that the first code does not meet the cloning condition, that is, the first code cannot be extracted to the outside of the first loop structure.
[0092] Figure 7A schematic diagram of the structure after the first loop invariant is externalized is given. Figure 7 As shown, in some embodiments, after the first loop invariant is extracted outside the first loop structure, it can be stored in basic block B7, which can be a hoist basic block. After the first code is extracted outside the first loop structure, it can be stored in basic block B6, which can be a condition basic block. After the first loop invariant and the first code are extracted outside the first loop structure, the second loop structure (i.e., the first loop structure that does not include the first code and the first loop invariant) is stored in basic block B3.
[0093] like Figure 7 As shown, in some embodiments, the first code is located before the first loop invariant.
[0094] That is, the B6 basic block (condition basic block) for storing the first code is located before the B7 basic block (hoist basic block) for storing the first loop invariant. In this way, if the initial condition for entering the loop is not met, the execution of this program ends and the loop is left through basic block B4; if the initial condition for entering the loop is met, the loop code of the second loop structure can continue to be executed.
[0095] In this way, after the first loop invariant (code for indicating the presence of an exception during program execution) is extracted outside the first loop structure, the semantics obtained by combining it with the second loop structure is the same as the semantics of the first loop structure.
[0096] It can be understood that in compiler optimization, especially in a control flow graph, a critical edge refers to an edge that directly connects a high fan-out node (i.e., a node with multiple successor nodes) to a low fan-in node (i.e., a node with multiple predecessor nodes).
[0097] Such edges can make it difficult for the compiler to optimize effectively, because they may introduce circular dependencies or other complexities that make certain optimization techniques difficult to apply.
[0098] Based on this, in some embodiments, when the first loop invariant and the first code are extracted to the outside of the first loop structure, a second code can also be added outside the first loop structure to utilize the second code to avoid the appearance of critical edges between basic blocks in the compiled program, thereby obtaining a compiled program.
[0099] In some embodiments, the second code may be stored in a basic block, which may be a placer basic block. By adding the second code outside the first loop structure, the connection mode between the original basic blocks can be changed, thereby avoiding the generation of critical edges.
[0100] It should be noted that in order to change the connection mode between the basic blocks in the original control flow graph, the placer basic block also needs to have a specific setting position.
[0101] For example, Figure 8 A schematic diagram of the structure before and after the loop structure is compiled and optimized is given. Figure 8 As shown, in some embodiments, in order to avoid the generation of critical edges in the control flow graph, a boundary block B8 and a boundary block B9 may be added to the optimized control flow graph, and these two boundary blocks are used to store the second code.
[0102] like Figure 8 As shown, the second code is located between the first code and the third code, and the second code is located between the third code and the conditional code, and the third code is a code indicating to leave the loop.
[0103] That is, the boundary block B8 for storing the second code is located between the header basic block (loop header basic block) storing the conditional code indicating whether to enter the loop condition of the first loop structure, and the exit basic block (loop exit block) storing the third code indicating to leave the loop. And, the boundary block B9 for storing the second code is located between the basic block B6 storing the first code, and the exit basic block (loop exit block) storing the third code indicating to leave the loop.
[0104] In the embodiment of the present application, the B7 basic block for storing the first loop invariant, the B6 basic block for storing the first code, and the B9 basic block for storing the second code together constitute a protection structure for extracting the first loop invariant outside the first loop structure. Through this protection structure, after the first loop invariant is extracted outside the first loop structure, the semantics of the combination of the first loop invariant obtained in the second loop structure are the same as the semantics of the first loop structure, thereby ensuring the stability of program operation.
[0105] In some embodiments, the target loop invariant further includes a second loop invariant, where the second loop invariant is a code of a non-built-in function whose internal behavior cannot be determined.
[0106] It is understandable that when a program is compiled and optimized, there are often non-built-in functions in the program. Non-built-in functions usually refer to functions that are not provided by the programming language itself, are user-defined, or come from a third-party library. In most programming languages, there are some built-in (or standard library) functions that are used to perform common tasks, such as mathematical operations, string processing, file operations, etc.
[0107] For non-built-in functions, since the compiler cannot identify the internal behavior of non-built-in functions, if they are extracted outside the loop structure, the original semantics may be destroyed. For example, if a global value is incremented by 1 in a non-built-in function, extracting the non-built-in function outside the loop structure will lead to inconsistent semantics before and after the loop structure is optimized.
[0108] Based on this, in some embodiments, in order to determine whether the second loop invariant is extracted outside the first loop structure, it can be determined first whether the second loop invariant is a target function in a preconfigured configuration file, and the preconfigured configuration file includes multiple target functions and behavior information of each target function; when it is determined that the second loop invariant is the target function, the second loop invariant is extracted outside the first loop structure to obtain a compiled program.
[0109] That is, if the second loop invariant is the target function in the pre-configured configuration file, the compiler can find the behavior information corresponding to the target function from the pre-configured configuration file. In this way, after extracting the second loop invariant outside the first loop structure, the compiler can find the behavior information corresponding to the second loop invariant from the pre-configured configuration file, thereby ensuring the semantic consistency before and after the optimization of the first loop structure.
[0110] By implementing this embodiment, the compiler can confirm the read and write behavior inside the first loop invariant to ensure semantic consistency before and after the loop structure is optimized. By extracting the second loop invariant to the outside of the first loop structure, the work instructions within the loop are reduced, and more efficient machine code can be generated, significantly improving the execution efficiency of the first loop structure.
[0111] In the embodiment of the present application, there is no limitation on the execution order of extracting the first loop invariant and the second loop invariant outside the first loop structure.
[0112] For example, in some embodiments, it is possible to first determine whether there is a first loop invariant in the first loop structure. When it is determined that there is a first loop invariant in the first loop structure, it is then determined whether the first code in the first loop structure can be extracted outside the first loop structure. If the first code can be extracted outside the first loop structure, the second code can be added at a specific position outside the first loop structure, thereby achieving external optimization processing of the first loop invariant. Subsequently, it is also possible to determine whether there is a second loop invariant in the first loop structure. If so, it is determined whether the second loop invariant is the target function in the preset file. If the second loop invariant is the target function, the second loop invariant can be extracted outside the first loop structure, thereby achieving external optimization processing of the second loop invariant.
[0113] In some other embodiments, it may be determined first whether the second loop invariant exists in the first loop structure, and then whether the first loop invariant exists in the first loop structure.
[0114] Of course, in some other embodiments, it is also possible to simultaneously determine whether the first loop structure has the first loop invariant and the second loop invariant, and execute the conditions for determining the external optimization processing of the first loop invariant and the second loop invariant in the above embodiment.
[0115] In some embodiments, after extracting the first loop invariant and / or the second loop invariant outside the first loop structure to obtain the second loop structure, other types of loop invariants in the first loop structure may continue to be analyzed to extract other loop invariants that can maintain semantic consistency before and after program optimization after the loop invariant externalization optimization operation to the outside of the first loop structure, thereby reducing the instructions executed in the loop and improving the performance of the program.
[0116] Fig. 9 A schematic diagram of the optimization before and after of other types of loop invariants is given. Fig. 9 As shown, before optimization, the expressions x=y+z and x*x are both loop invariants, and the loop invariants can be extracted outside the first loop structure, that is, the target loop invariants can also include such loop invariants that do not require special processing and can be directly extracted outside the first loop structure to maintain semantic consistency before and after program optimization.
[0117] By implementing the compilation optimization method provided in the embodiment of the present application, the target loop invariant that was originally extracted outside the first loop structure, which would change the semantics of the program, can be extracted outside the first loop structure without changing the before and after semantics of the program, thereby ensuring the stability of program operation.
[0118] For example, see Figure 5 ,like Figure 5 As shown, the getName() function is a function that may throw an exception (i.e., the first loop invariant), and the function is not a non-built-in function. By executing the compilation optimization method provided in the embodiment of the present application, the compiler can confirm the read and write behavior inside the function to ensure that the semantics of throwing exceptions remain consistent with those before optimization. And by extracting the function call statement outside the first loop structure, the working instructions in the first loop structure can be reduced, and more efficient machine code can be generated, which significantly improves the execution efficiency of the program to be compiled.
[0119] In the embodiment of the present application, after the first loop structure is compiled, the target loop invariant obtained may include a code for indicating that an abnormal situation occurs when the program is running (the first loop invariant), or may also include a code for a non-built-in function whose internal behavior cannot be determined by the compiler (the second loop invariant). In this way, the optimization application scope of the loop structure can be expanded and the optimization capability of the loop structure can be enhanced while ensuring that the semantics before and after the compilation optimization remain consistent.
[0120] The following describes an exemplary application of the embodiments of the present application in a practical application scenario.
[0121] Fig.10 The overall process of the compilation optimization method provided in the embodiment of the present application. Fig.10 As shown, the method comprises the following steps:
[0122] First, the program to be compiled is analyzed, and the basic blocks and control flow graph corresponding to the program to be compiled are established.
[0123] Then, a loop analysis is performed on the control flow graph to identify the loop structure in the control flow graph and mark the key basic blocks in the loop structure, such as the preheader basic block, header basic block, body basic block, exit basic block, etc. The preheader basic block usually stores some statements and expressions before entering the loop; the header basic block stores statements and expressions related to the judgment conditions of whether to enter the loop; the body basic block stores statements or expressions executed in the loop; and the exit basic block stores some statements or expressions after leaving the loop.
[0124] Then, it is further analyzed whether the structure can be optimized according to the relationship between the basic blocks in the loop structure. For example, if the loop structure is too complex (multi-layer nesting), no optimization is performed.
[0125] Next, check whether the first loop invariant (statements and expressions that may throw exceptions) exists in the loop structure. If the first loop invariant exists, add an additional protection structure to the loop structure. The core of this is to clone the condition for entering the loop for the first time (the first code) from the loop structure to ensure that after the first loop invariant is copied outside the loop structure, its semantics remain consistent with the semantics of the program before optimization.
[0126] Among them, if the condition for entering the loop for the first time cannot be met, then when executing the loop optimization of the program to be compiled, the loop structure in the program to be compiled will not be entered, and the first loop invariant will not be executed. Therefore, before the first loop invariant is extracted outside the loop structure, it is necessary to first determine whether the first code will be executed. If the initial condition for entering the loop indicated by the first code is not met, then after the first loop invariant is extracted to the loop structure, the first loop invariant cannot be executed, thereby ensuring the semantic consistency before and after the optimization.
[0127] If the condition for entering the loop for the first time cannot be cloned, a guard structure cannot be added. Even if there is a first loop invariant that can be extracted, the semantic consistency before and after optimization cannot be ensured because a guard structure cannot be added, and the first loop invariant cannot be extracted to the statement outside the loop structure. In this case, you can directly turn to check whether there is a second loop invariant (non-built-in function) in the loop structure.
[0128] After adding the guard structure, the control flow graph has more basic blocks, among which the condition block stores statements and expressions related to the initial conditions for entering the loop, the hoist block stores external invariant statements and expressions; the placer block exists to avoid the generation of critical edges (this situation is not conducive to compiler optimization). By adding additional guard structures, the semantic consistency before and after the first loop invariant is extracted to the loop structure is guaranteed.
[0129] Then check whether the non-built-in functions in the loop can be safely externalized outside the loop structure by comparing them with the function behavior information in the configuration file. If it is found that the behavior in the function meets the requirements for externalizing the loop invariant, extract the non-built-in function outside the loop structure; if the function does not exist in the configuration file, extract the non-built-in function outside the loop structure.
[0130] After the external optimization processing of the first loop invariant or the second loop invariant is completed, the traditional external optimization process of the loop invariant may continue to be executed.
[0131] By implementing this embodiment, an additional protection structure is added to the statements or expressions in the loop structure that are loop invariant and can throw exceptions, so that they can be lifted out of the loop under the condition that the program semantics before and after optimization are consistent; by configuring the behavior list of non-built-in functions, non-built-in function call statements or expressions can be lifted out of the loop structure under the condition that the program semantics before and after optimization are consistent. This expands the application scope of the traditional loop invariant lifting optimization and enhances the compiler's optimization capability for loop structures.
[0132] By implementing this embodiment, when compiling an application or system program, a higher quality machine code can be generated for a loop structure that meets the basic optimization conditions, thereby improving the execution efficiency of such functions. If these functions are hot functions of an interface program, the interface freeze can be reduced and the fluency can be improved; if these functions are hot functions of a computing application, the computing speed can be increased and the power consumption and heat generation of the computing device can be reduced.
[0133] It should be understood that, although the steps in the above-mentioned flowcharts are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the above-mentioned flowcharts may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these sub-steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.
[0134] Based on the foregoing embodiments, the embodiments of the present application provide a compilation optimization device, which includes the modules included and the units included in the modules, which can be implemented by a processor; of course, it can also be implemented by a specific logic circuit; in the implementation process, the processor can be a central processing unit (CPU), a microprocessor (MPU), a digital signal processor (DSP) or a field programmable gate array (FPGA), etc.
[0135] Fig.11 A schematic diagram of the structure of the compilation optimization device provided in the embodiment of the present application is shown in FIG. Fig.11 As shown, the device 1100 includes an acquisition module 1101 and a compilation module 1102, wherein:
[0136] An acquisition module 1101 is used to acquire a first loop structure in a program to be compiled, where the first loop structure is a loop logic described by a code language;
[0137] A compilation module 1102 is used to compile the first loop structure to obtain a compiled program, wherein the compiled program includes a second loop structure and a target loop invariant, wherein the second loop structure combined with the semantics of the target loop invariant is the same as the semantics of the first loop structure, the target loop invariant is located outside the second loop structure, the target loop invariant includes the first loop invariant and / or the second loop invariant, the first loop invariant is a code used to indicate that an abnormal situation occurs when the program is running, and the second loop invariant is a code of a non-built-in function.
[0138] In some embodiments, the first loop structure includes the target loop invariant, and the compiling module 1102 is used to extract the target loop invariant to the outside of the first loop structure to obtain the compiled program.
[0139] In some embodiments, the target loop invariant includes the first loop invariant, and the compiling module 1102 is specifically used to copy the conditional code in the first loop structure to obtain a first code, wherein the conditional code is a code indicating the initialization parameter setting in the first loop structure before the first loop structure is compiled;
[0140] The first code and the first loop invariant are extracted outside the first loop structure to obtain the compiled program.
[0141] In some embodiments, the first code is stored before the first loop invariant.
[0142] In some embodiments, the device also includes a judgment module, which is used to determine that the conditional code satisfies a cloning condition, and the cloning condition includes that the semantics of the loop structure obtained after extracting the first code to the outside of the first loop structure are the same as the semantics of the first loop structure.
[0143] In some embodiments, the target loop invariant also includes a second loop invariant, and the compilation module 1102 is specifically used to determine whether the second loop invariant is a target function in a preconfigured configuration file, and the preconfigured configuration file includes multiple target functions and behavior information of each target function; when it is determined that the second loop invariant is the target function, the second loop invariant is extracted to the outside of the first loop structure to obtain the compiled program.
[0144] In some embodiments, the compilation module 1102 is further specifically used to extract the target loop invariant to the outside of the first loop structure, and to add a second code outside the first loop structure to obtain the compiled program, and to avoid the occurrence of critical edges between basic blocks in the compiled program through the second code.
[0145] In some embodiments, the second code is located between the first code and a third code, and the second code is located between the third code and a conditional code, and the third code is a code indicating to leave a loop.
[0146] In some embodiments, the judgment module is also used to determine whether the loop nesting level in the first loop structure is less than a level threshold; when the loop nesting level of the first loop structure is less than the level threshold, the target loop invariant in the first loop structure is extracted to the outside of the first loop structure.
[0147] The description of the above device embodiment is similar to the description of the above method embodiment, and has similar beneficial effects as the method embodiment. For technical details not disclosed in the device embodiment of the present application, please refer to the description of the method embodiment of the present application for understanding.
[0148] It should be noted that in the embodiments of this application Fig.11 The division of modules in the compilation optimization device shown is schematic, which is only a logical function division. There may be other division methods in actual implementation. In addition, each functional unit in each embodiment of the present application can be integrated into a processing unit, or it can exist physically alone, or two or more units can be integrated into one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. It can also be implemented in the form of a combination of software and hardware.
[0149] It should be noted that in the embodiment of the present application, if the above method is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the embodiment of the present application can be essentially or partly embodied in the form of a software product that contributes to the relevant technology. The computer software product is stored in a storage medium, including several instructions to enable an electronic device to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as a U disk, a mobile hard disk, a read-only memory (ROM), a magnetic disk or an optical disk. In this way, the embodiment of the present application is not limited to any specific combination of hardware and software.
[0150] The embodiment of the present application provides a computer device, which may be a server, and its internal structure diagram may be as follows: Fig.12As shown. The computer device includes a processor, a memory and a network interface connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store data. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, the above method is implemented.
[0151] An embodiment of the present application provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps in the method provided in the above embodiment are implemented.
[0152] An embodiment of the present application provides a computer program product including instructions, which, when executed on a computer, enables the computer to execute the steps of the method provided in the above method embodiment.
[0153] Those skilled in the art will understand that Fig.12 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0154] In one embodiment, the compilation optimization device provided by the present application can be implemented in the form of a computer program. The computer program can be Fig.12 The computer device shown in the figure is run. The memory of the computer device can store various program modules constituting the above-mentioned device. The computer program composed of various program modules enables the processor to execute the steps in the method of each embodiment of the present application described in this specification.
[0155] It should be noted here that the description of the above storage medium and device embodiments is similar to the description of the above method embodiments, and has similar beneficial effects as the method embodiments. For technical details not disclosed in the storage medium, storage medium and device embodiments of this application, please refer to the description of the method embodiments of this application for understanding.
[0156] It should be understood that "one embodiment" or "an embodiment" or "some embodiments" mentioned throughout the specification means that specific features, structures or characteristics related to the embodiment are included in at least one embodiment of the present application. Therefore, "in one embodiment" or "in one embodiment" or "in some embodiments" appearing throughout the specification may not necessarily refer to the same embodiment. In addition, these specific features, structures or characteristics can be combined in one or more embodiments in any suitable manner. It should be understood that in various embodiments of the present application, the size of the sequence number of the above-mentioned processes does not mean the order of execution, and the execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present application. The above-mentioned sequence numbers of the embodiments of the present application are only for description and do not represent the advantages and disadvantages of the embodiments. The above description of each embodiment tends to emphasize the differences between the various embodiments, and the same or similar aspects can be referenced to each other. For the sake of brevity, this article will not repeat them.
[0157] The term "and / or" in this article is only a description of the association relationship of associated objects, indicating that there may be three relationships. For example, object A and / or object B can represent three situations: object A exists alone, object A and object B exist at the same time, and object B exists alone.
[0158] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0159] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The embodiments described above are only schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation, such as: multiple modules or components can be combined, or can be integrated into another system, or some features can be ignored, or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or modules can be electrical, mechanical or other forms.
[0160] The modules described above as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules; they may be located in one place or distributed on multiple network units; some or all of the modules may be selected according to actual needs to achieve the purpose of the present embodiment.
[0161] In addition, all functional modules in the embodiments of the present application may be integrated into one processing unit, or each module may be a separate unit, or two or more modules may be integrated into one unit; the above-mentioned integrated modules may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0162] A person skilled in the art can understand that all or part of the steps of implementing the above method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above method embodiment; and the aforementioned storage medium includes: mobile storage devices, read-only memories (ROM), magnetic disks or optical disks, etc., various media that can store program codes.
[0163] Alternatively, if the above-mentioned integrated unit of the present application is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present application can essentially or in other words, the part that contributes to the relevant technology can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for enabling an electronic device to execute all or part of the methods described in each embodiment of the present application. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROMs, magnetic disks, or optical disks.
[0164] The methods disclosed in several method embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments.
[0165] The features disclosed in several product embodiments provided in this application can be arbitrarily combined without conflict to obtain new product embodiments.
[0166] The features disclosed in several method or device embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments or device embodiments.
[0167] The above is only an implementation method of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A compilation optimization method, characterized in that: The method comprises: Acquire a first loop structure in the program to be compiled, where the first loop structure is a loop logic described by a code language; The first loop structure is compiled to obtain a compiled program, wherein the compiled program includes a second loop structure and a target loop invariant, wherein the second loop structure has the same semantics as the first loop structure in combination with the semantics of the target loop invariant, wherein the target loop invariant is located outside the second loop structure, wherein the target loop invariant includes the first loop invariant and / or the second loop invariant, wherein the first loop invariant is a code for indicating that an abnormal situation occurs when the program is running, and wherein the second loop invariant is a code of a non-built-in function.
2. The method according to claim 1, characterized in that The first loop structure includes the target loop invariant, and the compiling the first loop structure to obtain a compiled program includes: The target loop invariant is extracted outside the first loop structure to obtain the compiled program.
3. The method according to claim 2, characterized in that The target loop invariant includes the first loop invariant, and extracting the target loop invariant to the outside of the first loop structure to obtain the compiled program includes: Copying the conditional code in the first loop structure to obtain a first code, wherein the conditional code is a code indicating the initialization parameter setting in the first loop structure before compiling the first loop structure; The first code and the first loop invariant are extracted outside the first loop structure to obtain the compiled program.
4. The method according to claim 3, characterized in that The first code is located before the first loop invariant.
5. The method according to claim 3, characterized in that: Before extracting the first code and the first loop invariant to the outside of the first loop structure, the method further includes: It is determined that the conditional code satisfies a cloning condition, where the cloning condition includes that the semantics of a loop structure obtained after extracting the first code outside the first loop structure are the same as the semantics of the first loop structure.
6. The method according to claim 3, characterized in that: The target loop invariant further includes a second loop invariant, and extracting the target loop invariant in the first loop structure to the outside of the first loop structure to obtain the compiled program includes: Determining whether the second loop invariant is an objective function in a preconfigured configuration file, wherein the preconfigured configuration file includes a plurality of objective functions and behavior information of each objective function; When it is determined that the second loop invariant is the target function, the second loop invariant is extracted outside the first loop structure to obtain the compiled program.
7. The method according to claim 3, characterized in that The step of extracting the target loop invariant to the outside of the first loop structure to obtain the compiled program includes: The target loop invariant is extracted to the outside of the first loop structure, and a second code is added to the outside of the first loop structure to obtain the compiled program, and the second code is used to avoid critical edges between basic blocks in the compiled program.
8. The method according to claim 7, characterized in that The second code is located between the first code and a third code, and the second code is located between the third code and the conditional code, and the third code is a code indicating to leave a loop.
9. The method according to claim 2, characterized in that: Before extracting the target loop invariant in the first loop structure to the outside of the first loop structure, the method further includes: Determine whether the loop nesting level in the first loop structure is less than a level threshold; When the loop nesting level of the first loop structure is less than the level threshold, the target loop invariant in the first loop structure is extracted to the outside of the first loop structure.
10. A compilation optimization device, characterized in that: include: An acquisition module, used for acquiring a first loop structure in a program to be compiled, wherein the first loop structure is a loop logic described by a code language; A compiling module is used to compile the first loop structure to obtain a compiled program, wherein the compiled program includes a second loop structure and a target loop invariant, wherein the second loop structure has the same semantics as the first loop structure in combination with the semantics of the target loop invariant, wherein the target loop invariant is located outside the second loop structure, wherein the target loop invariant includes the first loop invariant and / or the second loop invariant, wherein the first loop invariant is a code for indicating that an abnormal situation occurs when the program is running, and the second loop invariant is a code of a non-built-in function.
11. A computer device comprising a memory and a processor, wherein the memory stores a computer program that can be run on the processor, characterized in that: When the processor executes the program, the steps of the method according to any one of claims 1 to 9 are implemented.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.