Compiler optimization option prediction method, storage medium, electronic device and computer program product

By extracting the circular structure features from the source code file and using neural network models to predict the optimization option sequence, the problem of low efficiency of compilation optimization options is solved, and fast and low resource consumption compiler optimization is achieved, improving compilation efficiency and accuracy.

CN120578393APending Publication Date: 2025-09-02ZTE CORP +1
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510179215.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-18
Publication Date
2025-09-02

AI Technical Summary

Technical Problem

The method of selecting compilation optimization options in the prior art is less efficient, resulting in high time cost in large programs in large programs and difficult to use directly in production environments.

Method used

By extracting the code features related to the loop structure from the source code file to be optimized, converting them into program vectors of preset dimensions, and using pre-trained neural network models to predict the target optimization option sequence, directly predicting the appropriate compiler optimization options.

Benefits of technology

It realizes fast and low resource consumption compiler optimization option prediction, improves compilation efficiency and accuracy, and reduces time costs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120578393A_ABST
    Figure CN120578393A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a method for predicting compiler optimization options, a storage medium, an electronic device and a computer program product, and the method comprises the steps: extracting code features related to a loop structure from a to-be-optimized source code file, and converting the code features into a preset first program vector of a first dimension; and according to the first program vector and a pre-trained neural network model, predicting a target optimization option sequence used for optimizing the operation performance of the to-be-optimized source code file, thereby solving the problem of low efficiency of a method for selecting compiling optimization options in related technologies, and achieving the effects of high prediction speed and low resource consumption.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present application relate to the field of communications, and in particular, to a method for predicting compiler optimization options, a storage medium, an electronic device, and a computer program product. Background Art

[0002] Program performance optimization is one of the most important activities in software development. Compiler optimization is a key means of optimizing program performance. Compiler optimization does not require modifying the program code; it simply controls the compiler's optimization process by enabling or disabling compiler optimization options to generate high-performance executable programs. During the compilation of compute-intensive programs, improper loop handling often presents a significant performance bottleneck. The compiler provides multiple loop-related optimization options to control loop optimization behavior.

[0003] Currently, the method for selecting compilation optimization options is usually to use dynamic iterative compilation driven by optimization search. Dynamic iterative compilation requires compiling and executing the program hundreds or thousands of times. This method is inefficient for large programs, has a high time cost, and is difficult to use directly in a production environment. Summary of the Invention

[0004] Embodiments of the present application provide a method for predicting compiler optimization options, a storage medium, an electronic device, and a computer program product to at least solve the problem of low efficiency in methods for selecting compiler optimization options in related arts.

[0005] According to one embodiment of the present application, a method for predicting compiler optimization options is provided, comprising:

[0006] Extracting code features related to loop structures from the source code file to be optimized, and converting the code features into a first program vector of a preset first dimension;

[0007] A target optimization option sequence for optimizing the running performance of the source code file to be optimized is predicted based on the first program vector and a pre-trained neural network model.

[0008] According to another embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored. The computer program is configured to execute the steps of any one of the above method embodiments when run.

[0009] According to another embodiment of the present application, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any one of the above method embodiments.

[0010] According to another embodiment of the present application, a computer program product is provided, including a computer program, which implements the steps of any of the above method embodiments when executed by a processor.

[0011] In an embodiment of the present application, code features related to loop structures are extracted from the source code file to be optimized, and the code features are converted into a first program vector of a preset first dimension; based on the first program vector and a pre-trained neural network model, a target optimization option sequence for optimizing the running performance of the source code file to be optimized is predicted, thereby solving the problem of low efficiency of the method for selecting compilation optimization options in the related art, and achieving the effects of fast prediction speed and low resource consumption. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] Figure 1 This is a hardware structure block diagram of a mobile terminal on which the method embodiment of the present application is executed;

[0013] Figure 2 is a flowchart of a method for predicting compiler optimization options according to an embodiment of the present application;

[0014] Figure 3 1 is a schematic diagram of a program source code feature extraction process according to an embodiment of the present application;

[0015] Figure 4 is a schematic diagram of one-hot encoding of an optimization option sequence according to an embodiment of the present application;

[0016] Figure 5 is a schematic diagram of the optimization option prediction process according to an embodiment of the present application;

[0017] Figure 6 is a structural block diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0018] The embodiments of the present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.

[0019] It should be noted that the terms "first", "second", etc. in the description and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence.

[0020] Related technologies typically use an optimization search-driven dynamic iterative compilation method to select compilation optimization options. During the iterative process, a search strategy is used to continuously compile and execute the program using different optimization options. The search strategy is then adjusted based on performance changes. This dynamic iterative compilation method requires hundreds or even thousands of compilations and executions, making it inefficient and time-consuming for large programs, making it difficult to use directly in production environments.

[0021] Based on the above-mentioned technical problems, an embodiment of the present application proposes a method for predicting compiler optimization options. By extracting code features related to the loop structure in the source code file to be optimized, the code features are converted into program vectors of preset dimensions. Based on the program vectors and a pre-trained neural network model, a sequence of optimization options suitable for the source code file to be optimized is predicted. Only the static features of the program need to be extracted to directly predict the loop optimization options suitable for the program. This solves the problem of low efficiency of the method for selecting compilation optimization options in related technologies, and achieves the effects of fast prediction speed and low resource consumption.

