Skip to content

Commit 0b471bf

Browse files
authored
Rollup merge of #67033 - cuviper:ValueName2, r=rkruppe
Migrate to LLVM{Get,Set}ValueName2 The deprecated `LLVM{Get,Set}ValueName` only work with NUL-terminated strings, but the `2` variants use explicit lengths, which fits better with Rust strings and slices. We now use these in new helper functions `llvm::{get,set}_value_name` that convert to/from `&[u8]`. Closes #64223. r? @rkruppe
2 parents fd4cec0 + 16d2178 commit 0b471bf

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
lines changed

src/librustc_codegen_llvm/back/write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_fs_util::{path_to_c_string, link_or_copy};
2222
use rustc_data_structures::small_c_str::SmallCStr;
2323
use errors::{Handler, FatalError};
2424

25-
use std::ffi::{CString, CStr};
25+
use std::ffi::CString;
2626
use std::fs;
2727
use std::io::{self, Write};
2828
use std::path::{Path, PathBuf};
@@ -833,16 +833,16 @@ fn create_msvc_imps(
833833
})
834834
.filter_map(|val| {
835835
// Exclude some symbols that we know are not Rust symbols.
836-
let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
837-
if ignored(name.to_bytes()) {
836+
let name = llvm::get_value_name(val);
837+
if ignored(name) {
838838
None
839839
} else {
840840
Some((val, name))
841841
}
842842
})
843843
.map(move |(val, name)| {
844844
let mut imp_name = prefix.as_bytes().to_vec();
845-
imp_name.extend(name.to_bytes());
845+
imp_name.extend(name);
846846
let imp_name = CString::new(imp_name).unwrap();
847847
(imp_name, val)
848848
})

src/librustc_codegen_llvm/consts.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc::ty::layout::{self, Size, Align, LayoutOf};
2121

2222
use rustc::hir::{self, CodegenFnAttrs, CodegenFnAttrFlags};
2323

24-
use std::ffi::{CStr, CString};
24+
use std::ffi::CStr;
2525

2626
pub fn const_alloc_to_llvm(cx: &CodegenCx<'ll, '_>, alloc: &Allocation) -> &'ll Value {
2727
let mut llvals = Vec::with_capacity(alloc.relocations().len() + 1);
@@ -392,16 +392,14 @@ impl StaticMethods for CodegenCx<'ll, 'tcx> {
392392
} else {
393393
// If we created the global with the wrong type,
394394
// correct the type.
395-
let empty_string = const_cstr!("");
396-
let name_str_ref = CStr::from_ptr(llvm::LLVMGetValueName(g));
397-
let name_string = CString::new(name_str_ref.to_bytes()).unwrap();
398-
llvm::LLVMSetValueName(g, empty_string.as_ptr());
395+
let name = llvm::get_value_name(g).to_vec();
396+
llvm::set_value_name(g, b"");
399397

400398
let linkage = llvm::LLVMRustGetLinkage(g);
401399
let visibility = llvm::LLVMRustGetVisibility(g);
402400

403401
let new_g = llvm::LLVMRustGetOrInsertGlobal(
404-
self.llmod, name_string.as_ptr(), val_llty);
402+
self.llmod, name.as_ptr().cast(), name.len(), val_llty);
405403

406404
llvm::LLVMRustSetLinkage(new_g, linkage);
407405
llvm::LLVMRustSetVisibility(new_g, visibility);

src/librustc_codegen_llvm/debuginfo/mod.rs

+6-18
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use rustc_codegen_ssa::mir::debuginfo::{FunctionDebugContext, DebugScope,
3232

3333
use libc::c_uint;
3434
use std::cell::RefCell;
35-
use std::ffi::{CStr, CString};
35+
use std::ffi::CString;
3636

3737
use smallvec::SmallVec;
3838
use syntax_pos::{self, BytePos, Span, Pos};
@@ -255,23 +255,11 @@ impl DebugInfoBuilderMethods<'tcx> for Builder<'a, 'll, 'tcx> {
255255
return;
256256
}
257257

258-
let old_name = unsafe {
259-
CStr::from_ptr(llvm::LLVMGetValueName(value))
260-
};
261-
match old_name.to_str() {
262-
Ok("") => {}
263-
Ok(_) => {
264-
// Avoid replacing the name if it already exists.
265-
// While we could combine the names somehow, it'd
266-
// get noisy quick, and the usefulness is dubious.
267-
return;
268-
}
269-
Err(_) => return,
270-
}
271-
272-
let cname = SmallCStr::new(name);
273-
unsafe {
274-
llvm::LLVMSetValueName(value, cname.as_ptr());
258+
// Avoid replacing the name if it already exists.
259+
// While we could combine the names somehow, it'd
260+
// get noisy quick, and the usefulness is dubious.
261+
if llvm::get_value_name(value).is_empty() {
262+
llvm::set_value_name(value, name.as_bytes());
275263
}
276264
}
277265
}

src/librustc_codegen_llvm/declare.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ impl DeclareMethods<'tcx> for CodegenCx<'ll, 'tcx> {
7676
name: &str, ty: &'ll Type
7777
) -> &'ll Value {
7878
debug!("declare_global(name={:?})", name);
79-
let namebuf = SmallCStr::new(name);
8079
unsafe {
81-
llvm::LLVMRustGetOrInsertGlobal(self.llmod, namebuf.as_ptr(), ty)
80+
llvm::LLVMRustGetOrInsertGlobal(self.llmod, name.as_ptr().cast(), name.len(), ty)
8281
}
8382
}
8483

