Instruction conversion method and device and related equipment

By determining the number of loop unrolls in the target basic block and vectorizing it, the problem that scalar instructions cannot be fully vectorized is solved, and the processor's computing performance and program efficiency are improved.

CN118626145APending Publication Date: 2024-09-10HYGON INFORMATION TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410851860.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-09-10

AI Technical Summary

Technical Problem

In the existing technology, the efficiency of vectorization of scalar instructions is low, resulting in the processor being unable to fully utilize the computing performance of vector instructions. Especially when the number of scalar instructions is not an integer multiple of the number of vectorizable scalar instructions, complete vectorization processing cannot be achieved. .

Method used

By determining the number of loop unrolls of the target basic block so that the number of unrolled scalar instructions becomes an integer multiple of the number of vectorizable scalar instructions supported by the processor, vectorization processing is performed to achieve full vectorization of scalar instructions.

Benefits of technology

It increases the number of scalar instructions converted into vector instructions, reduces the limitations of vectorization, and improves the execution efficiency of the program and the utilization of hardware computing power.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118626145A
    Figure CN118626145A
Patent Text Reader

Abstract

The embodiment of the invention provides an instruction conversion method and device and related equipment, and the method comprises the steps: determining a target basic block, the target basic block is determined based on a source program, and the target basic block comprises a plurality of target scalar instructions with continuous addresses; determining a target loop expansion frequency corresponding to the target basic block at least according to the number of the plurality of target scalar instructions; the target basic block is expanded based on the target cycle expansion times to obtain an expanded basic block, the expanded basic block comprises a plurality of expanded target scalar instructions, and the number of the expanded target scalar instructions is an integral multiple of the number of vectorized scalar instructions supported by a processor; and carrying out vectorization processing on the expanded basic block to obtain a converted vector instruction. According to the technical scheme provided by the embodiment of the invention, the limitation on vectorization of the scalar instruction can be reduced, so that the conversion quantity of converting the scalar instruction into the vector instruction is increased.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The embodiments of the present application relate to the field of computer technology, and specifically to an instruction conversion method, apparatus, and related equipment. Background Art

[0002] Single Instruction Multiple Data (SIMD) is a parallel computing method, that is, one instruction can operate on multiple data at the same time. SIMD aims to explore the data-level parallelism of programs in data processing related fields. Compiler auto-vectorization is one of the vector exploration methods based on SIMD, that is, auto-vectorization can realize the conversion of scalar instructions to vector instructions, thereby effectively improving the parallel data processing capabilities of data processing devices such as processors.

[0003] In this context, how to provide a technical solution to reduce the limitations of vectorization of scalar instructions, thereby increasing the number of conversions from scalar instructions to vector instructions, has become a technical problem that technical personnel in this field urgently need to solve. Summary of the invention

[0004] In view of this, an embodiment of the present invention provides an instruction conversion method, apparatus and related devices to reduce the limitations on vectorization of scalar instructions, thereby increasing the number of conversions from scalar instructions to vector instructions.

[0005] To achieve the above objectives, the embodiments of the present invention provide the following technical solutions.

[0006] In a first aspect, an embodiment of the present invention provides an instruction conversion method, comprising:

[0007] Determine a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions;

[0008] Determining a target number of loop unrollings corresponding to the target basic block based at least on the number of the plurality of target scalar instructions;

[0009] Expanding the target basic block based on the target loop expansion number to obtain an expanded basic block, wherein the expanded basic block includes a plurality of expanded target scalar instructions, and the number of the plurality of expanded target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor;

[0010] Vectorization is performed on the expanded basic block to obtain converted vector instructions.

[0011] In a second aspect, an embodiment of the present invention provides an instruction conversion device, including:

[0012] A target basic block determination module, configured to determine a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions;

[0013] A loop unrolling number determination module, configured to determine a target loop unrolling number corresponding to the target basic block according to at least the number of the plurality of target scalar instructions;

[0014] A loop unrolling module, configured to unroll the target basic block based on the target loop unrolling number to obtain an unrolled basic block, wherein the unrolled basic block includes a plurality of unrolled target scalar instructions, and the number of the plurality of unrolled target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor;

[0015] The vector instruction generation module is used to perform vectorization processing on the expanded basic block to obtain a converted vector instruction.

[0016] In a third aspect, an embodiment of the present invention provides an electronic device, including a memory and a processor, wherein the memory stores a program, and the processor calls the program stored in the memory to execute the instruction conversion method as described in the first aspect.

[0017] In a fourth aspect, an embodiment of the present invention provides a storage medium, wherein the storage medium stores a program, and when the program is executed, the instruction conversion method as described in the first aspect is implemented.

[0018] In a fifth aspect, an embodiment of the present invention provides a computer program product, including a computer program, which implements the instruction conversion method described in the first aspect when executed by a processor.

[0019] An instruction conversion method provided by an embodiment of the present invention comprises the following steps: first, determining a target basic block, wherein the target basic block is determined based on a source program and includes a plurality of target scalar instructions; then, determining a target loop expansion number corresponding to the target basic block at least according to the number of the plurality of target scalar instructions; expanding the target basic block based on the target loop expansion number to obtain an expanded basic block, wherein the expanded basic block includes a plurality of expanded target scalar instructions, and the number of the plurality of expanded target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by a processor; finally, vectorizing the expanded basic block to obtain a converted vector instruction.

[0020] The instruction conversion method provided by the embodiment of the present invention, when realizing the automatic vectorization processing of scalar instructions, for the target basic block, at least according to the number of target scalar instructions included in the target basic block, calculates the target loop expansion number corresponding to the target basic block; thereby, using the target loop expansion number, loop expansion is performed on each target scalar instruction of the target basic block, so that the number of the expanded target scalar instructions in the expanded basic block is: an integer multiple of the number of vectorizable scalar instructions supported by the processor. Therefore, each expanded target scalar instruction in the expanded basic block can be divided according to the number of vectorizable scalar instructions, so as to be merged into multiple vector instructions, and the vectorization processing of the expanded target scalar instructions can be realized. It can be seen that the embodiment of the present invention can make the number of the expanded target scalar instructions in the expanded basic block an integer multiple of the number of vectorizable scalar instructions, so that the target basic block can realize vectorization processing after expansion, reduce the limitation of the vectorization of scalar instructions, and improve the conversion number of scalar instructions into vector instructions. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0022] Figure 1 It is a schematic diagram of an implementation process of automatic vectorization provided by an embodiment of the present invention;

[0023] Figure 2 is a flow chart of an instruction conversion method provided by an embodiment of the present invention;

[0024] Figure 3 is another flow chart of the instruction conversion method provided by an embodiment of the present invention;

[0025] Figure 4 is a structural diagram of a target basic block;

[0026] Figure 5 The instruction conversion method provided by the embodiment of the present invention is Figure 4 A schematic diagram of a result of processing the target basic block is shown;

[0027] Figure 6 is a flowchart of automatic vectorization corresponding to the instruction conversion method provided by an embodiment of the present invention;

[0028] Figure 7 It is a structural diagram of an instruction conversion device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0029] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only 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.

[0030] SIMD instruction sets are widely adopted based on the demand of modern processors to improve data processing performance and power efficiency. However, it is difficult to write code that effectively utilizes SIMD instruction sets, and the types of SIMD instruction sets used or supported on different platforms are also different. In the case where the processor supports the execution of vector instructions, compiler-based automatic vectorization is a solution to write code that effectively utilizes SIMD instruction sets.

[0031] The compiler's automatic vectorization is mainly divided into loop vectorization and superword level parallelism (SLP) vectorization.

