Skip to content

Commit c651610

Browse files
committed
[ser] Declare HitObject type regardless of SM, use availability attributes to check SM version for SER
1 parent acba5c7 commit c651610

11 files changed

Lines changed: 49 additions & 125 deletions

tools/clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7662,8 +7662,8 @@ def warn_hlsl_rayquery_flags_conflict : Warning<
76627662
" must have RAYQUERY_FLAG_ALLOW_OPACITY_MICROMAPS set.">, DefaultError, InGroup<HLSLRayQueryFlags>;
76637663
def err_hlsl_unsupported_builtin_op: Error<
76647664
"operator cannot be used with built-in type %0">;
7665-
def warn_hlsl_builtin_constant_unavailable: Warning<
7666-
"potential misuse of built-in constant %0 in shader model %1; introduced"
7665+
def warn_hlsl_builtin_unavailable: Warning<
7666+
"potential misuse of built-in %select{constant|function}3 '%0' in shader model %1; introduced"
76677667
" in shader model %2">, InGroup<HLSLAvailabilityConstant>;
76687668
def err_hlsl_unsupported_char_literal : Error<
76697669
"unsupported style of char literal - use a single-character char-based literal">;

tools/clang/lib/AST/ASTContextHLSL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,10 @@ CXXRecordDecl *hlsl::DeclareHitObjectType(NamespaceDecl &NSDecl) {
12431243
static_cast<int>(hlsl::IntrinsicOp::MOP_DxHitObject_MakeNop)));
12441244
pConstructorDecl->addAttr(HLSLCXXOverloadAttr::CreateImplicit(Context));
12451245

1246+
// Shader Execution Reordering requires SM6.9
1247+
VersionTuple VT69 = VersionTuple(6, 9);
1248+
pConstructorDecl->addAttr(ConstructAvailabilityAttribute(Context, VT69));
1249+
12461250
// Add the implicit HLSLHitObjectAttr attribute to unambiguously recognize the
12471251
// builtin HitObject type (SM6.9+). This distinguishes it from any
12481252
// user-defined type named 'HitObject' pre SM6.9.

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,15 +1941,14 @@ static void AddHLSLIntrinsicAttr(FunctionDecl *FD, ASTContext &context,
19411941
FD->addAttr(PureAttr::CreateImplicit(context));
19421942
if (pIntrinsic->Flags & INTRIN_FLAG_IS_WAVE)
19431943
FD->addAttr(HLSLWaveSensitiveAttr::CreateImplicit(context));
1944-
// TBD: Add availability attribute if MinShaderModel is set.
1945-
// if (pIntrinsic->MinShaderModel) {
1946-
// unsigned Major = pIntrinsic->MinShaderModel >> 4;
1947-
// unsigned Minor = pIntrinsic->MinShaderModel & 0xF;
1948-
// FD->addAttr(AvailabilityAttr::CreateImplicit(
1949-
// context, &context.Idents.get(""), clang::VersionTuple(Major, Minor),
1950-
// clang::VersionTuple(), clang::VersionTuple(), false,
1951-
// "HLSL Intrinsic availability limited by shader model."));
1952-
//}
1944+
if (pIntrinsic->MinShaderModel) {
1945+
unsigned Major = pIntrinsic->MinShaderModel >> 4;
1946+
unsigned Minor = pIntrinsic->MinShaderModel & 0xF;
1947+
FD->addAttr(AvailabilityAttr::CreateImplicit(
1948+
context, &context.Idents.get(""), clang::VersionTuple(Major, Minor),
1949+
clang::VersionTuple(), clang::VersionTuple(), false,
1950+
"HLSL Intrinsic availability limited by shader model."));
1951+
}
19531952
}
19541953

