1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
//===- HLSLRootSignatureUtils.cpp - HLSL Root Signature helpers -----------===//
//
// 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/HLSLRootSignatureUtils.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/bit.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/ScopedPrinter.h"
namespace llvm {
namespace hlsl {
namespace rootsig {
template <typename T>
static std::optional<StringRef> getEnumName(const T Value,
ArrayRef<EnumEntry<T>> Enums) {
for (const auto &EnumItem : Enums)
if (EnumItem.Value == Value)
return EnumItem.Name;
return std::nullopt;
}
template <typename T>
static raw_ostream &printEnum(raw_ostream &OS, const T Value,
ArrayRef<EnumEntry<T>> Enums) {
auto MaybeName = getEnumName(Value, Enums);
if (MaybeName)
OS << *MaybeName;
return OS;
}
template <typename T>
static raw_ostream &printFlags(raw_ostream &OS, const T Value,
ArrayRef<EnumEntry<T>> Flags) {
bool FlagSet = false;
unsigned Remaining = llvm::to_underlying(Value);
while (Remaining) {
unsigned Bit = 1u << llvm::countr_zero(Remaining);
if (Remaining & Bit) {
if (FlagSet)
OS << " | ";
auto MaybeFlag = getEnumName(T(Bit), Flags);
if (MaybeFlag)
OS << *MaybeFlag;
else
OS << "invalid: " << Bit;
FlagSet = true;
}
Remaining &= ~Bit;
}
if (!FlagSet)
OS << "None";
return OS;
}
static const EnumEntry<RegisterType> RegisterNames[] = {
{"b", RegisterType::BReg},
{"t", RegisterType::TReg},
{"u", RegisterType::UReg},
{"s", RegisterType::SReg},
};
static raw_ostream &operator<<(raw_ostream &OS, const Register &Reg) {
printEnum(OS, Reg.ViewType, ArrayRef(RegisterNames));
OS << Reg.Number;
return OS;
}
static const EnumEntry<ShaderVisibility> VisibilityNames[] = {
{"All", ShaderVisibility::All},
{"Vertex", ShaderVisibility::Vertex},
{"Hull", ShaderVisibility::Hull},
{"Domain", ShaderVisibility::Domain},
{"Geometry", ShaderVisibility::Geometry},
{"Pixel", ShaderVisibility::Pixel},
{"Amplification", ShaderVisibility::Amplification},
{"Mesh", ShaderVisibility::Mesh},
};
static raw_ostream &operator<<(raw_ostream &OS,
const ShaderVisibility &Visibility) {
printEnum(OS, Visibility, ArrayRef(VisibilityNames));
return OS;
}
static const EnumEntry<SamplerFilter> SamplerFilterNames[] = {
{"MinMagMipPoint", SamplerFilter::MinMagMipPoint},
{"MinMagPointMipLinear", SamplerFilter::MinMagPointMipLinear},
{"MinPointMagLinearMipPoint", SamplerFilter::MinPointMagLinearMipPoint},
{"MinPointMagMipLinear", SamplerFilter::MinPointMagMipLinear},
{"MinLinearMagMipPoint", SamplerFilter::MinLinearMagMipPoint},
{"MinLinearMagPointMipLinear", SamplerFilter::MinLinearMagPointMipLinear},
{"MinMagLinearMipPoint", SamplerFilter::MinMagLinearMipPoint},
{"MinMagMipLinear", SamplerFilter::MinMagMipLinear},
{"Anisotropic", SamplerFilter::Anisotropic},
{"ComparisonMinMagMipPoint", SamplerFilter::ComparisonMinMagMipPoint},
{"ComparisonMinMagPointMipLinear",
SamplerFilter::ComparisonMinMagPointMipLinear},
{"ComparisonMinPointMagLinearMipPoint",
SamplerFilter::ComparisonMinPointMagLinearMipPoint},
{"ComparisonMinPointMagMipLinear",
SamplerFilter::ComparisonMinPointMagMipLinear},
{"ComparisonMinLinearMagMipPoint",
SamplerFilter::ComparisonMinLinearMagMipPoint},
{"ComparisonMinLinearMagPointMipLinear",
SamplerFilter::ComparisonMinLinearMagPointMipLinear},
{"ComparisonMinMagLinearMipPoint",
SamplerFilter::ComparisonMinMagLinearMipPoint},
{"ComparisonMinMagMipLinear", SamplerFilter::ComparisonMinMagMipLinear},
{"ComparisonAnisotropic", SamplerFilter::ComparisonAnisotropic},
{"MinimumMinMagMipPoint", SamplerFilter::MinimumMinMagMipPoint},
{"MinimumMinMagPointMipLinear", SamplerFilter::MinimumMinMagPointMipLinear},
{"MinimumMinPointMagLinearMipPoint",
SamplerFilter::MinimumMinPointMagLinearMipPoint},
{"MinimumMinPointMagMipLinear", SamplerFilter::MinimumMinPointMagMipLinear},
{"MinimumMinLinearMagMipPoint", SamplerFilter::MinimumMinLinearMagMipPoint},
{"MinimumMinLinearMagPointMipLinear",
SamplerFilter::MinimumMinLinearMagPointMipLinear},
{"MinimumMinMagLinearMipPoint", SamplerFilter::MinimumMinMagLinearMipPoint},
{"MinimumMinMagMipLinear", SamplerFilter::MinimumMinMagMipLinear},
{"MinimumAnisotropic", SamplerFilter::MinimumAnisotropic},
{"MaximumMinMagMipPoint", SamplerFilter::MaximumMinMagMipPoint},
{"MaximumMinMagPointMipLinear", SamplerFilter::MaximumMinMagPointMipLinear},
{"MaximumMinPointMagLinearMipPoint",
SamplerFilter::MaximumMinPointMagLinearMipPoint},
{"MaximumMinPointMagMipLinear", SamplerFilter::MaximumMinPointMagMipLinear},
{"MaximumMinLinearMagMipPoint", SamplerFilter::MaximumMinLinearMagMipPoint},
{"MaximumMinLinearMagPointMipLinear",
SamplerFilter::MaximumMinLinearMagPointMipLinear},
{"MaximumMinMagLinearMipPoint", SamplerFilter::MaximumMinMagLinearMipPoint},
{"MaximumMinMagMipLinear", SamplerFilter::MaximumMinMagMipLinear},
{"MaximumAnisotropic", SamplerFilter::MaximumAnisotropic},
};
static raw_ostream &operator<<(raw_ostream &OS, const SamplerFilter &Filter) {
printEnum(OS, Filter, ArrayRef(SamplerFilterNames));
return OS;
}
static const EnumEntry<TextureAddressMode> TextureAddressModeNames[] = {
{"Wrap", TextureAddressMode::Wrap},
{"Mirror", TextureAddressMode::Mirror},
{"Clamp", TextureAddressMode::Clamp},
{"Border", TextureAddressMode::Border},
{"MirrorOnce", TextureAddressMode::MirrorOnce},
};
static raw_ostream &operator<<(raw_ostream &OS,
const TextureAddressMode &Address) {
printEnum(OS, Address, ArrayRef(TextureAddressModeNames));
return OS;
}
static const EnumEntry<ComparisonFunc> ComparisonFuncNames[] = {
{"Never", ComparisonFunc::Never},
{"Less", ComparisonFunc::Less},
{"Equal", ComparisonFunc::Equal},
{"LessEqual", ComparisonFunc::LessEqual},
{"Greater", ComparisonFunc::Greater},
{"NotEqual", ComparisonFunc::NotEqual},
{"GreaterEqual", ComparisonFunc::GreaterEqual},
{"Always", ComparisonFunc::Always},
};
static raw_ostream &operator<<(raw_ostream &OS,
const ComparisonFunc &CompFunc) {
printEnum(OS, CompFunc, ArrayRef(ComparisonFuncNames));
return OS;
}
static const EnumEntry<StaticBorderColor> StaticBorderColorNames[] = {
{"TransparentBlack", StaticBorderColor::TransparentBlack},
{"OpaqueBlack", StaticBorderColor::OpaqueBlack},
{"OpaqueWhite", StaticBorderColor::OpaqueWhite},
{"OpaqueBlackUint", StaticBorderColor::OpaqueBlackUint},
{"OpaqueWhiteUint", StaticBorderColor::OpaqueWhiteUint},
};
static raw_ostream &operator<<(raw_ostream &OS,
const StaticBorderColor &BorderColor) {
printEnum(OS, BorderColor, ArrayRef(StaticBorderColorNames));
return OS;
}
static const EnumEntry<dxil::ResourceClass> ResourceClassNames[] = {
{"CBV", dxil::ResourceClass::CBuffer},
{"SRV", dxil::ResourceClass::SRV},
{"UAV", dxil::ResourceClass::UAV},
{"Sampler", dxil::ResourceClass::Sampler},
};
static raw_ostream &operator<<(raw_ostream &OS, const ClauseType &Type) {
printEnum(OS, dxil::ResourceClass(llvm::to_underlying(Type)),
ArrayRef(ResourceClassNames));
return OS;
}
static const EnumEntry<RootDescriptorFlags> RootDescriptorFlagNames[] = {
{"DataVolatile", RootDescriptorFlags::DataVolatile},
{"DataStaticWhileSetAtExecute",
RootDescriptorFlags::DataStaticWhileSetAtExecute},
{"DataStatic", RootDescriptorFlags::DataStatic},
};
static raw_ostream &operator<<(raw_ostream &OS,
const RootDescriptorFlags &Flags) {
printFlags(OS, Flags, ArrayRef(RootDescriptorFlagNames));
return OS;
}
static const EnumEntry<DescriptorRangeFlags> DescriptorRangeFlagNames[] = {
{"DescriptorsVolatile", DescriptorRangeFlags::DescriptorsVolatile},
{"DataVolatile", DescriptorRangeFlags::DataVolatile},
{"DataStaticWhileSetAtExecute",
DescriptorRangeFlags::DataStaticWhileSetAtExecute},
{"DataStatic", DescriptorRangeFlags::DataStatic},
{"DescriptorsStaticKeepingBufferBoundsChecks",
DescriptorRangeFlags::DescriptorsStaticKeepingBufferBoundsChecks},
};
static raw_ostream &operator<<(raw_ostream &OS,
const DescriptorRangeFlags &Flags) {
printFlags(OS, Flags, ArrayRef(DescriptorRangeFlagNames));
return OS;
}
static const EnumEntry<RootFlags> RootFlagNames[] = {
{"AllowInputAssemblerInputLayout",
RootFlags::AllowInputAssemblerInputLayout},
{"DenyVertexShaderRootAccess", RootFlags::DenyVertexShaderRootAccess},
{"DenyHullShaderRootAccess", RootFlags::DenyHullShaderRootAccess},
{"DenyDomainShaderRootAccess", RootFlags::DenyDomainShaderRootAccess},
{"DenyGeometryShaderRootAccess", RootFlags::DenyGeometryShaderRootAccess},
{"DenyPixelShaderRootAccess", RootFlags::DenyPixelShaderRootAccess},
{"AllowStreamOutput", RootFlags::AllowStreamOutput},
{"LocalRootSignature", RootFlags::LocalRootSignature},
{"DenyAmplificationShaderRootAccess",
RootFlags::DenyAmplificationShaderRootAccess},
{"DenyMeshShaderRootAccess", RootFlags::DenyMeshShaderRootAccess},
{"CBVSRVUAVHeapDirectlyIndexed", RootFlags::CBVSRVUAVHeapDirectlyIndexed},
{"SamplerHeapDirectlyIndexed", RootFlags::SamplerHeapDirectlyIndexed},
};
raw_ostream &operator<<(raw_ostream &OS, const RootFlags &Flags) {
OS << "RootFlags(";
printFlags(OS, Flags, ArrayRef(RootFlagNames));
OS << ")";
return OS;
}
raw_ostream &operator<<(raw_ostream &OS, const RootConstants &Constants) {
OS << "RootConstants(num32BitConstants = " << Constants.Num32BitConstants
<< ", " << Constants.Reg << ", space = " << Constants.Space
<< ", visibility = " << Constants.Visibility << ")";
return OS;
}
raw_ostream &operator<<(raw_ostream &OS, const DescriptorTable &Table) {
OS << "DescriptorTable(numClauses = " << Table.NumClauses
<< ", visibility = " << Table.Visibility << ")";
return OS;
}
raw_ostream &operator<<(raw_ostream &OS, const DescriptorTableClause &Clause) {
OS << Clause.Type << "(" << Clause.Reg << ", numDescriptors = ";
if (Clause.NumDescriptors == NumDescriptorsUnbounded)
OS << "unbounded";
else
OS << Clause.NumDescriptors;
OS << ", space = " << Clause.Space << ", offset = ";
if (Clause.Offset == DescriptorTableOffsetAppend)
OS << "DescriptorTableOffsetAppend";
else
OS << Clause.Offset;
OS << ", flags = " << Clause.Flags << ")";
return OS;
}
raw_ostream &operator<<(raw_ostream &OS, const RootDescriptor &Descriptor) {
ClauseType Type = ClauseType(llvm::to_underlying(Descriptor.Type));
OS << "Root" << Type << "(" << Descriptor.Reg
<< ", space = " << Descriptor.Space
<< ", visibility = " << Descriptor.Visibility
<< ", flags = " << Descriptor.Flags << ")";
return OS;
}
raw_ostream &operator<<(raw_ostream &OS, const StaticSampler &Sampler) {
OS << "StaticSampler(" << Sampler.Reg << ", filter = " << Sampler.Filter
<< ", addressU = " << Sampler.AddressU
<< ", addressV = " << Sampler.AddressV
<< ", addressW = " << Sampler.AddressW
<< ", mipLODBias = " << Sampler.MipLODBias
<< ", maxAnisotropy = " << Sampler.MaxAnisotropy
<< ", comparisonFunc = " << Sampler.CompFunc
<< ", borderColor = " << Sampler.BorderColor
<< ", minLOD = " << Sampler.MinLOD << ", maxLOD = " << Sampler.MaxLOD
<< ", space = " << Sampler.Space << ", visibility = " << Sampler.Visibility
<< ")";
return OS;
}
namespace {
// We use the OverloadVisit with std::visit to ensure the compiler catches if a
// new RootElement variant type is added but it's operator<< or metadata
// generation isn't handled.
template <class... Ts> struct OverloadedVisit : Ts... {
using Ts::operator()...;
};
template <class... Ts> OverloadedVisit(Ts...) -> OverloadedVisit<Ts...>;
} // namespace
raw_ostream &operator<<(raw_ostream &OS, const RootElement &Element) {
const auto Visitor = OverloadedVisit{
[&OS](const RootFlags &Flags) { OS << Flags; },
[&OS](const RootConstants &Constants) { OS << Constants; },
[&OS](const RootDescriptor &Descriptor) { OS << Descriptor; },
[&OS](const DescriptorTableClause &Clause) { OS << Clause; },
[&OS](const DescriptorTable &Table) { OS << Table; },
[&OS](const StaticSampler &Sampler) { OS << Sampler; },
};
std::visit(Visitor, Element);
return OS;
}
void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements) {
OS << " RootElements{";
bool First = true;
for (const RootElement &Element : Elements) {
if (!First)
OS << ",";
OS << " " << Element;
First = false;
}
OS << "}";
}
MDNode *MetadataBuilder::BuildRootSignature() {
const auto Visitor = OverloadedVisit{
[this](const RootFlags &Flags) -> MDNode * {
return BuildRootFlags(Flags);
},
[this](const RootConstants &Constants) -> MDNode * {
return BuildRootConstants(Constants);
},
[this](const RootDescriptor &Descriptor) -> MDNode * {
return BuildRootDescriptor(Descriptor);
},
[this](const DescriptorTableClause &Clause) -> MDNode * {
return BuildDescriptorTableClause(Clause);
},
[this](const DescriptorTable &Table) -> MDNode * {
return BuildDescriptorTable(Table);
},
[this](const StaticSampler &Sampler) -> MDNode * {
return BuildStaticSampler(Sampler);
},
};
for (const RootElement &Element : Elements) {
MDNode *ElementMD = std::visit(Visitor, Element);
assert(ElementMD != nullptr &&
"Root Element must be initialized and validated");
GeneratedMetadata.push_back(ElementMD);
}
return MDNode::get(Ctx, GeneratedMetadata);
}
MDNode *MetadataBuilder::BuildRootFlags(const RootFlags &Flags) {
IRBuilder<> Builder(Ctx);
Metadata *Operands[] = {
MDString::get(Ctx, "RootFlags"),
ConstantAsMetadata::get(Builder.getInt32(llvm::to_underlying(Flags))),
};
return MDNode::get(Ctx, Operands);
}
MDNode *MetadataBuilder::BuildRootConstants(const RootConstants &Constants) {
IRBuilder<> Builder(Ctx);
Metadata *Operands[] = {
MDString::get(Ctx, "RootConstants"),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Constants.Visibility))),
ConstantAsMetadata::get(Builder.getInt32(Constants.Reg.Number)),
ConstantAsMetadata::get(Builder.getInt32(Constants.Space)),
ConstantAsMetadata::get(Builder.getInt32(Constants.Num32BitConstants)),
};
return MDNode::get(Ctx, Operands);
}
MDNode *MetadataBuilder::BuildRootDescriptor(const RootDescriptor &Descriptor) {
IRBuilder<> Builder(Ctx);
std::optional<StringRef> TypeName =
getEnumName(dxil::ResourceClass(llvm::to_underlying(Descriptor.Type)),
ArrayRef(ResourceClassNames));
assert(TypeName && "Provided an invalid Resource Class");
llvm::SmallString<7> Name({"Root", *TypeName});
Metadata *Operands[] = {
MDString::get(Ctx, Name),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Descriptor.Visibility))),
ConstantAsMetadata::get(Builder.getInt32(Descriptor.Reg.Number)),
ConstantAsMetadata::get(Builder.getInt32(Descriptor.Space)),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Descriptor.Flags))),
};
return MDNode::get(Ctx, Operands);
}
MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) {
IRBuilder<> Builder(Ctx);
SmallVector<Metadata *> TableOperands;
// Set the mandatory arguments
TableOperands.push_back(MDString::get(Ctx, "DescriptorTable"));
TableOperands.push_back(ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Table.Visibility))));
// Remaining operands are references to the table's clauses. The in-memory
// representation of the Root Elements created from parsing will ensure that
// the previous N elements are the clauses for this table.
assert(Table.NumClauses <= GeneratedMetadata.size() &&
"Table expected all owned clauses to be generated already");
// So, add a refence to each clause to our operands
TableOperands.append(GeneratedMetadata.end() - Table.NumClauses,
GeneratedMetadata.end());
// Then, remove those clauses from the general list of Root Elements
GeneratedMetadata.pop_back_n(Table.NumClauses);
return MDNode::get(Ctx, TableOperands);
}
MDNode *MetadataBuilder::BuildDescriptorTableClause(
const DescriptorTableClause &Clause) {
IRBuilder<> Builder(Ctx);
std::optional<StringRef> Name =
getEnumName(dxil::ResourceClass(llvm::to_underlying(Clause.Type)),
ArrayRef(ResourceClassNames));
assert(Name && "Provided an invalid Resource Class");
Metadata *Operands[] = {
MDString::get(Ctx, *Name),
ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)),
ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)),
ConstantAsMetadata::get(Builder.getInt32(Clause.Space)),
ConstantAsMetadata::get(Builder.getInt32(Clause.Offset)),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Clause.Flags))),
};
return MDNode::get(Ctx, Operands);
}
MDNode *MetadataBuilder::BuildStaticSampler(const StaticSampler &Sampler) {
IRBuilder<> Builder(Ctx);
Metadata *Operands[] = {
MDString::get(Ctx, "StaticSampler"),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.Filter))),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.AddressU))),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.AddressV))),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.AddressW))),
ConstantAsMetadata::get(llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx),
Sampler.MipLODBias)),
ConstantAsMetadata::get(Builder.getInt32(Sampler.MaxAnisotropy)),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.CompFunc))),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.BorderColor))),
ConstantAsMetadata::get(
llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MinLOD)),
ConstantAsMetadata::get(
llvm::ConstantFP::get(llvm::Type::getFloatTy(Ctx), Sampler.MaxLOD)),
ConstantAsMetadata::get(Builder.getInt32(Sampler.Reg.Number)),
ConstantAsMetadata::get(Builder.getInt32(Sampler.Space)),
ConstantAsMetadata::get(
Builder.getInt32(llvm::to_underlying(Sampler.Visibility))),
};
return MDNode::get(Ctx, Operands);
}
std::optional<const RangeInfo *>
ResourceRange::getOverlapping(const RangeInfo &Info) const {
MapT::const_iterator Interval = Intervals.find(Info.LowerBound);
if (!Interval.valid() || Info.UpperBound < Interval.start())
return std::nullopt;
return Interval.value();
}
const RangeInfo *ResourceRange::lookup(uint32_t X) const {
return Intervals.lookup(X, nullptr);
}
void ResourceRange::clear() { return Intervals.clear(); }
std::optional<const RangeInfo *> ResourceRange::insert(const RangeInfo &Info) {
uint32_t LowerBound = Info.LowerBound;
uint32_t UpperBound = Info.UpperBound;
std::optional<const RangeInfo *> Res = std::nullopt;
MapT::iterator Interval = Intervals.begin();
while (true) {
if (UpperBound < LowerBound)
break;
Interval.advanceTo(LowerBound);
if (!Interval.valid()) // No interval found
break;
// Let Interval = [x;y] and [LowerBound;UpperBound] = [a;b] and note that
// a <= y implicitly from Intervals.find(LowerBound)
if (UpperBound < Interval.start())
break; // found interval does not overlap with inserted one
if (!Res.has_value()) // Update to be the first found intersection
Res = Interval.value();
if (Interval.start() <= LowerBound && UpperBound <= Interval.stop()) {
// x <= a <= b <= y implies that [a;b] is covered by [x;y]
// -> so we don't need to insert this, report an overlap
return Res;
} else if (LowerBound <= Interval.start() &&
Interval.stop() <= UpperBound) {
// a <= x <= y <= b implies that [x;y] is covered by [a;b]
// -> so remove the existing interval that we will cover with the
// overwrite
Interval.erase();
} else if (LowerBound < Interval.start() && UpperBound <= Interval.stop()) {
// a < x <= b <= y implies that [a; x] is not covered but [x;b] is
// -> so set b = x - 1 such that [a;x-1] is now the interval to insert
UpperBound = Interval.start() - 1;
} else if (Interval.start() <= LowerBound && Interval.stop() < UpperBound) {
// a < x <= b <= y implies that [y; b] is not covered but [a;y] is
// -> so set a = y + 1 such that [y+1;b] is now the interval to insert
LowerBound = Interval.stop() + 1;
}
}
assert(LowerBound <= UpperBound && "Attempting to insert an empty interval");
Intervals.insert(LowerBound, UpperBound, &Info);
return Res;
}
} // namespace rootsig
} // namespace hlsl
} // namespace llvm
|