Revert to previous define-on-declare solution for -dllimport=defaultLibsOnly (#3932)

See #3931 for context.
This commit is contained in:
Martin Kinkelin 2022-03-03 19:39:01 +01:00 committed by GitHub
parent 5d1080c26d
commit f068482026
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 16 deletions

View file

@ -138,8 +138,8 @@ int linkObjToBinaryMSVC(llvm::StringRef outputPath,
if (willLinkAgainstSharedDefaultLibs) {
// Suppress linker warning LNK4217 wrt. 'importing locally defined symbol'
// (dllimport of symbol dllexported from the same binary), because there
// might be *many* of those (=> instantiated globals) if compiled without
// -linkonce-templates.
// might be *many* of those (=> instantiated globals) if compiled with
// -dllimport=all (and without -linkonce-templates).
args.push_back("/IGNORE:4217");
}

View file

@ -1733,9 +1733,16 @@ static bool isDefaultLibSymbol(Dsymbol *sym) {
(md->packages.length > 1 && md->packages.ptr[1] == Id::io)));
}
bool defineOnDeclare(Dsymbol *sym, bool) {
return global.params.linkonceTemplates != LinkonceTemplates::no &&
sym->isInstantiated();
bool defineOnDeclare(Dsymbol *sym, bool isFunction) {
// -linkonce-templates: all instantiated symbols
if (global.params.linkonceTemplates != LinkonceTemplates::no)
return sym->isInstantiated();
// -dllimport=defaultLibsOnly: all data symbols instantiated from
// druntime/Phobos templates
// see https://github.com/ldc-developers/ldc/issues/3931
return !isFunction && global.params.dllimport == DLLImport::defaultLibsOnly &&
sym->isInstantiated() && isDefaultLibSymbol(sym);
}
bool dllimportDataSymbol(Dsymbol *sym) {
@ -1747,17 +1754,15 @@ bool dllimportDataSymbol(Dsymbol *sym) {
isDefaultLibSymbol(sym))) {
// Okay, this symbol is a candidate. Use dllimport unless we have a
// guaranteed-codegen'd definition in a root module.
if (auto mod = sym->isModule()) {
if (auto mod = sym->isModule())
return !mod->isRoot(); // non-root ModuleInfo symbol
} else if (sym->inNonRoot()) {
if (sym->inNonRoot())
return true; // not instantiated, and defined in non-root
} else if (global.params.linkonceTemplates == LinkonceTemplates::no &&
sym->isInstantiated()) {
if (sym->isInstantiated() && !defineOnDeclare(sym, false))
return true; // instantiated but potentially culled (needsCodegen())
} else if (auto vd = sym->isVarDeclaration()) {
if (auto vd = sym->isVarDeclaration())
if (vd->storage_class & STCextern)
return true; // externally defined global variable
}
}
return false;

View file

@ -1,12 +1,18 @@
// REQUIRES: Windows
// RUN: %ldc -output-ll -dllimport=all -of=%t_all.ll %s && FileCheck %s < %t_all.ll
// RUN: %ldc -output-ll -dllimport=defaultLibsOnly -of=%t_dlo.ll %s && FileCheck %s < %t_dlo.ll
// RUN: %ldc -output-ll -dllimport=all -of=%t_all.ll %s && FileCheck %s --check-prefix=ALL < %t_all.ll
// RUN: %ldc -output-ll -dllimport=defaultLibsOnly -of=%t_dlo.ll %s && FileCheck %s --check-prefix=DLO < %t_dlo.ll
import std.random : Xorshift; // pre-instantiated template
void foo()
{
// CHECK: _D3std6random__T14XorshiftEngine{{.*}}6__initZ = external dllimport
const i = __traits(initSymbol, Xorshift);
}
// -dllimport=all: dllimport declaration
// ALL: @_D3std6random__T14XorshiftEngine{{.*}}6__initZ = external dllimport constant
// -dllimport=defaultLibsOnly: define-on-declare instantiated druntime/Phobos data symbols
// see https://github.com/ldc-developers/ldc/issues/3931
// DLO: @_D3std6random__T14XorshiftEngine{{.*}}6__initZ = weak_odr constant

View file

@ -1,7 +1,6 @@
// REQUIRES: Windows
// RUN: %ldc -output-ll -dllimport=all -of=%t_all.ll %s && FileCheck %s < %t_all.ll
// RUN: %ldc -output-ll -dllimport=defaultLibsOnly -of=%t_dlo.ll %s && FileCheck %s < %t_dlo.ll
// RUN: %ldc -output-ll -dllimport=all -of=%t.ll %s && FileCheck %s < %t.ll
import std.variant : Variant; // pre-instantiated template