<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/63788>63788</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Unable to replicate clang optimization pipeline using opt with LLVM 16
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lucasreis1
      </td>
    </tr>
</table>

<pre>
    I've been experimenting with optimization via Clang and I'm in need of a custom optimization pipeline for IR. I figured that I would start by replicating `clang -O3` using opt and remove passes I don't need. 

A few quick tests showed me that for a specific application, Clang produced a binary that is 10% faster than when using opt. I've tried mimicking everything Clang does up until codegen except for optimizations, which I apply at opt. The result is still visible.

Here's a summary of my pipeline:

```
clang -c -emit-llvm -O3 a.cpp
clang -c -emit-llvm -O3 b.cpp
clang -c -emit-llvm -O3 c.cpp
llvm-link a.bc b.bc c.bc -o from_clang.bc
```

```
clang -Xclang -mframe-pointer=all -Xclang -vectorize-loops -Xclang vectorize-slp  -Xclang -disableO0-optnone -c -emit-llvm a.bc
opt a.bc -passes="default<O3>" -o a.bc
clang -Xclang -mframe-pointer=all -Xclang -vectorize-loops -Xclang vectorize-slp  -Xclang -disableO0-optnone -c -emit-llvm b.cpp
opt b.bc -passes="default<O3>" -o b.bc
clang -Xclang -mframe-pointer=all -Xclang -vectorize-loops -Xclang vectorize-slp  -Xclang -disableO0-optnone -c -emit-llvm c.cpp
opt c.bc -passes="default<O3>" -o c.bc

llvm-link a.bc b.bc c.bc from_opt.bc
```

When generating binaries, `from_clang.bc` was 10% faster than `from_opt.bc`.

To my understanding, [Clang's optimization pipeline](https://github.com/llvm/llvm-project/blob/release/16.x/clang/lib/CodeGen/BackendUtil.cpp#L992C1-L992C1) is [identical to opt's](https://github.com/llvm/llvm-project/blob/release/16.x/llvm/lib/Passes/PassBuilder.cpp#L1207) when running the new pass manager.

So, is there anything I'm missing here? How come the results are so different between these two binaries with alleged identical optimization pipelines?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMls1u4zYQgJ-GvgwsSFQk2wcf8lN3DaRI0e62vRUUOZKmoUiVpOz1Pn1ByomTIki3QIEuYEgyOSS_-afwnjqDuGXVDavuFmIKvXVbPUnhHZIvFo1Vp-2e8dUBoUE0gJ9HdDSgCWQ6OFLowY6BBvoiAlkDBxJwq4XpQBgFceUAZMAgKrAtCJCTD3Z4vWikETUZhNY62P-UwR5a6iaHCkIvAuzhaCetwAfhAjQncDhqkiIxsDqX6cDlQ8nqHCYfR-0YEoHDwR4QRuE9etiDsobxVUhAGbD8juXX8_MaWjzCnxPJRwjogwff2yMqGHCmiHAC_IiSWpIgxjND3PH2rPTorJokKhDQkBHuNC8lD0XOeAWt8AFdHDRw7NFcaDM4mzk4iofSQPIxzuEB3Sn08XM-Q1n0MI0wmUAapFXYJcdIHGfIl7b1ke3Yk-xhn5BPIMJ83scewaGfdOLzgbSGA3lqNGYvDfMBHTK-8lH5aRiiUraF4fTsNlZev5RndX7-pb9n50hY4kBhqfVhiK4CkclxfF-k-WcReRGJY0tN5hFE1kho4kPGx9JC6-zwe9oja-SbmO-x_3Z-D60TAy5HSyagY-Wd0Poye0AZrKMvuNTWjv554jLu9QgXeUVeNBof8qUdg7EG_6aeeEZNwZw0meOYlXeMc4WtmHRg5e1DycrvGOdR08uq_xX-hesiffOV9M23QS9f08uvpJeX4Ho_IlM4xiR8Nxh_jQWiQ4NurnSpohCmlGZ1_jqm6xyO4q0y8yR5Pq7OXyX3RxszeTIKnQ_CKDJd2r66ScUm5f2btZpVd4yv-xBGH_Of7xjfdRT6qcmkHRjfReXPr-Xo7B8oA-O7RtuG8Z1DjcIj47uizj4zvpPzaTtNcfrWKvweDeO7GyEf0ahPgXTyCS_vNxt-WyznF-ObWLxYdUMq9iQpNAQbiSP5f8z4JJ0Qf5yjYf64mUgrdE-EBc9XkSxVeDcZE70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