[llvm] [HLSL] Add support to lookup a ResourceBindingInfo from its use (PR #126556)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 14 15:39:05 PST 2025


================
@@ -0,0 +1,295 @@
+//===- llvm/unittests/Target/DirectX/PointerTypeAnalysisTests.cpp ---------===//
+//
+// 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 "DirectXTargetMachine.h"
+#include "llvm/Analysis/DXILResource.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/Instructions.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Type.h"
+#include "llvm/Passes/PassBuilder.h"
+#include "llvm/Support/Casting.h"
+#include "llvm/Support/SourceMgr.h"
+
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::dxil;
+
+namespace {
+class UniqueResourceFromUseTest : public testing::Test {
+protected:
+  PassBuilder *PB;
+  ModuleAnalysisManager *MAM;
+
+  virtual void SetUp() {
+    MAM = new ModuleAnalysisManager();
+    PB = new PassBuilder();
+    PB->registerModuleAnalyses(*MAM);
+    MAM->registerPass([&] { return DXILResourceTypeAnalysis(); });
+    MAM->registerPass([&] { return DXILResourceBindingAnalysis(); });
+  }
+
+  virtual void TearDown() {
+    delete PB;
+    delete MAM;
+  }
+};
+
+TEST_F(UniqueResourceFromUseTest, TestTrivialUse) {
+  StringRef Assembly = R"(
+define void @main() {
+entry:
+  %handle = call target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_f32_1_0t(i32 1, i32 2, i32 3, i32 4, i1 false)
+  call void @a.func(target("dx.RawBuffer", float, 1, 0) %handle)
+  call void @a.func(target("dx.RawBuffer", float, 1, 0) %handle)
+  ret void
+}
+
+declare target("dx.RawBuffer", float, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_f32_1_0t(i32, i32, i32, i32, i1)
+declare void @a.func(target("dx.RawBuffer", float, 1, 0) %handle)
+  )";
+
+  LLVMContext Context;
+  SMDiagnostic Error;
+  auto M = parseAssemblyString(Assembly, Error, Context);
+  ASSERT_TRUE(M) << "Bad assembly?";
+
+  const DXILBindingMap &DBM = MAM->getResult<DXILResourceBindingAnalysis>(*M);
+  for (const Function &F : M->functions()) {
+    if (F.getName() != "a.func") {
+      continue;
+    }
+
+    unsigned CalledResources = 0;
+
+    for (const User *U : F.users()) {
+      const CallInst *CI = dyn_cast<CallInst>(U);
+      ASSERT_TRUE(CI) << "All users of @a.func must be CallInst";
----------------
bogner wrote:

You can probably just use `cast<CallInst>(U)` instead of `dyn_cast` and let the LLVM asserts in that file catch misuse here - this isn't really part of the test per se.

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


More information about the llvm-commits mailing list