作者suhorng ( )
看板CompilerDev
标题[讨论] 用 opt 时设定 pass 的组态参数
时间Mon Feb 21 12:18:23 2022
最近刚好用到 opt 观察个别的 pass 最佳化结果时遇到跟
#1XeRxLcp 类似的问题
有的最佳化用 O1 会出现, 但是直接执行对应的 pass 却没有效果.
後来刚好查到的讨论, 我觉得可能有关
(原本的问题连结:
┌─────────────────────────────────────┐
│ 文章代码(AID):
#1XeRxLcp (CompilerDev) [ptt.cc] [问题] 用 opt 重现 O3 │
│ 文章网址:
https://webptt.com/cn.aspx?n=bbs/CompilerDev/M.1637990101.A.9B3.html │
│ 这一篇文章值 88 Ptt币 │
└─────────────────────────────────────┘)
LLVM 用 O1 最佳化程式时, 个别的 pass 不是全部用预设的组态执行.
对於在特定位置执行的 pass, 有些会启用不同的变换选项.
例如在跑 SimplifyCFG 时, 这里会把 switch 转换成 lookup table
https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/llvm/lib/Passes/PassBuilder.cpp#L1267
L1258
// Now that we've formed fast to execute loop structures, we do further
// optimizations. These are run afterward as they might block doing complex
// analyses and transforms such as what are needed for loop vectorization.
// Cleanup after loop vectorization, etc. Simplification passes like CVP and
// GVN, loop transforms, and others have already run, so it's now better to
// convert to more optimized IR using more aggressive simplify CFG options.
// The extra sinking transform can create larger basic blocks, so do this
// before SLP vectorization.
FPM.addPass(SimplifyCFGPass(SimplifyCFGOptions()
.forwardSwitchCondToPhi(true)
.convertSwitchToLookupTable(true)
.needCanonicalLoops(false)
.hoistCommonInsts(true)
.sinkCommonInsts(true)));
想用 opt 重现这个效果的话, 不能执行预设的 -simplifycfg pass,
要手动调整参数. 把 O1 的 pass pipeline 印出来看的时候也很容易忽略这部分
opt --passes='simplifycfg<switch-to-lookup>' -S -o output.ll input.ll
类似的 pass 还有像执行 loop unswitching 的时候是不是 non-trivial 等等.
举例来说, non-trivial loop unswitching 的策略会复制整个回圈,
但最基本的 trivial 策略只会单独把特定 branch/if 提升到回圈外面.
像这里列出了 simplifycfg 的选项, 可以看到上面 forwardSwitchCondToPhi,
convertSwitchToLookupTable, hoistCommonInsts 等都可以手动调整.
有多个选项的话, 选项之间用分号区隔:
opt --passes='simplifycfg<switch-to-lookup;hoist-common-insts>' \
-S -o output.ll input.ll
https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/llvm/lib/Passes/PassRegistry.def#L355
这里则列出了 loop unswitching 的选项
https://github.com/llvm/llvm-project/blob/llvmorg-13.0.0/llvm/lib/Passes/PassRegistry.def#L448
另外谢谢板主在
#1XPc02WZ 分享 -disable-O0-optnone 的用法!
完全想不到可以控制这个
(这篇
┌─────────────────────────────────────┐
│ 文章代码(AID):
#1XPc02WZ (CompilerDev) [ptt.cc] Re: [问题] llvm opt工具? │
│ 文章网址:
https://webptt.com/cn.aspx?n=bbs/CompilerDev/M.1634099202.A.823.html │
│ 这一篇文章值 102 Ptt币 │
└─────────────────────────────────────┘)
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 162.237.29.174 (美国)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/CompilerDev/M.1645417109.A.537.html
1F:推 mshockwave: 推推 只可惜现在PassPlugin不能用这种方式传参数 02/22 14:52
2F:→ suhorng: 这就在我守备范围外了, 现在只是勉强用 CLI tool XD 02/24 12:04