已合并
[Huawei][AscendNPU IR] Add compile timing scopes #1470
[Huawei][AscendNPU IR] Add compile timing scopes #1470
已合并
shijingchang创建于 7月8日
shijingchang成员
7月8日

Summary

  • Add compile-level MLIR timing context for bishengir-compile.
  • Enable nested timing scopes around retriable BiShengHIR pipeline execution.
  • Profile external tool execution through the same timing context.
  • Register MLIR default timing manager CLI options in bishengir-compile.
  • Add LLVM patch to support --mlir-timing-display=sort, sorted by self time with timer paths.

Motivation

This change makes bishengir-compile timing output more useful for locating compile-time bottlenecks across internal pipelines and external compiler invocations.

The new timing context allows individual pipeline runs and external tools to appear under one compile timing tree, while the added sort display mode helps identify high self-time regions without losing nesting
context.

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 shijingchang 的贡献)
Sshijingchang成员
7月8日 创建了 pull request,commit 9490d0ed
atomgit-bot
atomgit-bot
7月8日 评论:

变更摘要

此 PR 为 bishengir-compile 引入编译级 MLIR 计时上下文,通过新增 CompileTimingScopedCompileTimingContextExternalToolProfiler 等基础设施,使内部 BiShengHIR 管线执行与外部工具调用均纳入统一的计时树中。同时通过 LLVM patch 为 MLIR 计时框架新增 sort 显示模式,支持按 self time 降序排列并保留路径嵌套信息,便于快速定位编译耗时瓶颈。

主要改动

  • 新增编译计时基础设施:在 Utils.h / Utils.cpp 中定义 CompileTiming 结构体(封装 DefaultTimingManager 与根 TimingScope)、线程局部的 ScopedCompileTimingContext RAII 上下文管理,以及 ExternalToolProfiler::run() 用于在嵌套计时作用域中执行外部工具并采集耗时。
  • 管线执行嵌套计时:在 RetriablePassManager::runOnce() 中通过 getCurrentCompileTimingScope() 获取当前编译计时根作用域,嵌套以管线名称为标签的子作用域,并调用 passManager.enableTiming() 将其传递给 PassManager。
  • 根计时上下文启用:在 BiShengIRCompileMain.cpprunBiShengIRPipeline() 入口处创建 CompileTiming timing 实例,建立整个编译流程的顶层计时上下文。
  • CLI 选项注册:在 bishengir-compile.cpp 中调用 mlir::registerDefaultTimingManagerCLOptions() 注册 MLIR 默认计时管理器的命令行选项。
  • LLVM 补丁:sort 显示模式:为 MLIR 计时框架新增 DisplayMode::Sort,实现 printAsSort()(按 self time 降序排列计时条目)、getSelfTimeRecord()(计算自身耗时 = 总耗时 − 子节点耗时),并在 OutputTextStrategyOutputJsonStrategy 中实现 printSortEntry() 输出方法,新增 CLI 选项 --mlir-timing-display=sort
likedislike
atomgit-bot
atomgit-bot
7月8日 评论:

代码审查

我已对所有 6 个变更文件进行了全面审查。以下是详细分析:


文件 1: bishengir/include/bishengir/Tools/Utils/Utils.h

  • 新增了 CompileTimingScopedCompileTimingContextExternalToolProfiler 结构体和 getCurrentCompileTimingScope() 函数声明
  • 前向声明 + 完整定义的顺序正确:ScopedCompileTimingContext 先被前向声明(第43行),被 CompileTimingunique_ptr 成员使用(第48行),然后在第56行给出完整定义。这是正确的 C++ 循环依赖处理模式。
  • 无问题。

文件 2: bishengir/lib/Tools/RetriablePassManager/RetriablePassManager.cpp

  • 新增了 #include "bishengir/Tools/Utils/Utils.h" 以使用 getCurrentCompileTimingScope()
  • runOnce 中添加了 timing scope 逻辑(第105-110行):通过 getCurrentCompileTimingScope() 检查 timing 是否启用,若启用则创建嵌套 scope 并调用 passManager.enableTiming()。scope 生命周期正确——在 passManager.run() 期间有效,函数返回时销毁。
  • BiShengIRPassManager 继承自 mlir::PassManagerenableTiming 方法可用。
  • 无问题。

