Program optimization method, device, electronic device and storage medium

By constructing input code vectors and calculating optimization scores to determine the optimized code conversion sequence, the problem that existing compilers cannot determine the appropriate code conversion order by themselves is solved, and the efficient execution performance improvement of loop code snippets is achieved.

CN119759327AActive Publication Date: 2025-04-04BEI JING BDA NETWORK &INFORMATION CO LTD

Patent Information

Application Number
CN202510272533.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-10
Publication Date
2025-04-04
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, the input code vector is constructed, the optimization scores of each candidate code conversion sequence are calculated, and the optimization code conversion sequence is determined, and inputted to the compiler for optimization.

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 CN119759327A_ABST
    Figure CN119759327A_ABST
Patent Text Reader

Abstract

The present invention provides a program optimization method, device, electronic device and storage medium. By acquiring a loop code snippet to be optimized and a plurality of candidate code conversion sequences given by a compiler for the loop code snippet, input code vectors of the loop code snippet corresponding to each candidate code conversion sequence are constructed, and optimization scores of the corresponding candidate code conversion sequences for the loop code snippet are determined based on each input code vector. Then, based on the optimization scores of each candidate code conversion sequence for the loop code snippet, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input into the compiler so that the compiler can optimize the loop code snippet based on the optimized code conversion sequence. The code conversion order with the best optimization performance can be automatically determined, thereby improving the execution performance of the loop code snippet.
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, device, electronic device and storage medium. Background Art

[0002] Writing high-performance software is essential in many fields. Highly optimized code can significantly improve the execution speed when compared to unoptimized code. For example, in deep learning, optimized versions of state-of-the-art neural network models such as XLNet are 1.8 times faster than equivalent PyTorch implementations. 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 tuning. Current compilers, such as TIRAMISU, already have mechanisms to provide multiple code transformation options for each program code and apply 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 effectiveness of program optimization, but current compilers cannot determine the appropriate code transformation order by themselves, and thus cannot provide the highest optimization performance. Summary of the invention

[0003] The present invention provides a program optimization method, device, electronic device and storage medium, which are used to solve the defect in the prior art that the appropriate code conversion order cannot be determined automatically, thereby failing to provide the highest optimization performance.

[0004] The present invention provides a program optimization method, comprising: Obtaining a loop code snippet to be optimized; the loop code snippet is described based on a domain-specific language provided by a compiler, including 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; Based on the optimization scores of the candidate code conversion sequences for the loop code fragment, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input to the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence.

[0005] 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 conversion sequence includes: 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; Combine loop variables and loop iteration counts of a loop level corresponding to any assignment statement, 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 read mode of the object to be read, and the number of operations of each arithmetic operation, to obtain a calculation vector of any assignment statement; The input code vector of the loop code fragment corresponding to any candidate code conversion sequence includes the calculation vectors of each assignment statement.

[0006] According to a program optimization method provided by the present invention, 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: 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.

[0007] 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 fragment 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.

[0008] According to a program optimization method provided by the present invention, 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.

[0009] According to a program optimization method provided by the present invention, the calculation vector of each assignment statement in any input code vector is used to determine the embedding vector of each assignment statement, including: 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.

[0010] According to a program optimization method provided by the present invention, the step of determining the optimization score of the corresponding candidate code conversion sequence for the loop code fragment 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.

[0011] The present invention also provides a program optimization device, comprising: A code snippet acquisition unit, used to acquire 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 a loop control statement and an assignment statement; 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 code fragment optimization unit is used 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.

[0012] The present invention also provides 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, any one of the program optimization methods described above is implemented.

[0013] 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 described in any one of the above is implemented.

[0014] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the program optimization methods described above.

[0015] The present invention provides a program optimization method, device, electronic device and storage medium. By acquiring a loop code snippet to be optimized and a plurality of candidate code conversion sequences given by a compiler for the loop code snippet, input code vectors of the loop code snippet corresponding to each candidate code conversion sequence are constructed, and optimization scores of the corresponding candidate code conversion sequences for the loop code snippet are determined based on each input code vector. Furthermore, based on the optimization scores of each candidate code conversion sequence for the loop code snippet, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input into the compiler so that the compiler can optimize the loop code snippet based on the optimized code conversion sequence. The code conversion order with the best optimization performance can be automatically determined, thereby improving the execution performance of the loop code snippet. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0017] Figure 1 It is a flow chart of a program optimization method provided by the present invention; Figure 2 It is a schematic diagram of the process of the optimization score evaluation method provided by the present invention; Figure 3 It is a structural schematic diagram of a program optimization device provided by the present invention; Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0018] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0019] Figure 1 It is a flowchart of a program optimization method provided by the present invention, such as Figure 1 As shown, the method includes: Step 110, obtaining 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 a loop control statement and an assignment statement; Step 120, obtaining a plurality of candidate code conversion sequences provided 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; Step 130, constructing input code vectors of the loop code snippet corresponding to each candidate code conversion sequence, and determining an optimization score of the corresponding candidate code conversion sequence for the loop code snippet based on each input code vector; Step 140, determining an optimized code conversion sequence based on the optimization scores of each candidate code conversion sequence for the loop code snippet, and inputting the optimized code conversion sequence into the compiler, so that the compiler optimizes the loop code snippet based on the optimized code conversion sequence.

