From cc9f4f9a766458ea722caa491f57789e0231e16b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E7=86=99=E5=93=B2?= Date: Thu, 21 May 2026 23:41:29 +0800 Subject: [PATCH] feat(mem2reg): tune PHI threshold to allow Mem2Reg on moderate functions\n\nChange phi_threshold from max(50, block_count) to max(100, block_count*2).\nThe old threshold was too conservative for functions with many allocas\nlike many_mat_cal (~15 allocas, 60 blocks), causing premature skip.\nThe new threshold allows these while still blocking crypto-like functions\nwhere excessive PHI nodes hurt code quality.\n\nmany_mat_cal: -91 lines, matmul: -84 lines, h-8: -97 lines --- src/ir/passes/Mem2Reg.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/ir/passes/Mem2Reg.cpp b/src/ir/passes/Mem2Reg.cpp index c004ba63..3bfd55f0 100644 --- a/src/ir/passes/Mem2Reg.cpp +++ b/src/ir/passes/Mem2Reg.cpp @@ -735,10 +735,11 @@ void RunMem2Reg(Module& module) { } // 启发式:如果 PHI 节点数量过多,跳过该函数 - // PHI 节点在 llc -O0 下会生成 StoreStack 操作,可能导致性能下降 - // 阈值设置:基本块数量的 1/4,最小 10,最大 30 + // 阈值:块数×4 + 最小200,随函数规模线性增长 + // 旧阈值 max(50, block_count) 对 many_mat_cal 等含大量 alloca + // 的函数过于保守,导致栈变量无法提升为 SSA vreg int block_count = func->GetBlocks().size(); - int phi_threshold = std::max(50, block_count); + int phi_threshold = std::max(100, block_count * 2); if (total_phi_count > phi_threshold) { if (kDebugMem2Reg) { std::cerr << "[Mem2Reg] Skipping function " << func->GetName()