[llvm-dev] Replicate Individual O3 optimizations

David Greene via llvm-dev llvm-dev at lists.llvm.org
Fri Oct 25 12:14:51 PDT 2019

Neil Nelson <nnelson at infowest.com> writes:

> clang is_sorted.cpp -S -emit-llvm -o is_sorted_.ll
> clang is_sorted.cpp -O0 -S -emit-llvm -o is_sorted_O0.ll
> clang is_sorted.cpp -O0 -Xclang -disable-llvm-passes -S -emit-llvm -o is_sorted_disable.ll
> No difference in the prior three ll files.
> clang is_sorted.cpp -O1 -S -emit-llvm -o is_sorted_O1.ll
> Many differences between is_sorted_O1.ll and is_sorted_.ll.

Sure.  One is optimized and the other is not.

> opt -O3 -S is_sorted_.ll -o is_sorted_optO3.ll
> clang is_sorted.cpp -mllvm -debug-pass=Arguments -O3 -S -emit-llvm -o is_sorted_O3arg.ll
> opt <optimization sequence obtained in prior step> -S is_sorted_.ll -o is_sorted_opt_parms.ll
> No difference between is_sorted_optO3.ll and is_sorted_opt_parms.ll, the last two opt runs.

Ok.  This isn't surprising to me.

> Many differences between is_sorted_O3arg.ll and is_sorted_opt_parms.ll, the last two runs,
> clang and opt.

I think the problem is that without an optimization argument (-O1, -O3,
etc.) clang sets the "optnone" attribute on all functions and opt will
refuse to optimize.  I think this is very unfortunate behavior.

> Conclusions:
> Given my current understanding, the ll files from the first three clang runs
> are before any optimizations. Those ll files are from the front-end phase (CFE).
> But this is a simple program and it may be that for a more complex program that
> the ll files could be different.
> Whether or not we use a -O3 optimization or use the parameters provided by clang for a
> -03 optimization, we obtain the same result.

Yep, in both cases opt is not doing any optimization.  :)

> The difference in question is why an opt run using the CFE ll before optimization
> obtains a different ll than a CFE run that includes optimization. That is, for this case,
> it is not the expansion of the -O3 parameters that is the difference.

I think it's the optnone attribute set by clang in the first three runs.

> Initially, it would be interesting to have an ll listing before optimization from the
> clang run that includes optimization to compare with the ll from the clang run without
> optimization.

Unfortunately I don't know of a great way to do that.  -mllvm
-print-before-all might be close but it will also dump out a ton of
stuff and not all dumps are complete (e.g. only dumps a Function rather
than the whole Module).


More information about the llvm-dev mailing list