Skip to content
4 changes: 4 additions & 0 deletions driver/codegenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ void CodeGenerator::prepareLLModule(Module *m) {
// name, as it should not collide with a symbol name used somewhere in the
// module.
ir_ = new IRState(m->srcfile.toChars(), context_);
#if LDC_LLVM_VER >= 2100
ir_->module.setTargetTriple(*global.params.targetTriple);
#else
ir_->module.setTargetTriple(global.params.targetTriple->str());
#endif
ir_->module.setDataLayout(*gDataLayout);

// TODO: Make ldc::DIBuilder per-Module to be able to emit several CUs for
Expand Down
8 changes: 7 additions & 1 deletion driver/targetmachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,13 @@ createTargetMachine(const std::string targetTriple, const std::string arch,
finalFeaturesString.c_str());
}

return target->createTargetMachine(triple.str(), cpu, finalFeaturesString,
return target->createTargetMachine(
#if LDC_LLVM_VER >= 2100
triple,
#else
triple.str(),
#endif
cpu, finalFeaturesString,
targetOptions, relocModel, codeModel,
static_cast<llvm::CodeGenOptLevel>(codeGenOptLevel));
}
Expand Down
2 changes: 1 addition & 1 deletion gen/abi/aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct AArch64TargetABI : TargetABI {
// compiler magic: pass va_list args implicitly by reference
if (!isReturnVal && isAAPCS64VaList(t)) {
arg.byref = true;
arg.ltype = LLPointerType::getUnqual(arg.ltype);
arg.ltype = LLPointerType::get(getGlobalContext(), 0);
return;
}

Expand Down
4 changes: 4 additions & 0 deletions gen/abi/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,11 @@ struct IndirectByvalRewrite : ABIRewrite {
auto &attrs = arg.attrs;
attrs.clear();
attrs.addAttribute(LLAttribute::NoAlias);
#if LDC_LLVM_VER >= 2100
attrs.addCapturesAttr(llvm::CaptureInfo::none());
#else
attrs.addAttribute(LLAttribute::NoCapture);
#endif
if (auto alignment = DtoAlignment(arg.type))
attrs.addAlignmentAttr(alignment);
}
Expand Down
2 changes: 1 addition & 1 deletion gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ DtoVirtualFunctionPointer(DValue *inst, FuncDeclaration *fdecl) {
// get the vtbl for objects
vtable = DtoGEP(irtc->getMemoryLLType(), vthis, 0u, 0);
// load vtbl ptr
vtable = DtoLoad(LLPointerType::getUnqual(vtblType), vtable);
vtable = DtoLoad(LLPointerType::get(getGlobalContext(), 0), vtable);
// index vtbl
const std::string name = fdecl->toChars();
const auto vtblname = name + "@vtbl";
Expand Down
5 changes: 3 additions & 2 deletions gen/dcompute/druntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "dmd/aggregate.h"
#include "dmd/mtype.h"
#include "gen/dcompute/target.h"
#include "gen/llvmhelpers.h"
#include "gen/irstate.h"
#include "gen/llvm.h"
#include "gen/tollvm.h"
Expand All @@ -37,11 +38,11 @@ struct DcomputePointer {
Type *type;
DcomputePointer(int as, Type *ty) : addrspace(as), type(ty) {}
LLType *toLLVMType(bool translate) {
auto llType = DtoType(type);
DtoType(type);
int as = addrspace;
if (translate)
as = gIR->dcomputetarget->mapping[as];
return LLPointerType::get(llType, as);
return LLPointerType::get(getGlobalContext(), as);
}
};
llvm::Optional<DcomputePointer> toDcomputePointer(StructDeclaration *sd);
4 changes: 4 additions & 0 deletions gen/dcompute/targetCUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ class TargetCUDA : public DComputeTarget {
llvm::Reloc::Static, llvm::CodeModel::Medium, codeGenOptLevel(), false);

_ir = new IRState("dcomputeTargetCUDA", ctx);
#if LDC_LLVM_VER >= 2100
_ir->module.setTargetTriple(llvm::Triple(tripleString));
#else
_ir->module.setTargetTriple(tripleString);
#endif
_ir->module.setDataLayout(targetMachine->createDataLayout());
_ir->dcomputetarget = this;
}
Expand Down
11 changes: 8 additions & 3 deletions gen/dcompute/targetOCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,19 @@ class TargetOCL : public DComputeTarget {
const bool is64 = global.params.targetTriple->isArch64Bit();

_ir = new IRState("dcomputeTargetOCL", ctx);
std::string targTriple = is64 ? SPIR_TARGETTRIPLE64
: SPIR_TARGETTRIPLE32;
std::string targTripleStr = is64 ? SPIR_TARGETTRIPLE64
: SPIR_TARGETTRIPLE32;
#if LDC_LLVM_VER >= 2100
llvm::Triple targTriple = llvm::Triple(targTripleStr);
#else
std::string targTriple = targTripleStr;
#endif
_ir->module.setTargetTriple(targTriple);

#if LDC_LLVM_VER >= 1600
auto floatABI = ::FloatABI::Hard;
targetMachine = createTargetMachine(
targTriple,
targTripleStr,
is64 ? "spirv64" : "spirv32",
"", {},
is64 ? ExplicitBitness::M64 : ExplicitBitness::M32, floatABI,
Expand Down
6 changes: 3 additions & 3 deletions gen/nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ DValue *DtoNestedVariable(Loc loc, Type *astype, VarDeclaration *vd,
IF_LOG Logger::cout() << "Frame index: " << *val << '\n';
currFrame = getIrFunc(fd)->frameType;
gIR->DBuilder.OpDeref(dwarfAddrOps);
val = DtoAlignedLoad(LLPointerType::getUnqual(currFrame), val,
val = DtoAlignedLoad(LLPointerType::get(getGlobalContext(), 0), val,
(std::string(".frame.") + vdparent->toChars()).c_str());
IF_LOG Logger::cout() << "Frame: " << *val << '\n';
}
Expand Down Expand Up @@ -417,7 +417,7 @@ static void DtoCreateNestedContextType(FuncDeclaration *fd) {
builder.addType(innerFrameType->getElementType(i), target.ptrsize);
}
// Add frame pointer type for last frame
builder.addType(LLPointerType::getUnqual(innerFrameType), target.ptrsize);
builder.addType(LLPointerType::get(getGlobalContext(), 0), target.ptrsize);
}

// Add the direct nested variables of this function, and update their
Expand Down Expand Up @@ -505,7 +505,7 @@ void DtoCreateNestedContext(FuncGenState &funcGen) {
mem = gIR->ir->CreateAdd(mem, DtoConstSize_t(mask));
mem = gIR->ir->CreateAnd(mem, DtoConstSize_t(~mask));
frame =
gIR->ir->CreateIntToPtr(mem, LLPointerType::getUnqual(frameType), ".frame");
gIR->ir->CreateIntToPtr(mem, LLPointerType::get(getGlobalContext(), 0), ".frame");
}
} else {
frame = DtoRawAlloca(frameType, frameAlignment, ".frame");
Expand Down
28 changes: 28 additions & 0 deletions gen/passes/GarbageCollect2Stack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSwitch.h"
#if LDC_LLVM_VER >= 2100
#include "llvm/IR/AbstractCallSite.h"
#endif
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
Expand Down Expand Up @@ -350,7 +353,23 @@ static void RemoveCall(CallBase *CB, const G2StackAnalysis &A) {

// Remove the runtime call.
if (A.CGNode) {
#if LDC_LLVM_VER >= 2100
//FIXME: Look into using `LazyCallGraph` and the new pass manager
for (auto I = A.CGNode->begin(); ; I++) {
assert(I != A.CGNode->end() && "Cannot find callsite to remove!");
if (I->first && *I->first == CB) {
A.CGNode->removeCallEdge(I);

// Remove all references to callback functions if there are any.
forEachCallbackFunction(*CB, [=](Function *_CB) {
A.CGNode->removeOneAbstractEdgeTo(A.CG->getOrInsertFunction(_CB));
});
break;
}
}
#else
A.CGNode->removeCallEdgeFor(*CB);
#endif
}
static_cast<Instruction *>(CB)->eraseFromParent();
}
Expand Down Expand Up @@ -726,10 +745,19 @@ bool isSafeToStackAllocate(BasicBlock::iterator Alloc, Value *V,
auto B = CB->arg_begin(), E = CB->arg_end();
for (auto A = B; A != E; ++A) {
if (A->get() == V) {
#if LDC_LLVM_VER >= 2100
if (CB->paramHasAttr(A - B, llvm::Attribute::AttrKind::Captures)) {
return capturesNothing(
CB->getParamAttr(A - B, llvm::Attribute::AttrKind::Captures)
.getCaptureInfo());
}

#else
if (!CB->paramHasAttr(A - B, llvm::Attribute::AttrKind::NoCapture)) {
// The parameter is not marked 'nocapture' - captured.
return false;
}
#endif

if (auto call = dyn_cast<CallInst>(static_cast<Instruction *>(CB))) {
if (call->isTailCall()) {
Expand Down
13 changes: 11 additions & 2 deletions gen/passes/SimplifyDRuntimeCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "gen/passes/Passes.h"
#include "gen/passes/SimplifyDRuntimeCalls.h"
#include "gen/llvmhelpers.h"
#include "gen/tollvm.h"
#include "gen/runtime.h"
#include "llvm/ADT/Statistic.h"
Expand Down Expand Up @@ -183,7 +184,7 @@ Value *ArraySliceCopyOpt::CallOptimizer(Function *Callee, CallInst *CI,
IRBuilder<> &B) {
// Verify we have a reasonable prototype for _d_array_slice_copy
const FunctionType *FT = Callee->getFunctionType();
const llvm::Type *VoidPtrTy = PointerType::getUnqual(B.getInt8Ty());
const llvm::Type *VoidPtrTy = PointerType::get(getGlobalContext(), 0);
if (Callee->arg_size() != 5 || FT->getReturnType() != B.getVoidTy() ||
FT->getParamType(0) != VoidPtrTy ||
!isa<IntegerType>(FT->getParamType(1)) ||
Expand All @@ -208,8 +209,16 @@ Value *ArraySliceCopyOpt::CallOptimizer(Function *Callee, CallInst *CI,
Sz = (Int->getValue() * ElemSz->getValue()).getZExtValue();
}

#if LDC_LLVM_VER >= 2100
llvm::LocationSize Sz2 =
(Sz == llvm::MemoryLocation::UnknownSize)
? llvm::LocationSize::beforeOrAfterPointer()
: llvm::LocationSize::precise(Sz);
#else
std::uint64_t Sz2 = Sz;
#endif
// Check if the pointers may alias
if (AA->alias(CI->getOperand(0), Sz, CI->getOperand(2), Sz)) {
if (AA->alias(CI->getOperand(0), Sz2, CI->getOperand(2), Sz2)) {
return nullptr;
}

Expand Down
101 changes: 66 additions & 35 deletions gen/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,44 +498,75 @@ static void buildRuntimeModule() {
//////////////////////////////////////////////////////////////////////////////

// Construct some attribute lists used below (possibly multiple times)
AttrSet NoAttrs,
Attr_NoUnwind(NoAttrs, LLAttributeList::FunctionIndex,
llvm::Attribute::NoUnwind),
AttrSet NoAttrs, Attr_NoUnwind, Attr_ReadOnly, Attr_ReadOnly_NoUnwind, Attr_Cold, Attr_Cold_NoReturn, Attr_Cold_NoReturn_NoUnwind,
Attr_ReadOnly_1_NoCapture, Attr_ReadOnly_1_3_NoCapture, Attr_ReadOnly_NoUnwind_1_NoCapture,
Attr_ReadOnly_NoUnwind_1_2_NoCapture, Attr_1_NoCapture, Attr_1_2_NoCapture, Attr_1_3_NoCapture,
Attr_1_4_NoCapture;
// `nounwind`
{
auto addNoUnwind = [&](AttrSet& a) {
llvm::AttrBuilder ab(context);
ab.addAttribute(llvm::Attribute::NoUnwind);
a.addToFunction(ab);
};
addNoUnwind(Attr_NoUnwind);
addNoUnwind(Attr_Cold_NoReturn_NoUnwind);
addNoUnwind(Attr_ReadOnly_NoUnwind);
addNoUnwind(Attr_ReadOnly_NoUnwind_1_NoCapture);
addNoUnwind(Attr_ReadOnly_NoUnwind_1_2_NoCapture);
}
// `readonly`
{
auto addReadOnly = [&](AttrSet& a) {
#if LDC_LLVM_VER >= 1600
Attr_ReadOnly(llvm::AttributeList().addFnAttribute(
context, llvm::Attribute::getWithMemoryEffects(
context, llvm::MemoryEffects::readOnly()))),
a = a.merge(AttrSet(llvm::AttributeList().addFnAttribute(
context, llvm::Attribute::getWithMemoryEffects(
context, llvm::MemoryEffects::readOnly()))));
#else
llvm::AttrBuilder ab(context);
ab.addAttribute(llvm::Attribute::ReadOnly);
a = a.addToFunction(ab);
#endif
};
addReadOnly(Attr_ReadOnly);
addReadOnly(Attr_ReadOnly_NoUnwind);
addReadOnly(Attr_ReadOnly_1_NoCapture);
addReadOnly(Attr_ReadOnly_1_3_NoCapture);
addReadOnly(Attr_ReadOnly_NoUnwind_1_NoCapture);
addReadOnly(Attr_ReadOnly_NoUnwind_1_2_NoCapture);
}
// `cold`
{
auto addCold = [&](AttrSet& a) {
llvm::AttrBuilder ab(context);
ab.addAttribute(llvm::Attribute::Cold);
};
addCold(Attr_Cold);
addCold(Attr_Cold_NoReturn);
addCold(Attr_Cold_NoReturn_NoUnwind);
}
// `nocapture`/ `captures(none)`
{
auto addCapturesNone = [&](int extra, AttrSet& a) {
llvm::AttrBuilder ab(context);
#if LDC_LLVM_VER >= 2100
ab.addCapturesAttr(llvm::CaptureInfo::none());
#else
Attr_ReadOnly(NoAttrs, LLAttributeList::FunctionIndex,
llvm::Attribute::ReadOnly),
ab.addAttribute(llvm::Attribute::NoCapture);
#endif
Attr_Cold(NoAttrs, LLAttributeList::FunctionIndex, llvm::Attribute::Cold),
Attr_Cold_NoReturn(Attr_Cold, LLAttributeList::FunctionIndex,
llvm::Attribute::NoReturn),
Attr_Cold_NoReturn_NoUnwind(Attr_Cold_NoReturn,
LLAttributeList::FunctionIndex,
llvm::Attribute::NoUnwind),
Attr_ReadOnly_NoUnwind(Attr_ReadOnly, LLAttributeList::FunctionIndex,
llvm::Attribute::NoUnwind),
Attr_ReadOnly_1_NoCapture(Attr_ReadOnly, LLAttributeList::FirstArgIndex,
llvm::Attribute::NoCapture),
Attr_ReadOnly_1_3_NoCapture(Attr_ReadOnly_1_NoCapture,
LLAttributeList::FirstArgIndex + 2,
llvm::Attribute::NoCapture),
Attr_ReadOnly_NoUnwind_1_NoCapture(Attr_ReadOnly_1_NoCapture,
LLAttributeList::FunctionIndex,
llvm::Attribute::NoUnwind),
Attr_ReadOnly_NoUnwind_1_2_NoCapture(Attr_ReadOnly_NoUnwind_1_NoCapture,
LLAttributeList::FirstArgIndex + 1,
llvm::Attribute::NoCapture),
Attr_1_NoCapture(NoAttrs, LLAttributeList::FirstArgIndex,
llvm::Attribute::NoCapture),
Attr_1_2_NoCapture(Attr_1_NoCapture, LLAttributeList::FirstArgIndex + 1,
llvm::Attribute::NoCapture),
Attr_1_3_NoCapture(Attr_1_NoCapture, LLAttributeList::FirstArgIndex + 2,
llvm::Attribute::NoCapture),
Attr_1_4_NoCapture(Attr_1_NoCapture, LLAttributeList::FirstArgIndex + 3,
llvm::Attribute::NoCapture);
a.addToParam(0, ab);
if (extra)
a.addToParam(extra-1, ab);
};
addCapturesNone(0, Attr_ReadOnly_1_NoCapture);
addCapturesNone(3, Attr_ReadOnly_1_3_NoCapture);
addCapturesNone(0, Attr_ReadOnly_NoUnwind_1_NoCapture);
addCapturesNone(2, Attr_ReadOnly_NoUnwind_1_2_NoCapture);
addCapturesNone(0, Attr_1_NoCapture);
addCapturesNone(2, Attr_1_2_NoCapture);
addCapturesNone(3, Attr_1_3_NoCapture);
addCapturesNone(4, Attr_1_4_NoCapture);
}

//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Expand Down
6 changes: 5 additions & 1 deletion gen/tocall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,11 @@ DValue *DtoCallFunction(Loc loc, Type *resulttype, DValue *fnval,
call->setCallingConv(cf->getCallingConv());
if (cf->isIntrinsic()) { // override intrinsic attrs
attrlist =
llvm::Intrinsic::getAttributes(gIR->context(), cf->getIntrinsicID());
llvm::Intrinsic::getAttributes(gIR->context(), cf->getIntrinsicID()
#if LDC_LLVM_VER >= 2100
,cf->getFunctionType()
#endif
);
}
} else if (dfnval) {
call->setCallingConv(getCallingConvention(dfnval->func));
Expand Down
2 changes: 1 addition & 1 deletion gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2826,7 +2826,7 @@ class ToElemVisitor : public Visitor {
LLValue *val = DtoRVal(ex);

// Get and load vtbl pointer.
llvm::Value *vtbl = DtoLoad(LLPointerType::getUnqual(vtblType),
llvm::Value *vtbl = DtoLoad(LLPointerType::get(getGlobalContext(), 0),
DtoGEP(irtc->getMemoryLLType(), val, 0u, 0));

// TypeInfo ptr is first vtbl entry.
Expand Down
16 changes: 13 additions & 3 deletions gen/variable_lifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static llvm::cl::opt<bool> fEmitLocalVarLifetime(
LocalVariableLifetimeAnnotator::LocalVariableLifetimeAnnotator(IRState &irs)
: irs(irs) {
allocaType =
LLPointerType::get(LLType::getInt8Ty(irs.context()),
LLPointerType::get(irs.context(),
irs.module.getDataLayout().getAllocaAddrSpace());
}

Expand Down Expand Up @@ -78,7 +78,12 @@ llvm::Function *LocalVariableLifetimeAnnotator::getLLVMLifetimeStartFn() {
if (lifetimeStartFunction)
return lifetimeStartFunction;

lifetimeStartFunction = llvm::Intrinsic::getDeclaration(
lifetimeStartFunction = llvm::Intrinsic::
#if LDC_LLVM_VER >= 2100
getOrInsertDeclaration(
#else
getDeclaration(
#endif
&irs.module, llvm::Intrinsic::lifetime_start, allocaType);
assert(lifetimeStartFunction);
return lifetimeStartFunction;
Expand All @@ -89,7 +94,12 @@ llvm::Function *LocalVariableLifetimeAnnotator::getLLVMLifetimeEndFn() {
if (lifetimeEndFunction)
return lifetimeEndFunction;

lifetimeEndFunction = llvm::Intrinsic::getDeclaration(
lifetimeEndFunction = llvm::Intrinsic::
#if LDC_LLVM_VER >= 2100
getOrInsertDeclaration(
#else
getDeclaration(
#endif
&irs.module, llvm::Intrinsic::lifetime_end, allocaType);
assert(lifetimeEndFunction);
return lifetimeEndFunction;
Expand Down
Loading