Skip to content

Commit c95ce68

Browse files
joaosaffranJoao Saffran
authored andcommitted
refactoring
refactoring clean up format formating fix import issues formating refactoring init refactoring adding validation clean implementing finish implementing && fix tests sync parent sync parent address comments fix test fix tests format address changes add preserved addressing comments updating format adding tests clean up address comments adding root constants clean moving code arround clean addressing comments address comments update code cleanup address comments from inbelic
1 parent 81ed756 commit c95ce68

19 files changed

+546
-40
lines changed

llvm/include/llvm/Analysis/DXILResource.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ class DXILResourceTypeMap;
3333

3434
namespace dxil {
3535

36+
inline StringRef getResourceClassName(ResourceClass RC) {
37+
switch (RC) {
38+
case ResourceClass::SRV:
39+
return "SRV";
40+
case ResourceClass::UAV:
41+
return "UAV";
42+
case ResourceClass::CBuffer:
43+
return "CBuffer";
44+
case ResourceClass::Sampler:
45+
return "Sampler";
46+
}
47+
llvm_unreachable("Unhandled ResourceClass");
48+
}
49+
3650
// Returns the resource name from dx_resource_handlefrombinding or
3751
// dx_resource_handlefromimplicitbinding call
3852
LLVM_ABI StringRef getResourceNameFromBindingCall(CallInst *CI);

llvm/include/llvm/Frontend/HLSL/HLSLBinding.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class BindingInfo {
5757
}
5858
// Size == -1 means unbounded array
5959
LLVM_ABI std::optional<uint32_t> findAvailableBinding(int32_t Size);
60+
LLVM_ABI bool isBound(BindingRange B);
6061
};
6162

