Skip to content

[HLSL][DirectX] Extract HLSLBinding out of DXILResource. NFC #150633

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 5 commits into from
Jul 31, 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
82 changes: 11 additions & 71 deletions llvm/include/llvm/Analysis/DXILResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Frontend/HLSL/HLSLBinding.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/PassManager.h"
Expand Down Expand Up @@ -633,86 +634,25 @@ LLVM_ABI ModulePass *createDXILResourceWrapperPassPass();
// register slots to resources with implicit bindings, and in a
// post-optimization validation pass that will raise diagnostic about
// overlapping bindings.
//
// For example for these resource bindings:
//
// RWBuffer<float> A[10] : register(u3);
// RWBuffer<float> B[] : register(u5, space2)
//
// The analysis result for UAV binding type will look like this:
//
// UAVSpaces {
// ResClass = ResourceClass::UAV,
// Spaces = {
// { Space = 0, FreeRanges = {{ 0, 2 }, { 13, UINT32_MAX }} },
// { Space = 2, FreeRanges = {{ 0, 4 }} }
// }
// }
//
class DXILResourceBindingInfo {
public:
struct BindingRange {
uint32_t LowerBound;
uint32_t UpperBound;
BindingRange(uint32_t LB, uint32_t UB) : LowerBound(LB), UpperBound(UB) {}
};

struct RegisterSpace {
uint32_t Space;
SmallVector<BindingRange> FreeRanges;
RegisterSpace(uint32_t Space) : Space(Space) {
FreeRanges.emplace_back(0, UINT32_MAX);
}
// Size == -1 means unbounded array
LLVM_ABI std::optional<uint32_t> findAvailableBinding(int32_t Size);
};

struct BindingSpaces {
dxil::ResourceClass RC;
llvm::SmallVector<RegisterSpace> Spaces;
BindingSpaces(dxil::ResourceClass RC) : RC(RC) {}
LLVM_ABI RegisterSpace &getOrInsertSpace(uint32_t Space);
};

private:
BindingSpaces SRVSpaces, UAVSpaces, CBufferSpaces, SamplerSpaces;
bool ImplicitBinding;
bool OverlappingBinding;
hlsl::BindingInfo Bindings;
bool HasImplicitBinding = false;
bool HasOverlappingBinding = false;

// Populate the resource binding info given explicit resource binding calls
// in the module.
void populate(Module &M, DXILResourceTypeMap &DRTM);

public:
DXILResourceBindingInfo()
: SRVSpaces(dxil::ResourceClass::SRV),
UAVSpaces(dxil::ResourceClass::UAV),
CBufferSpaces(dxil::ResourceClass::CBuffer),
SamplerSpaces(dxil::ResourceClass::Sampler), ImplicitBinding(false),
OverlappingBinding(false) {}

bool hasImplicitBinding() const { return ImplicitBinding; }
void setHasImplicitBinding(bool Value) { ImplicitBinding = Value; }
bool hasOverlappingBinding() const { return OverlappingBinding; }

BindingSpaces &getBindingSpaces(dxil::ResourceClass RC) {
switch (RC) {
case dxil::ResourceClass::SRV:
return SRVSpaces;
case dxil::ResourceClass::UAV:
return UAVSpaces;
case dxil::ResourceClass::CBuffer:
return CBufferSpaces;
case dxil::ResourceClass::Sampler:
return SamplerSpaces;
}
bool hasImplicitBinding() const { return HasImplicitBinding; }
void setHasImplicitBinding(bool Value) { HasImplicitBinding = Value; }
bool hasOverlappingBinding() const { return HasOverlappingBinding; }
void setHasOverlappingBinding(bool Value) { HasOverlappingBinding = Value; }

llvm_unreachable("Invalid resource class");
}

// Size == -1 means unbounded array
LLVM_ABI std::optional<uint32_t>
findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size);
findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size) {
return Bindings.findAvailableBinding(RC, Space, Size);
}

friend class DXILResourceBindingAnalysis;
friend class DXILResourceBindingWrapperPass;
Expand Down
162 changes: 162 additions & 0 deletions llvm/include/llvm/Frontend/HLSL/HLSLBinding.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//===- HLSLBinding.h - Representation for resource bindings in HLSL -------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// \file This file contains objects to represent resource bindings.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_FRONTEND_HLSL_HLSLBINDING_H
#define LLVM_FRONTEND_HLSL_HLSLBINDING_H

#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/DXILABI.h"
#include "llvm/Support/ErrorHandling.h"