[0020] Here, the loop code snippet to be optimized includes statements representing loops in the programming language, such as for loops, while loops, etc., and the loop code snippet is described based on the domain-specific language (DSL) provided by the 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] += widths[fout, fin, y, x]* input[n, fin, y+k0, x+k1].

[0021] Example: for (n in 0..batch) for (fout in 0..out_features) for (y in 0..H-2) for (x in 0..W-2) for (fin in 0..in_features) for (k0 in 0..3) for (k1 in 0..3) conv[n, fout, y, x] += weigths[fout, fin, y, x]* input[n, fin, y+k0,x+k1] Then, a plurality of candidate code conversion sequences given by the compiler for the loop code snippet are obtained. Any candidate code conversion sequence includes one or more code conversion methods among loop merging, loop blocking, loop swapping and loop unrolling, and the order of the code conversion methods in different candidate code conversion sequences including the same type of code conversion methods is different. In addition, the code conversion method for each loop level of the loop code snippet is also marked in the candidate code conversion sequence.

[0022] In order to quantify the degree of optimization of the code conversion order provided by different candidate code conversion sequences for the loop code snippet to be optimized, the input code vectors corresponding to each candidate code conversion sequence can be constructed for the above loop code snippet, and regression analysis can be performed on each input code vector using regression to determine the optimization score of the corresponding candidate code conversion sequence for the loop code snippet. Among them, the higher the optimization score of any candidate code conversion 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 conversion sequence relative 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 conversion sequence for the loop code snippet, the input code vector corresponding to each candidate code conversion sequence constructed contains the semantic information of the loop code snippet itself and the semantic information after the corresponding candidate code conversion sequence is applied to the loop code snippet.

[0023] In some embodiments, in order to construct an input code vector corresponding to any candidate code conversion sequence of a loop code snippet, several loop levels of the loop code snippet can be determined based on the loop control statement of the loop code snippet. In some embodiments, the loop level can be marked with a loop variable of the corresponding loop level. Taking the above example as an example, the loop levels of the loop code snippet include n, fout, y, x, fin, k0 and k1 from high to low.

[0024] Based on the loop control statements corresponding to each loop level, the loop variables and the number of loop iterations of each loop level, as well as the marks of each loop level applying each code conversion method in the candidate code conversion sequence can be determined. Specifically, for any loop level, the number of loop iterations can be determined based on the upper and lower bounds of the loop variables in the loop control statements corresponding to the loop level. Taking loop level n as an example, its number of loop iterations is batch+1. For any loop level and any candidate code conversion sequence, the mark of the loop level applying each code conversion method in the candidate code conversion sequence is a Boolean value. The mark of applying any code conversion method as 1 indicates that the compiler will use the code conversion method to optimize the loop level. It should be noted that the mark is determined based on the candidate code conversion sequence.

[0025] For any assignment statement in the loop code snippet, the number of dimensions 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 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. Take 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 assigned to it 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, an all-zero 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), and 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; then, 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 relative 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 A to be read is:

[0026] Then, the loop variables and loop iteration count of the loop level corresponding to any assignment statement (when any assignment statement is located in the loop body of a certain loop level, the assignment statement corresponds to the loop level, and in the loop nesting scenario, one assignment statement can correspond to multiple loop levels), the mark of each code conversion method in the candidate code conversion sequence applied to the corresponding loop level, 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 reading 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 to obtain the input code vector of the loop code fragment corresponding to any candidate code conversion sequence.

[0027] In some embodiments, Figure 2As shown, the optimization score of any candidate code conversion sequence for the above loop code snippet can be determined based on the following method: Step 210, determining an embedding vector of each assignment statement based on the calculation vector of each assignment statement in any one of the input code vectors; Step 220, constructing 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; Step 230, traversing the call relationship tree from the bottom to the top starting from the leaf node of the call relationship tree, and 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 for the current non-leaf node; Step 240: Determine an optimization score of a corresponding candidate code conversion sequence for the loop code snippet based on the embedding vector of the root node in the call relationship tree.

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

