LLVM 计时说明

获取 LLVM Pass 计时

当启用 NUMBA_LLVM_PASS_TIMINGS 或将 numba.config.LLVM_PASS_TIMINGS 设置为真值时,调度器将 LLVM pass 计时存储在调度器对象元数据中,键名为 llvm_pass_timings。计时信息包含每个 pass 所花费时间的详细信息。pass 计时也按其目的分组。例如,将有函数级预优化、模块级优化和目标代码生成的 pass 计时。

代码示例

来自 numba/tests/doc_examples/test_llvm_pass_timings.py 中的 test_pass_timings
 1import numba
 2
 3@numba.njit
 4def foo(n):
 5    c = 0
 6    for i in range(n):
 7        for j in range(i):
 8            c += j
 9    return c
10
11foo(10)
12md = foo.get_metadata(foo.signatures[0])
13print(md['llvm_pass_timings'])

示例输出

Printing pass timings for JITCodeLibrary('DocsLLVMPassTimings.test_pass_timings.<locals>.foo')
Total time: 0.0376
== #0 Function passes on '_ZN5numba5tests12doc_examples22test_llvm_pass_timings19DocsLLVMPassTimings17test_pass_timings12$3clocals$3e7foo$241Ex'
Percent: 4.8%
Total 0.0018s
Top timings:
  0.0015s ( 81.6%) SROA #3
  0.0002s (  9.3%) Early CSE #2
  0.0001s (  4.0%) Simplify the CFG #9
  0.0000s (  1.5%) Prune NRT refops #4
  0.0000s (  1.1%) Post-Dominator Tree Construction #5
== #1 Function passes on '_ZN7cpython5numba5tests12doc_examples22test_llvm_pass_timings19DocsLLVMPassTimings17test_pass_timings12$3clocals$3e7foo$241Ex'
Percent: 0.8%
Total 0.0003s
Top timings:
  0.0001s ( 30.4%) Simplify the CFG #10
  0.0001s ( 24.1%) Early CSE #3
  0.0001s ( 17.8%) SROA #4
  0.0000s (  8.8%) Prune NRT refops #5
  0.0000s (  5.6%) Post-Dominator Tree Construction #6
== #2 Function passes on 'cfunc._ZN5numba5tests12doc_examples22test_llvm_pass_timings19DocsLLVMPassTimings17test_pass_timings12$3clocals$3e7foo$241Ex'
Percent: 0.5%
Total 0.0002s
Top timings:
  0.0001s ( 27.7%) Early CSE #4
  0.0001s ( 26.8%) Simplify the CFG #11
  0.0000s ( 13.8%) Prune NRT refops #6
  0.0000s (  7.4%) Post-Dominator Tree Construction #7
  0.0000s (  6.7%) Dominator Tree Construction #29
== #3 Module passes (cheap optimization for refprune)
Percent: 3.7%
Total 0.0014s
Top timings:
  0.0007s ( 52.0%) Combine redundant instructions
  0.0001s (  5.4%) Function Integration/Inlining
  0.0001s (  4.9%) Prune NRT refops #2
  0.0001s (  4.8%) Natural Loop Information
  0.0001s (  4.6%) Post-Dominator Tree Construction #2
== #4 Module passes (full optimization)
Percent: 43.9%
Total 0.0165s
Top timings:
  0.0032s ( 19.5%) Combine redundant instructions #9
  0.0022s ( 13.5%) Combine redundant instructions #7
  0.0010s (  6.1%) Induction Variable Simplification
  0.0008s (  4.8%) Unroll loops #2
  0.0007s (  4.5%) Loop Vectorization
== #5 Finalize object
Percent: 46.3%
Total 0.0174s
Top timings:
  0.0060s ( 34.6%) X86 DAG->DAG Instruction Selection #2
  0.0019s ( 11.0%) Greedy Register Allocator #2
  0.0013s (  7.4%) Machine Instruction Scheduler #2
  0.0012s (  7.1%) Loop Strength Reduction
  0.0004s (  2.3%) Induction Variable Users

自定义分析的 API

可以获得比上述示例中摘要文本更多的详细信息。pass 计时存储在 numba.misc.llvm_pass_timings.PassTimingsCollection 中,该类包含用于访问每个 pass 的单独记录的方法。

numba.misc.llvm_pass_timings.PassTimingsCollection(name)

一个 pass 计时集合。

此类别实现了用于访问各个计时记录的 Sequence 协议。

__getitem__(i)

获取第 i 个计时记录。

返回
res: (name, timings)

一个包含两个字段的命名元组

  • name: str

  • timings: ProcessedPassTimings

__len__()

此集合的长度。

get_total_time()

计算所有包含计时记录的总时间之和。

返回
res: float or None

返回总秒数,如果没有记录计时则返回 None

list_longest_first()

按总时间持续时间的降序返回计时记录。

返回
res: List[ProcessedPassTimings]
summary(topn=5)

返回一个表示计时摘要的字符串。

参数
topn: 整型; 可选, 默认值=5。

这限制了要显示的最大项目数。此函数将显示 topn 个最耗时的 pass。

返回
res: str
另请参阅 ProcessedPassTimings.summary()
numba.misc.llvm_pass_timings.ProcessedPassTimings(raw_data)

一个用于处理来自 LLVM 的原始计时报告的类。

处理是惰性执行的,因此我们不会浪费时间处理未使用的计时信息。

get_raw_data()

返回原始字符串数据。

返回
res: str
get_total_time()

计算所有 pass 中花费的总时间。

返回
res: 浮点型
list_records()

获取计时报告的已处理数据。

返回
res: List[PassTimingRecord]
list_top(n)

返回前 n 个(按实际运行时间计算)最耗时的 pass。

参数
n: 整型

这限制了要显示的最大项目数。此函数将显示 n 个最耗时的 pass。

返回
res: List[PassTimingRecord]

按降序返回前 n 个最耗时的 pass。

summary(topn=5, indent=0)

返回一个总结计时信息的字符串。

参数
topn: 整型; 可选

这限制了要显示的最大项目数。此函数将显示 topn 个最耗时的 pass。

indent: 整型; 可选

设置缩进级别。默认为 0,表示无缩进。

返回
res: str
numba.misc.llvm_pass_timings.PassTimingRecord(user_time, user_percent, system_time, system_percent, user_system_time, user_system_percent, wall_time, wall_percent, pass_name, instruction)