[0032] The loop vectorization solution is mainly applied to loop structures formed by loop statements in the source program, especially loops with a fixed number of iterations in the source program. Loop vectorization focuses on vectorization opportunities between loop iterations, which will reduce the number of loop iterations and increase the amount of computational data for a single iteration.

[0033] The SLP vectorization scheme is mainly aimed at a single basic block, where a basic block is a code segment formed by a group of scalar instructions with potential vectorization opportunities, and there are no control flow inlets and outlets in the basic block. SLP vectorization focuses on vectorization opportunities within iterations (within basic blocks), and will merge similar scalar calculations / memory accesses in a single iteration into a vector instruction, that is, reducing the number of instructions generated within a single iteration.

[0034] The process of compiler automatically vectorizing source programs can be referred to Figure 1 , Figure 1 The figure is a schematic diagram of an implementation process of automatic vectorization provided by an embodiment of the present invention.

[0035] The compiler's automatic vectorization includes two vectorization analysis and processing processes (PASS):

[0036] The first PASS is the analysis and processing of loop vectorization (loop vectorization PASS).

[0037] After completing the loop vectorization PASS, the second PASS is performed.

[0038] The second PASS is the analysis and processing of SLP vectorization (SLP vectorization PASS).

[0039] The implementation process of automatic vectorization can be found in the following Figure 1 , mainly including the following steps.

[0040] First, perform loop vectorization PASS:

[0041] Step 1: Traverse the basic blocks divided based on the source program.

[0042] Step 2: Perform data dependency analysis on each traversed basic block.

[0043] When performing data dependency analysis, the main focus is to determine the execution status of the scalar instructions of each basic block and whether the scalar instructions of each basic block are interdependent. For basic blocks where the scalar instructions are not interdependent, determine whether the operation types of the scalar instructions are the same and whether the addresses of the operands (including source operands and destination operands) are continuous; thereby determining a group of scalar instructions with potential vectorization opportunities, and then determining that the basic block formed by this group of scalar instructions is a basic block that can be automatically vectorized.

[0044] Step 3: Analyze the feasibility of SLP vectorization for the basic blocks that have completed data dependency analysis.

[0045] After dividing the source program into basic blocks, the basic blocks are traversed, and the feasibility of SLP vectorization is analyzed for the basic blocks that successfully pass the data dependency analysis.

[0046] When performing SLP vectorization feasibility analysis on a basic block, the main focus is on analyzing whether the number of scalar instructions in the basic block meets the number of vectorizable scalar instructions supported by the processor (for example, the number of vectorizable scalar instructions is 2 n , the value of n can be 0, 1, 2, etc.).

[0047] After determining that the basic block can be SLP vectorized, the SLP vectorization PASS can be directly performed, that is, step 6: SLP vectorization PASS is executed, where PASS represents the vectorization analysis and processing process.

[0048] After determining that the basic block is not SLP vectorizable, step 4 is performed: performing loop vectorization feasibility analysis on the basic block.

[0049] In step 4, loop vectorization feasibility analysis refers to analyzing whether the scalar instructions in the basic block can be loop vectorized based on the source program. The loop vectorization process mainly determines whether there is a loop program in the source program that can be vectorized, and then vectorizes the loop program mainly.

[0050] Step 5: Perform loop vectorization on the basic block.

[0051] The loop vectorization PASS is completed, and then the SLP vectorization PASS is performed:

[0052] Step 6: SLP vectorizes PASS.

[0053] After completing the SLP vectorization PASS, the overall process of the compiler's automatic vectorization processing is completed.

[0054] It should be noted that the SLP vectorization feasibility analysis in the loop vectorization PASS is not the SLP vectorization PASS. The SLP vectorization feasibility analysis is a step in the vectorization analysis and processing process of the loop vectorization PASS.

[0055] However, the following problems exist in the implementation of the above automatic vectorization:

[0056] On the one hand, during the implementation of loop vectorization, when there is a strong correlation in the vertical operation logic, that is, in the loop program in the next loop, some operations need to wait for the operation results of the loop program in the previous loop, which results in the loop program in this loop being unable to be loop vectorized. Similarly, if some operations in the basic block are not exactly the same, the basic block cannot be SLP vectorized. Since automatic vectorization technology optimizes the source program based on the entire Basic Block or loop Basic Block (basic block that cannot be SLP), if some basic blocks cannot be vectorized, correctness analysis cannot be performed correctly, resulting in the inability to vectorize the scalar instructions in the basic block.

[0057] On the other hand, even if the SLP vectorization conditions are met, if the number of scalar instructions (elements) in the basic block is not a power of 2, since the number of scalar instructions that is not a power of 2 is not the number of vectorizable scalar instructions, it cannot be directly vectorized. In this case, the compiler will use a splitting method for vectorization.

[0058] For example, if a basic block includes three scalar instructions, the compiler will split the three scalar instructions into a 2+1 combination of scalar instructions. The scalar instruction combination of 2 is vectorized, and the remaining scalar instruction continues to be processed using scalars, which means that the SLP vectorization method cannot maximize the computing performance of vector instructions.

[0059] Based on this, an embodiment of the present invention provides an instruction conversion method to achieve full vectorization of scalar instructions in a basic block, increase the number of conversions of scalar instructions into vector instructions, and thereby maximize the utilization of the computing performance of vector instructions and improve the execution efficiency of the overall program.

[0060] Please refer to Figure 2 , Figure 2 It is a flowchart of an instruction conversion method provided by an embodiment of the present invention.

[0061] like Figure 2 As shown, the process may include the following steps:

[0062] Step S100 , determining a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions.

[0063] The target basic block can be obtained by partitioning the source program, which serves as the basis for subsequent analysis of whether it is SLP vectorization feasible or loop vectorization feasible.

[0064] When dividing a source program into basic blocks, it can be divided according to the program instructions of the source program. First, find the entry of the basic block in the program instructions of the source program. The entry of a basic block can be divided into three categories: 1. The first instruction of the code segment, 2. The target statement of the conditional jump and unconditional jump, 3. The next statement of the conditional jump statement; then, divide the source program into multiple basic blocks according to the divided entry; the range of a basic block is: starting from the determined entry until the entry of the next basic block is encountered.

[0065] When performing automatic vectorization, a basic block whose scalar instruction addresses are continuous and whose executions of various scalar instructions are independent of each other is selected from multiple basic blocks as the basis for determining the target basic block.

[0066] Step S101, determining a target loop unrolling number corresponding to the target basic block at least according to the number of the multiple target scalar instructions.

[0067] based on Figure 1As shown in the flowchart of automatic vectorization, it can be seen that when a basic block is automatically vectorized, in the loop vectorization PASS, it is first analyzed whether it has the feasibility of SLP vectorization, and if so, the SLP vectorization PASS is directly performed. However, the basic blocks with SLP vectorization feasibility also include basic blocks whose number of scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the non-processor.

[0068] For example, for a basic block including three scalar instructions, when the number of vectorizable scalar instructions is two, the three scalar instructions will be split and then vectorized. One of the processed instructions is still a scalar instruction, so that the scalar instructions in the basic block cannot be fully vectorized.

[0069] For basic blocks that do not have the feasibility of SLP vectorization, the feasibility of loop vectorization is analyzed for the basic blocks. However, since the loop vectorization PASS is performed on the entire source program, if the execution of the scalar instructions of a basic block in a loop Q requires the execution results of basic blocks in other loops P, then the basic blocks in the loop Q cannot be loop vectorized. In other words, for basic blocks that do not have the feasibility of SLP vectorization, some basic blocks cannot be vectorized.

