Skip to content

Commit 3d1eaf4

Browse files
committed
Auto merge of #94638 - erikdesjardins:noextranull, r=nagisa
cleanup: remove unused ability to have LLVM null-terminate const strings (and the copied function in rustc_codegen_gcc) Noticed this while writing #94450 (comment). r? `@nagisa`
2 parents 8876ca3 + e1a4bf6 commit 3d1eaf4

File tree

4 files changed

+26
-46
lines changed

4 files changed

+26
-46
lines changed

compiler/rustc_codegen_gcc/src/common.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
2626
bytes_in_context(self, bytes)
2727
}
2828

29-
fn const_cstr(&self, symbol: Symbol, _null_terminated: bool) -> LValue<'gcc> {
30-
// TODO(antoyo): handle null_terminated.
31-
if let Some(&value) = self.const_cstr_cache.borrow().get(&symbol) {
32-
return value;
33-
}
34-
35-
let global = self.global_string(symbol.as_str());
36-
37-
self.const_cstr_cache.borrow_mut().insert(symbol, global);
38-
global
39-
}
40-
4129
fn global_string(&self, string: &str) -> LValue<'gcc> {
4230
// TODO(antoyo): handle non-null-terminated strings.
4331
let string = self.context.new_string_literal(&*string);
@@ -171,8 +159,12 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
171159
}
172160

173161
fn const_str(&self, s: Symbol) -> (RValue<'gcc>, RValue<'gcc>) {
174-
let len = s.as_str().len();
175-
let cs = self.const_ptrcast(self.const_cstr(s, false).get_address(None),
162+
let s_str = s.as_str();
163+
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
164+
self.global_string(s_str)
165+
});
166+
let len = s_str.len();
167+
let cs = self.const_ptrcast(str_global.get_address(None),
176168
self.type_ptr_to(self.layout_of(self.tcx.types.str_).gcc_type(self, true)),
177169
);
178170
(cs, self.const_usize(len as u64))

compiler/rustc_codegen_gcc/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub struct CodegenCx<'gcc, 'tcx> {
8585
pub const_globals: RefCell<FxHashMap<RValue<'gcc>, RValue<'gcc>>>,
8686

8787
/// Cache of constant strings,
88-
pub const_cstr_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
88+
pub const_str_cache: RefCell<FxHashMap<Symbol, LValue<'gcc>>>,
8989

9090
/// Cache of globals.
9191
pub globals: RefCell<FxHashMap<String, RValue<'gcc>>>,
@@ -195,7 +195,7 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
195195
function_instances: Default::default(),
196196
vtables: Default::default(),
197197
const_globals: Default::default(),
198-
const_cstr_cache: Default::default(),
198+
const_str_cache: Default::default(),
199199
globals: Default::default(),
200200
scalar_types: Default::default(),
201201
types: Default::default(),

compiler/rustc_codegen_llvm/src/common.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,6 @@ impl<'ll> CodegenCx<'ll, '_> {
106106
bytes_in_context(self.llcx, bytes)
107107
}
108108

109-
fn const_cstr(&self, s: Symbol, null_terminated: bool) -> &'ll Value {
110-
unsafe {
111-
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
112-
return llval;
113-
}
114-
115-
let s_str = s.as_str();
116-
let sc = llvm::LLVMConstStringInContext(
117-
self.llcx,
118-
s_str.as_ptr() as *const c_char,
119-
s_str.len() as c_uint,
120-
!null_terminated as Bool,
121-
);
122-
let sym = self.generate_local_symbol_name("str");
123-
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
124-
bug!("symbol `{}` is already defined", sym);
125-
});
126-
llvm::LLVMSetInitializer(g, sc);
127-
llvm::LLVMSetGlobalConstant(g, True);
128-
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
129-
130-
self.const_cstr_cache.borrow_mut().insert(s, g);
131-
g
132-
}
133-
}
134-
135109
pub fn const_get_elt(&self, v: &'ll Value, idx: u64) -> &'ll Value {
136110
unsafe {
137111
assert_eq!(idx as c_uint as u64, idx);
@@ -204,9 +178,23 @@ impl<'ll, 'tcx> ConstMethods<'tcx> for CodegenCx<'ll, 'tcx> {
204178
}
205179

206180
fn const_str(&self, s: Symbol) -> (&'ll Value, &'ll Value) {
207-
let len = s.as_str().len();
181+
let s_str = s.as_str();
182+
let str_global = *self.const_str_cache.borrow_mut().entry(s).or_insert_with(|| {
183+
let sc = self.const_bytes(s_str.as_bytes());
184+
let sym = self.generate_local_symbol_name("str");
185+
let g = self.define_global(&sym, self.val_ty(sc)).unwrap_or_else(|| {
186+
bug!("symbol `{}` is already defined", sym);
187+
});
188+
unsafe {
189+
llvm::LLVMSetInitializer(g, sc);
190+
llvm::LLVMSetGlobalConstant(g, True);
191+
llvm::LLVMRustSetLinkage(g, llvm::Linkage::InternalLinkage);
192+
}
193+
g
194+
});
195+
let len = s_str.len();
208196
let cs = consts::ptrcast(
209-
self.const_cstr(s, false),
197+
str_global,
210198
self.type_ptr_to(self.layout_of(self.tcx.types.str_).llvm_type(self)),
211199
);
212200
(cs, self.const_usize(len as u64))

compiler/rustc_codegen_llvm/src/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub struct CodegenCx<'ll, 'tcx> {
5555
pub vtables:
5656
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
5757
/// Cache of constant strings,
58-
pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
58+
pub const_str_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,
5959

6060
/// Reverse-direction for const ptrs cast from globals.
6161
///
@@ -415,7 +415,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
415415
codegen_unit,
416416
instances: Default::default(),
417417
vtables: Default::default(),
418-
const_cstr_cache: Default::default(),
418+
const_str_cache: Default::default(),
419419
const_unsized: Default::default(),
420420
const_globals: Default::default(),
421421
statics_to_rauw: RefCell::new(Vec::new()),

0 commit comments

Comments
 (0)