1
Fork 0

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.
This commit is contained in:
Alex Crichton 2014-02-26 14:06:27 -08:00
parent 86177dbbcf
commit 1ac5e84e91
3 changed files with 78 additions and 8 deletions

View file

@ -166,7 +166,11 @@ LLVMRustWriteOutputFile(LLVMTargetMachineRef Target,
PassManager *PM = unwrap<PassManager>(PMR); PassManager *PM = unwrap<PassManager>(PMR);
std::string ErrorInfo; std::string ErrorInfo;
#if LLVM_VERSION_MINOR >= 4
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None); raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
#else
raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
#endif
if (ErrorInfo != "") { if (ErrorInfo != "") {
LLVMRustError = ErrorInfo.c_str(); LLVMRustError = ErrorInfo.c_str();
return false; return false;
@ -184,9 +188,21 @@ LLVMRustPrintModule(LLVMPassManagerRef PMR,
const char* path) { const char* path) {
PassManager *PM = unwrap<PassManager>(PMR); PassManager *PM = unwrap<PassManager>(PMR);
std::string ErrorInfo; std::string ErrorInfo;
#if LLVM_VERSION_MINOR >= 4
raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None); raw_fd_ostream OS(path, ErrorInfo, sys::fs::F_None);
#else
raw_fd_ostream OS(path, ErrorInfo, raw_fd_ostream::F_Binary);
#endif
formatted_raw_ostream FOS(OS); formatted_raw_ostream FOS(OS);
#if LLVM_VERSION_MINOR >= 5
PM->add(createPrintModulePass(FOS)); PM->add(createPrintModulePass(FOS));
#else
PM->add(createPrintModulePass(&FOS));
#endif
PM->run(*unwrap(M)); PM->run(*unwrap(M));
} }

View file

