[llvm] [HLSL] Analyze update counter usage (PR #130356)

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 19 17:56:30 PDT 2025


================
@@ -823,8 +827,153 @@ DXILBindingMap::findByUse(const Value *Key) const {
 
 //===----------------------------------------------------------------------===//
 
+static bool isUpdateCounterIntrinsic(Function &F) {
+  return F.getIntrinsicID() == Intrinsic::dx_resource_updatecounter;
+}
+
+void DXILResourceCounterDirectionMap::populate(Module &M, DXILBindingMap &DBM) {
+  std::vector<std::tuple<dxil::ResourceBindingInfo, ResourceCounterDirection,
+                         const Function *, const CallInst *>>
+      DiagCounterDirs;
+
+  for (Function &F : M.functions()) {
+    if (!isUpdateCounterIntrinsic(F))
+      continue;
+
+    for (const User *U : F.users()) {
+      const CallInst *CI = dyn_cast<CallInst>(U);
+      assert(CI && "Users of dx_resource_updateCounter must be call instrs");
+
+      // Determine if the use is an increment or decrement
+      Value *CountArg = CI->getArgOperand(1);
+      ConstantInt *CountValue = dyn_cast<ConstantInt>(CountArg);
+      int64_t CountLiteral = CountValue->getSExtValue();
+
+      ResourceCounterDirection Direction = ResourceCounterDirection::Unknown;
+      if (CountLiteral > 0)
+        Direction = ResourceCounterDirection::Increment;
+      if (CountLiteral < 0)
+        Direction = ResourceCounterDirection::Decrement;
+
+      // Collect all potential creation points for the handle arg
+      Value *HandleArg = CI->getArgOperand(0);
+      SmallVector<dxil::ResourceBindingInfo> RBInfos = DBM.findByUse(HandleArg);
+      for (const dxil::ResourceBindingInfo RBInfo : RBInfos)
+        DiagCounterDirs.emplace_back(RBInfo, Direction, &F, CI);
----------------
bogner wrote:

This is doing a lot of copies.

- It seems odd that `DBM.findByUse` is returning `SmallVector<dxil::ResourceBindingInfo> rather than `SmallVector<dxil::ResourceBindingInfo &>` - presumably the lifetime of the result of `findByUse` is always dominated by the lifetime of the binding analysis. Did we talk about this before or did I overlook it in the PR that added that API?
- The loop should certainly use `const dxil::ResourceBindingInfo &RBInfo`
- Arguably the value we store in `DiagCounterDirs` could also be a reference since the binding map will outlive it. This obviously depends on the first point above though.

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


More information about the llvm-commits mailing list