llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-directx

Author: Justin Bogner (bogner)

<details>
<summary>Changes</summary>

Generate metadata from target extension type based resources.

Part of #<!-- -->91366


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


3 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp (+39-8) 
- (modified) llvm/test/CodeGen/DirectX/CreateHandle.ll (+9-1) 
- (modified) llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll (+9-1) 


``````````diff
diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp 
b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index 007af0b46b9f3..f8621eea23448 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -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");
     UAVMD = MDResources.writeUAVs(M);
     HasResources = true;
   }
 
   if (MDResources.hasCBuffers()) {
+    assert(!CBufMD && "Old and new cbuffer representations can't coexist");
     CBufMD = MDResources.writeCBuffers(M);
     HasResources = true;
   }
@@ -46,7 +71,8 @@ static void emitResourceMetadata(Module &M,
       MDNode::get(M.getContext(), {SRVMD, UAVMD, CBufMD, SmpMD}));
 }
 
-static void translateMetadata(Module &M, const dxil::Resources &MDResources,
+static void translateMetadata(Module &M, const DXILResourceMap &DRM,
+                              const dxil::Resources &MDResources,
                               const ComputedShaderFlags &ShaderFlags) {
   dxil::ValidatorVersionMD ValVerMD(M);
   if (ValVerMD.isEmpty())
@@ -54,18 +80,19 @@ static void translateMetadata(Module &M, const 
dxil::Resources &MDResources,
   dxil::createShaderModelMD(M);
   dxil::createDXILVersionMD(M);
 
-  emitResourceMetadata(M, MDResources);
+  emitResourceMetadata(M, DRM, MDResources);
 
   dxil::createEntryMD(M, static_cast<uint64_t>(ShaderFlags));
 }
 
 PreservedAnalyses DXILTranslateMetadata::run(Module &M,
                                              ModuleAnalysisManager &MAM) {
+  const DXILResourceMap &DRM = MAM.getResult<DXILResourceAnalysis>(M);
   const dxil::Resources &MDResources = 
MAM.getResult<DXILResourceMDAnalysis>(M);
   const ComputedShaderFlags &ShaderFlags =
       MAM.getResult<ShaderFlagsAnalysis>(M);
 
-  translateMetadata(M, MDResources, ShaderFlags);
+  translateMetadata(M, DRM, MDResources, ShaderFlags);
 
   return PreservedAnalyses::all();
 }
@@ -80,17 +107,20 @@ class DXILTranslateMetadataLegacy : public ModulePass {
 
   void getAnalysisUsage(AnalysisUsage &AU) const override {
     AU.setPreservesAll();
+    AU.addRequired<DXILResourceWrapperPass>();
     AU.addRequired<DXILResourceMDWrapper>();
     AU.addRequired<ShaderFlagsAnalysisWrapper>();
   }
 
   bool runOnModule(Module &M) override {
+    const DXILResourceMap &DRM =
+        getAnalysis<DXILResourceWrapperPass>().getResourceMap();
     const dxil::Resources &MDResources =
         getAnalysis<DXILResourceMDWrapper>().getDXILResource();
     const ComputedShaderFlags &ShaderFlags =
         getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags();
 
-    translateMetadata(M, MDResources, ShaderFlags);
+    translateMetadata(M, DRM, MDResources, ShaderFlags);
     return true;
   }
 };
@@ -105,6 +135,7 @@ ModulePass *llvm::createDXILTranslateMetadataLegacyPass() {
 
 INITIALIZE_PASS_BEGIN(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
                       "DXIL Translate Metadata", false, false)
+INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(DXILResourceMDWrapper)
 INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
 INITIALIZE_PASS_END(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
diff --git a/llvm/test/CodeGen/DirectX/CreateHandle.ll 
b/llvm/test/CodeGen/DirectX/CreateHandle.ll
index 1fad869ab4305..f0d1c8da5a425 100644
--- a/llvm/test/CodeGen/DirectX/CreateHandle.ll
+++ b/llvm/test/CodeGen/DirectX/CreateHandle.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -dxil-op-lower %s | FileCheck %s
+; RUN: opt -S -passes=dxil-op-lower,dxil-translate-metadata %s | FileCheck %s
 
 target triple = "dxil-pc-shadermodel6.0-compute"
 
@@ -40,6 +40,14 @@ define void @test_buffers() {
   ret void
 }
 
+; Just check that we have the right types and number of metadata nodes, the
+; contents of the metadata are tested elsewhere.
+;
+; CHECK: !dx.resources = !{[[RESMD:![0-9]+]]}
+; CHECK: [[RESMD]] = !{[[SRVMD:![0-9]+]], [[UAVMD:![0-9]+]], null, null}
+; CHECK-DAG: [[SRVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}}
+; CHECK-DAG: [[UAVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
+
 ; Note: We need declarations for each handle.fromBinding in the same order as
 ; they appear in source to force a deterministic ordering of record IDs.
 declare target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
diff --git a/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll 
b/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
index e8bd8fe89132d..345459a60c5ab 100644
--- a/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
+++ b/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -dxil-op-lower %s | FileCheck %s
+; RUN: opt -S -passes=dxil-op-lower,dxil-translate-metadata %s | FileCheck %s
 
 target triple = "dxil-pc-shadermodel6.6-compute"
 
@@ -44,6 +44,14 @@ define void @test_bindings() {
   ret void
 }
 
+; Just check that we have the right types and number of metadata nodes, the
+; contents of the metadata are tested elsewhere.
+;
+; CHECK: !dx.resources = !{[[RESMD:![0-9]+]]}
+; CHECK: [[RESMD]] = !{[[SRVMD:![0-9]+]], [[UAVMD:![0-9]+]], null, null}
+; CHECK-DAG: [[SRVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}}
+; CHECK-DAG: [[UAVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
+
 ; Note: We need declarations for each handle.fromBinding in the same order as
 ; they appear in source to force a deterministic ordering of record IDs.
 declare target("dx.TypedBuffer", <4 x float>, 1, 0, 0)

``````````

</details>


https://github.com/llvm/llvm-project/pull/104447
_______________________________________________
llvm-branch-commits mailing list
llvm-branch-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits

Reply via email to