Skip to content

Commit 547f5c8

Browse files
hekotatex3d
authored andcommitted
DxilConv update - bug fixes and pre/post convert hooks (#2760)
* DxilConv update - bug fixes and pre/post convert hooks * Fix tests
1 parent 59e63bb commit 547f5c8

12 files changed

Lines changed: 188 additions & 20 deletions

File tree

projects/dxilconv/include/DxilConvPasses/NormalizeDxil.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,24 @@ namespace llvm {
1717
class Function;
1818
class PassRegistry;
1919
class FunctionPass;
20+
class Function;
21+
class DominatorTree;
2022

2123

2224
llvm::FunctionPass *createNormalizeDxilPass();
2325
void initializeNormalizeDxilPassPass(llvm::PassRegistry&);
2426

27+
class NormalizeDxil {
28+
public:
29+
NormalizeDxil(Function &F, DominatorTree &DT) : m_function(F), m_dominatorTree(DT) {}
30+
31+
virtual bool Run();
32+
33+
protected:
34+
Function &m_function;
35+
DominatorTree &m_dominatorTree;
36+
};
37+
2538
// The legacy pass manager's analysis pass to normalize dxil ir.
2639
class NormalizeDxilPass : public FunctionPass {
2740
public:

projects/dxilconv/include/DxilConvPasses/ScopeNestIterator.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,11 +833,11 @@ class ScopeNestIterator
833833
// Make sure we are not ending a scope end because that will cause
834834
// us to move from the stack again. Indicates some problem with the
835835
// state transition.
836+
// Allow SwitchEnd for empty case
836837
BranchKind Kind = annotation.Get();
837838
DXASSERT_LOCALVAR_NOMSG(Kind, Kind != BranchKind::IfEnd &&
838839
Kind != BranchKind::LoopBackEdge &&
839-
Kind != BranchKind::LoopExit &&
840-
Kind != BranchKind::SwitchEnd);
840+
Kind != BranchKind::LoopExit);
841841
}
842842
MoveToBlock(B);
843843
}

projects/dxilconv/include/Support/DXIncludes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@
3131
#include "DxbcSignatures.h"
3232
#include <d3dcompiler.h>
3333
#include <wincrypt.h>
34+
35+
#ifndef DECODE_D3D10_SB_TOKENIZED_PROGRAM_TYPE
3436
#include <d3d12TokenizedProgramFormat.hpp>
37+
#endif
38+
3539
#include <ShaderBinary/ShaderBinary.h>

projects/dxilconv/lib/DxbcConverter/DxbcConverter.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ void DxbcConverter::ConvertImpl(_In_reads_bytes_(DxbcSize) LPCVOID pDxbc,
224224
ConvertSignature(*m_pPatchConstantSignature, m_pPR->GetPatchConstOrPrimSignature());
225225
}
226226

