LLVM 22.0.0git
RootSignatureMetadata.cpp
Go to the documentation of this file.
1//===- RootSignatureMetadata.h - HLSL Root Signature helpers --------------===//
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 implements a library for working with HLSL Root Signatures
10/// and their metadata representation.
11///
12//===----------------------------------------------------------------------===//
13
16#include "llvm/IR/IRBuilder.h"
17#include "llvm/IR/Metadata.h"
20
21using namespace llvm;
22
23namespace llvm {
24namespace hlsl {
25namespace rootsig {
26
34
35template <typename T> char RootSignatureValidationError<T>::ID;
36
37static std::optional<uint32_t> extractMdIntValue(MDNode *Node,
38 unsigned int OpId) {
39 if (auto *CI =
40 mdconst::dyn_extract<ConstantInt>(Node->getOperand(OpId).get()))
41 return CI->getZExtValue();
42 return std::nullopt;
43}
44
45static std::optional<float> extractMdFloatValue(MDNode *Node,
46 unsigned int OpId) {
47 if (auto *CI = mdconst::dyn_extract<ConstantFP>(Node->getOperand(OpId).get()))
48 return CI->getValueAPF().convertToFloat();
49 return std::nullopt;
50}
51
52static std::optional<StringRef> extractMdStringValue(MDNode *Node,
53 unsigned int OpId) {
54 MDString *NodeText = dyn_cast<MDString>(Node->getOperand(OpId));
55 if (NodeText == nullptr)
56 return std::nullopt;
57 return NodeText->getString();
58}
59
60template <typename T, typename = std::enable_if_t<
61 std::is_enum_v<T> &&
62 std::is_same_v<std::underlying_type_t<T>, uint32_t>>>
63static Expected<T>
64extractEnumValue(MDNode *Node, unsigned int OpId, StringRef ErrText,
65 llvm::function_ref<bool(uint32_t)> VerifyFn) {
66 if (std::optional<uint32_t> Val = extractMdIntValue(Node, OpId)) {
67 if (!VerifyFn(*Val))
69 return static_cast<T>(*Val);
70 }
71 return make_error<InvalidRSMetadataValue>("ShaderVisibility");
72}
73
74namespace {
75
76// We use the OverloadVisit with std::visit to ensure the compiler catches if a
77// new RootElement variant type is added but it's metadata generation isn't
78// handled.
79template <class... Ts> struct OverloadedVisit : Ts... {
80 using Ts::operator()...;
81};
82template <class... Ts> OverloadedVisit(Ts...) -> OverloadedVisit<Ts...>;
83
84} // namespace
85
87 const auto Visitor = OverloadedVisit{
88 [this](const dxbc::RootFlags &Flags) -> MDNode * {
89 return BuildRootFlags(Flags);
90 },
91 [this](const RootConstants &Constants) -> MDNode * {
92 return BuildRootConstants(Constants);
93 },
94 [this](const RootDescriptor &Descriptor) -> MDNode * {
95 return BuildRootDescriptor(Descriptor);
96 },
97 [this](const DescriptorTableClause &Clause) -> MDNode * {
98 return BuildDescriptorTableClause(Clause);
99 },
100 [this](const DescriptorTable &Table) -> MDNode * {
101 return BuildDescriptorTable(Table);
102 },
103 [this](const StaticSampler &Sampler) -> MDNode * {
104 return BuildStaticSampler(Sampler);
105 },
106 };
107
108 for (const RootElement &Element : Elements) {
109 MDNode *ElementMD = std::visit(Visitor, Element);
110 assert(ElementMD != nullptr &&
111 "Root Element must be initialized and validated");
112 GeneratedMetadata.push_back(ElementMD);
113 }
114
115 return MDNode::get(Ctx, GeneratedMetadata);
116}
117
118MDNode *MetadataBuilder::BuildRootFlags(const dxbc::RootFlags &Flags) {
119 IRBuilder<> Builder(Ctx);
120 Metadata *Operands[] = {
121 MDString::get(Ctx, "RootFlags"),
122 ConstantAsMetadata::get(Builder.getInt32(to_underlying(Flags))),
123 };
124 return MDNode::get(Ctx, Operands);
125}
126
127MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {
128 IRBuilder<> Builder(Ctx);
129 Metadata *Operands[] = {
130 MDString::get(Ctx, "RootConstants"),
132 Builder.getInt32(to_underlying(Constants.Visibility))),
133 ConstantAsMetadata::get(Builder.getInt32(Constants.Reg.Number)),
134 ConstantAsMetadata::get(Builder.getInt32(Constants.Space)),
135 ConstantAsMetadata::get(Builder.getInt32(Constants.Num32BitConstants)),
136 };
137 return MDNode::get(Ctx, Operands);
138}
139
140MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
141 IRBuilder<> Builder(Ctx);
142 StringRef ResName = dxil::getResourceClassName(Descriptor.Type);
143 assert(!ResName.empty() && "Provided an invalid Resource Class");
144 SmallString<7> Name({"Root", ResName});
145 Metadata *Operands[] = {
146 MDString::get(Ctx, Name),
148 Builder.getInt32(to_underlying(Descriptor.Visibility))),
149 ConstantAsMetadata::get(Builder.getInt32(Descriptor.Reg.Number)),
150 ConstantAsMetadata::get(Builder.getInt32(Descriptor.Space)),
152 Builder.getInt32(to_underlying(Descriptor.Flags))),
153 };
154 return MDNode::get(Ctx, Operands);
155}
156
157MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
158 IRBuilder<> Builder(Ctx);
159 SmallVector<Metadata *> TableOperands;
160 // Set the mandatory arguments
161 TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));
163 Builder.getInt32(to_underlying(Table.Visibility))));
164
165 // Remaining operands are references to the table's clauses. The in-memory
166 // representation of the Root Elements created from parsing will ensure that
167 // the previous N elements are the clauses for this table.
168 assert(Table.NumClauses <= GeneratedMetadata.size() &&
169 "Table expected all owned clauses to be generated already");
170 // So, add a refence to each clause to our operands
171 TableOperands.append(GeneratedMetadata.end() - Table.NumClauses,
172 GeneratedMetadata.end());
173 // Then, remove those clauses from the general list of Root Elements
174 GeneratedMetadata.pop_back_n(Table.NumClauses);
175
176 return MDNode::get(Ctx, TableOperands);
177}
178
179MDNode *MetadataBuilder::BuildDescriptorTableClause(
180 const DescriptorTableClause &Clause) {
181 IRBuilder<> Builder(Ctx);
182 StringRef ResName = dxil::getResourceClassName(Clause.Type);
183 assert(!ResName.empty() && "Provided an invalid Resource Class");
184 Metadata *Operands[] = {
185 MDString::get(Ctx, ResName),
186 ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)),
187 ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)),
188 ConstantAsMetadata::get(Builder.getInt32(Clause.Space)),
189 ConstantAsMetadata::get(Builder.getInt32(Clause.Offset)),
190 ConstantAsMetadata::get(Builder.getInt32(to_underlying(Clause.Flags))),
191 };
192 return MDNode::get(Ctx, Operands);
193}
194
195MDNode *MetadataBuilder::BuildStaticSampler(const StaticSampler &Sampler) {
196 IRBuilder<> Builder(Ctx);
197 Metadata *Operands[] = {
198 MDString::get(Ctx, "StaticSampler"),
199 ConstantAsMetadata::get(Builder.getInt32(to_underlying(Sampler.Filter))),
201 Builder.getInt32(to_underlying(Sampler.AddressU))),
203 Builder.getInt32(to_underlying(Sampler.AddressV))),
205 Builder.getInt32(to_underlying(Sampler.AddressW))),
207 ConstantFP::get(Type::getFloatTy(Ctx), Sampler.MipLODBias)),
208 ConstantAsMetadata::get(Builder.getInt32(Sampler.MaxAnisotropy)),
210 Builder.getInt32(to_underlying(Sampler.CompFunc))),
212 Builder.getInt32(to_underlying(Sampler.BorderColor))),
214 ConstantFP::get(Type::getFloatTy(Ctx), Sampler.MinLOD)),
216 ConstantFP::get(Type::getFloatTy(Ctx), Sampler.MaxLOD)),
217 ConstantAsMetadata::get(Builder.getInt32(Sampler.Reg.Number)),
218 ConstantAsMetadata::get(Builder.getInt32(Sampler.Space)),
220 Builder.getInt32(to_underlying(Sampler.Visibility))),
221 };
222 return MDNode::get(Ctx, Operands);
223}
224
225Error MetadataParser::parseRootFlags(mcdxbc::RootSignatureDesc &RSD,
226 MDNode *RootFlagNode) {
227 if (RootFlagNode->getNumOperands() != 2)
228 return make_error<InvalidRSMetadataFormat>("RootFlag Element");
229
230 if (std::optional<uint32_t> Val = extractMdIntValue(RootFlagNode, 1))
231 RSD.Flags = *Val;
232 else
233 return make_error<InvalidRSMetadataValue>("RootFlag");
234
235 return Error::success();
236}
237
238Error MetadataParser::parseRootConstants(mcdxbc::RootSignatureDesc &RSD,
239 MDNode *RootConstantNode) {
240 if (RootConstantNode->getNumOperands() != 5)
241 return make_error<InvalidRSMetadataFormat>("RootConstants Element");
242
243 Expected<dxbc::ShaderVisibility> Visibility =
245 "ShaderVisibility",
247 if (auto E = Visibility.takeError())
248 return Error(std::move(E));
249
250 mcdxbc::RootConstants Constants;
251 if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 2))
252 Constants.ShaderRegister = *Val;
253 else
254 return make_error<InvalidRSMetadataValue>("ShaderRegister");
255
256 if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 3))
257 Constants.RegisterSpace = *Val;
258 else
259 return make_error<InvalidRSMetadataValue>("RegisterSpace");
260
261 if (std::optional<uint32_t> Val = extractMdIntValue(RootConstantNode, 4))
262 Constants.Num32BitValues = *Val;
263 else
264 return make_error<InvalidRSMetadataValue>("Num32BitValues");
265
266 RSD.ParametersContainer.addParameter(dxbc::RootParameterType::Constants32Bit,
267 *Visibility, Constants);
268
269 return Error::success();
270}
271
272Error MetadataParser::parseRootDescriptors(
273 mcdxbc::RootSignatureDesc &RSD, MDNode *RootDescriptorNode,
274 RootSignatureElementKind ElementKind) {
275 assert((ElementKind == RootSignatureElementKind::SRV ||
276 ElementKind == RootSignatureElementKind::UAV ||
277 ElementKind == RootSignatureElementKind::CBV) &&
278 "parseRootDescriptors should only be called with RootDescriptor "
279 "element kind.");
280 if (RootDescriptorNode->getNumOperands() != 5)
281 return make_error<InvalidRSMetadataFormat>("Root Descriptor Element");
282
284 switch (ElementKind) {
286 Type = dxbc::RootParameterType::SRV;
287 break;
289 Type = dxbc::RootParameterType::UAV;
290 break;
292 Type = dxbc::RootParameterType::CBV;
293 break;
294 default:
295 llvm_unreachable("invalid Root Descriptor kind");
296 break;
297 }
298
299 Expected<dxbc::ShaderVisibility> Visibility =
300 extractEnumValue<dxbc::ShaderVisibility>(RootDescriptorNode, 1,
301 "ShaderVisibility",
303 if (auto E = Visibility.takeError())
304 return Error(std::move(E));
305
306 mcdxbc::RootDescriptor Descriptor;
307 if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 2))
308 Descriptor.ShaderRegister = *Val;
309 else
310 return make_error<InvalidRSMetadataValue>("ShaderRegister");
311
312 if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 3))
313 Descriptor.RegisterSpace = *Val;
314 else
315 return make_error<InvalidRSMetadataValue>("RegisterSpace");
316
317 if (RSD.Version == 1) {
318 RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor);
319 return Error::success();
320 }
321 assert(RSD.Version > 1);
322
323 if (std::optional<uint32_t> Val = extractMdIntValue(RootDescriptorNode, 4))
324 Descriptor.Flags = *Val;
325 else
326 return make_error<InvalidRSMetadataValue>("Root Descriptor Flags");
327
328 RSD.ParametersContainer.addParameter(Type, *Visibility, Descriptor);
329 return Error::success();
330}
331
332Error MetadataParser::parseDescriptorRange(mcdxbc::DescriptorTable &Table,
333 MDNode *RangeDescriptorNode) {
334 if (RangeDescriptorNode->getNumOperands() != 6)
335 return make_error<InvalidRSMetadataFormat>("Descriptor Range");
336
337 mcdxbc::DescriptorRange Range;
338
339 std::optional<StringRef> ElementText =
340 extractMdStringValue(RangeDescriptorNode, 0);
341
342 if (!ElementText.has_value())
343 return make_error<InvalidRSMetadataFormat>("Descriptor Range");
344
345 if (*ElementText == "CBV")
347 else if (*ElementText == "SRV")
349 else if (*ElementText == "UAV")
351 else if (*ElementText == "Sampler")
353 else
354 return make_error<GenericRSMetadataError>("Invalid Descriptor Range type.",
355 RangeDescriptorNode);
356
357 if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 1))
358 Range.NumDescriptors = *Val;
359 else
360 return make_error<GenericRSMetadataError>("Number of Descriptor in Range",
361 RangeDescriptorNode);
362
363 if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 2))
364 Range.BaseShaderRegister = *Val;
365 else
366 return make_error<InvalidRSMetadataValue>("BaseShaderRegister");
367
368 if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 3))
369 Range.RegisterSpace = *Val;
370 else
371 return make_error<InvalidRSMetadataValue>("RegisterSpace");
372
373 if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 4))
374 Range.OffsetInDescriptorsFromTableStart = *Val;
375 else
377 "OffsetInDescriptorsFromTableStart");
378
379 if (std::optional<uint32_t> Val = extractMdIntValue(RangeDescriptorNode, 5))
380 Range.Flags = *Val;
381 else
382 return make_error<InvalidRSMetadataValue>("Descriptor Range Flags");
383
384 Table.Ranges.push_back(Range);
385 return Error::success();
386}
387
388Error MetadataParser::parseDescriptorTable(mcdxbc::RootSignatureDesc &RSD,
389 MDNode *DescriptorTableNode) {
390 const unsigned int NumOperands = DescriptorTableNode->getNumOperands();
391 if (NumOperands < 2)
392 return make_error<InvalidRSMetadataFormat>("Descriptor Table");
393
394 Expected<dxbc::ShaderVisibility> Visibility =
395 extractEnumValue<dxbc::ShaderVisibility>(DescriptorTableNode, 1,
396 "ShaderVisibility",
398 if (auto E = Visibility.takeError())
399 return Error(std::move(E));
400
401 mcdxbc::DescriptorTable Table;
402
403 for (unsigned int I = 2; I < NumOperands; I++) {
404 MDNode *Element = dyn_cast<MDNode>(DescriptorTableNode->getOperand(I));
405 if (Element == nullptr)
407 "Missing Root Element Metadata Node.", DescriptorTableNode);
408
409 if (auto Err = parseDescriptorRange(Table, Element))
410 return Err;
411 }
412
413 RSD.ParametersContainer.addParameter(dxbc::RootParameterType::DescriptorTable,
414 *Visibility, Table);
415 return Error::success();
416}
417
418Error MetadataParser::parseStaticSampler(mcdxbc::RootSignatureDesc &RSD,
419 MDNode *StaticSamplerNode) {
420 if (StaticSamplerNode->getNumOperands() != 14)
421 return make_error<InvalidRSMetadataFormat>("Static Sampler");
422
423 mcdxbc::StaticSampler Sampler;
424
425 Expected<dxbc::SamplerFilter> Filter = extractEnumValue<dxbc::SamplerFilter>(
426 StaticSamplerNode, 1, "Filter", dxbc::isValidSamplerFilter);
427 if (auto E = Filter.takeError())
428 return Error(std::move(E));
429 Sampler.Filter = *Filter;
430
431 Expected<dxbc::TextureAddressMode> AddressU =
433 StaticSamplerNode, 2, "AddressU", dxbc::isValidAddress);
434 if (auto E = AddressU.takeError())
435 return Error(std::move(E));
436 Sampler.AddressU = *AddressU;
437
438 Expected<dxbc::TextureAddressMode> AddressV =
440 StaticSamplerNode, 3, "AddressV", dxbc::isValidAddress);
441 if (auto E = AddressV.takeError())
442 return Error(std::move(E));
443 Sampler.AddressV = *AddressV;
444
445 Expected<dxbc::TextureAddressMode> AddressW =
447 StaticSamplerNode, 4, "AddressW", dxbc::isValidAddress);
448 if (auto E = AddressW.takeError())
449 return Error(std::move(E));
450 Sampler.AddressW = *AddressW;
451
452 if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 5))
453 Sampler.MipLODBias = *Val;
454 else
455 return make_error<InvalidRSMetadataValue>("MipLODBias");
456
457 if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 6))
458 Sampler.MaxAnisotropy = *Val;
459 else
460 return make_error<InvalidRSMetadataValue>("MaxAnisotropy");
461
462 Expected<dxbc::ComparisonFunc> ComparisonFunc =
464 StaticSamplerNode, 7, "ComparisonFunc", dxbc::isValidComparisonFunc);
465 if (auto E = ComparisonFunc.takeError())
466 return Error(std::move(E));
467 Sampler.ComparisonFunc = *ComparisonFunc;
468
469 Expected<dxbc::StaticBorderColor> BorderColor =
471 StaticSamplerNode, 8, "BorderColor", dxbc::isValidBorderColor);
472 if (auto E = BorderColor.takeError())
473 return Error(std::move(E));
474 Sampler.BorderColor = *BorderColor;
475
476 if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 9))
477 Sampler.MinLOD = *Val;
478 else
479 return make_error<InvalidRSMetadataValue>("MinLOD");
480
481 if (std::optional<float> Val = extractMdFloatValue(StaticSamplerNode, 10))
482 Sampler.MaxLOD = *Val;
483 else
484 return make_error<InvalidRSMetadataValue>("MaxLOD");
485
486 if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 11))
487 Sampler.ShaderRegister = *Val;
488 else
489 return make_error<InvalidRSMetadataValue>("ShaderRegister");
490
491 if (std::optional<uint32_t> Val = extractMdIntValue(StaticSamplerNode, 12))
492 Sampler.RegisterSpace = *Val;
493 else
494 return make_error<InvalidRSMetadataValue>("RegisterSpace");
495
496 Expected<dxbc::ShaderVisibility> Visibility =
497 extractEnumValue<dxbc::ShaderVisibility>(StaticSamplerNode, 13,
498 "ShaderVisibility",
500 if (auto E = Visibility.takeError())
501 return Error(std::move(E));
502 Sampler.ShaderVisibility = *Visibility;
503
504 RSD.StaticSamplers.push_back(Sampler);
505 return Error::success();
506}
507
508Error MetadataParser::parseRootSignatureElement(mcdxbc::RootSignatureDesc &RSD,
509 MDNode *Element) {
510 std::optional<StringRef> ElementText = extractMdStringValue(Element, 0);
511 if (!ElementText.has_value())
512 return make_error<InvalidRSMetadataFormat>("Root Element");
513
514 RootSignatureElementKind ElementKind =
515 StringSwitch<RootSignatureElementKind>(*ElementText)
516 .Case("RootFlags", RootSignatureElementKind::RootFlags)
517 .Case("RootConstants", RootSignatureElementKind::RootConstants)
518 .Case("RootCBV", RootSignatureElementKind::CBV)
519 .Case("RootSRV", RootSignatureElementKind::SRV)
520 .Case("RootUAV", RootSignatureElementKind::UAV)
521 .Case("DescriptorTable", RootSignatureElementKind::DescriptorTable)
522 .Case("StaticSampler", RootSignatureElementKind::StaticSamplers)
524
525 switch (ElementKind) {
526
528 return parseRootFlags(RSD, Element);
530 return parseRootConstants(RSD, Element);
534 return parseRootDescriptors(RSD, Element, ElementKind);
536 return parseDescriptorTable(RSD, Element);
538 return parseStaticSampler(RSD, Element);
540 return make_error<GenericRSMetadataError>("Invalid Root Signature Element",
541 Element);
542 }
543
544 llvm_unreachable("Unhandled RootSignatureElementKind enum.");
545}
546
547static Error
549 uint32_t Location) {
551 for (const mcdxbc::DescriptorRange &Range : Table.Ranges) {
552 if (Range.RangeType == dxil::ResourceClass::Sampler &&
554 return make_error<TableSamplerMixinError>(CurrRC, Location);
555 CurrRC = Range.RangeType;
556 }
557 return Error::success();
558}
559
560static Error
562 uint32_t Location) {
563 uint64_t Offset = 0;
564 bool IsPrevUnbound = false;
565 for (const mcdxbc::DescriptorRange &Range : Table.Ranges) {
566 // Validation of NumDescriptors should have happened by this point.
567 if (Range.NumDescriptors == 0)
568 continue;
569
571 Range.BaseShaderRegister, Range.NumDescriptors);
572
573 if (!verifyNoOverflowedOffset(RangeBound))
575 Range.RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
576
577 bool IsAppending =
578 Range.OffsetInDescriptorsFromTableStart == DescriptorTableOffsetAppend;
579 if (!IsAppending)
580 Offset = Range.OffsetInDescriptorsFromTableStart;
581
582 if (IsPrevUnbound && IsAppending)
584 Range.RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
585
586 const uint64_t OffsetBound =
588
589 if (!verifyNoOverflowedOffset(OffsetBound))
591 Range.RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
592
593 Offset = OffsetBound + 1;
594 IsPrevUnbound =
596 }
597
598 return Error::success();
599}
600
601Error MetadataParser::validateRootSignature(
602 const mcdxbc::RootSignatureDesc &RSD) {
603 Error DeferredErrs = Error::success();
605 DeferredErrs =
606 joinErrors(std::move(DeferredErrs),
607 make_error<RootSignatureValidationError<uint32_t>>(
608 "Version", RSD.Version));
609 }
610
612 DeferredErrs =
613 joinErrors(std::move(DeferredErrs),
614 make_error<RootSignatureValidationError<uint32_t>>(
615 "RootFlags", RSD.Flags));
616 }
617
619
620 switch (Info.Type) {
621 case dxbc::RootParameterType::Constants32Bit:
622 break;
623
624 case dxbc::RootParameterType::CBV:
625 case dxbc::RootParameterType::UAV:
626 case dxbc::RootParameterType::SRV: {
627 const mcdxbc::RootDescriptor &Descriptor =
630 DeferredErrs =
631 joinErrors(std::move(DeferredErrs),
632 make_error<RootSignatureValidationError<uint32_t>>(
633 "ShaderRegister", Descriptor.ShaderRegister));
634
636 DeferredErrs =
637 joinErrors(std::move(DeferredErrs),
638 make_error<RootSignatureValidationError<uint32_t>>(
639 "RegisterSpace", Descriptor.RegisterSpace));
640
641 if (RSD.Version > 1) {
643 Descriptor.Flags))
644 DeferredErrs =
645 joinErrors(std::move(DeferredErrs),
646 make_error<RootSignatureValidationError<uint32_t>>(
647 "RootDescriptorFlag", Descriptor.Flags));
648 }
649 break;
650 }
651 case dxbc::RootParameterType::DescriptorTable: {
652 const mcdxbc::DescriptorTable &Table =
654 for (const mcdxbc::DescriptorRange &Range : Table) {
655 if (!hlsl::rootsig::verifyRegisterSpace(Range.RegisterSpace))
656 DeferredErrs =
657 joinErrors(std::move(DeferredErrs),
658 make_error<RootSignatureValidationError<uint32_t>>(
659 "RegisterSpace", Range.RegisterSpace));
660
661 if (!hlsl::rootsig::verifyNumDescriptors(Range.NumDescriptors))
662 DeferredErrs =
663 joinErrors(std::move(DeferredErrs),
664 make_error<RootSignatureValidationError<uint32_t>>(
665 "NumDescriptors", Range.NumDescriptors));
666
668 RSD.Version, Range.RangeType,
670 DeferredErrs =
671 joinErrors(std::move(DeferredErrs),
672 make_error<RootSignatureValidationError<uint32_t>>(
673 "DescriptorFlag", Range.Flags));
674
675 if (Error Err =
677 DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(Err));
678
679 if (Error Err =
681 DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(Err));
682 }
683 break;
684 }
685 }
686 }
687
688 for (const mcdxbc::StaticSampler &Sampler : RSD.StaticSamplers) {
689
691 DeferredErrs = joinErrors(std::move(DeferredErrs),
692 make_error<RootSignatureValidationError<float>>(
693 "MipLODBias", Sampler.MipLODBias));
694
696 DeferredErrs =
697 joinErrors(std::move(DeferredErrs),
698 make_error<RootSignatureValidationError<uint32_t>>(
699 "MaxAnisotropy", Sampler.MaxAnisotropy));
700
702 DeferredErrs = joinErrors(std::move(DeferredErrs),
703 make_error<RootSignatureValidationError<float>>(
704 "MinLOD", Sampler.MinLOD));
705
707 DeferredErrs = joinErrors(std::move(DeferredErrs),
708 make_error<RootSignatureValidationError<float>>(
709 "MaxLOD", Sampler.MaxLOD));
710
711 if (!hlsl::rootsig::verifyRegisterValue(Sampler.ShaderRegister))
712 DeferredErrs =
713 joinErrors(std::move(DeferredErrs),
714 make_error<RootSignatureValidationError<uint32_t>>(
715 "ShaderRegister", Sampler.ShaderRegister));
716
718 DeferredErrs =
719 joinErrors(std::move(DeferredErrs),
720 make_error<RootSignatureValidationError<uint32_t>>(
721 "RegisterSpace", Sampler.RegisterSpace));
722
724 DeferredErrs =
725 joinErrors(std::move(DeferredErrs),
726 make_error<RootSignatureValidationError<uint32_t>>(
727 "Static Sampler Flag", Sampler.Flags));
728 }
729
730 return DeferredErrs;
731}
732
733Expected<mcdxbc::RootSignatureDesc>
735 Error DeferredErrs = Error::success();
737 RSD.Version = Version;
738 for (const auto &Operand : Root->operands()) {
739 MDNode *Element = dyn_cast<MDNode>(Operand);
740 if (Element == nullptr)
741 return joinErrors(std::move(DeferredErrs),
743 "Missing Root Element Metadata Node.", nullptr));
744
745 if (auto Err = parseRootSignatureElement(RSD, Element))
746 DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(Err));
747 }
748
749 if (auto Err = validateRootSignature(RSD))
750 DeferredErrs = joinErrors(std::move(DeferredErrs), std::move(Err));
751
752 if (DeferredErrs)
753 return std::move(DeferredErrs);
754
755 return std::move(RSD);
756}
757} // namespace rootsig
758} // namespace hlsl
759} // namespace llvm
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
dxil translate DXIL Translate Metadata
#define I(x, y, z)
Definition MD5.cpp:58
mir Rename Register Operands
This file contains the declarations for metadata subclasses.
#define T
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:535
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2780
Metadata node.
Definition Metadata.h:1077
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1441
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1561
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1447
A single uniqued string.
Definition Metadata.h:720
LLVM_ABI StringRef getString() const
Definition Metadata.cpp:617
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:607
Root of the metadata hierarchy.
Definition Metadata.h:63
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:285
An efficient, type-erasing, non-owning reference to a callable.
LLVM_ABI MDNode * BuildRootSignature()
Iterates through elements and dispatches onto the correct Build* method.
LLVM_ABI llvm::Expected< llvm::mcdxbc::RootSignatureDesc > ParseRootSignature(uint32_t Version)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isValidShaderVisibility(uint32_t V)
bool isValidSamplerFilter(uint32_t V)
bool isValidBorderColor(uint32_t V)
bool isValidComparisonFunc(uint32_t V)
bool isValidAddress(uint32_t V)
LLVM_ABI StringRef getResourceClassName(ResourceClass RC)
Definition DXILABI.cpp:21
static std::optional< uint32_t > extractMdIntValue(MDNode *Node, unsigned int OpId)
LLVM_ABI bool verifyRootDescriptorFlag(uint32_t Version, uint32_t FlagsVal)
LLVM_ABI uint64_t computeRangeBound(uint64_t Offset, uint32_t Size)
static const uint32_t NumDescriptorsUnbounded
static Error validateDescriptorTableRegisterOverflow(const mcdxbc::DescriptorTable &Table, uint32_t Location)
LLVM_ABI bool verifyStaticSamplerFlags(uint32_t Version, uint32_t FlagsNumber)
LLVM_ABI bool verifyRegisterSpace(uint32_t RegisterSpace)
static const uint32_t DescriptorTableOffsetAppend
LLVM_ABI bool verifyDescriptorRangeFlag(uint32_t Version, dxil::ResourceClass Type, dxbc::DescriptorRangeFlags FlagsVal)
static Error validateDescriptorTableSamplerMixin(const mcdxbc::DescriptorTable &Table, uint32_t Location)
LLVM_ABI bool verifyVersion(uint32_t Version)
static std::optional< StringRef > extractMdStringValue(MDNode *Node, unsigned int OpId)
LLVM_ABI bool verifyRootFlag(uint32_t Flags)
LLVM_ABI bool verifyLOD(float LOD)
std::variant< dxbc::RootFlags, RootConstants, RootDescriptor, DescriptorTable, DescriptorTableClause, StaticSampler > RootElement
Models RootElement : RootFlags | RootConstants | RootParam | DescriptorTable | DescriptorTableClause ...
LLVM_ABI bool verifyNoOverflowedOffset(uint64_t Offset)
LLVM_ABI bool verifyMipLODBias(float MipLODBias)
LLVM_ABI bool verifyNumDescriptors(uint32_t NumDescriptors)
LLVM_ABI bool verifyMaxAnisotropy(uint32_t MaxAnisotropy)
static Expected< T > extractEnumValue(MDNode *Node, unsigned int OpId, StringRef ErrText, llvm::function_ref< bool(uint32_t)> VerifyFn)
LLVM_ABI bool verifyRegisterValue(uint32_t RegisterValue)
static std::optional< float > extractMdFloatValue(MDNode *Node, unsigned int OpId)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
Definition Metadata.h:694
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:477
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
FunctionAddr VTableAddr uintptr_t uintptr_t Version
Definition InstrProf.h:302
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition Error.h:442
constexpr std::underlying_type_t< Enum > to_underlying(Enum E)
Returns underlying integer value of an enum.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
SmallVector< DescriptorRange > Ranges
const RootDescriptor & getRootDescriptor(size_t Index) const
const DescriptorTable & getDescriptorTable(size_t Index) const
void addParameter(dxbc::RootParameterType Type, dxbc::ShaderVisibility Visibility, RootConstants Constant)
SmallVector< StaticSampler > StaticSamplers
mcdxbc::RootParametersContainer ParametersContainer