Skip to content

Commit f3378fe

Browse files
committed
[SER] HitObject::GetAttributes HLSL -> DXIL lowering and attributes sema
1 parent c5e0ec0 commit f3378fe

8 files changed

Lines changed: 152 additions & 24 deletions

File tree

lib/HLSL/HLOperationLower.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6323,7 +6323,23 @@ Value *TranslateHitObjectGetAttributes(CallInst *CI, IntrinsicOp IOP,
63236323
HLOperationLowerHelper &Helper,
63246324
HLObjectOperationLowerHelper *pObjHelper,
63256325
bool &Translated) {
6326-
return UndefValue::get(CI->getType()); // TODO: Merge SER DXIL patches
6326+
hlsl::OP *OP = &Helper.hlslOP;
6327+
IRBuilder<> Builder(CI);
6328+
6329+
Value *HitObjectPtr = CI->getArgOperand(1);
6330+
Value *HitObject = Builder.CreateLoad(HitObjectPtr);
6331+
6332+
Type *AttrTy = cast<PointerType>(CI->getType())->getPointerElementType();
6333+
6334+
IRBuilder<> EntryBuilder(
6335+
dxilutil::FindAllocaInsertionPt(CI->getParent()->getParent()));
6336+
unsigned AttrAlign = Helper.dataLayout.getABITypeAlignment(AttrTy);
6337+
AllocaInst *AttrMem = EntryBuilder.CreateAlloca(AttrTy);
6338+
AttrMem->setAlignment(AttrAlign);
6339+
Constant *opArg = OP->GetU32Const((unsigned)OpCode);
6340+
TrivialDxilOperation(OpCode, {opArg, HitObject, AttrMem}, CI->getType(),
6341+
Helper.voidTy, OP, Builder);
6342+
return AttrMem;
63276343
}
63286344

63296345
Value *TranslateHitObjectScalarGetter(CallInst *CI, IntrinsicOp IOP,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7646,7 +7646,7 @@ def err_payload_requires_inout : Error<
76467646
def err_attributes_requiers_in : Error<
76477647
"intersection attributes parameter %0 must be 'in'">;
76487648
def err_payload_attrs_must_be_udt : Error<
7649-
"%select{payload|attributes|callable}0 parameter %1 must be a user-defined type composed of only numeric types">;
7649+
"%select{payload|attributes|callable}0 %select{parameter %2|type}1 must be a user-defined type composed of only numeric types">;
76507650
def err_shader_must_return_void : Error<
76517651
"return type for '%0' shaders must be void">;
76527652
def err_raytracing_entry_param_count : Error<
@@ -7885,7 +7885,7 @@ def err_hlsl_unsupported_long_vector
78857885
"cbuffers or tbuffers|user-defined struct parameter|"
78867886
"entry function parameters|entry function return type|"
78877887
"patch constant function parameters|patch constant function return type|"
7888-
"payload parameters}0 are not supported">;
7888+
"payload parameters|attributes}0 are not supported">;
78897889
def err_hlsl_logical_binop_scalar : Error<
78907890
"operands for short-circuiting logical binary operator must be scalar, for non-scalar types use '%select{and|or}0'">;
78917891
def err_hlsl_ternary_scalar : Error<

tools/clang/lib/Sema/SemaDXR.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,8 @@ void DiagnoseCallableEntry(Sema &S, FunctionDecl *FD,
11671167

11681168
if (!(hlsl::IsHLSLCopyableAnnotatableRecord(Ty)))
11691169
S.Diag(Param->getLocation(), diag::err_payload_attrs_must_be_udt)
1170-
<< /*payload|attributes|callable*/ 2 << Param;
1170+
<< /*payload|attributes|callable*/ 2 << /*parameter %2|type*/ 0
1171+
<< Param;
11711172
}
11721173
return;
11731174
}
@@ -1208,7 +1209,8 @@ void DiagnoseMissOrAnyHitEntry(Sema &S, FunctionDecl *FD,
12081209

12091210
if (!(hlsl::IsHLSLCopyableAnnotatableRecord(Ty))) {
12101211
S.Diag(Param->getLocation(), diag::err_payload_attrs_must_be_udt)
1211-
<< /*payload|attributes|callable*/ Idx << Param;
1212+
<< /*payload|attributes|callable*/ Idx << /*parameter %2|type*/ 0
1213+
<< Param;
12121214
}
12131215
}
12141216
return;
@@ -1261,7 +1263,8 @@ void DiagnoseClosestHitEntry(Sema &S, FunctionDecl *FD,
12611263

12621264
if (!(hlsl::IsHLSLCopyableAnnotatableRecord(Ty))) {
12631265
S.Diag(Param->getLocation(), diag::err_payload_attrs_must_be_udt)
1264-
<< /*payload|attributes|callable*/ Idx << Param;
1266+
<< /*payload|attributes|callable*/ Idx << /*parameter %2|type*/ 0
1267+
<< Param;
12651268
}
12661269
}
12671270
return;

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10770,6 +10770,22 @@ HLSLExternalSource::ApplyTypeSpecSignToParsedType(clang::QualType &type,
1077010770
}
1077110771
}
1077210772

