Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compilation with LLVM 17. #4533

Merged
merged 17 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion .github/workflows/supported_llvm_versions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ jobs:
fail-fast: false
matrix:
include:
- job_name: Ubuntu 20.04, LLVM 16, latest LDC beta
- job_name: Ubuntu 20.04, LDC-LLVM 17, latest LDC beta
os: ubuntu-20.04
host_dc: ldc-beta
llvm_version: 17.0.6
- job_name: Ubuntu 20.04, LDC-LLVM 16, latest LDC beta
os: ubuntu-20.04
host_dc: ldc-beta
llvm_version: 16.0.6 # LDC-LLVM
Expand Down Expand Up @@ -90,16 +94,25 @@ jobs:
version='${{ matrix.llvm_version }}'
if [[ '${{ runner.os }}' == macOS ]]; then
suffix='x86_64-apple-darwin'
elif [[ "$version" =~ ^1[7-9]\. ]]; then
suffix='x86_64-linux-gnu-ubuntu-22.04' # LLVM 17+
elif [[ "$version" =~ ^1[3-9]\. ]]; then
suffix='x86_64-linux-gnu-ubuntu-18.04' # LLVM 13.0.1+
else
suffix='x86_64-linux-gnu-ubuntu-16.04'
fi
url="https://github.com/llvm/llvm-project/releases/download/llvmorg-$version/clang+llvm-$version-$suffix.tar.xz"

# FIXME: weird crashes with official v16.0.0 archive; use LDC-LLVM instead
if [[ "$version" =~ ^16\. ]]; then
url="https://github.com/ldc-developers/llvm-project/releases/download/ldc-v$version/llvm-$version-linux-x86_64.tar.xz"
fi
# FIXME: Use LDC-LLVM v17.0.6 RC package because it still works on Ubuntu 20 (official release doesn't), so
# we can still use older gdb and postpone fxixing issues with newer gdb
if [[ "$version" =~ ^17\. ]]; then
url="https://github.com/ldc-developers/llvm-project/releases/download/CI/llvm-be7fee94-linux-x86_64.tar.xz"
fi

