Path: blob/main/contrib/llvm-project/llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp
213799 views
//===- RootSignatureMetadata.h - HLSL Root Signature helpers --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file This file implements a library for working with HLSL Root Signatures9/// and their metadata representation.10///11//===----------------------------------------------------------------------===//1213#include "llvm/Frontend/HLSL/RootSignatureMetadata.h"14#include "llvm/IR/IRBuilder.h"15#include "llvm/IR/Metadata.h"16#include "llvm/Support/ScopedPrinter.h"1718namespace llvm {19namespace hlsl {20namespace rootsig {2122static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {23{"CBV", dxil::ResourceClass::CBuffer},24{"SRV", dxil::ResourceClass::SRV},25{"UAV", dxil::ResourceClass::UAV},26{"Sampler", dxil::ResourceClass::Sampler},27};2829static std::optional<StringRef> getResourceName(dxil::ResourceClass Class) {30for (const auto &ClassEnum : ResourceClassNames)31if (ClassEnum.Value == Class)32return ClassEnum.Name;33return std::nullopt;34}3536namespace {3738// We use the OverloadVisit with std::visit to ensure the compiler catches if a39// new RootElement variant type is added but it's metadata generation isn't40// handled.41template <class... Ts> struct OverloadedVisit : Ts... {42using Ts::operator()...;43};44template <class... Ts> OverloadedVisit(Ts...) -> OverloadedVisit<Ts...>;4546} // namespace4748MDNode *MetadataBuilder::BuildRootSignature() {49const auto Visitor = OverloadedVisit{50[this](const dxbc::RootFlags &Flags) -> MDNode * {51return BuildRootFlags(Flags);52},53[this](const RootConstants &Constants) -> MDNode * {54return BuildRootConstants(Constants);55},56[this](const RootDescriptor &Descriptor) -> MDNode * {57return BuildRootDescriptor(Descriptor);58},59[this](const DescriptorTableClause &Clause) -> MDNode * {60return BuildDescriptorTableClause(Clause);61},62[this](const DescriptorTable &Table) -> MDNode * {63return BuildDescriptorTable(Table);64},65[this](const StaticSampler &Sampler) -> MDNode * {66return BuildStaticSampler(Sampler);67},68};6970for (const RootElement &Element : Elements) {71MDNode *ElementMD = std::visit(Visitor, Element);72assert(ElementMD != nullptr &&73"Root Element must be initialized and validated");74GeneratedMetadata.push_back(ElementMD);75}7677return MDNode::get(Ctx, GeneratedMetadata);78}7980MDNode *MetadataBuilder::BuildRootFlags(const dxbc::RootFlags &Flags) {81IRBuilder<> Builder(Ctx);82Metadata *Operands[] = {83MDString::get(Ctx, "RootFlags"),84ConstantAsMetadata::get(Builder.getInt32(llvm::to_underlying(Flags))),85};86return MDNode::get(Ctx, Operands);87}8889MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {90IRBuilder<> Builder(Ctx);91Metadata *Operands[] = {92MDString::get(Ctx, "RootConstants"),93ConstantAsMetadata::get(94Builder.getInt32(llvm::to_underlying(Constants.Visibility))),95ConstantAsMetadata::get(Builder.getInt32(Constants.Reg.Number)),96ConstantAsMetadata::get(Builder.getInt32(Constants.Space)),97ConstantAsMetadata::get(Builder.getInt32(Constants.Num32BitConstants)),98};99return MDNode::get(Ctx, Operands);100}101102MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {103IRBuilder<> Builder(Ctx);104std::optional<StringRef> ResName = getResourceName(105dxil::ResourceClass(llvm::to_underlying(Descriptor.Type)));106assert(ResName && "Provided an invalid Resource Class");107llvm::SmallString<7> Name({"Root", *ResName});108Metadata *Operands[] = {109MDString::get(Ctx, Name),110ConstantAsMetadata::get(111Builder.getInt32(llvm::to_underlying(Descriptor.Visibility))),112ConstantAsMetadata::get(Builder.getInt32(Descriptor.Reg.Number)),113ConstantAsMetadata::get(Builder.getInt32(Descriptor.Space)),114ConstantAsMetadata::get(115Builder.getInt32(llvm::to_underlying(Descriptor.Flags))),116};117return MDNode::get(Ctx, Operands);118}119120MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {121IRBuilder<> Builder(Ctx);122SmallVector<Metadata *> TableOperands;123// Set the mandatory arguments124TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));125TableOperands.push_back(ConstantAsMetadata::get(126Builder.getInt32(llvm::to_underlying(Table.Visibility))));127128// Remaining operands are references to the table's clauses. The in-memory129// representation of the Root Elements created from parsing will ensure that130// the previous N elements are the clauses for this table.131assert(Table.NumClauses <= GeneratedMetadata.size() &&132"Table expected all owned clauses to be generated already");133// So, add a refence to each clause to our operands134TableOperands.append(GeneratedMetadata.end() - Table.NumClauses,135GeneratedMetadata.end());136// Then, remove those clauses from the general list of Root Elements137GeneratedMetadata.pop_back_n(Table.NumClauses);138139return MDNode::get(Ctx, TableOperands);140}141142MDNode *MetadataBuilder::BuildDescriptorTableClause(143const DescriptorTableClause &Clause) {144IRBuilder<> Builder(Ctx);145std::optional<StringRef> ResName =146getResourceName(dxil::ResourceClass(llvm::to_underlying(Clause.Type)));147assert(ResName && "Provided an invalid Resource Class");148Metadata *Operands[] = {149MDString::get(Ctx, *ResName),150ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)),151ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)),152ConstantAsMetadata::get(Builder.getInt32(Clause.Space)),153ConstantAsMetadata::get(Builder.getInt32(Clause.Offset)),154ConstantAsMetadata::get(155Builder.getInt32(llvm::to_underlying(Clause.Flags))),156};157return MDNode::get(Ctx, Operands);158}159160MDNode *MetadataBuilder::BuildStaticSampler(const StaticSampler &Sampler) {161IRBuilder<> Builder(Ctx);162Metadata *Operands[] = {163MDString::get(Ctx, "StaticSampler"),164ConstantAsMetadata::get(165Builder.getInt32(llvm::to_underlying(Sampler.Filter))),166ConstantAsMetadata::get(167Builder.getInt32(llvm::to_underlying(Sampler.AddressU))),168ConstantAsMetadata::get(169Builder.getInt32(llvm::to_underlying(Sampler.AddressV))),170ConstantAsMetadata::get(171Builder.getInt32(llvm::to_underlying(Sampler.AddressW))),172ConstantAsMetadata::get(llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx),173Sampler.MipLODBias)),174ConstantAsMetadata::get(Builder.getInt32(Sampler.MaxAnisotropy)),175ConstantAsMetadata::get(176Builder.getInt32(llvm::to_underlying(Sampler.CompFunc))),177ConstantAsMetadata::get(178Builder.getInt32(llvm::to_underlying(Sampler.BorderColor))),179ConstantAsMetadata::get(180llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MinLOD)),181ConstantAsMetadata::get(182llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MaxLOD)),183ConstantAsMetadata::get(Builder.getInt32(Sampler.Reg.Number)),184ConstantAsMetadata::get(Builder.getInt32(Sampler.Space)),185ConstantAsMetadata::get(186Builder.getInt32(llvm::to_underlying(Sampler.Visibility))),187};188return MDNode::get(Ctx, Operands);189}190191} // namespace rootsig192} // namespace hlsl193} // namespace llvm194195196