src/librustc_codegen_llvm/llvm/ffi.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -701,8 +701,8 @@ extern "C" {
701701

702702
// Operations on all values
703703
pub fn LLVMTypeOf(Val: &Value) -> &Type;
704-
pub fn LLVMGetValueName(Val: &Value) -> *const c_char;
705-
pub fn LLVMSetValueName(Val: &Value, Name: *const c_char);
704+
pub fn LLVMGetValueName2(Val: &Value, Length: *mut size_t) -> *const c_char;
705+
pub fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t);
706706
pub fn LLVMReplaceAllUsesWith(OldVal: &'a Value, NewVal: &'a Value);
707707
pub fn LLVMSetMetadata(Val: &'a Value, KindID: c_uint, Node: &'a Value);
708708

@@ -774,7 +774,8 @@ extern "C" {
774774
pub fn LLVMIsAGlobalVariable(GlobalVar: &Value) -> Option<&Value>;
775775
pub fn LLVMAddGlobal(M: &'a Module, Ty: &'a Type, Name: *const c_char) -> &'a Value;
776776
pub fn LLVMGetNamedGlobal(M: &Module, Name: *const c_char) -> Option<&Value>;
777-
pub fn LLVMRustGetOrInsertGlobal(M: &'a Module, Name: *const c_char, T: &'a Type) -> &'a Value;
777+
pub fn LLVMRustGetOrInsertGlobal(M: &'a Module, Name: *const c_char, NameLen: size_t,
778+
T: &'a Type) -> &'a Value;
778779
pub fn LLVMRustInsertPrivateGlobal(M: &'a Module, T: &'a Type) -> &'a Value;
779780
pub fn LLVMGetFirstGlobal(M: &Module) -> Option<&Value>;
780781
pub fn LLVMGetNextGlobal(GlobalVar: &Value) -> Option<&Value>;
@@ -1811,7 +1812,7 @@ extern "C" {
18111812

18121813
pub fn LLVMRustPositionBuilderAtStart(B: &Builder<'a>, BB: &'a BasicBlock);
18131814

1814-
pub fn LLVMRustSetComdat(M: &'a Module, V: &'a Value, Name: *const c_char);
1815+
pub fn LLVMRustSetComdat(M: &'a Module, V: &'a Value, Name: *const c_char, NameLen: size_t);
18151816
pub fn LLVMRustUnsetComdat(V: &Value);
18161817
pub fn LLVMRustSetModulePICLevel(M: &Module);
18171818
pub fn LLVMRustSetModulePIELevel(M: &Module);

src/librustc_codegen_llvm/llvm/mod.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ pub fn SetFunctionCallConv(fn_: &'a Value, cc: CallConv) {
115115
// For more details on COMDAT sections see e.g., http://www.airs.com/blog/archives/52
116116
pub fn SetUniqueComdat(llmod: &Module, val: &'a Value) {
117117
unsafe {
118-
LLVMRustSetComdat(llmod, val, LLVMGetValueName(val));
118+
let name = get_value_name(val);
119+
LLVMRustSetComdat(llmod, val, name.as_ptr().cast(), name.len());
119120
}
120121
}
121122

@@ -217,6 +218,23 @@ pub fn get_param(llfn: &'a Value, index: c_uint) -> &'a Value {
217218
}
218219
}
219220

221+
/// Safe wrapper for `LLVMGetValueName2` into a byte slice
222+
pub fn get_value_name(value: &'a Value) -> &'a [u8] {
223+
unsafe {
224+
let mut len = 0;
225+
let data = LLVMGetValueName2(value, &mut len);
226+
std::slice::from_raw_parts(data.cast(), len)
227+
}
228+
}
229+
230+
/// Safe wrapper for `LLVMSetValueName2` from a byte slice
231+
pub fn set_value_name(value: &Value, name: &[u8]) {
232+
unsafe {
233+
let data = name.as_ptr().cast();
234+
LLVMSetValueName2(value, data, name.len());
235+
}
236+
}
237+
220238
pub fn build_string(f: impl FnOnce(&RustString)) -> Result<String, FromUtf8Error> {
221239
let sr = RustString {
222240
bytes: RefCell::new(Vec::new()),

src/rustllvm/RustWrapper.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ extern "C" LLVMValueRef LLVMRustGetOrInsertFunction(LLVMModuleRef M,
129129
}
130130

131131
extern "C" LLVMValueRef
132-
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, LLVMTypeRef Ty) {
133-
return wrap(unwrap(M)->getOrInsertGlobal(Name, unwrap(Ty)));
132+
LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) {
133+
StringRef NameRef(Name, NameLen);
134+
return wrap(unwrap(M)->getOrInsertGlobal(NameRef, unwrap(Ty)));
134135
}
135136

136137
extern "C" LLVMValueRef
@@ -1287,11 +1288,12 @@ extern "C" void LLVMRustPositionBuilderAtStart(LLVMBuilderRef B,
12871288
}
12881289

12891290
extern "C" void LLVMRustSetComdat(LLVMModuleRef M, LLVMValueRef V,
1290-
const char *Name) {
1291+
const char *Name, size_t NameLen) {
12911292
Triple TargetTriple(unwrap(M)->getTargetTriple());
12921293
GlobalObject *GV = unwrap<GlobalObject>(V);
12931294
if (!TargetTriple.isOSBinFormatMachO()) {
1294-
GV->setComdat(unwrap(M)->getOrInsertComdat(Name));
1295+
StringRef NameRef(Name, NameLen);
1296+
GV->setComdat(unwrap(M)->getOrInsertComdat(NameRef));
12951297
}
12961298
}
12971299

0 commit comments

Comments
 (0)