[0029] 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 node of the call relationship tree is the assignment statement, the root node is the outermost loop control statement, and the child node is within the control range of the parent node (that is, the child node is in the loop body of the parent node). Subsequently, starting from the leaf node of the call relationship tree, the non-leaf nodes in the call relationship tree are traversed from the bottom to the top. For the current non-leaf node traversed, the embedding vector of the current non-leaf node can be determined based on the embedding vector of the child node of the current non-leaf node. It should be noted that if the child node of the current non-leaf node does not have an embedding vector, the embedding vector of the child node must be extracted first, and the embedding vector of the child node is extracted in a similar way, which can be determined based on the embedding vector of the child node of the child node. The embedding vector of the current non-leaf node can be determined by the above recursive method.

[0030] In some embodiments, in order to obtain the embedding vector of the current non-leaf node, the type of the child node of the current non-leaf node may 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 in the child nodes of the current non-leaf node, semantic extraction is performed on the embedding vectors of the child nodes of the type of leaf nodes 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 type of non-leaf nodes 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 using different long short-term memory networks to perform targeted semantic extraction on the embedding vectors of different types of child nodes, the differences in semantic expressions of different types of child nodes can be fully considered, thereby better extracting the semantic information contained in the embedding vectors of each child node.

[0031] Finally, based on the embedding vector of the root node in the call relationship tree, the optimization score of the candidate code conversion sequence for the loop code snippet can be determined. In some embodiments, the embedding vector of the root node in the call relationship tree can be regressed based on a shallow third feedforward neural network to obtain the optimization score of the candidate code conversion sequence for the loop code snippet. 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.

[0032] It can be known that the second feedforward neural network, the first long short-term memory network, the second long short-term memory network, the first feedforward neural network and the third feedforward neural network constitute a regression model for quantifying the optimization score of each candidate code conversion sequence for the loop code snippet. The regression model can be trained based on the sample loop code snippet, multiple candidate code conversion sequences given by the compiler for the sample loop code snippet, the actual execution time of the original sample loop code snippet, and the actual execution time of the sample loop code snippet after the compiler applies each candidate code conversion sequence.

[0033] Subsequently, based on the optimization scores of various candidate code conversion sequences for the loop code snippet, the candidate code conversion sequence with the highest optimization score can be determined as the optimized code conversion sequence, and the optimized code conversion sequence can be input into the compiler so that the compiler can optimize the loop code snippet based on the optimized code conversion sequence, thereby maximizing the execution performance of the loop code snippet.

[0034] In summary, the method provided by the embodiment of the present invention obtains the loop code snippet to be optimized and multiple candidate code conversion sequences given by the compiler for the loop code snippet, thereby constructing input code vectors of the loop code snippet corresponding to each candidate code conversion sequence, and determines the optimization score of the corresponding candidate code conversion sequence for the loop code snippet based on each input code vector, and then determines the optimized code conversion sequence based on the optimization score of each candidate code conversion sequence for the loop code snippet, and inputs the optimized code conversion sequence into the compiler, so that the compiler can optimize the loop code snippet based on the optimized code conversion sequence, and can automatically determine the code conversion order with the best optimization performance, thereby improving the execution performance of the loop code snippet.

[0035] A program optimization device provided by the present invention is described below. The program optimization device described below and the program optimization method described above can refer to each other.

[0036] Based on any of the above embodiments, Figure 3 is a schematic diagram of the structure of a program optimization device provided by the present invention, such as Figure 3 As shown, the device comprises: A code snippet acquisition unit 310 is used to acquire 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 a loop control statement and an assignment statement; A code conversion acquisition unit 320 is used to 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 of loop merging, loop blocking, loop swapping and loop unrolling; The optimization score evaluation unit 330 is used to construct the 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 code snippet optimization unit 340 is used to determine an optimized code conversion sequence based on the optimization scores of each candidate code conversion sequence for the loop code snippet, and input the optimized code conversion sequence to the compiler so that the compiler optimizes the loop code snippet based on the optimized code conversion sequence.

[0037] The device provided by the embodiment of the present invention obtains the loop code snippet to be optimized and multiple candidate code conversion sequences given by the compiler for the loop code snippet, thereby constructing input code vectors of the loop code snippet corresponding to each candidate code conversion sequence, and determines the optimization score of the corresponding candidate code conversion sequence for the loop code snippet based on each input code vector, and then determines the optimized code conversion sequence based on the optimization score of each candidate code conversion sequence for the loop code snippet, and inputs the optimized code conversion sequence to the compiler, so that the compiler can optimize the loop code snippet based on the optimized code conversion sequence. The code conversion order with the best optimization performance can be automatically determined, thereby improving the execution performance of the loop code snippet.

[0038] Based on any of the above embodiments, constructing an input code vector of the loop code segment corresponding to any candidate code conversion sequence includes: 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; Combine loop variables and loop iteration counts of a loop level corresponding to any assignment statement, 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 read mode of the object to be read, and the number of operations of each arithmetic operation, to obtain a calculation vector of any assignment statement; The input code vector of the loop code fragment corresponding to any candidate code conversion sequence includes the calculation vectors of each assignment statement.