文件 3: bishengir/lib/Tools/Utils/Utils.cpp

  • thread_local CompileTiming *currentCompileTiming = nullptr 用于线程安全的 TLS 指针,正确。
  • ScopedCompileTimingContext 构造/析构函数正确地保存/恢复 currentCompileTiming,遵循 RAII。
  • CompileTiming::CompileTiming() 在 timing 禁用时跳过 rootScope 创建,context 始终创建(允许后续 getCurrentCompileTimingScope() 返回 null 来跳过 timing)。
  • ExternalToolProfiler::run 正确检查 null timing scope。
  • execute()profilerName 的生命周期覆盖 ExternalToolProfiler::run 调用,StringRef 不会悬空。
  • 无问题。

文件 4: bishengir/lib/Tools/bishengir-compile/BiShengIRCompileMain.cpp

  • runBiShengIRPipeline 开头添加 CompileTiming timing;(第260行),为整个编译流程建立 timing 上下文。
  • CompileTiming 在所有返回路径上通过 RAII 正确销毁。
  • include 重排序仅为整理,无功能性影响。
  • 无问题。

文件 5: bishengir/tools/bishengir-compile/bishengir-compile.cpp

  • 新增 mlir::registerDefaultTimingManagerCLOptions(); 注册 --mlir-timing--mlir-timing-display 等 CLI 选项。
  • 与已有的 mlir::registerPassManagerCLOptions() 不冲突(分别管理不同的选项集)。
  • 无问题。

文件 6: build-tools/patches/llvm-project/0078-[Huawei][LLVM]-Enable-timing-sort.patch

  • 为 MLIR timing 系统新增 Sort 显示模式。
  • printHeader 签名变更为增加 bool printColumnHeader 参数,Sort 模式下抑制列头(由 printSortEntry 自行打印)。
  • OutputTextStrategyOutputJsonStrategyprintSortEntry 实现正确。
  • collectSortEntries 正确递归构建路径字符串,使用 / 分隔。
  • printAsSort 按 self wall time 降序排列。
  • JSON 输出的逗号处理正确:所有 sort entry + "Rest" 均带逗号,"Total" 不带,JSON 数组格式有效。
  • printListEntry("Rest", ...) 显式传递 lastEntry=false,语义与默认值一致,仅提高可读性。
  • getSelfTimeRecord() 中的 std::max(0.0, ...) 防止计时精度导致的负值。
  • 无问题。

总结

优先级 发现数量
P0 0
P1 0
P2 0
P3 0

总体风险判断:此变更安全,未发现任何 correctness、security、reliability 或 breaking change 问题。 代码遵循 RAII 原则,正确处理了 timing 未启用的边界情况,thread_local 指针使用正确,析构顺序保证了数据一致性。LLVM patch 的 sort 模式实现与现有的 list/tree 模式一致,JSON 和文本输出均正确。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
ascend-robot
ascend-robot成员
7月8日 评论:

libing-pipeline创建中,请等待……

likedislike
ascend-robotascend-robot成员
7月8日 添加了label:ascend-cla/yes
此处折叠了134条消息 查看更多
ascend-robotascend-robot成员
11 天前 添加了label:NPUIR-DT-RUNNING
ascend-robotascend-robot成员
11 天前 删除了label:NPUIR-DT-RUNNING
ascend-robotascend-robot成员
11 天前 添加了label:NPUIR-DT-SUCC
ascend-robot
ascend-robot成员
11 天前 评论:
流水线 PR-pipeline_npuir-smoke#1540 已完成
阶段 任务名 状态 详情
编译构建 Compile >>>
开发者测试 CVOps >>>
流水线 PR-pipeline_npuir-smoke >>>
likedislike
ascend-robotascend-robot成员
11 天前 合入了pull request,合并节点 SHA:9032061f74dfc84b79481f726a7ff2b7c9f8d909