facebookresearch / CompilerGym

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

How to add additional optimization options to the action space of llvm? #743

Closed lxw-1006 closed 2 years ago

lxw-1006 commented 2 years ago

The action space of llvm used has 124 optimization options.However, additional options are required in our research,so how should we add these optimization options to the behavior space? Is it necessary to modify the source code of compiler-gym in order to add additional optimization options?

ChrisCummins commented 2 years ago

Hi @lxw-1006, good question. It depends on what you want the extra actions to do. Are the extra actions implemented in LLVM, or are they separate tools?

If the extra action you want to add can be implemented in Python, then you could consider using a wrapper class to extend the action space and handle the extra action within Python.

If you want to interact with LLVM, then you will have to modify the the source code of CompilerGym. Here is the function which applies an action in the LLVM environment:

https://github.com/facebookresearch/CompilerGym/blob/b58e83bb8fa7edad29af398bab44c28a2673b8cc/compiler_gym/envs/llvm/service/LlvmSession.cc#L116-L136

Note there are two generated files:

https://github.com/facebookresearch/CompilerGym/blob/b58e83bb8fa7edad29af398bab44c28a2673b8cc/compiler_gym/envs/llvm/service/passes/10.0.0/ActionEnum.h#L9

https://github.com/facebookresearch/CompilerGym/blob/b58e83bb8fa7edad29af398bab44c28a2673b8cc/compiler_gym/envs/llvm/service/passes/10.0.0/ActionSwitch.h#L9

which list all of the 124 passes, and how to apply them. You could extend those?

Cheers, Chris

ChrisCummins commented 2 years ago

Closing this as I have answered the question but please feel free to continue the discussion if you have follow up questions 🙂

lxw-1006 commented 2 years ago

First of all, thank you very much for your response. Secondly, the additional options we want to add can be regarded as a pass of llvm, such as different options of ollvm. Because our goal is to get the optimization sequence with the largest code difference compared with the o0 optimization sequence through compiler gym. So we think we need to modify the source code of compiler gym. Not only do we need to add additional options, we may also need to expand the additional reward section.

ChrisCummins commented 2 years ago

That sounds really interesting. In that case, take a look at the code pointers above for expanding the action space.

To add a new reward space you expose this through observations (i.e. the instruction count reward is just the change in instruction count observation from one step to the next). Here's your starting point for adding a new observation space:

https://github.com/facebookresearch/CompilerGym/blob/dd66b2c0fa3970205793086aaa18f79091b858a0/compiler_gym/envs/llvm/service/ObservationSpaces.h#L13-L24

I'd be interested to hear how you get on. Keep me posted! 🙂

Cheers, Chris

lxw-1006 commented 1 year ago

Hello, I'm sorry to bother you. We may need your help.We have new questions about expanding the action space of the llvm. We added 9 self-made passes on the basis of 124 passes. However, we found that the number of passes is still 129. Is there a limit on the size of the action space of the llvm, which cannot exceed 129 options?Looking forward to your reply, thank you!

ChrisCummins commented 1 year ago

Hey! Sorry for the slow response. It may be easier to have a chat to go through the changes you want to make. Shoot me an email at chrisc.101@gmail.com and we can figure out a time to hop on Zoom.

Cheers, Chris