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

    <tr>
        <th>Summary</th>
        <td>
            [llvm] Crash when running new PassManager on multiple modules
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          kalle-llvm
      </td>
    </tr>
</table>

<pre>
    I get a crash when I run a PassManager on multiple modules like this:

`    PassManager  PM;`
`    // initialization...`
`    PM.run(m1, ...);`
`    PM.run(m2, ...); // Crash`

The crash seems to be due to the ModuleInlinerWrapperPass modifying its internal pipeline in the run() function each time it is called.  In particular at line 1164 in Inliner.cpp:

`MPM.addPass(std::move(AfterCGMPM));`
`MRM.run(M, MAM);`

The second time around AfterCGMPM will contain a vector of null unique_ptr's that are pushed onto MPM -> crash.

Also, unless I misread the code, the run() function will push a pass onto MPM everytime it is called, so MPM will keep growing.

This on windows 10 using llvm 15.0.2, but this code seems to be the same in main.

(I'm assuming you're supposed to be able to reuse a PassManager like this, but I guess that is not explicitly stated anywhere.)

Simple reproducer:
[new_pm_crash-cpp.txt](https://github.com/llvm/llvm-project/files/9990762/new_pm_crash-cpp.txt)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyVVNtu4zYQ_RrpZRBBF9uyH_TgTbqFHwQEbYE-BrQ0tthQJMtLvO7Xd4ZO1nGyi6KCYV04PHPmzBnuzXjudnDEAAIGJ_wEpwk17MBFTZ8ehfe90OKIDoyGOaogrUKYzRgVelDyGSFM0mfNNisfsvLtf1UCXe-3w2OfNV9o4SYiq7_SD6SWQQol_xFBGl0Uxce4x74gRlm9nqusvgeOqDc_wLvG1Tdxb4nuucbrpvT_x4SvtXvE2UMwsEcYI_JToMU-VbvTSmp0fzphLToujWWQh7PUR5DBUxEBnRYKrLTIsfQl7b8wIiJwiHrg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