Skip to content

Commit c5f62d9

Browse files
authored
[SER] Patch 1: HitObject type lowering and SM 6.9 enablement (microsoft#7097)
Reduction of the complete SER implementation to just the HitObject type and its default constructor. This has most of the infrastructure changes in DXC to support SER, eg static member functions for builtins, HitObject scalar type. Specification PR: microsoft/hlsl-specs#277
1 parent 9a06f4d commit c5f62d9

37 files changed

+1174
-158
lines changed

include/dxc/DXIL/DxilUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ GetHLSLResourceProperties(llvm::Type *Ty);
162162
bool IsHLSLResourceType(llvm::Type *Ty);
163163
bool IsHLSLObjectType(llvm::Type *Ty);
164164
bool IsHLSLRayQueryType(llvm::Type *Ty);
165+
llvm::Type *GetHLSLHitObjectType(llvm::Module *M);
166+
bool IsHLSLHitObjectType(llvm::Type *Ty);
165167
bool IsHLSLResourceDescType(llvm::Type *Ty);
166168
bool IsResourceSingleComponent(llvm::Type *Ty);
167169
uint8_t GetResourceComponentCount(llvm::Type *Ty);

include/dxc/HlslIntrinsicOp.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ enum class IntrinsicOp {
333333
MOP_TraceRayInline = 325,
334334
MOP_WorldRayDirection = 326,
335335
MOP_WorldRayOrigin = 327,
336+
MOP_DxHitObject_MakeNop = 358,
337+
IOP_DxMaybeReorderThread = 359,
336338
MOP_Count = 328,
337339
MOP_FinishedCrossGroupSharing = 329,
338340
MOP_GetGroupNodeOutputRecords = 330,
@@ -364,7 +366,7 @@ enum class IntrinsicOp {
364366
IOP_usign = 355,
365367
MOP_InterlockedUMax = 356,
366368
MOP_InterlockedUMin = 357,
367-
Num_Intrinsics = 358,
369+
Num_Intrinsics = 360,
368370
};
369371
inline bool HasUnsignedIntrinsicOpcode(IntrinsicOp opcode) {
370372
switch (opcode) {

include/dxc/dxcapi.internal.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ enum LEGAL_INTRINSIC_COMPTYPES {
126126
LICOMPTYPE_GROUP_NODE_OUTPUT_RECORDS = 49,
127127
LICOMPTYPE_THREAD_NODE_OUTPUT_RECORDS = 50,
128128

129-
LICOMPTYPE_COUNT = 51
129+
LICOMPTYPE_HIT_OBJECT = 51,
130+
131+
LICOMPTYPE_COUNT = 52
130132
};
131133

132134
static const BYTE IA_SPECIAL_BASE = 0xf0;
@@ -164,6 +166,7 @@ struct HLSL_INTRINSIC_ARGUMENT {
164166
static const UINT INTRIN_FLAG_READ_ONLY = 1U << 0;
165167
static const UINT INTRIN_FLAG_READ_NONE = 1U << 1;
166168
static const UINT INTRIN_FLAG_IS_WAVE = 1U << 2;
169+
static const UINT INTRIN_FLAG_STATIC_MEMBER = 1U << 3;
167170

168171
struct HLSL_INTRINSIC {
169172
UINT Op; // Intrinsic Op ID

lib/DXIL/DxilUtil.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,9 @@ bool IsHLSLObjectType(llvm::Type *Ty) {
574574

575575
if (IsHLSLNodeIOType(Ty))
576576
return true;
577+
578+
if (IsHLSLHitObjectType(Ty))
579+
return true;
577580
}
578581
return false;
579582
}
@@ -591,6 +594,24 @@ bool IsHLSLRayQueryType(llvm::Type *Ty) {
591594
return false;
592595
}
593596

597+
llvm::Type *GetHLSLHitObjectType(llvm::Module *M) {
598+
using namespace llvm;
599+
StructType *HitObjectTy = M->getTypeByName("dx.types.HitObject");
600+
if (!HitObjectTy)
601+
HitObjectTy = StructType::create({Type::getInt8PtrTy(M->getContext(), 0)},
602+
"dx.types.HitObject", false);
603+
return HitObjectTy;
604+
}
605+
606+
bool IsHLSLHitObjectType(llvm::Type *Ty) {
607+
llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty);
608+
if (!ST)
609+
return false;
610+
if (!ST->hasName())
611+
return false;
612+
return ST->getName() == "dx.types.HitObject";
613+
}
614+
594615
bool IsHLSLResourceDescType(llvm::Type *Ty) {
595616
if (llvm::StructType *ST = dyn_cast<llvm::StructType>(Ty)) {
596617
if (!ST->hasName())

lib/HLSL/HLOperationLower.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6062,6 +6062,24 @@ Value *TranslateUnpack(CallInst *CI, IntrinsicOp IOP, OP::OpCode opcode,
60626062

60636063
} // namespace
60646064

6065+
// Shader Execution Reordering.
6066+
namespace {
6067+
Value *TranslateHitObjectMake(CallInst *CI, IntrinsicOp IOP, OP::OpCode opcode,
6068+
HLOperationLowerHelper &helper,
6069+
HLObjectOperationLowerHelper *pObjHelper,
6070+
bool &Translated) {
6071+
return UndefValue::get(CI->getType()); // TODO: Merge SER DXIL patches
6072+
}
6073+
6074+
Value *TranslateMaybeReorderThread(CallInst *CI, IntrinsicOp IOP,
6075+
OP::OpCode opcode,
6076+
HLOperationLowerHelper &helper,
6077+
HLObjectOperationLowerHelper *pObjHelper,
6078+
bool &Translated) {
6079+
return nullptr; // TODO: Merge SER DXIL patches
6080+
}
6081+
} // namespace
6082+
60656083
// Resource Handle.
60666084
namespace {
60676085
Value *TranslateGetHandleFromHeap(CallInst *CI, IntrinsicOp IOP,
@@ -6794,6 +6812,12 @@ IntrinsicLower gLowerTable[] = {
67946812
DXIL::OpCode::NumOpCodes},
67956813
{IntrinsicOp::MOP_InterlockedUMin, TranslateMopAtomicBinaryOperation,
67966814
DXIL::OpCode::NumOpCodes},
6815+
{IntrinsicOp::MOP_DxHitObject_MakeNop, TranslateHitObjectMake,
6816+
DXIL::OpCode::NumOpCodes_Dxil_1_8}, // FIXME: Just a placeholder Dxil
6817+
// opcode
6818+
{IntrinsicOp::IOP_DxMaybeReorderThread, TranslateMaybeReorderThread,
6819+
DXIL::OpCode::NumOpCodes_Dxil_1_8}, // FIXME: Just a placeholder Dxil
6820+
// opcode
67976821
};
67986822
} // namespace
67996823
static_assert(

tools/clang/include/clang/AST/HlslTypes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ clang::CXXRecordDecl *
391391
DeclareConstantBufferViewType(clang::ASTContext &context,
392392
clang::InheritableAttr *Attr);
393393
clang::CXXRecordDecl *DeclareRayQueryType(clang::ASTContext &context);
394+
clang::CXXRecordDecl *DeclareHitObjectType(clang::NamespaceDecl &NSDecl);
394395
clang::CXXRecordDecl *DeclareResourceType(clang::ASTContext &context,
395396
bool bSampler);
396397

@@ -472,6 +473,7 @@ bool IsHLSLNodeInputType(clang::QualType type);
472473
bool IsHLSLDynamicResourceType(clang::QualType type);
473474
bool IsHLSLDynamicSamplerType(clang::QualType type);
474475
bool IsHLSLNodeType(clang::QualType type);
476+
bool IsHLSLHitObjectType(clang::QualType type);
475477

476478
bool IsHLSLObjectWithImplicitMemberAccess(clang::QualType type);
477479
bool IsHLSLObjectWithImplicitROMemberAccess(clang::QualType type);
@@ -545,6 +547,7 @@ clang::CXXMethodDecl *CreateObjectFunctionDeclarationWithParams(
545547
clang::QualType resultType, llvm::ArrayRef<clang::QualType> paramTypes,
546548
llvm::ArrayRef<clang::StringRef> paramNames,
547549
clang::DeclarationName declarationName, bool isConst,
550+
clang::StorageClass SC = clang::StorageClass::SC_None,
548551
bool isTemplateFunction = false);
549552

550553
DXIL::ResourceClass GetResourceClassForType(const clang::ASTContext &context,

tools/clang/include/clang/Basic/Attr.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,14 @@ def HLSLRayQueryObject : InheritableAttr {
11571157
let Documentation = [Undocumented];
11581158
}
11591159

1160+
// HLSL HitObject Attribute
1161+
1162+
def HLSLHitObject : InheritableAttr {
1163+
let Spellings = []; // No spellings!
1164+
let Subjects = SubjectList<[CXXRecord]>;
1165+
let Documentation = [Undocumented];
1166+
}
1167+
11601168
// HLSL Parameter Attributes
11611169

11621170
def HLSLMaxRecords : InheritableAttr {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7665,6 +7665,9 @@ def err_hlsl_unsupported_builtin_op: Error<
76657665
def warn_hlsl_builtin_constant_unavailable: Warning<
76667666
"potential misuse of built-in constant %0 in shader model %1; introduced"
76677667
" in shader model %2">, InGroup<HLSLAvailabilityConstant>;
7668+
def warn_hlsl_builtin_type_unavailable: Warning<
7669+
"potential misuse of built-in type %0 in shader model %1; introduced"
7670+
" in shader model %2">, DefaultError, InGroup<HLSLAvailability>;
76687671
def err_hlsl_unsupported_char_literal : Error<
76697672
"unsupported style of char literal - use a single-character char-based literal">;
76707673
def err_hlsl_unsupported_clipplane_argument_expression : Error<
@@ -7991,6 +7994,12 @@ def warn_hlsl_legacy_integer_literal_signedness: Warning<
79917994
InGroup<HLSLLegacyLiterals>, DefaultIgnore;
79927995
def err_hlsl_unsupported_semantic_index: Error<
79937996
"'%0' is defined with semantic index %1, but only values 0 through %2 are supported">;
7997+
7998+
// Shader Execution Reordering
7999+
def err_hlsl_reorder_unsupported_stage : Error<
8000+
"dx::MaybeReorderThread is unavailable in shader stage '%0' (requires 'raygeneration')">;
8001+
def err_hlsl_hitobject_unsupported_stage : Error<
8002+
"dx::HitObject is unavailable in shader stage '%0' (requires 'raygeneration', 'closesthit' or 'miss')">;
79948003
// HLSL Change Ends
79958004

79968005
// SPIRV Change Starts

tools/clang/lib/AST/ASTContextHLSL.cpp

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "clang/AST/ExternalASTSource.h"
2424
#include "clang/AST/HlslBuiltinTypeDeclBuilder.h"
2525
#include "clang/AST/TypeLoc.h"
26+
#include "clang/Basic/Specifiers.h"
2627
#include "clang/Sema/Overload.h"
2728
#include "clang/Sema/Sema.h"
2829
#include "clang/Sema/SemaDiagnostic.h"
@@ -1070,7 +1071,7 @@ static void CreateConstructorDeclaration(
10701071
static void CreateObjectFunctionDeclaration(
10711072
ASTContext &context, CXXRecordDecl *recordDecl, QualType resultType,
10721073
ArrayRef<QualType> args, DeclarationName declarationName, bool isConst,
1073-
CXXMethodDecl **functionDecl, TypeSourceInfo **tinfo) {
1074+
StorageClass SC, CXXMethodDecl **functionDecl, TypeSourceInfo **tinfo) {
10741075
DXASSERT_NOMSG(recordDecl != nullptr);
10751076
DXASSERT_NOMSG(functionDecl != nullptr);
10761077

@@ -1082,8 +1083,8 @@ static void CreateObjectFunctionDeclaration(
10821083
*tinfo = context.getTrivialTypeSourceInfo(functionQT, NoLoc);
10831084
DXASSERT_NOMSG(*tinfo != nullptr);
10841085
*functionDecl = CXXMethodDecl::Create(
1085-
context, recordDecl, NoLoc, declNameInfo, functionQT, *tinfo,
1086-
StorageClass::SC_None, InlineSpecifiedFalse, IsConstexprFalse, NoLoc);
1086+
context, recordDecl, NoLoc, declNameInfo, functionQT, *tinfo, SC,
1087+
InlineSpecifiedFalse, IsConstexprFalse, NoLoc);
10871088
DXASSERT_NOMSG(*functionDecl != nullptr);
10881089
(*functionDecl)->setLexicalDeclContext(recordDecl);
10891090
(*functionDecl)->setAccess(AccessSpecifier::AS_public);
@@ -1092,15 +1093,16 @@ static void CreateObjectFunctionDeclaration(
10921093
CXXMethodDecl *hlsl::CreateObjectFunctionDeclarationWithParams(
10931094
ASTContext &context, CXXRecordDecl *recordDecl, QualType resultType,
10941095
ArrayRef<QualType> paramTypes, ArrayRef<StringRef> paramNames,
1095-
DeclarationName declarationName, bool isConst, bool isTemplateFunction) {
1096+
DeclarationName declarationName, bool isConst, StorageClass SC,
1097+
bool isTemplateFunction) {
10961098
DXASSERT_NOMSG(recordDecl != nullptr);
10971099
DXASSERT_NOMSG(!resultType.isNull());
10981100
DXASSERT_NOMSG(paramTypes.size() == paramNames.size());
10991101

11001102
TypeSourceInfo *tinfo;
11011103
CXXMethodDecl *functionDecl;
11021104
CreateObjectFunctionDeclaration(context, recordDecl, resultType, paramTypes,
1103-
declarationName, isConst, &functionDecl,
1105+
declarationName, isConst, SC, &functionDecl,
11041106
&tinfo);
11051107

11061108
// Create and associate parameters to method.
@@ -1215,6 +1217,46 @@ CXXRecordDecl *hlsl::DeclareRayQueryType(ASTContext &context) {
12151217
return typeDeclBuilder.getRecordDecl();
12161218
}
12171219

1220+
CXXRecordDecl *hlsl::DeclareHitObjectType(NamespaceDecl &NSDecl) {
1221+
ASTContext &Context = NSDecl.getASTContext();
1222+
// HitObject { ... }
1223+
BuiltinTypeDeclBuilder TypeDeclBuilder(&NSDecl, "HitObject");
1224+
TypeDeclBuilder.startDefinition();
1225+
1226+
// Add handle to mark as HLSL object.
1227+
TypeDeclBuilder.addField("h", GetHLSLObjectHandleType(Context));
1228+
CXXRecordDecl *RecordDecl = TypeDeclBuilder.getRecordDecl();
1229+
1230+
CanQualType canQualType = Context.getCanonicalType(
1231+
Context.getRecordType(TypeDeclBuilder.getRecordDecl()));
1232+
1233+
// Add constructor that will be lowered to MOP_HitObject_MakeNop.
1234+
CXXConstructorDecl *pConstructorDecl = nullptr;
1235+
TypeSourceInfo *pTypeSourceInfo = nullptr;
1236+
CreateConstructorDeclaration(
1237+
Context, RecordDecl, Context.VoidTy, {},
1238+
Context.DeclarationNames.getCXXConstructorName(canQualType), false,
1239+
&pConstructorDecl, &pTypeSourceInfo);
1240+
RecordDecl->addDecl(pConstructorDecl);
1241+
pConstructorDecl->addAttr(HLSLIntrinsicAttr::CreateImplicit(
1242+
Context, "op", "",
1243+
static_cast<int>(hlsl::IntrinsicOp::MOP_DxHitObject_MakeNop)));
1244+
pConstructorDecl->addAttr(HLSLCXXOverloadAttr::CreateImplicit(Context));
1245+
1246+
// Add AvailabilityAttribute for SM6.9+
1247+
VersionTuple VT69 = VersionTuple(6, 9);
1248+
RecordDecl->addAttr(ConstructAvailabilityAttribute(Context, VT69));
1249+
1250+
// Add the implicit HLSLHitObjectAttr attribute to unambiguously recognize the
1251+
// builtin HitObject type.
1252+
RecordDecl->addAttr(HLSLHitObjectAttr::CreateImplicit(Context));
1253+
RecordDecl->setImplicit(true);
1254+
1255+
// Add to namespace
1256+
RecordDecl->setDeclContext(&NSDecl);
1257+
return RecordDecl;
1258+
}
1259+
12181260
CXXRecordDecl *hlsl::DeclareResourceType(ASTContext &context, bool bSampler) {
12191261
// struct ResourceDescriptor { uint8 desc; }
12201262
StringRef Name = bSampler ? ".Sampler" : ".Resource";

tools/clang/lib/AST/HlslTypes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,10 @@ bool IsHLSLResourceType(clang::QualType type) {
507507
return false;
508508
}
509509

510+
bool IsHLSLHitObjectType(QualType type) {
511+
return nullptr != getAttr<HLSLHitObjectAttr>(type);
512+
}
513+
510514
DXIL::NodeIOKind GetNodeIOType(clang::QualType type) {
511515
if (const HLSLNodeObjectAttr *Attr = getAttr<HLSLNodeObjectAttr>(type))
512516
return Attr->getNodeIOType();

0 commit comments

Comments
 (0)