Redundant operation optimization method and device, compiler, equipment, medium and program
By optimizing redundant operations in program compilation through intermediate representation analysis and linking, the method addresses performance issues in existing programs, leading to enhanced execution performance.
Patent Information
- Application Number
- CN202411134221.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-16
- Publication Date
- 2024-12-06
AI Technical Summary
Existing programs suffer from reduced performance due to redundant operations that do not affect the program results, which cannot be effectively optimized during the compilation process using existing tools like full-program analyzers.
A method and device that optimize redundant operations by analyzing and optimizing intermediate representations of source files during the compilation and linking phases, utilizing techniques such as LTO and IPO to identify and eliminate redundant operations at the semantic and data flow levels.
This approach enhances program processing performance by eliminating redundant operations during compilation, resulting in optimized executable programs with improved execution speed and efficiency.
Smart Images

Figure CN119088361A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the technical field of program optimization, and specifically to a redundant operation optimization method, apparatus, compiler, device, medium and program. Background Art
[0002] As the complexity of programs (such as functional complexity, structural complexity, etc.) increases, a large number of invalid calculations in the programs become one of the key factors affecting the program processing performance. Therefore, it is necessary to optimize the invalid calculations in the programs, such as eliminating the invalid calculations in the programs.
[0003] Redundant operations are operations in a program that do not affect the program results. That is, redundant operations are redundant in a program and can be regarded as an invalid calculation of the program. Therefore, how to provide a redundant operation optimization solution to optimize redundant operations in a program and thereby improve program processing performance has become a technical problem that those skilled in the art need to solve urgently. Summary of the invention
[0004] In view of this, embodiments of the present application provide a redundant operation optimization method, apparatus, compiler, device, medium and program to optimize redundant operations in a program and improve program processing performance.
[0005] To achieve the above objectives, the embodiments of the present application provide the following technical solutions.
[0006] In a first aspect, an embodiment of the present application provides a redundant operation optimization method, including:
[0007] In the compilation phase of the program, intermediate representations corresponding to the plurality of source files of the program are obtained, wherein the intermediate representations of the source files at least record the semantic structure information of the source files;
[0008] In the linking phase of program compilation, the intermediate representations of the various source files are linked into an intermediate file; the redundant operations in the intermediate file are determined; and the redundant operations in the intermediate file are optimized.
[0009] In a second aspect, an embodiment of the present application provides a redundant operation optimization device, including:
[0010] A compilation optimization module, used for obtaining intermediate representations corresponding to a plurality of source files of a program respectively during the compilation phase of program compilation, wherein the intermediate representations of the source files at least record semantic structure information of the source files;
[0011] The link optimization module is used to link the intermediate representations of various source files into intermediate files during the link phase of program compilation; determine the redundant operations in the intermediate files; and optimize the redundant operations in the intermediate files.
[0012] In a third aspect, an embodiment of the present application provides a compiler, comprising the redundant operation optimization device as described in the second aspect above.
[0013] In a fourth aspect, an embodiment of the present application provides a compilation device, comprising at least one processor and at least one memory; the memory stores instructions of a compiler, and the processor calls the instructions of the compiler to execute the redundant operation optimization method as described in the first aspect above.
[0014] In a fifth aspect, an embodiment of the present application provides a storage medium, wherein the storage medium stores instructions of a compiler, and when the instructions of the compiler are executed, the redundant operation optimization method as described in the first aspect above is implemented.
[0015] In a sixth aspect, an embodiment of the present application provides a computer program product, including instructions of a compiler, which, when executed, implements the redundant operation optimization method described in the first aspect above.
[0016] The redundant operation optimization method provided by the embodiment of the present application can obtain the intermediate representations corresponding to the multiple source files of the program respectively during the compilation phase of the program compilation, wherein the intermediate representation of the source file at least records the semantic structure information of the source file; during the linking phase of the program compilation, the intermediate representations of the various source files are linked into an intermediate file; and then the redundant operations in the intermediate file are determined; and the redundant operations of the intermediate file are optimized to achieve the optimization of the redundant operations during the linking phase of the program compilation. It can be seen that the redundant operation optimization method provided by the embodiment of the present application can optimize the redundant operations during the program compilation process, and is applicable to the program compilation process; and then, after the program compilation is completed, based on the redundant operations optimized during the program compilation process, the embodiment of the present application can provide an executable program with a large number of redundant operations optimized, thereby improving the execution performance of the executable program and achieving the improvement of the program processing performance. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0018] Figure 1 This is an example diagram of the stages of the program compilation process.
[0019] Figure 2 A flowchart of a redundant operation optimization method provided in an embodiment of the present application.
[0020] Figure 3A flowchart for determining an intermediate representation of a source file provided in an embodiment of the present application.
[0021] Figure 4 This is an example diagram of how to form an intermediate file.
[0022] Figure 5 A flowchart for determining redundant operations in an intermediate file provided in an embodiment of the present application.
[0023] Fig. 6A An example diagram of an irregular redundant sparse vector.
[0024] Figure 6B An example diagram of an irregular redundant sparse matrix.
[0025] Figure 6C An example graph of a regular redundant sparse diagonal matrix.
[0026] Fig. 7A An example diagram for optimizing irregular redundant sparse vectors.
[0027] Figure 7B An example diagram for optimizing irregular redundant sparse matrices.
[0028] Figure 7C An example graph for optimizing a regular redundant sparse diagonal matrix.
[0029] Figure 8 A block diagram of a redundant operation optimization device provided for the implementation of the present application. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0031] Eliminating invalid calculations in programs can be mainly divided into two directions: invalid code removal optimization and redundant operation elimination optimization.
[0032] Dead code removal optimization is mainly used to remove dead code in the program. The dead code in the program mainly targets useless code and unreachable code in the program, that is, dead code removal optimization can be used to remove useless code and unreachable code in the program. Among them, although the useless code is executed in the program, the execution result is not used by other program codes. Therefore, the useless code has no effect on the function of the program and is regarded as a type of invalid code in the program and can be deleted. Unreachable code will not be executed in the execution process of the program. For example, unreachable code is not included in the effective control flow path of the program. The effective control flow path refers to the code path that is actually executed during the execution of the program. Since the unreachable code has no chance to be executed in the program, the unreachable code has no effect on the function of the program and is regarded as a type of invalid code in the program and can be deleted.
[0033] Redundant operation elimination optimization is mainly used to eliminate redundant operations in a program, wherein the operation results of the redundant operations have no effect on the program results; that is, the redundant operations are redundant in the program.
[0034] To facilitate understanding of redundant operations, an example of redundant operations is described below.
[0035] A loop accumulation operation with zero increment, for example, a variable is accumulated in a loop accumulation operation, but the accumulated value each time is 0. Since the loop accumulation operation does not actually change the value of the variable, the loop accumulation operation is a redundant operation;
[0036] Repeated loading operations that repeatedly load the same data at the same memory location, such as repeatedly reading data from the same memory location without modifying the data during the reading (i.e., the data read each time is the same), then except for the first read data, the remaining read data are unnecessary, so the repeated loading operation except for the first load operation is a redundant operation;
[0037] A continuous loading operation that continuously loads the same data into adjacent memory locations, for example, reading data from consecutive memory locations, but the consecutive memory locations store the same data (i.e., continuous loading operations are performed on adjacent memory locations, but the loaded values are the same each time). Since the data stored in adjacent memory locations have no difference or correlation during the process of querying adjacent memory locations by the program, the operation of continuously loading adjacent memory locations becomes redundant, and thus the continuous loading operation is a redundant operation.
[0038] Of course, redundant operations may exist in other forms. The above-mentioned form of redundant operations is only an example to assist in understanding redundant operations. As long as the operation result does not affect the program result, the operation that does not affect the program result can be regarded as a redundant operation.
[0039] It should be noted that the dead code removal optimization is mainly to identify and remove useless code and unreachable code in the program through static code analysis. Redundant operation elimination optimization is different from dead code removal optimization. Since redundant operations are related to the data structure design, algorithm design and data flow of the program when it is running, for example, redundant operations involve calculations that are actually executed in the program but have no effect on the program results, eliminating redundant operations cannot be achieved simply through static code analysis, but requires in-depth analysis of the program logic to identify and reconstruct unnecessary calculation processes in the program.
[0040] Based on this, detecting and eliminating redundant operations in a program is of great significance for improving program processing performance.
[0041] One way to detect redundant operations is to analyze an executable program using a full program analyzer to identify redundant memory load operations in the executable program and quantify the cost of the redundant memory load operations.
[0042] Specifically, the full program analyzer performs instrumentation processing on the memory load operation in the executable program to record and track each memory load operation and the corresponding load value, thereby establishing a history record of memory accesses, so as to analyze whether the memory load operation is a redundant memory load operation; wherein instrumentation is a technology for inserting monitoring code, and the full program analyzer can insert additional monitoring code into the executable program through instrumentation processing to record and track the behavior of the memory load operation;
[0043] Through the history of memory access established by the full program analyzer, it is possible to analyze whether the memory load operation is a redundant memory load operation, thereby detecting the redundant memory load operation; for example, the redundant memory load operation can be divided into a time-redundant memory load operation and a space-redundant memory load operation; the time-redundant memory load operation refers to a repeated load operation of repeatedly loading the same data at the same memory location, and the relevant content can refer to the description of the corresponding part of the previous text; the space-redundant memory load operation refers to a continuous load operation of continuously loading the same data at adjacent memory locations, and the relevant content can refer to the description of the corresponding part of the previous text;
[0044] Furthermore, after identifying redundant memory load operations, the full-program analyzer can also determine the redundant scope of the redundant memory load operations in the memory space, such as the size and distribution of the memory area affected by the redundant memory load operations; further, the full-program analyzer can quantify the cost of redundant memory load operations, such as converting the redundant scope of redundant memory load operations into performance loss indicators, such as the additional processing time and increased number of memory accesses caused by redundant memory load operations.
[0045] Although the above-mentioned full-program analyzer can detect redundant operations, the object of analysis of the full-program analyzer is the executable program, and the executable program is the file after the program is compiled. Therefore, the full-program analyzer is suitable for optimizing redundant operations after the program is compiled, but not for optimizing redundant operations during the program compilation process. Its applicable scenarios are limited.
[0046] It should be noted that an executable program refers to an executable file formed by compiling and linking a program, and an executable program can be loaded and executed by a computer's operating system. For ease of understanding, the program compilation process is introduced below.
[0047] Program compilation is the process of converting program code written in a high-level programming language (referred to as source code) into computer-executable machine language code (referred to as machine code); the machine code generated by program compilation can be read and executed by the computer's processor, that is, program compilation is the process of preparing an instruction set that can be executed by the computer's processor.
[0048] For ease of understanding, Figure 1 The following is an example diagram showing the stages of the program compilation process: Figure 1 As shown, the program compilation process can be divided into a compilation phase 110 and a linking phase 120. In the compilation phase 110, the source code of the program is compiled into machine code; in the linking phase 120, the target file composed of the machine code can be linked into an executable file (i.e., an executable program). It can be seen that when the program compilation is completed, the result of the program compilation is an executable file. Further, after the program compilation is completed, the loader of the operating system can be responsible for reading the executable file and loading the contents of the executable file into the memory to start executing the machine code of the executable file.
[0049] Based on the above description, it can be seen that the full program analyzer detects and quantifies the cost of redundant operations in the executable program after the program is compiled. It is suitable for optimizing redundant operations after the program is compiled, but not for optimizing redundant operations during the program compilation process. In other words, the full program analyzer is an analysis tool that detects redundant operations in the executable program after the program is compiled. It is not an optimization component used by the compiler during the program compilation process, and its applicable scenarios are limited.
[0050] Based on this, the embodiment of the present application provides a redundant operation optimization solution in the program compilation process, so as to optimize the redundant operations in the program compilation process, for example, at least detecting the redundant operations in the program compilation process, and further eliminating and optimizing the detected redundant operations. Furthermore, after the program compilation is completed, based on the redundant operations eliminated and optimized in the program compilation process, the embodiment of the present application can provide an executable program with a large number of redundant operations eliminated and optimized, thereby improving the execution performance of the executable program and realizing the improvement of program processing performance.
[0051] As an optional implementation, Figure 2 An optional flow chart of the redundant operation optimization method provided by the embodiment of the present application is exemplarily shown. The method flow can be implemented by a compiler, wherein the compiler can be regarded as a computer program that converts the source code written in a high-level programming language in the program to be compiled into machine code so that the computer can execute the converted machine code. Figure 2 , the process of the redundant operation optimization method may include the following steps.
[0052] In step S210, in the compilation phase of program compilation, intermediate representations corresponding to a plurality of source files of the program are obtained, wherein the intermediate representations of the source files at least record semantic structure information of the source files.
[0053] As an optional implementation, the program to be compiled (hereinafter referred to as the program) may include multiple source files. The source file can be regarded as a program file composed of source code. For example, a source file of the program can correspond to a functional module of the program, so that different source files of the program can be responsible for different tasks of the program or correspond to different functional modules of the program.
[0054] It should be noted that the number of source files included in a program can be determined based on the size and complexity of the program. For example, the size and complexity of a program are positively correlated with the number of source files included in the program. Thus, the larger the program and the higher the complexity, the more source files the program contains; the smaller the program and the lower the complexity, the fewer source files the program contains. The embodiments of the present application do not set a limit on the number of source files included in a program, which can be determined according to specific circumstances.
[0055] During the compilation phase of program compilation, different from the method of directly compiling the source files of the program into target files, the embodiment of the present application compiles each source file of the program into an intermediate representation (IR) to obtain the intermediate representations of each source file of the program, that is, the intermediate representations corresponding to the multiple source files of the program respectively; thus, the intermediate representation of the source file at least records the semantic structure information of the source file, so as to subsequently optimize redundant operations at the level of the intermediate representation of the program.
[0056] It should be noted that the target file is a file formed by compiling the source code of the source file into machine code, that is, compiling the source code of the source file into machine code to obtain the target file corresponding to the source file. When compiling the source file, the embodiment of the present application does not directly compile the source file into the target file corresponding to the machine code, but compiles each source file into an intermediate representation.
[0057] As an optional implementation, the intermediate representation can be used as a data structure used by the compiler to bridge the source code and the machine code and comprehensively express the semantic structure information of the source code. In other words, the intermediate representation of the source file can bridge the source code of the source file and the machine code of the target file corresponding to the source file, and the intermediate representation of the source file can record the semantic structure information of the source file.
[0058] In an optional implementation, the intermediate representation of the source file may indicate the semantic structure information of the source file by describing the execution intention information of the source file (e.g., the intention and function corresponding to the source code of the source file). In a further optional implementation, the intermediate representation of the source file may describe the execution intention information, control flow information, and data flow information of the source file.
[0059] The execution intention information of the source file can represent the program intention of the source file execution. As an implementation example, the execution intention information of the source file includes but is not limited to variable operation information (such as variable definition, assignment, use, etc.), function call information (such as call relationship information between functions, including parameter passing and return value, etc.). Among them, variables can be regarded as identifiers used to store data in program programming. For example, the program uses variables to store temporary data values at runtime; through the operation of variables (such as variable definition, assignment and use, etc.), the program can perform data calculations and manage data. Functions can be regarded as basic statement blocks of programs, such as a group of statement blocks organized together in a program to perform specific tasks, to achieve tasks such as performing calculations, processing data, and returning information; functions can have the ability to accept input (parameters) and generate output (return values).
[0060] The control flow information of a source file may represent the flow of the program execution path of the source file, such as the execution order of various operations in the source file. As an implementation example, the control flow information of a source file may include but is not limited to conditional statements, loop structures, etc., wherein the conditional statements define the conditions that affect the execution path of the program, and the loop settings, loop termination conditions, and loop behaviors of the loop structure affect the loop execution path of the program.
[0061] The data flow information of the source file can represent the data flow of the source file, such as the flow and conversion mechanism of the data in the source file, which is related to the data generation, use and change of the source file. As an implementation example, the data flow information of the source file may include but is not limited to the life cycle of the variable, data dependency, etc.; wherein the life cycle of the variable may include the creation of the variable (such as the creation location of the variable, the creation conditions, etc.), the use of the variable (such as the use location of the variable, the use conditions, etc.), and the deactivation of the variable (such as the time to stop using the variable, etc.); the data dependency indicates that the output of one operation becomes the input of another operation, that is, the input and output dependency between the operations of the source file.
[0062] In an optional implementation, the embodiment of the present application may at least convert the syntax tree of the source file into an intermediate representation to obtain the intermediate representation of the source file. Figure 3 An optional flow chart for determining the intermediate representation of a source file provided in an embodiment of the present application is shown as an example, referring to Figure 3 , the process may include the following steps.
[0063] In step S310, a syntax tree of the source file is determined.
[0064] In an optional implementation, for any source file of a program, an embodiment of the present application can determine a syntax tree of the source file. The syntax tree of the source file can be a tree representation of the abstract syntax structure of the source code of the source file. Each node of the syntax tree can represent a construct in the source code of the source file, such as a statement, an expression, a control structure, etc.
[0065] As an optional implementation, during the compilation phase, the source file can construct a syntax tree through preprocessing, lexical analysis, syntax analysis and other processes. After constructing the syntax tree of the source file, the compiler can at least convert the syntax tree of the source file into an intermediate representation of the source file.
[0066] It should be noted that preprocessing mainly processes the preprocessing instructions in the source code of the source file, thereby performing necessary text replacement and modification on the source code, etc. For example, the preprocessing instructions may include an include instruction, thereby processing the include instruction, and inserting the specified file content into the location of the include instruction; for example, the preprocessing instructions may include macro definition and replacement instructions, thereby processing the macro definition and replacement instructions, and replacing the instances matching the macro name in the source file with the content of the macro definition; for example, the preprocessing instructions may include a delete comment instruction, thereby processing the delete comment instruction, and removing the comment from the source code (the comment is the content used to explain the code in programming but not actually executed).
[0067] Lexical analysis is mainly used to convert a source file (such as a preprocessed source file) into a series of lexical units, thereby using the lexical units to construct a syntax tree, wherein the lexical units represent keywords, identifiers, operators, numeric constants, etc. represented by the source code in the source file. Furthermore, after checking that the lexical units comply with the grammatical rules, the lexical units can be used to construct a tree data structure that reflects the program structure of the source file, thereby expressing the nested structure of the source code of the source file through the tree data structure to construct a syntax tree. A node in the syntax tree of the source file can represent a structure in the source code of the source file, such as an expression, a statement, or a control flow structure.
[0068] In step S320, at least the syntax tree of the source file is converted into an intermediate representation of the source file.
[0069] In an optional implementation, the compiler may at least convert the syntax tree of the source file into an intermediate representation. As an optional implementation example, the compiler may traverse the syntax tree of the source file and analyze the type and attributes of each node of the syntax tree; while traversing the syntax tree, the compiler may perform semantic analysis to check the semantic consistency of the syntax tree, such as whether the declaration and use of variables and functions are consistent, whether the type check passes, etc.; thus, after the semantic analysis passes, for each node of the syntax tree, the compiler may generate corresponding intermediate codes for the node, for example, generating corresponding intermediate codes for the node according to the type and attributes of the node; for example, the nodes of arithmetic expressions may be converted into a series of intermediate representation instructions that perform corresponding arithmetic operations, and control structures such as conditional statements and loops may be converted into jump and conditional branch instructions in the intermediate representation, etc.
[0070] It should be noted that the syntax tree is a tree representation of the abstract syntax structure of the source code of the source file, which mainly reflects the structure and syntax relationship of the source code of the source file, and does not cover the execution details. Therefore, although the syntax tree of the source file provides a detailed view of the source file, it may not represent the control flow and data flow of the source file, so that the compiler can construct the control flow information of the source file and the data flow information of the source file, and then the compiler can combine the intermediate code corresponding to the nodes of the syntax tree of the source file, the control flow information of the source file and the data flow information of the source file to form an intermediate representation of the source file in the compilation stage of program compilation.
[0071] Back to Figure 2 As shown, in step S220, in the linking stage of program compilation, the intermediate representations of the various source files are linked into an intermediate file.
[0072] After obtaining the intermediate representation of each source file, the embodiment of the present application can link the intermediate representation of each source file into an intermediate file (module file), thereby forming a global intermediate representation of the program. In an optional implementation, the embodiment of the present application can be processed by LTO (Link Time Optimization), and the intermediate representations of each source file are linked into an intermediate file in the linking stage. In other words, the compiler can divide the optimization of the compilation process into optimization in the compilation stage and optimization in the linking stage, and the optimization in the linking stage can be regarded as LTO processing. Thus, after obtaining the intermediate representation of each source file, the embodiment of the present application can enter the linking stage; and then, in the linking stage, the intermediate representations of each source file are linked into an intermediate file.
[0073] Different from the method of linking the target file into an executable file in the linking stage, the embodiment of the present application uses LTO processing in the linking stage to link the intermediate representations of each source file at the logical level to form an intermediate file that can express the global intermediate representation of the program, so as to subsequently optimize the global redundant operations of the program at the level of the intermediate file.
[0074] In an optional implementation example, during the linking phase of program compilation, the embodiment of the present application can link the intermediate representations of various source files into an intermediate file according to the structural logic of the program, and ensure the consistency of global symbol definitions and the correctness of function calls.
[0075] Optionally, different source files may have different local symbol definitions and global symbol definitions (such as function definitions, global variable definitions, etc.). Therefore, when linking the intermediate representations of various source files into an intermediate file, it is necessary to ensure the consistency of the global symbol definitions in the intermediate representations. Based on this, the embodiments of the present application can unify the global symbol definitions in the intermediate representations of various source files to avoid naming conflicts.
[0076] Optionally, the embodiment of the present application can adjust the function reference in the intermediate representation (for example, adjust the address or identifier of the function reference), so as to ensure the correctness of the function reference in the intermediate file when linking the intermediate representations of each source file into an intermediate file, that is, it is necessary to ensure in the intermediate file that the call of the function can correctly point to the implementation of the function. For example, when function A1 calls function A2, if function A1 and function A2 are located in different source files, then when linking the intermediate representations of different source files to the intermediate file, it is necessary to modify and adjust the function reference in the intermediate representation of the source file in the intermediate file to ensure that the call of function A1 in the intermediate file can correctly point to the implementation of function A2.
[0077] For example, Figure 4 The example diagram of forming the intermediate file is shown as an example, which can be used for reference. Figure 4As shown, taking two source files (source file 1 and source file 2) of a program as an example, the compiler can determine the intermediate representation 11 of source file 1 (for example, source file 1 is compiled into intermediate representation 11) and the intermediate representation 21 of source file 2 (for example, source file 2 is compiled into intermediate representation 21) during the compilation stage; thus, during the linking stage, the compiler (specifically, the linker or LTO tool in the compiler) can obtain the intermediate representation 11 and the intermediate representation 21, and link them into intermediate files. For example, each function and global variable of the intermediate representation 11 and the intermediate representation 21 is identified as an independent entity; then, symbol resolution (for example, symbol resolution of function calls) is performed, for example, Func2 is called in source file 1, and the definition of Func2 is in source file 2, then in the intermediate file formed by the link, it is necessary to ensure that the call of Func2 in source file 1 can correctly point to the definition of Func2 in source file 2, so it is necessary to process this cross-source file symbol resolution; then, a global intermediate file containing Func1, Func2, and Func3 is constructed, and all function calls and variable accesses in the intermediate file are redirected to the correct definition.
[0078] In step S230, redundant operations in the intermediate file are determined.
[0079] After forming the intermediate file, the embodiment of the present application can identify redundant operations in the intermediate file. Since the intermediate file is an intermediate representation of the entire program, identifying redundant operations in the intermediate file can enable redundant operation detection for the entire program. For example, redundant operations in the intermediate file can be regarded as redundant operations in the program logic represented by the intermediate file.
[0080] In an optional implementation, the embodiment of the present application can perform IPO (Interprocedural Optimization) processing on the intermediate file, so as to perform redundant operation detection on at least the data flow path corresponding to the intermediate file, so as to determine the redundant operations in the intermediate file.
[0081] In an optional implementation, an embodiment of the present application can determine the data flow information corresponding to the intermediate file, wherein the data flow information can indicate data operations, and data dependencies between data operations; thereby, based on the data flow path of the data flow information, an embodiment of the present application can detect whether the data operations on the data flow path are redundant operations, so as to determine the redundant operations in the intermediate file; wherein the data flow path can indicate data operations with data dependencies.
[0082] As an optional implementation, the data flow information may be graphical information, for example, the data flow information may be in the form of a data flow graph (DFG), so that the data flow graph may visualize the data dependencies of the program. Figure 5 An optional flowchart for determining redundant operations in an intermediate file provided in an embodiment of the present application is shown as an example. Figure 5 As shown, the process may include the following steps.
[0083] In step S510, data operations in the intermediate file and data dependencies between the data operations are determined.
[0084] Data operations include operations that process data, such as assigning values to data, calculating data, or calling functions. Data dependencies between data operations refer to the use of the result of one data operation by another data operation, for example, the result of one data operation is used as the input of another operation. Since the intermediate file is an intermediate representation of the entire program, the embodiments of the present application can determine data operations and data dependencies between data operations at the level of the intermediate file, so as to globally determine data operations in the program and data dependencies between data operations.
[0085] In an optional implementation, the data operation may include a variable definition (def) operation and a variable use (use) operation. The variable definition operation may be an operation in which a variable is assigned a value, and the variable use operation may be an operation in which a variable is referenced or read. Thus, the dependency relationship between the variable definition operation and the use operation may be a data dependency relationship, such as a variable definition (def) and use (use) chain may be a dependency relationship between the variable definition operation and the use operation.
[0086] In step S520, a data flow graph is constructed by using nodes in the data flow graph to represent data operations and edges in the data flow graph to represent data dependencies between data operations.
[0087] As an optional implementation, the data flow graph may include nodes and edges (directed edges); wherein a node may represent a data operation, such as data operations such as assignment, calculation, or function call; an edge may represent a data dependency relationship between data operations, for example, the data dependency relationship between data operations may be converted into a directed edge between nodes corresponding to the data operations, and the direction of the edge expresses the data dependency direction. Thus, based on the data operations determined from the intermediate file and the data dependency relationship between the data operations, the embodiment of the present application may obtain a data flow graph corresponding to the intermediate file, that is, the nodes in the data flow graph represent the data operations, and the edges in the data flow graph represent the data dependency relationship between the nodes corresponding to the data operations, so as to display the data dependency relationship between the data operations in a graphical manner.
[0088] In an optional implementation, data operations may include definition operations and use operations of variables, so that the embodiment of the present application can determine a data flow graph based on the definition operations and use operations of variables; for example, the relationship between the definition operations and use operations of variables can form a definition (def) and use (use) chain of variables, so that the definition operations and use operations of variables can be linked in the data flow graph based on the definition and use chains of variables to obtain a data flow graph. For example, the embodiment of the present application can establish nodes corresponding to the definition operations and use operations of variables in the data flow graph, so that according to the definition and use chains of variables (i.e., the relationship between the definition and use of variables), the nodes corresponding to the definition operations of the variables and the nodes corresponding to the use operations are linked using directed edges to generate a data flow graph. For example, operation B1 defines variable X, and operation B2 uses variable X, then operation B1 and operation B2 can be represented as nodes in the data flow graph, and a directed edge is established between the nodes of operation B1 and the nodes of operation B2, indicating that data flows from operation B1 to operation B2.
[0089] In an alternative implementation example, combining Figure 4 As shown, BB1 is a data loading operation, that is, loading the i-th data element A[i] of array A. Then, as a data operation, BB1 can be represented as a node in the data flow graph; BB2 is a data calculation operation, that is, BB2 calculates R=A[i]+C, C can be a constant, and then assigns R to the i-th data element B[i] of array B. Then, as a data operation, BB2 can be represented as a node in the data flow graph; since BB2 uses A[i] loaded by BB1, BB2 depends on the result of BB1, so a directed edge from the node of BB1 to the node of BB2 can be constructed, indicating that the value of A[i] is loaded and used for subsequent BB2 calculations.
[0090] In step S530, it is detected whether the data operation on the data flow path of the data flow graph is a redundant operation to determine the redundant operation in the intermediate file.
[0091] As an optional implementation, in a data flow graph, data operations with data dependencies can form a data flow path. For example, a directed sequence of variable definition operations and use operations can form a data flow path, that is, a data flow path can be a tracking route of a variable from definition to use.
[0092] For example, taking a data flow graph as an example, the starting point (starting node) of a data flow path can be the node corresponding to the definition operation of the variable, that is, the node where the variable is set to an initial value; the end point (terminating node) of the data flow path can be the node corresponding to the use operation of the variable; and there may or may not be intermediate nodes between the starting point and the end point of the data flow path, and the intermediate nodes can represent the data operations that the variable may undergo between the starting point (definition) and the end point (use).
[0093] In an optional implementation, the embodiment of the present application can perform redundant operation detection on the data operation on the data flow path of the data flow information (such as a data flow graph), that is, perform redundant operation detection on the data operation involved in the data flow path to achieve the determination of redundant operations in the intermediate file. Redundant operations are operations that do not affect the program results, and the data operations that do not affect the program results on the data flow path can be used as redundant operations. For example, for any data operation on the data flow path, if the data operation does not affect the program results, or the data operation is performed but the result of the data operation is not used for subsequent data operations, the data operation is considered to be a redundant operation.
[0094] In an optional implementation example, for any data operation on the data flow path, the embodiment of the present application can use operation flow analysis technology (such as forward and backward propagation algorithms) to determine the impact of the data operation. If the data operation does not affect the results of any other data operations, the data operation can be determined to be a redundant operation.
[0095] In a further optional implementation, the embodiment of the present application can perform redundant operation detection on data operations on a key data flow path of data flow information (e.g., a data flow graph). The key data flow path can be a data flow path with the longest execution time, or a data flow path with the greatest impact on the program result (i.e., the data flow path that is most important to the program result); thus, the embodiment of the present application can perform redundant operation detection on data operations on the key data flow path.
[0096] Back to Figure 2 As shown, in step S240, redundant operations in the intermediate file are optimized.
[0097] After determining the redundant operations in the intermediate file, the embodiment of the present application can optimize the redundant operations in the intermediate file (for example, eliminate the redundant operations of the intermediate file). Since the intermediate file is the intermediate representation of the program as a whole, the redundant operations are optimized at the level of the intermediate file, which can achieve the optimization of the redundant operations of the program as a whole, so that the redundant operations in the program do not participate in the calculation, reducing the invalid calculations in the program. As an optional implementation, after determining the redundant operations, the embodiment of the present application can mark the determined redundant operations as optimization items of the LTO technology in the linking stage, so as to eliminate the determined redundant operations in the linking stage.
[0098] In a further optional implementation, the intermediate file after optimizing redundant operations (i.e., the global intermediate representation of the program after optimizing redundant operations) can be converted into machine code, and a compiled executable program can be obtained based on the converted machine code. In an optional implementation, the source code and the machine code can be bridged based on the intermediate representation, so the intermediate file after optimizing redundant operations can be converted into machine code, and then the compiled executable program can be obtained. For example, the intermediate file after optimizing redundant operations can be converted into machine code and linked with necessary system libraries and runtime components to generate an executable program. Since the machine code of the executable program is generated based on the intermediate file after optimizing redundant operations, and the intermediate file eliminates and optimizes a large number of redundant operations during the program compilation process, the embodiment of the present application can provide an executable program with a large number of redundant operations eliminated and optimized, thereby improving the execution performance of the executable program and achieving improved program processing performance.
[0099] The redundant operation optimization method provided by the embodiment of the present application can obtain the intermediate representations corresponding to the multiple source files of the program respectively during the compilation phase of the program compilation, wherein the intermediate representation of the source file at least records the semantic structure information of the source file; during the linking phase of the program compilation, the intermediate representations of the various source files are linked into an intermediate file; and then the redundant operations in the intermediate file are determined; and the redundant operations of the intermediate file are optimized to achieve the optimization of the redundant operations during the linking phase of the program compilation. It can be seen that the redundant operation optimization method provided by the embodiment of the present application can optimize the redundant operations during the program compilation process, and is applicable to the program compilation process; and then, after the program compilation is completed, based on the redundant operations optimized during the program compilation process, the embodiment of the present application can provide an executable program with a large number of redundant operations optimized, thereby improving the execution performance of the executable program and achieving the improvement of the program processing performance.
[0100] In an optional implementation, an embodiment of the present application can optimize redundant operations for a sparse data structure, wherein most of the data elements in the sparse data structure are zero values (indicating that they carry no information) and a small number of data elements are non-zero values (indicating valid information), so that the calculation operations of the zero-valued data elements in the sparse data structure can be regarded as redundant operations.
[0101] In an optional implementation example, if multiple data points of a variable (one data point can represent one data element) are stored in a continuous memory space range, and the value of most of the data points is zero (for example, the data elements with zero values are more than the data elements with non-zero values), the data structure corresponding to the variable can be regarded as a sparse data structure. In other words, the data structure corresponding to the zero value stored in the continuous memory space range of the same variable is a sparse data structure, in which the data elements with zero values are more than the data elements with non-zero values. An application example of the redundant operation optimization scheme provided in an embodiment of the present application can be to optimize the redundant operation of the data elements with zero values in such a sparse data structure. For example, for data processing tasks (such as scientific computing tasks, machine learning tasks, etc.) that process the above-mentioned sparse data structure, the embodiment of the present application can optimize the calculation operations involved in the data elements with zero values in the above-mentioned sparse data structure during program compilation, thereby improving the calculation speed of the data processing task.
[0102] For sparse data structures, redundant operations of sparse data structures mainly refer to computational operations that unnecessarily process zero values in sparse data structures. Thus, for programs that process the above sparse data structures, embodiments of the present application can detect and optimize redundant operations of sparse data structures at the level of intermediate files (i.e., the level of the global intermediate representation of the program) during the process of compiling the program using the solution provided by embodiments of the present application.
[0103] In an optional implementation, the embodiment of the present application can perform redundant operation detection on the calculation operation of the data elements of the sparse data structure in the intermediate file when determining the redundant operation in the intermediate file, so as to determine the calculation operation of the data elements of zero value in the sparse data structure as a redundant operation. For example, the embodiment of the present application can determine whether the data operation on the data flow path is to calculate the data elements of zero value in the sparse data structure. If the data operation on the data flow path is to calculate the data elements of zero value in the sparse data structure, it can be determined that the data operation is a redundant operation and needs to be optimized.
[0104] As an alternative implementation, sparse data structures with redundant operations can be further classified, including:
[0105] Irregular redundant sparse vector, irregular redundant sparse vector is a sparse data structure in the form of a one-dimensional array. For example, when the variable of the irregular redundant sparse vector is initialized or used, the memory is accessed in the form of a one-dimensional array, that is, the irregular redundant sparse vector is stored and accessed in the memory as a one-dimensional array; in the irregular redundant sparse vector, most of the data elements are zero values (for example, the number of zero value data elements is more than the number of non-zero value data elements), and the position of the non-zero value data elements has no fixed pattern (for example, the position of the zero value data element and the position of the non-zero value data element are staggered and there is no pattern); for example, Fig. 6A An example diagram of an irregular redundant sparse vector is shown exemplarily, which can be used for reference;
[0106] Irregular redundant sparse matrix, irregular redundant sparse matrix is a sparse data structure in the form of a two-dimensional matrix, that is, the data elements are organized in a two-dimensional manner (such as rows and columns). For example, when the variables of the irregular redundant sparse matrix are initialized or used, the memory is accessed in a two-dimensional matrix manner (such as rows and columns). That is to say, the irregular redundant sparse matrix is stored and accessed in the memory as a two-dimensional matrix; it should be noted that the one-dimensional value is named in a vector manner, and the two-dimensional matrix is commanded in a matrix manner; in an irregular redundant sparse matrix, most of the data elements are zero values (for example, the number of zero value data elements is more than the number of non-zero value data elements), and the position of the non-zero value data elements has no fixed pattern (for example, the position of the zero value data element and the position of the non-zero value data element are staggered and there is no pattern); for example, Figure 6B An example diagram of an irregular redundant sparse matrix is shown exemplarily, which can be used for reference;
[0107] A regular redundant sparse diagonal matrix is a sparse data structure in the form of a two-dimensional matrix, in which most data elements are zero values (for example, there are more zero-valued data elements than non-zero-valued data elements), and the non-zero-valued data elements are located on the matrix diagonal (that is, the data elements on the matrix diagonal are valid values), and the data elements at other positions of the matrix are all zero values (data elements on non-matrix diagonals are invalid values); for example, Figure 6C An example diagram of a regular redundant sparse diagonal matrix is shown exemplarily, which can be used for reference.
[0108] The above irregular redundant sparse vectors, irregular redundant sparse matrices and regular redundant sparse diagonal matrices contain a large number of zero values, which do not play a role in the calculation, that is, they do not affect the program results but occupy computing resources and memory. Therefore, the above irregular redundant sparse vectors, irregular redundant sparse matrices and regular redundant sparse diagonal matrices are sparse data structures with redundant operations.
[0109] As an optional implementation, an embodiment of the present application can determine, for any data operation on the data flow path, whether the data operation is a calculation of zero-valued data elements of any item in an irregular redundant sparse vector, an irregular redundant sparse matrix, and a regular redundant sparse diagonal matrix. If so, it can be determined that the data operation is a redundant operation and needs to be optimized.
[0110] In a further optional implementation, for sparse data structures, embodiments of the present application may optimize the computational operations of data elements with zero values in the sparse data structure, thereby optimizing redundant operations; for example, for computational operations of data elements of a sparse data structure, embodiments of the present application may insert optimization instructions so that data elements with non-zero values in the sparse data structure participate in the computational operations, and data elements with zero values in the sparse data structure do not participate in the computational operations.
[0111] For different types of sparse data structures, the embodiments of the present application can perform different optimization processes. As an example, taking irregular redundant sparse vectors, irregular redundant sparse matrices and regular redundant sparse diagonal matrices as examples, the optional implementation of optimizing redundant operations is described below.
[0112] In an optional implementation, for an irregular redundant sparse vector or an irregular redundant sparse matrix, before performing a calculation operation on the data elements of the irregular redundant sparse vector or the irregular redundant sparse matrix, an embodiment of the present application may check each data element of the irregular redundant sparse vector or the irregular redundant sparse matrix to determine whether the data element is a non-zero value; thereby, for data elements with zero values, the calculation of the data elements with zero values is skipped to avoid invalid calculations on zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix.
[0113] Based on this, as an optional implementation, an embodiment of the present application can check the data elements with non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix by inserting a conditional statement before performing the calculation operation on the data elements of the irregular redundant sparse vector or the irregular redundant sparse matrix; thereby, based on the check result, the calculation operation of the data elements with zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix is canceled, and the calculation operation of the data elements with non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix is continued to avoid invalid calculations on the zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix.
[0114] For example, the conditional statement may be, for example, if (element!=0), where element represents a data element in an irregular redundant sparse vector or an irregular redundant sparse matrix, and element!=0 represents that the value of the data element is not equal to 0; thus, through the above-mentioned conditional statement of the example, it is possible to check whether the value of the data element in the irregular redundant sparse vector or the irregular redundant sparse matrix is a non-zero value, so that the data elements with non-zero values can participate in the calculation operation, while the data elements with zero values do not participate in the calculation operation.
[0115] In a further example, Fig. 7A An example diagram showing the optimization of irregular redundant sparse vectors is shown in FIG. Fig. 7A In this example, A represents an irregular redundant sparse vector, A[i] is the i-th data element in the irregular redundant sparse vector, and insert Fig. 7A In the conditional statement of the example, if the i-th data element is not 0, that is, A[i] != 0, then the calculation operation of A[i] is continued, and A[i] is added with constant C and assigned to the i-th data element B[i] of vector B.
[0116] Figure 7B An example diagram showing the optimization of an irregular redundant sparse matrix is shown in FIG. Figure 7B In this example, A represents an irregular redundant sparse matrix, A[i][j] is the data element in the i-th row and j-th column of the irregular redundant sparse matrix, and Figure 7B In the conditional statement of the example, if the data element in the i-th row and j-th column is not 0, that is, A[i][j] != 0, then the calculation operation of A[i][j] is continued, and A[i][j] is added with the constant C and assigned to the data element B[i][j] in the i-th row and j-th column of matrix B.
[0117] In an optional implementation, for a regular redundant sparse diagonal matrix, the data elements on the matrix diagonal are non-zero values (i.e., valid values), while the data elements on the non-matrix diagonal are zero values (i.e., invalid values), so the embodiment of the present application can adjust the data element access method of the regular redundant sparse diagonal matrix, so that only the data elements on the matrix diagonal are accessed and participate in the calculation operation. Based on this, as an optional implementation, the embodiment of the present application can change the access strategy of the regular redundant sparse diagonal matrix, so that when accessing the regular redundant sparse diagonal matrix to perform calculation operations using the data elements of the regular redundant sparse diagonal matrix, the embodiment of the present application can convert the access to the regular redundant sparse diagonal matrix into the access to the matrix diagonal, thereby achieving that the data elements with non-zero values on the matrix diagonal are accessed and participate in the calculation operation, while the data elements with zero values on the non-matrix diagonal are not accessed and do not participate in the calculation operation, thereby eliminating the invalid calculation involved in the regular redundant sparse diagonal matrix.
[0118] For example, Figure 7C An example diagram of optimizing a regular redundant sparse diagonal matrix is shown in FIG. Figure 7C In the example, A[i] is the i-th data element on the matrix diagonal of the regular redundant sparse diagonal matrix. When accessing the regular redundant sparse diagonal matrix, access conversion is performed to access the data elements on the matrix diagonal of the regular redundant sparse diagonal matrix. The accessed data elements on the matrix diagonal can form the corresponding vectors of the matrix diagonal, for example Figure 7C A in the example can represent the vector corresponding to the matrix diagonal. For the i-th data element A[i] on the matrix diagonal obtained by access, Figure 7C The calculation operation of A[i] in the example is to add constant C to A[i] and assign it to the i-th data element B[i] of vector B.
[0119] In a further optional implementation, in the case where the program processes a sparse data structure, the embodiment of the present application can determine the number of redundant operations and the number of non-redundant operations of the sparse data structure at the level of the intermediate file during the program compilation process, thereby determining the redundancy factor based on the number of redundant operations and the number of non-redundant operations; the redundancy factor can be used to measure the potential performance improvement of the program (i.e., the performance benefit of the program) after eliminating the redundant operations of the sparse data structure. For example, the redundancy factor can be defined as the number of redundant operations of the sparse data structure divided by the number of non-redundant operations.
[0120] For example, when the vector or matrix processed by the program has a sparse characteristic, such as when the program processes any sparse data structure with redundant operations such as irregular redundant sparse vectors, irregular redundant sparse matrices and regular redundant sparse diagonal matrices, eliminating redundant operations in the sparse data structure can bring a large performance improvement to the program. At this time, a redundancy factor can be defined to measure the potential performance improvement of the program after eliminating the redundant operations of the sparse data structure. For example, the redundancy factor can be defined as: the number of redundant operations corresponding to the sparse data structure (for example, the number of redundant operations corresponding to the sparse data structure in the intermediate file), divided by the number of non-redundant operations (for example, the number of non-redundant operations corresponding to the sparse data structure in the intermediate file).
[0121] In an optional implementation, the embodiments of the present application can measure the program performance benefits of optimizing redundant operations through a redundancy factor. For example, when the redundancy factor is greater than a set redundancy factor (such as 1), eliminating redundant operations of the sparse data structure can bring performance benefits to the program; taking the setting of the redundancy factor to 1 as an example, if the redundancy factor is greater than 1, it means that the number of redundant operations of the sparse data structure exceeds the number of non-redundant operations, which indicates that by optimizing redundant operations (such as skipping calculation operations of zero values in the sparse data structure), a significant performance improvement of the program can be obtained.
[0122] The embodiment of the present application can compile and obtain the intermediate representation of each source file of the program during the compilation phase of program compilation; in the linking phase of program compilation, the intermediate representations of each source file are linked into an intermediate file to obtain the intermediate representation of the program as a whole; thus, redundant operation detection and optimization can be performed at the level of the intermediate file (i.e., the level of the intermediate representation of the program as a whole), and redundant operation elimination optimization can be performed during program compilation, so that the redundant operations of the executable program that is finally compiled can be eliminated and optimized in large quantities, thereby improving the processing performance of the program. Further, redundant operation optimization is performed for sparse data structures (for example, the calculation operation of the data elements with zero values in irregular redundant sparse vectors, irregular redundant sparse matrices, and regular redundant sparse diagonal matrices is optimized), and when the vectors or matrices processed by the program have sparse characteristics, the redundant operations of the sparse data structure can be eliminated to bring greater performance benefits to the program.
[0123] The embodiment of the present application also provides a redundant operation optimization device, which can be regarded as a functional module required for the compiler to implement the redundant operation optimization method provided in the embodiment of the present application. The content described below can be referenced to the content described above.
[0124] As an optional implementation, Figure 8 The optional block diagram of the redundant operation optimization device provided by the present application is exemplarily shown. The redundant operation optimization device can be applied to a compiler, such as Figure 8 As shown, the redundant operation optimization device may include:
[0125] The compilation optimization module 810 is used to obtain intermediate representations corresponding to multiple source files of the program during the compilation phase of the program, wherein the intermediate representations of the source files at least record the semantic structure information of the source files;
[0126] The link optimization module 820 is used to link the intermediate representations of various source files into an intermediate file during the link phase of program compilation; determine the redundant operations in the intermediate file; and optimize the redundant operations in the intermediate file.
[0127] In an optional implementation, the compilation optimization module 810 is used to obtain intermediate representations corresponding to multiple source files of the program during the compilation phase of the program compilation, including:
[0128] During the compilation phase of program compilation, for any source file of the program, a syntax tree of the source file is determined; at least the syntax tree of the source file is converted into an intermediate representation of the source file.
[0129] In an optional implementation, the compilation optimization module 810, for converting at least the syntax tree of the source file into an intermediate representation of the source file, comprises:
[0130] Generate corresponding intermediate code for each node in the syntax tree of the source file; construct the control flow information of the source file and the data flow information of the source file, and combine the intermediate code corresponding to the node of the syntax tree of the source file, the control flow information of the source file and the data flow information of the source file to form an intermediate representation of the source file.
[0131] In an optional implementation, the link optimization module 820 is used to link the intermediate representations of each source file into an intermediate file during the link phase of program compilation, including:
[0132] During the linking phase of program compilation, link-time optimization is used to logically link the intermediate representations of each source file to form an intermediate file.
[0133] In an optional implementation, the link optimization module 820 is used to use link-time optimization processing during the link phase of program compilation to logically link the intermediate representations of various source files to form an intermediate file including:
[0134] During the linking phase of program compilation, the intermediate representations of each source file are linked to an intermediate file according to the structural logic of the program, and the global symbol definitions in the intermediate representation are unified, and the function references in the intermediate representation are adjusted to ensure that the function call in the intermediate file correctly points to the function implementation.
[0135] In an optional implementation, the link optimization module 820 is configured to determine redundant operations in the intermediate file, including:
[0136] Inter-process optimization processing is performed on the intermediate file, and redundant operation detection is performed at least on the data flow path corresponding to the intermediate file to determine the redundant operations in the intermediate file; wherein the data flow path indicates data operations with data dependencies.
[0137] In an optional implementation, the link optimization module 820 is used to perform inter-process optimization processing on the intermediate file, and at least perform redundant operation detection on the data flow path corresponding to the intermediate file, including:
[0138] Determine the data operations in the intermediate files and the data dependencies between the data operations;
[0139] The data flow graph is constructed by using nodes in the data flow graph to represent data operations and edges in the data flow graph to represent data dependencies between data operations;
[0140] Whether data operations on a data flow path of a data flow graph are redundant operations is detected to determine the redundant operations in the intermediate file.
[0141] In an optional implementation, the link optimization module 820 is used to determine the data operations in the intermediate file and the data dependencies between the data operations, including:
[0142] Determine the definition operation and use operation of the variable in the intermediate file, as well as the definition and use chain of the variable, wherein the definition and use chain of the variable is the dependency relationship between the definition operation and the use operation of the variable.
[0143] In an optional implementation, the link optimization module 820 is used to represent data operations with nodes in a data flow graph and to represent data dependencies between data operations with edges in the data flow graph. Constructing the data flow graph includes:
[0144] Establish nodes corresponding to the definition operation and use operation of variables in the data flow graph;
[0145] According to the definition and usage chain of the variable, the nodes corresponding to the definition operation of the variable and the nodes corresponding to the usage operation are linked using directed edges.
[0146] In an optional implementation, the above-mentioned data flow path may include a critical data flow path; the critical data flow path includes a data flow path with the longest execution time, or a data flow path with the greatest impact on program results.
[0147] In an optional implementation, the redundant operations determined in the intermediate file may include: redundant operations on a sparse data structure; the sparse data structure is a data structure corresponding to zero values stored in a continuous memory space for the same variable, and the number of zero-valued data elements is greater than the number of non-zero-valued data elements.
[0148] In an optional implementation, the redundant operation of the sparse data structure may include: a calculation operation of a data element with a zero value in the sparse data structure. Accordingly, the link optimization module 820 for optimizing the redundant operation of the intermediate file may include:
[0149] For the calculation operation of the data elements of the sparse data structure, the optimization instruction is inserted so that the data elements with non-zero values in the sparse data structure participate in the calculation operation and the data elements with zero values in the sparse data structure do not participate in the calculation operation.
[0150] In an optional implementation, the sparse data structure may include any of the following:
[0151] An irregular redundant sparse vector, wherein the irregular redundant sparse vector is a sparse data structure in the form of a one-dimensional array, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the positions of the non-zero-valued data elements have no fixed pattern;
[0152] An irregular redundant sparse matrix, wherein the irregular redundant sparse matrix is a sparse data structure in the form of a two-dimensional matrix, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the positions of the non-zero-valued data elements have no fixed pattern;
[0153] A regular redundant sparse diagonal matrix, wherein the regular redundant sparse diagonal matrix is a sparse data structure in the form of a two-dimensional matrix, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the non-zero-valued data elements are located on the matrix diagonal, while the data elements on the non-matrix diagonal are zero-valued.
[0154] In an optional implementation, the link optimization module 820, for inserting optimization instructions for computing operations of data elements of the sparse data structure so that data elements with non-zero values in the sparse data structure participate in the computing operations and data elements with zero values in the sparse data structure do not participate in the computing operations, may include:
[0155] Before performing a calculation operation on the data elements of the irregular redundant sparse vector or the irregular redundant sparse matrix, checking the data elements of non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix by inserting a conditional statement; based on the checking result, canceling the calculation operation of the data elements of zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix, and continuing the calculation operation of the data elements of non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix;
[0156] and / or,
[0157] When accessing a regular redundant sparse diagonal matrix to perform a computing operation using data elements of the regular redundant sparse diagonal matrix, the access to the regular redundant sparse diagonal matrix is converted into an access to the matrix diagonal so that data elements with non-zero values on the matrix diagonal participate in the computing operation.
[0158] In a further optional implementation, the link optimization module 820 can also be used to determine the number of redundant operations and the number of non-redundant operations of the sparse data structure; determine the redundancy factor based on the number of redundant operations and the number of non-redundant operations, and the redundancy factor represents the program performance benefit of eliminating redundant operations.
[0159] In a further optional implementation, the link optimization module 820 may also be used to convert the intermediate file of the optimized redundant operation into a machine code, and obtain a compiled executable program based on the converted machine code.
[0160] In a further optional implementation, the embodiment of the present application also provides a compiler, which may include the redundant operation optimization device provided in the embodiment of the present application.
[0161] In a further optional implementation, the embodiment of the present application also provides a compilation device, such as a computer device on which compilation software is installed, the compilation software being, for example, a compiler, and the compilation device may include: at least one processor and at least one memory; the memory stores instructions of the compiler program, and the processor calls the instructions of the compiler program to execute the redundant operation optimization method provided in the embodiment of the present application.
[0162] In a further optional implementation, the embodiment of the present application also provides a storage medium, which stores instructions of a compiler. When the instructions of the compiler are executed (for example, when executed by a processor), the redundant operation optimization method provided by the embodiment of the present application is implemented.
[0163] In a further optional implementation, the embodiment of the present application also provides a computer program product, such as a compiler product (such as compiler software), which includes compiler instructions. When the compiler instructions are executed (for example, when executed by a processor), the redundant operation optimization method provided by the embodiment of the present application is implemented.
[0164] The above describes multiple implementation schemes provided by the embodiments of the present application. The various optional methods introduced in each implementation scheme can be combined and cross-referenced with each other without conflict, thereby extending a variety of possible implementation schemes, which can all be considered as implementation schemes disclosed and open in the embodiments of the present application.
[0165] Although the embodiments of the present application are disclosed above, the present application is not limited thereto. Any person skilled in the art may make various changes and modifications without departing from the spirit and scope of the present application. Therefore, the scope of protection of the present application shall be subject to the scope defined by the claims.
Claims
1. A redundant operation optimization method, It is characterized in that include: In the compilation phase of the program, intermediate representations corresponding to the plurality of source files of the program are obtained, wherein the intermediate representations of the source files at least record the semantic structure information of the source files; In the linking phase of program compilation, the intermediate representations of the various source files are linked into an intermediate file; the redundant operations in the intermediate file are determined; and the redundant operations in the intermediate file are optimized.
2. The method according to claim 1, It is characterized in that In the compilation phase of the program, obtaining the intermediate representations corresponding to the plurality of source files of the program includes: During the compilation phase of program compilation, for any source file of the program, a syntax tree of the source file is determined; at least the syntax tree of the source file is converted into an intermediate representation of the source file.
3. The method according to claim 2, It is characterized in that The converting of at least the syntax tree of the source file into an intermediate representation of the source file comprises: Generate corresponding intermediate code for each node in the syntax tree of the source file; construct the control flow information of the source file and the data flow information of the source file, and combine the corresponding intermediate code of the node of the syntax tree of the source file, the control flow information of the source file and the data flow information of the source file to form an intermediate representation of the source file.
4. The method according to any one of claims 1 to 3, It is characterized in that In the linking stage of program compilation, linking the intermediate representations of the source files into intermediate files includes: During the linking phase of program compilation, link-time optimization is used to logically link the intermediate representations of the various source files to form intermediate files.
5. The method according to claim 4, It is characterized in that In the linking stage of program compilation, link-time optimization is used to logically link the intermediate representations of the source files to form intermediate files including: During the linking phase of program compilation, the intermediate representations of each source file are linked to an intermediate file according to the structural logic of the program, and the global symbol definitions in the intermediate representation are unified, and the function references in the intermediate representation are adjusted to ensure that the function call in the intermediate file correctly points to the function implementation.
6. The method according to claim 1, It is characterized in that Determining the redundant operations in the intermediate file includes: Inter-process optimization processing is performed on the intermediate file, and redundant operation detection is performed at least on the data flow path corresponding to the intermediate file to determine the redundant operations in the intermediate file; wherein the data flow path indicates data operations with data dependencies.
7. The method according to claim 6, It is characterized in that The inter-process optimization processing of the intermediate file and the redundant operation detection at least on the data flow path corresponding to the intermediate file include: Determine the data operations in the intermediate files and the data dependencies between the data operations; The data flow graph is constructed by using nodes in the data flow graph to represent data operations and edges in the data flow graph to represent data dependencies between data operations; Whether data operations on a data flow path of a data flow graph are redundant operations is detected to determine the redundant operations in the intermediate file.
8. The method according to claim 7, It is characterized in that Determining the data operations in the intermediate file and the data dependencies between the data operations includes: Determine definition operations and use operations of variables in the intermediate file, as well as definition and use chains of the variables, wherein the definition and use chains of the variables are dependencies between definition operations and use operations of the variables; The nodes in the data flow graph represent data operations, and the edges in the data flow graph represent data dependencies between data operations. Constructing the data flow graph includes: Establish nodes corresponding to the definition operation and use operation of variables in the data flow graph; According to the definition and usage chain of the variable, the nodes corresponding to the definition operation of the variable and the nodes corresponding to the usage operation are linked using directed edges.
9. The method according to claim 7, It is characterized in that The data flow paths include critical data flow paths; the critical data flow paths include data flow paths that take the longest time to execute, or data flow paths that have the greatest impact on program results.
10. The method according to claim 1, It is characterized in that The redundant operation includes: redundant operation of sparse data structure; the sparse data structure is a data structure corresponding to zero values stored in a continuous memory space for the same variable, and the number of zero-valued data elements is greater than the number of non-zero-valued data elements.
11. The method according to claim 10, It is characterized in that The redundant operation of the sparse data structure includes: the calculation operation of the data elements with zero value in the sparse data structure; the optimization of the redundant operation of the intermediate file includes: For the calculation operation of the data elements of the sparse data structure, the optimization instruction is inserted so that the data elements with non-zero values in the sparse data structure participate in the calculation operation and the data elements with zero values in the sparse data structure do not participate in the calculation operation.
12. The method according to claim 11, It is characterized in that The sparse data structure includes any of the following: An irregular redundant sparse vector, wherein the irregular redundant sparse vector is a sparse data structure in the form of a one-dimensional array, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the positions of the non-zero-valued data elements have no fixed pattern; An irregular redundant sparse matrix, wherein the irregular redundant sparse matrix is a sparse data structure in the form of a two-dimensional matrix, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the positions of the non-zero-valued data elements have no fixed pattern; A regular redundant sparse diagonal matrix, wherein the regular redundant sparse diagonal matrix is a sparse data structure in the form of a two-dimensional matrix, wherein the number of zero-valued data elements is greater than the number of non-zero-valued data elements, and the non-zero-valued data elements are located on the matrix diagonal, while the data elements on the non-matrix diagonal are zero-valued.
13. The method according to claim 12, It is characterized in that The step of inserting optimization instructions for computing operations on data elements of the sparse data structure so that data elements with non-zero values in the sparse data structure participate in the computing operations and data elements with zero values in the sparse data structure do not participate in the computing operations comprises: Before performing a calculation operation on the data elements of the irregular redundant sparse vector or the irregular redundant sparse matrix, checking the data elements of non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix by inserting a conditional statement; based on the checking result, canceling the calculation operation of the data elements of zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix, and continuing the calculation operation of the data elements of non-zero values in the irregular redundant sparse vector or the irregular redundant sparse matrix; and / or, When accessing a regular redundant sparse diagonal matrix to perform a computing operation using data elements of the regular redundant sparse diagonal matrix, the access to the regular redundant sparse diagonal matrix is converted into an access to the matrix diagonal so that data elements with non-zero values on the matrix diagonal participate in the computing operation.
14. The method according to any one of claims 10 to 13, It is characterized in that Also includes: Determine the number of redundant operations and the number of non-redundant operations on sparse data structures; A redundancy factor is determined based on the number of redundant operations and the number of non-redundant operations, wherein the redundancy factor represents a program performance gain of eliminating redundant operations.
15. A redundant operation optimization device, It is characterized in that include: A compilation optimization module, used for obtaining intermediate representations corresponding to a plurality of source files of a program respectively during the compilation phase of program compilation, wherein the intermediate representations of the source files at least record semantic structure information of the source files; The link optimization module is used to link the intermediate representations of various source files into intermediate files during the link phase of program compilation; determine the redundant operations in the intermediate files; and optimize the redundant operations in the intermediate files.
16. A compiler, It is characterized in that Includes the redundant operation optimization device as described in claim 15.
17. A compiling device, It is characterized in that It includes at least one processor and at least one memory; the memory stores the instructions of the compiler, and the processor calls the instructions of the compiler to execute the redundant operation optimization method as described in any one of claims 1-14.
18. A storage medium, It is characterized in that The storage medium stores instructions of a compiler, and when the instructions of the compiler are executed, the redundant operation optimization method according to any one of claims 1 to 14 is implemented.
19. A computer program product, It is characterized in that It includes instructions of a compiler, and when the instructions of the compiler are executed, the redundant operation optimization method as described in any one of claims 1 to 14 is implemented.
