[llvm] [DXIL] Consume Metadata Analysis information in passes (PR #108034)

S. Bharadwaj Yadavalli via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 10 08:46:14 PDT 2024


https://github.com/bharadwajy updated https://github.com/llvm/llvm-project/pull/108034

>From 75fb226992648c132b0f374614806e9381732db8 Mon Sep 17 00:00:00 2001
From: Bharadwaj Yadavalli <Bharadwaj.Yadavalli at microsoft.com>
Date: Wed, 21 Aug 2024 15:35:17 -0400
Subject: [PATCH 1/2] [DXIL] Consume Metadata Analysis information in
 DXILTranslateMetadata and DXILPrepare passes.

---
 .../llvm/Analysis/DXILMetadataAnalysis.h      |   6 +-
 llvm/lib/Analysis/DXILMetadataAnalysis.cpp    |   8 +-
 llvm/lib/Target/DirectX/CMakeLists.txt        |   1 -
 .../lib/Target/DirectX/DXContainerGlobals.cpp |  10 +-
 llvm/lib/Target/DirectX/DXILMetadata.cpp      | 335 ------------------
 llvm/lib/Target/DirectX/DXILMetadata.h        |  43 ---
 llvm/lib/Target/DirectX/DXILPrepare.cpp       |  12 +-
 .../Target/DirectX/DXILTranslateMetadata.cpp  | 286 ++++++++++++++-
 .../Target/DirectX/DirectXTargetMachine.cpp   |   2 +
 .../CodeGen/DirectX/legalize-module-flags.ll  |   2 +-
 .../CodeGen/DirectX/legalize-module-flags2.ll |   2 +-
 llvm/test/CodeGen/DirectX/strip-call-attrs.ll |   2 +-
 llvm/test/CodeGen/DirectX/typed_ptr.ll        |   2 +-
 13 files changed, 299 insertions(+), 412 deletions(-)
 delete mode 100644 llvm/lib/Target/DirectX/DXILMetadata.cpp
 delete mode 100644 llvm/lib/Target/DirectX/DXILMetadata.h

diff --git a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
index ed342c28b2d78b..cb442669a24dfe 100644
--- a/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
+++ b/llvm/include/llvm/Analysis/DXILMetadataAnalysis.h
@@ -21,20 +21,20 @@ class Function;
 namespace dxil {
 
 struct EntryProperties {
-  const Function *Entry;
+  const Function *Entry{nullptr};
   // Specific target shader stage may be specified for entry functions
   Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
   unsigned NumThreadsX{0}; // X component
   unsigned NumThreadsY{0}; // Y component
   unsigned NumThreadsZ{0}; // Z component
 
-  EntryProperties(const Function &Fn) : Entry(&Fn) {};
+  EntryProperties(const Function *Fn = nullptr) : Entry(Fn) {};
 };
 
 struct ModuleMetadataInfo {
   VersionTuple DXILVersion{};
   VersionTuple ShaderModelVersion{};
-  Triple::EnvironmentType ShaderStage = Triple::UnknownEnvironment;
+  Triple::EnvironmentType ShaderProfile = Triple::UnknownEnvironment;
   VersionTuple ValidatorVersion{};
   SmallVector<EntryProperties> EntryPropertyVec{};
   void print(raw_ostream &OS) const;
diff --git a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
index cebfe4b84dcdfb..a7f666a3f8b48f 100644
--- a/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
+++ b/llvm/lib/Analysis/DXILMetadataAnalysis.cpp
@@ -27,7 +27,7 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
   Triple TT(Triple(M.getTargetTriple()));
   MMDAI.DXILVersion = TT.getDXILVersion();
   MMDAI.ShaderModelVersion = TT.getOSVersion();
-  MMDAI.ShaderStage = TT.getEnvironment();
+  MMDAI.ShaderProfile = TT.getEnvironment();
   NamedMDNode *ValidatorVerNode = M.getNamedMetadata("dx.valver");
   if (ValidatorVerNode) {
     auto *ValVerMD = cast<MDNode>(ValidatorVerNode->getOperand(0));
@@ -42,7 +42,7 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
     if (!F.hasFnAttribute("hlsl.shader"))
       continue;
 
-    EntryProperties EFP(F);
+    EntryProperties EFP(&F);
     // Get "hlsl.shader" attribute
     Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
     assert(EntryAttr.isValid() &&
@@ -74,8 +74,8 @@ static ModuleMetadataInfo collectMetadataInfo(Module &M) {
 void ModuleMetadataInfo::print(raw_ostream &OS) const {
   OS << "Shader Model Version : " << ShaderModelVersion.getAsString() << "\n";
   OS << "DXIL Version : " << DXILVersion.getAsString() << "\n";
-  OS << "Target Shader Stage : " << Triple::getEnvironmentTypeName(ShaderStage)
-     << "\n";
+  OS << "Target Shader Stage : "
+     << Triple::getEnvironmentTypeName(ShaderProfile) << "\n";
   OS << "Validator Version : " << ValidatorVersion.getAsString() << "\n";
   for (const auto &EP : EntryPropertyVec) {
     OS << " " << EP.Entry->getName() << "\n";
diff --git a/llvm/lib/Target/DirectX/CMakeLists.txt b/llvm/lib/Target/DirectX/CMakeLists.txt
index f7ae09957996b5..55d32deb49b085 100644
--- a/llvm/lib/Target/DirectX/CMakeLists.txt
+++ b/llvm/lib/Target/DirectX/CMakeLists.txt
@@ -21,7 +21,6 @@ add_llvm_target(DirectXCodeGen
   DXContainerGlobals.cpp
   DXILFinalizeLinkage.cpp
   DXILIntrinsicExpansion.cpp
-  DXILMetadata.cpp
   DXILOpBuilder.cpp
   DXILOpLowering.cpp
   DXILPrepare.cpp
diff --git a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
index aa7769899ff270..0838e2d3faeb79 100644
--- a/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
+++ b/llvm/lib/Target/DirectX/DXContainerGlobals.cpp
@@ -151,16 +151,16 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
   dxil::ModuleMetadataInfo &MMI =
       getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
   assert(MMI.EntryPropertyVec.size() == 1 ||
-         MMI.ShaderStage == Triple::Library);
+         MMI.ShaderProfile == Triple::Library);
   PSV.BaseData.ShaderStage =
-      static_cast<uint8_t>(MMI.ShaderStage - Triple::Pixel);
+      static_cast<uint8_t>(MMI.ShaderProfile - Triple::Pixel);
 
   // Hardcoded values here to unblock loading the shader into D3D.
   //
   // TODO: Lots more stuff to do here!
   //
   // See issue https://github.com/llvm/llvm-project/issues/96674.
-  switch (MMI.ShaderStage) {
+  switch (MMI.ShaderProfile) {
   case Triple::Compute:
     PSV.BaseData.NumThreadsX = MMI.EntryPropertyVec[0].NumThreadsX;
     PSV.BaseData.NumThreadsY = MMI.EntryPropertyVec[0].NumThreadsY;
@@ -170,10 +170,10 @@ void DXContainerGlobals::addPipelineStateValidationInfo(
     break;
   }
 
-  if (MMI.ShaderStage != Triple::Library)
+  if (MMI.ShaderProfile != Triple::Library)
     PSV.EntryName = MMI.EntryPropertyVec[0].Entry->getName();
 
-  PSV.finalize(MMI.ShaderStage);
+  PSV.finalize(MMI.ShaderProfile);
   PSV.write(OS);
   Constant *Constant =
       ConstantDataArray::getString(M.getContext(), Data, /*AddNull*/ false);
diff --git a/llvm/lib/Target/DirectX/DXILMetadata.cpp b/llvm/lib/Target/DirectX/DXILMetadata.cpp
deleted file mode 100644
index 1f5759c3630135..00000000000000
--- a/llvm/lib/Target/DirectX/DXILMetadata.cpp
+++ /dev/null
@@ -1,335 +0,0 @@
-//===- DXILMetadata.cpp - DXIL Metadata helper objects --------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-///
-/// \file This file contains helper objects for working with DXIL metadata.
-///
-//===----------------------------------------------------------------------===//
-
-#include "DXILMetadata.h"
-#include "llvm/IR/Constants.h"
-#include "llvm/IR/IRBuilder.h"
-#include "llvm/IR/Metadata.h"
-#include "llvm/IR/Module.h"
-#include "llvm/Support/VersionTuple.h"
-#include "llvm/TargetParser/Triple.h"
-
-using namespace llvm;
-using namespace llvm::dxil;
-
-ValidatorVersionMD::ValidatorVersionMD(Module &M)
-    : Entry(M.getOrInsertNamedMetadata("dx.valver")) {}
-
-void ValidatorVersionMD::update(VersionTuple ValidatorVer) {
-  auto &Ctx = Entry->getParent()->getContext();
-  IRBuilder<> B(Ctx);
-  Metadata *MDVals[2];
-  MDVals[0] = ConstantAsMetadata::get(B.getInt32(ValidatorVer.getMajor()));
-  MDVals[1] =
-      ConstantAsMetadata::get(B.getInt32(ValidatorVer.getMinor().value_or(0)));
-
-  if (isEmpty())
-    Entry->addOperand(MDNode::get(Ctx, MDVals));
-  else
-    Entry->setOperand(0, MDNode::get(Ctx, MDVals));
-}
-
-bool ValidatorVersionMD::isEmpty() { return Entry->getNumOperands() == 0; }
-
-VersionTuple ValidatorVersionMD::getAsVersionTuple() {
-  if (isEmpty())
-    return VersionTuple(1, 0);
-  auto *ValVerMD = cast<MDNode>(Entry->getOperand(0));
-  auto *MajorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(0));
-  auto *MinorMD = mdconst::extract<ConstantInt>(ValVerMD->getOperand(1));
-  return VersionTuple(MajorMD->getZExtValue(), MinorMD->getZExtValue());
-}
-
-static StringRef getShortShaderStage(Triple::EnvironmentType Env) {
-  switch (Env) {
-  case Triple::Pixel:
-    return "ps";
-  case Triple::Vertex:
-    return "vs";
-  case Triple::Geometry:
-    return "gs";
-  case Triple::Hull:
-    return "hs";
-  case Triple::Domain:
-    return "ds";
-  case Triple::Compute:
-    return "cs";
-  case Triple::Library:
-    return "lib";
-  case Triple::Mesh:
-    return "ms";
-  case Triple::Amplification:
-    return "as";
-  default:
-    break;
-  }
-  llvm_unreachable("Unsupported environment for DXIL generation.");
-  return "";
-}
-
-void dxil::createShaderModelMD(Module &M) {
-  NamedMDNode *Entry = M.getOrInsertNamedMetadata("dx.shaderModel");
-  Triple TT(M.getTargetTriple());
-  VersionTuple Ver = TT.getOSVersion();
-  LLVMContext &Ctx = M.getContext();
-  IRBuilder<> B(Ctx);
-
-  Metadata *Vals[3];
-  Vals[0] = MDString::get(Ctx, getShortShaderStage(TT.getEnvironment()));
-  Vals[1] = ConstantAsMetadata::get(B.getInt32(Ver.getMajor()));
-  Vals[2] = ConstantAsMetadata::get(B.getInt32(Ver.getMinor().value_or(0)));
-  Entry->addOperand(MDNode::get(Ctx, Vals));
-}
-
-void dxil::createDXILVersionMD(Module &M) {
-  Triple TT(Triple::normalize(M.getTargetTriple()));
-  VersionTuple Ver = TT.getDXILVersion();
-  LLVMContext &Ctx = M.getContext();
-  IRBuilder<> B(Ctx);
-  NamedMDNode *Entry = M.getOrInsertNamedMetadata("dx.version");
-  Metadata *Vals[2];
-  Vals[0] = ConstantAsMetadata::get(B.getInt32(Ver.getMajor()));
-  Vals[1] = ConstantAsMetadata::get(B.getInt32(Ver.getMinor().value_or(0)));
-  Entry->addOperand(MDNode::get(Ctx, Vals));
-}
-
-static uint32_t getShaderStage(Triple::EnvironmentType Env) {
-  return (uint32_t)Env - (uint32_t)llvm::Triple::Pixel;
-}
-
-namespace {
-
-struct EntryProps {
-  Triple::EnvironmentType ShaderKind;
-  // FIXME: support more shader profiles.
-  // See https://github.com/llvm/llvm-project/issues/57927.
-  struct {
-    unsigned NumThreads[3];
-  } CS;
-
-  EntryProps(Function &F, Triple::EnvironmentType ModuleShaderKind)
-      : ShaderKind(ModuleShaderKind) {
-
-    if (ShaderKind == Triple::EnvironmentType::Library) {
-      Attribute EntryAttr = F.getFnAttribute("hlsl.shader");
-      StringRef EntryProfile = EntryAttr.getValueAsString();
-      Triple T("", "", "", EntryProfile);
-      ShaderKind = T.getEnvironment();
-    }
-
-    if (ShaderKind == Triple::EnvironmentType::Compute) {
-      auto NumThreadsStr =
-          F.getFnAttribute("hlsl.numthreads").getValueAsString();
-      SmallVector<StringRef> NumThreads;
-      NumThreadsStr.split(NumThreads, ',');
-      assert(NumThreads.size() == 3 && "invalid numthreads");
-      auto Zip =
-          llvm::zip(NumThreads, MutableArrayRef<unsigned>(CS.NumThreads));
-      for (auto It : Zip) {
-        StringRef Str = std::get<0>(It);
-        APInt V;
-        [[maybe_unused]] bool Result = Str.getAsInteger(10, V);
-        assert(!Result && "Failed to parse numthreads");
-
-        unsigned &Num = std::get<1>(It);
-        Num = V.getLimitedValue();
-      }
-    }
-  }
-
-  MDTuple *emitDXILEntryProps(uint64_t RawShaderFlag, LLVMContext &Ctx,
-                              bool IsLib) {
-    std::vector<Metadata *> MDVals;
-
-    if (RawShaderFlag != 0)
-      appendShaderFlags(MDVals, RawShaderFlag, Ctx);
-
-    // Add shader kind for lib entrys.
-    if (IsLib && ShaderKind != Triple::EnvironmentType::Library)
-      appendShaderKind(MDVals, Ctx);
-
-    if (ShaderKind == Triple::EnvironmentType::Compute)
-      appendNumThreads(MDVals, Ctx);
-    // FIXME: support more props.
-    // See https://github.com/llvm/llvm-project/issues/57948.
-    return MDNode::get(Ctx, MDVals);
-  }
-
-  static MDTuple *emitEntryPropsForEmptyEntry(uint64_t RawShaderFlag,
-                                              LLVMContext &Ctx) {
-    if (RawShaderFlag == 0)
-      return nullptr;
-
-    std::vector<Metadata *> MDVals;
-
-    appendShaderFlags(MDVals, RawShaderFlag, Ctx);
-    // FIXME: support more props.
-    // See https://github.com/llvm/llvm-project/issues/57948.
-    return MDNode::get(Ctx, MDVals);
-  }
-
-private:
-  enum EntryPropsTag {
-    ShaderFlagsTag = 0,
-    GSStateTag,
-    DSStateTag,
-    HSStateTag,
-    NumThreadsTag,
-    AutoBindingSpaceTag,
-    RayPayloadSizeTag,
-    RayAttribSizeTag,
-    ShaderKindTag,
-    MSStateTag,
-    ASStateTag,
-    WaveSizeTag,
-    EntryRootSigTag,
-  };
-
-  void appendNumThreads(std::vector<Metadata *> &MDVals, LLVMContext &Ctx) {
-    MDVals.emplace_back(ConstantAsMetadata::get(
-        ConstantInt::get(Type::getInt32Ty(Ctx), NumThreadsTag)));
-
-    std::vector<Metadata *> NumThreadVals;
-    for (auto Num : ArrayRef<unsigned>(CS.NumThreads))
-      NumThreadVals.emplace_back(ConstantAsMetadata::get(
-          ConstantInt::get(Type::getInt32Ty(Ctx), Num)));
-    MDVals.emplace_back(MDNode::get(Ctx, NumThreadVals));
-  }
-
-  static void appendShaderFlags(std::vector<Metadata *> &MDVals,
-                                uint64_t RawShaderFlag, LLVMContext &Ctx) {
-    MDVals.emplace_back(ConstantAsMetadata::get(
-        ConstantInt::get(Type::getInt32Ty(Ctx), ShaderFlagsTag)));
-    MDVals.emplace_back(ConstantAsMetadata::get(
-        ConstantInt::get(Type::getInt64Ty(Ctx), RawShaderFlag)));
-  }
-
-  void appendShaderKind(std::vector<Metadata *> &MDVals, LLVMContext &Ctx) {
-    MDVals.emplace_back(ConstantAsMetadata::get(
-        ConstantInt::get(Type::getInt32Ty(Ctx), ShaderKindTag)));
-    MDVals.emplace_back(ConstantAsMetadata::get(
-        ConstantInt::get(Type::getInt32Ty(Ctx), getShaderStage(ShaderKind))));
-  }
-};
-
-class EntryMD {
-  Function &F;
-  LLVMContext &Ctx;
-  EntryProps Props;
-
-public:
-  EntryMD(Function &F, Triple::EnvironmentType ModuleShaderKind)
-      : F(F), Ctx(F.getContext()), Props(F, ModuleShaderKind) {}
-
-  MDTuple *emitEntryTuple(MDTuple *Resources, uint64_t RawShaderFlag) {
-    // FIXME: add signature for profile other than CS.
-    // See https://github.com/llvm/llvm-project/issues/57928.
-    MDTuple *Signatures = nullptr;
-    return emitDXILEntryPointTuple(
-        &F, F.getName().str(), Signatures, Resources,
-        Props.emitDXILEntryProps(RawShaderFlag, Ctx, /*IsLib*/ false), Ctx);
-  }
-
-  MDTuple *emitEntryTupleForLib(uint64_t RawShaderFlag) {
-    // FIXME: add signature for profile other than CS.
-    // See https://github.com/llvm/llvm-project/issues/57928.
-    MDTuple *Signatures = nullptr;
-    return emitDXILEntryPointTuple(
-        &F, F.getName().str(), Signatures,
-        /*entry in lib doesn't need resources metadata*/ nullptr,
-        Props.emitDXILEntryProps(RawShaderFlag, Ctx, /*IsLib*/ true), Ctx);
-  }
-
-  // Library will have empty entry metadata which only store the resource table
-  // metadata.
-  static MDTuple *emitEmptyEntryForLib(MDTuple *Resources,
-                                       uint64_t RawShaderFlag,
-                                       LLVMContext &Ctx) {
-    return emitDXILEntryPointTuple(
-        nullptr, "", nullptr, Resources,
-        EntryProps::emitEntryPropsForEmptyEntry(RawShaderFlag, Ctx), Ctx);
-  }
-
-private:
-  static MDTuple *emitDXILEntryPointTuple(Function *Fn, const std::string &Name,
-                                          MDTuple *Signatures,
-                                          MDTuple *Resources,
-                                          MDTuple *Properties,
-                                          LLVMContext &Ctx) {
-    Metadata *MDVals[5];
-    MDVals[0] = Fn ? ValueAsMetadata::get(Fn) : nullptr;
-    MDVals[1] = MDString::get(Ctx, Name.c_str());
-    MDVals[2] = Signatures;
-    MDVals[3] = Resources;
-    MDVals[4] = Properties;
-    return MDNode::get(Ctx, MDVals);
-  }
-};
-} // namespace
-
-void dxil::createEntryMD(Module &M, const uint64_t ShaderFlags) {
-  SmallVector<Function *> EntryList;
-  for (auto &F : M.functions()) {
-    if (!F.hasFnAttribute("hlsl.shader"))
-      continue;
-    EntryList.emplace_back(&F);
-  }
-
-  // If there are no entries, do nothing. This is mostly to allow for writing
-  // tests with no actual entry functions.
-  if (EntryList.empty())
-    return;
-
-  auto &Ctx = M.getContext();
-  // FIXME: generate metadata for resource.
-  // See https://github.com/llvm/llvm-project/issues/57926.
-  MDTuple *MDResources = nullptr;
-  if (auto *NamedResources = M.getNamedMetadata("dx.resources"))
-    MDResources = dyn_cast<MDTuple>(NamedResources->getOperand(0));
-
-  std::vector<MDNode *> Entries;
-  Triple T = Triple(M.getTargetTriple());
-  switch (T.getEnvironment()) {
-  case Triple::EnvironmentType::Library: {
-    // Add empty entry to put resource metadata.
-    MDTuple *EmptyEntry =
-        EntryMD::emitEmptyEntryForLib(MDResources, ShaderFlags, Ctx);
-    Entries.emplace_back(EmptyEntry);
-
-    for (Function *Entry : EntryList) {
-      EntryMD MD(*Entry, T.getEnvironment());
-      Entries.emplace_back(MD.emitEntryTupleForLib(0));
-    }
-  } break;
-  case Triple::EnvironmentType::Compute:
-  case Triple::EnvironmentType::Amplification:
-  case Triple::EnvironmentType::Mesh:
-  case Triple::EnvironmentType::Vertex:
-  case Triple::EnvironmentType::Hull:
-  case Triple::EnvironmentType::Domain:
-  case Triple::EnvironmentType::Geometry:
-  case Triple::EnvironmentType::Pixel: {
-    assert(EntryList.size() == 1 &&
-           "non-lib profiles should only have one entry");
-    EntryMD MD(*EntryList.front(), T.getEnvironment());
-    Entries.emplace_back(MD.emitEntryTuple(MDResources, ShaderFlags));
-  } break;
-  default:
-    assert(0 && "invalid profile");
-    break;
-  }
-
-  NamedMDNode *EntryPointsNamedMD =
-      M.getOrInsertNamedMetadata("dx.entryPoints");
-  for (auto *Entry : Entries)
-    EntryPointsNamedMD->addOperand(Entry);
-}
diff --git a/llvm/lib/Target/DirectX/DXILMetadata.h b/llvm/lib/Target/DirectX/DXILMetadata.h
deleted file mode 100644
index e05db8d5370dbe..00000000000000
--- a/llvm/lib/Target/DirectX/DXILMetadata.h
+++ /dev/null
@@ -1,43 +0,0 @@
-//===- DXILMetadata.h - DXIL Metadata helper objects ----------------------===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-///
-/// \file This file contains helper objects for working with DXIL metadata.
-///
-//===----------------------------------------------------------------------===//
-
-#ifndef LLVM_TARGET_DIRECTX_DXILMETADATA_H
-#define LLVM_TARGET_DIRECTX_DXILMETADATA_H
-
-#include <stdint.h>
-
-namespace llvm {
-class Module;
-class NamedMDNode;
-class VersionTuple;
-namespace dxil {
-
-class ValidatorVersionMD {
-  NamedMDNode *Entry;
-
-public:
-  ValidatorVersionMD(Module &M);
-
-  void update(VersionTuple ValidatorVer);
-
-  bool isEmpty();
-  VersionTuple getAsVersionTuple();
-};
-
-void createShaderModelMD(Module &M);
-void createDXILVersionMD(Module &M);
-void createEntryMD(Module &M, const uint64_t ShaderFlags);
-
-} // namespace dxil
-} // namespace llvm
-
-#endif // LLVM_TARGET_DIRECTX_DXILMETADATA_H
diff --git a/llvm/lib/Target/DirectX/DXILPrepare.cpp b/llvm/lib/Target/DirectX/DXILPrepare.cpp
index f6b7355b936255..eee3aa6ca693a9 100644
--- a/llvm/lib/Target/DirectX/DXILPrepare.cpp
+++ b/llvm/lib/Target/DirectX/DXILPrepare.cpp
@@ -11,7 +11,6 @@
 /// Language (DXIL).
 //===----------------------------------------------------------------------===//
 
-#include "DXILMetadata.h"
 #include "DXILResourceAnalysis.h"
 #include "DXILShaderFlags.h"
 #include "DirectX.h"
@@ -173,8 +172,9 @@ class DXILPrepareModule : public ModulePass {
         AttrMask.addAttribute(I);
     }
 
-    dxil::ValidatorVersionMD ValVerMD(M);
-    VersionTuple ValVer = ValVerMD.getAsVersionTuple();
+    const dxil::ModuleMetadataInfo MetadataInfo =
+        getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
+    VersionTuple ValVer = MetadataInfo.ValidatorVersion;
     bool SkipValidation = ValVer.getMajor() == 0 && ValVer.getMinor() == 0;
 
     for (auto &F : M.functions()) {
@@ -246,9 +246,8 @@ class DXILPrepareModule : public ModulePass {
 
   DXILPrepareModule() : ModulePass(ID) {}
   void getAnalysisUsage(AnalysisUsage &AU) const override {
-    AU.addPreserved<ShaderFlagsAnalysisWrapper>();
-    AU.addPreserved<DXILResourceMDWrapper>();
-    AU.addPreserved<DXILMetadataAnalysisWrapperPass>();
+    AU.setPreservesAll();
+    AU.addRequired<DXILMetadataAnalysisWrapperPass>();
   }
   static char ID; // Pass identification.
 };
@@ -258,6 +257,7 @@ char DXILPrepareModule::ID = 0;
 
 INITIALIZE_PASS_BEGIN(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module",
                       false, false)
+INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
 INITIALIZE_PASS_END(DXILPrepareModule, DEBUG_TYPE, "DXIL Prepare Module", false,
                     false)
 
diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index 11cd9df1d1dc42..3c7a9168a59257 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -7,20 +7,24 @@
 //===----------------------------------------------------------------------===//
 
 #include "DXILTranslateMetadata.h"
-#include "DXILMetadata.h"
 #include "DXILResource.h"
 #include "DXILResourceAnalysis.h"
 #include "DXILShaderFlags.h"
 #include "DirectX.h"
-#include "llvm/ADT/StringSet.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
 #include "llvm/Analysis/DXILResource.h"
 #include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/Module.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Pass.h"
+#include "llvm/Support/VersionTuple.h"
 #include "llvm/TargetParser/Triple.h"
+#include <cstdint>
 
 using namespace llvm;
 using namespace llvm::dxil;
@@ -65,18 +69,273 @@ static void emitResourceMetadata(Module &M, const DXILResourceMap &DRM,
       MDNode::get(M.getContext(), {SRVMD, UAVMD, CBufMD, SmpMD}));
 }
 
+static StringRef getShortShaderStage(Triple::EnvironmentType Env) {
+  switch (Env) {
+  case Triple::Pixel:
+    return "ps";
+  case Triple::Vertex:
+    return "vs";
+  case Triple::Geometry:
+    return "gs";
+  case Triple::Hull:
+    return "hs";
+  case Triple::Domain:
+    return "ds";
+  case Triple::Compute:
+    return "cs";
+  case Triple::Library:
+    return "lib";
+  case Triple::Mesh:
+    return "ms";
+  case Triple::Amplification:
+    return "as";
+  default:
+    break;
+  }
+  llvm_unreachable("Unsupported environment for DXIL generation.");
+  return "";
+}
+
+static uint32_t getShaderStage(Triple::EnvironmentType Env) {
+  return (uint32_t)Env - (uint32_t)llvm::Triple::Pixel;
+}
+
+struct ShaderEntryMDInfo : EntryProperties {
+
+  enum EntryPropsTag {
+    ShaderFlagsTag = 0,
+    GSStateTag,
+    DSStateTag,
+    HSStateTag,
+    NumThreadsTag,
+    AutoBindingSpaceTag,
+    RayPayloadSizeTag,
+    RayAttribSizeTag,
+    ShaderKindTag,
+    MSStateTag,
+    ASStateTag,
+    WaveSizeTag,
+    EntryRootSigTag,
+  };
+
+  ShaderEntryMDInfo(EntryProperties &EP, LLVMContext &C,
+                    Triple::EnvironmentType SP, MDTuple *MDR = nullptr,
+                    uint64_t ShaderFlags = 0)
+      : EntryProperties(EP), Ctx(C), EntryShaderFlags(ShaderFlags),
+        MDResources(MDR), ShaderProfile(SP) {};
+
+  MDTuple *getAsMetadata() {
+    MDTuple *Properties = constructEntryPropMetadata();
+    // FIXME: Add support to construct Signatures
+    // See https://github.com/llvm/llvm-project/issues/57928
+    MDTuple *Signatures = nullptr;
+    return constructEntryMetadata(Signatures, MDResources, Properties);
+  }
+
+private:
+  LLVMContext &Ctx;
+  // Shader Flags for the Entry - from ShadeFLagsAnalysis pass
+  uint64_t EntryShaderFlags{0};
+  MDTuple *MDResources{nullptr};
+  Triple::EnvironmentType ShaderProfile{
+      Triple::EnvironmentType::UnknownEnvironment};
+  // Each entry point metadata record specifies:
+  //  * reference to the entry point function global symbol
+  //  * unmangled name
+  //  * list of signatures
+  //  * list of resources
+  //  * list of tag-value pairs of shader capabilities and other properties
+
+  MDTuple *constructEntryMetadata(MDTuple *Signatures, MDTuple *Resources,
+                                  MDTuple *Properties) {
+    Metadata *MDVals[5];
+    MDVals[0] =
+        Entry ? ValueAsMetadata::get(const_cast<Function *>(Entry)) : nullptr;
+    MDVals[1] = MDString::get(Ctx, Entry ? Entry->getName() : "");
+    MDVals[2] = Signatures;
+    MDVals[3] = Resources;
+    MDVals[4] = Properties;
+    return MDNode::get(Ctx, MDVals);
+  }
+
+  SmallVector<Metadata *> getTagValueAsMetadata(EntryPropsTag Tag,
+                                                uint64_t Value) {
+    SmallVector<Metadata *> MDVals;
+    MDVals.emplace_back(
+        ConstantAsMetadata::get(ConstantInt::get(Type::getInt32Ty(Ctx), Tag)));
+    switch (Tag) {
+    case ShaderFlagsTag:
+      MDVals.emplace_back(ConstantAsMetadata::get(
+          ConstantInt::get(Type::getInt64Ty(Ctx), Value)));
+      break;
+    case ShaderKindTag:
+      MDVals.emplace_back(ConstantAsMetadata::get(
+          ConstantInt::get(Type::getInt32Ty(Ctx), Value)));
+      break;
+    default:
+      assert(false && "NYI: Unhandled entry property tag");
+    }
+    return MDVals;
+  }
+
+  MDTuple *constructEntryPropMetadata() {
+    SmallVector<Metadata *> MDVals;
+    if (EntryShaderFlags != 0)
+      MDVals.append(getTagValueAsMetadata(ShaderFlagsTag, EntryShaderFlags));
+
+    if (Entry != nullptr) {
+      // FIXME: support more props.
+      // See https://github.com/llvm/llvm-project/issues/57948.
+      // Add shader kind for lib entries.
+      if (ShaderProfile == Triple::EnvironmentType::Library &&
+          ShaderStage != Triple::EnvironmentType::Library)
+        MDVals.append(
+            getTagValueAsMetadata(ShaderKindTag, getShaderStage(ShaderStage)));
+
+      if (ShaderStage == Triple::EnvironmentType::Compute) {
+        MDVals.emplace_back(ConstantAsMetadata::get(
+            ConstantInt::get(Type::getInt32Ty(Ctx), NumThreadsTag)));
+        std::vector<Metadata *> NumThreadVals;
+        NumThreadVals.emplace_back(ConstantAsMetadata::get(
+            ConstantInt::get(Type::getInt32Ty(Ctx), NumThreadsX)));
+        NumThreadVals.emplace_back(ConstantAsMetadata::get(
+            ConstantInt::get(Type::getInt32Ty(Ctx), NumThreadsY)));
+        NumThreadVals.emplace_back(ConstantAsMetadata::get(
+            ConstantInt::get(Type::getInt32Ty(Ctx), NumThreadsZ)));
+        MDVals.emplace_back(MDNode::get(Ctx, NumThreadVals));
+      }
+    }
+    if (MDVals.empty())
+      return nullptr;
+    return MDNode::get(Ctx, MDVals);
+  }
+};
+
+static void createEntryMD(Module &M, const uint64_t ShaderFlags,
+                          const dxil::ModuleMetadataInfo &MDAnalysisInfo) {
+  auto &Ctx = M.getContext();
+  // FIXME: generate metadata for resource.
+  MDTuple *MDResources = nullptr;
+  if (auto *NamedResources = M.getNamedMetadata("dx.resources"))
+    MDResources = dyn_cast<MDTuple>(NamedResources->getOperand(0));
+
+  std::vector<MDNode *> EntryFnMDNodes;
+  switch (MDAnalysisInfo.ShaderProfile) {
+  case Triple::EnvironmentType::Library: {
+    // Library has an entry metadata with resource table metadata and all other
+    // MDNodes as null.
+    EntryProperties EP{};
+    // FIXME: ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags
+    // for each entry function. Currently, ShaderFlags value provided by
+    // ShaderFlagsAnalysis pass is created by walking *all* the function
+    // instructions of the module. Is it is correct to use this value for
+    // metadata of the empty library entry?
+    ShaderEntryMDInfo EmptyFunEntryProps(EP, Ctx, MDAnalysisInfo.ShaderProfile,
+                                         MDResources, ShaderFlags);
+    MDTuple *EmptyMDT = EmptyFunEntryProps.getAsMetadata();
+    EntryFnMDNodes.emplace_back(EmptyMDT);
+
+    for (auto EntryProp : MDAnalysisInfo.EntryPropertyVec) {
+      // FIXME: ShaderFlagsAnalysis pass needs to collect and provide
+      // ShaderFlags for each entry function. For now, assume shader flags value
+      // of entry functions being compiled for lib_* shader profile viz.,
+      // EntryPro.Entry is 0.
+      ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
+                            nullptr, 0);
+      MDTuple *EmptyMDT = SEP.getAsMetadata();
+      EntryFnMDNodes.emplace_back(EmptyMDT);
+    }
+  } break;
+  case Triple::EnvironmentType::Compute: {
+    size_t NumEntries = MDAnalysisInfo.EntryPropertyVec.size();
+    if (NumEntries > 0) {
+      assert(NumEntries == 1 &&
+             "Compute shader: One and only one entry expected");
+      EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
+      // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
+      // each entry function. Currently, even though the ShaderFlags value
+      // provided by ShaderFlagsAnalysis pass is created by walking all the
+      // function instructions of the module, it is sufficient to since there is
+      // only one entry function in the module.
+      ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
+                            MDResources, ShaderFlags);
+      MDTuple *EmptyMDT = SEP.getAsMetadata();
+      EntryFnMDNodes.emplace_back(EmptyMDT);
+    }
+    break;
+  }
+  case Triple::EnvironmentType::Amplification:
+  case Triple::EnvironmentType::Mesh:
+  case Triple::EnvironmentType::Vertex:
+  case Triple::EnvironmentType::Hull:
+  case Triple::EnvironmentType::Domain:
+  case Triple::EnvironmentType::Geometry:
+  case Triple::EnvironmentType::Pixel: {
+    size_t NumEntries = MDAnalysisInfo.EntryPropertyVec.size();
+    if (NumEntries > 0) {
+      assert(NumEntries == 1 && "non-lib profiles should only have one entry");
+      EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
+      // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
+      // each entry function. Currently, even though the ShaderFlags value
+      // provided by ShaderFlagsAnalysis pass is created by walking all the
+      // function instructions of the module, it is sufficient to since there is
+      // only one entry function in the module.
+      ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
+                            MDResources, ShaderFlags);
+      MDTuple *EmptyMDT = SEP.getAsMetadata();
+      EntryFnMDNodes.emplace_back(EmptyMDT);
+    }
+  } break;
+  default:
+    assert(0 && "invalid profile");
+    break;
+  }
+
+  NamedMDNode *EntryPointsNamedMD =
+      M.getOrInsertNamedMetadata("dx.entryPoints");
+  for (auto *Entry : EntryFnMDNodes)
+    EntryPointsNamedMD->addOperand(Entry);
+}
+
 static void translateMetadata(Module &M, const DXILResourceMap &DRM,
                               const dxil::Resources &MDResources,
-                              const ComputedShaderFlags &ShaderFlags) {
-  dxil::ValidatorVersionMD ValVerMD(M);
-  if (ValVerMD.isEmpty())
-    ValVerMD.update(VersionTuple(1, 0));
-  dxil::createShaderModelMD(M);
-  dxil::createDXILVersionMD(M);
+                              const ComputedShaderFlags &ShaderFlags,
+                              const dxil::ModuleMetadataInfo &MDAnalysisInfo) {
+  LLVMContext &Ctx = M.getContext();
+  IRBuilder<> IRB(Ctx);
+  if (MDAnalysisInfo.ValidatorVersion.empty()) {
+    // Module has no metadata node signifying valid validator version.
+    // Create metadata dx.valver node with version value of 1.0
+    const VersionTuple DefaultValidatorVer{1, 0};
+    Metadata *MDVals[2];
+    MDVals[0] =
+        ConstantAsMetadata::get(IRB.getInt32(DefaultValidatorVer.getMajor()));
+    MDVals[1] = ConstantAsMetadata::get(
+        IRB.getInt32(DefaultValidatorVer.getMinor().value_or(0)));
+    NamedMDNode *ValVerNode = M.getOrInsertNamedMetadata("dx.valver");
+    ValVerNode->addOperand(MDNode::get(Ctx, MDVals));
+  }
+
+  Metadata *SMVals[3];
+  VersionTuple SM = MDAnalysisInfo.ShaderModelVersion;
+  SMVals[0] =
+      MDString::get(Ctx, getShortShaderStage(MDAnalysisInfo.ShaderProfile));
+  SMVals[1] = ConstantAsMetadata::get(IRB.getInt32(SM.getMajor()));
+  SMVals[2] = ConstantAsMetadata::get(IRB.getInt32(SM.getMinor().value_or(0)));
+  NamedMDNode *SMMDNode = M.getOrInsertNamedMetadata("dx.shaderModel");
+  SMMDNode->addOperand(MDNode::get(Ctx, SMVals));
+
+  VersionTuple DXILVer = MDAnalysisInfo.DXILVersion;
+  Metadata *DXILVals[2];
+  DXILVals[0] = ConstantAsMetadata::get(IRB.getInt32(DXILVer.getMajor()));
+  DXILVals[1] =
+      ConstantAsMetadata::get(IRB.getInt32(DXILVer.getMinor().value_or(0)));
+  NamedMDNode *DXILVerMDNode = M.getOrInsertNamedMetadata("dx.version");
+  DXILVerMDNode->addOperand(MDNode::get(Ctx, DXILVals));
 
   emitResourceMetadata(M, DRM, MDResources);
 
-  dxil::createEntryMD(M, static_cast<uint64_t>(ShaderFlags));
+  createEntryMD(M, static_cast<uint64_t>(ShaderFlags), MDAnalysisInfo);
 }
 
 PreservedAnalyses DXILTranslateMetadata::run(Module &M,
@@ -85,8 +344,10 @@ PreservedAnalyses DXILTranslateMetadata::run(Module &M,
   const dxil::Resources &MDResources = MAM.getResult<DXILResourceMDAnalysis>(M);
   const ComputedShaderFlags &ShaderFlags =
       MAM.getResult<ShaderFlagsAnalysis>(M);
+  const dxil::ModuleMetadataInfo MetadataInfo =
+      MAM.getResult<DXILMetadataAnalysis>(M);
 
-  translateMetadata(M, DRM, MDResources, ShaderFlags);
+  translateMetadata(M, DRM, MDResources, ShaderFlags, MetadataInfo);
 
   return PreservedAnalyses::all();
 }
@@ -114,8 +375,10 @@ class DXILTranslateMetadataLegacy : public ModulePass {
         getAnalysis<DXILResourceMDWrapper>().getDXILResource();
     const ComputedShaderFlags &ShaderFlags =
         getAnalysis<ShaderFlagsAnalysisWrapper>().getShaderFlags();
+    dxil::ModuleMetadataInfo MetadataInfo =
+        getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
 
-    translateMetadata(M, DRM, MDResources, ShaderFlags);
+    translateMetadata(M, DRM, MDResources, ShaderFlags, MetadataInfo);
     return true;
   }
 };
@@ -133,5 +396,6 @@ INITIALIZE_PASS_BEGIN(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
 INITIALIZE_PASS_DEPENDENCY(DXILResourceWrapperPass)
 INITIALIZE_PASS_DEPENDENCY(DXILResourceMDWrapper)
 INITIALIZE_PASS_DEPENDENCY(ShaderFlagsAnalysisWrapper)
+INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
 INITIALIZE_PASS_END(DXILTranslateMetadataLegacy, "dxil-translate-metadata",
                     "DXIL Translate Metadata", false, false)
diff --git a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
index a29fc210421637..927b67adee7d40 100644
--- a/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
+++ b/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp
@@ -28,6 +28,7 @@
 #include "llvm/CodeGen/TargetPassConfig.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/InitializePasses.h"
 #include "llvm/MC/MCSectionDXContainer.h"
 #include "llvm/MC/SectionKind.h"
 #include "llvm/MC/TargetRegistry.h"
@@ -50,6 +51,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeDirectXTarget() {
   initializeDXContainerGlobalsPass(*PR);
   initializeDXILOpLoweringLegacyPass(*PR);
   initializeDXILTranslateMetadataLegacyPass(*PR);
+  initializeDXILMetadataAnalysisWrapperPassPass(*PR);
   initializeDXILResourceMDWrapperPass(*PR);
   initializeShaderFlagsAnalysisWrapperPass(*PR);
   initializeDXILFinalizeLinkageLegacyPass(*PR);
diff --git a/llvm/test/CodeGen/DirectX/legalize-module-flags.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
index 1483a87e0b4bd3..6c29deabc2aa3b 100644
--- a/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+; RUN: opt -S -dxil-prepare -mtriple=dxil-unknown-shadermodel6.0-compute %s | FileCheck %s
 
 ; Make sure behavior flag > 6 is fixed.
 ; CHECK: !{i32 2, !"frame-pointer", i32 2}
diff --git a/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
index e1803b4672684f..244ec8d54e131e 100644
--- a/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
+++ b/llvm/test/CodeGen/DirectX/legalize-module-flags2.ll
@@ -1,4 +1,4 @@
-; RUN: opt -S -dxil-prepare < %s | FileCheck %s
+; RUN: opt -S -dxil-prepare -mtriple=dxil-unknown-shadermodel6.0-library %s | FileCheck %s
 
 ; CHECK: define void @main()
 ; Make sure behavior flag > 6 is fixed.
diff --git a/llvm/test/CodeGen/DirectX/strip-call-attrs.ll b/llvm/test/CodeGen/DirectX/strip-call-attrs.ll
index f530e12fa7e580..e232ab24d69f34 100644
--- a/llvm/test/CodeGen/DirectX/strip-call-attrs.ll
+++ b/llvm/test/CodeGen/DirectX/strip-call-attrs.ll
@@ -1,6 +1,6 @@
 
 ; RUN: opt -S -dxil-prepare < %s | FileCheck %s
-target triple = "dxil-unknown-unknown"
+target triple = "dxil-unknown-shadermodel6.0-library"
 
 @f = internal unnamed_addr global float 0.000000e+00, align 4
 @llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @_GLOBAL__sub_I_static_global.hlsl, ptr null }]
diff --git a/llvm/test/CodeGen/DirectX/typed_ptr.ll b/llvm/test/CodeGen/DirectX/typed_ptr.ll
index 5453e87651dd72..355c4f13d0e4c6 100644
--- a/llvm/test/CodeGen/DirectX/typed_ptr.ll
+++ b/llvm/test/CodeGen/DirectX/typed_ptr.ll
@@ -1,6 +1,6 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 3
 ; RUN: opt -S -dxil-prepare < %s | FileCheck %s
-target triple = "dxil-unknown-unknown"
+target triple = "dxil-unknown-shadermodel6.0-compute"
 
 @gs = external addrspace(3) global [20 x [6 x float]], align 4
 

>From 5f2457f75c2531c6eecfadf0b7012b00a7dbd335 Mon Sep 17 00:00:00 2001
From: "S. Bharadwaj Yadavalli" <Bharadwaj.Yadavalli at microsoft.com>
Date: Tue, 10 Sep 2024 11:21:16 -0400
Subject: [PATCH 2/2] Fix mis-named variable that was not edited appropriately
 after a copy-paste.

Add necessary function attributes to tests and remove
unnecessary check for mumber of entries for non-library
shaders in createEntryMD()
---
 .../Target/DirectX/DXILTranslateMetadata.cpp  | 54 +++++++++----------
 llvm/test/CodeGen/DirectX/CreateHandle.ll     |  4 +-
 .../DirectX/CreateHandleFromBinding.ll        |  4 +-
 3 files changed, 29 insertions(+), 33 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
index 3c7a9168a59257..95cc37a9190f3a 100644
--- a/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
+++ b/llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp
@@ -242,26 +242,24 @@ static void createEntryMD(Module &M, const uint64_t ShaderFlags,
       // EntryPro.Entry is 0.
       ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
                             nullptr, 0);
-      MDTuple *EmptyMDT = SEP.getAsMetadata();
-      EntryFnMDNodes.emplace_back(EmptyMDT);
+      MDTuple *MDT = SEP.getAsMetadata();
+      EntryFnMDNodes.emplace_back(MDT);
     }
   } break;
   case Triple::EnvironmentType::Compute: {
     size_t NumEntries = MDAnalysisInfo.EntryPropertyVec.size();
-    if (NumEntries > 0) {
-      assert(NumEntries == 1 &&
-             "Compute shader: One and only one entry expected");
-      EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
-      // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
-      // each entry function. Currently, even though the ShaderFlags value
-      // provided by ShaderFlagsAnalysis pass is created by walking all the
-      // function instructions of the module, it is sufficient to since there is
-      // only one entry function in the module.
-      ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
-                            MDResources, ShaderFlags);
-      MDTuple *EmptyMDT = SEP.getAsMetadata();
-      EntryFnMDNodes.emplace_back(EmptyMDT);
-    }
+    assert(NumEntries == 1 &&
+           "Compute shader: One and only one entry expected");
+    EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
+    // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
+    // each entry function. Currently, even though the ShaderFlags value
+    // provided by ShaderFlagsAnalysis pass is created by walking all the
+    // function instructions of the module, it is sufficient to since there is
+    // only one entry function in the module.
+    ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
+                          MDResources, ShaderFlags);
+    MDTuple *MDT = SEP.getAsMetadata();
+    EntryFnMDNodes.emplace_back(MDT);
     break;
   }
   case Triple::EnvironmentType::Amplification:
@@ -272,19 +270,17 @@ static void createEntryMD(Module &M, const uint64_t ShaderFlags,
   case Triple::EnvironmentType::Geometry:
   case Triple::EnvironmentType::Pixel: {
     size_t NumEntries = MDAnalysisInfo.EntryPropertyVec.size();
-    if (NumEntries > 0) {
-      assert(NumEntries == 1 && "non-lib profiles should only have one entry");
-      EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
-      // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
-      // each entry function. Currently, even though the ShaderFlags value
-      // provided by ShaderFlagsAnalysis pass is created by walking all the
-      // function instructions of the module, it is sufficient to since there is
-      // only one entry function in the module.
-      ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
-                            MDResources, ShaderFlags);
-      MDTuple *EmptyMDT = SEP.getAsMetadata();
-      EntryFnMDNodes.emplace_back(EmptyMDT);
-    }
+    assert(NumEntries == 1 && "non-lib profiles should only have one entry");
+    EntryProperties EntryProp = MDAnalysisInfo.EntryPropertyVec[0];
+    // ShaderFlagsAnalysis pass needs to collect and provide ShaderFlags for
+    // each entry function. Currently, even though the ShaderFlags value
+    // provided by ShaderFlagsAnalysis pass is created by walking all the
+    // function instructions of the module, it is sufficient to since there is
+    // only one entry function in the module.
+    ShaderEntryMDInfo SEP(EntryProp, Ctx, MDAnalysisInfo.ShaderProfile,
+                          MDResources, ShaderFlags);
+    MDTuple *MDT = SEP.getAsMetadata();
+    EntryFnMDNodes.emplace_back(MDT);
   } break;
   default:
     assert(0 && "invalid profile");
diff --git a/llvm/test/CodeGen/DirectX/CreateHandle.ll b/llvm/test/CodeGen/DirectX/CreateHandle.ll
index 40b3b2c7122722..4653baf8a3b21a 100644
--- a/llvm/test/CodeGen/DirectX/CreateHandle.ll
+++ b/llvm/test/CodeGen/DirectX/CreateHandle.ll
@@ -14,7 +14,7 @@ target triple = "dxil-pc-shadermodel6.0-compute"
 
 declare i32 @some_val();
 
-define void @test_buffers() {
+define void @test_buffers() #0 {
   ; RWBuffer<float4> Buf : register(u5, space3)
   %typed0 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
               @llvm.dx.handle.fromBinding.tdx.TypedBuffer_v4f32_1_0_0(
@@ -68,4 +68,4 @@ define void @test_buffers() {
 ; CHECK-DAG: [[SRVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}}
 ; CHECK-DAG: [[UAVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
 
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute"}
diff --git a/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll b/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
index d0c80c018b8d7e..869eeafec63222 100644
--- a/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
+++ b/llvm/test/CodeGen/DirectX/CreateHandleFromBinding.ll
@@ -14,7 +14,7 @@ target triple = "dxil-pc-shadermodel6.6-compute"
 
 declare i32 @some_val();
 
-define void @test_bindings() {
+define void @test_bindings() #0 {
   ; RWBuffer<float4> Buf : register(u5, space3)
   %typed0 = call target("dx.TypedBuffer", <4 x float>, 1, 0, 0)
               @llvm.dx.handle.fromBinding.tdx.TypedBuffer_v4f32_1_0_0(
@@ -73,4 +73,4 @@ define void @test_bindings() {
 ; CHECK-DAG: [[SRVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}, !{{[0-9]+}}}
 ; CHECK-DAG: [[UAVMD]] = !{!{{[0-9]+}}, !{{[0-9]+}}}
 
-attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) }
+attributes #0 = { nocallback nofree nosync nounwind willreturn memory(none) "hlsl.numthreads"="1,2,1" "hlsl.shader"="compute"}



More information about the llvm-commits mailing list