Is there any function pass that can potentially undo what debug compilation did?

For example, I have a function that was generated with -O0 -g, it is full of allocas intermediate stores into that allocas, loads from that and so on. Basically I want to strip my function from all of it as mush as it is possible.

Use case, I want to debug one specific function in my code (and everything that will be called from that function) so I want a full debug information and full debug support for that specific “code place” but I don’t mind all of the rest of the code to be undebuggable, but faster that it would be if it all was compiled with honest -O0 -g.

To answer this very specific line, you can use the mem2reg and SROA passes to turn them into normal SSA definitions and uses.

There is a much simpler solution: you can always compile different source files with different debug / optimization flags. For example, compile the source file that contains the function you want to debug with -O0 -g while rest of the projects is built with -O2-ish.

For small projects you can do this discriminations manually. For larger projects, you can use Clang with the CCC_OVERRIDE_OPTIONS environment variable, one of the most underrated features in Clang IMHO. Here are the steps (assuming you’re on Unix/Linux):

  1. Build the project as release mode
  2. Run $ touch file_to_debug.cpp to update the timestamp of that file. This makes most of the build systems (e.g. GNU Make / Ninja) thought that the file has changed since last build.
  3. Run $ env CCC_OVERRIDE_OPTIONS="+-g x-O1 x-O2 x-O3" make / ninja to rebuild

The string passed to CCC_OVERRIDE_OPTIONS is pretty self-explanatory: add -g and remove -O1/2/3 from the original compiler (driver) invocation. Eventually, only file_to_debug.cpp will be built with -g while rest of the project is built in release mode.

Note: IIRC this is a Clang-specific feature, correct me if I’m wrong.


You could also use the attribute optnone to set the optimization level for a particular function to zero - functions without this attribute will be built with the optimization level you request on the command line. Here is a example.

1 Like