[llvm] [DirectX] Introduce the DXILResourceAccess pass (PR #116726)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 26 15:12:21 PST 2024


================
@@ -0,0 +1,196 @@
+//===- DXILResourceAccess.cpp - Resource access via load/store ------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "DXILResourceAccess.h"
+#include "DirectX.h"
+#include "llvm/Analysis/DXILResource.h"
+#include "llvm/IR/Dominators.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/IntrinsicInst.h"
+#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/IntrinsicsDirectX.h"
+#include "llvm/InitializePasses.h"
+
+#define DEBUG_TYPE "dxil-resource-access"
+
+using namespace llvm;
+
+static void replaceTypedBufferAccess(IntrinsicInst *II,
+                                     dxil::ResourceInfo &RI) {
+  const DataLayout &DL = II->getDataLayout();
+
+  auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType());
+  assert(HandleType->getName() == "dx.TypedBuffer" &&
+         "Unexpected typed buffer type");
+  Type *ContainedType = HandleType->getTypeParameter(0);
+  Type *ScalarType = ContainedType->getScalarType();
+  uint64_t ScalarSize = DL.getTypeSizeInBits(ScalarType) / 8;
+  int NumElements = ContainedType->getNumContainedTypes();
+  if (!NumElements)
+    NumElements = 1;
+
+  // Process users keeping track of indexing accumulated from GEPs.
+  struct AccessAndIndex {
+    User *Access;
+    Value *Index;
+  };
+  SmallVector<AccessAndIndex> Worklist;
+  for (User *U : II->users())
+    Worklist.push_back({U, nullptr});
+
+  SmallVector<Instruction *> DeadInsts;
+  while (!Worklist.empty()) {
+    AccessAndIndex Current = Worklist.back();
+    Worklist.pop_back();
+
+    if (auto *GEP = dyn_cast<GetElementPtrInst>(Current.Access)) {
+      IRBuilder<> Builder(GEP);
+
+      Value *Index;
+      APInt ConstantOffset(DL.getIndexTypeSizeInBits(GEP->getType()), 0);
+      if (GEP->accumulateConstantOffset(DL, ConstantOffset)) {
----------------
bogner wrote:

`accumulateConstantOffset` will do the right thing for GEP chains, but in practice I'm not sure they can really come up anyways since the set of types that are possible in a typed buffer are very limited.

https://github.com/llvm/llvm-project/pull/116726


More information about the llvm-commits mailing list