Skip to content

Commit 48f55ba

Browse files
authored
[Transforms][DXIL] Tool to generate resource metadata and annotations (#98939)
This introduces dxil::ResourceInfo, which can generate DXIL-style metadata for resources and the constants for the DXIL 6.6+ annotateResource operation. These are done together so that it's easier to see all of the information the ResourceInfo class needs to store. This will be used for lowering resource in the DirectX backend and is intended to help with the translation in the other direction as well. To do that, we'll need the inverse functions of `getAsMetadata` and `getAnnotateProps`.
1 parent 656f617 commit 48f55ba

File tree

6 files changed

+881
-0
lines changed

6 files changed

+881
-0
lines changed

llvm/include/llvm/Support/DXILABI.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ enum class ElementType : uint32_t {
9494
PackedU8x32,
9595
};
9696

97+
/// Metadata tags for extra resource properties.
98+
enum class ExtPropTags : uint32_t {
99+
ElementType = 0,
100+
StructuredBufferStride = 1,
101+
SamplerFeedbackKind = 2,
102+
Atomic64Use = 3,
103+
};
104+
105+
enum class SamplerType : uint32_t {
106+
Default = 0,
107+
Comparison = 1,
108+
Mono = 2, // Note: Seems to be unused.
109+
};
110+
111+
enum class SamplerFeedbackType : uint32_t {
112+
MinMip = 0,
113+
MipRegionUsed = 1,
114+
};
115+
97116
} // namespace dxil
98117
} // namespace llvm
99118

Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
//===- DXILResource.h - Tools to translate DXIL resources -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H
10+
#define LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H
11+
12+
#include "llvm/IR/Metadata.h"
13+
#include "llvm/IR/Value.h"
14+
#include "llvm/Support/DXILABI.h"
15+
16+
namespace llvm {
17+
namespace dxil {
18+
19+
struct ResourceBinding {
20+
uint32_t Space;
21+
uint32_t LowerBound;
22+
uint32_t Size;
23+
24+
bool operator==(const ResourceBinding &RHS) const {
25+
return std::tie(Space, LowerBound, Size) ==
26+
std::tie(RHS.Space, RHS.LowerBound, RHS.Size);
27+
}
28+
bool operator!=(const ResourceBinding &RHS) const { return !(*this == RHS); }
29+
};
30+
31+
class ResourceInfo {
32+
struct UAVInfo {
33+
bool GloballyCoherent;
34+
bool HasCounter;
35+
bool IsROV;
36+
37+
bool operator==(const UAVInfo &RHS) const {
38+
return std::tie(GloballyCoherent, HasCounter, IsROV) ==
39+
std::tie(RHS.GloballyCoherent, RHS.HasCounter, RHS.IsROV);
40+
}
41+
bool operator!=(const UAVInfo &RHS) const { return !(*this == RHS); }
42+
};
43+
44+
struct StructInfo {
45+
uint32_t Stride;
46+
Align Alignment;
47+
48+
bool operator==(const StructInfo &RHS) const {
49+
return std::tie(Stride, Alignment) == std::tie(RHS.Stride, RHS.Alignment);
50+
}
51+
bool operator!=(const StructInfo &RHS) const { return !(*this == RHS); }
52+
};
53+
54+
struct TypedInfo {
55+
dxil::ElementType ElementTy;
56+
uint32_t ElementCount;
57+
58+
bool operator==(const TypedInfo &RHS) const {
59+
return std::tie(ElementTy, ElementCount) ==
60+
std::tie(RHS.ElementTy, RHS.ElementCount);
61+
}
62+
bool operator!=(const TypedInfo &RHS) const { return !(*this == RHS); }
63+
};
64+
65+
struct MSInfo {
66+
uint32_t Count;
67+
68+
bool operator==(const MSInfo &RHS) const { return Count == RHS.Count; }
69+
bool operator!=(const MSInfo &RHS) const { return !(*this == RHS); }
70+
};
71+
72+
struct FeedbackInfo {
73+
dxil::SamplerFeedbackType Type;
74+
75+
bool operator==(const FeedbackInfo &RHS) const { return Type == RHS.Type; }
76+
bool operator!=(const FeedbackInfo &RHS) const { return !(*this == RHS); }
77+
};
78+
79+
// Universal properties.
80+
Value *Symbol;
81+
StringRef Name;
82+
83+
ResourceBinding Binding;
84+
uint32_t UniqueID;
85+
86+
dxil::ResourceClass RC;
87+
dxil::ResourceKind Kind;
88+
89+
// Resource class dependent properties.
90+
// CBuffer, Sampler, and RawBuffer end here.
91+
union {
92+
UAVInfo UAVFlags; // UAV
93+
uint32_t CBufferSize; // CBuffer
94+
dxil::SamplerType SamplerTy; // Sampler
95+
};
96+
97+
// Resource kind dependent properties.
98+
union {
99+
StructInfo Struct; // StructuredBuffer
100+
TypedInfo Typed; // All SRV/UAV except Raw/StructuredBuffer
101+
FeedbackInfo Feedback; // FeedbackTexture
102+
};
103+
104+
MSInfo MultiSample;
105+
106+
// Conditions to check before accessing union members.
107+
bool isUAV() const;
108+
bool isCBuffer() const;
109+
bool isSampler() const;
110+
bool isStruct() const;
111+
bool isTyped() const;
112+
bool isFeedback() const;
113+
bool isMultiSample() const;
114+
115+
ResourceInfo(dxil::ResourceClass RC, dxil::ResourceKind Kind, Value *Symbol,
116+
StringRef Name, ResourceBinding Binding, uint32_t UniqueID)
117+
: Symbol(Symbol), Name(Name), Binding(Binding), UniqueID(UniqueID),
118+
RC(RC), Kind(Kind) {}
119+
120+
public:
121+
static ResourceInfo SRV(Value *Symbol, StringRef Name,
122+
ResourceBinding Binding, uint32_t UniqueID,
123+
dxil::ElementType ElementTy, uint32_t ElementCount,
124+
dxil::ResourceKind Kind);
125+
static ResourceInfo RawBuffer(Value *Symbol, StringRef Name,
126+
ResourceBinding Binding, uint32_t UniqueID);
127+
static ResourceInfo StructuredBuffer(Value *Symbol, StringRef Name,
128+
ResourceBinding Binding,
129+
uint32_t UniqueID, uint32_t Stride,
130+
Align Alignment);
131+
static ResourceInfo Texture2DMS(Value *Symbol, StringRef Name,
132+
ResourceBinding Binding, uint32_t UniqueID,
133+
dxil::ElementType ElementTy,
134+
uint32_t ElementCount, uint32_t SampleCount);
135+
static ResourceInfo
136+
Texture2DMSArray(Value *Symbol, StringRef Name, ResourceBinding Binding,
137+
uint32_t UniqueID, dxil::ElementType ElementTy,
138+
uint32_t ElementCount, uint32_t SampleCount);
139+
140+
static ResourceInfo UAV(Value *Symbol, StringRef Name,
141+
ResourceBinding Binding, uint32_t UniqueID,
142+
dxil::ElementType ElementTy, uint32_t ElementCount,
143+
bool GloballyCoherent, bool IsROV,
144+
dxil::ResourceKind Kind);
145+
static ResourceInfo RWRawBuffer(Value *Symbol, StringRef Name,
146+
ResourceBinding Binding, uint32_t UniqueID,
147+
bool GloballyCoherent, bool IsROV);
148+
static ResourceInfo RWStructuredBuffer(Value *Symbol, StringRef Name,
149+
ResourceBinding Binding,
150+
uint32_t UniqueID, uint32_t Stride,
151+
Align Alignment, bool GloballyCoherent,
152+
bool IsROV, bool HasCounter);
153+
static ResourceInfo RWTexture2DMS(Value *Symbol, StringRef Name,
154+
ResourceBinding Binding, uint32_t UniqueID,
155+
dxil::ElementType ElementTy,
156+
uint32_t ElementCount, uint32_t SampleCount,
157+
bool GloballyCoherent);
158+
static ResourceInfo
159+
RWTexture2DMSArray(Value *Symbol, StringRef Name, ResourceBinding Binding,
160+
uint32_t UniqueID, dxil::ElementType ElementTy,
161+
uint32_t ElementCount, uint32_t SampleCount,
162+
bool GloballyCoherent);
163+
static ResourceInfo FeedbackTexture2D(Value *Symbol, StringRef Name,
164+
ResourceBinding Binding,
165+
uint32_t UniqueID,
166+
dxil::SamplerFeedbackType FeedbackTy);
167+
static ResourceInfo
168+
FeedbackTexture2DArray(Value *Symbol, StringRef Name, ResourceBinding Binding,
169+
uint32_t UniqueID,
170+
dxil::SamplerFeedbackType FeedbackTy);
171+
172+
static ResourceInfo CBuffer(Value *Symbol, StringRef Name,
173+
ResourceBinding Binding, uint32_t UniqueID,
174+
uint32_t Size);
175+
176+
static ResourceInfo Sampler(Value *Symbol, StringRef Name,
177+
ResourceBinding Binding, uint32_t UniqueID,
178+
dxil::SamplerType SamplerTy);
179+
180+
bool operator==(const ResourceInfo &RHS) const;
181+
182+
MDTuple *getAsMetadata(LLVMContext &Ctx) const;
183+
184+
ResourceBinding getBinding() const { return Binding; }
185+
std::pair<uint32_t, uint32_t> getAnnotateProps() const;
186+
};
187+
188+
} // namespace dxil
189+
} // namespace llvm
190+
191+
#endif // LLVM_TRANSFORMS_UTILS_DXILRESOURCE_H

llvm/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ add_llvm_component_library(LLVMTransformUtils
2020
CountVisits.cpp
2121
Debugify.cpp
2222
DemoteRegToStack.cpp
23+
DXILResource.cpp
2324
DXILUpgrade.cpp
2425
EntryExitInstrumenter.cpp
2526
EscapeEnumerator.cpp

0 commit comments

Comments
 (0)