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);
36 uint64_t ScalarSize =
DL.getTypeSizeInBits(ScalarType) / 8;
39 struct AccessAndIndex {
44 for (
User *U :
II->users())
48 while (!Worklist.
empty()) {
49 AccessAndIndex Current = Worklist.
back();
52 if (
auto *
GEP = dyn_cast<GetElementPtrInst>(Current.Access)) {
56 APInt ConstantOffset(
DL.getIndexTypeSizeInBits(
GEP->getType()), 0);
57 if (
GEP->accumulateConstantOffset(
DL, ConstantOffset)) {
61 auto IndexIt =
GEP->idx_begin();
62 assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 &&
63 "GEP is not indexing through pointer");
66 assert(++IndexIt ==
GEP->idx_end() &&
"Too many indices in GEP");
73 }
else if (
auto *SI = dyn_cast<StoreInst>(Current.Access)) {
74 assert(SI->getValueOperand() !=
II &&
"Pointer escaped!");
77 Value *V = SI->getValueOperand();
78 if (V->getType() == ContainedType) {
80 }
else if (V->getType() == ScalarType) {
84 ContainedType, Intrinsic::dx_resource_load_typedbuffer,
85 {
II->getOperand(0),
II->getOperand(1)});
87 Value *IndexOp = Current.Index
96 Builder.
getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
97 {II->getOperand(0), II->getOperand(1), V});
98 SI->replaceAllUsesWith(Inst);
101 }
else if (
auto *LI = dyn_cast<LoadInst>(Current.Access)) {
104 ContainedType, Intrinsic::dx_resource_load_typedbuffer,
105 {
II->getOperand(0),
II->getOperand(1)});
118 Dead->eraseFromParent();
119 II->eraseFromParent();
123 bool Changed =
false;
127 if (
auto *
II = dyn_cast<IntrinsicInst>(&
I))
128 if (
II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
129 auto *HandleTy = cast<TargetExtType>(
II->getArgOperand(0)->getType());
133 for (
auto &[
II, RI] : Resources) {
151 assert(DRTM &&
"DXILResourceTypeAnalysis must be available");
168 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
172 StringRef getPassName()
const override {
return "DXIL Resource Access"; }
181char DXILResourceAccessLegacy::ID = 0;
185 "DXIL Resource Access",
false,
false)
191 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="")
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
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.
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'.
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.