20#include "llvm/IR/IntrinsicsDirectX.h"
25#define DEBUG_TYPE "dxil-resource-access"
31 assert(!PrevOffset &&
"Non-constant GEP chains not handled yet");
41 ScalarSize =
DL.getTypeSizeInBits(ScalarType) / 8;
44 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(
GEP->getType()), 0);
45 if (
GEP->accumulateConstantOffset(
DL, ConstantOffset)) {
50 auto IndexIt =
GEP->idx_begin();
52 "GEP is not indexing through pointer");
55 assert(++IndexIt ==
GEP->idx_end() &&
"Too many indices in GEP");
65 Value *V =
SI->getValueOperand();
66 if (V->getType() == ContainedType) {
72 auto *Load = Builder.CreateIntrinsic(
73 LoadType, Intrinsic::dx_resource_load_typedbuffer,
74 {
II->getOperand(0),
II->getOperand(1)});
75 auto *
Struct = Builder.CreateExtractValue(Load, {0});
79 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
85 auto *Inst = Builder.CreateIntrinsic(
86 Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
87 {II->getOperand(0), II->getOperand(1), V});
88 SI->replaceAllUsesWith(Inst);
95 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
96 Value *V =
SI->getValueOperand();
98 auto *Inst = Builder.CreateIntrinsic(
99 Builder.getVoidTy(), Intrinsic::dx_resource_store_rawbuffer,
100 {II->getOperand(0), II->getOperand(1), Offset, V});
101 SI->replaceAllUsesWith(Inst);
143 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_typedbuffer,
144 {
II->getOperand(0),
II->getOperand(1)});
145 V = Builder.CreateExtractValue(V, {0});
148 V = Builder.CreateExtractElement(V,
Offset);
155 Builder.getInt32(0));
165 Offset = ConstantInt::get(Builder.getInt32Ty(), 0);
167 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_rawbuffer,
168 {
II->getOperand(0),
II->getOperand(1),
Offset});
169 V = Builder.CreateExtractValue(V, {0});
210 auto *BB = Start->getParent();
213 for (
User *U : Start->users()) {
215 if (
I->getParent() == BB)
221 while (!Worklist.
empty()) {
227 for (
User *U :
I->users()) {
229 if (J->getParent() == BB)
233 for (
Use &V :
I->operands()) {
235 if (J->getParent() == BB && V != Start)
259 Value *Val = Phi->getIncomingValueForBlock(BB);
261 Builder.SetInsertPoint(&BB->
back());
265 VMap[PhiNested] = PhiNested->getIncomingValueForBlock(BB);
271 Builder.Insert(Clone);
280 for (
User *U :
II->users()) {
289 for (
unsigned I = 0,
E = Phi->getNumIncomingValues();
I <
E;
I++) {
290 auto *CurrIncomingBB = Phi->getIncomingBlock(
I);
293 PrevBBDeadInsts.
push_back(&CurrIncomingBB->back());
308 Dead->eraseFromParent();
309 CurrBBDeadInsts.
clear();
314 struct AccessAndOffset {
319 for (
User *U :
II->users())
323 while (!Worklist.
empty()) {
324 AccessAndOffset Current = Worklist.
back();
336 assert(
SI->getValueOperand() !=
II &&
"Pointer escaped!");
349 Dead->eraseFromParent();
350 II->eraseFromParent();
360 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer)
365 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
370 for (
auto *Dead : PrevBBDeadInsts)
371 Dead->eraseFromParent();
372 PrevBBDeadInsts.
clear();
373 for (
auto *Dead : DeadBB)
374 Dead->eraseFromParent();
377 for (
auto &[
II, RI] : Resources)
380 return !Resources.
empty();
388 assert(DRTM &&
"DXILResourceTypeAnalysis must be available");
405 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
408 StringRef getPassName()
const override {
return "DXIL Resource Access"; }
409 DXILResourceAccessLegacy() : FunctionPass(
ID) {}
412 void getAnalysisUsage(llvm::AnalysisUsage &AU)
const override {
417char DXILResourceAccessLegacy::ID = 0;
421 "DXIL Resource Access",
false,
false)
427 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 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
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...
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.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
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 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
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.
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 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.