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

Emit struct TypeInfos in referencing compilation units only #3491

Merged
merged 3 commits into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dmd/aggregate.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ class StructDeclaration : public AggregateDeclaration
bool hasIdentityEquals; // true if has identity opEquals
bool hasNoFields; // has no fields
bool hasCopyCtor; // copy constructor
#if !IN_LLVM
// Even if struct is defined as non-root symbol, some built-in operations
// (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
// For those, today TypeInfo_Struct is generated in COMDAT.
bool requestTypeInfo;
#endif

FuncDeclarations postblits; // Array of postblit functions
FuncDeclaration *postblit; // aggregate postblit
Expand Down
9 changes: 9 additions & 0 deletions dmd/dstruct.d
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ extern (C++) void semanticTypeInfo(Scope* sc, Type t)
Scope scx;
scx._module = sd.getModule();
getTypeInfoType(sd.loc, t, &scx);
version (IN_LLVM) {} else
{
sd.requestTypeInfo = true;
}
}
else if (!sc.minst)
{
Expand All @@ -114,7 +117,10 @@ extern (C++) void semanticTypeInfo(Scope* sc, Type t)
else
{
getTypeInfoType(sd.loc, t, sc);
version (IN_LLVM) {} else
{
sd.requestTypeInfo = true;
}

// https://issues.dlang.org/show_bug.cgi?id=15149
// if the typeid operand type comes from a
Expand Down Expand Up @@ -205,10 +211,13 @@ extern (C++) class StructDeclaration : AggregateDeclaration
bool hasIdentityEquals; // true if has identity opEquals
bool hasNoFields; // has no fields
bool hasCopyCtor; // copy constructor
version (IN_LLVM) {} else
{
// Even if struct is defined as non-root symbol, some built-in operations
// (e.g. TypeidExp, NewExp, ArrayLiteralExp, etc) request its TypeInfo.
// For those, today TypeInfo_Struct is generated in COMDAT.
bool requestTypeInfo;
}

FuncDeclarations postblits; // Array of postblit functions
FuncDeclaration postblit; // aggregate postblit
Expand Down
1 change: 1 addition & 0 deletions dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -7105,6 +7105,7 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
{
if (ad.dtor)
{
err |= !ad.dtor.functionSemantic();
err |= exp.checkPurity(sc, ad.dtor);
err |= exp.checkSafety(sc, ad.dtor);
err |= exp.checkNogc(sc, ad.dtor);
Expand Down
29 changes: 19 additions & 10 deletions dmd/typinf.d
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ void genTypeInfo(Loc loc, Type torig, Scope* sc)
t.vtinfo = getTypeInfoDeclaration(t);
assert(t.vtinfo);

version (IN_LLVM)
{
// LDC handles emission in the codegen layer
}
else
{
/* If this has a custom implementation in std/typeinfo, then
* do not generate a COMDAT for it.
*/
Expand All @@ -89,16 +95,10 @@ void genTypeInfo(Loc loc, Type torig, Scope* sc)
}
else // if in obj generation pass
{
version (IN_LLVM)
{
Declaration_codegen(t.vtinfo);
}
else
{
toObjFile(t.vtinfo, global.params.multiobj);
}
}
}
} // !IN_LLVM
}
if (!torig.vtinfo)
torig.vtinfo = t.vtinfo; // Types aren't merged, but we can share the vtinfo's
Expand Down Expand Up @@ -157,12 +157,19 @@ private TypeInfoDeclaration getTypeInfoDeclaration(Type t)
}
}

version (IN_LLVM)
{
// LDC handles TypeInfo emission in the codegen layer
// => no need to take care of speculative types.
}
else
{

/**************************************************
* Returns:
* true if any part of type t is speculative.
* if t is null, returns false.
*/
extern (C++) // IN_LLVM
bool isSpeculativeType(Type t)
{
static bool visitVector(TypeVector t)
Expand Down Expand Up @@ -255,6 +262,8 @@ bool isSpeculativeType(Type t)
}
}

} // !IN_LLVM

