Skip to content

Commit 30f55e0

Browse files
committed
remove CString dependence with StringRef
1 parent bbb9f6c commit 30f55e0

File tree

5 files changed

+21
-22
lines changed

5 files changed

+21
-22
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
//! Set and unset common attributes on LLVM values.
2-
3-
use std::ffi::c_char;
4-
52
use rustc_attr_parsing::{InlineAttr, InstructionSetAttr, OptimizeAttr};
63
use rustc_codegen_ssa::traits::*;
74
use rustc_hir::def_id::DefId;
@@ -34,15 +31,15 @@ pub(crate) fn has_attr(llfn: &Value, idx: AttributePlace, attr: AttributeKind) -
3431
llvm::HasAttributeAtIndex(llfn, idx, attr)
3532
}
3633

37-
pub(crate) fn has_string_attr(llfn: &Value, name: *const c_char) -> bool {
34+
pub(crate) fn has_string_attr(llfn: &Value, name: &str) -> bool {
3835
llvm::HasStringAttribute(llfn, name)
3936
}
4037

4138
pub(crate) fn remove_from_llfn(llfn: &Value, place: AttributePlace, kind: AttributeKind) {
4239
llvm::RemoveRustEnumAttributeAtIndex(llfn, place, kind);
4340
}
4441

45-
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: *const c_char) {
42+
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: &str) {
4643
llvm::RemoveStringAttrFromFn(llfn, name);
4744
}
4845

compiler/rustc_codegen_llvm/src/back/lto.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -671,21 +671,19 @@ pub(crate) fn run_pass_manager(
671671
SimpleCx::new(module.module_llvm.llmod(), &module.module_llvm.llcx, cgcx.pointer_size);
672672

673673
for function in cx.get_functions() {
674-
let enzyme_marker = CString::new("enzyme_marker").unwrap();
675-
let marker_ptr = enzyme_marker.as_ptr();
676-
677-
if attributes::has_string_attr(function, marker_ptr) {
674+
let enzyme_marker = "enzyme_marker";
675+
if attributes::has_string_attr(function, enzyme_marker) {
678676
// Sanity check: Ensure 'noinline' is present before replacing it.
679677
assert!(
680678
!attributes::has_attr(function, Function, llvm::AttributeKind::NoInline),
681679
"Expected __enzyme function to have 'noinline' before adding 'alwaysinline'"
682680
);
683681

684682
attributes::remove_from_llfn(function, Function, llvm::AttributeKind::NoInline);
685-
attributes::remove_string_attr_from_llfn(function, marker_ptr);
683+
attributes::remove_string_attr_from_llfn(function, enzyme_marker);
686684

687685
assert!(
688-
!attributes::has_string_attr(function, marker_ptr),
686+
!attributes::has_string_attr(function, enzyme_marker),
689687
"Expected function to not have 'enzyme_marker'"
690688
);
691689

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ unsafe extern "C" {
1919
pub(crate) fn LLVMRustVerifyFunction(V: &Value, action: LLVMRustVerifierFailureAction) -> Bool;
2020
pub(crate) fn LLVMRustHasAttributeAtIndex(V: &Value, i: c_uint, Kind: AttributeKind) -> bool;
2121
pub(crate) fn LLVMRustGetArrayNumElements(Ty: &Type) -> u64;
22-
pub(crate) fn LLVMRustHasFnAttribute(F: &Value, Name: *const c_char) -> bool;
23-
pub(crate) fn LLVMRustRemoveFnAttribute(F: &Value, Name: *const c_char);
22+
pub(crate) fn LLVMRustHasFnAttribute(
23+
F: &Value,
24+
Name: *const c_char,
25+
NameLen: libc::size_t,
26+
) -> bool;
27+
pub(crate) fn LLVMRustRemoveFnAttribute(F: &Value, Name: *const c_char, NameLen: libc::size_t);
2428
pub(crate) fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>;
2529
pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>;
2630
pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(

compiler/rustc_codegen_llvm/src/llvm/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(non_snake_case)]
22

3-
use std::ffi::{CStr, CString, c_char};
3+
use std::ffi::{CStr, CString};
44
use std::ptr;
55
use std::str::FromStr;
66
use std::string::FromUtf8Error;
@@ -49,12 +49,12 @@ pub(crate) fn HasAttributeAtIndex<'ll>(
4949
unsafe { LLVMRustHasAttributeAtIndex(llfn, idx.as_uint(), kind) }
5050
}
5151

52-
pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: *const c_char) -> bool {
53-
unsafe { LLVMRustHasFnAttribute(llfn, name) }
52+
pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool {
53+
unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
5454
}
5555

56-
pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: *const c_char) {
57-
unsafe { LLVMRustRemoveFnAttribute(llfn, name) }
56+
pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) {
57+
unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) }
5858
}
5959

6060
pub(crate) fn RemoveRustEnumAttributeAtIndex(

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -979,16 +979,16 @@ LLVMRustRemoveEnumAttributeAtIndex(LLVMValueRef F, size_t index,
979979
LLVMRemoveEnumAttributeAtIndex(F, index, fromRust(RustAttr));
980980
}
981981

982-
extern "C" bool LLVMRustHasFnAttribute(LLVMValueRef F, const char *Name) {
982+
extern "C" bool LLVMRustHasFnAttribute(LLVMValueRef F, const char *Name, size_t NameLen) {
983983
if (auto *Fn = dyn_cast<Function>(unwrap<Value>(F))) {
984-
return Fn->hasFnAttribute(Name);
984+
return Fn->hasFnAttribute(StringRef(Name, NameLen));
985985
}
986986
return false;
987987
}
988988

989-
extern "C" void LLVMRustRemoveFnAttribute(LLVMValueRef Fn, const char *Name) {
989+
extern "C" void LLVMRustRemoveFnAttribute(LLVMValueRef Fn, const char *Name, size_t NameLen) {
990990
if (auto *F = dyn_cast<Function>(unwrap<Value>(Fn))) {
991-
F->removeFnAttr(Name);
991+
F->removeFnAttr(StringRef(Name, NameLen));
992992
}
993993
}
994994

0 commit comments

Comments
 (0)