A program optimization method, device, electronic device and storage medium

By constructing input code vectors and calculating optimization scores, the problem that the compiler cannot determine the appropriate code conversion order by itself is solved, and the highest execution performance optimization of loop code snippets is achieved.

CN119759327BActive Publication Date: 2025-05-30BEI JING BDA NETWORK &INFORMATION CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510272533.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-03-10
Publication Date
2025-05-30
Estimated Expiration
2045-03-10

AI Technical Summary

Technical Problem

Existing compilers cannot determine the appropriate code conversion order by themselves, resulting in the inability to provide the highest optimization performance.

Method used

By obtaining the loop code snippet to be optimized and multiple candidate code conversion sequences provided by the compiler, an input code vector is constructed, and the optimization scores of each candidate code conversion sequence are determined based on these vectors, and the optimization code conversion sequence is finally determined and input to the compiler.

Benefits of technology

Automatically determine the code conversion order with the best optimization performance, improving the execution performance of loop code snippets.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119759327B_ABST
    Figure CN119759327B_ABST
Patent Text Reader

Abstract

The present invention provides a program optimization method, apparatus, electronic device, and storage medium. By obtaining a loop code segment to be optimized and multiple candidate code conversion sequences given by a compiler for the loop code segment, input code vectors corresponding to the loop code segment for each candidate code conversion sequence are constructed, and based on each input code vector, an optimization score of the corresponding candidate code conversion sequence for the loop code segment is determined. Furthermore, based on the optimization scores of each candidate code conversion sequence for the loop code segment, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input to the compiler for the compiler to optimize the loop code segment based on the optimized code conversion sequence, which can automatically determine the code conversion order with the optimal optimization performance and improve the execution performance of the loop code segment.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code optimization, and in particular, to a program optimization method, apparatus, electronic device, and storage medium. Background Art

[0002] Writing high-performance software is essential in many fields. Compared with unoptimized code, highly optimized code can significantly improve the execution speed when executed. For example, in deep learning, the optimized version of state-of-the-art neural network models such as XLNet is 1.8 times faster than the equivalent PyTorch implementation. However, writing highly optimized code requires programmers with relevant experience and is very time-consuming and error-prone. Therefore, one of the long-term goals of compiler designers is to develop compilers that can automatically optimize high-level code. These compilers automatically apply code transformations to make the code run faster, so there is no need for manual program adjustment. Current compilers, such as TIRAMISU, already have mechanisms to provide multiple code transformation options for each program code and apply the selected code transformations to the program, such as loop fission, fusion, parallelization, and vectorization. However, different code transformation orders have an important impact on the effect of program optimization, but current compilers cannot determine the appropriate code transformation order by themselves, thus unable to provide the highest optimization performance. Summary of the Invention

[0003] The present invention provides a program optimization method, apparatus, electronic device, and storage medium to solve the defect in the prior art that the appropriate code transformation order cannot be determined by itself, thus unable to provide the highest optimization performance.

[0004] The present invention provides a program optimization method, including:

[0005] Obtain a loop code segment to be optimized; the loop code segment is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements;

[0006] Obtain multiple candidate code transformation sequences given by the compiler for the loop code segment; any candidate code transformation sequence includes one or more code transformation methods among loop merging, loop blocking, loop swapping, and loop unrolling;

[0007] Construct input code vectors corresponding to the loop code segment for each candidate code transformation sequence, and determine the optimization score of the corresponding candidate code transformation sequence for the loop code segment based on each input code vector;

[0008] Based on the optimization scores of each candidate code transformation sequence for the loop code fragment, determine the optimized code transformation sequence, and input the optimized code transformation sequence into the compiler for the compiler to optimize the loop code fragment based on the optimized code transformation sequence.

[0009] According to a program optimization method provided by the present invention, constructing an input code vector of the loop code fragment corresponding to any candidate code transformation sequence includes:

[0010] Based on the loop control statement of the loop code fragment, determine several loop levels of the loop code fragment;

[0011] Based on the loop control statements corresponding to the several loop levels, determine the loop variables and loop iteration counts of each loop level, and the flags for applying each code transformation method in any candidate code transformation sequence for each loop level;

[0012] For any assignment statement of the loop code fragment, determine the dimensionality of the assignment object and the size of each dimension in the assignment statement, as well as the unique identifier of the object to be read, the memory read mode of the object to be read, and the number of operations of each arithmetic operation in the assignment expression of the assignment statement;

[0013] Combine the loop variables and loop iteration counts of the loop level corresponding to any assignment statement, the flags for applying each code transformation method in the corresponding loop level in any candidate code transformation sequence, the dimensionality of the assignment object and the size of each dimension in the assignment statement, and the unique identifier of the object to be read, the memory read mode of the object to be read, and the number of operations of each arithmetic operation in the assignment expression of the assignment statement to obtain the calculation vector of the assignment statement;

[0014] Wherein, the input code vector of the loop code fragment corresponding to any candidate code transformation sequence includes the calculation vectors of each assignment statement.

