Skip to content

Commit 9975a80

Browse files
authored
Enable template dependent globallycoherent types (microsoft#4718)
This change alters how globallycoherent attributes are validated. We will need similar changes for other HLSL type attributes as well. Without this change, globallycoherent attributes are only allowed to apply to declarations of builtin objects during parsing. Additionally if applied to a non-UAV we catch that late (during code generation) and report a different error there. This patch reworks all of that. Instead, during parsing we allow verify the attribute is applying to a UAV builtin type and issue a diagnostic if applied to a non-UAV type. We also allow the attribute to be applied to any dependent type. If the attribute is applied to a dependent type, we verify that the dependent type is resolved to a valid UAV type during instantation. This change also reworks the existing test case for globallycoherent diagnostics and deletes duplicated tests that were not adding meanigful value. Fixes 4583.
1 parent 1e98bf3 commit 9975a80

File tree

14 files changed

+78
-113
lines changed

14 files changed

+78
-113
lines changed

tools/clang/include/clang/Sema/Sema.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3793,6 +3793,7 @@ class Sema {
37933793
// HLSL Change Begins
37943794
bool CheckHLSLUnaryExprOrTypeTraitOperand(QualType ExprType, SourceLocation Loc,
37953795
UnaryExprOrTypeTrait ExprKind);
3796+
void DiagnoseHLSLDeclAttr(const Decl *D, const Attr *A);
37963797
// HLSL Change Ends
37973798

37983799
bool CheckUnaryExprOrTypeTraitOperand(Expr *E, UnaryExprOrTypeTrait ExprKind);

tools/clang/lib/CodeGen/CGHLSLMS.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,15 +3312,6 @@ bool CGMSHLSLRuntime::SetUAVSRV(SourceLocation loc,
33123312
hlslRes->SetGloballyCoherent(true);
33133313
}
33143314
if (resClass == hlsl::DxilResourceBase::Class::SRV) {
3315-
if (hlslRes->IsGloballyCoherent()) {
3316-
DiagnosticsEngine &Diags = CGM.getDiags();
3317-
unsigned DiagID = Diags.getCustomDiagID(
3318-
DiagnosticsEngine::Error, "globallycoherent can only be used with "
3319-
"Unordered Access View buffers.");
3320-
Diags.Report(loc, DiagID);
3321-
return false;
3322-
}
3323-
33243315
hlslRes->SetRW(false);
33253316
hlslRes->SetID(m_pHLModule->GetSRVs().size());
33263317
} else {

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// //
1111
///////////////////////////////////////////////////////////////////////////////
1212

13+
#include "clang/AST/DeclBase.h"
1314
#include "clang/Basic/Diagnostic.h"
1415
#include "llvm/ADT/SmallPtrSet.h"
1516
#include "llvm/ADT/DenseMap.h"
@@ -12131,6 +12132,26 @@ bool ValidateAttributeTargetIsFunction(Sema& S, Decl* D, const AttributeList &A)
1213112132
return false;
1213212133
}
1213312134

12135+
void Sema::DiagnoseHLSLDeclAttr(const Decl *D, const Attr *A) {
12136+
HLSLExternalSource *ExtSource = HLSLExternalSource::FromSema(this);
12137+
if (const HLSLGloballyCoherentAttr *HLSLGCAttr =
12138+
dyn_cast<HLSLGloballyCoherentAttr>(A)) {
12139+
const ValueDecl *TD = cast<ValueDecl>(D);
12140+
if (!TD->getType()->isDependentType()) {
12141+
QualType DeclType = TD->getType();
12142+
while (DeclType->isArrayType())
12143+
DeclType = QualType(DeclType->getArrayElementTypeNoTypeQual(), 0);
12144+
if (ExtSource->GetTypeObjectKind(DeclType) != AR_TOBJ_OBJECT ||
12145+
hlsl::GetResourceClassForType(getASTContext(), DeclType) !=
12146+
hlsl::DXIL::ResourceClass::UAV) {
12147+
Diag(A->getLocation(), diag::err_hlsl_varmodifierna)
12148+
<< A << "non-UAV type";
12149+
}
12150+
}
12151+
return;
12152+
}
12153+
}
12154+
1213412155
void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A, bool& Handled)
1213512156
{
1213612157
DXASSERT_NOMSG(D != nullptr);
@@ -12296,6 +12317,7 @@ void hlsl::HandleDeclAttributeForHLSL(Sema &S, Decl *D, const AttributeList &A,
1229612317

1229712318
if (declAttr != nullptr)
1229812319
{
12320+
S.DiagnoseHLSLDeclAttr(D, declAttr);
1229912321
DXASSERT_NOMSG(Handled);
1230012322
D->addAttr(declAttr);
1230112323
return;
@@ -13127,12 +13149,7 @@ bool Sema::DiagnoseHLSLDecl(Declarator &D, DeclContext *DC, Expr *BitWidth,
1312713149
result = false;
1312813150
}
1312913151
break;
13130-
case AttributeList::AT_HLSLGloballyCoherent:
13131-
if (!bIsObject) {
13132-
Diag(pAttr->getLoc(), diag::err_hlsl_varmodifierna)
13133-
<< pAttr->getName() << "non-UAV type";
13134-
result = false;
13135-
}
13152+
case AttributeList::AT_HLSLGloballyCoherent: // Handled elsewhere
1313613153
break;
1313713154
case AttributeList::AT_HLSLUniform:
1313813155
if (!(isGlobal || isParameter)) {

tools/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "clang/AST/TypeLoc.h"
2323
#include "clang/Sema/Lookup.h"
2424
#include "clang/Sema/PrettyDeclStackTrace.h"
25+
#include "clang/Sema/SemaHLSL.h" // HLSL Change
2526
#include "clang/Sema/Template.h"
2627

2728
using namespace clang;
@@ -266,6 +267,10 @@ void Sema::InstantiateAttrs(const MultiLevelTemplateArgumentList &TemplateArgs,
266267
continue;
267268
}
268269

270+
// HLSL Change Begin - Validate post-instantiation attributes
271+
DiagnoseHLSLDeclAttr(New, TmplAttr);
272+
// HLSL Change End
273+
269274
// Existing DLL attribute on the instantiation takes precedence.
270275
if (TmplAttr->getKind() == attr::DLLExport ||
271276
TmplAttr->getKind() == attr::DLLImport) {

tools/clang/lib/Sema/SemaType.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5797,11 +5797,6 @@ static bool handleHLSLTypeAttr(TypeProcessingState &State,
57975797
!hlsl::GetOriginalElementType(&S, Type)->isFloatingType()) {
57985798
S.Diag(Attr.getLoc(), diag::err_hlsl_norm_float_only) << Attr.getRange();
57995799
return true;
5800-
} else if (Kind == AttributeList::AT_HLSLGloballyCoherent &&
5801-
!hlsl::IsObjectType(&S, Type)) {
5802-
S.Diag(Attr.getLoc(), diag::err_hlsl_varmodifierna) <<
5803-
Attr.getName() << "non-UAV type";
5804-
return true;
58055800
}
58065801

58075802
const AttributedType *pMatrixOrientation = nullptr;

tools/clang/test/CodeGenHLSL/globallycoherent2.hlsl

Lines changed: 0 additions & 17 deletions
This file was deleted.

tools/clang/test/CodeGenHLSL/globallycoherent3.hlsl

Lines changed: 0 additions & 16 deletions
This file was deleted.

tools/clang/test/DXILValidation/globallycoherent2.hlsl

Lines changed: 0 additions & 17 deletions
This file was deleted.

tools/clang/test/DXILValidation/globallycoherent3.hlsl

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_cc1 -fsyntax-only -ffreestanding -verify %s
2+
3+
globallycoherent RWTexture1D<float4> uav1 : register(u3);
4+
RWBuffer<float4> uav2;
5+
globallycoherent Buffer<float4> srv; // expected-error {{'globallycoherent' is not a valid modifier for a non-UAV type}}
6+
globallycoherent float m; // expected-error {{'globallycoherent' is not a valid modifier for a non-UAV type}}
7+
8+
globallycoherent RWTexture2D<float> tex[12];
9+
globallycoherent RWTexture2D<float> texMD[12][12];
10+
11+
float4 main(uint2 a : A, uint2 b : B) : SV_Target
12+
{
13+
globallycoherent RWTexture1D<float4> uav3 = uav1;
14+
globallycoherent float x = 3; // expected-error {{'globallycoherent' is not a valid modifier for a non-UAV type}}
15+
uav3[0] = srv[0];
16+
uav1[0] = 2;
17+
uav2[1] = 3;
18+
return 0;
19+
}

0 commit comments

Comments
 (0)