[0070] Therefore, in the process of SLP vectorization and loop vectorization, the target basic block has insufficient scalar instruction vectorization. In view of this problem, in the embodiment of the present invention, based on the determined target basic block (regardless of whether the target basic block satisfies SLP vectorization or loop vectorization), the target basic block is processed by directly performing loop expansion according to the target loop expansion times, so that in the expanded basic block, the number of expanded target scalar instructions is an integer multiple of the number of vectorizable scalar instructions, so that after the target basic block is expanded, all the expanded target scalar instructions included in the expanded basic block can be vectorized, thereby improving the number of conversions of scalar instructions to vector instructions.

[0071] The target loop unrolling times are used to copy each target scalar instruction of the target basic block a corresponding number of times.

[0072] For example, the target loop unrolling times is 2, and the target basic block includes a first target scalar instruction, a second target scalar instruction, and a third target scalar instruction. Then each target scalar instruction in the target basic block is copied with the target loop unrolling times being 2, that is, loop unrolling of each target scalar instruction is achieved. The obtained unrolled target scalar instructions include 2 identical first target scalar instructions, 2 identical second target scalar instructions, and 2 identical third target scalar instructions, for a total of 6 unrolled target scalar instructions.

[0073] Step S102: Expand the target basic block based on the target loop expansion number to obtain an expanded basic block.

[0074] The expanded basic block includes a plurality of expanded target scalar instructions, and the number of the plurality of expanded target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor.

[0075] Vectorization can be considered as a process of dividing and combining the scalar instructions in a basic block into corresponding numbers according to the number of vectorizable scalar instructions. Therefore, when the number of scalar instructions in the target basic block is not an integer multiple of the number of vectorizable scalar instructions, all vectorization cannot be performed.

[0076] It should be noted that a target basic block that is not an integer multiple of the number of vectorizable scalar instructions may be a basic block in which the number of target scalar instructions is less than the number of vectorizable scalar instructions, for example, a basic block in which the number of vectorizable scalar instructions is 4 and the number of scalar instructions of the basic block is 3.

[0077] Alternatively, the target basic block may be a basic block in which the number of target scalar instructions is not less than the number of vectorizable scalar instructions, but the number of target scalar instructions is not an integer multiple of the number of vectorizable scalar instructions. For example, the number of vectorizable scalar instructions is 4, and the number of scalar instructions of the basic block is 6 (in this case, the target basic block is a basic block in which the number of target scalar instructions is greater than the number of vectorizable scalar instructions).

[0078] Step S103, performing vectorization processing on the expanded basic block to obtain converted vector instructions.

[0079] Since the number of expanded target scalar instructions in the expanded basic block is an integer multiple K of the number S of vectorizable scalar instructions supported by the processor, the expanded target scalar instructions can be vectorized according to the number of vectorizable scalar instructions and divided into K vector instructions, each vector instruction including S expanded target scalar instructions.

[0080] It can be seen that the instruction conversion method provided by the embodiment of the present invention, when realizing the automatic vectorization processing of scalar instructions, for the target basic block, at least according to the number of target scalar instructions included in the target basic block, calculates the target loop expansion number corresponding to the target basic block; thereby, using the target loop expansion number, loop expansion is performed on each target scalar instruction of the target basic block, so that the number of expanded target scalar instructions in the expanded basic block is: an integer multiple of the number of vectorizable scalar instructions supported by the processor. Thus, each expanded target scalar instruction in the expanded basic block can be divided according to the number of vectorizable scalar instructions, so as to be merged into multiple vector instructions, and the vectorization processing of the expanded target scalar instruction can be realized. It can be seen that the embodiment of the present invention can make the number of expanded target scalar instructions in the expanded basic block an integer multiple of the number of vectorizable scalar instructions, so that the target basic block can realize vectorization processing after expansion, reduce the limitation of vectorization of scalar instructions, and improve the conversion number of scalar instructions into vector instructions.

[0081] In order to ensure that the number of target scalar instructions after loop unrolling is an integer multiple of the number of vectorizable scalar instructions, and to ensure full vectorization of the target scalar instructions in the target basic block, in one implementation, the target loop unrolling times can be accurately calculated based on the actual configuration of the processor and the number of target scalar instructions in the target basic block.

[0082] Please refer to Figure 3 , Figure 3 It is another flowchart diagram of the instruction conversion method provided by an embodiment of the present invention.

[0083] like Figure 3 As shown, the method comprises the following steps:

[0084] Step S200, obtaining configuration parameters of the processor.

[0085] The configuration parameters of the processor are determined according to the specific functions implemented by the processor, and different processors have different configuration parameters.

[0086] Step S201, determining the number of vectorizable scalar instructions supported by the processor according to the configuration parameters of the processor.

[0087] The number of vectorizable scalar instructions supported by the processor is determined according to the specific functional implementation of the processor, and the configuration parameters of the processor correspond to the specific functional implementation of the processor. Therefore, the number of vectorizable scalar instructions supported by the processor can be obtained according to the configuration parameters of the processor, so as to determine the number of loop unrollings later.

[0088] For example, in the configuration parameters of the processor, the bit width of the vector register is 128 bits, then the number of vectorizable scalar instructions supported by the processor can be, merging two 64-bit scalar instructions into one vector instruction, that is, the number of vectorizable scalar instructions supported by the processor is 2. Of course, it is also possible to merge four 32-bit scalar instructions into one vector instruction, then the number of vectorizable scalar instructions supported by the processor is 4.

[0089] Step S202 : determining a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions.

[0090] The target basic block includes multiple target scalar instructions, and the operation type of each target scalar instruction is the same. For example, the target basic block includes 3 target scalar instructions, which can be expressed as t[j]+=a[i][j], j=1, 2, 3, and i is any value greater than or equal to 1. Among them, t[j] is the destination operand of the one-dimensional array, a[i][j] is the source operand of the two-dimensional array, and t[j]+=a[i][j] means t[j]=t[j]+a[i][j]; the addresses of each target scalar instruction are continuous, for example, the addresses of a[i][1], a[i][2] and a[i][3] are continuous, and the addresses of t[1], t[2] and t[3] are continuous, and the operations of each target scalar instruction are independent of each other.

[0091] In the embodiment of the present invention, after traversing and analyzing the data dependency of the basic blocks, the basic blocks that can be SLP vectorized PASS but cannot realize the full vectorization of scalar instructions and the basic blocks that cannot be SLP vectorized PASS are used as target basic blocks, so as to perform loop expansion on the target basic blocks and disguise the target basic blocks as basic blocks that can be fully vectorized (i.e., the expanded basic blocks).

[0092] In order to accurately determine the target basic block and achieve full vectorization of the scalar instructions of the basic block, in one implementation, step S202 may include:

[0093] A source program is obtained, wherein the source program includes multiple basic blocks, one basic block includes multiple scalar instructions, and the basic block is located in a loop of the source program; a candidate basic block is determined from the multiple basic blocks, the addresses of the multiple scalar instructions contained in the candidate basic block are continuous, and the operation types of the multiple scalar instructions are the same; based on the number of scalar instructions in the candidate basic block, a target basic block is determined from the candidate basic blocks.

[0094] Based on the above content, we can know that basic blocks are obtained by dividing the source program according to different types of entries. Therefore, there may be basic blocks that do not have vectorization potential, that is, the operation types of scalar instructions in the basic block are different, and the addresses of various scalar instructions are not continuous, so they do not have vectorization potential and cannot be automatically vectorized.