namespace llvm {
namespace hlsl {

/// BindingInfo represents the ranges of bindings and free space for each
/// `dxil::ResourceClass`. This can represent HLSL-level bindings as well as
/// bindings described in root signatures, and can be used for analysis of
/// overlapping or missing bindings as well as for finding space for implicit
/// bindings.
///
/// As an example, given these resource bindings:
///
/// RWBuffer<float> A[10] : register(u3);
/// RWBuffer<float> B[] : register(u5, space2)
///
/// The binding info for UAV bindings should look like this:
///
/// UAVSpaces {
/// ResClass = ResourceClass::UAV,
/// Spaces = {
/// { Space = 0u, FreeRanges = {{ 0u, 2u }, { 13u, ~0u }} },
/// { Space = 2u, FreeRanges = {{ 0u, 4u }} }
/// }
/// }
class BindingInfo {
public:
struct BindingRange {
uint32_t LowerBound;
uint32_t UpperBound;
BindingRange(uint32_t LB, uint32_t UB) : LowerBound(LB), UpperBound(UB) {}
};

struct RegisterSpace {
uint32_t Space;
SmallVector<BindingRange> FreeRanges;
RegisterSpace(uint32_t Space) : Space(Space) {
FreeRanges.emplace_back(0, ~0u);
}
// Size == -1 means unbounded array
LLVM_ABI std::optional<uint32_t> findAvailableBinding(int32_t Size);
};

struct BindingSpaces {
dxil::ResourceClass RC;
llvm::SmallVector<RegisterSpace> Spaces;
BindingSpaces(dxil::ResourceClass RC) : RC(RC) {}
LLVM_ABI RegisterSpace &getOrInsertSpace(uint32_t Space);
};

private:
BindingSpaces SRVSpaces{dxil::ResourceClass::SRV};
BindingSpaces UAVSpaces{dxil::ResourceClass::UAV};
BindingSpaces CBufferSpaces{dxil::ResourceClass::CBuffer};
BindingSpaces SamplerSpaces{dxil::ResourceClass::Sampler};

public:
BindingSpaces &getBindingSpaces(dxil::ResourceClass RC) {
switch (RC) {
case dxil::ResourceClass::SRV:
return SRVSpaces;
case dxil::ResourceClass::UAV:
return UAVSpaces;
case dxil::ResourceClass::CBuffer:
return CBufferSpaces;
case dxil::ResourceClass::Sampler:
return SamplerSpaces;
}

llvm_unreachable("Invalid resource class");
}
const BindingSpaces &getBindingSpaces(dxil::ResourceClass RC) const {
return const_cast<BindingInfo *>(this)->getBindingSpaces(RC);
}

// Size == -1 means unbounded array
LLVM_ABI std::optional<uint32_t>
findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size);

friend class BindingInfoBuilder;
};

/// Builder class for creating a /c BindingInfo.
class BindingInfoBuilder {
public:
struct Binding {
dxil::ResourceClass RC;
uint32_t Space;
uint32_t LowerBound;
uint32_t UpperBound;
const void *Cookie;

Binding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound,
uint32_t UpperBound, const void *Cookie)
: RC(RC), Space(Space), LowerBound(LowerBound), UpperBound(UpperBound),
Cookie(Cookie) {}

bool isUnbounded() const { return UpperBound == ~0U; }

bool operator==(const Binding &RHS) const {
return std::tie(RC, Space, LowerBound, UpperBound, Cookie) ==
std::tie(RHS.RC, RHS.Space, RHS.LowerBound, RHS.UpperBound,
RHS.Cookie);
}
bool operator!=(const Binding &RHS) const { return !(*this == RHS); }

bool operator<(const Binding &RHS) const {
return std::tie(RC, Space, LowerBound) <
std::tie(RHS.RC, RHS.Space, RHS.LowerBound);
}
};

private:
SmallVector<Binding> Bindings;

public:
void trackBinding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound,
uint32_t UpperBound, const void *Cookie) {
Bindings.emplace_back(RC, Space, LowerBound, UpperBound, Cookie);
}
/// Calculate the binding info - \c ReportOverlap will be called once for each
/// overlapping binding.
BindingInfo calculateBindingInfo(
llvm::function_ref<void(const BindingInfoBuilder &Builder,
const Binding &Overlapping)>
ReportOverlap);

/// Calculate the binding info - \c HasOverlap will be set to indicate whether
/// there are any overlapping bindings.
BindingInfo calculateBindingInfo(bool &HasOverlap) {
HasOverlap = false;
return calculateBindingInfo(
[&HasOverlap](auto, auto) { HasOverlap = true; });
}

/// For use in the \c ReportOverlap callback of \c calculateBindingInfo -
/// finds a binding that the \c ReportedBinding overlaps with.
const Binding &findOverlapping(const Binding &ReportedBinding) const;
};

} // namespace hlsl
} // namespace llvm

#endif // LLVM_FRONTEND_HLSL_HLSLBINDING_H
1 change: 1 addition & 0 deletions llvm/lib/Analysis/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ add_llvm_component_library(LLVMAnalysis
LINK_COMPONENTS
BinaryFormat
Core
FrontendHLSL
Object
ProfileData
Support
Expand Down
Loading