curl -fL --retry 3 --max-time 300 -o llvm.tar.xz "$url"
mkdir llvm
tar -xf llvm.tar.xz --strip 1 -C llvm
Expand Down
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,13 @@ function(copy_compilerrt_lib llvm_lib_name ldc_lib_name fixup_dylib)
set(ldc_lib_path ${PROJECT_BINARY_DIR}/lib${LIB_SUFFIX}/${ldc_lib_name})
copy_and_rename_file(${llvm_lib_path} ${ldc_lib_path})
if (fixup_dylib)
execute_process(COMMAND install_name_tool -id @rpath/${ldc_lib_name} ${ldc_lib_path})
execute_process(COMMAND install_name_tool -id @rpath/${ldc_lib_name} ${ldc_lib_path} ERROR_VARIABLE INSTALL_NAME_TOOL_STDERR)
if(${INSTALL_NAME_TOOL_STDERR} MATCHES "warning: changes being made to the file will invalidate the code signature")
# Eat the warning, it's ok.
else()
message(WARNING "install_name_tool stderr: ${INSTALL_NAME_TOOL_STDERR}")
endif()
execute_process(COMMAND codesign --force -s - ${ldc_lib_path})
endif()
install(FILES ${ldc_lib_path} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib${LIB_SUFFIX})
else()
Expand Down
7 changes: 5 additions & 2 deletions cmake/Modules/FindLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
# We also want an user-specified LLVM_ROOT_DIR to take precedence over the
# system default locations such as /usr/local/bin. Executing find_program()
# multiples times is the approach recommended in the docs.
set(llvm_config_names llvm-config-16.0 llvm-config160 llvm-config-16
set(llvm_config_names llvm-config-17.0 llvm-config170 llvm-config-17
llvm-config-16.0 llvm-config160 llvm-config-16
llvm-config-15.0 llvm-config150 llvm-config-15
llvm-config-14.0 llvm-config140 llvm-config-14
llvm-config-13.0 llvm-config130 llvm-config-13
Expand All @@ -48,10 +49,12 @@ if(APPLE)
# extra fallbacks for MacPorts & Homebrew
find_program(LLVM_CONFIG
NAMES ${llvm_config_names}
PATHS /opt/local/libexec/llvm-16/bin /opt/local/libexec/llvm-15/bin
PATHS /opt/local/libexec/llvm-17/bin
/opt/local/libexec/llvm-16/bin /opt/local/libexec/llvm-15/bin
/opt/local/libexec/llvm-14/bin /opt/local/libexec/llvm-13/bin
/opt/local/libexec/llvm-12/bin /opt/local/libexec/llvm-11/bin
/opt/local/libexec/llvm/bin
/usr/local/opt/llvm@17/bin
/usr/local/opt/llvm@16/bin /usr/local/opt/llvm@15/bin
/usr/local/opt/llvm@14/bin /usr/local/opt/llvm@13/bin
/usr/local/opt/llvm@12/bin /usr/local/opt/llvm@11/bin
Expand Down
4 changes: 4 additions & 0 deletions dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
#include "compiler.h"

#if IN_LLVM
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#else
#include "llvm/TargetParser/Triple.h"
#endif

enum OUTPUTFLAG
{
Expand Down
7 changes: 6 additions & 1 deletion driver/archiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
#include "driver/timetrace.h"
#include "driver/tool.h"
#include "gen/logger.h"
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#include "llvm/Support/Host.h"
#else
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/Triple.h"
#endif
#include "llvm/Object/Archive.h"
#include "llvm/Object/ArchiveWriter.h"
#include "llvm/Object/MachO.h"
#include "llvm/Object/ObjectFile.h"
#include "llvm/Support/Errc.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ToolDrivers/llvm-lib/LibDriver.h"
Expand Down
4 changes: 4 additions & 0 deletions driver/cl_options-llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#include "driver/cl_options-llvm.h"

#if LDC_WITH_LLD
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#else
#include "llvm/TargetParser/Triple.h"
#endif
#endif

// Pull in command-line options and helper functions from special LLVM header
Expand Down
7 changes: 7 additions & 0 deletions driver/cl_options-llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

#pragma once

#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Optional.h"
#else
#include <optional>
namespace llvm {
template <typename T> using Optional = std::optional<T>;
}
#endif
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Target/TargetOptions.h"
Expand Down
4 changes: 3 additions & 1 deletion driver/cl_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,9 @@ cl::opt<bool> dynamicCompileTlsWorkaround(
cl::Hidden);
#endif

#if LDC_LLVM_VER >= 1400
#if LDC_LLVM_VER >= 1700
bool enableOpaqueIRPointers = true; // typed pointers are no longer supported from LLVM 17
#elif LDC_LLVM_VER >= 1400
bool enableOpaqueIRPointers = false;
#endif

Expand Down
4 changes: 4 additions & 0 deletions driver/cl_options_instrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@
#include "dmd/errors.h"
#include "dmd/globals.h"
#include "gen/to_string.h"
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#else
#include "llvm/TargetParser/Triple.h"
#endif

namespace {
namespace cl = llvm::cl;
Expand Down
7 changes: 7 additions & 0 deletions driver/linker-gcc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@

#if LDC_WITH_LLD
#include "lld/Common/Driver.h"
#if LDC_LLVM_VER >= 1700
LLD_HAS_DRIVER(coff)
LLD_HAS_DRIVER(elf)
LLD_HAS_DRIVER(mingw)
LLD_HAS_DRIVER(macho)
LLD_HAS_DRIVER(wasm)
#endif
#endif

//////////////////////////////////////////////////////////////////////////////
Expand Down
8 changes: 7 additions & 1 deletion driver/linker-msvc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

#if LDC_WITH_LLD
#include "lld/Common/Driver.h"
#if LDC_LLVM_VER >= 1700
LLD_HAS_DRIVER(coff)
#endif
#endif

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -36,7 +39,10 @@ void addMscrtLibs(bool useInternalToolchain, std::vector<std::string> &args) {
// We need the vcruntime lib for druntime's exception handling (ldc.eh_msvc).
// Pick one of the 4 variants matching the selected main UCRT lib.

#if LDC_LLVM_VER >= 1300
#if LDC_LLVM_VER >= 1700
#define contains_lower contains_insensitive
#define endswith_lower ends_with_insensitive
#elif LDC_LLVM_VER >= 1300
#define contains_lower contains_insensitive
#define endswith_lower endswith_insensitive
#endif
Expand Down
19 changes: 16 additions & 3 deletions driver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
#include "llvm/LinkAllIR.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/Support/FileSystem.h"
#if LDC_LLVM_VER >= 1700
#include "llvm/TargetParser/Host.h"
#else
#include "llvm/Support/Host.h"
#endif
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/StringSaver.h"
Expand Down Expand Up @@ -565,7 +569,11 @@ void parseCommandLine(Strings &sourceFiles) {
}
#endif

#if LDC_LLVM_VER >= 1500
#if LDC_LLVM_VER >= 1700
if (!opts::enableOpaqueIRPointers)
error(Loc(),
"LLVM version 17 or above only supports --opaque-pointers=true");
#elif LDC_LLVM_VER >= 1500
getGlobalContext().setOpaquePointers(opts::enableOpaqueIRPointers);
#elif LDC_LLVM_VER >= 1400
if (opts::enableOpaqueIRPointers)
Expand Down Expand Up @@ -597,9 +605,11 @@ void initializePasses() {
initializeGlobalISel(Registry);
initializeTarget(Registry);

#if LDC_LLVM_VER < 1700
// Initialize passes not included above
initializeRewriteSymbolsLegacyPassPass(Registry);
initializeSjLjEHPreparePass(Registry);
#endif
}

/// Register the MIPS ABI.
Expand Down Expand Up @@ -1023,8 +1033,11 @@ void registerPredefinedVersions() {
VersionCondition::addPredefinedGlobalIdent("LDC_ThreadSanitizer");
}

#if LDC_LLVM_VER >= 1400
// A version identifier for whether opaque pointers are enabled or not. (needed e.g. for intrinsic mangling)
// Set a version identifier for whether opaque pointers are enabled or not. (needed e.g. for intrinsic mangling)
#if LDC_LLVM_VER >= 1700
// Since LLVM 17, IR pointers are always opaque.
VersionCondition::addPredefinedGlobalIdent("LDC_LLVM_OpaquePointers");
#elif LDC_LLVM_VER >= 1400
if (!getGlobalContext().supportsTypedPointers()) {
VersionCondition::addPredefinedGlobalIdent("LDC_LLVM_OpaquePointers");
}
Expand Down
26 changes: 19 additions & 7 deletions driver/targetmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,34 @@
#include "gen/logger.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringSwitch.h"
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Triple.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/SubtargetFeature.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/TargetParser.h"
#if LDC_LLVM_VER >= 1400
#include "llvm/Support/AArch64TargetParser.h"
#include "llvm/Support/ARMTargetParser.h"
#endif
#else
#include "llvm/TargetParser/AArch64TargetParser.h"
#include "llvm/TargetParser/ARMTargetParser.h"
#include "llvm/TargetParser/Host.h"
#include "llvm/TargetParser/SubtargetFeature.h"
#include "llvm/TargetParser/TargetParser.h"
#include "llvm/TargetParser/Triple.h"
#endif
#include "llvm/IR/Module.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/Support/CommandLine.h"
#if LDC_LLVM_VER >= 1400
#include "llvm/MC/TargetRegistry.h"
#else
#include "llvm/Support/TargetRegistry.h"
#endif
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#if LDC_LLVM_VER >= 1400
#include "llvm/Support/AArch64TargetParser.h"
#include "llvm/Support/ARMTargetParser.h"
#endif

#include "gen/optimizer.h"

Expand Down Expand Up @@ -630,7 +639,10 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
// LLVM fork. LLVM 7+ enables regular emutls by default; prevent that.
if (triple.getEnvironment() == llvm::Triple::Android) {
targetOptions.EmulatedTLS = false;
#if LDC_LLVM_VER < 1700
// Removed in this commit: https://github.com/llvm/llvm-project/commit/0d333bf0e3aa37e2e6ae211e3aa80631c3e01b85
targetOptions.ExplicitEmulatedTLS = true;
#endif
}

const std::string finalFeaturesString =
Expand Down
7 changes: 7 additions & 0 deletions driver/targetmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@

#pragma once

#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Optional.h"
#else
#include <optional>
namespace llvm {
template <typename T> using Optional = std::optional<T>;
}
#endif
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/CodeGen.h"
Expand Down
9 changes: 9 additions & 0 deletions driver/toobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,19 @@

using CodeGenFileType = llvm::CodeGenFileType;

#if LDC_LLVM_VER < 1700
static llvm::cl::opt<bool>
NoIntegratedAssembler("no-integrated-as", llvm::cl::ZeroOrMore,
llvm::cl::Hidden,
llvm::cl::desc("Disable integrated assembler"));
#else
namespace llvm {
namespace codegen {
bool getDisableIntegratedAS();
}
}
#define NoIntegratedAssembler llvm::codegen::getDisableIntegratedAS()
#endif

namespace {

Expand Down
7 changes: 7 additions & 0 deletions gen/dcompute/druntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/tollvm.h"
#if LDC_LLVM_VER < 1700
#include "llvm/ADT/Optional.h"
#else
#include <optional>
namespace llvm {
template <typename T> using Optional = std::optional<T>;
}
#endif

class Dsymbol;
class Type;
Expand Down
5 changes: 5 additions & 0 deletions gen/dvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ DRValue *DLValue::getRVal() {
////////////////////////////////////////////////////////////////////////////////

DSpecialRefValue::DSpecialRefValue(Type *t, LLValue *v) : DLValue(v, t) {
#if LDC_LLVM_VER >= 1700 // LLVM >= 17 uses opaque pointers, type check boils
// down to pointer check only.
assert(v->getType()->isPointerTy());
#else
assert(v->getType() == DtoPtrToType(t)->getPointerTo());
#endif
}

DRValue *DSpecialRefValue::getRVal() {
Expand Down
5 changes: 5 additions & 0 deletions gen/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,12 @@ void defineParameters(IrFuncTy &irFty, VarDeclarations &parameters) {
if (irparam->arg->byref) {
// The argument is an appropriate lvalue passed by reference.
// Use the passed pointer as parameter storage.
#if LDC_LLVM_VER >= 1700 // LLVM >= 17 uses opaque pointers, type check boils
// down to pointer check only.
assert(irparam->value->getType()->isPointerTy());
#else
assert(irparam->value->getType() == DtoPtrToType(paramType));
#endif
} else {
// Let the ABI transform the parameter back to an lvalue.
irparam->value =
Expand Down
10 changes: 9 additions & 1 deletion gen/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Path.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#if LDC_LLVM_VER >= 1700
#include "llvm/Support/VirtualFileSystem.h"
#endif

#if _AIX || __sun
#include <alloca.h>
Expand Down Expand Up @@ -354,7 +357,12 @@ void loadInstrProfileData(IRState *irs) {
global.params.datafileInstrProf);

auto readerOrErr =
llvm::IndexedInstrProfReader::create(global.params.datafileInstrProf);
llvm::IndexedInstrProfReader::create(global.params.datafileInstrProf
#if LDC_LLVM_VER >= 1700
,
*llvm::vfs::getRealFileSystem()
#endif
);
if (auto E = readerOrErr.takeError()) {
handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
error(irs->dmodule->loc, "Could not read profile file '%s': %s",
Expand Down
Loading