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

    <tr>
        <th>Summary</th>
        <td>
            Opt crashes if not given stdin input
        </td>
    </tr>

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

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

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

<pre>
    Running opt in a mode where it accepts its input via stdin, and then not providing any input results in a crash. An example of when this could happen is by piping in the IR generated with an optimization pass, however said pass errors out.
This is a regression starting from llvm11. llvm10 fails gracefully in this case. This crash was found while using Ubuntu 22.04, and has been reproduced using llvm17.0.1.

llvm11:
```
$ build/bin/opt -S llvm/test/Transforms/SampleProfile/section-accurate-samplepgo.ll -passes=sample-profile -sample-profile-file=nonexistant.prof | build/bin/opt -S -codegenprepare
error: nonexistant.prof: Could not open profile: No such file or directory
LLVM ERROR: Trying to construct TargetPassConfig without a target machine. Scheduling a CodeGen pass without a target triple set?
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: build/bin/opt -S -codegenprepare
 #0 0x00005555590214e4 llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/gbmatt/llvm-project/llvm/lib/Support/Unix/Signals.inc:564:22
 #1 0x000055555902187c PrintStackTraceSignalHandler(void*) /home/gbmatt/llvm-project/llvm/lib/Support/Unix/Signals.inc:625:1
 #2 0x000055555901f2a3 llvm::sys::RunSignalHandlers() /home/gbmatt/llvm-project/llvm/lib/Support/Signals.cpp:68:20
 #3 0x0000555559020e26 SignalHandler(int) /home/gbmatt/llvm-project/llvm/lib/Support/Unix/Signals.inc:406:1
 #4 0x00007ffff7fae420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #5 0x00007ffff7a4b00b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007ffff7a2a859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #7 0x0000555558f18c0e llvm::install_bad_alloc_error_handler(void (*)(void*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool), void*) /home/gbmatt/llvm-project/llvm/lib/Support/ErrorHandling.cpp:130:61
 #8 0x0000555558f189bb llvm::report_fatal_error(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, bool) /home/gbmatt/llvm-project/llvm/lib/Support/ErrorHandling.cpp:86:77
 #9 0x000055555822783b /home/gbmatt/llvm-project/llvm/lib/CodeGen/TargetPassConfig.cpp:460:21
#10 0x000055555822d07b llvm::Pass* llvm::callDefaultCtor<llvm::TargetPassConfig>() /home/gbmatt/llvm-project/llvm/include/llvm/PassSupport.h:80:76
#11 0x00005555585363ee llvm::PassInfo::createPass() const /home/gbmatt/llvm-project/llvm/include/llvm/PassInfo.h:102:3
#12 0x000055555852e674 llvm::PMTopLevelManager::schedulePass(llvm::Pass*) /home/gbmatt/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:789:38
#13 0x0000555558536f12 llvm::legacy::PassManagerImpl::add(llvm::Pass*) /home/gbmatt/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:560:3
#14 0x0000555558532fdb llvm::legacy::PassManager::add(llvm::Pass*) /home/gbmatt/llvm-project/llvm/lib/IR/LegacyPassManager.cpp:1732:1
#15 0x0000555556147191 OptCustomPassManager::add(llvm::Pass*) /home/gbmatt/llvm-project/llvm/tools/opt/opt.cpp:340:17
#16 0x000055555613efa4 addPass(llvm::legacy::PassManagerBase&, llvm::Pass*) /home/gbmatt/llvm-project/llvm/tools/opt/opt.cpp:375:17
#17 0x0000555556141a22 main /home/gbmatt/llvm-project/llvm/tools/opt/opt.cpp:893:22
#18 0x00007ffff7a2c083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#19 0x000055555610d8ee _start (build/bin/opt+0xbb98ee)
Aborted (core dumped)
```

llvm10:
```
$ build/bin/opt -S llvm/test/Transforms/SampleProfile/section-accurate-samplepgo.ll -passes=sample-profile -sample-profile-file=nonexistant.prof | build/bin/opt -S -codegenprepare
error: nonexistant.prof: Could not open profile: No such file or directory
; ModuleID = '<stdin>'
source_filename = "<stdin>"
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsmN9zozgSx_8a-aXLFEgY8IMfHCe-S1Wyk0qy9-oSogHdYomSRBLPX38lQeIf2avb2Zmde9nUjG2Q6P7o2y2pBbdWNgpxRRZXZHE944NrtVk15T137pdZqavD6nFQSqoGdO9AKuCw1xXCa4sGQTrgQmDvLEj_X_WDgxfJwbpKKkI3wFUFrkUFSjvojX6RlTfG1WHqbdAOXXgWOAjDbRvBWgG-8X3fIejau1LgWmlB6KGroOV9jwqkhfIAvey9Pel7INw-QoMKDXdYwat0LXDlweVefuVOagU9t9ZztfoVX9CA5bIKNwGN0caCHlxE4msSr5-9S2mBg8HGoLX-eeu4cd5jbfQeuu5lnyTR-B1DzWVnoTFcYD103WHE8uDcYgTBYBgivHILtR5UBa-t7BAG623-Wg7KDUBpFKfv4rXcQomowGBvdDUIrKbewWkexVEyAY-fIxNh0yXJ4unfeElTKAfZVYRuSx-irY_r_ClYI3Tr0DpCt8-GK1trs7eEbp9CKB6MrmWHhG4tCq_lnAsxeKnnNnToGx11Hcy9mmgJux5vz_vxQZifX8-DNXattMI3aR1XLvJNQPLN7yPOha6wQdUb7LnBcUAhbISt4dKOv7cJCeNTT_uUmTz7ll802EG0EMi0gUoaFE6bw2j17u5f93Dz-Pjl0Xd-NgcvuNMgtLLODMLBMzcNugdu7UarWjYh3fTggIMLTbDnopUKI3gSLVZDF_IeNrrCf-CYiJ-fcUb6rLfoCNuOKA93N-unG7BDuffTDcqh8bmgjfNArXO99cGmW0K3jXTtUEZC-1BOEfVfXvN_o_CRldYO6KMasksq0Q0VhskzZm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