ldc/ldc-1.32.2-llvm-16-use_std_optional_api.patch

205 lines
6.9 KiB
Diff
Raw Normal View History

From 8e1dffa5203439996d6c461485cad566ec321884 Mon Sep 17 00:00:00 2001
From: Ikey Doherty <ikey@serpentos.com>
Date: Mon, 22 May 2023 10:53:55 +0100
Subject: [PATCH] For LLVM >= 16, use std::optional APIs
Unfortunately when building with LLVM < 15, we're using C++-11,
so the std::optional bits are only available in C++-17.
Even more unfortunately we can't really override this, rather
inherit the fact of using C++-17 when using LLVM > 16.
Thus, we add a slightly messy conditional compilation situation
to allow usage of LDC on older LLVM versions, and with newer
LLVM/libcxx.
Signed-off-by: Ikey Doherty <ikey@serpentos.com>
---
driver/args.cpp | 4 ++++
driver/linker.cpp | 4 ++++
driver/linker.h | 4 ++++
gen/dcompute/druntime.cpp | 7 ++++++-
gen/dibuilder.cpp | 21 +++++++++++++++++++++
gen/optimizer.cpp | 5 +++++
gen/uda.cpp | 4 ++++
utils/not.cpp | 4 ++++
8 files changed, 52 insertions(+), 1 deletion(-)
diff --git a/driver/args.cpp b/driver/args.cpp
index da291202d1..bad8e45dee 100644
--- a/driver/args.cpp
+++ b/driver/args.cpp
@@ -175,7 +175,11 @@ int executeAndWait(std::vector<const char *> fullArgs,
}
const std::vector<llvm::StringRef> argv = toRefsVector(fullArgs);
+#if LDC_LLVM_VER < 1600
auto envVars = llvm::None;
+#else
+ auto envVars = std::nullopt;
+#endif
return llvm::sys::ExecuteAndWait(argv[0], argv, envVars, {}, 0, 0, errorMsg);
}
diff --git a/driver/linker.cpp b/driver/linker.cpp
index 76266379d2..803d5e883e 100644
--- a/driver/linker.cpp
+++ b/driver/linker.cpp
@@ -195,7 +195,11 @@ static std::vector<std::string> getDefaultLibNames() {
llvm::Optional<std::vector<std::string>> getExplicitPlatformLibs() {
if (platformLib.getNumOccurrences() > 0)
return parseLibNames(platformLib);
+#if LDC_LLVM_VER < 1600
return llvm::None;
+#else
+ return std::nullopt;
+#endif
}
//////////////////////////////////////////////////////////////////////////////
diff --git a/driver/linker.h b/driver/linker.h
index f962054efa..28362d8652 100644
--- a/driver/linker.h
+++ b/driver/linker.h
@@ -42,7 +42,11 @@ bool linkAgainstSharedDefaultLibs();
/**
* Returns the -platformlib library names, if specified.
*/
+#if LDC_LLVM_VER < 1600
llvm::Optional<std::vector<std::string>> getExplicitPlatformLibs();
+#else
+std::optional<std::vector<std::string>> getExplicitPlatformLibs();
+#endif
/**
* Returns the value of -mscrtlib.
diff --git a/gen/dcompute/druntime.cpp b/gen/dcompute/druntime.cpp
index 6867765466..aee93f4c53 100644
--- a/gen/dcompute/druntime.cpp
+++ b/gen/dcompute/druntime.cpp
@@ -42,8 +42,13 @@ bool isFromLDC_OpenCL(Dsymbol *sym) {
}
llvm::Optional<DcomputePointer> toDcomputePointer(StructDeclaration *sd) {
- if (sd->ident != Id::dcPointer || !isFromLDC_DCompute(sd))
+ if (sd->ident != Id::dcPointer || !isFromLDC_DCompute(sd)) {
+#if LDC_LLVM_VER < 1600
return llvm::Optional<DcomputePointer>(llvm::None);
+#else
+ return std::optional<DcomputePointer>(std::nullopt);
+#endif
+ }
TemplateInstance *ti = sd->isInstantiated();
int addrspace = isExpression((*ti->tiargs)[0])->toInteger();
diff --git a/gen/dibuilder.cpp b/gen/dibuilder.cpp
index 1e817ee3f4..f7068294dd 100644
--- a/gen/dibuilder.cpp
+++ b/gen/dibuilder.cpp
@@ -372,7 +372,11 @@ DIType DIBuilder::CreateEnumType(TypeEnum *type) {
DIType DIBuilder::CreatePointerType(TypePointer *type) {
// TODO: The addressspace is important for dcompute targets. See e.g.
// https://www.mail-archive.com/dwarf-discuss@lists.dwarfstd.org/msg00326.html
+#if LDC_LLVM_VER < 1600
const llvm::Optional<unsigned> DWARFAddressSpace = llvm::None;
+#else
+ const std::optional<unsigned> DWARFAddressSpace = std::nullopt;
+#endif
const auto name = processDIName(type->toPrettyChars(true));
@@ -730,7 +734,11 @@ DISubroutineType DIBuilder::CreateFunctionType(Type *type) {
}
DISubroutineType DIBuilder::CreateEmptyFunctionType() {
+#if LDC_LLVM_VER < 1600
auto paramsArray = DBuilder.getOrCreateTypeArray(llvm::None);
+#else
+ auto paramsArray = DBuilder.getOrCreateTypeArray(std::nullopt);
+#endif
return DBuilder.createSubroutineType(paramsArray);
}
@@ -774,9 +782,16 @@ DIType DIBuilder::CreateTypeDescription(Type *t, bool voidToUbyte) {
return nullptr;
if (t->ty == TY::Tnull) {
// display null as void*
+#if LDC_LLVM_VER < 1600
return DBuilder.createPointerType(
CreateTypeDescription(Type::tvoid), target.ptrsize * 8, 0,
/* DWARFAddressSpace */ llvm::None, "typeof(null)");
+#else
+ return DBuilder.createPointerType(
+ CreateTypeDescription(Type::tvoid), target.ptrsize * 8, 0,
+ /* DWARFAddressSpace */ std::nullopt, "typeof(null)");
+#endif
+
}
if (auto te = t->isTypeEnum())
return CreateEnumType(te);
@@ -798,8 +813,14 @@ DIType DIBuilder::CreateTypeDescription(Type *t, bool voidToUbyte) {
const auto aggregateDIType = CreateCompositeType(t);
const auto name =
(tc->sym->toPrettyChars(true) + llvm::StringRef("*")).str();
+#if LDC_LLVM_VER < 1600
return DBuilder.createPointerType(aggregateDIType, target.ptrsize * 8, 0,
llvm::None, processDIName(name));
+#else
+ return DBuilder.createPointerType(aggregateDIType, target.ptrsize * 8, 0,
+ std::nullopt, processDIName(name));
+#endif
+
}
if (auto tf = t->isTypeFunction())
return CreateFunctionType(tf);
diff --git a/gen/optimizer.cpp b/gen/optimizer.cpp
index 86146bf6f3..13bad60a04 100644
--- a/gen/optimizer.cpp
+++ b/gen/optimizer.cpp
@@ -572,8 +572,13 @@ static llvm::Optional<PGOOptions> getPGOOptions() {
PGOOptions::CSPGOAction::NoCSAction,
debugInfoForProfiling, pseudoProbeForProfiling);
}
+#if LDC_LLVM_VER < 1600
return None;
+#else
+ return std::nullopt;
+#endif
}
+
static PipelineTuningOptions getPipelineTuningOptions(unsigned optLevelVal, unsigned sizeLevelVal) {
PipelineTuningOptions pto;
diff --git a/gen/uda.cpp b/gen/uda.cpp
index 371b565b38..521205c5da 100644
--- a/gen/uda.cpp
+++ b/gen/uda.cpp
@@ -219,7 +219,11 @@ void applyAttrAllocSize(StructLiteralExp *sle, IrFunction *irFunc) {
if (numArgIdx >= 0) {
builder.addAllocSizeAttr(llvmSizeIdx, llvmNumIdx);
} else {
+#if LDC_LLVM_VER < 1600
builder.addAllocSizeAttr(llvmSizeIdx, llvm::Optional<unsigned>());
+#else
+ builder.addAllocSizeAttr(llvmSizeIdx, std::optional<unsigned>());
+#endif
}
llvm::Function *func = irFunc->getLLVMFunc();
diff --git a/utils/not.cpp b/utils/not.cpp
index b434af7331..7ca8d1d8a6 100644
--- a/utils/not.cpp
+++ b/utils/not.cpp
@@ -43,7 +43,11 @@ int main(int argc, const char **argv) {
Argv.reserve(argc);
for (int i = 0; i < argc; ++i)
Argv.push_back(argv[i]);
+#if LDC_LLVM_VER < 1600
auto Env = llvm::None;
+#else
+ auto Env = std::nullopt;
+#endif
std::string ErrMsg;
int Result = sys::ExecuteAndWait(*Program, Argv, Env, {}, 0, 0, &ErrMsg);