[0015] According to a program optimization method provided by the present invention, the memory read mode of the object to be read in the assignment expression of any assignment statement is determined based on the following method:

[0016] Construct a zero-filled access matrix; the number of rows of the access matrix is the dimensionality of the access index of the object to be read in the assignment expression of any assignment statement, and the number of columns of the access matrix is the number of loop variables in the loop code fragment plus 1;

[0017] Update the access matrix based on the access index of the object to be read, to obtain the memory read mode of the object to be read; wherein, any dimension of the access index of the object to be read is the result of a linear transformation of one or more loop variables.

[0018] According to a program optimization method provided by the present invention, determining an optimization score of a corresponding candidate code conversion sequence for the loop code segment based on any input code vector includes:

[0019] Determine the embedding vector of each assignment statement based on the calculation vector of each assignment statement in the any input code vector;

[0020] Construct a call relationship tree according to the abstract syntax tree of the loop code segment; wherein, the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node;

[0021] Traverse the call relationship tree from the leaf nodes of the call relationship tree from bottom to top. For the current non-leaf node, determine the embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node;

[0022] Determine the optimization score of the corresponding candidate code conversion sequence for the loop code segment based on the embedding vector of the root node in the call relationship tree.

[0023] According to a program optimization method provided by the present invention, determining the embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node includes:

[0024] If all the child nodes of the current non-leaf node are leaf nodes, perform semantic extraction on the embedding vectors of the child nodes of the current non-leaf node based on a first long short-term memory network to obtain a first memory vector, and process the first memory vector based on a first feedforward neural network to obtain the embedding vector of the current non-leaf node;

[0025] If there are both non-leaf nodes and leaf nodes among the child nodes of the current non-leaf node, perform semantic extraction on the embedding vectors of the child nodes of the leaf node type based on a first long short-term memory network to obtain a first memory vector, perform semantic extraction on the embedding vectors of the child nodes of the non-leaf node type based on a second long short-term memory network to obtain a second memory vector, and process the first memory vector and the second memory vector based on a first feedforward neural network to obtain the embedding vector of the current non-leaf node;

[0026] If all the child nodes of the current non-leaf node are non-leaf nodes, then semantic extraction is performed on the embedding vectors of the child nodes of the current non-leaf node based on the second long short-term memory network to obtain a second memory vector, and the second memory vector is processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node.

[0027] According to a program optimization method provided by the present invention, determining the embedding vector of each assignment statement based on the calculation vectors of the assignment statements in any one of the input code vectors includes:

[0028] The calculation vectors of the assignment statements in any one of the input code vectors are processed based on the second feedforward neural network to obtain the embedding vectors of the assignment statements.

[0029] According to a program optimization method provided by the present invention, determining the optimization score of a corresponding candidate code transformation sequence for the loop code segment based on the embedding vector of the root node in the call relationship tree includes:

[0030] Regression calculation is performed on the embedding vector of the root node in the call relationship tree based on the third feedforward neural network to obtain the optimization score of the corresponding candidate code transformation sequence for the loop code segment; wherein, the number of layers of the third feedforward neural network is less than that of the first feedforward neural network and the second feedforward neural network.

[0031] The present invention also provides a program optimization device, including:

[0032] A code segment acquisition unit, configured to acquire a loop code segment to be optimized; the loop code segment is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements;

[0033] A code transformation acquisition unit, configured to acquire a plurality of candidate code transformation sequences given by the compiler for the loop code segment; any one of the candidate code transformation sequences includes one or more code transformation methods of loop merging, loop blocking, loop swapping, and loop unrolling;

[0034] An optimization score evaluation unit, configured to construct input code vectors corresponding to the loop code segment for each candidate code transformation sequence, and determine the optimization score of the corresponding candidate code transformation sequence for the loop code segment based on each input code vector;

[0035] A code segment optimization unit, configured to determine an optimized code transformation sequence based on the optimization scores of the candidate code transformation sequences for the loop code segment, and input the optimized code transformation sequence into the compiler for the compiler to optimize the loop code segment based on the optimized code transformation sequence.

[0036] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the processor executes the program, the program optimization method as described in any one of the above is implemented.

[0037] The present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the program optimization method as described in any one of the above is implemented.

[0038] The present invention also provides a computer program product, including a computer program. When the computer program is executed by a processor, the program optimization method as described in any one of the above is implemented.

[0039] A program optimization method, device, electronic device, and storage medium provided by the present invention obtain a loop code segment to be optimized and a plurality of candidate code conversion sequences given by a compiler for the loop code segment, thereby constructing input code vectors corresponding to the loop code segment for each candidate code conversion sequence, and determining an optimization score of the corresponding candidate code conversion sequence for the loop code segment based on each input code vector. Furthermore, based on the optimization scores of each candidate code conversion sequence for the loop code segment, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input to the compiler for the compiler to optimize the loop code segment based on the optimized code conversion sequence, which can automatically determine the code conversion order with the optimal optimization performance and improve the execution performance of the loop code segment. BRIEF DESCRIPTION OF THE DRAWINGS

