|
| 1 | +//===- DXILRootSignature.cpp - DXIL Root Signature helper objects ---------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +/// |
| 9 | +/// \file This file contains helper objects and APIs for working with DXIL |
| 10 | +/// Root Signatures. |
| 11 | +/// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | +#include "DXILRootSignature.h" |
| 14 | +#include "DirectX.h" |
| 15 | +#include "llvm/ADT/StringSwitch.h" |
| 16 | +#include "llvm/BinaryFormat/DXContainer.h" |
| 17 | +#include "llvm/IR/Constants.h" |
| 18 | +#include "llvm/IR/Metadata.h" |
| 19 | +#include "llvm/IR/Module.h" |
| 20 | + |
| 21 | +using namespace llvm; |
| 22 | +using namespace llvm::dxil; |
| 23 | + |
| 24 | +static bool parseRootFlags(ModuleRootSignature *MRS, MDNode *RootFlagNode) { |
| 25 | + |
| 26 | + assert(RootFlagNode->getNumOperands() == 2 && |
| 27 | + "Invalid format for RootFlag Element"); |
| 28 | + auto *Flag = mdconst::extract<ConstantInt>(RootFlagNode->getOperand(1)); |
| 29 | + auto Value = Flag->getZExtValue(); |
| 30 | + |
| 31 | + // Root Element validation, as specified: https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation |
| 32 | + if ((Value & ~0x80000fff) != 0) |
| 33 | + return true; |
| 34 | + |
| 35 | + MRS->Flags = Value; |
| 36 | + return false; |
| 37 | +} |
| 38 | + |
| 39 | +static bool parseRootSignatureElement(ModuleRootSignature *MRS, MDNode *Element) { |
| 40 | + MDString *ElementText = cast<MDString>(Element->getOperand(0)); |
| 41 | + |
| 42 | + assert(ElementText != nullptr && "First preoperty of element is not "); |
| 43 | + |
| 44 | + RootSignatureElementKind ElementKind = |
| 45 | + StringSwitch<RootSignatureElementKind>(ElementText->getString()) |
| 46 | + .Case("RootFlags", RootSignatureElementKind::RootFlags) |
| 47 | + .Case("RootConstants", RootSignatureElementKind::RootConstants) |
| 48 | + .Case("RootCBV", RootSignatureElementKind::RootDescriptor) |
| 49 | + .Case("RootSRV", RootSignatureElementKind::RootDescriptor) |
| 50 | + .Case("RootUAV", RootSignatureElementKind::RootDescriptor) |
| 51 | + .Case("Sampler", RootSignatureElementKind::RootDescriptor) |
| 52 | + .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable) |
| 53 | + .Case("StaticSampler", RootSignatureElementKind::StaticSampler) |
| 54 | + .Default(RootSignatureElementKind::None); |
| 55 | + |
| 56 | + switch (ElementKind) { |
| 57 | + |
| 58 | + case RootSignatureElementKind::RootFlags: { |
| 59 | + return parseRootFlags(MRS, Element); |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + case RootSignatureElementKind::RootConstants: |
| 64 | + case RootSignatureElementKind::RootDescriptor: |
| 65 | + case RootSignatureElementKind::DescriptorTable: |
| 66 | + case RootSignatureElementKind::StaticSampler: |
| 67 | + case RootSignatureElementKind::None: |
| 68 | + llvm_unreachable("Not Implemented yet"); |
| 69 | + break; |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | +} |
| 74 | + |
| 75 | +bool ModuleRootSignature::parse( int32_t Version, |
| 76 | + NamedMDNode *Root) { |
| 77 | + this->Version = Version; |
| 78 | + bool HasError = false; |
| 79 | + |
| 80 | + for (unsigned int Sid = 0; Sid < Root->getNumOperands(); Sid++) { |
| 81 | + // This should be an if, for error handling |
| 82 | + MDNode *Node = cast<MDNode>(Root->getOperand(Sid)); |
| 83 | + |
| 84 | + // Not sure what use this for... |
| 85 | + Metadata *Func = Node->getOperand(0).get(); |
| 86 | + |
| 87 | + // This should be an if, for error handling |
| 88 | + MDNode *Elements = cast<MDNode>(Node->getOperand(1).get()); |
| 89 | + |
| 90 | + for (unsigned int Eid = 0; Eid < Elements->getNumOperands(); Eid++) { |
| 91 | + MDNode *Element = cast<MDNode>(Elements->getOperand(Eid)); |
| 92 | + |
| 93 | + HasError = HasError || parseRootSignatureElement(this, Element); |
| 94 | + } |
| 95 | + } |
| 96 | + return HasError; |
| 97 | +} |
| 98 | + |
| 99 | +void ModuleRootSignature::write(raw_ostream &OS) { |
| 100 | + dxbc::RootSignatureDesc Out{this->Version, this->Flags}; |
| 101 | + |
| 102 | + if (sys::IsBigEndianHost) { |
| 103 | + Out.swapBytes(); |
| 104 | + } |
| 105 | + |
| 106 | + OS.write(reinterpret_cast<const char *>(&Out), sizeof(dxbc::RootSignatureDesc)); |
| 107 | +} |
| 108 | + |
| 109 | +AnalysisKey RootSignatureAnalysis::Key; |
| 110 | + |
| 111 | +ModuleRootSignature RootSignatureAnalysis::run(Module &M, |
| 112 | + ModuleAnalysisManager &AM) { |
| 113 | + ModuleRootSignature MRSI; |
| 114 | + |
| 115 | + NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures"); |
| 116 | + if (RootSignatureNode) { |
| 117 | + MRSI.parse(1, RootSignatureNode); |
| 118 | + } |
| 119 | + |
| 120 | + return MRSI; |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | + |
| 125 | +//===----------------------------------------------------------------------===// |
| 126 | +bool RootSignatureAnalysisWrapper::runOnModule(Module &M) { |
| 127 | + ModuleRootSignature MRS; |
| 128 | + |
| 129 | + NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures"); |
| 130 | + if (RootSignatureNode) { |
| 131 | + MRS.parse(1, RootSignatureNode); |
| 132 | + this->MRS = MRS; |
| 133 | + } |
| 134 | + |
| 135 | + |
| 136 | + return false; |
| 137 | +} |
| 138 | + |
| 139 | +void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const { |
| 140 | + AU.setPreservesAll(); |
| 141 | +} |
| 142 | + |
| 143 | +char RootSignatureAnalysisWrapper::ID = 0; |
| 144 | + |
| 145 | +INITIALIZE_PASS(RootSignatureAnalysisWrapper, "dx-root-signature-analysis", |
| 146 | + "DXIL Root Signature Analysis", true, true) |
0 commit comments