作者shane87123 (阳光大肥宅)
看板CompilerDev
标题[问题] frame-pointer 与 performance
时间Mon Nov 8 22:11:06 2021
大家好,
最近发现 llvm ir 有一个 attribute 叫做 frame-pointer,
它会影响 performance
目前 O3 预设是 none 的,
而如果是用 clang -emit-llvm -Xclang -disable-O0-optnone
这样的方式取得没有优化过的 llvm ir,则会是 all
据我说知,他会消除 frame pointer 的储存(如果是 none 的话),
"理论上"会让程式的 performance 好一点,毕竟会减少 register 的使用
经过测试,确实如果同是使用 O3 sequence,frame-pointer=none performance确实比较好
但是!!
我用我自己的优化顺序,
frame-pointer=none 得到的 runtime = 8 sec 左右
frame-pointer=all 得到的 runtime = 3.8 sec
差非常多!
然後我把他们转成 Assembly code,确实不太一样,
但 none 程式码比较短,而且减少很多存取
却让 performance 更差劲
可以明白指令的多寡与 performance 无关,
但据我说知,frame-pointer 不去储存与使用,应该会更快吧?
甚至我自己有些 IR 从 all 改 none 会更好
唯独某几个 IR code 会更差。
我测试的 source code 的是 insertion sort
https://imgur.com/nqexaZb
https://imgur.com/hKigtrh
https://imgur.com/FR3qETS
https://imgur.com/5119ek8
https://imgur.com/g3RHx98
这些是 Assembly code 的差异,
感觉与 insertion sort 本身的逻辑无关
补上 perf 之後的结果:
frame-pointer=all
Performance counter stats for './190_all' (10 runs):
142666 cache-misses # 0.020 % of all cache refs ( +- 5.01% )
698701320 cache-references ( +- 0.71% )
234781 branch-misses ( +- 0.44% )
13059296783 cycles ( +- 0.16% )
59991967735 instructions # 4.59 insn per cycle ( +- 0.05% )
3.417880975 seconds time elapsed ( +- 0.26% )
frame-pointer=none
Performance counter stats for './190_none' (10 runs):
352932 cache-misses # 0.046 % of all cache refs ( +- 2.58% )
770977710 cache-references ( +- 0.81% )
260282 branch-misses ( +- 0.33% )
30052057516 cycles ( +- 0.05% )
60037013675 instructions # 2.00 insn per cycle ( +- 0.05% )
7.921856465 seconds time elapsed ( +- 0.05% )
看起来branch-misses 高大概10%
Insn per cycle 直接慢一半..
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.43.59.118 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/CompilerDev/M.1636380668.A.B00.html
1F:推 sonicyang: runetime 比较短performance 比较差?我漏看了什麽吗 ? 11/09 00:50
抱歉,我秒数弄错了
2F:→ Lipraxde: Assembly 都在你眼前了,再加点油分析一下。 11/09 01:55
3F:→ Lipraxde: LLVM 可以在每个 pass 跑完後 dump IR / machine IR,11/09 01:55
4F:→ Lipraxde: 对了解优化、为什麽生出这样的 pattern 很有帮助。11/09 01:55
5F:→ Lipraxde: 另外就是不太确定你有没有读过 System V ABI,如果要做11/09 01:55
6F:→ Lipraxde: 的这麽深的优化的话熟悉 ABI 是很重要的!11/09 01:55
7F:→ Lipraxde: 啊...好像讲了些不太相干的东西,回到你的问题,虽然给11/09 02:04
8F:→ Lipraxde: 的资讯有点少,不过从执行时间的差距、codegen 结果的11/09 02:04
9F:→ Lipraxde: 差异来看,我会觉得有可能是由於 cache 所造成的。11/09 02:04
刚刚使用 Linux 的工具 perf 分析两者差异,
在 cache misses, cache reference 上没有差异,
但在 instrcutions per cycle 上有着显着的差异:
frame-pointer=all 的 有 4.56 instruction num per cycles,
frame-pointer=none 的则只有 1.99 instruction num per cycles.
※ 编辑: shane87123 (114.43.59.118 台湾), 11/09/2021 02:17:11
※ 编辑: shane87123 (114.43.59.118 台湾), 11/09/2021 02:32:19
10F:→ Lipraxde: Branch miss 呢?11/09 09:19
补上了!谢谢大大
※ 编辑: shane87123 (101.12.89.21 台湾), 11/09/2021 13:28:27
※ 编辑: shane87123 (101.12.89.21 台湾), 11/09/2021 13:28:51
11F:→ Lipraxde: 有开 frame-pointer 的版本因为有多的 push、move 个关 11/09 16:50
12F:→ Lipraxde: 系,因此不建议直接对 instruction num per cycles 下 11/09 16:50
13F:→ Lipraxde: 定论。然後我注意到一个地方,all、none 的 instructio 11/09 16:50
14F:→ Lipraxde: n 数量是差不多的,可以看看是为什麽 :) 11/09 16:50