[0095] By traversing each basic block and analyzing data dependency, a basic block including multiple scalar instructions with consecutive addresses and the same operation type can be determined from multiple basic blocks as a candidate basic block. The candidate basic block is a basic block that can be automatically vectorized.

[0096] According to the number of scalar instructions of the candidate basic block, the target basic block according to the embodiment of the present invention is determined.

[0097] In one embodiment, determining a target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks includes:

[0098] A candidate basic block whose number of scalar instructions satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions is determined as a target basic block.

[0099] A candidate basic block that satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions; wherein, the number of vectorizable scalar instructions means that the number of scalar instructions of the candidate basic block is not less than the number of vectorizable scalar instructions. For example, when the number of vectorizable scalar instructions is S=2, the number of scalar instructions of the candidate basic block is N=3, and at this time, the candidate basic block can be vectorized. For example, when the number of scalar instructions of the candidate basic block is N=3, two of the scalar instructions are usually merged into one vector instruction, and the original one scalar instruction is retained (i.e., a method that cannot fully realize the vectorization of scalar instructions). However, the split processing method will cause the scalar instructions of the candidate basic block to be unable to be fully vectorized, thereby failing to effectively realize the hardware computing power.

[0100] Therefore, in an embodiment of the present invention, the above-mentioned type of candidate basic blocks are determined as target basic blocks, so that the scalar instructions of the target basic blocks are completely vectorized by processing the target loop unrolling times.

[0101] In some other implementations, determining the target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks includes:

[0102] A candidate basic block whose number of scalar instructions does not satisfy the number of vectorizable scalar instructions is determined as a target basic block.

[0103] The number of scalar instructions does not satisfy the candidate basic block of the number of vectorizable scalar instructions. For example, when the number of vectorizable scalar instructions is S=4, the number of scalar instructions of the candidate basic block is 3. At this time, the three scalar instructions do not satisfy the requirement of merging the four scalar instructions into one vector instruction. Therefore, the candidate basic block cannot be vectorized, and thus each scalar instruction in the candidate basic block cannot be vectorized, which reduces the program lines, affects the program running speed, and cannot give full play to the hardware computing power.

[0104] Therefore, in the embodiment of the present invention, the above-mentioned types of candidate basic blocks are also determined as target basic blocks. After the expansion processing using the target loop expansion times, the number of target scalar instructions after the expansion is expanded to an integer multiple of the number of vectorizable scalar instructions, thereby achieving full vectorization of this type of candidate basic blocks, increasing program lines, improving program running speed, and better utilizing hardware computing power.

[0105] Step S203: determining a target loop unrolling number corresponding to the target basic block according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions.

[0106] Since the SLP vectorization feasibility analysis is based on the number of vectorizable scalar instructions, there are target basic blocks that can be vectorized but not completely vectorized (for example, when the number of vectorizable scalar instructions is 2, the target basic blocks include 3 scalar instructions) and target basic blocks that cannot be SLP vectorized (for example, when the number of vectorizable scalar instructions is 4, the target basic blocks include 3 scalar instructions).

[0107] Therefore, in order to enable the target scalar instructions in the target basic block to be fully vectorized, in an embodiment of the present invention, the target loop unrolling times are calculated in combination with the number of vectorizable scalar instructions and the number of target scalar instructions included in the target basic block, so that in the basic block after the loop is unrolled, the number of unrolled target scalar instructions is an integer multiple of the number of vectorizable scalar instructions.

[0108] For example, if the number S of vectorizable scalar instructions is 2, and the number N of target scalar instructions of the target basic block is 3, then after loop unrolling of the target loop unrolling number X (e.g., X=2), the number of unrolled target scalar instructions is N×X=3×2=6, which is three times the number S of vectorizable scalar instructions. Therefore, all unrolled target scalar instructions can be vectorized to obtain three converted vector instructions, each of which is formed by two unrolled target scalar instructions.

[0109] In one implementation, step S203 may include the following steps:

[0110] Determine the least common multiple of the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions; divide the least common multiple by the number of the multiple target scalar instructions, and determine the result as the target loop unrolling count corresponding to the target basic block.

[0111] For example, if the number of target scalar instructions N of the target basic block is 3, and the number of vectorizable scalar instructions S is 4, the least common multiple can be calculated to be 12; then the least common multiple 12 is divided by the number of target scalar instructions N to obtain the target loop unrolling times X=4.

[0112] Please continue to refer to Figure 3 , the method further comprises:

[0113] Step S204: Expand the target basic block based on the target loop expansion number to obtain an expanded basic block.

[0114] The target loop unrolling number X=4 obtained by the above calculation is continued as an example for description.

[0115] The three target scalar instructions in the target basic block are loop-unrolled X=4 times, and the expanded basic block includes 3×4=12 expanded target scalar instructions, which is three times S=4. Therefore, according to the number of vectorizable scalar instructions S=4, the expanded target scalar instructions can be divided and merged into three vector instructions, wherein every four expanded target scalar instructions are divided into one vector instruction, and all the expanded target scalar instructions in the expanded basic block are vectorized, that is, all the target scalar instructions of the target basic block can be vectorized.

[0116] In one implementation, step S204 may include:

[0117] Based on the target number of loop expansions, loop expansion is performed on the destination operands of each target scalar instruction to obtain expanded destination operands, and the number of expanded destination operands is an integer multiple of the number of vectorizable scalar instructions; and based on the target number of loop expansions, loop expansion is performed on the source operands of each target scalar instruction to obtain expanded source operands, and the number of expanded source operands is an integer multiple of the number of vectorizable scalar instructions; based on each expanded destination operand and each expanded source operand, an expanded target scalar instruction is formed to obtain an expanded basic block.

[0118] For ease of explanation, here we take the case where the number of vectorizable scalar instructions is S=2, the number of target scalar instructions is N=3, and the target basic block is set to BB1 as an example, where the target scalar instructions of the target basic block BB1 are: total[1]+=a[i][1], total[2]+=a[i][2], total[3]+=a[i][3].

[0119] According to the aforementioned method of calculating the target loop unrolling number X, it can be obtained that the target loop unrolling number is X=2 at this time.

[0120] In each target scalar instruction in BB1, total[1], total[2], and total[3] are destination operands, which are one-dimensional arrays with continuous addresses. a[i][1], a[i][2], and a[i][3] are source operands, and a[i][1]-a[i][3] are two-dimensional arrays with continuous addresses (that is, the addresses are continuous within the range of i rows and 3 columns, the addresses of a[i+1][1] and a[i][1] are discontinuous, and i is an arbitrary value).

[0121] Unrolling BB1 X = 2 times can be expressed as:

[0122] The destination operand is loop expanded X=2 times, and the destination operands after expansion are: total[1], total[2], total[3], total[1], total[2], total[3]. Since total[1], total[2], total[3] are one-dimensional arrays with continuous addresses, the addresses of the destination operands after expansion are still continuous.

[0123] The source operands are loop expanded X=2 times, and the expanded source operands are: a[1][1], a[1][2], a[1][3], a[2][1], a[2][2], a[2][3]. Since a[i][1], a[i][2], a[i][3] are consecutive addresses within the range of i rows and 3 columns, the addresses of a[1][1], a[1][2], a[1][3] are consecutive, the addresses of a[2][1], a[2][2], a[2][3] are consecutive, and the addresses of a[1][3] and a[2][1] are not consecutive.

[0124] It can be seen that the number of the expanded destination operands and the number of the expanded source operands are both 6, which is 3 times the number of vectorizable scalar instructions 2. After the expanded target scalar instructions are obtained based on the expanded destination operands and the expanded source operands, the number of the expanded target scalar instructions is 6, which is also 3 times the number of vectorizable scalar instructions 2. Therefore, the expanded target scalar instructions can be fully vectorized according to the number of vectorizable scalar instructions.

