safe_conditional
- Date:
02-06-2023
NAME
safe_conditional - specifies that it is safe to execute all references and operations within all conditional branches of a loop
SYNOPSIS
#pragma _CRI safe_conditional
!DIR$ SAFE_CONDITIONAL
DESCRIPTION
The safe_conditional directive specifies that it is safe to execute all references and operations within all conditional branches of a loop. In other words, it is your declaration that you know that these memory references can be safely executed in each iteration of the loop. This directive specifies that memory and arithmetic operations are safe.
This directive applies to scalar and vector loop nests. It can improve performance by allowing the hoisting of invariant expressions from conditional code and by allowing prefetching of memory references.
The safe_conditional directive is an advisory directive. That is, the compiler may override the directive if it determines the directive is not beneficial.
Caution:
Incorrect use of the directive can result in segmentation faults, bus errors, excessive page faulting, or arithmetic aborts. However, it should not result in incorrect answers. Incorrect usage can result in severe performance degradations or program aborts.
EXAMPLES
Example 1: safe_conditional directive
In the C/C++ example below, without the safe_conditional directive, the compiler cannot precompute the invariant expression s1*s2 because their values are unknown and may cause an arithmetic trap if executed unconditionally. However, if you know that the condition is true at least once, then s1*s2 is safe to speculatively execute. The safe_conditional compiler directive can be used to imply the safety of the operation. With the directive, the compiler evaluates s1*s2 outside the loop, rather than under control of the conditional code. In addition, all control flow is removed from the body of the vector loop, because s1*s2 no longer poses a safety risk.
void
safe_cond( double a[restrict 1000], double s1, double s2 )
{
int i;
#pragma _CRI safe_conditional
for (i = 0; i< 1000; i++) {
if( a[i] != 0.0) {
a[i] = a[i] + s1*s2;
}
}
}
Example 2: SAFE_CONDITIONAL directive
In the Fortran example below, the compiler cannot precompute the invariant expression s1*s2 because these values are unknown and may cause an arithmetic trap if executed unconditionally. However, if you know that the condition is true at least once, then it is safe to use the SAFE_CONDITIONAL directive and execute s1*s2 speculatively.
SUBROUTINE SAFE_COND( A, N, S1, S2 )
REAL A(N), S1, S2
!DIR$ SAFE_CONDITIONAL
DO I = 1,N
IF ( A(I) /= 0.0 ) THEN
A(I) = A(I) + S1*S2
ENDIF
ENDDO
END
SEE ALSO
intro_directives(1)