Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Regression] LLVM asserts "conflicting locations for variable" since 1.82 #131944

Closed
cuviper opened this issue Oct 19, 2024 · 14 comments · Fixed by #132613
Closed

[Regression] LLVM asserts "conflicting locations for variable" since 1.82 #131944

cuviper opened this issue Oct 19, 2024 · 14 comments · Fixed by #132613
Assignees
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. P-critical Critical priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@cuviper
Copy link
Member

cuviper commented Oct 19, 2024

Code

I tried this code: cargo itself, building release + debug=2
(multiple versions, but at the time of this report I have cargo cf53cc54bb593b5ec3dc2be4b1702f50c36d24d5)

I expected to see this happen: successful compilation

Instead, this happened:

In Fedora 39 builds (with its LLVM 17), rustc hits a libstdc++ assertion via LLVM:

/usr/bin/../lib/gcc/x86_64-redhat-linux/13/../../../../include/c++/13/optional:479: _Tp &std::_Optional_base_impl<llvm::DIExpression::FragmentInfo, std::_Optional_base<llvm::DIExpression::FragmentInfo>>::_M_get() [_Tp = llvm::DIExpression::FragmentInfo, _Dp = std::_Optional_base<llvm::DIExpression::FragmentInfo>]: Assertion 'this->_M_is_engaged()' failed.
rustc exited with signal: 6 (SIGABRT) (core dumped)

While investigating, I found that llc also crashes with the extracted IR:
https://bugzilla.redhat.com/show_bug.cgi?id=2226564#c10

In local builds with rust's LLVM 19 and assertions enabled, and also with CI "alt" builds, LLVM asserts "conflicting locations for variable":

