feat(tpc): 新增 --func-hotspot 函数热点统计,Ctrl+C 时落盘

启用后为每个函数定义生成 internal i64 计数器并在入口递增;各模块 Ctrl+C 处理器以追加方式写最终文件(含 main 的模块启动时清空一次),经 llvm.global_ctors 自注册;未传参数时零影响。
This commit is contained in:
TermiNexus committed 2026-09-13 17:50:01 +08:00
1 parent 355811d64e
commit 5680592f37
7 files changed
+339 -14

No files matched your search

+10
View File
@@ -149,6 +149,7 @@ def main() -> None:
parser.add_argument('--clear-cache', action='store_true', help='清除 .transpyc_cache 全局缓存(.vpi/.stub.ll/.doc.json)')
parser.add_argument('--asan', action='store_true', help='ASan 模式:对全部 .ll 做 opt -passes=asan 插桩,并显式链接 clang64 的 ASan 运行库(mingw64 的 clang 没有 x86_64-w64-windows-gnu 目标的 ASan runtime,不能用 -fsanitize=address)')
parser.add_argument('--nowarn-register', action='store_true', help='抑制"外部函数返回类型未登记"类 WARN 输出(透传给自举编译的 TransPyV.exe)')
parser.add_argument('--func-hotspot', help='函数热点统计:为每个模块的函数定义插入计数器,运行时 Ctrl+C 各模块以追加方式把计数写入 <path>(含 main 的模块在启动时清空一次,保证每次运行一份结果)(可用 python Projectrans.py --project ... --func-hotspot <path>)')
args: argparse.Namespace = parser.parse_args()
@@ -193,6 +194,11 @@ def main() -> None:
pass
target_triple: str | None = resolved.get('target', {}).get('triple')
target_datalayout: str | None = resolved.get('target', {}).get('datalayout')
# 函数热点统计(--func-hotspot):解析为绝对路径,保证运行时(exe 工作目录为
# output_dir)各模块处理器追加写入的路径与预期完全一致。
func_hotspot_file: str | None = args.func_hotspot
if func_hotspot_file:
func_hotspot_file = os.path.abspath(func_hotspot_file)
if isinstance(src, list):
if len(src) == 1 and os.path.isdir(src[0]):
@@ -209,6 +215,8 @@ def main() -> None:
_vlog().info(f"目标三元组: {target_triple}")
if target_datalayout:
_vlog().info(f"数据布局: {target_datalayout}")
if func_hotspot_file:
_vlog().info(f"函数热点统计: {func_hotspot_file}(各模块运行时以追加方式写入,含 main 的模块启动时清空一次)")
# 每次编译自动清理子进程性能剖析输出(PROF 目录 *.prof,硬编码路径)
from lib.Projectrans.Phase2Translator import _CleanProfFiles, PROF_DIR
@@ -314,6 +322,8 @@ def main() -> None:
trans: Phase2Translator = Phase2Translator(src, temp_dir, output_dir, compile_cmd=cc_cmd, compile_flags=cc_flags, linker_cmd=linker_cmd, linker_flags=linker_flags, linker_output=linker_output, static_libs=static_libs, dynamic_libs=dynamic_libs, include_dirs=project_include_dirs, target_triple=target_triple, target_datalayout=target_datalayout, force_recompile_includes=force_recompile_includes, asan_mode=args.asan)
trans.sha1_map = Load_sha1_map(temp_dir)
trans.slice_level = slice_level
# 函数热点统计(--func-hotspot):None=未启用(零影响)
trans.func_hotspot_file = func_hotspot_file
trans.run()
if args.run and linker_output and not linker_output.endswith('.bin'):