@ -91,10 +91,14 @@ extern "C" void LLVMRemoveReturnAttribute(LLVMValueRef Fn, LLVMAttribute PA) {
AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B)); AttributeSet::get(A->getContext(), AttributeSet::ReturnIndex, B));
} }
#if LLVM_VERSION_MINOR >= 5
extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) { extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) {
Function *A = unwrap<Function>(Fn); Function *A = unwrap<Function>(Fn);
A->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold); A->addAttribute(AttributeSet::FunctionIndex, Attribute::Cold);
} }
#else
extern "C" void LLVMAddColdAttribute(LLVMValueRef Fn) {}
#endif
extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B, extern "C" LLVMValueRef LLVMBuildAtomicLoad(LLVMBuilderRef B,
LLVMValueRef source, LLVMValueRef source,
@ -156,7 +160,11 @@ DIT unwrapDI(LLVMValueRef ref) {
return DIT(ref ? unwrap<MDNode>(ref) : NULL); return DIT(ref ? unwrap<MDNode>(ref) : NULL);
} }
#if LLVM_VERSION_MINOR >= 5
extern "C" const uint32_t LLVMRustDebugMetadataVersion = DEBUG_METADATA_VERSION; extern "C" const uint32_t LLVMRustDebugMetadataVersion = DEBUG_METADATA_VERSION;
#else
extern "C" const uint32_t LLVMRustDebugMetadataVersion = 1;
#endif
extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M, extern "C" void LLVMRustAddModuleFlag(LLVMModuleRef M,
const char *name, const char *name,
@ -278,8 +286,12 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateStructType(
unwrapDI<DIType>(DerivedFrom), unwrapDI<DIType>(DerivedFrom),
unwrapDI<DIArray>(Elements), unwrapDI<DIArray>(Elements),
RunTimeLang, RunTimeLang,
unwrapDI<DIType>(VTableHolder), unwrapDI<DIType>(VTableHolder)
UniqueId)); #if LLVM_VERSION_MINOR >= 5
,UniqueId));
#else
));
#endif
} }
extern "C" LLVMValueRef LLVMDIBuilderCreateMemberType( extern "C" LLVMValueRef LLVMDIBuilderCreateMemberType(
@ -440,8 +452,12 @@ extern "C" LLVMValueRef LLVMDIBuilderCreateUnionType(
AlignInBits, AlignInBits,
Flags, Flags,
unwrapDI<DIArray>(Elements), unwrapDI<DIArray>(Elements),
RunTimeLang, RunTimeLang
UniqueId)); #if LLVM_VERSION_MINOR >= 5
,UniqueId));
#else
));
#endif
} }
extern "C" void LLVMSetUnnamedAddr(LLVMValueRef Value, LLVMBool Unnamed) { extern "C" void LLVMSetUnnamedAddr(LLVMValueRef Value, LLVMBool Unnamed) {
@ -541,6 +557,7 @@ extern "C" char *LLVMValueToString(LLVMValueRef Value) {
return strdup(os.str().data()); return strdup(os.str().data());
} }
#if LLVM_VERSION_MINOR >= 5
extern "C" bool extern "C" bool
LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) { LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
Module *Dst = unwrap(dst); Module *Dst = unwrap(dst);
@ -559,6 +576,26 @@ LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
} }
return true; return true;
} }
#else
extern "C" bool
LLVMRustLinkInExternalBitcode(LLVMModuleRef dst, char *bc, size_t len) {
Module *Dst = unwrap(dst);
MemoryBuffer* buf = MemoryBuffer::getMemBufferCopy(StringRef(bc, len));
std::string Err;
Module *Src = llvm::getLazyBitcodeModule(buf, Dst->getContext(), &Err);
if (!Src) {
LLVMRustError = Err.c_str();
delete buf;
return false;
}
if (Linker::LinkModules(Dst, Src, Linker::DestroySource, &Err)) {
LLVMRustError = Err.c_str();
return false;
}
return true;
}
#endif
extern "C" void* extern "C" void*
LLVMRustOpenArchive(char *path) { LLVMRustOpenArchive(char *path) {
@ -578,9 +615,14 @@ LLVMRustOpenArchive(char *path) {
extern "C" const char* extern "C" const char*
LLVMRustArchiveReadSection(Archive *ar, char *name, size_t *size) { LLVMRustArchiveReadSection(Archive *ar, char *name, size_t *size) {
for (Archive::child_iterator child = ar->child_begin(), #if LLVM_VERSION_MINOR >= 5
Archive::child_iterator child = ar->child_begin(),
end = ar->child_end(); end = ar->child_end();
child != end; ++child) { #else
Archive::child_iterator child = ar->begin_children(),
end = ar->end_children();
#endif
for (; child != end; ++child) {
StringRef sect_name; StringRef sect_name;
error_code err = child->getName(sect_name); error_code err = child->getName(sect_name);
if (err) continue; if (err) continue;
@ -598,8 +640,15 @@ LLVMRustDestroyArchive(Archive *ar) {
delete ar; delete ar;
} }
#if LLVM_VERSION_MINOR >= 5
extern "C" void extern "C" void
LLVMRustSetDLLExportStorageClass(LLVMValueRef Value) { LLVMRustSetDLLExportStorageClass(LLVMValueRef Value) {
GlobalValue *V = unwrap<GlobalValue>(Value); GlobalValue *V = unwrap<GlobalValue>(Value);
V->setDLLStorageClass(GlobalValue::DLLExportStorageClass); V->setDLLStorageClass(GlobalValue::DLLExportStorageClass);
} }
#else
extern "C" void
LLVMRustSetDLLExportStorageClass(LLVMValueRef Value) {
LLVMSetLinkage(Value, LLVMDLLExportLinkage);
}
#endif

View file

@ -16,7 +16,6 @@
#include "llvm/PassManager.h" #include "llvm/PassManager.h"
#include "llvm/IR/InlineAsm.h" #include "llvm/IR/InlineAsm.h"
#include "llvm/IR/LLVMContext.h" #include "llvm/IR/LLVMContext.h"
#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/Analysis/Passes.h" #include "llvm/Analysis/Passes.h"
#include "llvm/Analysis/Lint.h" #include "llvm/Analysis/Lint.h"
#include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/ArrayRef.h"
@ -52,6 +51,12 @@
#include "llvm-c/ExecutionEngine.h" #include "llvm-c/ExecutionEngine.h"
#include "llvm-c/Object.h" #include "llvm-c/Object.h"
#if LLVM_VERSION_MINOR >= 5
#include "llvm/IR/IRPrintingPasses.h"
#else
#include "llvm/Assembly/PrintModulePass.h"
#endif
// Used by RustMCJITMemoryManager::getPointerToNamedFunction() // Used by RustMCJITMemoryManager::getPointerToNamedFunction()
// to get around glibc issues. See the function for more information. // to get around glibc issues. See the function for more information.
#ifdef __linux__ #ifdef __linux__