Skip to content

Commit 6d16791

Browse files
tex3djeffnn
andauthored
PIX passes: Centralize handle-generation code and update for 6.6 (#3628) (#3638)
This is little more than a move of the "create-uav" code in lib/DxilPIXPasses/DxilShaderAccessTracking.cpp to PixPassHelpers.cpp, followed by a factoring-out of the parts that create a handle (either pre-SM6.6 fashion, or with the newer create-from-binding etc.). All the other passes' near-identical code was then deleted and made to call the centralized function. (cherry picked from commit ea1efe9) Co-authored-by: Jeff Noyle <[email protected]>
1 parent 358918f commit 6d16791

7 files changed

Lines changed: 168 additions & 281 deletions

lib/DxilPIXPasses/DxilAddPixelHitInstrumentation.cpp

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "llvm/IR/PassManager.h"
2222
#include "llvm/Transforms/Utils/Local.h"
2323

24+
#include "PixPassHelpers.h"
25+
2426
using namespace llvm;
2527
using namespace hlsl;
2628

@@ -102,59 +104,7 @@ bool DxilAddPixelHitInstrumentation::runOnModule(Module &M) {
102104
IRBuilder<> Builder(
103105
dxilutil::FirstNonAllocaInsertionPt(DM.GetEntryFunction()));
104106

105-
unsigned int UAVResourceHandle =
106-
static_cast<unsigned int>(DM.GetUAVs().size());
107-
108-
// Set up a UAV with structure of a single int
109-
SmallVector<llvm::Type *, 1> Elements{Type::getInt32Ty(Ctx)};
110-
llvm::StructType *UAVStructTy =
111-
llvm::StructType::create(Elements, "class.RWStructuredBuffer");
112-
std::unique_ptr<DxilResource> pUAV = llvm::make_unique<DxilResource>();
113-
pUAV->SetGlobalName("PIX_CountUAVName");
114-
pUAV->SetGlobalSymbol(UndefValue::get(UAVStructTy->getPointerTo()));
115-
pUAV->SetID(UAVResourceHandle);
116-
pUAV->SetSpaceID(
117-
(unsigned int)-2); // This is the reserved-for-tools register space
118-
pUAV->SetSampleCount(1);
119-
pUAV->SetGloballyCoherent(false);
120-
pUAV->SetHasCounter(false);
121-
pUAV->SetCompType(CompType::getI32());
122-
pUAV->SetLowerBound(0);
123-
pUAV->SetRangeSize(1);
124-
pUAV->SetKind(DXIL::ResourceKind::RawBuffer);
125-
pUAV->SetRW(true);
126-
127-
auto pAnnotation = DM.GetTypeSystem().GetStructAnnotation(UAVStructTy);
128-
if (pAnnotation == nullptr) {
129-
pAnnotation = DM.GetTypeSystem().AddStructAnnotation(UAVStructTy);
130-
pAnnotation->GetFieldAnnotation(0).SetCBufferOffset(0);
131-
pAnnotation->GetFieldAnnotation(0).SetCompType(
132-
hlsl::DXIL::ComponentType::I32);
133-
pAnnotation->GetFieldAnnotation(0).SetFieldName("count");
134-
}
135-
136-
ID = DM.AddUAV(std::move(pUAV));
137-
138-
assert((unsigned)ID == UAVResourceHandle);
139-
140-
// Create handle for the newly-added UAV
141-
Function *CreateHandleOpFunc =
142-
HlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(Ctx));
143-
Constant *CreateHandleOpcodeArg =
144-
HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
145-
Constant *UAVArg = HlslOP->GetI8Const(
146-
static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
147-
DXIL::ResourceClass::UAV));
148-
Constant *MetaDataArg =
149-
HlslOP->GetU32Const(ID); // position of the metadata record in the
150-
// corresponding metadata list
151-
Constant *IndexArg = HlslOP->GetU32Const(0); //
152-
Constant *FalseArg =
153-
HlslOP->GetI1Const(0); // non-uniform resource index: false
154-
HandleForUAV = Builder.CreateCall(
155-
CreateHandleOpFunc,
156-
{CreateHandleOpcodeArg, UAVArg, MetaDataArg, IndexArg, FalseArg},
157-
"PIX_CountUAV_Handle");
107+
HandleForUAV = PIXPassHelpers::CreateUAV(DM, Builder, 0, "PIX_CountUAV_Handle");
158108

159109
DM.ReEmitDxilResources();
160110
}

