Repository: rust
Issue: Profile-Guided Optimization fail to compile with segmentation fault
Labels: A-LLVM,T-compiler,C-bug

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

I'm trying [profile-guided optimization](https://doc.rust-lang.org/rustc/profile-guided-optimization.html), but it failed to compile with segmentation fault.

```
RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata"
cargo build --release --target=x86_64-unknown-linux-gnu

Compiling kms v0.1.0 (kms_standalone)

error: could not compile `kms`

Caused by:
  process didn't exit successfully: `rustc --crate-name kms --edition=2021 ... `
  (signal: 7, SIGBUS: access to undefined memory)
```

Sometimes `(signal: 11, SIGSEGV: invalid memory reference)`.

I also tried a hello-world project, and it works fine.

My project: https://github.com/cita-cloud/kms_standalone

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.58.0-nightly (4e0d3973f 2021-10-27)
binary: rustc
commit-hash: 4e0d3973fafdfb1c51011bc74e44257b5e3863f1
commit-date: 2021-10-27
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Backtrace</summary>
<p>

```
RUST_BACKTRACE=1 RUSTFLAGS="-Cprofile-use=/tmp/pgo-data/merged.profdata" cargo build --release --target=x86_64-unknown-linux-gnu

   Compiling kms v0.1.0 (/home/user/kms_standalone)
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x4c7383)[0x7fe6f89cd383]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x141f0)[0x7fe6f81771f0]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZNK4llvm7Mangler17getNameWithPrefixERNS_15SmallVectorImplIcEEPKNS_11GlobalValueEb+0x2e3)[0x7fe6f56b2dc3]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZNK4llvm13TargetMachine9getSymbolEPKNS_11GlobalValueE+0x6f)[0x7fe6f56b233f]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZNK4llvm24TargetLoweringObjectFile21emitCGProfileMetadataERNS_10MCStreamerERNS_6ModuleE+0x164)[0x7fe6f6290524]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZNK4llvm27TargetLoweringObjectFileELF18emitModuleMetadataERNS_10MCStreamerERNS_6ModuleE+0xd5)[0x7fe6f628ff75]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZN4llvm10AsmPrinter14doFinalizationERNS_6ModuleE+0x25d)[0x7fe6f61da04d]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZN4llvm13FPPassManager14doFinalizationERNS_6ModuleE+0x2d)[0x7fe6f61f6a0d]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/../lib/libLLVM-13-rust-1.58.0-nightly.so(_ZN4llvm6legacy15PassManagerImpl3runERNS_6ModuleE+0x3c5)[0x7fe6f61f60e5]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x2444ca4)[0x7fe6fa94aca4]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x243fa0f)[0x7fe6fa945a0f]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x2442600)[0x7fe6fa948600]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x23d51ab)[0x7fe6fa8db1ab]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x23cf8ed)[0x7fe6fa8d58ed]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x24038f0)[0x7fe6fa9098f0]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/librustc_driver-25ea0f19be037cbe.so(+0x24285ac)[0x7fe6fa92e5ac]
/home/user/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/../lib/libstd-8adcca4f1427867b.so(rust_metadata_std_fcea40badc263c8f+0xa94c3)[0x7fe6f82404c3]
/lib/x86_64-linux-gnu/libpthread.so.0(+0x9450)[0x7fe6f816c450]
/lib/x86_64-linux-gnu/libc.so.6(clone+0x43)[0x7fe6f807ad53]
error: could not compile `kms`

Caused by:
  process didn't exit successfully: `rustc --crate-name kms --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C opt-level=3 -C panic=abort -C lto -C metadata=5117a81938dc7a68 -C extra-filename=-5117a81938dc7a68 --out-dir /home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps -L dependency=/home/user/kms_standalone/target/release/deps --extern anyhow=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libanyhow-ba82fabb03a89ae9.rlib --extern clap=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libclap-301235565c36679a.rlib --extern efficient_sm2=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libefficient_sm2-12c48832db1cef3b.rlib --extern hex=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libhex-f5b979ff7f033c73.rlib --extern jemallocator_global=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libjemallocator_global-4fe19d1293a51495.rlib --extern libsm=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/liblibsm-9c872b881d08502d.rlib --extern lru=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/liblru-e67c713f2a010349.rlib --extern parking_lot=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libparking_lot-fa67ff07658cabc7.rlib --extern prost=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libprost-191245b8561b58f7.rlib --extern rand=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/librand-7a6361873a8c2f60.rlib --extern secrecy=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libsecrecy-d5955e43839579ac.rlib --extern serde=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libserde-eb01c2aa8aabd964.rlib --extern sqlx=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libsqlx-98d357f15c1c6a01.rlib --extern thiserror=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libthiserror-27a3e6dc89560ed9.rlib --extern tokio=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtokio-59441228a37ce303.rlib --extern toml=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtoml-d3591f74315998b5.rlib --extern tonic=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtonic-8d404e44f672e7d0.rlib --extern tracing=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtracing-dd8d5593a2e7f02b.rlib --extern tracing_appender=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtracing_appender-90d6db31334c96ee.rlib --extern tracing_subscriber=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/libtracing_subscriber-c6dbb15fb600395e.rlib --extern url=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/deps/liburl-886159ac8fb7d14c.rlib -Cprofile-use=/tmp/pgo-data/merged.profdata -L native=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/build/jemalloc-sys-561d18810a756faf/out/build/lib -L native=/home/user/kms_standalone/target/x86_64-unknown-linux-gnu/release/build/ring-c7ba6bf9e094ac7e/out`
  (signal: 11, SIGSEGV: invalid memory reference)
```
</p>
</details>