The Wayback Machine - https://web.archive.org/web/20180123165050/http://docs.cray.com:80/books/S-3692-51/html-S-3692-51/zfixedn3c8sk4c.html

5.12. VOLATILE Attribute and Statement

The VOLATILE attribute and statement specifies that the value of an object is unpredictable. The object's value can change without visible assignment by the program, and it's value can be affected by external events. The presence of this statement prevents the compiler from optimizing references to specified variables, arrays, and common blocks of data.

Note: The VOLATILE attribute and statement are Fortran 2003 features.

The following format is for a type declaration statement with the VOLATILE attribute:

type, VOLATILE [, attribute_list] :: entity_decl_list

Subject to the rules governing combinations of these attributes, attribute_list can contain the following:

ALLOCATABLE
AUTOMATIC (EXTENSION)
DIMENSION
INTENT
OPTIONAL
POINTER
PRIVATE
PROTECTED (F2003)
PUBLIC
SAVE
STATIC (EXTENSION)
TARGET
VALUE (F2003)

The entity_decl_list can include the name of a common block, enclosed in slash characters (for example, /common_block_name/).

The format for the VOLATILE statement is as follows:

Table 5-25.

EXT

volatile_stmt

is

VOLATILE   entity_decl_list

EXT

entity_decl_list

is

data_object_name

EXT

 

or

/common_block_name/

The following example shows a type declaration statement that specifies the VOLATILE attribute:

INTEGER, VOLATILE :: D, E

In the following example, the named common block, BLK1, and the variables D and E are volatile. Variables P1 and P4 become volatile because of the direct equivalence of P1 and the indirect equivalence of P4. The code that shows this is as follows:

PROGRAM TEST
LOGICAL(KIND=1) IPI(4)
INTEGER(KIND=4) A, B, C, D, E, ILOOK
INTEGER(KIND=4) P1, P2, P3, P4
COMMON /BLK1/A, B, C

VOLATILE /BLK1/, D, E
EQUIVALENCE(ILOOK, IPI)
EQUIVALENCE(A, P1)
EQUIVALENCE(P1, P4)

The presence of a VOLATILE attribute or statement can inhibit some optimizations because it asserts that the compiler must perform loads and stores from the specified objects. As an example, consider the following code fragment:

J = 1
DO I = 1,100000
  IF (J.EQ.2) PRINT 'FOO'
END DO

If the preceding code were included in a Fortran program, the compiler might remove the statement IF (J.EQ.2) PRINT 'FOO' because J is loop invariant and because J was previously assigned the value 1. If J were declared VOLATILE, the compiler would perform all loads of J because something else might affect the value of J.

Note: The Fortran standard does not describe the VOLATILE attribute or statement.

A variable or common block must be declared VOLATILE if it can be read or written in a way that is not visible to the compiler. This would be the case in the following situations:

If an array is declared VOLATILE, each element in the array is VOLATILE. If a common block is declared VOLATILE, each variable in the common block is VOLATILE.

If an object of derived type is declared VOLATILE, its components are VOLATILE.

If a pointer is declared VOLATILE, the pointer itself is VOLATILE.

A VOLATILE statement must not specify a procedure, function result, or NAMELIST group name.