Skip to content

Commit 23dfb42

Browse files
committed
still support LLVM4 for emscripten
1 parent 52d7740 commit 23dfb42

File tree

1 file changed

+167
-1
lines changed

1 file changed

+167
-1
lines changed

src/rustllvm/RustWrapper.cpp

Lines changed: 167 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
#include "llvm/Object/Archive.h"
1717
#include "llvm/Object/ObjectFile.h"
1818
#include "llvm/Bitcode/BitcodeWriterPass.h"
19+
1920
#include "llvm/IR/CallSite.h"
21+
22+
#if LLVM_VERSION_GE(5, 0)
2023
#include "llvm/ADT/Optional.h"
24+
#else
25+
#include <cstdlib>
26+
#endif
2127

2228
//===----------------------------------------------------------------------===
2329
//
@@ -170,7 +176,14 @@ extern "C" void LLVMRustAddCallSiteAttribute(LLVMValueRef Instr, unsigned Index,
170176
LLVMRustAttribute RustAttr) {
171177
CallSite Call = CallSite(unwrap<Instruction>(Instr));
172178
Attribute Attr = Attribute::get(Call->getContext(), fromRust(RustAttr));
179+
#if LLVM_VERSION_GE(5, 0)
173180
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
174187
}
175188

176189
extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr,
@@ -179,8 +192,14 @@ extern "C" void LLVMRustAddAlignmentCallSiteAttr(LLVMValueRef Instr,
179192
CallSite Call = CallSite(unwrap<Instruction>(Instr));
180193
AttrBuilder B;
181194
B.addAlignmentAttr(Bytes);
195+
#if LLVM_VERSION_GE(5, 0)
182196
Call.setAttributes(Call.getAttributes().addAttributes(
183197
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
184203
}
185204

186205
extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
@@ -189,8 +208,14 @@ extern "C" void LLVMRustAddDereferenceableCallSiteAttr(LLVMValueRef Instr,
189208
CallSite Call = CallSite(unwrap<Instruction>(Instr));
190209
AttrBuilder B;
191210
B.addDereferenceableAttr(Bytes);
211+
#if LLVM_VERSION_GE(5, 0)
192212
Call.setAttributes(Call.getAttributes().addAttributes(
193213
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
194219
}
195220

196221
extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
@@ -199,16 +224,26 @@ extern "C" void LLVMRustAddDereferenceableOrNullCallSiteAttr(LLVMValueRef Instr,
199224
CallSite Call = CallSite(unwrap<Instruction>(Instr));
200225
AttrBuilder B;
201226
B.addDereferenceableOrNullAttr(Bytes);
227+
#if LLVM_VERSION_GE(5, 0)
202228
Call.setAttributes(Call.getAttributes().addAttributes(
203229
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
204235
}
205236

206237
extern "C" void LLVMRustAddFunctionAttribute(LLVMValueRef Fn, unsigned Index,
207238
LLVMRustAttribute RustAttr) {
208239
Function *A = unwrap<Function>(Fn);
209240
Attribute Attr = Attribute::get(A->getContext(), fromRust(RustAttr));
210241
AttrBuilder B(Attr);
242+
#if LLVM_VERSION_GE(5, 0)
211243
A->addAttributes(Index, B);
244+
#else
245+
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
246+
#endif
212247
}
213248

214249
extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn,
@@ -217,15 +252,23 @@ extern "C" void LLVMRustAddAlignmentAttr(LLVMValueRef Fn,
217252
Function *A = unwrap<Function>(Fn);
218253
AttrBuilder B;
219254
B.addAlignmentAttr(Bytes);
255+
#if LLVM_VERSION_GE(5, 0)
220256
A->addAttributes(Index, B);
257+
#else
258+
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
259+
#endif
221260
}
222261

223262
extern "C" void LLVMRustAddDereferenceableAttr(LLVMValueRef Fn, unsigned Index,
224263
uint64_t Bytes) {
225264
Function *A = unwrap<Function>(Fn);
226265
AttrBuilder B;
227266
B.addDereferenceableAttr(Bytes);
267+
#if LLVM_VERSION_GE(5, 0)
228268
A->addAttributes(Index, B);
269+
#else
270+
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
271+
#endif
229272
}
230273

231274
extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn,
@@ -234,7 +277,11 @@ extern "C" void LLVMRustAddDereferenceableOrNullAttr(LLVMValueRef Fn,
234277
Function *A = unwrap<Function>(Fn);
235278
AttrBuilder B;
236279
B.addDereferenceableOrNullAttr(Bytes);
280+
#if LLVM_VERSION_GE(5, 0)
237281
A->addAttributes(Index, B);
282+
#else
283+
A->addAttributes(Index, AttributeSet::get(A->getContext(), Index, B));
284+
#endif
238285
}
239286

240287
extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
@@ -244,7 +291,11 @@ extern "C" void LLVMRustAddFunctionAttrStringValue(LLVMValueRef Fn,
244291
Function *F = unwrap<Function>(Fn);
245292
AttrBuilder B;
246293
B.addAttribute(Name, Value);
294+
#if LLVM_VERSION_GE(5, 0)
247295
F->addAttributes(Index, B);
296+
#else
297+
F->addAttributes(Index, AttributeSet::get(F->getContext(), Index, B));
298+
#endif
248299
}
249300

250301
extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
@@ -254,7 +305,12 @@ extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn,
254305
Attribute Attr = Attribute::get(F->getContext(), fromRust(RustAttr));
255306
AttrBuilder B(Attr);
256307
auto PAL = F->getAttributes();
308+
#if LLVM_VERSION_GE(5, 0)
257309
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
258314
F->setAttributes(PALNew);
259315
}
260316

@@ -304,6 +360,7 @@ enum class LLVMRustSynchronizationScope {
304360
CrossThread,
305361
};
306362

363+
#if LLVM_VERSION_GE(5, 0)
307364
static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) {
308365
switch (Scope) {
309366
case LLVMRustSynchronizationScope::SingleThread:
@@ -314,6 +371,18 @@ static SyncScope::ID fromRust(LLVMRustSynchronizationScope Scope) {
314371
report_fatal_error("bad SynchronizationScope.");
315372
}
316373
}
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
317386

318387
extern "C" LLVMValueRef
319388
LLVMRustBuildAtomicFence(LLVMBuilderRef B, LLVMAtomicOrdering Order,
@@ -353,6 +422,18 @@ extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm)
353422

354423
typedef DIBuilder *LLVMRustDIBuilderRef;
355424

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+
356437
template <typename DIT> DIT *unwrapDIPtr(LLVMMetadataRef Ref) {
357438
return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
358439
}
@@ -468,6 +549,11 @@ static DINode::DIFlags fromRust(LLVMRustDIFlags Flags) {
468549
if (isSet(Flags & LLVMRustDIFlags::FlagRValueReference)) {
469550
Result |= DINode::DIFlags::FlagRValueReference;
470551
}
552+
#if LLVM_VERSION_LE(4, 0)
553+
if (isSet(Flags & LLVMRustDIFlags::FlagExternalTypeRef)) {
554+
Result |= DINode::DIFlags::FlagExternalTypeRef;
555+
}
556+
#endif
471557
if (isSet(Flags & LLVMRustDIFlags::FlagIntroducedVirtual)) {
472558
Result |= DINode::DIFlags::FlagIntroducedVirtual;
473559
}
@@ -566,7 +652,9 @@ extern "C" LLVMMetadataRef LLVMRustDIBuilderCreatePointerType(
566652
uint64_t SizeInBits, uint32_t AlignInBits, const char *Name) {
567653
return wrap(Builder->createPointerType(unwrapDI<DIType>(PointeeTy),
568654
SizeInBits, AlignInBits,
655+
#if LLVM_VERSION_GE(5, 0)
569656
/* DWARFAddressSpace */ None,
657+
#endif
570658
Name));
571659
}
572660

@@ -737,8 +825,15 @@ LLVMRustDIBuilderCreateNameSpace(LLVMRustDIBuilderRef Builder,
737825
LLVMMetadataRef Scope, const char *Name,
738826
LLVMMetadataRef File, unsigned LineNo) {
739827
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+
,
741835
false // ExportSymbols (only relevant for C++ anonymous namespaces)
836+
#endif
742837
));
743838
}
744839

@@ -767,7 +862,12 @@ extern "C" int64_t LLVMRustDIBuilderCreateOpDeref() {
767862
}
768863

769864
extern "C" int64_t LLVMRustDIBuilderCreateOpPlusUconst() {
865+
#if LLVM_VERSION_GE(5, 0)
770866
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
771871
}
772872

773873
extern "C" void LLVMRustWriteTypeToString(LLVMTypeRef Ty, RustStringRef Str) {
@@ -839,12 +939,21 @@ extern "C" void LLVMRustUnpackOptimizationDiagnostic(
839939
*FunctionOut = wrap(&Opt->getFunction());
840940

841941
RawRustStringOstream FilenameOS(FilenameOut);
942+
#if LLVM_VERSION_GE(5,0)
842943
DiagnosticLocation loc = Opt->getLocation();
843944
if (loc.isValid()) {
844945
*Line = loc.getLine();
845946
*Column = loc.getColumn();
846947
FilenameOS << loc.getFilename();
847948
}
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
848957

849958
RawRustStringOstream MessageOS(MessageOut);
850959
MessageOS << Opt->getMsg();
@@ -1264,6 +1373,7 @@ LLVMRustModuleCost(LLVMModuleRef M) {
12641373
}
12651374

12661375
// Vector reductions:
1376+
#if LLVM_VERSION_GE(5, 0)
12671377
extern "C" LLVMValueRef
12681378
LLVMRustBuildVectorReduceFAdd(LLVMBuilderRef B, LLVMValueRef Acc, LLVMValueRef Src) {
12691379
return wrap(unwrap(B)->CreateFAddReduce(unwrap(Acc),unwrap(Src)));
@@ -1309,6 +1419,62 @@ LLVMRustBuildVectorReduceFMax(LLVMBuilderRef B, LLVMValueRef Src, bool NoNaN) {
13091419
return wrap(unwrap(B)->CreateFPMaxReduce(unwrap(Src), NoNaN));
13101420
}
13111421

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+
13121478
#if LLVM_VERSION_GE(6, 0)
13131479
extern "C" LLVMValueRef
13141480
LLVMRustBuildMinNum(LLVMBuilderRef B, LLVMValueRef LHS, LLVMValueRef RHS) {

0 commit comments

Comments
 (0)