6263
struct BindingSpaces {
@@ -95,6 +96,8 @@ class BindingInfo {
9596
LLVM_ABI std::optional<uint32_t>
9697
findAvailableBinding(dxil::ResourceClass RC, uint32_t Space, int32_t Size);
9798

99+
LLVM_ABI bool isBound(dxil::ResourceClass RC, uint32_t Space, BindingRange B);
100+
98101
friend class BindingInfoBuilder;
99102
};
100103

llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,125 @@
1616

1717
#include "llvm/ADT/IntervalMap.h"
1818
#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
19-
#include "llvm/Support/Compiler.h"
2019

2120
namespace llvm {
2221
namespace hlsl {
2322
namespace rootsig {
2423

2524
// Basic verification of RootElements
2625

27-
LLVM_ABI bool verifyRootFlag(uint32_t Flags);
28-
LLVM_ABI bool verifyVersion(uint32_t Version);
29-
LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue);
30-
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace);
31-
LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal);
32-
LLVM_ABI bool verifyRangeType(uint32_t Type);
33-
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
34-
uint32_t FlagsVal);
35-
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors);
36-
LLVM_ABI bool verifySamplerFilter(uint32_t Value);
37-
LLVM_ABI bool verifyAddress(uint32_t Address);
38-
LLVM_ABI bool verifyMipLODBias(float MipLODBias);
39-
LLVM_ABI bool verifyMaxAnisotropy(uint32_t MaxAnisotropy);
40-
LLVM_ABI bool verifyComparisonFunc(uint32_t ComparisonFunc);
41-
LLVM_ABI bool verifyBorderColor(uint32_t BorderColor);
42-
LLVM_ABI bool verifyLOD(float LOD);
26+
bool verifyRootFlag(uint32_t Flags);
27+
bool verifyVersion(uint32_t Version);
28+
bool verifyRegisterValue(uint32_t RegisterValue);
29+
bool verifyRegisterSpace(uint32_t RegisterSpace);
30+
bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal);
31+
bool verifyRangeType(uint32_t Type);
32+
bool verifyDescriptorRangeFlag(uint32_t Version, uint32_t Type,
33+
uint32_t FlagsVal);
34+
bool verifyNumDescriptors(uint32_t NumDescriptors);
35+
bool verifySamplerFilter(uint32_t Value);
36+
bool verifyAddress(uint32_t Address);
37+
bool verifyMipLODBias(float MipLODBias);
38+
bool verifyMaxAnisotropy(uint32_t MaxAnisotropy);
39+
bool verifyComparisonFunc(uint32_t ComparisonFunc);
40+
bool verifyBorderColor(uint32_t BorderColor);
41+
bool verifyLOD(float LOD);
42+
43+
struct RangeInfo {
44+
const static uint32_t Unbounded = ~0u;
45+
46+
// Interval information
47+
uint32_t LowerBound;
48+
uint32_t UpperBound;
49+
50+
// Information retained for determining overlap
51+
llvm::dxil::ResourceClass Class;
52+
uint32_t Space;
53+
llvm::dxbc::ShaderVisibility Visibility;
54+
};
55+
56+
class ResourceRange {
57+
public:
58+
using MapT = llvm::IntervalMap<uint32_t, const RangeInfo *, 16,
59+
llvm::IntervalMapInfo<uint32_t>>;
60+
61+
private:
62+
MapT Intervals;
63+
64+
public:
65+
ResourceRange(MapT::Allocator &Allocator) : Intervals(MapT(Allocator)) {}
66+
67+
// Returns a reference to the first RangeInfo that overlaps with
68+
// [Info.LowerBound;Info.UpperBound], or, std::nullopt if there is no overlap
69+
LLVM_ABI std::optional<const RangeInfo *>
70+
getOverlapping(const RangeInfo &Info) const;
71+
72+
// Return the mapped RangeInfo at X or nullptr if no mapping exists
73+
LLVM_ABI const RangeInfo *lookup(uint32_t X) const;
74+
75+
// Removes all entries of the ResourceRange
76+
LLVM_ABI void clear();
77+
78+
// Insert the required (sub-)intervals such that the interval of [a;b] =
79+
// [Info.LowerBound, Info.UpperBound] is covered and points to a valid
80+
// RangeInfo &.
81+
//
82+
// For instance consider the following chain of inserting RangeInfos with the
83+
// intervals denoting the Lower/Upper-bounds:
84+
//
85+
// A = [0;2]
86+
// insert(A) -> false
87+
// intervals: [0;2] -> &A
88+
// B = [5;7]
89+
// insert(B) -> false
90+
// intervals: [0;2] -> &A, [5;7] -> &B
91+
// C = [4;7]
92+
// insert(C) -> true
93+
// intervals: [0;2] -> &A, [4;7] -> &C
94+
// D = [1;5]
95+
// insert(D) -> true
96+
// intervals: [0;2] -> &A, [3;3] -> &D, [4;7] -> &C
97+
// E = [0;unbounded]
98+
// insert(E) -> true
99+
// intervals: [0;unbounded] -> E
100+
//
101+
// Returns a reference to the first RangeInfo that overlaps with
102+
// [Info.LowerBound;Info.UpperBound], or, std::nullopt if there is no overlap
103+
// (equivalent to getOverlapping)
104+
LLVM_ABI std::optional<const RangeInfo *> insert(const RangeInfo &Info);
105+
};
106+
107+
struct OverlappingRanges {
108+
const RangeInfo *A;
109+
const RangeInfo *B;
110+
111+
OverlappingRanges(const RangeInfo *A, const RangeInfo *B) : A(A), B(B) {}
112+
};
113+
114+
/// The following conducts analysis on resource ranges to detect and report
115+
/// any overlaps in resource ranges.
116+
///
117+
/// A resource range overlaps with another resource range if they have:
118+
/// - equivalent ResourceClass (SRV, UAV, CBuffer, Sampler)
119+
/// - equivalent resource space
120+
/// - overlapping visbility
121+
///
122+
/// The algorithm is implemented in the following steps:
123+
///
124+
/// 1. The user will collect RangeInfo from relevant RootElements:
125+
/// - RangeInfo will retain the interval, ResourceClass, Space and Visibility
126+
/// - It will also contain an index so that it can be associated to
127+
/// additional diagnostic information
128+
/// 2. Sort the RangeInfo's such that they are grouped together by
129+
/// ResourceClass and Space
130+
/// 3. Iterate through the collected RangeInfos by their groups
131+
/// - For each group we will have a ResourceRange for each visibility
132+
/// - As we iterate through we will:
133+
/// A: Insert the current RangeInfo into the corresponding Visibility
134+
/// ResourceRange
135+
/// B: Check for overlap with any overlapping Visibility ResourceRange
136+
LLVM_ABI llvm::SmallVector<OverlappingRanges>
137+
findOverlappingRanges(ArrayRef<RangeInfo> Infos);
43138

