From cd3dd5bd230b1d98ed46e3609a64865eb1d5fc58 Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 6 Feb 2025 04:25:40 +0000 Subject: [PATCH 1/2] Add tests for -Zdwarf-version lto behavior --- .../auxiliary/dwarf-mixed-versions-lto-aux.rs | 5 +++++ tests/assembly/dwarf-mixed-versions-lto.rs | 19 +++++++++++++++++++ .../auxiliary/dwarf-mixed-versions-lto-aux.rs | 5 +++++ tests/ui/lto/dwarf-mixed-versions-lto.rs | 15 +++++++++++++++ tests/ui/lto/dwarf-mixed-versions-lto.stderr | 4 ++++ 5 files changed, 48 insertions(+) create mode 100644 tests/assembly/auxiliary/dwarf-mixed-versions-lto-aux.rs create mode 100644 tests/assembly/dwarf-mixed-versions-lto.rs create mode 100644 tests/ui/lto/auxiliary/dwarf-mixed-versions-lto-aux.rs create mode 100644 tests/ui/lto/dwarf-mixed-versions-lto.rs create mode 100644 tests/ui/lto/dwarf-mixed-versions-lto.stderr diff --git a/tests/assembly/auxiliary/dwarf-mixed-versions-lto-aux.rs b/tests/assembly/auxiliary/dwarf-mixed-versions-lto-aux.rs new file mode 100644 index 0000000000000..faff6e7e2d052 --- /dev/null +++ b/tests/assembly/auxiliary/dwarf-mixed-versions-lto-aux.rs @@ -0,0 +1,5 @@ +//@ compile-flags: -g --crate-type=rlib -Zdwarf-version=4 + +pub fn check_is_even(number: &u64) -> bool { + number % 2 == 0 +} diff --git a/tests/assembly/dwarf-mixed-versions-lto.rs b/tests/assembly/dwarf-mixed-versions-lto.rs new file mode 100644 index 0000000000000..5b8e5ff4f4a4b --- /dev/null +++ b/tests/assembly/dwarf-mixed-versions-lto.rs @@ -0,0 +1,19 @@ +// This test ensures that if LTO occurs between crates with different DWARF versions, we +// will choose the highest DWARF version for the final binary. This matches Clang's behavior. + +//@ only-linux +//@ aux-build:dwarf-mixed-versions-lto-aux.rs +//@ compile-flags: -C lto -g -Zdwarf-version=5 +//@ assembly-output: emit-asm +//@ no-prefer-dynamic + +extern crate dwarf_mixed_versions_lto_aux; + +fn main() { + dwarf_mixed_versions_lto_aux::check_is_even(&0); +} + +// CHECK: .section .debug_info +// CHECK-NOT: {{\.(short|hword)}} 2 +// CHECK-NOT: {{\.(short|hword)}} 4 +// CHECK: {{\.(short|hword)}} 5 diff --git a/tests/ui/lto/auxiliary/dwarf-mixed-versions-lto-aux.rs b/tests/ui/lto/auxiliary/dwarf-mixed-versions-lto-aux.rs new file mode 100644 index 0000000000000..3c81127ee65c0 --- /dev/null +++ b/tests/ui/lto/auxiliary/dwarf-mixed-versions-lto-aux.rs @@ -0,0 +1,5 @@ +//@ compile-flags: -g --crate-type=rlib -Zdwarf-version=4 + +pub fn say_hi() { + println!("hello there") +} diff --git a/tests/ui/lto/dwarf-mixed-versions-lto.rs b/tests/ui/lto/dwarf-mixed-versions-lto.rs new file mode 100644 index 0000000000000..14ef65a868e02 --- /dev/null +++ b/tests/ui/lto/dwarf-mixed-versions-lto.rs @@ -0,0 +1,15 @@ +// This test verifies that we do not produce a warning when performing LTO on a +// crate graph that contains a mix of different DWARF version settings. This +// matches Clang's behavior. + +//@ ignore-msvc Platform must use DWARF +//@ aux-build:dwarf-mixed-versions-lto-aux.rs +//@ compile-flags: -C lto -g -Zdwarf-version=5 +//@ no-prefer-dynamic +//@ build-pass + +extern crate dwarf_mixed_versions_lto_aux; + +fn main() { + dwarf_mixed_versions_lto_aux::say_hi(); +} diff --git a/tests/ui/lto/dwarf-mixed-versions-lto.stderr b/tests/ui/lto/dwarf-mixed-versions-lto.stderr new file mode 100644 index 0000000000000..15988383c29ce --- /dev/null +++ b/tests/ui/lto/dwarf-mixed-versions-lto.stderr @@ -0,0 +1,4 @@ +warning: linking module flags 'Dwarf Version': IDs have conflicting values ('i32 4' from with 'i32 5' from dwarf_mixed_versions_lto.7f4a44b55cf2f174-cgu.0) + +warning: 1 warning emitted + From bbc40e78226c2b4a84b44c717b5ad0983e29944c Mon Sep 17 00:00:00 2001 From: Wesley Wiser Date: Thu, 6 Feb 2025 04:50:17 +0000 Subject: [PATCH 2/2] Pick the max DWARF version when LTO'ing modules with different versions Currently, when rustc compiles code with `-Clto` enabled that was built with different choices for `-Zdwarf-version`, a warning will be reported. It's very easy to observe this by compiling most anything (eg, "hello world") and specifying `-Clto -Zdwarf-version=5` since the standard library is distributed with `-Zdwarf-version=4`. This behavior isn't actually useful for a few reasons: - from observation, LLVM chooses to pick the highest DWARF version anyway after issuing the warning - Clang specifies that in this case, the max version should be picked without a warning and as a general principle, we want to support x-lang LTO with Clang which implies using the same module flag merge behaviors - Debuggers need to be able to handle a variety of versions withing the same debugging session as you can easily have some parts of a binary (or some dynamic libraries within an application) all compiled with different DWARF versions This commit changes the module flag merge behavior to match Clang and use the highest version of DWARF. It also adds a test to ensure this behavior is respected in the case of two crates being LTO'd together and updates the test added in the previous commit to ensure no warning is printed. --- compiler/rustc_codegen_llvm/src/debuginfo/mod.rs | 6 +++++- tests/ui/lto/dwarf-mixed-versions-lto.stderr | 4 ---- 2 files changed, 5 insertions(+), 5 deletions(-) delete mode 100644 tests/ui/lto/dwarf-mixed-versions-lto.stderr diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs index 496178c6b1d94..471cdc17148c0 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/mod.rs @@ -97,7 +97,11 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> { // Android has the same issue (#22398) llvm::add_module_flag_u32( self.llmod, - llvm::ModuleFlagMergeBehavior::Warning, + // In the case where multiple CGUs with different dwarf version + // values are being merged together, such as with cross-crate + // LTO, then we want to use the highest version of dwarf + // we can. This matches Clang's behavior as well. + llvm::ModuleFlagMergeBehavior::Max, "Dwarf Version", sess.dwarf_version(), ); diff --git a/tests/ui/lto/dwarf-mixed-versions-lto.stderr b/tests/ui/lto/dwarf-mixed-versions-lto.stderr deleted file mode 100644 index 15988383c29ce..0000000000000 --- a/tests/ui/lto/dwarf-mixed-versions-lto.stderr +++ /dev/null @@ -1,4 +0,0 @@ -warning: linking module flags 'Dwarf Version': IDs have conflicting values ('i32 4' from with 'i32 5' from dwarf_mixed_versions_lto.7f4a44b55cf2f174-cgu.0) - -warning: 1 warning emitted -