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

Use less string interning #98638

Merged
merged 3 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions compiler/rustc_codegen_gcc/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use rustc_middle::mir::Mutability;
use rustc_middle::ty::ScalarInt;
use rustc_middle::ty::layout::{TyAndLayout, LayoutOf};
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
use rustc_span::Symbol;
use rustc_target::abi::{self, HasDataLayout, Pointer, Size};

use crate::consts::const_alloc_to_gcc;
Expand Down Expand Up @@ -125,12 +124,15 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
self.context.new_rvalue_from_double(typ, val)
}

fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
let s_str = s.as_str();
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
self.global_string(s_str)
});
let len = s_str.len();
fn const_str(&self, s: &str) -> (RValue<'gcc>, RValue<'gcc>) {
let str_global = *self
.const_str_cache
.borrow_mut()
.raw_entry_mut()
.from_key(s)
.or_insert_with(|| (s.to_owned(), self.global_string(s)))
.1;
let len = s.len();
let cs = self.const_ptrcast(str_global.get_address(None),
self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_codegen_gcc/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rustc_middle::mir::mono::CodegenUnit;
use rustc_middle::ty::{self, Instance, ParamEnv, PolyExistentialTraitRef, Ty, TyCtxt};
use rustc_middle::ty::layout::{FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasParamEnv, HasTyCtxt, LayoutError, TyAndLayout, LayoutOfHelpers};
use rustc_session::Session;
use rustc_span::{Span, Symbol};
use rustc_span::Span;
use rustc_target::abi::{call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx};
use rustc_target::spec::{HasTargetSpec, Target, TlsModel};

Expand Down Expand Up @@ -101,7 +101,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
pub global_lvalues: RefCell<FxHashMap<RValue<'gcc>, LValue<'gcc>>>,

/// Cache of constant strings,
pub const_str_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
pub const_str_cache: RefCell<FxHashMap<String, LValue<'gcc>>>,

/// Cache of globals.
pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_codegen_gcc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
* TODO(antoyo): remove the patches.
*/

#![feature(rustc_private, decl_macro, associated_type_bounds, never_type, trusted_len)]
#![feature(
rustc_private,
decl_macro,
associated_type_bounds,
never_type,
trusted_len,
hash_raw_entry
)]
#![allow(broken_intra_doc_links)]
#![recursion_limit="256"]
#![warn(rust_2018_idioms)]
Expand Down
38 changes: 21 additions & 17 deletions compiler/rustc_codegen_llvm/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use rustc_middle::bug;
use rustc_middle::mir::interpret::{ConstAllocation, GlobalAlloc, Scalar};
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
use rustc_middle::ty::ScalarInt;
use rustc_span::symbol::Symbol;
use rustc_target::abi::{self, AddressSpace, HasDataLayout, Pointer, Size};

use libc::{c_char, c_uint};
Expand Down Expand Up @@ -181,22 +180,27 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
unsafe { llvm::LLVMConstReal(t, val) }
}

fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
let s_str = s.as_str();
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
let sc = self.const_bytes(s_str.as_bytes());
let sym = self.generate_local_symbol_name("str");
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);
}
g
});
let len = s_str.len();
fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
let str_global = *self
.const_str_cache
.borrow_mut()
.raw_entry_mut()
.from_key(s)
.or_insert_with(|| {
let sc = self.const_bytes(s.as_bytes());
let sym = self.generate_local_symbol_name("str");
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);
}
(s.to_owned(), g)
})
.1;
let len = s.len();
let cs = consts::ptrcast(
str_global,
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use rustc_session::config::{BranchProtection, CFGuard, CFProtection};
use rustc_session::config::{CrateType, DebugInfo, PAuthKey, PacRet};
use rustc_session::Session;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;
use rustc_target::abi::{
call::FnAbi, HasDataLayout, PointeeInfo, Size, TargetDataLayout, VariantIdx,
};
Expand Down Expand Up @@ -56,7 +55,7 @@ pub struct CodegenCx<'ll, 'tcx> {
pub vtables:
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
/// Cache of constant strings,
pub const_str_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
pub const_str_cache: RefCell<FxHashMap<String, &'ll Value>>,

/// Reverse-direction for const ptrs cast from globals.
///
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! This API is completely unstable and subject to change.

#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
#![feature(hash_raw_entry)]
#![feature(let_chains)]
#![feature(let_else)]
#![feature(extern_types)]
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_codegen_ssa/src/back/archive.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use rustc_data_structures::temp_dir::MaybeTempDir;
use rustc_session::cstore::DllImport;
use rustc_session::Session;
use rustc_span::symbol::Symbol;

use std::io;
use std::path::{Path, PathBuf};

pub(super) fn find_library(
name: Symbol,
name: &str,
verbatim: bool,
search_paths: &[PathBuf],
sess: &Session,
Expand Down
6 changes: 0 additions & 6 deletions compiler/rustc_codegen_ssa/src/back/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::io;
use std::mem;
use std::process::{self, Output};

use rustc_span::symbol::Symbol;
use rustc_target::spec::LldFlavor;

#[derive(Clone)]
Expand Down Expand Up @@ -47,11 +46,6 @@ impl Command {
self
}

pub fn sym_arg(&mut self, arg: Symbol) -> &mut Command {
self.arg(arg.as_str());
self
}

pub fn args<I>(&mut self, args: I) -> &mut Command
where
I: IntoIterator<Item: AsRef<OsStr>>,
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ fn link_rlib<'a, B: ArchiveBuilder<'a>>(
}
if let Some(name) = lib.name {
let location =
find_library(name, lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
find_library(name.as_str(), lib.verbatim.unwrap_or(false), &lib_search_paths, sess);
ab.add_archive(&location, |_| false).unwrap_or_else(|e| {
sess.fatal(&format!(
"failed to add native library {}: {}",
Expand Down Expand Up @@ -1117,7 +1117,7 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
let path = find_sanitizer_runtime(&sess, &filename);
let rpath = path.to_str().expect("non-utf8 component in path");
linker.args(&["-Wl,-rpath", "-Xlinker", rpath]);
linker.link_dylib(Symbol::intern(&filename), false, true);
linker.link_dylib(&filename, false, true);
} else {
let filename = format!("librustc{}_rt.{}.a", channel, name);
let path = find_sanitizer_runtime(&sess, &filename).join(&filename);
Expand Down Expand Up @@ -2199,6 +2199,7 @@ fn add_local_native_libraries(
let Some(name) = lib.name else {
continue;
};
let name = name.as_str();

// Skip if this library is the same as the last.
last = if (lib.name, lib.kind, lib.verbatim) == last {
Expand Down Expand Up @@ -2362,6 +2363,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
let Some(name) = lib.name else {
continue;
};
let name = name.as_str();
if !relevant_lib(sess, lib) {
continue;
}
Expand Down Expand Up @@ -2519,7 +2521,7 @@ fn add_upstream_rust_crates<'a, B: ArchiveBuilder<'a>>(
}
let filestem = cratepath.file_stem().unwrap().to_str().unwrap();
cmd.link_rust_dylib(
Symbol::intern(&unlib(&sess.target, filestem)),
&unlib(&sess.target, filestem),
parent.unwrap_or_else(|| Path::new("")),
);
}
Expand Down Expand Up @@ -2551,6 +2553,7 @@ fn add_upstream_native_libraries(
let Some(name) = lib.name else {
continue;
};
let name = name.as_str();
if !relevant_lib(sess, &lib) {
continue;
}
Expand Down
Loading