[0022] The method embodiments provided in the embodiments of the present application can be executed in a mobile terminal, a computer terminal or a similar computing device. Taking running on a mobile terminal as an example, Figure 1 This is a hardware block diagram of the mobile terminal running the method embodiment of this application. Figure 1 As shown, the mobile terminal may include one or more ( Figure 1 Only one is shown) a processor 102 (the processor 102 may include but is not limited to a microprocessor MCU or a programmable logic device FPGA and other processing devices) and a memory 104 for storing data, wherein the mobile terminal may also include a transmission device 106 and an input and output device 108 for communication functions. It will be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the mobile terminal. Figure 1 More or fewer components than shown, or with Figure 1 Different configurations shown.

[0023] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the prediction method of the compiler optimization option in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, implementing the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely located relative to the processor 102, and these remote memories can be connected to the mobile terminal via a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.

[0024] The transmission device 106 is used to receive or send data via a network. A specific example of the aforementioned network may include a wireless network provided by the mobile terminal's communications provider. In one embodiment, the transmission device 106 includes a network interface controller (NIC), which can be connected to other network devices via a base station to enable communication with the Internet. In another embodiment, the transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0025] In this embodiment, a method for predicting compiler optimization options running on the above mobile terminal is provided. Figure 2 is a flowchart of a method for predicting compiler optimization options according to an embodiment of the present application, such as Figure 2 As shown, the process may include the following steps:

[0026] Step S201 : extracting code features related to loop structures from a source code file to be optimized, and converting the code features into a first program vector of a preset first dimension.

[0027] For example, a source code file can be a program code written in a programming language such as C / C++, which is the original input for software development. The source code file can contain various programming structures such as functions, loop structures, conditional statements, etc.

[0028] A loop structure is a programming pattern within a source code file that repeatedly executes a section of code until a specific condition is met. While loop structures can effectively manage repetitive code execution, their efficiency often depends on complex details such as the internal instruction types, data dependencies, and memory access patterns. Optimizing loop structures can significantly improve program execution speed and resource utilization.

[0029] For example, in source code files, loop structures can take various forms, such as nested loops, conditional loops, while loops, and for loops. Each form may require different optimizations. Loop structure-related code features are a series of metrics that describe the characteristics of loops in source code files, such as the loop type (e.g., for loop, while loop), the number of loop levels (i.e., the depth of loop nesting), and the number of loop executions.

[0030] As an example, code features related to loop structures can be integrated into a single vector to achieve unified management and standardized processing of these features, making it easier for the model to learn the relationships between features during training. Furthermore, feature data in vector form can be efficiently processed by neural network models, accelerating model training.

[0031] For example, openssl is a software source code package, openssl / crypto / lhash / lhash.c is the source code file to be optimized in the software source code package, and the perf performance analysis tool can be used to locate two functions, OPENSSL_LH_retrieve and ossl_lh_strcasehash, whose hot spots exceed 5% in the openssl file. The functions OPENSSL_LH_retrieve and ossl_lh_strcasehash are located in the openssl / crypto / lhash / lhash.c file and contain a loop structure.

[0032] The running time of the OPENSSL_LH_retrieve and ossl_lh_strcasehash functions accounts for a large proportion of the entire OpenSSL execution process. Optimizing these two functions can have a significant impact on the overall performance of OpenSSL.

[0033] Clang is the C / C++ compiler front-end for the LLVM project, converting source code into LLVM Intermediate Representation (LLVM IR). IR2Vec is an open-source tool that converts LLVM IR into a vector representation, allowing you to extract the structural and semantic features of code from the intermediate representation. You can use Clang to convert lhash.c into the intermediate representation file lhash.ll, and then use IR2Vec to convert lhash.ll into a 300-dimensional floating-point feature vector.

[0034] It should be noted that 300 dimensions is an example of a preset feature dimension, which means that 300 features related to code performance are extracted. Those skilled in the art can set the dimension arbitrarily according to actual conditions, and the embodiments of this application are not limited here.

[0035] The following further explains the specific process of source code feature extraction:

[0036] For example, Figure 3 This is a schematic diagram of the program source code feature extraction process according to an embodiment of the present application. Figure 3 As shown, the following steps may be specifically included:

[0037] 1) Input program source code file

[0038] That is, the source code file to be optimized contains information such as the program's logical structure and loop structure, and is the basis for subsequent feature extraction.

[0039] 2) Format conversion

[0040] You can call the front-end tool of a C / C++ compiler (such as GCC or Clang) to preprocess the source code file and convert it into an intermediate representation file (such as LLVM IR). During this process, you can use the "-O0" option to disable the compiler's optimization function to ensure that the generated intermediate representation file can fully retain the loop structure information in the source code file, avoiding the compiler from optimizing the loop structure during the conversion process, which may affect the accuracy of feature extraction.

[0041] 3) Convert to an intermediate representation file in .ll format

