Compiler optimization fault detection method and system based on association model

By generating mutation test programs and constructing diverse optimization sequences, combined with the SVM (Support Vector Machine) algorithm, efficient and accurate compiler optimization fault detection is achieved, solving the systemic deficiencies in compiler optimization fault detection and improving the reliability and ease of use of the compiler.

CN119902976BActive Publication Date: 2025-11-28NANJING UNIV OF AERONAUTICS & ASTRONAUTICS
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411826472.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-12
Publication Date
2025-11-28
Estimated Expiration
2044-12-12

AI Technical Summary

Technical Problem

The lack of systematic research on compiler optimization fault detection in existing technologies leads to poor compiler usability and may cause catastrophic consequences in safety-critical areas.

Method used

A compiler optimization fault detection method based on an association model is adopted. This method generates mutation test programs, constructs diverse optimization sequences, builds training samples, and uses the SVM support vector machine algorithm to construct an association model, predicts the probability of optimization faults, and performs differential testing.

Benefits of technology

It improves the efficiency and accuracy of compiler optimization fault detection, ensures the reliability of compiler optimization, and enhances the ease of use and security of the compiler.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119902976B_ABST
    Figure CN119902976B_ABST
Patent Text Reader

Abstract

The application discloses a compiler optimization fault detection method and system based on an association model, and first generates a test program and constructs a diversified optimization sequence by randomly selecting an optimization level and processing a difference set; then constructs a training sample, associates the test program and the optimization sequence, and inputs them into a compiler for differential testing, marks the output test result, and constructs a training sample with a label; then converts the test program and the optimization sequence in the training sample into a vector representation form, constructs an association model based on a support vector machine algorithm; finally, uses the trained SVM model to predict the probability of triggering an optimization fault of a new test program under a certain optimization sequence, sorts the optimization sequences in descending order according to the predicted probability, uses the new test program and the optimization sequences in the Top-K to perform differential testing on the compiler. The application can improve the detection efficiency and accuracy of the compiler optimization fault, and provides a guarantee for the reliability of the compiler optimization.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of compiler optimization and compiler testing, and particularly relates to an optimization fault testing method and system for the back end of a compiler. BACKGROUND

[0002] Compilers play a crucial role in modern software development, not only converting high-level programming languages into machine code, but also generating optimized code to improve program execution efficiency. However, due to the complexity of compiler optimization, compiler optimization is one of the most prone to faults. These faults seriously affect the ease of use of the compiler and may cause catastrophic consequences in safety-critical areas. Therefore, effective testing techniques are crucial for faults caused by compiler optimization.

[0003] In recent years, although research on compiler fault detection has been emerging, there is still a lack of systematic research on compiler faults caused by compiler optimization. Compiler optimization is diverse and complex, and the influence between them is complex. Therefore, it is an urgent task to design and implement an efficient compiler optimization fault detection mechanism. The present application aims to explore a compiler optimization fault detection mechanism based on the association model between test cases and optimization sequences. By understanding and utilizing the relationship between test cases and compiler optimization sequences, the detection efficiency is improved, thereby solving the problem of faults caused by compiler optimization. SUMMARY

[0004] The present application 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-mentioned purpose, the present application adopts the following technical solutions:

[0006] In a first aspect, the present application provides a compiler optimization fault detection method based on an association model, comprising the following steps:

[0007] (1) A test program generation method based on mutation, which generates a new test program by inserting code segments in a seed program;

[0008] (2) A diversified optimization sequence is constructed by randomly selecting optimization levels and processing difference sets;

[0009] (3) A training sample is constructed, the test program and the optimization sequence are associated, and they are input into the compiler for differential testing, the output test results are labeled, and a labeled training sample is constructed;

[0010] (4) Convert the test program and optimization sequence in the training sample into a unified vector representation form and input it into the SVM support vector machine algorithm to build a correlation 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 triggering an optimization fault of a new test program under a certain optimization sequence, sort the optimization sequences in descending order according to the predicted probability, and use the new test program and the optimization sequences in the Top-K to perform differential testing on the compiler.

[0012] Further, in step (1), the test program generation method based on mutation extracts all code fragments of the set structure by program structure extraction, and then randomly inserts these code fragments into the seed program to form a plurality of new test programs.

[0013] Further, 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 the difference set of optimization options between the optimization level pair; 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, randomly arrange these elements to generate a plurality of optimization sequences between the two optimization levels.

[0014] Further, in step (3), the test program and optimization sequence correlation method assumes that the test program set is P={p i}(1≤i≤n), and the optimization sequence set is S={s j}(1≤j≤m), and the correlation pair constructed is 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] Further, in step (3), the label of the training sample is a numerical label 0 or 1, and when the constructed correlation pair <p i ,s j > detects an optimization fault in the compiler differential testing, the label of the correlation pair is set to 1, otherwise, the label of the correlation pair is set to 0.

