Replicate Individual O3 optimizations

Hello,
I want to study the individual O3 optimizations. For this I am using following commands, but unable to replicate O3 behavior.

  1. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O1 -Xclang -disable-llvm-passes -emit-llvm -S vecsum.c -o vecsum-noopt.ll

  2. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O3 -mllvm -debug-pass=Arguments -emit-llvm -S vecsum.c

  3. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/opt <optimization sequence obtained in step 2> -S vecsum-noopt.ll -S -o o3-chk.ll

Why the IR obtained by above step i.e individual O3 sequences, is not same when O3 is passed?

Where I am doing mistake?

Please help.

Thank You
Regards

hameeza ahmed via llvm-dev <llvm-dev@lists.llvm.org> writes:

Hello,
I want to study the individual O3 optimizations. For this I am using
following commands, but unable to replicate O3 behavior.

1. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O1
-Xclang -disable-llvm-passes -emit-llvm -S vecsum.c -o vecsum-noopt.ll

2. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O3
-mllvm -debug-pass=Arguments -emit-llvm -S vecsum.c

3. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/opt
<optimization sequence obtained in step 2> -S vecsum-noopt.ll -S -o
o3-chk.ll

Why the IR obtained by above step i.e individual O3 sequences, is not same
when O3 is passed?

Where I am doing mistake?

I think you need to turn off LLVM optimizations when doing the
-emit-llvm dump. Something like this:

Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O3 \
  -mllvm -debug-pass=Arguments -Xclang -disable-llvm-optzns -emit-llvm \
  -S vecsum.c

Otherwise you are effectively running the O3 pipeline twice, as clang
will emit LLVM IR after optimization, not before (this confused me too
when I first tried it).

That said, I'm not sure you will get the same IR out of opt as with
clang -O3 even with the above. For example, clang sets
TargetTransformInfo for the pass pipeline and the detailed information
it uses may or may not be transmitted via the IR it dumps out. I have
not personally tried to do this kind of thing in a while.

                  -David

hameeza ahmed via llvm-dev <llvm-dev@lists.llvm.org> writes:

Hello,
I want to study the individual O3 optimizations. For this I am using
following commands, but unable to replicate O3 behavior.

  1. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O1
    -Xclang -disable-llvm-passes -emit-llvm -S vecsum.c -o vecsum-noopt.ll

  2. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O3
    -mllvm -debug-pass=Arguments -emit-llvm -S vecsum.c

  3. Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/opt
    <optimization sequence obtained in step 2> -S vecsum-noopt.ll -S -o
    o3-chk.ll

Why the IR obtained by above step i.e individual O3 sequences, is not same
when O3 is passed?

Where I am doing mistake?

If you could provide the full reproducer, it could help to debug this.

I think you need to turn off LLVM optimizations when doing the
-emit-llvm dump. Something like this:

Documents/clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04/bin/clang -O3
-mllvm -debug-pass=Arguments -Xclang -disable-llvm-optzns -emit-llvm
-S vecsum.c

Otherwise you are effectively running the O3 pipeline twice, as clang
will emit LLVM IR after optimization, not before (this confused me too
when I first tried it).

This is the common pitfall indeed!
I think they are doing it correctly in step 1 though by including: -Xclang -disable-llvm-passes.

That said, I’m not sure you will get the same IR out of opt as with
clang -O3 even with the above. For example, clang sets
TargetTransformInfo for the pass pipeline and the detailed information
it uses may or may not be transmitted via the IR it dumps out. I have
not personally tried to do this kind of thing in a while.

I struggled as well to setup TTI and TLI the same way clang does :frowning:
It’d be nice to revisit our PassManagerBuilder setup and the opt integration to provide reproducibility (maybe could be a starter project for someone?).

is_sorted.cpp
bool is_sorted(int *a, int n) {

  for (int i = 0; i < n - 1; i++)

if` `(a[i] > a[i + 1])` return false;` ```return` `true; }`

 How Clang Compiles a Function
 How LLVM Optimizes a Function
clang version 10.0.0, Xubuntu 19.04

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.

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.
Many differences between is_sorted_O3arg.ll and is_sorted_opt_parms.ll, the last two runs,
clang and opt.

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.

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.

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.

Neil Nelson


I run matrix multiplication code with both the approaches o3 at clang and o3 at opt. clang o3 is about 2.97x faster than opt o3.

Yes, this is another indication that there some processing or bridge in the clang -O3 compile not so far evidenced as well when compiling with clang to its IR before the optimization passes.

This may be an issue explained in a yet to be known documentation page. Or it may be a point at the moment overlooked by the well informed.

An issue being noted here but not well addressed is that a well stated design of LLVM with its front-ends and back ends is that the front-ends compile to an IR without optimization that LLVM uses for optimization and preparation for various back-ends. But that with clang -O3, given this evidence, we are not easily seeing how the division between the clang front end and LLVM works, though the assumed design suggests it should be quite easy.

We should be able to compile with clang to the IR before optimization and then apply the LLVM optimization separately to obtain the same final IR as a clang -O3 compile doing both of those. But we are not seeing that.

This also bears on the e2e thread in that this assumed division posits that the separate clang and LLVM debug sequences can provide a high reliability since the IR intermediate between the two is not expected to be that error prone. The errors are expected to be primarily either in clang in obtaining a correct IR or in opt (LLVM) in optimizing that IR for the back-end. But since we are not able to identify the IR between the two under clang -O3 it is a question as to what debug sequence would handle what we could not identify.

Neil Nelson

It’s ‘known’ (by some number of LLVM developers) that opt -O3 isn’t the same as clang -O3. It’d be nice if they were closer - patches welcome, etc, but it hasn’t been a priority for anyone. opt -O3 is rarely used - usually opt is used for testing specific optimizations.

Clang’s IR output will differ between -O0 and -O3 (even before running any LLVM optimizations) - things like lifetime intrinsics, etc, are emitted only with optimizations enabled, for instance.

If you want to reproduce clang’s -O3, best to use clang -O3 (with source code, or with LLVM IR generated from clang -O3 (so it has lifetime intrinsics, etc))

Neil Nelson <nnelson@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. :slight_smile:

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).

                      -David