[0042] For example, running the command "clang -O0 -S -emit -llvm test.co test.ll" converts a C / C++ source code file into an LLVM IR .ll format file. This intermediate representation file is more uniform in format, making it easier to process with subsequent feature extraction tools.

[0043] 4) Feature extraction

[0044] You can call the getFunctionVectors function in the IR2Vec tool to perform feature extraction on the converted intermediate representation file. The IR2Vec tool converts intermediate representation code into numeric vectors. It can capture the characteristics of loop structures in source code files and convert them into fixed-dimensional vector representations for easier processing by neural network models.

[0045] 5) Generate program vector

[0046] The getFunctionVectors function of the IR2Vec tool can be called to extract code features from the intermediate representation file and generate a first program vector. This first program vector can be a fixed-length floating-point vector with a predefined dimension, for example, 300. Each vector element in the first program vector can represent a characteristic of the loop structure in the source code file, such as instruction usage frequency, data dependencies, and loop count. The vectorized representation of these features allows the characteristics of the loop structure to be understood and learned by the neural network in numerical form.

[0047] In the above process, by converting the source code file into an intermediate representation file, and then using a feature extraction tool to convert the loop structure features in the intermediate representation file into numerical vectors, the data representation of the loop structure characteristics is achieved, providing the necessary input data for the training and prediction of the neural network model.

[0048] In the embodiments of this application, by extracting code features related to loop structures, the inherent properties of loop structures can be captured and quantified, providing a basis for predicting the optimal loop optimization options. Through feature extraction and quantification, the complexity of loop structures can be converted into numerical forms that can be understood by the model, allowing machine learning algorithms to learn the laws and patterns of how loop optimization options affect program performance based on these features.

[0049] Step S202: predicting a target optimization option sequence for optimizing the running performance of the source code file to be optimized based on the first program vector and a pre-trained neural network model.

[0050] Exemplarily, an optimization option sequence refers to a sequence consisting of multiple compiler optimization options. Optimization options are specific rules or strategies that the compiler can turn on or off when compiling source code, and can be used to control the compiler's optimization behavior of the code during the compilation process. Each optimization option can correspond to an optimization technique, such as loop unrolling, vectorization, loop stripping, etc. For example, in a C / C++ compiler, optimization options such as -funswitch-loops, -ftree-loop-vectorize, -fno-unroll-loops, etc., respectively control optimization behaviors such as loop splitting, vectorization, and loop unrolling. Enabling or disabling these options will significantly affect how the compiler handles loop structures in the source code, and thus affect the running efficiency of the generated executable program.

[0051] For example, the target optimization option sequence can be a combination of optimization options output during the model inference phase of an embodiment of the present application, which is predicted to be an optimization sequence capable of improving the performance of a specific source code file to be optimized. The target optimization option sequence can be predicted by a neural network model based on the code features of the source code file to be optimized, and is intended to be directly applied during the compilation process to generate an executable program with better performance.

[0052] For example, the first program vector can be input into a pre-trained neural network model. This neural network model can be pre-trained on a large amount of loop structure source code and can learn and understand the relationship between loop structure and optimization options. The neural network model can output a probability vector, each element of which can represent the probability that the source code file to be optimized is suitable for a certain optimization option sequence. From the output probability vector, the optimization option sequence corresponding to the highest probability can be selected as the target optimization option sequence.

[0053] In an exemplary embodiment, predicting a target optimization option sequence for optimizing the running performance of the source code file to be optimized based on the first program vector and a pre-trained neural network model includes:

[0054] Inputting the first program vector into the neural network model to obtain a preset probability vector of a second dimension output by the neural network model, wherein each vector element of the probability vector represents a probability that each predicted optimization option sequence belongs to an optimization option sequence in a preset set of optimization option sequences;

[0055] A target vector element is determined from a plurality of vector elements of the probability vector, and an optimization option sequence corresponding to the target vector element is determined as the target optimization option sequence; wherein the target vector element is the vector element with the largest corresponding probability.

[0056] As an example, based on the source code file to be optimized, a pre-designed program feature extraction tool (such as IR2Vec) can be used to convert the code features related to the loop structure in the source code file into a numerical vector representation, namely a first program vector. The first program vector is used as the input of the neural network model to predict the optimal sequence of optimization options. The dimension of the first program vector can be preset, and those skilled in the art can set it according to actual conditions. The embodiment of the present application does not limit this.

[0057] As an example, the first program vector is input into a pre-trained neural network model to obtain a probability vector of a preset second dimension output by the pre-trained neural network model. The second dimension can be pre-set, and the size of the second dimension can be equal to the size of the optimization option sequence set, and each vector element can correspond to an optimization option sequence in the optimization option sequence set. For example, if the optimization option sequence set contains 77 different optimization option sequences, then the dimension of the probability vector is 77, and the probability vector includes 77 vector elements, each of which can represent the probability that an optimization option sequence is predicted to be the most suitable for the source code file to be optimized.

[0058] For example, the 300-dimensional first program vector can be input into a pre-trained neural network model to obtain a 77-dimensional floating-point probability vector (i.e., a probability vector of the second dimension) output by the pre-trained neural network model. Each vector element of the probability vector can represent the probability that the predicted optimization option sequence belongs to each optimization option sequence in the optimization option sequence label set.

