LLVM 23.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"
11#include "llvm/ADT/SetVector.h"
15#include "llvm/IR/BasicBlock.h"
17#include "llvm/IR/Dominators.h"
18#include "llvm/IR/IRBuilder.h"
19#include "llvm/IR/Instruction.h"
22#include "llvm/IR/Intrinsics.h"
23#include "llvm/IR/IntrinsicsDirectX.h"
24#include "llvm/IR/LLVMContext.h"
25#include "llvm/IR/User.h"
29
30#define DEBUG_TYPE "dxil-resource-access"
31
32using namespace llvm;
33
36 LLVMContext &Context = I->getContext();
37 std::string InstStr;
38 raw_string_ostream InstOS(InstStr);
39 I->print(InstOS);
40 Context.diagnose(
41 DiagnosticInfoGeneric("At resource access:" + Twine(InstStr), DS_Note));
42
43 for (auto *Handle : Handles) {
44 std::string HandleStr;
45 raw_string_ostream HandleOS(HandleStr);
46 Handle->print(HandleOS);
47 Context.diagnose(DiagnosticInfoGeneric(
48 "Uses resource handle:" + Twine(HandleStr), DS_Note));
49 }
50 Context.diagnose(DiagnosticInfoGeneric(
51 "Resource access is not guaranteed to map to a unique global resource"));
52}
53
55 Value *Ptr, uint64_t AccessSize) {
56 Value *Offset = nullptr;
57
58 while (Ptr) {
59 if (auto *II = dyn_cast<IntrinsicInst>(Ptr)) {
60 assert((II->getIntrinsicID() == Intrinsic::dx_resource_getpointer ||
61 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer) &&
62 "Resource access through unexpected intrinsic");
63 return Offset ? Offset : ConstantInt::get(Builder.getInt32Ty(), 0);
64 }
65
67 assert(GEP && "Resource access through unexpected instruction");
68
69 unsigned NumIndices = GEP->getNumIndices();
70 uint64_t IndexScale = DL.getTypeAllocSize(GEP->getSourceElementType());
71 APInt ConstantOffset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
72 Value *GEPOffset;
73 if (GEP->accumulateConstantOffset(DL, ConstantOffset)) {
74 // We have a constant offset (in bytes).
75 GEPOffset =
76 ConstantInt::get(DL.getIndexType(GEP->getType()), ConstantOffset);
77 IndexScale = 1;
78 } else if (NumIndices == 1) {
79 // If we have a single index we're indexing into a top level array. This
80 // generally only happens with cbuffers.
81 GEPOffset = *GEP->idx_begin();
82 } else if (NumIndices == 2) {
83 // If we have two indices, this should be an access through a pointer.
84 auto *IndexIt = GEP->idx_begin();
85 assert(cast<ConstantInt>(IndexIt)->getZExtValue() == 0 &&
86 "GEP is not indexing through pointer");
87 GEPOffset = *(++IndexIt);
88 } else
89 llvm_unreachable("Unhandled GEP structure for resource access");
90
91 uint64_t ElemSize = AccessSize;
92 if (!(IndexScale % ElemSize)) {
93 // If our scale is an exact multiple of the access size, adjust the
94 // scaling to avoid an unnecessary division.
95 IndexScale /= ElemSize;
96 ElemSize = 1;
97 }
98 if (IndexScale != 1)
99 GEPOffset = Builder.CreateMul(
100 GEPOffset, ConstantInt::get(Builder.getInt32Ty(), IndexScale));
101 if (ElemSize != 1)
102 GEPOffset = Builder.CreateUDiv(
103 GEPOffset, ConstantInt::get(Builder.getInt32Ty(), ElemSize));
104
105 Offset = Offset ? Builder.CreateAdd(Offset, GEPOffset) : GEPOffset;
106 Ptr = GEP->getPointerOperand();
107 }
108
109 llvm_unreachable("GEP of null pointer?");
110}
111
114 const DataLayout &DL = SI->getDataLayout();
115 IRBuilder<> Builder(SI);
116 Type *ContainedType = RTI.getHandleTy()->getTypeParameter(0);
117 Type *ScalarType = ContainedType->getScalarType();
118 Type *LoadType = StructType::get(ContainedType, Builder.getInt1Ty());
119
120 Value *V = SI->getValueOperand();
121 if (V->getType() == ContainedType) {
122 // V is already the right type.
123 assert(SI->getPointerOperand() == II &&
124 "Store of whole element has mismatched address to store to");
125 } else if (V->getType() == ScalarType) {
126 // We're storing a scalar, so we need to load the current value and only
127 // replace the relevant part.
128 auto *Load = Builder.CreateIntrinsic(
129 LoadType, Intrinsic::dx_resource_load_typedbuffer,
130 {II->getOperand(0), II->getOperand(1)});
131 auto *Struct = Builder.CreateExtractValue(Load, {0});
132
133 uint64_t AccessSize = DL.getTypeSizeInBits(ScalarType) / 8;
134 Value *Offset =
135 traverseGEPOffsets(DL, Builder, SI->getPointerOperand(), AccessSize);
136 V = Builder.CreateInsertElement(Struct, V, Offset);
137 } else {
138 llvm_unreachable("Store to typed resource has invalid type");
139 }
140
141 auto *Inst = Builder.CreateIntrinsic(
142 Builder.getVoidTy(), Intrinsic::dx_resource_store_typedbuffer,
143 {II->getOperand(0), II->getOperand(1), V});
144 SI->replaceAllUsesWith(Inst);
145}
146
147static void emitRawStore(IRBuilder<> &Builder, Value *Buffer, Value *Index,
149 // For raw buffer (ie, HLSL's ByteAddressBuffer), we need to fold the access
150 // entirely into the index.
151 if (!RTI.isStruct()) {
152 auto *ConstantOffset = dyn_cast<ConstantInt>(Offset);
153 if (!ConstantOffset || !ConstantOffset->isZero())
154 Index = Builder.CreateAdd(Index, Offset);
155 Offset = llvm::PoisonValue::get(Builder.getInt32Ty());
156 }
157
158 Builder.CreateIntrinsic(Builder.getVoidTy(),
159 Intrinsic::dx_resource_store_rawbuffer,
160 {Buffer, Index, Offset, V});
161}
162
165 const DataLayout &DL = SI->getDataLayout();
166 IRBuilder<> Builder(SI);
167
168 Value *V = SI->getValueOperand();
169 assert(!V->getType()->isAggregateType() &&
170 "Resource store should be scalar or vector type");
171
172 Value *Index = II->getOperand(1);
173 // The offset for the rawbuffer load and store ops is always in bytes.
174 uint64_t AccessSize = 1;
175 Value *Offset =
176 traverseGEPOffsets(DL, Builder, SI->getPointerOperand(), AccessSize);
177
178 auto *VT = dyn_cast<FixedVectorType>(V->getType());
179 if (VT && VT->getNumElements() > 4) {
180 // Split into stores of at most 4 elements.
181 Type *EltTy = VT->getElementType();
182 Value *Stride = ConstantInt::get(Builder.getInt32Ty(),
183 4 * (DL.getTypeSizeInBits(EltTy) / 8));
184
185 SmallVector<int, 4> Indices;
186 for (unsigned int I = 0, N = VT->getNumElements(); I < N; I += 4) {
187 if (I > 0)
188 Offset = Builder.CreateAdd(Offset, Stride);
189
190 for (unsigned int J = I, E = std::min(N, J + 4); J < E; ++J)
191 Indices.push_back(J);
192 Value *Part = Builder.CreateShuffleVector(V, Indices);
193 emitRawStore(Builder, II->getOperand(0), Index, Offset, Part, RTI);
194
195 Indices.clear();
196 }
197 } else
198 emitRawStore(Builder, II->getOperand(0), Index, Offset, V, RTI);
199}
200
232
235 const DataLayout &DL = LI->getDataLayout();
236 IRBuilder<> Builder(LI);
237 Type *ContainedType = RTI.getHandleTy()->getTypeParameter(0);
238 Type *LoadType = StructType::get(ContainedType, Builder.getInt1Ty());
239
240 Value *V =
241 Builder.CreateIntrinsic(LoadType, Intrinsic::dx_resource_load_typedbuffer,
242 {II->getOperand(0), II->getOperand(1)});
243 V = Builder.CreateExtractValue(V, {0});
244
245 Type *ScalarType = ContainedType->getScalarType();
246 uint64_t AccessSize = DL.getTypeSizeInBits(ScalarType) / 8;
247 Value *Offset =
248 traverseGEPOffsets(DL, Builder, LI->getPointerOperand(), AccessSize);
249 auto *ConstantOffset = dyn_cast<ConstantInt>(Offset);
250 if (!ConstantOffset || !ConstantOffset->isZero())
251 V = Builder.CreateExtractElement(V, Offset);
252
253 // If we loaded a <1 x ...> instead of a scalar (presumably to feed a
254 // shufflevector), then make sure we're maintaining the resulting type.
255 if (auto *VT = dyn_cast<FixedVectorType>(LI->getType()))
256 if (VT->getNumElements() == 1 && !isa<FixedVectorType>(V->getType()))
257 V = Builder.CreateInsertElement(PoisonValue::get(VT), V,
258 Builder.getInt32(0));
259
260 LI->replaceAllUsesWith(V);
261}
262
265 const DataLayout &DL = LI->getDataLayout();
266 IRBuilder<> Builder(LI);
267 Type *ContainedType = RTI.getHandleTy()->getTypeParameter(0);
268
269 Value *Handle = II->getOperand(0);
270 Value *Coords = II->getOperand(1);
271
272 // For operator[], mip level is 0.
273 Value *MipLevel = Builder.getInt32(0);
274
275 // For operator[], offsets are zero.
276 Type *CoordTy = Coords->getType();
277 Type *OffsetTy;
278 if (auto *VecTy = dyn_cast<FixedVectorType>(CoordTy))
279 OffsetTy =
280 FixedVectorType::get(Builder.getInt32Ty(), VecTy->getNumElements());
281 else
282 OffsetTy = Builder.getInt32Ty();
283 Value *Offsets = Constant::getNullValue(OffsetTy);
284
285 Value *V =
286 Builder.CreateIntrinsic(ContainedType, Intrinsic::dx_resource_load_level,
287 {Handle, Coords, MipLevel, Offsets});
288
289 Type *ScalarType = ContainedType->getScalarType();
290 uint64_t AccessSize = DL.getTypeSizeInBits(ScalarType) / 8;
291 Value *Offset =
292 traverseGEPOffsets(DL, Builder, LI->getPointerOperand(), AccessSize);
293 auto *ConstantOffset = dyn_cast<ConstantInt>(Offset);
294 if (!ConstantOffset || !ConstantOffset->isZero())
295 V = Builder.CreateExtractElement(V, Offset);
296
297 // If we loaded a <1 x ...> instead of a scalar (presumably to feed a
298 // shufflevector), then make sure we're maintaining the resulting type.
299 if (auto *VT = dyn_cast<FixedVectorType>(LI->getType()))
300 if (VT->getNumElements() == 1 && !isa<FixedVectorType>(V->getType()))
301 V = Builder.CreateInsertElement(PoisonValue::get(VT), V,
302 Builder.getInt32(0));
303
304 LI->replaceAllUsesWith(V);
305}
306
307static Value *emitRawLoad(IRBuilder<> &Builder, Type *Ty, Value *Buffer,
308 Value *Index, Value *Offset,
310 // For raw buffer (ie, HLSL's ByteAddressBuffer), we need to fold the access
311 // entirely into the index.
312 if (!RTI.isStruct()) {
313 auto *ConstantOffset = dyn_cast<ConstantInt>(Offset);
314 if (!ConstantOffset || !ConstantOffset->isZero())
315 Index = Builder.CreateAdd(Index, Offset);
316 Offset = llvm::PoisonValue::get(Builder.getInt32Ty());
317 }
318
319 // The load intrinsic includes the bit for CheckAccessFullyMapped, so we need
320 // to add that to the return type.
321 Type *TypeWithCheck = StructType::get(Ty, Builder.getInt1Ty());
322 Value *V = Builder.CreateIntrinsic(TypeWithCheck,
323 Intrinsic::dx_resource_load_rawbuffer,
324 {Buffer, Index, Offset});
325 return Builder.CreateExtractValue(V, {0});
326}
327
330 const DataLayout &DL = LI->getDataLayout();
331 IRBuilder<> Builder(LI);
332
333 Value *Index = II->getOperand(1);
334 // The offset for the rawbuffer load and store ops is always in bytes.
335 uint64_t AccessSize = 1;
336 Value *Offset =
337 traverseGEPOffsets(DL, Builder, LI->getPointerOperand(), AccessSize);
338
339 // TODO: We could make this handle aggregates by walking the structure and
340 // handling each field individually, but we don't ever generate code that
341 // would hit that so it seems superfluous.
342 assert(!LI->getType()->isAggregateType() &&
343 "Resource load should be scalar or vector type");
344
345 Value *V;
346 if (auto *VT = dyn_cast<FixedVectorType>(LI->getType())) {
347 // Split into loads of at most 4 elements.
348 Type *EltTy = VT->getElementType();
349 Value *Stride = ConstantInt::get(Builder.getInt32Ty(),
350 4 * (DL.getTypeSizeInBits(EltTy) / 8));
351
353 for (unsigned int I = 0, N = VT->getNumElements(); I < N; I += 4) {
354 Type *Ty = FixedVectorType::get(EltTy, N - I < 4 ? N - I : 4);
355 if (I > 0)
356 Offset = Builder.CreateAdd(Offset, Stride);
357 Parts.push_back(
358 emitRawLoad(Builder, Ty, II->getOperand(0), Index, Offset, RTI));
359 }
360
361 V = Parts.size() > 1 ? concatenateVectors(Builder, Parts) : Parts[0];
362 } else
363 V = emitRawLoad(Builder, LI->getType(), II->getOperand(0), Index, Offset,
364 RTI);
365
366 LI->replaceAllUsesWith(V);
367}
368
369namespace {
370/// Helper for building a `load.cbufferrow` intrinsic given a simple type.
371struct CBufferRowIntrin {
372 Intrinsic::ID IID;
373 Type *RetTy;
374 unsigned int EltSize;
375 unsigned int NumElts;
376
377 CBufferRowIntrin(const DataLayout &DL, Type *Ty) {
378 assert(Ty == Ty->getScalarType() && "Expected scalar type");
379
380 switch (DL.getTypeSizeInBits(Ty)) {
381 case 16:
382 IID = Intrinsic::dx_resource_load_cbufferrow_8;
383 RetTy = StructType::get(Ty, Ty, Ty, Ty, Ty, Ty, Ty, Ty);
384 EltSize = 2;
385 NumElts = 8;
386 break;
387 case 32:
388 IID = Intrinsic::dx_resource_load_cbufferrow_4;
389 RetTy = StructType::get(Ty, Ty, Ty, Ty);
390 EltSize = 4;
391 NumElts = 4;
392 break;
393 case 64:
394 IID = Intrinsic::dx_resource_load_cbufferrow_2;
395 RetTy = StructType::get(Ty, Ty);
396 EltSize = 8;
397 NumElts = 2;
398 break;
399 default:
400 llvm_unreachable("Only 16, 32, and 64 bit types supported");
401 }
402 }
403};
404} // namespace
405
408 const DataLayout &DL = LI->getDataLayout();
409
410 Type *Ty = LI->getType();
411 assert(!isa<StructType>(Ty) && "Structs not handled yet");
412 CBufferRowIntrin Intrin(DL, Ty->getScalarType());
413
414 StringRef Name = LI->getName();
415 Value *Handle = II->getOperand(0);
416
417 IRBuilder<> Builder(LI);
418
419 ConstantInt *GlobalOffset =
420 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer
421 ? ConstantInt::get(Builder.getInt32Ty(), 0)
422 : dyn_cast<ConstantInt>(II->getOperand(1));
423 assert(GlobalOffset && "CBuffer getpointer index must be constant");
424
425 uint64_t GlobalOffsetVal = GlobalOffset->getZExtValue();
426 Value *CurrentRow = ConstantInt::get(
427 Builder.getInt32Ty(), GlobalOffsetVal / hlsl::CBufferRowSizeInBytes);
428 unsigned int CurrentIndex =
429 (GlobalOffsetVal % hlsl::CBufferRowSizeInBytes) / Intrin.EltSize;
430
431 // Every object in a cbuffer either fits in a row or is aligned to a row. This
432 // means that only the very last pointer access can point into a row.
433 auto *LastGEP = dyn_cast<GEPOperator>(LI->getPointerOperand());
434 if (!LastGEP) {
435 // If we don't have a GEP at all we're just accessing the resource through
436 // the result of getpointer directly.
437 assert(LI->getPointerOperand() == II &&
438 "Unexpected indirect access to resource without GEP");
439 } else {
440 Value *GEPOffset = traverseGEPOffsets(
441 DL, Builder, LastGEP->getPointerOperand(), hlsl::CBufferRowSizeInBytes);
442 CurrentRow = Builder.CreateAdd(GEPOffset, CurrentRow);
443
444 APInt ConstantOffset(DL.getIndexTypeSizeInBits(LastGEP->getType()), 0);
445 if (LastGEP->accumulateConstantOffset(DL, ConstantOffset)) {
446 APInt Remainder(DL.getIndexTypeSizeInBits(LastGEP->getType()),
448 APInt::udivrem(ConstantOffset, Remainder, ConstantOffset, Remainder);
449 CurrentRow = Builder.CreateAdd(
450 CurrentRow, ConstantInt::get(Builder.getInt32Ty(), ConstantOffset));
451 CurrentIndex += Remainder.udiv(Intrin.EltSize).getZExtValue();
452 } else {
453 assert(LastGEP->getNumIndices() == 1 &&
454 "Last GEP of cbuffer access is not array or struct access");
455 // We assume a non-constant access will be row-aligned. This is safe
456 // because arrays and structs are always row aligned, and accesses to
457 // vector elements will show up as a load of the vector followed by an
458 // extractelement.
459 CurrentRow = cast<ConstantInt>(CurrentRow)->isZero()
460 ? *LastGEP->idx_begin()
461 : Builder.CreateAdd(CurrentRow, *LastGEP->idx_begin());
462 CurrentIndex = 0;
463 }
464 }
465
466 auto *CBufLoad = Builder.CreateIntrinsic(
467 Intrin.RetTy, Intrin.IID, {Handle, CurrentRow}, nullptr, Name + ".load");
468 auto *Elt =
469 Builder.CreateExtractValue(CBufLoad, {CurrentIndex++}, Name + ".extract");
470
471 // At this point we've loaded the first scalar of our result, but our original
472 // type may have been a vector.
473 unsigned int Remaining =
474 ((DL.getTypeSizeInBits(Ty) / 8) / Intrin.EltSize) - 1;
475 if (Remaining == 0) {
476 // We only have a single element, so we're done.
477 Value *Result = Elt;
478
479 // However, if we loaded a <1 x T>, then we need to adjust the type.
480 if (auto *VT = dyn_cast<FixedVectorType>(Ty)) {
481 assert(VT->getNumElements() == 1 && "Can't have multiple elements here");
482 Result = Builder.CreateInsertElement(PoisonValue::get(VT), Result,
483 Builder.getInt32(0), Name);
484 }
485 LI->replaceAllUsesWith(Result);
486 return;
487 }
488
489 // Walk each element and extract it, wrapping to new rows as needed.
490 SmallVector<Value *> Extracts{Elt};
491 while (Remaining--) {
492 CurrentIndex %= Intrin.NumElts;
493
494 if (CurrentIndex == 0) {
495 CurrentRow = Builder.CreateAdd(CurrentRow,
496 ConstantInt::get(Builder.getInt32Ty(), 1));
497 CBufLoad = Builder.CreateIntrinsic(Intrin.RetTy, Intrin.IID,
498 {Handle, CurrentRow}, nullptr,
499 Name + ".load");
500 }
501
502 Extracts.push_back(Builder.CreateExtractValue(CBufLoad, {CurrentIndex++},
503 Name + ".extract"));
504 }
505
506 // Finally, we build up the original loaded value.
507 Value *Result = PoisonValue::get(Ty);
508 for (int I = 0, E = Extracts.size(); I < E; ++I)
509 Result = Builder.CreateInsertElement(
510 Result, Extracts[I], Builder.getInt32(I), Name + formatv(".upto{}", I));
511 LI->replaceAllUsesWith(Result);
512}
513
547
549 if (auto *LI = dyn_cast<LoadInst>(AI))
550 return dyn_cast<Instruction>(LI->getPointerOperand());
551 if (auto *SI = dyn_cast<StoreInst>(AI))
552 return dyn_cast<Instruction>(SI->getPointerOperand());
553
554 return nullptr;
555}
556
557static const std::array<Intrinsic::ID, 2> HandleIntrins = {
558 Intrinsic::dx_resource_handlefrombinding,
559 Intrinsic::dx_resource_handlefromimplicitbinding,
560};
561
563 SmallVector<Value *> Worklist = {Ptr};
565
566 while (!Worklist.empty()) {
567 Value *X = Worklist.pop_back_val();
568
569 if (!X->getType()->isPointerTy() && !X->getType()->isTargetExtTy())
570 return {}; // Early exit on store/load into non-resource
571
572 if (auto *Phi = dyn_cast<PHINode>(X))
573 for (Use &V : Phi->incoming_values())
574 Worklist.push_back(V.get());
575 else if (auto *Select = dyn_cast<SelectInst>(X))
576 for (Value *V : {Select->getTrueValue(), Select->getFalseValue()})
577 Worklist.push_back(V);
578 else if (auto *II = dyn_cast<IntrinsicInst>(X)) {
579 Intrinsic::ID IID = II->getIntrinsicID();
580
581 if (IID == Intrinsic::dx_resource_getpointer)
582 Worklist.push_back(II->getArgOperand(/*Handle=*/0));
583
585 Handles.push_back(II);
586 }
587 }
588
589 return Handles;
590}
591
593 DXILResourceTypeMap &DRTM) {
595 "Only expects a Handle as determined from collectUsedHandles.");
596
597 auto *HandleTy = cast<TargetExtType>(Handle->getType());
598 dxil::ResourceClass Class = DRTM[HandleTy].getResourceClass();
599 uint32_t Space = cast<ConstantInt>(Handle->getArgOperand(0))->getZExtValue();
600 uint32_t LowerBound =
601 cast<ConstantInt>(Handle->getArgOperand(1))->getZExtValue();
602 uint32_t Size = cast<ConstantInt>(Handle->getArgOperand(2))->getZExtValue();
603 uint32_t UpperBound = Size == UINT32_MAX ? UINT32_MAX : LowerBound + Size - 1;
604
605 return hlsl::Binding(Class, Space, LowerBound, UpperBound, nullptr);
606}
607
608namespace {
609/// Helper for propagating the current handle and ptr indices.
610struct AccessIndices {
611 Value *GetPtrIdx;
612 Value *HandleIdx;
613
614 bool hasGetPtrIdx() { return GetPtrIdx != nullptr; }
615 bool hasHandleIdx() { return HandleIdx != nullptr; }
616};
617} // namespace
618
619// getAccessIndices traverses up the control flow that a ptr came from and
620// propagates back the indicies used to access the resource (AccessIndices):
621//
622// - GetPtrIdx is the index of dx.resource.getpointer
623// - HandleIdx is the index of dx.resource.handlefrom.*
624static AccessIndices
626 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
627 if (llvm::is_contained(HandleIntrins, II->getIntrinsicID())) {
628 DeadInsts.insert(II);
629 return {nullptr, II->getArgOperand(/*Index=*/3)};
630 }
631
632 if (II->getIntrinsicID() == Intrinsic::dx_resource_getpointer) {
633 auto *V = dyn_cast<Instruction>(II->getArgOperand(/*Handle=*/0));
634 auto AccessIdx = getAccessIndices(V, DeadInsts);
635 assert(!AccessIdx.hasGetPtrIdx() &&
636 "Encountered multiple dx.resource.getpointers in ptr chain?");
637 AccessIdx.GetPtrIdx = II->getArgOperand(1);
638
639 DeadInsts.insert(II);
640 return AccessIdx;
641 }
642 }
643
644 if (auto *Phi = dyn_cast<PHINode>(I)) {
645 unsigned NumEdges = Phi->getNumIncomingValues();
646 assert(NumEdges != 0 && "Malformed Phi Node");
647
648 IRBuilder<> Builder(Phi);
649 PHINode *GetPtrPhi = PHINode::Create(Builder.getInt32Ty(), NumEdges);
650 PHINode *HandlePhi = PHINode::Create(Builder.getInt32Ty(), NumEdges);
651
652 bool HasGetPtr = true;
653 for (unsigned Idx = 0; Idx < NumEdges; Idx++) {
654 auto *BB = Phi->getIncomingBlock(Idx);
655 auto *V = dyn_cast<Instruction>(Phi->getIncomingValue(Idx));
656 auto AccessIdx = getAccessIndices(V, DeadInsts);
657 HasGetPtr &= AccessIdx.hasGetPtrIdx();
658 if (HasGetPtr)
659 GetPtrPhi->addIncoming(AccessIdx.GetPtrIdx, BB);
660 HandlePhi->addIncoming(AccessIdx.HandleIdx, BB);
661 }
662
663 if (HasGetPtr)
664 Builder.Insert(GetPtrPhi);
665 else
666 GetPtrPhi = nullptr;
667
668 Builder.Insert(HandlePhi);
669
670 DeadInsts.insert(Phi);
671 return {GetPtrPhi, HandlePhi};
672 }
673
674 if (auto *Select = dyn_cast<SelectInst>(I)) {
675 auto *TrueV = dyn_cast<Instruction>(Select->getTrueValue());
676 auto TrueAccessIdx = getAccessIndices(TrueV, DeadInsts);
677
678 auto *FalseV = dyn_cast<Instruction>(Select->getFalseValue());
679 auto FalseAccessIdx = getAccessIndices(FalseV, DeadInsts);
680
681 IRBuilder<> Builder(Select);
682 Value *GetPtrSelect = nullptr;
683
684 if (TrueAccessIdx.hasGetPtrIdx() && FalseAccessIdx.hasGetPtrIdx())
685 GetPtrSelect =
686 Builder.CreateSelect(Select->getCondition(), TrueAccessIdx.GetPtrIdx,
687 FalseAccessIdx.GetPtrIdx);
688
689 auto *HandleSelect =
690 Builder.CreateSelect(Select->getCondition(), TrueAccessIdx.HandleIdx,
691 FalseAccessIdx.HandleIdx);
692 DeadInsts.insert(Select);
693 return {GetPtrSelect, HandleSelect};
694 }
695
696 llvm_unreachable("collectUsedHandles should assure this does not occur");
697}
698
699static void
702 auto AccessIdx = getAccessIndices(Ptr, DeadInsts);
703 assert(AccessIdx.hasGetPtrIdx() && AccessIdx.hasHandleIdx() &&
704 "Couldn't retrieve indices. This is guaranteed by getAccessIndices");
705
706 IRBuilder<> Builder(Ptr);
707 IntrinsicInst *Handle = cast<IntrinsicInst>(OldHandle->clone());
708 Handle->setArgOperand(/*Index=*/3, AccessIdx.HandleIdx);
709 Builder.Insert(Handle);
710
711 auto *GetPtr =
712 Builder.CreateIntrinsic(Ptr->getType(), Intrinsic::dx_resource_getpointer,
713 {Handle, AccessIdx.GetPtrIdx});
714
715 Ptr->replaceAllUsesWith(GetPtr);
716 DeadInsts.insert(Ptr);
717}
718
719// Try to legalize dx.resource.handlefrom.*.binding and dx.resource.getpointer
720// calls with their respective index values and propagate the index values to
721// be used at resource access.
722//
723// If it can't be transformed to be legal then:
724//
725// Reports an error if a resource access is not guaranteed into a unique global
726// resource.
727//
728// Returns true if any changes are made.
731 for (BasicBlock &BB : make_early_inc_range(F)) {
732 for (Instruction &I : BB) {
733 if (auto *PtrOp = getStoreLoadPointerOperand(&I)) {
735 unsigned NumHandles = Handles.size();
736 if (NumHandles <= 1)
737 continue; // Legal, no-replacement required
738
739 bool SameGlobalBinding = true;
740 hlsl::Binding B = getHandleIntrinsicBinding(Handles[0], DRTM);
741 for (unsigned Idx = 1; Idx < NumHandles; Idx++)
742 SameGlobalBinding &=
743 (B == getHandleIntrinsicBinding(Handles[Idx], DRTM));
744
745 if (!SameGlobalBinding) {
747 continue;
748 }
749
750 replaceHandleWithIndices(PtrOp, Handles[0], DeadInsts);
751 }
752 }
753 }
754
755 bool MadeChanges = false;
756
757 for (auto *I : llvm::reverse(DeadInsts))
758 if (I->hasNUses(0)) { // Handle can still be used outside of replaced path
759 I->eraseFromParent();
760 MadeChanges = true;
761 }
762
763 return MadeChanges;
764}
765
767 SmallVector<User *> Worklist;
768 for (User *U : II->users())
769 Worklist.push_back(U);
770
772 while (!Worklist.empty()) {
773 User *U = Worklist.back();
774 Worklist.pop_back();
775
776 if (auto *GEP = dyn_cast<GetElementPtrInst>(U)) {
777 for (User *U : GEP->users())
778 Worklist.push_back(U);
779 DeadInsts.push_back(GEP);
780
781 } else if (auto *SI = dyn_cast<StoreInst>(U)) {
782 assert(SI->getValueOperand() != II && "Pointer escaped!");
784 DeadInsts.push_back(SI);
785
786 } else if (auto *LI = dyn_cast<LoadInst>(U)) {
787 createLoadIntrinsic(II, LI, RTI);
788 DeadInsts.push_back(LI);
789 } else
790 llvm_unreachable("Unhandled instruction - pointer escaped?");
791 }
792
793 // Traverse the now-dead instructions in RPO and remove them.
794 for (Instruction *Dead : llvm::reverse(DeadInsts))
795 Dead->eraseFromParent();
796 II->eraseFromParent();
797}
798
802 for (Instruction &I : BB)
803 if (auto *II = dyn_cast<IntrinsicInst>(&I))
804 if (II->getIntrinsicID() == Intrinsic::dx_resource_getpointer ||
805 II->getIntrinsicID() == Intrinsic::dx_resource_getbasepointer) {
806 auto *HandleTy = cast<TargetExtType>(II->getArgOperand(0)->getType());
807 assert(
808 (DRTM[HandleTy].isCBuffer() ||
809 II->getIntrinsicID() != Intrinsic::dx_resource_getbasepointer) &&
810 "dx_resource_getbasepointer should only be used by cbuffers");
811 Resources.emplace_back(II, DRTM[HandleTy]);
812 }
813
814 for (auto &[II, RI] : Resources)
815 replaceAccess(II, RI);
816
817 return !Resources.empty();
818}
819
822 auto &MAMProxy = FAM.getResult<ModuleAnalysisManagerFunctionProxy>(F);
823 DXILResourceTypeMap *DRTM =
824 MAMProxy.getCachedResult<DXILResourceTypeAnalysis>(*F.getParent());
825 assert(DRTM && "DXILResourceTypeAnalysis must be available");
826
827 bool MadeHandleChanges = legalizeResourceHandles(F, *DRTM);
828 bool MadeResourceChanges = transformResourcePointers(F, *DRTM);
829 if (!(MadeHandleChanges || MadeResourceChanges))
830 return PreservedAnalyses::all();
831
835 return PA;
836}
837
838namespace {
839class DXILResourceAccessLegacy : public FunctionPass {
840public:
841 bool runOnFunction(Function &F) override {
842 DXILResourceTypeMap &DRTM =
843 getAnalysis<DXILResourceTypeWrapperPass>().getResourceTypeMap();
844 bool MadeHandleChanges = legalizeResourceHandles(F, DRTM);
845 bool MadeResourceChanges = transformResourcePointers(F, DRTM);
846 return MadeHandleChanges || MadeResourceChanges;
847 }
848 StringRef getPassName() const override { return "DXIL Resource Access"; }
849 DXILResourceAccessLegacy() : FunctionPass(ID) {}
850
851 static char ID; // Pass identification.
852 void getAnalysisUsage(llvm::AnalysisUsage &AU) const override {
853 AU.addRequired<DXILResourceTypeWrapperPass>();
854 AU.addPreserved<DominatorTreeWrapperPass>();
855 }
856};
857char DXILResourceAccessLegacy::ID = 0;
858} // end anonymous namespace
859
860INITIALIZE_PASS_BEGIN(DXILResourceAccessLegacy, DEBUG_TYPE,
861 "DXIL Resource Access", false, false)
863INITIALIZE_PASS_END(DXILResourceAccessLegacy, DEBUG_TYPE,
864 "DXIL Resource Access", false, false)
865
867 return new DXILResourceAccessLegacy();
868}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:853
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
DXIL Remove Unused Resources
static void diagnoseNonUniqueResourceAccess(Instruction *I, ArrayRef< IntrinsicInst * > Handles)
static void createLoadIntrinsic(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static Value * emitRawLoad(IRBuilder<> &Builder, Type *Ty, Value *Buffer, Value *Index, Value *Offset, dxil::ResourceTypeInfo &RTI)
static bool legalizeResourceHandles(Function &F, DXILResourceTypeMap &DRTM)
static AccessIndices getAccessIndices(Instruction *I, SmallSetVector< Instruction *, 16 > &DeadInsts)
static void createTypedBufferLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void createTypedBufferStore(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static SmallVector< IntrinsicInst * > collectUsedHandles(Value *Ptr)
static const std::array< Intrinsic::ID, 2 > HandleIntrins
static bool transformResourcePointers(Function &F, DXILResourceTypeMap &DRTM)
static void createTextureLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void replaceHandleWithIndices(Instruction *Ptr, IntrinsicInst *OldHandle, SmallSetVector< Instruction *, 16 > &DeadInsts)
static void emitRawStore(IRBuilder<> &Builder, Value *Buffer, Value *Index, Value *Offset, Value *V, dxil::ResourceTypeInfo &RTI)
static Value * traverseGEPOffsets(const DataLayout &DL, IRBuilder<> &Builder, Value *Ptr, uint64_t AccessSize)
static hlsl::Binding getHandleIntrinsicBinding(IntrinsicInst *Handle, DXILResourceTypeMap &DRTM)
static void createStoreIntrinsic(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static void createCBufferLoad(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static void createRawStores(IntrinsicInst *II, StoreInst *SI, dxil::ResourceTypeInfo &RTI)
static void createRawLoads(IntrinsicInst *II, LoadInst *LI, dxil::ResourceTypeInfo &RTI)
static Instruction * getStoreLoadPointerOperand(Instruction *AI)
static void replaceAccess(IntrinsicInst *II, dxil::ResourceTypeInfo &RTI)
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
Hexagon Common GEP
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
FunctionAnalysisManager FAM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file implements a set that has insertion order iteration characteristics.
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1599
static LLVM_ABI void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition APInt.cpp:1793
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1563
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Analysis pass which computes a DominatorTree.
Definition Dominators.h:270
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:869
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2900
LLVM_ABI Instruction * clone() const
Create a copy of 'this' instruction that is identical in all ways except the following:
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.
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
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.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:151
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:339
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.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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.
Definition Type.cpp:479
Type * getTypeParameter(unsigned i) const
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:319
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:306
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
TargetExtType * getHandleTy() const
LLVM_ABI bool isStruct() const
dxil::ResourceKind getResourceKind() const
A raw_ostream that writes to an std::string.
#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
const unsigned CBufferRowSizeInBytes
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:558
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
@ Dead
Unused definition.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
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...
Definition STLExtras.h:633
LLVM_ABI Value * concatenateVectors(IRBuilderBase &Builder, ArrayRef< Value * > Vecs)
Concatenate a list of vectors.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
FunctionPass * createDXILResourceAccessLegacyPass()
Pass to update resource accesses to use load/store directly.
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...
Definition Casting.h:547
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1946
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.
Definition Error.cpp:177
#define N