Skip to content

Enable groupshared arg on templates + tests#8217

Open
spall wants to merge 2 commits intomicrosoft:mainfrom
spall:groupsharedArgTemplates
Open

Enable groupshared arg on templates + tests#8217
spall wants to merge 2 commits intomicrosoft:mainfrom
spall:groupsharedArgTemplates

Conversation

@spall
Copy link
Collaborator

@spall spall commented Mar 2, 2026

As a follow up to #8013 and issue #7969, add support for the use of the groupshared parameter attribute in templates.
Adds new tests showing codegen and updates existing Sema tests to show relevant errors produced.

template<typename T>
T fn(groupshared T A) {
  return A;
}

@github-actions
Copy link
Contributor

github-actions bot commented Mar 2, 2026

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff d4b2d044f150c5bf9ac056fcacb2a30df54b5d93 c1a417eeb59db3dfc459bb1d62220cf1f5e53d77 -- tools/clang/lib/Sema/SemaDecl.cpp tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
View the diff from clang-format here.
diff --git a/tools/clang/lib/Sema/SemaDecl.cpp b/tools/clang/lib/Sema/SemaDecl.cpp
index b1069551..b07d4a9a 100644
--- a/tools/clang/lib/Sema/SemaDecl.cpp
+++ b/tools/clang/lib/Sema/SemaDecl.cpp
@@ -10460,10 +10460,10 @@ ParmVarDecl *Sema::CheckParameter(DeclContext *DC, SourceLocation StartLoc,
     // OpenCL allows function arguments declared to be an array of a type
     // to be qualified with an address space.
     if (!(getLangOpts().OpenCL && T->isArrayType()) &&
-	// HLSL allows parameters to be qualified with the groupshared
-	// address space.
-	!(getLangOpts().HLSL && T.getAddressSpace() ==
-	  hlsl::DXIL::kTGSMAddrSpace)) {
+        // HLSL allows parameters to be qualified with the groupshared
+        // address space.
+        !(getLangOpts().HLSL &&
+          T.getAddressSpace() == hlsl::DXIL::kTGSMAddrSpace)) {
       Diag(NameLoc, diag::err_arg_with_address_space);
       New->setInvalidDecl();
     }
diff --git a/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 571c6e49..c3cd903a 100644
--- a/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -269,12 +269,12 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
     }
 
     if (isa<HLSLGroupSharedAttr>(TmplAttr)) {
-      // the type wasn't set properly before so we need to instantiate this type as
-      // a groupshared reference
+      // the type wasn't set properly before so we need to instantiate this type
+      // as a groupshared reference
       ParmVarDecl *NewParm = cast<ParmVarDecl>(New);
       NewParm->addAttr(TmplAttr->clone(getASTContext()));
-      NewParm->setType(Context.getAddrSpaceQualType(NewParm->getType(),
-						    hlsl::DXIL::kTGSMAddrSpace));
+      NewParm->setType(Context.getAddrSpaceQualType(
+          NewParm->getType(), hlsl::DXIL::kTGSMAddrSpace));
       NewParm->setType(Context.getLValueReferenceType(NewParm->getType()));
       continue;
     }
  • Check this box to apply formatting changes to this branch.

// expected-error@-1{{'template' is a reserved keyword in HLSL}}
template [noinline] void fn3<float>(groupshared float A, groupshared float B);
// expected-error@-1{{use of undeclared identifier 'noinline'}}
// expected-error@-2{{expected unqualified-id}} No newline at end of file
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: file ending

[numthreads(4, 1, 1)]
void main(uint3 TID : SV_GroupThreadID) {
fn1(SharedData);
tfoo<int4>(SharedData1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expect something like tfoo<uint4>(SharedData1) to be valid?

edit: I see we don't allow uint16_t to half below

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah casting isn't allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New

Development

Successfully merging this pull request may close these issues.

3 participants