[0125] It should be noted that, although the target loop unrolling times are used to unroll the target basic block in the embodiment of the present invention, it is different from loop vectorization. Loop vectorization is the unrolling of the loop structure formed by the loop statement in the source program, which is based on the loop iteration direction of the source program. For example, when loop vectorization is performed, if x+=y[i] appears in one loop, and x+=y[i+1] appears in the next loop, and the addresses of y[i] and y[i+1] are continuous, then direct vectorization processing can be performed on x+=y[i] and x+=y[i+1] whose addresses are continuous in the loop iteration direction of the source program.

[0126] In the technical solution provided by the embodiment of the present invention, the vectorization is performed based on continuous memory access in a single loop, that is, in a single loop, for scalar instructions (target scalar instructions) with continuous addresses, loop expansion is performed so that the number of expanded elements (expanded target scalar instructions) can be filled with an integer multiple of the number of vectorizable scalar instructions supported by the processor. For example, when vectorization is performed using the technical solution provided by the embodiment of the present invention, x1+=y[i] and x2+=y[i+1] appear in a round of loops, where the addresses of y[i] and y[i+1] are continuous, then x1+=y[i] and x2+=y[i+1] can be used as the target scalar instructions described in the embodiment of the present invention, and then in the round of loops, the target scalar instructions are loop expanded using the determined target loop expansion times, so that the expanded target scalar instructions are an integer multiple of the number of vectorizable scalar instructions supported by the processor, and then the expanded target scalar instructions are vectorized.

[0127] It can be seen that loop vectorization essentially requires that the addresses of the expanded scalar instructions in the loop iteration direction are continuous. Therefore, during vectorization processing, the expanded scalar instructions are vectorized based on the loop iteration direction; and the technical solution provided by the embodiment of the present invention is to give priority to vectorizing the continuous memory accesses in the same operation in the expanded target basic block (the expanded target scalar instructions obtained after the loop expansion of the target scalar instructions with continuous addresses), and this process is assisted by loop expansion to complete the rounding of the number of vectorizable scalar instructions supported by the processor.

[0128] Please continue to refer to Figure 3, the method further comprises:

[0129] Step S205: performing vectorization processing on the expanded basic block to obtain converted vector instructions.

[0130] The expanded basic block includes an integer multiple of the number of the vectorizable scalar instructions of the expanded target scalar instructions, and thus, full vectorization of the expanded basic block can be achieved.

[0131] In one embodiment, the implementation of fully vectorizing the expanded target scalar instruction, that is, the implementation of step S205, may include:

[0132] According to the number of the vectorizable scalar instructions, the expanded destination operands are grouped to obtain an expanded destination operand array; according to the number of the vectorizable scalar instructions, source operands are read according to the addresses of the expanded source operands, and the read source operands are processed to make the read source operands consistent with the expanded source operands to obtain each group of expanded source operand arrays, wherein each group of expanded source operand arrays contains the number of source operands read by the vectorizable scalar instructions; each group of expanded destination operand arrays is combined with each group of expanded source operand arrays to obtain converted vector instructions.

[0133] The following description is continued with the example that the number of vectorizable scalar instructions S=2, the number of target scalar instructions of the target basic block N=3, and the target number of loop unrolling times X=2.

[0134] The expanded destination operands are: total[1], total[2], total[3], total[1], total[2], total[3]. When performing vectorization processing, the two expanded destination operands can be grouped as one according to S=2, and divided into three groups of expanded destination operand arrays: {total[1], total[2]}, {total[3], total[1]}, {total[2], total[3]}.

[0135] The expanded source operands are: a[1][1], a[1][2], a[1][3], a[2][1], a[2][2], a[2][3]. Then, according to the number of vectorizable scalar instructions S=2, the corresponding operands are accessed or read according to the addresses of the expanded operands.

[0136] For example, in a specific implementation, the address of the expanded source operand can be loaded into the vector register according to the number S=2 of the vectorizable scalar instructions. Since continuous addresses are loaded into the vector register, for the expanded operands a[1][1], a[1][2], a[1][3], a[2][1], a[2][2], a[2][3], the addresses are loaded as {a[1][1], a[1][2]}, {a[1][3], a[1][4]} and {a[1][5]}. [2][1], a[2][2]}, {a[2][3], a[2][4]} are loaded, where a[1][4] is set to the next address consecutive to a[1][3], and a[2][4] is set to the next address consecutive to a[2][3]; therefore, the data read according to the address loaded at this time will include unnecessary source operands: the data read from the address a[1][4] and the data read from the address a[2][4] (i.e., data inconsistent with the data read based on the address of the expanded source operand).

[0137] Therefore, in the vector register, the source operand read is further processed according to the expanded source operand, for example, the data a read from the address a[2][1] is processed using the concatenation instruction. 21 Concatenate to data a read from address a[1][3] 13 , to replace the unnecessary data read from address a[1][4].

[0138] Finally, the expanded source operand arrays of each group of source operands read including the vectorizable scalar instructions, number S=2, are obtained: {a[1][1], a[1][2]}, {a[1][3] , a[2][1]}, {a[2][2], a[2][3]}.

[0139] Each group of expanded source operation arrays and each group of expanded destination operation arrays are combined to obtain converted vector instructions.

[0140] The converted vector instruction can be expressed as:

[0141] For the first group {total[1], total[2]} and the first group of expanded source operation arrays {a[1][1], a[1][2]}, the converted vector instruction 1 is: vect_total_1_2+=vect_a_1_2[i], i=1,2.

[0142] For the second group {total[3], total[1]} and the second group, the expanded source operation array {a[1][3] ,a[2][1]}, the converted vector instruction 2 is: vect_total_3_1+=<a[i][3],a[i+1][1]> , i=1,2.

[0143] For the third group {total[2], total[3]} and the third group of expanded source operation arrays {a[2][2], a[2][3]}, the converted vector instruction 3 is: vect_total_2_3+=vect_a_2_3[i+1], i=1,2.

[0144] The addresses of the expanded destination operands of each group are continuous, and the expanded source operation array of each group includes expanded source operands with continuous addresses (vect_a_1_2[i], vect_a_2_3[i+1]) and discontinuous addresses (a[i][3], a[i+1][1]).

[0145] The above embodiment is a vectorization processing process corresponding to the case where the number of scalar instructions satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions, and is determined as a target basic block. The corresponding converted vector instructions include:

[0146] Vector instructions with consecutive destination operand addresses and consecutive source operand addresses;

[0147] Also, vector instructions whose destination operand addresses are continuous but whose source operand addresses are not continuous.

[0148] The vector instructions whose destination operand addresses are continuous and whose source operand addresses are continuous are vect_total_1_2+=vect_a_1_2[i] and vect_total_2_3+=vect_a_2_3[i+1], where i=1,2.

[0149] The vector instruction with continuous destination operand addresses but discontinuous source operand addresses is vect_total_3_1+=<a[i][3],a[i+1][1]> , i=1,2.

[0150] Taking the loop execution of the target basic block BB1 in the source program with i < N (N = 3) as an example, that is, when the number of target scalar instructions in the target basic block is N = 3 and the number of vectorizable scalar instructions is S = 2, the target basic block BB1 needs to execute 2 loops. Therefore, in the embodiment of the present invention, the target loop is unfolded X = 2 times, and the loop step size is the target loop unfolding times. That is, after one loop execution, it is equivalent to completing 2 rounds of loops. Therefore, after the instruction conversion method provided by the embodiment of the present invention, the result after one execution completes the execution of the target basic block BB1, which is equivalent to the loop executing 2 times in the original method.

