19#include "llvm/IR/IntrinsicsDirectX.h"
27#define DEBUG_TYPE "dxil-resource"
34 case ResourceKind::Texture1D:
36 case ResourceKind::Texture2D:
38 case ResourceKind::Texture2DMS:
40 case ResourceKind::Texture3D:
42 case ResourceKind::TextureCube:
44 case ResourceKind::Texture1DArray:
45 return "Texture1DArray";
46 case ResourceKind::Texture2DArray:
47 return "Texture2DArray";
48 case ResourceKind::Texture2DMSArray:
49 return "Texture2DMSArray";
50 case ResourceKind::TextureCubeArray:
51 return "TextureCubeArray";
52 case ResourceKind::TypedBuffer:
54 case ResourceKind::RawBuffer:
56 case ResourceKind::StructuredBuffer:
57 return "StructuredBuffer";
58 case ResourceKind::CBuffer:
60 case ResourceKind::Sampler:
62 case ResourceKind::TBuffer:
64 case ResourceKind::RTAccelerationStructure:
65 return "RTAccelerationStructure";
66 case ResourceKind::FeedbackTexture2D:
67 return "FeedbackTexture2D";
68 case ResourceKind::FeedbackTexture2DArray:
69 return "FeedbackTexture2DArray";
70 case ResourceKind::NumEntries:
71 case ResourceKind::Invalid:
81 case ElementType::I16:
83 case ElementType::U16:
85 case ElementType::I32:
87 case ElementType::U32:
89 case ElementType::I64:
91 case ElementType::U64:
93 case ElementType::F16:
95 case ElementType::F32:
97 case ElementType::F64:
99 case ElementType::SNormF16:
101 case ElementType::UNormF16:
103 case ElementType::SNormF32:
105 case ElementType::UNormF32:
107 case ElementType::SNormF64:
109 case ElementType::UNormF64:
111 case ElementType::PackedS8x32:
113 case ElementType::PackedU8x32:
115 case ElementType::Invalid:
123 case ElementType::I1:
125 case ElementType::I16:
127 case ElementType::U16:
129 case ElementType::I32:
131 case ElementType::U32:
133 case ElementType::I64:
135 case ElementType::U64:
137 case ElementType::F16:
138 case ElementType::SNormF16:
139 case ElementType::UNormF16:
141 case ElementType::F32:
142 case ElementType::SNormF32:
143 case ElementType::UNormF32:
145 case ElementType::F64:
146 case ElementType::SNormF64:
147 case ElementType::UNormF64:
149 case ElementType::PackedS8x32:
150 return "int8_t4_packed";
151 case ElementType::PackedU8x32:
152 return "uint8_t4_packed";
153 case ElementType::Invalid:
161 case SamplerType::Default:
163 case SamplerType::Comparison:
165 case SamplerType::Mono:
173 case SamplerFeedbackType::MinMip:
175 case SamplerFeedbackType::MipRegionUsed:
176 return "MipRegionUsed";
183 Ty = Ty->getScalarType();
185 if (Ty->isIntegerTy()) {
186 switch (Ty->getIntegerBitWidth()) {
188 return IsSigned ? ElementType::I16 : ElementType::U16;
190 return IsSigned ? ElementType::I32 : ElementType::U32;
192 return IsSigned ? ElementType::I64 : ElementType::U64;
195 return ElementType::Invalid;
197 }
else if (Ty->isFloatTy()) {
198 return ElementType::F32;
199 }
else if (Ty->isDoubleTy()) {
200 return ElementType::F64;
201 }
else if (Ty->isHalfTy()) {
202 return ElementType::F16;
205 return ElementType::Invalid;
219 : HandleTy(HandleTy) {
236 Kind = Ty->getDimension();
239 Kind = Ty->getDimension();
242 Kind = Ty->getDimension();
254 bool IsWriteable,
bool IsROV,
255 Type *ContainedType =
nullptr,
256 bool IsSigned =
true) {
259 DestStream << (IsROV ?
"RasterizerOrdered" :
"RW");
267 ArrayDimensions.
push_back(AT->getNumElements());
268 ContainedType = AT->getElementType();
277 "invalid element type for raw buffer");
281 ElementName = ST->getStructName();
284 DestStream <<
"<" << ElementName;
286 DestStream << VTy->getNumElements();
287 for (
uint64_t Dim : ArrayDimensions)
288 DestStream <<
"[" << Dim <<
"]";
294 if (Ty && Ty->getNumElements() == 1 && Ty->getElementType(0) == ElemType)
304 ElementTypes.
reserve(ST->getNumElements());
305 for (
Type *ElTy : ST->elements()) {
312 if (ElementTypes.
size() == 2)
314 if (ElementTypes[1] == AT->getElementType())
318 if (ElementTypes.
size() == 1)
319 return ElementTypes[0];
326 AT->getNumElements());
344 RTy->isROV(), RTy->getResourceType(), RTy->isSigned());
351 false, RTy->getResourceType(), RTy->isSigned());
357 RTy->isROV(), RTy->getResourceType(), RTy->isSigned());
369 Type *Ty = RTy->getResourceType();
371 RTy->isROV(), RTy->getResourceType(),
true);
385 if (!CBufferName.
empty()) {
387 Name.append(CBufferName);
402 TypeName =
formatv(
"SamplerState<{0}>",
503 return {
isROV(Kind, HandleTy)};
513 return LayoutTy->getSize();
515 return DL.getTypeAllocSize(ElTy);
532 Alignment =
DL.getStructLayout(STy)->getAlignment();
534 return {Stride, AlignLog2};
548 return {RTy->getResourceType(), RTy->isSigned()};
553 return {RTy->getResourceType(), RTy->isSigned()};
557 return {RTy->getResourceType(), RTy->isSigned()};
582 Count = VTy->getNumElements();
583 return {ET, DXILStorageTy,
Count};
596 return HandleTy == RHS.HandleTy;
602 if (std::tie(RC, Kind) < std::tie(RHS.RC, RHS.Kind))
609 if (
isUAV() && RHS.isUAV() &&
getUAV() < RHS.getUAV())
612 getStruct(DummyDL) < RHS.getStruct(DummyDL))
636 OS <<
" IsROV: " << UAVFlags.
IsROV <<
"\n";
643 OS <<
" Buffer Stride: " << Struct.Stride <<
"\n";
644 OS <<
" Alignment: " << Struct.AlignLog2 <<
"\n";
659 assert(!Symbol &&
"Symbol has already been created");
661 int64_t
Size = Binding.Size;
680 auto getIntMD = [&I32Ty](
uint32_t V) {
684 auto getBoolMD = [&I1Ty](
uint32_t V) {
689 MDVals.
push_back(getIntMD(Binding.RecordID));
690 assert(Symbol &&
"Cannot yet create useful resource metadata without symbol");
693 MDVals.
push_back(getIntMD(Binding.Space));
694 MDVals.
push_back(getIntMD(Binding.LowerBound));
695 MDVals.
push_back(getIntMD(Binding.Size));
741std::pair<uint32_t, uint32_t>
747 bool IsUAV = RTI.
isUAV();
750 bool IsROV = IsUAV && UAVFlags.
IsROV;
752 uint8_t SamplerCmpOrHasCounter = 0;
762 Word0 |= (AlignLog2 & 0xF) << 8;
763 Word0 |= (IsUAV & 1) << 12;
764 Word0 |= (IsROV & 1) << 13;
765 Word0 |= (IsGloballyCoherent & 1) << 14;
766 Word0 |= (SamplerCmpOrHasCounter & 1) << 15;
781 Word1 |= (CompType & 0xFF) << 0;
782 Word1 |= (CompCount & 0xFF) << 8;
783 Word1 |= (SampleCount & 0xFF) << 16;
786 return {Word0, Word1};
792 OS <<
" Name: " << Name <<
"\n";
796 Symbol->printAsOperand(OS);
801 <<
" Record ID: " << Binding.RecordID <<
"\n"
802 <<
" Space: " << Binding.Space <<
"\n"
803 <<
" Lower Bound: " << Binding.LowerBound <<
"\n"
804 <<
" Size: " << Binding.Size <<
"\n";
807 OS <<
" Counter Direction: ";
830 ModuleAnalysisManager::Invalidator &Inv) {
833 return !PAC.preservedWhenStateless();
838 return F.getIntrinsicID() == Intrinsic::dx_resource_updatecounter;
846 case Intrinsic::dx_resource_handlefrombinding:
847 case Intrinsic::dx_resource_handlefromimplicitbinding:
857 assert(CA && CA->isString() &&
"expected constant string");
860 if (Name.ends_with(
'\0'))
861 Name = Name.drop_back(1);
865void DXILResourceMap::populateResourceInfos(
Module &M,
870 if (!
F.isDeclaration())
877 case Intrinsic::dx_resource_handlefrombinding: {
881 for (
User *U :
F.users())
894 Size, HandleTy, Name};
905 const auto &[LCI, LRI, LRTI] =
LHS;
906 const auto &[RCI, RRI, RRTI] =
RHS;
912 return std::tie(LRC, LRI, LRTI) < std::tie(RRC, RRI, RRTI);
914 for (
auto [CI, RI, RTI] : CIToInfos) {
915 if (Infos.empty() || RI != Infos.back())
917 CallMap[CI] = Infos.size() - 1;
922 FirstUAV = FirstCBuffer = FirstSampler =
Size;
924 for (
unsigned I = 0,
E =
Size;
I !=
E; ++
I) {
940 FirstCBuffer = std::min({FirstCBuffer, FirstSampler});
941 FirstUAV = std::min({FirstUAV, FirstCBuffer});
948void DXILResourceMap::populateCounterDirections(
Module &M) {
949 for (Function &
F :
M.functions()) {
953 LLVM_DEBUG(
dbgs() <<
"Update Counter Function: " <<
F.getName() <<
"\n");
955 for (
const User *U :
F.users()) {
957 assert(CI &&
"Users of dx_resource_updateCounter must be call instrs");
965 if (CountLiteral == 0)
969 if (CountLiteral > 0)
978 else if (RBInfo->CounterDirection !=
Direction) {
980 HasInvalidDirection =
true;
987void DXILResourceMap::populate(
Module &M, DXILResourceTypeMap &DRTM) {
988 populateResourceInfos(M, DRTM);
989 populateCounterDirections(M);
994 for (
unsigned I = 0, E = Infos.size();
I != E; ++
I) {
995 OS <<
"Resource " <<
I <<
":\n";
1001 for (
const auto &[CI, Index] : CallMap) {
1002 OS <<
"Call bound to " << Index <<
":";
1011 for (
const Value *V : Phi->operands()) {
1012 Children.append(findByUse(V));
1023 case Intrinsic::dx_resource_handlefrombinding: {
1024 auto Pos = CallMap.find(CI);
1025 assert(Pos != CallMap.end() &&
"HandleFromBinding must be in resource map");
1026 return {&Infos[Pos->second]};
1037 if (
V->getType() != UseType)
1048void DXILResourceBindingInfo::populate(
Module &M, DXILResourceTypeMap &DRTM) {
1049 hlsl::BindingInfoBuilder Builder;
1053 for (Function &
F :
M.functions()) {
1054 if (!
F.isDeclaration())
1057 switch (
F.getIntrinsicID()) {
1060 case Intrinsic::dx_resource_handlefrombinding: {
1064 for (User *U :
F.users())
1068 uint32_t LowerBound =
1076 assert((
Size < 0 || (
unsigned)LowerBound +
Size - 1 <= UINT32_MAX) &&
1077 "upper bound register overflow");
1078 uint32_t UpperBound =
Size < 0 ? UINT32_MAX : LowerBound +
Size - 1;
1084 case Intrinsic::dx_resource_handlefromimplicitbinding: {
1085 HasImplicitBinding =
true;
1092 [
this](
auto,
auto) { this->HasOverlappingBinding =
true; });
1097AnalysisKey DXILResourceTypeAnalysis::Key;
1098AnalysisKey DXILResourceAnalysis::Key;
1099AnalysisKey DXILResourceBindingAnalysis::Key;
1105 Data.populate(M, DRTM);
1113 Data.populate(M, DRTM);
1122 DRM.
print(OS, DRTM, M.getDataLayout());
1126void DXILResourceTypeWrapperPass::anchor() {}
1132 "DXIL Resource Type Analysis",
false,
true)
1152 Map->populate(M, *DRTM);
1161 OS <<
"No resource map has been built!\n";
1164 Map->print(OS, *DRTM, M->getDataLayout());
1167#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1173 "DXIL Resources Analysis",
false,
true)
1195 BindingInfo->populate(M, DRTM);
1203 "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
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 dxil::ElementType toDXILElementType(Type *Ty, bool IsSigned)
static bool isUpdateCounterIntrinsic(Function &F)
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)
Module.h This file contains the declarations for the Module class.
Loop::LoopBounds::Direction Direction
Machine Check Debug Module
#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.
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.
StringRef - Represent a constant reference to a string, i.e.
constexpr bool empty() const
empty - 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 void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
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)
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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)
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
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()
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.
FunctionAddr VTableAddr Count
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
FunctionAddr VTableAddr uintptr_t uintptr_t Data
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