From ca6242ec4087276809b45060234e0804e7de9abc Mon Sep 17 00:00:00 2001 From: Helena Kotas Date: Fri, 7 Jun 2024 12:08:00 -0700 Subject: [PATCH] Set internal linkage on groupshared variables; check if function is defined --- clang/include/clang/Basic/Attr.td | 4 +++- clang/lib/AST/Decl.cpp | 6 ++++++ clang/lib/CodeGen/CGHLSLRuntime.cpp | 8 ++++---- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 17d9a710d948b2..4e292826d4e1a1 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -158,8 +158,10 @@ def FunctionTmpl def HLSLEntry : SubsetSubjectisExternallyVisible() && !isa(S)}], + [{S->getDeclContext()->getRedeclContext()->isFileContext() && + S->getStorageClass() != SC_Static}], "global functions">; + def HLSLBufferObj : SubsetSubject(S)}], "cbuffer/tbuffer">; diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp index 1f19dadafa44e8..dc5566bab312cc 100644 --- a/clang/lib/AST/Decl.cpp +++ b/clang/lib/AST/Decl.cpp @@ -621,6 +621,7 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, // - a variable, variable template, function, or function template // that is explicitly declared static; or // (This bullet corresponds to C99 6.2.2p3.) + // - also applies to HLSL return LinkageInfo::internal(); } @@ -657,6 +658,11 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D, if (PrevVar->getStorageClass() == SC_Static) return LinkageInfo::internal(); } + + if (Context.getLangOpts().HLSL && + Var->hasAttr()) + return LinkageInfo::internal(); + } else if (const auto *IFD = dyn_cast(D)) { // - a data member of an anonymous union. const VarDecl *VD = IFD->getVarDecl(); diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp b/clang/lib/CodeGen/CGHLSLRuntime.cpp index 8e5123daf2084b..173f0c1449c869 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.cpp +++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp @@ -361,10 +361,10 @@ void CGHLSLRuntime::emitFunctionProlog(const FunctionDecl *FD, if (FD->hasAttr()) { emitEntryFunction(FD, Fn); } else { - // HLSL functions that are not shader entry points or exported - // have internal linkage by default. - // FIXME: skip this for exported functions (Issue #92812) - Fn->setLinkage(GlobalValue::InternalLinkage); + // HLSL functions defined in the current translation unit that are not + // shader entry points or exported have internal linkage by default. + if (FD->isDefined()) + Fn->setLinkage(GlobalValue::InternalLinkage); } }