lib/DxilPIXPasses/DxilDebugInstrumentation.cpp

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ class DxilDebugInstrumentation : public ModulePass {
252252

253253
private:
254254
SystemValueIndices addRequiredSystemValues(BuilderContext &BC);
255-
void addUAV(BuilderContext &BC);
256255
void addInvocationSelectionProlog(BuilderContext &BC,
257256
SystemValueIndices SVIndices);
258257
Value *addPixelShaderProlog(BuilderContext &BC, SystemValueIndices SVIndices);
@@ -548,52 +547,6 @@ DxilDebugInstrumentation::addPixelShaderProlog(BuilderContext &BC,
548547
return ComparePos;
549548
}
550549

551-
void DxilDebugInstrumentation::addUAV(BuilderContext &BC) {
552-
// Set up a UAV with structure of a single int
553-
unsigned int UAVResourceHandle =
554-
static_cast<unsigned int>(BC.DM.GetUAVs().size());
555-
SmallVector<llvm::Type *, 1> Elements{Type::getInt32Ty(BC.Ctx)};
556-
llvm::StructType *UAVStructTy =
557-
llvm::StructType::create(Elements, "PIX_DebugUAV_Type");
558-
std::unique_ptr<DxilResource> pUAV = llvm::make_unique<DxilResource>();
559-
pUAV->SetGlobalName("PIX_DebugUAVName");
560-
pUAV->SetGlobalSymbol(UndefValue::get(UAVStructTy->getPointerTo()));
561-
pUAV->SetID(UAVResourceHandle);
562-
pUAV->SetSpaceID(
563-
(unsigned int)-2); // This is the reserved-for-tools register space
564-
pUAV->SetSampleCount(1);
565-
pUAV->SetGloballyCoherent(false);
566-
pUAV->SetHasCounter(false);
567-
pUAV->SetCompType(CompType::getI32());
568-
pUAV->SetLowerBound(0);
569-
pUAV->SetRangeSize(1);
570-
pUAV->SetKind(DXIL::ResourceKind::RawBuffer);
571-
pUAV->SetRW(true);
572-
573-
auto ID = BC.DM.AddUAV(std::move(pUAV));
574-
assert(ID == UAVResourceHandle);
575-
576-
BC.DM.m_ShaderFlags.SetEnableRawAndStructuredBuffers(true);
577-
578-
// Create handle for the newly-added UAV
579-
Function *CreateHandleOpFunc =
580-
BC.HlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(BC.Ctx));
581-
Constant *CreateHandleOpcodeArg =
582-
BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
583-
Constant *UAVVArg = BC.HlslOP->GetI8Const(
584-
static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
585-
DXIL::ResourceClass::UAV));
586-
Constant *MetaDataArg = BC.HlslOP->GetU32Const(
587-
ID); // position of the metadata record in the corresponding metadata list
588-
Constant *IndexArg = BC.HlslOP->GetU32Const(0); //
589-
Constant *FalseArg =
590-
BC.HlslOP->GetI1Const(0); // non-uniform resource index: false
591-
m_HandleForUAV = BC.Builder.CreateCall(
592-
CreateHandleOpFunc,
593-
{CreateHandleOpcodeArg, UAVVArg, MetaDataArg, IndexArg, FalseArg},
594-
"PIX_DebugUAV_Handle");
595-
}
596-
597550
void DxilDebugInstrumentation::addInvocationSelectionProlog(
598551
BuilderContext &BC, SystemValueIndices SVIndices) {
599552
auto ShaderModel = BC.DM.GetShaderModel();
@@ -959,7 +912,8 @@ bool DxilDebugInstrumentation::runOnModule(Module &M) {
959912

960913
BuilderContext BC{M, DM, Ctx, HlslOP, Builder};
961914

962-
addUAV(BC);
915+
m_HandleForUAV = PIXPassHelpers::CreateUAV(BC.DM, BC.Builder, 0, "PIX_DebugUAV_Handle");
916+
963917
auto SystemValues = addRequiredSystemValues(BC);
964918
addInvocationSelectionProlog(BC, SystemValues);
965919
addInvocationStartMarker(BC);

lib/DxilPIXPasses/DxilOutputColorBecomesConstant.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include "llvm/Transforms/Utils/Local.h"
2121
#include <array>
2222

23+
#include "PixPassHelpers.h"
24+
2325
using namespace llvm;
2426
using namespace hlsl;
2527

@@ -166,32 +168,17 @@ bool DxilOutputColorBecomesConstant::runOnModule(Module &M) {
166168
pCBuf->SetRangeSize(1);
167169
pCBuf->SetSize(4);
168170

169-
ID = DM.AddCBuffer(std::move(pCBuf));
170-
171171
Instruction *entryPointInstruction =
172172
&*(DM.GetEntryFunction()->begin()->begin());
173173
IRBuilder<> Builder(entryPointInstruction);
174174

175175
// Create handle for the newly-added constant buffer (which is achieved via
176176
// a function call)
177177
auto ConstantBufferName = "PIX_Constant_Color_CB_Handle";
178-
Function *createHandle =
179-
HlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(Ctx));
180-
Constant *CreateHandleOpcodeArg =
181-
HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
182-
Constant *CBVArg = HlslOP->GetI8Const(
183-
static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
184-
DXIL::ResourceClass::CBuffer));
185-
Constant *MetaDataArg =
186-
HlslOP->GetU32Const(ID); // position of the metadata record in the
187-
// corresponding metadata list
188-
Constant *IndexArg = HlslOP->GetU32Const(0); //
189-
Constant *FalseArg =
190-
HlslOP->GetI1Const(0); // non-uniform resource index: false
191-
CallInst *callCreateHandle = Builder.CreateCall(
192-
createHandle,
193-
{CreateHandleOpcodeArg, CBVArg, MetaDataArg, IndexArg, FalseArg},
194-
ConstantBufferName);
178+
179+
CallInst* callCreateHandle = PIXPassHelpers::CreateHandleForResource(DM, Builder, pCBuf.get(), ConstantBufferName);
180+
181+
DM.AddCBuffer(std::move(pCBuf));
195182

196183
DM.ReEmitDxilResources();
197184

lib/DxilPIXPasses/DxilPIXMeshShaderOutputInstrumentation.cpp

Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <winerror.h>
2929
#endif
3030

31+
#include "PixPassHelpers.h"
32+
3133
// Keep these in sync with the same-named value in the debugger application's
3234
// WinPixShaderUtils.h
3335

@@ -72,7 +74,6 @@ class DxilPIXMeshShaderOutputInstrumentation : public ModulePass
7274
IRBuilder<> &Builder;
7375
};
7476

