21#include "llvm/IR/IntrinsicsDirectX.h"
27#define DEBUG_TYPE "dxil-resource-access"
33 assert(!PrevOffset &&
"Non-constant GEP chains not handled yet");
43 ScalarSize =
DL.getTypeSizeInBits(ScalarType) / 8;
46 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(
GEP->getType()), 0);
47 if (
GEP->accumulateConstantOffset(
DL, ConstantOffset)) {
49 return ConstantInt::get(
DL.getIndexType(
GEP->getType()),
Scaled);
52 unsigned NumIndices =
GEP->getNumIndices();
57 return *
GEP->idx_begin();
60 if (NumIndices == 2) {
61 auto IndexIt =
GEP->idx_begin();
63 "GEP is not indexing through pointer");
66 assert(++IndexIt ==
GEP->idx_end() &&
"Too many indices in GEP");
79 Value *V =
SI->getValueOperand();
80 if (V->getType() == ContainedType) {
86 auto *Load = Builder.CreateIntrinsic(
87 LoadType, Intrinsic::dx_resource_load_typedbuffer,
88 {
II->getOperand(0),
II->getOperand(1)});
89 auto *Struct = Builder.CreateExtractValue(Load, {0});
93 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
94 V = Builder.CreateInsertElement(Struct, V,
Offset);
99 auto *Inst = Builder.CreateIntrinsic(
100 Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
101 {II->getOperand(0), II->getOperand(1), V});
102 SI->replaceAllUsesWith(Inst);
109 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
110 Value *V =
SI->getValueOperand();
112 auto *Inst = Builder.CreateIntrinsic(
113 Builder.getVoidTy(), Intrinsic::dx_resource_store_rawbuffer,
114 {II->getOperand(0), II->getOperand(1), Offset, V});
115 SI->replaceAllUsesWith(Inst);
157 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_typedbuffer,
158 {
II->getOperand(0),
II->getOperand(1)});
159 V = Builder.CreateExtractValue(V, {0});
162 V = Builder.CreateExtractElement(V,
Offset);
169 Builder.getInt32(0));
179 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
181 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_rawbuffer,
182 {
II->getOperand(0),
II->getOperand(1),
Offset});
183 V = Builder.CreateExtractValue(V, {0});
190struct CBufferRowIntrin {
193 unsigned int EltSize;
194 unsigned int NumElts;
196 CBufferRowIntrin(
const DataLayout &
DL,
Type *Ty) {
199 switch (
DL.getTypeSizeInBits(Ty)) {
201 IID = Intrinsic::dx_resource_load_cbufferrow_8;
207 IID = Intrinsic::dx_resource_load_cbufferrow_4;
213 IID = Intrinsic::dx_resource_load_cbufferrow_2;
231 CBufferRowIntrin Intrin(
DL, Ty->getScalarType());
234 Value *Handle =
II->getOperand(0);
239 assert(GlobalOffset &&
"CBuffer getpointer index must be constant");
241 unsigned int FixedOffset = GlobalOffset->
getZExtValue();
245 FixedOffset += ConstOffset->getZExtValue();
249 Value *CurrentRow = ConstantInt::get(
251 unsigned int CurrentIndex =
255 "Dynamic indexing into elements of cbuffer rows is not supported");
259 CurrentRow = FixedOffset ? Builder.CreateAdd(CurrentRow,
Offset) :
Offset;
261 auto *CBufLoad = Builder.CreateIntrinsic(
262 Intrin.RetTy, Intrin.IID, {Handle, CurrentRow},
nullptr, Name +
".load");
264 Builder.CreateExtractValue(CBufLoad, {CurrentIndex++}, Name +
".extract");
268 unsigned int Remaining =
269 ((
DL.getTypeSizeInBits(Ty) / 8) / Intrin.EltSize) - 1;
270 if (Remaining == 0) {
276 assert(VT->getNumElements() == 1 &&
"Can't have multiple elements here");
278 Builder.getInt32(0), Name);
286 while (Remaining--) {
287 CurrentIndex %= Intrin.NumElts;
289 if (CurrentIndex == 0) {
290 CurrentRow = Builder.CreateAdd(CurrentRow,
291 ConstantInt::get(Builder.getInt32Ty(), 1));
292 CBufLoad = Builder.CreateIntrinsic(Intrin.RetTy, Intrin.IID,
293 {Handle, CurrentRow},
nullptr,
297 Extracts.
push_back(Builder.CreateExtractValue(CBufLoad, {CurrentIndex++},
303 for (
int I = 0,
E = Extracts.
size();
I <
E; ++
I)
304 Result = Builder.CreateInsertElement(
305 Result, Extracts[
I], Builder.getInt32(
I), Name +
formatv(
".upto{}",
I));
346 auto *BB = Start->getParent();
349 for (
User *U : Start->users()) {
351 if (
I->getParent() == BB)
357 while (!Worklist.
empty()) {
363 for (
User *U :
I->users()) {
365 if (J->getParent() == BB)
369 for (
Use &V :
I->operands()) {
371 if (J->getParent() == BB && V != Start)
395 Value *Val = Phi->getIncomingValueForBlock(BB);
397 Builder.SetInsertPoint(&BB->
back());
401 VMap[PhiNested] = PhiNested->getIncomingValueForBlock(BB);
407 Builder.Insert(Clone);
416 for (
User *U :
II->users()) {
425 for (
unsigned I = 0,
E = Phi->getNumIncomingValues();
I <
E;
I++) {
426 auto *CurrIncomingBB = Phi->getIncomingBlock(
I);
429 PrevBBDeadInsts.
push_back(&CurrIncomingBB->back());
444 Dead->eraseFromParent();
445 CurrBBDeadInsts.
clear();
450 struct AccessAndOffset {
455 for (
User *U :
II->users())
459 while (!Worklist.
empty()) {
460 AccessAndOffset Current = Worklist.
back();
472 assert(
SI->getValueOperand() !=
II &&
"Pointer escaped!");
485 Dead->eraseFromParent();
486 II->eraseFromParent();
496 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer)
501 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
506 for (
auto *Dead : PrevBBDeadInsts)
507 Dead->eraseFromParent();
508 PrevBBDeadInsts.
clear();
509 for (
auto *Dead : DeadBB)
510 Dead->eraseFromParent();
513 for (
auto &[
II, RI] : Resources)
516 return !Resources.
empty();
524 assert(DRTM &&
"DXILResourceTypeAnalysis must be available");
541 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
544 StringRef getPassName()
const override {
return "DXIL Resource Access"; }
545 DXILResourceAccessLegacy() : FunctionPass(
ID) {}
548 void getAnalysisUsage(llvm::AnalysisUsage &AU)
const override {
553char DXILResourceAccessLegacy::ID = 0;
557 "DXIL Resource Access",
false,
false)
563 return new DXILResourceAccessLegacy();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void phiNodeRemapHelper(PHINode *Phi, BasicBlock *BB, IRBuilder<> &Builder, SmallVector< Instruction * > &UsesInBlock)
static Value * calculateGEPOffset(GetElementPtrInst *GEP, Value *PrevOffset, dxil::ResourceTypeInfo &RTI)
static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM)
static void createRawLoad(IntrinsicInst *II, LoadInst *LI, Value *Offset)
static void createLoadIntrinsic(IntrinsicInst *II, LoadInst *LI, Value *Offset, dxil::ResourceTypeInfo &RTI)
static void createTypedBufferLoad(IntrinsicInst *II, LoadInst *LI, Value *Offset, dxil::ResourceTypeInfo &RTI)
static void createStoreIntrinsic(IntrinsicInst *II, StoreInst *SI, Value *Offset, dxil::ResourceTypeInfo &RTI)
static void createCBufferLoad(IntrinsicInst *II, LoadInst *LI, Value *Offset, dxil::ResourceTypeInfo &RTI)
static void createTypedBufferStore(IntrinsicInst *II, StoreInst *SI, Value *Offset, dxil::ResourceTypeInfo &RTI)
static SmallVector< Instruction * > collectBlockUseDef(Instruction *Start)
static void phiNodeReplacement(IntrinsicInst *II, SmallVectorImpl< Instruction * > &PrevBBDeadInsts, SetVector< BasicBlock * > &DeadBB)
static void replaceAccess(IntrinsicInst *II, dxil::ResourceTypeInfo &RTI)
static void createRawStore(IntrinsicInst *II, StoreInst *SI, Value *Offset)
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.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
const Instruction & back() const
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...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A parsed version of the target data layout string in and methods for querying it.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Analysis pass which computes a DominatorTree.
FunctionPass class - This class is used to implement most global optimizations.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
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.
An instruction for reading from memory.
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.
A vector that has set insertion semantics.
void clear()
Completely clear the SetVector.
bool insert(const value_type &X)
Insert a new element into the SetVector.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
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.
StringRef - 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
The instances of the Type class are immutable: once they are created, they are never changed.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
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.
LLVM_ABI bool isTyped() const
TargetExtType * getHandleTy() const
dxil::ResourceKind getResourceKind() const
#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.
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.
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
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...
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.
void sort(IteratorTy Start, IteratorTy End)
@ RF_IgnoreMissingLocals
If this flag is set, the remapper ignores missing function-local entries (Argument,...
@ RF_NoModuleLevelChanges
If this flag is set, the remapper knows that only local values within a function (such as an instruct...
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...
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr, const MetadataPredicate *IdentityMD=nullptr)
Convert the instruction operands from referencing the current values into those specified by VM.
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
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.