[0059] As an example, the vector element with the largest probability (ie, the target vector element) may be searched from the output probability vector, and the optimization option sequence corresponding to the target vector element may be determined as the target optimization option sequence.

[0060] Through the above steps, the embodiments of the present application can quickly predict the most suitable sequence of loop optimization options based on static source code file characteristics, thereby effectively improving program performance during the compilation phase. The prediction process avoids the time-consuming and resource-intensive operations of dynamic iterative compilation and execution, improving the efficiency and accuracy of loop optimization.

[0061] In an exemplary embodiment, each optimization option sequence in the optimization option sequence set corresponds to an optimization option sequence label, and each vector element of the probability vector is a predicted probability that each optimization option sequence label belongs to a label in the optimization option sequence set;

[0062] Determining the optimization option sequence corresponding to the target vector element as the target optimization option sequence includes:

[0063] Determining a target optimization option sequence label corresponding to the target vector element;

[0064] The target optimization option sequence corresponding to the target optimization option sequence label is determined from the optimization option sequence set.

[0065] As an example, an optimization sequence set may include at least one optimization sequence and an optimization sequence tag corresponding to each optimization sequence. Each optimization sequence in the optimization sequence set has its own unique optimization sequence tag. The optimization sequence tag is a unique identifier for a specific optimization sequence and can be used to identify the specific optimization sequence during model training and inference.

[0066] In an embodiment of the present application, each optimization option sequence can be pre-assigned an optimization option sequence label. The optimization option sequence label can be used to quickly locate and reference the corresponding optimization option sequence in the optimization option sequence set. The optimization option sequence label can simplify the output of the neural network model, making the model prediction results easy to understand and process. For example, for the optimization option sequence "-funswitch-loops-fno-split-loops-O3", it can be assigned the label "17", while for another optimization option sequence "-O3-floop-unroll-and-jam-fno-tree-loop-vectorize", it can be assigned the label "43". In this way, the model can output a probability vector, in which each vector element corresponds to a specific optimization option sequence label, and the value of each vector element can be the probability that the predicted optimization option sequence belongs to the optimization option sequence identified by the label in the optimization option sequence set.

[0067] As an example, the neural network model can output a probability vector after completing the processing of the input program vector. Each dimension of the probability vector corresponds to an optimization option sequence label, that is, each element in the probability vector corresponds one-to-one to an optimization option sequence label in the optimization option sequence set, indicating the probability that the model predicts that the source code file is most suitable for using the corresponding optimization option sequence.

[0068] As an example, the target vector element (i.e., the vector element corresponding to the maximum probability value or the vector element with the maximum value) can be determined from multiple vector elements in the probability vector, and the optimization option sequence label corresponding to the target vector element can be determined based on the preset correspondence between the target vector element and the optimization option sequence label. Based on the optimization option sequence label corresponding to the target vector element, the optimization option sequence corresponding to the optimization option sequence label is searched from the optimization option sequence set.

[0069] As an example, there is a one-to-one correspondence between the optimization option sequence label and the optimization option sequence. Each label directly points to a specific optimization option sequence in the optimization option sequence set, and each optimization option sequence also has a unique label associated with it. This correspondence ensures that the probability value of each vector element in the probability vector output by the model can be directly associated with a specific optimization option sequence. For example, if the value (i.e., probability value) of the vector element corresponding to the optimization option sequence label "17" in the probability vector output by the model is the largest, then by searching the optimization option sequence set, the optimization option sequence corresponding to the optimization option sequence label "17" can be determined as the target optimization option sequence.

[0070] In an embodiment of the present application, code features related to loop structures are extracted from the source code file to be optimized and converted into a first program vector of a preset first dimension. Based on the first program vector and a pre-trained neural network model, a target optimization option sequence for optimizing the runtime performance of the source code file to be optimized is predicted. Simply extracting the program's static features allows for direct prediction of appropriate loop optimization options for the program, addressing the inefficient compilation optimization option selection methods used in related art, achieving rapid prediction and minimal resource consumption.

[0071] The neural network model in step S202 is pre-trained. In an exemplary embodiment, the training process of the neural network model may be as follows:

[0072] Obtaining a preset training data set, the training data set including a second program vector of a source code file for training and a second probability vector of a first optimization option sequence label corresponding to the source code file; wherein the dimension of the second program vector is the first dimension, and the dimension of the second probability vector is the second dimension;

[0073] The neural network model is trained with the second program vector of the first dimension as input and the second probability vector of the second dimension as output to obtain the trained neural network model.

[0074] As an example, before model training, a training dataset can be constructed. Multiple source code files can be obtained from a preset loop structure source code set. Each source code file in the loop structure source code set can be subjected to code feature extraction and optimized option sequence label encoding to obtain a training dataset that can be directly used by the neural network model. The loop structure source code set can be generated based on at least one program source code containing a loop structure.

[0075] In an exemplary embodiment, before obtaining a preset training data set, the method further includes:

[0076] Encoding a plurality of first optimization option sequence labels in the optimization option sequence set to generate the second probability vector of the second dimension corresponding to each first optimization option sequence label;

