Skip to content

[DirectX] Match DXC's resource order in DX container #130233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/include/llvm/BinaryFormat/DXContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ ArrayRef<EnumEntry<ResourceKind>> getResourceKinds();

#define RESOURCE_FLAG(Index, Enum) bool Enum = false;
struct ResourceFlags {
ResourceFlags() {};
ResourceFlags() : Flags(0U) {};
struct FlagsBits {
#include "llvm/BinaryFormat/DXContainerConstants.def"
};
Expand Down
94 changes: 57 additions & 37 deletions llvm/lib/Target/DirectX/DXContainerGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,51 +186,71 @@ void DXContainerGlobals::addResourcesForPSV(Module &M, PSVRuntimeInfo &PSV) {
DXILResourceTypeMap &DRTM =
getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();

for (const dxil::ResourceBindingInfo &RBI : DBM) {
auto MakeBinding =
[](const dxil::ResourceBindingInfo::ResourceBinding &Binding,
const dxbc::PSV::ResourceType Type, const dxil::ResourceKind Kind,
const dxbc::PSV::ResourceFlags Flags = dxbc::PSV::ResourceFlags()) {
dxbc::PSV::v2::ResourceBindInfo BindInfo;
BindInfo.Type = Type;
BindInfo.LowerBound = Binding.LowerBound;
BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
BindInfo.Space = Binding.Space;
BindInfo.Kind = static_cast<dxbc::PSV::ResourceKind>(Kind);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this conversion be safer done with a helper function or constructor? I couldn't find the definition for dxbc::PSV::ResourceKind but dxil::ResourceKind was just an enum. Seems like something that we could accidentally break ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dxbc::PSV::ResourceKind and dxil::ResourceKind happen to be the exact same enum. One is defined in DXContainerConstants and the other in DXILABI, but if they didn't match exactly we would have a serious problem.

This means that technically the static cast is safe - but it's definitely a break in abstraction. Arguably we should tie these two enums together in a clearer way, though I'm not sure exactly where that should best happen.

In any case, I think that should be out of scope of this PR - I am just moving this cast after all, not introducing it here.

BindInfo.Flags = Flags;
return BindInfo;
};

for (const dxil::ResourceBindingInfo &RBI : DBM.cbuffers()) {
const dxil::ResourceBindingInfo::ResourceBinding &Binding =
RBI.getBinding();
PSV.Resources.push_back(MakeBinding(Binding, dxbc::PSV::ResourceType::CBV,
dxil::ResourceKind::CBuffer));
}
for (const dxil::ResourceBindingInfo &RBI : DBM.samplers()) {
const dxil::ResourceBindingInfo::ResourceBinding &Binding =
RBI.getBinding();
PSV.Resources.push_back(MakeBinding(Binding,
dxbc::PSV::ResourceType::Sampler,
dxil::ResourceKind::Sampler));
}
for (const dxil::ResourceBindingInfo &RBI : DBM.srvs()) {
const dxil::ResourceBindingInfo::ResourceBinding &Binding =
RBI.getBinding();
dxbc::PSV::v2::ResourceBindInfo BindInfo;
BindInfo.LowerBound = Binding.LowerBound;
BindInfo.UpperBound = Binding.LowerBound + Binding.Size - 1;
BindInfo.Space = Binding.Space;

dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
dxbc::PSV::ResourceType ResType = dxbc::PSV::ResourceType::Invalid;
bool IsUAV = TypeInfo.getResourceClass() == dxil::ResourceClass::UAV;
switch (TypeInfo.getResourceKind()) {
case dxil::ResourceKind::Sampler:
ResType = dxbc::PSV::ResourceType::Sampler;
break;
case dxil::ResourceKind::CBuffer:
ResType = dxbc::PSV::ResourceType::CBV;
break;
case dxil::ResourceKind::StructuredBuffer:
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVStructured
: dxbc::PSV::ResourceType::SRVStructured;
if (IsUAV && TypeInfo.getUAV().HasCounter)
ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
break;
case dxil::ResourceKind::RTAccelerationStructure:
dxbc::PSV::ResourceType ResType;
if (TypeInfo.isStruct())
ResType = dxbc::PSV::ResourceType::SRVStructured;
else if (TypeInfo.isTyped())
ResType = dxbc::PSV::ResourceType::SRVTyped;
else
ResType = dxbc::PSV::ResourceType::SRVRaw;
break;
case dxil::ResourceKind::RawBuffer:
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVRaw
: dxbc::PSV::ResourceType::SRVRaw;
break;
default:
ResType = IsUAV ? dxbc::PSV::ResourceType::UAVTyped
: dxbc::PSV::ResourceType::SRVTyped;
break;
}
BindInfo.Type = ResType;

BindInfo.Kind =
static_cast<dxbc::PSV::ResourceKind>(TypeInfo.getResourceKind());

PSV.Resources.push_back(
MakeBinding(Binding, ResType, TypeInfo.getResourceKind()));
}
for (const dxil::ResourceBindingInfo &RBI : DBM.uavs()) {
const dxil::ResourceBindingInfo::ResourceBinding &Binding =
RBI.getBinding();

dxil::ResourceTypeInfo &TypeInfo = DRTM[RBI.getHandleTy()];
dxbc::PSV::ResourceType ResType;
if (TypeInfo.getUAV().HasCounter)
ResType = dxbc::PSV::ResourceType::UAVStructuredWithCounter;
else if (TypeInfo.isStruct())
ResType = dxbc::PSV::ResourceType::UAVStructured;
else if (TypeInfo.isTyped())
ResType = dxbc::PSV::ResourceType::UAVTyped;
else
ResType = dxbc::PSV::ResourceType::UAVRaw;

dxbc::PSV::ResourceFlags Flags;
// TODO: Add support for dxbc::PSV::ResourceFlag::UsedByAtomic64, tracking
// with https://github.com/llvm/llvm-project/issues/104392
BindInfo.Flags.Flags = 0u;
Flags.Flags = 0u;

PSV.Resources.emplace_back(BindInfo);
PSV.Resources.push_back(
MakeBinding(Binding, ResType, TypeInfo.getResourceKind(), Flags));
}
}

Expand Down
26 changes: 26 additions & 0 deletions llvm/test/CodeGen/DirectX/ContainerData/PSVResources-order.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
; RUN: llc %s --filetype=obj -o - | obj2yaml | FileCheck %s

; Check that resources are emitted to the object in the order that matches what
; the DXIL validator expects: CBuffers, Samplers, SRVs, and then UAVs.

; CHECK: Resources:
; CHECK: - Type: CBV
; TODO: - Type: Sampler
; CHECK: - Type: SRVRaw
; CHECK: - Type: UAVTyped

target triple = "dxil-unknown-shadermodel6.0-compute"

define void @main() #0 {
%uav0 = call target("dx.TypedBuffer", i32, 1, 0, 1)
@llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_i32_1_0t(
i32 2, i32 7, i32 1, i32 0, i1 false)
%srv0 = call target("dx.RawBuffer", i8, 0, 0)
@llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i8_0_0t(
i32 1, i32 8, i32 1, i32 0, i1 false)
%cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
@llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)
ret void
}

attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }
11 changes: 11 additions & 0 deletions llvm/test/CodeGen/DirectX/ContainerData/PSVResources.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ target triple = "dxil-unknown-shadermodel6.0-compute"

define void @main() #0 {

; cbuffer : register(b2, space3) { float x; }
; CHECK: - Type: CBV
; CHECK: Space: 3
; CHECK: LowerBound: 2
; CHECK: UpperBound: 2
; CHECK: Kind: CBuffer
; CHECK: Flags:
; CHECK: UsedByAtomic64: false
%cbuf = call target("dx.CBuffer", target("dx.Layout", {float}, 4, 0))
@llvm.dx.resource.handlefrombinding(i32 3, i32 2, i32 1, i32 0, i1 false)

; ByteAddressBuffer Buf : register(t8, space1)
; CHECK: - Type: SRVRaw
; CHECK: Space: 1
Expand Down