Fix LLVM 5.0 build (#2173)

* Fix LLVM 5.0 compilation.

* Fix adding linker option metadata to LLVM module.

See https://reviews.llvm.org/D31349

* Version the SPIRV cache dir on travis.
This commit is contained in:
Johan Engelen 2017-07-02 11:35:40 +02:00 committed by GitHub
parent 76ebf6476c
commit fdd66e5ea7
6 changed files with 81 additions and 16 deletions

View file

@ -23,7 +23,7 @@ matrix:
env: LLVM_VERSION=3.5.2 OPTS="-DTEST_COVERAGE=ON"
- os: osx
d: ldc-beta
env: LLVM_VERSION=5.0.0 OPTS="-DBUILD_SHARED_LIBS=OFF" LLVM_SPIRV_AVAILABLE=ON
env: LLVM_VERSION=5.0.0-1 OPTS="-DBUILD_SHARED_LIBS=OFF" LLVM_SPIRV_AVAILABLE=ON
- os: osx
d: ldc
env: LLVM_VERSION=4.0.0 OPTS="-DBUILD_SHARED_LIBS=ON"
@ -32,7 +32,7 @@ matrix:
cache:
directories:
- llvm-spirv-5.0.0
- llvm-spirv-5.0.0-1
- llvm-4.0.1
- llvm-4.0.0
- llvm-3.9.1

View file

@ -73,7 +73,8 @@ createAndSetDiagnosticsOutputFile(IRState &irs, llvm::LLVMContext &ctx,
namespace {
/// Add the linker options metadata flag.
#if LDC_LLVM_VER < 500
/// Add the Linker Options module flag.
/// If the flag is already present, merge it with the new data.
void emitLinkerOptions(IRState &irs, llvm::Module &M, llvm::LLVMContext &ctx) {
if (!M.getModuleFlag("Linker Options")) {
@ -119,6 +120,31 @@ void emitLinkerOptions(IRState &irs, llvm::Module &M, llvm::LLVMContext &ctx) {
#endif
}
}
#else
/// Add the "llvm.linker.options" metadata.
/// If the metadata is already present, merge it with the new data.
void emitLinkerOptions(IRState &irs, llvm::Module &M, llvm::LLVMContext &ctx) {
auto *linkerOptionsMD = M.getOrInsertNamedMetadata("llvm.linker.options");
// Add the new operands in front of the existing ones, such that linker
// options of .bc files passed on the cmdline are put _after_ the compiled .d
// file.
// Temporarily store metadata nodes that are already present
llvm::SmallVector<llvm::MDNode *, 5> oldMDNodes;
for (auto *MD : linkerOptionsMD->operands())
oldMDNodes.push_back(MD);
// Clear the list and add the new metadata nodes.
linkerOptionsMD->clearOperands();
for (auto *MD : irs.LinkerMetadataArgs)
linkerOptionsMD->addOperand(MD);
// Re-add metadata nodes that were already present
for (auto *MD : oldMDNodes)
linkerOptionsMD->addOperand(MD);
}
#endif
void emitLLVMUsedArray(IRState &irs) {
if (irs.usedArray.empty()) {

View file

@ -118,33 +118,45 @@ static inline llvm::Optional<llvm::Reloc::Model> getRelocModel() {
static inline llvm::Reloc::Model getRelocModel() { return mRelocModel; }
#endif
void printVersion() {
printf("LDC - the LLVM D compiler (%s):\n", global.ldc_version);
printf(" based on DMD %s and LLVM %s\n", global.version,
global.llvm_version);
printf(" built with %s\n", ldc::built_with_Dcompiler_version);
// This function exits the program.
void printVersion(llvm::raw_ostream &OS) {
OS << "LDC - the LLVM D compiler (" << global.ldc_version << "):\n";
OS << " based on DMD " << global.version << " and LLVM " << global.llvm_version << "\n";
OS << " built with " << ldc::built_with_Dcompiler_version << "\n";
#if defined(__has_feature)
#if __has_feature(address_sanitizer)
printf(" compiled with address sanitizer enabled\n");
OS << " compiled with address sanitizer enabled\n";
#endif
#endif
printf(" Default target: %s\n", llvm::sys::getDefaultTargetTriple().c_str());
OS << " Default target: " << llvm::sys::getDefaultTargetTriple() << "\n";
std::string CPU = llvm::sys::getHostCPUName();
if (CPU == "generic") {
CPU = "(unknown)";
}
printf(" Host CPU: %s\n", CPU.c_str());
printf(" http://dlang.org - http://wiki.dlang.org/LDC\n");
printf("\n");
OS << " Host CPU: " << CPU << "\n";
OS << " http://dlang.org - http://wiki.dlang.org/LDC\n";
OS << "\n";
// Without explicitly flushing here, only the target list is visible when
// redirecting stdout to a file.
fflush(stdout);
OS.flush();
llvm::TargetRegistry::printRegisteredTargetsForVersion(
#if LDC_LLVM_VER >= 500
OS
#endif
);
llvm::TargetRegistry::printRegisteredTargetsForVersion();
exit(EXIT_SUCCESS);
}
// This function exits the program.
void printVersionStdout() {
printVersion(llvm::outs());
assert(false);
}
namespace {
// Helper function to handle -d-debug=* and -d-version=*
@ -351,7 +363,11 @@ void parseCommandLine(int argc, char **argv, Strings &sourceFiles,
// finalize by expanding response files specified in config file
expandResponseFiles(allocator, allArguments);
#if LDC_LLVM_VER >= 500
cl::SetVersionPrinter(&printVersion);
#else
cl::SetVersionPrinter(&printVersionStdout);
#endif
opts::hideLLVMOptions();
opts::createClashingOptions();

View file

@ -200,7 +200,9 @@ struct IRState {
llvm::StringMap<llvm::GlobalVariable *> stringLiteral4ByteCache;
/// Vector of options passed to the linker as metadata in object file.
#if LDC_LLVM_VER >= 306
#if LDC_LLVM_VER >= 500
llvm::SmallVector<llvm::MDNode *, 5> LinkerMetadataArgs;
#elif LDC_LLVM_VER >= 306
llvm::SmallVector<llvm::Metadata *, 5> LinkerMetadataArgs;
#else
llvm::SmallVector<llvm::Value *, 5> LinkerMetadataArgs;

View file

@ -1,6 +1,8 @@
// Test passing of LLVM bitcode file with Linker Options set
// REQUIRES: atleast_llvm306
// LLVM >= 5.0 uses llvm.linker.options instead. See link_bitcode_libs_500.d.
// REQUIRES: atmost_llvm400
// Linker Options are currently only set on Windows platform, so we must (cross-)compile to Windows
// REQUIRES: target_X86

View file

@ -0,0 +1,19 @@
// Test passing of LLVM bitcode file with Linker Options set
// REQUIRES: atleast_llvm500
// Linker Options are currently only set on Windows platform, so we must (cross-)compile to Windows
// REQUIRES: target_X86
// RUN: %ldc -mtriple=x86_64-windows -c -output-bc %S/inputs/link_bitcode_libs_input.d -of=%t.bc \
// RUN: && %ldc -mtriple=x86_64-windows -c -singleobj -output-ll %t.bc %s -of=%t.ll \
// RUN: && FileCheck %s < %t.ll
pragma(lib, "library_one");
pragma(lib, "library_two");
// CHECK: !llvm.linker.options = !{![[ATTR_LIB1:[0-9]+]], ![[ATTR_LIB2:[0-9]+]], ![[ATTR_LIB3:[0-9]+]], ![[ATTR_LIB4:[0-9]+]]}
// CHECK: ![[ATTR_LIB1]]{{.*}}library_one
// CHECK: ![[ATTR_LIB2]]{{.*}}library_two
// CHECK: ![[ATTR_LIB3]]{{.*}}imported_one
// CHECK: ![[ATTR_LIB4]]{{.*}}imported_two