rust/compiler/rustc_codegen_llvm/src
bors 62ebe3a2b1 Auto merge of #115417 - dpaoliello:fixdi, r=wesleywiser
Use the same DISubprogram for each instance of the same inlined function within a caller

# Issue Details:
The call to `panic` within a function like `Option::unwrap` is translated to LLVM as a `tail call` (as it will never return), when multiple calls to the same function like this are inlined LLVM will notice the common `tail call` block (i.e., loading the same panic string + location info and then calling `panic`) and merge them together.

When merging these instructions together, LLVM will also attempt to merge the debug locations as well, but this fails (i.e., debug info is dropped) as Rust emits a new `DISubprogram` at each inline site thus LLVM doesn't recognize that these are actually the same function and so thinks that there isn't a common debug location.

As an example of this, consider the following program:
```rust
#[no_mangle]
fn add_numbers(x: &Option<i32>, y: &Option<i32>) -> i32 {
    let x1 = x.unwrap();
    let y1 = y.unwrap();

    x1 + y1
}
```

 When building for x86_64 Windows using 1.72 it generates (note the lack of `.cv_loc` before the call to `panic`, thus it will be attributed to the same line at the `addq` instruction):

```llvm
	.cv_loc	0 1 3 0                        # src\lib.rs:3:0
	addq	$40, %rsp
	retq
	leaq	.Lalloc_f570dea0a53168780ce9a91e67646421(%rip), %rcx
	leaq	.Lalloc_629ace53b7e5b76aaa810d549cc84ea3(%rip), %r8
	movl	$43, %edx
	callq	_ZN4core9panicking5panic17h12e60b9063f6dee8E
	int3
```

# Fix Details:
Cache the `DISubprogram` emitted for each inlined function instance within a caller so that this can be reused if that instance is encountered again.

Ideally, we would also deduplicate child scopes and variables, however my attempt to do that with #114643 resulted in asserts when building for Linux (#115156) which would require some deep changes to Rust to fix (#115455).

Instead, when using an inlined function as a debug scope, we will also create a new child scope such that subsequent child scopes and variables do not collide (from LLVM's perspective).

After this change the above assembly now (with <https://reviews.llvm.org/D159226> as well) shows the `panic!` was inlined from `unwrap` in `option.rs` at line 935 into the current function in `lib.rs` at line 0 (line 0 is emitted since it is ambiguous which line to use as there were two inline sites that lead to this same code):

```llvm
	.cv_loc	0 1 3 0                        # src\lib.rs:3:0
	addq	$40, %rsp
	retq
	.cv_inline_site_id 6 within 0 inlined_at 1 0 0
	.cv_loc	6 2 935 0                       # library\core\src\option.rs:935:0
	leaq	.Lalloc_5f55955de67e57c79064b537689facea(%rip), %rcx
	leaq	.Lalloc_e741d4de8cb5801e1fd7a6c6795c1559(%rip), %r8
	movl	$43, %edx
	callq	_ZN4core9panicking5panic17hde1558f32d5b1c04E
	int3
```
2023-09-08 20:56:01 +00:00
..
back Auto merge of #115641 - durin42:llvm-18-fatlto-take-2, r=nikic 2023-09-08 19:07:17 +00:00
coverageinfo Inline functions called from add_coverage 2023-08-22 10:59:19 +02:00
debuginfo Auto merge of #115417 - dpaoliello:fixdi, r=wesleywiser 2023-09-08 20:56:01 +00:00
llvm Auto merge of #115641 - durin42:llvm-18-fatlto-take-2, r=nikic 2023-09-08 19:07:17 +00:00
abi.rs the wasm ABI behavior is a bug 2023-09-08 09:14:07 +02:00
allocator.rs Auto merge of #113722 - bjorn3:allocator_shim_refactor, r=jackh726 2023-08-13 16:49:49 +00:00
asm.rs Auto merge of #114467 - Amanieu:asm-unstable-features, r=davidtwco 2023-08-15 11:59:02 +00:00
attributes.rs Auto merge of #113814 - ChoKyuWon:master, r=davidtwco 2023-08-18 13:20:37 +00:00
base.rs cg_llvm: remove pointee types and pointercast/bitcast-of-ptr 2023-07-29 13:18:17 -04:00
builder.rs Rollup merge of #113593 - rcvalle:rust-cfi-fix-90546, r=wesleywiser 2023-08-08 21:44:43 +02:00
callee.rs cg_llvm: remove pointee types and pointercast/bitcast-of-ptr 2023-07-29 13:18:17 -04:00
common.rs Auto merge of #105545 - erikdesjardins:ptrclean, r=bjorn3 2023-08-01 19:44:17 +00:00
consts.rs Auto merge of #105545 - erikdesjardins:ptrclean, r=bjorn3 2023-08-01 19:44:17 +00:00
context.rs Don't ICE on layout computation failure 2023-08-28 12:40:39 -07:00
declare.rs CFI: Fix SIGILL reached via trait objects 2023-05-09 20:04:19 +00:00
errors.rs Better diagnostics for dlltool errors. 2023-07-17 20:20:01 +00:00
intrinsic.rs Apply suggestions from code review 2023-08-06 15:47:40 -07:00
lib.rs Use standard Rust capitalization rules for names containing "LTO". 2023-07-31 16:21:02 +10:00
llvm_util.rs inline format!() args up to and including rustc_codegen_llvm 2023-07-30 14:22:50 +02:00
mono_item.rs rustc: Move crate_types from Session to GlobalCtxt 2023-08-09 14:17:54 +08:00
type_.rs cg_ssa: remove pointee types and pointercast/bitcast-of-ptr 2023-07-29 13:18:20 -04:00
type_of.rs Rollup merge of #115240 - RalfJung:llvm-no-type, r=bjorn3 2023-08-28 19:53:55 +02:00
va_arg.rs cg_llvm: remove pointee types and pointercast/bitcast-of-ptr 2023-07-29 13:18:17 -04:00
value.rs mv compiler to compiler/ 2020-08-30 18:45:07 +03:00