[0077] converting the plurality of source code files into the second program vectors of the first dimension respectively;

[0078] The training data set is constructed according to the second probability vector of the second dimension corresponding to a plurality of the second program vectors and each of the first optimization option sequence labels.

[0079] As an example, the training dataset may include second program vectors of source code files used for training (i.e., for model training), where each source code file corresponds to a second program vector, and each second program vector has the same first dimension. The second program vectors may be converted from code features related to loop structures extracted from the source code files, and the second program vectors may serve as input to the neural network model to be trained.

[0080] For example, you can refer to Figure 3 Code features can be extracted from each of the multiple source code files used for training. For example, using the test.c program, you can use the clang front-end tool to run "clang -O0 -S -emit -llvm test.c -otest.ll" to generate an intermediate representation file in .ll format. "-O0" disables optimization during the clang conversion process, allowing the intermediate representation file to contain more original information. The generated intermediate representation file, test.ll, can be converted into a 300-dimensional floating-point feature vector using the getFunctionVectors method of the program feature extraction tool IR2Vec.

[0081] As an example, the first optimization option sequence tag is a tag for the first optimization option sequence. In the embodiment of the present application, each optimization option sequence can have a unique tag. The first optimization option sequence can be an optimization option sequence that improves the running performance of the source code file. Each source code file can correspond to multiple first optimization option sequences.

[0082] As an example, the training data set may include a second probability vector, which may be obtained by encoding and transforming the first optimization option sequence label. The dimension of the second probability vector may be the same as the number of first optimization option sequences or the number of first optimization option sequence labels (i.e., the second dimension). The second probability vector may serve as the output of the neural network model to be trained.

[0083] In an exemplary embodiment, the first optimization option sequence label corresponds to the first vector element in the second probability vector, the first vector element is a vector element whose corresponding probability is 1, and the second vector elements remaining in the second probability vector except the first vector element are vector elements whose corresponding probability is 0.

[0084] One-hot encoding can be performed on an optimization option sequence label corresponding to the test.c program.

[0085] For example, Figure 4 This is a schematic diagram of one-hot encoding of an optimization option sequence according to an embodiment of the present application. The optimization option sequence set may include all optimal optimization option sequences (i.e., first optimization option sequences) determined through a dynamic iterative compilation process. Each first optimization option sequence is assigned a unique serial number m. These serial numbers can be used to locate and mark specific optimization option sequences in the one-hot encoding.

[0086] For each optimization sequence label, you can initialize an N-dimensional integer vector, where N is the size of the optimization sequence label set. For example, if the optimization sequence label set has 77 different optimization sequences, then the length of the vector is 77.

[0087] In the initialized N-dimensional vector, all elements are set to 0. Next, based on the order m of the optimization option sequence label, the value of the m-th element of the vector is set to 1, and all other elements remain 0. For example, if the order of the optimization option sequence label is 73, the 73rd element of the vector is set to 1, and the remaining 76 elements are set to 0.

[0088] After processing, each optimization option sequence label is converted into a one-hot encoded vector. During model training, these one-hot encoded vectors serve as labels for the neural network model and are paired with program vectors extracted from the source code files to form the training dataset. By learning these pairs, the neural network model can understand the relationship between source code file features and the optimal loop optimization option sequence.

[0089] In the embodiment of the present application, the second program vector of the first dimension can be used as the input of the neural network model to be trained, and the second probability vector of the second dimension can be used as the output of the neural network model to be trained to train the model to obtain a trained neural network model.

[0090] For example, you can predefine a fully connected neural network model consisting of 4 fully connected layers and 1 softmax layer, with an input dimension of 300 and an output dimension of 77.

[0091] As an example, the training data set can be randomly shuffled and divided into a training part and a validation part. The training part is used to iteratively train the above-mentioned neural network model, and the validation part can be used to test the prediction accuracy of the model in each round of iteration. The training can be iterated a preset number of times to solidify the neural network model.

[0092] In an exemplary embodiment, before obtaining a preset training data set, the method further includes:

[0093] A first optimization option sequence corresponding to each of the plurality of source code files is determined, and a first optimization option sequence label corresponding to each of the source code files is generated to obtain the optimization option sequence set.

[0094] As an example, the first optimization option sequence may be an optimization option sequence that optimizes the running performance of the source code file. The optimization option sequence set may include the first optimization option sequence and the first optimization option sequence label corresponding to each source code file of multiple source code files.

[0095] In an exemplary embodiment, determining a first optimization option sequence corresponding to each of the plurality of source code files includes:

[0096] Generate a loop optimization option set including a plurality of optimization options according to loop optimization-related option field information in a preset compiler configuration file; wherein each optimization option in the loop optimization option set includes an on state and a off state;

[0097] generating, according to the switch states of all the optimization options in the loop optimization option set, at least one second optimization option sequence having the same optimization option and different optimization option switch states;

[0098] The first optimization option sequence corresponding to the source code file is selected from at least one of the second optimization option sequences.

[0099] As an example, a C / C++ compiler configuration file can be pre-analyzed, and all option fields related to loop optimization can be extracted based on the description information in the configuration file to obtain a loop optimization option set. Each optimization option in the loop optimization option set has two states: on and off.

