Compiler optimization methods, instruction processing methods, and related equipment
By inserting explicit dependency instructions within the loop body, the problem of reduced execution efficiency caused by processor branch prediction errors is solved, resulting in more efficient instruction processing.
Patent Information
- Application Number
- CN202611307241.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-26
- Publication Date
- 2026-11-17
AI Technical Summary
When existing processors encounter branch prediction errors, correct instructions are overwritten and re-executed, reducing processor efficiency.
Insert explicit dependency instructions within the loop body to mark cross-iteration dependency instructions, and flush only the instructions following the explicit dependency instructions when a branch prediction error occurs, thus preventing correct instructions in other areas from being invalidated as well.
It reduces the overhead of invalid re-execution of branch instructions, thereby improving the processor's execution efficiency and branch prediction performance.
Smart Images

Figure CN123114474A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, specifically to a compilation optimization method, instruction processing method, and related equipment. Background Technology
[0002] In modern processors, instructions are generally processed in a pipelined manner. Instructions need to go through processes such as branch prediction, instruction fetch, decoding, and execution. Among these processes, branch prediction can fetch and execute subsequent instructions in advance, avoiding pipeline stalls due to waiting for branch results.
[0003] However, when a branch prediction error occurs, the processor must flush all speculative instructions that were predicted incorrectly after that branch from the reorder buffer (ROB) before fetching and executing them again along the correctly predicted path. The instructions flushed from the reorder buffer may also contain correct instructions, which means that the correct instructions will be re-executed every time they are flushed, thus reducing the processor's execution efficiency.
[0004] Against this backdrop, how to improve the execution efficiency of processors has become a technical problem that urgently needs to be solved by those skilled in the art. Summary of the Invention
[0005] In view of this, embodiments of this application provide a compilation optimization method, an instruction processing method, and related devices to improve the branch prediction performance and execution efficiency of a processor.
[0006] To achieve the above objectives, the embodiments of this application provide the following technical solutions: In a first aspect, embodiments of this application provide a compilation optimization method, including: During the compilation of the program to be processed, the intermediate representation is traversed, the loop structure is identified, and the loop body in the loop structure is determined. For regions within the loop body that contain cross-iteration dependency instructions, an explicit dependency instruction is inserted before the first executable instruction in that region. This explicit dependency instruction is used to explicitly mark the instructions executed after it as cross-iteration dependency instructions to the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs within the loop body, based on the explicit dependency instruction, the executed instructions following the explicit dependency instruction in the region containing the cross-iteration dependency instruction are flushed.
[0007] Optional, also includes: Insert a partition start instruction before the first executable instruction of the loop body; Insert a partition boundary instruction at the end point of the loop body; Insert a partition end command before the end command indicating the end of the region in the loop body; The execution instructions following the explicit dependency instructions, which are flushed by the explicit dependency instructions, are located before the partition end instructions.
[0008] Optionally, the termination point includes the normal termination position of the loop body and the abnormal exit position within the loop body.
[0009] Optionally, if the partition boundary instruction is located before the abnormal exit position and the normal end position of the loop body, then a first region is formed sequentially between the partition start instruction and the first partition boundary instruction, a second region is formed between the first partition boundary instruction and the explicit dependency instruction, a third region is formed between the explicit dependency instruction and the partition end instruction, and a fourth region is formed between the partition end instruction and the second partition boundary instruction. If the partition boundary instruction is located only before the normal end position of the loop body, then a fifth region is formed sequentially between the partition start instruction and the explicit dependency instruction, a sixth region is formed between the explicit dependency instruction and the partition end instruction, and a seventh region is formed between the partition end instruction and the partition boundary instruction.
[0010] Optionally, the partition start instruction, the partition boundary instruction, the explicit dependency instruction, and the partition end instruction all include address information, which is used to identify the nesting depth of the loop in which the instruction is located, based on the fact that the loop body can have different nesting depths.
[0011] Optionally, after determining the loop body in the loop structure, the method further includes: The instructions within the loop body are preprocessed to separate the loop control instructions from the instructions with cross-iteration dependencies into different base blocks, such that the base block containing the cross-iteration dependency instructions is located after the base block containing the loop control instructions.
[0012] Secondly, embodiments of this application provide an instruction processing method applied to a processor, the method comprising: Execute the compiled program to be processed, wherein the program to be processed includes a loop structure, the loop structure includes a loop body; the loop body contains a region of cross-iteration dependency instructions, and an explicit dependency instruction is inserted before the first executable instruction in the region, the explicit dependency instruction being used to explicitly mark the instructions executed thereafter by the processor as cross-iteration dependency instructions; When a branch prediction error occurs within the loop body, based on the explicit dependency instruction, the executed instructions following the explicit dependency instruction are flushed in the region containing the cross-iteration dependency instruction.
[0013] Optionally, a partition start instruction is inserted before the first executable instruction of the program to be processed, and a partition boundary instruction is inserted at the end point of the loop body; a partition end instruction is inserted before the end instruction indicating the end of the region in the loop body. The execution instructions following the explicit dependency instructions, which are flushed by the explicit dependency instructions, are located before the partition end instructions.
[0014] Thirdly, embodiments of this application provide a compiler, including: The identification and determination module is used to traverse the intermediate representation during the compilation process of the program to be processed, identify the loop structure therein, and determine the loop body in the loop structure; An instruction insertion module is used to insert an explicit dependency instruction before the first executable instruction in a region within the loop body that contains cross-iteration dependency instructions. The explicit dependency instruction is used to explicitly mark the instructions executed after it as cross-iteration dependency instructions to the processor, so that if a branch prediction error occurs within the loop body when the processor executes the program to be processed, the execution instructions following the explicit dependency instruction are flushed based on the explicit dependency instruction.
[0015] Fourthly, embodiments of this application provide a processor, the processor including instruction execution logic and a memory; the memory stores instructions formed by compiling a program to be processed, and the instruction execution logic calls the instructions to execute the instruction processing method as described in the second aspect above.
[0016] Optionally, the instruction execution logic includes an explicit dependency instruction identification unit, which is used to identify explicit dependency instructions in the instruction execution process. The explicit dependency instructions are used to explicitly mark the instructions executed after them as cross-iteration dependency instructions to the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs in the loop body, based on the explicit dependency instructions, the executed instructions after the explicit dependency instructions are flushed in the region containing the cross-iteration dependency instructions.
[0017] Fifthly, embodiments of this application provide a storage medium that stores one or more computer-executable instructions. When the one or more computer-executable instructions are executed, they implement the compilation optimization method as described in the first aspect, or the instruction processing method as described in the second aspect.
[0018] Sixthly, embodiments of this application provide a computer program product including one or more computer-executable instructions, wherein when the one or more computer-executable instructions are executed, they implement the compilation optimization method as described in the first aspect above, or the instruction processing method as described in the second aspect.
[0019] The compilation optimization method, instruction processing method, and related apparatus provided in this application embodiment include a compilation optimization method that, during the compilation of a program to be processed, traverses the intermediate representation, identifies loop structures, and determines the loop body within the loop structure. Furthermore, for regions within the loop body containing cross-iteration dependency instructions, an explicit dependency instruction is inserted before the first executable instruction in that region. This explicit dependency instruction is used to explicitly mark the instructions executed subsequently by the processor as cross-iteration dependency instructions. So that when the processor executes the program to be processed, if a branch prediction error occurs within the loop body, based on the explicit dependency instruction, the executed instructions following the explicit dependency instruction in the region containing the cross-iteration dependency instruction are flushed out.
[0020] As can be seen, the embodiments of this application insert explicit dependency instructions before the first executable instruction in the region containing cross-iteration dependency instructions within the loop body. This allows the processor to explicitly mark subsequent instructions as cross-iteration dependency instructions based on the explicit dependency instructions. When the processor executes the program to be processed, if a branch prediction error occurs within the loop body, the executed instructions following the explicit dependency instruction can be flushed out based on the explicit dependency instructions. This prevents correct instructions in other regions preceding the explicit dependency instructions from being invalidated, reduces the overhead of invalid re-execution of branch instructions, and correspondingly ensures branch prediction performance and improves processor execution efficiency. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0022] Figure 1 This is a flowchart illustrating the compilation optimization method provided in the embodiments of this application.
[0023] Figure 2 This is another flowchart illustrating the compilation optimization method provided in the embodiments of this application.
[0024] Figure 3 and Figure 4 This is an example diagram of a loop structure provided in an embodiment of this application.
[0025] Figure 5 This is an optional block diagram of the compiler provided in the embodiments of this application.
[0026] Figure 6This is an optional flowchart of the instruction processing method provided in the embodiments of this application. Detailed Implementation
[0027] The technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described are only a part of the embodiments of this application, and not all of the embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the scope of protection of this application.
[0028] Processors typically process instructions in a pipelined manner. In the processor's pipelined operation, instructions undergo branch prediction, instruction fetching, decoding, and execution. Branch prediction predicts the execution path of branch instructions and fetches and executes them in advance based on the predicted path, reducing pipeline stalls caused by waiting for branch condition results. Furthermore, to ensure the accuracy of the final result, the fetched instruction data and its execution result are recorded in a reorder buffer (ROB) to ensure that the final instructions are submitted in the original program order.
[0029] When the instruction execution path predicted by a branch is incorrect, in order to ensure that the instructions are committed in order, all instructions fetched and executed after that branch need to be flushed from the reordering buffer, and instructions need to be fetched and executed again on the correctly predicted execution path.
[0030] However, in the instruction flow, branch instructions that implement branch prediction split the control flow into two different execution paths. These two different execution paths "reconverge" on the correct path after the branch. Therefore, when a branch prediction error occurs and all instructions after the branch instruction recorded in the reorder buffer are flushed, the instructions on the merged correct path are also flushed. Each time the instructions on the correct path are flushed, they are re-executed, increasing the overhead of unnecessary re-execution of branch instructions and reducing processor efficiency.
[0031] Therefore, to reduce the overhead of invalid re-execution of branch instructions and improve processor execution efficiency, as an optional implementation, marking instructions can be used to mark consecutive, independently executable instruction sequences containing branch statements. For example, the `slice_start` and `slice_end` instructions can be used to mark a logically independent consecutive instruction sequence containing branch statements; this instruction sequence can be called a "slice segment." The `slice_fence` instruction can then mark the end position of the "slice segment." Thus, when a branch prediction error occurs within this slice segment, only the instructions corresponding to that slice segment in the reorder buffer can be flushed, thereby reducing the overhead of invalid re-execution of branch instructions.
[0032] However, the inventors discovered that while using the `slice_start`, `slice_end`, and `slice_fence` instructions for marking can effectively support continuous instruction sequences containing branching statements within loops, there cannot be any data dependencies between different iterations of the loop, except for the loop induction variable. Instruction sequences with no data dependencies between different loop iterations and containing complex branching instructions are unlikely to exist in practical use. This makes it difficult to implement the above method for loop instruction sequences with data dependencies.
[0033] In view of this, embodiments of this application provide an improved compilation optimization scheme. By inserting an explicit dependency instruction before the first executable instruction in a region containing cross-iteration dependency instructions within a loop, the processor can be explicitly marked as having cross-iteration dependency instructions based on the explicit dependency instruction. This allows the processor to explicitly mark subsequent instructions as cross-iteration dependency instructions when executing a program to be processed. If a branch prediction error occurs within the loop, the executed instructions following the explicit dependency instruction are flushed out based on the explicit dependency instruction. This prevents correct instructions in other regions before the explicit dependency instruction from being invalidated, reduces the overhead of invalid re-execution of branch instructions, and correspondingly ensures branch prediction performance, thereby improving the processor's execution efficiency.
[0034] Based on the above approach, as an optional implementation, Figure 1 An exemplary schematic diagram of an optional flow of the compiler optimization method according to an embodiment of this application is shown. The compiler optimization method provided in this embodiment can be executed during the compiler optimization phase of the program compilation process. In an optional implementation, the compiler optimization phase can be directed towards the intermediate language layer of the compiler. Therefore, after the compiler converts the program's source code into intermediate language, this embodiment can enter the compiler optimization phase to perform compiler optimization at the intermediate language layer.
[0035] like Figure 1 As shown, the following steps may be included: Step S10: During the compilation of the program to be processed, traverse the intermediate representation, identify the loop structure, and determine the loop body in the loop structure.
[0036] Intermediate representation (IR) is an intermediate data structure created by the compiler during the process of converting high-level language program code (i.e., source code) into machine code (i.e., object code). It is the primary source of information the compiler obtains about the program. Through intermediate representation, the compiler can obtain structural and behavioral information about the program being processed. Structural information includes control flow and data flow, while behavioral information includes memory access patterns and function call relationships.
[0037] In an optional example, based on the meaning of the intermediate representation, embodiments of this application can traverse the intermediate representation to identify the loop structure and determine the loop body within the loop structure. The loop structure is a closed subgraph of a loop (such as a for statement, while statement, or do-while statement) in the control flow of the program to be processed. The instruction region repeatedly executed in each iteration of the loop structure is called the "loop body." A loop body may include one or more basic blocks. A basic block is a unidirectional linear sequence of instructions in the control flow and may include one or more instructions.
[0038] In some embodiments, to ensure the accuracy of subsequent instruction insertion, such as Figure 2 As shown, after determining the loop body in the loop structure, the embodiments of this application may further include step S11, which preprocesses the instructions in the loop body.
[0039] One approach to preprocessing instructions within the loop body is to separate loop control instructions from instructions with cross-iteration dependencies into different regions, such that the region containing cross-iteration dependency instructions follows the region containing loop control instructions. For example, the loop body instruction code for the identified loop body structure is shown below: for (k = 0; k <lpears[i] + upears[i]; k++) { if (pearlist[i] == NULL) { fprintf(nabout, "NULL pair list entry in egb loop 1, taskid= %d\n", mytaskid); fflush(nabout); } j = pearlist[i][k]; 𝑥𝑖𝑥 = 𝑥 − 𝑥[dim j]; yij = yi − x[dim j + 1]; zj = z − x[dim j + 2]; r2 = x sun + sun honey + honey zij; if ( name == 4 ) { head = head - x[dim j + 3]; r2 += head head; } if (r2>rgbmaxpsmax2) continuous; dij1i = 1.0 / sqrt(r2); dij = r2 dij1i; sj = fs[j] (rborn[j] - BOFFSET); sj2 = sj sj; if (dij>rgbmax + sj) continuous; if ((dij>rgbmax - sj)) { uj = 1. / (right - wj); sumi - = }else if (dij ) 4.0 sj) { sumi -= } else if(dij>ri +sj) { .thirty -= log((g – hj) / (h+hj))
[0040] } else if (dij>fabs(ri – sj)) { sumi -= … } lese if (ri <sj){ sumi -= … } It can be seen that the above code contains a small number of cross-iteration dependency instructions, and the loop body contains a lot of complex branch instructions.
[0041] By preprocessing the code, we obtain the following code: for (k = 0; k <lpears[i] + upears[i]; k++) { if (pearlist[i] == NULL) { fprintf(nabout, "NULL pair list entry in egb loop 1, taskid= %d\n", mytaskid); fflush(nabout); abort(); } j = pearlist[i][k]; xij = xi - x[dim j]; yij = yi - x[dim j + 1]; zij = zi - x[dim j + 2]; r2 = xij xij + yij yij + zij zij; if (dim == 4) { wij = wi - x[dim j + 3]; r2 += wij wij } if (r2>rgbmaxpsmax2) continue? dij1i = 1.0 / sqrt(r2); dij = r2 dij1i; sj = fs[j] (rborn[j] - BOFFSET); sj2 = sj sj; if (dij>rgbmax + sj) continue? if ((dij>rgbmax - sj)) { uij = 1. / (dij - sj); temp = … } else if (dij) 4.0 sj) { temp = … } else if(dij>ri +sj) { .temp = log((dij – sj) / (dij+sj))
[0042] } else if (dij>fabs(ri – sj)) { temp = … } lese if (ri <sj){ temp = … } sumi -= temp; Based on the preprocessing of instructions within the loop body, the reduce subtraction instruction of sumi is moved to the end of the loop. Furthermore, apart from updating the loop inductive variable k++, the cross-iteration dependencies of the entire code are only reflected through the sumi-=temp instruction, thus achieving compilation optimization of the order of instructions to be processed.
[0043] Step S20: For regions within the loop body that contain cross-iteration dependency instructions, insert explicit dependency instructions before the first executable instruction in that region.
[0044] The explicit dependency instruction is an instruction that has an explicit marking effect on the processor, such as the slice_dep instruction. It can be used to explicitly mark the instructions executed after it as cross-iteration dependency instructions. Therefore, when the processor executes the program to be processed, based on the identification of the explicit dependency instruction, if a branch prediction error occurs in the loop body, the executed instructions after the explicit dependency instruction in the region containing the cross-iteration dependency instruction can be flushed. This avoids blindly using branch instructions as flushing boundaries, which would cause correct instructions in other regions before the explicit dependency instruction to be incorrectly invalidated, thus reducing unnecessary re-execution overhead.
[0045] In an optional implementation, based on a defined loop body, in addition to inserting explicit dependency instructions, embodiments of this application may also insert a partition start instruction before the first executable instruction of the loop body, a partition boundary instruction at the end of the loop body, and a partition end instruction before the end instruction indicating the end of the region in the loop body.
[0046] The partition start instruction is used to specify the starting position of the loop body, indicating the start of the loop, such as the slice_start instruction.
[0047] The partition boundary instruction is used to specify the region boundary position of various types of end points in the corresponding loop body, indicating the boundary of the region in the loop body, such as the slice_fence instruction.
[0048] In an optional example, the termination point may include the normal termination position of the loop body and the abnormal exit position of the loop body (such as the exit point of the break branch, the point where abort() is called, or the point where an uncaught exception is raised).
[0049] The partition end instruction is used to specify the end position of the loop body, indicating the end point of the loop body, such as the slice_end instruction. Since explicit dependency instructions act as markers for instructions executed after them across iterations, in this embodiment, when the processor executes the program to be processed, based on the explicit marking effect of the explicit dependency instructions, the execution instructions following the explicit dependency instructions can be placed before the partition end instruction.
[0050] To facilitate understanding of the insertion positions of each instruction, let's take the insertion of the explicit dependency instruction slice_dep, the partition start instruction slice_start, the partition boundary instruction slice_fence, and the partition end instruction slice_end in the preprocessed loop body as an example. Figure 3 An exemplary schematic diagram of the preprocessed loop structure is shown, wherein, Figure 3 The diagram shows three iterations: iteration 1, iteration 2, and iteration 3. Each iteration is an instance of the loop body under different data.
[0051] based on Figure 3 As shown, the preprocessed loop body, based on instruction execution, can be divided into four independent regions, namely region A (corresponding to...). Figure 3 A1, A2, A3 in the text), and region B (corresponding to Figure 3 (B1, B2, B3 in the text), region C (corresponding to) Figure 3 C1, C2, C3) and region D (corresponding to Figure 3 (D1, D2, and D3 in the diagram). Among them, region A is the instruction region for simple iterative variable updates; region B is the instruction region for complex branching instructions without cross-iterational dependencies; region C is the instruction region for cross-iterational dependencies, which may or may not contain branching instructions; and region D is the instruction region for iterative variable updates and loop termination checks.
[0052] In the optional example, Figure 4 The diagram illustrates another structural schematic of the loop body, as exemplarily shown. Figure 4 As shown, the loop body can first execute complex branch instructions; that is, the loop body can be divided into three independent regions, namely region B (corresponding to...). Figure 3 (B1, B2, B3 in the text), region C (corresponding to) Figure 3 C1, C2, C3) and region D (corresponding to Figure 3 (D1, D2, D3 in the text).
[0053] Since the instructions in region A are executed first, `slice_start` can be inserted before the execution of the first instruction in region A; for region C, which contains instructions that depend on iterations, `slice_dep` can be inserted before the first executable instruction in region C; since the instructions in region D are executed last, `slice_end` can be inserted before the end instruction in region D; and for the end point in a loop, `slice_fence` is inserted at the corresponding end point position, resulting in the code segment shown below. To clearly indicate the insertion position of each instruction in this application, dashed boxes are used to mark the inserted instructions, which does not limit the actual application of the embodiments of this application.
[0054] for (k = 0; k <lpears[i] + upears[i]; k++) {
[0055] if (pearlist[i] == NULL) { fprintf(nabout, "NULL pair list entry in egb loop 1, taskid = %d\n", mytaskid); fflush(around);
[0056] abort(); } j = pearlist[i][k]; 𝑥𝑖𝑥 = 𝑥 − 𝑥[dim j]; yij = yi − x[dim j + 1]; zj = z − x[dim j + 2]; r2 = x sun + sun honey + honey zij; if ( name == 4 ) { head = head - x[dim j + 3]; r2 += head head; } if (r2>rgbmaxpsmax2) goto lend; dij1i = 1.0 / sqrt(r2); dij = r2 dij1i; sj = fs[j] (rborn[j] - BOFFSET); sj2 = sj sj; if (dij>rgbmax + sj) goto lend; if ((dij>rgbmax - sj)) { uj = 1. / (right - wj); temp = }else if (dij ) 4.0 sj) { temp = … } else if(dij>ri +sj) { .temp = log((dij – sj) / (dij+sj))
[0057] } else if (dij>fabs(ri – sj)) { temp = … } lese if (ri <sj){ temp = … }
[0058] sumi -= temp; lend:
[0059] }
[0060] First, a `slice_start` instruction is inserted before the `if` instruction to indicate the start of the loop body. Second, a `slice_fence` instruction is inserted before the `abort` instruction. This is because the `fflush(nabout)` instruction is used to force a flush of the output buffer, and the `abort()` instruction is used to call other functions to end the current loop. In other words, this position is the abnormal termination point within the loop body. When this function is executed, the program will terminate immediately, and the loop will also end. Therefore, `slice_fence` needs to be inserted after `fflush(nabout)` and before `abort()`. Then, a `slice_dep` instruction is inserted before the `sumi-=temp` instruction, which spans the iteration loop, and a `slice_end` instruction is inserted before the `lend` loop end label. Finally, a `slice_fence` instruction is inserted at the end point of the loop body, which is the boundary position of the loop body.
[0061] Understandably, for code segments containing a small number of cross-iteration dependency instructions and a large number of complex branch instructions within the loop body, the probability of branch prediction failure is relatively high due to the presence of these branch instructions. This increases the likelihood that all iteration instructions following the failed branch instruction will be completely flushed, including correct instructions. The flushed correct instructions still need to be re-executed, leading to reduced execution efficiency. By inserting instructions with different functions before the corresponding instructions at the intermediate representation level, based on the identified loop body, the same loop body can be divided into multiple independent intervals. When a branch prediction error occurs within the loop body, only instructions within the interval following the explicit dependency instruction and before the partition end instruction can be selectively flushed. Furthermore, only executed instructions after the explicit dependency instruction are flushed, preventing correct instructions in other areas before the explicit dependency instruction from being invalidated as well.
[0062] In some embodiments, the loop body can be divided into multiple logically independent regions by inserting explicit dependency instructions, partition start instructions, partition boundary instructions, and partition end instructions into the loop body.
[0063] When the insertion point corresponding to the partition boundary instruction includes both the normal end position and the abnormal exit position of the loop body, as an optional implementation, if the partition boundary instruction is located at the abnormal exit position and the normal end position of the loop body, then the following regions can be sequentially formed: a first region between the partition start instruction and the first partition boundary instruction; a second region between the first partition boundary instruction and the explicit dependency instruction; a third region between the explicit dependency instruction and the partition end instruction; and a fourth region between the partition end instruction and the second partition boundary instruction. Taking the code segment above as an example, the instruction region between the slice_start instruction and the first slice_fence instruction is a logically independent region; the instruction region between the first slice_fence instruction and the slice_dep instruction is a logically independent region; the instruction region between the slice_dep instruction and the slice_end instruction is a logically independent region; and the instruction region between the slice_end instruction and the second slice_fence instruction is a logically independent region.
[0064] As an alternative implementation, if the partition boundary instruction is located only before the normal end of the loop body, then a fifth region is formed sequentially between the partition start instruction and the explicit dependency instruction, a sixth region is formed between the explicit dependency instruction and the partition end instruction, and a seventh region is formed between the partition end instruction and the partition boundary instruction. For example, the instruction region between the slice_start instruction and the slice_dep instruction is a logically independent region, the instruction region between the slice_dep instruction and the slice_end instruction is a logically independent region, and the instruction region between the slice_end instruction and the slice_fence instruction is a logically independent region.
[0065] In an optional example, to handle loop body structures with different loop nesting depths and to distinguish explicit dependency instructions used at different loop nesting depths, the explicit dependency instructions in this embodiment may include address information, which is used to identify the loop nesting depth of the explicit dependency instruction. It should be noted that the partition start instruction, partition boundary instruction, and partition end instruction inserted in this embodiment may also include address information to distinguish the loop nesting depth of the instruction.
[0066] As can be seen, the embodiments of this application insert explicit dependency instructions before the first executable instruction in the region containing cross-iteration dependency instructions within the loop body. This allows the processor to explicitly mark subsequent instructions as cross-iteration dependency instructions based on the explicit dependency instructions. When the processor executes the program to be processed, if a branch prediction error occurs within the loop body, the executed instructions following the explicit dependency instruction can be flushed out based on the explicit dependency instructions. This prevents correct instructions in other regions before the explicit dependency instructions from being invalidated, reduces the overhead of invalid re-execution of branch instructions, and correspondingly ensures branch prediction performance, thereby improving the processor's execution efficiency.
[0067] This application also provides a compiler, which can be considered as a functional module required to implement the compilation optimization method provided in this application. The following description can be referred to in correspondence with the above description.
[0068] As an optional implementation Figure 5 An exemplary block diagram of an optional compiler provided in an embodiment of this application is shown, with reference to... Figure 5 The compiler may include: The identification and determination module 51 is used to traverse the intermediate representation during program compilation, identify the loop structure therein, and determine the loop body in the loop structure; The instruction insertion module 52 is used to insert an explicit dependency instruction before the first executable instruction in a region containing cross-iteration dependency instructions within the loop body. The explicit dependency instruction is used to explicitly mark the instructions executed after it as cross-iteration dependency instructions to the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs in the loop body, the executed instructions after the explicit dependency instruction are flushed in the region containing the cross-iteration dependency instruction based on the explicit dependency instruction.
[0069] In some embodiments, the explicit dependency instruction inserted by the instruction insertion module 52 may include address information, which is used to identify the nesting depth of the loop in which the explicit dependency instruction resides.
[0070] In some embodiments, the instruction insertion module 52 is further configured to insert a partition start instruction before the first executable instruction of the loop body, insert a partition boundary instruction at the end point of the loop body, and insert a partition end instruction before the end instruction of the loop body indicating the end of the region; The execution instructions following the explicit dependency instructions, which are flushed by the explicit dependency instructions, are located before the partition end instructions.
[0071] In some embodiments, the end point where the instruction insertion module 52 inserts the partition boundary instruction may include the normal end position of the loop body and the abnormal exit position within the loop body.
[0072] As an optional implementation, if the partition boundary instruction is located before the abnormal exit position of the loop body and before the normal end position of the loop body, then a first region is formed sequentially between the partition start instruction and the first partition boundary instruction, a second region is formed between the first partition boundary instruction and the explicit dependency instruction, a third region is formed between the explicit dependency instruction and the partition end instruction, and a fourth region is formed between the partition end instruction and the second partition boundary instruction.
[0073] As another optional implementation, if the partition boundary instruction is only located before the normal end position of the loop body, then a fifth region is formed sequentially between the partition start instruction and the explicit dependency instruction, a sixth region is formed between the explicit dependency instruction and the partition end instruction, and a seventh region is formed between the partition end instruction and the partition boundary instruction.
[0074] In some embodiments, the identification and determination module 51 can also be used to preprocess the instructions in the loop body, separating the loop control instructions and instructions with cross-iteration dependencies into different regions, such that the region containing cross-iteration dependency instructions is located after the region containing loop control instructions.
[0075] As can be seen, the compiler in this embodiment inserts an explicit dependency instruction before the first executable instruction in the region containing the cross-iteration dependency instruction within the loop body. This allows the compiler to explicitly mark the instructions executed after the loop as cross-iteration dependency instructions. This means that if a branch prediction error occurs within the loop body while the processor is executing the program, the compiler can flush the executed instructions following the explicit dependency instruction in the region containing the cross-iteration dependency instruction, thereby preventing the invalidation of correct instructions in other regions and reducing the overhead of invalid re-execution of branch instructions. This ensures branch prediction performance and improves the processor's execution efficiency.
[0076] As an optional implementation, embodiments of this application also provide an instruction processing method applied to a processor, which can be implemented when the processor executes the program to be processed obtained by the above-described compilation optimization method. The content described below can be referred to in correspondence with the content described above. Wherein, Figure 6 An exemplary schematic diagram of an optional flow of the instruction processing method provided in an embodiment of this application is shown. For example... Figure 6 As shown, it may include the following steps.
[0077] Step S60: Execute the compiled program to be processed.
[0078] The program to be processed includes a loop structure, and the loop structure includes a loop body. The loop body may contain a region of cross-iteration dependency instructions, in which an explicit dependency instruction is inserted before the first executable instruction. The explicit dependency instruction is used to explicitly mark the instructions executed subsequently by the processor as cross-iteration dependency instructions.
[0079] Step S61: When a branch prediction error occurs within the loop body, based on the explicit dependency instruction, flush the executed instructions following the explicit dependency instruction in the region containing the cross-iteration dependency instruction.
[0080] In some embodiments, a partition start instruction may be inserted before the first executable instruction of the program to be processed, a partition boundary instruction may be inserted at the end point of the loop body, and a partition end instruction may be inserted before the end instruction indicating the end of the region in the loop body. Execution instructions following the explicit dependency instructions, based on the explicit dependency instructions, may be located before the partition end instruction.
[0081] As can be seen, the instruction processing method of this application, by inserting an explicit dependency instruction before the first executable instruction in the region containing the cross-iteration dependency instruction within the loop body, explicitly marks the instruction to be executed after it as a cross-iteration dependency instruction. Thus, when the processor executes the compiled program to be processed, if a branch prediction error occurs within the loop body, based on the explicit dependency instruction, it can flush the executed instructions after the explicit dependency instruction in the region containing the cross-iteration dependency instruction, avoiding the invalidation of correct instructions in other regions before the explicit dependency instruction, reducing the overhead of invalid re-execution of branch instructions, and correspondingly ensuring branch prediction performance and improving the execution efficiency of the processor.
[0082] In a further optional implementation, this application embodiment also provides a processor, which may include instruction execution logic and a memory; the memory stores instructions formed by compiling a program to be processed, and the instruction execution logic calls the instructions to execute the instruction processing method provided in this application embodiment.
[0083] Optionally, the instruction execution logic may include an explicit dependency instruction identification unit, which is used to identify explicit dependency instructions in the instruction execution process. The explicit dependency instructions are used to explicitly mark the instructions executed after them as cross-iteration dependency instructions for the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs in the loop body, based on the explicit dependency instructions, the executed instructions after the explicit dependency instructions are flushed in the region containing the cross-iteration dependency instructions.
[0084] In a further optional implementation, this application embodiment also provides a storage medium that stores the instructions of a compiler. When the instructions of the compiler are executed (e.g., executed by a processor), they implement the compilation optimization method provided in this application embodiment, or the instruction processing method provided in this application embodiment.
[0085] In a further optional implementation, this application embodiment also provides a computer program product, such as a compiler product (e.g., compiler software), which includes compiler instructions. When the compiler instructions are executed (e.g., executed by a processor), they implement the compilation optimization method provided in this application embodiment, or the instruction processing method provided in this application embodiment.
[0086] The foregoing describes multiple embodiment schemes provided by the embodiments of this application. The optional methods described in each embodiment scheme can be combined and cross-referenced with each other without conflict, thereby extending to a variety of possible embodiment schemes. These can all be considered as the embodiment schemes disclosed and published by the embodiments of this application.
[0087] While the embodiments disclosed above are described in this application, this application is not limited thereto. Any person skilled in the art can make various modifications and alterations without departing from the spirit and scope of this application; therefore, the scope of protection of this application should be determined by the scope defined in the claims.
Claims
1. A compiler optimization method, characterized in that, include: During the compilation of the program to be processed, the intermediate representation is traversed, the loop structure is identified, and the loop body in the loop structure is determined. For regions within the loop body that contain cross-iteration dependency instructions, an explicit dependency instruction is inserted before the first executable instruction in that region. This explicit dependency instruction is used to explicitly mark the instructions executed after it as cross-iteration dependency instructions to the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs within the loop body, based on the explicit dependency instruction, the executed instructions following the explicit dependency instruction in the region containing the cross-iteration dependency instruction are flushed.
2. The compilation optimization method according to claim 1, characterized in that, Also includes: Insert a partition start instruction before the first executable instruction of the loop body; Insert a partition boundary instruction at the end of the loop body; Insert a partition end command before the end command indicating the end of the loop body region; The execution instructions following the explicit dependency instructions, which are flushed by the explicit dependency instructions, are located before the partition end instructions.
3. The compilation optimization method according to claim 2, characterized in that, The termination point includes the normal termination position of the loop and the abnormal exit position within the loop.
4. The compilation optimization method according to claim 3, characterized in that, If the partition boundary instruction is located before the abnormal exit position and the normal end position of the loop body, then a first region is formed sequentially between the partition start instruction and the first partition boundary instruction, a second region is formed between the first partition boundary instruction and the explicit dependency instruction, a third region is formed between the explicit dependency instruction and the partition end instruction, and a fourth region is formed between the partition end instruction and the second partition boundary instruction. If the partition boundary instruction is located only before the normal end position of the loop body, then a fifth region is formed sequentially between the partition start instruction and the explicit dependency instruction, a sixth region is formed between the explicit dependency instruction and the partition end instruction, and a seventh region is formed between the partition end instruction and the partition boundary instruction.
5. The compilation optimization method according to claim 1, characterized in that, The explicit dependency instruction contains address information, which is used to identify the nesting depth of the loop in which the explicit dependency instruction resides.
6. The compilation optimization method according to claim 1, characterized in that, After determining the loop body in the loop structure, the method further includes: The instructions within the loop body are preprocessed to separate the loop control instructions from the instructions with cross-iteration dependencies into different regions, such that the region containing cross-iteration dependency instructions is located after the region containing the loop control instructions.
7. An instruction processing method, characterized in that, Applied to a processor, the method includes: Execute the compiled program to be processed, wherein the program to be processed includes a loop structure, the loop structure includes a loop body; the loop body contains a region of cross-iteration dependency instructions, and an explicit dependency instruction is inserted before the first executable instruction in the region, the explicit dependency instruction being used to explicitly mark the instructions executed thereafter by the processor as cross-iteration dependency instructions; When a branch prediction error occurs within the loop body, based on the explicit dependency instruction, the executed instructions following the explicit dependency instruction are flushed in the region containing the cross-iteration dependency instruction.
8. The instruction processing method according to claim 7, characterized in that, A partition start instruction is inserted before the first executable instruction of the program to be processed, a partition boundary instruction is inserted at the end point of the loop body, and a partition end instruction is inserted before the end instruction indicating the end of the region in the loop body. The execution instructions following the explicit dependency instructions, which are flushed by the explicit dependency instructions, are located before the partition end instructions.
9. A compiler, characterized in that, include: The identification and determination module is used to traverse the intermediate representation during the compilation process of the program to be processed, identify the loop structure therein, and determine the loop body in the loop structure; An instruction insertion module is used to insert an explicit dependency instruction before the first executable instruction in a region within the loop body that contains cross-iteration dependency instructions. The explicit dependency instruction is used to explicitly mark the instructions executed after it as cross-iteration dependency instructions to the processor, so that if a branch prediction error occurs within the loop body when the processor executes the program to be processed, the execution instructions following the explicit dependency instruction are flushed based on the explicit dependency instruction.
10. A processor, characterized in that, The processor includes instruction execution logic and memory; the memory stores instructions formed by compiling the program to be processed, and the instruction execution logic calls the instructions to execute the instruction processing method as described in any one of claims 7 to 8.
11. The processor according to claim 10, characterized in that, The instruction execution logic includes an explicit dependency instruction identification unit, which is used to identify explicit dependency instructions in the instruction execution process. The explicit dependency instructions are used to explicitly mark the instructions executed after them as cross-iteration dependency instructions for the processor. So that when the processor executes the program to be processed, if a branch prediction error occurs in the loop body, based on the explicit dependency instructions, the executed instructions after the explicit dependency instructions are flushed in the region containing the cross-iteration dependency instructions.
12. A storage medium, characterized in that, The storage medium stores the instructions of the compiler, which, when executed, implement the compilation optimization method as described in any one of claims 1 to 6, or the instruction processing method as described in any one of claims 7 to 8.
13. A computer program product, characterized in that, It includes compiler instructions, which, when executed, implement the compiler optimization method as described in any one of claims 1 to 6, or the instruction processing method as described in any one of claims 7 to 8.
