SPRUIV4C May 2020 – December 2023
In order to software pipeline a loop (and thus improve performance), the only branch that may occur in a loop is a branch back to the top of the loop. Branches for if-then and if-then-else statements or for other control-flow constructs will prevent software pipelining.
To get around this limitation, the compiler performs if-conversion. If-conversion attempts to remove branches associated with if-then and if-then-else statements, by predicating instructions so that they conditionally execute depending on the test in the "if" statement. As long as there are not too many nesting levels, too many condition terms, or too many instructions in the if-then or if-then-else statements, if-conversion usually succeeds.
The following example demonstrates if-conversion. In order to software pipeline the "for" loop in this C++ code, if-conversion must be performed. The pragmas are used to prevent the compiler from vectorizing and generating additional code that is not important for this example.
// if_conversion.cpp
// Compile with "cl7x -mv7100 --opt_level=3 --debug_software_pipeline
// --src_interlist --symdebug:none if_conversion.cpp"
void function_1(int * restrict a, int *restrict b, int *restrict out, int n)
{
#pragma UNROLL(1)
#pragma MUST_ITERATE(1024, ,32)
for (int i = 0; i < n; i++)
{
int result;
if (a[i] < b[i])
result = a[i] + b[i];
else
result = 0;
out [i] = result;
}
}
After compilation, the single-scheduled iteration of the loop in the software pipeline information comment block looks like the following:
;*----------------------------------------------------------------------------*
;* SINGLE SCHEDULED ITERATION
;*
;* ||$C$C65||:
;* 0 TICK ; [A_U]
;* 1 SLDW .D1 *D2++(4),A1 ; [A_D1] |17| ^
;* || SLDW .D2 *D1++(4),A2 ; [A_D2] |17| ^
;* 2 NOP 0x5 ; [A_B]
;* 7 CMPGEW .L1 A2,A1,A0 ; [A_L1] |17| ^
;* 8 [!A0] ADDW .D2 A1,A2,D3 ; [A_D2] |17| ^
;* 9 [ A0] MVKU32 .S1 0,D3 ; [A_S1] |17|
;* 10 STW .D1 D3,*D0++(4) ; [A_D1] |17|
;* || BNL .B1 ||$C$C65|| ; [A_B] |9|
;* 11 ; BRANCHCC OCCURS {||$C$C65||} ; [] |9|
;*----------------------------------------------------------------------------
The instruction [!A0] ADDW.D2 A1,A2,D3
represents the "then" part of
the if statement. The instruction [A0] MVK32.S1 0,D3
represents the
"else" part of the if statement. The CMPGEW instruction computes the if-condition
and puts the result into a predicate register, which is used to conditionally
execute the ADDW and MVKU32 instructions.