Skip to content

Commit

Permalink
Fix issue ldc-developers#3496 - missing IR declarations for some fwd-…
Browse files Browse the repository at this point in the history
…declared functions

Whenever we need an IR function, we'd better make sure it exists. Handle
that in DtoCallee(), by invoking DtoDeclareFunction() by default,
instead of the previous DtoResolveFunction() + DtoCallee() combo.
DtoResolveFunction() usually declares the function, but somehow doesn't
for abstract and body-less functions.
  • Loading branch information
kinke committed Jul 10, 2020
1 parent 0d09a17 commit 83529ad
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 35 deletions.
2 changes: 0 additions & 2 deletions gen/classes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ DValue *DtoNewClass(Loc &loc, TypeClass *tc, NewExp *newexp) {
}
// custom allocator
else if (newexp->allocator) {
DtoResolveFunction(newexp->allocator);
DFuncValue dfn(newexp->allocator, DtoCallee(newexp->allocator));
DValue *res = DtoCallFunction(newexp->loc, nullptr, &dfn, newexp->newargs);
mem = DtoBitCast(DtoRVal(res), DtoType(tc), ".newclass_custom");
Expand Down Expand Up @@ -141,7 +140,6 @@ DValue *DtoNewClass(Loc &loc, TypeClass *tc, NewExp *newexp) {

Logger::println("Calling constructor");
assert(newexp->arguments != NULL);
DtoResolveFunction(newexp->member);
DFuncValue dfn(newexp->member, DtoCallee(newexp->member), mem);
// ignore ctor return value (C++ ctors on Posix may not return `this`)
DtoCallFunction(newexp->loc, tc, &dfn, newexp->arguments);
Expand Down
7 changes: 4 additions & 3 deletions gen/dibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1175,11 +1175,12 @@ void DIBuilder::EmitFuncEnd(FuncDeclaration *fd) {
Logger::println("D to dwarf funcend");
LOG_SCOPE;

assert(static_cast<llvm::MDNode *>(getIrFunc(fd)->diSubprogram) != 0);
auto irFunc = getIrFunc(fd);

assert(static_cast<llvm::MDNode *>(irFunc->diSubprogram) != 0);
EmitStopPoint(fd->endloc);

// Only attach subprogram entries to function definitions
DtoFunction(fd)->setSubprogram(getIrFunc(fd)->diSubprogram);
irFunc->getLLVMFunc()->setSubprogram(irFunc->diSubprogram);
}

void DIBuilder::EmitBlockStart(Loc &loc) {
Expand Down
5 changes: 1 addition & 4 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,6 @@ void callPostblit(Loc &loc, Expression *exp, LLValue *val) {
fd->toParent()->error(
loc, "is not copyable because it is annotated with `@disable`");
}
DtoResolveFunction(fd);
Expressions args;
DFuncValue dfn(fd, DtoCallee(fd), val);
DtoCallFunction(loc, Type::basic[Tvoid], &dfn, &args);
Expand Down Expand Up @@ -1621,9 +1620,8 @@ DValue *DtoSymbolAddress(Loc &loc, Type *type, Declaration *decl) {
// We need to codegen the function here, because literals are not added
// to the module member list.
DtoDefineFunction(flitdecl);
assert(DtoCallee(flitdecl));

return new DFuncValue(flitdecl, DtoCallee(flitdecl));
return new DFuncValue(flitdecl, DtoCallee(flitdecl, false));
}

if (FuncDeclaration *fdecl = decl->isFuncDeclaration()) {
Expand Down Expand Up @@ -1700,7 +1698,6 @@ llvm::Constant *DtoConstSymbolAddress(Loc &loc, Declaration *decl) {
}
// static function
if (FuncDeclaration *fd = decl->isFuncDeclaration()) {
DtoResolveFunction(fd);
return DtoCallee(fd);
}

Expand Down
1 change: 0 additions & 1 deletion gen/rttibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ void RTTIBuilder::push_size_as_vp(uint64_t s) {

void RTTIBuilder::push_funcptr(FuncDeclaration *fd, Type *castto) {
if (fd) {
DtoResolveFunction(fd);
LLConstant *F = DtoCallee(fd);
if (castto) {
F = DtoBitCast(F, DtoType(castto));
Expand Down
2 changes: 1 addition & 1 deletion gen/toconstelem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ class ToConstElemVisitor : public Visitor {
// added to the module member list.
Declaration_codegen(fd, p);

result = DtoCallee(fd);
result = DtoCallee(fd, false);
assert(result);

if (fd->tok != TOKfunction) {
Expand Down
16 changes: 5 additions & 11 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,6 @@ class ToElemVisitor : public Visitor {
assert(dve);
FuncDeclaration *fdecl = dve->var->isFuncDeclaration();
assert(fdecl);
DtoDeclareFunction(fdecl);
Expression *thisExp = dve->e1;
LLValue *thisArg = thisExp->type->toBasetype()->ty == Tclass
? DtoRVal(thisExp)
Expand Down Expand Up @@ -890,7 +889,6 @@ class ToElemVisitor : public Visitor {
// Logger::println("FuncDeclaration");
FuncDeclaration *fd = fv->func;
assert(fd);
DtoResolveFunction(fd);
result = new DFuncValue(fd, DtoCallee(fd));
return;
}
Expand Down Expand Up @@ -984,8 +982,6 @@ class ToElemVisitor : public Visitor {
// Logger::cout() << "mem: " << *arrptr << '\n';
result = new DLValue(e->type, DtoBitCast(arrptr, DtoPtrToType(e->type)));
} else if (FuncDeclaration *fdecl = e->var->isFuncDeclaration()) {
DtoResolveFunction(fdecl);

// This is a bit more convoluted than it would need to be, because it
// has to take templated interface methods into account, for which
// isFinalFunc is not necessarily true.
Expand All @@ -998,6 +994,7 @@ class ToElemVisitor : public Visitor {
// Get the actual function value to call.
LLValue *funcval = nullptr;
if (nonFinal) {
DtoResolveFunction(fdecl);
funcval = DtoVirtualFunctionPointer(l, fdecl, e->toChars());
} else {
funcval = DtoCallee(fdecl);
Expand Down Expand Up @@ -1507,7 +1504,6 @@ class ToElemVisitor : public Visitor {
LLValue *mem = nullptr;
if (e->allocator) {
// custom allocator
DtoResolveFunction(e->allocator);
DFuncValue dfn(e->allocator, DtoCallee(e->allocator));
DValue *res = DtoCallFunction(e->loc, nullptr, &dfn, e->newargs);
mem = DtoBitCast(DtoRVal(res), DtoType(ntype->pointerTo()),
Expand Down Expand Up @@ -1536,7 +1532,6 @@ class ToElemVisitor : public Visitor {

IF_LOG Logger::println("Calling constructor");
assert(e->arguments != NULL);
DtoResolveFunction(e->member);
DFuncValue dfn(e->member, DtoCallee(e->member), mem);
DtoCallFunction(e->loc, ts, &dfn, e->arguments);
}
Expand Down Expand Up @@ -1734,7 +1729,6 @@ class ToElemVisitor : public Visitor {

Logger::print("calling struct invariant");

DtoResolveFunction(invDecl);
DFuncValue invFunc(invDecl, DtoCallee(invDecl), DtoRVal(cond));
DtoCallFunction(e->loc, nullptr, &invFunc, nullptr);
}
Expand Down Expand Up @@ -2174,7 +2168,7 @@ class ToElemVisitor : public Visitor {
DtoDeclareFunction(fd);
assert(!fd->isNested());
}
assert(DtoCallee(fd));
assert(DtoCallee(fd, false));
}

//////////////////////////////////////////////////////////////////////////////
Expand All @@ -2188,6 +2182,7 @@ class ToElemVisitor : public Visitor {
assert(fd);

genFuncLiteral(fd, e);
LLFunction *callee = DtoCallee(fd, false);

if (fd->isNested()) {
LLType *dgty = DtoType(e->type);
Expand Down Expand Up @@ -2217,12 +2212,12 @@ class ToElemVisitor : public Visitor {
}
cval = DtoBitCast(cval, dgty->getContainedType(0));

LLValue *castfptr = DtoBitCast(DtoCallee(fd), dgty->getContainedType(1));
LLValue *castfptr = DtoBitCast(callee, dgty->getContainedType(1));

result = new DImValue(e->type, DtoAggrPair(cval, castfptr, ".func"));

} else {
result = new DFuncValue(e->type, fd, DtoCallee(fd));
result = new DFuncValue(e->type, fd, callee);
}
}

Expand Down Expand Up @@ -2862,7 +2857,6 @@ bool toInPlaceConstruction(DLValue *lhs, Expression *rhs) {
auto lval = DtoLVal(lhs);
ToElemVisitor::emitStructLiteral(sle, lval);
// ... and invoke the ctor directly on it
DtoDeclareFunction(fd);
auto fnval = new DFuncValue(fd, DtoCallee(fd), lval);
DtoCallFunction(ce->loc, ce->type, fnval, ce->arguments);
return true;
Expand Down
2 changes: 0 additions & 2 deletions ir/irclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ LLConstant *IrClass::getVtblInit() {
}
}

DtoResolveFunction(fd);
assert(isIrFuncCreated(fd) && "invalid vtbl function");
c = DtoBitCast(DtoCallee(fd), voidPtrType);

if (cd->isFuncHidden(fd) && !fd->isFuture()) {
Expand Down
11 changes: 5 additions & 6 deletions ir/irfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "ir/irfunction.h"

#include "driver/cl_options.h"
#include "gen/functions.h"
#include "gen/llvm.h"
#include "gen/llvmhelpers.h"
#include "gen/irstate.h"
Expand Down Expand Up @@ -102,12 +103,10 @@ bool isIrFuncCreated(FuncDeclaration *decl) {
return t == IrDsymbol::FuncType;
}

llvm::Function *DtoFunction(FuncDeclaration *decl, bool create) {
assert(decl != nullptr);
return getIrFunc(decl, create)->getLLVMFunc();
}

llvm::Function *DtoCallee(FuncDeclaration *decl, bool create) {
assert(decl != nullptr);
return getIrFunc(decl, create)->getLLVMCallee();
if (create) {
DtoDeclareFunction(decl);
}
return getIrFunc(decl)->getLLVMCallee();
}
6 changes: 1 addition & 5 deletions ir/irfunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,6 @@ struct IrFunction {
IrFunction *getIrFunc(FuncDeclaration *decl, bool create = false);
bool isIrFuncCreated(FuncDeclaration *decl);

/// Returns the associated LLVM function.
/// Use DtoCallee() for the LLVM function to be used for calls.
llvm::Function *DtoFunction(FuncDeclaration *decl, bool create = false);

/// Returns the associated LLVM function to be used for calls (potentially
/// some sort of wrapper, e.g., a JIT wrapper).
llvm::Function *DtoCallee(FuncDeclaration *decl, bool create = false);
llvm::Function *DtoCallee(FuncDeclaration *decl, bool create = true);
13 changes: 13 additions & 0 deletions tests/compilable/gh3496.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %ldc -c %s

interface I
{
static void staticFoo();
final void finalFoo();
}

void foo()
{
I.staticFoo();
(new class I{}).finalFoo();
}

0 comments on commit 83529ad

Please sign in to comment.