safe_address
- Date:
02-06-2023
NAME
safe_address, SAFE_ADDRESS - specifies that it is safe to speculatively execute memory references within all conditional branches of a loop
SYNOPSIS
#pragma _CRI safe_address
!DIR$ SAFE_ADDRESS
DESCRIPTION
Scope: Local
The safe address directives behave similarly in both C/C++ and Fortran. The C/C++ directive names are used below to explain their behaviors for both languages.
The safe_address directive specifies that it is safe to speculatively execute memory references 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.
For most code, the safe_address directive can improve performance significantly by preloading vector expressions. However, most loops do not require this directive to have preloading performed. The directive is required only when the safety of the operation cannot be determined or index expressions are very complicated.
The safe_address directive is an advisory directive. That is, the compiler may override the directive if it determines the directive is not beneficial.
If you do not use the directive on a loop and the compiler determines that it would benefit from the directive, it issues a message indicating such. The message is similar to this:
CC-6375 cc: VECTOR File = ctest.c, Line = 6
A loop would benefit from "#pragma safe_address".
If you use the directive on a loop, and the compiler determines that it does not benefit from the directive, the compiler issues a message that states the directive is superfluous and can be removed.
To see the messages in C/C++, you must use the -h report=v or -h msgs option. To see the messages in Fortran, you must use the -O msgs option.
Caution:
Incorrect use of this directive can result in segmentation faults, bus errors, or excessive page faulting. However, it should not result in incorrect answers. Incorrect usage can result in very severe performance degradations or program aborts.
EXAMPLES
Example 1: safe_address directive in C/C++
In this C/C++ example, the compiler will not preload vector expressions, because the value of j is unknown. However, if you know that references to b[i][j] are safe to evaluate for all iterations of the loop, regardless of the condition, you can use the safe_address directive for this loop as shown below:
void x3( double a[restrict 1000], int j )
{
int i;
#pragma _CRI safe_address
for ( i = 0; i < 1000; i++ ) {
if ( a[i] != 0.0 ) {
b[j][i] = 0.0;
}
}
}
With the directive, the compiler can load b[i][j] with a full vector mask, merge 0.0 where the condition is true, and store the resulting vector safely.
Example 2: SAFE_ADDRESS directive in Fortran
In the Fortran example below, the compiler will not preload vector expressions, because the value of j is unknown. However, if you know that references to b(i,j) are safe to evaluate for all iterations of the loop, regardless of the condition, we can use the SAFE_ADDRESS directive for this loop.
SUBROUTINE X3( A, B, N, M, J )
REAL A(N), B(N,M)
!DIR$ SAFE_ADDRESS
DO I = 1,64 ! VECTORIZED LOOP
IF ( A(I).NE.0.0 ) THEN
B(I,J) = 0.0 ! VALUE OF 'J' IS UNKNOWN
ENDIF
ENDDO
END
With the directive, the compiler can load b(i,j) with a full vector mask, merge 0.0 where the condition is true, and store the resulting vector using a full mask.
SEE ALSO
intro_directives(1)