[0039] 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: 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.

[0040] Based on any of the above embodiments, 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.

[0041] Based on any of the foregoing embodiments, 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.

[0042] Based on any of the foregoing embodiments, determining the embedding vector of each assignment statement based on the calculation vector of each assignment statement in any of the input code vectors includes: 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.

[0043] Based on any of the above embodiments, 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.

[0044] Figure 4 is a schematic diagram of the structure of the electronic device provided by the present invention, such as Figure 4 As shown, the electronic device may include: a processor (processor) 410, a memory (memory) 420, a communication interface (Communications Interface) 430 and a communication bus 440, wherein the processor 410, the memory 420, and the communication interface 430 communicate with each other through the communication bus 440. The processor 410 can call the logic instructions in the memory 420 to execute the program optimization method, which includes: obtaining a loop code fragment to be optimized; the loop code fragment is described based on a domain-specific language provided by the compiler, including loop control statements and assignment statements; obtaining multiple candidate code conversion sequences given by the compiler for the loop code fragment; any candidate code conversion sequence contains one or more code conversion methods among loop merging, loop blocking, loop swapping and loop unrolling; constructing input code vectors corresponding to each candidate code conversion sequence of the loop code fragment, and determining the optimization score of the corresponding candidate code conversion sequence for the loop code fragment based on each input code vector; determining an optimized code conversion sequence based on the optimization score of each candidate code conversion sequence for the loop code fragment, and inputting the optimized code conversion sequence into the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence.

[0045] In addition, the logic instructions in the above-mentioned memory 420 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0046] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and 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 the above methods, and the method includes: obtaining a loop code fragment to be optimized; the loop code fragment is described based on a domain-specific language provided by a compiler, including loop control statements and assignment statements; obtaining multiple candidate code conversion sequences given by the compiler for the loop code fragment; any candidate code conversion sequence contains one or more code conversion methods of loop merging, loop blocking, loop swapping and loop unrolling; constructing input code vectors corresponding to each candidate code conversion sequence of the loop code fragment, and determining the optimization score of the corresponding candidate code conversion sequence for the loop code fragment based on each input code vector; determining an optimized code conversion sequence based on the optimization score of each candidate code conversion sequence for the loop code fragment, and inputting the optimized code conversion sequence into the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence.

[0047] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented by a processor to perform the above-mentioned program optimization methods, the method comprising: obtaining a loop code fragment to be optimized; the loop code fragment is described based on a domain-specific language provided by a compiler, including loop control statements and assignment statements; obtaining multiple candidate code conversion sequences given by the compiler for the loop code fragment; any candidate code conversion sequence contains one or more code conversion methods of loop merging, loop blocking, loop swapping and loop unrolling; constructing input code vectors corresponding to each candidate code conversion sequence of the loop code fragment, and determining the optimization score of the corresponding candidate code conversion sequence for the loop code fragment based on each input code vector; determining an optimized code conversion sequence based on the optimization score of each candidate code conversion sequence for the loop code fragment, and inputting the optimized code conversion sequence into the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence.

[0048] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0049] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for 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.

[0050] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments 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; Based on the optimization scores of the candidate code conversion sequences for the loop code fragment, an optimized code conversion sequence is determined, and the optimized code conversion sequence is input to the compiler, so that the compiler optimizes the loop code fragment based on the optimized code conversion sequence.

2. The program optimization method according to claim 1, characterized in that: Constructing an input code vector of the loop code fragment corresponding to any candidate code conversion sequence, including: 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; Combine loop variables and loop iteration counts of a loop level corresponding to any assignment statement, 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 read mode of the object to be read, and the number of operations of each arithmetic operation, to obtain a calculation vector of any assignment statement; The input code vector of the loop code fragment corresponding to any candidate code conversion sequence includes the calculation vectors of each 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 2, characterized in that: 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.

5. The program optimization method according to claim 4, 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.

6. The program optimization method according to claim 5, 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.

7. The program optimization method according to claim 6, 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.

8. 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 code fragment optimization unit is used 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.

9. 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 7 is implemented.

10. 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 7 is implemented.

Citation Information

Patent Citations

  • Code completion method based on abstract syntax tree code representation

    CN116700780A

  • Code clone detection method and system based on abstract syntax tree optimization and multi-representation

    CN117389616A

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

    CN118780351A

  • Compilation optimization method and apparatus, computer device and storage medium

    WO2023030507A1

Cited By

  • Code optimization method, electronic equipment, storage medium and program product

    CN120540966A

  • Code optimization method, electronic device, storage medium and program product

    CN120540966B

  • A multi-solver heterogeneous platform adaptive compilation optimization adaptation method and system

    CN122614381A