Skip to content

Commit fd4bf82

Browse files
authored
Rollup merge of #137741 - cuviper:const_str-raw_entry, r=Mark-Simulacrum
Stop using `hash_raw_entry` in `CodegenCx::const_str` That unstable feature (#56167) completed fcp-close, so the compiler needs to be migrated away to allow its removal. In this case, `cg_llvm` and `cg_gcc` were using raw entries to optimize their `const_str_cache` lookup and insertion. We can change that to separate `get` and (on miss) `insert` calls, so we still have the fast path avoiding string allocation when the cache hits.
2 parents 1305212 + 396c2a8 commit fd4bf82

File tree

4 files changed

+25
-31
lines changed

4 files changed

+25
-31
lines changed

compiler/rustc_codegen_gcc/src/common.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,12 @@ impl<'gcc, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
146146
}
147147

148148
fn const_str(&self, s: &str) -> (RValue<'gcc>, RValue<'gcc>) {
149-
let str_global = *self
150-
.const_str_cache
151-
.borrow_mut()
152-
.raw_entry_mut()
153-
.from_key(s)
154-
.or_insert_with(|| (s.to_owned(), self.global_string(s)))
155-
.1;
149+
let mut const_str_cache = self.const_str_cache.borrow_mut();
150+
let str_global = const_str_cache.get(s).copied().unwrap_or_else(|| {
151+
let g = self.global_string(s);
152+
const_str_cache.insert(s.to_owned(), g);
153+
g
154+
});
156155
let len = s.len();
157156
let cs = self.const_ptrcast(
158157
str_global.get_address(None),

compiler/rustc_codegen_gcc/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#![allow(internal_features)]
1717
#![doc(rust_logo)]
1818
#![feature(rustdoc_internals)]
19-
#![feature(rustc_private, decl_macro, never_type, trusted_len, hash_raw_entry, let_chains)]
19+
#![feature(rustc_private, decl_macro, never_type, trusted_len, let_chains)]
2020
#![allow(broken_intra_doc_links)]
2121
#![recursion_limit = "256"]
2222
#![warn(rust_2018_idioms)]

compiler/rustc_codegen_llvm/src/common.rs

+18-22
Original file line numberDiff line numberDiff line change
@@ -209,28 +209,24 @@ impl<'ll, 'tcx> ConstCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
209209
}
210210

211211
fn const_str(&self, s: &str) -> (&'ll Value, &'ll Value) {
212-
let str_global = *self
213-
.const_str_cache
214-
.borrow_mut()
215-
.raw_entry_mut()
216-
.from_key(s)
217-
.or_insert_with(|| {
218-
let sc = self.const_bytes(s.as_bytes());
219-
let sym = self.generate_local_symbol_name("str");
220-
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
221-
bug!("symbol `{}` is already defined", sym);
222-
});
223-
llvm::set_initializer(g, sc);
224-
unsafe {
225-
llvm::LLVMSetGlobalConstant(g, True);
226-
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
227-
}
228-
llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
229-
// Cast to default address space if globals are in a different addrspace
230-
let g = self.const_pointercast(g, self.type_ptr());
231-
(s.to_owned(), g)
232-
})
233-
.1;
212+
let mut const_str_cache = self.const_str_cache.borrow_mut();
213+
let str_global = const_str_cache.get(s).copied().unwrap_or_else(|| {
214+
let sc = self.const_bytes(s.as_bytes());
215+
let sym = self.generate_local_symbol_name("str");
216+
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
217+
bug!("symbol `{}` is already defined", sym);
218+
});
219+
llvm::set_initializer(g, sc);
220+
unsafe {
221+
llvm::LLVMSetGlobalConstant(g, True);
222+
llvm::LLVMSetUnnamedAddress(g, llvm::UnnamedAddr::Global);
223+
}
224+
llvm::set_linkage(g, llvm::Linkage::InternalLinkage);
225+
// Cast to default address space if globals are in a different addrspace
226+
let g = self.const_pointercast(g, self.type_ptr());
227+
const_str_cache.insert(s.to_owned(), g);
228+
g
229+
});
234230
let len = s.len();
235231
(str_global, self.const_usize(len as u64))
236232
}

compiler/rustc_codegen_llvm/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![feature(exact_size_is_empty)]
1313
#![feature(extern_types)]
1414
#![feature(file_buffered)]
15-
#![feature(hash_raw_entry)]
1615
#![feature(if_let_guard)]
1716
#![feature(impl_trait_in_assoc_type)]
1817
#![feature(iter_intersperse)]

0 commit comments

Comments
 (0)