/* ========================================================================= */

/* These decide if there's an instance for them already in std.typeinfo,
Expand All @@ -263,8 +272,8 @@ bool isSpeculativeType(Type t)
// IN_LLVM: replaced `private` with `extern(C++)`
extern(C++) bool builtinTypeInfo(Type t)
{
// LDC_FIXME: if I enable for Tclass, the way LDC does typeinfo will cause
// a bunch of linker errors to missing ClassInfo init symbols.
// IN_LLVM: the Tclass case seems to be a DMD hack
// (in order not to define ClassInfos in each referencing module)
if (t.isTypeBasic() || (!IN_LLVM && t.ty == Tclass) || t.ty == Tnull)
return !t.mod;
if (t.ty == Tarray)
Expand Down
80 changes: 42 additions & 38 deletions gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,23 @@ class CodegenVisitor : public Visitor {
return;
}

if (decl->members && decl->symtab) {
DtoResolveClass(decl);
decl->ir->setDefined();
if (!(decl->members && decl->symtab)) {
return;
}

// Emit any members (e.g. final functions).
for (auto m : *decl->members) {
m->accept(this);
}
DtoResolveClass(decl);
decl->ir->setDefined();

// Emit TypeInfo.
IrClass *ir = getIrAggr(decl);
if (!ir->suppressTypeInfo() && !isSpeculativeType(decl->type)) {
llvm::GlobalVariable *interfaceZ = ir->getClassInfoSymbol();
defineGlobal(interfaceZ, ir->getClassInfoInit(), decl);
}
// Emit any members (e.g. final functions).
for (auto m : *decl->members) {
m->accept(this);
}

// Emit TypeInfo.
IrClass *ir = getIrAggr(decl);
if (!ir->suppressTypeInfo()) {
llvm::GlobalVariable *interfaceZ = ir->getClassInfoSymbol();
defineGlobal(interfaceZ, ir->getClassInfoInit(), decl);
}
}

Expand All @@ -128,6 +130,7 @@ class CodegenVisitor : public Visitor {
}

if (!(decl->members && decl->symtab)) {
// nothing to do for opaque structs anymore
return;
}

Expand All @@ -151,11 +154,9 @@ class CodegenVisitor : public Visitor {
setLinkageAndVisibility(decl, initGlobal);
}

// emit typeinfo
// Emit special __xopEquals/__xopCmp/__xtoHash member functions required
// for the TypeInfo.
if (!ir->suppressTypeInfo()) {
DtoTypeInfoOf(decl->type, /*base=*/false);

// Emit __xopEquals/__xopCmp/__xtoHash.
if (decl->xeq && decl->xeq != decl->xerreq) {
decl->xeq->accept(this);
}
Expand All @@ -165,6 +166,8 @@ class CodegenVisitor : public Visitor {
if (decl->xhash) {
decl->xhash->accept(this);
}

// the TypeInfo itself is emitted into each referencing CU
}
}
}
Expand All @@ -188,31 +191,33 @@ class CodegenVisitor : public Visitor {
return;
}

if (decl->members && decl->symtab) {
DtoResolveClass(decl);
decl->ir->setDefined();
if (!(decl->members && decl->symtab)) {
return;
}

for (auto m : *decl->members) {
m->accept(this);
}
DtoResolveClass(decl);
decl->ir->setDefined();

IrClass *ir = getIrAggr(decl);
for (auto m : *decl->members) {
m->accept(this);
}

auto &initZ = ir->getInitSymbol();
auto initGlobal = llvm::cast<LLGlobalVariable>(initZ);
initZ = irs->setGlobalVarInitializer(initGlobal, ir->getDefaultInit());
setLinkageAndVisibility(decl, initGlobal);
IrClass *ir = getIrAggr(decl);

llvm::GlobalVariable *vtbl = ir->getVtblSymbol();
defineGlobal(vtbl, ir->getVtblInit(), decl);
auto &initZ = ir->getInitSymbol();
auto initGlobal = llvm::cast<LLGlobalVariable>(initZ);
initZ = irs->setGlobalVarInitializer(initGlobal, ir->getDefaultInit());
setLinkageAndVisibility(decl, initGlobal);

ir->defineInterfaceVtbls();
llvm::GlobalVariable *vtbl = ir->getVtblSymbol();
defineGlobal(vtbl, ir->getVtblInit(), decl);

// Emit TypeInfo.
if (!ir->suppressTypeInfo() && !isSpeculativeType(decl->type)) {
llvm::GlobalVariable *classZ = ir->getClassInfoSymbol();
defineGlobal(classZ, ir->getClassInfoInit(), decl);
}
ir->defineInterfaceVtbls();

// Emit TypeInfo.
if (!ir->suppressTypeInfo()) {
llvm::GlobalVariable *classZ = ir->getClassInfoSymbol();
defineGlobal(classZ, ir->getClassInfoInit(), decl);
}
}

