Don't optimize when using -ftrapuv for uninitialized variable detection

ID 678741
Updated 8/27/2018
Version Latest
Public

author-image

By

Product and Version
Intel® Fortran and C++ Compilers 13 and newer

Operating System
Windows*, Linux*, macOS*

Problem Description
If the switch /Qftrapuv (-ftrapuv) for run-time detection of uninitialized local scalar variables is used in conjunction with optimization flags such as /O2 (-O2), it may lead to unexpected floating-point exceptions that are not related to uninitialized variables.

Explanation 
The switch /Qftrapuv (-ftrapuv) sets local, scalar variables that are not otherwise initialized to an "unusual" initial value such as 0xCCCCCCCC. When the main program is compiled with this switch, it also unmasks the "INVALID" floating-point exception. so that exceptions may be raised when the "unusual" values are used in floating-point operations. The switch also changes the default optimization level from /O2 to /Od (from -O2 to -O0). This is so that exceptions will not be raised as a result of speculated floating-point operations or other optimizations. If the new default optimization level of /Od (-O0) is explicitly overridden, optimizations such as floating-point speculation associated with masked vector operations may result in INVALID exceptions that would not otherwise have been raised.

Solution
Either 
Do not override the default /Od (-O0) optimization level when using /Qtrapuv (-ftrapuv). Sometimes, it may be sufficient to use /Qfp-speculation:safe (-fp-speculation safe) in conjunction with -O2.

or
Intel® Inspector, a component of Intel® Parallel Studio XE, may also be used for the detection of some instances of uninitialized variables being read.

or (Fortran only)  
Use the switch /check:uninit (-check uninit) in preference to /Qtrapuv (-ftrapuv) for the run-time detection of uninitialized, local scalar variables. This uses a different mechanism for uninitialized variable detection and is less likely to produce unrelated floating-point exceptions.

Beginning with Intel Fortran Compiler 18, the initialization values of classes of variables caused by the switch /Qinit:keyword  (-init=keyword) has been expanded from zero and signaling NaN (snan) to include huge (largest representable positive real value), minus huge (largest representable negative real value), infinity, minus infinity, tiny and minus tiny. For a complete description of this compiler switch, see the Intel Fortran User Guide.

See Detection of Uninitialized Floating Point Variable in Intel Fortran for more about detecting uninitialized floating-point variables using the Intel Fortran Compiler version 16 and later.