Skip to content

Commit 1ac5e84

Browse files
committed
rustc: Get rustc compiling with LLVM 3.{3,4} again
The travis builds have been breaking recently because LLVM 3.5 upstream is changing. This looks like it's likely to continue, so it would be more useful for us if we could lock ourselves to a system LLVM version that is not changing. This commit has the support to bring our C++ glue to LLVM back in line with what was possible back in LLVM 3.{3,4}. I don't think we're going to be able to reasonably protect against regressions in the future, but this kind of code is a good sign that we can continue to use the system LLVM for simple-ish things. Codegen for ARM won't work and it won't have some of the perf improvements we have, but using the system LLVM should work well enough for development.
1 parent 86177db commit 1ac5e84

File tree

3 files changed

+78
-8
lines changed

3 files changed

+78
-8
lines changed

src/rustllvm/PassWrapper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,11 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
166166
PassManager *PM = unwrap<PassManager>(PMR);
167167

168168
std::string ErrorInfo;
169+
#if LLVM_VERSION_MINOR >= 4
169170
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
171+
#else
172+
raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
173+
#endif
170174
if (ErrorInfo != "") {
171175
LLVMRustError = ErrorInfo.c_str();
172176
return false;
@@ -184,9 +188,21 @@ LLVMRustPrintModule(LLVMPassManagerRef PMR,
184188
const char* path) {
185189
PassManager *PM = unwrap<PassManager>(PMR);
186190
std::string ErrorInfo;
191+
192+
#if LLVM_VERSION_MINOR >= 4
187193
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
194+
#else
195+
raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
196+
#endif
197+
188198
formatted_raw_ostream FOS(OS);
199+
200+
#if LLVM_VERSION_MINOR >= 5
189201
PM->add(createPrintModulePass(FOS));
202+
#else
203+
PM->add(createPrintModulePass(&FOS));
204+
#endif
205+
190206
PM->run(*unwrap(M));
191207
}
192208

src/rustllvm/RustWrapper.cpp

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,14 @@ extern "C" void LLVMRemoveReturnAttribute(LLVMValueRef Fn, LLVMAttribute PA) {
9191
AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B));
9292
}
9393

94+
#if LLVM_VERSION_MINOR >= 5
9495
extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) {
9596
Function *A = unwrap<Function>(Fn);
9697
A->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
9798
}
99+
#else
100+
extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) {}
101+
#endif
98102

99103
extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,
100104
LLVMValueRef source,
@@ -156,7 +160,11 @@ DIT unwrapDI(LLVMValueRef ref) {
156160
return DIT(ref ? unwrap<MDNode>(ref) : NULL);
157161
}
158162

163+
#if LLVM_VERSION_MINOR >= 5
159164
extern "C" const uint32_t LLVMRustDebugMetadataVersion = DEBUG_METADATA_VERSION;
165+
#else
166+
extern "C" const uint32_t LLVMRustDebugMetadataVersion = 1;
167+
#endif
160168

161169
extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M,
162170
const char *name,
@@ -278,8 +286,12 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateStructType(
278286
unwrapDI<DIType>(DerivedFrom),
279287
unwrapDI<DIArray>(Elements),
280288
RunTimeLang,
281-
unwrapDI<DIType>(VTableHolder),
282-
UniqueId));
289+
unwrapDI<DIType>(VTableHolder)
290+
#if LLVM_VERSION_MINOR >= 5
291+
,UniqueId));
292+
#else
293+
));
294+
#endif
283295
}
284296

285297
extern "C" LLVMValueRef LLVMDIBuilderCreateMemberType(
@@ -440,8 +452,12 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateUnionType(
440452
AlignInBits,
441453
Flags,
442454
unwrapDI<DIArray>(Elements),
443-
RunTimeLang,
444-
UniqueId));
455+
RunTimeLang
456+
#if LLVM_VERSION_MINOR >= 5
457+
,UniqueId));
458+
#else
459+
));
460+
#endif
445461
}
446462

