C++: Avoid caching calls that rethrow or query exception state
Compiler now avoids caching function calls during constant evaluation if they involve exceptions.
This commit prevents the C++ compiler from caching function calls during constant evaluation if those calls involve exception handling mechanisms like rethrow, __builtin_uncaught_exceptions, or __builtin_current_exception. Caching such calls could lead to incorrect results if the execution context’s exception state changes. By marking these operations as non-cacheable, the compiler ensures more reliable constant evaluation in the presence of exceptions.
In Details
GCC's constant evaluation for C++ constexpr functions caches results to avoid redundant computations. This change modifies cxx_eval_cxa_builtin_fn to prevent caching of calls that interact with the exception handling runtime, such as rethrow, __builtin_uncaught_exceptions, and __builtin_current_exception. These operations' results can depend on the runtime exception state, making them unsafe to cache across different evaluation contexts.
- constexpr
- A C++ keyword that allows functions and variables to be evaluated at compile time, enabling performance optimizations and compile-time checks.
- constant evaluation
- The process by which the compiler evaluates expressions with constant values at compile time, rather than at runtime.
- rethrow
- A C++ exception handling mechanism that re-throws the currently active exception.
- __builtin_uncaught_exceptions
- A GCC built-in function that returns the number of currently active exception handlers.
- __builtin_current_exception
- A GCC built-in function that returns a pointer to the current exception object.