[0016] Further, in step (4), the CodeBERT model is used to convert the test program and the optimization sequence into a unified vector representation form for subsequent machine learning model processing.

[0017] Further, in step (4), the SVM support vector machine algorithm is used to build the association model between the test program and the optimization sequence, and a sequential minimal optimization algorithm is used to train the SVM model; in the training process, the SVM automatically selects a part of sample vectors as support vectors, and these support vectors determine the position of the classification boundary; the optimization problem is represented as:

[0018]

[0019] where α i is the Lagrange multiplier, each sample vector corresponds to an α i , the support vector is the training sample vector with α 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 optimization sequence in the ith association pair, y i is the true label 0 or 1 of the ith association pair, φ(x i ) is the feature vector mapped by the kernel function, and subject to represents the constraint condition that the model must comply with when finding the optimal solution in the optimization process.

[0020] Further, in step (5), the trained SVM model is used to predict the optimization fault, 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 predicted label according to the support vector and the decision function, and the decision function is in the form of:

[0021]

[0022] where K(x i ,x) is a Gaussian kernel function.

[0023] In a second aspect, the present application provides a compiler optimization fault detection system based on an association model, comprising:

[0024] A test program generation module is used to generate a new test program by inserting a code segment into a seed program based on a mutation-based test program generation method;

[0025] An optimization sequence generation module is used to construct a diversified optimization sequence by randomly selecting an optimization level and processing a difference set;

[0026] A training sample construction module is used to construct a training sample, associate the test program and the optimization sequence, and input them into a compiler for differential testing, mark the output test results, and thereby construct a training sample with labels;

[0027] The association model training module is used to convert the test program and optimization sequence in the training samples into a unified vector representation and input it into the SVM support vector machine algorithm to build the association model. The SVM model is trained by using the sequence minimum optimization algorithm.

[0028] It also includes a prediction and detection module, which uses a trained SVM model to predict the probability that a new test program will trigger an optimization failure under a certain optimization sequence, sorts the optimization sequence in descending order according to the predicted probability, and performs differential testing on the compiler using the new test program and the optimization sequence sorted in the Top-K.

[0029] Thirdly, the present invention provides a computer program product, including a computer program / instruction, wherein when the computer program / instruction is executed by a processor, it implements the steps of the compiler optimization fault detection method based on an association model.

[0030] Beneficial Effects: Compared with existing technologies, the advantages of this invention are as follows: 1. This invention, through a mutation-based test program generation method and an optimized sequence generation method, constructs samples of correlated test programs and optimized sequences. Based on the SVM (Support Vector Machine) algorithm, it builds a correlation model, achieving high hit rate and accuracy in detecting optimization faults on GCC and LLVM compilers. 2. The correlation model-based detection method of this invention improves the efficiency and accuracy of compiler optimization fault detection, ensuring the reliability of compiler optimization. Attached Figure Description

[0031] Figure 1 The flowchart of the compiler optimization fault detection method based on the association model provided by the present invention is shown. Detailed Implementation

[0032] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments.

[0033] like Figure 1 As shown in the figure, the compiler optimization fault detection method based on the association model disclosed in this invention has the following main steps:

[0034] Step 1, a mutation-based test program generation method, generates new test programs by inserting code snippets into the seed program.

[0035] Specifically, in step 1, the mutation-based test program generation method extracts code fragments of all defined structures through program structure extraction, and then randomly inserts these code fragments into the seed program to form several new test programs. 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 the program features, a total of nine program structures are extracted, which are If statement, DoWhile statement, While statement, For statement, Switch statement, Return statement, Goto statement, Assignment statement and Case statement;

[0037] Step 102, program synthesis: a new test program is generated by inserting the code snippet into the seed program.

[0038] Exemplarily, the seed program and the code snippet are represented as an abstract syntax tree by the pycparser tool, a node in the abstract syntax tree of the seed program is randomly selected, and the syntax tree of the code snippet is inserted into the abstract syntax tree of the seed program, thereby generating a new abstract syntax tree, and after the new abstract syntax is converted into code, a new program is generated. In addition to inserting the code snippet, the new code also inserts the verification function transparent_crc(), which is used to test whether the verification values are the same under different compilers or different optimization sequences. Specifically, when the program is compiled using different versions of the compiler or under different optimization sequences, if the verification value of a variable after a certain compilation is inconsistent with that under other conditions, an optimization fault may occur.

[0039] Step 2, construct a diversified optimization sequence by randomly selecting an optimization level and a processing difference set.

[0040] Specifically, the optimization sequence construction method in this 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 the difference set of optimization options between the pair.

