A compilation automatic vectorization method based on reorder instruction fusion
Through the compilation automatic vectorization method based on rearrangement instruction fusion, the problems of locality and limited application scope of the vectorization method in the existing technology are solved, and higher parallelism and throughput improvement are achieved.
Patent Information
- Application Number
- CN202111096907.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-17
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2041-09-17
AI Technical Summary
The existing superword parallel vectorization method is highly localized, making it difficult to exploit parallelism capabilities, and only processes the relationship between instruction groups with the same vectorization factor, which limits the application scope of the vectorization method.
Adopting a compilation automatic vectorization method based on rearrangement instruction fusion, by initializing the vector data stream, collecting and grouping Store instructions, constructing all feasible vector data streams, and selecting the optimal vector data stream to perform substantial program vectorization transformation. Use rearrangement instructions to integrate different expressions within basic blocks and expand vectorization factors.
It realizes the discovery of parallelism between different seed instruction groups, generates programs with higher vectorization factors, improves the throughput of the program, and broadens the ability of the form of vectorized programs to adapt to complex program dependency structures.
Smart Images

Figure CN113849180B
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of compilation optimization, and relates to a compilation automatic vectorization method, and specifically to a compilation automatic vectorization method based on reordering instruction fusion. Background Art
[0002] CPU design technology has undergone significant development. It has become difficult to improve CPU performance simply by increasing the main frequency. Using parallelism has become an important way to improve CPU performance. Modern CPUs can use various forms of parallelism through specially designed hardware modules, such as pipelines and SIMD (Single Instruction Multiple Data) extensions. For example, in 1996, Intel took the lead in integrating SIMD extensions into processors, and has been improving SIMD extensions since then, including vector register length and instruction sets. MMX (Multi Media eXtension), SSE (Streaming SIMD Extensions), AVX (Advanced Vector Extensions) and IMCI (Initial Many-Core Instructions) have appeared one after another.
[0003] Currently, the parallelism used by CPUs includes task-level parallelism, instruction-level parallelism, and data-level parallelism.
[0004] At present, multi-core CPUs often improve the overall CPU performance by adding multiple groups of independent computing cores, thereby enhancing the system's task-level parallelism. However, blindly increasing the number of CPU cores is not advisable, as the cost of data communication between cores will increase dramatically as the number of cores increases.
[0005] Instruction-level parallelism refers to the parallelism between adjacent instructions in the instruction stream executed within a single core. The performance indicator for measuring CPU instruction-level parallelism is the number of instructions executed per cycle (IPC). Modern CPUs have developed pipeline technology, which divides the execution process of instructions into several stages, allowing different instructions to be in different execution stages at the same time.
[0006] Data-level parallelism is another type of parallelism that exists within a single core. Assuming that there are a large number of operations of the same type in the program, the order of these operation instructions can be exchanged without affecting the semantics of the program. If the same type of operation instructions can be concentrated in one place and executed at the same time, the performance of the program will be improved. Because data-level parallelism is related to the scale of data processed by the CPU, the performance indicator that measures the CPU's ability to handle data-level parallelism is throughput, that is, the scale of data processed by the CPU per unit time. For compute-intensive programs, the performance bottleneck of the program is generally limited by the CPU throughput, and data-level parallelism is an important means to improve the performance of such programs.
[0007] Vectorized computing is a special type of parallel computing. Compared to general programs that only execute one operation at a time, it can execute multiple operations at the same time, usually executing the same instruction or a batch of instructions on different data, or applying instructions to an array / vector.
[0008] Superword Level Parallelism (SLP) is a widely used method in data-level parallelism. The superword parallel vectorization method is to find instruction parallel opportunities within a basic block. The process is to find adjacent memory accesses within the basic block, pair them up and pack them into vectorized packages, then expand the packages according to the Define-Use (DU) chain and the Use-Define (UD) chain, and then combine isomorphic operations according to the requirements of vectorization to generate vectorized code. In the specific implementation, the loop is expanded according to a certain number of iterations, and the data with continuous addresses is found and packaged as vectors for processing, and then the isomorphic statements in the program are replaced with vector instructions. Statements of different loops can also be merged into the same basic block, so that statement parallelism between iterations and instruction parallelism within iterations can be achieved, so that data SIMD vectorization between iterations and within iterations can be achieved.
[0009] However, the existing superword parallel vectorization methods are highly localized and limited in their ability to discover parallelism. Specifically, the process of constructing vector data streams in the prior art is very simple. After determining the seed instruction group, it simply extends on the UD chain (use-def chain). The processing of different seed instruction groups is completely independent. Therefore, the existing superword parallel vectorization methods do not have the ability to discover parallelism between different seed instruction groups.
[0010] In addition, existing superword parallel vectorization methods generally only process the relationship between instruction groups with the same vectorization factor, or use reordering instructions to continuously reduce the vectorization factor during the vectorization process, which limits the application scope of the vectorization method. Summary of the invention
[0011] The object of the present invention is to provide a compilation automatic vectorization method based on reorder instruction fusion to expand the application scope of the vectorization method.
[0012] In order to achieve the above object, the present invention provides a compilation automatic vectorization method based on reorder instruction fusion, comprising:
[0013] Step S1: Initialize a vector data stream, wherein the vector data stream is used to record instruction groups, indexes of instruction groups, and dependencies between instruction groups;
[0014] Step S2: Collect the Store instructions in a basic block and group them according to the data type accessed and the address continuity, and finally form several Store instruction groups. All Store instructions in each Store instruction group access a continuous address space and have the same accessed data width; all Store instruction groups are added to the same vector data stream as seed instruction groups, and at the same time, an index is assigned to each Store instruction group;
[0015] Step S3: starting from the vector data stream with the seed instruction group constructed in step S2, construct all feasible vector data streams, and select the optimal vector data stream from them;
[0016] Step S4: Determine whether the cost of the optimal vector data flow obtained in step S3 is greater than 0. If it is greater than 0, abandon vectorization; otherwise, perform substantial vectorization transformation of the program according to the optimal vector data flow.
[0017] Preferably, step S2 comprises:
[0018] Step S21: searching for Store instructions in the basic block through the Store instruction collection module;
[0019] Step S22: checking the dependency between the addresses accessed by the Store instructions, grouping multiple Store instructions that have no dependency between the addresses accessed as Store instructions that can be executed in parallel, and obtaining multiple groups of Store instructions that have undergone initial grouping; for each group of Store instructions that have undergone initial grouping, further sorting and grouping are performed according to the continuity of the addresses accessed by the Store instructions, and obtaining multiple Store instruction groups;
[0020] Step S23: All Store instruction groups are added to the same vector data stream as seed instruction groups. At the same time, for each Store instruction group, an index is assigned in ascending order according to the access address, so that all Store instructions therein have different index values.
[0021] Preferably, in step S22, further sorting and grouping is performed, including: treating multiple Store instructions in the same group of initially grouped Store instructions that access a continuous address space and have the same accessed data width as one Store instruction group, and splitting multiple Store instructions that access different data widths or access discontinuous address spaces into different Store instruction groups.
[0022] Preferably, in step S3, constructing a vector data stream includes:
[0023] Step S31: the vector data stream obtained by adding all seed instruction groups to the vector data stream is used as the initial vector data stream, the initial vector data stream is used as the vector data stream of the current level recursion and the current current level recursion is used as the 0th level recursion;
[0024] Step S32: setting the optimal return vector data stream of the current level of recursion and the candidate instruction group set of the current level of recursion to invalid values;
[0025] Step S33: constructing a set of candidate instruction groups for the current level of recursion according to the vector data stream for the current level of recursion;
[0026] Step S34: determine whether the set of candidate instruction groups for the current level of recursion is empty; if the set of candidate instruction groups for the current level of recursion is determined to be empty, use the vector data stream for the current level of recursion as the optimal return vector data stream for the current level of recursion at the lowest level of recursion, and then execute step S36; otherwise, back up the vector data stream for the current level of recursion, the set of candidate instruction groups, and the optimal return vector data stream; then execute step S35;
[0027] Step S35: selecting a candidate instruction group that has not been taken from the set of candidate instruction groups of the current recursion level, and adding the candidate instruction group to the vector data stream of the current recursion level, so that the recursion level of the vector data stream of the current recursion level is increased by one; then turning to step S32;
[0028] Step S36: restore the vector data stream, the set of candidate instruction groups and the optimal return vector data stream corresponding to the previous recursive level where the vector data stream of the current recursive level is located from the backup, and return the optimal return vector data stream of the current recursive level as a return value to the previous recursive level;
[0029] Step S37: taking the previous level recursion in step S36 as the new current level recursion, calculating the index and cost of the return value of step S36, comparing it with the cost of the optimal return vector data flow of the current level recursion, and updating the optimal return vector data flow of the current level recursion according to the comparison result;
[0030] Step S38: Determine whether the selection branches of all candidate instruction groups of the vector data stream of the current level of recursion have been calculated. If not, return to the step S35; otherwise, return to step S36 to further return to the previous level of recursion until there is no previous level of recursion, and output the optimal return vector data stream and its cost of the current level of recursion at this time as the optimal vector data stream and its cost.
[0031] Preferably, in step S37, the index and cost of the recursive vector data stream of this level are calculated by incrementally calculating the incremental results of the index and cost of the vector data stream;
[0032] And if the optimal return vector data stream of the current level recursion is an invalid value or the cost of the optimal return vector data stream of the current level recursion is higher than the cost of the return value, the optimal return vector data stream of the current level recursion is updated to the return value; otherwise, the optimal return vector data stream of the current level recursion remains unchanged.
[0033] Preferably, in step S37, the incremental result of the index is calculated first, and then the incremental result of the cost is calculated.
[0034] Preferably, the incremental calculation refers to calculating the index and cost of the candidate instruction group selected in step S35, and adding it to the index and cost of all subsequently added candidate instruction groups and all reordering instructions after the level of recursive vector data stream where the selected branch of the recursive vector data stream of the current level is located and has not yet been calculated, as the incremental result of the index and cost of the recursive vector data stream of the current level.
[0035] Preferably, the steps for calculating the index of the candidate instruction group selected in step S35 are as follows:
[0036] Step S371: taking the added candidate instruction group as the current instruction group, determining whether the current instruction group is a Load instruction group, and if so, assigning an index to it;
[0037] Step S372: Determine the dependency relationship between the current instruction group and other instruction groups of the recursive vector data stream at this level to determine the instruction groups on which the current instruction group depends; if the size of an instruction group on which the current instruction group depends does not match the size of the current instruction group, insert a reorder instruction between the two; if the current instruction group has the same size as an instruction group with which there is a dependency, and the relevant instruction group has been indexed, derive the index of the current instruction group based on the index of the relevant instruction group;
[0038] Step S373: Determine whether the current instruction group is indexed. If the current instruction group is not indexed, assign an arbitrary index to the current instruction group.
[0039] Preferably, the candidate instruction group refers to an instruction group having a resolvable dependency relationship with an instruction group in a given incomplete vector data stream; the set of candidate instruction groups refers to a set of all instruction groups that meet the conditions of the candidate instruction group;
[0040] The resolvable dependency relationship means that if two instruction groups are each replaced by a SIMD instruction, a dependency relationship of the original semantics of the program can be directly established between the two SIMD instructions or by inserting a reorder instruction.
[0041] Preferably, in step S33, a set of candidate instruction groups for the current level of recursion is constructed according to the vector data stream for the current level of recursion, including:
[0042] Step S331: Initialize and obtain a set of instruction groups as a combination space;
[0043] Step S332: for each sub-instruction group except the Load instruction group in the recursive vector data stream of this level and each parameter position of all instructions in the sub-instruction group, all instructions corresponding to all parameters at a single parameter position are taken to form an initial instruction group;
[0044] Step S333: Determine whether the instructions in the initial instruction group are of the same type; if the instructions in the initial instruction group are of the same type, directly add the initial instruction group to the combination space; if the instructions in the initial instruction group are not of the same operation type, check whether the instructions in the initial instruction group can be divided into two split instruction groups, and each split instruction group has the same operation type, and if so, add the two split instruction groups to the combination space respectively;
[0045] Step S334: in the combination space, pairing instruction groups with the same operation type and the same size into one instruction group;
[0046] Step S335: Perform Load instruction judgment on each instruction group in the combination space, and add it to the set of candidate instruction groups or discard it according to the Load instruction judgment result;
[0047] Among them, the Load instruction judgment includes: judging whether the instruction group is a Load instruction group and whether the Load instruction accesses the same size and continuous address space; if the result of the Load instruction judgment of the instruction group is: the instruction group is a Load instruction group, and the Load instruction does not access the same size and continuous address space, then the instruction group is discarded and not added to the candidate instruction group set; otherwise, the instruction group is added to the set of candidate instruction groups.
[0048] The compilation automatic vectorization method based on rearrangement instruction fusion of the present invention incorporates all seed instruction groups with vectorization conditions in the basic block into the same vector data flow construction process, so that the generated candidate instruction groups can be any combination of instructions with parallelism, one basic block can be vectorized at a time and the parallelism between different seed instruction groups can be discovered, thereby generating a program with a higher vectorization factor for the program, further improving the throughput of the program.
[0049] The compilation automatic vectorization method based on rearrangement instruction fusion of the present invention can utilize rearrangement instructions to integrate different expressions in basic blocks to improve vectorization factors, can fully utilize large-capacity SIMD registers, and improve the throughput of vectorized programs. The present invention regards rearrangement instructions as a means of bridging instruction groups with different vectorization factors, which greatly broadens the form of vectorized programs. While existing methods generally only deal with the relationship between instruction groups with the same vectorization factor, or utilize rearrangement instructions to continuously reduce the vectorization factor during the vectorization process, the present invention makes more full use of rearrangement instructions, not only supports dynamic reduction of vectorization factors, but also supports dynamic expansion of vectorization factors, thereby being able to more flexibly adapt to complex program dependency structures and expand the scope of application of vectorization methods. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 A flowchart of a compilation automatic vectorization method based on reorder instruction fusion of the present invention;
[0051] Figure 2 A schematic diagram describing the dependencies between vector data streams of the present invention. DETAILED DESCRIPTION
[0052] An embodiment of the present invention is given below in conjunction with the accompanying drawings to further explain and illustrate the scheme of the present invention in detail.
[0053] like Figure 1 As shown, the compilation automatic vectorization method based on rearrangement instruction fusion of the present invention is mainly based on the following principles: first, for a basic block, all vector data streams with vectorization potential are constructed, and the cost of each vector data stream is evaluated according to the cost function, and the vector data stream with the lowest cost is selected. If the cost of the vector data stream with the lowest cost is negative, that is, the vectorized program has a performance advantage over the original program, then the instruction group to be vectorized in the basic block is replaced with SIMD instructions with reference to the vector data stream, and necessary rearrangement instructions (i.e., Shuffle instructions) are inserted.
[0054] like Figure 1 The figure shows a compilation automatic vectorization method based on reorder instruction fusion, and its steps are:
[0055] Step S1: Initialize a vector data stream, which is used to record the instruction groups, instruction group indexes and dependencies between instruction groups determined in the subsequent vectorization process; the vector data stream is the core data structure maintained by the vectorization process, and the instruction groups and the dependencies between instruction groups are logically regarded as a directed acyclic graph, in which the instruction groups are points and the dependencies are edges; and the index of the instruction group is additional information attached to each instruction group (a number is mapped to each instruction in the instruction group).
[0056] The index of the instruction group is manually defined. The dependency between instruction groups is specifically the dependency between instructions in different instruction groups. If a pair of dependent instructions belong to two instruction groups, then the two instruction groups have a dependency, and the direction of the dependency is the same as the dependency between the two instructions.
[0057] Step S2: Collect Store instructions: Collect Store instructions in a basic block and group them according to the data type accessed and address continuity, and finally form several Store instruction groups. All Store instructions in each Store instruction group access a continuous address space and have the same accessed data width; all Store instruction groups are added to the same vector data stream as seed instruction groups, and at the same time, each Store instruction group is assigned an index;
[0058] Among them, the Store instruction is a storage instruction, and the Load instruction is a loading instruction.
[0059] The step S2 comprises:
[0060] Step S21: searching for Store instructions in the basic block through the Store instruction collection module;
[0061] These Store instructions are used to build the seed instruction set in subsequent steps.
[0062] Step S22: Check the dependency between the addresses accessed by each of these Store instructions (that is, check whether there is a situation where the same address is accessed in the Store instructions. This is an implicit dependency, and there is no parallelism between multiple store instructions accessing the same address), and group the multiple Store instructions that have no dependency between the addresses accessed as Store instructions that can be executed in parallel, to obtain multiple groups of Store instructions that have undergone initial grouping; for each of the above groups of Store instructions that have undergone initial grouping, further sorting and grouping are performed according to the continuity of the addresses accessed by the Store instructions to obtain multiple Store instruction groups.
[0063] Among them, further sorting and grouping are performed, including: treating multiple Store instructions that access a continuous address space and have the same data width in the same group of Store instructions that have undergone initial grouping as one Store instruction group, and splitting multiple Store instructions that access different data widths or access discontinuous address spaces into different Store instruction groups. If there are multiple Store instructions in the same group of Store instructions that have undergone initial grouping that access different data widths, or the accessed address spaces are discontinuous, then when further sorting and grouping are performed, they should be divided into two Store instruction groups. The further sorting and grouping here is because Store instruction groups with different data types or discontinuous addresses cannot be replaced with SIMD instructions.
[0064] Therefore, because the addresses are continuous, the SIMD instructions generated by the Store instructions in the Store instruction group can write all the data at once, so the data to be written is required to be arranged from small to large according to the address. It can be regarded that the Store instructions occur in parallel, and the same applies to the load instructions.
[0065] Thus, a plurality of Store instruction groups are finally obtained, wherein all the Store instructions in each Store instruction group are executable in parallel, and the Store instructions in the same Store instruction group are sorted from small to large according to the continuity of the accessed addresses.
[0066] Step S23: All Store instruction groups are added to the same vector data stream as seed instruction groups. At the same time, for each Store instruction group, an index is assigned in ascending order according to the access address, so that all Store instructions therein have different index values. Limited by the SIMD hardware characteristics, it is stipulated that Store instruction groups have mandatory indexes (for n Store instructions on consecutive addresses, they are assigned 0 to n-1 respectively in ascending order according to the address).
[0067] The index is the arrangement of instructions in the instruction group, and is a unique number assigned to each instruction in the instruction group, similar to the instruction group being a vector, the instructions in the instruction group being a component, and the index being the position of the component. Thus, the resulting vector data stream records the index of each seed instruction group, but the specified time of the index may be later than the time when the Store instruction group is added to the vector data stream.
[0068] Therefore, all seed instruction groups in the basic block can be processed at one time. Different from the existing method of processing one seed at a time, the compilation automatic vectorization method based on reordering instruction fusion of the present invention provides the premise for any combination of instruction groups and maximizing parallelism.
[0069] Step S3: constructing a vector data stream: starting from the vector data stream with the seed instruction group constructed in step S2, constructing all feasible vector data streams, and selecting the optimal (i.e., the lowest cost) vector data stream from them;
[0070] Among them, each vector data stream represents a vectorization scheme, and the vector data stream records the instruction group in the vectorization scheme, the index of the instruction group, and the dependency between the instruction groups. There may be more than one vectorization scheme for a basic block, and correspondingly, the vector data stream is also diverse. In order to obtain the optimal vectorization scheme, it is necessary to construct all feasible vector data streams to describe all feasible vectorization schemes. Therefore, unlike the existing greedy method, when it is necessary to decide how to choose from multiple construction methods, a construction method is selected based on a local, heuristic method, and the exploration of other construction methods is abandoned, which is not conducive to finding the optimal solution. The process of constructing the vector data stream in step S3 of the present invention constructs all possible vector data streams, and selects the optimal vector data stream (in the sense of the cost model) therefrom, which can ensure that the optimal solution is found.
[0071] In step S3, all feasible vector data streams are constructed according to the vector data stream construction method, and the optimal vector data stream is selected from them. The vector data stream construction method refers to a process of recursively enumerating all possible vector data streams given a basic block and selecting the optimal vector data stream from them. Therefore, in step S3, all feasible vector data streams are constructed and the optimal vector data stream is selected from them, including the following steps:
[0072] Step S31: the vector data stream obtained after adding all seed instruction groups to the vector data stream is used as the initial vector data stream (the vector data stream at this time is an incomplete vector data stream), the initial vector data stream is used as the vector data stream of the current level recursion and the current current level recursion is used as the 0th level recursion;
[0073] The initial vector data stream of the vector data stream of the current level of recursion is constructed in step S31 before the recursion starts, so it is used as the vector data stream of the 0th level of recursion. The vector data stream of any level of recursion is incomplete before calling the next level of recursion, and is further expanded by the next level of recursion. When the next level of recursion returns, the returned vector data stream is complete and the index and cost can be calculated.
[0074] Step S32: setting the optimal return vector data stream of the current level recursion and the candidate instruction group set of the current level recursion to invalid values; thereby, initialization of the optimal return vector data stream of the current level recursion and the candidate instruction group set of the current level recursion is achieved.
[0075] Among them, for each different recursive level, the optimal return vector data stream of this level of recursion is used to store all information of a single vector data stream, including all instruction groups and dependencies between instruction groups. However, the index of the optimal return vector data stream of this level of recursion is calculated incrementally in the process of returning upward level by level.
[0076] Step S33: constructing a set of candidate instruction groups for the current level of recursion according to the vector data stream for the current level of recursion;
[0077] The step S33 of constructing a set of candidate instruction groups for the current level of recursion according to the vector data stream for the current level of recursion will be described in detail below.
[0078] Step S34: determine whether the set of candidate instruction groups for the current level of recursion is empty; according to the determination result, if the set of candidate instruction groups for the current level of recursion is determined to be empty, it indicates that the construction of a complete vector data stream is completed, and the vector data stream for the current level of recursion is used as the optimal return vector data stream for the current level of recursion at the lowest level of recursion, and then step S36 is executed (i.e., step S35 is skipped); otherwise, it indicates that the set is not empty, and the vector data stream for the current level of recursion, the set of candidate instruction groups for the current level of recursion, and the optimal return vector data stream for the current level of recursion are backed up; then, step S35 is executed;
[0079] Thus, the optimal return vector data stream of the current level recursion is obtained, and the returned vector data stream of the current level recursion is a complete vector data stream. Since this is the data of the node with the lowest recursive level (i.e., the recursive leaf node), it is the only vector data stream of the current level recursion, so it must also be the optimal return vector data stream of the current level recursion when the recursive level is the lowest level.
[0080] It should be noted that the lowest level recursion means the recursion level has the largest value, and the 0th level recursion is the highest level recursion.
[0081] Step S35: selecting a candidate instruction group that has not been taken (in any order) from the set of candidate instruction groups of the current recursion level, and adding the candidate instruction group to the vector data stream of the current recursion level, so that the recursion level of the vector data stream of the current recursion level is increased by one; then turning to step S32;
[0082] The backed-up vector data stream is the entire vector data stream, that is, the instruction groups therein, dependencies between instruction groups, and indexes of some instruction groups (at this time, only the Store instruction group has an index).
[0083] The candidate instruction group that has not been taken refers to an operation of taking the candidate instruction group and adding it to the recursive vector data stream of the current level, which has never been performed before.
[0084] Therefore, step S34 saves the recursive context of the vector data stream of the current level of recursion when the set is not empty. The recursive context includes: 1) the vector data stream of the current level of recursion; 2) the candidate instruction group set, the candidate instruction group set of each level of recursion is calculated based on the current vector data stream at the beginning of the next level of recursion, and is independent of each other, and needs to be saved and restored when the recursion is deepened and returned; 3) the optimal return vector data stream of the current level of recursion, similar to 2) the candidate instruction group set, corresponding to the need to distinguish the optimal return vector data stream of the current level of recursion at different recursive levels.
[0085] Step S36: return the optimal return vector data stream of the current level recursion as the return value to the previous level recursion; that is, restore the vector data stream corresponding to the previous level recursion where the vector data stream of the current level recursion is located (the vector data stream of the previous level recursion is an incomplete vector data stream), the set of candidate instruction groups and the optimal return vector data stream from the backup, and return the optimal return vector data stream of the current level recursion as the return value to the previous level recursion; thereby, recursive return is achieved.
[0086] Step S37: taking the previous level of recursion in the step S36 as the new current level of recursion (that is, the vector data stream, the set of candidate instruction groups and the optimal return vector data stream corresponding to the previous level of recursion are the new vector data stream, the set of candidate instruction groups and the optimal return vector data stream of the current level of recursion), calculating the index and cost of the return value of step S36 (the return value at this time is the "next level of recursive optimal return vector data stream" of the new current level of recursion), comparing it with the cost of the optimal return vector data stream of the current level of recursion, and updating the optimal return vector data stream of the current level of recursion according to the comparison result;
[0087] Among them, if the optimal return vector data stream of the current level recursion is an invalid value or the cost of the optimal return vector data stream of the current level recursion is higher than the cost of the return value, the optimal return vector data stream of the current level recursion is set to the return value to achieve the update; otherwise, the optimal return vector data stream of the current level recursion remains unchanged.
[0088] Therefore, the optimal return vector data stream can be compared and selected in a mode similar to an elimination match, that is, the vector data stream of each level of recursion must select the optimal return vector data stream of the current level of recursion to return to its previous level of recursion, and then the previous level of recursion (generally, there are multiple candidate instruction groups, each candidate instruction group leads to a next level of recursion, that is, causes the next level of recursion to return an "optimal return vector data stream of the next level of recursion") selects the best one from all the optimal return vector data streams of its next level of recursion to participate in the comparison of the next level of recursion.
[0089] It should be noted that the current vector data stream of this level of recursion (usually incomplete) and the optimal return vector data stream of this level of recursion (complete) are two different objects. In the vector data stream of this level of recursion, the return value returned to the vector data stream of the previous level of recursion (i.e., the optimal return vector data stream of this level of recursion) and the vector data stream of the previous level of recursion itself should be regarded as different objects and do not overlap each other. The vector data stream of this level of recursion is usually an incomplete vector data stream (for example, the initial vector data stream is the vector data stream of the 0th level of recursion); and the optimal return vector data stream of this level of recursion is used to store the best (i.e., the one with the lowest cost) of all complete vector data streams formed with the vector data stream of this level of recursion as the head through continuous updates.
[0090] Step S38: Determine whether the selection branches of all candidate instruction groups of the vector data stream of the current level of recursion have been calculated. If not, return to step S35 to further calculate the remaining selection branches; otherwise, it means that the selection branches of all candidate instruction groups have been calculated, return to step S36 to further return to the previous level of recursion, until the selection branches of all candidate instruction groups of the 0th level of recursion are also calculated, and there is no previous level of recursion. At this time, the process ends, and the index and cost of the seed instruction group in the vector data stream of the current level of recursion are calculated to calculate the optimal return vector data stream of the current level of recursion, and the optimal return vector data stream and its cost of the current level of recursion are output as the optimal vector data stream and its cost.
[0091] In step S37, the index and cost of the recursive vector data stream at this level are calculated by incrementally calculating the incremental results of the index and cost of the vector data stream (except for the case where the process ends in step S38).
[0092] In step S37, the calculation of index and cost is involved. The calculation method of index and cost is as follows:
[0093] There is a sequence relationship between the calculation of the index and the calculation of the cost, because different indexing schemes will introduce different reordering instructions (i.e., shuffle instructions), resulting in different costs. Therefore, it is necessary to calculate the incremental result of the index first, and then calculate the incremental result of the cost.
[0094] Among them, the cost model is a static evaluation method for code execution time, which assigns a cost value to each instruction; an instruction group in a vector data stream can be regarded as a group of instructions in the original program that are scalarized and vectorized to replace a SIMD instruction of this group of instructions. The relative cost of this instruction group is the cost of the latter minus the cost of the former (that is, the cost of replacing a SIMD instruction of an instruction group minus the cost of each instruction of the instruction group); the cost of a vector data stream is the sum of the relative costs of all instruction groups of the vector data stream and the costs of all rearranged instructions. If the total cost is less than 0, then according to the cost model, the vectorization scheme represented by this vector data stream is profitable.
[0095] If there are multiple different vector data streams, the sum of the costs of the instructions involved in each of the vector data streams is calculated respectively, and the performance of different versions of the program (ie, different vector data streams) is comprehensively compared based on such costs.
[0096] Logically, the aforementioned vector data stream construction method constitutes the solution space of the vector data stream. Recursion is regarded as a tree, and each recursion corresponds to a subtree. Obviously, the initial recursion corresponds to the whole tree. The several sub-recursions (i.e., the next level of recursion) of each recursion are all subtrees of the corresponding subtree; at the same time, each recursion corresponds to a backup vector data stream, where the backup vector data stream corresponds to the root node of the subtree corresponding to this recursion; each subtree includes a root node, at least one intermediate node, and at least one leaf node with increasing recursion levels. At this time, each intermediate node in the subtree corresponds to an incomplete vector data stream, and each leaf node corresponds to a complete vector data stream. In particular, the vector data stream corresponding to each node is a part of the vector data stream corresponding to its descendant nodes; conversely, the vector data stream corresponding to each node is also obtained by expanding the vector data stream of its ancestor node; for a given node, the same part of the vector data stream corresponding to its different child nodes is exactly the vector data stream corresponding to this node.
[0097] For the purpose of cost comparison, only the index and cost of the difference between the two vector data streams need to be calculated, avoiding the calculation of the index and cost of the same part, which can avoid repeated calculations. (When recursively returning level by level, each level selects the best vector data stream corresponding to all sibling nodes, and the non-optimal vector data stream is discarded from then on).
[0098] Therefore, as described above, in the step S37, the index and cost of the recursive vector data stream of this level are obtained by incremental calculation after the entire recursive return process (i.e., after step S38 returns to step S35). Incremental calculation refers to calculating the index and cost of the candidate instruction group selected in step S35, and adding it to the index and cost of all subsequent candidate instruction groups and all rearrangement instructions after the recursive vector data stream of this level (i.e., the recursive vector data stream of the level where the selected branch where the recursive vector data stream of this level is located has not been calculated) when step S38 returns to step S35 before selecting the candidate instruction group, as the incremental result of the index and cost of the recursive vector data stream of this level. In this way, the index and cost of all subsequent candidate instruction groups added to the vector data stream that has not been calculated recursively returned to the selected branch can be obtained. This part of the index and cost constitutes the index and cost of the difference part of any two complete vector data streams with the above-mentioned selected branch as a part of the vector data stream that has not been calculated, and the two vector data streams can be compared by this part of the cost.
[0099] The index calculation process is essentially the process of analyzing and inserting the reordering instruction. The index calculation steps of the candidate instruction group selected in step S35 are as follows:
[0100] Step S371: taking the added candidate instruction group as the current instruction group, determining whether the current instruction group is a Load instruction group, and if so, assigning an index to it;
[0101] Among them, if the current instruction group is a Load instruction group, similar to the Store instruction group, the current instruction group is assigned an index according to the address continuity;
[0102] According to the candidate instruction group generation process, the Load instruction groups in the vector data stream must be continuous by address; wherein, the index of each instruction group continuous by address is independently named, and for an instruction group, if there are n scalar instructions, the index value ranges from 0 to n-1.
[0103] Step S372: Determine the dependency relationship between the current instruction group and other instruction groups of the recursive vector data stream at this level, so as to determine the instruction groups on which the current instruction group depends; if the size of an instruction group on which the current instruction group depends does not match the size of the current instruction group, insert a reorder instruction between the two (i.e., record in the vector data stream that this dependency relationship corresponds to a reorder instruction); if the current instruction group has the same size as an instruction group with which there is a dependency, and the relevant instruction group has been indexed, derive the index of the current instruction group based on the index of the relevant instruction group;
[0104] The index of the current instruction group is derived based on the index of the related instruction group. This is to ensure that the instructions at the same position of the two instruction groups have a dependency relationship (SIMD hardware constraint). At this time, the index of one instruction group can determine the index of the other instruction group.
[0105] Step S373: Determine whether the current instruction group is indexed. If the current instruction group is not indexed (ie, none of the above conditions are met), an arbitrary index may be assigned to the current instruction group.
[0106] Thus, the reordering instructions that need to be inserted are determined during index calculation, that is, the cost associated with the current instruction group can also be calculated.
[0107] The specific method of constructing the set of candidate instruction groups for the current level of recursion in step S33 is described in detail below.
[0108] The candidate instruction group refers to an instruction group that has a resolvable dependency relationship with an instruction group in a given incomplete vector data stream; and the set of candidate instruction groups refers to the set of all instruction groups that meet the conditions of the candidate instruction group.
[0109] The resolvable dependency relationship means that if two instruction groups are each replaced by a SIMD instruction, then the dependency relationship between the two SIMD instructions can be established directly or by inserting a reordering instruction (i.e., Shuffle instruction) to maintain the original semantics of the program. Whether the dependency can be established directly depends on hardware support, and it is generally required that the vectorization factors of the two SIMD instructions are the same. Inserting reordering instructions can adjust the dependency relationship to meet hardware constraints, but it introduces additional costs, so the number of inserted reordering instructions needs to be limited. If the above conditions cannot be met, that is, the dependency relationship is not resolvable, it is impossible to replace the two instruction groups with SIMD instructions separately and maintain the original semantics of the program. (The computer program observes the structure of the two instruction groups (the size of the vectorization factor) and the dependency relationship of the scalars in the instruction groups to determine whether it is resolvable. The insertion modes supported by the reordering instructions are shown below. Figure 2 .
[0110] Therefore, the present invention expands the universal scope of vectorization by applying reordering instructions, so that the dependencies between instruction groups that do not conform to the existing SIMD instruction dependency specifications (that is, the sizes of the two instruction groups corresponding to the two SIMD instructions must be the same) can be compliant by applying reordering instructions; the existing methods have not yet promoted the use of reordering instructions into a universal method, and there are only some applications that match specific patterns.
[0111] The incomplete vector data stream expands itself by continuously selecting candidate instruction groups to add until no candidate instruction groups can be selected in the vector data stream. If the size of the set of candidate instruction groups is greater than 1 (that is, the number of candidate instruction groups in the set of candidate instruction groups is greater than 1), the above recursion has branches and the vector data stream has diversity.
[0112] In step S33, a set of candidate instruction groups for the current level of recursion is constructed according to the vector data stream for the current level of recursion, including the following steps:
[0113] Step S331: Initialize and obtain a set of instruction groups as a combination space;
[0114] The resulting combination space is an empty set.
[0115] Step S332: for each parameter position (a unary operation has one parameter position, and a binary operation has two parameter positions) of all instructions in each sub-instruction group (except the Load instruction group) in the recursive vector data stream of this level, all instructions corresponding to all parameters at a single parameter position are taken to form an initial instruction group;
[0116] It should be noted that the sub-instruction group itself should be excluded here, and only the instruction group formed by its parameters should be used to participate in the construction of the combined space, so as to avoid the problem of the sub-instruction group being repeatedly added to the initial instruction group.
[0117] Step S333: Determine whether the instructions in the initial instruction group are of the same type; if the instructions in the initial instruction group are of the same type, then directly add the initial instruction group to the combination space (wherein the operation type includes all binary operation instruction types, all type conversion instruction types, all unary operation instruction types, Load instruction types, etc.); if the instructions in the initial instruction group are not of the same operation type, then check whether the instructions in the initial instruction group can be divided into two split instruction groups, each split instruction group having the same operation type, and if so, add the two split instruction groups to the combination space respectively;
[0118] It should be noted that if it is still impossible to ensure that the instructions do not have the same operation type when divided into two split instruction groups, it means that this instruction group does not have the conditions for continuing vectorization and no longer participates in the construction of the combination space.
[0119] Step S334: in the combination space, pairing instruction groups with the same operation type and the same size into one instruction group;
[0120] Thus, two small instruction groups in the combination space are merged into a smaller number of large-size instruction groups. In addition, two instruction groups, rather than multiple instruction groups, are merged into one instruction group, which avoids the problem of excessive execution cost of corresponding operations or a possible sharp increase in the size of the combination space.
[0121] Step S335: Perform Load instruction judgment on each instruction group in the combination space, and add it to the set of candidate instruction groups or discard it according to the Load instruction judgment result;
[0122] The Load instruction judgment includes: judging whether the instruction group is a Load instruction group and whether the Load instruction accesses the same size and continuous address space. If the result of the Load instruction judgment of the instruction group is: the instruction group is a Load instruction group, and the Load instruction does not access the same size and continuous address space, the instruction group is discarded and is not added to the candidate instruction group set; otherwise, the instruction group is added to the candidate instruction group set.
[0123] The above enumeration method ensures that the dependency between any candidate instruction group and the instruction group in the existing vector data stream is resolvable.
[0124] Therefore, the present invention adds all seed instruction groups to the vector data stream in step S31, and when constructing the set of candidate instruction groups for this level of recursion in step S33, all instructions for the same operation that can be executed in parallel are combined and enumerated, thereby exploring the parallelism between different instruction groups, which is impossible for existing algorithms, because existing algorithms only process one seed instruction group at a time and cannot explore the parallelism between instruction groups on the UD chain of different seed instruction groups. In addition, when the present invention constructs the set of candidate instruction groups for this level of recursion in step S33, the candidate instruction groups take into account different instruction group sizes, and cooperate with the process of recursively constructing vector data streams to achieve dynamic adjustment of the instruction group size in a vector data stream with the program structure, and fully explore possible vector data streams (i.e., explore different vectorization schemes); existing methods often fix specific instruction group sizes to simplify the design.
[0125] Step S4: Determine whether the cost of the optimal vector data flow obtained in step S3 is greater than 0. If it is greater than 0, abandon vectorization; otherwise, it is considered that vectorization has performance benefits, and the actual program vectorization transformation is performed according to the optimal vector data flow.
[0126] The cost of a vector data stream is the sum of the relative costs of all instruction groups of the vector data stream and the costs of all reordering instructions. The relative cost of each instruction group is the cost of replacing a SIMD instruction of an instruction group minus the cost of each instruction of the instruction group.
[0127] The substantial vectorization transformation of the program includes: executing a method of SIMD instruction generation and replacement using a vectorization solution corresponding to an optimal vector data flow.
[0128] The specific steps of the method for SIMD instruction generation and replacement are as follows:
[0129] Step S41: Sort the original basic blocks according to the instruction groups and the dependencies between the instruction groups in the optimal vector data stream to determine the order of the instructions in the basic blocks after sorting; the order of the instructions after sorting makes the instructions of the same instruction group in the vectorization scheme arranged continuously in the sorted basic blocks, and the order between the instructions in different instruction groups does not violate the dependency relationship between the instruction groups to which they belong.
[0130] Step S42: Generate corresponding SIMD instructions for each instruction group according to the instruction groups and indexes recorded in the optimal vector data stream, and insert reordering instructions between required SIMD instructions according to the records in the optimal vector data stream.
[0131] Step S43: After the SIMD instruction generation is completed, all scalar instructions replaced by SIMD instructions are deleted, and vectorization is completed.
[0132] The present invention does not elaborate on some of the well-known technologies that belong to those skilled in the art.
[0133] The above description is only a specific implementation of the present invention, but the protection scope of the present invention is not limited thereto. Any person familiar with the technology can understand and think of any changes or substitutions within the technical scope disclosed by the present invention, which should be included in the scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A compilation automatic vectorization method based on reorder instruction fusion, characterized in that: include: Step S1: Initialize a vector data stream, wherein the vector data stream is used to record instruction groups, indexes of instruction groups, and dependencies between instruction groups; Step S2: Collect the Store instructions in a basic block and group them according to the data type accessed and the address continuity, and finally form several Store instruction groups. All Store instructions in each Store instruction group access a continuous address space and have the same accessed data width; all Store instruction groups are added to the same vector data stream as seed instruction groups, and at the same time, an index is assigned to each Store instruction group; Step S3: starting from the vector data stream with the seed instruction group constructed in step S2, construct all feasible vector data streams, and select the optimal vector data stream from them; Step S4: determining whether the cost of determining the optimal vector data flow obtained in step S3 is greater than 0, and if so, abandoning vectorization; Otherwise, a substantial vectorization transformation of the program is performed according to the optimal vector data flow; The step S2 comprises: Step S21: searching for Store instructions in the basic block through the Store instruction collection module; Step S22: checking the dependency between the addresses accessed by the Store instructions, grouping multiple Store instructions that have no dependency between the addresses accessed as Store instructions that can be executed in parallel, and obtaining multiple groups of Store instructions that have undergone initial grouping; for each group of Store instructions that have undergone initial grouping, further sorting and grouping are performed according to the continuity of the addresses accessed by the Store instructions, and obtaining multiple Store instruction groups; Step S23: All the Store instruction groups are added to the same vector data stream as seed instruction groups. At the same time, for each Store instruction group, an index is assigned in ascending order according to the access address, so that all the Store instructions therein have different index values; wherein the index is the arrangement relationship of the instructions in the instruction group, and is a unique number assigned to each instruction in the instruction group; In step S3, constructing a vector data stream includes: Step S31: the vector data stream obtained by adding all seed instruction groups to the vector data stream is used as the initial vector data stream, the initial vector data stream is used as the vector data stream of the current level recursion and the current current level recursion is used as the 0th level recursion; Step S32: setting the optimal return vector data stream of the current level of recursion and the candidate instruction group set of the current level of recursion to invalid values; Step S33: constructing a set of candidate instruction groups for the current level of recursion according to the vector data stream for the current level of recursion; Step S34: determine whether the set of candidate instruction groups for the current level of recursion is empty; if the set of candidate instruction groups for the current level of recursion is determined to be empty, use the vector data stream for the current level of recursion as the optimal return vector data stream for the current level of recursion at the lowest level of recursion, and then execute step S36; otherwise, back up the vector data stream for the current level of recursion, the set of candidate instruction groups, and the optimal return vector data stream, and then execute step S35; Step S35: selecting a candidate instruction group that has not been taken from the set of candidate instruction groups of the current recursion level, and adding the candidate instruction group to the vector data stream of the current recursion level, so that the recursion level of the vector data stream of the current recursion level is increased by one; then turning to step S32; Step S36: restore the vector data stream, the set of candidate instruction groups and the optimal return vector data stream corresponding to the previous recursive level where the vector data stream of the current recursive level is located from the backup, and return the optimal return vector data stream of the current recursive level as a return value to the previous recursive level; Step S37: taking the previous level recursion in step S36 as the new current level recursion, calculating the index and cost of the return value of step S36, comparing it with the cost of the optimal return vector data flow of the current level recursion, and updating the optimal return vector data flow of the current level recursion according to the comparison result; Step S38: determining whether all the selected branches of the candidate instruction groups of the vector data stream of the current level of recursion have been calculated, if not, returning to the step S35; otherwise, returning to the step S36 to further return to the previous level of recursion until there is no previous level of recursion, and outputting the optimal return vector data stream and its cost of the current level of recursion at this time as the optimal vector data stream and its cost; In the step S37, the index and cost of the recursive vector data stream of the current level are calculated by incrementally calculating the incremental result of the index and cost of the vector data stream; the incremental result of the index is calculated first, and then the incremental result of the cost is calculated; the incremental calculation refers to calculating the index and cost of the candidate instruction group selected in step S35, and adding it to the index and cost of all subsequently added candidate instruction groups and all rearrangement instructions after the selected branch of the recursive vector data stream of the current level where the recursive vector data stream is located and has not yet been calculated, as the incremental result of the index and cost of the recursive vector data stream of the current level; If the optimal return vector data stream of the current level recursion is an invalid value or the cost of the optimal return vector data stream of the current level recursion is higher than the cost of the return value, the optimal return vector data stream of the current level recursion is updated to the return value; otherwise, the optimal return vector data stream of the current level recursion remains unchanged; The steps for calculating the index of the candidate instruction group selected in step S35 are as follows: Step S371: taking the added candidate instruction group as the current instruction group, determining whether the current instruction group is a Load instruction group, and if so, assigning an index to it; Step S372: Determine the dependency relationship between the current instruction group and other instruction groups of the recursive vector data stream at this level to determine the instruction groups on which the current instruction group depends; if the size of an instruction group on which the current instruction group depends does not match the size of the current instruction group, insert a reorder instruction between the two; if the current instruction group has the same size as an instruction group with which there is a dependency, and the relevant instruction group has been indexed, derive the index of the current instruction group based on the index of the relevant instruction group; Step S373: determining whether the current instruction group is indexed at this time, if the current instruction group is not indexed at this time, assigning an arbitrary index to the current instruction group; The optimal vector data flow refers to the vector data flow with the lowest cost; the cost of a vector data flow is the sum of the relative costs of all instruction groups of the vector data flow and the costs of all reordering instructions; the relative cost of an instruction group of a vector data flow is the cost of a SIMD instruction that replaces the instruction group minus the cost of each instruction of the instruction group.
2. The compilation automatic vectorization method based on reorder instruction fusion according to claim 1, characterized in that: In the step S22, further sorting and grouping is performed, including: treating multiple Store instructions that access a continuous address space and have the same accessed data width in the same group of Store instructions that have undergone initial grouping as one Store instruction group, and splitting multiple Store instructions that access data widths with different widths or access discontinuous address spaces into different Store instruction groups.
3. The compilation automatic vectorization method based on reorder instruction fusion according to claim 1, characterized in that: The candidate instruction group refers to an instruction group that has a resolvable dependency relationship with an instruction group in a given incomplete vector data stream; the set of candidate instruction groups refers to a set of all instruction groups that meet the conditions of the candidate instruction group; The resolvable dependency relationship means that if two instruction groups are each replaced by a SIMD instruction, a dependency relationship of the original semantics of the program can be directly established between the two SIMD instructions or by inserting a reorder instruction.
4. The compilation automatic vectorization method based on reorder instruction fusion according to claim 3, characterized in that: In step S33, a set of candidate instruction groups for the current level of recursion is constructed according to the vector data stream for the current level of recursion, including: Step S331: Initialize and obtain a set of instruction groups as a combination space; Step S332: for each parameter position of all instructions in each sub-instruction group except the Load instruction group in the recursive vector data stream of this level, all instructions corresponding to all parameters at a single parameter position are taken to form an initial instruction group; Step S333: Determine whether the instructions in the initial instruction group are of the same type; if the instructions in the initial instruction group are of the same type, directly add the initial instruction group to the combination space; if the instructions in the initial instruction group are not of the same operation type, check whether the instructions in the initial instruction group can be divided into two split instruction groups, and each split instruction group has the same operation type, and if so, add the two split instruction groups to the combination space respectively; Step S334: in the combination space, pairing instruction groups with the same operation type and the same size into one instruction group; Step S335: Perform Load instruction judgment on each instruction group in the combination space, and add it to the set of candidate instruction groups or discard it according to the Load instruction judgment result; Among them, the Load instruction judgment includes: judging whether the instruction group is a Load instruction group and whether the Load instruction accesses the same size and continuous address space; if the result of the Load instruction judgment of the instruction group is: the instruction group is a Load instruction group, and the Load instruction does not access the same size and continuous address space, then the instruction group is discarded and not added to the candidate instruction group set; otherwise, the instruction group is added to the set of candidate instruction groups.
Citation Information
Patent Citations
Automatic vectorizing method for heterogeneous SIMD expansion components
CN103279327A
Method, device and system for writing assembly codes and readable storage medium
CN108304218A
