Skip to content

Commit 4e441b2

Browse files
committed
[SER] Patch 1: HitObject type lowering and SM 6.9 enablement
SER: - dx::HitObject HLSL type. - dx::HitObject::MakeNop as default ctor. - dx::MaybeReorderThread HLSL intrinsic. - Diagnostics for SER builtins in unsupported shader stages. - SM6.9 AvailabilityAttributes on SER intrinsics and HitObject type. Generic HLSL: - 'static' gen_intrin_main.txt attribute to define static member functions. - Enable 'min_sm' gen_intrin_main.txt attribute path. - 'namespace dx' implementation, intrinsic declaration (derived from 'namespace vk' impl) and lookup. Generic diag: - Diagnose AvailabilityAttribute of types (used for dx::HitObject). Specification: https://github.com/microsoft/hlsl-specs/blob/main/proposals/0027-shader-execution-reordering.md DXC SER implementation tracker: microsoft#7214
1 parent b646ad3 commit 4e441b2

37 files changed

+1174
-158
lines changed

include/dxc/DXIL/DxilUtil.h

+2
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

+3-1
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

+4-1
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

+21
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

+24
Original file line numberDiff line numberDiff line change
@@ -6070,6 +6070,24 @@ Value *TranslateUnpack(CallInst *CI, IntrinsicOp IOP, OP::OpCode opcode,
60706070

60716071
} // namespace
60726072

6073+
// Shader Execution Reordering.
6074+
namespace {
6075+
Value *TranslateHitObjectMake(CallInst *CI, IntrinsicOp IOP, OP::OpCode opcode,
6076+
HLOperationLowerHelper &helper,
6077+
HLObjectOperationLowerHelper *pObjHelper,
6078+
bool &Translated) {
6079+
return UndefValue::get(CI->getType()); // TODO: Merge SER DXIL patches
6080+
}
6081+
6082+
Value *TranslateMaybeReorderThread(CallInst *CI, IntrinsicOp IOP,
6083+
OP::OpCode opcode,
6084+
HLOperationLowerHelper &helper,
6085+
HLObjectOperationLowerHelper *pObjHelper,
6086+
bool &Translated) {
6087+
return nullptr; // TODO: Merge SER DXIL patches
6088+
}
6089+
} // namespace
6090+
60736091
// Resource Handle.
60746092
namespace {
60756093
Value *TranslateGetHandleFromHeap(CallInst *CI, IntrinsicOp IOP,
@@ -6802,6 +6820,12 @@ IntrinsicLower gLowerTable[] = {
68026820
DXIL::OpCode::NumOpCodes},
68036821
{IntrinsicOp::MOP_InterlockedUMin, TranslateMopAtomicBinaryOperation,
68046822
DXIL::OpCode::NumOpCodes},
6823+
{IntrinsicOp::MOP_DxHitObject_MakeNop, TranslateHitObjectMake,
6824+
DXIL::OpCode::NumOpCodes_Dxil_1_8}, // FIXME: Just a placeholder Dxil
6825+
// opcode
6826+
{IntrinsicOp::IOP_DxMaybeReorderThread, TranslateMaybeReorderThread,
6827+
DXIL::OpCode::NumOpCodes_Dxil_1_8}, // FIXME: Just a placeholder Dxil
6828+
// opcode
68056829
};
68066830
} // namespace
68076831
static_assert(

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

+3
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

+8
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

+9
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

+47-5
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

+4
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();

tools/clang/lib/CodeGen/CGHLSLMS.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -2500,9 +2500,11 @@ void CGMSHLSLRuntime::AddHLSLFunctionInfo(Function *F, const FunctionDecl *FD) {
25002500

25012501
// Type annotation for this pointer.
25022502
if (const CXXMethodDecl *MFD = dyn_cast<CXXMethodDecl>(FD)) {
2503-
const CXXRecordDecl *RD = MFD->getParent();
2504-
QualType Ty = CGM.getContext().getTypeDeclType(RD);
2505-
AddTypeAnnotation(Ty, dxilTypeSys, arrayEltSize);
2503+
if (!MFD->isStatic()) {
2504+
const CXXRecordDecl *RD = MFD->getParent();
2505+
QualType Ty = CGM.getContext().getTypeDeclType(RD);
2506+
AddTypeAnnotation(Ty, dxilTypeSys, arrayEltSize);
2507+
}
25062508
}
25072509

25082510
for (const ValueDecl *param : FD->params()) {

tools/clang/lib/CodeGen/CodeGenTypes.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414
#include "CodeGenTypes.h"
1515
#include "CGCXXABI.h"
1616
#include "CGCall.h"
17+
#include "CGHLSLRuntime.h" // HLSL Change
1718
#include "CGOpenCLRuntime.h"
1819
#include "CGRecordLayout.h"
20+
#include "CodeGenModule.h" // HLSL Change
1921
#include "TargetInfo.h"
22+
#include "dxc/DXIL/DxilUtil.h" // HLSL Change
2023
#include "clang/AST/ASTContext.h"
2124
#include "clang/AST/DeclCXX.h"
22-
#include "clang/AST/DeclTemplate.h"
2325
#include "clang/AST/DeclObjC.h"
26+
#include "clang/AST/DeclTemplate.h" // HLSL Change - clang-format
2427
#include "clang/AST/Expr.h"
28+
#include "clang/AST/HlslTypes.h" // HLSL Change
2529
#include "clang/AST/RecordLayout.h"
2630
#include "clang/CodeGen/CGFunctionInfo.h"
2731
#include "llvm/IR/DataLayout.h"
2832
#include "llvm/IR/DerivedTypes.h"
2933
#include "llvm/IR/Module.h"
30-
#include "CodeGenModule.h" // HLSL Change
31-
#include "CGHLSLRuntime.h" // HLSL Change
3234
using namespace clang;
3335
using namespace CodeGen;
3436

@@ -365,7 +367,8 @@ llvm::Type *CodeGenTypes::ConvertType(QualType T) {
365367
.getConstantArrayType(eltTy, llvm::APInt(32, count),
366368
ArrayType::ArraySizeModifier::Normal, 0)
367369
.getTypePtr();
368-
}
370+
} else if (hlsl::IsHLSLHitObjectType(T)) // HLSL Change
371+
return hlsl::dxilutil::GetHLSLHitObjectType(&TheModule);
369372
else
370373
return ConvertRecordDeclType(RT->getDecl());
371374
}

tools/clang/lib/Sema/SemaExpr.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -2787,13 +2787,18 @@ bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS,
27872787
// Never if a scope specifier was provided.
27882788
if (SS.isSet()) {
27892789
// HLSL Change begins
2790-
// We want to be able to have intrinsics inside the "vk" namespace.
2790+
// We want to be able to have intrinsics inside the "vk" and "dx"
2791+
// namespaces.
27912792
const bool isVkNamespace =
27922793
SS.getScopeRep() && SS.getScopeRep()->getAsNamespace() &&
27932794
SS.getScopeRep()->getAsNamespace()->getName() == "vk";
27942795

2795-
if (!isVkNamespace)
2796-
// HLSL Change ends
2796+
const bool isDxNamespace =
2797+
SS.getScopeRep() && SS.getScopeRep()->getAsNamespace() &&
2798+
SS.getScopeRep()->getAsNamespace()->getName() == "dx";
2799+
2800+
if (!isVkNamespace && !isDxNamespace)
2801+
// HLSL Change ends
27972802
return false;
27982803
}
27992804

0 commit comments

Comments
 (0)