23#include "llvm/IR/IntrinsicsDirectX.h"
30#define DEBUG_TYPE "dxil-resource-access"
43 for (
auto *Handle : Handles) {
44 std::string HandleStr;
46 Handle->print(HandleOS);
51 "Resource access is not guaranteed to map to a unique global resource"));
60 assert((
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer ||
61 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer) &&
62 "Resource access through unexpected intrinsic");
63 return Offset ?
Offset : ConstantInt::get(Builder.getInt32Ty(), 0);
67 assert(
GEP &&
"Resource access through unexpected instruction");
69 unsigned NumIndices =
GEP->getNumIndices();
70 uint64_t IndexScale =
DL.getTypeAllocSize(
GEP->getSourceElementType());
71 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(
GEP->getType()), 0);
73 if (
GEP->accumulateConstantOffset(
DL, ConstantOffset)) {
76 ConstantInt::get(
DL.getIndexType(
GEP->getType()), ConstantOffset);
78 }
else if (NumIndices == 1) {
81 GEPOffset = *
GEP->idx_begin();
82 }
else if (NumIndices == 2) {
84 auto *IndexIt =
GEP->idx_begin();
86 "GEP is not indexing through pointer");
87 GEPOffset = *(++IndexIt);
92 if (!(IndexScale % ElemSize)) {
95 IndexScale /= ElemSize;
99 GEPOffset = Builder.CreateMul(
100 GEPOffset, ConstantInt::get(Builder.getInt32Ty(), IndexScale));
102 GEPOffset = Builder.CreateUDiv(
103 GEPOffset, ConstantInt::get(Builder.getInt32Ty(), ElemSize));
106 Ptr =
GEP->getPointerOperand();
120 Value *V =
SI->getValueOperand();
121 if (V->getType() == ContainedType) {
124 "Store of whole element has mismatched address to store to");
125 }
else if (V->getType() == ScalarType) {
128 auto *Load = Builder.CreateIntrinsic(
129 LoadType, Intrinsic::dx_resource_load_typedbuffer,
130 {
II->getOperand(0),
II->getOperand(1)});
131 auto *Struct = Builder.CreateExtractValue(Load, {0});
133 uint64_t AccessSize =
DL.getTypeSizeInBits(ScalarType) / 8;
136 V = Builder.CreateInsertElement(Struct, V,
Offset);
141 auto *Inst = Builder.CreateIntrinsic(
142 Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
143 {II->getOperand(0), II->getOperand(1), V});
144 SI->replaceAllUsesWith(Inst);
153 if (!ConstantOffset || !ConstantOffset->isZero())
154 Index = Builder.CreateAdd(Index,
Offset);
158 Builder.CreateIntrinsic(Builder.getVoidTy(),
159 Intrinsic::dx_resource_store_rawbuffer,
160 {Buffer, Index, Offset, V});
168 Value *V =
SI->getValueOperand();
169 assert(!V->getType()->isAggregateType() &&
170 "Resource store should be scalar or vector type");
172 Value *Index =
II->getOperand(1);
179 if (VT && VT->getNumElements() > 4) {
181 Type *EltTy = VT->getElementType();
182 Value *Stride = ConstantInt::get(Builder.getInt32Ty(),
183 4 * (
DL.getTypeSizeInBits(EltTy) / 8));
186 for (
unsigned int I = 0,
N = VT->getNumElements();
I <
N;
I += 4) {
190 for (
unsigned int J =
I,
E = std::min(
N, J + 4); J <
E; ++J)
192 Value *Part = Builder.CreateShuffleVector(V, Indices);
241 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_typedbuffer,
242 {
II->getOperand(0),
II->getOperand(1)});
243 V = Builder.CreateExtractValue(V, {0});
246 uint64_t AccessSize =
DL.getTypeSizeInBits(ScalarType) / 8;
250 if (!ConstantOffset || !ConstantOffset->isZero())
251 V = Builder.CreateExtractElement(V,
Offset);
258 Builder.getInt32(0));
269 Value *Handle =
II->getOperand(0);
270 Value *Coords =
II->getOperand(1);
273 Value *MipLevel = Builder.getInt32(0);
286 Builder.CreateIntrinsic(ContainedType, Intrinsic::dx_resource_load_level,
287 {Handle, Coords, MipLevel, Offsets});
290 uint64_t AccessSize =
DL.getTypeSizeInBits(ScalarType) / 8;
294 if (!ConstantOffset || !ConstantOffset->isZero())
295 V = Builder.CreateExtractElement(V,
Offset);
302 Builder.getInt32(0));
314 if (!ConstantOffset || !ConstantOffset->isZero())
315 Index = Builder.CreateAdd(Index,
Offset);
322 Value *V = Builder.CreateIntrinsic(TypeWithCheck,
323 Intrinsic::dx_resource_load_rawbuffer,
325 return Builder.CreateExtractValue(V, {0});
333 Value *Index =
II->getOperand(1);
343 "Resource load should be scalar or vector type");
348 Type *EltTy = VT->getElementType();
349 Value *Stride = ConstantInt::get(Builder.getInt32Ty(),
350 4 * (
DL.getTypeSizeInBits(EltTy) / 8));
353 for (
unsigned int I = 0,
N = VT->getNumElements();
I <
N;
I += 4) {
371struct CBufferRowIntrin {
374 unsigned int EltSize;
375 unsigned int NumElts;
377 CBufferRowIntrin(
const DataLayout &
DL,
Type *Ty) {
380 switch (
DL.getTypeSizeInBits(Ty)) {
382 IID = Intrinsic::dx_resource_load_cbufferrow_8;
388 IID = Intrinsic::dx_resource_load_cbufferrow_4;
394 IID = Intrinsic::dx_resource_load_cbufferrow_2;
412 CBufferRowIntrin Intrin(
DL, Ty->getScalarType());
415 Value *Handle =
II->getOperand(0);
420 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer
421 ? ConstantInt::get(Builder.getInt32Ty(), 0)
423 assert(GlobalOffset &&
"CBuffer getpointer index must be constant");
426 Value *CurrentRow = ConstantInt::get(
428 unsigned int CurrentIndex =
438 "Unexpected indirect access to resource without GEP");
442 CurrentRow = Builder.CreateAdd(GEPOffset, CurrentRow);
444 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(LastGEP->getType()), 0);
445 if (LastGEP->accumulateConstantOffset(
DL, ConstantOffset)) {
446 APInt Remainder(
DL.getIndexTypeSizeInBits(LastGEP->getType()),
448 APInt::udivrem(ConstantOffset, Remainder, ConstantOffset, Remainder);
449 CurrentRow = Builder.CreateAdd(
450 CurrentRow, ConstantInt::get(Builder.getInt32Ty(), ConstantOffset));
453 assert(LastGEP->getNumIndices() == 1 &&
454 "Last GEP of cbuffer access is not array or struct access");
460 ? *LastGEP->idx_begin()
461 : Builder.CreateAdd(CurrentRow, *LastGEP->idx_begin());
466 auto *CBufLoad = Builder.CreateIntrinsic(
467 Intrin.RetTy, Intrin.IID, {Handle, CurrentRow},
nullptr, Name +
".load");
469 Builder.CreateExtractValue(CBufLoad, {CurrentIndex++}, Name +
".extract");
473 unsigned int Remaining =
474 ((
DL.getTypeSizeInBits(Ty) / 8) / Intrin.EltSize) - 1;
475 if (Remaining == 0) {
481 assert(VT->getNumElements() == 1 &&
"Can't have multiple elements here");
483 Builder.getInt32(0), Name);
491 while (Remaining--) {
492 CurrentIndex %= Intrin.NumElts;
494 if (CurrentIndex == 0) {
495 CurrentRow = Builder.CreateAdd(CurrentRow,
496 ConstantInt::get(Builder.getInt32Ty(), 1));
497 CBufLoad = Builder.CreateIntrinsic(Intrin.RetTy, Intrin.IID,
498 {Handle, CurrentRow},
nullptr,
502 Extracts.
push_back(Builder.CreateExtractValue(CBufLoad, {CurrentIndex++},
508 for (
int I = 0,
E = Extracts.
size();
I <
E; ++
I)
509 Result = Builder.CreateInsertElement(
510 Result, Extracts[
I], Builder.getInt32(
I), Name +
formatv(
".upto{}",
I));
558 Intrinsic::dx_resource_handlefrombinding,
559 Intrinsic::dx_resource_handlefromimplicitbinding,
566 while (!Worklist.
empty()) {
569 if (!
X->getType()->isPointerTy() && !
X->getType()->isTargetExtTy())
573 for (
Use &V : Phi->incoming_values())
581 if (IID == Intrinsic::dx_resource_getpointer)
595 "Only expects a Handle as determined from collectUsedHandles.");
603 uint32_t UpperBound =
Size == UINT32_MAX ? UINT32_MAX : LowerBound +
Size - 1;
605 return hlsl::Binding(Class, Space, LowerBound, UpperBound,
nullptr);
610struct AccessIndices {
614 bool hasGetPtrIdx() {
return GetPtrIdx !=
nullptr; }
615 bool hasHandleIdx() {
return HandleIdx !=
nullptr; }
629 return {
nullptr,
II->getArgOperand(3)};
632 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
635 assert(!AccessIdx.hasGetPtrIdx() &&
636 "Encountered multiple dx.resource.getpointers in ptr chain?");
637 AccessIdx.GetPtrIdx =
II->getArgOperand(1);
645 unsigned NumEdges = Phi->getNumIncomingValues();
646 assert(NumEdges != 0 &&
"Malformed Phi Node");
652 bool HasGetPtr =
true;
653 for (
unsigned Idx = 0; Idx < NumEdges; Idx++) {
654 auto *BB = Phi->getIncomingBlock(Idx);
657 HasGetPtr &= AccessIdx.hasGetPtrIdx();
664 Builder.Insert(GetPtrPhi);
668 Builder.Insert(HandlePhi);
671 return {GetPtrPhi, HandlePhi};
682 Value *GetPtrSelect =
nullptr;
684 if (TrueAccessIdx.hasGetPtrIdx() && FalseAccessIdx.hasGetPtrIdx())
686 Builder.CreateSelect(
Select->getCondition(), TrueAccessIdx.GetPtrIdx,
687 FalseAccessIdx.GetPtrIdx);
690 Builder.CreateSelect(
Select->getCondition(), TrueAccessIdx.HandleIdx,
691 FalseAccessIdx.HandleIdx);
693 return {GetPtrSelect, HandleSelect};
703 assert(AccessIdx.hasGetPtrIdx() && AccessIdx.hasHandleIdx() &&
704 "Couldn't retrieve indices. This is guaranteed by getAccessIndices");
709 Builder.Insert(Handle);
712 Builder.CreateIntrinsic(Ptr->
getType(), Intrinsic::dx_resource_getpointer,
713 {Handle, AccessIdx.GetPtrIdx});
735 unsigned NumHandles = Handles.
size();
739 bool SameGlobalBinding =
true;
741 for (
unsigned Idx = 1; Idx < NumHandles; Idx++)
745 if (!SameGlobalBinding) {
755 bool MadeChanges =
false;
758 if (
I->hasNUses(0)) {
759 I->eraseFromParent();
768 for (
User *U :
II->users())
772 while (!Worklist.
empty()) {
782 assert(
SI->getValueOperand() !=
II &&
"Pointer escaped!");
795 Dead->eraseFromParent();
796 II->eraseFromParent();
804 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer ||
805 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer) {
808 (DRTM[HandleTy].isCBuffer() ||
809 II->getIntrinsicID() != Intrinsic::dx_resource_getbasepointer) &&
810 "dx_resource_getbasepointer should only be used by cbuffers");
825 assert(DRTM &&
"DXILResourceTypeAnalysis must be available");
829 if (!(MadeHandleChanges || MadeResourceChanges))
843 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
846 return MadeHandleChanges || MadeResourceChanges;
848 StringRef getPassName()
const override {
return "DXIL Resource Access"; }
849 DXILResourceAccessLegacy() : FunctionPass(
ID) {}
852 void getAnalysisUsage(llvm::AnalysisUsage &AU)
const override {
857char DXILResourceAccessLegacy::ID = 0;
861 "DXIL Resource Access",
false,
false)
867 return new DXILResourceAccessLegacy();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Remove Unused Resources
static void diagnoseNonUniqueResourceAccess(Instruction *I, ArrayRef< IntrinsicInst * > Handles)
static void createLoadIntrinsic(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static Value * emitRawLoad(IRBuilder<> &Builder, Type *Ty, Value *Buffer, Value *Index, Value *Offset, dxil::ResourceTypeInfo &RTI)
static bool legalizeResourceHandles(Function &F, DXILResourceTypeMap &DRTM)
static AccessIndices getAccessIndices(Instruction *I, SmallSetVector< Instruction *, 16 > &DeadInsts)
static void createTypedBufferLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void createTypedBufferStore(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static SmallVector< IntrinsicInst * > collectUsedHandles(Value *Ptr)
static const std::array< Intrinsic::ID, 2 > HandleIntrins
static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM)
static void createTextureLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void replaceHandleWithIndices(Instruction *Ptr, IntrinsicInst *OldHandle, SmallSetVector< Instruction *, 16 > &DeadInsts)
static void emitRawStore(IRBuilder<> &Builder, Value *Buffer, Value *Index, Value *Offset, Value *V, dxil::ResourceTypeInfo &RTI)
static Value * traverseGEPOffsets(const DataLayout &DL, IRBuilder<> &Builder, Value *Ptr, uint64_t AccessSize)
static hlsl::Binding getHandleIntrinsicBinding(IntrinsicInst *Handle, DXILResourceTypeMap &DRTM)
static void createStoreIntrinsic(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static void createCBufferLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void createRawStores(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static void createRawLoads(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static Instruction * getStoreLoadPointerOperand(Instruction *AI)
static void replaceAccess(IntrinsicInst *II, dxil::ResourceTypeInfo &RTI)
static bool runOnFunction(Function &F, bool PostInlining)
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file implements a set that has insertion order iteration characteristics.
Class for arbitrary precision integers.
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
uint64_t getZExtValue() const
Get zero extended value.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
LLVM Basic Block Representation.
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A parsed version of the target data layout string in and methods for querying it.
Analysis pass which computes a DominatorTree.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
FunctionPass class - This class is used to implement most global optimizations.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
Value * getPointerOperand()
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
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.
PreservedAnalyses & preserve()
Mark an analysis as preserved.
bool insert(const value_type &X)
Insert a new element into the SetVector.
A SetVector that performs no allocations if smaller than a certain size.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
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.
Type * getTypeParameter(unsigned i) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
bool isAggregateType() const
Return true if the type is an aggregate type.
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
TargetExtType * getHandleTy() const
LLVM_ABI bool isStruct() const
dxil::ResourceKind getResourceKind() const
A raw_ostream that writes to an std::string.
#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.
@ RTAccelerationStructure
const unsigned CBufferRowSizeInBytes
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
OuterAnalysisManagerProxy< ModuleAnalysisManager, Function > ModuleAnalysisManagerFunctionProxy
Provide the ModuleAnalysisManager to Function proxy.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
LLVM_ABI Value * concatenateVectors(IRBuilderBase &Builder, ArrayRef< Value * > Vecs)
Concatenate a list of vectors.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
FunctionPass * createDXILResourceAccessLegacyPass()
Pass to update resource accesses to use load/store directly.
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...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.