facebookresearch / CompilerGym

Reinforcement learning environments for compiler and program optimization tasks
https://compilergym.ai/
MIT License
879 stars 123 forks source link

How to optimize a real project? #820

Open zcfh opened 9 months ago

zcfh commented 9 months ago

❓ Questions and Help

How to optimize a real project? I tested it on bzip2. Steps I performed:

  1. Modify the original compilation command and modify the command to compile the target file: Add -Xclang -disable-O0-optnone -Xclang -disable-llvm-passes -O0 -emit-llvm to generate an origin.bc
  2. Use the ppo model of Ranking 1 to optimize the bc file and obtain ppo.bc
  3. Then pass llc -O0 --filetype=obj ppo.bc -o opt.o to convert the optimized bc file into a target file.
  4. Finally, the target file is processed and the resulting binary is linked (ppo_bzip2 is used later)

Compress a small file clang O3 built binary, took 3.70 s, ppo_bzip2 takes 8.9s

Here I tried llc -O3 again, and the performance was still slightly worse than the binary compiled by O3, and it still took 3.75s.

ChrisCummins commented 8 months ago

Hi @zcfh, sorry for the slow reply.

Your steps look correct. Are you comparing the version that is built with a single end-to-end clang invocation against the version in which you first lower to IR and then lower from IR to executable? Note that the exact configuration of optimizations and options of clang -O3 foo.c -o a.out cannot be replicated by a standalone opt invocation. That is a known limitation of LLVM and you can find some discussion of it on the LLVM discourse.

Cheers, Chris