10773+
bool DiagnoseIntersectionAttributes(Sema &S, SourceLocation Loc, QualType Ty) {
10774+
// Must be a UDT
10775+
if (Ty.isNull() || !hlsl::IsHLSLCopyableAnnotatableRecord(Ty)) {
10776+
S.Diag(Loc, diag::err_payload_attrs_must_be_udt)
10777+
<< /*payload|attributes|callable*/ 1 << /*parameter %2|type*/ 1;
10778+
return false;
10779+
}
10780+
10781+
if (ContainsLongVector(Ty)) {
10782+
const unsigned AttributesIdx = 11;
10783+
S.Diag(Loc, diag::err_hlsl_unsupported_long_vector) << AttributesIdx;
10784+
return false;
10785+
}
10786+
return true;
10787+
}
10788+
1077310789
Sema::TemplateDeductionResult
1077410790
HLSLExternalSource::DeduceTemplateArgumentsForHLSL(
1077510791
FunctionTemplateDecl *FunctionTemplate,
@@ -10878,6 +10894,7 @@ HLSLExternalSource::DeduceTemplateArgumentsForHLSL(
1087810894
LPCSTR tableName = cursor.GetTableName();
1087910895
// Currently only intrinsic we allow for explicit template arguments are
1088010896
// for Load/Store for ByteAddressBuffer/RWByteAddressBuffer
10897+
// and HitObject::GetAttributes with user-defined intersection attributes.
1088110898

1088210899
// Check Explicit template arguments
1088310900
UINT intrinsicOp = (*cursor)->Op;
@@ -10892,28 +10909,38 @@ HLSLExternalSource::DeduceTemplateArgumentsForHLSL(
1089210909
IsBABLoad = intrinsicOp == (UINT)IntrinsicOp::MOP_Load;
1089310910
IsBABStore = intrinsicOp == (UINT)IntrinsicOp::MOP_Store;
1089410911
}
10895-
if (ExplicitTemplateArgs && ExplicitTemplateArgs->size() > 0) {
10896-
bool isLegalTemplate = false;
10912+
bool IsHitObjectGetAttributes =
10913+
intrinsicOp == (UINT)IntrinsicOp::MOP_DxHitObject_GetAttributes;
10914+
if (ExplicitTemplateArgs && ExplicitTemplateArgs->size() >= 1) {
1089710915
SourceLocation Loc = ExplicitTemplateArgs->getLAngleLoc();
10898-
auto TemplateDiag = diag::err_hlsl_intrinsic_template_arg_unsupported;
10899-
if (ExplicitTemplateArgs->size() >= 1 && (IsBABLoad || IsBABStore)) {
10900-
TemplateDiag = diag::err_hlsl_intrinsic_template_arg_requires_2018;
10901-
Loc = (*ExplicitTemplateArgs)[0].getLocation();
10902-
if (Is2018) {
10903-
TemplateDiag = diag::err_hlsl_intrinsic_template_arg_numeric;
10904-
if (ExplicitTemplateArgs->size() == 1 &&
10905-
!functionTemplateTypeArg.isNull() &&
10906-
hlsl::IsHLSLNumericOrAggregateOfNumericType(
10907-
functionTemplateTypeArg)) {
10908-
isLegalTemplate = true;
10909-
}
10910-
}
10916+
if (!IsBABLoad && !IsBABStore && !IsHitObjectGetAttributes) {
10917+
getSema()->Diag(Loc, diag::err_hlsl_intrinsic_template_arg_unsupported)
10918+
<< intrinsicName;
10919+
return Sema::TemplateDeductionResult::TDK_Invalid;
1091110920
}
10912-
10913-
if (!isLegalTemplate) {
10914-
getSema()->Diag(Loc, TemplateDiag) << intrinsicName;
10921+
Loc = (*ExplicitTemplateArgs)[0].getLocation();
10922+
if (!Is2018) {
10923+
getSema()->Diag(Loc,
10924+
diag::err_hlsl_intrinsic_template_arg_requires_2018)
10925+
<< intrinsicName;
1091510926
return Sema::TemplateDeductionResult::TDK_Invalid;
1091610927
}
10928+
10929+
if (IsBABLoad || IsBABStore) {
10930+
const bool IsLegalTemplate =
10931+
!functionTemplateTypeArg.isNull() &&
10932+
hlsl::IsHLSLNumericOrAggregateOfNumericType(
10933+
functionTemplateTypeArg);
10934+
if (!IsLegalTemplate) {
10935+
getSema()->Diag(Loc, diag::err_hlsl_intrinsic_template_arg_numeric)
10936+
<< intrinsicName;
10937+
return Sema::TemplateDeductionResult::TDK_Invalid;
10938+
}
10939+
}
10940+
if (IsHitObjectGetAttributes &&
10941+
!DiagnoseIntersectionAttributes(*getSema(), Loc,
10942+
functionTemplateTypeArg))
10943+
return Sema::TemplateDeductionResult::TDK_Invalid;
1091710944
} else if (IsBABStore) {
1091810945
// Prior to HLSL 2018, Store operation only stored scalar uint.
1091910946
if (!Is2018) {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %dxc -T lib_6_9 -E main %s | FileCheck %s --check-prefix DXIL
2+
3+
// DXIL: %[[APTR:[^ ]+]] = alloca %struct.CustomAttrs, align 4
4+
// DXIL: %[[NOP:[^ ]+]] = call %dx.types.HitObject @dx.op.hitObject_MakeNop(i32 266) ; HitObject_MakeNop()
5+
// DXIL: call void @dx.op.hitObject_Attributes.struct.CustomAttrs(i32 289, %dx.types.HitObject %[[NOP]], %struct.CustomAttrs* nonnull %[[APTR]]) ; HitObject_Attributes(hitObject,attributes)
6+
// DXIL: %[[VPTR:[^ ]+]] = getelementptr inbounds %struct.CustomAttrs, %struct.CustomAttrs* %[[APTR]], i32 0, i32 0
7+
// DXIL: %{{[^ ]+}} = load <4 x float>, <4 x float>* %[[VPTR]], align 4
8+
// DXIL: %[[IPTR:[^ ]+]] = getelementptr inbounds %struct.CustomAttrs, %struct.CustomAttrs* %[[APTR]], i32 0, i32 1
9+
// DXIL: %{{[^ ]+}} = load i32, i32* %[[IPTR]], align 4
10+
// DXIL: ret void
11+
12+
RWByteAddressBuffer outbuf;
13+
14+
struct
15+
CustomAttrs {
16+
float4 v;
17+
int y;
18+
};
19+
20+
[shader("raygeneration")]
21+
void main() {
22+
dx::HitObject hit;
23+
CustomAttrs attrs = hit.GetAttributes<CustomAttrs>();
24+
float sum = attrs.v.x + attrs.v.y + attrs.v.z + attrs.v.w + attrs.y;
25+
outbuf.Store(0, sum);
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: %dxc -T lib_6_9 -E main %s -ast-dump-implicit | FileCheck %s --check-prefix AST
2+
// RUN: %dxc -T lib_6_9 -E main %s -fcgl | FileCheck %s --check-prefix FCGL
3+
4+
// AST: | | |-FunctionTemplateDecl {{[^ ]+}} <<invalid sloc>> <invalid sloc> GetAttributes
5+
// AST-NEXT: | | | |-TemplateTypeParmDecl {{[^ ]+}} <<invalid sloc>> <invalid sloc> class TResult
6+
// AST-NEXT: | | | |-CXXMethodDecl {{[^ ]+}} <<invalid sloc>> <invalid sloc> implicit GetAttributes 'TResult () const'
7+
// AST-NEXT: | | | `-CXXMethodDecl {{[^ ]+}} <<invalid sloc>> <invalid sloc> used GetAttributes 'CustomAttrs &()' extern
8+
// AST-NEXT: | | | |-TemplateArgument type 'CustomAttrs'
9+
// AST-NEXT: | | | |-HLSLIntrinsicAttr {{[^ ]+}} <<invalid sloc>> Implicit "op" "" 364
10+
// AST-NEXT: | | | `-AvailabilityAttr {{[^ ]+}} <<invalid sloc>> Implicit 6.9 0 0 ""
11+
12+
// FCGL: %{{[^ ]+}} = call %struct.CustomAttrs* @"dx.hl.op..%struct.CustomAttrs* (i32, %dx.types.HitObject*)"(i32 364, %dx.types.HitObject* %{{[^ ]+}})
13+
14+
RWByteAddressBuffer outbuf;
15+
16+
struct
17+
CustomAttrs {
18+
float4 v;
19+
int y;
20+
};
21+
22+
[shader("raygeneration")]
23+
void main() {
24+
dx::HitObject hit;
25+
CustomAttrs attrs = hit.GetAttributes<CustomAttrs>();
26+
float sum = attrs.v.x + attrs.v.y + attrs.v.z + attrs.v.w + attrs.y;
27+
outbuf.Store(0, sum);
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %dxc -T lib_6_9 -E main %s -verify
2+
3+
struct
4+
CustomAttrs {
5+
vector<float, 32> v;
6+
int y;
7+
};
8+
9+
[shader("raygeneration")]
10+
void main() {
11+
dx::HitObject hit;
12+
// expected-error@+1{{vectors of over 4 elements in attributes are not supported}}
13+
CustomAttrs attrs = hit.GetAttributes<CustomAttrs>();
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %dxc -T lib_6_9 -E main %s -verify
2+
3+
struct
4+
CustomAttrs {
5+
vector<float, 32> v;
6+
RWStructuredBuffer<float> buf;
7+
};
8+
9+
[shader("raygeneration")]
10+
void main() {
11+
dx::HitObject hit;
12+
// expected-error@+1{{attributes type must be a user-defined type composed of only numeric types}}
13+
CustomAttrs attrs = hit.GetAttributes<CustomAttrs>();
14+
}

0 commit comments

Comments
 (0)