[0040] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0041] Figure 1 is a flowchart of a program optimization method provided by the present invention;

[0042] Figure 2 is a flowchart of an optimization score evaluation method provided by the present invention;

[0043] Figure 3 is a structural schematic diagram of a program optimization device provided by the present invention;

[0044] Figure 4 is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS

[0045] To make the objectives, technical solutions, and advantages of the present invention clearer, the technical solutions in the present invention will be clearly and completely described below with reference to the accompanying drawings in the present invention. Apparently, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0046] Figure 1 is a schematic flowchart of a program optimization method provided by the present invention. As Figure 1 shown, the method includes:

[0047] Step 110: Obtain a loop code snippet to be optimized; the loop code snippet is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements;

[0048] Step 120: Obtain multiple candidate code transformation sequences given by the compiler for the loop code snippet; any one of the candidate code transformation sequences includes one or more code transformation methods such as loop merging, loop blocking, loop swapping, and loop unrolling;

[0049] Step 130: Construct input code vectors corresponding to the loop code snippet for each candidate code transformation sequence, and determine the optimization scores of the corresponding candidate code transformation sequences for the loop code snippet based on the respective input code vectors;

[0050] Step 140: Determine an optimized code transformation sequence based on the optimization scores of the respective candidate code transformation sequences for the loop code snippet, and input the optimized code transformation sequence into the compiler for the compiler to optimize the loop code snippet based on the optimized code transformation sequence.

[0051] Here, the loop code snippet to be optimized contains statements representing loops in a programming language, such as for loops, while loops, etc., and the loop code snippet is described based on a domain-specific language (DSL) provided by a compiler, which includes loop control statements and assignment statements, and the assignment statements are located in the loop body. In some embodiments, the compiler may be TIRAMISU. For example, in the following loop code snippet, the loop control statement is a statement containing for, such as for (n in 0..batch), and the assignment statement is conv[n, fout, y, x] += weigths[fout, fin, y, x]* input[n, fin, y+k0, x+k1].

[0052] Example: for (n in 0..batch)

[0053] for (fout in 0..out_features)

[0054] for (y in 0..H-2)

[0055] for (x in 0..W-2)

[0056] for (fin in 0..in_features)

[0057] for (k0 in 0..3)

[0058] for (k1 in 0..3)

[0059] conv[n, fout, y, x] += weigths[fout, fin, y, x]* input[n, fin, y+k0,x+k1]

[0060] Subsequently, multiple candidate code transformation sequences given by the compiler for the above loop code snippet are obtained. Among them, any candidate code transformation sequence includes one or more code transformation methods such as loop fusion, loop tiling, loop interchange, and loop unrolling, and the order of the above code transformation methods in different candidate code transformation sequences containing the same type of code transformation method is different. In addition, the code transformation method for each loop level of the above loop code snippet is marked in the candidate code transformation sequence.

[0061] In order to quantify the optimization degree of the code transformation order provided by different candidate code transformation sequences for the loop code snippet to be optimized, input code vectors corresponding to the above loop code snippet for each candidate code transformation sequence can be constructed, and regression analysis can be performed on each input code vector using the regression method, so as to determine the optimization score of the corresponding candidate code transformation sequence for the loop code snippet. Among them, the higher the optimization score of any candidate code transformation sequence for the loop code snippet, the more obvious the improvement in the execution speed of the loop code snippet after applying the candidate code transformation sequence compared to the execution speed of the original loop code snippet. Here, in order to more accurately analyze the speed improvement ability of each candidate code transformation sequence for the loop code snippet, the input code vectors corresponding to each candidate code transformation sequence constructed contain the semantic information of the loop code snippet itself and the semantic information after applying the corresponding candidate code transformation sequence to the loop code snippet.

[0062] In some embodiments, in order to construct an input code vector of a loop code snippet corresponding to any candidate code transformation sequence, several loop levels of the loop code snippet can be determined based on the loop control statements of the loop code snippet. In some embodiments, loop variables of the corresponding loop levels can be used to label the loop levels. Taking the above example as an illustration, the loop levels of the loop code snippet, from high to low, include n, fout, y, x, fin, k0, and k1 in sequence.

[0063] Based on the loop control statements corresponding to each loop level, the loop variables, loop iteration counts, and the labels for applying each code transformation method in the candidate code transformation sequence for each loop level can be determined. Specifically, for any loop level, the loop iteration count can be determined according to the upper and lower bounds of the loop variable in the loop control statement corresponding to that loop level. Taking the loop level n as an example, its loop iteration count is batch + 1. For any loop level and any candidate code transformation sequence, the label for applying each code transformation method in the candidate code transformation sequence for that loop level is a boolean value. A label of 1 for applying any code transformation method indicates that the compiler will optimize that loop level using that code transformation method. It should be noted that this label is determined based on the candidate code transformation sequence.

