A Method, System and Medium for Reducing the Search Space of Compilation Option Sequences
By establishing a compilation option relationship model and using genetic algorithms, the optimization efficiency problem caused by the numerous compiler's compilation options and complex associations is solved, and the low-overhead compilation option sequence optimization is achieved, which is suitable for compilation optimization of large programs.
Patent Information
- Application Number
- CN202510480736.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-17
- Publication Date
- 2025-06-20
- Estimated Expiration
- 2045-04-17
AI Technical Summary
In the prior art, compiler compilation options are numerous and complex associations are caused by low optimization efficiency. Traditional compilation optimization methods are overhead and have poor results, making it difficult to adapt to a variety of program types or platforms.
By establishing a compilation option relationship model, narrowing the search space, reducing the number of iterations of genetic algorithms, filtering the effective compilation option set, building a two-dimensional option matrix and compilation option relationship directed graph, and using depth-first search and genetic algorithm optimization to generate the optimal compilation option sequence.
Significantly reduce compilation optimization overhead, improve compilation option sequence selection efficiency, ensure program performance is optimized, and is suitable for compilation optimization scenarios of large programs.
Smart Images

Figure CN119987785B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of compilation optimization, and particularly relates to a method, system and medium for reducing the search space of compilation option sequences. Background Art
[0002] As an essential tool for software development, the compiler is responsible for converting high-level and intermediate computer languages into executable low-level machine languages, and is fundamental and important. The quality of the compiler's code conversion has an important impact on the final software performance. However, there are multiple technical obstacles in selecting the optimal compilation option sequence for a specific program. With the wide use of large-scale application programs and the diverse development of software and hardware platforms, the problems of excessive overhead and poor effect of traditional compilation optimization methods have become important obstacles to program performance optimization. Existing research shows that the versatility of preset compilation option sequences (such as ) is not good, and it is difficult to adapt to diverse program types or diverse platforms. Summary of the Invention
[0003] The technical problem to be solved by the present invention is: aiming at the above problems of the prior art, to provide a method, system and medium for reducing the search space of compilation option sequences. The present invention aims to solve the problem of low optimization efficiency caused by numerous and complexly related compilation options of the compiler. By establishing a compilation option relationship model, the search space is reduced, the number of iterations of the genetic algorithm is reduced, and while ensuring the optimization of program performance, the compilation optimization overhead is significantly reduced.
[0004] To solve the above technical problems, the technical solution adopted by the present invention is:
[0005] A method for reducing the search space of compilation option sequences, including the following steps:
[0006] S1, start the programs in the single-compilation-option sampling template program set one by one, and obtain the speedup data of the programs in the template program set under each compilation option relative to the preset compilation option sequence ;
[0007] S2, calculate the harmonic mean of the speedup data, and screen out the effective compilation option set ;
[0008] S3, use the effective compilation option set to construct a two-dimensional option matrix, compare the acceleration effects of binary option sequences according to the two-dimensional option matrix, and establish a directed graph of compilation option relationships;
[0009] S4, use depth-first search on the directed graph of compilation option relationships to obtain the set of candidate compilation option sequences ;
[0010] S5, use the genetic algorithm to process the set of candidate compilation option sequences and iterate and optimize the compilation option subsequences in the target program to generate the optimal compilation option sequence with the shortest running time.
[0011] Optionally, in step S1, the speedup data of the template program set under each compilation option relative to the preset compilation option sequence is a matrix , where is the total number of template programs in the template program set, is the number of compilation options of a specific version compiler; any th row and th column element in the matrix represents the speedup of the th program under the optimization of the th compilation option, where
[0012] Optionally, the harmonic mean of the speedup data calculated in step S2 refers to the speedup data composed of a matrix . Calculate the harmonic mean of each column of data in the matrix respectively to represent the optimization effect of the corresponding compilation option on the template program set; and for any th column of harmonic mean , if the optimization effect satisfies , then add the th compilation option to the set of valid compilation options , where .
[0013] Optionally, in step S3, use the set of valid compilation options to construct a two-dimensional option matrix, compare the acceleration effects of binary option sequences according to the two-dimensional option matrix, and establish a directed graph of compilation option relationships, including:
[0014] S3.1, use the set of valid compilation options to construct a two-dimensional option matrix ~ . For any th two-dimensional option matrix , the value of the th row and th column represents the optimization effect of the th program under the optimization of the binary sequence , where is the set of valid compilation options The number of elements in are the a-th and b-th compilation options respectively, ;
[0015] S3.2. For the two-dimensional option matrix of to sum them up to obtain a matrix of size and take the median of all the values in the matrix ; ;
[0016] S3.3. Establish the adjacency matrix of the compilation option relationship graph of . For each value in the matrix , if the condition is satisfied, then set the value at the corresponding position in the adjacency matrix to 1; if the condition is satisfied, then set the value at the corresponding position in the adjacency matrix to 0; the value at the -th row and -th column in the adjacency matrix is 1, indicating that the optimization effect of the binary sequence is acceptable; if it is 0, it means that the optimization effect of the binary sequence is unacceptable; the adjacency matrix is used to define the directed graph of compilation option relationships V , where the vertex set represents the set of compilation options, and the edge set is determined by the directed edges in the adjacency matrix ; if the value at the -th row and -th column in the adjacency matrix is , it means that there is a directed edge from the vertex to the vertex in the directed graph of compilation option relationships , indicating that the optimization effect from the compilation option of the vertex to the compilation option of the vertex
[0017] is acceptable. Optionally, when constructing the two-dimensional option matrix of using the set of valid compilation options to in step S3.1, the construction process of any -th two-dimensional option matrix is as follows: Obtain the -th program in the binary sequence Optimize the speedup ratio relative to the preset compilation option sequence ; Obtain the binary sequence ; Optimize the speedup ratio relative to the preset compilation option sequence Optimize the speedup ratio relative to the preset compilation option sequence ; Obtain the speedup ratio ; Fetch the matrix The value of the th row and th column in the matrix ; Fetch the matrix The value of the th row and th column in the matrix ; When , mark the element in the two-dimensional option matrix at the th row and th column as 1. When , mark the element in the two-dimensional option matrix at the th row and th column as 0. Traverse the two-dimensional option matrix in this way to obtain all values, and finally obtain the th two-dimensional option matrix .
[0018] Optionally, when obtaining the set of candidate compilation option subsequences by applying depth-first search on the compilation option relationship directed graph in step S4 , it includes connecting the searched binary compilation option sequences one by one to obtain the compilation option subsequence , and adding it to the set of candidate compilation option sequences , ensuring that all elements in the set of candidate compilation option sequences are not subsequences of other elements.
[0019] Optionally, when using the genetic algorithm to iteratively optimize the compilation option subsequences in the set of candidate compilation option sequences in step S5 to generate the optimal compilation option sequence with the shortest running time, it includes using the set of candidate compilation option sequences as the initial population of the genetic algorithm, performing iterative optimization on the target program, and finally obtaining the optimal compilation option sequence with the shortest running time.
[0020] In addition, this embodiment also provides a system for reducing the search space of compilation option sequences, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the method for reducing the search space of compilation option sequences.
[0021] In addition, this embodiment further provides a computer-readable storage medium, in which a computer program or instruction is stored. The computer program or instruction is programmed or configured to execute the method of reducing the compilation option sequence search space through a processor.
[0022] In addition, this embodiment also provides a computer program product, including a computer program or instructions, where the computer program or instructions are programmed or configured to execute the method of reducing the compilation option sequence search space through a processor.
[0023] Compared with the prior art, the present invention can mainly achieve the following beneficial effects: the present invention first screens the effective compilation option set and explores the optimization effect of the binary compilation option sequence, determines the influence of the compilation option sequence on continuous optimization, greatly reduces the compilation option search space, and is conducive to obtaining high-quality compilation option subsequences with low overhead. The high-quality subsequence is used as the initial population of the genetic algorithm to ensure the effectiveness of the individuals in the initial population, improve the convergence speed, and achieve the goal of determining the optimal compilation option sequence with low overhead, so that the present invention can effectively solve the problem of low optimization efficiency caused by the large number of compiler compilation options and complex associations, narrow the search space by establishing a compilation option relationship model, and reduce the number of optimization attempts by combining genetic algorithms, while ensuring that the program performance is optimized, the compilation optimization overhead is significantly reduced, which is particularly suitable for the compilation optimization scenario of large programs. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 Schematic diagram of the basic flow of the method of the embodiment of the present invention.
[0025] Figure 2 The directed graph of compilation option relationships expressed by the adjacency matrix obtained in the embodiment of the present invention is Example. DETAILED DESCRIPTION
[0026] In order to enable those skilled in the art to better understand the technical solution of the present invention, the technical solution of the present invention will be further described in detail below in conjunction with the accompanying drawings in the embodiments of the present invention.
[0027] like Figure 1 As shown, the method for reducing the compilation option sequence search space in this embodiment includes the following steps:
[0028] S1, open a single compilation option item by item to sample the programs in the template assembly, and obtain the relative preset compilation option sequence of the programs in the template assembly under each compilation option (like etc.)
[0029] S2, calculate the harmonic mean of the speedup data, and filter out the valid compilation option set based on the harmonic mean ;
[0030] S3. Apply the set of effective compilation options Construct a two-dimensional option matrix, compare the acceleration effects of the binary option sequences according to the two-dimensional option matrix, and establish a directed graph of compilation option relationships;
[0031] S4. Use depth-first search on the directed graph of compilation option relationships to obtain a set of candidate compilation option sequences ;
[0032] S5. Adopt a genetic algorithm for the candidate compilation option sequences in the Iteratively optimize on the target program to generate an optimal compilation option sequence with the shortest running time.
[0033] Taking the Clang / LLVM compiler in version 14.0.0 as an example, this compiler has a total of 134 compilation options (such as -tti, -targetlibinfo, -targetpassconfig, etc.). Among them, -machine-branch-prob will optimize the branch layout of the machine code using branch probability information, and improve the CPU pipeline efficiency by optimizing the branches; -machine-cse is used to eliminate duplicate calculations in the machine code and reuse the calculated results to reduce redundant memory access; when compiling a program, these compilation options optimize the intermediate representation of the program in turn according to the input order to generate an optimized binary program; taking a template program , enabling a single compilation option , obtaining its speedup ratio relative to the expert-prescribed compilation option sequence (such as ) as an example: obtaining the time when running while compiling with ; obtaining the time when running while compiling with ; Enabling a single compilation option relative to is the speedup ratio ; in step S1 of this embodiment, the speedup ratio data of the template program set relative to the preset compilation option sequence under each compilation option is matrix , where is the total number of template programs in the template program set, is the number of compilation options of a specific version compiler; any in the matrix row column element represents the The speedup ratio of a program under the optimization of the compilation option, where .
[0034] In step S2 of this embodiment, the harmonic mean of the speedup ratio data is calculated for the matrix constituting the speedup ratio data. The harmonic mean of each column of data in the matrix is calculated respectively to represent the optimization effect of the corresponding compilation option on the template program set. And for any column, the harmonic mean , if the optimization effect satisfies , then the th compilation option is added to the set of valid compilation options , where .
[0035] In step S3 of this embodiment, the set of valid compilation options is used to construct a two-dimensional option matrix. Comparing the acceleration effects of binary option sequences according to the two-dimensional option matrix and establishing a directed graph of compilation option relationships includes:
[0036] S3.1, using the set of valid compilation options to construct a two-dimensional option matrix ~ . For any two-dimensional option matrix , the value at the th row and th column represents the optimization effect of the th program under the binary sequence optimization, where is the number of elements in the set of valid compilation options , are the a-th and b-th compilation options respectively;
[0037] S3.2, summing the two-dimensional option matrix ~ to obtain a matrix of size . The median of all values in the matrix is taken;
[0038] S3.3, establishing the adjacency matrix of the compilation option relationship graph . For each value of the matrix , if the condition is satisfied , then the value of the corresponding position in the adjacency matrix is set to 1; if the condition is satisfied , then the adjacency matrix the value of the corresponding position is set to 0; in the adjacency matrix the row and column value is 1, representing that the optimization effect of the binary sequence is acceptable; if it is 0, it represents that the optimization effect of the binary sequence is unacceptable; the adjacency matrix is used to define the directed graph of compilation option relationships , where the vertex set V represents the set of compilation options, and the edge set is determined by the directed edges in the adjacency matrix ; if in the adjacency matrix the row and column value , then it means that there is a directed edge from the vertex in the directed graph of compilation option relationships to the vertex , indicating that the optimization effect from the compilation option of the vertex to the compilation option of the vertex is acceptable. Figure 2 is the directed graph of compilation option relationships expressed by the adjacency matrix obtained in this embodiment Example, which shows the dependency relationships of some compilation options. Each node is a compilation option: -assumption-cache-tracker: Assumption cache tracker. This option is used to track and optimize the cache behavior assumed by the compiler. -targetlibinfo: Target library information. This option is used to provide information about the target platform library to help the compiler optimize. -deadargelim: Dead argument elimination. This option is used to remove those arguments that are not used in the function to reduce code size and improve performance. -callsite-splitting: Call site splitting. This option is used to split the code at the call site to improve code locality and performance. -block-freq: Block frequency. This option is used to collect and utilize the execution frequency information of code blocks to guide compiler optimization. -early-cse: Early common subexpression elimination. This option is used to eliminate common subexpressions in the early stage of compilation to reduce code redundancy. -lower-expect: Lower expectation. This option is used to lower the compiler's expectation of the code execution path to improve the accuracy of optimization. -lazy-value-info: Lazy value information. This option is used to delay the collection and use of value information to reduce compilation time. -postdomtree: Postdominance tree. This option is used to construct the postdominance tree, which is a data structure for analyzing the control flow graph. Figure 2 Any directed edge in Figure 2 , such as edge ①, indicates two options: the optimization dependency relationship between the two compilation options -lower-expect and -postdomtree: when the sequence formed by these two compilation options is: <-lower-expect, -postdomtree>, the performance optimization effect is better than any other sequence that these two compilation options may form (for example, <-postdomtree, -lower-expect>, <-lower-expect>, <-postdomtree>).
[0039] In step S3.1 of this embodiment, the effective compilation option set is used to construct the two-dimensional option matrix ~ When constructing the nth two-dimensional option matrix from to , the construction process of any nth two-dimensional option matrix is as follows: Obtain the speedup ratio of the nth program under the binary sequence optimization relative to the preset compilation option sequence ; Obtain the speedup ratio of the binary sequence optimization relative to the preset compilation option sequence ; Take the matrix optimization relative to the preset compilation option sequence ; Take the speedup ratio ; Take the matrix The value of the row and column in ; Take the value of the row and column in ; When , the element in the two-dimensional option matrix at the row and column is marked as 1. When , the element in the two-dimensional option matrix at the row
[0040] In step S4 of this embodiment, when obtaining the set of candidate compilation option subsequences by applying depth-first search on the compilation option relationship directed graph, it includes connecting the searched binary compilation option sequences one by one to obtain the compilation option subsequence , and adding it to the set of candidate compilation option sequences , ensuring that all elements in the set of candidate compilation option sequences are not subsequences of other elements.
[0041] In step S5 of this embodiment, when using the genetic algorithm to iteratively optimize the compilation option subsequences in the set of candidate compilation option sequences to generate the optimal compilation option sequence with the shortest running time on the target program, it includes using the set of candidate compilation option sequences as the initial population of the genetic algorithm, performing iterative optimization on the target program, and finally obtaining the optimal compilation option sequence with the shortest running time. In this embodiment, the crossover probability of the genetic algorithm generally ranges from 0.7 to 0.8, and the mutation probability generally ranges from 0.05 to 0.15), and the number of iterations generally ranges from 800 to 1500. It should be noted that the genetic algorithm is a well-known existing algorithm, so its implementation details will not be elaborated here.
[0042] In summary, the method for reducing the search space of compilation option sequences in this embodiment pre-samples the optimization effects of each compilation option on the template assembly, obtains the relationship diagram of compilation options by comparing with the effects of compilation option sequences on the template program, obtains high-quality compilation option subsequences through depth-first search, uses them as the initial population of the genetic algorithm, and obtains the optimal compilation option sequence after multiple rounds of iterative compilation. Thus, the method in this embodiment can reduce the selection overhead of program compilation option sequences and can make up for the problem of low optimization efficiency caused by numerous and complexly related compilation options of the compiler.
[0043] In addition, this embodiment also provides a system for reducing the search space of compilation option sequences, including a microprocessor and a memory connected to each other, and the microprocessor is programmed or configured to execute the method for reducing the search space of compilation option sequences.
[0044] In addition, this embodiment also provides a computer-readable storage medium, in which a computer program or instruction is stored, and the computer program or instruction is programmed or configured to execute the method for reducing the search space of compilation option sequences through a processor.
[0045] In addition, this embodiment also provides a computer program product, including a computer program or instruction, and the computer program or instruction is programmed or configured to execute the method for reducing the search space of compilation option sequences through a processor.
[0046] Those skilled in the art should understand that the technical solution provided by the present invention can be in the form of a method, a system, or a computer program product. Therefore, the present invention can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can adopt the form of a computer program product implemented on one or more computer-readable storage media (including but not limited to disk memories, CD-ROMs, optical memories, etc.) containing computer-usable program codes. The present invention is described with reference to the flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, and the combination of processes and / or blocks in the flowchart and / or block diagram, can be realized by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate for realizing in the process Figure 1 a process or multiple processes and / or blocks Figure 1a device for the functions specified in one or more boxes. These computer program instructions can also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to work in a specific manner, such that the instructions stored in the computer-readable memory produce a manufactured article including an instruction device that implements the functions specified in the process Figure 1 one process or more processes and / or boxes Figure 1 a device for the functions specified in one or more boxes. These computer program instructions can also be loaded onto a computer or other programmable data processing device, such that a series of operation steps are executed on the computer or other programmable device to produce a computer-implemented process, and thus the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in the process Figure 1 one process or more processes and / or boxes Figure 1 a device for the functions specified in one or more boxes.
[0047] The above is only the preferred embodiment of the present invention, and the protection scope of the present invention is not limited to the above embodiments. All technical solutions falling within the idea of the present invention belong to the protection scope of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the principle of the present invention, several improvements and refinements should also be regarded as within the protection scope of the present invention.
Claims
1. A method for reducing the search space of compilation option sequences, characterized in that: The steps include: S1, open a single compilation option item by item to sample the programs in the template assembly, and obtain the relative preset compilation option sequence of the programs in the template assembly under each compilation option Speedup data of S2, calculate the harmonic mean of the speedup data, and filter out the valid compilation option set based on the harmonic mean ; S3, using a valid set of compilation options Construct a two-dimensional option matrix, compare the acceleration effects of binary option sequences based on the two-dimensional option matrix, and establish a directed graph of compilation option relationships, including: S3.1, Use a valid compilation option set Build The two-dimensional option matrix ~ , any A two-dimensional option matrix Middle Line The column values represent Programs in binary sequences The optimization effect under optimization, where A set of valid compilation options The number of elements in , They are the a and b compilation options respectively. ; S3.2, yes The two-dimensional option matrix ~ Sum and get the size The matrix , take the matrix The median of all values in ; S3.3, Establishment Adjacency matrix of the compilation option relationship graph , for the matrix Each value of , if the condition is met , then the adjacency matrix The value of the corresponding position is set to 1; if the condition is met , then the adjacency matrix The corresponding position value is set to 0; the adjacency matrix Middle Line The column value is 1, representing a binary sequence The optimization effect is acceptable; if it is 0, it means a binary sequence The optimization effect is unacceptable; the adjacency matrix Used to define the directed graph of compilation option relationships , where the vertex set V Represents a set of compilation options, edge set From the adjacency matrix The directed edges in are determined; if the adjacency matrix Middle Line Column value , then it means that from the compilation option relationship directed graph The vertices in To the top There is a directed edge from vertex Compile options to Vertex The optimization effect of the compilation options is acceptable; S4, use depth-first search on the directed graph of compilation option relations to obtain a set of compilation option subsequences to be selected ; S5, using genetic algorithm to select the compilation option sequence set The compile option subsequence in Iterative optimization is performed on the target program to generate the optimal compilation option sequence with the shortest running time.
2. The method for reducing the compilation option sequence search space according to claim 1, characterized in that: In step S1, the template assembly is set relative to the preset compilation option sequence under each compilation option The speedup data is The matrix ,in is the total number of template programs in the template program set, The number of compile options for a particular version of the compiler; the matrix Any OK The column elements represent the The program is in The speedup ratio under the optimization of the compilation options is .
3. The method for reducing the search space of compilation option sequences according to claim 2, characterized in that: The calculation of the harmonic mean of the acceleration ratio data in step S2 refers to The matrix The acceleration ratio data is composed of the matrix The harmonic mean of each column of data in represents the optimization effect of the compilation option corresponding to the column on the template assembly; and for any Harmonic mean of the column If the optimization effect satisfy , then put the Add compilation options to the set of valid compilation options ,in .
4. The method for reducing the compilation option sequence search space according to claim 1, characterized in that: Step S3.1 applies a valid compilation option set Build The two-dimensional option matrix ~ At any time A two-dimensional option matrix The construction process is as follows: Get the Programs in binary sequences Optimize relative preset compilation option sequence Speedup ; Get binary sequence Optimize relative preset compilation option sequence Speedup ; Take the matrix Middle Line Column value ; Take the matrix Middle Line Column value ;when When the two-dimensional option matrix Middle Line The column is recorded as 1. When the two-dimensional option matrix Middle Line The column is recorded as 0, and the two-dimensional option matrix is obtained by traversing in this way. All the values in the final result are A two-dimensional option matrix .
5. The method for reducing the compilation option sequence search space according to claim 4, characterized in that: In step S4, a depth-first search is performed on the directed graph of compilation option relationships to obtain a set of compilation option subsequences to be selected. When , the binary compilation option sequence searched is connected one by one to obtain the compilation option subsequence , and add it to the list of compilation options to be selected , ensure that the list of compilation options to be selected is entered All elements in are not subsequences of any other element.
6. The method for reducing the search space of compilation option sequences according to claim 5, characterized in that: In step S5, a genetic algorithm is used to select a set of compilation option sequences. The compile option subsequence in When iterative optimization is performed on the target program to generate the optimal compilation option sequence with the shortest running time, the set of candidate compilation option sequences is included. As the initial population of the genetic algorithm, iterative optimization is performed on the target program, and finally the optimal compilation option sequence with the shortest running time is obtained.
7. A system for reducing the search space of a compilation option sequence, comprising a microprocessor and a memory connected to each other, characterized in that: The microprocessor is programmed or configured to execute the method for reducing the search space of compilation option sequences as claimed in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program or instruction stored therein, characterized in that: The computer program or instruction is programmed or configured to execute the method for reducing the search space of compilation option sequences as claimed in any one of claims 1 to 6 through a processor.
9. A computer program product comprising a computer program or instructions, characterized in that The computer program or instruction is programmed or configured to execute the method for reducing the search space of compilation option sequences as claimed in any one of claims 1 to 6 through a processor.
Citation Information
Patent Citations
Iterative Compilation Supporting Entity Instance-Specific Compiler Option Variations
US20090193402A1
Method and apparatus for automatically determining which compiler options should be used when compiling a computer program
US5966538A