Skip to content

Commit

Permalink
Merge pull request #4599 from liushuyu/llvm-18
Browse files Browse the repository at this point in the history
tree-wide: port LDC to LLVM 18
  • Loading branch information
kinke committed Mar 27, 2024
2 parents 3a6f2ca + fc4d884 commit 29ef439
Show file tree
Hide file tree
Showing 22 changed files with 6,111 additions and 32 deletions.
7 changes: 4 additions & 3 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-17.0 llvm-config170 llvm-config-17
set(llvm_config_names llvm-config-18.1 llvm-config181 llvm-config-18
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
Expand All @@ -49,12 +50,12 @@ if(APPLE)
# extra fallbacks for MacPorts & Homebrew
find_program(LLVM_CONFIG
NAMES ${llvm_config_names}
PATHS /opt/local/libexec/llvm-17/bin
PATHS /opt/local/libexec/llvm-18/bin /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@18/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 driver/archiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ namespace llvm_ar {
StringRef ArchiveName;
std::vector<const char *> Members;

#if LDC_LLVM_VER < 1800
bool Symtab = true;
#else
llvm::SymtabWritingMode Symtab = llvm::SymtabWritingMode::NormalSymtab;
#endif
bool Deterministic = true;
bool Thin = false;

Expand Down
3 changes: 2 additions & 1 deletion driver/codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "gen/logger.h"
#include "gen/modules.h"
#include "gen/runtime.h"
#include "gen/tollvm.h"
#include "ir/irdsymbol.h"
#if LDC_LLVM_VER >= 1400
#include "llvm/IR/DiagnosticInfo.h"
Expand Down Expand Up @@ -114,7 +115,7 @@ void emitLLVMUsedArray(IRState &irs) {
return;
}

auto *i8PtrType = llvm::Type::getInt8PtrTy(irs.context());
auto *i8PtrType = getVoidPtrType(irs.context());

// Convert all elements to i8* (the expected type for llvm.used)
for (auto &elem : irs.usedArray) {
Expand Down
4 changes: 2 additions & 2 deletions driver/targetmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
FloatABI::Type &floatABI,
llvm::Optional<llvm::Reloc::Model> relocModel,
llvm::Optional<llvm::CodeModel::Model> codeModel,
const llvm::CodeGenOpt::Level codeGenOptLevel,
const llvm::CodeGenOptLevel codeGenOptLevel,
const bool noLinkerStripDead) {
// Determine target triple. If the user didn't explicitly specify one, use
// the one set at LLVM configure time.
Expand Down Expand Up @@ -656,7 +656,7 @@ createTargetMachine(const std::string targetTriple, const std::string arch,

return target->createTargetMachine(triple.str(), cpu, finalFeaturesString,
targetOptions, relocModel, codeModel,
codeGenOptLevel);
static_cast<llvm::CodeGenOptLevel>(codeGenOptLevel));
}

ComputeBackend::Type getComputeTargetType(llvm::Module* m) {
Expand Down
7 changes: 6 additions & 1 deletion driver/targetmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ template <typename T> using Optional = std::optional<T>;
#include "llvm/Support/CodeGen.h"
#include <string>
#include <vector>
namespace llvm {
#if LDC_LLVM_VER < 1800
using CodeGenOptLevel = llvm::CodeGenOpt::Level;
#endif
}

namespace ExplicitBitness {
enum Type { None, M32, M64 };
Expand Down Expand Up @@ -66,7 +71,7 @@ createTargetMachine(std::string targetTriple, std::string arch, std::string cpu,
FloatABI::Type &floatABI,
llvm::Optional<llvm::Reloc::Model> relocModel,
llvm::Optional<llvm::CodeModel::Model> codeModel,
llvm::CodeGenOpt::Level codeGenOptLevel,
llvm::CodeGenOptLevel codeGenOptLevel,
bool noLinkerStripDead);

/**
Expand Down
13 changes: 11 additions & 2 deletions driver/toobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@

using CodeGenFileType = llvm::CodeGenFileType;

#if LDC_LLVM_VER >= 1800
constexpr llvm::CodeGenFileType CGFT_AssemblyFile = CodeGenFileType::AssemblyFile;
constexpr llvm::CodeGenFileType CGFT_ObjectFile = CodeGenFileType::ObjectFile;
constexpr llvm::CodeGenFileType CGFT_Null = CodeGenFileType::Null;
#endif

#if LDC_LLVM_VER < 1700
static llvm::cl::opt<bool>
NoIntegratedAssembler("no-integrated-as", llvm::cl::ZeroOrMore,
Expand Down Expand Up @@ -127,8 +133,11 @@ void codegenModule(llvm::TargetMachine &Target, llvm::Module &m,
nullptr, // DWO output file
// Always generate assembly for ptx as it is an assembly format
// The PTX backend fails if we pass anything else.
(cb == ComputeBackend::NVPTX) ? CGFT_AssemblyFile : fileType,
codeGenOptLevel())) {
(cb == ComputeBackend::NVPTX) ? CGFT_AssemblyFile : fileType
#if LDC_LLVM_VER < 1700
, codeGenOptLevel()
#endif
)) {
llvm_unreachable("no support for asm output");
}

Expand Down
12 changes: 10 additions & 2 deletions gen/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "gen/tollvm.h"
#include "ir/irfunction.h"
#include "ir/irmodule.h"
#include <llvm/Analysis/ConstantFolding.h>
#include <llvm/IR/Constant.h>

using namespace dmd;

Expand Down Expand Up @@ -556,8 +558,14 @@ llvm::Constant *arrayLiteralToConst(IRState *p, ArrayLiteralExp *ale) {
for (unsigned i = 0; i < ale->elements->length; ++i) {
llvm::Constant *val = toConstElem(indexArrayLiteral(ale, i), p);
// extend i1 to i8
if (val->getType()->isIntegerTy(1))
val = llvm::ConstantExpr::getZExt(val, LLType::getInt8Ty(p->context()));
if (val->getType()->isIntegerTy(1)) {
LLType *I8PtrTy = LLType::getInt8Ty(p->context());
#if LDC_LLVM_VER < 1800
val = llvm::ConstantExpr::getZExt(val, I8PtrTy);
#else
val = llvm::ConstantFoldCastOperand(llvm::Instruction::ZExt, val, I8PtrTy, *gDataLayout);
#endif
}
if (!elementType) {
elementType = val->getType();
} else {
Expand Down
5 changes: 4 additions & 1 deletion gen/dibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -622,13 +622,16 @@ DIType DIBuilder::CreateCompositeType(Type *t) {
const auto elemsArray = DBuilder.getOrCreateArray(elems);

DIType ret;
const auto runtimeLang = 0;
if (t->ty == TY::Tclass) {
ret = DBuilder.createClassType(
scope, name, file, lineNum, sizeInBits, alignmentInBits,
classOffsetInBits, DIFlags::FlagZero, derivedFrom, elemsArray,
#if LDC_LLVM_VER >= 1800
runtimeLang,
#endif
vtableHolder, templateParams, uniqueIdentifier);
} else {
const auto runtimeLang = 0;
ret = DBuilder.createStructType(scope, name, file, lineNum, sizeInBits,
alignmentInBits, DIFlags::FlagZero,
derivedFrom, elemsArray, runtimeLang,
Expand Down
11 changes: 10 additions & 1 deletion gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Utils/ModuleUtils.h"
#include <llvm/IR/Constant.h>
#include <llvm/Analysis/ConstantFolding.h>
#include <stack>

using namespace dmd;
Expand Down Expand Up @@ -1141,15 +1143,18 @@ LLConstant *DtoConstExpInit(const Loc &loc, Type *targetType, Expression *exp) {

LLType *llType = val->getType();
LLType *targetLLType = DtoMemType(baseTargetType);

// shortcut for zeros
if (val->isNullValue())
return llvm::Constant::getNullValue(targetLLType);

// extend i1 to i8
if (llType->isIntegerTy(1)) {
llType = LLType::getInt8Ty(gIR->context());
#if LDC_LLVM_VER < 1800
val = llvm::ConstantExpr::getZExt(val, llType);
#else
val = llvm::ConstantFoldCastOperand(llvm::Instruction::ZExt, val, llType, *gDataLayout);
#endif
}

if (llType == targetLLType)
Expand Down Expand Up @@ -1213,7 +1218,11 @@ LLConstant *DtoConstExpInit(const Loc &loc, Type *targetType, Expression *exp) {
"On initializer integer type mismatch, the target should be wider "
"than the source.");

#if LDC_LLVM_VER < 1800
return llvm::ConstantExpr::getZExtOrBitCast(val, target);
#else
return llvm::ConstantFoldCastOperand(llvm::Instruction::ZExt, val, target, *gDataLayout);
#endif
}

Logger::println("Unhandled type mismatch, giving up.");
Expand Down
5 changes: 2 additions & 3 deletions gen/ms-cxx-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ llvm::GlobalVariable *getTypeDescriptor(IRState &irs, ClassDeclaration *cd) {

// Declare and initialize the TypeDescriptor.
llvm::Constant *Fields[] = {
classInfoPtr, // VFPtr
llvm::ConstantPointerNull::get(
LLType::getInt8PtrTy(gIR->context())), // Runtime data
classInfoPtr, // VFPtr
llvm::ConstantPointerNull::get(getVoidPtrType()), // Runtime data
llvm::ConstantDataArray::getString(gIR->context(), TypeNameString)};
llvm::StructType *TypeDescriptorType =
getTypeDescriptorType(irs, classInfoPtr, TypeNameString);
Expand Down
21 changes: 12 additions & 9 deletions gen/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,15 @@ bool willCrossModuleInline() {

bool isOptimizationEnabled() { return optimizeLevel != 0; }

llvm::CodeGenOpt::Level codeGenOptLevel() {
llvm::CodeGenOptLevel codeGenOptLevel() {
// Use same appoach as clang (see lib/CodeGen/BackendUtil.cpp)
if (optLevel() == 0) {
return llvm::CodeGenOpt::None;
return llvm::CodeGenOptLevel::None;
}
if (optLevel() >= 3) {
return llvm::CodeGenOpt::Aggressive;
return llvm::CodeGenOptLevel::Aggressive;
}
return llvm::CodeGenOpt::Default;
return llvm::CodeGenOptLevel::Default;
}

std::unique_ptr<TargetLibraryInfoImpl> createTLII(llvm::Module &M) {
Expand Down Expand Up @@ -505,7 +505,13 @@ static void addPGOPasses(ModulePassManager &mpm,
options.NoRedZone = global.params.disableRedZone;
if (global.params.datafileInstrProf)
options.InstrProfileOutput = global.params.datafileInstrProf;
mpm.addPass(InstrProfiling(options));
mpm.addPass(
#if LDC_LLVM_VER < 1800
InstrProfiling(options)
#else
InstrProfilingLoweringPass(options)
#endif // LDC_LLVM_VER < 1800
);
} else if (opts::isUsingASTBasedPGOProfile()) {
// We are generating code with PGO profile information available.
// Do indirect call promotion from -O1
Expand Down Expand Up @@ -732,12 +738,9 @@ void runOptimizationPasses(llvm::Module *M) {
mpm = pb.buildO0DefaultPipeline(level, opts::isUsingLTO());
#if LDC_LLVM_VER >= 1700
} else if (opts::ltoFatObjects && opts::isUsingLTO()) {
mpm = pb.buildFatLTODefaultPipeline(level
#if LDC_LLVM_VER < 1800
,
mpm = pb.buildFatLTODefaultPipeline(level,
opts::isUsingThinLTO(),
opts::isUsingThinLTO()
#endif
);
#endif
} else if (opts::isUsingThinLTO()) {
Expand Down
8 changes: 7 additions & 1 deletion gen/optimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@

#include "llvm/Support/CommandLine.h"

namespace llvm {
#if LDC_LLVM_VER < 1800
using CodeGenOptLevel = llvm::CodeGenOpt::Level;
#endif
}

namespace llvm {
class raw_ostream;
}
Expand All @@ -39,7 +45,7 @@ unsigned optLevel();

bool isOptimizationEnabled();

llvm::CodeGenOpt::Level codeGenOptLevel();
llvm::CodeGenOptLevel codeGenOptLevel();

void verifyModule(llvm::Module *m);

Expand Down
11 changes: 9 additions & 2 deletions gen/pgo_ASTbased.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ llvm::cl::opt<bool, false, opts::FlagParser<bool>> enablePGOIndirectCalls(
llvm::cl::init(true));
}

#if LDC_LLVM_VER >= 1800
namespace llvm::support {
const auto little = llvm::endianness::little;
const auto big = llvm::endianness::big;
}
#endif

/// \brief Stable hasher for PGO region counters.
///
/// PGOHash produces a stable hash of a given function's control flow.
Expand Down Expand Up @@ -903,7 +910,7 @@ void CodeGenPGO::emitCounterIncrement(const RootObject *S) const {
assert(counter_it != (*RegionCounterMap).end() &&
"Statement not found in PGO counter map!");
unsigned counter = counter_it->second;
auto *I8PtrTy = llvm::Type::getInt8PtrTy(gIR->context());
auto *I8PtrTy = getVoidPtrType();
gIR->ir->CreateCall(GET_INTRINSIC_DECL(instrprof_increment),
{llvm::ConstantExpr::getBitCast(FuncNameVar, I8PtrTy),
gIR->ir->getInt64(FunctionHash),
Expand Down Expand Up @@ -1111,7 +1118,7 @@ void CodeGenPGO::valueProfile(uint32_t valueKind, llvm::Instruction *valueSite,
if (ptrCastNeeded)
value = gIR->ir->CreatePtrToInt(value, gIR->ir->getInt64Ty());

auto *i8PtrTy = llvm::Type::getInt8PtrTy(gIR->context());
auto *i8PtrTy = getVoidPtrType();
llvm::Value *Args[5] = {
llvm::ConstantExpr::getBitCast(FuncNameVar, i8PtrTy),
gIR->ir->getInt64(FunctionHash), value, gIR->ir->getInt32(valueKind),
Expand Down
5 changes: 5 additions & 0 deletions gen/tocall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "ir/irfunction.h"
#include "ir/irtype.h"
#include "llvm/IR/LLVMContext.h"
#include <llvm/IR/DerivedTypes.h>

using namespace dmd;

Expand Down Expand Up @@ -267,7 +268,11 @@ static LLType *getPtrToAtomicType(LLType *type) {
case 32:
case 64:
case 128:
#if LDC_LLVM_VER < 1800
return LLType::getIntNPtrTy(gIR->context(), static_cast<unsigned>(N));
#else
return LLPointerType::getIntNPtrTy(gIR->context(), static_cast<unsigned>(N));
#endif
default:
return nullptr;
}
Expand Down
10 changes: 8 additions & 2 deletions gen/toconstelem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "ir/irfunction.h"
#include "ir/irtypeclass.h"
#include "ir/irtypestruct.h"
#include <llvm/Analysis/ConstantFolding.h>
#include <llvm/IR/Constant.h>

using namespace dmd;

Expand Down Expand Up @@ -576,8 +578,12 @@ class ToConstElemVisitor : public Visitor {
LLConstant *c = toConstElem(elem, p);
// extend i1 to i8
if (c->getType()->isIntegerTy(1)) {
c = llvm::ConstantExpr::getZExt(c,
LLType::getInt8Ty(p->context()));
LLType *I8PtrTy = LLType::getInt8Ty(p->context());
#if LDC_LLVM_VER < 1800
c = llvm::ConstantExpr::getZExt(c, I8PtrTy);
#else
c = llvm::ConstantFoldCastOperand(llvm::Instruction::ZExt, c, I8PtrTy, *gDataLayout);
#endif
}
varInits[e->sd->fields[i]] = c;
}
Expand Down
6 changes: 5 additions & 1 deletion gen/tollvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,11 @@ LLPointerType *getPtrToType(LLType *t) {
}

LLPointerType *getVoidPtrType() {
return LLType::getInt8Ty(gIR->context())->getPointerTo();
return getVoidPtrType(gIR->context());
}

LLPointerType *getVoidPtrType(llvm::LLVMContext &C) {
return LLType::getInt8Ty(C)->getPointerTo();
}

llvm::ConstantPointerNull *getNullPtr(LLType *t) {
Expand Down
1 change: 1 addition & 0 deletions gen/tollvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ LLGlobalVariable *isaGlobalVar(LLValue *v);
LLType *getI8Type();
LLPointerType *getPtrToType(LLType *t);
LLPointerType *getVoidPtrType();
LLPointerType *getVoidPtrType(llvm::LLVMContext &C);
llvm::ConstantPointerNull *getNullPtr(LLType *t);
LLConstant *getNullValue(LLType *t);

Expand Down
2 changes: 1 addition & 1 deletion gen/trycatchfinally.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ TryCatchFinallyScopes::getLandingPadRef(CleanupCursor scope) {

namespace {
llvm::LandingPadInst *createLandingPadInst(IRState &irs) {
LLType *retType = LLStructType::get(LLType::getInt8PtrTy(irs.context()),
LLType *retType = LLStructType::get(getVoidPtrType(irs.context()),
LLType::getInt32Ty(irs.context()));
if (!irs.func()->hasLLVMPersonalityFn()) {
irs.func()->setLLVMPersonalityFn(
Expand Down
Loading

0 comments on commit 29ef439

Please sign in to comment.