[0151] Therefore, in the case of N = 3 and S = 2 above, in the original method, the target scalar instructions of the target basic block BB1 are split. Then, in one round of loop, 2 of the target scalar instructions are vectorized to form a converted vector instruction, and the remaining one remains a scalar instruction. Thus, a converted vector instruction vect_total_1_2+ = vect_a_1_2[i] and a scalar instruction total[3]+ = a[i][3] can be obtained. That is, in the original method, after one round of loop execution, 2 memory accesses (the memory access of the source operand of a vector instruction and the memory access of the source operand of a scalar instruction) and 2 addition operations need to be performed. Then, after executing all 2 rounds of loops, 4 memory accesses and 4 addition operations need to be performed.

[0152] In the embodiment of the present invention, since the loop step size is the target loop unfolding times, the loop execution of the target basic block BB1 can be completed after one execution, and 3 converted vector instructions are obtained, one of which is a vector instruction with discontinuous addresses of the source operands after unfolding. Therefore, in the embodiment of the present invention, only 2 memory accesses and 3 addition operations need to be performed. Compared with the original method, the embodiment of the present invention can save the execution of 2 memory accesses and 1 addition operation, effectively improving the running speed of the program.

[0153] When a candidate basic block with the number of scalar instructions not meeting the number of vectorizable scalar instructions is determined as the target basic block, for example, when the number of target scalar instructions of the target basic block BB2 described in the foregoing embodiment is N = 3 and the number of vectorizable scalar instructions is S = 4, the corresponding vectorization process can refer to Figure 4 and Figure 5 , Figure 4 is a schematic structural diagram of the target basic block, Figure 5 is the result of the instruction conversion method provided by the embodiment of the present invention for Figure 4 the target basic block shown in the figure.

[0154] As Figure 4As shown, the target scalar instructions of the target basic block BB2 are: t1+=a11, t2+=a12, t3+=a13, wherein t1-t3 are one-dimensional arrays with continuous addresses, and a11-a13 are two-dimensional arrays with continuous addresses.

[0155] First, through N=3 and S=4, the target loop unrolling times X=4 of BB2 are determined, and then the target scalar instruction of BB2 is loop unrolled 4 times, and the unrolled destination operands are obtained as follows: t1, t2, t3, t1, t2, t3, t1, t2, t3, t1, t2, t3. When vectorization is performed, the 12 unrolled destination operands can be divided into 3 groups according to S=4, and 3 unrolled destination operand arrays are obtained: {t1, t2, t3, t1, t2}, {t3, t1, t2, t3}.

[0156] After expansion, the source operands are: a11, a12, a13, a21, a22, a23, a31, a32, a33, a41, a42, a43. The corresponding operands are accessed or read according to the addresses of the expanded operands.

[0157] For example, according to the number S=4 of the vectorizable scalar instructions, the addresses of the expanded source operands can be loaded into the vector registers: {a11, a12, a13, a14}, {a21, a22, a23, a24}, {a31, a32, a33, a34}, {a41, a42, a43, a44}, where it is assumed that a14 is the next address consecutive to a13, a24 is the next address consecutive to a23, a34 is the next address consecutive to a33, and a4 4 is the next address consecutive to a43; then, each source operand is read according to the loaded address, and then each source operand is spliced ​​to obtain each group of expanded source operand arrays containing S=4 expanded source operands of vectorizable scalar instructions: {a11, a12, a13, a21}, {a22, a23, a31, a32}, {a33, a41, a42, a43}, so that the source operands read in each group of expanded source operand arrays are consistent with the expanded source operands.

[0158] Finally, each group of expanded source operation arrays and each group of expanded destination operation arrays are combined to obtain the converted vector instructions.

[0159] like Figure 5 As shown, the converted vector instruction can be expressed as:

[0160] For the first group {t1, t2, t3, t1} and the first group of expanded source operation arrays {a11, a12, a13, a21}, a converted vector instruction is obtained, in which the addresses of a11, a12, and a13 are continuous, and the addresses of a13 and a21 are not continuous.

[0161] For the second group {t2, t3, t1, t2} and the second group of expanded source operation arrays {a22, a23, a31, a32}, a converted vector instruction is obtained, in which the addresses of a22 and a23 are continuous, the addresses of a31 and a32 are continuous, and the addresses of a23 and a31 are not continuous.

[0162] For the third group {t3, t1, t2, t3} and the third group of expanded source operation arrays {a33, a41, a42, a43}, a converted vector instruction is obtained, in which the addresses of a41, a42, and a43 are continuous, and the addresses of a33 and a41 are not continuous.

[0163] When the target basic block is a candidate basic block whose number of scalar instructions does not satisfy the number of vectorizable scalar instructions, the converted vector instructions are vector instructions whose addresses of destination operands are continuous but whose addresses of source operands are discontinuous.

[0164] The target basic block that does not meet the number of vectorizable scalar instructions is usually that the number of target scalar instructions N is less than the number of vectorizable scalar instructions S. Therefore, after the target loop is expanded by the number of times X, the destination operand and the source operand in the expanded target scalar instruction are usually concatenated, and since the source operand is a two-dimensional array, it can only be guaranteed that the addresses of each source operand are continuous when i is the same. Therefore, the converted vector instruction is a vector instruction with a continuous address of the destination operand but a discontinuous address of the source operand.

[0165] It can be seen that in the embodiment of the present invention, by determining the basic blocks that cannot be vectorized and the basic blocks that can be vectorized but cannot be fully vectorized as the target basic blocks, and then using the least common multiple calculated by the number of target scalar instructions of the target basic blocks and the number of scalar instructions that can be vectorized, the target loop expansion number for the target basic block loop expansion is obtained, so that the number of expanded target scalar instructions of the expanded basic blocks is an integer multiple of the number of vectorizable scalar instructions, so that the expanded target scalar instructions can be fully vectorized, thereby improving program performance, accelerating program running speed, and better exerting hardware computing power.

[0166] and Figure 1 Compared with the automatic vectorization process shown in FIG. Figure 1On the basis of the process shown, loop unrolling (S=2, N=3) is performed on the target basic blocks that cannot be fully vectorized among the basic blocks that successfully pass the data dependency analysis to obtain the expanded basic blocks that can be fully vectorized. In addition, loop unrolling is performed on the target basic blocks that have vectorization potential (basic blocks that successfully pass the data dependency analysis) but have a number less than the number of vectorizable scalar instructions (S=4, N=3) to obtain the expanded basic blocks that can be fully vectorized, thereby achieving full vectorization of the target basic blocks without affecting the automatic vectorization of the compiler.

[0167] Please refer to Figure 6 , Figure 6 It is a flowchart of automatic vectorization corresponding to the instruction conversion method provided by an embodiment of the present invention.

[0168] exist Figure 1 On the basis of the automatic vectorization process shown, step 31 is added: the target basic block is expanded for the target number of loops, and the expanded basic block is vectorized, that is, for the target basic block that successfully passes the data dependency analysis, no matter whether the target basic block succeeds or fails in the SLP feasibility analysis, the target basic block can be expanded for the target number of loops in the embodiment of the present invention, so that the target basic block that cannot be fully vectorized (including the target basic block with S=2, N=3 or the target basic block with S=4, N=3) can use the instruction conversion method provided by the embodiment of the present invention to achieve full vectorization of the target basic block.