[0064] For any assignment statement in a loop code snippet, the number of dimensions of the assignment object in the assignment statement, the size of each dimension, the unique identifier of the object to be read in the assignment expression of the assignment statement, the memory read mode of the object to be read, and the number of operations of each arithmetic operation can be determined. Taking the assignment statement B[i,j]=A[i0, i0+i1, i1−2] / 2 as an example, bufferB is the assignment object, its number of dimensions is 2, and the size of each dimension corresponds to the value range of i and j; the assignment expression is A[i0, i0+i1, i1−2], the object to be read is bufferA, its unique identifier is allocated by the system, and the memory read mode of the object to be read indicates the way to read the data of the object to be read from the memory. In some embodiments, a zero-filled access matrix can be constructed, where the number of rows of the access matrix is the number of dimensions of the access index of the object to be read (the number of dimensions is 3 in the above example), the number of columns of the access matrix is the number of loop variables in the loop code snippet plus 1, that is, the last column of the access matrix corresponds to the constant term, and the other columns correspond to each loop variable respectively; subsequently, based on the access index of the object to be read, the access matrix is updated to obtain the memory read mode of the object to be read. It should be noted that any dimension of the access index of the object to be read is the result of a linear transformation of one or more loop variables. It can be seen that each row of the access matrix corresponds to a linear expression of the corresponding dimension of the access index with respect to each loop variable. Taking the assignment statement A[i0, i0+i1, i1−2] / 2 as an example, assuming that the loop variables in the entire loop code snippet are i0, i1, i2, and i3 respectively, then the memory read mode of the object to be read A is:

[0065]

[0066] Subsequently, the loop variables and loop iteration counts of the loop level corresponding to any assignment statement (when any assignment statement is within the loop body of a certain loop level, then the assignment statement corresponds to that loop level, and in the case of loop nesting, an assignment statement can correspond to multiple loop levels), the marks of applying each code transformation method in the corresponding loop level to the candidate code transformation sequence, the number of dimensions of the assignment object in the assignment statement and the size of each dimension, and the unique identifier of the object to be read in the assignment expression of the assignment statement, the memory read mode of the object to be read, and the number of operations of each arithmetic operation are combined to obtain the calculation vector of the assignment statement. After obtaining the calculation vectors of each assignment statement, the calculation vectors of each assignment statement are combined, that is, the input code vector corresponding to the loop code snippet for any candidate code transformation sequence is obtained.

[0067] In some embodiments, as Figure 2 shown, the optimization score of any candidate code transformation sequence for the above loop code snippet can be determined based on the following method:

[0068] Step 210: Determine the embedding vectors of each assignment statement based on the calculation vectors of each assignment statement in any of the input code vectors.

[0069] Step 220: Construct a call relationship tree according to the abstract syntax tree of the loop code snippet; wherein, the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node.

[0070] Step 230: Traverse the call relationship tree from the leaf nodes of the call relationship tree from bottom to top. For the current non-leaf node, determine the embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node.

[0071] Step 240: Determine the optimization score of the corresponding candidate code transformation sequence for the loop code snippet based on the embedding vector of the root node in the call relationship tree.

[0072] Specifically, the embedding vectors of each assignment statement can be determined based on the calculation vectors of each assignment statement in the input code vector corresponding to any candidate code transformation sequence. Here, considering that the value ranges of the values in each dimension of the calculation vector are different, each value in the calculation vector can be logarithmically transformed, and then a feedforward neural network (hereinafter referred to as the second feedforward neural network) is used to process the logarithmically transformed calculation vector to obtain the embedding vectors of each assignment statement.

[0073] In addition, according to the abstract syntax tree of the loop code snippet, its corresponding call relationship tree can be constructed. Among them, the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node (that is, the child nodes are in the loop body of the parent node). Subsequently, traverse the non-leaf nodes in the call relationship tree from the leaf nodes of the call relationship tree from bottom to top. For the currently traversed non-leaf node, the embedding vector of the current non-leaf node can be determined based on the embedding vectors of the child nodes of the current non-leaf node. It should be noted that if the embedding vector of the child node of the current non-leaf node does not exist, the embedding vector of the child node needs to be extracted first, and the extraction method of the embedding vector of the child node is similar to this, and can be determined based on the embedding vectors of the child nodes of the child node. Through the above recursive method, the embedding vector of the current non-leaf node can be determined.

[0074] In some embodiments, to obtain the embedding vector of the current non-leaf node, the types of the child nodes of the current non-leaf node can be determined first. If all the child nodes of the current non-leaf node are leaf nodes, semantic extraction is performed on the embedding vectors of the child nodes of the current non-leaf node based on the first long short-term memory network to obtain a first memory vector, and the first memory vector is processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node; if there are both non-leaf nodes and leaf nodes among the child nodes of the current non-leaf node, semantic extraction is performed on the embedding vectors of the child nodes of the leaf node type based on the first long short-term memory network to obtain a first memory vector, semantic extraction is performed on the embedding vectors of the child nodes of the non-leaf node type based on the second long short-term memory network to obtain a second memory vector, and the first memory vector and the second memory vector are processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node; if all the child nodes of the current non-leaf node are non-leaf nodes, semantic extraction is performed on the embedding vectors of the child nodes of the current non-leaf node based on the second long short-term memory network to obtain a second memory vector, and the second memory vector is processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node. By specifically performing semantic extraction on the embedding vectors of different types of child nodes through different long short-term memory networks, the differences in semantic expressions of different types of child nodes can be fully considered, so as to better extract the semantic information contained in the embedding vectors of each child node.

