From 6b754530720b11c0b8a010bd2fb9b89711e1ce79 Mon Sep 17 00:00:00 2001 From: "Moe Charm (CI)" Date: Sat, 29 Nov 2025 17:40:05 +0900 Subject: [PATCH] Phase 7-Step8: Replace SFC/HEAP_V2/ULTRA_SLIM runtime checks with config macros MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Goal**: Complete dead code elimination infrastructure for all runtime checks **Changes**: 1. core/box/tiny_front_config_box.h: - Rename sfc_cascade_enabled() → tiny_sfc_enabled() (avoid name collision) - Update TINY_FRONT_SFC_ENABLED macro to use tiny_sfc_enabled() 2. core/tiny_alloc_fast.inc.h (5 locations): - Line 274: tiny_heap_v2_alloc_by_class() - use TINY_FRONT_HEAP_V2_ENABLED - Line 431: SFC TLS cache init - use TINY_FRONT_SFC_ENABLED - Line 678: SFC cascade check - use TINY_FRONT_SFC_ENABLED - Line 740: Ultra SLIM debug check - use TINY_FRONT_ULTRA_SLIM_ENABLED 3. core/hakmem_tiny_free.inc (1 location): - Line 233: Heap V2 free path - use TINY_FRONT_HEAP_V2_ENABLED **Performance**: 79.5M ops/s (maintained, -0.4M vs Step 7, within noise) - Normal mode: Neutral (runtime checks preserved) - PGO mode: Ready for dead code elimination **Total Runtime Checks Replaced (Phase 7)**: - ✅ TINY_FRONT_FASTCACHE_ENABLED: 3 locations (Step 4-6) - ✅ TINY_FRONT_TLS_SLL_ENABLED: 7 locations (Step 7) - ✅ TINY_FRONT_SFC_ENABLED: 2 locations (Step 8) - ✅ TINY_FRONT_HEAP_V2_ENABLED: 2 locations (Step 8) - ✅ TINY_FRONT_ULTRA_SLIM_ENABLED: 1 location (Step 8) **Total**: 15 runtime checks → config macros **PGO Mode Expected Benefit**: - Eliminate 15 runtime checks across hot paths - Reduce branch mispredictions - Smaller code size (dead code removed by compiler) - Better instruction cache locality **Design Complete**: Config Box as single entry point for all Tiny Front policy - Unified macro interface for all feature toggles - Include order independent (static inline wrappers) - Dual-mode support (PGO compile-time vs normal runtime) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- core/box/tiny_front_config_box.h | 4 ++-- core/hakmem_tiny_free.inc | 3 ++- core/tiny_alloc_fast.inc.h | 13 ++++++++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/core/box/tiny_front_config_box.h b/core/box/tiny_front_config_box.h index 1950ae17..cbb665e2 100644 --- a/core/box/tiny_front_config_box.h +++ b/core/box/tiny_front_config_box.h @@ -90,7 +90,7 @@ static inline int tiny_fastcache_enabled(void) { return g_fastcache_enable; } -static inline int sfc_cascade_enabled(void) { +static inline int tiny_sfc_enabled(void) { extern int g_sfc_enabled; return g_sfc_enabled; } @@ -104,7 +104,7 @@ static inline int tiny_tls_sll_enabled(void) { // These expand to actual function calls in normal mode #define TINY_FRONT_ULTRA_SLIM_ENABLED ultra_slim_mode_enabled() #define TINY_FRONT_HEAP_V2_ENABLED tiny_heap_v2_enabled() -#define TINY_FRONT_SFC_ENABLED sfc_cascade_enabled() +#define TINY_FRONT_SFC_ENABLED tiny_sfc_enabled() #define TINY_FRONT_FASTCACHE_ENABLED tiny_fastcache_enabled() #define TINY_FRONT_TLS_SLL_ENABLED tiny_tls_sll_enabled() #define TINY_FRONT_UNIFIED_GATE_ENABLED front_gate_unified_enabled() diff --git a/core/hakmem_tiny_free.inc b/core/hakmem_tiny_free.inc index b1cc9e81..db9c9359 100644 --- a/core/hakmem_tiny_free.inc +++ b/core/hakmem_tiny_free.inc @@ -229,7 +229,8 @@ void hak_tiny_free_with_slab(void* ptr, TinySlab* slab) { } // Front-V2: try to return to TLS magazine first (A/B, default OFF) - if (__builtin_expect(tiny_heap_v2_enabled() && class_idx <= 3, 0)) { + // Phase 7-Step8: Use config macro for dead code elimination in PGO mode + if (__builtin_expect(TINY_FRONT_HEAP_V2_ENABLED && class_idx <= 3, 0)) { void* base = (void*)((uint8_t*)ptr - 1); if (tiny_heap_v2_try_push(class_idx, base)) { tiny_debug_ring_record(TINY_RING_EVENT_FREE_FAST, (uint16_t)class_idx, ptr, slab_idx); diff --git a/core/tiny_alloc_fast.inc.h b/core/tiny_alloc_fast.inc.h index 1200addc..6f461573 100644 --- a/core/tiny_alloc_fast.inc.h +++ b/core/tiny_alloc_fast.inc.h @@ -270,7 +270,8 @@ static inline void* tiny_heap_v2_alloc_by_class(int class_idx) { // FIX: Ensure TLS is initialized before first magazine access tiny_heap_v2_ensure_init(); if (class_idx < 0 || class_idx > 3) return NULL; - if (!tiny_heap_v2_enabled()) return NULL; + // Phase 7-Step8: Use config macro for dead code elimination in PGO mode + if (!TINY_FRONT_HEAP_V2_ENABLED) return NULL; if (!tiny_heap_v2_class_enabled(class_idx)) return NULL; TinyHeapV2Mag* mag = &g_tiny_heap_v2_mag[class_idx]; @@ -424,11 +425,11 @@ static inline void* tiny_alloc_fast_pop(int class_idx) { } // Box 5-NEW: Layer 0 - Try SFC first (if enabled) - // Cache g_sfc_enabled in TLS to avoid global load on every allocation + // Phase 7-Step8: Use config macro for dead code elimination in PGO mode static __thread int sfc_check_done = 0; static __thread int sfc_is_enabled = 0; if (__builtin_expect(!sfc_check_done, 0)) { - sfc_is_enabled = g_sfc_enabled; + sfc_is_enabled = TINY_FRONT_SFC_ENABLED; sfc_check_done = 1; } @@ -674,7 +675,8 @@ static inline int tiny_alloc_fast_refill(int class_idx) { } // Only cascade if explicitly enabled AND we have refilled blocks in SLL - if (sfc_cascade_enabled && g_sfc_enabled && refilled > 0) { + // Phase 7-Step8: Use config macro for dead code elimination in PGO mode + if (sfc_cascade_enabled && TINY_FRONT_SFC_ENABLED && refilled > 0) { // Transfer half of refilled blocks to SFC (keep half in SLL for future) int sfc_target = refilled / 2; if (sfc_target > 0) { @@ -734,7 +736,8 @@ static inline void* tiny_alloc_fast(size_t size) { #if !HAKMEM_BUILD_RELEASE static __thread int debug_checked = 0; if (!debug_checked) { - int enabled = ultra_slim_mode_enabled(); + // Phase 7-Step8: Use config macro for dead code elimination in PGO mode + int enabled = TINY_FRONT_ULTRA_SLIM_ENABLED; if (enabled) { fprintf(stderr, "[TINY_ALLOC_FAST] Ultra SLIM gate: ENABLED (will use 4-layer path)\n"); } else {