From 30d3ce06742fc5d0eb844239652a0af4d47bb095 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Mon, 21 Feb 2022 11:19:16 -0500 Subject: Add LLVM attributes in batches instead of individually This should improve performance. --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 165 +++++++++-------------- 1 file changed, 60 insertions(+), 105 deletions(-) (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp') diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index c8f31adbfd9..632a7985c79 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -232,142 +232,97 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) { report_fatal_error("bad AttributeKind"); } -template static inline void AddAttribute(T *t, unsigned Index, Attribute Attr) { +template static inline void AddAttributes(T *t, unsigned Index, + LLVMAttributeRef *Attrs, size_t AttrsLen) { + AttrBuilder B(t->getContext()); + for (LLVMAttributeRef Attr : makeArrayRef(Attrs, AttrsLen)) + B.addAttribute(unwrap(Attr)); + AttributeList PAL = t->getAttributes(); + AttributeList PALNew; #if LLVM_VERSION_LT(14, 0) - t->addAttribute(Index, Attr); + PALNew = PAL.addAttributes(t->getContext(), Index, B); #else - t->addAttributeAtIndex(Index, Attr); + PALNew = PAL.addAttributesAtIndex(t->getContext(), Index, B); #endif + t->setAttributes(PALNew); } -extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index, - LLVMRustAttribute RustAttr) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr)); - AddAttribute(Call, Index, Attr); -} - -extern "C" void LLVMRustAddCallSiteAttrString(LLVMValueRef Instr, unsigned Index, - const char *Name) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::get(Call->getContext(), Name); - AddAttribute(Call, Index, Attr); -} - -extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr, - unsigned Index, - uint32_t Bytes) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::getWithAlignment(Call->getContext(), Align(Bytes)); - AddAttribute(Call, Index, Attr); +template static inline void RemoveAttributes(T *t, unsigned Index, + LLVMRustAttribute *RustAttrs, + size_t RustAttrsLen) { + AttributeMask Mask; + for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen)) + Mask.addAttribute(fromRust(RustAttr)); + AttributeList PAL = t->getAttributes(); + AttributeList PALNew; +#if LLVM_VERSION_LT(14, 0) + PALNew = PAL.removeAttributes(t->getContext(), Index, Mask); +#else + PALNew = PAL.removeAttributesAtIndex(t->getContext(), Index, Mask); +#endif + t->setAttributes(PALNew); } -extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr, - unsigned Index, - uint64_t Bytes) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::getWithDereferenceableBytes(Call->getContext(), Bytes); - AddAttribute(Call, Index, Attr); +extern "C" void LLVMRustAddFunctionAttributes(LLVMValueRef Fn, unsigned Index, + LLVMAttributeRef *Attrs, size_t AttrsLen) { + Function *F = unwrap(Fn); + AddAttributes(F, Index, Attrs, AttrsLen); } -extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr, - unsigned Index, - uint64_t Bytes) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::getWithDereferenceableOrNullBytes(Call->getContext(), Bytes); - AddAttribute(Call, Index, Attr); +extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn, unsigned Index, + LLVMRustAttribute *RustAttrs, + size_t RustAttrsLen) { + Function *F = unwrap(Fn); + RemoveAttributes(F, Index, RustAttrs, RustAttrsLen); } -extern "C" void LLVMRustAddByValCallSiteAttr(LLVMValueRef Instr, unsigned Index, - LLVMTypeRef Ty) { +extern "C" void LLVMRustAddCallSiteAttributes(LLVMValueRef Instr, unsigned Index, + LLVMAttributeRef *Attrs, size_t AttrsLen) { CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::getWithByValType(Call->getContext(), unwrap(Ty)); - AddAttribute(Call, Index, Attr); + AddAttributes(Call, Index, Attrs, AttrsLen); } -extern "C" void LLVMRustAddStructRetCallSiteAttr(LLVMValueRef Instr, unsigned Index, - LLVMTypeRef Ty) { - CallBase *Call = unwrap(Instr); - Attribute Attr = Attribute::getWithStructRetType(Call->getContext(), unwrap(Ty)); - AddAttribute(Call, Index, Attr); +extern "C" LLVMAttributeRef LLVMRustCreateAttrNoValue(LLVMContextRef C, + LLVMRustAttribute RustAttr) { + return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr))); } -extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index, - LLVMRustAttribute RustAttr) { - Function *A = unwrap(Fn); - Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr)); - AddAttribute(A, Index, Attr); +extern "C" LLVMAttributeRef LLVMRustCreateAttrStringValue(LLVMContextRef C, + const char *Name, + const char *Value) { + return wrap(Attribute::get(*unwrap(C), StringRef(Name), StringRef(Value))); } -extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn, - unsigned Index, - uint32_t Bytes) { - Function *A = unwrap(Fn); - AddAttribute(A, Index, Attribute::getWithAlignment( - A->getContext(), llvm::Align(Bytes))); +extern "C" LLVMAttributeRef LLVMRustCreateAlignmentAttr(LLVMContextRef C, + uint64_t Bytes) { + return wrap(Attribute::getWithAlignment(*unwrap(C), llvm::Align(Bytes))); } -extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index, - uint64_t Bytes) { - Function *A = unwrap(Fn); - AddAttribute(A, Index, Attribute::getWithDereferenceableBytes(A->getContext(), - Bytes)); +extern "C" LLVMAttributeRef LLVMRustCreateDereferenceableAttr(LLVMContextRef C, + uint64_t Bytes) { + return wrap(Attribute::getWithDereferenceableBytes(*unwrap(C), Bytes)); } -extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn, - unsigned Index, - uint64_t Bytes) { - Function *A = unwrap(Fn); - AddAttribute(A, Index, Attribute::getWithDereferenceableOrNullBytes( - A->getContext(), Bytes)); +extern "C" LLVMAttributeRef LLVMRustCreateDereferenceableOrNullAttr(LLVMContextRef C, + uint64_t Bytes) { + return wrap(Attribute::getWithDereferenceableOrNullBytes(*unwrap(C), Bytes)); } -extern "C" void LLVMRustAddByValAttr(LLVMValueRef Fn, unsigned Index, - LLVMTypeRef Ty) { - Function *F = unwrap(Fn); - Attribute Attr = Attribute::getWithByValType(F->getContext(), unwrap(Ty)); - AddAttribute(F, Index, Attr); +extern "C" LLVMAttributeRef LLVMRustCreateByValAttr(LLVMContextRef C, LLVMTypeRef Ty) { + return wrap(Attribute::getWithByValType(*unwrap(C), unwrap(Ty))); } -extern "C" void LLVMRustAddStructRetAttr(LLVMValueRef Fn, unsigned Index, - LLVMTypeRef Ty) { - Function *F = unwrap(Fn); - Attribute Attr = Attribute::getWithStructRetType(F->getContext(), unwrap(Ty)); - AddAttribute(F, Index, Attr); +extern "C" LLVMAttributeRef LLVMRustCreateStructRetAttr(LLVMContextRef C, LLVMTypeRef Ty) { + return wrap(Attribute::getWithStructRetType(*unwrap(C), unwrap(Ty))); } -extern "C" void LLVMRustEmitUWTableAttr(LLVMValueRef Fn, bool Async) { - Function *F = unwrap(Fn); +extern "C" LLVMAttributeRef LLVMRustCreateUWTableAttr(LLVMContextRef C, bool Async) { #if LLVM_VERSION_LT(15, 0) - Attribute Attr = Attribute::get(F->getContext(), Attribute::UWTable); -#else - Attribute Attr = Attribute::getWithUWTableKind( - F->getContext(), Async ? UWTableKind::Async : UWTableKind::Sync); -#endif - AddAttribute(F, AttributeList::AttrIndex::FunctionIndex, Attr); -} - -extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn, - unsigned Index, - const char *Name, - const char *Value) { - Function *F = unwrap(Fn); - AddAttribute(F, Index, Attribute::get( - F->getContext(), StringRef(Name), StringRef(Value))); -} - -extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn, - unsigned Index, - LLVMRustAttribute RustAttr) { - Function *F = unwrap(Fn); - AttributeList PAL = F->getAttributes(); - AttributeList PALNew; -#if LLVM_VERSION_LT(14, 0) - PALNew = PAL.removeAttribute(F->getContext(), Index, fromRust(RustAttr)); + return wrap(Attribute::get(*unwrap(C), Attribute::UWTable)); #else - PALNew = PAL.removeAttributeAtIndex(F->getContext(), Index, fromRust(RustAttr)); + return wrap(Attribute::getWithUWTableKind( + *unwrap(C), Async ? UWTableKind::Async : UWTableKind::Sync)); #endif - F->setAttributes(PALNew); } // Enable a fast-math flag -- cgit 1.4.1-3-g733a5 From ac9f4f7d0de3986246a19678919ff15252870b15 Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Sat, 26 Feb 2022 16:58:45 -0500 Subject: use attrbuilder to remove attrs in old LLVM --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp') diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 632a7985c79..1ba54ca0779 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -250,14 +250,17 @@ template static inline void AddAttributes(T *t, unsigned Index, template static inline void RemoveAttributes(T *t, unsigned Index, LLVMRustAttribute *RustAttrs, size_t RustAttrsLen) { - AttributeMask Mask; - for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen)) - Mask.addAttribute(fromRust(RustAttr)); AttributeList PAL = t->getAttributes(); AttributeList PALNew; #if LLVM_VERSION_LT(14, 0) - PALNew = PAL.removeAttributes(t->getContext(), Index, Mask); + AttrBuilder B(t->getContext()); + for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen)) + B.addAttribute(fromRust(RustAttr)); + PALNew = PAL.removeAttributes(t->getContext(), Index, B); #else + AttributeMask Mask; + for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen)) + Mask.addAttribute(fromRust(RustAttr)); PALNew = PAL.removeAttributesAtIndex(t->getContext(), Index, Mask); #endif t->setAttributes(PALNew); -- cgit 1.4.1-3-g733a5 From 0d0cc4f6a0cb48600f183c382986df1897bdb7dc Mon Sep 17 00:00:00 2001 From: Erik Desjardins Date: Sat, 26 Feb 2022 17:16:01 -0500 Subject: AttrBuilder doesn't take a context in old LLVM --- compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp') diff --git a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp index 1ba54ca0779..d627af48ba5 100644 --- a/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp +++ b/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp @@ -234,14 +234,17 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) { template static inline void AddAttributes(T *t, unsigned Index, LLVMAttributeRef *Attrs, size_t AttrsLen) { - AttrBuilder B(t->getContext()); - for (LLVMAttributeRef Attr : makeArrayRef(Attrs, AttrsLen)) - B.addAttribute(unwrap(Attr)); AttributeList PAL = t->getAttributes(); AttributeList PALNew; #if LLVM_VERSION_LT(14, 0) + AttrBuilder B; + for (LLVMAttributeRef Attr : makeArrayRef(Attrs, AttrsLen)) + B.addAttribute(unwrap(Attr)); PALNew = PAL.addAttributes(t->getContext(), Index, B); #else + AttrBuilder B(t->getContext()); + for (LLVMAttributeRef Attr : makeArrayRef(Attrs, AttrsLen)) + B.addAttribute(unwrap(Attr)); PALNew = PAL.addAttributesAtIndex(t->getContext(), Index, B); #endif t->setAttributes(PALNew); @@ -253,7 +256,7 @@ template static inline void RemoveAttributes(T *t, unsigned Index, AttributeList PAL = t->getAttributes(); AttributeList PALNew; #if LLVM_VERSION_LT(14, 0) - AttrBuilder B(t->getContext()); + AttrBuilder B; for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen)) B.addAttribute(fromRust(RustAttr)); PALNew = PAL.removeAttributes(t->getContext(), Index, B); -- cgit 1.4.1-3-g733a5