[0100] As an example, multiple optimization options in a loop optimization option set can be sorted, and at least one of the sorted multiple optimization options can be randomly controlled to be in a closed state, and the remaining optimization options can be in an open state, to obtain multiple second optimization option sequences, wherein the multiple second optimization option sequences have the same optimization options and different optimization option switch states.

[0101] For example, you can use the "gcc --help=optimizers" command to obtain all performance optimization-related options in the compiler configuration file, then analyze the description of each option, extract all option fields related to loop optimization, and obtain a loop optimization option set, as shown in Table 1.

[0102] Table 1

[0103]

[0104] As an example, the first optimization option sequence corresponding to each source code file may be determined from a plurality of second optimization option sequences.

[0105] As an example, the source code file corresponding to the second optimization option sequence may be compiled to generate an executable program, and the executable program may be run. The second optimization option sequence corresponding to the executable program with the best running performance may be determined as the first optimization option sequence.

[0106] The following further explains the specific process of determining the first optimization option sequence corresponding to each source code file:

[0107] In an exemplary embodiment, selecting the first optimization option sequence corresponding to each source code file from at least one second optimization option sequence includes:

[0108] For each of the source code files, compile the source code file using each of the second optimization option sequences in turn to generate an executable program;

[0109] Running the executable program and recording the running time of the executable program;

[0110] The executable program with the shortest running time is determined from the currently running executable programs, and the second optimization option sequence corresponding to the executable program with the shortest running time is determined as the first optimization option sequence.

[0111] In an exemplary embodiment, determining the executable program with the shortest running time from the currently running executable programs includes:

[0112] Counting the total time the executable program has been currently running and the number of times the executable program has been generated;

[0113] When the total time is greater than a preset time threshold, or the number of times is greater than a preset number threshold, stop generating a new executable program;

[0114] From the currently running executable programs, the executable program with the shortest running time is determined.

[0115] In an exemplary embodiment, the method further includes:

[0116] When the total time is less than or equal to the preset time threshold and the number of times is less than or equal to the preset number threshold, a new executable program is generated, and the total time the executable program has been currently running and the number of times the executable program has been generated are counted until the total time is greater than the preset time threshold, or the number of times is greater than the preset number threshold.

[0117] As an example, program source code files corresponding to different types of programming topics in a preset database may be selected to generate a loop structure source code set.

[0118] Each program source code file in the loop structure source code set can be compiled and run iteratively. The optimization option sequence for each round is a random combination of optimization options in the loop optimization option set. The optimization indicator can be the program's running time, and the upper limit of the number of iterations can be set (for example, the upper limit of the number of iterations is set to 1000 times). Each optimization option has two states: on and off. Taking -funswitch-loops as an example, -funswitch-loops is on and -fno-unswitch-loops is off. For example, assuming the loop optimization option set contains 10 options, the search space for the optimization options contains 1024 permutations and combinations.

[0119] For example, the iterative process may include the following steps:

[0120] 1) You can use a C / C++ compiler and use the second optimization option sequence to compile the source code file to generate an executable program;

[0121] 2) Run the executable program and record the running time of the executable program;

[0122] 3) Count the total time the executable program has been running and the number of times the executable program has been generated;

[0123] 4) determining whether the number of times the executable program has been generated has reached a preset number threshold, or whether the total time the executable program has been running has reached a preset time threshold;

[0124] 5) If the total time of the currently running executable program in step 4) is less than or equal to (i.e., has not reached) the preset time threshold, and the number of times the currently generated executable program is less than or equal to (i.e., has not reached) the preset number threshold, then return to step 1) to generate a new executable program;

[0125] 6) If the number of executable programs currently generated in step 4) reaches a preset number threshold, or the total time of the currently running executable programs reaches a preset time threshold, then stop generating new executable programs, that is, stop iteration, and among the at least one executable program currently running, determine the executable program with the shortest running time, and determine the second optimization option sequence corresponding to the executable program with the shortest running time as the first optimization option sequence, and use the first optimization option sequence label of the first optimization option sequence as the label of the source code file.

[0126] After completing the above steps, we obtain an optimization sequence set. Each source code file corresponds to an optimization sequence label and optimization sequence in the optimization sequence set. Different source code files can correspond to the same optimization sequence label and optimization sequence. After removing duplicate optimization sequence labels from all optimization sequence labels, we obtain the optimization sequence set, as shown in Table 2.

[0127] Table 2

[0128]

[0129]

[0130] As an example, in actual applications, you can first construct a training set, then train the model, and then perform model prediction to predict the optimization options that are suitable for the program characteristics. The following example further illustrates the stages of training set construction, model training, and model prediction:

[0131] Example 1:

[0132] Figure 5 : is a schematic diagram of the optimization option prediction process according to an embodiment of the present application, such as Figure 5 As shown, it includes the training set construction stage, the model training stage and the model inference stage (i.e., the model prediction stage).

[0133] 1. Training set construction stage

[0134] 1) The loop structure source code set may include multiple pre-selected C / C++ program source code files with loop structures. The source code files in the loop structure source code set may be used to construct a training dataset.

