LLVM 20.0.0git
DXILResourceAccess.cpp
Go to the documentation of this file.
1//===- DXILResourceAccess.cpp - Resource access via load/store ------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "DirectX.h"
12#include "llvm/IR/Dominators.h"
13#include "llvm/IR/IRBuilder.h"
16#include "llvm/IR/Intrinsics.h"
17#include "llvm/IR/IntrinsicsDirectX.h"
19
20#define DEBUG_TYPE "dxil-resource-access"
21
22using namespace llvm;
23
26 const DataLayout &DL = II->getDataLayout();
27
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);
32
33 // We need the size of an element in bytes so that we can calculate the offset
34 // in elements given a total offset in bytes later.
35 Type *ScalarType = ContainedType->getScalarType();
36 uint64_t ScalarSize = DL.getTypeSizeInBits(ScalarType) / 8;
37
38 // Process users keeping track of indexing accumulated from GEPs.
39 struct AccessAndIndex {
40 User *Access;
41 Value *Index;
42 };
44 for (User *U : II->users())
45 Worklist.push_back({U, nullptr});
46
48 while (!Worklist.empty()) {
49 AccessAndIndex Current = Worklist.back();
50 Worklist.pop_back();
51
52 if (auto *GEP = dyn_cast<GetElementPtrInst>(Current.Access)) {
53 IRBuilder<> Builder(GEP);
54
55 Value *Index;
56 APInt ConstantOffset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
57 if (GEP->accumulateConstantOffset(DL, ConstantOffset)) {
58 APInt Scaled = ConstantOffset.udiv(ScalarSize);
59 Index = ConstantInt::get(Builder.getInt32Ty(), Scaled);
60 } else {
61 auto IndexIt = GEP->idx_begin();
62 assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 &&
63 "GEP is not indexing through pointer");
64 ++IndexIt;
65 Index = *IndexIt;
66 assert(++IndexIt == GEP->idx_end() && "Too many indices in GEP");
67 }
68
69 for (User *U : GEP->users())
70 Worklist.push_back({U, Index});
71 DeadInsts.push_back(GEP);
72
73 } else if (auto *SI = dyn_cast<StoreInst>(Current.Access)) {
74 assert(SI->getValueOperand() != II && "Pointer escaped!");
75 IRBuilder<> Builder(SI);
76
77 Value *V = SI->getValueOperand();
78 if (V->getType() == ContainedType) {
79 // V is already the right type.
80 } else if (V->getType() == ScalarType) {
81 // We're storing a scalar, so we need to load the current value and only
82 // replace the relevant part.
83 auto *Load = Builder.CreateIntrinsic(
84 ContainedType, Intrinsic::dx_resource_load_typedbuffer,
85 {II->getOperand(0), II->getOperand(1)});
86 // If we have an offset from seeing a GEP earlier, use it.
87 Value *IndexOp = Current.Index
88 ? Current.Index
89 : ConstantInt::get(Builder.getInt32Ty(), 0);
90 V = Builder.CreateInsertElement(Load, V, IndexOp);
91 } else {
92 llvm_unreachable("Store to typed resource has invalid type");
93 }
94
95 auto *Inst = Builder.CreateIntrinsic(
96 Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
97 {II->getOperand(0), II->getOperand(1), V});
98 SI->replaceAllUsesWith(Inst);
99 DeadInsts.push_back(SI);
100
101 } else if (auto *LI = dyn_cast<LoadInst>(Current.Access)) {
102 IRBuilder<> Builder(LI);
103 Value *V = Builder.CreateIntrinsic(
104 ContainedType, Intrinsic::dx_resource_load_typedbuffer,
105 {II->getOperand(0), II->getOperand(1)});
106 if (Current.Index)
107 V = Builder.CreateExtractElement(V, Current.Index);
108
109 LI->replaceAllUsesWith(V);
110 DeadInsts.push_back(LI);
111
112 } else
113 llvm_unreachable("Unhandled instruction - pointer escaped?");
114 }
115
116 // Traverse the now-dead instructions in RPO and remove them.
117 for (Instruction *Dead : llvm::reverse(DeadInsts))
118 Dead->eraseFromParent();
119 II->eraseFromParent();
120}
121
123 bool Changed = false;
125 for (BasicBlock &BB : F)
126 for (Instruction &I : BB)
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());
130 Resources.emplace_back(II, DRTM[HandleTy]);
131 }
132
133 for (auto &[II, RI] : Resources) {
134 if (RI.isTyped()) {
135 Changed = true;
137 }
138
139 // TODO: handle other resource types. We should probably have an
140 // `unreachable` here once we've added support for all of them.
141 }
142
143 return Changed;
144}
145
149 DXILResourceTypeMap *DRTM =
150 MAMProxy.getCachedResult<DXILResourceTypeAnalysis>(*F.getParent());
151 assert(DRTM && "DXILResourceTypeAnalysis must be available");
152
153 bool MadeChanges = transformResourcePointers(F, *DRTM);
154 if (!MadeChanges)
155 return PreservedAnalyses::all();
156
160 return PA;
161}
162
163namespace {
164class DXILResourceAccessLegacy : public FunctionPass {
165public:
166 bool runOnFunction(Function &F) override {
167 DXILResourceTypeMap &DRTM =
168 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
169
170 return transformResourcePointers(F, DRTM);
171 }
172 StringRef getPassName() const override { return "DXIL Resource Access"; }
173 DXILResourceAccessLegacy() : FunctionPass(ID) {}
174
175 static char ID; // Pass identification.
176 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
179 }
180};
181char DXILResourceAccessLegacy::ID = 0;
182} // end anonymous namespace
183
184INITIALIZE_PASS_BEGIN(DXILResourceAccessLegacy, DEBUG_TYPE,
185 "DXIL Resource Access", false, false)
187INITIALIZE_PASS_END(DXILResourceAccessLegacy, DEBUG_TYPE,
188 "DXIL Resource Access", false, false)
189
191 return new DXILResourceAccessLegacy();
192}
@ Scaled
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void replaceTypedBufferAccess(IntrinsicInst *II, dxil::ResourceTypeInfo &RTI)
static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM)
DXIL Resource Access
uint32_t Index
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
Hexagon Common GEP
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:57
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:410
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.
Definition: BasicBlock.h:61
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:317
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2503
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2491
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.
Definition: IRBuilder.cpp:890
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:523
Type * getVoidTy()
Fetch the type representing void.
Definition: IRBuilder.h:561
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2697
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
Definition: PassManager.h:692
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserve()
Mark an analysis as preserved.
Definition: Analysis.h:131
bool empty() const
Definition: SmallVector.h:81
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:937
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
LLVM Value Representation.
Definition: Value.h:74
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
#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.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
FunctionPass * createDXILResourceAccessLegacyPass()
Pass to update resource accesses to use load/store directly.