Skip to content

Commit 1c091e4

Browse files
authored
Rollup merge of #88732 - durin42:llvm-14-attrs-2, r=nikic
RustWrapper: avoid deleted unclear attribute methods These were deleted in https://reviews.llvm.org/D108614, and in C++ I definitely see the argument for their removal. I didn't try and propagate the changes up into higher layers of rustc in this change because my initial goal was to get rustc working against LLVM HEAD promptly, but I'm happy to follow up with some refactoring to make the API on the Rust side match the LLVM API more directly (though the way the enum works in Rust makes the API less scary IMO). r? ``@nagisa`` cc ``@nikic``
2 parents e0e3d85 + 4d04540 commit 1c091e4

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+31-25
Original file line numberDiff line numberDiff line change
@@ -203,56 +203,57 @@ 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+
t->addAttributeAtIndex(Index, Attr);
211+
#endif
212+
}
213+
206214
extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
207215
LLVMRustAttribute RustAttr) {
208216
CallBase *Call = unwrap<CallBase>(Instr);
209217
Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr));
210-
Call->addAttribute(Index, Attr);
218+
AddAttribute(Call, Index, Attr);
211219
}
212220

213221
extern "C" void LLVMRustAddCallSiteAttrString(LLVMValueRef Instr, unsigned Index,
214222
const char *Name) {
215223
CallBase *Call = unwrap<CallBase>(Instr);
216224
Attribute Attr = Attribute::get(Call->getContext(), Name);
217-
Call->addAttribute(Index, Attr);
225+
AddAttribute(Call, Index, Attr);
218226
}
219227

220-
221228
extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr,
222229
unsigned Index,
223230
uint32_t Bytes) {
224231
CallBase *Call = unwrap<CallBase>(Instr);
225-
AttrBuilder B;
226-
B.addAlignmentAttr(Bytes);
227-
Call->setAttributes(Call->getAttributes().addAttributes(
228-
Call->getContext(), Index, B));
232+
Attribute Attr = Attribute::getWithAlignment(Call->getContext(), Align(Bytes));
233+
AddAttribute(Call, Index, Attr);
229234
}
230235

231236
extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
232237
unsigned Index,
233238
uint64_t Bytes) {
234239
CallBase *Call = unwrap<CallBase>(Instr);
235-
AttrBuilder B;
236-
B.addDereferenceableAttr(Bytes);
237-
Call->setAttributes(Call->getAttributes().addAttributes(
238-
Call->getContext(), Index, B));
240+
Attribute Attr = Attribute::getWithDereferenceableBytes(Call->getContext(), Bytes);
241+
AddAttribute(Call, Index, Attr);
239242
}
240243

241244
extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
242245
unsigned Index,
243246
uint64_t Bytes) {
244247
CallBase *Call = unwrap<CallBase>(Instr);
245-
AttrBuilder B;
246-
B.addDereferenceableOrNullAttr(Bytes);
247-
Call->setAttributes(Call->getAttributes().addAttributes(
248-
Call->getContext(), Index, B));
248+
Attribute Attr = Attribute::getWithDereferenceableOrNullBytes(Call->getContext(), Bytes);
249+
AddAttribute(Call, Index, Attr);
249250
}
250251

251252
extern "C" void LLVMRustAddByValCallSiteAttr(LLVMValueRef Instr, unsigned Index,
252253
LLVMTypeRef Ty) {
253254
CallBase *Call = unwrap<CallBase>(Instr);
254255
Attribute Attr = Attribute::getWithByValType(Call->getContext(), unwrap(Ty));
255-
Call->addAttribute(Index, Attr);
256+
AddAttribute(Call, Index, Attr);
256257
}
257258

258259
extern "C" void LLVMRustAddStructRetCallSiteAttr(LLVMValueRef Instr, unsigned Index,
@@ -263,44 +264,44 @@ extern "C" void LLVMRustAddStructRetCallSiteAttr(LLVMValueRef Instr, unsigned In
263264
#else
264265
Attribute Attr = Attribute::get(Call->getContext(), Attribute::StructRet);
265266
#endif
266-
Call->addAttribute(Index, Attr);
267+
AddAttribute(Call, Index, Attr);
267268
}
268269

269270
extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
270271
LLVMRustAttribute RustAttr) {
271272
Function *A = unwrap<Function>(Fn);
272273
Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr));
273-
A->addAttribute(Index, Attr);
274+
AddAttribute(A, Index, Attr);
274275
}
275276

276277
extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn,
277278
unsigned Index,
278279
uint32_t Bytes) {
279280
Function *A = unwrap<Function>(Fn);
280-
A->addAttribute(Index, Attribute::getWithAlignment(
281+
AddAttribute(A, Index, Attribute::getWithAlignment(
281282
A->getContext(), llvm::Align(Bytes)));
282283
}
283284

284285
extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
285286
uint64_t Bytes) {
286287
Function *A = unwrap<Function>(Fn);
287-
A->addAttribute(Index, Attribute::getWithDereferenceableBytes(A->getContext(),
288+
AddAttribute(A, Index, Attribute::getWithDereferenceableBytes(A->getContext(),
288289
Bytes));
289290
}
290291

291292
extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn,
292293
unsigned Index,
293294
uint64_t Bytes) {
294295
Function *A = unwrap<Function>(Fn);
295-
A->addAttribute(Index, Attribute::getWithDereferenceableOrNullBytes(
296+
AddAttribute(A, Index, Attribute::getWithDereferenceableOrNullBytes(
296297
A->getContext(), Bytes));
297298
}
298299

299300
extern "C" void LLVMRustAddByValAttr(LLVMValueRef Fn, unsigned Index,
300301
LLVMTypeRef Ty) {
301302
Function *F = unwrap<Function>(Fn);
302303
Attribute Attr = Attribute::getWithByValType(F->getContext(), unwrap(Ty));
303-
F->addAttribute(Index, Attr);
304+
AddAttribute(F, Index, Attr);
304305
}
305306

306307
extern "C" void LLVMRustAddStructRetAttr(LLVMValueRef Fn, unsigned Index,
@@ -311,15 +312,15 @@ extern "C" void LLVMRustAddStructRetAttr(LLVMValueRef Fn, unsigned Index,
311312
#else
312313
Attribute Attr = Attribute::get(F->getContext(), Attribute::StructRet);
313314
#endif
314-
F->addAttribute(Index, Attr);
315+
AddAttribute(F, Index, Attr);
315316
}
316317

317318
extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
318319
unsigned Index,
319320
const char *Name,
320321
const char *Value) {
321322
Function *F = unwrap<Function>(Fn);
322-
F->addAttribute(Index, Attribute::get(
323+
AddAttribute(F, Index, Attribute::get(
323324
F->getContext(), StringRef(Name), StringRef(Value)));
324325
}
325326

@@ -330,7 +331,12 @@ extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
330331
Attribute Attr = Attribute::get(F->getContext(), fromRust(RustAttr));
331332
AttrBuilder B(Attr);
332333
auto PAL = F->getAttributes();
333-
auto PALNew = PAL.removeAttributes(F->getContext(), Index, B);
334+
AttributeList PALNew;
335+
#if LLVM_VERSION_LT(14, 0)
336+
PALNew = PAL.removeAttributes(F->getContext(), Index, B);
337+
#else
338+
PALNew = PAL.removeAttributesAtIndex(F->getContext(), Index, B);
339+
#endif
334340
F->setAttributes(PALNew);
335341
}
336342

0 commit comments

Comments
 (0)