[0135] 2) The configuration file of the C / C++ compiler can be analyzed, all options related to loop optimization in the configuration file can be extracted, and a set of loop optimization options can be generated.

[0136] 3) Option sequences may be constructed based on multiple optimization options in the loop optimization option set to form at least one optimization option sequence.

[0137] 4) Compile and run: You can use the compiler to compile the source code file according to the current optimization option sequence and generate an executable program. Then run the program and record its running performance (such as running time).

[0138] 5) Determine whether the number of iterations or iteration time has reached a set upper limit: This conditional judgment can be used to control the termination of the construction of a single sample in the training set. If the number of iterations or iteration time does not reach the preset upper limit, you can return to the option sequence construction stage in 3) to continue testing new optimization option combinations. If the number of iterations or iteration time reaches the upper limit, the collected optimal optimization option sequence can be used as the label of the source code file, and the optimal option sequence for a single sample in the training set can be determined.

[0139] For each source code file in the loop structure source code set, the above steps 3)-5) can be executed to determine the optimal option sequence corresponding to each sample in the training set.

[0140] 6) Optimization option sequence label set: After the iteration, each source code file can be associated with an optimal optimization option sequence and optimization option sequence label to form a label set that can be used for subsequent model training.

[0141] 2. Model training phase

[0142] 1) Feature Extraction: Code features can be extracted from each file in the loop structure source code set to form a program vector set (i.e., multiple second program vectors). Each source code file corresponds to a second program vector, and the program vector set contains the second program vectors of all source code files in the loop structure source code set. Code features can include the morphology of the loop structure and data dependencies, so that the model can understand the inherent characteristics of the source code during training.

[0143] 2) Neural Network Model: This is the neural network model to be trained, designed to predict optimization options. Its input is a program vector, and its output is a one-hot encoding of the optimization option sequence. Each program vector has a corresponding one-hot encoding, representing the optimal optimization option sequence.

[0144] 3) Determine whether the number of training cycles has reached the upper limit or the model performance meets the standard: This conditional judgment is used to control the termination of the training process. If the number of training cycles has not reached the upper limit or the model performance does not meet the standard, the system will return to the feature extraction stage to continue training the model. If the set conditions are met, the best-performing model will be selected and solidified for subsequent model inference.

[0145] 3. Model inference stage

[0146] 1) Project to be optimized: This is the source code file to be optimized, which is the input of the trained neural network model in actual application. It can be any C / C++ program project that requires performance optimization.

[0147] 2) Hotspot function file extraction: Performance bottlenecks, i.e., hotspot functions, can be located from the project to be optimized, and hotspot function files containing loop structures can be extracted.

[0148] 3) Feature extraction: Code feature extraction can be performed on the hotspot function file to generate a program vector (ie, the first program vector).

[0149] 4) Program vector set: a program vector set generated from multiple files to be optimized, including multiple first program vectors, used for model reasoning input.

[0150] 5) Model reasoning: The program vector (i.e., the first program vector) is input into the trained neural network model to predict the most suitable optimization option sequence.

[0151] For example, referring to Table 2, the optimization option sequence label corresponding to the largest vector element value in the probability vector can be selected, and according to the optimization option sequence label corresponding to the largest vector element value, the corresponding optimization option sequence is determined from the optimization option sequence set, namely: -O3-fno-unswitch-loops-fno-split-loops-ftree-loop-vectorize-fno-loop-unroll-and-jam-fno-peel-loops-fno-tree-loop-if-convert-fno-tree-slp-vectorize-fno-unroll-loops-funroll-all-loops-fno-prefetch-loop-arrays.

[0152] 6) Optimization option set: The optimization option sequence inferred by the trained neural network model can be used for compilation optimization of hot function files.

[0153] 7) Optimized executable program: The project to be optimized can be constructed using the predicted optimization option sequence to generate an executable program with optimized performance.

[0154] The entire process of Example 1 is a closed loop. Starting from the source code file, a neural network model for prediction is obtained through training. Finally, the trained neural network model is applied to a new project to achieve efficient prediction of loop optimization options and improve program performance.

[0155] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.

[0156] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. The computer program is configured to execute the steps of any of the above method embodiments when run.

[0157] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.

[0158] Figure 6 is a structural block diagram of an electronic device according to an embodiment of the present application, such as Figure 6 As shown, an embodiment of the present application further provides an electronic device 60, including a memory 601 and a processor 602, wherein the memory 601 stores a computer program, and the processor is configured to run the computer program to execute the steps in any of the above method embodiments.

[0159] In an exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.

[0160] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail here.

[0161] An embodiment of the present application further provides a computer program product, including a computer program, which implements the steps of any of the above method embodiments when executed by a processor.

[0162] Obviously, those skilled in the art should understand that the modules or steps of the present application described above can be implemented using a general-purpose computing device, they can be concentrated on a single computing device, or distributed across a network composed of multiple computing devices, they can be implemented using program code executable by the computing device, and thus, they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be performed in a different order than herein, or they can be fabricated into separate integrated circuit modules, or multiple modules or steps can be fabricated into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.

