Skip to content

Commit 84eeca2

Browse files
committed
Make some "safe" llvm ops actually sound
1 parent 6b3ae3f commit 84eeca2

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
@@ -430,7 +430,7 @@ impl<'ll> CodegenCx<'ll, '_> {
430430
// specific rules on what can be cast. So instead of adding a new way to
431431
// generate static initializers that match the static's type, we picked
432432
// the easier option and retroactively change the type of the static item itself.
433-
let name = llvm::get_value_name(g).to_vec();
433+
let name = llvm::get_value_name(g);
434434
llvm::set_value_name(g, b"");
435435

436436
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)