Expand Down Expand Up @@ -503,8 +508,7 @@ class CodegenVisitor : public Visitor {
//////////////////////////////////////////////////////////////////////////

void visit(TypeInfoDeclaration *decl) override {
if (!irs->dcomputetarget)
TypeInfoDeclaration_codegen(decl);
llvm_unreachable("Should be emitted from codegen layer only");
}
};

Expand Down
22 changes: 8 additions & 14 deletions gen/llvmhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -827,8 +827,9 @@ void DtoResolveDsymbol(Dsymbol *dsym) {
}

void DtoResolveVariable(VarDeclaration *vd) {
if (vd->isTypeInfoDeclaration()) {
return DtoResolveTypeInfo(static_cast<TypeInfoDeclaration *>(vd));
if (auto tid = vd->isTypeInfoDeclaration()) {
DtoResolveTypeInfo(tid);
return;
}

IF_LOG Logger::println("DtoResolveVariable(%s)", vd->toPrettyChars());
Expand Down Expand Up @@ -1263,17 +1264,12 @@ LLConstant *DtoTypeInfoOf(Type *type, bool base) {
type->toChars(), base);
LOG_SCOPE

TypeInfoDeclaration *tidecl =
getOrCreateTypeInfoDeclaration(Loc(), type, nullptr);
assert(tidecl);
Declaration_codegen(tidecl);
assert(getIrGlobal(tidecl)->value != NULL);
LLConstant *c = isaConstant(getIrGlobal(tidecl)->value);
assert(c != NULL);
auto tidecl = getOrCreateTypeInfoDeclaration(Loc(), type);
auto tiglobal = DtoResolveTypeInfo(tidecl);
if (base) {
return llvm::ConstantExpr::getBitCast(c, DtoType(getTypeInfoType()));
return llvm::ConstantExpr::getBitCast(tiglobal, DtoType(getTypeInfoType()));
}
return c;
return tiglobal;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1560,10 +1556,8 @@ DValue *DtoSymbolAddress(Loc &loc, Type *type, Declaration *decl) {
// typeinfo
if (TypeInfoDeclaration *tid = vd->isTypeInfoDeclaration()) {
Logger::println("TypeInfoDeclaration");
DtoResolveTypeInfo(tid);
assert(getIrValue(tid));
LLType *vartype = DtoType(type);
LLValue *m = getIrValue(tid);
LLValue *m = DtoResolveTypeInfo(tid);
if (m->getType() != getPtrToType(vartype)) {
m = gIR->ir->CreateBitCast(m, vartype);
}
Expand Down
4 changes: 2 additions & 2 deletions gen/toir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2669,8 +2669,8 @@ class ToElemVisitor : public Visitor {

void visit(TypeidExp *e) override {
if (Type *t = isType(e->obj)) {
result = DtoSymbolAddress(
e->loc, e->type, getOrCreateTypeInfoDeclaration(e->loc, t, nullptr));
result = DtoSymbolAddress(e->loc, e->type,
getOrCreateTypeInfoDeclaration(e->loc, t));
return;
}
if (Expression *ex = isExpression(e->obj)) {
Expand Down
Loading