|
| 1 | +//===- DXILRootSignature.cpp - DXIL Root Signature helper objects |
| 2 | +//-----------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +/// |
| 10 | +/// \file This file contains the parsing logic to extract root signature data |
| 11 | +/// from LLVM IR metadata. |
| 12 | +/// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "llvm/Analysis/DXILRootSignature.h" |
| 16 | +#include "llvm/ADT/StringSwitch.h" |
| 17 | +#include "llvm/IR/Constants.h" |
| 18 | +#include "llvm/IR/Metadata.h" |
| 19 | +#include "llvm/Support/ErrorHandling.h" |
| 20 | +#include <cassert> |
| 21 | + |
| 22 | +namespace llvm { |
| 23 | +namespace dxil { |
| 24 | + |
| 25 | +bool root_signature::MetadataParser::Parse(RootSignatureVersion Version, |
| 26 | + VersionedRootSignatureDesc *Desc) { |
| 27 | + Desc->Version = Version; |
| 28 | + bool HasError = false; |
| 29 | + |
| 30 | + for (unsigned int Sid = 0; Sid < Root->getNumOperands(); Sid++) { |
| 31 | + // This should be an if, for error handling |
| 32 | + MDNode *Node = cast<MDNode>(Root->getOperand(Sid)); |
| 33 | + |
| 34 | + // Not sure what use this for... |
| 35 | + Metadata *Func = Node->getOperand(0).get(); |
| 36 | + |
| 37 | + // This should be an if, for error handling |
| 38 | + MDNode *Elements = cast<MDNode>(Node->getOperand(1).get()); |
| 39 | + |
| 40 | + for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) { |
| 41 | + MDNode *Element = cast<MDNode>(Elements->getOperand(Eid)); |
| 42 | + |
| 43 | + HasError = HasError || ParseRootSignatureElement(Element, Desc); |
| 44 | + } |
| 45 | + } |
| 46 | + return HasError; |
| 47 | +} |
| 48 | + |
| 49 | +bool root_signature::MetadataParser::ParseRootFlags( |
| 50 | + MDNode *RootFlagNode, VersionedRootSignatureDesc *Desc) { |
| 51 | + |
| 52 | + assert(RootFlagNode->getNumOperands() == 2 && |
| 53 | + "Invalid format for RootFlag Element"); |
| 54 | + auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1)); |
| 55 | + auto Value = (RootSignatureFlags)Flag->getZExtValue(); |
| 56 | + |
| 57 | + if ((Value & ~RootSignatureFlags::ValidFlags) != RootSignatureFlags::None) |
| 58 | + return true; |
| 59 | + |
| 60 | + switch (Desc->Version) { |
| 61 | + |
| 62 | + case RootSignatureVersion::Version_1: |
| 63 | + Desc->Desc_1_0.Flags = (RootSignatureFlags)Value; |
| 64 | + break; |
| 65 | + case RootSignatureVersion::Version_1_1: |
| 66 | + case RootSignatureVersion::Version_1_2: |
| 67 | + llvm_unreachable("Not implemented yet"); |
| 68 | + break; |
| 69 | + } |
| 70 | + return false; |
| 71 | +} |
| 72 | + |
| 73 | +bool root_signature::MetadataParser::ParseRootSignatureElement( |
| 74 | + MDNode *Element, VersionedRootSignatureDesc *Desc) { |
| 75 | + MDString *ElementText = cast<MDString>(Element->getOperand(0)); |
| 76 | + |
| 77 | + assert(ElementText != nullptr && "First preoperty of element is not "); |
| 78 | + |
| 79 | + RootSignatureElementKind ElementKind = |
| 80 | + StringSwitch<RootSignatureElementKind>(ElementText->getString()) |
| 81 | + .Case("RootFlags", RootSignatureElementKind::RootFlags) |
| 82 | + .Case("RootConstants", RootSignatureElementKind::RootConstants) |
| 83 | + .Case("RootCBV", RootSignatureElementKind::RootDescriptor) |
| 84 | + .Case("RootSRV", RootSignatureElementKind::RootDescriptor) |
| 85 | + .Case("RootUAV", RootSignatureElementKind::RootDescriptor) |
| 86 | + .Case("Sampler", RootSignatureElementKind::RootDescriptor) |
| 87 | + .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable) |
| 88 | + .Case("StaticSampler", RootSignatureElementKind::StaticSampler) |
| 89 | + .Default(RootSignatureElementKind::None); |
| 90 | + |
| 91 | + switch (ElementKind) { |
| 92 | + |
| 93 | + case RootSignatureElementKind::RootFlags: { |
| 94 | + return ParseRootFlags(Element, Desc); |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + case RootSignatureElementKind::RootConstants: |
| 99 | + case RootSignatureElementKind::RootDescriptor: |
| 100 | + case RootSignatureElementKind::DescriptorTable: |
| 101 | + case RootSignatureElementKind::StaticSampler: |
| 102 | + case RootSignatureElementKind::None: |
| 103 | + llvm_unreachable("Not Implemented yet"); |
| 104 | + break; |
| 105 | + } |
| 106 | + |
| 107 | + return true; |
| 108 | +} |
| 109 | +} // namespace dxil |
| 110 | +} // namespace llvm |
0 commit comments