[0075] Finally, based on the embedding vector of the root node in the call relationship tree, the optimization score of the candidate code transformation sequence for the loop code segment can be determined. In some embodiments, regression calculation can be performed on the embedding vector of the root node in the call relationship tree based on a shallow third feedforward neural network to obtain the optimization score of the candidate code transformation sequence for the loop code segment. Among them, the number of layers of the third feedforward neural network is less than that of the above-mentioned first feedforward neural network and the above-mentioned second feedforward neural network.

[0076] It can be known that the above-mentioned second feedforward neural network, first long short-term memory network, second long short-term memory network, first feedforward neural network, and third feedforward neural network constitute a regression model for quantifying the optimization scores of each candidate code transformation sequence for the loop code segment. This regression model can be trained based on sample loop code segments, multiple candidate code transformation sequences given by the compiler for the sample loop code segments, the actual execution time of the original sample loop code segments, and the actual execution time of the sample loop code segments after the compiler applies each candidate code transformation sequence.

[0077] Subsequently, based on the optimization scores of each candidate code transformation sequence for the loop code snippet, the candidate code transformation sequence with the highest optimization score can be determined as the optimized code transformation sequence, and this optimized code transformation sequence is input into the compiler for the compiler to optimize the loop code snippet based on this optimized code transformation sequence, thereby maximizing the execution performance of the loop code snippet.

[0078] In summary, the method provided by the embodiments of the present invention obtains a loop code snippet to be optimized and multiple candidate code transformation sequences given by the compiler for the loop code snippet, thereby constructing input code vectors corresponding to the loop code snippet for each candidate code transformation sequence, and determining the optimization scores of the corresponding candidate code transformation sequences for the loop code snippet based on each input code vector. Furthermore, based on the optimization scores of each candidate code transformation sequence for the loop code snippet, the optimized code transformation sequence is determined and input into the compiler for the compiler to optimize the loop code snippet based on the optimized code transformation sequence, which can automatically determine the code transformation order with the optimal optimization performance and improve the execution performance of the loop code snippet.

[0079] Next, a program optimization device provided by the present invention will be described. The program optimization device described below can be correspondingly referred to with the program optimization method described above.

[0080] Based on any of the above embodiments, Figure 3 is a schematic structural diagram of a program optimization device provided by the present invention. As Figure 3 shown, the device includes:

[0081] A code snippet acquisition unit 310, configured to acquire a loop code snippet to be optimized; the loop code snippet is described based on a domain-specific language provided by the compiler and includes loop control statements and assignment statements;

[0082] A code transformation acquisition unit 320, configured to acquire multiple candidate code transformation sequences given by the compiler for the loop code snippet; any candidate code transformation sequence includes one or more code transformation methods such as loop merging, loop blocking, loop swapping, and loop unrolling;

[0083] An optimization score evaluation unit 330, configured to construct input code vectors corresponding to the loop code snippet for each candidate code transformation sequence, and determine the optimization scores of the corresponding candidate code transformation sequences for the loop code snippet based on each input code vector;

[0084] A code snippet optimization unit 340 is configured to determine an optimized code transformation sequence based on the optimization scores of each candidate code transformation sequence for the loop code snippet, and input the optimized code transformation sequence into the compiler for the compiler to optimize the loop code snippet based on the optimized code transformation sequence.

[0085] The device provided by the embodiment of the present invention constructs an input code vector corresponding to each candidate code transformation sequence for the loop code snippet by obtaining the loop code snippet to be optimized and multiple candidate code transformation sequences given by the compiler for the loop code snippet, determines the optimization scores of the corresponding candidate code transformation sequences for the loop code snippet based on each input code vector, further determines the optimized code transformation sequence based on the optimization scores of each candidate code transformation sequence for the loop code snippet, and inputs the optimized code transformation sequence into the compiler for the compiler to optimize the loop code snippet based on the optimized code transformation sequence, so as to automatically determine the code transformation order with the optimal optimization performance and improve the execution performance of the loop code snippet.

[0086] Based on any of the above embodiments, constructing the input code vector corresponding to any candidate code transformation sequence for the loop code snippet includes:

[0087] Determine several loop levels of the loop code snippet based on the loop control statement of the loop code snippet;

[0088] Determine the loop variables and loop iteration times of each loop level, and the flags of each code transformation method in the any candidate code transformation sequence applied to each loop level based on the loop control statements corresponding to the several loop levels;

[0089] For any assignment statement of the loop code snippet, determine the dimension number of the assignment object in the any assignment statement and the size of each dimension, as well as the unique identifier of the object to be read, the memory read mode of the object to be read, and the number of operations of each arithmetic operation in the assignment expression of the any assignment statement;

