Skip to content

Commit 609496e

Browse files
committed
Auto merge of rust-lang#108446 - Zoxc:named-allocs, r=oli-obk
Name LLVM anonymous constants by a hash of their contents This makes the names stable between different versions of a crate unlike the `AllocId` naming, making LLVM IR comparisons with `llvm-diff` more practical.
2 parents 5423745 + b897b2d commit 609496e

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

compiler/rustc_codegen_llvm/src/common.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::value::Value;
1010
use rustc_ast::Mutability;
1111
use rustc_codegen_ssa::mir::place::PlaceRef;
1212
use rustc_codegen_ssa::traits::*;
13+
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1314
use rustc_hir::def_id::DefId;
1415
use rustc_middle::bug;
1516
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
@@ -252,8 +253,13 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
252253
Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None),
253254
_ => self.static_addr_of(init, alloc.align, None),
254255
};
255-
if !self.sess().fewer_names() {
256-
llvm::set_value_name(value, format!("{:?}", alloc_id).as_bytes());
256+
if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty() {
257+
let hash = self.tcx.with_stable_hashing_context(|mut hcx| {
258+
let mut hasher = StableHasher::new();
259+
alloc.hash_stable(&mut hcx, &mut hasher);
260+
hasher.finish::<u128>()
261+
});
262+
llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes());
257263
}
258264
(value, AddressSpace::DATA)
259265
}

tests/codegen/consts.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
// CHECK: @STATIC = {{.*}}, align 4
1010

1111
// This checks the constants from inline_enum_const
12-
// CHECK: @alloc12 = {{.*}}, align 2
12+
// CHECK: @alloc_76bfe2f13a3e3b01074971d122eac57e = {{.*}}, align 2
1313

1414
// This checks the constants from {low,high}_align_const, they share the same
1515
// constant, but the alignment differs, so the higher one should be used
16-
// CHECK: [[LOW_HIGH:@alloc[0-9]+]] = {{.*}}, align 4
16+
// CHECK: [[LOW_HIGH:@alloc_[a-f0-9]+]] = {{.*}}, align 4
1717

1818
#[derive(Copy, Clone)]
1919
// repr(i16) is required for the {low,high}_align_const test

tests/codegen/remap_path_prefix/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod aux_mod;
1212
include!("aux_mod.rs");
1313

1414
// Here we check that the expansion of the file!() macro is mapped.
15-
// CHECK: @alloc2 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>
15+
// CHECK: @alloc_92a59126a55aa3c0019b6c8a007fe001 = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }>
1616
pub static FILE_PATH: &'static str = file!();
1717

1818
fn main() {

0 commit comments

Comments
 (0)