implement /OPT:ICF

Add identical COMDAT folding using a color-refinement over foldable
read-only sections. Hash section contents, relocation targets, and target
colors until partitions stabilize, then redirect folded COMDAT symlinks
and discard duplicate sections.

Honor .llvm_addrsig via /LLVM_ADDRSIG so address-significant sections
are not folded, keep separate color space for code, unwind info, and
MSVC vftables, and update COMDAT symbol/section mapping after folding.

Add linker tests covering function folding, relocation-sensitive folds,
alignment, color-space separation, symlink chains, .llvm_addrsig,
pdata/xdata, and C++ ICF cases.
This commit is contained in:
Nikita Smith authored and Ryan Fleury committed 2026-07-27 14:47:08 -07:00
1 parent 1ea9fae835
commit d34e1a7802
11 files changed
+1869 -153

No files matched your search

+6 -6
View File
@@ -4,7 +4,7 @@
internal void
tp_run_tasks(TP_Context *pool, TP_Worker *worker)
{
barrier_wait(pool->barrier);
barrier_wait(pool->run_barrier);
for (;;) {
S64 task_left = ins_atomic_u64_dec_eval(&pool->task_left);
@@ -19,14 +19,11 @@ tp_run_tasks(TP_Context *pool, TP_Worker *worker)
U64 task_id = pool->task_count - (task_left+1);
pool->task_func(arena, worker->id, task_id, pool->task_data, pool);
// cache task count so we dont touch pool memory after atomic inc
U64 task_count = pool->task_count;
// update task done count
ins_atomic_u64_inc_eval(&pool->task_done);
}
barrier_wait(pool->barrier);
barrier_wait(pool->run_barrier);
}
internal void
@@ -74,6 +71,7 @@ tp_alloc(Arena *arena, U32 worker_count, U32 max_worker_count, String8 name)
// init pool
TP_Context *pool = push_array(arena, TP_Context, 1);
pool->exec_semaphore = exec_semaphore;
pool->run_barrier = barrier_alloc(worker_count);
pool->barrier = barrier_alloc(worker_count);
pool->is_live = 1;
pool->worker_count = worker_count;
@@ -113,6 +111,7 @@ tp_release(TP_Context *pool)
if (is_shared) {
semaphore_release(pool->exec_semaphore);
}
barrier_release(pool->run_barrier);
barrier_release(pool->barrier);
MemoryZeroStruct(pool);
@@ -194,13 +193,14 @@ tp_for_parallel(TP_Context *pool, TP_Arena *task_arena, U64 task_count, TP_TaskF
// if we are in shared mode -> ping
if (*pool->exec_semaphore.u64) {
U64 drop_count64 = Min(task_count, pool->worker_count);
U64 drop_count64 = pool->worker_count - 1;
U32 drop_count = safe_cast_u32(drop_count64);
semaphore_drop_count(pool->exec_semaphore, drop_count);
}
// run tasks on main worker
tp_run_tasks(pool, pool->worker_arr);
Assert(pool->task_done == task_count);
}
}
+1 -1
View File
@@ -32,6 +32,7 @@ typedef struct TP_Context
Semaphore exec_semaphore;
Semaphore task_semaphore;
Semaphore main_semaphore;
Barrier run_barrier;
Barrier barrier;
void *broadcast;
U64 broadcast_size;
@@ -58,4 +59,3 @@ internal void tp_temp_end(TP_Temp temp);
internal void tp_for_parallel(TP_Context *pool, TP_Arena *arena, U64 task_count, TP_TaskFunc *task_func, void *task_data);
internal Rng1U64 * tp_divide_work(Arena *arena, U64 item_count, U32 worker_count);
#define tp_broadcast(p) tp_broadcast_(tp, task_id, p, sizeof(*p))