44139
} // namespace rootsig
45140
} // namespace hlsl

llvm/include/llvm/MC/DXContainerRootSignature.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
98
#ifndef LLVM_MC_DXCONTAINERROOTSIGNATURE_H
109
#define LLVM_MC_DXCONTAINERROOTSIGNATURE_H
1110

@@ -120,5 +119,4 @@ struct RootSignatureDesc {
120119
};
121120
} // namespace mcdxbc
122121
} // namespace llvm
123-
124122
#endif // LLVM_MC_DXCONTAINERROOTSIGNATURE_H

llvm/lib/Analysis/DXILResource.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,6 @@
2929
using namespace llvm;
3030
using namespace dxil;
3131

32-
static StringRef getResourceClassName(ResourceClass RC) {
33-
switch (RC) {
34-
case ResourceClass::SRV:
35-
return "SRV";
36-
case ResourceClass::UAV:
37-
return "UAV";
38-
case ResourceClass::CBuffer:
39-
return "CBuffer";
40-
case ResourceClass::Sampler:
41-
return "Sampler";
42-
}
43-
llvm_unreachable("Unhandled ResourceClass");
44-
}
45-
4632
static StringRef getResourceKindName(ResourceKind RK) {
4733
switch (RK) {
4834
case ResourceKind::Texture1D:

llvm/lib/Frontend/HLSL/HLSLBinding.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,22 @@ BindingInfo::RegisterSpace::findAvailableBinding(int32_t Size) {
6666
return std::nullopt;
6767
}
6868

69+
bool BindingInfo::RegisterSpace::isBound(BindingRange B) {
70+
for (BindingRange &R : FreeRanges) {
71+
if (B.LowerBound >= R.LowerBound && B.LowerBound < R.UpperBound &&
72+
B.UpperBound > R.LowerBound && B.UpperBound <= R.UpperBound)
73+
return false;
74+
}
75+
return true;
76+
}
77+
78+
bool BindingInfo::isBound(dxil::ResourceClass RC, uint32_t Space,
79+
BindingRange B) {
80+
BindingSpaces &BS = getBindingSpaces(RC);
81+
RegisterSpace &RS = BS.getOrInsertSpace(Space);
82+
return RS.isBound(B);
83+
}
84+
6985
BindingInfo BindingInfoBuilder::calculateBindingInfo(
7086
llvm::function_ref<void(const BindingInfoBuilder &Builder,
7187
const Binding &Overlapping)>

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "DXILOpLowering.h"
1010
#include "DXILConstants.h"
1111
#include "DXILOpBuilder.h"
12+
#include "DXILRootSignature.h"
1213
#include "DXILShaderFlags.h"
1314
#include "DirectX.h"
1415
#include "llvm/ADT/SmallVector.h"
@@ -945,6 +946,7 @@ class DXILOpLoweringLegacy : public ModulePass {
945946
AU.addPreserved<DXILResourceWrapperPass>();
946947
AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
947948
AU.addPreserved<ShaderFlagsAnalysisWrapper>();
949+
AU.addPreserved<RootSignatureAnalysisWrapper>();
948950
}
949951
};
950952
char DXILOpLoweringLegacy::ID = 0;

0 commit comments

Comments
 (0)