Skip to content

Commit 6e3d017

Browse files
authored
Rollup merge of #143722 - oli-obk:sound-llvm, r=dianqk
Make some "safe" llvm ops actually sound Noticed while doing other refactorings it may cause some extra unnecessary allocations, but the current use sites are rare ones anyway
2 parents d1a5767 + 84eeca2 commit 6e3d017

File tree

4 files changed

+9
-7
lines changed

4 files changed

+9
-7
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ fn create_msvc_imps(
11821182
.filter_map(|val| {
11831183
// Exclude some symbols that we know are not Rust symbols.
11841184
let name = llvm::get_value_name(val);
1185-
if ignored(name) { None } else { Some((val, name)) }
1185+
if ignored(&name) { None } else { Some((val, name)) }
11861186
})
11871187
.map(move |(val, name)| {
11881188
let mut imp_name = prefix.as_bytes().to_vec();

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ fn generate_enzyme_call<'ll>(
306306
// add outer_fn name to ad_name to make it unique, in case users apply autodiff to multiple
307307
// functions. Unwrap will only panic, if LLVM gave us an invalid string.
308308
let name = llvm::get_value_name(outer_fn);
309-
let outer_fn_name = std::str::from_utf8(name).unwrap();
309+
let outer_fn_name = std::str::from_utf8(&name).unwrap();
310310
ad_name.push_str(outer_fn_name);
311311

312312
// Let us assume the user wrote the following function square:

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl<'ll> CodegenCx<'ll, '_> {
429429
// specific rules on what can be cast. So instead of adding a new way to
430430
// generate static initializers that match the static's type, we picked
431431
// the easier option and retroactively change the type of the static item itself.
432-
let name = llvm::get_value_name(g).to_vec();
432+
let name = llvm::get_value_name(g);
433433
llvm::set_value_name(g, b"");
434434

435435
let linkage = llvm::get_linkage(g);

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ pub(crate) fn SetFunctionCallConv(fn_: &Value, cc: CallConv) {
211211
// function.
212212
// For more details on COMDAT sections see e.g., https://www.airs.com/blog/archives/52
213213
pub(crate) fn SetUniqueComdat(llmod: &Module, val: &Value) {
214-
let name_buf = get_value_name(val).to_vec();
214+
let name_buf = get_value_name(val);
215215
let name =
216216
CString::from_vec_with_nul(name_buf).or_else(|buf| CString::new(buf.into_bytes())).unwrap();
217217
set_comdat(llmod, val, &name);
@@ -319,12 +319,14 @@ pub(crate) fn get_param(llfn: &Value, index: c_uint) -> &Value {
319319
}
320320
}
321321

322-
/// Safe wrapper for `LLVMGetValueName2` into a byte slice
323-
pub(crate) fn get_value_name(value: &Value) -> &[u8] {
322+
/// Safe wrapper for `LLVMGetValueName2`
323+
/// Needs to allocate the value, because `set_value_name` will invalidate
324+
/// the pointer.
325+
pub(crate) fn get_value_name(value: &Value) -> Vec<u8> {
324326
unsafe {
325327
let mut len = 0;
326328
let data = LLVMGetValueName2(value, &mut len);
327-
std::slice::from_raw_parts(data.cast(), len)
329+
std::slice::from_raw_parts(data.cast(), len).to_vec()
328330
}
329331
}
330332

0 commit comments

Comments
 (0)