Restricting all optimizations to frontend

Hi all,

If I understand it correctly, code compiled with optimization following these steps:

  1. Generating IR and optimizing it, e.g., dead code elimination
  2. Generating machine-specific code
  3. Performing target-specific optimizations, e.g., choosing specific strcmp implementation
  4. Link time optimization

Now, I only want optimizations in step 1, but not all other steps. Is that possible by feeding some compilation/linking flags to clang?

FYI, I’m doing some instrumentation on LLVM IR level, and eventually compile and run the binary. I want the binary to be as similar as possible to IR as possible, and don’t want any optimization happens after IR compilation to mess up this structure.

You can try:

clang foo.c -emit-llvm -S -o foo.ll -O3

emit-llvm will emit LLVM IR instead of an executable. O3 will give you optimized IR. foo.ll is a text file containing LLVM IR.

Thanks for you reply!

In fact now I am dealing with optimized IR compiled with -O3. But I’m not sure whether some optimization during codegen and backend would restructure the code (e.g. function inlining), and I am looking for a way to check/disable it.

You can’t. Function inlining happens at LLVM IR. The backend might restructure/optimize loops. You can’t disable these optimizations.

Hello,

I just read about optnone can be used as a function attribute. The doc says:

The optnone attribute suppresses essentially all optimizations on a function or method

So would it be possible to first optmize in frontend and then give specific functions optnone? Will backend and codegen respect this attribute? The doc is unclear about this part though.

The frontend doesn’t really do optimizations at all. I believe backend and codegen mostly respect optnone, although I remember there were some things that couldn’t be turned off (caused correctness problems). The doc is unclear because there isn’t a clear dividing line between things that respect it and things that do not.

Thank you! That explains a lot.