Phase 7-Step8: Replace SFC/HEAP_V2/ULTRA_SLIM runtime checks with config macros

**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 <noreply@anthropic.com>
This commit is contained in:
Moe Charm (CI)
2025-11-29 17:40:05 +09:00
parent 69e6df4cbc
commit 6b75453072
3 changed files with 12 additions and 8 deletions

View File

@ -90,7 +90,7 @@ static inline int tiny_fastcache_enabled(void) {
return g_fastcache_enable; return g_fastcache_enable;
} }
static inline int sfc_cascade_enabled(void) { static inline int tiny_sfc_enabled(void) {
extern int g_sfc_enabled; extern int g_sfc_enabled;
return 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 // These expand to actual function calls in normal mode
#define TINY_FRONT_ULTRA_SLIM_ENABLED ultra_slim_mode_enabled() #define TINY_FRONT_ULTRA_SLIM_ENABLED ultra_slim_mode_enabled()
#define TINY_FRONT_HEAP_V2_ENABLED tiny_heap_v2_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_FASTCACHE_ENABLED tiny_fastcache_enabled()
#define TINY_FRONT_TLS_SLL_ENABLED tiny_tls_sll_enabled() #define TINY_FRONT_TLS_SLL_ENABLED tiny_tls_sll_enabled()
#define TINY_FRONT_UNIFIED_GATE_ENABLED front_gate_unified_enabled() #define TINY_FRONT_UNIFIED_GATE_ENABLED front_gate_unified_enabled()

View File

@ -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) // 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); void* base = (void*)((uint8_t*)ptr - 1);
if (tiny_heap_v2_try_push(class_idx, base)) { 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); tiny_debug_ring_record(TINY_RING_EVENT_FREE_FAST, (uint16_t)class_idx, ptr, slab_idx);

View File

@ -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 // FIX: Ensure TLS is initialized before first magazine access
tiny_heap_v2_ensure_init(); tiny_heap_v2_ensure_init();
if (class_idx < 0 || class_idx > 3) return NULL; 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; if (!tiny_heap_v2_class_enabled(class_idx)) return NULL;
TinyHeapV2Mag* mag = &g_tiny_heap_v2_mag[class_idx]; 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) // 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_check_done = 0;
static __thread int sfc_is_enabled = 0; static __thread int sfc_is_enabled = 0;
if (__builtin_expect(!sfc_check_done, 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; 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 // 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) // Transfer half of refilled blocks to SFC (keep half in SLL for future)
int sfc_target = refilled / 2; int sfc_target = refilled / 2;
if (sfc_target > 0) { if (sfc_target > 0) {
@ -734,7 +736,8 @@ static inline void* tiny_alloc_fast(size_t size) {
#if !HAKMEM_BUILD_RELEASE #if !HAKMEM_BUILD_RELEASE
static __thread int debug_checked = 0; static __thread int debug_checked = 0;
if (!debug_checked) { 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) { if (enabled) {
fprintf(stderr, "[TINY_ALLOC_FAST] Ultra SLIM gate: ENABLED (will use 4-layer path)\n"); fprintf(stderr, "[TINY_ALLOC_FAST] Ultra SLIM gate: ENABLED (will use 4-layer path)\n");
} else { } else {