.../cargo$ CARGO_PROFILE_RELEASE_DEBUG=2 cargo +e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt build --release
[...]
rustc: /checkout/src/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp:303: void llvm::Loc::MMI::addFrameIndexExpr(const DIExpression *, int): Assertion `(FrameIndexExprs.size() == 1 || llvm::all_of(FrameIndexExprs, [](const FrameIndexExpr &FIE) { return FIE.Expr && FIE.Expr->isFragment(); })) && "conflicting locations for variable"' failed.
error: could not compile `cargo` (lib)

Version it worked on

It most recently worked on: Rust 1.81.0

Version with regression

Rust 1.82.0, but at first it only crashed in my Fedora 39 build with LLVM 17. However, using the bundled rust-llvm with assertions enabled does trigger a debuginfo assertion.

Current nightly (e92993d) in alt mode reproduces this as well.

i.e. rustup-toolchain-install-master --alt e92993dbb43f0a5d17fe56e2d82f90435d6521c8

$ rustc +e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt -Vv
rustc 1.84.0-nightly (e92993dbb 2024-10-18)
binary: rustc
commit-hash: e92993dbb43f0a5d17fe56e2d82f90435d6521c8
commit-date: 2024-10-18
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.1

Backtrace

(gdb) backtrace

#0  __pthread_kill_implementation (threadid=<optimized out>, signo=signo@entry=6, no_tid=no_tid@entry=0) at pthread_kill.c:44
#1  0x00007f70eb682793 in __pthread_kill_internal (threadid=<optimized out>, signo=6) at pthread_kill.c:78
#2  0x00007f70eb629d0e in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26
#3  0x00007f70eb611942 in __GI_abort () at abort.c:79
#4  0x00007f70eb61185e in __assert_fail_base (fmt=0x7f70eb7c5cb0 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n",
    assertion=assertion@entry=0x7f70db9545d4 <.L.str.70> "(FrameIndexExprs.size() == 1 || llvm::all_of(FrameIndexExprs, [](const FrameIndexExpr &FIE) { return FIE.Expr && FIE.Expr->isFragment(); })) && \"conflicting locations for variable\"",
    file=file@entry=0x7f70db8eb1f8 <str.67.llvm> "/checkout/src/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp", line=line@entry=303,
    function=function@entry=0x7f70db233d86 <.L__PRETTY_FUNCTION__._ZN4llvm3Loc3MMI17addFrameIndexExprEPKNS_12DIExpressionEi> "void llvm::Loc::MMI::addFrameIndexExpr(const DIExpression *, int)") at assert.c:94
#5  0x00007f70eb621e47 in __assert_fail (
    assertion=0x7f70db9545d4 <.L.str.70> "(FrameIndexExprs.size() == 1 || llvm::all_of(FrameIndexExprs, [](const FrameIndexExpr &FIE) { return FIE.Expr && FIE.Expr->isFragment(); })) && \"conflicting locations for variable\"",
    file=0x7f70db8eb1f8 <str.67.llvm> "/checkout/src/llvm-project/llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp", line=303,
    function=0x7f70db233d86 <.L__PRETTY_FUNCTION__._ZN4llvm3Loc3MMI17addFrameIndexExprEPKNS_12DIExpressionEi> "void llvm::Loc::MMI::addFrameIndexExpr(const DIExpression *, int)") at assert.c:103
#6  0x00007f70e3710e95 in llvm::Loc::MMI::addFrameIndexExpr(llvm::DIExpression const*, int) [clone .cold] () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#7  0x00007f70e22f2086 in llvm::DwarfDebug::collectVariableInfoFromMFTable(llvm::DwarfCompileUnit&, llvm::DenseSet<std::pair<llvm::DINode const*, llvm::DILocation const*>, llvm::DenseMapInfo<std::pair<llvm::DINode const*, llvm::DILocation const*>, void> >&) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#8  0x00007f70e25914ab in llvm::DwarfDebug::collectEntityInfo(llvm::DwarfCompileUnit&, llvm::DISubprogram const*, llvm::DenseSet<std::pair<llvm::DINode const*, llvm::DILocation const*>, llvm::DenseMapInfo<std::pair<llvm::DINode const*, llvm::DILocation const*>, void> >&) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#9  0x00007f70e2592fe8 in llvm::DwarfDebug::endFunctionImpl(llvm::MachineFunction const*) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#10 0x00007f70e267f0a4 in llvm::DebugHandlerBase::endFunction(llvm::MachineFunction const*) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#11 0x00007f70e2bb6059 in llvm::AsmPrinter::emitFunctionBody() () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#12 0x00007f70e223d646 in llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#13 0x00007f70e2d7ac94 in llvm::FPPassManager::runOnFunction(llvm::Function&) [clone .warm] () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#14 0x00007f70e22d6376 in llvm::FPPassManager::runOnModule(llvm::Module&) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#15 0x00007f70e299f210 in llvm::legacy::PassManagerImpl::run(llvm::Module&) () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/libLLVM.so.19.1-rust-1.84.0-nightly
#16 0x00007f70e9f2aab2 in LLVMRustWriteOutputFile () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#17 0x00007f70e9f2a767 in rustc_codegen_llvm::back::write::write_output_file () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#18 0x00007f70e9ef1d5c in rustc_codegen_llvm::back::write::codegen () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#19 0x00007f70e9ef19e2 in rustc_codegen_ssa::back::write::finish_intra_module_work::<rustc_codegen_llvm::LlvmCodegenBackend> () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#20 0x00007f70ea003a36 in std::sys::backtrace::__rust_begin_short_backtrace::<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()> () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#21 0x00007f70ea000b28 in <<std::thread::Builder>::spawn_unchecked_<<rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::spawn_named_thread<rustc_codegen_ssa::back::write::spawn_work<rustc_codegen_llvm::LlvmCodegenBackend>::{closure#0}, ()>::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} ()
   from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#22 0x00007f70ea000eeb in std::sys::pal::unix::thread::Thread::new::thread_start () from /home/jistone/.rustup/toolchains/e92993dbb43f0a5d17fe56e2d82f90435d6521c8-alt/lib/librustc_driver-a4eb0db55e024726.so
#23 0x00007f70eb680797 in start_thread (arg=<optimized out>) at pthread_create.c:447
#24 0x00007f70eb70478c in __GI___clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:78

I bisected this to #128861 (cc @khuey)

  • The assertion fails with 3139ff09e9d07f7700f8d15ed25a231e29c43627-alt (3139ff0)
  • Compilation succeeds with 026e9ed3f0c5b3ee6233fd23b5e497cb94cf6434-alt (its parent 026e9ed)

@rustbot modify labels: +regression-from-stable-to-stable -regression-untriaged

@cuviper cuviper added C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. regression-untriaged Untriaged performance or correctness regression. labels Oct 19, 2024
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-untriaged Untriaged performance or correctness regression. labels Oct 19, 2024
@cuviper cuviper added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Oct 19, 2024
@cuviper
Copy link
Member Author

cuviper commented Oct 19, 2024

@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 20, 2024
@khuey
Copy link
Contributor

khuey commented Oct 21, 2024

I can reproduce this. I haven't gotten to the bottom of it yet but I think it's an interaction between MIR inlining and an LLVM optimization (not clear to me yet whether it's SROA, inlining, or both). The final bitcode is obviously garbage. Seeing how the LLVM IR evolves through the passes would be helpful, if you can easily isolate that.

@apiraino
Copy link
Contributor

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Oct 21, 2024
@cuviper
Copy link
Member Author

cuviper commented Oct 21, 2024

From -Csave-temps, I can reproduce this with an opt -O1 | llc pipeline on this no-opt.bc:
cargo-ec44d54913804da2.cargo.9f35cac0730afd57-cgu.13.rcgu.no-opt.bc.gz

Then llvm-reduce with that pipeline and grepping for the assertion string got me this:
reduced.bc.gz

The only DIExpressions left after reduction are in this block:

define void @_RINvXNvNtCs9ZNb7xDE5eg_18cargo_util_schemas8manifestsz_1__NtB5_14TomlLintConfigNtNtCsf9srf7wTiBp_5serde3ser9Serialize9serializeQNtNtNtCs6BCulG6QBAB_9toml_edit3ser3map18MapValueSerializerECsdFtfruI8BjD_5cargo(ptr %0, i64 %1) !dbg !4 {
  %3 = alloca [0 x [0 x [24 x i8]]], i32 0, align 8
  %4 = alloca [24 x i8], align 8
    #dbg_declare(ptr %3, !11, !DIExpression(), !20)
    #dbg_declare(ptr %4, !11, !DIExpression(), !20)
  %.not = icmp eq i64 %1, 0, !dbg !23
  br i1 %.not, label %5, label %common.ret

i.e. two allocas with the same metadata:

!11 = !DILocalVariable(name: "self", arg: 1, scope: !12, file: !13, line: 1994, type: !18)

@khuey
Copy link
Contributor

khuey commented Oct 21, 2024

is no-opt.bc the bitcode version of the IR rustc emits, before LLVM starts messing with it?

@cuviper
Copy link
Member Author

cuviper commented Oct 21, 2024

is no-opt.bc the bitcode version of the IR rustc emits, before LLVM starts messing with it?

Yes

@khuey
Copy link
Contributor

khuey commented Oct 21, 2024

Ok, thanks. opt -O1 is required to trigger the assert but it looks like the original IR rustc output is bad, which tracks with the bisect. Still digging.

@cuviper
Copy link
Member Author

cuviper commented Oct 21, 2024

FWIW, I've got a CI change that hits this in #132010, so hopefully we'll catch this kind of thing sooner next time.

@khuey
Copy link
Contributor

khuey commented Oct 21, 2024

The underlying issue here is a bad interaction between #128861 and proc macros. For reasons I don't understand (most likely details of LLVM's optimization pipeline) this sometimes but not always trips the reported assertion.

But it's fairly easy to cause rustc to generate IR where the same DIVariable and the same DILocation have two different declarations: a proc macro that generates code that uses the same inlined function twice will do it. #128861 effectively relies on the callsite span to produce a different DILocation and thus disambiguate different dbg_declares, but proc macros appear to have a single span for the entire macro site.

This comment may or may not be relevant

// FIXME(eddyb) this doesn't account for the macro-related

@DianQK
Copy link
Member

DianQK commented Oct 22, 2024

I reduced a bit:

// lib.rs
use serde::Serialize;
use toml_edit::ser::ValueSerializer;

#[derive(Serialize)]
pub struct TomlLintConfig2 {
    pub priority: i8,
    #[serde(flatten)]
    pub config: toml::Table,
}

#[no_mangle]
pub fn ser(value: TomlLintConfig2, serializer: ValueSerializer) -> Result<toml_edit::Value, toml_edit::ser::Error> {
    value.serialize(serializer)
}
# Cargo.toml
[package]
name = "pr131944"
version = "0.1.0"
edition = "2021"

[dependencies]
toml = "=0.8.19"
toml_edit = { version = "=0.22.22", features = ["serde"] }
serde = { version = "=1.0.210", features = ["derive"] }

[profile.release]
lto = false
opt-level = 2
codegen-units = 3
debug = "full"

I can reproduce it using stage1.

@khuey
Copy link
Contributor

khuey commented Oct 22, 2024

I think the correct fix here is to assign discriminators to DILocations corresponding to call sites inside proc macro bodies. That's work I can do but it's going to take time and I think that's way too scary to ship directly to stable.

If this is considered severe enough to spin a dot release (or to tag along with other stuff on a dot release) I think the thing to do is to back this out (it does revert cleanly from 1.82.0, though I didn't run tests) or to drop debug info that would conflict and trigger the assertion. I believe this patch would do the latter, though my testing so far is limited to building rustc itself, verifying that it fixes the reported regression, and checking that it doesn't regress the test from #128861.

diff --git a/compiler/rustc_codegen_ssa/src/mir/debuginfo.rs b/compiler/rustc_codegen_ssa/src/mir/debuginfo.rs
index 146f55f95c2..7d739680be2 100644
--- a/compiler/rustc_codegen_ssa/src/mir/debuginfo.rs
+++ b/compiler/rustc_codegen_ssa/src/mir/debuginfo.rs
@@ -479,7 +479,7 @@ pub(crate) fn compute_per_local_var_debug_info(
 
         let mut per_local = IndexVec::from_elem(vec![], &self.mir.local_decls);
         let mut constants = vec![];
-        let mut params_seen: FxHashMap<_, Bx::DIVariable> = Default::default();
+        let mut params_seen: FxHashMap<_, (Bx::DIVariable, Span, mir::SourceScope)> = Default::default();
         for var in &self.mir.var_debug_info {
             let dbg_scope_and_span = if full_debug_info {
                 self.adjusted_span_and_dbg_scope(var.source_info)
@@ -498,7 +498,7 @@ pub(crate) fn compute_per_local_var_debug_info(
                 }
             };
 
-            let dbg_var = dbg_scope_and_span.map(|(dbg_scope, _, span)| {
+            let dbg_var = dbg_scope_and_span.and_then(|(dbg_scope, _, span)| {
                 let var_kind = if let Some(arg_index) = var.argument_index
                     && var.composite.is_none()
                     && let mir::VarDebugInfoContents::Place(place) = var.value
@@ -524,18 +524,28 @@ pub(crate) fn compute_per_local_var_debug_info(
                     VariableKind::LocalVariable
                 };
 
-                if let VariableKind::ArgumentVariable(arg_index) = var_kind {
+                Some(if let VariableKind::ArgumentVariable(arg_index) = var_kind {
                     match params_seen.entry((dbg_scope, arg_index)) {
-                        Entry::Occupied(o) => o.get().clone(),
+                        Entry::Occupied(o) => {
+                            let (seen_var, seen_span, seen_source_scope) = o.get();
+                            if *seen_span == span && *seen_source_scope != var.source_info.scope {
+                                return None;
+                            } else {
+                                seen_var.clone()
+                            }
+                        },
                         Entry::Vacant(v) => v
-                            .insert(
+                            .insert((
                                 self.cx.create_dbg_var(var.name, var_ty, dbg_scope, var_kind, span),
-                            )
+                                span,
+                                var.source_info.scope,
+                            ))
+                            .0
                             .clone(),
                     }
                 } else {
                     self.cx.create_dbg_var(var.name, var_ty, dbg_scope, var_kind, span)
-                }
+                })
             });
 
             let fragment = if let Some(ref fragment) = var.composite {

@cuviper
Copy link
Member Author

cuviper commented Oct 22, 2024

I added your patch to #132010 to try it.

-            let dbg_var = dbg_scope_and_span.map(|(dbg_scope, _, span)| {
+            let dbg_var = dbg_scope_and_span.and_then(|(dbg_scope, _, span)| {

This is minor and off-topic, but if you make that a try { block, you'll get the later Some-wrapping for free.

@khuey
Copy link
Contributor

khuey commented Oct 22, 2024

Yeah I'm used to writing stable Rust and not rustc Rust.

@khuey
Copy link
Contributor

khuey commented Nov 4, 2024

@rustbot claim

khuey added a commit to khuey/rust that referenced this issue Nov 4, 2024
… into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same
location with different values. proc-macros make it possible for arbitrary code,
including multiple calls that get inlined, to happen at any given location in the source
code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the
unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 5, 2024
Add discriminators to DILocations when multiple functions are inlined into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same location with different values. proc-macros make it possible for arbitrary code, including multiple calls that get inlined, to happen at any given location in the source code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 5, 2024
Add discriminators to DILocations when multiple functions are inlined into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same location with different values. proc-macros make it possible for arbitrary code, including multiple calls that get inlined, to happen at any given location in the source code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944

try-job: x86_64-msvc
try-job: i686-msvc
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 7, 2024
Add discriminators to DILocations when multiple functions are inlined into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same location with different values. proc-macros make it possible for arbitrary code, including multiple calls that get inlined, to happen at any given location in the source code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944

try-job: x86_64-msvc
try-job: i686-msvc
try-job: x86_64-mingw
try-job: aarch64-apple
try-job: test-various
try-job: dist-various-1
try-job: armhf-gnu
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 7, 2024
Add discriminators to DILocations when multiple functions are inlined into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same location with different values. proc-macros make it possible for arbitrary code, including multiple calls that get inlined, to happen at any given location in the source code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944

try-job: x86_64-msvc
try-job: i686-msvc
try-job: x86_64-mingw
try-job: aarch64-apple
try-job: test-various
try-job: dist-various-1
try-job: armhf-gnu
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 7, 2024
Add discriminators to DILocations when multiple functions are inlined into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same location with different values. proc-macros make it possible for arbitrary code, including multiple calls that get inlined, to happen at any given location in the source code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944

try-job: x86_64-msvc
try-job: i686-msvc
try-job: x86_64-mingw
try-job: aarch64-apple
try-job: test-various
try-job: dist-various-1
try-job: armhf-gnu
@bors bors closed this as completed in 1dc1061 Nov 9, 2024
mati865 pushed a commit to mati865/rust that referenced this issue Nov 12, 2024
… into a single point.

LLVM does not expect to ever see multiple dbg_declares for the same variable at the same
location with different values. proc-macros make it possible for arbitrary code,
including multiple calls that get inlined, to happen at any given location in the source
code. Add discriminators when that happens so these locations are different to LLVM.

This may interfere with the AddDiscriminators pass in LLVM, which is added by the
unstable flag -Zdebug-info-for-profiling.

Fixes rust-lang#131944
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 20, 2024
Drop debug info instead of panicking if we exceed LLVM's capability to represent it

Recapping a bit of history here:

In rust-lang#128861 I made debug info correctly represent parameters to inline functions by removing a fake lexical block that had been inserted to suppress LLVM assertions and by deduplicating those parameters.

LLVM, however, expects to see a single parameter _with distinct locations_, particularly distinct inlinedAt values on the DILocations. This generally worked because no matter how deep the chain of inlines it takes two different call sites in the original function to result in the same function being present multiple times, and a function call requires a non-zero number of characters, but macros threw a wrench in that in rust-lang#131944. At the time I thought the issue there was limited to proc-macros, where an arbitrary amount of code can be generated at a single point in the source text.

In rust-lang#132613 I added discriminators to DILocations that would otherwise be the same to repair rust-lang#131944[^1]. This works, but LLVM's capacity for discriminators is not infinite (LLVM actually only allocates 12 bits for this internally). At the time I thought it would be very rare for anyone to hit the limit, but rust-lang#132900 proved me wrong. In the relatively-minimized test case it also became clear to me that the issue affects regular macros too, because the call to the inlined function will (without collapse_debuginfo on the macro) be attributed to the (repeated, if the macro is used more than once) textual callsite in the macro definition.

This PR fixes the panic by dropping debug info when we exceed LLVM's maximum discriminator value. There's also a preceding commit for a related but distinct issue: macros that use collapse_debuginfo should in fact have their inlinedAts collapsed to the macro callsite and thus not need discriminators at all (and not panic/warn accordingly when the discriminator limit is exhausted).

Fixes rust-lang#132900

r? `@jieyouxu`

[^1]: Editor's note: `fix` is a magic keyword in PR description that apparently will close the linked issue (it's closed already in this case, but still).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. P-critical Critical priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
6 participants