[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. By randomly arranging these elements, a plurality of optimization sequences between the two optimization levels are generated.

[0043] Step 3, construct a training sample, associate the test program with the optimization sequence, and input it into the compiler for differential testing, mark the output test result, and thereby construct a labeled training sample.

[0044] Specifically, the test program and optimization sequence association method in this step 3 includes:

[0045] Step 301, assuming that the test program set is P = {p i}(1≤i≤n), the optimization 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 represented 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, input the constructed association pairs into GCC and LLVM compilers for compilation, and obtain the executable files output by the compilers. If the running results of the executable files are inconsistent, the association pair triggers an optimization failure of a certain compiler, and the label is set to 1; if the running results of the executable files are consistent, the association pair does not trigger the optimization failure of the compiler, and the label is set to 0. This step is iterated until all constructed association pairs have corresponding labels, thereby constructing a labeled training sample.

[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 construct an association model, and train the SVM model by using the sequence minimum optimization algorithm.

[0048] Specifically, step 4 in this embodiment includes:

[0049] Step 401, convert the test program and optimization sequence into a unified vector representation using the CodeBERT model. Specifically, first read all files in the data set and read their contents as code text code_text. Use the given tokenizer to split the code text into tokens, and convert them into corresponding token IDs. Then input the code identifiers into the pre-trained CodeBERT model to obtain the corresponding code vectors, convert the code identifiers into PyTorch tensors, adjust them to conform to the shape required by the model input, and obtain the vector representation through model calculation. In order to obtain the average vector representation of the entire code text, perform average pooling operation on the code vectors, use the torch.mean function to calculate the average along the first dimension (i.e. the dimension of the code token), and obtain the average value of each code vector. 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 construct an association model between the test program and the optimization sequence. The detailed steps are as follows:

[0051] Step 4021, the training set is sorted and preprocessed, each training sample is composed of a test program vector and an optimization sequence vector, and each data point has a corresponding label, then the test program and optimization sequence vectors are 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 training and test sets in a ratio of 9:1.

[0052] Step 4022, build an SVM optimization problem, find the optimal hyperplane by solving a convex quadratic optimization problem to maximize the margin of support vectors to the classification boundary. This optimization problem can be represented as:

[0053]

[0054] where α i is the Lagrange multiplier, each sample vector corresponds to an α i , the support vector is the training sample vector with α 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 test program and optimization sequence in the ith associated pair, y i is the true label 0 or 1 of the ith associated pair, φ(x i ) is the feature vector mapped by the kernel function, subject to represents the constraint that the model must follow when finding the optimal solution in the optimization process.

[0055] Step 4023, train the SVM model by solving the above optimization problem using the Sequential Minimal Optimization (SMO) algorithm. During training, SVM will automatically select 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 model generalization ability. Select the parameter combination that achieves 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 fault under a certain optimization sequence, sort the optimization sequences in descending order according to the predicted probability, and use the new test program and the Top-K optimization sequences 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 that needs to be predicted, the model calculates its position in the feature space and determines its predicted label according to the support vector and decision function. The decision function is in the form of:

[0060]

[0061] where 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 support vector corresponding to the i non-zero determines the position of the classification boundary. If , the sign function outputs +1, that is, the predicted class is positive. If , the sign function outputs -1, that is, the predicted class is negative.

[0062] Specifically, the Gaussian kernel function is defined as follows:

[0063]

[0064] where σ>0 and is sufficiently small.

[0065] Step 502, according to the probability predicted by the association model, the association pairs of test programs and optimization sequences are sorted in descending order, and the optimization sequences ranked in the Top-K are selected and input 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, then select the association pairs composed of the optimization sequences ranked in the Top-10 as the input of the compiler.

[0067] Step 5022, collect the running results of GCC and LLVM compilers and compare them. For the same association pair, if the running results of the executable files output by different compilers are inconsistent, the association pair can trigger the compiler optimization fault; otherwise, the association pair cannot trigger the compiler optimization fault.

[0068] Based on the same inventive concept, the embodiment of the present application discloses a compiler optimization fault detection system based on an association model, comprising: a test program generation module, configured to generate a new test program by inserting a code segment in a seed program based on a test program generation method of variation; an optimization sequence generation module, configured to construct a diversified optimization sequence by randomly selecting an optimization level and processing a difference set; a training sample construction module, configured to construct a training sample, associate the test program and the optimization sequence, and input them into a compiler for differential testing, mark the output test result, and thereby construct a training sample with a label; an association model training module, configured to convert the test program and the optimization sequence in the training sample into a unified vector representation form, input them into an 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, configured 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 sequences in descending order according to the predicted probability, use the new test program and the optimization sequences in the Top-K to perform differential testing on the compiler. The detailed implementation process of each module is described in the above method embodiment, and will not be repeated here.

[0069] The embodiment of the present application also discloses a computer program product comprising computer programs / instructions, which, when executed by a processor, implement the steps of the compiler optimization fault detection method based on an association model. The program / instruction code for implementing the method of the present application can be written in any combination of one or more programming languages. The program / instruction code can be provided to the processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing apparatus, so that the program / instruction code, when executed by the processor or controller, causes the steps of the method of the present application to be implemented. The program / instruction code can be executed entirely on a machine, partially on a machine, partially on a machine as a separate software package, and partially on a remote machine, or entirely on a remote machine or server. The details of the present application that are not described are known to those skilled in the art.

Claims

1. A method for compiler optimization fault detection based on association model, characterized in that, The method comprises the following steps: (1) a mutation-based test program generation method generates new test programs by inserting code fragments in a seed program; (2) a diversified optimization sequence is constructed by randomly selecting optimization levels and processing a difference set; (3) a training sample is constructed, the test program and the optimization sequence are associated, and the training sample is input into a compiler for differential testing, the test result is marked, and a training sample with a label is constructed; (4) the test program and the optimization sequence in the training sample are converted into a unified vector representation form and input into an SVM support vector machine algorithm to construct an association model, and the SVM model is trained by using a sequential minimal optimization algorithm; (5) the trained SVM model is used to predict the probability of triggering an optimization fault of a new test program under a certain optimization sequence, the optimization sequences are sorted in descending order according to the predicted probability, and the compiler is subjected to differential testing by using the new test program and the optimization sequences in the Top-K optimization sequences.

2. The compiler optimization fault detection method based on association model according to claim 1, characterized in that, In step (1), the mutation-based test program generation method extracts all code fragments of a set structure by program structure extraction, and then randomly inserts the code fragments into a seed program to form several new test programs.

3. The compiler optimization fault detection method based on 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 optimization level pair; then one or more optimization options in the difference set are randomly deleted, 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, the elements are randomly arranged to generate several optimization sequences between the two optimization levels.

4. The compiler optimization fault detection method based on association model according to claim 1, characterized in that, In step (3), the test procedure and optimization sequence association method assumes that the test procedure set is P = {p i}(1≤i≤n), and the optimization sequence set is S = {s j}(1≤j≤m), and the constructed association pair is represented as R = {<p i ,s j >}(1≤i≤n,1≤j≤m), where n and m are the number of test procedures 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, when the constructed correlation pair <p i ,s j > In the compiler differential test, an optimization fault is detected, the label of the correlation pair is set to 1, otherwise, the label of the correlation 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 the optimization sequence into a unified vector representation form for subsequent machine learning model processing.

7. The correlation model based compiler optimization fault detection method of claim 1, wherein, 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 by using a sequential minimal optimization algorithm; during the training process, the SVM automatically selects a part of sample vectors as support vectors, and the support vectors determine the position of the classification boundary; the optimization problem is represented as: where a i is the Lagrange multiplier, each sample vector corresponds to one a i , the support vectors are the training sample vectors with a i > 0, N' is the number of support vectors in the training set, ||.|| represents the 2-norm of a vector, b is the bias, d 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 associated pair, y i is the true label 0 or 1 of the ith associated pair, f(x i ) is the feature vector mapped by the kernel function, subject to represents the constraint conditions that the model must comply with when finding the optimal solution in 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 an optimization fault, for the vector x of the test program and the optimization sequence in each association pair to be predicted, the model calculates the position of the vector x in the feature space, and determines the predicted label according to the support vector and the decision function, and the decision function is in the form of: Where, α i It is a Lagrange multiplier, x i It is the vector of the test procedure and the optimized sequence in the i-th association pair of the support vectors, y i It 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 association models, characterized in that, The method comprises the following steps: a test program generation module is configured to generate new test programs by inserting code fragments in a seed program based on a mutation-based test program generation method; an optimization sequence generation module is configured to construct a diversified optimization sequence by randomly selecting optimization levels and processing a difference set; a training sample construction module is configured to construct a training sample, associate the test program and the optimization sequence, and input the training sample into a compiler for differential testing, mark the test result, and construct a training sample with a label; and a training sample construction module is configured to construct a training sample, associate the test program and the optimization sequence, and input the training sample into a compiler for differential testing, mark the test result, and construct a training sample with a label; and The correlation model training module is configured to convert the test program and the optimization sequence in the training sample into a unified vector representation form, input the unified vector representation form into an SVM support vector machine algorithm to construct a correlation model, and train the SVM model by using a sequential minimal optimization algorithm; The prediction and detection module is configured to use the trained SVM model to predict a probability that a new test program triggers an optimization fault under a certain optimization sequence, sort the optimization sequences in descending order according to the predicted probability, and use the new test program and the optimization sequences in the Top-K optimization sequences to perform differential testing on the compiler.

10. A computer program product comprising computer programs / instructions, characterized in that, The computer program / instruction is executed by the processor to implement the steps of the compiler optimization fault detection method based on the correlation model according to any one of claims 1-8.

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