[0090] Combine the loop variables and loop iteration times of the loop level corresponding to any assignment statement, the flags of each code transformation method in the any candidate code transformation sequence applied to the corresponding loop level, the dimension number of the assignment object in the any assignment statement and the size of each dimension, as well as the unique identifier of the object to be read, the memory read mode of the object to be read, and the number of operations of each arithmetic operation in the assignment expression of the any assignment statement to obtain the calculation vector of the any assignment statement;

[0091] Wherein, the input code vector corresponding to any candidate code transformation sequence for the loop code snippet includes the calculation vectors of each assignment statement.

[0092] Based on any of the above embodiments, the memory reading mode of the object to be read in the assignment expression of any assignment statement is determined based on the following method:

[0093] Construct an access matrix of all zeros; the number of rows of the access matrix is the number of dimensions of the access index of the object to be read in the assignment expression of any assignment statement, and the number of columns of the access matrix is the number of loop variables in the loop code segment plus 1;

[0094] Based on the access index of the object to be read, update the access matrix to obtain the memory reading mode of the object to be read; wherein, any dimension of the access index of the object to be read is the result of a linear transformation of one or more loop variables.

[0095] Based on any of the above embodiments, determining the optimization score of a corresponding candidate code conversion sequence for the loop code segment based on any input code vector includes:

[0096] Based on the calculation vectors of the assignment statements in any input code vector, determine the embedding vectors of the assignment statements;

[0097] According to the abstract syntax tree of the loop code segment, construct a call relationship tree; wherein, the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node;

[0098] Traverse the call relationship tree from the leaf nodes of the call relationship tree from bottom to top. For the current non-leaf node, determine the embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node;

[0099] Based on the embedding vector of the root node in the call relationship tree, determine the optimization score of the corresponding candidate code conversion sequence for the loop code segment.

[0100] Based on any of the above embodiments, determining the embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node includes:

[0101] If all the child nodes of the current non-leaf node are leaf nodes, perform semantic extraction on the embedding vectors of the child nodes of the current non-leaf node based on a first long short-term memory network to obtain a first memory vector, and process the first memory vector based on a first feedforward neural network to obtain the embedding vector of the current non-leaf node;

[0102] If there are both non - leaf nodes and leaf nodes among the child nodes of the current non - leaf node, semantic extraction is performed on the embedding vectors of the child nodes of the leaf - node type based on the first long - short - term memory network to obtain a first memory vector, semantic extraction is performed on the embedding vectors of the child nodes of the non - leaf - node type based on the second long - short - term memory network to obtain a second memory vector, and the first feed - forward neural network is used to process the first memory vector and the second memory vector to obtain the embedding vector of the current non - leaf node;

[0103] If all the child nodes of the current non - leaf node are non - leaf nodes, semantic extraction is performed on the embedding vectors of the child nodes of the current non - leaf node based on the second long - short - term memory network to obtain a second memory vector, and the first feed - forward neural network is used to process the second memory vector to obtain the embedding vector of the current non - leaf node.

[0104] Based on any of the above - mentioned embodiments, determining the embedding vectors of the respective assignment statements based on the calculation vectors of the respective assignment statements in any of the input code vectors includes:

[0105] The second feed - forward neural network is used to process the calculation vectors of the respective assignment statements in any of the input code vectors to obtain the embedding vectors of the respective assignment statements.

[0106] Based on any of the above - mentioned embodiments, determining the optimization score of the corresponding candidate code transformation sequence for the loop code segment based on the embedding vector of the root node in the call relationship tree includes:

[0107] Regression calculation is performed on the embedding vector of the root node in the call relationship tree based on the third feed - forward neural network to obtain the optimization score of the corresponding candidate code transformation sequence for the loop code segment; wherein, the number of layers of the third feed - forward neural network is less than that of the first feed - forward neural network and the second feed - forward neural network.

[0108] Figure 4 It is a schematic structural diagram of the electronic device provided by the present invention, as Figure 4As shown in the figure, the electronic device may include: a processor 410, a memory 420, a communications interface 430, and a communication bus 440. Among them, the processor 410, the memory 420, and the communication interface 430 complete communication with each other through the communication bus 440. The processor 410 may call the logical instructions in the memory 420 to execute a program optimization method, which includes: obtaining a loop code segment to be optimized; the loop code segment is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements; obtaining a plurality of candidate code transformation sequences given by the compiler for the loop code segment; any candidate code transformation sequence includes one or more code transformation methods among loop merging, loop blocking, loop swapping, and loop unrolling; constructing an input code vector corresponding to the loop code segment for each candidate code transformation sequence, and determining an optimization score of the corresponding candidate code transformation sequence for the loop code segment based on each input code vector; determining an optimized code transformation sequence based on the optimization scores of each candidate code transformation sequence for the loop code segment, and inputting the optimized code transformation sequence into the compiler for the compiler to optimize the loop code segment based on the optimized code transformation sequence.

