Skip to content

Commit

Permalink
Get rid of special 'typeid(...)' LL types for TypeInfos
Browse files Browse the repository at this point in the history
Use the real LL type representing the TypeInfo (sub)class directly and
from the beginning, i.e., already for the declaration, instead of
building an extra LL struct type (firstly opaque, then finalized when
defining the TypeInfo via RTTIBuilder).

To get this to work in all cases, the dummy TypeInfo for opaque structs
is now a complete TypeInfo_Struct, with all 11/13 fields zero-
initialized. Previously, it was a special TypeInfo (no extra
TypeInfo_Struct fields) with TypeInfo_Struct vtable, something which DMD
also emits.

Also refactor the RTTIBuilder finalize() interface - e.g., don't set the
linkage there (had to be reverted for ModuleInfos) and only take the
global variable to be defined. Cast the field initializers if required,
e.g., null pointers to appropriately typed function pointers for
TypeInfo_Struct etc.
  • Loading branch information
kinke committed Oct 24, 2017
1 parent 3d0a127 commit 3f38f97
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 76 deletions.
3 changes: 1 addition & 2 deletions gen/moduleinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ llvm::GlobalVariable *genModuleInfo(Module *m) {

// Create a global symbol with the above initialiser.
LLGlobalVariable *moduleInfoSym = getIrModule(m)->moduleInfoSymbol();
b.finalize(moduleInfoSym->getType()->getPointerElementType(), moduleInfoSym);
setLinkage({LLGlobalValue::ExternalLinkage, false}, moduleInfoSym);
b.finalize(moduleInfoSym);
return moduleInfoSym;
}
39 changes: 19 additions & 20 deletions gen/rttibuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,36 +150,35 @@ void RTTIBuilder::push_funcptr(FuncDeclaration *fd, Type *castto) {
}
}

void RTTIBuilder::finalize(IrGlobal *tid) {
finalize(tid->getType(), tid->value);
}

