@@ -203,29 +203,67 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
203
203
report_fatal_error (" bad AttributeKind" );
204
204
}
205
205
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
+
206
225
extern " C" void LLVMRustAddCallSiteAttribute (LLVMValueRef Instr, unsigned Index,
207
226
LLVMRustAttribute RustAttr) {
208
227
CallBase *Call = unwrap<CallBase>(Instr);
209
228
Attribute Attr = Attribute::get (Call->getContext (), fromRust (RustAttr));
210
- Call-> addAttribute ( Index, Attr);
229
+ AddAttribute (Call, Index, Attr);
211
230
}
212
231
213
232
extern " C" void LLVMRustAddCallSiteAttrString (LLVMValueRef Instr, unsigned Index,
214
233
const char *Name) {
215
234
CallBase *Call = unwrap<CallBase>(Instr);
216
235
Attribute Attr = Attribute::get (Call->getContext (), Name);
217
- Call-> addAttribute ( Index, Attr);
236
+ AddAttribute (Call, Index, Attr);
218
237
}
219
238
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
+ }
220
259
221
260
extern " C" void LLVMRustAddAlignmentCallSiteAttr (LLVMValueRef Instr,
222
261
unsigned Index,
223
262
uint32_t Bytes) {
224
263
CallBase *Call = unwrap<CallBase>(Instr);
225
264
AttrBuilder B;
226
265
B.addAlignmentAttr (Bytes);
227
- Call->setAttributes (Call->getAttributes ().addAttributes (
228
- Call->getContext (), Index, B));
266
+ AddCallAttributes (Call, Index, B);
229
267
}
230
268
231
269
extern " C" void LLVMRustAddDereferenceableCallSiteAttr (LLVMValueRef Instr,
@@ -234,8 +272,7 @@ extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
234
272
CallBase *Call = unwrap<CallBase>(Instr);
235
273
AttrBuilder B;
236
274
B.addDereferenceableAttr (Bytes);
237
- Call->setAttributes (Call->getAttributes ().addAttributes (
238
- Call->getContext (), Index, B));
275
+ AddCallAttributes (Call, Index, B);
239
276
}
240
277
241
278
extern " C" void LLVMRustAddDereferenceableOrNullCallSiteAttr (LLVMValueRef Instr,
@@ -244,15 +281,14 @@ extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
244
281
CallBase *Call = unwrap<CallBase>(Instr);
245
282
AttrBuilder B;
246
283
B.addDereferenceableOrNullAttr (Bytes);
247
- Call->setAttributes (Call->getAttributes ().addAttributes (
248
- Call->getContext (), Index, B));
284
+ AddCallAttributes (Call, Index, B);
249
285
}
250
286
251
287
extern " C" void LLVMRustAddByValCallSiteAttr (LLVMValueRef Instr, unsigned Index,
252
288
LLVMTypeRef Ty) {
253
289
CallBase *Call = unwrap<CallBase>(Instr);
254
290
Attribute Attr = Attribute::getWithByValType (Call->getContext (), unwrap (Ty));
255
- Call-> addAttribute ( Index, Attr);
291
+ AddAttribute (Call, Index, Attr);
256
292
}
257
293
258
294
extern " C" void LLVMRustAddStructRetCallSiteAttr (LLVMValueRef Instr, unsigned Index,
@@ -263,44 +299,44 @@ extern "C" void LLVMRustAddStructRetCallSiteAttr(LLVMValueRef Instr, unsigned In
263
299
#else
264
300
Attribute Attr = Attribute::get (Call->getContext (), Attribute::StructRet);
265
301
#endif
266
- Call-> addAttribute ( Index, Attr);
302
+ AddAttribute (Call, Index, Attr);
267
303
}
268
304
269
305
extern " C" void LLVMRustAddFunctionAttribute (LLVMValueRef Fn, unsigned Index,
270
306
LLVMRustAttribute RustAttr) {
271
307
Function *A = unwrap<Function>(Fn);
272
308
Attribute Attr = Attribute::get (A->getContext (), fromRust (RustAttr));
273
- A-> addAttribute ( Index, Attr);
309
+ AddAttribute (A, Index, Attr);
274
310
}
275
311
276
312
extern " C" void LLVMRustAddAlignmentAttr (LLVMValueRef Fn,
277
313
unsigned Index,
278
314
uint32_t Bytes) {
279
315
Function *A = unwrap<Function>(Fn);
280
- A-> addAttribute ( Index, Attribute::getWithAlignment (
316
+ AddAttribute (A, Index, Attribute::getWithAlignment (
281
317
A->getContext (), llvm::Align (Bytes)));
282
318
}
283
319
284
320
extern " C" void LLVMRustAddDereferenceableAttr (LLVMValueRef Fn, unsigned Index,
285
321
uint64_t Bytes) {
286
322
Function *A = unwrap<Function>(Fn);
287
- A-> addAttribute ( Index, Attribute::getWithDereferenceableBytes (A->getContext (),
323
+ AddAttribute (A, Index, Attribute::getWithDereferenceableBytes (A->getContext (),
288
324
Bytes));
289
325
}
290
326
291
327
extern " C" void LLVMRustAddDereferenceableOrNullAttr (LLVMValueRef Fn,
292
328
unsigned Index,
293
329
uint64_t Bytes) {
294
330
Function *A = unwrap<Function>(Fn);
295
- A-> addAttribute ( Index, Attribute::getWithDereferenceableOrNullBytes (
331
+ AddAttribute (A, Index, Attribute::getWithDereferenceableOrNullBytes (
296
332
A->getContext (), Bytes));
297
333
}
298
334
299
335
extern " C" void LLVMRustAddByValAttr (LLVMValueRef Fn, unsigned Index,
300
336
LLVMTypeRef Ty) {
301
337
Function *F = unwrap<Function>(Fn);
302
338
Attribute Attr = Attribute::getWithByValType (F->getContext (), unwrap (Ty));
303
- F-> addAttribute ( Index, Attr);
339
+ AddAttribute (F, Index, Attr);
304
340
}
305
341
306
342
extern " C" void LLVMRustAddStructRetAttr (LLVMValueRef Fn, unsigned Index,
@@ -311,15 +347,15 @@ extern "C" void LLVMRustAddStructRetAttr(LLVMValueRef Fn, unsigned Index,
311
347
#else
312
348
Attribute Attr = Attribute::get (F->getContext (), Attribute::StructRet);
313
349
#endif
314
- F-> addAttribute ( Index, Attr);
350
+ AddAttribute (F, Index, Attr);
315
351
}
316
352
317
353
extern " C" void LLVMRustAddFunctionAttrStringValue (LLVMValueRef Fn,
318
354
unsigned Index,
319
355
const char *Name,
320
356
const char *Value) {
321
357
Function *F = unwrap<Function>(Fn);
322
- F-> addAttribute ( Index, Attribute::get (
358
+ AddAttribute (F, Index, Attribute::get (
323
359
F->getContext (), StringRef (Name), StringRef (Value)));
324
360
}
325
361
@@ -330,7 +366,23 @@ extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
330
366
Attribute Attr = Attribute::get (F->getContext (), fromRust (RustAttr));
331
367
AttrBuilder B (Attr);
332
368
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
334
386
F->setAttributes (PALNew);
335
387
}
336
388
0 commit comments