[llvm-branch-commits] [clang] [llvm] [HLSL][RootSignature] Add Metadata generation of Root Signatures for Attr (PR #125131)
Joshua Batista via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Wed Mar 5 10:52:18 PST 2025
================
@@ -0,0 +1,108 @@
+//===- HLSLRootSignature.cpp - HLSL Root Signature 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 helpers for working with HLSL Root Signatures.
+///
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Frontend/HLSL/HLSLRootSignature.h"
+#include "llvm/IR/IRBuilder.h"
+#include "llvm/IR/Metadata.h"
+#include "llvm/IR/Module.h"
+
+namespace llvm {
+namespace hlsl {
+namespace rootsig {
+
+// Static helper functions
+
+static MDString *ClauseTypeToName(LLVMContext &Ctx, ClauseType Type) {
+ StringRef Name;
+ switch (Type) {
+ case ClauseType::CBuffer:
+ Name = "CBV";
+ break;
+ case ClauseType::SRV:
+ Name = "SRV";
+ break;
+ case ClauseType::UAV:
+ Name = "UAV";
+ break;
+ case ClauseType::Sampler:
+ Name = "Sampler";
+ break;
+ }
+ return MDString::get(Ctx, Name);
+}
+
+// Helper struct so that we can use the overloaded notation of std::visit
+template <class... Ts> struct OverloadBuilds : Ts... {
+ using Ts::operator()...;
+};
+template <class... Ts> OverloadBuilds(Ts...) -> OverloadBuilds<Ts...>;
+
+MDNode *MetadataBuilder::BuildRootSignature() {
+ for (const RootElement &Element : Elements) {
+ MDNode *ElementMD =
+ std::visit(OverloadBuilds{
+ [&](DescriptorTable Table) -> MDNode * {
+ return BuildDescriptorTable(Table);
+ },
+ [&](DescriptorTableClause Clause) -> MDNode * {
+ return BuildDescriptorTableClause(Clause);
+ },
+ },
+ Element);
+ GeneratedMetadata.push_back(ElementMD);
+ }
+
+ return MDNode::get(Ctx, GeneratedMetadata);
+}
+
+MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
+ IRBuilder<> B(Ctx);
+ SmallVector<Metadata *> TableOperands;
+ // Set the mandatory arguments
+ TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));
+ TableOperands.push_back(ConstantAsMetadata::get(
+ B.getInt32(llvm::to_underlying(Table.Visibility))));
+
+ // Remaining operands are references to the table's clauses. The in-memory
----------------
bob80905 wrote:
Very helpful 👍
https://github.com/llvm/llvm-project/pull/125131
More information about the llvm-branch-commits
mailing list