void RTTIBuilder::finalize(LLType *type, LLValue *value) {
llvm::ArrayRef<LLConstant *> inits = llvm::makeArrayRef(this->inits);
LLStructType *st = isaStruct(type);
void RTTIBuilder::finalize(LLGlobalVariable *gvar) {
LLStructType *st = isaStruct(gvar->getType()->getPointerElementType());
assert(st);

// set struct body
// finalize the type if opaque (e.g., for ModuleInfos)
if (st->isOpaque()) {
const int n = inits.size();
std::vector<LLType *> types;
types.reserve(n);
for (int i = 0; i < n; ++i) {
types.push_back(inits[i]->getType());
std::vector<LLType *> fieldTypes;
fieldTypes.reserve(inits.size());
for (auto c : inits) {
fieldTypes.push_back(c->getType());
}
st->setBody(types);
st->setBody(fieldTypes);
}

// create the inititalizer
LLConstant *tiInit = LLConstantStruct::get(st, inits);
// create the initializer
LLConstant *tiInit = get_constant(st);

// set the initializer
llvm::GlobalVariable *gvar = llvm::cast<llvm::GlobalVariable>(value);
gvar->setInitializer(tiInit);
setLinkage({TYPEINFO_LINKAGE_TYPE, supportsCOMDAT()}, gvar);
}

LLConstant *RTTIBuilder::get_constant(LLStructType *initType) {
// just return the inititalizer
return LLConstantStruct::get(initType, inits);
assert(initType->getNumElements() == inits.size());

std::vector<LLConstant *> castInits;
castInits.reserve(inits.size());
for (unsigned i = 0; i < inits.size(); ++i) {
castInits.push_back(DtoBitCast(inits[i], initType->getElementType(i)));
}

return LLConstantStruct::get(initType, castInits);
}
4 changes: 2 additions & 2 deletions gen/rttibuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Type;
class TypeClass;
namespace llvm {
class StructType;
class GlobalVariable;
}

class RTTIBuilder {
Expand Down Expand Up @@ -79,8 +80,7 @@ class RTTIBuilder {
Dsymbol *mangle_sym);

/// Creates the initializer constant and assigns it to the global.
void finalize(IrGlobal *tid);
void finalize(llvm::Type *type, llvm::Value *value);
void finalize(llvm::GlobalVariable *gvar);

/// Creates the initializer constant and assigns it to the global.
llvm::Constant *get_constant(llvm::StructType *initType);
Expand Down
118 changes: 69 additions & 49 deletions gen/typinf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ void DtoResolveTypeInfo(TypeInfoDeclaration *tid) {
/* ========================================================================= */

class LLVMDefineVisitor : public Visitor {
LLGlobalVariable *const gvar;

public:
LLVMDefineVisitor(LLGlobalVariable *gvar) : gvar(gvar) {}

// Import all functions from class Visitor
using Visitor::visit;

Expand All @@ -140,7 +144,7 @@ class LLVMDefineVisitor : public Visitor {
LOG_SCOPE;

RTTIBuilder b(Type::dtypeinfo);
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand Down Expand Up @@ -175,7 +179,7 @@ class LLVMDefineVisitor : public Visitor {
}

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -189,7 +193,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->nextOf());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -203,7 +207,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->nextOf());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -225,7 +229,7 @@ class LLVMDefineVisitor : public Visitor {
b.push(DtoConstSize_t(static_cast<size_t>(tc->dim->toUInteger())));

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -248,7 +252,7 @@ class LLVMDefineVisitor : public Visitor {
b.push_typeinfo(tc->index);

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -264,7 +268,7 @@ class LLVMDefineVisitor : public Visitor {
// string deco
b.push_string(decl->tinfo->deco);
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -283,7 +287,7 @@ class LLVMDefineVisitor : public Visitor {
// string deco
b.push_string(decl->tinfo->deco);
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -298,10 +302,44 @@ class LLVMDefineVisitor : public Visitor {
TypeStruct *tc = static_cast<TypeStruct *>(decl->tinfo);
StructDeclaration *sd = tc->sym;

// On x86_64, class TypeInfo_Struct contains 2 additional fields
// (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs
// implementation. They are not present on any other cpu/os.
const bool isX86_64 =
global.params.targetTriple->getArch() == llvm::Triple::x86_64;
const unsigned expectedFields = 11 + (isX86_64 ? 2 : 0);
const unsigned actualFields =
Type::typeinfostruct->fields.dim -
1; // union of xdtor/xdtorti counts as 2 overlapping fields
if (actualFields != expectedFields) {
error(Loc(), "Unexpected number of `object.TypeInfo_Struct` fields; "
"druntime version does not match compiler");
fatal();
}

RTTIBuilder b(Type::typeinfostruct);

// handle opaque structs
if (!sd->members) {
RTTIBuilder b(Type::typeinfostruct);
b.finalize(getIrGlobal(decl));
Logger::println("is opaque struct, emitting dummy TypeInfo_Struct");

b.push_null_void_array(); // name
b.push_null_void_array(); // m_init
b.push_null_vp(); // xtoHash
b.push_null_vp(); // xopEquals
b.push_null_vp(); // xopCmp
b.push_null_vp(); // xtoString
b.push_uint(0); // m_flags
b.push_null_vp(); // xdtor/xdtorti
b.push_null_vp(); // xpostblit
b.push_uint(0); // m_align
if (isX86_64) {
b.push_null_vp(); // m_arg1
b.push_null_vp(); // m_arg2
}
b.push_null_vp(); // m_RTInfo

b.finalize(gvar);
return;
}

Expand Down Expand Up @@ -343,19 +381,6 @@ class LLVMDefineVisitor : public Visitor {
}

IrAggr *iraggr = getIrAggr(sd);
RTTIBuilder b(Type::typeinfostruct);

// On x86_64, class TypeInfo_Struct contains 2 additional fields
// (m_arg1/m_arg2) which are used for the X86_64 System V ABI varargs
// implementation. They are not present on any other cpu/os.
const bool isX86_64 =
global.params.targetTriple->getArch() == llvm::Triple::x86_64;
const unsigned expectedFields = 12 + (isX86_64 ? 2 : 0);
if (Type::typeinfostruct->fields.dim != expectedFields) {
error(Loc(), "Unexpected number of `object.TypeInfo_Struct` fields; "
"druntime version does not match compiler");
fatal();
}

// string name
b.push_string(sd->toPrettyChars());
Expand Down Expand Up @@ -432,7 +457,7 @@ class LLVMDefineVisitor : public Visitor {
}

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand Down Expand Up @@ -461,7 +486,7 @@ class LLVMDefineVisitor : public Visitor {
b.push_classinfo(tc->sym);

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand Down Expand Up @@ -495,7 +520,7 @@ class LLVMDefineVisitor : public Visitor {
b.push_array(arrC, dim, Type::dtypeinfo->type, nullptr);

// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -509,7 +534,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->mutableOf()->merge());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -523,7 +548,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->mutableOf()->merge());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -537,7 +562,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->unSharedOf()->merge());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -551,7 +576,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(decl->tinfo->mutableOf()->merge());
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}

/* ======================================================================= */
Expand All @@ -568,7 +593,7 @@ class LLVMDefineVisitor : public Visitor {
// TypeInfo base
b.push_typeinfo(tv->basetype);
// finish
b.finalize(getIrGlobal(decl));
b.finalize(gvar);
}
};

Expand All @@ -594,29 +619,22 @@ void TypeInfoDeclaration_codegen(TypeInfoDeclaration *decl, IRState *p) {
}

const auto irMangle = getIRMangledVarName(mangled, LINKd);
IrGlobal *irg = getIrGlobal(decl, true);
const LinkageWithCOMDAT lwc(LLGlobalValue::ExternalLinkage, false);

irg->value = gIR->module.getGlobalVariable(irMangle);
if (irg->value) {
assert(irg->getType()->isStructTy());
LLGlobalVariable *gvar = gIR->module.getGlobalVariable(irMangle);
if (gvar) {
assert(gvar->getType()->getContainedType(0)->isStructTy());
} else {
LLType *type;
if (builtinTypeInfo(
decl->tinfo)) { // this is a declaration of a builtin __initZ var
type = Type::dtypeinfo->type->ctype->isClass()->getMemoryLLType();
} else {
type = LLStructType::create(gIR->context(), decl->toPrettyChars());
}
LLType *type = DtoType(decl->type)->getPointerElementType();
// Create the symbol. We need to keep it mutable as the type is not declared
// as immutable on the D side, and e.g. synchronized() can be used on the
// implicit monitor.
auto g = new LLGlobalVariable(gIR->module, type, false, lwc.first,
nullptr, irMangle);
setLinkage(lwc, g);
irg->value = g;
gvar =
new LLGlobalVariable(gIR->module, type, false,
LLGlobalValue::ExternalLinkage, nullptr, irMangle);
}

IrGlobal *irg = getIrGlobal(decl, true);
irg->value = gvar;

emitTypeMetadata(decl);

// check if the definition can be elided
Expand All @@ -626,8 +644,10 @@ void TypeInfoDeclaration_codegen(TypeInfoDeclaration *decl, IRState *p) {
}

// define the TypeInfo global
LLVMDefineVisitor v;
LLVMDefineVisitor v(gvar);
decl->accept(&v);

setLinkage({TYPEINFO_LINKAGE_TYPE, supportsCOMDAT()}, gvar);
}

/* ========================================================================= */
Expand Down
9 changes: 6 additions & 3 deletions tests/codegen/static_typeid_gh1540.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ struct S
{
}

// CHECK: _D{{.*}}classvarC14TypeInfo_Class{{\"?}} = thread_local global %object.TypeInfo_Class* {{.*}}1C7__ClassZ
// CHECK-DAG: _D{{.*}}1C7__ClassZ{{\"?}} = global %object.TypeInfo_Class
// CHECK-DAG: _D{{.*}}classvarC14TypeInfo_Class{{\"?}} = thread_local global %object.TypeInfo_Class* {{.*}}1C7__ClassZ
auto classvar = typeid(C);

// CHECK: _D{{.*}}interfacevarC18TypeInfo_Interface{{\"?}} = thread_local global %object.TypeInfo_Interface* {{.*}}TypeInfo_C{{.*}}1I6__initZ
// CHECK-DAG: _D{{.*}}TypeInfo_C{{.*}}1I6__initZ{{\"?}} = linkonce_odr global %object.TypeInfo_Interface
// CHECK-DAG: _D{{.*}}interfacevarC18TypeInfo_Interface{{\"?}} = thread_local global %object.TypeInfo_Interface* {{.*}}TypeInfo_C{{.*}}1I6__initZ
auto interfacevar = typeid(I);

// CHECK: _D{{.*}}structvarC15TypeInfo_Struct{{\"?}} = thread_local global %object.TypeInfo_Struct* {{.*}}TypeInfo_S{{.*}}1S6__initZ
// CHECK-DAG: _D{{.*}}TypeInfo_S{{.*}}1S6__initZ{{\"?}} = linkonce_odr global %object.TypeInfo_Struct
// CHECK-DAG: _D{{.*}}structvarC15TypeInfo_Struct{{\"?}} = thread_local global %object.TypeInfo_Struct* {{.*}}TypeInfo_S{{.*}}1S6__initZ
auto structvar = typeid(S);

0 comments on commit 3f38f97

Please sign in to comment.