[0109] In addition, when the logical instructions in the above-mentioned memory 420 are implemented in the form of software functional units and sold or used as an independent product, they may be stored in a computer-readable storage medium. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art, or a part of this technical solution, may be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The foregoing storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs that can store program codes.

[0110] On the other hand, the present invention also provides a computer program product. The computer program product includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the program optimization method provided by each of the above methods. The method includes: obtaining a loop code segment to be optimized; the loop code segment is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements; obtaining a plurality of candidate code transformation sequences given by the compiler for the loop code segment; any one of the candidate code transformation sequences includes one or more code transformation methods among loop merging, loop blocking, loop swapping, and loop unrolling; constructing input code vectors corresponding to the loop code segment for each of the candidate code transformation sequences, and determining an optimization score of the corresponding candidate code transformation sequence for the loop code segment based on each input code vector; determining an optimized code transformation sequence based on the optimization scores of each candidate code transformation sequence for the loop code segment, and inputting the optimized code transformation sequence into the compiler for the compiler to optimize the loop code segment based on the optimized code transformation sequence.

[0111] In another aspect, the present invention also provides a non-transitory computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, it is configured to execute the program optimization method provided by each of the above. The method includes: obtaining a loop code segment to be optimized; the loop code segment is described based on a domain-specific language provided by a compiler and includes loop control statements and assignment statements; obtaining a plurality of candidate code transformation sequences given by the compiler for the loop code segment; any one of the candidate code transformation sequences includes one or more code transformation methods among loop merging, loop blocking, loop swapping, and loop unrolling; constructing input code vectors corresponding to the loop code segment for each of the candidate code transformation sequences, and determining an optimization score of the corresponding candidate code transformation sequence for the loop code segment based on each input code vector; determining an optimized code transformation sequence based on the optimization scores of each candidate code transformation sequence for the loop code segment, and inputting the optimized code transformation sequence into the compiler for the compiler to optimize the loop code segment based on the optimized code transformation sequence.

[0112] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed to multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment. A person of ordinary skill in the art can understand and implement it without creative effort.

[0113] Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on such an understanding, the essence of the above technical solution, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0114] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit them; although the present invention has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions described in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of each embodiment of the present invention.

Claims

1. A program optimization method, characterized in that: include: Get the loop code snippet to be optimized; The loop code snippet is described based on a domain-specific language provided by a compiler, and includes loop control statements and assignment statements; Acquire multiple candidate code conversion sequences given by the compiler for the loop code fragment; any candidate code conversion sequence includes one or more code conversion modes among loop merging, loop blocking, loop swapping and loop unrolling; Constructing input code vectors of the loop code snippets corresponding to the candidate code conversion sequences respectively, and determining an optimization score of the corresponding candidate code conversion sequence for the loop code snippet based on the input code vectors; The input code vector of the loop code snippet corresponding to any candidate code conversion sequence includes calculation vectors of each assignment statement, and the calculation vector of any assignment statement includes loop variables and loop iteration times of the loop level corresponding to any assignment statement, marks of the corresponding loop level applying each code conversion mode in any candidate code conversion sequence, the number of dimensions of the assignment object in any assignment statement and the size of each dimension, and the unique identifier of the object to be read in the assignment expression of any assignment statement, the memory reading mode of the object to be read, and the number of operations of each arithmetic operation; Determine an optimized code conversion sequence based on the optimization scores of each candidate code conversion sequence for the loop code fragment, and input the optimized code conversion sequence to the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence; Determining an optimization score of a corresponding candidate code conversion sequence for the loop code segment based on any input code vector includes: Determining an embedding vector of each assignment statement based on a calculation vector of each assignment statement in any one of the input code vectors; According to the abstract syntax tree of the loop code snippet, a call relationship tree is constructed; wherein the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node; Traversing the call relationship tree from the bottom to the top starting from the leaf node of the call relationship tree, and for a current non-leaf node, determining an embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node; Based on the embedding vector of the root node in the call relationship tree, an optimization score of a corresponding candidate code conversion sequence for the loop code fragment is determined.

2. The program optimization method according to claim 1, characterized in that: The computation vector of any assignment statement is constructed based on the following steps: Determining a plurality of loop levels of the loop code snippet based on a loop control statement of the loop code snippet; Based on the loop control statements corresponding to the plurality of loop levels, determining loop variables and loop iteration times of each loop level, and markings of applying each code conversion mode in any candidate code conversion sequence to each loop level; For any assignment statement of the loop code snippet, determine the number of dimensions and the size of each dimension of the assignment object in the any assignment statement, as well as the unique identifier of the object to be read, the memory reading mode of the object to be read, and the number of operations of each arithmetic operation in the assignment expression of the any assignment statement; The loop variables and the number of loop iterations of the loop level corresponding to any assignment statement, the marks of each code conversion method in any candidate code conversion sequence applied to the corresponding loop level, the number of dimensions of the assignment object and the size of each dimension in any assignment statement, and the unique identifier of the object to be read in the assignment expression of any assignment statement, the memory reading mode of the object to be read and the number of operations of each arithmetic operation are combined to obtain a calculation vector of any assignment statement.

