Mechanism for software pipelining loop nests6820250Abstract A method is provided for processing nested loops that include a modulo-scheduled inner loop within an outer loop. The nested loop is scheduled to execute the epilog stage of the inner loop for a given iteration of the outer loop with the prolog stage of the inner loop for the next iteration of the outer loop. For one embodiment of the invention, this is accomplished by initializing an epilog counter for the inner loop to a value that bypasses draining the software pipeline. This causes the processor to exit the inner loop before it begins draining the inner loop pipeline. The inner loop pipeline is drained during the next iteration of the outer loop, while the inner loop pipeline fills for the next iteration of the outer loop. Claims What is claimed is: Description BACKGROUND OF THE INVENTION
Real*4 A(10,100), B(10,100), C(10,100)
DO J=1, 100
DO I = 1,3
A(I,J) = B(I,J)/C(I,J)
ENDDO
ENDO
Here, each element of the two dimensional array A, e.g. A(i, j), is provided by dividing the (i, j).sup.th element of array B by the (i, j).sup.th element of array C. While nested loop (I) is a perfectly nested loop, i.e. the body of the outer counted loop (DO J=1,100) consists solely of the inner counted loop (DO I=1, 3), the present invention is not limited to perfectly nested loops. Without benefit of the present invention, a compiler developed for the 64 bit architecture of Intel.RTM. Corporation of Santa Clara, Calif., ("IA64") pipelines the inner loop of nested loop (I) into 12 stages having an initiation interval (II) of 5 clock cycles. In addition, 5 clock cycles are used to reset the addresses of the three arrays for each iteration of the outer loop and to adjust the loop tracking parameters (discussed below). If the outer loop body includes instructions in addition to those of the inner loop, the set up interval may increase to accommodate them. Using conventional modulo-scheduling techniques, the IA64 compiler generates the following intermediate code segment (II) for the exemplary nested loop (I):
(II) Outer_Loop
. . . . .
mov pr.rot = 0x10000
mov ar.ec = 12
mov ar.lc = 2
Inner_Loop
. . . . .
. . . . .
br.ctop Inner_loop
. . . . .
cmp4.1e p7, p6 = r2, r14
(p7) br.cond Outer_Loop
The mov instructions in Outer_Loop initialize a rotating segment of the predicate registers (pr.rot), along with epilog and loop counters, ar.ec and ar.lc, respectively, for the inner loop. General registers r2 and r14 store the number of iterations executed and the trip count, respectively, for the outer loop. They are used to determine whether additional iterations of the outer loop remain. Execution of code segment (II) generates a nested loop structure similar to that shown in FIG. 1C (FIG. 1D for short trip count loops). In particular, the software pipeline of Inner_Loop is filled and drained for each iteration of Outer_Loop, before the next iteration of Outer_Loop begins. During the set up portion of Outer_Loop, the mov, cmp4.1le, and branch instructions are executed along with any other instructions that may be present in the outer loop. The time required to execute the exemplary nested loop, using conventional modulo-scheduling techniques is: 100*(5+3*5+11*5)=7500 cycles The first term in parenthesis represents the 5 cycle overhead for resetting the arrays and loop tracking parameters. The second term represents the cycles consumed by the kernel phase of the inner loop, and the last term represents the cycles consumed by the epilog phase of the inner loop. The above values are provided to illustrate certain advantages of the present invention, which may be applied to nested loops independent of the number of stages they contain or the number of times the inner loop is iterated. In order to appreciate the advantages of the present invention, it is useful to understand the mechanisms through which loop branch operations may be controlled. FIG. 3 is a flow chart representing the operation of one embodiment of the branch instruction, br.ctop, in nested loop (I). The operations represented in FIG. 3 correspond to those implemented by a CTOP branch in the IA-64 instruction set architecture (ISA) of Intel.RTM. Corporation. Other ISAs that support software pipelining provide comparable functionality through various combinations of instructions, hardware, and compiler support. For the disclosed counted branch, LC is tested 310 for each iteration of a loop. If LC>0, the loop is in its kernel or prolog phase. LC is decremented 314, a (software pipeline) stage predicate is written 320, and a rotating register base (RRB) value is decremented 324. Writing one to the stage predicate ensures that all active stages of the software pipeline remain active when the registers are rotated. The value of EC, which is typically initialized to the number of stages in the loop, is preserved 318 through the kernel phase, as indicated. When LC reaches zero 310, all iterations of the loop have been started and the epilog phase is reached. Branching operation determines 330 whether to enter the epilog phase (330) or fall-through (350) to the instructions that follow the loop. In the epilog phase (LC=0, EC>1), the loop traverses path 330 (drain mode) to drain the stages of the software pipeline. When the "fall through" condition is detected (LC=0, EC=1), the loop status parameters are updated and control passes to the instructions following the loop. Under certain conditions, the fall through condition occurs for LC=0, EC=0, but the overall operation of the loop is similar. The operations by which the software pipeline is drained are designated "drain mode" in FIG. 3. For the disclosed br.ctop, these operations include: decrementing EC 338 to update the loop status (another stage deactivated); writing a zero 340 to the stage predicate to deactivate the associated stage instructions; and decrementing RRB 344 to rotate the registers. The value of LC is preserved 334, and the loop controlled by branch 300 is repeated with the updated values of EC, RRB, and the stage predicate. This process is repeated until it is determined 330 that EC=1, at which point branch 300 enters the path labeled "no drain mode". In this state, branch 300 performs a last set of bookkeeping steps 354, 358, 360, 364, and "falls through" to the instruction(s) that follow the loop. In the conventional implementation of nested loop (I), i.e. code segment (II), br.ctop falls through to a compare instruction (cmp4.le), which determines whether additional iterations of Outer_Loop remain. If they do, the predicate p7 is true and the outer loop branch, br.cond, directs the instruction flow back to Outer_Loop. For each new iteration of Outer_Loop, the inner loop is initialized through the series of mov instructions and executed according to the initialized values. As indicated in FIGS. 1C and 1D, executing the inner loop includes filling the pipeline during the prolog phase and draining the pipeline during the epilog phase for each iteration of the outer loop. For one embodiment of the present invention, the overhead associated with the inner loop epilog and prolog stages is reduced by bypassing the stage draining operations of the inner loop epilog phase for each iteration of the outer loop except the last iteration. One way to accomplish this is represented by code segment (III):
(III) mov pr.rot = 0x10000
mov ar.ec = 1
Outer_Loop:
mov ar.lc = 2
. . . . .
(IIIa) Inner_Loop:
. . . . .
. . . . .
btr.ctop Inner_Loop
. . . . .
cmp4.1e p7, p0 = r2, r14
cmp4.eq p6, p0 = r2, r14
(p7) cmp4e.eq p16 = r0, r0
(p7) mov ar.ec = 1;;
(p6) mov ar.ec = 12
(p7) br.cond Outer_Loop
For the modified exemplary loop, the inner loop is initialized with EC =1, prior to the first iteration of Outer_Loop. For each iteration of Outer_Loop, LC is initialized to the appropriate value for Inner_Loop, and Inner_Loop is executed until the kernel condition (LC =0) is met, i.e. the epilog is reached. Referring to FIG. 3, when LC =0, branch 300 leaves the kernel phase and checks the status 330 of the epilog phase. In the exemplary code, EC is initialized to 1 and this value is preserved through the kernal phase, e.g. via 318. With EC at 1, branch 300 proceeds down the "no-drain" path (350) and falls through to the instruction(s) that follow Inner_Loop. Thus, initializing EC to one allows the inner loop to bypass the pipelining draining operations of epilog phase of branch 300. Following Inner_Loop, instructions determine whether more iterations of Outer_Loop remain (cmp4.1e) or whether the final iteration of Outer_Loop has been reached (cmp4.eq). In the first case, the stage predicate is reset (comp4.eq p16=r0, r0), EC is reset to 1 (mov ar.ec=1), and Outer_Loop is reentered (br.cond Outer_Loop). In the second case, the stage predicate is reset and EC is initialized to indicate the actual number of inner loop stages, e.g (mov ar.ec=12) in the exemplary loop. Referring to FIG. 3, initializing EC to 12 directs Inner_Loop to "drain mode" for successive iterations, until EC reaches 1. Thus, on the final iteration of Outer_Loop, Inner_Loop traverses stage draining path 330 until its pipeline is fully drained. This allows all instructions associated with the last iterations of Inner_Loop to complete. The time required to execute nested loop (I), as implemented by code segment (III) according to the present invention is: 100*(6+3*5)+11*5=2155 cycles The first term in parenthesis represents the 6 cycle overhead for resetting the arrays and registers in modified code (III). The additional clock cycle overhead for code segment III represents the relatively minor cost of implementing the present invention. The second term represents the cycles consumed by the kernel phase of the inner loop. The last term on the right hand side represents the cycles consumed by the epilog phase of the inner loop in modified code (III). Since the epilog and prolog phases of consecutive inner loop pipelines are overlapped for all but the final iteration of the outer loop, the clock cycles consumed by the inner loop epilog phase only contributes once to overall processing time. For exemplary nested loop (I), the inner loop is a CTOP branch, whose loop conditions (310, 330) are tested at the end of the loop body, respectively. The present invention may also be implemented for counted loops in which the loop condition is tested somewhere other than the end of the loop body. These are referred to as CEXIT branches in the IA64 ISA. In addition, the present invention may be implemented for while-type inner loops. The IA64 ISA supports both TOP and EXIT variants of while-type loops (WTOP, WEXIT), which are tested similarly to the corresponding modulo-scheduled counted loops, CTOP and CEXIT, respectively. Another method for implementing the present invention is represented by the following code segment (IV):
(IV) mov pr.rot = 0x10000
mov r2 = 100
mov p6 = 0
Outer_Loop:
(p16) add r2 = r2, -1
(p16) mov ar.ec = 1
(p16) mov ar.lc = 2
(p6) mov ar.ec = 11
(p6) move ar.lc = 0
(IVa) Inner_Loop:
. . .
br.ctop Inner_Loop
(p17) comp4.gt.unc p16, p6 = r2, r0
. . .
(p16) br.cond Outer_Loop
(p6) br.cond Outer_Loop
For code segment (IV), general register r2 is initialized to the trip count for Outer_Loop and decremented (add r2=r2, -1) on each iteration of Outer_Loop. General register r0 is hardwired to 0. Predicate register p6 becomes true when the last iteration of Outer_Loop is reached, and predicate register p16 stores the complement of p6 (com4.gt.unc p16, p6=r2, r0). Predicate register, p17, is true as long as at least one more iteration of Outer_Loop remains. If at least one outer loop iteration remains (p16 is true), inner loop parameters, EC and LC, are set to 1 (no drain mode) and 2, respectively. If the final iteration of the outer loop is completed (p6 is true), inner loop parameter EC is initialized for drain mode (EC=SC-1=11) and LC is set to zero to indicate that the epilog has been reached. The loop body instructions of Inner_Loop are executed, and since p17=0 now, both p16 and p6 are reset because they are defined by an unconditional compare instruction (cmp4.gt.unc p16, p6=r2, r0). Yet another method for implementing the present invention is represented by the following code segment (V):
(V) mov r14 = 99
mov pr.rot = 0x10000
mov ar.ec = 0
mov ar.lc = 2
Outer_Loop (Va):
A
cmp.gt p7, p6 = r14, 1
add r14 = r14, -1;
(p6) mov ar.lc = 12
Inner_Loop:
. . .
br.ctop Inner_Loop;;
B
(p7) mov ar.lc = 3
br.ctop Outer_Loop
For code segment (V), A and B represent any outer loop instructions that are executed before and after, respectively, the inner loop. For this embodiment, the last iteration of the outer loop is checked at the top of the outer loop (cmp.gt p7, p6=r14, 1). If the last iteration is detected, p6 is true, and EC is adjusted to fully drain the inner loop pipeline (mov ar.ec=12). If additional outer loop iterations remain, EC remains 0. After the inner loop is executed, the value of LC is set to the inner loop trip count (mov ar.lc=3), when additional outer loop iterations remain, i.e. p7 is true. This allows a br.ctop instruction to be used for the outer loop closing branch as well. The outer loop br.ctop decrements LC to the inner loop trip count -1, sets the stage predicate (p63) to 1, eliminating an intialization step, and implements the register rotation that is not done by the inner loop closing branch (br.ctop) when EC=0 (no drain mode). For the last outer loop iteration, p7 is false, the inner loop exits with the correct LC and EC values (0 and 1, respectively), and the outer loop falls through. The outer loop instructions, A and B, are executed exactly the required number of times, without any additional predication. Code segments (III), (IV) and (V) represent just three of the many ways of implementing the present invention. Persons skilled in the art of computer programming, having the benefit of this disclosure, will recognize variations on these code segments which may accomplish the same efficiencies. Exemplary code segments (III), (IV) and (V) and their variants are suitable for any type of software pipelined inner loop that is nested within an outer counted loop. The number of outer loop iterations is known at the outset and reflected in the initial values of r14 for code segments III and V, and r2 for code segment (IV). The present invention is also applicable to while-type outer loops, as long as provisions are made to drain the inner loop software pipeline for the last iteration of the outer loop. For while-type loops, the trip count is not known when the nested loop is initiated. The loop status is indicated by a predicate, which is generated and tested on each iteration of the outer loop. If the inner loop is executed in no-drain mode and it is subsequently determined that the last outer loop iteration has completed, the inner loop pipeline will remain to be drained. Different methods for doing this are indicated in FIG. 4. FIG. 4 represents a counted nested loop 410 and while-type nested loops 420, 430 implemented in accordance with the present invention. For nested loop 410, which includes a counted outer loop 414, inner loop 418 is executed in no-drain (ND) mode as long as more than 1 iteration (>1) remains. On the last iteration (=1) it is executed in drain (D) mode, and nested loop 410 falls through (FT) to the instructions that follow it. Because while type loops do not recognize their last iteration until it is in progress, the iterations (>1) and (=1) are not distinguished. For nested loop 420, which includes a while-type outer loop 424, inner loop 428 is executed in ND mode while the outer loop condition is true (=1). In the disclosed example, final iterations of inner loop 428 are executed in D mode, when the outer loop condition is false (=0), and inner loop 428 jumps directly to the fall through instructions when it completes. Alternatively, a nested loop 430 that includes a while-type outer loop 434 may execute its inner loop 428 in ND mode as long as the outer loop condition remains true (=1). When the outer loop condition becomes false (=0), control may jump to code 440 following outer loop 434 that drains inner loop 438 and then falls through. FIG. 5 is a flowchart representing an overview of a method in accordance with the present invention for processing nested loops. The nested loop is initiated by beginning 510 an iteration of the outer loop. The outer loop instructions include an inner loop, which has been modulo scheduled. The inner loop is executed 520 in a "no-drain" mode for the current iteration of the outer loop. As above, "no-drain" mode means that the inner loop is initialized to bypass its epilog phase when it is executed during the current iteration of the outer loop. The inner loop software pipeline for this iteration of the outer loop is drained during the next iteration of the outer loop, as the inner loop software pipeline for the next outer loop iteration is filled. The status of the outer loop is tested 530 to determine whether its last iteration has been reached. If it is determined 530 that the next iteration of the outer loop is not the last iteration, the next iteration begins 510, and the inner loop is executed 520 in no-drain mode. If the next iteration of the outer loop is determined 530 to be the last iteration, the inner loop is executed 540 in "drain" mode, i.e. normally, and the nested loop is completed 550. FIG. 6 is a flowchart representing one embodiment of method 500 in greater detail. When. a nested loop is initiated, the modulo-scheduled inner loop is initialized 610 to "no-drain" mode. This may be accomplished, for example, by initializing a loop status parameter such as EC to indicate that there are no stages to be drained from the software pipeline of the modulo-scheduled inner loop. An iteration of the outer loop is initiated 620, and a kernel condition for the inner loop is initialized 624. For a counted inner loop, the kernel condition may be initialized by setting a loop counter parameter (LC) equal to the number of logical iterations for the counted loop. For a while-type loop, the kernel condition is typically represented by a logical expression that is determined dynamically for each iteration of the loop. The logical expression is tested for each loop iteration, and a new iteration of the loop is begun when the logical condition is, e.g. true. In this case, initialization may correspond to setting the logical expression true (or false) for the first iteration of the inner loop. The inner loop is iterated 628 and a "fall-through" condition is tested 630 to determine whether the loop should continue. For IA64 top-type loops (CTOP and WTOP) in "no-drain" mode, the fall-through condition is met when the kernel condition is false, e.g. LC=0 for a CTOP branch and br.pr=0 for a WTOP branch. In each case, the inner loop is exited without draining the associated software pipeline. Once the fall-through condition occurs for the inner loop, any instructions in the outer loop that follow the inner loop are executed 634 and the outer loop is tested 638 for completion. If the last iteration of the outer loop has completed, the nested loop is complete 640. If iterations of the outer loop remain, it is determined 644 whether the last iteration of the outer loop has been reached. If the last iteration of the outer loop has not been reached, the inner loop is set to "no-drain" mode and the next iteration of the outer loop is begun 620. For the last iteration of the outer loop, the inner loop is set 644 to "drain" mode, e.g. EC is initialized to indicate the inner loop stage count, and the final iteration of the outer loop is begun 620. For this final outer loop iteration, the "fall through" condition for the inner loop does not coincide with the kernel condition. That is once the kernel condition is met, the controlling loop branch continues to iterate the inner loop to drain the software pipeline. For the exemplary CTOP loop, "drain" mode is entered by setting EC to equal the actual number of stages in the modulo-scheduled loop. In FIG. 3, this means that the inner loop traverses drain path 330 when the kernel condition 310 is true, until the epilog condition (EC=1) is also true. Methods 500 and 600 may be adjusted for CEXIT and WEXIT branches to reflect their different looping behavior. That is CEXIT and WEXIT branches continue to their respective loops when they "fall through" and terminate their respective loops when they are taken. For CEXIT and WEXIT branches in "no-drain" mode, the fall through condition 630 is met when the kernel condition is true, and the inner loop continues executing. When the kernel condition becomes false for a CEXIT or WEXIT loop in "no-drain" mode, the inner loop is exited without draining the associated software pipeline. In "drain" mode, the fall through condition for CEXIT and WEXIT branches depends on both the kernel and epilog variables. In drain mode, CEXIT and WEXIT branches continue to loop when their kernel conditions are false, until their pipelines are emptied. One advantage of the present invention is that it provides for efficient processing of nested loops without significantly expanding the initiation interval of the software pipeline stages or the code size. For example, loop collapsing is a method for handling nested loops that combines inner and outer loops into a single loop. This approach often requires the addition of new instructions to the collapsed loop to track the parameters of the component loops. In some cases, the additional instructions may implement high latency calculations, which further expand the initiation interval. The present invention does not require any significant additions to the instructions of the nested loop, and it may be used to increase the efficiency of a variety of nested loops. The present invention has been described for cases in which any "live-in" values, i.e. values input to the inner loop, are either written to static registers for use in the first stage of the inner loop or are input to rotating registers for the inner loop. It is also possible to accommodate loops in which live-in values are written to static registers in stages other than the first stage, provided the stages of the inner loop executed in no-drain and drain modes are also adjusted. Adjustments to the "drain" and "no-drain" modes for the more general case are illustrated below. Let N1 represent the latest stage of the inner loop that receives a live-in value in a static register, and let N2 represent the latest stage of the inner loop that defines a live-out value (a value used by the outer loop). For no-drain mode, EC is set to the larger of N1 and N2, e.g. EC.sub.ND =Max (N1, N2), and the epilog phase of the inner loop is executed until (EC.sub.ND -1) stages are drained for a given outer loop iteration. The remaining stages are executed during the next outer loop iteration, in parallel with the prolog phase of the inner loop pipeline for this next outer loop iteration. For drain mode, EC.sub.D is set to EC as before. FIG. 7 is a block diagram of one embodiment of a system 700 in which the present invention may be implemented. System 700 includes a processor 702 and a main memory 770 that are coupled to system logic 790 through a system bus 780 and a memory bus 784. System 700 typically also includes a graphics system and peripheral device(s) (not shown) which also communicate through system logic 790. The disclosed embodiment of processor 702 includes execution resources 710, an instruction cache 720, a data cache 730, a cache controller 750, and a bus controller 760. Processor 702 typically also includes other logic elements (not shown) to retrieve and process instructions and to update its architectural state as instructions are retired. Bus controller 760 manages the flow of data and instructions between processor 702 and main memory 770. The present invention does not depend on the detailed structure of the memory system or the processor. Instruction cache 720, data cache 730, and main memory 770 form a memory hierarchy that provides data and instructions to execution resources 710. The instructions operate on data (operands) that are provided from register files 714 or bypassed to execution resources 710 from various components of the memory hierarchy. A predicate register file 718 may be used to conditionally execute selected instructions in a program and implement software pipelining. An application register file 716 includes status and control registers, such as ar.lc and ar.ec, which govern selected operations. Operand data is transferred to and from register file 714 through load and store operations, respectively. For the present invention, portions of register files 714, 718 may be rotated by a register renaming unit 716. When execution resources 710 implement a nested loop in accordance with the present invention, the status/control registers 716 indicate the current phase of the inner loop, and instructions direct the processor to process the loop in drain or non-drain mode accordingly. The instructions that implement the loop stages, pipeline filling, and pipeline draining operations are typically stored in an instruction region 778 of memory 770 during execution. They may be supplied to main memory from a non-volatile memory structure (hard disk, floppy disk, CD, etc.). The disclosed embodiments of the present invention are provided solely for purposes of illustration. Persons skilled in the art of programming and having the benefit of this disclosure will recognize variations on the disclosed embodiments that fall within the spirit of the present invention. The scope of the present invention should be limited only by the appended claims.
|
Same subclass Same class Consider this |
||||||||||
