Skip to content

Commit e5d4ac8

Browse files
committed
add attribute check and removal bindings
1 parent df9080e commit e5d4ac8

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,18 @@ pub(crate) fn has_attr(llfn: &Value, idx: AttributePlace, attr: AttributeKind) -
3232
llvm::HasAttributeAtIndex(llfn, idx, attr)
3333
}
3434

35+
pub(crate) fn has_string_attr(llfn: &Value, name: *const i8) -> bool {
36+
llvm::HasStringAttribute(llfn, name)
37+
}
38+
3539
pub(crate) fn remove_from_llfn(llfn: &Value, place: AttributePlace, kind: AttributeKind) {
3640
llvm::RemoveRustEnumAttributeAtIndex(llfn, place, kind);
3741
}
3842

43+
pub(crate) fn remove_string_attr_from_llfn(llfn: &Value, name: *const i8) {
44+
llvm::RemoveStringAttrFromFn(llfn, name);
45+
}
46+
3947
/// Get LLVM attribute for the provided inline heuristic.
4048
#[inline]
4149
fn inline_attr<'ll>(cx: &CodegenCx<'ll, '_>, inline: InlineAttr) -> Option<&'ll Attribute> {

compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ 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);
2224
}
2325

2426
unsafe extern "C" {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,11 @@ unsafe extern "C" {
11601160

11611161
pub(crate) fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>;
11621162
pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>;
1163-
pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(Fn: &Value, index: c_uint, kind: AttributeKind);
1163+
pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex(
1164+
Fn: &Value,
1165+
index: c_uint,
1166+
kind: AttributeKind,
1167+
);
11641168

11651169
pub(crate) fn LLVMDeleteGlobal(GlobalVar: &Value);
11661170
pub(crate) fn LLVMGetInitializer(GlobalVar: &Value) -> Option<&Value>;

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,27 @@ pub(crate) fn AddFunctionAttributes<'ll>(
4141
}
4242
}
4343

44-
pub(crate) fn HasAttributeAtIndex<'ll>(llfn: &'ll Value, idx: AttributePlace, kind: AttributeKind) -> bool {
44+
pub(crate) fn HasAttributeAtIndex<'ll>(
45+
llfn: &'ll Value,
46+
idx: AttributePlace,
47+
kind: AttributeKind,
48+
) -> bool {
4549
unsafe { LLVMRustHasAttributeAtIndex(llfn, idx.as_uint(), kind) }
4650
}
4751

48-
pub(crate) fn RemoveRustEnumAttributeAtIndex(llfn: &Value, place: AttributePlace, kind: AttributeKind) {
52+
pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: *const i8) -> bool {
53+
unsafe { LLVMRustHasFnAttribute(llfn, name) }
54+
}
55+
56+
pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: *const i8) {
57+
unsafe { LLVMRustRemoveFnAttribute(llfn, name) }
58+
}
59+
60+
pub(crate) fn RemoveRustEnumAttributeAtIndex(
61+
llfn: &Value,
62+
place: AttributePlace,
63+
kind: AttributeKind,
64+
) {
4965
unsafe {
5066
LLVMRustRemoveEnumAttributeAtIndex(llfn, place.as_uint(), kind);
5167
}

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,24 @@ extern "C" void LLVMRustRemoveEnumAttributeAtIndex(LLVMValueRef F, size_t index,
979979
LLVMRemoveEnumAttributeAtIndex(F, index, fromRust(RustAttr));
980980
}
981981

982+
983+
extern "C" bool LLVMRustHasFnAttribute(LLVMValueRef F, const char *Name) {
984+
if (auto *Fn = dyn_cast<Function>(unwrap<Value>(F))) {
985+
return Fn->hasFnAttribute(Name);
986+
}
987+
return false;
988+
}
989+
990+
991+
extern "C" void LLVMRustRemoveFnAttribute(LLVMValueRef Fn, const char *Name) {
992+
auto *F = dyn_cast<Function>(unwrap<Value>(Fn));
993+
assert(F);
994+
assert(F->hasFnAttribute(Name) &&
995+
"Function does not have the attribute to be removed");
996+
F->removeFnAttr(Name);
997+
}
998+
999+
9821000
extern "C" void LLVMRustGlobalAddMetadata(LLVMValueRef Global, unsigned Kind,
9831001
LLVMMetadataRef MD) {
9841002
unwrap<GlobalObject>(Global)->addMetadata(Kind, *unwrap<MDNode>(MD));

0 commit comments

Comments
 (0)