From b59c7172454f118f935f5d0696662a0427d29673 Mon Sep 17 00:00:00 2001 From: dragonmux Date: Wed, 26 Jul 2023 20:46:56 +0100 Subject: [PATCH] kernel/rtlil: Fixed the destruction guard for IdString The guard is optimised out on some compilers under certain conditions (eg: LTO on GCC) as constant under C++ lifetime rules. This is because the guard type's member is invalid to access (UB) after the type has been destroyed, resulting in `destruct_guard.ok` being unable to be `false` according to the optimiser, based on the lifetime rules. This patch still invokes UB (all accesses to the destroyed IdString instance are), but at least the optimiser can't reason that destruct_guard_ok cannot be false and therefore it's safe to optimise out from its guard role. --- kernel/rtlil.cc | 1 + kernel/rtlil.h | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 7f3508b2f..7011429ff 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -30,6 +30,7 @@ YOSYS_NAMESPACE_BEGIN +bool RTLIL::IdString::destruct_guard_ok = false; RTLIL::IdString::destruct_guard_t RTLIL::IdString::destruct_guard; std::vector RTLIL::IdString::global_id_storage_; dict RTLIL::IdString::global_id_index_; diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 7c7669caa..71ee7f989 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -85,10 +85,10 @@ namespace RTLIL // the global id string cache + static bool destruct_guard_ok; // POD, will be initialized to zero static struct destruct_guard_t { - bool ok; // POD, will be initialized to zero - destruct_guard_t() { ok = true; } - ~destruct_guard_t() { ok = false; } + destruct_guard_t() { destruct_guard_ok = true; } + ~destruct_guard_t() { destruct_guard_ok = false; } } destruct_guard; static std::vector global_id_storage_; @@ -147,7 +147,7 @@ namespace RTLIL static int get_reference(const char *p) { - log_assert(destruct_guard.ok); + log_assert(destruct_guard_ok); if (!p[0]) return 0; @@ -225,7 +225,7 @@ namespace RTLIL { // put_reference() may be called from destructors after the destructor of // global_refcount_storage_ has been run. in this case we simply do nothing. - if (!destruct_guard.ok || !idx) + if (!destruct_guard_ok || !idx) return; #ifdef YOSYS_XTRACE_GET_PUT