Open dcy11011 opened 2 years ago
Hi @dcy11011, this is a good question. Unfortunately there isn't a way to directly convert what the InstructionCountOz
observation does to a list of actions that your agent can perform, and it may deviate slightly from the opt
tool. The best thing would be to step through this code:
and to make a list of the passes that are run and convert that into a list of actions. BTW, this would be a great feature to have in CompilerGym if you do this, patches welcome! 🙂
Cheers, Chris
Thanks a lot for your advice! I checked the code you mentioned and made a pass list to immitate what these llvm functions do. It performs much better that the one i get from opt --debug-pass=Arguments
but still can't produce exactly 1.0 reward.
I noticed that many passes need parameters and they are given different parameters in different places in populateFunctionPassManager
and populateModulePassManager
. However actions in CompilerGym can only be converted to passes built by default parameters. I guess these parameters may have some influence on the result of optimazation, which cause my actions can not get the same result as Oz
. I'm not sure if my guess is correct since now I only read the code and didn't debug CompilerGym to see the exact passes list generated. I'll do a more careful check on this.
Thanks a lot for your advice! I checked the code you mentioned and made a pass list to immitate what these llvm functions do. It performs much better that the one i get from
opt --debug-pass=Arguments
but still can't produce exactly 1.0 reward.
That's great! Could you share the list of actions here?
I noticed that many passes need parameters and they are given different parameters in different places in
populateFunctionPassManager
andpopulateModulePassManager
. However actions in CompilerGym can only be converted to passes built by default parameters.
Yes we do not yet support parameterizing passes. That would be an interesting extension, though would require some thought as to how to best expose that through the action space.
I guess these parameters may have some influence on the result of optimazation, which cause my actions can not get the same result as
Oz
. I'm not sure if my guess is correct since now I only read the code and didn't debug CompilerGym to see the exact passes list generated. I'll do a more careful check on this.
Keep us posted 🙂
Cheers, Chris
That's great! Could you share the list of actions here?
Sure, but I'm sorry that I found I mistakenly took the result of my experimental removal of loop-rotate
as the result of the original list of actions. I tested the new action list (which obtained by reading the code) again and found it is actually very similar to the previous one (which obtained by opt
tool in my first post). Its optimization effect is also similar to previous one (I tested it on first 50 benchmark of csmith dataset, and geometry mean of IrInstructionCountOz/IrInstructionCount
is 0.804 on my new list, compare to 0.808 on the previous one). Here's the new action list:
-ee-instrument -simplifycfg -sroa -early-cse -lower-expect
-forceattrs -inferattrs -ipsccp -called-value-propagation -attributor -globalopt -mem2reg -deadargelim -instcombine -simplifycfg -prune-eh -inline -functionattrs -sroa -early-cse -speculative-execution -jump-threading -correlated-propagation -simplifycfg -instcombine -tailcallelim -simplifycfg -reassociate -loop-rotate -licm -loop-unswitch -simplifycfg -instcombine -indvars -loop-idiom -loop-deletion -loop-unroll -mldst-motion -gvn -memcpyopt -sccp -bdce -instcombine -jump-threading -correlated-propagation -dse -licm -adce -simplifycfg -instcombine -barrier -elim-avail-extern -rpo-functionattrs -globalopt -globaldce -float2int -lower-constant-intrinsics -loop-rotate -loop-distribute -loop-vectorize -loop-load-elim -simplifycfg -loop-unroll -instcombine -licm -alignment-from-assumptions -strip-dead-prototypes -globaldce -constmerge -loop-sink -instsimplify -div-rem-pairs -simplifycfg
The first line correspond to passes built by populateFunctionPassManager
, and the second correspond to populateModulePassManager
. I removed all the analysis passes and utility passes because they cannot be converted into actions and seem to be generated automatically when the transform passes is built. Compre to the passes given by opt
(removed analysis&utility passes), it has no loop-simplify
and lcssa
and several fewer instcombine
and replaced -early-cse-memssa
with -early-csa
.
(I'm still not sure if this result is correct since it all comes from code reading)
❓ Questions and Help
I'm trying to do some imitation learning with CompilerGym's llvm envornment. I want to get the passes taken by optlevel -Oz in llvm, then input them to CompilerGym, and hopefully on all benchmarks this can always produce 1.0 reward on reward space
IrInstructionCountOz
so that my RL algorithm can learn from this action sequence. I try to get the passes of -Oz level by:which produce:
I extract the passes allowed in llvm-v0's action space in the above output and execute them step by step in CompilerGym. However on almost every benchmark this can not produce the same instruction count as -Oz. I also tried to pass these commands to opt tool, and it didn't work same as -Oz either. As a beginnner learning LLVM, I wonder if it is possible to achieve the same effect as -Oz by passing a series of transform passes to opt tool of llvm. If it is possible, how to do it with CompilerGym to get a series of actions that always produce exactly 1.0 reward?
Besides, I found that if I write the bitcode of a benchmark to file, then optimize it using -Oz and read it back, I will get a different instruction count compared to -Oz baseline computed by CompilerGym. Here's my test code:
The result is:
Why would this happen?
I noticed that CompilerGym seems to use llvm source code instead of calling opt tool to run the baseline. Will these two methods produce different results?
Additional Context
my llvm version: 10.0.0 my CompilerGym version: 0.2.4