[llvm-branch-commits] [DirectX] Implement metadata lowering for resources (PR #104447)

Justin Bogner via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Aug 16 12:26:59 PDT 2024


================
@@ -13,27 +13,52 @@
 #include "DXILShaderFlags.h"
 #include "DirectX.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/Analysis/DXILResource.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
+#include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
 #include "llvm/TargetParser/Triple.h"
 
 using namespace llvm;
 using namespace llvm::dxil;
 
-static void emitResourceMetadata(Module &M,
+static void emitResourceMetadata(Module &M, const DXILResourceMap &DRM,
                                  const dxil::Resources &MDResources) {
-  Metadata *SRVMD = nullptr, *UAVMD = nullptr, *CBufMD = nullptr,
-           *SmpMD = nullptr;
-  bool HasResources = false;
+  LLVMContext &Context = M.getContext();
+
+  SmallVector<Metadata *> SRVs, UAVs, CBufs, Smps;
+  for (auto [_, RI] : DRM) {
+    switch (RI.getResourceClass()) {
+    case dxil::ResourceClass::SRV:
+      SRVs.push_back(RI.getAsMetadata(Context));
+      break;
+    case dxil::ResourceClass::UAV:
+      UAVs.push_back(RI.getAsMetadata(Context));
+      break;
+    case dxil::ResourceClass::CBuffer:
+      CBufs.push_back(RI.getAsMetadata(Context));
+      break;
+    case dxil::ResourceClass::Sampler:
+      Smps.push_back(RI.getAsMetadata(Context));
+      break;
+    }
+  }
+  Metadata *SRVMD = SRVs.empty() ? nullptr : MDNode::get(Context, SRVs);
+  Metadata *UAVMD = UAVs.empty() ? nullptr : MDNode::get(Context, UAVs);
+  Metadata *CBufMD = CBufs.empty() ? nullptr : MDNode::get(Context, CBufs);
+  Metadata *SmpMD = Smps.empty() ? nullptr : MDNode::get(Context, Smps);
+  bool HasResources = !DRM.empty();
 
   if (MDResources.hasUAVs()) {
+    assert(!UAVMD && "Old and new UAV representations can't coexist");
----------------
bogner wrote:

Yes. Once the frontend starts generating target extension types it will no longer generate the uav metadata node and we can remove all of that code. Similarly for the cbufs.

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


More information about the llvm-branch-commits mailing list