75-
CallInst *addUAV(BuilderContext &BC);
7677
Value *insertInstructionsToCalculateFlattenedGroupIdXandY(BuilderContext &BC);
7778
Value *insertInstructionsToCalculateGroupIdZ(BuilderContext &BC);
7879
Value *reserveDebugEntrySpace(BuilderContext &BC, uint32_t SpaceInBytes);
@@ -92,53 +93,6 @@ uint32_t DxilPIXMeshShaderOutputInstrumentation::UAVDumpingGroundOffset()
9293
return static_cast<uint32_t>(m_UAVSize - DebugBufferDumpingGroundSize);
9394
}
9495

95-
CallInst *DxilPIXMeshShaderOutputInstrumentation::addUAV(BuilderContext &BC)
96-
{
97-
// Set up a UAV with structure of a single int
98-
unsigned int UAVResourceHandle =
99-
static_cast<unsigned int>(BC.DM.GetUAVs().size());
100-
SmallVector<llvm::Type *, 1> Elements{Type::getInt32Ty(BC.Ctx)};
101-
llvm::StructType *UAVStructTy =
102-
llvm::StructType::create(Elements, "PIX_DebugUAV_Type");
103-
std::unique_ptr<DxilResource> pUAV = llvm::make_unique<DxilResource>();
104-
pUAV->SetGlobalName("PIX_DebugUAVName");
105-
pUAV->SetGlobalSymbol(UndefValue::get(UAVStructTy->getPointerTo()));
106-
pUAV->SetID(UAVResourceHandle);
107-
pUAV->SetSpaceID(
108-
(unsigned int)-2); // This is the reserved-for-tools register space
109-
pUAV->SetSampleCount(1);
110-
pUAV->SetGloballyCoherent(false);
111-
pUAV->SetHasCounter(false);
112-
pUAV->SetCompType(CompType::getI32());
113-
pUAV->SetLowerBound(0);
114-
pUAV->SetRangeSize(1);
115-
pUAV->SetKind(DXIL::ResourceKind::RawBuffer);
116-
pUAV->SetRW(true);
117-
118-
auto ID = BC.DM.AddUAV(std::move(pUAV));
119-
assert(ID == UAVResourceHandle);
120-
121-
BC.DM.m_ShaderFlags.SetEnableRawAndStructuredBuffers(true);
122-
123-
// Create handle for the newly-added UAV
124-
Function *CreateHandleOpFunc =
125-
BC.HlslOP->GetOpFunc(DXIL::OpCode::CreateHandle, Type::getVoidTy(BC.Ctx));
126-
Constant *CreateHandleOpcodeArg =
127-
BC.HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
128-
Constant *UAVVArg = BC.HlslOP->GetI8Const(
129-
static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
130-
DXIL::ResourceClass::UAV));
131-
Constant *MetaDataArg = BC.HlslOP->GetU32Const(
132-
ID); // position of the metadata record in the corresponding metadata list
133-
Constant *IndexArg = BC.HlslOP->GetU32Const(0); //
134-
Constant *FalseArg =
135-
BC.HlslOP->GetI1Const(0); // non-uniform resource index: false
136-
return BC.Builder.CreateCall(
137-
CreateHandleOpFunc,
138-
{CreateHandleOpcodeArg, UAVVArg, MetaDataArg, IndexArg, FalseArg},
139-
"PIX_DebugUAV_Handle");
140-
}
141-
14296
Value *DxilPIXMeshShaderOutputInstrumentation::
14397
insertInstructionsToCalculateFlattenedGroupIdXandY(BuilderContext &BC)
14498
{
@@ -275,7 +229,7 @@ bool DxilPIXMeshShaderOutputInstrumentation::runOnModule(Module &M)
275229

276230
m_OffsetMask = BC.HlslOP->GetU32Const(UAVDumpingGroundOffset() - 1);
277231

278-
m_OutputUAV = addUAV(BC);
232+
m_OutputUAV = PIXPassHelpers::CreateUAV(DM, Builder, 0, "PIX_DebugUAV_Handle");
279233

280234
auto GroupIdXandY = insertInstructionsToCalculateFlattenedGroupIdXandY(BC);
281235
auto GroupIdZ = insertInstructionsToCalculateGroupIdZ(BC);

lib/DxilPIXPasses/DxilShaderAccessTracking.cpp

Lines changed: 4 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "llvm/Transforms/Utils/Local.h"
2626
#include <deque>
2727

28+
#include "PixPassHelpers.h"
29+
2830
#ifdef _WIN32
2931
#include <winerror.h>
3032
#endif
@@ -620,104 +622,6 @@ DxilShaderAccessTracking::GetResourceFromHandle(Value *resHandle,
620622
return ret;
621623
}
622624

623-
static bool IsDynamicResourceShaderModel(DxilModule& DM) {
624-
return DM.GetShaderModel()->IsSMAtLeast(6, 6);
625-
}
626-
627-
// Set up a UAV with structure of a single int
628-
static llvm::CallInst* CreateUAV(DxilModule & DM, IRBuilder<> & Builder, unsigned int UAVResourceHandle, unsigned int bind, const char * name)
629-
{
630-
LLVMContext& Ctx = DM.GetModule()->getContext();
631-
632-
SmallVector<llvm::Type*, 1> Elements{ Type::getInt32Ty(Ctx) };
633-
llvm::StructType* UAVStructTy =
634-
llvm::StructType::create(Elements, "class.RWStructuredBuffer");
635-
std::unique_ptr<DxilResource> pUAV =
636-
llvm::make_unique<DxilResource>();
637-
pUAV->SetGlobalName((std::string("PIX_CountUAVName")+ std::to_string(UAVResourceHandle)).c_str());
638-
pUAV->SetGlobalSymbol(UndefValue::get(UAVStructTy->getPointerTo()));
639-
pUAV->SetID(UAVResourceHandle);
640-
pUAV->SetRW(true); //sets UAV class
641-
pUAV->SetSpaceID((
642-
unsigned int)-2); // This is the reserved-for-tools register space
643-
pUAV->SetSampleCount(1);
644-
pUAV->SetGloballyCoherent(false);
645-
pUAV->SetHasCounter(false);
646-
pUAV->SetCompType(CompType::getI32());
647-
pUAV->SetLowerBound(0);
648-
pUAV->SetRangeSize(1);
649-
pUAV->SetKind(DXIL::ResourceKind::RawBuffer);
650-
651-
auto pAnnotation =
652-
DM.GetTypeSystem().GetStructAnnotation(UAVStructTy);
653-
if (pAnnotation == nullptr) {
654-
655-
pAnnotation = DM.GetTypeSystem().AddStructAnnotation(UAVStructTy);
656-
pAnnotation->GetFieldAnnotation(0).SetCBufferOffset(0);
657-
pAnnotation->GetFieldAnnotation(0).SetCompType(
658-
hlsl::DXIL::ComponentType::I32);
659-
pAnnotation->GetFieldAnnotation(0).SetFieldName("count");
660-
}
661-
662-
OP *HlslOP = DM.GetOP();
663-
664-
// Create handle for the newly-added UAV
665-
if (IsDynamicResourceShaderModel(DM)) {
666-
Function *CreateHandleFromBindingOpFunc =
667-
HlslOP->GetOpFunc(DXIL::OpCode::CreateHandleFromBinding, Type::getVoidTy(Ctx));
668-
Constant *CreateHandleFromBindingOpcodeArg =
669-
HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandleFromBinding);
670-
DxilResourceBinding binding =
671-
resource_helper::loadBindingFromResourceBase(pUAV.get());
672-
Value *bindingV = resource_helper::getAsConstant(
673-
binding, HlslOP->GetResourceBindingType(), *DM.GetShaderModel());
674-
675-
Value *registerIndex = HlslOP->GetU32Const(UAVResourceHandle);
676-
677-
Value *isUniformRes = HlslOP->GetI1Const(0);
678-
679-
Value *createHandleFromBindingArgs[] = { CreateHandleFromBindingOpcodeArg, bindingV, registerIndex,
680-
isUniformRes};
681-
682-
auto * handle = Builder.CreateCall(
683-
CreateHandleFromBindingOpFunc,
684-
createHandleFromBindingArgs,
685-
name);
686-
687-
Function *annotHandleFn = HlslOP->GetOpFunc(DXIL::OpCode::AnnotateHandle, Type::getVoidTy(Ctx));
688-
Value *annotHandleArg = HlslOP->GetI32Const((unsigned)DXIL::OpCode::AnnotateHandle);
689-
DxilResourceProperties RP = resource_helper::loadPropsFromResourceBase(pUAV.get());
690-
Type *resPropertyTy = HlslOP->GetResourcePropertiesType();
691-
Value *propertiesV = resource_helper::getAsConstant(RP, resPropertyTy, *DM.GetShaderModel());
692-
693-
unsigned int ID = DM.AddUAV(std::move(pUAV));
694-
DXASSERT_LOCALVAR_NOMSG(ID, (unsigned)ID == UAVResourceHandle);
695-
696-
return Builder.CreateCall(annotHandleFn, {annotHandleArg, handle, propertiesV});
697-
} else {
698-
unsigned int ID = DM.AddUAV(std::move(pUAV));
699-
DXASSERT_NOMSG((unsigned)ID == UAVResourceHandle);
700-
701-
Function* CreateHandleOpFunc = HlslOP->GetOpFunc(
702-
DXIL::OpCode::CreateHandle, Type::getVoidTy(Ctx));
703-
Constant* CreateHandleOpcodeArg =
704-
HlslOP->GetU32Const((unsigned)DXIL::OpCode::CreateHandle);
705-
Constant* UAVArg = HlslOP->GetI8Const(
706-
static_cast<std::underlying_type<DxilResourceBase::Class>::type>(
707-
DXIL::ResourceClass::UAV));
708-
Constant* MetaDataArg =
709-
HlslOP->GetU32Const(ID); // position of the metadata record in the
710-
// corresponding metadata list
711-
Constant* IndexArg = HlslOP->GetU32Const(0); //
712-
Constant* FalseArg =
713-
HlslOP->GetI1Const(0); // non-uniform resource index: false
714-
return Builder.CreateCall(
715-
CreateHandleOpFunc,
716-
{ CreateHandleOpcodeArg, UAVArg, MetaDataArg, IndexArg, FalseArg },
717-
name);
718-
}
719-
}
720-
721625
static uint32_t EncodeShaderModel(DXIL::ShaderKind kind)
722626
{
723627
DXASSERT_NOMSG(static_cast<int>(DXIL::ShaderKind::Invalid) <= 16);
@@ -771,15 +675,12 @@ bool DxilShaderAccessTracking::runOnModule(Module &M) {
771675
FOS << "ShouldAssumeDsvAccess";
772676
}
773677
}
774-
678+
int uavRegId = 0;
775679
for (llvm::Function &F : M.functions()) {
776680
if (!F.getBasicBlockList().empty()) {
777681
IRBuilder<> Builder(F.getEntryBlock().getFirstInsertionPt());
778682

779-
unsigned int UAVResourceHandle =
780-
static_cast<unsigned int>(DM.GetUAVs().size());
781-
782-
m_FunctionToUAVHandle[&F] = CreateUAV(DM, Builder, UAVResourceHandle, 0, "PIX_CountUAV_Handle");
683+
m_FunctionToUAVHandle[&F] = PIXPassHelpers::CreateUAV(DM, Builder, uavRegId++, "PIX_CountUAV_Handle");
783684
auto const* shaderModel = DM.GetShaderModel();
784685
auto shaderKind = shaderModel->GetKind();
785686
OP *HlslOP = DM.GetOP();

0 commit comments

Comments
 (0)