Skip to content

Commit 624665f

Browse files
authored
[SER] HitObject::FromRayQuery HLSL -> DXIL lowering (#7370)
* HLSL -> DXIL lowering * ast, hlsl->dxil, dxilgen, and ScalarReplAggregatesHLSL tests SER implementation tracker (#7214)
1 parent 847d5ad commit 624665f

File tree

8 files changed

+694
-1
lines changed

8 files changed

+694
-1
lines changed

include/dxc/HLSL/HLOperations.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ const unsigned kHitObjectMakeMissRayDescOpIdx = 4;
441441
const unsigned kHitObjectTraceRay_RayDescOpIdx = 8;
442442
const unsigned kHitObjectTraceRay_NumOp = 10;
443443

444+
// HitObject::FromRayQuery
445+
const unsigned kHitObjectFromRayQuery_WithAttrs_AttributeOpIdx = 4;
446+
const unsigned kHitObjectFromRayQuery_WithAttrs_NumOp = 5;
447+
444448
} // namespace HLOperandIndex
445449

446450
llvm::Function *GetOrCreateHLFunction(llvm::Module &M,

lib/HLSL/HLOperationLower.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6312,7 +6312,32 @@ Value *TranslateHitObjectFromRayQuery(CallInst *CI, IntrinsicOp IOP,
63126312
HLOperationLowerHelper &Helper,
63136313
HLObjectOperationLowerHelper *pObjHelper,
63146314
bool &Translated) {
6315-
return UndefValue::get(CI->getType()); // TODO: Merge SER DXIL patches
6315+
hlsl::OP *OP = &Helper.hlslOP;
6316+
IRBuilder<> Builder(CI);
6317+
6318+
unsigned SrcIdx = 1;
6319+
Value *HitObjectPtr = CI->getArgOperand(SrcIdx++);
6320+
Value *RayQuery = CI->getArgOperand(SrcIdx++);
6321+
6322+
if (CI->getNumArgOperands() ==
6323+
HLOperandIndex::kHitObjectFromRayQuery_WithAttrs_NumOp) {
6324+
Value *HitKind = CI->getArgOperand(SrcIdx++);
6325+
Value *AttribSrc = CI->getArgOperand(SrcIdx++);
6326+
DXASSERT_NOMSG(SrcIdx == CI->getNumArgOperands());
6327+
OpCode = DXIL::OpCode::HitObject_FromRayQueryWithAttrs;
6328+
Type *AttrTy = AttribSrc->getType();
6329+
Value *OutHitObject = TrivialDxilOperation(
6330+
OpCode, {nullptr, RayQuery, HitKind, AttribSrc}, AttrTy, CI, OP);
6331+
Builder.CreateStore(OutHitObject, HitObjectPtr);
6332+
return nullptr;
6333+
}
6334+
6335+
DXASSERT_NOMSG(SrcIdx == CI->getNumArgOperands());
6336+
OpCode = DXIL::OpCode::HitObject_FromRayQuery;
6337+
Value *OutHitObject =
6338+
TrivialDxilOperation(OpCode, {nullptr, RayQuery}, Helper.voidTy, CI, OP);
6339+
Builder.CreateStore(OutHitObject, HitObjectPtr);
6340+
return nullptr;
63166341
}
63176342

63186343
Value *TranslateHitObjectTraceRay(CallInst *CI, IntrinsicOp IOP,

lib/Transforms/Scalar/ScalarReplAggregatesHLSL.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2795,6 +2795,31 @@ void SROA_Helper::RewriteCall(CallInst *CI) {
27952795
}
27962796
}
27972797
LLVM_FALLTHROUGH;
2798+
case IntrinsicOp::MOP_DxHitObject_FromRayQuery: {
2799+
const bool IsWithAttrs =
2800+
CI->getNumArgOperands() ==
2801+
HLOperandIndex::kHitObjectFromRayQuery_WithAttrs_NumOp;
2802+
if (IsWithAttrs &&
2803+
(OldVal ==
2804+
CI->getArgOperand(
2805+
HLOperandIndex::
2806+
kHitObjectFromRayQuery_WithAttrs_AttributeOpIdx))) {
2807+
RewriteCallArg(
2808+
CI,
2809+
HLOperandIndex::kHitObjectFromRayQuery_WithAttrs_AttributeOpIdx,
2810+
/*bIn*/ true, /*bOut*/ false);
2811+
break;
2812+
}
2813+
2814+
// For RayQuery methods, we want to replace the RayQuery this pointer
2815+
// with a load and use of the underlying handle value.
2816+
// This will allow elimination of RayQuery types earlier.
2817+
RewriteWithFlattenedHLIntrinsicCall(CI, OldVal, NewElts,
2818+
/*loadElts*/ true);
2819+
DeadInsts.push_back(CI);
2820+
break;
2821+
}
2822+
LLVM_FALLTHROUGH;
27982823
default:
27992824
// RayQuery this pointer replacement.
28002825
if (OldVal->getType()->isPointerTy() &&

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12093,6 +12093,7 @@ void Sema::DiagnoseReachableHLSLCall(CallExpr *CE, const hlsl::ShaderModel *SM,
1209312093
case hlsl::IntrinsicOp::MOP_TraceRayInline:
1209412094
DiagnoseTraceRayInline(*this, CE);
1209512095
break;
12096+
case hlsl::IntrinsicOp::MOP_DxHitObject_FromRayQuery:
1209612097
case hlsl::IntrinsicOp::MOP_DxHitObject_Invoke:
1209712098
case hlsl::IntrinsicOp::MOP_DxHitObject_MakeMiss:
1209812099
case hlsl::IntrinsicOp::MOP_DxHitObject_MakeNop:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// RUN: %dxc -T lib_6_9 -E main %s | FileCheck %s --check-prefix DXIL
2+
3+
// DXIL: %{{[^ ]+}} = call %dx.types.HitObject @dx.op.hitObject_FromRayQuery(i32 263, i32 %[[RQ:[^ ]+]]) ; HitObject_FromRayQuery(rayQueryHandle)
4+
// DXIL: %{{[^ ]+}} = call %dx.types.HitObject @dx.op.hitObject_FromRayQueryWithAttrs.struct.CustomAttrs(i32 264, i32 %[[RQ]], i32 16, %struct.CustomAttrs* nonnull %{{[^ ]+}}) ; HitObject_FromRayQueryWithAttrs(rayQueryHandle,HitKind,CommittedAttribs)
5+
6+
RaytracingAccelerationStructure RTAS;
7+
RWStructuredBuffer<float> UAV : register(u0);
8+
9+
RayDesc MakeRayDesc() {
10+
RayDesc desc;
11+
desc.Origin = float3(0, 0, 0);
12+
desc.Direction = float3(1, 0, 0);
13+
desc.TMin = 0.0f;
14+
desc.TMax = 9999.0;
15+
return desc;
16+
}
17+
18+
struct CustomAttrs {
19+
float x;
20+
float y;
21+
};
22+
23+
void Use(in dx::HitObject hit) {
24+
dx::MaybeReorderThread(hit);
25+
}
26+
27+
[shader("raygeneration")]
28+
void main() {
29+
RayQuery<RAY_FLAG_FORCE_OPAQUE | RAY_FLAG_ACCEPT_FIRST_HIT_AND_END_SEARCH> q;
30+
RayDesc ray = MakeRayDesc();
31+
q.TraceRayInline(RTAS, RAY_FLAG_NONE, 0xFF, ray);
32+
33+
Use(dx::HitObject::FromRayQuery(q));
34+
35+
CustomAttrs attrs = {1.f, 2.f};
36+
Use(dx::HitObject::FromRayQuery(q, 16, attrs));
37+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
; RUN: %dxopt %s -hlsl-passes-resume -dxilgen -S | FileCheck %s
2+
; REQUIRES: dxil-1-9
3+
4+
;
5+
; Buffer Definitions:
6+
;
7+
;
8+
; Resource Bindings:
9+
;
10+
; Name Type Format Dim ID HLSL Bind Count
11+
; ------------------------------ ---------- ------- ----------- ------- -------------- ------
12+
; RTAS texture i32 ras T0t4294967295,space4294967295 1
13+
;
14+
target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
15+
target triple = "dxil-ms-dx"
16+
17+
%struct.RaytracingAccelerationStructure = type { i32 }
18+
%struct.CustomAttrs = type { float, float }
19+
%dx.types.HitObject = type { i8* }
20+
%dx.types.Handle = type { i8* }
21+
%dx.types.ResourceProperties = type { i32, i32 }
22+
%"class.RWStructuredBuffer<float>" = type { float }
23+
%struct.RayDesc = type { <3 x float>, float, <3 x float>, float }
24+
%"class.dx::HitObject" = type { i32 }
25+
%"class.RayQuery<5, 0>" = type { i32 }
26+
27+
@"\01?RTAS@@3URaytracingAccelerationStructure@@A" = external global %struct.RaytracingAccelerationStructure, align 4
28+
29+
; CHECK: %[[ATTRA:[^ ]+]] = alloca %struct.CustomAttrs
30+
; CHECK: call void @dx.op.rayQuery_TraceRayInline(i32 179, i32 %[[RQH:[^ ]+]], %dx.types.Handle %{{[^ ]+}}, i32 0, i32 255, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float 0.000000e+00, float 1.000000e+00, float 0.000000e+00, float 0.000000e+00, float 9.999000e+03)
31+
; CHECK: %{{[^ ]+}} = call %dx.types.HitObject @dx.op.hitObject_FromRayQuery(i32 263, i32 %[[RQH]])
32+
; CHECK: %{{[^ ]+}} = call %dx.types.HitObject @dx.op.hitObject_FromRayQueryWithAttrs.struct.CustomAttrs(i32 264, i32 %[[RQH]], i32 16, %struct.CustomAttrs* %[[ATTRA]])
33+
34+
; Function Attrs: nounwind
35+
define void @"\01?main@@YAXXZ"() #0 {
36+
entry:
37+
%0 = alloca %struct.CustomAttrs
38+
%agg.tmp = alloca %dx.types.HitObject, align 4
39+
%agg.tmp1 = alloca %dx.types.HitObject, align 4
40+
%q2 = call i32 @"dx.hl.op..i32 (i32, i32, i32)"(i32 4, i32 5, i32 0), !dbg !38 ; line:29 col:78
41+
%1 = load %struct.RaytracingAccelerationStructure, %struct.RaytracingAccelerationStructure* @"\01?RTAS@@3URaytracingAccelerationStructure@@A", !dbg !42 ; line:31 col:3
42+
%2 = call %dx.types.Handle @"dx.hl.createhandle..%dx.types.Handle (i32, %struct.RaytracingAccelerationStructure)"(i32 0, %struct.RaytracingAccelerationStructure %1), !dbg !42 ; line:31 col:3
43+
%3 = call %dx.types.Handle @"dx.hl.annotatehandle..%dx.types.Handle (i32, %dx.types.Handle, %dx.types.ResourceProperties, %struct.RaytracingAccelerationStructure)"(i32 14, %dx.types.Handle %2, %dx.types.ResourceProperties { i32 16, i32 0 }, %struct.RaytracingAccelerationStructure zeroinitializer), !dbg !42 ; line:31 col:3
44+
call void @"dx.hl.op..void (i32, i32, %dx.types.Handle, i32, i32, <3 x float>, float, <3 x float>, float)"(i32 325, i32 %q2, %dx.types.Handle %3, i32 0, i32 255, <3 x float> zeroinitializer, float 0.000000e+00, <3 x float> <float 1.000000e+00, float 0.000000e+00, float 0.000000e+00>, float 9.999000e+03), !dbg !42 ; line:31 col:3
45+
call void @"dx.hl.op..void (i32, %dx.types.HitObject*, i32)"(i32 363, %dx.types.HitObject* %agg.tmp, i32 %q2), !dbg !43 ; line:33 col:7
46+
call void @"dx.hl.op..void (i32, %dx.types.HitObject*)"(i32 359, %dx.types.HitObject* %agg.tmp) #0, !dbg !44 ; line:24 col:3
47+
%.0 = getelementptr inbounds %struct.CustomAttrs, %struct.CustomAttrs* %0, i32 0, i32 0
48+
store float 1.000000e+00, float* %.0
49+
%.1 = getelementptr inbounds %struct.CustomAttrs, %struct.CustomAttrs* %0, i32 0, i32 1
50+
store float 2.000000e+00, float* %.1, align 4
51+
call void @"dx.hl.op..void (i32, %dx.types.HitObject*, i32, i32, %struct.CustomAttrs*)"(i32 363, %dx.types.HitObject* %agg.tmp1, i32 %q2, i32 16, %struct.CustomAttrs* %0), !dbg !47 ; line:36 col:7
52+
call void @"dx.hl.op..void (i32, %dx.types.HitObject*)"(i32 359, %dx.types.HitObject* %agg.tmp1) #0, !dbg !48 ; line:24 col:3
53+
ret void, !dbg !49 ; line:37 col:1
54+
}
55+
56+
; Function Attrs: nounwind
57+
declare void @llvm.lifetime.start(i64, i8* nocapture) #0
58+
59+
; Function Attrs: nounwind
60+
declare void @llvm.lifetime.end(i64, i8* nocapture) #0
61+
62+
; Function Attrs: nounwind
63+
declare void @"dx.hl.op..void (i32, %dx.types.HitObject*)"(i32, %dx.types.HitObject*) #0
64+
65+
; Function Attrs: nounwind readnone
66+
declare %dx.types.Handle @"dx.hl.createhandle..%dx.types.Handle (i32, %struct.RaytracingAccelerationStructure)"(i32, %struct.RaytracingAccelerationStructure) #1
67+
68+
; Function Attrs: nounwind readnone
69+
declare %dx.types.Handle @"dx.hl.annotatehandle..%dx.types.Handle (i32, %dx.types.Handle, %dx.types.ResourceProperties, %struct.RaytracingAccelerationStructure)"(i32, %dx.types.Handle, %dx.types.ResourceProperties, %struct.RaytracingAccelerationStructure) #1
70+
71+
; Function Attrs: nounwind
72+
declare i32 @"dx.hl.op..i32 (i32, i32, i32)"(i32, i32, i32) #0
73+
74+
; Function Attrs: nounwind
75+
declare void @"dx.hl.op..void (i32, i32, %dx.types.Handle, i32, i32, <3 x float>, float, <3 x float>, float)"(i32, i32, %dx.types.Handle, i32, i32, <3 x float>, float, <3 x float>, float) #0
76+
77+
; Function Attrs: nounwind
78+
declare void @"dx.hl.op..void (i32, %dx.types.HitObject*, i32, i32, %struct.CustomAttrs*)"(i32, %dx.types.HitObject*, i32, i32, %struct.CustomAttrs*) #0
79+
80+
; Function Attrs: nounwind
81+
declare void @"dx.hl.op..void (i32, %dx.types.HitObject*, i32)"(i32, %dx.types.HitObject*, i32) #0
82+
83+
attributes #0 = { nounwind }
84+
attributes #1 = { nounwind readnone }
85+
86+
!llvm.module.flags = !{!0}
87+
!pauseresume = !{!1}
88+
!dx.version = !{!2}
89+
!dx.valver = !{!2}
90+
!dx.shaderModel = !{!3}
91+
!dx.typeAnnotations = !{!4, !26}
92+
!dx.entryPoints = !{!30}
93+
!dx.fnprops = !{!35}
94+
!dx.options = !{!36, !37}
95+
96+
!0 = !{i32 2, !"Debug Info Version", i32 3}
97+
!1 = !{!"hlsl-hlemit", !"hlsl-hlensure"}
98+
!2 = !{i32 1, i32 9}
99+
!3 = !{!"lib", i32 6, i32 9}
100+
!4 = !{i32 0, %"class.RWStructuredBuffer<float>" undef, !5, %struct.RayDesc undef, !10, %"class.dx::HitObject" undef, !15, %"class.RayQuery<5, 0>" undef, !17, %struct.CustomAttrs undef, !23}
101+
!5 = !{i32 4, !6, !7}
102+
!6 = !{i32 6, !"h", i32 3, i32 0, i32 7, i32 9}
103+
!7 = !{i32 0, !8}
104+
!8 = !{!9}
105+
!9 = !{i32 0, float undef}
106+
!10 = !{i32 32, !11, !12, !13, !14}
107+
!11 = !{i32 6, !"Origin", i32 3, i32 0, i32 7, i32 9, i32 13, i32 3}
108+
!12 = !{i32 6, !"TMin", i32 3, i32 12, i32 7, i32 9}
109+
!13 = !{i32 6, !"Direction", i32 3, i32 16, i32 7, i32 9, i32 13, i32 3}
110+
!14 = !{i32 6, !"TMax", i32 3, i32 28, i32 7, i32 9}
111+
!15 = !{i32 4, !16}
112+
!16 = !{i32 6, !"h", i32 3, i32 0, i32 7, i32 4}
113+
!17 = !{i32 4, !18, !19}
114+
!18 = !{i32 6, !"h", i32 3, i32 0, i32 7, i32 5}
115+
!19 = !{i32 0, !20}
116+
!20 = !{!21, !22}
117+
!21 = !{i32 1, i64 5}
118+
!22 = !{i32 1, i64 0}
119+
!23 = !{i32 8, !24, !25}
120+
!24 = !{i32 6, !"x", i32 3, i32 0, i32 7, i32 9}
121+
!25 = !{i32 6, !"y", i32 3, i32 4, i32 7, i32 9}
122+
!26 = !{i32 1, void ()* @"\01?main@@YAXXZ", !27}
123+
!27 = !{!28}
124+
!28 = !{i32 1, !29, !29}
125+
!29 = !{}
126+
!30 = !{null, !"", null, !31, null}
127+
!31 = !{!32, null, null, null}
128+
!32 = !{!33}
129+
!33 = !{i32 0, %struct.RaytracingAccelerationStructure* @"\01?RTAS@@3URaytracingAccelerationStructure@@A", !"RTAS", i32 -1, i32 -1, i32 1, i32 16, i32 0, !34}
130+
!34 = !{i32 0, i32 4}
131+
!35 = !{void ()* @"\01?main@@YAXXZ", i32 7}
132+
!36 = !{i32 -2147483584}
133+
!37 = !{i32 -1}
134+
!38 = !DILocation(line: 29, column: 78, scope: !39)
135+
!39 = !DISubprogram(name: "main", scope: !40, file: !40, line: 28, type: !41, isLocal: false, isDefinition: true, scopeLine: 28, flags: DIFlagPrototyped, isOptimized: false, function: void ()* @"\01?main@@YAXXZ")
136+
!40 = !DIFile(filename: "tools/clang/test/CodeGenDXIL/hlsl/objects/HitObject/hitobject_fromrayquery.hlsl", directory: "")
137+
!41 = !DISubroutineType(types: !29)
138+
!42 = !DILocation(line: 31, column: 3, scope: !39)
139+
!43 = !DILocation(line: 33, column: 7, scope: !39)
140+
!44 = !DILocation(line: 24, column: 3, scope: !45, inlinedAt: !46)
141+
!45 = !DISubprogram(name: "Use", scope: !40, file: !40, line: 23, type: !41, isLocal: false, isDefinition: true, scopeLine: 23, flags: DIFlagPrototyped, isOptimized: false)
142+
!46 = distinct !DILocation(line: 33, column: 3, scope: !39)
143+
!47 = !DILocation(line: 36, column: 7, scope: !39)
144+
!48 = !DILocation(line: 24, column: 3, scope: !45, inlinedAt: !49)
145+
!49 = distinct !DILocation(line: 36, column: 3, scope: !39)
146+
!50 = !DILocation(line: 37, column: 1, scope: !39)

0 commit comments

Comments
 (0)