@@ -203,29 +203,67 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
203203 report_fatal_error (" bad AttributeKind" );
204204}
205205
206+ template <typename T> static inline void AddAttribute (T *t, unsigned Index, Attribute Attr) {
207+ #if LLVM_VERSION_LT(14, 0)
208+ t->addAttribute (Index, Attr);
209+ #else
210+ // TODO(durin42): we should probably surface the explicit functions to Rust
211+ // instead of this switch statement?
212+ switch (Index) {
213+ case AttributeList::ReturnIndex:
214+ t->addRetAttr (Attr);
215+ break ;
216+ case AttributeList::FunctionIndex:
217+ t->addFnAttr (Attr);
218+ break ;
219+ default :
220+ t->addParamAttr (Index-AttributeList::FirstArgIndex, Attr);
221+ }
222+ #endif
223+ }
224+
206225extern " C" void LLVMRustAddCallSiteAttribute (LLVMValueRef Instr, unsigned Index,
207226 LLVMRustAttribute RustAttr) {
208227 CallBase *Call = unwrap<CallBase>(Instr);
209228 Attribute Attr = Attribute::get (Call->getContext (), fromRust (RustAttr));
210- Call-> addAttribute ( Index, Attr);
229+ AddAttribute (Call, Index, Attr);
211230}
212231
213232extern " C" void LLVMRustAddCallSiteAttrString (LLVMValueRef Instr, unsigned Index,
214233 const char *Name) {
215234 CallBase *Call = unwrap<CallBase>(Instr);
216235 Attribute Attr = Attribute::get (Call->getContext (), Name);
217- Call-> addAttribute ( Index, Attr);
236+ AddAttribute (Call, Index, Attr);
218237}
219238
239+ static inline void AddCallAttributes (CallBase *Call, unsigned Index, const AttrBuilder& B) {
240+ AttributeList Attrs = Call->getAttributes ();
241+ #if LLVM_VERSION_LT(14, 0)
242+ Attrs = Attrs.addAttributes (Call->getContext (), Index, B);
243+ #else
244+ // TODO(durin42): we should probably surface the explicit functions to Rust
245+ // instead of this switch statement?
246+ switch (Index) {
247+ case AttributeList::ReturnIndex:
248+ Attrs = Attrs.addRetAttributes (Call->getContext (), B);
249+ break ;
250+ case AttributeList::FunctionIndex:
251+ Attrs = Attrs.addFnAttributes (Call->getContext (), B);
252+ break ;
253+ default :
254+ Attrs = Attrs.addParamAttributes (Call->getContext (), Index-AttributeList::FirstArgIndex, B);
255+ }
256+ #endif
257+ Call->setAttributes (Attrs);
258+ }
220259
221260extern " C" void LLVMRustAddAlignmentCallSiteAttr (LLVMValueRef Instr,
222261 unsigned Index,
223262 uint32_t Bytes) {
224263 CallBase *Call = unwrap<CallBase>(Instr);
225264 AttrBuilder B;
226265 B.addAlignmentAttr (Bytes);
227- Call->setAttributes (Call->getAttributes ().addAttributes (
228- Call->getContext (), Index, B));
266+ AddCallAttributes (Call, Index, B);
229267}
230268
231269extern " C" void LLVMRustAddDereferenceableCallSiteAttr (LLVMValueRef Instr,
@@ -234,8 +272,7 @@ extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
234272 CallBase *Call = unwrap<CallBase>(Instr);
235273 AttrBuilder B;
236274 B.addDereferenceableAttr (Bytes);
237- Call->setAttributes (Call->getAttributes ().addAttributes (
238- Call->getContext (), Index, B));
275+ AddCallAttributes (Call, Index, B);
239276}
240277
241278extern " C" void LLVMRustAddDereferenceableOrNullCallSiteAttr (LLVMValueRef Instr,
@@ -244,15 +281,14 @@ extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
244281 CallBase *Call = unwrap<CallBase>(Instr);
245282 AttrBuilder B;
246283 B.addDereferenceableOrNullAttr (Bytes);
247- Call->setAttributes (Call->getAttributes ().addAttributes (
248- Call->getContext (), Index, B));
284+ AddCallAttributes (Call, Index, B);
249285}
250286
251287extern " C" void LLVMRustAddByValCallSiteAttr (LLVMValueRef Instr, unsigned Index,
252288 LLVMTypeRef Ty) {
253289 CallBase *Call = unwrap<CallBase>(Instr);
254290 Attribute Attr = Attribute::getWithByValType (Call->getContext (), unwrap (Ty));
255- Call-> addAttribute ( Index, Attr);
291+ AddAttribute (Call, Index, Attr);
256292}
257293
258294extern " C" void LLVMRustAddStructRetCallSiteAttr (LLVMValueRef Instr, unsigned Index,
@@ -263,44 +299,44 @@ extern "C" void LLVMRustAddStructRetCallSiteAttr(LLVMValueRef Instr, unsigned In
263299#else
264300 Attribute Attr = Attribute::get (Call->getContext (), Attribute::StructRet);
265301#endif
266- Call-> addAttribute ( Index, Attr);
302+ AddAttribute (Call, Index, Attr);
267303}
268304
269305extern " C" void LLVMRustAddFunctionAttribute (LLVMValueRef Fn, unsigned Index,
270306 LLVMRustAttribute RustAttr) {
271307 Function *A = unwrap<Function>(Fn);
272308 Attribute Attr = Attribute::get (A->getContext (), fromRust (RustAttr));
273- A-> addAttribute ( Index, Attr);
309+ AddAttribute (A, Index, Attr);
274310}
275311
276312extern " C" void LLVMRustAddAlignmentAttr (LLVMValueRef Fn,
277313 unsigned Index,
278314 uint32_t Bytes) {
279315 Function *A = unwrap<Function>(Fn);
280- A-> addAttribute ( Index, Attribute::getWithAlignment (
316+ AddAttribute (A, Index, Attribute::getWithAlignment (
281317 A->getContext (), llvm::Align (Bytes)));
282318}
283319
284320extern " C" void LLVMRustAddDereferenceableAttr (LLVMValueRef Fn, unsigned Index,
285321 uint64_t Bytes) {
286322 Function *A = unwrap<Function>(Fn);
287- A-> addAttribute ( Index, Attribute::getWithDereferenceableBytes (A->getContext (),
323+ AddAttribute (A, Index, Attribute::getWithDereferenceableBytes (A->getContext (),
288324 Bytes));
289325}
290326
291327extern " C" void LLVMRustAddDereferenceableOrNullAttr (LLVMValueRef Fn,
292328 unsigned Index,
293329 uint64_t Bytes) {
294330 Function *A = unwrap<Function>(Fn);
295- A-> addAttribute ( Index, Attribute::getWithDereferenceableOrNullBytes (
331+ AddAttribute (A, Index, Attribute::getWithDereferenceableOrNullBytes (
296332 A->getContext (), Bytes));
297333}
298334
299335extern " C" void LLVMRustAddByValAttr (LLVMValueRef Fn, unsigned Index,
300336 LLVMTypeRef Ty) {
301337 Function *F = unwrap<Function>(Fn);
302338 Attribute Attr = Attribute::getWithByValType (F->getContext (), unwrap (Ty));
303- F-> addAttribute ( Index, Attr);
339+ AddAttribute (F, Index, Attr);
304340}
305341
306342extern " C" void LLVMRustAddStructRetAttr (LLVMValueRef Fn, unsigned Index,
@@ -311,15 +347,15 @@ extern "C" void LLVMRustAddStructRetAttr(LLVMValueRef Fn, unsigned Index,
311347#else
312348 Attribute Attr = Attribute::get (F->getContext (), Attribute::StructRet);
313349#endif
314- F-> addAttribute ( Index, Attr);
350+ AddAttribute (F, Index, Attr);
315351}
316352
317353extern " C" void LLVMRustAddFunctionAttrStringValue (LLVMValueRef Fn,
318354 unsigned Index,
319355 const char *Name,
320356 const char *Value) {
321357 Function *F = unwrap<Function>(Fn);
322- F-> addAttribute ( Index, Attribute::get (
358+ AddAttribute (F, Index, Attribute::get (
323359 F->getContext (), StringRef (Name), StringRef (Value)));
324360}
325361
@@ -330,7 +366,23 @@ extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
330366 Attribute Attr = Attribute::get (F->getContext (), fromRust (RustAttr));
331367 AttrBuilder B (Attr);
332368 auto PAL = F->getAttributes ();
333- auto PALNew = PAL.removeAttributes (F->getContext (), Index, B);
369+ AttributeList PALNew;
370+ #if LLVM_VERSION_LT(14, 0)
371+ PALNew = PAL.removeAttributes (F->getContext (), Index, B);
372+ #else
373+ // TODO(durin42): we should probably surface the explicit functions to Rust
374+ // instead of this switch statement?
375+ switch (Index) {
376+ case AttributeList::ReturnIndex:
377+ PALNew = PAL.removeRetAttributes (F->getContext (), B);
378+ break ;
379+ case AttributeList::FunctionIndex:
380+ PALNew = PAL.removeFnAttributes (F->getContext (), B);
381+ break ;
382+ default :
383+ PALNew = PAL.removeParamAttributes (F->getContext (), Index-AttributeList::FirstArgIndex, B);
384+ }
385+ #endif
334386 F->setAttributes (PALNew);
335387}
336388
0 commit comments