17#include "llvm/IR/IntrinsicsDirectX.h"
20#define DEBUG_TYPE "dxil-resource-access"
28 auto *HandleType = cast<TargetExtType>(
II->getOperand(0)->getType());
29 assert(HandleType->getName() ==
"dx.TypedBuffer" &&
30 "Unexpected typed buffer type");
31 Type *ContainedType = HandleType->getTypeParameter(0);
39 uint64_t ScalarSize =
DL.getTypeSizeInBits(ScalarType) / 8;
42 struct AccessAndIndex {
47 for (
User *U :
II->users())
51 while (!Worklist.
empty()) {
52 AccessAndIndex Current = Worklist.
back();
55 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(Current.Access)) {
59 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(
GEP->getType()), 0);
60 if (
GEP->accumulateConstantOffset(
DL, ConstantOffset)) {
64 auto IndexIt =
GEP->idx_begin();
65 assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 &&
66 "GEP is not indexing through pointer");
69 assert(++IndexIt ==
GEP->idx_end() &&
"Too many indices in GEP");
76 }
else if (
auto *SI = dyn_cast<StoreInst>(Current.Access)) {
77 assert(SI->getValueOperand() !=
II &&
"Pointer escaped!");
80 Value *V = SI->getValueOperand();
81 if (V->getType() == ContainedType) {
83 }
else if (V->getType() == ScalarType) {
87 LoadType, Intrinsic::dx_resource_load_typedbuffer,
88 {
II->getOperand(0),
II->getOperand(1)});
92 Value *IndexOp = Current.Index
101 Builder.
getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
102 {II->getOperand(0), II->getOperand(1), V});
103 SI->replaceAllUsesWith(Inst);
106 }
else if (
auto *LI = dyn_cast<LoadInst>(Current.Access)) {
109 LoadType, Intrinsic::dx_resource_load_typedbuffer,
110 {
II->getOperand(0),
II->getOperand(1)});
125 Dead->eraseFromParent();
126 II->eraseFromParent();
130 bool Changed =
false;
134 if (
auto *
II = dyn_cast<IntrinsicInst>(&
I))
135 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
136 auto *HandleTy = cast<TargetExtType>(
II->getArgOperand(0)->getType());
140 for (
auto &[
II, RI] : Resources) {
158 assert(DRTM &&
"DXILResourceTypeAnalysis must be available");
175 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
179 StringRef getPassName()
const override {
return "DXIL Resource Access"; }
188char DXILResourceAccessLegacy::ID = 0;
192 "DXIL Resource Access",
false,
false)
198 return new DXILResourceAccessLegacy();
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void replaceTypedBufferAccess(IntrinsicInst *II, dxil::ResourceTypeInfo &RTI)
static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM)
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
A container for analyses that lazily runs them and caches their results.
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.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
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.
Legacy analysis pass which computes a DominatorTree.
FunctionPass class - This class is used to implement most global optimizations.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Value * CreateExtractValue(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &Name="")
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, FMFSource FMFSource={}, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Type * getVoidTy()
Fetch the type representing void.
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 analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
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.
void preserve()
Mark an analysis as preserved.
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.
StringRef - Represent a constant reference to a string, i.e.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt1Ty(LLVMContext &C)
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
LLVM Value Representation.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
#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.
This is an optimization pass for GlobalISel generic memory operations.
auto reverse(ContainerTy &&C)
FunctionPass * createDXILResourceAccessLegacyPass()
Pass to update resource accesses to use load/store directly.