[0169] It can be seen that the instruction conversion method of the embodiment of the present invention is compatible with the original automatic vectorization processing flow, and at the same time can perform loop expansion on the basic blocks that cannot be fully vectorized or cannot be vectorized in the original automatic vectorization process, so as to achieve full vectorization of the target scalar instruction.

[0170] The embodiment of the present invention further provides an instruction conversion device to implement the instruction conversion method described in the above embodiment.

[0171] Please refer to Figure 7 , Figure 7 It is a structural diagram of an instruction conversion device provided by an embodiment of the present invention.

[0172] like Figure 7 As shown, the device comprises:

[0173] A target basic block determination module 71 is used to determine a target basic block, wherein the target basic block is determined based on a source program and the target basic block includes a plurality of target scalar instructions;

[0174] A loop unrolling number determination module 72, configured to determine a target loop unrolling number corresponding to the target basic block at least according to the number of the plurality of target scalar instructions;

[0175] A loop unrolling module 73, configured to unroll the target basic block based on the target loop unrolling number to obtain an unrolled basic block, wherein the unrolled basic block includes a plurality of unrolled target scalar instructions, and the number of the plurality of unrolled target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor;

[0176] The vector instruction generation module 74 is used to perform vectorization processing on the expanded basic block to obtain a converted vector instruction.

[0177] Please continue to refer to Figure 7 , the device may further include:

[0178] The processor configuration parameter acquisition module 700 is used to acquire the configuration parameters of the processor;

[0179] A vectorizable scalar instruction quantity determination module 701, configured to determine the quantity of vectorizable scalar instructions supported by the processor according to the configuration parameters of the processor;

[0180] The loop unrolling number determining module 72 is used to determine a target loop unrolling number corresponding to the target basic block at least according to the number of the multiple target scalar instructions, including:

[0181] A target loop unrolling number corresponding to the target basic block is determined according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions.

[0182] Optionally, the loop unrolling number determining module 72 is used to determine a target loop unrolling number corresponding to the target basic block according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions, including:

[0183] determining a least common multiple of the number of the plurality of target scalar instructions and the number of the vectorizable scalar instructions;

[0184] The least common multiple is divided by the number of the multiple target scalar instructions, and a result obtained is determined as the target loop unrolling count corresponding to the target basic block.

[0185] Optionally, the target basic block determination module 71 is used to expand the target basic block based on the target loop expansion number to obtain an expanded basic block, including:

[0186] Based on the target loop unrolling number, loop unrolling is performed on the destination operands of each target scalar instruction to obtain unrolled destination operands, wherein the number of unrolled destination operands is an integer multiple of the number of the vectorizable scalar instructions;

[0187] And, based on the target loop unrolling number, loop unrolling is performed on the source operands of each target scalar instruction to obtain unrolled source operands, the number of the unrolled source operands being an integer multiple of the number of the vectorizable scalar instructions;

[0188] An expanded target scalar instruction is formed based on each expanded destination operand and each expanded source operand to obtain an expanded basic block.

[0189] Optionally, the target basic block determination module 71 is used to perform vectorization processing on the expanded basic block to obtain a converted vector instruction, including:

[0190] Grouping the expanded destination operands according to the number of the vectorizable scalar instructions to obtain an expanded destination operand array;

[0191] According to the number of the vectorizable scalar instructions, source operands are read according to the addresses of the expanded source operands, and the read source operands are processed so that the read source operands are consistent with the expanded source operands, thereby obtaining each group of expanded source operand arrays, wherein each group of expanded source operand arrays contains the number of the vectorizable scalar instructions read source operands;

[0192] Each group of expanded destination operation arrays is combined with each group of expanded source operation arrays to obtain converted vector instructions.

[0193] Optionally, the target basic block determination module 71 is used to determine the target basic block, including:

[0194] Obtaining a source program, wherein the source program includes a plurality of basic blocks, one basic block includes a plurality of scalar instructions, and the basic block is located in a loop of the source program;

[0195] Determine a candidate basic block from the multiple basic blocks, where the addresses of the multiple scalar instructions included in the candidate basic block are continuous and the operation types of the multiple scalar instructions are the same;

[0196] A target basic block is determined from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks.

[0197] Optionally, the target basic block determination module 71 is used to determine the target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks, including:

[0198] A candidate basic block whose number of scalar instructions satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions is determined as a target basic block.

[0199] Optionally, the target basic block determination module 71 is used to determine the target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks, including:

[0200] A candidate basic block whose number of scalar instructions does not satisfy the number of vectorizable scalar instructions is determined as a target basic block.

[0201] It can be seen that the instruction conversion device provided by the embodiment of the present invention, when implementing the automatic vectorization processing of scalar instructions, for the target basic block, at least according to the number of target scalar instructions included in the target basic block, calculates the target loop expansion number corresponding to the target basic block; thereby, using the target loop expansion number, loop expansion is performed on each target scalar instruction of the target basic block, so that the number of expanded target scalar instructions in the expanded basic block is: an integer multiple of the number of vectorizable scalar instructions supported by the processor. Therefore, each expanded target scalar instruction in the expanded basic block can be divided according to the number of vectorizable scalar instructions to be merged into multiple vector instructions to implement vectorization processing of the expanded target scalar instructions. It can be seen that the embodiment of the present invention can make the number of expanded target scalar instructions in the expanded basic block an integer multiple of the number of vectorizable scalar instructions, so that the target basic block can implement vectorization processing after expansion, reduce the restrictions on the vectorization of scalar instructions, and increase the number of conversions of scalar instructions into vector instructions.

[0202] An embodiment of the present invention provides an electronic device, such as a terminal device, a server device, or other computer device, including a memory and a processor, wherein the memory stores a program, and the processor calls the program stored in the memory to execute the instruction conversion method as described in any of the aforementioned embodiments.

[0203] An embodiment of the present invention provides a storage medium, wherein the storage medium stores a program, and when the program is executed, the instruction conversion method as described in any of the above embodiments is implemented.

[0204] An embodiment of the present invention provides a computer program product, including a computer program, wherein when the computer program is executed by a processor, the instruction conversion method as described in any one of the above embodiments is implemented.

[0205] The above describes multiple implementation schemes provided by the embodiments of the present invention. The various optional methods introduced in the implementation schemes can be combined and cross-referenced with each other without conflict, thereby extending a variety of possible implementation schemes, which can all be considered as implementation schemes disclosed and open in the embodiments of the present invention.

[0206] Although the embodiments of the present invention are disclosed above, the present invention is not limited thereto. Any person skilled in the art can make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the scope defined by the claims.

Claims

1. A method for converting instructions, characterized in that: include: Determine a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions; Determining a target number of loop unrollings corresponding to the target basic block based at least on the number of the plurality of target scalar instructions; Expanding the target basic block based on the target loop expansion number to obtain an expanded basic block, wherein the expanded basic block includes a plurality of expanded target scalar instructions, and the number of the plurality of expanded target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor; Vectorization is performed on the expanded basic block to obtain converted vector instructions.

2. The instruction conversion method according to claim 1, characterized in that: Also includes: Get the configuration parameters of the processor; Determining the number of vectorizable scalar instructions supported by the processor according to configuration parameters of the processor; The step of determining a target number of loop unrollings corresponding to the target basic block at least according to the number of the plurality of target scalar instructions comprises: A target loop unrolling number corresponding to the target basic block is determined according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions.