19551954
static FunctionDecl *
@@ -3781,10 +3780,8 @@ class HLSLExternalSource : public ExternalSemaSource {
37813780
recordDecl = DeclareRayQueryType(*m_context);
37823781
} else if (kind == AR_OBJECT_HIT_OBJECT) {
37833782
// Declare 'HitObject' in '::dx' extension namespace.
3784-
if (SM->IsSM69Plus()) {
3785-
DXASSERT(m_dxNSDecl, "namespace ::dx must be declared in SM6.9+");
3786-
recordDecl = DeclareHitObjectType(*m_dxNSDecl);
3787-
}
3783+
DXASSERT(m_dxNSDecl, "namespace ::dx must be declared in SM6.9+");
3784+
recordDecl = DeclareHitObjectType(*m_dxNSDecl);
37883785
} else if (kind == AR_OBJECT_HEAP_RESOURCE) {
37893786
recordDecl = DeclareResourceType(*m_context, /*bSampler*/ false);
37903787
if (SM->IsSM66Plus()) {
@@ -4063,8 +4060,6 @@ class HLSLExternalSource : public ExternalSemaSource {
40634060
S.addExternalSource(this);
40644061

40654062
// Namespace ::dx only introduced with SM6.9
4066-
const auto *SM =
4067-
hlsl::ShaderModel::GetByName(m_sema->getLangOpts().HLSLProfile.c_str());
40684063
m_dxNSDecl =
40694064
NamespaceDecl::Create(context, context.getTranslationUnitDecl(),
40704065
/*Inline*/ false, SourceLocation(),
@@ -4091,8 +4086,7 @@ class HLSLExternalSource : public ExternalSemaSource {
40914086
AddIntrinsicTableMethods(intrinsic);
40924087
}
40934088

4094-
if (SM->IsSM69Plus())
4095-
AddDxIntrinsicFunctions();
4089+
AddDxIntrinsicFunctions();
40964090

40974091
#ifdef ENABLE_SPIRV_CODEGEN
40984092
if (m_sema->getLangOpts().SPIRV) {
@@ -5083,17 +5077,6 @@ class HLSLExternalSource : public ExternalSemaSource {
50835077
nameIdentifier, argumentCount));
50845078
}
50855079

5086-
bool IsEnabledIntrinsic(const HLSL_INTRINSIC *pIntrinsic,
5087-
const ShaderModel &SM) {
5088-
switch ((IntrinsicOp)pIntrinsic->Op) {
5089-
default:
5090-
return true;
5091-
case IntrinsicOp::MOP_DxHitObject_MakeNop:
5092-
case IntrinsicOp::IOP_DxMaybeReorderThread:
5093-
return SM.IsSM69Plus();
5094-
}
5095-
}
5096-
50975080
bool AddOverloadedCallCandidates(UnresolvedLookupExpr *ULE,
50985081
ArrayRef<Expr *> Args,
50995082
OverloadCandidateSet &CandidateSet,
@@ -5145,9 +5128,6 @@ class HLSLExternalSource : public ExternalSemaSource {
51455128
}
51465129
#endif // ENABLE_SPIRV_CODEGEN
51475130

5148-
const auto *SM =
5149-
hlsl::ShaderModel::GetByName(m_sema->getLangOpts().HLSLProfile.c_str());
5150-
51515131
IntrinsicDefIter cursor = FindIntrinsicByNameAndArgCount(
51525132
table, tableCount, StringRef(), nameIdentifier, Args.size());
51535133
IntrinsicDefIter end = IntrinsicDefIter::CreateEnd(
@@ -5156,13 +5136,6 @@ class HLSLExternalSource : public ExternalSemaSource {
51565136
for (; cursor != end; ++cursor) {
51575137
const HLSL_INTRINSIC *pIntrinsic = *cursor;
51585138

5159-
// Check whether this extension intrinsic is available in the current
5160-
// configuration. Note that this not necessary for member functions:
5161-
// if the builtin type is never declared, neither will any type refer
5162-
// to the member intrinsics table.
5163-
if (!IsEnabledIntrinsic(*cursor, *SM))
5164-
continue;
5165-
51665139
// If this is the intrinsic we're interested in, build up a representation
51675140
// of the types we need.
51685141
LPCSTR tableName = cursor.GetTableName();
@@ -11935,11 +11908,6 @@ void Sema::DiagnoseReachableCallForSER(CallExpr *CE, DXIL::ShaderKind EntrySK,
1193511908
<< DiagValidShaderKinds;
1193611909
Diag(EntryFD->getLocation(), diag::note_hlsl_entry_defined_here);
1193711910
}
11938-
11939-
if (!SM->IsSM69Plus()) {
11940-
// Legal entry, but version not supported
11941-
Diag(Loc, diag::err_hlsl_ser_invalid_version) << SM->GetName();
11942-
}
1194311911
}
1194411912

1194511913
// Check HLSL member call constraints for used functions.

tools/clang/lib/Sema/SemaHLSLDiagnoseTU.cpp

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/MapVector.h"
2525
#include "llvm/ADT/SmallPtrSet.h"
2626
#include "llvm/Support/Debug.h"
27+
#include "llvm/Support/raw_ostream.h"
2728
#include <optional>
2829

2930
using namespace clang;
@@ -432,13 +433,27 @@ class HLSLCallDiagnoseVisitor // Could rename to HLSLReachableDiagnoseVisitor
432433
// if the current shader model is lower than what
433434
// is stated in the availability attribute, emit
434435
// the availability warning.
435-
436-
if (SMVT < AAttrVT) {
437-
// TBD: Determine best way to distinguish between builtin constant decls
438-
// and other decls.
439-
sema->Diag(Loc, diag::warn_hlsl_builtin_constant_unavailable)
440-
<< ND << SM->GetName() << AAttrVT.getAsString();
436+
if (SMVT >= AAttrVT)
437+
return;
438+
439+
// TBD: Determine best way to distinguish between builtin constant decls
440+
// and other decls.
441+
enum class DiagKind { Constant = 0, Function = 1 };
442+
DiagKind Kind;
443+
switch (ND->getKind()) {
444+
case Decl::Function:
445+
case Decl::FunctionTemplate:
446+
case Decl::CXXMethod:
447+
Kind = DiagKind::Function;
448+
break;
449+
default:
450+
Kind = DiagKind::Constant;
451+
break;
441452
}
453+
454+
sema->Diag(Loc, diag::warn_hlsl_builtin_unavailable)
455+
<< ND->getQualifiedNameAsString() << SM->GetName()
456+
<< AAttrVT.getAsString() << (int)Kind;
442457
}
443458

444459
clang::Sema *getSema() { return sema; }
@@ -453,29 +468,6 @@ class HLSLCallDiagnoseVisitor // Could rename to HLSLReachableDiagnoseVisitor
453468
llvm::SmallPtrSetImpl<DeclRefExpr *> &DeclAvailabilityChecked;
454469
};
455470

456-
class HLSLDisallowSERDiagnoseVisitor
457-
: public RecursiveASTVisitor<HLSLDisallowSERDiagnoseVisitor> {
458-
public:
459-
explicit HLSLDisallowSERDiagnoseVisitor(Sema &S, DXIL::ShaderKind EntrySK,
460-
const FunctionDecl *EntryDecl)
461-
: S(S), EntrySK(EntrySK), EntryDecl(EntryDecl) {}
462-
463-
bool VisitTypeLoc(TypeLoc TL) {
464-
if (!hlsl::IsHLSLHitObjectType(TL.getType()))
465-
return true;
466-
467-
S.Diag(TL.getLocStart(), diag::err_hlsl_ser_unsupported)
468-
<< 0 << ShaderModel::FullNameFromKind(EntrySK) << 1;
469-
S.Diag(EntryDecl->getLocation(), diag::note_hlsl_entry_defined_here);
470-
return false;
471-
}
472-
473-
private:
474-
clang::Sema &S;
475-
DXIL::ShaderKind EntrySK;
476-
const FunctionDecl *EntryDecl;
477-
};
478-
479471
std::optional<uint32_t>
480472
getFunctionInputPatchCount(const FunctionDecl *function) {
481473
for (const auto *param : function->params()) {
@@ -697,18 +689,6 @@ void hlsl::DiagnoseTranslationUnit(clang::Sema *self) {
697689
}
698690
}
699691

700-
// Shader Execution Reordering
701-
// MaybeReorderThread(..) handled elsewhere.
702-
const bool AllowHitObject = (EntrySK == DXIL::ShaderKind::ClosestHit ||
703-
EntrySK == DXIL::ShaderKind::Miss ||
704-
EntrySK == DXIL::ShaderKind::RayGeneration);
705-
if (!AllowHitObject) {
706-
HLSLDisallowSERDiagnoseVisitor Visitor(*self, EntrySK, FDecl);
707-
for (FunctionDecl *FD : callGraph.GetVisitedFunctions()) {
708-
Visitor.TraverseDecl(FD);
709-
}
710-
}
711-
712692
// Visit all visited functions in call graph to collect illegal intrinsic
713693
// calls.
714694
HLSLCallDiagnoseVisitor Visitor(self, shaderModel, EntrySK, NodeLaunchTy,

tools/clang/test/SemaHLSL/hlsl/intrinsics/reorder/reorder-defineable-pre-sm69.hlsl renamed to tools/clang/test/SemaHLSL/hlsl/intrinsics/reorder/reorder-unavailable-pre-sm69.hlsl

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
// RUN: %dxc -T lib_6_6 %s -verify
21
// RUN: %dxc -T lib_6_8 %s -verify
32

43
// Check that intrinsic names of Shader Execution Reordering are unclaimed pre SM 6.9.
5-
// expected-no-diagnostics
6-
7-
namespace dx {
8-
void MaybeReorderThread(uint CoherenceHint, uint NumCoherenceHintBitsFromLSB) {
9-
}
10-
}
114

125
[shader("raygeneration")]
136
void main() {
7+
// expected-warning@+1{{potential misuse of built-in function 'dx::MaybeReorderThread' in shader model lib_6_8; introduced in shader model 6.9}}
148
dx::MaybeReorderThread(15u, 4u);
159
}

tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-entry-errors.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ struct [raypayload] Payload
99

1010
struct Attribs { float2 barys; };
1111

12-
void UseHitObject() {
13-
dx::HitObject hit;
12+
dx::HitObject UseHitObject() {
13+
return dx::HitObject::MakeNop();
1414
}
1515

1616
// expected-note@+3{{entry function defined here}}

tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-target-profile-error.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace dx {}
44

55
[shader("raygeneration")]
66
void main() {
7-
// expected-error@+1{{no type named 'HitObject' in namespace 'dx'}}
8-
dx::HitObject hit;
7+
// expected-warning@+1{{potential misuse of built-in function 'dx::HitObject::MakeNop' in shader model lib_6_8; introduced in shader model 6.9}}
8+
dx::HitObject::MakeNop();
99
}

tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-defineable-pre-sm69.hlsl renamed to tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-unavailable-pre-sm69.hlsl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
// RUN: %dxc -T lib_6_8 %s -verify
22

33
// Check that the HitObject type name of Shader Execution Reordering is unclaimed pre SM 6.9.
4-
// expected-no-diagnostics
5-
6-
namespace dx {
7-
struct HitObject {
8-
int notTheSM69HitObject;
9-
static HitObject MakeNop() {
10-
HitObject hit;
11-
hit.notTheSM69HitObject = 1;
12-
return hit;
13-
}
14-
};
15-
}
164

175
[shader("raygeneration")]
186
void main() {
7+
// expected-warning@+1{{potential misuse of built-in function 'dx::HitObject::MakeNop' in shader model lib_6_8; introduced in shader model 6.9}}
198
dx::HitObject hit = dx::HitObject::MakeNop();
209
}

tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-undeclared-pre-sm69.hlsl

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

tools/clang/test/SemaHLSL/hlsl/objects/HitObject/hitobject-unsupported-vs.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
// expected-note@+1{{entry function defined here}}
44
float main(RayDesc rayDesc: RAYDESC) : OUT {
55
// expected-error@+1{{dx::HitObject is unavailable in shader stage 'vertex' (requires 'raygeneration', 'closesthit' or 'miss')}}
6-
dx::HitObject hit;
6+
dx::HitObject::MakeNop();
77
return 0.f;
88
}

0 commit comments

Comments
 (0)