22#include "llvm/IR/IntrinsicsDirectX.h"
30#define DEBUG_TYPE "dxil-resource"
37 case ResourceKind::Texture1D:
39 case ResourceKind::Texture2D:
41 case ResourceKind::Texture2DMS:
43 case ResourceKind::Texture3D:
45 case ResourceKind::TextureCube:
47 case ResourceKind::Texture1DArray:
48 return "Texture1DArray";
49 case ResourceKind::Texture2DArray:
50 return "Texture2DArray";
51 case ResourceKind::Texture2DMSArray:
52 return "Texture2DMSArray";
53 case ResourceKind::TextureCubeArray:
54 return "TextureCubeArray";
55 case ResourceKind::TypedBuffer:
57 case ResourceKind::RawBuffer:
59 case ResourceKind::StructuredBuffer:
60 return "StructuredBuffer";
61 case ResourceKind::CBuffer:
63 case ResourceKind::Sampler:
65 case ResourceKind::TBuffer:
67 case ResourceKind::RTAccelerationStructure:
68 return "RTAccelerationStructure";
69 case ResourceKind::FeedbackTexture2D:
70 return "FeedbackTexture2D";
71 case ResourceKind::FeedbackTexture2DArray:
72 return "FeedbackTexture2DArray";
73 case ResourceKind::NumEntries:
74 case ResourceKind::Invalid:
84 case ElementType::I16:
86 case ElementType::U16:
88 case ElementType::I32:
90 case ElementType::U32:
92 case ElementType::I64:
94 case ElementType::U64:
96 case ElementType::F16:
98 case ElementType::F32:
100 case ElementType::F64:
102 case ElementType::SNormF16:
104 case ElementType::UNormF16:
106 case ElementType::SNormF32:
108 case ElementType::UNormF32:
110 case ElementType::SNormF64:
112 case ElementType::UNormF64:
114 case ElementType::PackedS8x32:
116 case ElementType::PackedU8x32:
118 case ElementType::Invalid:
126 case ElementType::I1:
128 case ElementType::I16:
130 case ElementType::U16:
132 case ElementType::I32:
134 case ElementType::U32:
136 case ElementType::I64:
138 case ElementType::U64:
140 case ElementType::F16:
141 case ElementType::SNormF16:
142 case ElementType::UNormF16:
144 case ElementType::F32:
145 case ElementType::SNormF32:
146 case ElementType::UNormF32:
148 case ElementType::F64:
149 case ElementType::SNormF64:
150 case ElementType::UNormF64:
152 case ElementType::PackedS8x32:
153 return "int8_t4_packed";
154 case ElementType::PackedU8x32:
155 return "uint8_t4_packed";
156 case ElementType::Invalid:
164 case SamplerType::Default:
166 case SamplerType::Comparison:
168 case SamplerType::Mono:
176 case SamplerFeedbackType::MinMip:
178 case SamplerFeedbackType::MipRegionUsed:
179 return "MipRegionUsed";
195 : HandleTy(HandleTy) {
212 Kind = Ty->getDimension();
215 Kind = Ty->getDimension();
218 Kind = Ty->getDimension();
230 bool IsWriteable,
bool IsROV,
231 Type *ContainedType =
nullptr,
232 bool IsSigned =
true) {
235 DestStream << (IsROV ?
"RasterizerOrdered" :
"RW");
243 ArrayDimensions.
push_back(AT->getNumElements());
244 ContainedType = AT->getElementType();
253 "invalid element type for raw buffer");
257 ElementName = ST->getStructName();
260 DestStream <<
"<" << ElementName;
262 DestStream << VTy->getNumElements();
263 for (
uint64_t Dim : ArrayDimensions)
264 DestStream <<
"[" << Dim <<
"]";
270 if (Ty && Ty->getNumElements() == 1 && Ty->getElementType(0) == ElemType)
280 ElementTypes.
reserve(ST->getNumElements());
281 for (
Type *ElTy : ST->elements()) {
288 if (ElementTypes.
size() == 2)
290 if (ElementTypes[1] == AT->getElementType())
294 if (ElementTypes.
size() == 1)
295 return ElementTypes[0];
302 AT->getNumElements());
320 RTy->isROV(), RTy->getResourceType(), RTy->isSigned());
327 false, RTy->getResourceType(), RTy->isSigned());
333 RTy->isROV(), RTy->getResourceType(), RTy->isSigned());
345 Type *Ty = RTy->getResourceType();
347 RTy->isROV(), RTy->getResourceType(),
true);
361 if (!CBufferName.
empty()) {
363 Name.append(CBufferName);
378 TypeName =
formatv(
"SamplerState<{0}>",
479 return {
isROV(Kind, HandleTy)};
489 return LayoutTy->getSize();
491 return DL.getTypeAllocSize(ElTy);
508 Alignment =
DL.getStructLayout(STy)->getAlignment();
510 return {Stride, AlignLog2};
524 return {RTy->getResourceType(), RTy->isSigned()};
529 return {RTy->getResourceType(), RTy->isSigned()};
533 return {RTy->getResourceType(), RTy->isSigned()};
558 Count = VTy->getNumElements();
559 return {ET, DXILStorageTy,
Count};
572 return HandleTy == RHS.HandleTy;
578 if (std::tie(RC, Kind) < std::tie(RHS.RC, RHS.Kind))
585 if (
isUAV() && RHS.isUAV() &&
getUAV() < RHS.getUAV())
588 getStruct(DummyDL) < RHS.getStruct(DummyDL))
612 OS <<
" IsROV: " << UAVFlags.
IsROV <<
"\n";
619 OS <<
" Buffer Stride: " << Struct.Stride <<
"\n";
620 OS <<
" Alignment: " << Struct.AlignLog2 <<
"\n";
635 assert(!Symbol &&
"Symbol has already been created");
659 auto getIntMD = [&I32Ty](
uint32_t V) {
663 auto getBoolMD = [&I1Ty](
uint32_t V) {
668 MDVals.
push_back(getIntMD(Binding.BindingID));
669 assert(Symbol &&
"Cannot yet create useful resource metadata without symbol");
672 MDVals.
push_back(getIntMD(Binding.Space));
673 MDVals.
push_back(getIntMD(Binding.LowerBound));
674 MDVals.
push_back(getIntMD(Binding.Size == 0 ? ~0u : Binding.Size));
720std::pair<uint32_t, uint32_t>
726 bool IsUAV = RTI.
isUAV();
729 bool IsROV = IsUAV && UAVFlags.
IsROV;
731 uint8_t SamplerCmpOrHasCounter = 0;
741 Word0 |= (AlignLog2 & 0xF) << 8;
742 Word0 |= (IsUAV & 1) << 12;
743 Word0 |= (IsROV & 1) << 13;
744 Word0 |= (IsGloballyCoherent & 1) << 14;
745 Word0 |= (SamplerCmpOrHasCounter & 1) << 15;
760 Word1 |= (CompType & 0xFF) << 0;
761 Word1 |= (CompCount & 0xFF) << 8;
762 Word1 |= (SampleCount & 0xFF) << 16;
765 return {Word0, Word1};
771 OS <<
" Name: " << Name <<
"\n";
775 Symbol->printAsOperand(OS);
782 <<
" Binding ID: " << Binding.BindingID <<
"\n"
783 <<
" Space: " << Binding.Space <<
"\n"
784 <<
" Lower Bound: " << Binding.LowerBound <<
"\n"
785 <<
" Size: " << Binding.Size <<
"\n";
787 OS <<
" HeapIndexID: " <<
getHeapID() <<
"\n";
792 OS <<
" Counter Direction: ";
815 ModuleAnalysisManager::Invalidator &Inv) {
818 return !PAC.preservedWhenStateless();
827 case Intrinsic::dx_resource_handlefrombinding:
828 case Intrinsic::dx_resource_handlefromimplicitbinding:
838 assert(CA && CA->isString() &&
"expected constant string");
841 if (Name.ends_with(
'\0'))
842 Name = Name.drop_back(1);
846void DXILResourceMap::populateResourceInfos(
Module &M,
857 if (!
F.isDeclaration())
864 case Intrinsic::dx_resource_handlefrombinding: {
868 for (
User *U :
F.users())
887 case Intrinsic::dx_resource_handlefromheap: {
891 for (User *U :
F.users()) {
896 auto Pos = IndexToHeapResID.
find(Index);
897 if (Pos == IndexToHeapResID.
end()) {
898 HeapResID = NextHeapResID++;
899 IndexToHeapResID[
Index] = HeapResID;
901 HeapResID = Pos->second;
913 const auto &[LCI, LRI, LRTI] =
LHS;
914 const auto &[RCI, RRI, RRTI] =
RHS;
920 return std::tie(LRC, LRI, LRTI) < std::tie(RRC, RRI, RRTI);
922 for (
auto [CI, RI, RTI] : CIToInfos) {
923 if (Infos.empty() || RI != Infos.back())
925 CallMap[CI] = Infos.size() - 1;
928 unsigned Size = Infos.size();
930 FirstUAV = FirstCBuffer = FirstSampler =
Size;
932 for (
unsigned I = 0,
E =
Size;
I !=
E; ++
I) {
948 FirstCBuffer = std::min({FirstCBuffer, FirstSampler});
949 FirstUAV = std::min({FirstUAV, FirstCBuffer});
959 Ptr =
GEP->getPointerOperand()->stripPointerCasts();
961 if (
II &&
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer)
962 return II->getArgOperand(0);
966void DXILResourceMap::populateAtomicUses(Instruction &
I) {
967 auto MarkFromHandle = [
this](
Value *Handle) {
977 if (AI->getValOperand()->getType()->isIntegerTy(64))
982 if (CX->getNewValOperand()->getType()->isIntegerTy(64))
987 if (CI->getIntrinsicID() == Intrinsic::dx_resource_atomic_binop &&
988 CI->getType()->isIntegerTy(64))
989 MarkFromHandle(CI->getArgOperand(0));
993void DXILResourceMap::populateRecordCounterDirection(Instruction &
I) {
995 if (!CI || CI->getIntrinsicID() != Intrinsic::dx_resource_updatecounter)
999 if (CountLiteral == 0)
1004 for (
ResourceInfo *RBInfo : findByUse(CI->getArgOperand(0))) {
1007 else if (RBInfo->CounterDirection !=
Direction) {
1009 HasInvalidDirection =
true;
1014void DXILResourceMap::populateFromInstructions(
Module &M) {
1017 populateAtomicUses(
I);
1018 populateRecordCounterDirection(
I);
1023void DXILResourceMap::populate(
Module &M, DXILResourceTypeMap &DRTM) {
1024 populateResourceInfos(M, DRTM);
1025 populateFromInstructions(M);
1030 for (
unsigned I = 0, E = Infos.size();
I != E; ++
I) {
1031 OS <<
"Resource " <<
I <<
":\n";
1037 for (
const auto &[CI, Index] : CallMap) {
1038 OS <<
"Call bound to " << Index <<
":";
1047 for (
const Value *V : Phi->operands()) {
1048 Children.append(findByUse(V));
1059 case Intrinsic::dx_resource_handlefrombinding:
1060 case Intrinsic::dx_resource_handlefromheap: {
1061 auto Pos = CallMap.find(CI);
1062 assert(Pos != CallMap.end() &&
1063 "handle initialization call must be in resource map");
1064 return {&Infos[Pos->second]};
1075 if (
V->getType() != UseType)
1086void DXILResourceBindingInfo::populate(
Module &M, DXILResourceTypeMap &DRTM) {
1087 hlsl::BindingInfoBuilder Builder;
1092 if (!
F.isDeclaration())
1095 switch (
F.getIntrinsicID()) {
1098 case Intrinsic::dx_resource_handlefrombinding: {
1102 for (User *U :
F.users())
1106 uint32_t LowerBound =
1116 "upper bound register overflow");
1117 uint32_t UpperBound =
Size == 0 ? UINT32_MAX : LowerBound +
Size - 1;
1123 case Intrinsic::dx_resource_handlefromimplicitbinding: {
1124 HasImplicitBinding =
true;
1131 [
this](
auto,
auto) { this->HasOverlappingBinding =
true; });
1136AnalysisKey DXILResourceTypeAnalysis::Key;
1137AnalysisKey DXILResourceAnalysis::Key;
1138AnalysisKey DXILResourceBindingAnalysis::Key;
1144 Data.populate(M, DRTM);
1152 Data.populate(M, DRTM);
1161 DRM.
print(OS, DRTM, M.getDataLayout());
1165void DXILResourceTypeWrapperPass::anchor() {}
1171 "DXIL Resource Type Analysis",
false,
true)
1191 Map->populate(M, *DRTM);
1200 OS <<
"No resource map has been built!\n";
1203 Map->print(OS, *DRTM, M->getDataLayout());
1206#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1212 "DXIL Resources Analysis",
false,
true)
1234 BindingInfo->populate(M, DRTM);
1242 "DXIL Resource Binding Analysis",
false,
true)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static StructType * getOrCreateElementStruct(Type *ElemType, StringRef Name)
static void formatTypeName(SmallString< 64 > &Dest, StringRef Name, bool IsWriteable, bool IsROV, Type *ContainedType=nullptr, bool IsSigned=true)
static StringRef getElementTypeName(ElementType ET)
static std::pair< Type *, bool > getTypedElementType(dxil::ResourceKind Kind, TargetExtType *Ty)
static dxil::ElementType toDXILStorageType(dxil::ElementType ET)
static bool isROV(dxil::ResourceKind Kind, TargetExtType *Ty)
static Type * getTypeWithoutPadding(Type *Ty)
static StringRef getResourceKindName(ResourceKind RK)
static StringRef getSamplerTypeName(SamplerType ST)
static StringRef getSamplerFeedbackTypeName(SamplerFeedbackType SFT)
static StringRef getElementTypeNameForTemplate(ElementType ET)
static Value * findResourceHandleFromPointer(Value *Ptr)
Module.h This file contains the declarations for the Module class.
Loop::LoopBounds::Direction Direction
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallString class.
This file defines the SmallVector class.
Class for arbitrary precision integers.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Value * getArgOperand(unsigned i) const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
This class represents a function call, abstracting a target machine's calling convention.
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
static LLVM_ABI Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
LLVM_ABI DXILResourceMap run(Module &M, ModuleAnalysisManager &AM)
Gather resource info for the module M.
LLVM_ABI DXILResourceBindingInfo run(Module &M, ModuleAnalysisManager &AM)
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
~DXILResourceBindingWrapperPass() override
DXILResourceBindingWrapperPass()
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
LLVM_ABI void print(raw_ostream &OS, DXILResourceTypeMap &DRTM, const DataLayout &DL) const
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI bool invalidate(Module &M, const PreservedAnalyses &PA, ModuleAnalysisManager::Invalidator &Inv)
DXILResourceTypeWrapperPass()
DXILResourceWrapperPass()
bool runOnModule(Module &M) override
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
~DXILResourceWrapperPass() override
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void print(raw_ostream &OS, const Module *M) const override
print - Print out the internal state of the pass.
A parsed version of the target data layout string in and methods for querying it.
iterator find(const_arg_type_t< KeyT > Val)
Class to represent fixed width SIMD vectors.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
@ ExternalLinkage
Externally visible function.
This is an important class for using LLVM in a threaded context.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
A Module instance is used to store all the information related to an LLVM module.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
constexpr bool empty() const
Check if the string is empty.
Class to represent struct types.
static LLVM_ABI StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
static LLVM_ABI StructType * getTypeByName(LLVMContext &C, StringRef Name)
Return the type with the specified name, or null if there is none by that name.
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
The dx.Layout target extension type.
TargetExtType * getHandleTy() const
LLVM_ABI std::pair< uint32_t, uint32_t > getAnnotateProps(Module &M, dxil::ResourceTypeInfo &RTI) const
LLVM_ABI void print(raw_ostream &OS, dxil::ResourceTypeInfo &RTI, const DataLayout &DL) const
void setBindingID(unsigned ID)
uint32_t getHeapID() const
const ResourceBinding & getBinding() const
LLVM_ABI GlobalVariable * createSymbol(Module &M, StructType *Ty)
LLVM_ABI MDTuple * getAsMetadata(Module &M, dxil::ResourceTypeInfo &RTI) const
ResourceCounterDirection CounterDirection
dxil::ResourceClass getResourceClass() const
LLVM_ABI uint32_t getMultiSampleCount() const
LLVM_ABI uint32_t getCBufferSize(const DataLayout &DL) const
LLVM_ABI bool operator<(const ResourceTypeInfo &RHS) const
LLVM_ABI bool isUAV() const
LLVM_ABI bool isMultiSample() const
LLVM_ABI bool isSampler() const
LLVM_ABI bool isTyped() const
LLVM_ABI dxil::SamplerType getSamplerType() const
LLVM_ABI ResourceTypeInfo(TargetExtType *HandleTy, const dxil::ResourceClass RC, const dxil::ResourceKind Kind)
LLVM_ABI bool isCBuffer() const
LLVM_ABI TypedInfo getTyped() const
LLVM_ABI StructType * createElementStruct(StringRef CBufferName="")
LLVM_ABI bool isFeedback() const
LLVM_ABI UAVInfo getUAV() const
LLVM_ABI StructInfo getStruct(const DataLayout &DL) const
LLVM_ABI bool isStruct() const
LLVM_ABI dxil::SamplerFeedbackType getFeedbackType() const
LLVM_ABI bool operator==(const ResourceTypeInfo &RHS) const
dxil::ResourceKind getResourceKind() const
LLVM_ABI void print(raw_ostream &OS, const DataLayout &DL) const
void trackBinding(dxil::ResourceClass RC, uint32_t Space, uint32_t LowerBound, uint32_t UpperBound, const void *Cookie)
LLVM_ABI BindingInfo calculateBindingInfo(llvm::function_ref< void(const BindingInfoBuilder &Builder, const Binding &Overlapping)> ReportOverlap)
Calculate the binding info - ReportOverlap will be called once for each overlapping binding.
This class implements an extremely fast bulk output stream that can only output to a stream.
A raw_ostream that writes to an SmallVector or SmallString.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI StringRef getResourceClassName(ResourceClass RC)
ResourceKind
The kind of resource for an SRV or UAV resource.
@ RTAccelerationStructure
ElementType
The element type of an SRV or UAV resource.
LLVM_ABI StringRef getResourceNameFromBindingCall(CallInst *CI)
LLVM_ABI dxil::ElementType getDXILElementType(Type *Ty, bool IsSigned)
Converts a scalar or vector LLVM type to its DXIL element type.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI ModulePass * createDXILResourceBindingWrapperPassPass()
void stable_sort(R &&Range)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
LLVM_ABI ModulePass * createDXILResourceTypeWrapperPassPass()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
unsigned Log2(Align A)
Returns the log2 of the alignment.
LLVM_ABI ModulePass * createDXILResourceWrapperPassPass()
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
dxil::ElementType DXILStorageTy
dxil::ElementType ElementTy