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

Add @hidden UDA #3855

Merged
merged 1 commit into from
Oct 23, 2021
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
1 change: 1 addition & 0 deletions dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,7 @@ immutable Msgtable[] msgtable =
{ "udaDynamicCompile", "_dynamicCompile" },
{ "udaDynamicCompileConst", "_dynamicCompileConst" },
{ "udaDynamicCompileEmit", "_dynamicCompileEmit" },
{ "udaHidden", "_hidden" },

// IN_LLVM: DCompute specific types and functionss
{ "dcompute" },
Expand Down
1 change: 1 addition & 0 deletions dmd/id.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct Id
static Identifier *udaDynamicCompile;
static Identifier *udaDynamicCompileConst;
static Identifier *udaDynamicCompileEmit;
static Identifier *udaHidden;
static Identifier *io;
#endif
};
45 changes: 36 additions & 9 deletions gen/tollvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,48 @@ void setLinkageAndVisibility(Dsymbol *sym, llvm::GlobalObject *obj) {
setVisibility(sym, obj);
}

namespace {
bool hasExportedLinkage(llvm::GlobalObject *obj) {
const auto l = obj->getLinkage();
return l == LLGlobalValue::ExternalLinkage ||
l == LLGlobalValue::WeakODRLinkage ||
l == LLGlobalValue::WeakAnyLinkage;
}
}

