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

Preliminary LLVM 20 support #4843

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions driver/cl_options_sanitizers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
#pragma once

#include "driver/cl_helpers.h"
#if LDC_LLVM_VER >= 2000
#include "llvm/Transforms/Utils/Instrumentation.h"
#else
#include "llvm/Transforms/Instrumentation.h"
#endif

class FuncDeclaration;
namespace llvm {
Expand Down
2 changes: 1 addition & 1 deletion gen/abi/aarch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct AArch64TargetABI : TargetABI {
// compiler magic: pass va_list args implicitly by reference
if (!isReturnVal && isAAPCS64VaList(t)) {
arg.byref = true;
arg.ltype = arg.ltype->getPointerTo();
arg.ltype = LLPointerType::getUnqual(arg.ltype);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ DValue *DtoDynamicCastObject(const Loc &loc, DValue *val, Type *_to) {
// Object _d_dynamic_cast(Object o, ClassInfo c)
llvm::Function *func =
getRuntimeFunction(loc, gIR->module, "_d_dynamic_cast");
LLFunctionType *funcTy = func->getFunctionType();
[[maybe_unused]] LLFunctionType *funcTy = func->getFunctionType();

// Object o
LLValue *obj = DtoRVal(val);
Expand Down Expand Up @@ -480,7 +480,7 @@ DtoVirtualFunctionPointer(DValue *inst, FuncDeclaration *fdecl) {
// get the vtbl for objects
vtable = DtoGEP(irtc->getMemoryLLType(), vthis, 0u, 0);
// load vtbl ptr
vtable = DtoLoad(vtblType->getPointerTo(), vtable);
vtable = DtoLoad(LLPointerType::getUnqual(vtblType), vtable);
// index vtbl
const std::string name = fdecl->toChars();
const auto vtblname = name + "@vtbl";
Expand Down
2 changes: 1 addition & 1 deletion gen/dcompute/druntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct DcomputePointer {
int as = addrspace;
if (translate)
as = gIR->dcomputetarget->mapping[as];
return llType->getPointerTo(as);
return LLPointerType::get(llType, as);
}
};
llvm::Optional<DcomputePointer> toDcomputePointer(StructDeclaration *sd);
6 changes: 5 additions & 1 deletion gen/dibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,11 @@ DISubroutineType DIBuilder::CreateFunctionType(Type *type,
if (pointeeType) {
DIType ditype = DBuilder.createReferenceType(
llvm::dwarf::DW_TAG_pointer_type, pointeeType, target.ptrsize * 8);
ditype = DBuilder.createObjectPointerType(ditype);
ditype = DBuilder.createObjectPointerType(ditype
#if LDC_LLVM_VER >= 2000
, /* Implicit */ true
#endif
);
params.emplace_back(ditype);
}
}
Expand Down
7 changes: 4 additions & 3 deletions gen/dynamiccompile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ llvm::GlobalVariable *generateModuleListElem(IRState *irs, const Types &types,
auto *modListElem = new llvm::GlobalVariable(
irs->module, elem_type, false, llvm::GlobalValue::PrivateLinkage, init,
".rtcompile_modlist_elem");
modListElem->setAlignment(irs->module.getDataLayout().getABITypeAlign(elem_type->getPointerTo()));
modListElem->setAlignment(irs->module.getDataLayout().getABITypeAlign(LLPointerType::getUnqual(elem_type)));

return modListElem;
}
Expand Down Expand Up @@ -687,7 +687,8 @@ void generateCtorBody(IRState *irs, const Types &types, llvm::Function *func,
auto modListHeadPtr = declareModListHead(irs->module, types);
llvm::Value *gepVals[] = {zero64, elemIndex};
auto elemNextPtr = builder.CreateGEP(types.modListElemType, modListElem, gepVals);
auto prevHeadVal = builder.CreateLoad(types.modListElemType->getPointerTo()->getPointerTo(), modListHeadPtr);
auto modListElemPtr = LLPointerType::getUnqual(types.modListElemType);
auto prevHeadVal = builder.CreateLoad(LLPointerType::getUnqual(modListElemPtr), modListHeadPtr);
auto voidPtr = builder.CreateBitOrPointerCast(
modListElem, getI8PtrType(irs->context()));
builder.CreateStore(voidPtr, modListHeadPtr);
Expand Down Expand Up @@ -835,7 +836,7 @@ void defineDynamicCompiledFunction(IRState *irs, IrFunction *func) {
auto srcFunc = func->getLLVMFunc();
auto it = irs->dynamicCompiledFunctions.find(srcFunc);
assert(irs->dynamicCompiledFunctions.end() != it);
auto thunkVarType = srcFunc->getFunctionType()->getPointerTo();
auto thunkVarType = LLPointerType::getUnqual(srcFunc->getFunctionType());
auto thunkVar = new llvm::GlobalVariable(
irs->module, thunkVarType, false, llvm::GlobalValue::PrivateLinkage,
llvm::ConstantPointerNull::get(thunkVarType),
Expand Down
5 changes: 4 additions & 1 deletion gen/llvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ using llvm::APFloat;
using llvm::APInt;
using llvm::IRBuilder;

#if LDC_LLVM_VER >= 1900
#if LDC_LLVM_VER >= 2000
#define GET_INTRINSIC_DECL(_X, _TY) \
(llvm::Intrinsic::getOrInsertDeclaration(&gIR->module, llvm::Intrinsic::_X, _TY))
#elif LDC_LLVM_VER >= 1900
#define GET_INTRINSIC_DECL(_X, _TY) \
(llvm::Intrinsic::getDeclaration(&gIR->module, llvm::Intrinsic::_X, _TY))
#else
Expand Down
4 changes: 2 additions & 2 deletions gen/nested.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ DValue *DtoNestedVariable(const Loc &loc, Type *astype, VarDeclaration *vd,
IF_LOG Logger::cout() << "Frame index: " << *val << '\n';
currFrame = getIrFunc(fd)->frameType;
gIR->DBuilder.OpDeref(dwarfAddrOps);
val = DtoAlignedLoad(currFrame->getPointerTo(), val,
val = DtoAlignedLoad(LLPointerType::getUnqual(currFrame), val,
(std::string(".frame.") + vdparent->toChars()).c_str());
IF_LOG Logger::cout() << "Frame: " << *val << '\n';
}
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, frameType->getPointerTo(), ".frame");
gIR->ir->CreateIntToPtr(mem, LLPointerType::getUnqual(frameType), ".frame");
}
} else {
frame = DtoRawAlloca(frameType, frameAlignment, ".frame");
Expand Down
80 changes: 64 additions & 16 deletions gen/optimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@
#include "llvm/LinkAllPasses.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Target/TargetMachine.h"
#if LDC_LLVM_VER >= 2000
#include "llvm/Transforms/Utils/Instrumentation.h"
#else
#include "llvm/Transforms/Instrumentation.h"
#endif
#include "llvm/Transforms/IPO.h"
#if LDC_LLVM_VER < 1700
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Expand Down Expand Up @@ -207,7 +211,12 @@ static OptimizationLevel getOptimizationLevel(){

#ifndef IN_JITRT
static void addAddressSanitizerPasses(ModulePassManager &mpm,
OptimizationLevel level ) {
OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
AddressSanitizerOptions aso;
aso.CompileKernel = false;
aso.Recover = opts::isSanitizerRecoveryEnabled(opts::AddressSanitizer);
Expand Down Expand Up @@ -250,13 +259,23 @@ static void addMemorySanitizerPass(ModulePassManager &mpm,
}
}
static void addThreadSanitizerPass(ModulePassManager &mpm,
OptimizationLevel level ) {
OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
mpm.addPass(ModuleThreadSanitizerPass());
mpm.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
}

static void addSanitizerCoveragePass(ModulePassManager &mpm,
OptimizationLevel level ) {
OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
#if LDC_LLVM_VER >= 1600
mpm.addPass(SanitizerCoveragePass(
opts::getSanitizerCoverageOptions()));
Expand All @@ -266,8 +285,7 @@ static void addSanitizerCoveragePass(ModulePassManager &mpm,
#endif
}
// Adds PGO instrumentation generation and use passes.
static void addPGOPasses(ModulePassManager &mpm,
OptimizationLevel level ) {
static void addPGOPasses(ModulePassManager &mpm, OptimizationLevel level) {
if (opts::isInstrumentingForASTBasedPGO()) {
InstrProfOptions options;
options.NoRedZone = global.params.disableRedZone;
Expand All @@ -291,8 +309,12 @@ static void addPGOPasses(ModulePassManager &mpm,
#endif // !IN_JITRT

static void addStripExternalsPass(ModulePassManager &mpm,
OptimizationLevel level ) {

OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
if (level == OptimizationLevel::O1 || level == OptimizationLevel::O2 ||
level == OptimizationLevel::O3) {
mpm.addPass(StripExternalsPass());
Expand All @@ -304,7 +326,12 @@ static void addStripExternalsPass(ModulePassManager &mpm,
}

static void addSimplifyDRuntimeCallsPass(ModulePassManager &mpm,
OptimizationLevel level ) {
OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
if (level == OptimizationLevel::O2 || level == OptimizationLevel::O3) {
mpm.addPass(createModuleToFunctionPassAdaptor(SimplifyDRuntimeCallsPass()));
if (verifyEach) {
Expand All @@ -314,7 +341,12 @@ static void addSimplifyDRuntimeCallsPass(ModulePassManager &mpm,
}

static void addGarbageCollect2StackPass(ModulePassManager &mpm,
OptimizationLevel level ) {
OptimizationLevel level
#if LDC_LLVM_VER >= 2000
,
ThinOrFullLTOPhase
#endif
) {
if (level == OptimizationLevel::O2 || level == OptimizationLevel::O3) {
mpm.addPass(createModuleToFunctionPassAdaptor(GarbageCollect2StackPass()));
if (verifyEach) {
Expand Down Expand Up @@ -457,10 +489,11 @@ void runOptimizationPasses(llvm::Module *M, llvm::TargetMachine *TM) {
ModulePassManager mpm;

if (!noVerify) {
pb.registerPipelineStartEPCallback([&](ModulePassManager &mpm,
OptimizationLevel level) {
mpm.addPass(VerifierPass());
});
pb.registerPipelineStartEPCallback(
[&](ModulePassManager &mpm, OptimizationLevel level,
ThinOrFullLTOPhase phase = ThinOrFullLTOPhase::None) {
mpm.addPass(VerifierPass());
});
}

// TODO: port over strip-debuginfos pass for -strip-debug
Expand All @@ -474,7 +507,8 @@ void runOptimizationPasses(llvm::Module *M, llvm::TargetMachine *TM) {

if (opts::isSanitizerEnabled(opts::MemorySanitizer)) {
pb.registerOptimizerLastEPCallback(
[&](ModulePassManager &mpm, OptimizationLevel level) {
[&](ModulePassManager &mpm, OptimizationLevel level,
ThinOrFullLTOPhase phase = ThinOrFullLTOPhase::None) {
FunctionPassManager fpm;
addMemorySanitizerPass(mpm, fpm, level);
mpm.addPass(createModuleToFunctionPassAdaptor(std::move(fpm)));
Expand Down Expand Up @@ -523,9 +557,23 @@ void runOptimizationPasses(llvm::Module *M, llvm::TargetMachine *TM) {

if (optLevelVal == 0) {
#ifdef IN_JITRT
mpm = pb.buildO0DefaultPipeline(level, false);
#if LDC_LLVM_VER >= 2000
const ThinOrFullLTOPhase ltoPrelink = ThinOrFullLTOPhase::None;
#else
const bool ltoPrelink = false;
#endif // LDC_LLVM_VER >= 2000
mpm = pb.buildO0DefaultPipeline(level, ltoPrelink);
#else
#if LDC_LLVM_VER >= 2000
const ThinOrFullLTOPhase ltoPrelink =
opts::isUsingLTO()
? (opts::isUsingThinLTO() ? ThinOrFullLTOPhase::ThinLTOPreLink
: ThinOrFullLTOPhase::FullLTOPreLink)
: ThinOrFullLTOPhase::None;
#else
mpm = pb.buildO0DefaultPipeline(level, opts::isUsingLTO());
const bool ltoPrelink = opts::isUsingLTO();
#endif // LDC_LLVM_VER >= 2000
mpm = pb.buildO0DefaultPipeline(level, ltoPrelink);
#if LDC_LLVM_VER >= 1700
} else if (opts::ltoFatObjects && opts::isUsingLTO()) {
mpm = pb.buildFatLTODefaultPipeline(level,
Expand Down
2 changes: 1 addition & 1 deletion gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2814,7 +2814,7 @@ class ToElemVisitor : public Visitor {
LLValue *val = DtoRVal(ex);

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

// TypeInfo ptr is first vtbl entry.
Expand Down
5 changes: 3 additions & 2 deletions gen/variable_lifetime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "driver/cl_options.h"
#include "gen/irstate.h"
#include "gen/llvm.h"

#include <vector>
#include <utility>
Expand All @@ -30,8 +31,8 @@ static llvm::cl::opt<bool> fEmitLocalVarLifetime(
LocalVariableLifetimeAnnotator::LocalVariableLifetimeAnnotator(IRState &irs)
: irs(irs) {
allocaType =
llvm::Type::getInt8Ty(irs.context())
->getPointerTo(irs.module.getDataLayout().getAllocaAddrSpace());
LLPointerType::get(LLType::getInt8Ty(irs.context()),
irs.module.getDataLayout().getAllocaAddrSpace());
}

void LocalVariableLifetimeAnnotator::pushScope() { scopes.emplace_back(); }
Expand Down
2 changes: 1 addition & 1 deletion ir/irtypefunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ IrTypeDelegate *IrTypeDelegate::get(Type *t) {
IrFuncTy irFty(tf);
llvm::Type *ltf =
DtoFunctionType(tf, irFty, nullptr, pointerTo(Type::tvoid));
llvm::Type *fptr = ltf->getPointerTo(gDataLayout->getProgramAddressSpace());
llvm::Type *fptr = LLPointerType::get(ltf, gDataLayout->getProgramAddressSpace());
llvm::Type *types[] = {getOpaquePtrType(), fptr};
LLStructType *lt = LLStructType::get(gIR->context(), types, false);

Expand Down
2 changes: 1 addition & 1 deletion ir/irtypestruct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ IrTypeStruct *IrTypeStruct::get(StructDeclaration *sd) {
int realAS = gIR->dcomputetarget->mapping[p->addrspace];

llvm::SmallVector<LLType *, 1> body;
body.push_back(DtoMemType(p->type)->getPointerTo(realAS));
body.push_back(LLPointerType::get(DtoMemType(p->type), realAS));

isaStruct(t->type)->setBody(body, false);
VarGEPIndices v;
Expand Down
3 changes: 3 additions & 0 deletions runtime/jit-rt/cpp-so/jit_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

#include "llvm/ADT/StringExtras.h"
#include "llvm/ExecutionEngine/JITLink/EHFrameSupport.h"
#if LDC_LLVM_VER >= 2000 && defined(LDC_JITRT_USE_JITLINK)
#include "llvm/ExecutionEngine/Orc/EHFrameRegistrationPlugin.h"
#endif
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
#include "llvm/ExecutionEngine/Orc/ObjectTransformLayer.h"
#include "llvm/IR/LLVMContext.h"
Expand Down
20 changes: 13 additions & 7 deletions utils/gen_gccbuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,15 @@ using namespace llvm;

#define BUILTIN_NAME_STRING "ClangBuiltinName"

string dtype(Record* rec, bool readOnlyMem)
#if LDC_LLVM_VER >= 2000
#define LLVM_20_CONST const
#else
#define LLVM_20_CONST
#endif

string dtype(LLVM_20_CONST Record* rec, bool readOnlyMem)
{
Init* typeInit = rec->getValueInit("VT");
LLVM_20_CONST Init* typeInit = rec->getValueInit("VT");
if(!typeInit)
return "";

Expand Down Expand Up @@ -72,7 +78,7 @@ string dtype(Record* rec, bool readOnlyMem)
return "";
}

StringRef attributes(ListInit* propertyList)
StringRef attributes(LLVM_20_CONST ListInit* propertyList)
{
const auto prop = propertyList->size()
? propertyList->getElementAsRecord(0)->getName()
Expand Down Expand Up @@ -100,13 +106,13 @@ void processRecord(raw_ostream& os, Record& rec, string arch)
replace(name.begin(), name.end(), '_', '.');
name = string("llvm.") + name;

ListInit* propsList = rec.getValueAsListInit("IntrProperties");
LLVM_20_CONST ListInit* propsList = rec.getValueAsListInit("IntrProperties");
const StringRef prop =
propsList->size() ? propsList->getElementAsRecord(0)->getName() : "";

bool readOnlyMem = prop == "IntrReadArgMem" || prop == "IntrReadMem";

ListInit* paramsList = rec.getValueAsListInit("ParamTypes");
LLVM_20_CONST ListInit* paramsList = rec.getValueAsListInit("ParamTypes");
vector<string> params;
for(unsigned int i = 0; i < paramsList->size(); i++)
{
Expand All @@ -117,7 +123,7 @@ void processRecord(raw_ostream& os, Record& rec, string arch)
params.push_back(t);
}

ListInit* retList = rec.getValueAsListInit("RetTypes");
LLVM_20_CONST ListInit* retList = rec.getValueAsListInit("RetTypes");
string ret;
size_t sz = retList->size();
if(sz == 0)
Expand Down Expand Up @@ -145,7 +151,7 @@ void processRecord(raw_ostream& os, Record& rec, string arch)

std::string arch;

bool emit(raw_ostream& os, RecordKeeper& records)
bool emit(raw_ostream& os, LLVM_20_CONST RecordKeeper& records)
{
os << "module ldc.gccbuiltins_";
os << arch;
Expand Down
Loading