Fix compilation with LLVM 14 (#3822)

This commit is contained in:
Nicholas Wilson 2021-09-17 22:41:49 +08:00 committed by GitHub
parent 613ed42dca
commit 1568d0cdca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 135 additions and 33 deletions

View file

@ -322,7 +322,7 @@ void outputIR2ObjRelevantCmdlineArgs(llvm::raw_ostream &hash_os) {
#if LDC_LLVM_VER >= 800 #if LDC_LLVM_VER >= 800
const auto framePointerUsage = opts::framePointerUsage(); const auto framePointerUsage = opts::framePointerUsage();
if (framePointerUsage.hasValue()) if (framePointerUsage.hasValue())
hash_os << framePointerUsage.getValue(); hash_os << static_cast<int>(framePointerUsage.getValue());
#else #else
hash_os << opts::disableFPElim(); hash_os << opts::disableFPElim();
#endif #endif

View file

@ -64,8 +64,14 @@ Optional<CodeModel::Model> getCodeModel() {
#endif #endif
} }
#if LDC_LLVM_VER >= 1400
using FPK = llvm::FramePointerKind;
#elif LDC_LLVM_VER >= 800
using FPK = llvm::FramePointer::FP;
#endif
#if LDC_LLVM_VER >= 800 #if LDC_LLVM_VER >= 800
llvm::Optional<llvm::FramePointer::FP> framePointerUsage() { llvm::Optional<FPK> framePointerUsage() {
#if LDC_LLVM_VER >= 1100 #if LDC_LLVM_VER >= 1100
// Defaults to `FP::None`; no way to check if set explicitly by user except // Defaults to `FP::None`; no way to check if set explicitly by user except
// indirectly via setFunctionAttributes()... // indirectly via setFunctionAttributes()...

View file

@ -24,7 +24,9 @@ namespace opts {
std::string getArchStr(); std::string getArchStr();
llvm::Optional<llvm::Reloc::Model> getRelocModel(); llvm::Optional<llvm::Reloc::Model> getRelocModel();
llvm::Optional<llvm::CodeModel::Model> getCodeModel(); llvm::Optional<llvm::CodeModel::Model> getCodeModel();
#if LDC_LLVM_VER >= 800 #if LDC_LLVM_VER >= 1400
llvm::Optional<llvm::FramePointerKind> framePointerUsage();
#elif LDC_LLVM_VER >= 800
llvm::Optional<llvm::FramePointer::FP> framePointerUsage(); llvm::Optional<llvm::FramePointer::FP> framePointerUsage();
#else #else
llvm::cl::boolOrDefault disableFPElim(); llvm::cl::boolOrDefault disableFPElim();

View file

@ -21,6 +21,9 @@
#include "llvm/ADT/StringSwitch.h" #include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SpecialCaseList.h" #include "llvm/Support/SpecialCaseList.h"
#if LDC_LLVM_VER >= 1400
#include "llvm/Support/VirtualFileSystem.h"
#endif
namespace { namespace {

View file

@ -152,20 +152,16 @@ void emitLLVMUsedArray(IRState &irs) {
llvmUsed->setSection("llvm.metadata"); llvmUsed->setSection("llvm.metadata");
} }
void inlineAsmDiagnosticHandler(const llvm::SMDiagnostic &d, void *context, bool inlineAsmDiagnostic(IRState* irs,const llvm::SMDiagnostic &d, unsigned locCookie)
unsigned locCookie) { {
if (d.getKind() == llvm::SourceMgr::DK_Error)
++global.errors;
if (!locCookie) { if (!locCookie) {
d.print(nullptr, llvm::errs()); d.print(nullptr, llvm::errs());
return; return true;
} }
// replace the `<inline asm>` dummy filename by the LOC of the actual D // replace the `<inline asm>` dummy filename by the LOC of the actual D
// expression/statement (`myfile.d(123)`) // expression/statement (`myfile.d(123)`)
const Loc &loc = const Loc &loc = irs->getInlineAsmSrcLoc(locCookie);
static_cast<IRState *>(context)->getInlineAsmSrcLoc(locCookie);
const char *filename = loc.toChars(/*showColumns*/ false); const char *filename = loc.toChars(/*showColumns*/ false);
// keep on using llvm::SMDiagnostic::print() for nice, colorful output // keep on using llvm::SMDiagnostic::print() for nice, colorful output
@ -173,8 +169,34 @@ void inlineAsmDiagnosticHandler(const llvm::SMDiagnostic &d, void *context,
d.getColumnNo(), d.getKind(), d.getMessage(), d.getColumnNo(), d.getKind(), d.getMessage(),
d.getLineContents(), d.getRanges(), d.getFixIts()); d.getLineContents(), d.getRanges(), d.getFixIts());
d2.print(nullptr, llvm::errs()); d2.print(nullptr, llvm::errs());
return true;
} }
#if LDC_LLVM_VER < 1300
void inlineAsmDiagnosticHandler(const llvm::SMDiagnostic &d, void *context,
unsigned locCookie) {
if (d.getKind() == llvm::SourceMgr::DK_Error)
++global.errors;
inlineAsmDiagnostic(static_cast<IRState *>(context), d, locCookie);
}
#else
struct InlineAsmDiagnosticHandler : public llvm::DiagnosticHandler {
IRState *irs;
InlineAsmDiagnosticHandler(IRState *irs) : irs(irs) {}
// return false to defer to LLVMContext::diagnose()
bool handleDiagnostics(const llvm::DiagnosticInfo &DI) override {
if (DI.getKind() != llvm::DK_SrcMgr)
return false;
const auto &DISM = llvm::cast<llvm::DiagnosticInfoSrcMgr>(DI);
if (DISM.getKind() == llvm::SourceMgr::DK_Error)
++global.errors;
return inlineAsmDiagnostic(irs, DISM.getSMDiag(), DISM.getLocCookie());
}
};
#endif
} // anonymous namespace } // anonymous namespace
namespace ldc { namespace ldc {
@ -265,7 +287,12 @@ void CodeGenerator::writeAndFreeLLModule(const char *filename) {
llvm::Metadata *IdentNode[] = {llvm::MDString::get(ir_->context(), Version)}; llvm::Metadata *IdentNode[] = {llvm::MDString::get(ir_->context(), Version)};
IdentMetadata->addOperand(llvm::MDNode::get(ir_->context(), IdentNode)); IdentMetadata->addOperand(llvm::MDNode::get(ir_->context(), IdentNode));
#if LDC_LLVM_VER < 1300
context_.setInlineAsmDiagnosticHandler(inlineAsmDiagnosticHandler, ir_); context_.setInlineAsmDiagnosticHandler(inlineAsmDiagnosticHandler, ir_);
#else
context_.setDiagnosticHandler(
std::make_unique<InlineAsmDiagnosticHandler>(ir_));
#endif
std::unique_ptr<llvm::ToolOutputFile> diagnosticsOutputFile = std::unique_ptr<llvm::ToolOutputFile> diagnosticsOutputFile =
createAndSetDiagnosticsOutputFile(*ir_, context_, filename); createAndSetDiagnosticsOutputFile(*ir_, context_, filename);

View file

@ -715,7 +715,13 @@ void translateArgs(const llvm::SmallVectorImpl<const char *> &ldmdArgs,
} }
} else { } else {
const auto ext = ls::path::extension(p); const auto ext = ls::path::extension(p);
if (ext.equals_lower(".exe")) { if (
#if LDC_LLVM_VER >= 1400
ext.equals_insensitive(".exe")
#else
ext.equals_lower(".exe")
#endif
) {
// should be for Windows targets only // should be for Windows targets only
ldcArgs.push_back(concat("-of=", p)); ldcArgs.push_back(concat("-of=", p));
continue; continue;

View file

@ -36,6 +36,10 @@ void addMscrtLibs(bool useInternalToolchain, std::vector<std::string> &args) {
// We need the vcruntime lib for druntime's exception handling (ldc.eh_msvc). // 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. // Pick one of the 4 variants matching the selected main UCRT lib.
#if LDC_LLVM_VER >= 1400
#define contains_lower contains_insensitive
#define endswith_lower endswith_insensitive
#endif
if (useInternalToolchain) { if (useInternalToolchain) {
assert(mscrtlibName.contains_lower("vcruntime")); assert(mscrtlibName.contains_lower("vcruntime"));
return; return;

View file

@ -11,18 +11,33 @@
#include "gen/irstate.h" #include "gen/irstate.h"
AttrSet::AttrSet(const AttrSet &base, unsigned index, LLAttribute attribute) AttrSet::AttrSet(const AttrSet &base, unsigned index, LLAttribute attribute)
#if LDC_LLVM_VER >= 1400
: set(base.set.addAttributeAtIndex(gIR->context(), index, attribute)) {}
#else
: set(base.set.addAttribute(gIR->context(), index, attribute)) {} : set(base.set.addAttribute(gIR->context(), index, attribute)) {}
#endif
AttrSet AttrSet
AttrSet::extractFunctionAndReturnAttributes(const llvm::Function *function) { AttrSet::extractFunctionAndReturnAttributes(const llvm::Function *function) {
auto old = function->getAttributes(); auto old = function->getAttributes();
return {LLAttributeList::get(gIR->context(), old.getFnAttributes(), return {LLAttributeList::get(gIR->context(),
old.getRetAttributes(), {})}; #if LDC_LLVM_VER >= 1400
old.getFnAttrs(),
old.getRetAttrs(),
#else
old.getFnAttributes(),
old.getRetAttributes(),
#endif
{})};
} }
AttrSet &AttrSet::add(unsigned index, const llvm::AttrBuilder &builder) { AttrSet &AttrSet::add(unsigned index, const llvm::AttrBuilder &builder) {
if (builder.hasAttributes()) { if (builder.hasAttributes()) {
#if LDC_LLVM_VER >= 1400
set = set.addAttributesAtIndex(gIR->context(), index, builder);
#else
set = set.addAttributes(gIR->context(), index, builder); set = set.addAttributes(gIR->context(), index, builder);
#endif
} }
return *this; return *this;
} }

View file

@ -43,6 +43,9 @@ void emitCoverageLinecountInc(const Loc &loc) {
case opts::CoverageIncrement::atomic: case opts::CoverageIncrement::atomic:
// Do an atomic increment, so this works when multiple threads are executed. // Do an atomic increment, so this works when multiple threads are executed.
gIR->ir->CreateAtomicRMW(llvm::AtomicRMWInst::Add, ptr, DtoConstUint(1), gIR->ir->CreateAtomicRMW(llvm::AtomicRMWInst::Add, ptr, DtoConstUint(1),
#if LDC_LLVM_VER >= 1400
LLAlign(4),
#endif
llvm::AtomicOrdering::Monotonic); llvm::AtomicOrdering::Monotonic);
break; break;
case opts::CoverageIncrement::nonatomic: { case opts::CoverageIncrement::nonatomic: {

View file

@ -37,8 +37,13 @@ struct TempDisableDiscardValueNames {
/// Note: don't add function _parameter_ attributes /// Note: don't add function _parameter_ attributes
void copyFnAttributes(llvm::Function *wannabe, llvm::Function *idol) { void copyFnAttributes(llvm::Function *wannabe, llvm::Function *idol) {
auto attrSet = idol->getAttributes(); auto attrSet = idol->getAttributes();
#if LDC_LLVM_VER >= 1400
auto fnAttrSet = attrSet.getFnAttrs();
wannabe->addFnAttrs(fnAttrSet);
#else
auto fnAttrSet = attrSet.getFnAttributes(); auto fnAttrSet = attrSet.getFnAttributes();
wannabe->addAttributes(LLAttributeList::FunctionIndex, fnAttrSet); wannabe->addAttributes(LLAttributeList::FunctionIndex, fnAttrSet);
#endif
} }
llvm::StringRef exprToString(StringExp *strexp) { llvm::StringRef exprToString(StringExp *strexp) {

View file

@ -508,8 +508,12 @@ bool DtoLowerMagicIntrinsic(IRState *p, FuncDeclaration *fndecl, CallExp *e,
fatal(); fatal();
} }
auto ret = p->ir->CreateAtomicCmpXchg(ptr, cmp, val, successOrdering, auto ret =
failureOrdering); p->ir->CreateAtomicCmpXchg(ptr, cmp, val,
#if LDC_LLVM_VER >= 1400
LLMaybeAlign(gDataLayout->getABITypeAlignment(val->getType())),
#endif
successOrdering, failureOrdering);
ret->setWeak(isWeak); ret->setWeak(isWeak);
// we return a struct; allocate on stack and store to both fields manually // we return a struct; allocate on stack and store to both fields manually
@ -554,6 +558,9 @@ bool DtoLowerMagicIntrinsic(IRState *p, FuncDeclaration *fndecl, CallExp *e,
LLValue *val = DtoRVal(exp2); LLValue *val = DtoRVal(exp2);
LLValue *ret = LLValue *ret =
p->ir->CreateAtomicRMW(llvm::AtomicRMWInst::BinOp(op), ptr, val, p->ir->CreateAtomicRMW(llvm::AtomicRMWInst::BinOp(op), ptr, val,
#if LDC_LLVM_VER >= 1400
LLMaybeAlign(gDataLayout->getABITypeAlignment(val->getType())),
#endif
llvm::AtomicOrdering(atomicOrdering)); llvm::AtomicOrdering(atomicOrdering));
result = new DImValue(exp2->type, ret); result = new DImValue(exp2->type, ret);
return true; return true;
@ -1028,9 +1035,15 @@ DValue *DtoCallFunction(const Loc &loc, Type *resulttype, DValue *fnval,
call->setCallingConv(callconv); call->setCallingConv(callconv);
} }
// merge in function attributes set in callOrInvoke // merge in function attributes set in callOrInvoke
#if LDC_LLVM_VER >= 1400
attrlist = attrlist.addFnAttributes(
gIR->context(),
llvm::AttrBuilder(call->getAttributes(), LLAttributeList::FunctionIndex));
#else
attrlist = attrlist.addAttributes( attrlist = attrlist.addAttributes(
gIR->context(), LLAttributeList::FunctionIndex, gIR->context(), LLAttributeList::FunctionIndex,
llvm::AttrBuilder(call->getAttributes(), LLAttributeList::FunctionIndex)); llvm::AttrBuilder(call->getAttributes(), LLAttributeList::FunctionIndex));
#endif
call->setAttributes(attrlist); call->setAttributes(attrlist);
// Special case for struct constructor calls: For temporaries, using the // Special case for struct constructor calls: For temporaries, using the

View file

@ -197,7 +197,11 @@ void applyAttrAllocSize(StructLiteralExp *sle, IrFunction *irFunc) {
llvm::Function *func = irFunc->getLLVMFunc(); llvm::Function *func = irFunc->getLLVMFunc();
#if LDC_LLVM_VER >= 1400
func->addFnAttrs(builder);
#else
func->addAttributes(LLAttributeList::FunctionIndex, builder); func->addAttributes(LLAttributeList::FunctionIndex, builder);
#endif
} }
// @llvmAttr("key", "value") // @llvmAttr("key", "value")
@ -414,7 +418,11 @@ void applyFuncDeclUDAs(FuncDeclaration *decl, IrFunction *irFunc) {
} else if (ident == Id::udaLLVMAttr) { } else if (ident == Id::udaLLVMAttr) {
llvm::AttrBuilder attrs; llvm::AttrBuilder attrs;
applyAttrLLVMAttr(sle, attrs); applyAttrLLVMAttr(sle, attrs);
#if LDC_LLVM_VER >= 1400
func->addFnAttrs(attrs);
#else
func->addAttributes(LLAttributeList::FunctionIndex, attrs); func->addAttributes(LLAttributeList::FunctionIndex, attrs);
#endif
} else if (ident == Id::udaLLVMFastMathFlag) { } else if (ident == Id::udaLLVMFastMathFlag) {
applyAttrLLVMFastMathFlag(sle, irFunc); applyAttrLLVMFastMathFlag(sle, irFunc);
} else if (ident == Id::udaOptStrategy) { } else if (ident == Id::udaOptStrategy) {

View file

@ -29,14 +29,24 @@ IrFunction::IrFunction(FuncDeclaration *fd)
} }
void IrFunction::setNeverInline() { void IrFunction::setNeverInline() {
assert(!func->getAttributes().hasAttribute(LLAttributeList::FunctionIndex, assert(!func->getAttributes()
#if LDC_LLVM_VER < 1400
.hasAttribute(LLAttributeList::FunctionIndex,
#else
.hasFnAttr(
#endif
llvm::Attribute::AlwaysInline) && llvm::Attribute::AlwaysInline) &&
"function can't be never- and always-inline at the same time"); "function can't be never- and always-inline at the same time");
func->addFnAttr(llvm::Attribute::NoInline); func->addFnAttr(llvm::Attribute::NoInline);
} }
void IrFunction::setAlwaysInline() { void IrFunction::setAlwaysInline() {
assert(!func->getAttributes().hasAttribute(LLAttributeList::FunctionIndex, assert(!func->getAttributes()
#if LDC_LLVM_VER < 1400
.hasAttribute(LLAttributeList::FunctionIndex,
#else
.hasFnAttr(
#endif
llvm::Attribute::NoInline) && llvm::Attribute::NoInline) &&
"function can't be never- and always-inline at the same time"); "function can't be never- and always-inline at the same time");
func->addFnAttr(llvm::Attribute::AlwaysInline); func->addFnAttr(llvm::Attribute::AlwaysInline);