[討論] 用 opt 時設定 pass 的組態參數

看板CompilerDev作者 ( )時間2年前 (2022/02/21 12:18), 編輯推噓1(101)
留言2則, 2人參與, 2年前最新討論串1/1
最近剛好用到 opt 觀察個別的 pass 最佳化結果時遇到跟 #1XeRxLcp 類似的問題 有的最佳化用 O1 會出現, 但是直接執行對應的 pass 卻沒有效果. 後來剛好查到的討論, 我覺得可能有關 (原本的問題連結: ┌─────────────────────────────────────┐ │ 文章代碼(AID): #1XeRxLcp (CompilerDev) [ptt.cc] [問題] 用 opt 重現 O3 │ │ 文章網址: https://www.ptt.cc/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://www.ptt.cc/bbs/CompilerDev/M.1634099202.A.823.html │ │ 這一篇文章值 102 Ptt幣 │ └─────────────────────────────────────┘) -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 162.237.29.174 (美國) ※ 文章網址: https://www.ptt.cc/bbs/CompilerDev/M.1645417109.A.537.html

02/22 14:52, 2年前 , 1F
推推 只可惜現在PassPlugin不能用這種方式傳參數
02/22 14:52, 1F

02/24 12:04, 2年前 , 2F
這就在我守備範圍外了, 現在只是勉強用 CLI tool XD
02/24 12:04, 2F
文章代碼(AID): #1Y4nALKt (CompilerDev)