3. The instruction conversion method according to claim 2, characterized in that: The step of determining a target number of loop unrollings corresponding to the target basic block according to the number of the plurality of target scalar instructions and the number of the vectorizable scalar instructions includes: determining a least common multiple of the number of the plurality of target scalar instructions and the number of the vectorizable scalar instructions; The least common multiple is divided by the number of the multiple target scalar instructions, and a result obtained is determined as the target loop unrolling count corresponding to the target basic block.

4. The instruction conversion method according to claim 3, characterized in that: The step of expanding the target basic block based on the target loop expansion number to obtain an expanded basic block includes: Based on the target loop unrolling number, loop unrolling is performed on the destination operands of each target scalar instruction to obtain unrolled destination operands, wherein the number of unrolled destination operands is an integer multiple of the number of the vectorizable scalar instructions; And, based on the target loop unrolling number, loop unrolling is performed on the source operands of each target scalar instruction to obtain unrolled source operands, the number of the unrolled source operands being an integer multiple of the number of the vectorizable scalar instructions; An expanded target scalar instruction is formed based on each expanded destination operand and each expanded source operand to obtain an expanded basic block.

5. The instruction conversion method according to claim 4, characterized in that: The vectorizing the expanded basic block to obtain a converted vector instruction includes: Grouping the expanded destination operands according to the number of the vectorizable scalar instructions to obtain an expanded destination operand array; According to the number of the vectorizable scalar instructions, source operands are read according to the addresses of the expanded source operands, and the read source operands are processed so that the read source operands are consistent with the expanded source operands, thereby obtaining each group of expanded source operand arrays, wherein each group of expanded source operand arrays contains the number of the vectorizable scalar instructions read source operands; Each group of expanded destination operation arrays is combined with each group of expanded source operation arrays to obtain converted vector instructions.

6. The instruction conversion method according to any one of claims 1 to 5, characterized in that: The step of determining the target basic block comprises: Obtaining a source program, wherein the source program includes a plurality of basic blocks, one basic block includes a plurality of scalar instructions, and the basic block is located in a loop of the source program; Determine a candidate basic block from the multiple basic blocks, where the addresses of the multiple scalar instructions included in the candidate basic block are continuous and the operation types of the multiple scalar instructions are the same; A target basic block is determined from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks.

7. The instruction conversion method according to claim 6, characterized in that: The step of determining a target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks comprises: A candidate basic block whose number of scalar instructions satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions is determined as a target basic block.

8. The instruction conversion method according to claim 7, characterized in that: The converted vector instructions include: Vector instructions with consecutive destination operand addresses and consecutive source operand addresses; Also, vector instructions whose destination operand addresses are continuous but whose source operand addresses are not continuous.

9. The instruction conversion method according to claim 6, characterized in that: The step of determining a target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks comprises: A candidate basic block whose number of scalar instructions does not satisfy the number of vectorizable scalar instructions is determined as a target basic block.

10. The instruction conversion method according to claim 9, characterized in that: The converted vector instruction is a vector instruction with continuous addresses of destination operands but discontinuous addresses of source operands.

11. An instruction conversion device, characterized in that: include: A target basic block determination module, configured to determine a target basic block, wherein the target basic block is determined based on a source program, and the target basic block includes a plurality of target scalar instructions; A loop unrolling number determination module, configured to determine a target loop unrolling number corresponding to the target basic block according to at least the number of the plurality of target scalar instructions; A loop unrolling module, configured to unroll the target basic block based on the target loop unrolling number to obtain an unrolled basic block, wherein the unrolled basic block includes a plurality of unrolled target scalar instructions, and the number of the plurality of unrolled target scalar instructions is an integer multiple of the number of vectorizable scalar instructions supported by the processor; The vector instruction generation module is used to perform vectorization processing on the expanded basic block to obtain a converted vector instruction.

12. The instruction conversion device according to claim 11, characterized in that: Also includes: A processor configuration parameter acquisition module, used to obtain the configuration parameters of the processor; A vectorizable scalar instruction quantity determination module, used to determine the quantity of vectorizable scalar instructions supported by the processor according to the configuration parameters of the processor; The loop unrolling number determining module is used to determine a target loop unrolling number corresponding to the target basic block at least according to the number of the multiple target scalar instructions, including: A target loop unrolling number corresponding to the target basic block is determined according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions.

13. The instruction conversion device according to claim 12, characterized in that: The loop unrolling number determining module is used to determine a target loop unrolling number corresponding to the target basic block according to the number of the multiple target scalar instructions and the number of the vectorizable scalar instructions, including: determining a least common multiple of the number of the plurality of target scalar instructions and the number of the vectorizable scalar instructions; The least common multiple is divided by the number of the multiple target scalar instructions, and a result obtained is determined as the target loop unrolling count corresponding to the target basic block.

14. The instruction conversion device according to claim 13, characterized in that: The target basic block determination module is used to expand the target basic block based on the target loop expansion number to obtain an expanded basic block, including: Based on the target loop unrolling number, loop unrolling is performed on the destination operands of each target scalar instruction to obtain unrolled destination operands, wherein the number of unrolled destination operands is an integer multiple of the number of the vectorizable scalar instructions; And, based on the target loop unrolling number, loop unrolling is performed on the source operands of each target scalar instruction to obtain unrolled source operands, the number of the unrolled source operands being an integer multiple of the number of the vectorizable scalar instructions; An expanded target scalar instruction is formed based on each expanded destination operand and each expanded source operand to obtain an expanded basic block.

15. The instruction conversion device according to claim 14, characterized in that: The target basic block determination module is used to perform vectorization processing on the expanded basic block to obtain a converted vector instruction, including: Grouping the expanded destination operands according to the number of the vectorizable scalar instructions to obtain an expanded destination operand array; According to the number of the vectorizable scalar instructions, source operands are read according to the addresses of the expanded source operands, and the read source operands are processed so that the read source operands are consistent with the expanded source operands, thereby obtaining each group of expanded source operand arrays, wherein each group of expanded source operand arrays contains the number of the vectorizable scalar instructions read source operands; Each group of expanded destination operation arrays is combined with each group of expanded source operation arrays to obtain converted vector instructions.

16. The instruction conversion device according to any one of claims 11 to 15, characterized in that: The target basic block determination module is used to determine the target basic block, including: Obtaining a source program, wherein the source program includes a plurality of basic blocks, one basic block includes a plurality of scalar instructions, and the basic block is located in a loop of the source program; Determine a candidate basic block from the multiple basic blocks, where the addresses of the multiple scalar instructions included in the candidate basic block are continuous and the operation types of the multiple scalar instructions are the same; A target basic block is determined from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks.

17. The instruction conversion device according to claim 16, characterized in that: The target basic block determination module is used to determine the target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks, and includes: A candidate basic block whose number of scalar instructions satisfies the number of vectorizable scalar instructions and is not an integer multiple of the number of vectorizable scalar instructions is determined as a target basic block.

18. The instruction conversion device according to claim 16, characterized in that: The target basic block determination module is used to determine the target basic block from the candidate basic blocks based on the number of scalar instructions of the candidate basic blocks, and includes: A candidate basic block whose number of scalar instructions does not satisfy the number of vectorizable scalar instructions is determined as a target basic block.

19. An electronic device, characterized in that: The method comprises a memory and a processor, wherein the memory stores a program, and the processor calls the program stored in the memory to execute the instruction conversion method according to any one of claims 1 to 10.

20. A storage medium, characterized in that: The storage medium stores a program, and when the program is executed, the instruction conversion method according to any one of claims 1 to 10 is implemented.

21. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the instruction conversion method according to any one of claims 1 to 10 is implemented.

Citation Information

Cited By

  • Memory access instruction processing method and processor

    CN119621149A