447463
extern "C" void LLVMSetUnnamedAddr(LLVMValueRef Value, LLVMBool Unnamed) {
@@ -541,6 +557,7 @@ extern "C" char *LLVMValueToString(LLVMValueRef Value) {
541557
return strdup(os.str().data());
542558
}
543559

560+
#if LLVM_VERSION_MINOR >= 5
544561
extern "C" bool
545562
LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
546563
Module *Dst = unwrap(dst);
@@ -559,6 +576,26 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
559576
}
560577
return true;
561578
}
579+
#else
580+
extern "C" bool
581+
LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
582+
Module *Dst = unwrap(dst);
583+
MemoryBuffer* buf = MemoryBuffer::getMemBufferCopy(StringRef(bc, len));
584+
std::string Err;
585+
Module *Src = llvm::getLazyBitcodeModule(buf, Dst->getContext(), &Err);
586+
if (!Src) {
587+
LLVMRustError = Err.c_str();
588+
delete buf;
589+
return false;
590+
}
591+
592+
if (Linker::LinkModules(Dst, Src, Linker::DestroySource, &Err)) {
593+
LLVMRustError = Err.c_str();
594+
return false;
595+
}
596+
return true;
597+
}
598+
#endif
562599

563600
extern "C" void*
564601
LLVMRustOpenArchive(char *path) {
@@ -578,9 +615,14 @@ LLVMRustOpenArchive(char *path) {
578615

579616
extern "C" const char*
580617
LLVMRustArchiveReadSection(Archive *ar, char *name, size_t *size) {
581-
for (Archive::child_iterator child = ar->child_begin(),
582-
end = ar->child_end();
583-
child != end; ++child) {
618+
#if LLVM_VERSION_MINOR >= 5
619+
Archive::child_iterator child = ar->child_begin(),
620+
end = ar->child_end();
621+
#else
622+
Archive::child_iterator child = ar->begin_children(),
623+
end = ar->end_children();
624+
#endif
625+
for (; child != end; ++child) {
584626
StringRef sect_name;
585627
error_code err = child->getName(sect_name);
586628
if (err) continue;
@@ -598,8 +640,15 @@ LLVMRustDestroyArchive(Archive *ar) {
598640
delete ar;
599641
}
600642

643+
#if LLVM_VERSION_MINOR >= 5
601644
extern "C" void
602645
LLVMRustSetDLLExportStorageClass(LLVMValueRef Value) {
603646
GlobalValue *V = unwrap<GlobalValue>(Value);
604647
V->setDLLStorageClass(GlobalValue::DLLExportStorageClass);
605648
}
649+
#else
650+
extern "C" void
651+
LLVMRustSetDLLExportStorageClass(LLVMValueRef Value) {
652+
LLVMSetLinkage(Value, LLVMDLLExportLinkage);
653+
}
654+
#endif

src/rustllvm/rustllvm.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "llvm/PassManager.h"
1717
#include "llvm/IR/InlineAsm.h"
1818
#include "llvm/IR/LLVMContext.h"
19-
#include "llvm/IR/IRPrintingPasses.h"
2019
#include "llvm/Analysis/Passes.h"
2120
#include "llvm/Analysis/Lint.h"
2221
#include "llvm/ADT/ArrayRef.h"
@@ -52,6 +51,12 @@
5251
#include "llvm-c/ExecutionEngine.h"
5352
#include "llvm-c/Object.h"
5453

54+
#if LLVM_VERSION_MINOR >= 5
55+
#include "llvm/IR/IRPrintingPasses.h"
56+
#else
57+
#include "llvm/Assembly/PrintModulePass.h"
58+
#endif
59+
5560
// Used by RustMCJITMemoryManager::getPointerToNamedFunction()
5661
// to get around glibc issues. See the function for more information.
5762
#ifdef __linux__

0 commit comments

Comments
 (0)