From 30ff3002896326adc930c0159f5b3ed5173c68c8 Mon Sep 17 00:00:00 2001 From: Daniel Paoliello Date: Wed, 9 Aug 2023 15:10:58 -0700 Subject: [PATCH] Enable constant folding across crates (weak linkage + comdat) --- compiler/rustc_codegen_llvm/src/common.rs | 47 ++++++++++++++-- .../src/debuginfo/metadata.rs | 56 +++++++++++++++++++ compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 15 +++++ .../src/const_eval/eval_queries.rs | 7 ++- .../rustc_const_eval/src/interpret/intern.rs | 18 +++--- .../src/interpret/intrinsics.rs | 5 +- .../rustc_llvm/llvm-wrapper/RustWrapper.cpp | 44 +++++++++++++++ .../src/mir/interpret/allocation.rs | 19 ++++++- .../rustc_middle/src/mir/interpret/mod.rs | 2 +- compiler/rustc_middle/src/ty/codec.rs | 4 +- compiler/rustc_middle/src/ty/context.rs | 12 ++-- compiler/rustc_middle/src/ty/vtable.rs | 6 +- .../src/build/expr/as_constant.rs | 11 ++-- .../rustc_mir_transform/src/large_enums.rs | 2 +- src/bootstrap/compile.rs | 3 +- tests/codegen/consts.rs | 4 +- tests/codegen/remap_path_prefix/main.rs | 2 +- 17 files changed, 217 insertions(+), 40 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 0b0816c27b6df..02f46a0bde3e3 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -2,6 +2,9 @@ use crate::consts::const_alloc_to_llvm; pub use crate::context::CodegenCx; +use crate::debuginfo::metadata::{ + build_const_str_di_node, build_opaque_pointer_global_var_di_node, +}; use crate::llvm::{self, BasicBlock, Bool, ConstantInt, False, OperandBundleDef, True}; use crate::type_::Type; use crate::value::Value; @@ -11,7 +14,9 @@ use rustc_codegen_ssa::traits::*; use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher}; use rustc_hir::def_id::DefId; use rustc_middle::bug; -use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar}; +use rustc_middle::mir::interpret::{ + ConstAllocation, ConstAllocationDebugHint, GlobalAlloc, Scalar, +}; use rustc_middle::ty::TyCtxt; use rustc_session::cstore::{DllCallingConvention, DllImport, PeImportNameType}; use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer}; @@ -20,6 +25,8 @@ use rustc_target::spec::Target; use libc::{c_char, c_uint}; use std::fmt::Write; +const RUST_VERSION: &str = env!("CFG_RELEASE_NUM"); + /* * A note on nomenclature of linking: "extern", "foreign", and "upcall". * @@ -196,15 +203,23 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { .from_key(s) .or_insert_with(|| { let sc = self.const_bytes(s.as_bytes()); - let sym = self.generate_local_symbol_name("str"); + let hash = self.tcx.with_stable_hashing_context(|mut hcx| { + let mut hasher = StableHasher::new(); + s.hash_stable(&mut hcx, &mut hasher); + hasher.finish::() + }); + let sym = format!("__rust_{RUST_VERSION}_conststr_{hash:032x}"); let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| { bug!("symbol `{}` is already defined", sym); }); unsafe { llvm::LLVMSetInitializer(g, sc); llvm::LLVMSetGlobalConstant(g, True); - llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage); + llvm::LLVMRustSetLinkage(g, llvm::Linkage::LinkOnceODRLinkage); + llvm::SetUniqueComdat(self.llmod, g); + llvm::LLVMRustSetVisibility(g, llvm::Visibility::Hidden); } + build_const_str_di_node(self, &sym, g); (s.to_owned(), g) }) .1; @@ -249,18 +264,38 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> { let (base_addr, base_addr_space) = match self.tcx.global_alloc(alloc_id) { GlobalAlloc::Memory(alloc) => { let init = const_alloc_to_llvm(self, alloc); + let debug_hint = alloc.1; let alloc = alloc.inner(); let value = match alloc.mutability { Mutability::Mut => self.static_addr_of_mut(init, alloc.align, None), - _ => self.static_addr_of(init, alloc.align, None), + _ => { + let value = self.static_addr_of(init, alloc.align, None); + llvm::set_linkage(value, llvm::Linkage::LinkOnceODRLinkage); + llvm::set_visibility(value, llvm::Visibility::Hidden); + value + } }; - if !self.sess().fewer_names() && llvm::get_value_name(value).is_empty() { + + if llvm::get_value_name(value).is_empty() { + let name_prefix = match debug_hint { + Some(ConstAllocationDebugHint::StrLiteral) => "str", + Some(ConstAllocationDebugHint::CallerLocation) => "callerloc", + Some(ConstAllocationDebugHint::TypeName) => "typename", + Some(ConstAllocationDebugHint::VTable) => "vtable", + None => "alloc", + }; + let hash = self.tcx.with_stable_hashing_context(|mut hcx| { let mut hasher = StableHasher::new(); alloc.hash_stable(&mut hcx, &mut hasher); hasher.finish::() }); - llvm::set_value_name(value, format!("alloc_{hash:032x}").as_bytes()); + let name = format!("__rust_{RUST_VERSION}_{name_prefix}_{hash:032x}"); + llvm::set_value_name(value, name.as_bytes()); + build_opaque_pointer_global_var_di_node(self, &name, value); + if alloc.mutability == Mutability::Not { + llvm::SetUniqueComdat(self.llmod, value); + } } (value, AddressSpace::DATA) } diff --git a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs index f8cbcbd5ec852..1bd8d903e9ae7 100644 --- a/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs +++ b/compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs @@ -1252,6 +1252,62 @@ pub fn build_global_var_di_node<'ll>(cx: &CodegenCx<'ll, '_>, def_id: DefId, glo } } +/// Creates debug information for the given constant string. +/// +/// Adds the created debuginfo nodes directly to the crate's IR. +pub fn build_const_str_di_node<'ll>( + cx: &CodegenCx<'ll, '_>, + var_name: &str, + const_str: &'ll Value, +) { + if cx.dbg_cx.is_none() { + return; + } + + // Only create type information if full debuginfo is enabled + if cx.sess().opts.debuginfo != DebugInfo::Full { + return; + } + + unsafe { + llvm::LLVMRustDIBuilderCreateConstStr( + DIB(cx), + var_name.as_ptr().cast(), + var_name.len(), + type_di_node(cx, cx.tcx.types.str_), + const_str, + ); + } +} + +/// Creates debug information for the given global variable which is a pointer to an +/// unknown or opaque type. +/// +/// Adds the created debuginfo nodes directly to the crate's IR. +pub fn build_opaque_pointer_global_var_di_node<'ll>( + cx: &CodegenCx<'ll, '_>, + var_name: &str, + global: &'ll Value, +) { + if cx.dbg_cx.is_none() { + return; + } + + // Only create type information if full debuginfo is enabled + if cx.sess().opts.debuginfo != DebugInfo::Full { + return; + } + + unsafe { + llvm::LLVMRustDIBuilderCreateOpaquePointerStaticVariable( + DIB(cx), + var_name.as_ptr().cast(), + var_name.len(), + global, + ); + } +} + /// Generates LLVM debuginfo for a vtable. /// /// The vtable type looks like a struct with a field for each function pointer and super-trait diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 84157d1e25ca3..bec45007f3f87 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -1948,6 +1948,21 @@ extern "C" { AlignInBits: u32, ) -> &'a DIGlobalVariableExpression; + pub fn LLVMRustDIBuilderCreateConstStr<'a>( + Builder: &DIBuilder<'a>, + Name: *const c_char, + NameLen: size_t, + Ty: &'a DIType, + Val: &'a Value, + ) -> &'a DIGlobalVariableExpression; + + pub fn LLVMRustDIBuilderCreateOpaquePointerStaticVariable<'a>( + Builder: &DIBuilder<'a>, + Name: *const c_char, + NameLen: size_t, + Val: &'a Value, + ) -> &'a DIGlobalVariableExpression; + pub fn LLVMRustDIBuilderCreateVariable<'a>( Builder: &DIBuilder<'a>, Tag: c_uint, diff --git a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs index 4c7e9194401ac..6ee538c419ef7 100644 --- a/compiler/rustc_const_eval/src/const_eval/eval_queries.rs +++ b/compiler/rustc_const_eval/src/const_eval/eval_queries.rs @@ -177,9 +177,10 @@ pub(super) fn op_to_const<'tcx>( (ecx.tcx.global_alloc(alloc_id).unwrap_memory(), offset.bytes()) } (None, _offset) => ( - ecx.tcx.mk_const_alloc(Allocation::from_bytes_byte_aligned_immutable( - b"" as &[u8], - )), + ecx.tcx.mk_const_alloc( + Allocation::from_bytes_byte_aligned_immutable(b"" as &[u8]), + None, + ), 0, ), }; diff --git a/compiler/rustc_const_eval/src/interpret/intern.rs b/compiler/rustc_const_eval/src/interpret/intern.rs index 910c3ca5d0a97..18de92316340d 100644 --- a/compiler/rustc_const_eval/src/interpret/intern.rs +++ b/compiler/rustc_const_eval/src/interpret/intern.rs @@ -18,7 +18,7 @@ use super::validity::RefTracking; use rustc_data_structures::fx::{FxIndexMap, FxIndexSet}; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; -use rustc_middle::mir::interpret::InterpResult; +use rustc_middle::mir::interpret::{ConstAllocationDebugHint, InterpResult}; use rustc_middle::ty::{self, layout::TyAndLayout, Ty}; use rustc_ast::Mutability; @@ -104,11 +104,11 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval: }; // This match is just a canary for future changes to `MemoryKind`, which most likely need // changes in this function. - match kind { - MemoryKind::Stack - | MemoryKind::Machine(const_eval::MemoryKind::Heap) - | MemoryKind::CallerLocation => {} - } + let const_allocation_kind = match kind { + MemoryKind::Stack | MemoryKind::Machine(const_eval::MemoryKind::Heap) => None, + MemoryKind::CallerLocation => Some(ConstAllocationDebugHint::CallerLocation), + }; + // Set allocation mutability as appropriate. This is used by LLVM to put things into // read-only memory, and also by Miri when evaluating other globals that // access this one. @@ -136,7 +136,7 @@ fn intern_shallow<'rt, 'mir, 'tcx, M: CompileTimeMachine<'mir, 'tcx, const_eval: }; // link the alloc id to the actual allocation leftover_allocations.extend(alloc.provenance().ptrs().iter().map(|&(_, alloc_id)| alloc_id)); - let alloc = tcx.mk_const_alloc(alloc); + let alloc = tcx.mk_const_alloc(alloc, const_allocation_kind); tcx.set_alloc_id_memory(alloc_id, alloc); None } @@ -428,7 +428,7 @@ pub fn intern_const_alloc_recursive< alloc.mutability = Mutability::Not; } } - let alloc = tcx.mk_const_alloc(alloc); + let alloc = tcx.mk_const_alloc(alloc, None); tcx.set_alloc_id_memory(alloc_id, alloc); for &(_, alloc_id) in alloc.inner().provenance().ptrs().iter() { if leftover_allocations.insert(alloc_id) { @@ -467,6 +467,6 @@ impl<'mir, 'tcx: 'mir, M: super::intern::CompileTimeMachine<'mir, 'tcx, !>> f(self, &dest.clone().into())?; let mut alloc = self.memory.alloc_map.remove(&dest.ptr.provenance.unwrap()).unwrap().1; alloc.mutability = Mutability::Not; - Ok(self.tcx.mk_const_alloc(alloc)) + Ok(self.tcx.mk_const_alloc(alloc, None)) } } diff --git a/compiler/rustc_const_eval/src/interpret/intrinsics.rs b/compiler/rustc_const_eval/src/interpret/intrinsics.rs index f22cd919c3695..a81f4373c001c 100644 --- a/compiler/rustc_const_eval/src/interpret/intrinsics.rs +++ b/compiler/rustc_const_eval/src/interpret/intrinsics.rs @@ -6,7 +6,8 @@ use rustc_hir::def_id::DefId; use rustc_middle::mir::{ self, interpret::{ - Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar, + Allocation, ConstAllocation, ConstAllocationDebugHint, ConstValue, GlobalId, InterpResult, + PointerArithmetic, Scalar, }, BinOp, NonDivergingIntrinsic, }; @@ -47,7 +48,7 @@ fn numeric_intrinsic(name: Symbol, bits: u128, kind: Primitive) -> Scalar< pub(crate) fn alloc_type_name<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ConstAllocation<'tcx> { let path = crate::util::type_name(tcx, ty); let alloc = Allocation::from_bytes_byte_aligned_immutable(path.into_bytes()); - tcx.mk_const_alloc(alloc) + tcx.mk_const_alloc(alloc, Some(ConstAllocationDebugHint::TypeName)) } /// The logic for all nullary intrinsics is implemented here. These intrinsics don't get evaluated diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 70cdf3d6d2395..1a0841eb14afb 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -1035,6 +1035,50 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateStaticVariable( return wrap(VarExpr); } +extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateConstStr( + LLVMRustDIBuilderRef Builder, + const char *Name, size_t NameLen, + LLVMMetadataRef Ty, LLVMValueRef V) { + llvm::GlobalVariable *InitVal = cast(unwrap(V)); + + llvm::DIGlobalVariableExpression *VarExpr = Builder->createGlobalVariableExpression( + /* context */ nullptr, StringRef(Name, NameLen), + /* linkageName */ StringRef(), + nullptr, 0, unwrapDI(Ty), /* isLocalToUnit */ false, + /* isDefined */ true, + /* expr */ nullptr, /* decl */ nullptr, + /* templateParams */ nullptr, + /* alignInBits */ 0); + + InitVal->setMetadata("dbg", VarExpr); + + return wrap(VarExpr); +} + +extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateOpaquePointerStaticVariable( + LLVMRustDIBuilderRef Builder, + const char *Name, size_t NameLen, + LLVMValueRef V) { + llvm::GlobalVariable *InitVal = cast(unwrap(V)); + + DataLayout Layout = InitVal->getParent()->getDataLayout(); + DIType *DebugType = Builder->createPointerType(nullptr, Layout.getTypeSizeInBits( + InitVal->getValueType())); + + llvm::DIGlobalVariableExpression *VarExpr = Builder->createGlobalVariableExpression( + /* context */ nullptr, StringRef(Name, NameLen), + /* linkageName */ StringRef(), + nullptr, 0, DebugType, /* isLocalToUnit */ false, + /* isDefined */ true, + /* expr */ nullptr, /* decl */ nullptr, + /* templateParams */ nullptr, + /* alignInBits */ 0); + + InitVal->setMetadata("dbg", VarExpr); + + return wrap(VarExpr); +} + extern "C" LLVMMetadataRef LLVMRustDIBuilderCreateVariable( LLVMRustDIBuilderRef Builder, unsigned Tag, LLVMMetadataRef Scope, const char *Name, size_t NameLen, diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index c787481bfbec7..fda5a1095ea6e 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -150,7 +150,10 @@ impl hash::Hash for Allocation { /// (`ConstAllocation`) are used quite a bit. #[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable)] #[rustc_pass_by_value] -pub struct ConstAllocation<'tcx>(pub Interned<'tcx, Allocation>); +pub struct ConstAllocation<'tcx>( + pub Interned<'tcx, Allocation>, + pub Option, +); impl<'tcx> fmt::Debug for ConstAllocation<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -166,6 +169,20 @@ impl<'tcx> ConstAllocation<'tcx> { } } +/// Hint used to tag the name of a `ConstAllocation` in debug information so +/// that data in the final binary can be attributed back to the allocator. +#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable, Encodable, Decodable)] +pub enum ConstAllocationDebugHint { + /// A string literal. + StrLiteral, + /// Caller infromation (e.g., for panics). + CallerLocation, + /// The name of a type. + TypeName, + /// A vtable. + VTable, +} + /// We have our own error type that does not know about the `AllocId`; that information /// is added when converting to `InterpError`. #[derive(Debug)] diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 3543158bf82d2..537369983ea0d 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -153,7 +153,7 @@ pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar}; pub use self::allocation::{ alloc_range, AllocBytes, AllocError, AllocRange, AllocResult, Allocation, ConstAllocation, - InitChunk, InitChunkIter, + ConstAllocationDebugHint, InitChunk, InitChunkIter, }; pub use self::pointer::{Pointer, PointerArithmetic, Provenance}; diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 7c05deae90a58..6af4ee975cd60 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -148,6 +148,7 @@ impl<'tcx, E: TyEncoder>> Encodable for ty::Const<'tcx> { impl<'tcx, E: TyEncoder>> Encodable for ConstAllocation<'tcx> { fn encode(&self, e: &mut E) { + self.1.encode(e); self.inner().encode(e) } } @@ -356,7 +357,8 @@ impl<'tcx, D: TyDecoder>> RefDecodable<'tcx, D> for [ty::ValTre impl<'tcx, D: TyDecoder>> Decodable for ConstAllocation<'tcx> { fn decode(decoder: &mut D) -> Self { - decoder.interner().mk_const_alloc(Decodable::decode(decoder)) + let debug_hint = Decodable::decode(decoder); + decoder.interner().mk_const_alloc(Decodable::decode(decoder), debug_hint) } } diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index be839e03cff69..2e9e58cb7b77c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -12,7 +12,7 @@ use crate::metadata::ModChild; use crate::middle::codegen_fn_attrs::CodegenFnAttrs; use crate::middle::resolve_bound_vars; use crate::middle::stability; -use crate::mir::interpret::{self, Allocation, ConstAllocation}; +use crate::mir::interpret::{self, Allocation, ConstAllocation, ConstAllocationDebugHint}; use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted}; use crate::query::plumbing::QuerySystem; use crate::query::LocalCrate; @@ -646,7 +646,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn allocate_bytes(self, bytes: &[u8]) -> interpret::AllocId { // Create an allocation that just contains these bytes. let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes); - let alloc = self.mk_const_alloc(alloc); + let alloc = self.mk_const_alloc(alloc, Some(ConstAllocationDebugHint::StrLiteral)); self.create_memory_alloc(alloc) } @@ -1456,7 +1456,7 @@ impl<'tcx, T: Hash> Hash for InternedInSet<'tcx, List> { } macro_rules! direct_interners { - ($($name:ident: $vis:vis $method:ident($ty:ty): $ret_ctor:ident -> $ret_ty:ty,)+) => { + ($($name:ident: $vis:vis $method:ident($ty:ty $(, $v_extra:ident: $ty_extra:ty)*): $ret_ctor:ident -> $ret_ty:ty,)+) => { $(impl<'tcx> Borrow<$ty> for InternedInSet<'tcx, $ty> { fn borrow<'a>(&'a self) -> &'a $ty { &self.0 @@ -1482,10 +1482,10 @@ macro_rules! direct_interners { } impl<'tcx> TyCtxt<'tcx> { - $vis fn $method(self, v: $ty) -> $ret_ty { + $vis fn $method(self, v: $ty $(, $v_extra: $ty_extra)*) -> $ret_ty { $ret_ctor(Interned::new_unchecked(self.interners.$name.intern(v, |v| { InternedInSet(self.interners.arena.alloc(v)) - }).0)) + }).0) $(, $v_extra)*) } })+ } @@ -1497,7 +1497,7 @@ macro_rules! direct_interners { direct_interners! { region: pub(crate) intern_region(RegionKind<'tcx>): Region -> Region<'tcx>, const_: intern_const(ConstData<'tcx>): Const -> Const<'tcx>, - const_allocation: pub mk_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>, + const_allocation: pub mk_const_alloc(Allocation, kind: Option): ConstAllocation -> ConstAllocation<'tcx>, layout: pub mk_layout(LayoutS): Layout -> Layout<'tcx>, adt_def: pub mk_adt_def_from_data(AdtDefData): AdtDef -> AdtDef<'tcx>, external_constraints: pub mk_external_constraints(ExternalConstraintsData<'tcx>): diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 97402caa0013b..14ecc939cdc13 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -1,6 +1,8 @@ use std::fmt; -use crate::mir::interpret::{alloc_range, AllocId, Allocation, Pointer, Scalar}; +use crate::mir::interpret::{ + alloc_range, AllocId, Allocation, ConstAllocationDebugHint, Pointer, Scalar, +}; use crate::ty::{self, Instance, PolyTraitRef, Ty, TyCtxt}; use rustc_ast::Mutability; @@ -112,5 +114,5 @@ pub(super) fn vtable_allocation_provider<'tcx>( } vtable.mutability = Mutability::Not; - tcx.create_memory_alloc(tcx.mk_const_alloc(vtable)) + tcx.create_memory_alloc(tcx.mk_const_alloc(vtable, Some(ConstAllocationDebugHint::VTable))) } diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index aaa37446e24d3..605c50795b16e 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -4,7 +4,7 @@ use crate::build::{parse_float_into_constval, Builder}; use rustc_ast as ast; use rustc_middle::mir; use rustc_middle::mir::interpret::{ - Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar, + Allocation, ConstAllocationDebugHint, ConstValue, LitToConstError, LitToConstInput, Scalar, }; use rustc_middle::mir::*; use rustc_middle::thir::*; @@ -132,14 +132,16 @@ fn lit_to_mir_constant<'tcx>( (ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => { let s = s.as_str(); let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes()); - let allocation = tcx.mk_const_alloc(allocation); + let allocation = + tcx.mk_const_alloc(allocation, Some(ConstAllocationDebugHint::StrLiteral)); ConstValue::Slice { data: allocation, start: 0, end: s.len() } } (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Slice(_)) => { let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]); - let allocation = tcx.mk_const_alloc(allocation); + let allocation = + tcx.mk_const_alloc(allocation, Some(ConstAllocationDebugHint::StrLiteral)); ConstValue::Slice { data: allocation, start: 0, end: data.len() } } (ast::LitKind::ByteStr(data, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => { @@ -149,7 +151,8 @@ fn lit_to_mir_constant<'tcx>( (ast::LitKind::CStr(data, _), ty::Ref(_, inner_ty, _)) if matches!(inner_ty.kind(), ty::Adt(def, _) if Some(def.did()) == tcx.lang_items().c_str()) => { let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]); - let allocation = tcx.mk_const_alloc(allocation); + let allocation = + tcx.mk_const_alloc(allocation, Some(ConstAllocationDebugHint::StrLiteral)); ConstValue::Slice { data: allocation, start: 0, end: data.len() } } (ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => { diff --git a/compiler/rustc_mir_transform/src/large_enums.rs b/compiler/rustc_mir_transform/src/large_enums.rs index 19108dabdf417..eca3c7f260114 100644 --- a/compiler/rustc_mir_transform/src/large_enums.rs +++ b/compiler/rustc_mir_transform/src/large_enums.rs @@ -114,7 +114,7 @@ impl EnumSizeOpt { tcx.data_layout.ptr_sized_integer().align(&tcx.data_layout).abi, Mutability::Not, ); - let alloc = tcx.create_memory_alloc(tcx.mk_const_alloc(alloc)); + let alloc = tcx.create_memory_alloc(tcx.mk_const_alloc(alloc, None)); Some((*adt_def, num_discrs, *alloc_cache.entry(ty).or_insert(alloc))) } fn optim<'tcx>(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { diff --git a/src/bootstrap/compile.rs b/src/bootstrap/compile.rs index cad370ad75658..1d320e1db7dab 100644 --- a/src/bootstrap/compile.rs +++ b/src/bootstrap/compile.rs @@ -943,7 +943,8 @@ pub fn rustc_cargo_env( cargo .env("CFG_RELEASE", builder.rust_release()) .env("CFG_RELEASE_CHANNEL", &builder.config.channel) - .env("CFG_VERSION", builder.rust_version()); + .env("CFG_VERSION", builder.rust_version()) + .env("CFG_RELEASE_NUM", &builder.version); if let Some(backend) = builder.config.default_codegen_backend() { cargo.env("CFG_DEFAULT_CODEGEN_BACKEND", backend); diff --git a/tests/codegen/consts.rs b/tests/codegen/consts.rs index 3797e1a99da7f..ddf6ad3a5a74b 100644 --- a/tests/codegen/consts.rs +++ b/tests/codegen/consts.rs @@ -9,11 +9,11 @@ // CHECK: @STATIC = {{.*}}, align 4 // This checks the constants from inline_enum_const -// CHECK: @alloc_af1f8e8e6f4b341431a1d405e652df2d = {{.*}}, align 2 +// CHECK: @__rust_{{[0-9.]+}}_alloc_af1f8e8e6f4b341431a1d405e652df2d = {{.*}}, align 2 // This checks the constants from {low,high}_align_const, they share the same // constant, but the alignment differs, so the higher one should be used -// CHECK: [[LOW_HIGH:@alloc_[a-f0-9]+]] = {{.*}}, align 4 +// CHECK: [[LOW_HIGH:@__rust_[0-9.]+_alloc_[a-f0-9]+]] = {{.*}}, align 4 #[derive(Copy, Clone)] // repr(i16) is required for the {low,high}_align_const test diff --git a/tests/codegen/remap_path_prefix/main.rs b/tests/codegen/remap_path_prefix/main.rs index f1e1dd69b9691..e2e1d43803ddb 100644 --- a/tests/codegen/remap_path_prefix/main.rs +++ b/tests/codegen/remap_path_prefix/main.rs @@ -12,7 +12,7 @@ mod aux_mod; include!("aux_mod.rs"); // Here we check that the expansion of the file!() macro is mapped. -// CHECK: @alloc_5761061597a97f66e13ef2ff92712c4b = private unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }> +// CHECK: @__rust_{{[0-9.]+}}_str_5761061597a97f66e13ef2ff92712c4b = linkonce_odr hidden unnamed_addr constant <{ [34 x i8] }> <{ [34 x i8] c"/the/src/remap_path_prefix/main.rs" }> pub static FILE_PATH: &'static str = file!(); fn main() {