16
16
#include " llvm/Object/Archive.h"
17
17
#include " llvm/Object/ObjectFile.h"
18
18
#include " llvm/Bitcode/BitcodeWriterPass.h"
19
+
19
20
#include " llvm/IR/CallSite.h"
21
+
22
+ #if LLVM_VERSION_GE(5, 0)
20
23
#include " llvm/ADT/Optional.h"
24
+ #else
25
+ #include < cstdlib>
26
+ #endif
21
27
22
28
// ===----------------------------------------------------------------------===
23
29
//
@@ -170,7 +176,14 @@ extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
170
176
LLVMRustAttribute RustAttr) {
171
177
CallSite Call = CallSite (unwrap<Instruction>(Instr));
172
178
Attribute Attr = Attribute::get (Call->getContext (), fromRust (RustAttr));
179
+ #if LLVM_VERSION_GE(5, 0)
173
180
Call.addAttribute (Index, Attr);
181
+ #else
182
+ AttrBuilder B (Attr);
183
+ Call.setAttributes (Call.getAttributes ().addAttributes (
184
+ Call->getContext (), Index,
185
+ AttributeSet::get (Call->getContext (), Index, B)));
186
+ #endif
174
187
}
175
188
176
189
extern " C" void LLVMRustAddAlignmentCallSiteAttr (LLVMValueRef Instr,
@@ -179,8 +192,14 @@ extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr,
179
192
CallSite Call = CallSite (unwrap<Instruction>(Instr));
180
193
AttrBuilder B;
181
194
B.addAlignmentAttr (Bytes);
195
+ #if LLVM_VERSION_GE(5, 0)
182
196
Call.setAttributes (Call.getAttributes ().addAttributes (
183
197
Call->getContext (), Index, B));
198
+ #else
199
+ Call.setAttributes (Call.getAttributes ().addAttributes (
200
+ Call->getContext (), Index,
201
+ AttributeSet::get (Call->getContext (), Index, B)));
202
+ #endif
184
203
}
185
204
186
205
extern " C" void LLVMRustAddDereferenceableCallSiteAttr (LLVMValueRef Instr,
@@ -189,8 +208,14 @@ extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
189
208
CallSite Call = CallSite (unwrap<Instruction>(Instr));
190
209
AttrBuilder B;
191
210
B.addDereferenceableAttr (Bytes);
211
+ #if LLVM_VERSION_GE(5, 0)
192
212
Call.setAttributes (Call.getAttributes ().addAttributes (
193
213
Call->getContext (), Index, B));
214
+ #else
215
+ Call.setAttributes (Call.getAttributes ().addAttributes (
216
+ Call->getContext (), Index,
217
+ AttributeSet::get (Call->getContext (), Index, B)));
218
+ #endif
194
219
}
195
220
196
221
extern " C" void LLVMRustAddDereferenceableOrNullCallSiteAttr (LLVMValueRef Instr,
@@ -199,16 +224,26 @@ extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
199
224
CallSite Call = CallSite (unwrap<Instruction>(Instr));
200
225
AttrBuilder B;
201
226
B.addDereferenceableOrNullAttr (Bytes);
227
+ #if LLVM_VERSION_GE(5, 0)
202
228
Call.setAttributes (Call.getAttributes ().addAttributes (
203
229
Call->getContext (), Index, B));
230
+ #else
231
+ Call.setAttributes (Call.getAttributes ().addAttributes (
232
+ Call->getContext (), Index,
233
+ AttributeSet::get (Call->getContext (), Index, B)));
234
+ #endif
204
235
}
205
236
206
237
extern " C" void LLVMRustAddFunctionAttribute (LLVMValueRef Fn, unsigned Index,
207
238
LLVMRustAttribute RustAttr) {
208
239
Function *A = unwrap<Function>(Fn);
209
240
Attribute Attr = Attribute::get (A->getContext (), fromRust (RustAttr));
210
241
AttrBuilder B (Attr);
242
+ #if LLVM_VERSION_GE(5, 0)
211
243
A->addAttributes (Index, B);
244
+ #else
245
+ A->addAttributes (Index, AttributeSet::get (A->getContext (), Index, B));
246
+ #endif
212
247
}
213
248
214
249
extern " C" void LLVMRustAddAlignmentAttr (LLVMValueRef Fn,
@@ -217,15 +252,23 @@ extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn,
217
252
Function *A = unwrap<Function>(Fn);
218
253
AttrBuilder B;
219
254
B.addAlignmentAttr (Bytes);
255
+ #if LLVM_VERSION_GE(5, 0)
220
256
A->addAttributes (Index, B);
257
+ #else
258
+ A->addAttributes (Index, AttributeSet::get (A->getContext (), Index, B));
259
+ #endif
221
260
}
222
261
223
262
extern " C" void LLVMRustAddDereferenceableAttr (LLVMValueRef Fn, unsigned Index,
224
263
uint64_t Bytes) {
225
264
Function *A = unwrap<Function>(Fn);
226
265
AttrBuilder B;
227
266
B.addDereferenceableAttr (Bytes);
267
+ #if LLVM_VERSION_GE(5, 0)
228
268
A->addAttributes (Index, B);
269
+ #else
270
+ A->addAttributes (Index, AttributeSet::get (A->getContext (), Index, B));
271
+ #endif
229
272
}
230
273
231
274
extern " C" void LLVMRustAddDereferenceableOrNullAttr (LLVMValueRef Fn,
@@ -234,7 +277,11 @@ extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn,
234
277
Function *A = unwrap<Function>(Fn);
235
278
AttrBuilder B;
236
279
B.addDereferenceableOrNullAttr (Bytes);
280
+ #if LLVM_VERSION_GE(5, 0)
237
281
A->addAttributes (Index, B);
282
+ #else
283
+ A->addAttributes (Index, AttributeSet::get (A->getContext (), Index, B));
284
+ #endif
238
285
}
239
286
240
287
extern " C" void LLVMRustAddFunctionAttrStringValue (LLVMValueRef Fn,
@@ -244,7 +291,11 @@ extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
244
291
Function *F = unwrap<Function>(Fn);
245
292
AttrBuilder B;
246
293
B.addAttribute (Name, Value);
294
+ #if LLVM_VERSION_GE(5, 0)
247
295
F->addAttributes (Index, B);
296
+ #else
297
+ F->addAttributes (Index, AttributeSet::get (F->getContext (), Index, B));
298
+ #endif
248
299
}
249
300
250
301
extern " C" void LLVMRustRemoveFunctionAttributes (LLVMValueRef Fn,
@@ -254,7 +305,12 @@ extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
254
305
Attribute Attr = Attribute::get (F->getContext (), fromRust (RustAttr));
255
306
AttrBuilder B (Attr);
256
307
auto PAL = F->getAttributes ();
308
+ #if LLVM_VERSION_GE(5, 0)
257
309
auto PALNew = PAL.removeAttributes (F->getContext (), Index, B);
310
+ #else
311
+ const AttributeSet PALNew = PAL.removeAttributes (
312
+ F->getContext (), Index, AttributeSet::get (F->getContext (), Index, B));
313
+ #endif
258
314
F->setAttributes (PALNew);
259
315
}
260
316
@@ -304,6 +360,7 @@ enum class LLVMRustSynchronizationScope {
304
360
CrossThread,
305
361
};
306
362
363
+ #if LLVM_VERSION_GE(5, 0)
307
364
static SyncScope::ID fromRust (LLVMRustSynchronizationScope Scope) {
308
365
switch (Scope) {
309
366
case LLVMRustSynchronizationScope::SingleThread:
@@ -314,6 +371,18 @@ static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) {
314
371
report_fatal_error (" bad SynchronizationScope." );
315
372
}
316
373
}
374
+ #else
375
+ static SynchronizationScope fromRust (LLVMRustSynchronizationScope Scope) {
376
+ switch (Scope) {
377
+ case LLVMRustSynchronizationScope::SingleThread:
378
+ return SingleThread;
379
+ case LLVMRustSynchronizationScope::CrossThread:
380
+ return CrossThread;
381
+ default :
382
+ report_fatal_error (" bad SynchronizationScope." );
383
+ }
384
+ }
385
+ #endif
317
386
318
387
extern " C" LLVMValueRef
319
388
LLVMRustBuildAtomicFence (LLVMBuilderRef B, LLVMAtomicOrdering Order,
@@ -353,6 +422,18 @@ extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm)
353
422
354
423
typedef DIBuilder *LLVMRustDIBuilderRef;
355
424
425
+ #if LLVM_VERSION_LT(5, 0)
426
+ typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
427
+
428
+ namespace llvm {
429
+ DEFINE_ISA_CONVERSION_FUNCTIONS (Metadata, LLVMMetadataRef)
430
+
431
+ inline Metadata **unwrap (LLVMMetadataRef *Vals) {
432
+ return reinterpret_cast <Metadata **>(Vals);
433
+ }
434
+ }
435
+ #endif
436
+
356
437
template <typename DIT> DIT *unwrapDIPtr (LLVMMetadataRef Ref) {
357
438
return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr );
358
439
}
@@ -468,6 +549,11 @@ static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
468
549
if (isSet (Flags & LLVMRustDIFlags::FlagRValueReference)) {
469
550
Result |= DINode::DIFlags::FlagRValueReference;
470
551
}
552
+ #if LLVM_VERSION_LE(4, 0)
553
+ if (isSet (Flags & LLVMRustDIFlags::FlagExternalTypeRef)) {
554
+ Result |= DINode::DIFlags::FlagExternalTypeRef;
555
+ }
556
+ #endif
471
557
if (isSet (Flags & LLVMRustDIFlags::FlagIntroducedVirtual)) {
472
558
Result |= DINode::DIFlags::FlagIntroducedVirtual;
473
559
}
@@ -566,7 +652,9 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreatePointerType(
566
652
uint64_t SizeInBits, uint32_t AlignInBits, const char *Name) {
567
653
return wrap (Builder->createPointerType (unwrapDI<DIType>(PointeeTy),
568
654
SizeInBits, AlignInBits,
655
+ #if LLVM_VERSION_GE(5, 0)
569
656
/* DWARFAddressSpace */ None,
657
+ #endif
570
658
Name));
571
659
}
572
660
@@ -737,8 +825,15 @@ LLVMRustDIBuilderCreateNameSpace(LLVMRustDIBuilderRef Builder,
737
825
LLVMMetadataRef Scope, const char *Name,
738
826
LLVMMetadataRef File, unsigned LineNo) {
739
827
return wrap (Builder->createNameSpace (
740
- unwrapDI<DIDescriptor>(Scope), Name,
828
+ unwrapDI<DIDescriptor>(Scope), Name
829
+ #if LLVM_VERSION_LT(5, 0)
830
+ ,
831
+ unwrapDI<DIFile>(File), LineNo
832
+ #endif
833
+ #if LLVM_VERSION_GE(4, 0)
834
+ ,
741
835
false // ExportSymbols (only relevant for C++ anonymous namespaces)
836
+ #endif
742
837
));
743
838
}
744
839
@@ -767,7 +862,12 @@ extern "C" int64_t LLVMRustDIBuilderCreateOpDeref() {
767
862
}
768
863
769
864
extern " C" int64_t LLVMRustDIBuilderCreateOpPlusUconst () {
865
+ #if LLVM_VERSION_GE(5, 0)
770
866
return dwarf::DW_OP_plus_uconst;
867
+ #else
868
+ // older LLVM used `plus` to behave like `plus_uconst`.
869
+ return dwarf::DW_OP_plus;
870
+ #endif
771
871
}
772
872
773
873
extern " C" void LLVMRustWriteTypeToString (LLVMTypeRef Ty, RustStringRef Str) {
@@ -839,12 +939,21 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
839
939
*FunctionOut = wrap (&Opt->getFunction ());
840
940
841
941
RawRustStringOstream FilenameOS (FilenameOut);
942
+ #if LLVM_VERSION_GE(5,0)
842
943
DiagnosticLocation loc = Opt->getLocation ();
843
944
if (loc.isValid ()) {
844
945
*Line = loc.getLine ();
845
946
*Column = loc.getColumn ();
846
947
FilenameOS << loc.getFilename ();
847
948
}
949
+ #else
950
+ const DebugLoc &loc = Opt->getDebugLoc ();
951
+ if (loc) {
952
+ *Line = loc.getLine ();
953
+ *Column = loc.getCol ();
954
+ FilenameOS << cast<DIScope>(loc.getScope ())->getFilename ();
955
+ }
956
+ #endif
848
957
849
958
RawRustStringOstream MessageOS (MessageOut);
850
959
MessageOS << Opt->getMsg ();
@@ -1264,6 +1373,7 @@ LLVMRustModuleCost(LLVMModuleRef M) {
1264
1373
}
1265
1374
1266
1375
// Vector reductions:
1376
+ #if LLVM_VERSION_GE(5, 0)
1267
1377
extern " C" LLVMValueRef
1268
1378
LLVMRustBuildVectorReduceFAdd (LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) {
1269
1379
return wrap (unwrap (B)->CreateFAddReduce (unwrap (Acc),unwrap (Src)));
@@ -1309,6 +1419,62 @@ LLVMRustBuildVectorReduceFMax(LLVMBuilderRef B, LLVMValueRef Src, bool NoNaN) {
1309
1419
return wrap (unwrap (B)->CreateFPMaxReduce (unwrap (Src), NoNaN));
1310
1420
}
1311
1421
1422
+ #else
1423
+
1424
+ extern " C" LLVMValueRef
1425
+ LLVMRustBuildVectorReduceFAdd (LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1426
+ return nullptr ;
1427
+ }
1428
+ extern " C" LLVMValueRef
1429
+ LLVMRustBuildVectorReduceFMul (LLVMBuilderRef, LLVMValueRef, LLVMValueRef) {
1430
+ return nullptr ;
1431
+ }
1432
+ extern " C" LLVMValueRef
1433
+ LLVMRustBuildVectorReduceAdd (LLVMBuilderRef, LLVMValueRef) {
1434
+ return nullptr ;
1435
+ }
1436
+ extern " C" LLVMValueRef
1437
+ LLVMRustBuildVectorReduceMul (LLVMBuilderRef, LLVMValueRef) {
1438
+ return nullptr ;
1439
+ }
1440
+ extern " C" LLVMValueRef
1441
+ LLVMRustBuildVectorReduceAnd (LLVMBuilderRef, LLVMValueRef) {
1442
+ return nullptr ;
1443
+ }
1444
+ extern " C" LLVMValueRef
1445
+ LLVMRustBuildVectorReduceOr (LLVMBuilderRef, LLVMValueRef) {
1446
+ return nullptr ;
1447
+ }
1448
+ extern " C" LLVMValueRef
1449
+ LLVMRustBuildVectorReduceXor (LLVMBuilderRef, LLVMValueRef) {
1450
+ return nullptr ;
1451
+ }
1452
+ extern " C" LLVMValueRef
1453
+ LLVMRustBuildVectorReduceMin (LLVMBuilderRef, LLVMValueRef, bool ) {
1454
+ return nullptr ;
1455
+ }
1456
+ extern " C" LLVMValueRef
1457
+ LLVMRustBuildVectorReduceMax (LLVMBuilderRef, LLVMValueRef, bool ) {
1458
+ return nullptr ;
1459
+ }
1460
+ extern " C" LLVMValueRef
1461
+ LLVMRustBuildVectorReduceFMin (LLVMBuilderRef, LLVMValueRef, bool ) {
1462
+ return nullptr ;
1463
+ }
1464
+ extern " C" LLVMValueRef
1465
+ LLVMRustBuildVectorReduceFMax (LLVMBuilderRef, LLVMValueRef, bool ) {
1466
+ return nullptr ;
1467
+ }
1468
+ #endif
1469
+
1470
+ #if LLVM_VERSION_LT(4, 0)
1471
+ extern " C" LLVMValueRef
1472
+ LLVMBuildExactUDiv (LLVMBuilderRef B, LLVMValueRef LHS,
1473
+ LLVMValueRef RHS, const char *Name) {
1474
+ return wrap (unwrap (B)->CreateExactUDiv (unwrap (LHS), unwrap (RHS), Name));
1475
+ }
1476
+ #endif
1477
+
1312
1478
#if LLVM_VERSION_GE(6, 0)
1313
1479
extern " C" LLVMValueRef
1314
1480
LLVMRustBuildMinNum (LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {
0 commit comments