Skip to content

Commit d1c94af

Browse files
committed
add and use better abstractions for Attribute
1 parent 84968bf commit d1c94af

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ use crate::typetree::to_enzyme_typetree;
1717
use crate::LlvmCodegenBackend;
1818
use crate::ModuleLlvm;
1919
use crate::DiffTypeTree;
20-
use llvm::{
20+
use llvm::{LLVMRustGetEnumAttributeAtIndex, LLVMRustAddEnumAttributeAtIndex, LLVMRustRemoveEnumAttributeAtIndex,
2121
enzyme_rust_forward_diff, enzyme_rust_reverse_diff, BasicBlock, CreateEnzymeLogic,
2222
CreateTypeAnalysis, EnzymeLogicRef, EnzymeTypeAnalysisRef, LLVMAddFunction,
2323
LLVMAppendBasicBlockInContext, LLVMBuildCall2, LLVMBuildExtractValue, LLVMBuildRet,
2424
LLVMCountParams, LLVMCountStructElementTypes, LLVMCreateBuilderInContext, LLVMDeleteFunction,
2525
LLVMDisposeBuilder, LLVMGetBasicBlockTerminator, LLVMGetElementType, LLVMGetModuleContext,
2626
LLVMGetParams, LLVMGetReturnType, LLVMPositionBuilderAtEnd, LLVMSetValueName2, LLVMTypeOf,
2727
LLVMVoidTypeInContext, LLVMGlobalGetValueType, LLVMGetStringAttributeAtIndex,
28-
LLVMIsStringAttribute, LLVMRemoveStringAttributeAtIndex, LLVMRemoveEnumAttributeAtIndex, AttributeKind,
29-
LLVMGetFirstFunction, LLVMGetNextFunction, LLVMGetEnumAttributeAtIndex, LLVMIsEnumAttribute,
30-
LLVMCreateStringAttribute, LLVMRustAddFunctionAttributes, LLVMCreateEnumAttribute, LLVMDumpModule,
28+
LLVMIsStringAttribute, LLVMRemoveStringAttributeAtIndex, AttributeKind,
29+
LLVMGetFirstFunction, LLVMGetNextFunction, LLVMIsEnumAttribute,
30+
LLVMCreateStringAttribute, LLVMRustAddFunctionAttributes, LLVMDumpModule,
3131
LLVMRustLLVMHasZlibCompressionForDebugSymbols, LLVMRustLLVMHasZstdCompressionForDebugSymbols,
3232
};
3333
use rustc_codegen_ssa::back::link::ensure_removed;
@@ -831,7 +831,7 @@ pub(crate) unsafe fn differentiate(
831831
if LLVMIsStringAttribute(attr) {
832832
LLVMRemoveStringAttributeAtIndex(lf, c_uint::MAX, myhwattr.as_ptr() as *const c_char, myhwattr.as_bytes().len() as c_uint);
833833
} else {
834-
LLVMRemoveEnumAttributeAtIndex(lf, c_uint::MAX, AttributeKind::SanitizeHWAddress);
834+
LLVMRustRemoveEnumAttributeAtIndex(lf, c_uint::MAX, AttributeKind::SanitizeHWAddress);
835835
}
836836

837837

@@ -876,13 +876,12 @@ pub(crate) unsafe fn optimize(
876876
f = LLVMGetNextFunction(lf);
877877
let myhwattr = "enzyme_hw";
878878
let myhwv = "";
879-
let prevattr = LLVMGetEnumAttributeAtIndex(lf, c_uint::MAX, AttributeKind::SanitizeHWAddress);
879+
let prevattr = LLVMRustGetEnumAttributeAtIndex(lf, c_uint::MAX, AttributeKind::SanitizeHWAddress);
880880
if LLVMIsEnumAttribute(prevattr) {
881881
let attr = LLVMCreateStringAttribute(llcx, myhwattr.as_ptr() as *const c_char, myhwattr.as_bytes().len() as c_uint, myhwv.as_ptr() as *const c_char, myhwv.as_bytes().len() as c_uint);
882882
LLVMRustAddFunctionAttributes(lf, c_uint::MAX, &attr, 1);
883883
} else {
884-
let attr = LLVMCreateEnumAttribute(llcx, AttributeKind::SanitizeHWAddress, 0);
885-
LLVMRustAddFunctionAttributes(lf, c_uint::MAX, &attr, 1);
884+
LLVMRustAddEnumAttributeAtIndex(llcx, lf, c_uint::MAX, AttributeKind::SanitizeHWAddress);
886885
}
887886

888887
} else {

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub enum AttributeKind {
191191
OptimizeNone = 24,
192192
ReturnsTwice = 25,
193193
ReadNone = 26,
194-
SanitizeHWAddress = 51,
194+
SanitizeHWAddress = 28,
195195
WillReturn = 29,
196196
StackProtectReq = 30,
197197
StackProtectStrong = 31,
@@ -980,19 +980,21 @@ pub type GetSymbolsErrorCallback = unsafe extern "C" fn(*const c_char) -> *mut c
980980
extern "C" {
981981

982982
// Enzyme
983-
//pub fn LLVMReplaceAllUsesWith(old: &Value, new: &Value);
984-
pub fn GibtsNicht(M: &Module) -> bool;
985983
pub fn LLVMIsStructTy(ty: &Type) -> bool;
986984
pub fn LLVMGetReturnType(T: &Type) -> &Type;
987985
pub fn LLVMDumpModule(M: &Module);
988986
pub fn LLVMCountStructElementTypes(T: &Type) -> c_uint;
989987
pub fn LLVMDeleteFunction(V: &Value);
988+
989+
pub fn LLVMCreateEnumAttribute(C : &Context, Kind: Attribute, val:u64) -> &Attribute;
990990
pub fn LLVMRemoveStringAttributeAtIndex(F : &Value, Idx: c_uint, K: *const c_char, KLen : c_uint);
991991
pub fn LLVMGetStringAttributeAtIndex(F : &Value, Idx: c_uint, K: *const c_char, KLen : c_uint) -> &Attribute;
992-
pub fn LLVMRemoveEnumAttributeAtIndex(F : &Value, Idx: c_uint, K: AttributeKind);
993-
pub fn LLVMGetEnumAttributeAtIndex(F : &Value, Idx: c_uint, K: AttributeKind) -> &Attribute;
992+
993+
pub fn LLVMAddAttributeAtIndex(F : &Value, Idx: c_uint, K: &Attribute);
994+
pub fn LLVMRemoveEnumAttributeAtIndex(F : &Value, Idx: c_uint, K: Attribute);
995+
pub fn LLVMGetEnumAttributeAtIndex(F : &Value, Idx: c_uint, K: Attribute) -> &Attribute;
996+
994997
pub fn LLVMIsEnumAttribute(A : &Attribute) -> bool;
995-
pub fn LLVMCreateEnumAttribute(C : &Context, Kind: AttributeKind, val:u64) -> &Attribute;
996998
pub fn LLVMIsStringAttribute(A : &Attribute) -> bool;
997999
pub fn LLVMVerifyFunction(V: &Value, action: LLVMVerifierFailureAction) -> bool;
9981000
pub fn LLVMGetParams(Fnc: &Value, parms: *mut &Value);
@@ -1207,6 +1209,9 @@ extern "C" {
12071209

12081210
// Operations on attributes
12091211
pub fn LLVMRustCreateAttrNoValue(C: &Context, attr: AttributeKind) -> &Attribute;
1212+
pub fn LLVMRustAddEnumAttributeAtIndex(C: &Context, V: &Value, index: c_uint, attr: AttributeKind);
1213+
pub fn LLVMRustRemoveEnumAttributeAtIndex(V: &Value, index: c_uint, attr: AttributeKind);
1214+
pub fn LLVMRustGetEnumAttributeAtIndex(V: &Value, index: c_uint, attr: AttributeKind) ->&Attribute;
12101215
pub fn LLVMCreateStringAttribute(
12111216
C: &Context,
12121217
Name: *const c_char,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,19 @@ extern "C" LLVMAttributeRef LLVMRustCreateAttrNoValue(LLVMContextRef C,
319319
return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr)));
320320
}
321321

322+
extern "C" void LLVMRustRemoveEnumAttributeAtIndex(LLVMValueRef F, size_t index, LLVMRustAttribute RustAttr) {
323+
LLVMRemoveEnumAttributeAtIndex(F, index, fromRust(RustAttr));
324+
}
325+
326+
extern "C" void LLVMRustAddEnumAttributeAtIndex(LLVMContextRef C, LLVMValueRef F, size_t index, LLVMRustAttribute RustAttr) {
327+
LLVMAddAttributeAtIndex(F, index, LLVMRustCreateAttrNoValue(C, RustAttr));
328+
}
329+
330+
extern "C" LLVMAttributeRef LLVMRustGetEnumAttributeAtIndex(LLVMValueRef F, size_t index,
331+
LLVMRustAttribute RustAttr) {
332+
return LLVMGetEnumAttributeAtIndex(F, index, fromRust(RustAttr));
333+
}
334+
322335
extern "C" LLVMAttributeRef LLVMRustCreateAlignmentAttr(LLVMContextRef C,
323336
uint64_t Bytes) {
324337
return wrap(Attribute::getWithAlignment(*unwrap(C), llvm::Align(Bytes)));

0 commit comments

Comments
 (0)