3. The program optimization method according to claim 2, characterized in that: The memory read mode of the object to be read in the assignment expression of any assignment statement is determined based on the following method: Constructing an all-zero access matrix; the number of rows of the access matrix is ​​the number of dimensions of the access index of the object to be read in the assignment expression of any assignment statement, and the number of columns of the access matrix is ​​the number of loop variables in the loop code fragment plus 1; Based on the access index of the object to be read, the access matrix is ​​updated to obtain the memory read mode of the object to be read; wherein any dimension of the access index of the object to be read is the result of a linear transformation of one or more loop variables.

4. The program optimization method according to claim 1, characterized in that: The determining the embedding vector of the current non-leaf node based on the embedding vector of the child node of the current non-leaf node includes: If all the child nodes of the current non-leaf node are leaf nodes, semantic extraction is performed on the embedding vectors of the child nodes of the current non-leaf node based on the first long short-term memory network to obtain a first memory vector, and the first memory vector is processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node; If there are both non-leaf nodes and leaf nodes in the child nodes of the current non-leaf node, semantic extraction is performed on the embedding vector of the child node of the leaf node type based on the first long short-term memory network to obtain a first memory vector, semantic extraction is performed on the embedding vector of the child node of the non-leaf node type based on the second long short-term memory network to obtain a second memory vector, and the first memory vector and the second memory vector are processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node; If all the child nodes of the current non-leaf node are non-leaf nodes, semantic extraction is performed on the embedding vectors of the child nodes of the current non-leaf node based on the second long short-term memory network to obtain a second memory vector, and the second memory vector is processed based on the first feedforward neural network to obtain the embedding vector of the current non-leaf node.

5. The program optimization method according to claim 4, characterized in that: The step of determining the embedding vector of each assignment statement based on the calculation vector of each assignment statement in any one of the input code vectors comprises: Based on the second feedforward neural network, the calculation vector of each assignment statement in any input code vector is processed to obtain the embedding vector of each assignment statement.

6. The program optimization method according to claim 5, characterized in that: The step of determining the optimization score of the corresponding candidate code conversion sequence for the loop code snippet based on the embedding vector of the root node in the call relationship tree includes: Based on the third feedforward neural network, a regression calculation is performed on the embedding vector of the root node in the call relationship tree to obtain an optimization score of the corresponding candidate code conversion sequence for the loop code fragment; wherein the number of layers of the third feedforward neural network is smaller than that of the first feedforward neural network and the second feedforward neural network.

7. A program optimization device, characterized in that: include: A code snippet acquisition unit, used for acquiring a loop code snippet to be optimized; The loop code snippet is described based on a domain-specific language provided by a compiler, and includes loop control statements and assignment statements; A code conversion acquisition unit, used to acquire a plurality of candidate code conversion sequences given by the compiler for the loop code fragment; any candidate code conversion sequence includes one or more code conversion modes of loop merging, loop blocking, loop swapping and loop unrolling; An optimization score evaluation unit, used to construct input code vectors of the loop code fragments corresponding to the respective candidate code conversion sequences, and determine the optimization score of the corresponding candidate code conversion sequence for the loop code fragment based on the respective input code vectors; The input code vector of the loop code snippet corresponding to any candidate code conversion sequence includes calculation vectors of each assignment statement, and the calculation vector of any assignment statement includes loop variables and loop iteration times of the loop level corresponding to any assignment statement, marks of the corresponding loop level applying each code conversion mode in any candidate code conversion sequence, the number of dimensions of the assignment object in any assignment statement and the size of each dimension, and the unique identifier of the object to be read in the assignment expression of any assignment statement, the memory reading mode of the object to be read, and the number of operations of each arithmetic operation; A code fragment optimization unit, configured to determine an optimized code conversion sequence based on the optimization scores of each candidate code conversion sequence for the loop code fragment, and input the optimized code conversion sequence to the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence; Determining an optimization score of a corresponding candidate code conversion sequence for the loop code segment based on any input code vector includes: Determining an embedding vector of each assignment statement based on a calculation vector of each assignment statement in any one of the input code vectors; According to the abstract syntax tree of the loop code snippet, a call relationship tree is constructed; wherein the leaf nodes of the call relationship tree are assignment statements, the root node is the outermost loop control statement, and the child nodes are within the control range of the parent node; Traversing the call relationship tree from the bottom to the top starting from the leaf node of the call relationship tree, and for a current non-leaf node, determining an embedding vector of the current non-leaf node based on the embedding vectors of the child nodes of the current non-leaf node; Based on the embedding vector of the root node in the call relationship tree, an optimization score of a corresponding candidate code conversion sequence for the loop code fragment is determined.

8. 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 program, the program optimization method according to any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the program optimization method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Code completion method based on abstract syntax tree code representation

    CN116700780A

  • Deep learning compilation optimization method and device, electronic equipment and storage medium

    CN118780351A