227+
// 3.5. Callback before conversion
228+
PreConvertHook(pByteCode);
229+
227230
// 4. Transform DXBC to DXIL.
228231
Parser.SetShader(pByteCode);
229232
ConvertInstructions(Parser);
@@ -235,6 +238,9 @@ void DxbcConverter::ConvertImpl(_In_reads_bytes_(DxbcSize) LPCVOID pDxbc,
235238
// 6. Cleanup/Optimize DXIL.
236239
Optimize();
237240

241+
// 7. Callback after conversion
242+
PostConvertHook(pByteCode);
243+
238244
// Serialize DXIL.
239245
SmallVector<char, 4*1024> DxilBuffer;
240246
SerializeDxil(DxilBuffer);
@@ -385,6 +391,9 @@ void DxbcConverter::ConvertInDriverImpl(_In_reads_bytes_(8) const UINT32 *pByteC
385391
ConvertSignature(*m_pPatchConstantSignature, m_pPR->GetPatchConstOrPrimSignature());
386392
}
387393

394+
// 3.5. Callback before conversion
395+
PreConvertHook(pByteCode);
396+
388397
// 4. Transform DXBC to DXIL.
389398
Parser.SetShader(pByteCode);
390399
ConvertInstructions(Parser);
@@ -395,6 +404,9 @@ void DxbcConverter::ConvertInDriverImpl(_In_reads_bytes_(8) const UINT32 *pByteC
395404
// 6. Cleanup/Optimize DXIL.
396405
Optimize();
397406

407+
// 7. Callback after conversion
408+
PostConvertHook(pByteCode);
409+
398410
// Serialize DXIL.
399411
SmallVector<char, 8*1024> DxilBuffer;
400412
raw_svector_ostream DxilStream(DxilBuffer);
@@ -3653,6 +3665,8 @@ void DxbcConverter::ConvertInstructions(D3D10ShaderBinary::CShaderCodeParser &Pa
36533665
// Resource.
36543666
OperandValue InRes;
36553667
LoadOperand(InRes, Inst, uOpUAV, CMask::MakeXMask(), CompType::getInvalid());
3668+
// SetHasCounter.
3669+
SetHasCounter(Inst, uOpUAV);
36563670

36573671
// Create BufferUpdateCounter call.
36583672
Value *Args[3];
@@ -4509,6 +4523,14 @@ void DxbcConverter::LogConvertResult(bool InDriver, _In_ const LARGE_INTEGER *pQ
45094523
// intentionaly empty - override to report conversion results
45104524
}
45114525

4526+
HRESULT DxbcConverter::PreConvertHook(const CShaderToken *pByteCode) {
4527+
return S_OK;
4528+
}
4529+
4530+
HRESULT DxbcConverter::PostConvertHook(const CShaderToken *pByteCode) {
4531+
return S_OK;
4532+
}
4533+
45124534
void DxbcConverter::HandleUnknownInstruction(D3D10ShaderBinary::CInstruction &Inst) {
45134535
DXASSERT_ARGS(false, "OpCode %u is not yet implemented", Inst.OpCode());
45144536
}
@@ -5707,6 +5729,18 @@ Value *DxbcConverter::LoadConstFloat(float& fVal) {
57075729
}
57085730
}
57095731

5732+
void DxbcConverter::SetHasCounter(D3D10ShaderBinary::CInstruction &Inst, const unsigned uOpUAV) {
5733+
D3D10ShaderBinary::COperandBase &O = Inst.m_Operands[uOpUAV];
5734+
DXASSERT_DXBC(O.m_Type == D3D11_SB_OPERAND_TYPE_UNORDERED_ACCESS_VIEW);
5735+
5736+
// Retrieve UAV range ID and record.
5737+
DXASSERT_DXBC(O.m_IndexType[0] == D3D10_SB_OPERAND_INDEX_IMMEDIATE32);
5738+
unsigned RangeID = O.m_Index[0].m_RegIndex;
5739+
unsigned RecIdx = m_UAVRangeMap[RangeID];
5740+
DxilResource &R = m_pPR->GetUAV(RecIdx);
5741+
R.SetHasCounter(true);
5742+
}
5743+
57105744
void DxbcConverter::LoadOperand(OperandValue &SrcVal,
57115745
D3D10ShaderBinary::CInstruction &Inst,
57125746
const unsigned OpIdx,
@@ -6365,6 +6399,7 @@ void DxbcConverter::LoadOperand(OperandValue &SrcVal,
63656399
case D3D11_SB_OPERAND_TYPE_INPUT_JOIN_INSTANCE_ID: {
63666400
Scope &HullScope = m_ScopeStack.FindParentHullLoop();
63676401
Value *pValue = m_pBuilder->CreateLoad(HullScope.pInductionVar);
6402+
pValue = ApplyOperandModifiers(pValue, O);
63686403

63696404
for (OperandValueHelper OVH(SrcVal, Mask, O); !OVH.IsDone(); OVH.Advance()) {
63706405
OVH.SetValue(pValue);

projects/dxilconv/lib/DxbcConverter/DxbcConverterImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,8 @@ class DxbcConverter : public IDxbcConverter {
528528

529529

530530
// Callbacks added to support conversion of custom intrinsics.
531+
virtual HRESULT PreConvertHook(const CShaderToken *pByteCode);
532+
virtual HRESULT PostConvertHook(const CShaderToken *pByteCode);
531533
virtual void HandleUnknownInstruction(D3D10ShaderBinary::CInstruction &Inst);
532534
virtual unsigned GetResourceSlot(D3D10ShaderBinary::CInstruction &Inst);
533535

@@ -605,6 +607,7 @@ class DxbcConverter : public IDxbcConverter {
605607
void CheckDxbcString(const char *pStr, const void *pMaxPtrInclusive);
606608

607609
Value *LoadConstFloat(float& fVal);
610+
void SetHasCounter(D3D10ShaderBinary::CInstruction &Inst, const unsigned uOpUAV);
608611
void LoadOperand(OperandValue &SrcVal, D3D10ShaderBinary::CInstruction &Inst, const unsigned OpIdx, const CMask &Mask, const CompType &ValueType);
609612
const DxilResource& LoadSRVOperand(OperandValue &SrcVal, D3D10ShaderBinary::CInstruction &Inst, const unsigned OpIdx, const CMask &Mask, const CompType &ValueType);
610613
const DxilResource& GetSRVFromOperand(D3D10ShaderBinary::CInstruction &Inst, const unsigned OpIdx);

projects/dxilconv/lib/DxilConvPasses/NormalizeDxil.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,6 @@ bool NormalizeResourceHandle::Run(Function &function, DominatorTree &DT) {
143143
return candidates.size() > 0;
144144
}
145145

146-
class NormalizeDxil {
147-
public:
148-
NormalizeDxil(Function &F, DominatorTree &DT) : m_function(F), m_dominatorTree(DT) {}
149-
150-
bool Run();
151-
152-
private:
153-
Function &m_function;
154-
DominatorTree &m_dominatorTree;
155-
};
156-
157-
158146
bool NormalizeDxil::Run() {
159147
return NormalizeResourceHandle().Run(m_function, m_dominatorTree);
160148
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %fxc /Tps_5_0 %s /Fo %t.fxc
2+
// RUN: %dxbc2dxil %t.fxc /emit-llvm | %FileCheck %s -check-prefix=DXIL
3+
4+
// DXIL: !{i32 0, %dx.types.i8x28 addrspace(1)* undef, !"U0", i32 0, i32 5, i32 1, i32 12, i1 false, i1 true
5+
6+
struct X {
7+
float4 a;
8+
float3 b;
9+
};
10+
11+
AppendStructuredBuffer<X> buf : register(u5);
12+
13+
#ifdef DX12
14+
#define RS "DescriptorTable(" \
15+
"UAV(u5), "\
16+
"visibility=SHADER_VISIBILITY_ALL)"
17+
18+
[RootSignature( RS )]
19+
#endif
20+
float4 main(float i : A) : SV_TARGET
21+
{
22+
X x = (X)0;
23+
x.a = i;
24+
x.b = i + 1;
25+
buf.Append(x);
26+
return x.a;
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %fxc /Tps_5_0 %s /Fo %t.fxc
2+
// RUN: %dxbc2dxil %t.fxc /emit-llvm | %FileCheck %s -check-prefix=DXIL
3+
4+
// DXIL: !{i32 0, %dx.types.i8x36 addrspace(1)* undef, !"U0", i32 0, i32 5, i32 1, i32 12, i1 false, i1 true
5+
6+
struct X {
7+
float4 a;
8+
float3 b;
9+
float2 c;
10+
};
11+
12+
ConsumeStructuredBuffer<X> buf : register(u5);
13+
14+
#ifdef DX12
15+
#define RS "DescriptorTable(" \
16+
"UAV(u5), "\
17+
"visibility=SHADER_VISIBILITY_ALL)"
18+
19+
[RootSignature( RS )]
20+
#endif
21+
float4 main(int i : A) : SV_TARGET
22+
{
23+
return buf.Consume().a;
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %fxc /Tps_5_0 %s /Fo %t.fxc
2+
// RUN: %dxbc2dxil %t.fxc /emit-llvm | %FileCheck %s -check-prefix=DXIL
3+
4+
// DXIL: !{i32 0, %dx.types.i8x28 addrspace(1)* undef, !"U0", i32 0, i32 1, i32 1, i32 12, i1 false, i1 true
5+
6+
struct Foo {
7+
float4 a;
8+
float3 b;
9+
};
10+
11+
12+
RWStructuredBuffer<Foo> buf2;
13+
14+
int main() : SV_Target
15+
{
16+
return buf2.IncrementCounter();
17+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %fxc /Ths_5_0 %s /Fo %t.dxbc
2+
// RUN: %dxbc2dxil %t.dxbc /emit-llvm | %FileCheck %s
3+
4+
// RUN: %fxc /Ths_5_1 /DDX12 %s /Fo %t.dxbc
5+
// RUN: %dxbc2dxil %t.dxbc /emit-llvm | %FileCheck %s
6+
7+
// CHECK: hullloop0:
8+
// CHECK: %[[InstanceID:.*]] = phi i32
9+
// CHECK: sub i32 0, %[[InstanceID:.*]]
10+
11+
#ifndef DX12
12+
#define RS
13+
#else
14+
#define RS [RootSignature("CBV(b2)")]
15+
#endif
16+
17+
struct Vertex
18+
{
19+
float4 pos : SV_POSITION;
20+
float4 c : COLOR;
21+
};
22+
23+
struct HullShaderConstantData
24+
{
25+
float Edges[4] : SV_TessFactor;
26+
float Inside[2] : SV_InsideTessFactor;
27+
};
28+
29+
30+
cbuffer Buf0 : register(b2) {
31+
uint foo;
32+
}
33+
34+
HullShaderConstantData ConstantFunction( InputPatch<Vertex, 5> In )
35+
{
36+
HullShaderConstantData Out = (HullShaderConstantData)0;
37+
for (int i = 0; i<4; ++i) {
38+
Out.Edges[i] += In[foo - i].c.w;
39+
}
40+
return Out;
41+
}
42+
43+
RS
44+
[domain("quad")]
45+
[partitioning("integer")]
46+
[outputtopology("point")]
47+
[outputcontrolpoints(0)]
48+
[patchconstantfunc("ConstantFunction")]
49+
void main( InputPatch<Vertex, 5> In, uint i : SV_OutputControlPointID )
50+
{
51+
}

0 commit comments

Comments
 (0)