void setVisibility(Dsymbol *sym, llvm::GlobalObject *obj) {
if (global.params.targetTriple->isOSWindows()) {
const auto &triple = *global.params.targetTriple;

const bool hasHiddenUDA = obj->hasHiddenVisibility();

if (triple.isOSWindows()) {
bool isExported = sym->isExport();
if (!isExported && global.params.dllexport) {
const auto l = obj->getLinkage();
isExported = l == LLGlobalValue::ExternalLinkage ||
l == LLGlobalValue::WeakODRLinkage ||
l == LLGlobalValue::WeakAnyLinkage;
// also export with -fvisibility=public without @hidden
if (!isExported && global.params.dllexport && !hasHiddenUDA) {
isExported = hasExportedLinkage(obj);
}
// reset default visibility & DSO locality - on Windows, the DLL storage
// classes matter
if (hasHiddenUDA) {
obj->setVisibility(LLGlobalValue::DefaultVisibility);
obj->setDSOLocal(false);
}
obj->setDLLStorageClass(isExported ? LLGlobalValue::DLLExportStorageClass
: LLGlobalValue::DefaultStorageClass);
} else {
if (opts::symbolVisibility == opts::SymbolVisibility::hidden &&
!sym->isExport()) {
obj->setVisibility(LLGlobalValue::HiddenVisibility);
if (sym->isExport()) {
obj->setVisibility(LLGlobalValue::DefaultVisibility); // overrides @hidden
} else if (!hasHiddenUDA) {
// Hide with -fvisibility=hidden, or linkonce_odr etc.
// The Apple linker warns about hidden linkonce_odr symbols from object
// files compiled with -linkonce-templates being folded with *public*
// weak_odr symbols from non-linkonce-templates code (e.g., Phobos), so
// don't hide instantiated symbols for Mac.
if (opts::symbolVisibility == opts::SymbolVisibility::hidden ||
(!hasExportedLinkage(obj) &&
!(triple.isOSDarwin() && sym->isInstantiated()))) {
obj->setVisibility(LLGlobalValue::HiddenVisibility);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions gen/uda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ void applyVarDeclUDAs(VarDeclaration *decl, llvm::GlobalVariable *gvar) {
auto ident = sle->sd->ident;
if (ident == Id::udaSection) {
applyAttrSection(sle, gvar);
} else if (ident == Id::udaHidden) {
if (!decl->isExport()) // export visibility is stronger
gvar->setVisibility(LLGlobalValue::HiddenVisibility);
} else if (ident == Id::udaOptStrategy || ident == Id::udaTarget) {
sle->error(
"Special attribute `ldc.attributes.%s` is only valid for functions",
Expand Down Expand Up @@ -423,6 +426,9 @@ void applyFuncDeclUDAs(FuncDeclaration *decl, IrFunction *irFunc) {
#else
func->addAttributes(LLAttributeList::FunctionIndex, attrs);
#endif
} else if (ident == Id::udaHidden) {
if (!decl->isExport()) // export visibility is stronger
func->setVisibility(LLGlobalValue::HiddenVisibility);
} else if (ident == Id::udaLLVMFastMathFlag) {
applyAttrLLVMFastMathFlag(sle, irFunc);
} else if (ident == Id::udaOptStrategy) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/druntime
Submodule druntime updated 1 files
+11 −0 src/ldc/attributes.d
44 changes: 44 additions & 0 deletions tests/codegen/fvisibility_posix.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Non-Windows: test generated IR for -fvisibility / `export` / @hidden combinations.

// UNSUPPORTED: Windows

// RUN: %ldc -fvisibility=public -output-ll -of=%t_public.ll %s
// RUN: FileCheck %s --check-prefix=PUBLIC --check-prefix=COMMON < %t_public.ll

// RUN: %ldc -fvisibility=hidden -output-ll -of=%t_hidden.ll %s
// RUN: FileCheck %s --check-prefix=HIDDEN --check-prefix=COMMON < %t_hidden.ll

import ldc.attributes : hidden;

extern(C):

export
{
// COMMON-DAG: @exportedGlobal = global i32
__gshared int exportedGlobal;
// COMMON-DAG: define void @exportedFunc()
void exportedFunc() {}
}

// PUBLIC-DAG: @global = global i32
// HIDDEN-DAG: @global = hidden global i32
__gshared int global;
// PUBLIC-DAG: define void @func()
// HIDDEN-DAG: define hidden void @func()
void func() {}

@hidden
{
// COMMON-DAG: @hiddenGlobal = hidden global i32
__gshared int hiddenGlobal;
// COMMON-DAG: define hidden void @hiddenFunc()
void hiddenFunc() {}

export
{
// COMMON-DAG: @exportedHiddenGlobal = global i32
__gshared int exportedHiddenGlobal;
// COMMON-DAG: define void @exportedHiddenFunc()
void exportedHiddenFunc() {}
}
}
44 changes: 44 additions & 0 deletions tests/codegen/fvisibility_windows.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Windows: test generated IR for -fvisibility / `export` / @hidden combinations.

// REQUIRES: Windows

// RUN: %ldc -fvisibility=public -output-ll -of=%t_public.ll %s
// RUN: FileCheck %s --check-prefix=PUBLIC --check-prefix=COMMON < %t_public.ll

// RUN: %ldc -fvisibility=hidden -output-ll -of=%t_hidden.ll %s
// RUN: FileCheck %s --check-prefix=HIDDEN --check-prefix=COMMON < %t_hidden.ll

import ldc.attributes : hidden;

extern(C):

export
{
// COMMON-DAG: @exportedGlobal = dllexport global i32
__gshared int exportedGlobal;
// COMMON-DAG: define dllexport void @exportedFunc()
void exportedFunc() {}
}

// PUBLIC-DAG: @global = dllexport global i32
// HIDDEN-DAG: @global = global i32
__gshared int global;
// PUBLIC-DAG: define dllexport void @func()
// HIDDEN-DAG: define void @func()
void func() {}

@hidden
{
// COMMON-DAG: @hiddenGlobal = global i32
__gshared int hiddenGlobal;
// COMMON-DAG: define void @hiddenFunc()
void hiddenFunc() {}

export
{
// COMMON-DAG: @exportedHiddenGlobal = dllexport global i32
__gshared int exportedHiddenGlobal;
// COMMON-DAG: define dllexport void @exportedHiddenFunc()
void exportedHiddenFunc() {}
}
}
6 changes: 3 additions & 3 deletions tests/codegen/lambdas_gh3648.d
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ void foo()
}

// the global variables should be defined as linkonce_odr:
// CHECK: _D14lambdas_gh36489__lambda5FZ10global_bari{{.*}} = linkonce_odr thread_local global
// CHECK: _D14lambdas_gh36489__lambda6FZ18global_bar_inlinedOi{{.*}} = linkonce_odr global
// CHECK: _D14lambdas_gh36483fooFZ__T9__lambda1TiZQnFiZ12lambda_templi{{.*}} = linkonce_odr global
// CHECK: _D14lambdas_gh36489__lambda5FZ10global_bari{{.*}} = linkonce_odr {{(hidden )?}}thread_local global
// CHECK: _D14lambdas_gh36489__lambda6FZ18global_bar_inlinedOi{{.*}} = linkonce_odr {{(hidden )?}}global
// CHECK: _D14lambdas_gh36483fooFZ__T9__lambda1TiZQnFiZ12lambda_templi{{.*}} = linkonce_odr {{(hidden )?}}global

// foo() should only call two lambdas:
// CHECK: define {{.*}}_D14lambdas_gh36483fooFZv
Expand Down
4 changes: 2 additions & 2 deletions tests/codegen/lambdas_gh3648b.d
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ void foo()
}

// the global variables should be defined as linkonce_odr:
// CHECK: _D14lambdas_gh36489__lambda5FZ10global_bari{{.*}} = linkonce_odr thread_local global
// CHECK: _D14lambdas_gh36489__lambda6FZ18global_bar_inlinedOi{{.*}} = linkonce_odr global
// CHECK: _D14lambdas_gh36489__lambda5FZ10global_bari{{.*}} = linkonce_odr {{(hidden )?}}thread_local global
// CHECK: _D14lambdas_gh36489__lambda6FZ18global_bar_inlinedOi{{.*}} = linkonce_odr {{(hidden )?}}global

// foo() should only call one lambda:
// CHECK: define {{.*}}_D15lambdas_gh3648b3fooFZv
Expand Down