[0163] The above description is merely a preferred embodiment of the present application and is not intended to limit the present application. Various modifications and variations are possible for those skilled in the art. Any modifications, equivalent substitutions, improvements, etc. made within the principles of the present application shall be included within the scope of protection of the present application.

Claims

1. A method for predicting compiler optimization options, characterized in that: include: Extracting code features related to loop structures from the source code file to be optimized, and converting the code features into a first program vector of a preset first dimension; A target optimization option sequence for optimizing the running performance of the source code file to be optimized is predicted based on the first program vector and a pre-trained neural network model.

2. The method according to claim 1, characterized in that The step of predicting a target optimization option sequence for optimizing the running performance of the source code file to be optimized based on the first program vector and a pre-trained neural network model includes: Inputting the first program vector into the neural network model to obtain a preset probability vector of a second dimension output by the neural network model, wherein each vector element of the probability vector represents a probability that each predicted optimization option sequence belongs to an optimization option sequence in a preset set of optimization option sequences; A target vector element is determined from a plurality of vector elements of the probability vector, and an optimization option sequence corresponding to the target vector element is determined as the target optimization option sequence; wherein the target vector element is the vector element with the largest corresponding probability.

3. The method according to claim 2, characterized in that An optimization option sequence label corresponding to each optimization option sequence in the optimization option sequence set, and each vector element of the probability vector is a predicted probability that each optimization option sequence label belongs to a label in the optimization option sequence set; Determining the optimization option sequence corresponding to the target vector element as the target optimization option sequence includes: Determining a target optimization option sequence label corresponding to the target vector element; The target optimization option sequence corresponding to the target optimization option sequence label is determined from the optimization option sequence set.

4. The method according to claim 2, characterized in that The training process of the neural network model is as follows: Obtaining a preset training data set, the training data set including a second program vector of a source code file for training and a second probability vector of a first optimization option sequence label corresponding to the source code file; wherein the dimension of the second program vector is the first dimension, and the dimension of the second probability vector is the second dimension; The neural network model is trained with the second program vector of the first dimension as input and the second probability vector of the second dimension as output to obtain the trained neural network model.

5. The method according to claim 4, characterized in that in, The first optimization option sequence label corresponds to the first vector element in the second probability vector, the first vector element is a vector element whose corresponding probability is 1, and the second vector elements remaining in the second probability vector except the first vector element are vector elements whose corresponding probability is 0.

6. The method according to claim 4, characterized in that Before obtaining the preset training data set, it also includes: Encoding a plurality of first optimization option sequence labels in the optimization option sequence set to generate the second probability vector of the second dimension corresponding to each first optimization option sequence label; converting the plurality of source code files into the second program vectors of the first dimension respectively; The training data set is constructed according to the second probability vector of the second dimension corresponding to a plurality of the second program vectors and each of the first optimization option sequence labels.

7. The method according to claim 4, characterized in that Before obtaining the preset training data set, it also includes: A first optimization option sequence corresponding to each of the plurality of source code files is determined, and a first optimization option sequence label corresponding to each of the source code files is generated to obtain the optimization option sequence set.

8. The method according to claim 7, characterized in that The determining of a first optimization option sequence corresponding to each of the plurality of source code files includes: Generate a loop optimization option set including a plurality of optimization options according to loop optimization-related option field information in a preset compiler configuration file; wherein each optimization option in the loop optimization option set includes an on state and a off state; generating, according to the switch states of all the optimization options in the loop optimization option set, at least one second optimization option sequence having the same optimization option and different optimization option switch states; The first optimization option sequence corresponding to the source code file is selected from at least one of the second optimization option sequences.

9. The method according to claim 7, characterized in that The step of selecting the first optimization option sequence corresponding to each source code file from at least one of the second optimization option sequences includes: For each of the source code files, compile the source code file using each of the second optimization option sequences in turn to generate an executable program; Running the executable program and recording the running time of the executable program; The executable program with the shortest running time is determined from the currently running executable programs, and the second optimization option sequence corresponding to the executable program with the shortest running time is determined as the first optimization option sequence.

10. The method according to claim 9, characterized in that Determining the executable program with the shortest running time from the currently running executable programs includes: Counting the total time the executable program has been currently running and the number of times the executable program has been generated; When the total time is greater than a preset time threshold, or the number of times is greater than a preset number threshold, stop generating a new executable program; From the currently running executable programs, the executable program with the shortest running time is determined.

11. The method according to claim 10, characterized in that Also includes: When the total time is less than or equal to the preset time threshold and the number of times is less than or equal to the preset number threshold, a new executable program is generated, and the total time the executable program has been currently running and the number of times the executable program has been generated are counted until the total time is greater than the preset time threshold, or the number of times is greater than the preset number threshold.

12. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the method described in any one of claims 1 to 11 are implemented.

13. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 11 are implemented.

14. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 11 are implemented.

Citation Information

Patent Citations

  • Automatic compiler tuning method and device based on artificial intelligence

    CN116931955A

  • LLVM compiler-oriented RISC-V program vectorization optimization method and system

    CN119002872A

  • Method and apparatus for determining compiler parameters settings

    WO2020083487A1