Skip to content

Commit b8dd93b

Browse files
Enable groupshared arg on templates + tests (#8217)
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; } ``` --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 70ebb47 commit b8dd93b

8 files changed

Lines changed: 81 additions & 0 deletions

File tree

tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,17 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
268268
continue;
269269
}
270270

271+
if (isa<HLSLGroupSharedAttr>(TmplAttr)) {
272+
// the type wasn't set properly before so we need to instantiate this type
273+
// as a groupshared reference
274+
ParmVarDecl *NewParm = cast<ParmVarDecl>(New);
275+
NewParm->addAttr(TmplAttr->clone(getASTContext()));
276+
NewParm->setType(Context.getAddrSpaceQualType(
277+
NewParm->getType(), hlsl::DXIL::kTGSMAddrSpace));
278+
NewParm->setType(Context.getLValueReferenceType(NewParm->getType()));
279+
continue;
280+
}
281+
271282
// HLSL Change Begin - Validate post-instantiation attributes
272283
DiagnoseHLSLDeclAttr(New, TmplAttr);
273284
// HLSL Change End

tools/clang/test/CodeGenHLSL/groupsharedArgs/TemplateTest.hlsl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@ void fn1(groupshared Shared<int> Sh) {
2626
Sh.Arr[1] = D;
2727
}
2828

29+
// CHECK-LABEL: <4 x i32> @"\01??$tfoo@V?$vector@H$03@@@@YA?AV?$vector@H$03@@AGAV0@@Z"(<4 x i32> addrspace(3)* dereferenceable(16) %a)
30+
// CHECK: [[B:%.*]] = load <4 x i32>, <4 x i32> addrspace(3)* %a, align 4
31+
// CHECK: ret <4 x i32> [[B]]
32+
template<typename T>
33+
T tfoo(groupshared T a) {
34+
return a;
35+
}
36+
37+
groupshared int4 SharedData1;
38+
2939
[numthreads(4, 1, 1)]
3040
void main(uint3 TID : SV_GroupThreadID) {
3141
fn1(SharedData);
42+
tfoo<int4>(SharedData1);
43+
tfoo(SharedData1);
3244
}

tools/clang/test/SemaHLSL/v202x/groupshared/ExplicitCast.hlsl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ void fn1(groupshared half Sh) {
77
Sh = 5;
88
}
99

10+
template<typename T>
11+
T fnT(groupshared T A) {
12+
// expected-note@-1{{candidate function [with T = half] not viable: 1st argument ('half') is in address space 0, but parameter must be in address space 3}}
13+
return A;
14+
}
15+
1016
void fn2() {
1117
fn1((half)SharedData);
1218
// expected-error@-1{{no matching function for call to 'fn1'}}
19+
// not sure why someone would write this but make sure templates do something sane.
20+
fnT<half>((half)SharedData);
21+
// expected-error@-1{{no matching function for call to 'fnT'}}
1322
}

tools/clang/test/SemaHLSL/v202x/groupshared/ExportNoInlineTest.hlsl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,14 @@ export void fn1(groupshared uint Sh) {
99
// expected-error@-1{{groupshared and export/noinline cannot be used together for a parameter}}
1010
Sh = 6;
1111
}
12+
13+
template<typename T>
14+
void fn3(groupshared T A, groupshared T B) {
15+
A = B;
16+
}
17+
18+
export template void fn3<uint>(groupshared uint A, groupshared uint B);
19+
// expected-error@-1{{'template' is a reserved keyword in HLSL}}
20+
template [noinline] void fn3<float>(groupshared float A, groupshared float B);
21+
// expected-error@-1{{use of undeclared identifier 'noinline'}}
22+
// expected-error@-2{{expected unqualified-id}}

tools/clang/test/SemaHLSL/v202x/groupshared/InOut.hlsl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ void fn8() {
2323
fn1(SharedData);
2424
// expected-warning@-1{{Passing groupshared variable to a parameter annotated with inout. See 'groupshared' parameter annotation added in 202x}}
2525
}
26+
27+
template<typename T>
28+
T fn9(inout groupshared T A) {
29+
// expected-error@-1{{'inout' and 'groupshared' cannot be used together for a parameter}}
30+
return A;
31+
}
32+
template<typename T>
33+
T fn10(in groupshared T A) {
34+
// expected-error@-1{{'in' and 'groupshared' cannot be used together for a parameter}}
35+
return A;
36+
}
37+
template<typename T>
38+
T fn11(out groupshared T A) {
39+
// expected-error@-1{{'out' and 'groupshared' cannot be used together for a parameter}}
40+
return A;
41+
}

tools/clang/test/SemaHLSL/v202x/groupshared/NotGroupSharedTest.hlsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ void fn1(groupshared half Sh) {
55
Sh = 5;
66
}
77

8+
template<typename T>
9+
T fn2(groupshared T Sh) {
10+
// expected-note@-1{{candidate template ignored: can't deduce a type for 'T' that would make '__attribute__((address_space(3))) T' equal 'half'}}
11+
return Sh;
12+
}
13+
814
[numthreads(4, 1, 1)]
915
void main(uint3 TID : SV_GroupThreadID) {
1016
half tmp = 1.0;
1117
fn1(tmp);
1218
// expected-error@-1{{no matching function for call to 'fn1'}}
19+
fn2(tmp);
20+
// expected-error@-1{{no matching function for call to 'fn2'}}
1321
}

tools/clang/test/SemaHLSL/v202x/groupshared/Pre202xWarning.hlsl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ void fn1(groupshared uint Sh) {
1010
void fn2() {
1111
fn1(SharedData);
1212
}
13+
14+
template<typename T>
15+
T fn3(groupshared T A) {
16+
// expected-warning@-1{{Support for groupshared parameter annotation not added until HLSL 202x}}
17+
return A;
18+
}

tools/clang/test/SemaHLSL/v202x/groupshared/ScalarTest.hlsl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ void fn1(groupshared half Sh) {
77
Sh = 5;
88
}
99

10+
template<typename T>
11+
T fn3(groupshared T A) {
12+
// expected-note@-1{{candidate function [with T = half] not viable: no known conversion from '__attribute__((address_space(3))) uint16_t' to '__attribute__((address_space(3))) half' for 1st argument}}
13+
return A;
14+
}
15+
1016
void fn2() {
1117
fn1(SharedData);
1218
// expected-error@-1{{no matching function for call to 'fn1'}}
19+
fn3<half>(SharedData);
20+
// expected-error@-1{{no matching function for call to 'fn3'}}
1321
}

0 commit comments

Comments
 (0)