Compiler optimization fault detection method and system based on correlation model
By detecting compiler optimization failures based on the associative model, the problem of low compiler optimization failure detection efficiency is solved, efficient and accurate fault detection is achieved, and the reliability of the compiler is enhanced.
Patent Information
- Application Number
- CN202411826472.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-12
- Publication Date
- 2025-04-29
- Estimated Expiration
- 2044-12-12
AI Technical Summary
The lack of systematic research on failures caused by compiler optimization leads to less ease of use of compilers and can lead to catastrophic consequences in security-critical areas.
The compiler optimized fault detection method based on the association model is adopted. By generating variant test programs and diversified optimization sequences, training samples are constructed and the association model is constructed using the SVM support vector machine algorithm to predict the probability of optimization failure and perform differential testing.
Improves the detection efficiency and accuracy of compiler optimization failures, enhances the reliability of compiler optimization, especially on GCC and LLVM compilers, which show high hit rate and accuracy.
Smart Images

Figure CN119902976A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of compilation optimization and compiler testing, and mainly to an optimization fault testing method and system for a compiler mid-end. Background Art
[0002] Compilers play a vital role in modern software development. Their role is not only to convert high-level programming languages into machine code, but also to generate optimized code to improve program execution efficiency. However, due to the complexity of compiler optimization, compiler optimization is one of the parts most prone to failure. These failures seriously affect the ease of use of the compiler and may lead to catastrophic consequences in safety-critical areas. Therefore, effective testing techniques are essential for failures caused by compiler optimization.
[0003] In recent years, although research on compiler fault detection has continued to emerge, there is still a lack of systematic research on detecting compiler faults caused by compiler optimization. There are many types of compiler optimizations, and the impact between them is intricate. Therefore, designing and implementing an efficient compiler optimization fault detection mechanism has become an urgent task. This invention aims to explore a compiler optimization fault detection mechanism based on a test case and optimization sequence association model, and improve the detection efficiency by understanding and utilizing the relationship between test cases and compiler optimization sequences, thereby solving the fault problem caused by compiler optimization. Summary of the invention
[0004] Purpose of the invention: In view of the problems existing in the above-mentioned background technology, the present invention proposes a compiler optimization fault detection method and system based on an association model, aiming to improve the detection efficiency and accuracy of optimization faults through machine learning methods.
[0005] Technical solution: To achieve the above purpose, the present invention adopts the following technical solution:
[0006] In a first aspect, the present invention provides a compiler optimization fault detection method based on an association model, comprising the following steps:
[0007] (1) Mutation-based test program generation method generates new test programs by inserting code snippets into seed programs;
[0008] (2) constructing a diverse optimization sequence by randomly selecting optimization levels and processing difference sets;
[0009] (3) Construct training samples, associate the test program with the optimization sequence, input it into the compiler for differential testing, and label the output test results to construct labeled training samples;
[0010] (4) Convert the test program and optimization sequence in the training sample into a unified vector representation and input it into the SVM support vector machine algorithm to build an association model, and train the SVM model by using the sequence minimum optimization algorithm;
[0011] (5) Use the trained SVM model to predict the probability of a new test program triggering an optimization failure under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence ranked in the Top-K to perform differential testing on the compiler.
[0012] Furthermore, in step (1), the mutation-based test program generation method extracts all code snippets of a set structure by means of program structure extraction, and then randomly inserts these code snippets into the seed program to form several new test programs.
[0013] Furthermore, in step (2), the optimization sequence construction method first randomly selects two optimization levels from -O0, -O1, -O2, -O3, and -Os to form an optimization level pair, and identifies a difference set of optimization options between the pair of optimization levels; then randomly deletes one or more optimization options in the difference set, and all elements in the optimization sequence are composed of the remaining optimization options in the difference set and the lower level optimization level in the optimization level pair; finally, these elements are randomly arranged to generate a number of optimization sequences between the two optimization levels.
[0014] Furthermore, in step (3), the test program and the optimization sequence association method assume that the test program set is P = {p i}(1≤i≤n), the optimized sequence set is S={s j}(1≤j≤m), then the constructed association pair is expressed as R={ <p i ,s j >}(1≤i≤n,1≤j≤m), where n and m are the number of test programs and the number of optimization sequences, respectively.
[0015] Furthermore, in step (3), the label of the training sample is a numerical label 0 or 1. <p i ,s j >If an optimization failure is detected during compiler differential testing, the tag of the association pair is set to 1, otherwise, the tag of the association pair is set to 0.
[0016] Furthermore, in step (4), the CodeBERT model is used to convert the test program and the optimization sequence into a unified vector representation to facilitate subsequent machine learning model processing.
[0017] Furthermore, in step (4), the SVM support vector machine algorithm is used to construct an association model between the test program and the optimization sequence, and the SVM model is trained using a sequence minimum optimization algorithm; during the training process, the SVM automatically selects a portion of sample vectors as support vectors, which determine the location of the classification boundary; the optimization problem is expressed as:
[0018]
[0019] where α i is the Lagrange multiplier, each sample vector corresponds to an α i , the support vector is α i >0, N′ is the number of support vectors in the training set, ‖.‖ represents the 2-norm of the vector, b is the bias, δ i is the slack variable, C is the regularization parameter, N is the number of training samples, x i is the vector of the test program and the optimized sequence in the ith association pair, y i is the true label of the i-th associated pair 0 or 1, φ(x i ) is the feature vector after being mapped by the kernel function, and subject to represents the constraints that the model must comply with when searching for the optimal solution during the optimization process.
[0020] Furthermore, in step (5), the trained SVM model is used to predict the optimization failure. For each vector x of the test program and the optimization sequence in the association pair to be predicted, the model calculates its position in the feature space and determines its prediction label based on the support vector and the decision function. The decision function is in the form of:
[0021]
[0022] Among them, K(x i ,x) is the Gaussian kernel function.
[0023] In a second aspect, the present invention provides a compiler optimization fault detection system based on an association model, comprising:
[0024] A test program generation module is used for a mutation-based test program generation method to generate a new test program by inserting code snippets into a seed program;
[0025] Optimization sequence generation module for building diversified optimization sequences by randomly selecting optimization levels and processing difference sets;
[0026] A training sample construction module is used to construct training samples, associate the test program with the optimization sequence, input it into the compiler for differential testing, and mark the output test results, thereby constructing labeled training samples;
[0027] The association model training module is used to convert the test program and optimization sequence in the training sample into a unified vector representation and input it into the SVM support vector machine algorithm to build an association model, and train the SVM model by using a sequence minimum optimization algorithm;
[0028] And the prediction and detection module is used to use the trained SVM model to predict the probability of a new test program triggering an optimization failure under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence ranked in the Top-K to perform differential testing on the compiler.
[0029] In a third aspect, the present invention provides a computer program product, comprising a computer program / instruction, which, when executed by a processor, implements the steps of a compiler optimization fault detection method based on an association model.
[0030] Beneficial effects: Compared with the prior art, the present invention has the following effects: 1. The present invention constructs samples of associated test programs and optimized sequences through a test program generation method based on mutation, and proposes an optimization sequence generation method, and constructs an association model based on the SVM support vector machine algorithm, which can achieve a high hit rate and accuracy in optimization fault detection on GCC and LLVM compilers. 2. The detection method based on the association model of the present invention improves the detection efficiency and accuracy of compiler optimization faults, and provides a guarantee for the reliability of compiler optimization. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] Figure 1 This is a flow chart of the compiler optimization fault detection method based on the association model provided by the present invention. DETAILED DESCRIPTION
[0032] The present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0033] like Figure 1 As shown, a compiler optimization fault detection method based on an association model disclosed in an embodiment of the present invention has the following main steps:
[0034] Step 1, a mutation-based test program generation method, generates a new test program by inserting code snippets into a seed program.
[0035] Specifically, in step 1, the mutation-based test program generation method extracts all code snippets of the set structure by program structure extraction, and then randomly inserts these code snippets into the seed program to form several new test programs. The detailed steps include:
[0036] Step 101, code snippet extraction: mainly using the pycparser tool to extract code snippets from the historical data set according to program features, and extracting nine program structures, namely If statement, DoWhile statement, While statement, For statement, Switch statement, Return statement, Goto statement, Assignment statement and Case statement;
[0037] Step 102, program synthesis: inserting the code snippet into the seed program by insertion to generate a new test program.
[0038] Exemplarily, the seed program and the code snippet are represented as an abstract syntax tree using the pycparser tool, and a node in the abstract syntax tree of the seed program is randomly selected to insert the syntax tree of the code snippet into the abstract syntax tree of the seed program, thereby generating a new abstract syntax tree, and converting the new abstract syntax into code to generate a new program. In addition to inserting the code snippet, a check function transparent_crc() is also inserted into the new code, which is used to test whether the check values under different compilers or different optimization sequences are the same. Specifically, when compiling a program using different versions of compilers or under different optimization sequences, if the check value of the variable after a certain compilation is inconsistent with that in other cases, an optimization failure may have occurred.
[0039] In step 2, a diversified optimization sequence is constructed by randomly selecting optimization levels and processing difference sets.
[0040] Specifically, the optimized sequence construction method in step 2 includes:
[0041] Step 201 , randomly select two optimization levels from -O0, -O1, -O2, -O3, and -Os to form an optimization level pair, and identify a difference set of optimization options between the pair of optimization levels.
[0042] Step 202, randomly delete some (one or more) optimization options in the difference set, and all elements in the optimization sequence are composed of the remaining optimization options in the difference set and the lower level optimization level in the optimization level pair. These elements are randomly arranged to generate several optimization sequences between two optimization levels.
[0043] Step 3: construct training samples, associate the test program with the optimization sequence, and input it into the compiler for differential testing, label the output test results, and thus construct labeled training samples.
[0044] Specifically, the testing procedure and the optimization sequence association method in step 3 include:
[0045] Step 301, assuming that the test program set is P = {p i}(1≤i≤n), the optimized sequence set is S={s j}(1≤j≤m), n and m are the number of test programs and the number of optimization sequences respectively. Each test program can be combined with any optimization sequence, and the constructed association pair is expressed as R={ <p i ,s j >}(1≤i≤n,1≤j≤m). These association pairs can be used as input to the compiler.
[0046] Step 302, the constructed association pairs are input into the GCC and LLVM compilers for compilation, and the executable files output by the compilers are obtained. If the running results of the executable files are inconsistent, the association pair triggers an optimization fault of a compiler, and the label is set to 1; if the running results of the executable files are consistent, the association pair does not trigger a compiler optimization fault, and the label is set to 0. This step is iterated until all constructed association pairs have corresponding labels, thereby constructing labeled training samples.
[0047] Step 4: Convert the test program and optimization sequence in the training sample into a unified vector representation and input it into the SVM support vector machine algorithm to build an association model. Train the SVM model by using the sequence minimum optimization algorithm.
[0048] Specifically, this step 4 includes:
[0049] Step 401, using the CodeBERT model to convert the test program and optimization sequence into a unified vector representation. Specifically, first read all the files in the dataset and read their contents as code text code_text. Use the given tokenizer to split the code text into words (tokens) and convert them into corresponding identifiers (token IDs). Then input the code identifier into the pre-trained CodeBERT model to obtain the corresponding code vector, convert the code identifier into a PyTorch tensor, and adjust it to a shape that meets the model input requirements, and obtain the vector representation through model calculation. In order to obtain the average vector representation of the entire code text, the code vector is averaged and pooled, and the torch.mean function is used to calculate the average along the first dimension (i.e., the dimension of the code token), and the average value of each code vector is obtained. Write the file name and the corresponding code vector into a CSV file for subsequent processing and analysis.
[0050] Step 402: Use the SVM support vector machine algorithm to build a correlation model between the test program and the optimization sequence. The detailed steps are as follows:
[0051] Step 4021, organize and preprocess the training set, where each training sample consists of a test program vector and an optimization sequence vector, and each data point has a corresponding label. The vectors of the test program and the optimization sequence are then standardized by subtracting the mean and dividing by the standard deviation to ensure that each feature is on a similar scale, and the data set is divided into a training set and a test set in a ratio of 9:1.
[0052] Step 4022, construct an SVM optimization problem, and find the optimal hyperplane by solving a convex quadratic optimization problem to maximize the interval from the support vector to the classification boundary. This optimization problem can be expressed as:
[0053]
[0054] where α i is the Lagrange multiplier, each sample vector corresponds to an α i , the support vector is α i >0, N′ is the number of support vectors in the training set, ‖.‖ represents the 2-norm of the vector, b is the bias, δ i is the slack variable, C is the regularization parameter, N is the number of training samples, x i is the vector of the test program and the optimized sequence in the ith association pair, y i is the true label of the i-th associated pair 0 or 1, φ(x i ) is the feature vector after being mapped by the kernel function, and subject to represents the constraints that the model must comply with when searching for the optimal solution during the optimization process.
[0055] Step 4023, the SVM model is trained by solving the above optimization problem using a sequential minimal optimization (SMO) algorithm. During the training process, the SVM automatically selects a portion of sample vectors as support vectors, which determine the location of the classification boundary.
[0056] Step 4024, use cross-validation techniques to evaluate model performance and adjust hyperparameters to optimize the generalization ability of the model. Select the parameter combination that can achieve the best performance on the validation set and retrain the final SVM model.
[0057] Step 5: Use the trained SVM model to predict the probability of a new test program triggering an optimization failure under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence ranked in the Top-K to perform differential testing on the compiler.
[0058] Specifically, this step 5 includes:
[0059] Step 501, use the trained SVM model to predict the new test program and optimization sequence. For each vector x of the test program and optimization sequence in the association pair to be predicted, the model calculates its position in the feature space and determines its prediction label based on the support vector and decision function. The decision function is in the form of:
[0060]
[0061] Among them, K(x i ,x) is the Gaussian kernel function. The output of this decision function is a real number, and by applying the sign function to it, the classification result can be obtained. The α corresponding to the support vector i Non-zero, determines the location of the classification boundary. Then the sign function outputs +1, which means the predicted category is positive. Then the sign function outputs -1, which means the predicted category is negative.
[0062] Specifically, the Gaussian kernel function is defined as follows:
[0063]
[0064] Among them, σ>0 and is sufficiently small.
[0065] Step 502: sort the correlation pairs of the test program and the optimization sequence in descending order according to the probability predicted by the correlation model, select the optimization sequence in the top-K sorting, and input it into the GCC and LLVM compilers for differential testing. The detailed steps are as follows:
[0066] Step 5021, for the same test program, set K=10, and select the associated pairs consisting of the optimization sequences ranked in the top-10 as the input of the compiler.
[0067] Step 5022: Collect and compare the running results of the GCC and LLVM compilers. For the same association pair, if the running results of the executable files output by different compilers are inconsistent, the association pair can trigger a compiler optimization fault; otherwise, the association pair cannot trigger a compiler optimization fault.
[0068] Based on the same inventive concept, a compiler optimization fault detection system based on an association model disclosed in an embodiment of the present invention includes: a test program generation module, which is used for a test program generation method based on mutation, and generates a new test program by inserting a code snippet into a seed program; an optimization sequence generation module, which is used to construct a diversified optimization sequence by randomly selecting an optimization level and processing a difference set; a training sample construction module, which is used to construct a training sample, associate the test program with the optimization sequence, and input it into the compiler for differential testing, and mark the output test results, thereby constructing a labeled training sample; an association model training module, which is used to convert the test program and the optimization sequence in the training sample into a unified vector representation form and input it into the SVM support vector machine algorithm to construct an association model, and train the SVM model by using a sequence minimum optimization algorithm; and a prediction and detection module, which is used to use the trained SVM model to predict the probability of a new test program triggering an optimization fault under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence sorted in Top-K to perform differential testing on the compiler. The detailed implementation process of each module refers to the above method embodiment, which will not be repeated.
[0069] A computer program product is also disclosed in an embodiment of the present invention, including a computer program / instruction, which implements the steps of a compiler optimization fault detection method based on an association model when executed by a processor. The program / instruction code for implementing the method of the present invention can be written in any combination of one or more programming languages. These program / instruction codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer or other programmable data processing device, so that the program / instruction code implements the steps of the method of the present invention when executed by the processor or controller. The program / instruction code can be executed entirely on the machine, partially on the machine, partially on the machine as an independent software package, or entirely on a remote machine or server. The parts not described in detail in the present invention are all well-known technologies of those skilled in the art.
Claims
1. A compiler optimization fault detection method based on an association model, characterized in that: The following steps are involved: (1) Mutation-based test program generation method generates new test programs by inserting code snippets into seed programs; (2) constructing a diverse optimization sequence by randomly selecting optimization levels and processing difference sets; (3) Construct training samples, associate the test program with the optimization sequence, input it into the compiler for differential testing, and label the output test results to construct labeled training samples; (4) Convert the test program and optimization sequence in the training sample into a unified vector representation and input it into the SVM support vector machine algorithm to build an association model, and train the SVM model by using the sequence minimum optimization algorithm; (5) Use the trained SVM model to predict the probability of a new test program triggering an optimization failure under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence ranked in the Top-K to perform differential testing on the compiler.
2. The compiler optimization fault detection method based on the association model according to claim 1, characterized in that: In step (1), the mutation-based test program generation method extracts all code snippets of a set structure by means of program structure extraction, and then randomly inserts these code snippets into a seed program to form a number of new test programs.
3. The compiler optimization fault detection method based on the association model according to claim 1, characterized in that: In step (2), the optimization sequence construction method first randomly selects two optimization levels from -O0, -O1, -O2, -O3, and -Os to form an optimization level pair, and identifies a difference set of optimization options between the pair of optimization levels; then randomly deletes one or more optimization options in the difference set, and all elements in the optimization sequence are composed of the remaining optimization options in the difference set and the lower level optimization level in the optimization level pair; finally, these elements are randomly arranged to generate a number of optimization sequences between the two optimization levels.
4. The compiler optimization fault detection method based on the association model according to claim 1, characterized in that: In step (3), the test program and the optimization sequence association method assume that the test program set is P = {p i }(1≤i≤n), the optimized sequence set is S={s j }(1≤j≤m), then the constructed association pair is expressed as R={ <p i ,s j >}(1≤i≤n,1≤j≤m), where n and m are the number of test programs and the number of optimization sequences, respectively.
5. The compiler optimization fault detection method based on association model according to claim 1, characterized in that: In step (3), the label of the training sample is a numerical label 0 or 1. <p i ,s j >If an optimization failure is detected during compiler differential testing, the tag of the association pair is set to 1, otherwise, the tag of the association pair is set to 0.
6. The compiler optimization fault detection method based on association model according to claim 1, characterized in that: In step (4), the CodeBERT model is used to convert the test program and optimization sequence into a unified vector representation to facilitate subsequent machine learning model processing.
7. The compiler optimization fault detection method based on association model according to claim 1, characterized in that: In step (4), the SVM support vector machine algorithm is used to construct an association model between the test program and the optimization sequence, and the SVM model is trained using a sequence minimum optimization algorithm; during the training process, the SVM automatically selects a portion of sample vectors as support vectors, which determine the location of the classification boundary; the optimization problem is expressed as: where α i is the Lagrange multiplier, each sample vector corresponds to an α i , the support vector is α i >0, N′ is the number of support vectors in the training set, ‖.‖ represents the 2-norm of the vector, b is the bias, δ i is the slack variable, C is the regularization parameter, N is the number of training samples, x i is the vector of the test program and the optimized sequence in the ith association pair, y i is the true label of the i-th associated pair 0 or 1, φ(x i ) is the feature vector after being mapped by the kernel function, and subject to represents the constraints that the model must comply with when searching for the optimal solution during the optimization process.
8. The compiler optimization fault detection method based on association model according to claim 1, characterized in that: In step (5), the trained SVM model is used to predict the optimization failure. For each vector x of the test program and the optimization sequence in the association pair to be predicted, the model calculates its position in the feature space and determines its prediction label based on the support vector and the decision function. The decision function is in the form of: Among them, α i is the Lagrange multiplier, x i is the vector of the test program and the optimized sequence in the ith associated pair in the support vector, y i is x i The corresponding label, K(x i ,x) is the Gaussian kernel function, N′ is the number of support vectors, and b is the bias.
9. A compiler optimization fault detection system based on an association model, characterized in that: include: A test program generation module is used for a mutation-based test program generation method to generate a new test program by inserting code snippets into a seed program; Optimization sequence generation module for building diversified optimization sequences by randomly selecting optimization levels and processing difference sets; A training sample construction module is used to construct training samples, associate the test program with the optimization sequence, input it into the compiler for differential testing, and mark the output test results, thereby constructing labeled training samples; The association model training module is used to convert the test program and optimization sequence in the training sample into a unified vector representation and input it into the SVM support vector machine algorithm to build an association model, and train the SVM model by using a sequence minimum optimization algorithm; And the prediction and detection module is used to use the trained SVM model to predict the probability of a new test program triggering an optimization failure under a certain optimization sequence, sort the optimization sequence in descending order according to the predicted probability, and use the new test program and the optimization sequence ranked in the Top-K to perform differential testing on the compiler.
10. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps of a compiler optimization fault detection method based on an association model according to any one of claims 1 to 8 are implemented.
Citation Information
Patent Citations
Method, device and equipment for model training and compiler automatic tuning
CN116860259A
Deep learning compiler test acceleration method based on reinforcement learning
CN118820078A
Methods and apparatus for machine learning-guided compiler optimizations for register-based hardware architectures
US20220121430A1