From 1e84849cb2a1d777b38b45dab8c6e5ecd49b6b77 Mon Sep 17 00:00:00 2001 From: Iain Sandoe Date: Thu, 4 Sep 2025 16:21:16 +0100 Subject: [PATCH] libstdc++: Implement P1494 and P3641 Partial program correctness [PR119060] This implements the library parts of P1494 as amended by P3641. For GCC the compiler itself treats stdio operations as equivalent to the observable checkpoint and thus it does not appear to be necessary to add calls to those functions (it will not alter the outcome). This adds the facility for C++26, although there is no reason, in principle, that it would not work back to C++11 at least. PR c++/119060 libstdc++-v3/ChangeLog: * include/bits/version.def: Add observable_checkpoint at present allowed from C++26. * include/bits/version.h: Regenerate. * include/std/utility: Add std::observable_checkpoint(). * src/c++23/std.cc.in: Add obervable_checkpoint () to utility. Signed-off-by: Iain Sandoe --- libstdc++-v3/include/bits/version.def | 9 +++++++++ libstdc++-v3/include/bits/version.h | 10 ++++++++++ libstdc++-v3/include/std/utility | 19 +++++++++++++++++++ libstdc++-v3/src/c++23/std.cc.in | 3 +++ 4 files changed, 41 insertions(+) diff --git a/libstdc++-v3/include/bits/version.def b/libstdc++-v3/include/bits/version.def index 1118da4b541..f60a518b28d 100644 --- a/libstdc++-v3/include/bits/version.def +++ b/libstdc++-v3/include/bits/version.def @@ -1950,6 +1950,15 @@ ftms = { }; }; +ftms = { + name = observable_checkpoint; + values = { + v = 202506; + cxxmin = 26; + extra_cond = "__has_builtin(__builtin_observable_checkpoint)"; + }; +}; + ftms = { name = algorithm_default_value_type; values = { diff --git a/libstdc++-v3/include/bits/version.h b/libstdc++-v3/include/bits/version.h index c452bbeec8e..cbd82ff81e8 100644 --- a/libstdc++-v3/include/bits/version.h +++ b/libstdc++-v3/include/bits/version.h @@ -2181,6 +2181,16 @@ #endif /* !defined(__cpp_lib_unreachable) */ #undef __glibcxx_want_unreachable +#if !defined(__cpp_lib_observable_checkpoint) +# if (__cplusplus > 202302L) && (__has_builtin(__builtin_observable_checkpoint)) +# define __glibcxx_observable_checkpoint 202506L +# if defined(__glibcxx_want_all) || defined(__glibcxx_want_observable_checkpoint) +# define __cpp_lib_observable_checkpoint 202506L +# endif +# endif +#endif /* !defined(__cpp_lib_observable_checkpoint) && defined(__glibcxx_want_observable_checkpoint) */ +#undef __glibcxx_want_observable_checkpoint + #if !defined(__cpp_lib_algorithm_default_value_type) # if (__cplusplus > 202302L) # define __glibcxx_algorithm_default_value_type 202403L diff --git a/libstdc++-v3/include/std/utility b/libstdc++-v3/include/std/utility index 8a85ccfd09b..3ae1852426e 100644 --- a/libstdc++-v3/include/std/utility +++ b/libstdc++-v3/include/std/utility @@ -98,6 +98,7 @@ #define __glibcxx_want_tuple_element_t #define __glibcxx_want_tuples_by_type #define __glibcxx_want_unreachable +#define __glibcxx_want_observable_checkpoint #define __glibcxx_want_tuple_like #define __glibcxx_want_constrained_equality #include @@ -234,6 +235,24 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION } #endif + +#ifdef __cpp_lib_observable_checkpoint // C++ >= 26 + /// Informs the compiler that prior actions are considered observable. + /** + * This may be used to limit the extent to which optimisations based on the + * assumed unreachability of undefined behaviour can propagate to earlier + * code. + * + * @since C++26 + */ + [[__gnu__::__always_inline__]] + inline void + observable_checkpoint() noexcept + { + return __builtin_observable_checkpoint(); + } +#endif + _GLIBCXX_END_NAMESPACE_VERSION } // namespace diff --git a/libstdc++-v3/src/c++23/std.cc.in b/libstdc++-v3/src/c++23/std.cc.in index 9cf5fa7dcb6..e849307f186 100644 --- a/libstdc++-v3/src/c++23/std.cc.in +++ b/libstdc++-v3/src/c++23/std.cc.in @@ -3337,6 +3337,9 @@ export namespace std #if __cpp_lib_unreachable using std::unreachable; #endif +#if __cpp_lib_observable_checkpoint + using std::observable_checkpoint; +#endif } // -- 2.47.3